Salome HOME
updated copyright message
[modules/med.git] / src / MEDCalculator / Swig / MEDCalculator.i
1 // Copyright (C) 2007-2023  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 %module MEDCalculator
22
23 %include std_vector.i
24 %include std_string.i
25
26 %include "MEDLoaderCommon.i"
27
28 %{
29 #include "MEDCalculatorBrowserLiteStruct.hxx"
30 #include "MEDCalculatorBrowserField.hxx"
31 #include "MEDCalculatorDBField.hxx"
32 #include "MEDCalculatorTypemaps.i"
33
34 using namespace MEDCoupling;
35 using namespace INTERP_KERNEL;
36 %}
37
38 %typemap(out) MEDCoupling::MEDCalculatorDBField*
39 {
40   $result=convertMEDCalculatorDBField($1,$owner);
41 }
42
43 %newobject MEDCoupling::MEDCalculatorDBField::operator+;
44 %newobject MEDCoupling::MEDCalculatorDBField::operator-;
45 %newobject MEDCoupling::MEDCalculatorDBField::operator*;
46 %newobject MEDCoupling::MEDCalculatorDBField::operator/;
47 %newobject MEDCoupling::MEDCalculatorDBFieldReal::__radd__;
48 %newobject MEDCoupling::MEDCalculatorDBFieldReal::__rsub__;
49 %newobject MEDCoupling::MEDCalculatorDBFieldReal::__rmul__;
50 %newobject MEDCoupling::MEDCalculatorDBFieldReal::__rdiv__;
51 %newobject MEDCoupling::MEDCalculatorDBFieldReal::__getitem__;
52 %newobject MEDCoupling::MEDCalculatorDBFieldReal::__setitem__;
53 %newobject MEDCoupling::MEDCalculatorDBFieldReal::operator();
54 %newobject MEDCoupling::MEDCalculatorDBFieldReal::buildCstFieldFromThis;
55 %newobject MEDCoupling::MEDCalculatorDBFieldReal::dot;
56 %newobject MEDCoupling::MEDCalculatorDBFieldReal::operator^;
57 %newobject MEDCoupling::MEDCalculatorDBFieldReal::crossProduct;
58 %newobject MEDCoupling::MEDCalculatorDBFieldReal::doublyContractedProduct;
59 %newobject MEDCoupling::MEDCalculatorDBFieldReal::determinant;
60 %newobject MEDCoupling::MEDCalculatorDBFieldReal::eigenValues;
61 %newobject MEDCoupling::MEDCalculatorDBFieldReal::eigenVectors;
62 %newobject MEDCoupling::MEDCalculatorDBFieldReal::inverse;
63 %newobject MEDCoupling::MEDCalculatorDBFieldReal::trace;
64 %newobject MEDCoupling::MEDCalculatorDBFieldReal::deviator;
65 %newobject MEDCoupling::MEDCalculatorDBFieldReal::magnitude;
66
67 %feature("unref") MEDCalculatorDBField "$this->decrRef();"
68
69 namespace MEDCoupling
70 {
71   class MEDCalculatorBrowserLiteStruct
72   {
73   public :
74     MEDCalculatorBrowserLiteStruct(const char *);
75     const MEDCalculatorBrowserField& getField(int) const;
76   };
77
78   class MEDCalculatorDBField : public RefCountObject
79     {
80     public:
81       virtual MEDCalculatorDBField *operator+(const MEDCalculatorDBField& other) const throw(INTERP_KERNEL::Exception);
82       MEDCalculatorDBField *operator+(double val) const throw(INTERP_KERNEL::Exception);
83       virtual MEDCalculatorDBField *operator-(const MEDCalculatorDBField& other) const throw(INTERP_KERNEL::Exception);
84       MEDCalculatorDBField *operator-(double val) const throw(INTERP_KERNEL::Exception);
85       virtual MEDCalculatorDBField *operator*(const MEDCalculatorDBField& other) const throw(INTERP_KERNEL::Exception);
86       MEDCalculatorDBField *operator*(double val) const throw(INTERP_KERNEL::Exception);
87       virtual MEDCalculatorDBField *operator/(const MEDCalculatorDBField& other) const throw(INTERP_KERNEL::Exception);
88       MEDCalculatorDBField *operator/(double val) const throw(INTERP_KERNEL::Exception);
89       virtual bool isEqual(const MEDCalculatorDBField& other, double precM, double precF) const;
90     };
91
92   class MEDCalculatorDBFieldReal : public MEDCalculatorDBField
93   {
94   public:
95     MEDCalculatorDBFieldReal(const MEDCalculatorBrowserField& ls);
96     ~MEDCalculatorDBFieldReal();
97     MEDCalculatorDBFieldReal *buildCstFieldFromThis(double val) const;
98     MEDCoupling::TypeOfField getType() const;
99     void fetchData() const throw(INTERP_KERNEL::Exception);
100     int getNumberOfSteps() const;
101     int getNumberOfFetchedSteps() const;
102     int getNumberOfComponents() const;
103     void setName(const char *name);
104     void setDescription(const char *descr);
105     std::string getInfoOnComponent(int i) const throw(INTERP_KERNEL::Exception);
106     void setInfoOnComponent(int i, const char *info) throw(INTERP_KERNEL::Exception);
107     void write(const char *fName, bool writeFromScratch) const throw(INTERP_KERNEL::Exception);
108     void display() const throw(INTERP_KERNEL::Exception);
109     void applyFunc(const char *func) throw(INTERP_KERNEL::Exception);
110     MEDCalculatorDBField *operator^(const MEDCalculatorDBFieldReal& other) const throw(INTERP_KERNEL::Exception);
111     MEDCalculatorDBField *dot(const MEDCalculatorDBFieldReal& other) const throw(INTERP_KERNEL::Exception);
112     MEDCalculatorDBField *crossProduct(const MEDCalculatorDBFieldReal& other) const throw(INTERP_KERNEL::Exception);
113     MEDCalculatorDBField *doublyContractedProduct() const throw(INTERP_KERNEL::Exception);
114     MEDCalculatorDBField *determinant() const throw(INTERP_KERNEL::Exception);
115     MEDCalculatorDBField *eigenValues() const throw(INTERP_KERNEL::Exception);
116     MEDCalculatorDBField *eigenVectors() const throw(INTERP_KERNEL::Exception);
117     MEDCalculatorDBField *inverse() const throw(INTERP_KERNEL::Exception);
118     MEDCalculatorDBField *trace() const throw(INTERP_KERNEL::Exception);
119     MEDCalculatorDBField *deviator() const throw(INTERP_KERNEL::Exception);
120     MEDCalculatorDBField *magnitude() const throw(INTERP_KERNEL::Exception);
121     %extend
122        {
123          MEDCalculatorDBField *__radd__(double val)
124          {
125            MEDCalculatorDBFieldCst tmp(val);
126            return tmp+(*self);
127          }
128
129          MEDCalculatorDBField *__rsub__(double val)
130          {
131            MEDCalculatorDBFieldCst tmp(val);
132            return tmp-(*self);
133          }
134
135          MEDCalculatorDBField *__rmul__(double val)
136          {
137            MEDCalculatorDBFieldCst tmp(val);
138            return tmp*(*self);
139          }
140
141          MEDCalculatorDBField *__rdiv__(double val)
142          {
143            MEDCalculatorDBFieldCst tmp(val);
144            return tmp/(*self);
145          }
146
147          MEDCalculatorDBFieldReal *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception)
148          {
149            if(!PyTuple_Check(obj))
150              throw INTERP_KERNEL::Exception("Unexpected type found : Tuple expected ! Should never happen !");
151            int sz=PyTuple_Size(obj);
152            if(sz!=3)
153              throw INTERP_KERNEL::Exception("Unexpected nb of element in slice : 3 expected !\n1st is for time, 2nd for geometrical entities and 3rd for components of field !");
154            MEDCalculatorDBRangeSelection tr,pr,cr;
155            PyObject *obj0=PyTuple_GetItem(obj,0);
156            convertPyObjToRS2(obj0,tr,"for 1st tuple element for time");
157            PyObject *obj1=PyTuple_GetItem(obj,1);
158            convertPyObjToRS2(obj1,pr,"for 2nd tuple element for geometrical entities");
159            PyObject *obj2=PyTuple_GetItem(obj,2);
160            convertPyObjToRS2(obj2,cr,"for 3rd tuple element for components of field");
161            return self->operator()(tr,pr,cr);
162          }
163
164          MEDCalculatorDBFieldReal *__setitem__(PyObject *obj, PyObject *val) throw(INTERP_KERNEL::Exception)
165          {
166            MEDCalculatorDBRangeSelection tr,pr,cr;
167            if(!PyTuple_Check(obj))
168              {
169                const char msg[]="Unexpected type found : Tuple expected or [:] !";
170                if(!PySlice_Check(obj))
171                  throw INTERP_KERNEL::Exception(msg);
172                Py_ssize_t strt,stp,step;
173                PySlice_GetIndices(obj,std::numeric_limits<int>::max(),&strt,&stp,&step);
174                if(strt!=0 || stp!=std::numeric_limits<int>::max() || step!=1)
175                  throw INTERP_KERNEL::Exception(msg);
176                tr.setAll(); pr.setAll(); cr.setAll();
177              }
178            else
179              {
180                int sz=PyTuple_Size(obj);
181                if(sz!=3)
182                  throw INTERP_KERNEL::Exception("Unexpected nb of element in slice : 3 expected !\n1st is for time, 2nd for geometrical entities and 3rd for components of field !");
183                PyObject *obj0=PyTuple_GetItem(obj,0);
184                convertPyObjToRS2(obj0,tr,"for 1st tuple element for time");
185                PyObject *obj1=PyTuple_GetItem(obj,1);
186                convertPyObjToRS2(obj1,pr,"for 2nd tuple element for geometrical entities");
187                PyObject *obj2=PyTuple_GetItem(obj,2);
188                convertPyObjToRS2(obj2,cr,"for 3rd tuple element for components of field");
189              }
190            MCAuto<MEDCalculatorDBFieldReal> ret=self->operator()(tr,pr,cr);
191            if(PyLong_Check(val))
192              {
193                (*ret)=double(PyLong_AS_LONG(val));
194                ret->incrRef();
195                return ret;
196              }
197            if(PyFloat_Check(val))
198              {
199                (*ret)=PyFloat_AS_DOUBLE(val);
200                ret->incrRef();
201                return ret;
202              }
203            void *argp;
204            int status=SWIG_ConvertPtr(val,&argp,SWIGTYPE_p_MEDCoupling__MEDCalculatorDBFieldReal,0|0);
205            if(!SWIG_IsOK(status))
206              {
207                throw INTERP_KERNEL::Exception("For setting only float, int or DBFieldReal supported !");
208              }
209           const MEDCoupling::MEDCalculatorDBFieldReal *arg=reinterpret_cast< const MEDCoupling::MEDCalculatorDBFieldReal * >(argp);
210           (*ret)=*arg;
211           ret->incrRef();
212           return ret;
213          }
214
215          MEDCalculatorDBFieldReal *operator()(PyObject *t, PyObject *p, PyObject *c) throw(INTERP_KERNEL::Exception)
216          {
217            MEDCalculatorDBRangeSelection tr,pr,cr;
218            convertPyObjToRS(t,tr);
219            convertPyObjToRS(p,pr);
220            convertPyObjToRS(c,cr);
221            return self->operator()(tr,pr,cr);
222          }
223
224          const MEDCalculatorDBFieldReal& assign(const MEDCalculatorDBFieldReal& other) throw(INTERP_KERNEL::Exception)
225          {
226            (*self)=other;
227            return *self;
228          }
229
230          const MEDCalculatorDBFieldReal& assign(const MEDCalculatorDBFieldReal& other, const char *name) throw(INTERP_KERNEL::Exception)
231          {
232            (*self)=other;
233            self->setName(name);
234            return *self;
235          }
236
237          const MEDCalculatorDBFieldReal& assign(double val) throw(INTERP_KERNEL::Exception)
238          {
239            (*self)=val;
240            return *self;
241          }
242
243          const MEDCalculatorDBFieldReal& assign(double val, const char *name) throw(INTERP_KERNEL::Exception)
244          {
245            (*self)=val;
246            self->setName(name);
247            return *self;
248          }
249
250          const MEDCalculatorDBFieldReal& assign(int val) throw(INTERP_KERNEL::Exception)
251          {
252            (*self)=(double) val;
253            return *self;
254          }
255
256          const MEDCalculatorDBFieldReal& assign(int val, const char *name) throw(INTERP_KERNEL::Exception)
257          {
258            (*self)=(double) val;
259            self->setName(name);
260            return *self;
261          }
262
263          PyObject *getFields() const throw(INTERP_KERNEL::Exception)
264          {
265            std::vector<MEDCouplingFieldDouble *> r=self->getFields();
266            PyObject *ret = PyList_New(r.size());
267            for(unsigned int i=0;i<r.size();i++)
268              {
269                MEDCouplingFieldDouble *tmp=r[i];
270                PyList_SetItem(ret,i,SWIG_NewPointerObj(SWIG_as_voidptr(tmp),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble, SWIG_POINTER_OWN | 0 ));
271              }
272            return ret;
273          }
274
275          PyObject *getValues() const throw(INTERP_KERNEL::Exception)
276          {
277            std::vector<MEDCouplingFieldDouble *> r=self->getFields();
278            PyObject *ret = PyList_New(r.size());
279            for(unsigned int i=0;i<r.size();i++)
280              {
281                MEDCouplingFieldDouble *tmp=r[i];
282                const double *valtmp=tmp->getArray()->getConstPointer();
283                int nbvals=tmp->getNumberOfValues();
284                PyObject *ret2=PyList_New(nbvals);
285                for(int j=0;j<nbvals;j++)
286                  PyList_SetItem(ret2,j,PyFloat_FromDouble(valtmp[j]));
287                PyList_SetItem(ret,i,ret2);
288                tmp->decrRef();
289              }
290            return ret;
291          }
292
293          std::string __str__() const
294          {
295            return self->simpleRepr();
296          }
297        }
298   };
299
300   class MEDCalculatorDBFieldCst : public MEDCalculatorDBField
301   {
302   public:
303     MEDCalculatorDBFieldCst(double val);
304     MEDCalculatorDBFieldCst(int val);
305     %extend
306     {
307       std::string __str__() const
308         {
309           std::ostringstream oss; oss << self->getValue();
310           return oss.str();
311         }
312     }
313   };
314 }
315
316 %pythoncode %{
317 def MEDCouplingDataArrayDoubleIadd(self,*args):
318     import _MEDCalculator
319     return _MEDCalculator.DataArrayDouble____iadd___(self, self, *args)
320 def MEDCouplingDataArrayDoubleIsub(self,*args):
321     import _MEDCalculator
322     return _MEDCalculator.DataArrayDouble____isub___(self, self, *args)
323 def MEDCouplingDataArrayDoubleImul(self,*args):
324     import _MEDCalculator
325     return _MEDCalculator.DataArrayDouble____imul___(self, self, *args)
326 def MEDCouplingDataArrayDoubleIdiv(self,*args):
327     import _MEDCalculator
328     return _MEDCalculator.DataArrayDouble____idiv___(self, self, *args)
329 def MEDCouplingDataArrayDoubleIpow(self,*args):
330     import _MEDCalculator
331     return _MEDCalculator.DataArrayDouble____ipow___(self, self, *args)
332 def MEDCouplingDataArrayDoubleTupleIadd(self,*args):
333     import _MEDCalculator
334     return _MEDCalculator.DataArrayDoubleTuple____iadd___(self, self, *args)
335 def MEDCouplingDataArrayDoubleTupleIsub(self,*args):
336     import _MEDCalculator
337     return _MEDCalculator.DataArrayDoubleTuple____isub___(self, self, *args)
338 def MEDCouplingDataArrayDoubleTupleImul(self,*args):
339     import _MEDCalculator
340     return _MEDCalculator.DataArrayDoubleTuple____imul___(self, self, *args)
341 def MEDCouplingDataArrayDoubleTupleIdiv(self,*args):
342     import _MEDCalculator
343     return _MEDCalculator.DataArrayDoubleTuple____idiv___(self, self, *args)
344 def MEDCouplingFieldDoubleIadd(self,*args):
345     import _MEDCalculator
346     return _MEDCalculator.MEDCouplingFieldDouble____iadd___(self, self, *args)
347 def MEDCouplingFieldDoubleIsub(self,*args):
348     import _MEDCalculator
349     return _MEDCalculator.MEDCouplingFieldDouble____isub___(self, self, *args)
350 def MEDCouplingFieldDoubleImul(self,*args):
351     import _MEDCalculator
352     return _MEDCalculator.MEDCouplingFieldDouble____imul___(self, self, *args)
353 def MEDCouplingFieldDoubleIdiv(self,*args):
354     import _MEDCalculator
355     return _MEDCalculator.MEDCouplingFieldDouble____idiv___(self, self, *args)
356 def MEDCouplingFieldDoubleIpow(self,*args):
357     import _MEDCalculator
358     return _MEDCalculator.MEDCouplingFieldDouble____ipow___(self, self, *args)
359 def MEDCouplingDataArrayIntIadd(self,*args):
360     import _MEDCalculator
361     return _MEDCalculator.DataArrayInt____iadd___(self, self, *args)
362 def MEDCouplingDataArrayIntIsub(self,*args):
363     import _MEDCalculator
364     return _MEDCalculator.DataArrayInt____isub___(self, self, *args)
365 def MEDCouplingDataArrayIntImul(self,*args):
366     import _MEDCalculator
367     return _MEDCalculator.DataArrayInt____imul___(self, self, *args)
368 def MEDCouplingDataArrayIntIdiv(self,*args):
369     import _MEDCalculator
370     return _MEDCalculator.DataArrayInt____idiv___(self, self, *args)
371 def MEDCouplingDataArrayIntImod(self,*args):
372     import _MEDCalculator
373     return _MEDCalculator.DataArrayInt____imod___(self, self, *args)
374 def MEDCouplingDataArrayIntIpow(self,*args):
375     import _MEDCalculator
376     return _MEDCalculator.DataArrayInt____ipow___(self, self, *args)
377 def MEDCouplingDataArrayIntTupleIadd(self,*args):
378     import _MEDCalculator
379     return _MEDCalculator.DataArrayIntTuple____iadd___(self, self, *args)
380 def MEDCouplingDataArrayIntTupleIsub(self,*args):
381     import _MEDCalculator
382     return _MEDCalculator.DataArrayIntTuple____isub___(self, self, *args)
383 def MEDCouplingDataArrayIntTupleImul(self,*args):
384     import _MEDCalculator
385     return _MEDCalculator.DataArrayIntTuple____imul___(self, self, *args)
386 def MEDCouplingDataArrayIntTupleIdiv(self,*args):
387     import _MEDCalculator
388     return _MEDCalculator.DataArrayIntTuple____idiv___(self, self, *args)
389 def MEDCouplingDataArrayIntTupleImod(self,*args):
390     import _MEDCalculator
391     return _MEDCalculator.DataArrayIntTuple____imod___(self, self, *args)
392 def MEDCouplingDenseMatrixIadd(self,*args):
393     import _MEDCalculator
394     return _MEDCalculator.DenseMatrix____iadd___(self, self, *args)
395 def MEDCouplingDenseMatrixIsub(self,*args):
396     import _MEDCalculator
397     return _MEDCalculator.DenseMatrix____isub___(self, self, *args)
398 %}
399
400 %include "MEDCouplingFinalize.i"
401
402 %pythoncode %{
403 MEDCalculatorDBFieldReal.__rtruediv__ = MEDCalculatorDBFieldReal.__rdiv__
404 def MEDCouplingMEDFileUMeshReduce(self):
405   return MEDCouplingStdReduceFunct,(MEDFileUMesh,((),(self.__getstate__()),))
406 MEDFileUMesh.__reduce__=MEDCouplingMEDFileUMeshReduce
407 del MEDCouplingMEDFileUMeshReduce
408 %}