Geant4 10.7.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 &)
 
G4String ConvertToString (G4int ival)
 
G4String ConvertToString (G4double dval)
 

Detailed Description

Definition at line 41 of file G4GDMLEvaluator.hh.

Constructor & Destructor Documentation

◆ G4GDMLEvaluator()

G4GDMLEvaluator::G4GDMLEvaluator ( )

Definition at line 37 of file G4GDMLEvaluator.cc.

38{
39 eval.clear();
40 eval.setStdMath();
41 eval.setSystemOfUnits(meter, kilogram, second, ampere, kelvin, mole, candela);
42}
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 45 of file G4GDMLEvaluator.cc.

46{
47 eval.clear();
48 eval.setStdMath();
49 eval.setSystemOfUnits(meter, kilogram, second, ampere, kelvin, mole, candela);
50
51 variableList.clear();
52}

Referenced by G4GDMLReadStructure::Clear().

◆ ConvertToString() [1/2]

G4String G4GDMLEvaluator::ConvertToString ( G4double  dval)

Definition at line 323 of file G4GDMLEvaluator.cc.

324{
325 std::ostringstream os;
326 os << dval;
327 G4String vl = os.str();
328 return vl;
329}

◆ ConvertToString() [2/2]

G4String G4GDMLEvaluator::ConvertToString ( G4int  ival)

Definition at line 314 of file G4GDMLEvaluator.cc.

315{
316 std::ostringstream os;
317 os << ival;
318 G4String vl = os.str();
319 return vl;
320}

Referenced by G4GDMLWriteStructure::ExportEnergyCuts().

◆ DefineConstant()

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

Definition at line 55 of file G4GDMLEvaluator.cc.

56{
57 if(eval.findVariable(name))
58 {
59 G4String error_msg = "Redefinition of constant or variable: " + name;
60 G4Exception("G4GDMLEvaluator::DefineConstant()", "InvalidExpression",
61 FatalException, error_msg);
62 }
63 eval.setVariable(name.c_str(), value);
64}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
void setVariable(const char *name, double value)
Definition: Evaluator.cc:689
bool findVariable(const char *name) const
Definition: Evaluator.cc:723
const char * name(G4int ptype)

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.

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

Referenced by G4GDMLReadDefine::MatrixRead().

◆ DefineVariable()

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

Definition at line 67 of file G4GDMLEvaluator.cc.

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

Referenced by G4GDMLReadDefine::VariableRead().

◆ Evaluate()

G4double G4GDMLEvaluator::Evaluate ( const G4String in)

Definition at line 238 of file G4GDMLEvaluator.cc.

239{
240 G4String expression = SolveBrackets(in);
241
242 G4double value = 0.0;
243
244 if(!expression.empty())
245 {
246 value = eval.evaluate(expression.c_str());
247
248 if(eval.status() != G4Evaluator::OK)
249 {
250 eval.print_error();
251 G4String error_msg = "Error in expression: " + expression;
252 G4Exception("G4GDMLEvaluator::Evaluate()", "InvalidExpression",
253 FatalException, error_msg);
254 }
255 }
256 return value;
257}
double G4double
Definition: G4Types.hh:83
G4String SolveBrackets(const G4String &)
void print_error() const
Definition: Evaluator.cc:643
double evaluate(const char *expression)
Definition: Evaluator.cc:613
int status() const
Definition: Evaluator.cc:633

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(), G4GDMLReadParamvol::Ellipsoid_dimensionsRead(), G4GDMLReadSolids::EllipsoidRead(), G4GDMLReadSolids::EltubeRead(), EvaluateInteger(), G4GDMLReadDefine::ExpressionRead(), G4GDMLReadMaterials::FractionRead(), G4GDMLReadSolids::GenericPolyconeRead(), G4GDMLReadSolids::GenericPolyhedraRead(), 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(), G4GDMLReadParamvol::Polycone_dimensionsRead(), G4GDMLReadSolids::PolyconeRead(), G4GDMLReadParamvol::Polyhedra_dimensionsRead(), G4GDMLReadSolids::PolyhedraRead(), G4GDMLReadDefine::PositionRead(), G4GDMLReadMaterials::PRead(), G4GDMLReadStructure::QuantityRead(), G4GDMLReadDefine::QuantityRead(), G4GDMLReadSolids::ReflectedSolidRead(), G4GDMLReadDefine::RotationRead(), G4GDMLReadSolids::RZPointRead(), G4GDMLReadDefine::ScaleRead(), G4GDMLReadSolids::SectionRead(), G4GDMLReadParamvol::Sphere_dimensionsRead(), G4GDMLReadSolids::SphereRead(), G4GDMLReadParamvol::Torus_dimensionsRead(), G4GDMLReadSolids::TorusRead(), G4GDMLReadParamvol::Trap_dimensionsRead(), G4GDMLReadSolids::TrapRead(), G4GDMLReadParamvol::Trd_dimensionsRead(), G4GDMLReadSolids::TrdRead(), G4GDMLReadMaterials::TRead(), G4GDMLReadParamvol::Tube_dimensionsRead(), G4GDMLReadSolids::TubeRead(), G4GDMLReadSolids::TwistedboxRead(), G4GDMLReadSolids::TwistedtrapRead(), G4GDMLReadSolids::TwistedtrdRead(), G4GDMLReadSolids::TwistedtubsRead(), G4GDMLReadSolids::TwoDimVertexRead(), G4GDMLReadDefine::VariableRead(), G4GDMLReadDefine::VectorRead(), and G4GDMLReadSolids::ZplaneRead().

