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