| [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 | //
|
|---|
| [987] | 27 | // $Id: G4GDMLEvaluator.cc,v 1.22 2009/02/26 10:13:35 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>
|
|---|
| [818] | 37 | #include "G4GDMLEvaluator.hh"
|
|---|
| 38 |
|
|---|
| [850] | 39 | G4GDMLEvaluator::G4GDMLEvaluator()
|
|---|
| 40 | {
|
|---|
| [818] | 41 | eval.clear();
|
|---|
| 42 | eval.setStdMath();
|
|---|
| [850] | 43 | eval.setSystemOfUnits(meter,kilogram,second,ampere,kelvin,mole,candela);
|
|---|
| [818] | 44 | }
|
|---|
| 45 |
|
|---|
| [850] | 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 | }
|
|---|
| [818] | 56 |
|
|---|
| [850] | 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 | }
|
|---|
| [818] | 65 | eval.setVariable(name.c_str(),value);
|
|---|
| [850] | 66 | variableList.push_back(name);
|
|---|
| [818] | 67 | }
|
|---|
| 68 |
|
|---|
| [850] | 69 | void G4GDMLEvaluator::DefineMatrix(const G4String& name,
|
|---|
| 70 | G4int coldim,
|
|---|
| 71 | std::vector<G4double> valueList)
|
|---|
| 72 | {
|
|---|
| 73 | const G4int size = valueList.size();
|
|---|
| [818] | 74 |
|
|---|
| [850] | 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 | }
|
|---|
| [818] | 95 |
|
|---|
| [850] | 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 | }
|
|---|
| [818] | 129 | eval.setVariable(name.c_str(),value);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| [850] | 132 | G4bool G4GDMLEvaluator::IsVariable(const G4String& name) const
|
|---|
| 133 | {
|
|---|
| 134 | const size_t variableCount = variableList.size();
|
|---|
| [818] | 135 |
|
|---|
| [850] | 136 | for (size_t i=0;i<variableCount;i++)
|
|---|
| 137 | {
|
|---|
| 138 | if (variableList[i] == name) { return true; }
|
|---|
| 139 | }
|
|---|
| [818] | 140 |
|
|---|
| [850] | 141 | return false;
|
|---|
| [818] | 142 | }
|
|---|
| 143 |
|
|---|
| [850] | 144 | G4String G4GDMLEvaluator::SolveBrackets(const G4String& in)
|
|---|
| 145 | {
|
|---|
| [987] | 146 | std::string::size_type full = in.size();
|
|---|
| 147 | std::string::size_type open = in.find("[",0);
|
|---|
| 148 | std::string::size_type close = in.find("]",0);
|
|---|
| [818] | 149 |
|
|---|
| [850] | 150 | if (open==close) { return in; }
|
|---|
| [818] | 151 |
|
|---|
| [850] | 152 | if (open>close)
|
|---|
| 153 | {
|
|---|
| 154 | G4String error_msg = "Bracket mismatch: " + in;
|
|---|
| 155 | G4Exception("G4GDMLEvaluator::SolveBrackets()", "InvalidExpression",
|
|---|
| 156 | FatalException, error_msg);
|
|---|
| [818] | 157 | }
|
|---|
| 158 |
|
|---|
| [850] | 159 | std::string::size_type begin = open;
|
|---|
| 160 | std::string::size_type end = 0;
|
|---|
| [987] | 161 | std::string::size_type end1 = 0;
|
|---|
| [850] | 162 | std::string out;
|
|---|
| 163 | out.append(in,0,open);
|
|---|
| [987] | 164 |
|
|---|
| 165 | do // Loop for all possible matrix elements in 'in'
|
|---|
| [850] | 166 | {
|
|---|
| [987] | 167 | do // SolveBrackets for one matrix element
|
|---|
| 168 | {
|
|---|
| 169 | end = in.find(",",begin+1);
|
|---|
| 170 | end1= in.find("]",begin+1);
|
|---|
| 171 | if (end>end1) { end = end1; }
|
|---|
| 172 | if (end==std::string::npos) { end = close;}
|
|---|
| 173 |
|
|---|
| 174 | std::stringstream indexStream;
|
|---|
| 175 | indexStream << "_" << EvaluateInteger(in.substr(begin+1,end-begin-1))-1;
|
|---|
| [850] | 176 |
|
|---|
| [987] | 177 | out.append(indexStream.str());
|
|---|
| [850] | 178 |
|
|---|
| [987] | 179 | begin = end;
|
|---|
| [850] | 180 |
|
|---|
| [987] | 181 | } while (end<close);
|
|---|
| 182 |
|
|---|
| 183 | if (full==close) { return out; }
|
|---|
| 184 |
|
|---|
| 185 | open = in.find("[",begin);
|
|---|
| 186 | close = in.find("]",begin+1);
|
|---|
| [850] | 187 |
|
|---|
| [987] | 188 | if (open==close) { out.append(in.substr(end+1,full-end-1)); return out; }
|
|---|
| 189 | out.append(in.substr(end+1,open-end-1));
|
|---|
| [850] | 190 |
|
|---|
| [987] | 191 | begin=open;
|
|---|
| 192 |
|
|---|
| 193 | } while (close<full);
|
|---|
| 194 |
|
|---|
| [850] | 195 | return out;
|
|---|
| [818] | 196 | }
|
|---|
| 197 |
|
|---|
| [850] | 198 | G4double G4GDMLEvaluator::Evaluate(const G4String& in)
|
|---|
| 199 | {
|
|---|
| 200 | G4String expression = SolveBrackets(in);
|
|---|
| [818] | 201 |
|
|---|
| 202 | G4double value = 0.0;
|
|---|
| 203 |
|
|---|
| [850] | 204 | if (!expression.empty())
|
|---|
| 205 | {
|
|---|
| [818] | 206 | value = eval.evaluate(expression.c_str());
|
|---|
| 207 |
|
|---|
| [850] | 208 | if (eval.status() != HepTool::Evaluator::OK)
|
|---|
| 209 | {
|
|---|
| [818] | 210 | eval.print_error();
|
|---|
| [850] | 211 | G4String error_msg = "Error in expression: " + expression;
|
|---|
| 212 | G4Exception("G4GDMLEvaluator::Evaluate()", "InvalidExpression",
|
|---|
| 213 | FatalException, error_msg);
|
|---|
| [818] | 214 | }
|
|---|
| 215 | }
|
|---|
| 216 | return value;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| [850] | 219 | G4int G4GDMLEvaluator::EvaluateInteger(const G4String& expression)
|
|---|
| 220 | {
|
|---|
| 221 | // This function is for evaluating integer expressions,
|
|---|
| 222 | // like loop variables and matrix indices.
|
|---|
| 223 | // Complains if the evaluated expression has a fractional
|
|---|
| 224 | // part different from zero
|
|---|
| [818] | 225 |
|
|---|
| 226 | G4double value = Evaluate(expression);
|
|---|
| 227 |
|
|---|
| 228 | G4int whole = (G4int)value;
|
|---|
| 229 | G4double frac = value - (G4double)whole;
|
|---|
| 230 |
|
|---|
| [850] | 231 | if (frac != 0.0)
|
|---|
| 232 | {
|
|---|
| 233 | G4String error_msg = "Expression '" + expression
|
|---|
| 234 | + "' is expected to have an integer value!";
|
|---|
| 235 | G4Exception("G4GDMLEvaluator::EvaluateInteger()", "InvalidExpression",
|
|---|
| 236 | FatalException, error_msg);
|
|---|
| 237 | }
|
|---|
| [818] | 238 | return whole;
|
|---|
| 239 | }
|
|---|
| [850] | 240 |
|
|---|
| 241 | G4double G4GDMLEvaluator::GetConstant(const G4String& name)
|
|---|
| 242 | {
|
|---|
| 243 | if (IsVariable(name))
|
|---|
| 244 | {
|
|---|
| 245 | G4String error_msg = "Constant '" + name
|
|---|
| 246 | + "' is not defined! It is a variable!";
|
|---|
| 247 | G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
|
|---|
| 248 | FatalException, error_msg);
|
|---|
| 249 | }
|
|---|
| 250 | if (!eval.findVariable(name))
|
|---|
| 251 | {
|
|---|
| 252 | G4String error_msg = "Constant '" + name + "' is not defined!";
|
|---|
| 253 | G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
|
|---|
| 254 | FatalException, error_msg);
|
|---|
| 255 | }
|
|---|
| 256 | return Evaluate(name);
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | G4double G4GDMLEvaluator::GetVariable(const G4String& name)
|
|---|
| 260 | {
|
|---|
| 261 | if (!IsVariable(name))
|
|---|
| 262 | {
|
|---|
| 263 | G4String error_msg = "Variable '" + name + "' is not a defined!";
|
|---|
| 264 | G4Exception("G4GDMLEvaluator::GetVariable()", "InvalidSetup",
|
|---|
| 265 | FatalException, error_msg);
|
|---|
| 266 | }
|
|---|
| 267 | return Evaluate(name);
|
|---|
| 268 | }
|
|---|