◆ EvaluateInteger()

G4int G4GDMLEvaluator::EvaluateInteger ( const G4String expression)

Definition at line 260 of file G4GDMLEvaluator.cc.

261{
262 // This function is for evaluating integer expressions,
263 // like loop variables and matrix indices.
264 // Complains if the evaluated expression has a fractional
265 // part different from zero
266
267 G4double value = Evaluate(expression);
268
269 G4int whole = (G4int) value;
270 G4double frac = value - (G4double) whole;
271
272 if(frac != 0.0)
273 {
274 G4String error_msg =
275 "Expression '" + expression + "' is expected to have an integer value!";
276 G4Exception("G4GDMLEvaluator::EvaluateInteger()", "InvalidExpression",
277 FatalException, error_msg);
278 }
279 return whole;
280}
G4double Evaluate(const G4String &)

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

◆ GetConstant()

G4double G4GDMLEvaluator::GetConstant ( const G4String name)

Definition at line 283 of file G4GDMLEvaluator.cc.

284{
285 if(IsVariable(name))
286 {
287 G4String error_msg =
288 "Constant '" + name + "' is not defined! It is a variable!";
289 G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
290 FatalException, error_msg);
291 }
292 if(!eval.findVariable(name))
293 {
294 G4String error_msg = "Constant '" + name + "' is not defined!";
295 G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
296 FatalException, error_msg);
297 }
298 return Evaluate(name);
299}
G4bool IsVariable(const G4String &) const

Referenced by G4GDMLReadDefine::GetConstant().

◆ GetVariable()

G4double G4GDMLEvaluator::GetVariable ( const G4String name)

Definition at line 302 of file G4GDMLEvaluator.cc.

303{
304 if(!IsVariable(name))
305 {
306 G4String error_msg = "Variable '" + name + "' is not a defined!";
307 G4Exception("G4GDMLEvaluator::GetVariable()", "InvalidSetup",
308 FatalException, error_msg);
309 }
310 return Evaluate(name);
311}

Referenced by G4GDMLReadDefine::GetVariable().

◆ IsVariable()

G4bool G4GDMLEvaluator::IsVariable ( const G4String name) const

Definition at line 148 of file G4GDMLEvaluator.cc.

149{
150 const std::size_t variableCount = variableList.size();
151
152 for(std::size_t i = 0; i < variableCount; ++i)
153 {
154 if(variableList[i] == name)
155 {
156 return true;
157 }
158 }
159
160 return false;
161}

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

◆ SetVariable()

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

Definition at line 136 of file G4GDMLEvaluator.cc.

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

Referenced by G4GDMLRead::LoopRead().

◆ SolveBrackets()

G4String G4GDMLEvaluator::SolveBrackets ( const G4String in)

Definition at line 164 of file G4GDMLEvaluator.cc.

165{
166 std::string::size_type full = in.size();
167 std::string::size_type open = in.find("[", 0);
168 std::string::size_type close = in.find("]", 0);
169
170 if(open == close)
171 {
172 return in;
173 }
174
175 if((open > close) || (open == std::string::npos) ||
176 (close == std::string::npos))
177 {
178 G4String error_msg = "Bracket mismatch: " + in;
179 G4Exception("G4GDMLEvaluator::SolveBrackets()", "InvalidExpression",
180 FatalException, error_msg);
181 return in;
182 }
183
184 std::string::size_type begin = open;
185 std::string::size_type end = 0;
186 std::string::size_type end1 = 0;
187 std::string out;
188 out.append(in, 0, open);
189
190 do // Loop for all possible matrix elements in 'in'
191 {
192 do // SolveBrackets for one matrix element
193 {
194 end = in.find(",", begin + 1);
195 end1 = in.find("]", begin + 1);
196 if(end > end1)
197 {
198 end = end1;
199 }
200 if(end == std::string::npos)
201 {
202 end = close;
203 }
204
205 std::stringstream indexStream;
206 indexStream << "_"
207 << EvaluateInteger(in.substr(begin + 1, end - begin - 1)) - 1;
208
209 out.append(indexStream.str());
210
211 begin = end;
212
213 } while(end < close);
214
215 if(full == close)
216 {
217 return out;
218 }
219
220 open = in.find("[", begin);
221 close = in.find("]", begin + 1);
222
223 if(open == close)
224 {
225 out.append(in.substr(end + 1, full - end - 1));
226 return out;
227 }
228 out.append(in.substr(end + 1, open - end - 1));
229
230 begin = open;
231
232 } while(close < full);
233
234 return out;
235}
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: