source: trunk/source/persistency/gdml/src/G4GDMLEvaluator.cc@ 1353

Last change on this file since 1353 was 1347, checked in by garnier, 15 years ago

geant4 tag 9.4

File size: 8.7 KB
Line 
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,v 1.26 2010/11/04 11:33:49 gcosmo Exp $
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 "globals.hh"
39
40#include "G4GDMLEvaluator.hh"
41
42G4GDMLEvaluator::G4GDMLEvaluator()
43{
44 eval.clear();
45 eval.setStdMath();
46 eval.setSystemOfUnits(meter,kilogram,second,ampere,kelvin,mole,candela);
47}
48
49void G4GDMLEvaluator::Clear()
50{
51 eval.clear();
52 eval.setStdMath();
53 eval.setSystemOfUnits(meter,kilogram,second,ampere,kelvin,mole,candela);
54
55 variableList.clear();
56}
57
58void G4GDMLEvaluator::DefineConstant(const G4String& name, G4double value)
59{
60 if (eval.findVariable(name))
61 {
62 G4String error_msg = "Redefinition of constant or variable: "+name;
63 G4Exception("G4GDMLEvaluator::DefineConstant()", "InvalidExpression",
64 FatalException, error_msg);
65 }
66 eval.setVariable(name.c_str(),value);
67}
68
69void G4GDMLEvaluator::DefineVariable(const G4String& name,G4double value)
70{
71 if (eval.findVariable(name))
72 {
73 G4String error_msg = "Redefinition of constant or variable: "+name;
74 G4Exception("G4GDMLEvaluator::DefineVariable()", "InvalidExpression",
75 FatalException, error_msg);
76 }
77 eval.setVariable(name.c_str(),value);
78 variableList.push_back(name);
79}
80
81void G4GDMLEvaluator::DefineMatrix(const G4String& name,
82 G4int coldim,
83 std::vector<G4double> valueList)
84{
85 const G4int size = valueList.size();
86
87 if (size == 0)
88 {
89 G4String error_msg = "Matrix '"+name+"' is empty!";
90 G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
91 FatalException, error_msg);
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 if (size % coldim != 0)
102 {
103 G4String error_msg = "Matrix '" + name + "' is not filled correctly!";
104 G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
105 FatalException, error_msg);
106 }
107
108 if ((size == coldim) || (coldim == 1)) // Row- or column matrix
109 {
110 for (G4int i=0;i<size;i++)
111 {
112 std::stringstream MatrixElementNameStream;
113 MatrixElementNameStream << name << "_" << i;
114 DefineConstant(MatrixElementNameStream.str(),valueList[i]);
115 }
116 }
117 else // Normal matrix
118 {
119 const G4int rowdim = size/coldim;
120
121 for (G4int i=0;i<rowdim;i++)
122 {
123 for (G4int j=0;j<coldim;j++)
124 {
125 std::stringstream MatrixElementNameStream;
126 MatrixElementNameStream << name << "_" << i << "_" << j;
127 DefineConstant(MatrixElementNameStream.str(),valueList[coldim*i+j]);
128 }
129 }
130 }
131}
132
133void G4GDMLEvaluator::SetVariable(const G4String& name, G4double value)
134{
135 if (!IsVariable(name))
136 {
137 G4String error_msg = "Variable '" + name + "' is not defined!";
138 G4Exception("G4GDMLEvaluator::SetVariable()", "InvalidSetup",
139 FatalException, error_msg);
140 }
141 eval.setVariable(name.c_str(),value);
142}
143
144G4bool G4GDMLEvaluator::IsVariable(const G4String& name) const
145{
146 const size_t variableCount = variableList.size();
147
148 for (size_t i=0;i<variableCount;i++)
149 {
150 if (variableList[i] == name) { return true; }
151 }
152
153 return false;
154}
155
156G4String G4GDMLEvaluator::SolveBrackets(const G4String& in)
157{
158 std::string::size_type full = in.size();
159 std::string::size_type open = in.find("[",0);
160 std::string::size_type close = in.find("]",0);
161
162 if (open==close) { return in; }
163
164 if ((open>close) || (open==std::string::npos) || (close==std::string::npos))
165 {
166 G4String error_msg = "Bracket mismatch: " + in;
167 G4Exception("G4GDMLEvaluator::SolveBrackets()", "InvalidExpression",
168 FatalException, error_msg);
169 return in;
170 }
171
172 std::string::size_type begin = open;
173 std::string::size_type end = 0;
174 std::string::size_type end1 = 0;
175 std::string out;
176 out.append(in,0,open);
177
178 do // Loop for all possible matrix elements in 'in'
179 {
180 do // SolveBrackets for one matrix element
181 {
182 end = in.find(",",begin+1);
183 end1= in.find("]",begin+1);
184 if (end>end1) { end = end1; }
185 if (end==std::string::npos) { end = close;}
186
187 std::stringstream indexStream;
188 indexStream << "_" << EvaluateInteger(in.substr(begin+1,end-begin-1))-1;
189
190 out.append(indexStream.str());
191
192 begin = end;
193
194 } while (end<close);
195
196 if (full==close) { return out; }
197
198 open = in.find("[",begin);
199 close = in.find("]",begin+1);
200
201 if (open==close) { out.append(in.substr(end+1,full-end-1)); return out; }
202 out.append(in.substr(end+1,open-end-1));
203
204 begin=open;
205
206 } while (close<full);
207
208 return out;
209}
210
211G4double G4GDMLEvaluator::Evaluate(const G4String& in)
212{
213 G4String expression = SolveBrackets(in);
214
215 G4double value = 0.0;
216
217 if (!expression.empty())
218 {
219 value = eval.evaluate(expression.c_str());
220
221 if (eval.status() != HepTool::Evaluator::OK)
222 {
223 eval.print_error();
224 G4String error_msg = "Error in expression: " + expression;
225 G4Exception("G4GDMLEvaluator::Evaluate()", "InvalidExpression",
226 FatalException, error_msg);
227 }
228 }
229 return value;
230}
231
232G4int G4GDMLEvaluator::EvaluateInteger(const G4String& expression)
233{
234 // This function is for evaluating integer expressions,
235 // like loop variables and matrix indices.
236 // Complains if the evaluated expression has a fractional
237 // part different from zero
238
239 G4double value = Evaluate(expression);
240
241 G4int whole = (G4int)value;
242 G4double frac = value - (G4double)whole;
243
244 if (frac != 0.0)
245 {
246 G4String error_msg = "Expression '" + expression
247 + "' is expected to have an integer value!";
248 G4Exception("G4GDMLEvaluator::EvaluateInteger()", "InvalidExpression",
249 FatalException, error_msg);
250 }
251 return whole;
252}
253
254G4double G4GDMLEvaluator::GetConstant(const G4String& name)
255{
256 if (IsVariable(name))
257 {
258 G4String error_msg = "Constant '" + name
259 + "' is not defined! It is a variable!";
260 G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
261 FatalException, error_msg);
262 }
263 if (!eval.findVariable(name))
264 {
265 G4String error_msg = "Constant '" + name + "' is not defined!";
266 G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
267 FatalException, error_msg);
268 }
269 return Evaluate(name);
270}
271
272G4double G4GDMLEvaluator::GetVariable(const G4String& name)
273{
274 if (!IsVariable(name))
275 {
276 G4String error_msg = "Variable '" + name + "' is not a defined!";
277 G4Exception("G4GDMLEvaluator::GetVariable()", "InvalidSetup",
278 FatalException, error_msg);
279 }
280 return Evaluate(name);
281}
Note: See TracBrowser for help on using the repository browser.