Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4GDMLEvaluator Class Reference

#include <G4GDMLEvaluator.hh>

Public Member Functions

 G4GDMLEvaluator ()
 
void Clear ()
 
void DefineConstant (const G4String &, G4double)
 
void DefineVariable (const G4String &, G4double)
 
void DefineMatrix (const G4String &, G4int, std::vector< G4double >)
 
void SetVariable (const G4String &, G4double)
 
G4bool IsVariable (const G4String &) const
 
G4String SolveBrackets (const G4String &)
 
G4double Evaluate (const G4String &)
 
G4int EvaluateInteger (const G4String &)
 
G4double GetConstant (const G4String &)
 
G4double GetVariable (const G4String &)
 

Detailed Description

Definition at line 47 of file G4GDMLEvaluator.hh.

Constructor & Destructor Documentation

◆ G4GDMLEvaluator()

G4GDMLEvaluator::G4GDMLEvaluator ( )

Definition at line 41 of file G4GDMLEvaluator.cc.

42{
43 eval.clear();
44 eval.setStdMath();
45 eval.setSystemOfUnits(meter,kilogram,second,ampere,kelvin,mole,candela);
46}
void setSystemOfUnits(double meter=1.0, double kilogram=1.0, double second=1.0, double ampere=1.0, double kelvin=1.0, double mole=1.0, double candela=1.0)

Member Function Documentation

◆ Clear()

void G4GDMLEvaluator::Clear ( )

Definition at line 48 of file G4GDMLEvaluator.cc.

49{
50 eval.clear();
51 eval.setStdMath();
52 eval.setSystemOfUnits(meter,kilogram,second,ampere,kelvin,mole,candela);
53
54 variableList.clear();
55}

Referenced by G4GDMLReadStructure::Clear().

◆ DefineConstant()

void G4GDMLEvaluator::DefineConstant ( const G4String name,
G4double  value 
)

Definition at line 57 of file G4GDMLEvaluator.cc.

58{
59 if (eval.findVariable(name))
60 {
61 G4String error_msg = "Redefinition of constant or variable: "+name;
62 G4Exception("G4GDMLEvaluator::DefineConstant()", "InvalidExpression",
63 FatalException, error_msg);
64 }
65 eval.setVariable(name.c_str(),value);
66}
@ FatalException
void setVariable(const char *name, double value)
Definition: Evaluator.cc:687
bool findVariable(const char *name) const
Definition: Evaluator.cc:721
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41

Referenced by G4GDMLReadDefine::ConstantRead(), DefineMatrix(), G4GDMLReadDefine::ExpressionRead(), and G4GDMLReadDefine::QuantityRead().

◆ DefineMatrix()

void G4GDMLEvaluator::DefineMatrix ( const G4String name,
G4int  coldim,
std::vector< G4double valueList 
)

Definition at line 80 of file G4GDMLEvaluator.cc.

83{
84 const G4int size = valueList.size();
85
86 if (size == 0)
87 {
88 G4String error_msg = "Matrix '"+name+"' is empty!";
89 G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
90 FatalException, error_msg);
91 }
92 /*
93 if (size == 1)
94 {
95 G4String error_msg = "Matrix '" + name
96 + "' has only one element! "
97 + "Define a constant instead!!";
98 G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
99 FatalException, error_msg);
100 }
101 */
102
103 if (size % coldim != 0)
104 {
105 G4String error_msg = "Matrix '" + name + "' is not filled correctly!";
106 G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
107 FatalException, error_msg);
108 }
109
110 if ((size == coldim) || (coldim == 1)) // Row- or column matrix
111 {
112 for (G4int i=0;i<size;i++)
113 {
114 std::stringstream MatrixElementNameStream;
115 MatrixElementNameStream << name << "_" << i;
116 DefineConstant(MatrixElementNameStream.str(),valueList[i]);
117 }
118 }
119 else // Normal matrix
120 {
121 const G4int rowdim = size/coldim;
122
123 for (G4int i=0;i<rowdim;i++)
124 {
125 for (G4int j=0;j<coldim;j++)
126 {
127 std::stringstream MatrixElementNameStream;
128 MatrixElementNameStream << name << "_" << i << "_" << j;
129 DefineConstant(MatrixElementNameStream.str(),valueList[coldim*i+j]);
130 }
131 }
132 }
133}
int G4int
Definition: G4Types.hh:66
void DefineConstant(const G4String &, G4double)

Referenced by G4GDMLReadDefine::MatrixRead().

◆ DefineVariable()

void G4GDMLEvaluator::DefineVariable ( const G4String name,
G4double  value 
)

Definition at line 68 of file G4GDMLEvaluator.cc.

69{
70 if (eval.findVariable(name))
71 {
72 G4String error_msg = "Redefinition of constant or variable: "+name;
73 G4Exception("G4GDMLEvaluator::DefineVariable()", "InvalidExpression",
74 FatalException, error_msg);
75 }
76 eval.setVariable(name.c_str(),value);
77 variableList.push_back(name);
78}

