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

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

branch_openmp merged with trunk r1544

  • 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
Line 
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"
9#include "utils.hpp"
10
11using namespace std;
12
13namespace xios
14{
15  class COperatorExpr
16  {
17    public:
18    typedef double (*functionScalar)(double);
19    typedef double (*functionScalarScalar)(double, double);
20    typedef double (*functionScalarScalarScalar)(double, double,double);
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>&);
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   
33    COperatorExpr(void)
34    {
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;
43
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;
54      opScalarScalar[string("ne")] = ne_ss;
55      opScalarScalarScalar[string("cond")] = cond_sss;
56     
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;
65
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;
76      opFieldField[string("ne")] = ne_ff;
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;
88      opFieldScalar[string("ne")] = ne_fs;
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;
99      opScalarField[string("ne")] = ne_sf;
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
110    }
111
112    functionScalar getOpScalar(const string& id)
113    {
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;
118    }
119
120    functionScalarScalar getOpScalarScalar(const string& id)
121    {
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;
126    }
127
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
136    functionField getOpField(const string& id)
137    {
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;
142    }
143
144    functionFieldField getOpFieldField(const string& id)
145    {
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;
150    }
151
152    functionFieldScalar getOpFieldScalar(const string& id)
153    {
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;
158    }
159
160    functionScalarField getOpScalarField(const string& id)
161    {
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;
166    }
167
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   
225    map<string,functionScalar> opScalar;
226    map<string,functionScalarScalar> opScalarScalar;
227    map<string,functionScalarScalarScalar> opScalarScalarScalar;
228    map<string,functionField> opField;
229    map<string,functionFieldField> opFieldField;
230    map<string,functionFieldScalar> opFieldScalar;
231    map<string,functionScalarField> opScalarField;
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;
239
240
241
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); }
250
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); }
256
257    static inline double eq_ss(double x, double y) // specific check for NaN
258    {
259       bool xNan=NumTraits<double>::isNan(x) ;
260       bool yNan=NumTraits<double>::isNan(y) ;
261       if (xNan && yNan) return true ;
262       else if (xNan) return false ;
263       else if (yNan) return false ;
264       else return x == y;
265    }
266
267    static inline double lt_ss(double x, double y)    { return x < y; }
268    static inline double gt_ss(double x, double y)    { return x > y; }
269    static inline double le_ss(double x, double y)    { return x <= y; }
270    static inline double ge_ss(double x, double y)    { return x >= y; }
271    static inline double ne_ss(double x, double y) // specific check for NaN
272    {
273       bool xNan=NumTraits<double>::isNan(x) ;
274       bool yNan=NumTraits<double>::isNan(y) ;
275       if (xNan && yNan) return false ;
276       else if (xNan) return true ;
277       else if (yNan) return true ;     
278       else return x != y;
279    }
280
281    static inline CArray<double,1> neg_f(const CArray<double,1>& x)   { return Array<double,1>(-x); }
282    static inline CArray<double,1> cos_f(const CArray<double,1>& x)   { return Array<double,1>(cos(x)); }
283    static inline CArray<double,1> sin_f(const CArray<double,1>& x)   { return Array<double,1>(sin(x)); }
284    static inline CArray<double,1> tan_f(const CArray<double,1>& x)   { return Array<double,1>(tan(x)); }
285    static inline CArray<double,1> exp_f(const CArray<double,1>& x)   { return Array<double,1>(exp(x)); }
286    static inline CArray<double,1> log_f(const CArray<double,1>& x)   { return Array<double,1>(log(x)); }
287    static inline CArray<double,1> log10_f(const CArray<double,1>& x) { return Array<double,1>(log10(x)); }
288    static inline CArray<double,1> sqrt_f(const CArray<double,1>& x)  { return Array<double,1>(sqrt(x)); }
289
290    static inline CArray<double,1> add_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
291    static inline CArray<double,1> minus_ff(const CArray<double,1>& x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
292    static inline CArray<double,1> mult_ff(const CArray<double,1>& x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
293    static inline CArray<double,1> div_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
294    static inline CArray<double,1> pow_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(pow(x,y)); }
295    static inline CArray<double,1> eq_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x == y); }
296    static inline CArray<double,1> lt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x < y); }
297    static inline CArray<double,1> gt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x > y); }
298    static inline CArray<double,1> le_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x <= y); }
299    static inline CArray<double,1> ge_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x >= y); }
300    static inline CArray<double,1> ne_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x != y); }
301
302    static inline CArray<double,1> add_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x + y); }
303    static inline CArray<double,1> minus_fs(const CArray<double,1>& x, double y) { return Array<double,1>(x - y); }
304    static inline CArray<double,1> mult_fs(const CArray<double,1>& x, double y)  { return Array<double,1>(x * y); }
305    static inline CArray<double,1> div_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x / y); }
306    static inline CArray<double,1> pow_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(pow(x,y)); }
307    static inline CArray<double,1> eq_fs(const CArray<double,1>& x, double y) // specific check for NaN
308    {
309      if (NumTraits<double>::isNan(y))
310      {
311        CArray<double,1> ret(x.numElements()) ;
312        Array<double,1>::const_iterator itx=x.begin(),itxe=x.end();
313        Array<double,1>::iterator itret=ret.begin() ;
314        for(;itx!=itxe;++itx,++itret) *itret=NumTraits<double>::isNan(*itx) ;
315        return ret ;
316      }
317      else return Array<double,1>(x == y); 
318    }
319
320    static inline CArray<double,1> lt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x < y); }
321    static inline CArray<double,1> gt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x > y); }
322    static inline CArray<double,1> le_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x <= y); }
323    static inline CArray<double,1> ge_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x >= y); }
324    static  inline CArray<double,1> ne_fs(const CArray<double,1>& x, double y) // specific check for NaN
325    {
326      if (NumTraits<double>::isNan(y))
327      {
328        CArray<double,1> ret(x.numElements()) ;
329        Array<double,1>::const_iterator itx=x.begin(),itxe=x.end();
330        Array<double,1>::iterator itret=ret.begin() ;
331        for(;itx!=itxe;++itx,++itret) *itret = !NumTraits<double>::isNan(*itx) ;
332        return ret ;
333      }
334      else return Array<double,1>(x != y); 
335    }
336
337    static inline CArray<double,1> add_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
338    static inline CArray<double,1> minus_sf(double x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
339    static inline CArray<double,1> mult_sf(double x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
340    static inline CArray<double,1> div_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
341    static inline CArray<double,1> eq_sf(double x, const CArray<double,1>& y) // specific check for NaN
342    {
343      if (NumTraits<double>::isNan(x))
344      {
345        CArray<double,1> ret(y.numElements()) ;
346        Array<double,1>::const_iterator ity=y.begin(),itye=y.end();
347        Array<double,1>::iterator itret=ret.begin() ;
348        for(;ity!=itye;++ity,++itret) *itret=NumTraits<double>::isNan(*ity) ;
349        return ret ;
350      }
351      else return Array<double,1>(x == y); 
352    }
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); }
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    }
369    static inline double cond_sss(double x, double y, double z)   { return (x==0) ? z : y ; }
370
371    static inline CArray<double,1> cond_ssf(double x, double y, const CArray<double,1>& z)
372    {
373      CArray<double,1> ret(z.numElements()) ;
374      if (x==0) ret=z ;
375      else ret=y ;
376      return ret ;
377    }
378
379    static inline CArray<double,1> cond_sfs(double x, const CArray<double,1>& y, double z)
380    {
381      CArray<double,1> ret(y.numElements()) ;
382     if (x==0) ret=z ;
383      else ret=y ;
384      return ret ;
385    }
386
387    static inline CArray<double,1> cond_sff(double x, const CArray<double,1>& y, const CArray<double,1>& z)
388    {
389      CArray<double,1> ret(y.numElements()) ;
390      if (x==0) ret=z ;
391      else ret=y ;
392      return ret ;
393    }
394   
395    static inline CArray<double,1> cond_fss(const CArray<double,1>& x, double y, double z)
396    {
397      CArray<double,1> ret(x.numElements()) ;
398      Array<double,1>::const_iterator itx=x.begin(),itxe=x.end();
399      Array<double,1>::iterator itret=ret.begin() ;
400     
401      for(;itx!=itxe;++itx,++itret) *itret=( (*itx==0)?z:y) ;
402      return ret ;
403    }
404
405    static inline CArray<double,1> cond_fsf(const CArray<double,1>& x, double y, const CArray<double,1>& z)
406    {
407      CArray<double,1> ret(x.numElements()) ;
408      Array<double,1>::const_iterator itx=x.begin(), itxe=x.end(), itz=z.begin();
409      Array<double,1>::iterator itret=ret.begin() ;
410      for(;itx!=itxe;++itx,++itz,++itret) *itret=( (*itx==0)?*itz:y) ;
411      return ret ;
412    }
413
414    static inline CArray<double,1> cond_ffs(const CArray<double,1>& x, const CArray<double,1>& y, double z)
415    {
416      CArray<double,1> ret(x.numElements()) ;
417      Array<double,1>::const_iterator itx=x.begin(), itxe=x.end(), ity=y.begin() ;
418      Array<double,1>::iterator itret=ret.begin() ;
419      for(;itx!=itxe;++itx,++ity,++itret) *itret=( (*itx==0)?z:*ity) ;
420      return ret ;
421    }
422
423    static inline CArray<double,1> cond_fff(const CArray<double,1>& x, const CArray<double,1>& y, const CArray<double,1>&  z)
424    {
425      CArray<double,1> ret(x.numElements()) ;
426      Array<double,1>::const_iterator itx=x.begin(), itxe=x.end(), ity=y.begin(), itz=z.begin() ;
427      Array<double,1>::iterator itret=ret.begin() ;
428      for(;itx!=itxe;++itx,++ity,++itz,++itret) *itret=( (*itx==0)?*itz:*ity) ;
429      return ret ;
430    }
431
432  };
433
434  extern COperatorExpr operatorExpr;
435}
436
437#endif
438
Note: See TracBrowser for help on using the repository browser.