Ignore:
Timestamp:
Sep 10, 2008, 5:40:37 PM (16 years ago)
Author:
garnier
Message:

geant4.8.2 beta

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/persistency/gdml/src/G4GDMLEvaluator.cc

    r818 r850  
    2525//
    2626//
    27 // $Id: G4GDMLEvaluator.cc,v 1.11 2007/11/28 10:27:18 ztorzsok Exp $
     27// $Id: G4GDMLEvaluator.cc,v 1.20 2008/07/16 15:46:34 gcosmo Exp $
    2828// GEANT4 tag $ Name:$
    2929//
     
    3434// --------------------------------------------------------------------
    3535
     36#include <sstream>
    3637#include "G4GDMLEvaluator.hh"
    3738
    38 G4GDMLEvaluator::G4GDMLEvaluator() {
    39 
     39G4GDMLEvaluator::G4GDMLEvaluator()
     40{
    4041   eval.clear();
    4142   eval.setStdMath();
    42    eval.setSystemOfUnits(1.e+3,1./1.60217733e-25,1.e+9,1./1.60217733e-10,1.0,1.0,1.0);
    43 }
    44 
    45 void G4GDMLEvaluator::defineConstant(const G4String& name,G4double value) {
    46 
    47    if (eval.findVariable(name)) G4Exception("GDML: Constant or variable '"+name+"' is already defined!");
    48 
     43   eval.setSystemOfUnits(meter,kilogram,second,ampere,kelvin,mole,candela);
     44}
     45
     46void 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   }
    4954   eval.setVariable(name.c_str(),value);
    5055}
    5156
    52 void G4GDMLEvaluator::defineVariable(const G4String& name,G4double value) {
    53 
    54    if (eval.findVariable(name)) G4Exception("GDML: Constant or variable '"+name+"' is already defined!");
    55 
     57void 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   }
    5665   eval.setVariable(name.c_str(),value);
    5766   variableList.push_back(name);
    5867}
    5968
    60 void G4GDMLEvaluator::setVariable(const G4String& name,G4double value) {
    61 
    62    checkVariable(name);
    63 
     69void 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
     121void 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   }
    64129   eval.setVariable(name.c_str(),value);
    65130}
    66131
    67 void G4GDMLEvaluator::checkVariable(const G4String& name) {
    68 
    69    for (std::vector<G4String>::iterator iter = variableList.begin(); iter != variableList.end(); iter++) {
    70 
    71       if (name == *iter) return;
    72    }
    73 
    74    G4Exception("GDML: Variable '"+name+"' is not defined!");
    75 }
    76 
    77 G4double G4GDMLEvaluator::Evaluate(const G4String& expression) {
     132G4bool 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
     144G4String 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
     181G4double G4GDMLEvaluator::Evaluate(const G4String& in)
     182{
     183   G4String expression = SolveBrackets(in);
    78184
    79185   G4double value = 0.0;
    80186
    81    if (!expression.empty()) {
    82    
     187   if (!expression.empty())
     188   {
    83189      value = eval.evaluate(expression.c_str());
    84190
    85       if (eval.status() != HepTool::Evaluator::OK) {
    86 
     191      if (eval.status() != HepTool::Evaluator::OK)
     192      {
    87193         eval.print_error();
    88          G4Exception("Error in evaluator!");
     194         G4String error_msg = "Error in expression: " + expression;
     195         G4Exception("G4GDMLEvaluator::Evaluate()", "InvalidExpression",
     196                     FatalException, error_msg);
    89197      }
    90198   }
    91    
    92199   return value;
    93200}
    94201
    95 G4int G4GDMLEvaluator::EvaluateInteger(const G4String& expression) {
     202G4int 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
    96208
    97209   G4double value = Evaluate(expression);
     
    100212   G4double frac = value - (G4double)whole;
    101213
    102    if (frac != 0.0) G4Exception("GDML: Expression '"+expression+"' is expected to have an integer value!");
    103 
     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   }
    104221   return whole;
    105222}
     223
     224G4double 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
     242G4double 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}
Note: See TracChangeset for help on using the changeset viewer.