Referenced by G4GDMLReadDefine::VariableRead().

◆ Evaluate()

G4double G4GDMLEvaluator::Evaluate ( const G4String in)

Definition at line 213 of file G4GDMLEvaluator.cc.

214{
215 G4String expression = SolveBrackets(in);
216
217 G4double value = 0.0;
218
219 if (!expression.empty())
220 {
221 value = eval.evaluate(expression.c_str());
222
223 if (eval.status() != G4Evaluator::OK)
224 {
225 eval.print_error();
226 G4String error_msg = "Error in expression: " + expression;
227 G4Exception("G4GDMLEvaluator::Evaluate()", "InvalidExpression",
228 FatalException, error_msg);
229 }
230 }
231 return value;
232}
double G4double
Definition: G4Types.hh:64
G4String SolveBrackets(const G4String &)
void print_error() const
Definition: Evaluator.cc:641
double evaluate(const char *expression)
Definition: Evaluator.cc:611
int status() const
Definition: Evaluator.cc:631

Referenced by G4GDMLReadMaterials::AtomRead(), G4GDMLReadStructure::AxisRead(), G4GDMLReadParamvol::Box_dimensionsRead(), G4GDMLReadSolids::BoxRead(), G4GDMLReadParamvol::Cone_dimensionsRead(), G4GDMLReadSolids::ConeRead(), G4GDMLReadDefine::ConstantRead(), G4GDMLReadSolids::CutTubeRead(), G4GDMLReadStructure::DivisionvolRead(), G4GDMLReadMaterials::DRead(), G4GDMLReadSolids::ElconeRead(), G4GDMLReadMaterials::ElementRead(), G4GDMLReadSolids::EllipsoidRead(), G4GDMLReadSolids::EltubeRead(), EvaluateInteger(), G4GDMLReadDefine::ExpressionRead(), G4GDMLReadMaterials::FractionRead(), G4GDMLReadSolids::GenTrapRead(), GetConstant(), GetVariable(), G4GDMLReadParamvol::Hype_dimensionsRead(), G4GDMLReadSolids::HypeRead(), G4GDMLReadMaterials::MaterialRead(), G4GDMLReadDefine::MatrixRead(), G4GDMLReadMaterials::MEERead(), G4GDMLReadSolids::OpticalSurfaceRead(), G4GDMLReadParamvol::Orb_dimensionsRead(), G4GDMLReadSolids::OrbRead(), G4GDMLReadParamvol::Para_dimensionsRead(), G4GDMLReadSolids::ParaboloidRead(), G4GDMLReadParamvol::ParameterisedRead(), G4GDMLReadSolids::ParaRead(), G4GDMLReadSolids::PolyconeRead(), G4GDMLReadSolids::PolyhedraRead(), G4GDMLReadDefine::PositionRead(), G4GDMLReadMaterials::PRead(), G4GDMLReadSolids::QuadrangularRead(), G4GDMLReadStructure::QuantityRead(), G4GDMLReadDefine::QuantityRead(), G4GDMLReadSolids::ReflectedSolidRead(), G4GDMLReadDefine::RotationRead(), G4GDMLReadDefine::ScaleRead(), G4GDMLReadSolids::SectionRead(), G4GDMLReadParamvol::Sphere_dimensionsRead(), G4GDMLReadSolids::SphereRead(), G4GDMLReadSolids::TetRead(), G4GDMLReadParamvol::Torus_dimensionsRead(), G4GDMLReadSolids::TorusRead(), G4GDMLReadParamvol::Trap_dimensionsRead(), G4GDMLReadSolids::TrapRead(), G4GDMLReadParamvol::Trd_dimensionsRead(), G4GDMLReadSolids::TrdRead(), G4GDMLReadMaterials::TRead(), G4GDMLReadSolids::TriangularRead(), G4GDMLReadParamvol::Tube_dimensionsRead(), G4GDMLReadSolids::TubeRead(), G4GDMLReadSolids::TwistedboxRead(), G4GDMLReadSolids::TwistedtrapRead(), G4GDMLReadSolids::TwistedtrdRead(), G4GDMLReadSolids::TwistedtubsRead(), G4GDMLReadSolids::TwoDimVertexRead(), G4GDMLReadDefine::VariableRead(), G4GDMLReadDefine::VectorRead(), G4GDMLReadSolids::XtruRead(), and G4GDMLReadSolids::ZplaneRead().

◆ EvaluateInteger()

G4int G4GDMLEvaluator::EvaluateInteger ( const G4String expression)

Definition at line 234 of file G4GDMLEvaluator.cc.

235{
236 // This function is for evaluating integer expressions,
237 // like loop variables and matrix indices.
238 // Complains if the evaluated expression has a fractional
239 // part different from zero
240
241 G4double value = Evaluate(expression);
242
243 G4int whole = (G4int)value;
244 G4double frac = value - (G4double)whole;
245
246 if (frac != 0.0)
247 {
248 G4String error_msg = "Expression '" + expression
249 + "' is expected to have an integer value!";
250 G4Exception("G4GDMLEvaluator::EvaluateInteger()", "InvalidExpression",
251 FatalException, error_msg);
252 }
253 return whole;
254}
G4double Evaluate(const G4String &)

