source: trunk/source/persistency/gdml/src/G4GDMLEvaluator.cc @ 1175

Last change on this file since 1175 was 987, checked in by garnier, 15 years ago

fichiers manquants

File size: 8.4 KB
Line 
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.22 2009/02/26 10:13:35 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#include "G4GDMLEvaluator.hh"
38
39G4GDMLEvaluator::G4GDMLEvaluator()
40{
41   eval.clear();
42   eval.setStdMath();
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   }
54   eval.setVariable(name.c_str(),value);
55}
56
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   }
65   eval.setVariable(name.c_str(),value);
66   variableList.push_back(name);
67}
68
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   }
129   eval.setVariable(name.c_str(),value);
130}
131
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   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);
149
150   if (open==close) { return in; }
151
152   if (open>close)
153   {
154     G4String error_msg = "Bracket mismatch: " + in;
155     G4Exception("G4GDMLEvaluator::SolveBrackets()", "InvalidExpression",
156                 FatalException, error_msg);
157   }
158
159   std::string::size_type begin = open;
160   std::string::size_type end = 0;
161   std::string::size_type end1 = 0;
162   std::string out;
163   out.append(in,0,open);
164
165   do  // Loop for all possible matrix elements in 'in'
166   {
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;
176
177       out.append(indexStream.str());
178
179       begin = end;
180
181     } while (end<close);
182   
183     if (full==close) { return out; }
184   
185     open  = in.find("[",begin);
186     close = in.find("]",begin+1);
187
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));
190
191     begin=open;
192   
193   } while (close<full);
194   
195   return out;
196}
197
198G4double G4GDMLEvaluator::Evaluate(const G4String& in)
199{
200   G4String expression = SolveBrackets(in);
201
202   G4double value = 0.0;
203
204   if (!expression.empty())
205   {
206      value = eval.evaluate(expression.c_str());
207
208      if (eval.status() != HepTool::Evaluator::OK)
209      {
210         eval.print_error();
211         G4String error_msg = "Error in expression: " + expression;
212         G4Exception("G4GDMLEvaluator::Evaluate()", "InvalidExpression",
213                     FatalException, error_msg);
214      }
215   }
216   return value;
217}
218
219G4int 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
225
226   G4double value = Evaluate(expression);
227
228   G4int whole = (G4int)value;
229   G4double frac = value - (G4double)whole;
230
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   }
238   return whole;
239}
240
241G4double 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
259G4double 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}
Note: See TracBrowser for help on using the repository browser.