Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4GDMLEvaluator.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4GDMLEvaluator.cc 89243 2015-03-27 16:24:39Z gcosmo $
28 // GEANT4 tag $ Name:$
29 //
30 // class G4GDMLEvaluator Implementation
31 //
32 // Original author: Zoltan Torzsok, November 2007
33 //
34 // --------------------------------------------------------------------
35 
36 #include <sstream>
37 
38 #include "G4GDMLEvaluator.hh"
39 #include "G4SystemOfUnits.hh"
40 
42 {
43  eval.clear();
44  eval.setStdMath();
46 }
47 
49 {
50  eval.clear();
51  eval.setStdMath();
53 
54  variableList.clear();
55 }
56 
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 }
67 
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 }
79 
81  G4int coldim,
82  std::vector<G4double> valueList)
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 }
134 
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 }
145 
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 }
157 
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 }
212 
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 }
233 
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 }
255 
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 }
273 
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 }
284 
286 {
287  std::ostringstream os;
288  os << ival;
289  G4String vl = os.str();
290  return vl;
291 }
292 
294 {
295  std::ostringstream os;
296  os << dval;
297  G4String vl = os.str();
298  return vl;
299 }
static constexpr double candela
Definition: G4SIunits.hh:317
const XML_Char * name
Definition: expat.h:151
G4int EvaluateInteger(const G4String &)
G4double GetConstant(const G4String &)
void DefineConstant(const G4String &, G4double)
int status() const
Definition: Evaluator.cc:634
static constexpr double ampere
Definition: G4SIunits.hh:189
static constexpr double second
Definition: G4SIunits.hh:157
int G4int
Definition: G4Types.hh:78
static constexpr double meter
Definition: G4SIunits.hh:82
const XML_Char int const XML_Char * value
Definition: expat.h:331
G4String ConvertToString(G4int ival)
bool G4bool
Definition: G4Types.hh:79
void print_error() const
Definition: Evaluator.cc:644
void DefineMatrix(const G4String &, G4int, std::vector< G4double >)
double evaluate(const char *expression)
Definition: Evaluator.cc:614
G4bool IsVariable(const G4String &) const
static constexpr double kelvin
Definition: G4SIunits.hh:281
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4String SolveBrackets(const G4String &)
void setVariable(const char *name, double value)
Definition: Evaluator.cc:690
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)
void SetVariable(const G4String &, G4double)
static constexpr double kilogram
Definition: G4SIunits.hh:177
G4String & append(const G4String &)
bool findVariable(const char *name) const
Definition: Evaluator.cc:724
G4double GetVariable(const G4String &)
double G4double
Definition: G4Types.hh:76
void DefineVariable(const G4String &, G4double)
static constexpr double mole
Definition: G4SIunits.hh:286
G4double Evaluate(const G4String &)