[818] | 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 | // |
---|
[1347] | 27 | // $Id: G4GDMLEvaluator.cc,v 1.26 2010/11/04 11:33:49 gcosmo Exp $ |
---|
[818] | 28 | // GEANT4 tag $ Name:$ |
---|
| 29 | // |
---|
| 30 | // class G4GDMLEvaluator Implementation |
---|
| 31 | // |
---|
| 32 | // Original author: Zoltan Torzsok, November 2007 |
---|
| 33 | // |
---|
| 34 | // -------------------------------------------------------------------- |
---|
| 35 | |
---|
[850] | 36 | #include <sstream> |
---|
[1228] | 37 | |
---|
| 38 | #include "globals.hh" |
---|
| 39 | |
---|
[818] | 40 | #include "G4GDMLEvaluator.hh" |
---|
| 41 | |
---|
[850] | 42 | G4GDMLEvaluator::G4GDMLEvaluator() |
---|
| 43 | { |
---|
[818] | 44 | eval.clear(); |
---|
| 45 | eval.setStdMath(); |
---|
[850] | 46 | eval.setSystemOfUnits(meter,kilogram,second,ampere,kelvin,mole,candela); |
---|
[818] | 47 | } |
---|
| 48 | |
---|
[1347] | 49 | void 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 | |
---|
[850] | 58 | void 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 | } |
---|
[818] | 68 | |
---|
[850] | 69 | void 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 | } |
---|
[818] | 77 | eval.setVariable(name.c_str(),value); |
---|
[850] | 78 | variableList.push_back(name); |
---|
[818] | 79 | } |
---|
| 80 | |
---|
[850] | 81 | void G4GDMLEvaluator::DefineMatrix(const G4String& name, |
---|
| 82 | G4int coldim, |
---|
| 83 | std::vector<G4double> valueList) |
---|
| 84 | { |
---|
| 85 | const G4int size = valueList.size(); |
---|
[818] | 86 | |
---|
[850] | 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 | } |
---|
[818] | 107 | |
---|
[850] | 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 | |
---|
| 133 | void 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 | } |
---|
[818] | 141 | eval.setVariable(name.c_str(),value); |
---|
| 142 | } |
---|
| 143 | |
---|
[850] | 144 | G4bool G4GDMLEvaluator::IsVariable(const G4String& name) const |
---|
| 145 | { |
---|
| 146 | const size_t variableCount = variableList.size(); |
---|
[818] | 147 | |
---|
[850] | 148 | for (size_t i=0;i<variableCount;i++) |
---|
| 149 | { |
---|
| 150 | if (variableList[i] == name) { return true; } |
---|
| 151 | } |
---|
[818] | 152 | |
---|
[850] | 153 | return false; |
---|
[818] | 154 | } |
---|
| 155 | |
---|
[850] | 156 | G4String G4GDMLEvaluator::SolveBrackets(const G4String& in) |
---|
| 157 | { |
---|
[987] | 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); |
---|
[818] | 161 | |
---|
[850] | 162 | if (open==close) { return in; } |
---|
[818] | 163 | |
---|
[1347] | 164 | if ((open>close) || (open==std::string::npos) || (close==std::string::npos)) |
---|
[850] | 165 | { |
---|
| 166 | G4String error_msg = "Bracket mismatch: " + in; |
---|
| 167 | G4Exception("G4GDMLEvaluator::SolveBrackets()", "InvalidExpression", |
---|
| 168 | FatalException, error_msg); |
---|
[1347] | 169 | return in; |
---|
[818] | 170 | } |
---|
| 171 | |
---|
[850] | 172 | std::string::size_type begin = open; |
---|
| 173 | std::string::size_type end = 0; |
---|
[987] | 174 | std::string::size_type end1 = 0; |
---|
[850] | 175 | std::string out; |
---|
| 176 | out.append(in,0,open); |
---|
[987] | 177 | |
---|
| 178 | do // Loop for all possible matrix elements in 'in' |
---|
[850] | 179 | { |
---|
[987] | 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; |
---|
[850] | 189 | |
---|
[987] | 190 | out.append(indexStream.str()); |
---|
[850] | 191 | |
---|
[987] | 192 | begin = end; |
---|
[850] | 193 | |
---|
[987] | 194 | } while (end<close); |
---|
| 195 | |
---|
| 196 | if (full==close) { return out; } |
---|
| 197 | |
---|
| 198 | open = in.find("[",begin); |
---|
| 199 | close = in.find("]",begin+1); |
---|
[850] | 200 | |
---|
[987] | 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)); |
---|
[850] | 203 | |
---|
[987] | 204 | begin=open; |
---|
| 205 | |
---|
| 206 | } while (close<full); |
---|
| 207 | |
---|
[850] | 208 | return out; |
---|
[818] | 209 | } |
---|
| 210 | |
---|
[850] | 211 | G4double G4GDMLEvaluator::Evaluate(const G4String& in) |
---|
| 212 | { |
---|
| 213 | G4String expression = SolveBrackets(in); |
---|
[818] | 214 | |
---|
| 215 | G4double value = 0.0; |
---|
| 216 | |
---|
[850] | 217 | if (!expression.empty()) |
---|
| 218 | { |
---|
[818] | 219 | value = eval.evaluate(expression.c_str()); |
---|
| 220 | |
---|
[850] | 221 | if (eval.status() != HepTool::Evaluator::OK) |
---|
| 222 | { |
---|
[818] | 223 | eval.print_error(); |
---|
[850] | 224 | G4String error_msg = "Error in expression: " + expression; |
---|
| 225 | G4Exception("G4GDMLEvaluator::Evaluate()", "InvalidExpression", |
---|
| 226 | FatalException, error_msg); |
---|
[818] | 227 | } |
---|
| 228 | } |
---|
| 229 | return value; |
---|
| 230 | } |
---|
| 231 | |
---|
[850] | 232 | G4int 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 |
---|
[818] | 238 | |
---|
| 239 | G4double value = Evaluate(expression); |
---|
| 240 | |
---|
| 241 | G4int whole = (G4int)value; |
---|
| 242 | G4double frac = value - (G4double)whole; |
---|
| 243 | |
---|
[850] | 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 | } |
---|
[818] | 251 | return whole; |
---|
| 252 | } |
---|
[850] | 253 | |
---|
| 254 | G4double 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 | |
---|
| 272 | G4double 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 | } |
---|