1 // Copyright (C) 2007-2024 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
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 "MEDCouplingFieldDiscretizationOnNodesFE.hxx"
32 #include "MEDCouplingMultiFields.hxx"
33 #include "MEDCouplingPartDefinition.hxx"
34 #include "MEDCouplingCartesianAMRMesh.hxx"
38 auto PyDeleter = [](PyObject *ob) { Py_DECREF(ob) ; };
40 namespace { // Only in this TU, so in an anonymous namespace
41 class AutoPyPtr : std::unique_ptr<PyObject,decltype(PyDeleter)>
44 AutoPyPtr(PyObject *ob):std::unique_ptr<PyObject,decltype(PyDeleter)>(ob,PyDeleter) { }
45 PyObject *retn() { return this->release(); }
46 operator PyObject *() const { return this->get(); }
50 AutoPyPtr convertMapStringInt(const std::map<std::string,mcIdType>& cpp)
52 AutoPyPtr ret(PyDict_New());
55 AutoPyPtr st(PyString_FromString(it.first.c_str()));
56 AutoPyPtr val(PyInt_FromLong(it.second));
57 PyDict_SetItem(ret,st,val);
62 AutoPyPtr convertMapStringVectString(const std::map<std::string,std::vector<std::string>>& cpp)
64 AutoPyPtr ret(PyDict_New());
67 AutoPyPtr st(PyString_FromString(it.first.c_str()));
68 AutoPyPtr vec(PyList_New(it.second.size()));
70 for(auto it2 : it.second)
72 AutoPyPtr st2(PyString_FromString(it2.c_str()));
73 PyList_SetItem(vec,itc++,st2.retn());
75 PyDict_SetItem(ret,st,vec.retn());
80 static PyObject *convertMesh(MEDCoupling::MEDCouplingMesh *mesh, int owner)
88 if(dynamic_cast<MEDCoupling::MEDCouplingUMesh *>(mesh))
89 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingUMesh,owner);
90 if(dynamic_cast<MEDCoupling::MEDCoupling1SGTUMesh *>(mesh))
91 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCoupling1SGTUMesh,owner);
92 if(dynamic_cast<MEDCoupling::MEDCoupling1DGTUMesh *>(mesh))
93 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCoupling1DGTUMesh,owner);
94 if(dynamic_cast<MEDCoupling::MEDCouplingMappedExtrudedMesh *>(mesh))
95 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingMappedExtrudedMesh,owner);
96 if(dynamic_cast<MEDCoupling::MEDCouplingCMesh *>(mesh))
97 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingCMesh,owner);
98 if(dynamic_cast<MEDCoupling::MEDCouplingCurveLinearMesh *>(mesh))
99 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingCurveLinearMesh,owner);
100 if(dynamic_cast<MEDCoupling::MEDCouplingIMesh *>(mesh))
101 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDCouplingIMesh,owner);
103 throw INTERP_KERNEL::Exception("Not recognized type of mesh on downcast !");
107 static PyObject *convertFieldDiscretization(MEDCoupling::MEDCouplingFieldDiscretization *fd, int owner)
115 if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationP0 *>(fd))
116 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationP0,owner);
117 if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationP1 *>(fd))
118 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationP1,owner);
119 if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationGauss *>(fd))
120 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationGauss,owner);
121 if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationGaussNE *>(fd))
122 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationGaussNE,owner);
123 if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationKriging *>(fd))
124 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationKriging,owner);
125 if(dynamic_cast<MEDCoupling::MEDCouplingFieldDiscretizationOnNodesFE *>(fd))
126 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDiscretizationOnNodesFE,owner);
128 throw INTERP_KERNEL::Exception("Not recognized type of field discretization on downcast !");
132 static PyObject *convertField(MEDCoupling::MEDCouplingField *f, int owner)
140 if(dynamic_cast<MEDCoupling::MEDCouplingFieldDouble *>(f))
141 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(f),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,owner);
142 if(dynamic_cast<MEDCoupling::MEDCouplingFieldInt32 *>(f))
143 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(f),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldInt32,owner);
144 if(dynamic_cast<MEDCoupling::MEDCouplingFieldInt64 *>(f))
145 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(f),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldInt64,owner);
146 if(dynamic_cast<MEDCoupling::MEDCouplingFieldFloat *>(f))
147 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(f),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldFloat,owner);
149 throw INTERP_KERNEL::Exception("Not recognized type of field on downcast !");
153 static PyObject* convertMultiFields(MEDCoupling::MEDCouplingMultiFields *mfs, int owner)
161 if(dynamic_cast<MEDCoupling::MEDCouplingFieldOverTime *>(mfs))
162 ret=SWIG_NewPointerObj((void*)mfs,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldOverTime,owner);
164 ret=SWIG_NewPointerObj((void*)mfs,SWIGTYPE_p_MEDCoupling__MEDCouplingMultiFields,owner);
168 static PyObject *convertCartesianAMRMesh(MEDCoupling::MEDCouplingCartesianAMRMeshGen *mesh, int owner)
175 if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRMeshSub *>(mesh))
177 return SWIG_NewPointerObj(reinterpret_cast<void*>(mesh),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRMeshSub,owner);
179 if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRMesh *>(mesh))
181 return SWIG_NewPointerObj(reinterpret_cast<void*>(mesh),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRMesh,owner);
183 throw INTERP_KERNEL::Exception("convertCartesianAMRMesh wrap : unrecognized type of cartesian AMR mesh !");
186 static PyObject *convertDataForGodFather(MEDCoupling::MEDCouplingDataForGodFather *data, int owner)
193 if(dynamic_cast<MEDCoupling::MEDCouplingAMRAttribute *>(data))
195 return SWIG_NewPointerObj(reinterpret_cast<void*>(data),SWIGTYPE_p_MEDCoupling__MEDCouplingAMRAttribute,owner);
197 throw INTERP_KERNEL::Exception("convertDataForGodFather wrap : unrecognized data type for AMR !");
200 static PyObject *convertCartesianAMRPatch(MEDCoupling::MEDCouplingCartesianAMRPatchGen *patch, int owner)
207 if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRPatchGF *>(patch))
209 return SWIG_NewPointerObj(reinterpret_cast<void*>(patch),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRPatchGF,owner);
211 if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRPatch *>(patch))
213 return SWIG_NewPointerObj(reinterpret_cast<void*>(patch),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRPatch,owner);
215 throw INTERP_KERNEL::Exception("convertCartesianAMRPatch wrap : unrecognized type of cartesian AMR patch !");
218 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___add__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
220 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.";
221 const char msg2[]="in MEDCouplingFieldDouble.__add__ : self field has no Array of values set !";
224 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
226 MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
228 return (*self)+(*other);
230 throw INTERP_KERNEL::Exception(msg);
234 MEDCoupling::DataArrayDouble *a;
235 MEDCoupling::DataArrayDoubleTuple *aa;
236 std::vector<double> bb;
238 convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
243 if(!self->getArray())
244 throw INTERP_KERNEL::Exception(msg2);
245 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=self->getArray()->deepCopy();
246 ret->applyLin(1.,val);
247 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
253 if(!self->getArray())
254 throw INTERP_KERNEL::Exception(msg2);
255 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Add(self->getArray(),a);
256 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
262 if(!self->getArray())
263 throw INTERP_KERNEL::Exception(msg2);
264 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
265 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Add(self->getArray(),aaa);
266 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
272 if(!self->getArray())
273 throw INTERP_KERNEL::Exception(msg2);
274 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=MEDCoupling::DataArrayDouble::New(); aaa->useArray(&bb[0],false,MEDCoupling::DeallocType::CPP_DEALLOC,1,(int)bb.size());
275 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Add(self->getArray(),aaa);
276 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
281 { throw INTERP_KERNEL::Exception(msg); }
285 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___radd__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
287 return MEDCoupling_MEDCouplingFieldDouble___add__Impl(self,obj);
290 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___rsub__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
292 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.";
293 const char msg2[]="in MEDCouplingFieldDouble.__rsub__ : self field has no Array of values set !";
296 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
298 MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
300 return (*other)-(*self);
302 throw INTERP_KERNEL::Exception(msg);
306 MEDCoupling::DataArrayDouble *a;
307 MEDCoupling::DataArrayDoubleTuple *aa;
308 std::vector<double> bb;
310 convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
315 if(!self->getArray())
316 throw INTERP_KERNEL::Exception(msg2);
317 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=self->getArray()->deepCopy();
318 ret->applyLin(-1.,val);
319 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
325 if(!self->getArray())
326 throw INTERP_KERNEL::Exception(msg2);
327 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Substract(a,self->getArray());
328 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
334 if(!self->getArray())
335 throw INTERP_KERNEL::Exception(msg2);
336 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
337 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Substract(aaa,self->getArray());
338 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
344 if(!self->getArray())
345 throw INTERP_KERNEL::Exception(msg2);
346 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=MEDCoupling::DataArrayDouble::New(); aaa->useArray(&bb[0],false,MEDCoupling::DeallocType::CPP_DEALLOC,1,(int)bb.size());
347 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Substract(aaa,self->getArray());
348 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
353 { throw INTERP_KERNEL::Exception(msg); }
357 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___mul__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
359 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.";
360 const char msg2[]="in MEDCouplingFieldDouble.__mul__ : self field has no Array of values set !";
363 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
365 MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
367 return (*self)*(*other);
369 throw INTERP_KERNEL::Exception(msg);
373 MEDCoupling::DataArrayDouble *a;
374 MEDCoupling::DataArrayDoubleTuple *aa;
375 std::vector<double> bb;
377 convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
382 if(!self->getArray())
383 throw INTERP_KERNEL::Exception(msg2);
384 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=self->getArray()->deepCopy();
385 ret->applyLin(val,0.);
386 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
392 if(!self->getArray())
393 throw INTERP_KERNEL::Exception(msg2);
394 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Multiply(self->getArray(),a);
395 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
401 if(!self->getArray())
402 throw INTERP_KERNEL::Exception(msg2);
403 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
404 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Multiply(self->getArray(),aaa);
405 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
411 if(!self->getArray())
412 throw INTERP_KERNEL::Exception(msg2);
413 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=MEDCoupling::DataArrayDouble::New(); aaa->useArray(&bb[0],false,MEDCoupling::DeallocType::CPP_DEALLOC,1,(int)bb.size());
414 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Multiply(self->getArray(),aaa);
415 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
420 { throw INTERP_KERNEL::Exception(msg); }
424 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___rmul__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
426 return MEDCoupling_MEDCouplingFieldDouble___mul__Impl(self,obj);
429 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___rdiv__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
431 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.";
432 const char msg2[]="in MEDCouplingFieldDouble.__div__ : self field has no Array of values set !";
435 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
437 MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
439 return (*other)/(*self);
441 throw INTERP_KERNEL::Exception(msg);
445 MEDCoupling::DataArrayDouble *a;
446 MEDCoupling::DataArrayDoubleTuple *aa;
447 std::vector<double> bb;
449 convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
454 if(!self->getArray())
455 throw INTERP_KERNEL::Exception(msg2);
456 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=self->getArray()->deepCopy();
458 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
464 if(!self->getArray())
465 throw INTERP_KERNEL::Exception(msg2);
466 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Divide(a,self->getArray());
467 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
473 if(!self->getArray())
474 throw INTERP_KERNEL::Exception(msg2);
475 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
476 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Divide(aaa,self->getArray());
477 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
483 if(!self->getArray())
484 throw INTERP_KERNEL::Exception(msg2);
485 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> aaa=MEDCoupling::DataArrayDouble::New(); aaa->useArray(&bb[0],false,MEDCoupling::DeallocType::CPP_DEALLOC,1,(int)bb.size());
486 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=MEDCoupling::DataArrayDouble::Divide(aaa,self->getArray());
487 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
492 { throw INTERP_KERNEL::Exception(msg); }
497 typename MEDCoupling::Traits<T>::FieldType *fieldT_buildSubPart(const MEDCoupling::MEDCouplingFieldT<T> *self, PyObject *li)
501 std::vector<mcIdType> multiVal;
502 std::pair<mcIdType, std::pair<mcIdType,mcIdType> > slic;
503 MEDCoupling::DataArrayIdType *daIntTyypp=0;
504 const MEDCoupling::MEDCouplingMesh *mesh=self->getMesh();
506 throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : field lies on a null mesh !");
507 mcIdType nbc=mesh->getNumberOfCells();
508 convertIntStarOrSliceLikePyObjToCpp(li,nbc,sw,singleVal,multiVal,slic,daIntTyypp);
515 std::ostringstream oss;
516 oss << "Requesting for cell id " << singleVal << " having only " << nbc << " cells !";
517 throw INTERP_KERNEL::Exception(oss.str().c_str());
520 return self->buildSubPart(&singleVal,&singleVal+1);
525 mcIdType tmp=nbc+singleVal;
526 return self->buildSubPart(&tmp,&tmp+1);
530 std::ostringstream oss;
531 oss << "Requesting for cell id " << singleVal << " having only " << nbc << " cells !";
532 throw INTERP_KERNEL::Exception(oss.str().c_str());
538 return self->buildSubPart(&multiVal[0],&multiVal[0]+multiVal.size());
542 return self->buildSubPartRange(slic.first,slic.second.first,slic.second.second);
547 throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : null instance has been given in input !");
548 daIntTyypp->checkAllocated();
549 return self->buildSubPart(daIntTyypp->begin(),daIntTyypp->end());
552 throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : unrecognized type in input ! Possibilities are : int, list or tuple of int DataArrayIdType instance !");
557 typename MEDCoupling::Traits<T>::FieldType *fieldT__getitem__(const MEDCoupling::MEDCouplingFieldT<T> *self, PyObject *li)
559 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";
560 if(PyTuple_Check(li))
562 Py_ssize_t sz=PyTuple_Size(li);
564 throw INTERP_KERNEL::Exception(msg);
565 PyObject *elt0=PyTuple_GetItem(li,0),*elt1=PyTuple_GetItem(li,1);
567 std::size_t singleVal;
568 std::vector<std::size_t> multiVal;
569 std::pair<mcIdType, std::pair<mcIdType,mcIdType> > slic;
570 MEDCoupling::DataArrayIdType *daIntTyypp=0;
571 if(!self->getArray())
572 throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::__getitem__ : no array set on field to deduce number of components !");
574 { convertIntStarOrSliceLikePyObjToCpp(elt1,ToIdType(self->getArray()->getNumberOfComponents()),sw,singleVal,multiVal,slic,daIntTyypp); }
575 catch(INTERP_KERNEL::Exception& e)
576 { std::ostringstream oss; oss << "MEDCouplingFieldDouble::__getitem__ : invalid type in 2nd parameter (compo) !" << e.what(); throw INTERP_KERNEL::Exception(oss.str().c_str()); }
577 typename MEDCoupling::MCAuto< typename MEDCoupling::Traits<T>::FieldType > ret0(fieldT_buildSubPart<T>(self,elt0));
578 typename MEDCoupling::Traits<T>::ArrayType *ret0Arr=ret0->getArray();
580 throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::__getitem__ : no array exists to apply restriction on component on it !");
585 std::vector<std::size_t> v2(1,singleVal);
586 MEDCoupling::MCAuto< typename MEDCoupling::Traits<T>::ArrayType > aarr(ret0Arr->keepSelectedComponents(v2));
587 ret0->setArray(aarr);
592 MEDCoupling::MCAuto< typename MEDCoupling::Traits<T>::ArrayType > aarr(ret0Arr->keepSelectedComponents(multiVal));
593 ret0->setArray(aarr);
598 mcIdType nbOfComp(MEDCoupling::DataArray::GetNumberOfItemGivenBESRelative(slic.first,slic.second.first,slic.second.second,"MEDCouplingFieldDouble::__getitem__ : invalid range in 2nd parameter (components) !"));
599 std::vector<std::size_t> v2(nbOfComp);
600 for(mcIdType i=0;i<nbOfComp;i++)
601 v2[i]=slic.first+i*slic.second.second;
602 MEDCoupling::MCAuto< typename MEDCoupling::Traits<T>::ArrayType > aarr(ret0Arr->keepSelectedComponents(v2));
603 ret0->setArray(aarr);
607 throw INTERP_KERNEL::Exception(msg);
611 return fieldT_buildSubPart<T>(self,li);
614 template<class FIELDT>
615 PyObject *field_getTinySerializationInformation(const FIELDT *self)
617 std::vector<double> a0;
618 std::vector<mcIdType> a1;
619 std::vector<std::string> a2;
620 self->getTinySerializationDbleInformation(a0);
621 self->getTinySerializationIntInformation(a1);
622 self->getTinySerializationStrInformation(a2);
624 PyObject *ret(PyTuple_New(3));
625 PyTuple_SetItem(ret,0,convertDblArrToPyList2(a0));
626 PyTuple_SetItem(ret,1,convertIntArrToPyList2(a1));
627 std::size_t sz(a2.size());
628 PyObject *ret2(PyList_New(sz));
630 for(std::size_t i=0;i<sz;i++)
631 PyList_SetItem(ret2,i,PyString_FromString(a2[i].c_str()));
633 PyTuple_SetItem(ret,2,ret2);
638 PyObject *field_serialize(const typename MEDCoupling::Traits<T>::FieldType *self)
640 MEDCoupling::DataArrayIdType *ret0(0);
641 std::vector<typename MEDCoupling::Traits<T>::ArrayType *> ret1;
642 self->serialize(ret0,ret1);
645 std::size_t sz(ret1.size());
646 PyObject *ret(PyTuple_New(2));
647 PyTuple_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(ret0),SWIGTITraits<mcIdType>::TI, SWIG_POINTER_OWN | 0 ));
648 PyObject *ret1Py(PyList_New(sz));
649 for(std::size_t i=0;i<sz;i++)
653 PyList_SetItem(ret1Py,i,convertArray(ret1[i],SWIG_POINTER_OWN | 0));
655 PyTuple_SetItem(ret,1,ret1Py);
659 template<class FIELDT>
660 PyObject *field__getnewargs__(FIELDT *self)
662 self->checkConsistencyLight();
663 PyObject *ret(PyTuple_New(1));
664 PyObject *ret0(PyDict_New());
666 PyObject *a(PyInt_FromLong(0)),*b(PyInt_FromLong(self->getTypeOfField())),*c(PyInt_FromLong(self->getTimeDiscretization()));
667 PyObject *d(PyTuple_New(2)); PyTuple_SetItem(d,0,b); PyTuple_SetItem(d,1,c);
668 PyDict_SetItem(ret0,a,d);
669 Py_DECREF(a); Py_DECREF(d);
671 PyTuple_SetItem(ret,0,ret0);
675 template<class FIELDT>
676 PyObject *field__getstate__(const FIELDT *self, PyObject *(*tinyserial)(const FIELDT *), PyObject *(*bigserial)(const FIELDT *))
678 self->checkConsistencyLight();
679 PyObject *ret0(tinyserial(self));
680 PyObject *ret1(bigserial(self));
681 const MEDCoupling::MEDCouplingMesh *mesh(self->getMesh());
684 PyObject *ret(PyTuple_New(3));
685 PyTuple_SetItem(ret,0,ret0);
686 PyTuple_SetItem(ret,1,ret1);
687 PyTuple_SetItem(ret,2,convertMesh(const_cast<MEDCoupling::MEDCouplingMesh *>(mesh),SWIG_POINTER_OWN | 0 ));
692 void field__setstate__(typename MEDCoupling::Traits<T>::FieldType *self, PyObject *inp)
694 static const char MSG[]="MEDCouplingFieldDouble.__setstate__ : expected input is a tuple of size 3 !";
695 if(!PyTuple_Check(inp))
696 throw INTERP_KERNEL::Exception(MSG);
697 std::size_t sz(PyTuple_Size(inp));
699 throw INTERP_KERNEL::Exception(MSG);
701 PyObject *elt2(PyTuple_GetItem(inp,2));
703 int status(SWIG_ConvertPtr(elt2,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingMesh,0|0));
704 if(!SWIG_IsOK(status))
705 throw INTERP_KERNEL::Exception(MSG);
706 self->setMesh(reinterpret_cast< const MEDCoupling::MEDCouplingMesh * >(argp));
708 PyObject *elt0(PyTuple_GetItem(inp,0));
709 PyObject *elt1(PyTuple_GetItem(inp,1));
710 std::vector<double> a0;
711 std::vector<mcIdType> a1;
712 std::vector<std::string> a2;
713 MEDCoupling::DataArrayIdType *b0(0);
714 std::vector<typename MEDCoupling::Traits<T>::ArrayType *>b1;
716 if(!PyTuple_Check(elt0) && PyTuple_Size(elt0)!=3)
717 throw INTERP_KERNEL::Exception(MSG);
718 PyObject *a0py(PyTuple_GetItem(elt0,0)),*a1py(PyTuple_GetItem(elt0,1)),*a2py(PyTuple_GetItem(elt0,2));
720 fillArrayWithPyListDbl3(a0py,tmp,a0);
721 convertPyToNewIntArr3(a1py,a1);
722 fillStringVector(a2py,a2);
725 if(!PyTuple_Check(elt1) && PyTuple_Size(elt1)!=2)
726 throw INTERP_KERNEL::Exception(MSG);
727 PyObject *b0py(PyTuple_GetItem(elt1,0)),*b1py(PyTuple_GetItem(elt1,1));
729 int status(SWIG_ConvertPtr(b0py,&argp,SWIGTITraits<mcIdType>::TI,0|0));
730 if(!SWIG_IsOK(status))
731 throw INTERP_KERNEL::Exception(MSG);
732 b0=reinterpret_cast<MEDCoupling::DataArrayIdType *>(argp);
733 convertFromPyObjVectorOfObj<typename MEDCoupling::Traits<T>::ArrayType *>(b1py,SWIGTITraits<T>::TI,MEDCoupling::Traits<T>::ArrayTypeName,b1);
735 self->checkForUnserialization(a1,b0,b1);
736 // useless here to call resizeForUnserialization because arrays are well resized.
737 self->finishUnserialization(a1,a0,a2);
740 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)
742 MEDCoupling::MCAuto<MEDCoupling::DataArrayIdType> elts,eltsIndex;
743 int spaceDim=self->getSpaceDimension();
745 int res1=SWIG_ConvertPtr(p,&da,SWIGTYPE_p_MEDCoupling__DataArrayDouble, 0 | 0 );
746 if (!SWIG_IsOK(res1))
749 INTERP_KERNEL::AutoCPtr<double> tmp=convertPyToNewDblArr2(p,&size);
750 mcIdType nbOfPoints=size/spaceDim;
753 throw INTERP_KERNEL::Exception("MEDCouplingMesh::getCellsContainingPoints : Invalid list length ! Must be a multiple of self.getSpaceDimension() !");
755 func(tmp,nbOfPoints,eps,elts,eltsIndex);
759 MEDCoupling::DataArrayDouble *da2=reinterpret_cast< MEDCoupling::DataArrayDouble * >(da);
761 throw INTERP_KERNEL::Exception("MEDCouplingMesh::getCellsContainingPoints : Not null DataArrayDouble instance expected !");
762 da2->checkAllocated();
763 mcIdType size=da2->getNumberOfTuples();
764 mcIdType nbOfCompo=ToIdType(da2->getNumberOfComponents());
765 if(nbOfCompo!=spaceDim)
767 throw INTERP_KERNEL::Exception("MEDCouplingMesh::getCellsContainingPoints : Invalid DataArrayDouble nb of components ! Expected same as self.getSpaceDimension() !");
769 func(da2->getConstPointer(),size,eps,elts,eltsIndex);
771 PyObject *ret=PyTuple_New(2);
772 PyTuple_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(elts.retn()),SWIGTITraits<mcIdType>::TI, SWIG_POINTER_OWN | 0 ));
773 PyTuple_SetItem(ret,1,SWIG_NewPointerObj(SWIG_as_voidptr(eltsIndex.retn()),SWIGTITraits<mcIdType>::TI, SWIG_POINTER_OWN | 0 ));