Referenced by G4GDMLReadMaterials::CompositeRead(), G4GDMLReadStructure::DivisionvolRead(), G4GDMLReadMaterials::IsotopeRead(), G4GDMLRead::LoopRead(), G4GDMLReadDefine::MatrixRead(), G4GDMLReadSolids::PolyhedraRead(), SolveBrackets(), and G4GDMLReadStructure::Volume_contentRead().

◆ GetConstant()

G4double G4GDMLEvaluator::GetConstant ( const G4String name)

Definition at line 256 of file G4GDMLEvaluator.cc.

257{
258 if (IsVariable(name))
259 {
260 G4String error_msg = "Constant '" + name
261 + "' is not defined! It is a variable!";
262 G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
263 FatalException, error_msg);
264 }
265 if (!eval.findVariable(name))
266 {
267 G4String error_msg = "Constant '" + name + "' is not defined!";
268 G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
269 FatalException, error_msg);
270 }
271 return Evaluate(name);
272}
G4bool IsVariable(const G4String &) const

Referenced by G4GDMLReadDefine::GetConstant().

◆ GetVariable()

G4double G4GDMLEvaluator::GetVariable ( const G4String name)

Definition at line 274 of file G4GDMLEvaluator.cc.

275{
276 if (!IsVariable(name))
277 {
278 G4String error_msg = "Variable '" + name + "' is not a defined!";
279 G4Exception("G4GDMLEvaluator::GetVariable()", "InvalidSetup",
280 FatalException, error_msg);
281 }
282 return Evaluate(name);
283}

Referenced by G4GDMLReadDefine::GetVariable().

◆ IsVariable()

G4bool G4GDMLEvaluator::IsVariable ( const G4String name) const

Definition at line 146 of file G4GDMLEvaluator.cc.

147{
148 const size_t variableCount = variableList.size();
149
150 for (size_t i=0;i<variableCount;i++)
151 {
152 if (variableList[i] == name) { return true; }
153 }
154
155 return false;
156}

Referenced by GetConstant(), GetVariable(), G4GDMLReadDefine::IsValidID(), G4GDMLRead::LoopRead(), and SetVariable().

◆ SetVariable()

void G4GDMLEvaluator::SetVariable ( const G4String name,
G4double  value 
)

Definition at line 135 of file G4GDMLEvaluator.cc.

136{
137 if (!IsVariable(name))
138 {
139 G4String error_msg = "Variable '" + name + "' is not defined!";
140 G4Exception("G4GDMLEvaluator::SetVariable()", "InvalidSetup",
141 FatalException, error_msg);
142 }
143 eval.setVariable(name.c_str(),value);
144}

Referenced by G4GDMLRead::LoopRead().

◆ SolveBrackets()

G4String G4GDMLEvaluator::SolveBrackets ( const G4String in)

Definition at line 158 of file G4GDMLEvaluator.cc.

159{
160 std::string::size_type full = in.size();
161 std::string::size_type open = in.find("[",0);
162 std::string::size_type close = in.find("]",0);
163
164 if (open==close) { return in; }
165
166 if ((open>close) || (open==std::string::npos) || (close==std::string::npos))
167 {
168 G4String error_msg = "Bracket mismatch: " + in;
169 G4Exception("G4GDMLEvaluator::SolveBrackets()", "InvalidExpression",
170 FatalException, error_msg);
171 return in;
172 }
173
174 std::string::size_type begin = open;
175 std::string::size_type end = 0;
176 std::string::size_type end1 = 0;
177 std::string out;
178 out.append(in,0,open);
179
180 do // Loop for all possible matrix elements in 'in'
181 {
182 do // SolveBrackets for one matrix element
183 {
184 end = in.find(",",begin+1);
185 end1= in.find("]",begin+1);
186 if (end>end1) { end = end1; }
187 if (end==std::string::npos) { end = close;}
188
189 std::stringstream indexStream;
190 indexStream << "_" << EvaluateInteger(in.substr(begin+1,end-begin-1))-1;
191
192 out.append(indexStream.str());
193
194 begin = end;
195
196 } while (end<close);
197
198 if (full==close) { return out; }
199
200 open = in.find("[",begin);
201 close = in.find("]",begin+1);
202
203 if (open==close) { out.append(in.substr(end+1,full-end-1)); return out; }
204 out.append(in.substr(end+1,open-end-1));
205
206 begin=open;
207
208 } while (close<full);
209
210 return out;
211}
G4int EvaluateInteger(const G4String &)
G4String & append(const G4String &)

Referenced by Evaluate(), G4GDMLRead::GenerateName(), and G4GDMLRead::GeneratePhysvolName().


The documentation for this class was generated from the following files: