1 // Copyright (C) 2007-2022 CEA/DEN, EDF R&D
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 "MEDCouplingMultiFields.hxx"
32 #include "MEDCouplingPartDefinition.hxx"
33 #include "MEDCouplingCartesianAMRMesh.hxx"
37 auto PyDeleter = [](PyObject *ob) { Py_DECREF(ob) ; };
39 namespace { // Only in this TU, so in an anonymous namespace
40 class AutoPyPtr : std::unique_ptr<PyObject,decltype(PyDeleter)>
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(); }
49 AutoPyPtr convertMapStringInt(const std::map<std::string,mcIdType>& cpp)
51 AutoPyPtr ret(PyDict_New());
54 AutoPyPtr st(PyString_FromString(it.first.c_str()));
55 AutoPyPtr val(PyInt_FromLong(it.second));
56 PyDict_SetItem(ret,st,val);
61 AutoPyPtr convertMapStringVectString(const std::map<std::string,std::vector<std::string>>& cpp)
63 AutoPyPtr ret(PyDict_New());
66 AutoPyPtr st(PyString_FromString(it.first.c_str()));
67 AutoPyPtr vec(PyList_New(it.second.size()));
69 for(auto it2 : it.second)
71 AutoPyPtr st2(PyString_FromString(it2.c_str()));
72 PyList_SetItem(vec,itc++,st2.retn());
74 PyDict_SetItem(ret,st,vec.retn());
79 static PyObject *convertMesh(MEDCoupling::MEDCouplingMesh *mesh, int owner)
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);
102 throw INTERP_KERNEL::Exception("Not recognized type of mesh on downcast !");
106 static PyObject *convertFieldDiscretization(MEDCoupling::MEDCouplingFieldDiscretization *fd, int owner)
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);
125 throw INTERP_KERNEL::Exception("Not recognized type of field discretization on downcast !");
129 static PyObject *convertField(MEDCoupling::MEDCouplingField *f, int owner)
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);
146 throw INTERP_KERNEL::Exception("Not recognized type of field on downcast !");
150 static PyObject* convertMultiFields(MEDCoupling::MEDCouplingMultiFields *mfs, int owner)
158 if(dynamic_cast<MEDCoupling::MEDCouplingFieldOverTime *>(mfs))
159 ret=SWIG_NewPointerObj((void*)mfs,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldOverTime,owner);
161 ret=SWIG_NewPointerObj((void*)mfs,SWIGTYPE_p_MEDCoupling__MEDCouplingMultiFields,owner);
165 static PyObject *convertCartesianAMRMesh(MEDCoupling::MEDCouplingCartesianAMRMeshGen *mesh, int owner)
172 if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRMeshSub *>(mesh))
174 return SWIG_NewPointerObj(reinterpret_cast<void*>(mesh),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRMeshSub,owner);
176 if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRMesh *>(mesh))
178 return SWIG_NewPointerObj(reinterpret_cast<void*>(mesh),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRMesh,owner);
180 throw INTERP_KERNEL::Exception("convertCartesianAMRMesh wrap : unrecognized type of cartesian AMR mesh !");
183 static PyObject *convertDataForGodFather(MEDCoupling::MEDCouplingDataForGodFather *data, int owner)
190 if(dynamic_cast<MEDCoupling::MEDCouplingAMRAttribute *>(data))
192 return SWIG_NewPointerObj(reinterpret_cast<void*>(data),SWIGTYPE_p_MEDCoupling__MEDCouplingAMRAttribute,owner);
194 throw INTERP_KERNEL::Exception("convertDataForGodFather wrap : unrecognized data type for AMR !");
197 static PyObject *convertCartesianAMRPatch(MEDCoupling::MEDCouplingCartesianAMRPatchGen *patch, int owner)
204 if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRPatchGF *>(patch))
206 return SWIG_NewPointerObj(reinterpret_cast<void*>(patch),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRPatchGF,owner);
208 if(dynamic_cast<MEDCoupling::MEDCouplingCartesianAMRPatch *>(patch))
210 return SWIG_NewPointerObj(reinterpret_cast<void*>(patch),SWIGTYPE_p_MEDCoupling__MEDCouplingCartesianAMRPatch,owner);
212 throw INTERP_KERNEL::Exception("convertCartesianAMRPatch wrap : unrecognized type of cartesian AMR patch !");
215 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___add__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
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 !";
221 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
223 MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
225 return (*self)+(*other);
227 throw INTERP_KERNEL::Exception(msg);
231 MEDCoupling::DataArrayDouble *a;
232 MEDCoupling::DataArrayDoubleTuple *aa;
233 std::vector<double> bb;
235 convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
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);
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);
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);
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);
278 { throw INTERP_KERNEL::Exception(msg); }
282 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___radd__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
284 return MEDCoupling_MEDCouplingFieldDouble___add__Impl(self,obj);
287 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___rsub__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
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 !";
293 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
295 MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
297 return (*other)-(*self);
299 throw INTERP_KERNEL::Exception(msg);
303 MEDCoupling::DataArrayDouble *a;
304 MEDCoupling::DataArrayDoubleTuple *aa;
305 std::vector<double> bb;
307 convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
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);
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);
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);
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);
350 { throw INTERP_KERNEL::Exception(msg); }
354 static MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___mul__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
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 !";
360 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
362 MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
364 return (*self)*(*other);
366 throw INTERP_KERNEL::Exception(msg);
370 MEDCoupling::DataArrayDouble *a;
371 MEDCoupling::DataArrayDoubleTuple *aa;
372 std::vector<double> bb;
374 convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
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);
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);
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);
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);
417 { throw INTERP_KERNEL::Exception(msg); }
421 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___rmul__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
423 return MEDCoupling_MEDCouplingFieldDouble___mul__Impl(self,obj);
426 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling_MEDCouplingFieldDouble___rdiv__Impl(MEDCoupling::MEDCouplingFieldDouble *self, PyObject *obj)
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 !";
432 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0)))
434 MEDCoupling::MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp);
436 return (*other)/(*self);
438 throw INTERP_KERNEL::Exception(msg);
442 MEDCoupling::DataArrayDouble *a;
443 MEDCoupling::DataArrayDoubleTuple *aa;
444 std::vector<double> bb;
446 convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb);
451 if(!self->getArray())
452 throw INTERP_KERNEL::Exception(msg2);
453 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> ret=self->getArray()->deepCopy();
455 MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret2=self->clone(false);
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);
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);
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);
489 { throw INTERP_KERNEL::Exception(msg); }
494 typename MEDCoupling::Traits<T>::FieldType *fieldT_buildSubPart(const MEDCoupling::MEDCouplingFieldT<T> *self, PyObject *li)
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();
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);
512 std::ostringstream oss;
513 oss << "Requesting for cell id " << singleVal << " having only " << nbc << " cells !";
514 throw INTERP_KERNEL::Exception(oss.str().c_str());
517 return self->buildSubPart(&singleVal,&singleVal+1);
522 mcIdType tmp=nbc+singleVal;
523 return self->buildSubPart(&tmp,&tmp+1);
527 std::ostringstream oss;
528 oss << "Requesting for cell id " << singleVal << " having only " << nbc << " cells !";
529 throw INTERP_KERNEL::Exception(oss.str().c_str());
535 return self->buildSubPart(&multiVal[0],&multiVal[0]+multiVal.size());
539 return self->buildSubPartRange(slic.first,slic.second.first,slic.second.second);
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());
549 throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : unrecognized type in input ! Possibilities are : int, list or tuple of int DataArrayIdType instance !");
554 typename MEDCoupling::Traits<T>::FieldType *fieldT__getitem__(const MEDCoupling::MEDCouplingFieldT<T> *self, PyObject *li)
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))
559 Py_ssize_t sz=PyTuple_Size(li);
561 throw INTERP_KERNEL::Exception(msg);
562 PyObject *elt0=PyTuple_GetItem(li,0),*elt1=PyTuple_GetItem(li,1);
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 !");
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();
577 throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::__getitem__ : no array exists to apply restriction on component on it !");
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);
589 MEDCoupling::MCAuto< typename MEDCoupling::Traits<T>::ArrayType > aarr(ret0Arr->keepSelectedComponents(multiVal));
590 ret0->setArray(aarr);
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);
604 throw INTERP_KERNEL::Exception(msg);
608 return fieldT_buildSubPart<T>(self,li);
611 template<class FIELDT>
612 PyObject *field_getTinySerializationInformation(const FIELDT *self)
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);
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));
627 for(std::size_t i=0;i<sz;i++)
628 PyList_SetItem(ret2,i,PyString_FromString(a2[i].c_str()));
630 PyTuple_SetItem(ret,2,ret2);
635 PyObject *field_serialize(const typename MEDCoupling::Traits<T>::FieldType *self)
637 MEDCoupling::DataArrayIdType *ret0(0);
638 std::vector<typename MEDCoupling::Traits<T>::ArrayType *> ret1;
639 self->serialize(ret0,ret1);
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++)
650 PyList_SetItem(ret1Py,i,convertArray(ret1[i],SWIG_POINTER_OWN | 0));
652 PyTuple_SetItem(ret,1,ret1Py);
656 template<class FIELDT>
657 PyObject *field__getnewargs__(FIELDT *self)
659 self->checkConsistencyLight();
660 PyObject *ret(PyTuple_New(1));
661 PyObject *ret0(PyDict_New());
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);
668 PyTuple_SetItem(ret,0,ret0);
672 template<class FIELDT>
673 PyObject *field__getstate__(const FIELDT *self, PyObject *(*tinyserial)(const FIELDT *), PyObject *(*bigserial)(const FIELDT *))
675 self->checkConsistencyLight();
676 PyObject *ret0(tinyserial(self));
677 PyObject *ret1(bigserial(self));
678 const MEDCoupling::MEDCouplingMesh *mesh(self->getMesh());
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 ));
689 void field__setstate__(typename MEDCoupling::Traits<T>::FieldType *self, PyObject *inp)
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));
696 throw INTERP_KERNEL::Exception(MSG);
698 PyObject *elt2(PyTuple_GetItem(inp,2));
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));
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;
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));
717 fillArrayWithPyListDbl3(a0py,tmp,a0);
718 convertPyToNewIntArr3(a1py,a1);
719 fillStringVector(a2py,a2);
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));
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);
732 self->checkForUnserialization(a1,b0,b1);
733 // useless here to call resizeForUnserialization because arrays are well resized.
734 self->finishUnserialization(a1,a0,a2);
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)
739 MEDCoupling::MCAuto<MEDCoupling::DataArrayIdType> elts,eltsIndex;
740 int spaceDim=self->getSpaceDimension();
742 int res1=SWIG_ConvertPtr(p,&da,SWIGTYPE_p_MEDCoupling__DataArrayDouble, 0 | 0 );
743 if (!SWIG_IsOK(res1))
746 INTERP_KERNEL::AutoCPtr<double> tmp=convertPyToNewDblArr2(p,&size);
747 mcIdType nbOfPoints=size/spaceDim;
750 throw INTERP_KERNEL::Exception("MEDCouplingMesh::getCellsContainingPoints : Invalid list length ! Must be a multiple of self.getSpaceDimension() !");
752 func(tmp,nbOfPoints,eps,elts,eltsIndex);
756 MEDCoupling::DataArrayDouble *da2=reinterpret_cast< MEDCoupling::DataArrayDouble * >(da);
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)
764 throw INTERP_KERNEL::Exception("MEDCouplingMesh::getCellsContainingPoints : Invalid DataArrayDouble nb of components ! Expected same as self.getSpaceDimension() !");
766 func(da2->getConstPointer(),size,eps,elts,eltsIndex);
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 ));