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