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