G4GDMLEvaluator.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 // GEANT4 tag $ Name:$
00029 //
00030 // class G4GDMLEvaluator Implementation
00031 //
00032 // Original author: Zoltan Torzsok, November 2007
00033 //
00034 // --------------------------------------------------------------------
00035 
00036 #include <sstream>
00037 
00038 #include "G4GDMLEvaluator.hh"
00039 #include "G4SystemOfUnits.hh"
00040 
00041 G4GDMLEvaluator::G4GDMLEvaluator()
00042 {
00043    eval.clear();
00044    eval.setStdMath();
00045    eval.setSystemOfUnits(meter,kilogram,second,ampere,kelvin,mole,candela);
00046 }
00047 
00048 void G4GDMLEvaluator::Clear()
00049 {
00050   eval.clear();
00051   eval.setStdMath();
00052   eval.setSystemOfUnits(meter,kilogram,second,ampere,kelvin,mole,candela);
00053 
00054   variableList.clear();
00055 }
00056 
00057 void G4GDMLEvaluator::DefineConstant(const G4String& name, G4double value)
00058 {
00059    if (eval.findVariable(name))
00060    {
00061      G4String error_msg = "Redefinition of constant or variable: "+name;
00062      G4Exception("G4GDMLEvaluator::DefineConstant()", "InvalidExpression",
00063                  FatalException, error_msg);
00064    }
00065    eval.setVariable(name.c_str(),value);
00066 }
00067 
00068 void G4GDMLEvaluator::DefineVariable(const G4String& name,G4double value)
00069 {
00070    if (eval.findVariable(name))
00071    {
00072      G4String error_msg = "Redefinition of constant or variable: "+name;
00073      G4Exception("G4GDMLEvaluator::DefineVariable()", "InvalidExpression",
00074                  FatalException, error_msg);
00075    }
00076    eval.setVariable(name.c_str(),value);
00077    variableList.push_back(name);
00078 }
00079 
00080 void G4GDMLEvaluator::DefineMatrix(const G4String& name,
00081                                          G4int coldim,
00082                                          std::vector<G4double> valueList)
00083 {
00084    const G4int size = valueList.size();
00085 
00086    if (size == 0)
00087    {
00088      G4String error_msg = "Matrix '"+name+"' is empty!";
00089      G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
00090                  FatalException, error_msg);
00091    }
00092    /*
00093    if (size == 1)
00094    {
00095      G4String error_msg = "Matrix '" + name
00096                         + "' has only one element! "
00097                         + "Define a constant instead!!";
00098      G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
00099                  FatalException, error_msg);
00100    }
00101    */
00102 
00103    if (size % coldim != 0)
00104    {
00105      G4String error_msg = "Matrix '" + name + "' is not filled correctly!";
00106      G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
00107                  FatalException, error_msg);
00108    }
00109 
00110    if ((size == coldim) || (coldim == 1))   // Row- or column matrix
00111    {
00112       for (G4int i=0;i<size;i++)
00113       {
00114          std::stringstream MatrixElementNameStream;
00115          MatrixElementNameStream << name << "_" << i;
00116          DefineConstant(MatrixElementNameStream.str(),valueList[i]);
00117       }
00118    }
00119    else   // Normal matrix
00120    {
00121       const G4int rowdim = size/coldim;
00122 
00123       for (G4int i=0;i<rowdim;i++)
00124       {
00125         for (G4int j=0;j<coldim;j++)
00126         {
00127           std::stringstream MatrixElementNameStream;
00128           MatrixElementNameStream << name << "_" << i << "_" << j;
00129           DefineConstant(MatrixElementNameStream.str(),valueList[coldim*i+j]);
00130         }
00131       }
00132    }
00133 }
00134 
00135 void G4GDMLEvaluator::SetVariable(const G4String& name, G4double value)
00136 {
00137    if (!IsVariable(name))
00138    {
00139      G4String error_msg = "Variable '" + name + "' is not defined!";
00140      G4Exception("G4GDMLEvaluator::SetVariable()", "InvalidSetup",
00141                  FatalException, error_msg);
00142    }
00143    eval.setVariable(name.c_str(),value);
00144 }
00145 
00146 G4bool G4GDMLEvaluator::IsVariable(const G4String& name) const
00147 {
00148    const size_t variableCount = variableList.size();
00149 
00150    for (size_t i=0;i<variableCount;i++)
00151    {
00152       if (variableList[i] == name)  { return true; }
00153    }
00154 
00155    return false;
00156 }
00157 
00158 G4String G4GDMLEvaluator::SolveBrackets(const G4String& in)
00159 {
00160    std::string::size_type full  = in.size();
00161    std::string::size_type open  = in.find("[",0);
00162    std::string::size_type close = in.find("]",0);
00163 
00164    if (open==close) { return in; }
00165 
00166    if ((open>close) || (open==std::string::npos) || (close==std::string::npos))
00167    {
00168      G4String error_msg = "Bracket mismatch: " + in;
00169      G4Exception("G4GDMLEvaluator::SolveBrackets()", "InvalidExpression",
00170                  FatalException, error_msg);
00171      return in;
00172    }
00173 
00174    std::string::size_type begin = open;
00175    std::string::size_type end = 0;
00176    std::string::size_type end1 = 0;
00177    std::string out;
00178    out.append(in,0,open);
00179 
00180    do  // Loop for all possible matrix elements in 'in' 
00181    {
00182      do   // SolveBrackets for one matrix element
00183      { 
00184        end = in.find(",",begin+1);
00185        end1= in.find("]",begin+1);
00186        if (end>end1)                { end = end1; }
00187        if (end==std::string::npos)  { end = close;}
00188       
00189        std::stringstream indexStream;
00190        indexStream << "_" << EvaluateInteger(in.substr(begin+1,end-begin-1))-1;
00191 
00192        out.append(indexStream.str());
00193 
00194        begin = end;
00195 
00196      } while (end<close);
00197     
00198      if (full==close) { return out; }
00199     
00200      open  = in.find("[",begin);
00201      close = in.find("]",begin+1);
00202 
00203      if (open==close) { out.append(in.substr(end+1,full-end-1)); return out; }
00204      out.append(in.substr(end+1,open-end-1));
00205 
00206      begin=open;
00207     
00208    } while (close<full);
00209    
00210    return out;
00211 }
00212 
00213 G4double G4GDMLEvaluator::Evaluate(const G4String& in)
00214 {
00215    G4String expression = SolveBrackets(in);
00216 
00217    G4double value = 0.0;
00218 
00219    if (!expression.empty())
00220    {
00221       value = eval.evaluate(expression.c_str());
00222 
00223       if (eval.status() != G4Evaluator::OK)
00224       {
00225          eval.print_error();
00226          G4String error_msg = "Error in expression: " + expression;
00227          G4Exception("G4GDMLEvaluator::Evaluate()", "InvalidExpression",
00228                      FatalException, error_msg);
00229       }
00230    }
00231    return value;
00232 }
00233 
00234 G4int G4GDMLEvaluator::EvaluateInteger(const G4String& expression)
00235 {
00236    // This function is for evaluating integer expressions,
00237    // like loop variables and matrix indices.
00238    // Complains if the evaluated expression has a fractional
00239    // part different from zero
00240 
00241    G4double value = Evaluate(expression);
00242 
00243    G4int whole = (G4int)value;
00244    G4double frac = value - (G4double)whole;
00245 
00246    if (frac != 0.0)
00247    {
00248      G4String error_msg = "Expression '" + expression
00249                         + "' is expected to have an integer value!";
00250      G4Exception("G4GDMLEvaluator::EvaluateInteger()", "InvalidExpression",
00251                  FatalException, error_msg);
00252    }
00253    return whole;
00254 }
00255 
00256 G4double G4GDMLEvaluator::GetConstant(const G4String& name)
00257 {
00258    if (IsVariable(name))
00259    {
00260      G4String error_msg = "Constant '" + name
00261                         + "' is not defined! It is a variable!";
00262      G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
00263                  FatalException, error_msg);
00264    }
00265    if (!eval.findVariable(name))
00266    {
00267      G4String error_msg = "Constant '" + name + "' is not defined!";
00268      G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
00269                  FatalException, error_msg);
00270    }
00271    return Evaluate(name);
00272 }
00273 
00274 G4double G4GDMLEvaluator::GetVariable(const G4String& name)
00275 {
00276    if (!IsVariable(name))
00277    {
00278      G4String error_msg = "Variable '" + name + "' is not a defined!";
00279      G4Exception("G4GDMLEvaluator::GetVariable()", "InvalidSetup",
00280                  FatalException, error_msg);
00281    }
00282    return Evaluate(name);
00283 }

Generated on Mon May 27 17:48:19 2013 for Geant4 by  doxygen 1.4.7