source: XIOS/dev/branch_openmp/src/parse_expr/operator_expr.hpp @ 1482

Last change on this file since 1482 was 1482, checked in by yushan, 6 years ago

Branch EP merged with Dev_cmip6 @r1481

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 20.1 KB
RevLine 
[458]1#ifndef __XIOS_OPERATOR_EXPR_HPP__
2#define __XIOS_OPERATOR_EXPR_HPP__
3
4#include <map>
5#include <string>
6#include <cmath>
7#include "exception.hpp"
8#include "array_new.hpp"
[1482]9#include "utils.hpp"
[458]10
[728]11using namespace std;
[458]12
13namespace xios
14{
15  class COperatorExpr
16  {
[728]17    public:
18    typedef double (*functionScalar)(double);
19    typedef double (*functionScalarScalar)(double, double);
[1038]20    typedef double (*functionScalarScalarScalar)(double, double,double);
[728]21    typedef CArray<double,1> (*functionField)(const CArray<double,1>&);
22    typedef CArray<double,1> (*functionFieldField)(const CArray<double,1>&, const CArray<double,1>&);
23    typedef CArray<double,1> (*functionFieldScalar)(const CArray<double,1>&, double);
24    typedef CArray<double,1> (*functionScalarField)(double, const CArray<double,1>&);
[1038]25    typedef CArray<double,1> (*functionScalarScalarField)(double, double, const CArray<double,1>&);
26    typedef CArray<double,1> (*functionScalarFieldScalar)(double, const CArray<double,1>&, double);
27    typedef CArray<double,1> (*functionScalarFieldField)(double, const CArray<double,1>&, const CArray<double,1>&);
28    typedef CArray<double,1> (*functionFieldScalarScalar)(const CArray<double,1>&, double, double);
29    typedef CArray<double,1> (*functionFieldScalarField)(const CArray<double,1>&, double, const CArray<double,1>&);
30    typedef CArray<double,1> (*functionFieldFieldScalar)(const CArray<double,1>&,  const CArray<double,1>&, double);
31    typedef CArray<double,1> (*functionFieldFieldField)(const CArray<double,1>&,  const CArray<double,1>&, const CArray<double,1>&);
32   
[458]33    COperatorExpr(void)
34    {
[728]35      opScalar[string("neg")] = neg_s;
36      opScalar[string("cos")] = cos_s;
37      opScalar[string("sin")] = sin_s;
38      opScalar[string("tan")] = tan_s;
39      opScalar[string("exp")] = exp_s;
40      opScalar[string("log")] = log_s;
41      opScalar[string("log10")] = log10_s;
42      opScalar[string("sqrt")] = sqrt_s;
[458]43
[728]44      opScalarScalar[string("add")] = add_ss;
45      opScalarScalar[string("minus")] = minus_ss;
46      opScalarScalar[string("mult")] = mult_ss;
47      opScalarScalar[string("div")] = div_ss;
48      opScalarScalar[string("pow")] = pow_ss;
49      opScalarScalar[string("eq")] = eq_ss;
50      opScalarScalar[string("lt")] = lt_ss;
51      opScalarScalar[string("gt")] = gt_ss;
52      opScalarScalar[string("le")] = le_ss;
53      opScalarScalar[string("ge")] = ge_ss;
[1019]54      opScalarScalar[string("ne")] = ne_ss;
[1038]55      opScalarScalarScalar[string("cond")] = cond_sss;
56     
[728]57      opField[string("neg")] = neg_f;
58      opField[string("cos")] = cos_f;
59      opField[string("sin")] = sin_f;
60      opField[string("tan")] = tan_f;
61      opField[string("exp")] = exp_f;
62      opField[string("log")] = log_f;
63      opField[string("log10")] = log10_f;
64      opField[string("sqrt")] = sqrt_f;
[458]65
[728]66      opFieldField[string("add")] = add_ff;
67      opFieldField[string("minus")] = minus_ff;
68      opFieldField[string("mult")] = mult_ff;
69      opFieldField[string("div")] = div_ff;
70      opFieldField[string("pow")] = pow_ff;
71      opFieldField[string("eq")] = eq_ff;
72      opFieldField[string("lt")] = lt_ff;
73      opFieldField[string("gt")] = gt_ff;
74      opFieldField[string("le")] = le_ff;
75      opFieldField[string("ge")] = ge_ff;
[1019]76      opFieldField[string("ne")] = ne_ff;
[728]77
78      opFieldScalar[string("add")] = add_fs;
79      opFieldScalar[string("minus")] = minus_fs;
80      opFieldScalar[string("mult")] = mult_fs;
81      opFieldScalar[string("div")] = div_fs;
82      opFieldScalar[string("pow")] = pow_fs;
83      opFieldScalar[string("eq")] = eq_fs;
84      opFieldScalar[string("lt")] = lt_fs;
85      opFieldScalar[string("gt")] = gt_fs;
86      opFieldScalar[string("le")] = le_fs;
87      opFieldScalar[string("ge")] = ge_fs;
[1019]88      opFieldScalar[string("ne")] = ne_fs;
[728]89
90      opScalarField[string("add")] = add_sf;
91      opScalarField[string("minus")] = minus_sf;
92      opScalarField[string("mult")] = mult_sf;
93      opScalarField[string("div")] = div_sf;
94      opScalarField[string("eq")] = eq_sf;
95      opScalarField[string("lt")] = lt_sf;
96      opScalarField[string("gt")] = gt_sf;
97      opScalarField[string("le")] = le_sf;
98      opScalarField[string("ge")] = ge_sf;
[1019]99      opScalarField[string("ne")] = ne_sf;
[1038]100
101      opScalarScalarField[string("cond")] = cond_ssf;
102      opScalarFieldScalar[string("cond")] = cond_sfs;
103      opScalarFieldField[string("cond")] = cond_sff;
104      opFieldScalarScalar[string("cond")] = cond_fss;
105      opFieldScalarField[string("cond")] = cond_fsf;
106      opFieldFieldScalar[string("cond")] = cond_ffs;
107      opFieldFieldField[string("cond")] = cond_fff;
108
109
[728]110    }
111
[458]112    functionScalar getOpScalar(const string& id)
113    {
[728]114      map<string,double (*)(double)>::iterator it;
115      it = opScalar.find(id);
116      if (it == opScalar.end()) ERROR("functionScalar COperatorExpr::getOpScalar(const string& id)", << "unknown operator : " << id)
117      return it->second;
[458]118    }
[728]119
[458]120    functionScalarScalar getOpScalarScalar(const string& id)
121    {
[728]122      map<string,double (*)(double,double)>::iterator it;
123      it = opScalarScalar.find(id);
124      if (it == opScalarScalar.end()) ERROR("functionScalarScalar COperatorExpr::getOpScalarScalar(const string& id)", << "unknown operator : " << id)
125      return it->second;
[458]126    }
[728]127
[1038]128    functionScalarScalarScalar getOpScalarScalarScalar(const string& id)
129    {
130      map<string,double (*)(double,double,double)>::iterator it;
131      it = opScalarScalarScalar.find(id);
132      if (it == opScalarScalarScalar.end()) ERROR("functionScalarScalarScalar getOpScalarScalarScalar(const string& id)", << "unknown operator : " << id)
133      return it->second;
134    }
135
[458]136    functionField getOpField(const string& id)
137    {
[728]138      map<string,functionField>::iterator it;
139      it = opField.find(id);
140      if (it == opField.end()) ERROR("functionField COperatorExpr::getOpField(const string& id)", << "unknown operator : " << id)
141      return it->second;
[458]142    }
[728]143
[458]144    functionFieldField getOpFieldField(const string& id)
145    {
[728]146      map<string,functionFieldField>::iterator it;
147      it = opFieldField.find(id);
148      if (it == opFieldField.end()) ERROR("dfunctionFieldField COperatorExpr::getOpFieldField(const string& id)", << "unknown operator : " << id)
149      return it->second;
[458]150    }
[728]151
[458]152    functionFieldScalar getOpFieldScalar(const string& id)
153    {
[728]154      map<string,functionFieldScalar>::iterator it;
155      it = opFieldScalar.find(id);
156      if (it == opFieldScalar.end()) ERROR("functionFieldScalar COperatorExpr::getOpFieldScalar(const string& id)", << "unknown operator : " << id)
157      return it->second;
[458]158    }
[728]159
[458]160    functionScalarField getOpScalarField(const string& id)
161    {
[728]162      map<string,functionScalarField>::iterator it;
163      it = opScalarField.find(id);
164      if (it == opScalarField.end()) ERROR("functionScalarField COperatorExpr::getOpFieldField(const string& id)", << "unknown operator : " << id)
165      return it->second;
[458]166    }
167
[1038]168    functionScalarScalarField getOpScalarScalarField(const string& id)
169    {
170      map<string,functionScalarScalarField>::iterator it;
171      it = opScalarScalarField.find(id);
172      if (it == opScalarScalarField.end()) ERROR("functionScalarScalarField getOpScalarScalarField(const string& id)", << "unknown operator : " << id)
173      return it->second;
174    }
175
176    functionScalarFieldScalar getOpScalarFieldScalar(const string& id)
177    {
178      map<string,functionScalarFieldScalar>::iterator it;
179      it = opScalarFieldScalar.find(id);
180      if (it == opScalarFieldScalar.end()) ERROR("functionScalarFieldScalar getOpScalarScalarField(const string& id)", << "unknown operator : " << id)
181      return it->second;
182    }
183
184    functionScalarFieldField getOpScalarFieldField(const string& id)
185    {
186      map<string,functionScalarFieldField>::iterator it;
187      it = opScalarFieldField.find(id);
188      if (it == opScalarFieldField.end()) ERROR("functionScalarFieldField getOpScalarFieldField(const string& id)", << "unknown operator : " << id)
189      return it->second;
190    }
191
192    functionFieldScalarScalar getOpFieldScalarScalar(const string& id)
193    {
194      map<string,functionFieldScalarScalar>::iterator it;
195      it = opFieldScalarScalar.find(id);
196      if (it == opFieldScalarScalar.end()) ERROR("functionFieldScalarScalar getOpFieldScalarScalar(const string& id)", << "unknown operator : " << id)
197      return it->second;
198    }
199
200    functionFieldScalarField getOpFieldScalarField(const string& id)
201    {
202      map<string,functionFieldScalarField>::iterator it;
203      it = opFieldScalarField.find(id);
204      if (it == opFieldScalarField.end()) ERROR("functionFieldScalarField getOpFieldScalarField(const string& id)", << "unknown operator : " << id)
205      return it->second;
206    }
207
208    functionFieldFieldScalar getOpFieldFieldScalar(const string& id)
209    {
210      map<string,functionFieldFieldScalar>::iterator it;
211      it = opFieldFieldScalar.find(id);
212      if (it == opFieldFieldScalar.end()) ERROR("functionFieldFieldScalar getOpFieldFieldScalar(const string& id)", << "unknown operator : " << id)
213      return it->second;
214    }
215
216    functionFieldFieldField getOpFieldFieldField(const string& id)
217    {
218      map<string,functionFieldFieldField>::iterator it;
219      it = opFieldFieldField.find(id);
220      if (it == opFieldFieldField.end()) ERROR("functionFieldFieldField getOpFieldFieldField(const string& id)", << "unknown operator : " << id)
221      return it->second;
222    }
223   
224   
[728]225    map<string,functionScalar> opScalar;
226    map<string,functionScalarScalar> opScalarScalar;
[1038]227    map<string,functionScalarScalarScalar> opScalarScalarScalar;
[728]228    map<string,functionField> opField;
229    map<string,functionFieldField> opFieldField;
230    map<string,functionFieldScalar> opFieldScalar;
231    map<string,functionScalarField> opScalarField;
[1038]232    map<string,functionScalarScalarField> opScalarScalarField;
233    map<string,functionScalarFieldScalar> opScalarFieldScalar;
234    map<string,functionScalarFieldField> opScalarFieldField;
235    map<string,functionFieldScalarScalar> opFieldScalarScalar;
236    map<string,functionFieldScalarField> opFieldScalarField;
237    map<string,functionFieldFieldScalar> opFieldFieldScalar;
238    map<string,functionFieldFieldField> opFieldFieldField;
[458]239
[1038]240
241
[728]242    static inline double neg_s(double x)   { return -x; }
243    static inline double cos_s(double x)   { return std::cos(x); }
244    static inline double sin_s(double x)   { return std::sin(x); }
245    static inline double tan_s(double x)   { return std::tan(x); }
246    static inline double exp_s(double x)   { return std::exp(x); }
247    static inline double log_s(double x)   { return std::log(x); }
248    static inline double log10_s(double x) { return std::log10(x); }
249    static inline double sqrt_s(double x)  { return std::sqrt(x); }
[458]250
[728]251    static inline double add_ss(double x, double y)   { return x + y; }
252    static inline double minus_ss(double x, double y) { return x - y; }
253    static inline double mult_ss(double x, double y)  { return x * y; }
254    static inline double div_ss(double x, double y)   { return x / y; }
255    static inline double pow_ss(double x, double y)   { return std::pow(x,y); }
[1482]256    static inline double eq_ss(double x, double y) // specific check for NaN
257    {
258       bool xNan=NumTraits<double>::isNan(x) ;
259       bool yNan=NumTraits<double>::isNan(y) ;
260       if (xNan && yNan) return true ;
261       else if (xNan) return false ;
262       else if (yNan) return false ;
263       else return x == y;
264    }
265
[728]266    static inline double lt_ss(double x, double y)    { return x < y; }
267    static inline double gt_ss(double x, double y)    { return x > y; }
268    static inline double le_ss(double x, double y)    { return x <= y; }
269    static inline double ge_ss(double x, double y)    { return x >= y; }
[1482]270    static inline double ne_ss(double x, double y) // specific check for NaN
271    {
272       bool xNan=NumTraits<double>::isNan(x) ;
273       bool yNan=NumTraits<double>::isNan(y) ;
274       if (xNan && yNan) return false ;
275       else if (xNan) return true ;
276       else if (yNan) return true ;     
277       else return x != y;
278    }
[728]279
280    static inline CArray<double,1> neg_f(const CArray<double,1>& x)   { return Array<double,1>(-x); }
281    static inline CArray<double,1> cos_f(const CArray<double,1>& x)   { return Array<double,1>(cos(x)); }
282    static inline CArray<double,1> sin_f(const CArray<double,1>& x)   { return Array<double,1>(sin(x)); }
283    static inline CArray<double,1> tan_f(const CArray<double,1>& x)   { return Array<double,1>(tan(x)); }
284    static inline CArray<double,1> exp_f(const CArray<double,1>& x)   { return Array<double,1>(exp(x)); }
285    static inline CArray<double,1> log_f(const CArray<double,1>& x)   { return Array<double,1>(log(x)); }
286    static inline CArray<double,1> log10_f(const CArray<double,1>& x) { return Array<double,1>(log10(x)); }
287    static inline CArray<double,1> sqrt_f(const CArray<double,1>& x)  { return Array<double,1>(sqrt(x)); }
288
289    static inline CArray<double,1> add_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
290    static inline CArray<double,1> minus_ff(const CArray<double,1>& x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
291    static inline CArray<double,1> mult_ff(const CArray<double,1>& x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
292    static inline CArray<double,1> div_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
293    static inline CArray<double,1> pow_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(pow(x,y)); }
294    static inline CArray<double,1> eq_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x == y); }
295    static inline CArray<double,1> lt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x < y); }
296    static inline CArray<double,1> gt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x > y); }
297    static inline CArray<double,1> le_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x <= y); }
298    static inline CArray<double,1> ge_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x >= y); }
[1019]299    static inline CArray<double,1> ne_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x != y); }
[728]300
301    static inline CArray<double,1> add_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x + y); }
302    static inline CArray<double,1> minus_fs(const CArray<double,1>& x, double y) { return Array<double,1>(x - y); }
303    static inline CArray<double,1> mult_fs(const CArray<double,1>& x, double y)  { return Array<double,1>(x * y); }
304    static inline CArray<double,1> div_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x / y); }
305    static inline CArray<double,1> pow_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(pow(x,y)); }
[1482]306    static inline CArray<double,1> eq_fs(const CArray<double,1>& x, double y) // specific check for NaN
307    {
308      if (NumTraits<double>::isNan(y))
309      {
310        CArray<double,1> ret(x.numElements()) ;
311        Array<double,1>::const_iterator itx=x.begin(),itxe=x.end();
312        Array<double,1>::iterator itret=ret.begin() ;
313        for(;itx!=itxe;++itx,++itret) *itret=NumTraits<double>::isNan(*itx) ;
314        return ret ;
315      }
316      else return Array<double,1>(x == y); 
317    }
318
[728]319    static inline CArray<double,1> lt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x < y); }
320    static inline CArray<double,1> gt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x > y); }
321    static inline CArray<double,1> le_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x <= y); }
322    static inline CArray<double,1> ge_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x >= y); }
[1482]323    static  inline CArray<double,1> ne_fs(const CArray<double,1>& x, double y) // specific check for NaN
324    {
325      if (NumTraits<double>::isNan(y))
326      {
327        CArray<double,1> ret(x.numElements()) ;
328        Array<double,1>::const_iterator itx=x.begin(),itxe=x.end();
329        Array<double,1>::iterator itret=ret.begin() ;
330        for(;itx!=itxe;++itx,++itret) *itret = !NumTraits<double>::isNan(*itx) ;
331        return ret ;
332      }
333      else return Array<double,1>(x != y); 
334    }
[728]335
336    static inline CArray<double,1> add_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
337    static inline CArray<double,1> minus_sf(double x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
338    static inline CArray<double,1> mult_sf(double x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
339    static inline CArray<double,1> div_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
[1482]340    static inline CArray<double,1> eq_sf(double x, const CArray<double,1>& y) // specific check for NaN
341    {
342      if (NumTraits<double>::isNan(x))
343      {
344        CArray<double,1> ret(y.numElements()) ;
345        Array<double,1>::const_iterator ity=y.begin(),itye=y.end();
346        Array<double,1>::iterator itret=ret.begin() ;
347        for(;ity!=itye;++ity,++itret) *itret=NumTraits<double>::isNan(*ity) ;
348        return ret ;
349      }
350      else return Array<double,1>(x == y); 
351    }
352
[728]353    static inline CArray<double,1> lt_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x < y); }
354    static inline CArray<double,1> gt_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x > y); }
355    static inline CArray<double,1> le_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x <= y); }
356    static inline CArray<double,1> ge_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x >= y); }
[1482]357    static inline CArray<double,1> ne_sf(double x, const CArray<double,1>& y) // specific check for NaN
358    {
359      if (NumTraits<double>::isNan(x))
360      {
361        CArray<double,1> ret(y.numElements()) ;
362        Array<double,1>::const_iterator ity=y.begin(),itye=y.end();
363        Array<double,1>::iterator itret=ret.begin() ;
364        for(;ity!=itye;++ity,++itret) *itret=!NumTraits<double>::isNan(*ity) ;
365        return ret ;
366      }
367      else return Array<double,1>(x != y);
368    }
[1038]369
370
[1482]371
[1038]372    static inline double cond_sss(double x, double y, double z)   { return (x==0) ? z : y ; }
373
374    static inline CArray<double,1> cond_ssf(double x, double y, const CArray<double,1>& z)
375    {
376      CArray<double,1> ret(z.numElements()) ;
377      if (x==0) ret=z ;
378      else ret=y ;
379      return ret ;
380    }
381
382    static inline CArray<double,1> cond_sfs(double x, const CArray<double,1>& y, double z)
383    {
384      CArray<double,1> ret(y.numElements()) ;
385     if (x==0) ret=z ;
386      else ret=y ;
387      return ret ;
388    }
389
390    static inline CArray<double,1> cond_sff(double x, const CArray<double,1>& y, const CArray<double,1>& z)
391    {
392      CArray<double,1> ret(y.numElements()) ;
393      if (x==0) ret=z ;
394      else ret=y ;
395      return ret ;
396    }
397   
398    static inline CArray<double,1> cond_fss(const CArray<double,1>& x, double y, double z)
399    {
400      CArray<double,1> ret(x.numElements()) ;
401      Array<double,1>::const_iterator itx=x.begin(),itxe=x.end();
402      Array<double,1>::iterator itret=ret.begin() ;
403     
404      for(;itx!=itxe;++itx,++itret) *itret=( (*itx==0)?z:y) ;
405      return ret ;
406    }
407
408    static inline CArray<double,1> cond_fsf(const CArray<double,1>& x, double y, const CArray<double,1>& z)
409    {
410      CArray<double,1> ret(x.numElements()) ;
411      Array<double,1>::const_iterator itx=x.begin(), itxe=x.end(), itz=z.begin();
412      Array<double,1>::iterator itret=ret.begin() ;
413      for(;itx!=itxe;++itx,++itz,++itret) *itret=( (*itx==0)?*itz:y) ;
414      return ret ;
415    }
416
417    static inline CArray<double,1> cond_ffs(const CArray<double,1>& x, const CArray<double,1>& y, double z)
418    {
419      CArray<double,1> ret(x.numElements()) ;
420      Array<double,1>::const_iterator itx=x.begin(), itxe=x.end(), ity=y.begin() ;
421      Array<double,1>::iterator itret=ret.begin() ;
422      for(;itx!=itxe;++itx,++ity,++itret) *itret=( (*itx==0)?z:*ity) ;
423      return ret ;
424    }
425
426    static inline CArray<double,1> cond_fff(const CArray<double,1>& x, const CArray<double,1>& y, const CArray<double,1>&  z)
427    {
428      CArray<double,1> ret(x.numElements()) ;
429      Array<double,1>::const_iterator itx=x.begin(), itxe=x.end(), ity=y.begin(), itz=z.begin() ;
430      Array<double,1>::iterator itret=ret.begin() ;
431      for(;itx!=itxe;++itx,++ity,++itz,++itret) *itret=( (*itx==0)?*itz:*ity) ;
432      return ret ;
433    }
434
[728]435  };
436
437  extern COperatorExpr operatorExpr;
[458]438}
439
440#endif
441
Note: See TracBrowser for help on using the repository browser.