Salome HOME
Copyright update 2020
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingTypemaps.i
1 // Copyright (C) 2007-2020  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 #ifndef __MEDCOUPLINGTYPEMAPS_I__
22 #define __MEDCOUPLINGTYPEMAPS_I__
23
24 #include "MEDCouplingDataArrayTypemaps.i"
25 #include "MEDCouplingUMesh.hxx"
26 #include "MEDCouplingCMesh.hxx"
27 #include "MEDCouplingIMesh.hxx"
28 #include "MEDCouplingCurveLinearMesh.hxx"
29 #include "MEDCouplingMappedExtrudedMesh.hxx"
30 #include "MEDCoupling1GTUMesh.hxx"
31 #include "MEDCouplingFieldDiscretization.hxx"
32 #include "MEDCouplingMultiFields.hxx"
33 #include "MEDCouplingPartDefinition.hxx"
34 #include "MEDCouplingCartesianAMRMesh.hxx"
35
36 #include <memory>
37
38 auto PyDeleter = [](PyObject *ob) { Py_DECREF(ob) ; };
39
40 class AutoPyPtr : std::unique_ptr<PyObject,decltype(PyDeleter)>
41 {
42   public:
43   AutoPyPtr(PyObject *ob):std::unique_ptr<PyObject,decltype(PyDeleter)>(ob,PyDeleter) { }
44   PyObject *retn() { return this->release(); }
45   operator PyObject *() const { return this->get(); }
46 };
47
48 AutoPyPtr convertMapStringInt(const std::map<std::string,mcIdType>& cpp)
49 {
50   AutoPyPtr ret(PyDict_New());
51   for(auto it : cpp)
52   {
53     AutoPyPtr st(PyString_FromString(it.first.c_str()));
54     AutoPyPtr val(PyInt_FromLong(it.second));
55     PyDict_SetItem(ret,st,val);
56   }
57   return ret;
58 }
59
60 AutoPyPtr convertMapStringVectString(const std::map<std::string,std::vector<std::string>>& cpp)
61 {
62   AutoPyPtr ret(PyDict_New());
63   for(auto it : cpp)
64   {
65     AutoPyPtr st(PyString_FromString(it.first.c_str()));
66     AutoPyPtr vec(PyList_New(it.second.size()));
67     std::size_t itc(0);
68     for(auto it2 : it.second)
69     {
70       AutoPyPtr st2(PyString_FromString(it2.c_str()));
71       PyList_SetItem(vec,itc++,st2.retn());
72     }
73     PyDict_SetItem(ret,st,vec.retn());
74   }
75   return ret;
76 }
77
78 static PyObject *convertMesh(MEDCoupling::MEDCouplingMesh *mesh, int owner)
79 {
80   PyObject *ret=0;
81   if(!mesh)
82     {
83       Py_XINCREF(Py_None);
84       return Py_None;
85     }
86   if(dynamic_cast<MEDCoupling::MEDCouplingUMesh *>(mesh))
87     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingUMesh,owner);
88   if(dynamic_cast<MEDCoupling::MEDCoupling1SGTUMesh *>(mesh))
89     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCoupling1SGTUMesh,owner);
90   if(dynamic_cast<MEDCoupling::MEDCoupling1DGTUMesh *>(mesh))
91     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCoupling1DGTUMesh,owner);
92   if(dynamic_cast<MEDCoupling::MEDCouplingMappedExtrudedMesh *>(mesh))
93     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingMappedExtrudedMesh,owner);
94   if(dynamic_cast<MEDCoupling::MEDCouplingCMesh *>(mesh))
95     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingCMesh,owner);
96   if(dynamic_cast<MEDCoupling::MEDCouplingCurveLinearMesh *>(mesh))
97     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingCurveLinearMesh,owner);
98   if(dynamic_cast<MEDCoupling::MEDCouplingIMesh *>(mesh))
99     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingIMesh,owner);
100   if(!ret)
101     throw INTERP_KERNEL::Exception("Not recognized type of mesh on downcast !");
102   return ret;
103 }
104
105 static PyObject *convertFieldDiscretization(MEDCoupling::MEDCouplingFieldDiscretization *fd, int owner)
106 {
107   PyObject *ret=0;
108   if(!fd)
109     {
110       Py_XINCREF(Py_None);
111       return Py_None;
112     }
113   if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationP0 *>(fd))
114     ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationP0,owner);
115   if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationP1 *>(fd))
116     ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationP1,owner);
117   if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationGauss *>(fd))
118     ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationGauss,owner);
119   if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationGaussNE *>(fd))
120     ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationGaussNE,owner);
121   if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationKriging *>(fd))
122     ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationKriging,owner);
123   if(!ret)
124     throw INTERP_KERNEL::Exception("Not recognized type of field discretization on downcast !");
125   return ret;
126 }
127
128 static PyObject *convertField(MEDCoupling::MEDCouplingField *f, int owner)
129 {
130   PyObject *ret(NULL);
131   if(!f)
132     {
133       Py_XINCREF(Py_None);
134       return Py_None;
135     }
136   if(dynamic_cast<MEDCoupling::MEDCouplingFieldDouble *>(f))
137     ret=SWIG_NewPointerObj(reinterpret_cast<void*>(f),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,owner);
138   if(dynamic_cast<MEDCoupling::MEDCouplingFieldInt *>(f))
139     ret=SWIG_NewPointerObj(reinterpret_cast<void*>(f),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldInt,owner);
140   if(dynamic_cast<MEDCoupling::MEDCouplingFieldFloat *>(f))
141     ret=SWIG_NewPointerObj(reinterpret_cast<void*>(f),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldFloat,owner);
142   if(!ret)
143     throw INTERP_KERNEL::Exception("Not recognized type of field on downcast !");
144   return ret;
145 }
146
147 static PyObject* convertMultiFields(MEDCoupling::MEDCouplingMultiFields *mfs, int owner)
148 {
149   PyObject *ret=0;
150   if(!mfs)
151     {
152       Py_XINCREF(Py_None);
153       return Py_None;
154     }
155   if(dynamic_cast<MEDCoupling::MEDCouplingFieldOverTime *>(mfs))
156     ret=SWIG_NewPointerObj((void*)mfs,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldOverTime,owner);
157   else
158     ret=SWIG_NewPointerObj((void*)mfs,SWIGTYPE_p_MEDCoupling__MEDCouplingMultiFields,owner);
159   return ret;
160 }
161
162 static PyObject *convertCartesianAMRMesh(MEDCoupling::MEDCouplingCartesianAMRMeshGen *mesh, int owner)
163 {
164   if(!mesh)
165     {
166       Py_XINCREF(Py_None);
167       return Py_None;
168     }
169   if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRMeshSub *>(mesh))
170     {
171       return SWIG_NewPointerObj(reinterpret_cast<void*>(mesh),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRMeshSub,owner);
172     }
173   if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRMesh *>(mesh))
174     {
175       return SWIG_NewPointerObj(reinterpret_cast<void*>(mesh),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRMesh,owner);
176     }
177   throw INTERP_KERNEL::Exception("convertCartesianAMRMesh wrap : unrecognized type of cartesian AMR mesh !");
178 }
179
180 static PyObject *convertDataForGodFather(MEDCoupling::MEDCouplingDataForGodFather *data, int owner)
181 {
182   if(!data)
183     {
184       Py_XINCREF(Py_None);
185       return Py_None;
186     }
187   if(dynamic_cast<MEDCoupling::MEDCouplingAMRAttribute *>(data))
188     {
189       return SWIG_NewPointerObj(reinterpret_cast<void*>(data),SWIGTYPE_p_MEDCoupling__MEDCouplingAMRAttribute,owner);
190     }
191   throw INTERP_KERNEL::Exception("convertDataForGodFather wrap : unrecognized data type for AMR !");
192 }
193
194 static PyObject *convertCartesianAMRPatch(MEDCoupling::MEDCouplingCartesianAMRPatchGen *patch, int owner)
195 {
196   if(!patch)
197     {
198       Py_XINCREF(Py_None);
199       return Py_None;
200     }
201   if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRPatchGF *>(patch))
202     {
203       return SWIG_NewPointerObj(reinterpret_cast<void*>(patch),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRPatchGF,owner);
204     }
205   if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRPatch *>(patch))
206     {
207       return SWIG_NewPointerObj(reinterpret_cast<void*>(patch),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRPatch,owner);
208     }
209   throw INTERP_KERNEL::Exception("convertCartesianAMRPatch wrap : unrecognized type of cartesian AMR patch !");
210 }
211
212 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___add__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
213 {
214   const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__add__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double.";
215   const char msg2[]="in MEDCouplingFieldDouble.__add__ : self field has no Array of values set !";
216   void *argp;
217   //
218   if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
219     {
220       MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
221       if(other)
222         return (*self)+(*other);
223       else
224         throw INTERP_KERNEL::Exception(msg);
225     }
226   //
227   double val;
228   MEDCoupling::DataArrayDouble *a;
229   MEDCoupling::DataArrayDoubleTuple *aa;
230   std::vector<double> bb;
231   mcIdType sw;
232   convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
233   switch(sw)
234     {
235     case 1:
236       {
237         if(!self->getArray())
238           throw INTERP_KERNEL::Exception(msg2);
239         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=self->getArray()->deepCopy();
240         ret->applyLin(1.,val);
241         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
242         ret2->setArray(ret);
243         return ret2.retn();
244       }
245     case 2:
246       {
247         if(!self->getArray())
248           throw INTERP_KERNEL::Exception(msg2);
249         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Add(self->getArray(),a);
250         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
251         ret2->setArray(ret);
252         return ret2.retn();
253       }
254     case 3:
255       {
256         if(!self->getArray())
257           throw INTERP_KERNEL::Exception(msg2);
258         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
259         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Add(self->getArray(),aaa);
260         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
261         ret2->setArray(ret);
262         return ret2.retn();
263       }
264     case 4:
265       {
266         if(!self->getArray())
267           throw INTERP_KERNEL::Exception(msg2);
268         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=MEDCoupling::DataArrayDouble::New(); aaa->useArray(&bb[0],false,MEDCoupling::DeallocType::CPP_DEALLOC,1,(int)bb.size());
269         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Add(self->getArray(),aaa);
270         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
271         ret2->setArray(ret);
272         return ret2.retn();
273       }
274     default:
275       { throw INTERP_KERNEL::Exception(msg); }
276     }
277 }
278
279 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___radd__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
280 {
281   return MEDCoupling_MEDCouplingFieldDouble___add__Impl(self,obj);
282 }
283
284 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___rsub__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
285 {
286   const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__rsub__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double.";
287   const char msg2[]="in MEDCouplingFieldDouble.__rsub__ : self field has no Array of values set !";
288   void *argp;
289   //
290   if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
291     {
292       MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
293       if(other)
294         return (*other)-(*self);
295       else
296         throw INTERP_KERNEL::Exception(msg);
297     }
298   //
299   double val;
300   MEDCoupling::DataArrayDouble *a;
301   MEDCoupling::DataArrayDoubleTuple *aa;
302   std::vector<double> bb;
303   mcIdType sw;
304   convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
305   switch(sw)
306     {
307     case 1:
308       {
309         if(!self->getArray())
310           throw INTERP_KERNEL::Exception(msg2);
311         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=self->getArray()->deepCopy();
312         ret->applyLin(-1.,val);
313         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
314         ret2->setArray(ret);
315         return ret2.retn();
316       }
317     case 2:
318       {
319         if(!self->getArray())
320           throw INTERP_KERNEL::Exception(msg2);
321         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Substract(a,self->getArray());
322         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
323         ret2->setArray(ret);
324         return ret2.retn();
325       }
326     case 3:
327       {
328         if(!self->getArray())
329           throw INTERP_KERNEL::Exception(msg2);
330         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
331         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Substract(aaa,self->getArray());
332         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
333         ret2->setArray(ret);
334         return ret2.retn();
335       }
336     case 4:
337       {
338         if(!self->getArray())
339           throw INTERP_KERNEL::Exception(msg2);
340         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=MEDCoupling::DataArrayDouble::New(); aaa->useArray(&bb[0],false,MEDCoupling::DeallocType::CPP_DEALLOC,1,(int)bb.size());
341         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Substract(aaa,self->getArray());
342         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
343         ret2->setArray(ret);
344         return ret2.retn();
345       }
346     default:
347       { throw INTERP_KERNEL::Exception(msg); }
348     }
349 }
350
351 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___mul__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
352 {
353   const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__mul__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double.";
354   const char msg2[]="in MEDCouplingFieldDouble.__mul__ : self field has no Array of values set !";
355   void *argp;
356   //
357   if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
358     {
359       MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
360       if(other)
361         return (*self)*(*other);
362       else
363         throw INTERP_KERNEL::Exception(msg);
364     }
365   //
366   double val;
367   MEDCoupling::DataArrayDouble *a;
368   MEDCoupling::DataArrayDoubleTuple *aa;
369   std::vector<double> bb;
370   mcIdType sw;
371   convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
372   switch(sw)
373     {
374     case 1:
375       {
376         if(!self->getArray())
377           throw INTERP_KERNEL::Exception(msg2);
378         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=self->getArray()->deepCopy();
379         ret->applyLin(val,0.);
380         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
381         ret2->setArray(ret);
382         return ret2.retn();
383       }
384     case 2:
385       {
386         if(!self->getArray())
387           throw INTERP_KERNEL::Exception(msg2);
388         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Multiply(self->getArray(),a);
389         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
390         ret2->setArray(ret);
391         return ret2.retn();
392       }
393     case 3:
394       {
395         if(!self->getArray())
396           throw INTERP_KERNEL::Exception(msg2);
397         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
398         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Multiply(self->getArray(),aaa);
399         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
400         ret2->setArray(ret);
401         return ret2.retn();
402       }
403     case 4:
404       {
405         if(!self->getArray())
406           throw INTERP_KERNEL::Exception(msg2);
407         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=MEDCoupling::DataArrayDouble::New(); aaa->useArray(&bb[0],false,MEDCoupling::DeallocType::CPP_DEALLOC,1,(int)bb.size());
408         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Multiply(self->getArray(),aaa);
409         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
410         ret2->setArray(ret);
411         return ret2.retn();
412       }
413     default:
414       { throw INTERP_KERNEL::Exception(msg); }
415     }
416 }
417
418 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___rmul__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
419 {
420   return MEDCoupling_MEDCouplingFieldDouble___mul__Impl(self,obj);
421 }
422
423 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___rdiv__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
424 {
425   const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__rdiv__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double.";
426   const char msg2[]="in MEDCouplingFieldDouble.__div__ : self field has no Array of values set !";
427   void *argp;
428   //
429   if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
430     {
431       MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
432       if(other)
433         return (*other)/(*self);
434       else
435         throw INTERP_KERNEL::Exception(msg);
436     }
437   //
438   double val;
439   MEDCoupling::DataArrayDouble *a;
440   MEDCoupling::DataArrayDoubleTuple *aa;
441   std::vector<double> bb;
442   mcIdType sw;
443   convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
444   switch(sw)
445     {
446     case 1:
447       {
448         if(!self->getArray())
449           throw INTERP_KERNEL::Exception(msg2);
450         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=self->getArray()->deepCopy();
451         ret->applyInv(val);
452         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
453         ret2->setArray(ret);
454         return ret2.retn();
455       }
456     case 2:
457       {
458         if(!self->getArray())
459           throw INTERP_KERNEL::Exception(msg2);
460         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Divide(a,self->getArray());
461         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
462         ret2->setArray(ret);
463         return ret2.retn();
464       }
465     case 3:
466       {
467         if(!self->getArray())
468           throw INTERP_KERNEL::Exception(msg2);
469         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
470         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Divide(aaa,self->getArray());
471         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
472         ret2->setArray(ret);
473         return ret2.retn();
474       }
475     case 4:
476       {
477         if(!self->getArray())
478           throw INTERP_KERNEL::Exception(msg2);
479         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=MEDCoupling::DataArrayDouble::New(); aaa->useArray(&bb[0],false,MEDCoupling::DeallocType::CPP_DEALLOC,1,(int)bb.size());
480         MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Divide(aaa,self->getArray());
481         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
482         ret2->setArray(ret);
483         return ret2.retn();
484       }
485     default:
486       { throw INTERP_KERNEL::Exception(msg); }
487     }
488 }
489
490 template<class T>
491 typename MEDCoupling::Traits<T>::FieldType *fieldT_buildSubPart(const MEDCoupling::MEDCouplingFieldT<T> *self, PyObject *li)
492 {
493   mcIdType sw;
494   mcIdType singleVal;
495   std::vector<mcIdType> multiVal;
496   std::pair<mcIdType, std::pair<mcIdType,mcIdType> > slic;
497   MEDCoupling::DataArrayIdType *daIntTyypp=0;
498   const MEDCoupling::MEDCouplingMesh *mesh=self->getMesh();
499   if(!mesh)
500     throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : field lies on a null mesh !");
501   mcIdType nbc=mesh->getNumberOfCells();
502   convertIntStarOrSliceLikePyObjToCpp(li,nbc,sw,singleVal,multiVal,slic,daIntTyypp);
503   switch(sw)
504     {
505     case 1:
506       {
507         if(singleVal>=nbc)
508           {
509             std::ostringstream oss;
510             oss << "Requesting for cell id " << singleVal << " having only " << nbc << " cells !";
511             throw INTERP_KERNEL::Exception(oss.str().c_str());
512           }
513         if(singleVal>=0)
514           return self->buildSubPart(&singleVal,&singleVal+1);
515         else
516           {
517             if(nbc+singleVal>0)
518               {
519                 mcIdType tmp=nbc+singleVal;
520                 return self->buildSubPart(&tmp,&tmp+1);
521               }
522             else
523               {
524                 std::ostringstream oss;
525                 oss << "Requesting for cell id " << singleVal << " having only " << nbc << " cells !";
526                 throw INTERP_KERNEL::Exception(oss.str().c_str());
527               }
528           }
529       }
530     case 2:
531       {
532         return self->buildSubPart(&multiVal[0],&multiVal[0]+multiVal.size());
533       }
534     case 3:
535       {
536         return self->buildSubPartRange(slic.first,slic.second.first,slic.second.second);
537       }
538     case 4:
539       {
540         if(!daIntTyypp)
541           throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : null instance has been given in input !");
542         daIntTyypp->checkAllocated();
543         return self->buildSubPart(daIntTyypp->begin(),daIntTyypp->end());
544       }
545     default:
546       throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : unrecognized type in input ! Possibilities are : int, list or tuple of int DataArrayIdType instance !");
547     }
548 }
549
550 template<class T>
551 typename MEDCoupling::Traits<T>::FieldType *fieldT__getitem__(const MEDCoupling::MEDCouplingFieldT<T> *self, PyObject *li)
552 {
553   const char msg[]="MEDCouplingFieldDouble::__getitem__ : invalid call  Available API are : \n-myField[DataArrayIdType]\n-myField[slice]\n-myField[pythonListOfCellIds]\n-myField[integer]\n-myField[DataArrayIdType,1]\n-myField[slice,1]\n-myField[pythonListOfCellIds,1]\n-myField[integer,1]\n";
554   if(PyTuple_Check(li))
555     {
556       Py_ssize_t sz=PyTuple_Size(li);
557       if(sz!=2)
558         throw INTERP_KERNEL::Exception(msg);
559       PyObject *elt0=PyTuple_GetItem(li,0),*elt1=PyTuple_GetItem(li,1);
560       mcIdType sw;
561       std::size_t singleVal;
562       std::vector<std::size_t> multiVal;
563       std::pair<mcIdType, std::pair<mcIdType,mcIdType> > slic;
564       MEDCoupling::DataArrayIdType *daIntTyypp=0;
565       if(!self->getArray())
566         throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::__getitem__ : no array set on field to deduce number of components !");
567       try
568       { convertIntStarOrSliceLikePyObjToCpp(elt1,ToIdType(self->getArray()->getNumberOfComponents()),sw,singleVal,multiVal,slic,daIntTyypp); }
569       catch(INTERP_KERNEL::Exception& e)
570         { std::ostringstream oss; oss << "MEDCouplingFieldDouble::__getitem__ : invalid type in 2nd parameter (compo) !" << e.what(); throw INTERP_KERNEL::Exception(oss.str().c_str()); }
571       typename MEDCoupling::MCAuto< typename MEDCoupling::Traits<T>::FieldType > ret0(fieldT_buildSubPart<T>(self,elt0));
572       typename MEDCoupling::Traits<T>::ArrayType *ret0Arr=ret0->getArray();
573       if(!ret0Arr)
574         throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::__getitem__ : no array exists to apply restriction on component on it !");
575       switch(sw)
576         {
577         case 1:
578           {
579             std::vector<std::size_t> v2(1,singleVal);
580             MEDCoupling::MCAuto< typename MEDCoupling::Traits<T>::ArrayType > aarr(ret0Arr->keepSelectedComponents(v2));
581             ret0->setArray(aarr);
582             return ret0.retn();
583           }
584         case 2:
585           {
586             MEDCoupling::MCAuto< typename MEDCoupling::Traits<T>::ArrayType > aarr(ret0Arr->keepSelectedComponents(multiVal));
587             ret0->setArray(aarr);
588             return ret0.retn();
589           }
590         case 3:
591           {
592             mcIdType nbOfComp(MEDCoupling::DataArray::GetNumberOfItemGivenBESRelative(slic.first,slic.second.first,slic.second.second,"MEDCouplingFieldDouble::__getitem__ : invalid range in 2nd parameter (components) !"));
593             std::vector<std::size_t> v2(nbOfComp);
594             for(mcIdType i=0;i<nbOfComp;i++)
595               v2[i]=slic.first+i*slic.second.second;
596             MEDCoupling::MCAuto< typename MEDCoupling::Traits<T>::ArrayType > aarr(ret0Arr->keepSelectedComponents(v2));
597             ret0->setArray(aarr);
598             return ret0.retn();
599           }
600         default:
601           throw INTERP_KERNEL::Exception(msg);
602         }
603     }
604   else
605     return fieldT_buildSubPart<T>(self,li);
606 }
607
608 template<class FIELDT>
609 PyObject *field_getTinySerializationInformation(const FIELDT *self)
610 {
611   std::vector<double> a0;
612   std::vector<mcIdType> a1;
613   std::vector<std::string> a2;
614   self->getTinySerializationDbleInformation(a0);
615   self->getTinySerializationIntInformation(a1);
616   self->getTinySerializationStrInformation(a2);
617   //
618   PyObject *ret(PyTuple_New(3));
619   PyTuple_SetItem(ret,0,convertDblArrToPyList2(a0));
620   PyTuple_SetItem(ret,1,convertIntArrToPyList2(a1));
621   std::size_t sz(a2.size());
622   PyObject *ret2(PyList_New(sz));
623   {
624     for(std::size_t i=0;i<sz;i++)
625       PyList_SetItem(ret2,i,PyString_FromString(a2[i].c_str()));
626   }
627   PyTuple_SetItem(ret,2,ret2);
628   return ret;
629 }
630
631 template<class T>
632 PyObject *field_serialize(const typename MEDCoupling::Traits<T>::FieldType *self)
633 {
634   MEDCoupling::DataArrayIdType *ret0(0);
635   std::vector<typename MEDCoupling::Traits<T>::ArrayType *> ret1;
636   self->serialize(ret0,ret1);
637   if(ret0)
638     ret0->incrRef();
639   std::size_t sz(ret1.size());
640   PyObject *ret(PyTuple_New(2));
641   PyTuple_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(ret0),SWIGTITraits<mcIdType>::TI, SWIG_POINTER_OWN | 0 ));
642   PyObject *ret1Py(PyList_New(sz));
643   for(std::size_t i=0;i<sz;i++)
644     {
645       if(ret1[i])
646         ret1[i]->incrRef();
647       PyList_SetItem(ret1Py,i,convertArray(ret1[i],SWIG_POINTER_OWN | 0));
648     }
649   PyTuple_SetItem(ret,1,ret1Py);
650   return ret;
651 }
652
653 template<class FIELDT>
654 PyObject *field__getnewargs__(FIELDT *self)
655 {
656   self->checkConsistencyLight();
657   PyObject *ret(PyTuple_New(1));
658   PyObject *ret0(PyDict_New());
659   {
660     PyObject *a(PyInt_FromLong(0)),*b(PyInt_FromLong(self->getTypeOfField())),*c(PyInt_FromLong(self->getTimeDiscretization()));
661     PyObject *d(PyTuple_New(2)); PyTuple_SetItem(d,0,b); PyTuple_SetItem(d,1,c);
662     PyDict_SetItem(ret0,a,d);
663     Py_DECREF(a); Py_DECREF(d);
664   }
665   PyTuple_SetItem(ret,0,ret0);
666   return ret;
667 }
668
669 template<class FIELDT>
670 PyObject *field__getstate__(const FIELDT *self, PyObject *(*tinyserial)(const FIELDT *), PyObject *(*bigserial)(const FIELDT *))
671 {
672   self->checkConsistencyLight();
673   PyObject *ret0(tinyserial(self));
674   PyObject *ret1(bigserial(self));
675   const MEDCoupling::MEDCouplingMesh *mesh(self->getMesh());
676   if(mesh)
677     mesh->incrRef();
678   PyObject *ret(PyTuple_New(3));
679   PyTuple_SetItem(ret,0,ret0);
680   PyTuple_SetItem(ret,1,ret1);
681   PyTuple_SetItem(ret,2,convertMesh(const_cast<MEDCoupling::MEDCouplingMesh *>(mesh),SWIG_POINTER_OWN | 0 ));
682   return ret;
683 }
684
685 template<class T>
686 void field__setstate__(typename MEDCoupling::Traits<T>::FieldType *self, PyObject *inp)
687 {
688   static const char MSG[]="MEDCouplingFieldDouble.__setstate__ : expected input is a tuple of size 3 !";
689   if(!PyTuple_Check(inp))
690     throw INTERP_KERNEL::Exception(MSG);
691   std::size_t sz(PyTuple_Size(inp));
692   if(sz!=3)
693     throw INTERP_KERNEL::Exception(MSG);
694   // mesh
695   PyObject *elt2(PyTuple_GetItem(inp,2));
696   void *argp=0;
697   int status(SWIG_ConvertPtr(elt2,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingMesh,0|0));
698   if(!SWIG_IsOK(status))
699     throw INTERP_KERNEL::Exception(MSG);
700   self->setMesh(reinterpret_cast< const MEDCoupling::MEDCouplingMesh * >(argp));
701   //
702   PyObject *elt0(PyTuple_GetItem(inp,0));
703   PyObject *elt1(PyTuple_GetItem(inp,1));
704   std::vector<double> a0;
705   std::vector<mcIdType> a1;
706   std::vector<std::string> a2;
707   MEDCoupling::DataArrayIdType *b0(0);
708   std::vector<typename MEDCoupling::Traits<T>::ArrayType *>b1;
709   {
710     if(!PyTuple_Check(elt0) && PyTuple_Size(elt0)!=3)
711       throw INTERP_KERNEL::Exception(MSG);
712     PyObject *a0py(PyTuple_GetItem(elt0,0)),*a1py(PyTuple_GetItem(elt0,1)),*a2py(PyTuple_GetItem(elt0,2));
713     mcIdType tmp(-1);
714     fillArrayWithPyListDbl3(a0py,tmp,a0);
715     convertPyToNewIntArr3(a1py,a1);
716     fillStringVector(a2py,a2);
717   }
718   {
719     if(!PyTuple_Check(elt1) && PyTuple_Size(elt1)!=2)
720       throw INTERP_KERNEL::Exception(MSG);
721     PyObject *b0py(PyTuple_GetItem(elt1,0)),*b1py(PyTuple_GetItem(elt1,1));
722     void *argp(0);
723     int status(SWIG_ConvertPtr(b0py,&argp,SWIGTITraits<mcIdType>::TI,0|0));
724     if(!SWIG_IsOK(status))
725       throw INTERP_KERNEL::Exception(MSG);
726     b0=reinterpret_cast<MEDCoupling::DataArrayIdType *>(argp);
727     convertFromPyObjVectorOfObj<typename MEDCoupling::Traits<T>::ArrayType *>(b1py,SWIGTITraits<T>::TI,MEDCoupling::Traits<T>::ArrayTypeName,b1);
728   }
729   self->checkForUnserialization(a1,b0,b1);
730   // useless here to call resizeForUnserialization because arrays are well resized.
731   self->finishUnserialization(a1,a0,a2);
732 }
733
734 PyObject *Mesh_getCellsContainingPointsLike(PyObject *p, double eps, const MEDCoupling::MEDCouplingMesh *self, std::function<void(const double *,mcIdType,double,MEDCoupling::MCAuto<MEDCoupling::DataArrayIdType>&,MEDCoupling::MCAuto<MEDCoupling::DataArrayIdType>&)> func)
735 {
736   MEDCoupling::MCAuto<MEDCoupling::DataArrayIdType> elts,eltsIndex;
737   int spaceDim=self->getSpaceDimension();
738   void *da=0;
739   int res1=SWIG_ConvertPtr(p,&da,SWIGTYPE_p_MEDCoupling__DataArrayDouble, 0 |  0 );
740   if (!SWIG_IsOK(res1))
741     {
742       mcIdType size;
743       INTERP_KERNEL::AutoCPtr<double> tmp=convertPyToNewDblArr2(p,&size);
744       mcIdType nbOfPoints=size/spaceDim;
745       if(size%spaceDim!=0)
746         {
747           throw INTERP_KERNEL::Exception("MEDCouplingMesh::getCellsContainingPoints : Invalid list length ! Must be a multiple of self.getSpaceDimension() !");
748         }
749       func(tmp,nbOfPoints,eps,elts,eltsIndex);
750     }
751   else
752     {
753       MEDCoupling::DataArrayDouble *da2=reinterpret_cast< MEDCoupling::DataArrayDouble * >(da);
754       if(!da2)
755         throw INTERP_KERNEL::Exception("MEDCouplingMesh::getCellsContainingPoints : Not null DataArrayDouble instance expected !");
756       da2->checkAllocated();
757       mcIdType size=da2->getNumberOfTuples();
758       mcIdType nbOfCompo=ToIdType(da2->getNumberOfComponents());
759       if(nbOfCompo!=spaceDim)
760         {
761           throw INTERP_KERNEL::Exception("MEDCouplingMesh::getCellsContainingPoints : Invalid DataArrayDouble nb of components ! Expected same as self.getSpaceDimension() !");
762         }
763       func(da2->getConstPointer(),size,eps,elts,eltsIndex);
764     }
765   PyObject *ret=PyTuple_New(2);
766   PyTuple_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(elts.retn()),SWIGTITraits<mcIdType>::TI, SWIG_POINTER_OWN | 0 ));
767   PyTuple_SetItem(ret,1,SWIG_NewPointerObj(SWIG_as_voidptr(eltsIndex.retn()),SWIGTITraits<mcIdType>::TI, SWIG_POINTER_OWN | 0 ));
768   return ret;
769 }
770
771 #endif