Salome HOME
Get relevant changes from V7_dev branch (copyright update, adm files etc)
[tools/medcoupling.git] / src / INTERP_KERNEL / ExprEval / InterpKernelUnit.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "InterpKernelUnit.hxx"
22 #include "InterpKernelExprParser.hxx"
23
24 #include <cmath>
25 #include <sstream>
26 #include <iomanip>
27 #include <limits>
28
29 using namespace INTERP_KERNEL;
30
31 UnitDataBase UnitDataBase::_uniqueMapForExpr;
32
33 static const char InterpKernelMuAscii[2]={-0x4B,0x0};
34
35 static const char InterpKernelMuUnicode[3]={-0x3E,-0x4B,0x0};
36
37 const char *UnitDataBase::PREF_POW10[NB_OF_PREF_POW10]={"y","z","a","f","p","n",InterpKernelMuAscii,InterpKernelMuUnicode,"u","m","c","d",
38                                                         "da","h","k","M","G","T","P","E","Z","Y"};
39
40 const double UnitDataBase::POW10[NB_OF_PREF_POW10]={1e-24,1e-21,1e-18,1e-15,1e-12,1e-9,1e-6,1e-6,1e-6,1e-3,1e-2,1e-1,
41                                                   1e1,1e2,1e3,1e6,1e9,1e12,1e15,1e18,1e21,1e24};
42
43 static const char InterpKernelDegreeCAscii[3]={-0x50,0x43,0x0};
44
45 static const char InterpKernelDegreeCUnicode[4]={-0x3E,-0x50,0x43,0x0};
46
47 static const char InterpKernelDegreeCUnicodeWin[3]={-0x08,0x43,0x0};
48
49 const char *UnitDataBase::UNITS_RECOGN[NB_OF_UNITS_RECOGN]={"g","m","s","A","K",
50                                                             "W","J","Hz","V","h","min","t","N","dyn",
51                                                             "eV","Pa","atm","bar",InterpKernelDegreeCAscii,"C","ohm","F","S",
52                                                             "T","H","P","St",InterpKernelDegreeCUnicode,InterpKernelDegreeCUnicodeWin};
53
54 const short UnitDataBase::PROJ_IN_BASE[NB_OF_UNITS_RECOGN][SIZE_OF_UNIT_BASE]=
55   {
56     {1,0,0,0,0},//g
57     {0,1,0,0,0},//m
58     {0,0,1,0,0},//s
59     {0,0,0,1,0},//A
60     {0,0,0,0,1},//K
61     {1,2,-3,0,0},//W
62     {1,2,-2,0,0},//J
63     {0,0,-1,0,0},//Hz
64     {1,2,-3,-1,0},//V
65     {0,0,1,0,0},//h
66     {0,0,1,0,0},//min
67     {1,0,0,0,0},//t
68     {1,1,-2,0,0},//N
69     {1,1,-2,0,0},//dyn
70     {1,2,-2,0,0},//eV
71     {1,-1,-2,0,0},//Pa
72     {1,-1,-2,0,0},//atm
73     {1,-1,-2,0,0},//bar
74     {0,0,0,0,1},//degree C
75     {0,0,1,1,0},//C
76     {1,2,-3,-2,0},//ohm
77     {-1,-2,4,2,0},//F
78     {-1,-2,3,2,0},//S
79     {1,0,-2,-1,0},//T
80     {1,2,-2,-2,0},//H
81     {1,-1,-1,0,0},//P
82     {0,2,-1,0,0},//St
83     {0,0,0,0,1},//degree C
84     {0,0,0,0,1}//degree C
85   };
86
87 const double UnitDataBase::MUL_COEFF[NB_OF_UNITS_RECOGN]=
88   { 1.,1.,1.,1.,1.,
89     1000.,1000.,1.,1000.,3600.,3600.,1e6,1000.,1e-2,
90     1.60217733e-16,1000.,1.01325e8,1e8,1.,1.,1000.,1e-3,
91     1000.,1000.,100.,1.,1.,1.,1.};
92
93 const double UnitDataBase::ADD_COEFF[NB_OF_UNITS_RECOGN]=
94   { 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 273.15, 0., 0., 0., 0., 0., 0., 0., 0., 273.15 ,273.15};
95
96 UnitDataBase::UnitDataBase()
97 {
98   for(int i=0;i<NB_OF_PREF_POW10;i++)
99     _prefix_pow_10[PREF_POW10[i]]=POW10[i];
100   for(int i=0;i<NB_OF_UNITS_RECOGN;i++)
101     {
102       _units_semantic[UNITS_RECOGN[i]]=PROJ_IN_BASE[i];
103       _units_mul[UNITS_RECOGN[i]]=MUL_COEFF[i];
104       _units_add[UNITS_RECOGN[i]]=ADD_COEFF[i];
105     }
106 }
107
108 const short *UnitDataBase::getInfoForUnit(const std::string& unit, double& addFact, double& mFact) const
109 {
110   std::size_t lgth=unit.length();
111   std::string work,work2;
112   const short *ret=0;
113   for(std::size_t i=0;i<lgth && !ret;i++)
114     {
115       work=unit.substr(i);
116       std::map<std::string,const short *>::const_iterator iter=_units_semantic.find(work);
117       if(iter!=_units_semantic.end())
118         {
119           ret=(*iter).second;
120           std::map<std::string,double>::const_iterator iter2=_units_add.find(work);
121           addFact=(*iter2).second;
122           std::map<std::string,double>::const_iterator iter3=_units_mul.find(work);
123           mFact=(*iter3).second;
124           work2=unit.substr(0,i);
125         }
126     }
127   if(!ret)
128     {
129       std::ostringstream os;
130       os << "Unit : " << unit << " not recognized !";
131       throw INTERP_KERNEL::Exception(os.str().c_str());
132     }
133   if(!work2.empty())
134     {
135       std::map<std::string,double>::const_iterator iter4=_prefix_pow_10.find(work2);
136       if(iter4==_prefix_pow_10.end())
137         {
138           std::ostringstream os;
139           os << "Unit : " << unit << " not fully recognized : \"" << work << "\" detected as core unit and \"";
140           os << work2 << "\" not recognized prefix !";
141           throw INTERP_KERNEL::Exception(os.str().c_str());
142         }
143       addFact=0.;
144       mFact*=(*iter4).second;
145     }
146   return ret;
147 }
148
149 DecompositionInUnitBase::DecompositionInUnitBase():_add_to_base(0.),_mult_fact_to_base(1.)
150 {
151   _value[0]=0;
152   _value[1]=0;
153   _value[2]=0;
154   _value[3]=0;
155   _value[4]=0;
156 }
157
158 void DecompositionInUnitBase::setInfo(const short *vals, double addFact, double mFact)
159 {
160   _add_to_base=addFact;
161   _mult_fact_to_base=mFact;
162   _value[0]=vals[0];
163   _value[1]=vals[1];
164   _value[2]=vals[2];
165   _value[3]=vals[3];
166   _value[4]=vals[4];
167 }
168
169 bool DecompositionInUnitBase::operator==(const DecompositionInUnitBase& other) const
170 {
171   return _value[0]==other._value[0] && _value[1]==other._value[1] && _value[2]==other._value[2] && _value[3]==other._value[3] && _value[4]==other._value[4];
172 }
173
174 void DecompositionInUnitBase::getTranslationParams(const DecompositionInUnitBase& other, double& mul, double& add) const
175 {
176   if((*this)==other)
177     {
178       mul=_mult_fact_to_base/other._mult_fact_to_base;
179       add=_add_to_base/other._mult_fact_to_base-other._add_to_base;
180     }
181   else
182     {
183       mul=std::numeric_limits<double>::max();
184       add=std::numeric_limits<double>::max();
185     }
186 }
187
188 bool DecompositionInUnitBase::isEqual(short mass, short lgth, short time, short intensity, short temp, double add, double mult)
189 {
190   bool ret1=mass==_value[0];
191   bool ret2=lgth==_value[1];
192   bool ret3=time==_value[2];
193   bool ret4=intensity==_value[3];
194   bool ret5=temp==_value[4];
195   bool ret6=areDoubleEquals(add,_add_to_base);
196   bool ret7=areDoubleEquals(mult,_mult_fact_to_base);
197   return ret1 && ret2 && ret3 && ret4 && ret5 && ret6 && ret7;
198 }
199
200 void DecompositionInUnitBase::negate()
201 {
202   _mult_fact_to_base=-_mult_fact_to_base;
203 }
204
205 bool DecompositionInUnitBase::isAdimensional() const
206 {
207   return _value[0]==0 && _value[1]==0 && _value[2]==0 && _value[3]==0 && _value[4]==0;
208 }
209
210 bool DecompositionInUnitBase::isUnitary() const
211 {
212   return areDoubleEquals(_add_to_base,0.) && areDoubleEquals(_mult_fact_to_base,1.);
213 }
214
215 void DecompositionInUnitBase::tryToConvertInUnit(double val)
216 {
217   int valI=(int)val;
218   if((val-(double)valI)!=0.)
219     {
220       std::ostringstream os;
221       os << "Double value " << val << " can't be considered as integer. Not admitable for units !";
222       throw INTERP_KERNEL::Exception(os.str().c_str());
223     }
224   _value[0]=0;
225   _value[1]=0;
226   _value[2]=0;
227   _value[3]=0;
228   _value[4]=0;
229   _add_to_base=0;
230   _mult_fact_to_base=valI;
231 }
232
233 DecompositionInUnitBase &DecompositionInUnitBase::operator*(const DecompositionInUnitBase& other)
234 {
235   _value[0]+=other._value[0]; _value[1]+=other._value[1]; _value[2]+=other._value[2]; _value[3]+=other._value[3]; _value[4]+=other._value[4];
236   _mult_fact_to_base*=other._mult_fact_to_base;
237   _add_to_base=0.;
238   return *this;
239 }
240
241 DecompositionInUnitBase &DecompositionInUnitBase::operator/(const DecompositionInUnitBase& other)
242 {
243   _value[0]-=other._value[0]; _value[1]-=other._value[1]; _value[2]-=other._value[2]; _value[3]-=other._value[3]; _value[4]-=other._value[4];
244   _mult_fact_to_base/=other._mult_fact_to_base;
245  _add_to_base=0.;
246  return *this;
247 }
248
249 DecompositionInUnitBase &DecompositionInUnitBase::operator^(const DecompositionInUnitBase& other)
250 {
251   if(!other.isAdimensional())
252     throw INTERP_KERNEL::Exception("Trying to execute operator ^ with a second member not adimensionnal");
253   int exp=couldItBeConsideredAsInt(other._mult_fact_to_base);
254   _value[0]*=exp; _value[1]*=exp; _value[2]*=exp; _value[3]*=exp; _value[4]*=exp;
255   _mult_fact_to_base=powInt(_mult_fact_to_base,exp);
256   _add_to_base=0.;
257   return *this;
258 }
259
260 void DecompositionInUnitBase::dealWithAddFactor(const DecompositionInUnitBase& other)
261 {
262   if(!areDoubleEquals(_add_to_base,0.))
263     if(other.isAdimensional())
264       if(areDoubleEquals(other._mult_fact_to_base,1.))
265         return ;
266   if(!other.areDoubleEquals(_add_to_base,0.))
267     if(isAdimensional())
268       if(areDoubleEquals(_mult_fact_to_base,1.))
269         return ;
270   _add_to_base=0.;
271 }
272
273 double DecompositionInUnitBase::powInt(double val, int exp)
274 {
275   double work=1.;
276   if(exp==0)
277     return 1.;
278   if(exp>0)
279     for(int i=0;i<exp;i++)
280       work*=val;
281   else
282     {
283       int tmp=-exp;
284       for(int i=0;i<tmp;i++)
285         work*=1/val;
286     }
287   return work;
288 }
289
290 bool DecompositionInUnitBase::areDoubleEquals(double a, double b)
291 {
292   if(a==0. || b==0.)
293     return a==b;
294   double ref=std::max(a,b);
295   return fabs((a-b)/ref)<1e-7;
296 }
297
298 int DecompositionInUnitBase::couldItBeConsideredAsInt(double val)
299 {
300   int ret=(int)val;
301   double valT=(double) ret;
302   if(valT==val)
303     return ret;
304   else
305     {
306       std::ostringstream stream; stream << "Invalid double number " << std::setprecision(16) << val << " can's be considered for ^ operation on unit.";
307       throw INTERP_KERNEL::Exception(stream.str().c_str());
308     }
309 }
310
311 Unit::Unit(const char *reprC, bool tryToInterp):_coarse_repr(reprC),
312                                                 _is_interpreted(false),
313                                                 _is_interpretation_ok(false)
314 {
315   if(tryToInterp)
316     tryToInterprate();
317 }
318
319 Unit::Unit(const char *reprFortran, int sizeOfRepr, bool tryToInterp):_coarse_repr(ExprParser::buildStringFromFortran(reprFortran,sizeOfRepr)),
320                                                                       _is_interpreted(false),
321                                                                       _is_interpretation_ok(false)
322 {
323 }
324
325 void Unit::tryToInterprate() const
326 {
327   if(!_is_interpreted)
328     {
329       _is_interpreted=true;
330       _is_interpretation_ok=false;
331       try
332         {
333           ExprParser expr(_coarse_repr.c_str());
334           expr.parse();
335           _decomp_in_base=expr.evaluateUnit();
336           _is_interpretation_ok=true;
337         }
338       catch(INTERP_KERNEL::Exception&) { }
339     }
340 }
341
342 bool Unit::isInterpretationOK() const
343 {
344   return _is_interpretation_ok;
345 }
346
347 bool Unit::isCompatibleWith(const Unit& other) const
348 {
349   tryToInterprate();
350   other.tryToInterprate();
351   if(_is_interpretation_ok && other._is_interpretation_ok)
352     return _decomp_in_base==other._decomp_in_base;
353   else
354     return false;
355 }
356
357 double Unit::convert(const Unit& target, double sourceVal) const
358 {
359   if(isCompatibleWith(target))
360     {
361       double mult,add;
362       _decomp_in_base.getTranslationParams(target._decomp_in_base,mult,add);
363       return mult*sourceVal+add;
364     }
365   else
366     return std::numeric_limits<double>::max();
367 }
368
369 std::string Unit::getCoarseRepr() const
370 {
371   return _coarse_repr;
372 }