Salome HOME
Small refactoring of interpolation and implementation of localization of points regar...
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingFieldT.txx
1 // Copyright (C) 2016-2022  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 #ifndef __MEDCOUPLINGFIELDT_TXX__
22 #define __MEDCOUPLINGFIELDT_TXX__
23
24 #include "MEDCouplingTimeDiscretization.hxx"
25 #include "MEDCouplingMesh.hxx"
26
27 namespace MEDCoupling
28 {
29   template<class T>
30   MEDCouplingFieldT<T>::MEDCouplingFieldT(const MEDCouplingFieldT<T>& other, bool deepCopy):MEDCouplingField(other,deepCopy),_time_discr(other._time_discr->performCopyOrIncrRef(deepCopy))
31   {
32   }
33   
34   /*!
35    * Checks if \a this field is correctly defined, else an exception is thrown.
36    *  \throw If the mesh is not set.
37    *  \throw If the data array is not set.
38    *  \throw If the spatial discretization of \a this field is NULL.
39    *  \throw If \a this->getTimeTolerance() < 0.
40    *  \throw If the temporal discretization data is incorrect.
41    *  \throw If mesh data does not correspond to field data.
42    */
43   template<class T>
44   void MEDCouplingFieldT<T>::checkConsistencyLight() const
45   {
46     MEDCouplingField::checkConsistencyLight();
47     _time_discr->checkConsistencyLight();
48     _type->checkCoherencyBetween(_mesh,getArray());
49   }
50
51   template<class T>
52   MEDCouplingFieldT<T>::MEDCouplingFieldT(const MEDCouplingField& other, MEDCouplingTimeDiscretizationTemplate<T> *timeDiscr, bool deepCopy):MEDCouplingField(other,deepCopy),_time_discr(timeDiscr)
53   {
54   }
55   
56   template<class T>
57   MEDCouplingFieldT<T>::MEDCouplingFieldT(TypeOfField type, MEDCouplingTimeDiscretizationTemplate<T> *timeDiscr):MEDCouplingField(type),_time_discr(timeDiscr)
58   {
59   }
60
61   template<class T>
62   MEDCouplingFieldT<T>::MEDCouplingFieldT(MEDCouplingFieldDiscretization *type, NatureOfField n, MEDCouplingTimeDiscretizationTemplate<T> *timeDiscr):MEDCouplingField(type,n),_time_discr(timeDiscr)
63   {
64   }
65
66   template<class T>
67   MEDCouplingFieldT<T>::~MEDCouplingFieldT()
68   {
69     delete _time_discr;
70   }
71
72   /*!
73    * This method synchronizes time information (time, iteration, order, time unit) regarding the information in \c this->_mesh.
74    * \throw If no mesh is set in this. Or if \a this is not compatible with time setting (typically NO_TIME)
75    */
76   template<class T>
77   void MEDCouplingFieldT<T>::synchronizeTimeWithMesh()
78   {
79     if(!_mesh)
80       throw INTERP_KERNEL::Exception("MEDCouplingFieldT::synchronizeTimeWithMesh : no mesh set in this !");
81     int it(-1),ordr(-1);
82     double val(_mesh->getTime(it,ordr));
83     std::string timeUnit(_mesh->getTimeUnit());
84     setTime(val,it,ordr);
85     setTimeUnit(timeUnit);
86   }
87
88   /*!
89    * Returns a new MEDCouplingFieldDouble which is a copy of \a this one. The data
90    * of \a this field is copied either deep or shallow depending on \a recDeepCpy
91    * parameter. But the underlying mesh is always deep copied.
92    * Data that can be copied either deeply or shallow are:
93    * - \ref MEDCouplingTemporalDisc "temporal discretization" data that holds array(s)
94    * of field values,
95    * - \ref MEDCouplingSpatialDisc "a spatial discretization".
96    * 
97    * This method behaves exactly like clone() except that here the underlying **mesh is
98    * always deeply duplicated**, whatever the value \a recDeepCpy parameter.
99    * The result of \c cloneWithMesh(true) is exactly the same as that of deepCopy().
100    * So the resulting field can not be used together with \a this one in the methods
101    * like operator+(), operator*() etc. To avoid deep copying the underlying mesh,
102    * the user can call clone().
103    *  \param [in] recDeepCpy - if \c true, the copy of the underlying data arrays is
104    *         deep, else all data arrays of \a this field are shared by the new field.
105    *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble. The
106    *         caller is to delete this field using decrRef() as it is no more needed.
107    * \sa clone()
108    */
109   template<class T>
110   typename Traits<T>::FieldType *MEDCouplingFieldT<T>::cloneWithMesh(bool recDeepCpy) const
111   {
112     MCAuto<  typename Traits<T>::FieldType > ret(clone(recDeepCpy));
113     if(_mesh)
114       {
115         MCAuto<MEDCouplingMesh> mCpy(_mesh->deepCopy());
116         ret->setMesh(mCpy);
117       }
118     return ret.retn();
119   }
120
121   template<class T>
122   bool MEDCouplingFieldT<T>::isEqual(const MEDCouplingFieldT<T> *other, double meshPrec, T valsPrec) const
123   {
124     std::string tmp;
125     return isEqualIfNotWhy(other,meshPrec,valsPrec,tmp);
126   }
127   
128   template<class T>
129   bool MEDCouplingFieldT<T>::isEqualIfNotWhy(const MEDCouplingFieldT<T> *other, double meshPrec, T valsPrec, std::string& reason) const
130   {
131     if(!other)
132       throw INTERP_KERNEL::Exception("MEDCouplingFieldT::isEqualIfNotWhy : other instance is NULL !");
133     if(!isEqualIfNotWhyProtected(other,meshPrec,reason))
134       return false;
135     if(!_time_discr->isEqualIfNotWhy(other->_time_discr,T(valsPrec),reason))
136       {
137         reason.insert(0,"In FieldT time discretizations differ :");
138         return false;
139       }
140     return true;
141   }
142   
143   /*!
144    * Checks equality of \a this and \a other field. Only numeric data is considered,
145    * i.e. names, description etc are not compared.
146    *  \param [in] other - the field to compare with.
147    *  \param [in] meshPrec - a precision used to compare node coordinates of meshes.
148    *  \param [in] valsPrec - a precision used to compare data arrays of the two fields.
149    *  \return bool - \c true if the two fields are equal, \c false else.
150    *  \throw If \a other == NULL.
151    *  \throw If the spatial discretization of \a this field is NULL.
152    */
153   template<class T>
154   bool MEDCouplingFieldT<T>::isEqualWithoutConsideringStr(const MEDCouplingFieldT<T> *other, double meshPrec, T valsPrec) const
155   {
156     if(!other)
157       return false;
158     if(!isEqualWithoutConsideringStrProtected(other,meshPrec))
159       return false;
160     if(!_time_discr->isEqualWithoutConsideringStr(other->_time_discr,valsPrec))
161       return false;
162     return true;
163   }
164   
165   /*!
166    * Copies tiny info (component names, name and description) from an \a other field to
167    * \a this one.
168    * \warning The underlying mesh is not renamed (for safety reason).
169    *  \param [in] other - the field to copy the tiny info from.
170    *  \throw If \a this->getNumberOfComponents() != \a other->getNumberOfComponents()
171    */
172   template<class T>
173   void MEDCouplingFieldT<T>::copyTinyStringsFrom(const MEDCouplingField *other)
174   {
175     MEDCouplingField::copyTinyStringsFrom(other);
176     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
177     if(otherC)
178       {
179         _time_discr->copyTinyStringsFrom(*otherC->_time_discr);
180       }
181   }
182
183   /*!
184    * Copies only times, order and iteration from an \a other field to
185    * \a this one. The underlying mesh is not impacted by this method.
186    * Arrays are not impacted neither.
187    *  \param [in] other - the field to tiny attributes from.
188    *  \throw If \a this->getNumberOfComponents() != \a other->getNumberOfComponents()
189    */
190   template<class T>
191   void MEDCouplingFieldT<T>::copyTinyAttrFrom(const MEDCouplingFieldT<T> *other)
192   {
193     if(other)
194       {
195         _time_discr->copyTinyAttrFrom(*other->_time_discr);
196       }
197   }
198   
199   template<class T>
200   void MEDCouplingFieldT<T>::copyAllTinyAttrFrom(const MEDCouplingFieldT<T> *other)
201   {
202     copyTinyStringsFrom(other);
203     copyTinyAttrFrom(other);
204   }
205   
206   /*!
207    * Permutes values of \a this field according to a given permutation array for cells
208    * renumbering. The underlying mesh is deeply copied and its cells are also permuted. 
209    * The number of cells remains the same; for that the permutation array \a old2NewBg
210    * should not contain equal ids.
211    * ** Warning, this method modifies the mesh aggreagated by \a this (by performing a deep copy ) **.
212    *
213    *  \param [in] old2NewBg - the permutation array in "Old to New" mode. Its length is
214    *         to be equal to \a this->getMesh()->getNumberOfCells().
215    *  \param [in] check - if \c true, \a old2NewBg is transformed to a new permutation
216    *         array, so that its maximal cell id to correspond to (be less than) the number
217    *         of cells in mesh. This new array is then used for the renumbering. If \a 
218    *         check == \c false, \a old2NewBg is used as is, that is less secure as validity 
219    *         of ids in \a old2NewBg is not checked.
220    *  \throw If the mesh is not set.
221    *  \throw If the spatial discretization of \a this field is NULL.
222    *  \throw If \a check == \c true and \a old2NewBg contains equal ids.
223    *  \throw If mesh nature does not allow renumbering (e.g. structured mesh).
224    * 
225    *  \if ENABLE_EXAMPLES
226    *  \ref cpp_mcfielddouble_renumberCells "Here is a C++ example".<br>
227    *  \ref  py_mcfielddouble_renumberCells "Here is a Python example".
228    *  \endif
229    */
230   template<class T>
231   void MEDCouplingFieldT<T>::renumberCells(const mcIdType *old2NewBg, bool check)
232   {
233     renumberCellsWithoutMesh(old2NewBg,check);
234     MCAuto<MEDCouplingMesh> m(_mesh->deepCopy());
235     m->renumberCells(old2NewBg,check);
236     setMesh(m);
237     updateTime();
238   }
239
240   /*!
241    * Permutes values of \a this field according to a given permutation array for cells
242    * renumbering. The underlying mesh is \b not permuted. 
243    * The number of cells remains the same; for that the permutation array \a old2NewBg
244    * should not contain equal ids.
245    * This method performs a part of job of renumberCells(). The reasonable use of this
246    * method is only for multi-field instances lying on the same mesh to avoid a
247    * systematic duplication and renumbering of _mesh attribute. 
248    * \warning Use this method with a lot of care!
249    *  \param [in] old2NewBg - the permutation array in "Old to New" mode. Its length is
250    *         to be equal to \a this->getMesh()->getNumberOfCells().
251    *  \param [in] check - if \c true, \a old2NewBg is transformed to a new permutation
252    *         array, so that its maximal cell id to correspond to (be less than) the number
253    *         of cells in mesh. This new array is then used for the renumbering. If \a 
254    *         check == \c false, \a old2NewBg is used as is, that is less secure as validity 
255    *         of ids in \a old2NewBg is not checked.
256    *  \throw If the mesh is not set.
257    *  \throw If the spatial discretization of \a this field is NULL.
258    *  \throw If \a check == \c true and \a old2NewBg contains equal ids.
259    *  \throw If mesh nature does not allow renumbering (e.g. structured mesh).
260    */
261   template<class T>
262   void MEDCouplingFieldT<T>::renumberCellsWithoutMesh(const mcIdType *old2NewBg, bool check)
263   {
264     if(!_mesh)
265       throw INTERP_KERNEL::Exception("Expecting a defined mesh to be able to operate a renumbering !");
266     if(_type.isNull())
267       throw INTERP_KERNEL::Exception("Expecting a spatial discretization to be able to operate a renumbering !");
268     //
269     _type->renumberCells(old2NewBg,check);
270     std::vector< typename MEDCoupling::Traits<T>::ArrayType *> arrays;
271     timeDiscrSafe()->getArrays(arrays);
272     std::vector<DataArray *> arrays2(arrays.size()); std::copy(arrays.begin(),arrays.end(),arrays2.begin());
273     _type->renumberArraysForCell(_mesh,arrays2,old2NewBg,check);
274     //
275     updateTime();
276   }
277   
278   /*!
279    * This method is more strict than MEDCouplingField::areCompatibleForMerge method.
280    * This method is used for operation on fields to operate a first check before attempting operation.
281    */
282   template<class T>
283   bool MEDCouplingFieldT<T>::areStrictlyCompatible(const MEDCouplingField *other) const
284   {
285     std::string tmp;
286     if(!MEDCouplingField::areStrictlyCompatible(other))
287       return false;
288     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
289     if(!otherC)
290       return false;
291     if(!_time_discr->areStrictlyCompatible(otherC->_time_discr,tmp))
292       return false;
293     return true;
294   }
295   
296   template<class T>
297   bool MEDCouplingFieldT<T>::areStrictlyCompatibleForMulDiv(const MEDCouplingField *other) const
298   {
299     if(!MEDCouplingField::areStrictlyCompatibleForMulDiv(other))
300       return false;
301     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
302     if(!otherC)
303       return false;
304     if(!_time_discr->areStrictlyCompatibleForDiv(otherC->_time_discr))
305       return false;
306     return true;
307   }
308
309   /*!
310    * Method with same principle than MEDCouplingFieldDouble::areStrictlyCompatibleForMulDiv method except that
311    * number of components between \a this and 'other' can be different here (for operator/).
312    */
313   template<class T>
314   bool MEDCouplingFieldT<T>::areCompatibleForDiv(const MEDCouplingField *other) const
315   {
316     if(!MEDCouplingField::areStrictlyCompatibleForMulDiv(other))
317       return false;
318     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
319     if(!otherC)
320       return false;
321     if(!_time_discr->areStrictlyCompatibleForDiv(otherC->_time_discr))
322       return false;
323     return true;
324   }
325   
326   template<class T>
327   bool MEDCouplingFieldT<T>::areCompatibleForMul(const MEDCouplingField *other) const
328   {
329     if(!MEDCouplingField::areStrictlyCompatibleForMulDiv(other))
330       return false;
331     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
332     if(!otherC)
333       return false;
334     if(!_time_discr->areStrictlyCompatibleForMul(otherC->_time_discr))
335       return false;
336     return true;
337   }
338
339   /*!
340    * Returns a string describing \a this field. This string is outputted by \c print
341    * Python command. The string includes info on
342    * - name,
343    * - description,
344    * - \ref MEDCouplingSpatialDisc "spatial discretization",
345    * - \ref MEDCouplingTemporalDisc "time discretization",
346    * - \ref NatureOfField,
347    * - components,
348    * - mesh.
349    *
350    *  \return std::string - the string describing \a this field.
351    */
352   template<class T>
353   std::string MEDCouplingFieldT<T>::simpleRepr() const
354   {
355     std::ostringstream ret;
356     ret << Traits<T>::FieldTypeName << " with name : \"" << getName() << "\"\n";
357     ret << "Description of field is : \"" << getDescription() << "\"\n";
358     if(_type)
359       { ret << Traits<T>::FieldTypeName << " space discretization is : " << _type->getStringRepr() << "\n"; }
360     else
361       { ret << Traits<T>::FieldTypeName << " has no spatial discretization !\n"; }
362     if(_time_discr)
363       { ret << Traits<T>::FieldTypeName << " time discretization is : " << _time_discr->getStringRepr() << "\n"; }
364     else
365       { ret << Traits<T>::FieldTypeName << " has no time discretization !\n"; }
366     ret << Traits<T>::FieldTypeName << " nature of field is : \"" << MEDCouplingNatureOfField::GetReprNoThrow(_nature) << "\"\n";
367     if(getArray())
368       {
369         if(getArray()->isAllocated())
370           {
371             std::size_t nbOfCompo=getArray()->getNumberOfComponents();
372             ret << Traits<T>::FieldTypeName << " default array has " << nbOfCompo << " components and " << getArray()->getNumberOfTuples() << " tuples.\n";
373             ret << Traits<T>::FieldTypeName << " default array has following info on components : ";
374             for(std::size_t i=0;i<nbOfCompo;i++)
375               ret << "\"" << getArray()->getInfoOnComponent(i) << "\" ";
376             ret << "\n";
377           }
378         else
379           {
380             ret << "Array set but not allocated !\n";
381           }
382       }
383     if(_mesh)
384       ret << "Mesh support information :\n__________________________\n" << _mesh->simpleRepr();
385     else
386       ret << "Mesh support information : No mesh set !\n";
387     return ret.str();
388   }
389   
390   template<class T>
391   void MEDCouplingFieldT<T>::reprQuickOverview(std::ostream& stream) const
392   {
393     stream << Traits<T>::FieldTypeName << " C++ instance at " << this << ". Name : \"" << _name << "\"." << std::endl;
394     const char *nat(0);
395     try
396       {
397         nat=MEDCouplingNatureOfField::GetRepr(_nature);
398         stream << "Nature of field : " << nat << ".\n";
399       }
400     catch(INTERP_KERNEL::Exception&)
401       {  }
402     const MEDCouplingFieldDiscretization *fd(_type);
403     if(!fd)
404       stream << "No spatial discretization set !";
405     else
406       fd->reprQuickOverview(stream);
407     stream << std::endl;
408     if(!_mesh)
409       stream << "\nNo mesh support defined !";
410     else
411       {
412         std::ostringstream oss;
413         _mesh->reprQuickOverview(oss);
414         std::string tmp(oss.str());
415         stream << "\nMesh info : " << tmp.substr(0,tmp.find('\n'));
416       }
417     if(_time_discr)
418       {
419         const typename Traits<T>::ArrayType *arr(_time_discr->getArray());
420         if(arr)
421           {
422             stream << "\n\nArray info : ";
423             arr->reprQuickOverview(stream);
424           }
425         else
426           {
427             stream << "\n\nNo data array set !";
428           }
429       }
430   }
431
432   /*!
433    * Returns a type of \ref MEDCouplingTemporalDisc "time discretization" of \a this field.
434    *  \return MEDCoupling::TypeOfTimeDiscretization - an enum item describing the time
435    *          discretization type.
436    */
437   template<class T>
438   TypeOfTimeDiscretization MEDCouplingFieldT<T>::getTimeDiscretization() const
439   {
440     return _time_discr->getEnum();
441   }
442
443   /*!
444    * Builds a newly created field, that the caller will have the responsibility to deal with.
445    * \n This method makes the assumption that \a this field is correctly defined when this method is called (\a this->checkConsistencyLight() returns without any exception thrown), **no check of this will be done**.
446    * \n This method returns a restriction of \a this so that only tuple ids specified in [ \a partBg , \a partEnd ) will be contained in the returned field.
447    * \n Parameter [\a partBg, \a partEnd ) specifies **cell ids whatever the spatial discretization** of \a this (
448    * \ref MEDCoupling::ON_CELLS "ON_CELLS", 
449    * \ref MEDCoupling::ON_NODES "ON_NODES",
450    * \ref MEDCoupling::ON_GAUSS_PT "ON_GAUSS_PT", 
451    * \ref MEDCoupling::ON_GAUSS_NE "ON_GAUSS_NE",
452    * \ref MEDCoupling::ON_NODES_KR "ON_NODES_KR",
453    * \ref MEDCoupling::ON_NODES_FE "ON_NODES_FE").
454    *
455    * For example, \a this is a field on cells lying on a mesh that have 10 cells, \a partBg contains the following cell ids [3,7,6].
456    * Then the returned field will lie on mesh having 3 cells and will contain 3 tuples.
457    *- Tuple #0 of the result field will refer to the cell #0 of returned mesh. The cell #0 of returned mesh will be equal to the cell #3 of \a this->getMesh().
458    *- Tuple #1 of the result field will refer to the cell #1 of returned mesh. The cell #1 of returned mesh will be equal to the cell #7 of \a this->getMesh().
459    *- Tuple #2 of the result field will refer to the cell #2 of returned mesh. The cell #2 of returned mesh will be equal to the cell #6 of \a this->getMesh().
460    *
461    * Let, for example, \a this be a field on nodes lying on a mesh that have 10 cells and 11 nodes, and \a partBg contains following cellIds [3,7,6].
462    * Thus \a this currently contains 11 tuples. If the restriction of mesh to 3 cells leads to a mesh with 6 nodes, then the returned field
463    * will contain 6 tuples and \a this field will lie on this restricted mesh. 
464    *
465    * \param [in] partBg - start (included) of input range of cell ids to select [ \a partBg, \a partEnd )
466    * \param [in] partEnd - end (not included) of input range of cell ids to select [ \a partBg, \a partEnd )
467    * \return a newly allocated field the caller should deal with.
468    * 
469    * \throw if there is presence of an invalid cell id in [ \a partBg, \a partEnd ) regarding the number of cells of \a this->getMesh().
470    *
471    * \if ENABLE_EXAMPLES
472    * \ref cpp_mcfielddouble_subpart1 "Here a C++ example."<br>
473    * \ref py_mcfielddouble_subpart1 "Here a Python example."
474    * \endif
475    * \sa MEDCoupling::MEDCouplingFieldDouble::buildSubPart(const DataArrayInt *) const, MEDCouplingFieldDouble::buildSubPartRange
476    */
477   template<class T>
478   typename Traits<T>::FieldType *MEDCouplingFieldT<T>::buildSubPart(const mcIdType *partBg, const mcIdType *partEnd) const
479   {
480     if(_type.isNull())
481       throw INTERP_KERNEL::Exception("MEDCouplingFieldT::buildSubPart : Expecting a not NULL spatial discretization !");
482     DataArrayIdType *arrSelect;
483     MCAuto<MEDCouplingMesh> m=_type->buildSubMeshData(_mesh,partBg,partEnd,arrSelect);
484     MCAuto<DataArrayIdType> arrSelect2(arrSelect);
485     MCAuto< typename Traits<T>::FieldType > ret(clone(false));//quick shallow copy.
486     const MEDCouplingFieldDiscretization *disc=getDiscretization();
487     if(disc)
488       ret->setDiscretization(MCAuto<MEDCouplingFieldDiscretization>(disc->clonePart(partBg,partEnd)));
489     ret->setMesh(m);
490     std::vector<typename Traits<T>::ArrayType *> arrays;
491     timeDiscrSafe()->getArrays(arrays);
492     std::vector<typename Traits<T>::ArrayType *> arrs;
493     std::vector< MCAuto< typename Traits<T>::ArrayType > > arrsSafe;
494     const mcIdType *arrSelBg=arrSelect->begin();
495     const mcIdType *arrSelEnd=arrSelect->end();
496     for(typename std::vector<typename Traits<T>::ArrayType *>::const_iterator iter=arrays.begin();iter!=arrays.end();iter++)
497       {
498         typename Traits<T>::ArrayType *arr(0);
499         if(*iter)
500           arr=(*iter)->selectByTupleIdSafe(arrSelBg,arrSelEnd);
501         arrs.push_back(arr); arrsSafe.push_back(arr);
502       }
503     ret->timeDiscrSafe()->setArrays(arrs,0);
504     return ret.retn();
505   }
506
507   /*!
508    * Builds a newly created field, that the caller will have the responsibility to deal with (decrRef()).
509    * This method makes the assumption that the field is correctly defined when this method is called, no check of this will be done.
510    * This method returns a restriction of \a this so that only tuples with ids specified in \a part will be contained in the returned field.
511    * Parameter \a part specifies **cell ids whatever the spatial discretization of this** (
512    * \ref MEDCoupling::ON_CELLS "ON_CELLS", 
513    * \ref MEDCoupling::ON_NODES "ON_NODES",
514    * \ref MEDCoupling::ON_GAUSS_PT "ON_GAUSS_PT", 
515    * \ref MEDCoupling::ON_GAUSS_NE "ON_GAUSS_NE",
516    * \ref MEDCoupling::ON_NODES_KR "ON_NODES_KR",
517    * \ref MEDCoupling::ON_NODES_FE "ON_NODES_FE").
518    *
519    * For example, \a this is a field on cells lying on a mesh that have 10 cells, \a part contains following cell ids [3,7,6].
520    * Then the returned field will lie on mesh having 3 cells and the returned field will contain 3 tuples.<br>
521    * Tuple #0 of the result field will refer to the cell #0 of returned mesh. The cell #0 of returned mesh will be equal to the cell #3 of \a this->getMesh().<br>
522    * Tuple #1 of the result field will refer to the cell #1 of returned mesh. The cell #1 of returned mesh will be equal to the cell #7 of \a this->getMesh().<br>
523    * Tuple #2 of the result field will refer to the cell #2 of returned mesh. The cell #2 of returned mesh will be equal to the cell #6 of \a this->getMesh().
524    *
525    * Let, for example, \a this be a field on nodes lying on a mesh that have 10 cells and 11 nodes, and \a part contains following cellIds [3,7,6].
526    * Thus \a this currently contains 11 tuples. If the restriction of mesh to 3 cells leads to a mesh with 6 nodes, then the returned field
527    * will contain 6 tuples and \a this field will lie on this restricted mesh. 
528    *
529    *  \param [in] part - an array of cell ids to include to the result field.
530    *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble. The caller is to delete this field using decrRef() as it is no more needed.
531    *
532    *  \if ENABLE_EXAMPLES
533    *  \ref cpp_mcfielddouble_subpart1 "Here is a C++ example".<br>
534    *  \ref  py_mcfielddouble_subpart1 "Here is a Python example".
535    *  \endif
536    *  \sa MEDCouplingFieldDouble::buildSubPartRange
537    */
538   template<class T>
539   typename Traits<T>::FieldType *MEDCouplingFieldT<T>::buildSubPart(const DataArrayIdType *part) const
540   {
541     if(part==0)
542       throw INTERP_KERNEL::Exception("MEDCouplingFieldT::buildSubPart : not empty array must be passed to this method !");
543     return buildSubPart(part->begin(),part->end());
544   }
545   
546   /*!
547    * This method is equivalent to MEDCouplingFieldDouble::buildSubPart, the only difference is that the input range of cell ids is
548    * given using a range given \a begin, \a end and \a step to optimize the part computation.
549    * 
550    * \sa MEDCouplingFieldDouble::buildSubPart
551    */
552   template<class T>
553   typename Traits<T>::FieldType *MEDCouplingFieldT<T>::buildSubPartRange(mcIdType begin, mcIdType end, mcIdType step) const
554   {
555     if(_type.isNull())
556       throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : Expecting a not NULL spatial discretization !");
557     DataArrayIdType *arrSelect;
558     mcIdType beginOut,endOut,stepOut;
559     MCAuto<MEDCouplingMesh> m(_type->buildSubMeshDataRange(_mesh,begin,end,step,beginOut,endOut,stepOut,arrSelect));
560     MCAuto<DataArrayIdType> arrSelect2(arrSelect);
561     MCAuto< typename Traits<T>::FieldType > ret(clone(false));//quick shallow copy.
562     const MEDCouplingFieldDiscretization *disc=getDiscretization();
563     if(disc)
564       ret->setDiscretization(MCAuto<MEDCouplingFieldDiscretization>(disc->clonePartRange(begin,end,step)));
565     ret->setMesh(m);
566     std::vector<typename Traits<T>::ArrayType *> arrays;
567     timeDiscrSafe()->getArrays(arrays);
568     std::vector<typename Traits<T>::ArrayType *> arrs;
569     std::vector< MCAuto< typename Traits<T>::ArrayType > > arrsSafe;
570     for(typename std::vector<typename Traits<T>::ArrayType *>::const_iterator iter=arrays.begin();iter!=arrays.end();iter++)
571       {
572         typename Traits<T>::ArrayType *arr(0);
573         if(*iter)
574           {
575             if(arrSelect)
576               {
577                 const mcIdType *arrSelBg=arrSelect->begin();
578                 const mcIdType *arrSelEnd=arrSelect->end();
579                 arr=(*iter)->selectByTupleIdSafe(arrSelBg,arrSelEnd);
580               }
581             else
582               arr=(*iter)->selectByTupleIdSafeSlice(beginOut,endOut,stepOut);
583           }
584         arrs.push_back(arr); arrsSafe.push_back(arr);
585       }
586     ret->timeDiscrSafe()->setArrays(arrs,0);
587     return ret.retn();
588   }
589   
590   template<class T>
591   const MEDCouplingTimeDiscretizationTemplate<T> *MEDCouplingFieldT<T>::timeDiscrSafe() const
592   {
593     const MEDCouplingTimeDiscretizationTemplate<T> *ret(_time_discr);
594     if(!ret)
595       throw INTERP_KERNEL::Exception("const FieldT : Null type of time discr !");
596     return ret;
597   }
598   
599   template<class T>
600   MEDCouplingTimeDiscretizationTemplate<T> *MEDCouplingFieldT<T>::timeDiscrSafe()
601   {
602     MEDCouplingTimeDiscretizationTemplate<T> *ret(_time_discr);
603     if(!ret)
604       throw INTERP_KERNEL::Exception("const FieldT : Null type of time discr !");
605     return ret;
606   }
607
608   template<class T>
609   void MEDCouplingFieldT<T>::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
610   {
611     tinyInfo.clear();
612     timeDiscrSafe()->getTinySerializationStrInformation(tinyInfo);
613     tinyInfo.push_back(_name);
614     tinyInfo.push_back(_desc);
615     tinyInfo.push_back(getTimeUnit());
616   }
617
618   /*!
619    * This method retrieves some critical values to resize and prepare remote instance.
620    * The first two elements returned in tinyInfo correspond to the parameters to give in constructor.
621    * @param tinyInfo out parameter resized correctly after the call. The length of this vector is tiny.
622    */
623   template<class T>
624   void MEDCouplingFieldT<T>::getTinySerializationIntInformation(std::vector<mcIdType>& tinyInfo) const
625   {
626     if(_type.isNull())
627       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationIntInformation !");
628     tinyInfo.clear();
629     tinyInfo.push_back(ToIdType(_type->getEnum()));
630     tinyInfo.push_back(ToIdType(timeDiscrSafe()->getEnum()));
631     tinyInfo.push_back(ToIdType(_nature));
632     timeDiscrSafe()->getTinySerializationIntInformation(tinyInfo);
633     std::vector<mcIdType> tinyInfo2;
634     _type->getTinySerializationIntInformation(tinyInfo2);
635     tinyInfo.insert(tinyInfo.end(),tinyInfo2.begin(),tinyInfo2.end());
636     tinyInfo.push_back(ToIdType(tinyInfo2.size()));
637   }
638
639   /*!
640    * This method retrieves some critical values to resize and prepare remote instance.
641    * @param tinyInfo out parameter resized correctly after the call. The length of this vector is tiny.
642    */
643   template<class T>
644   void MEDCouplingFieldT<T>::getTinySerializationDbleInformation(std::vector<double>& tinyInfo) const
645   {
646     if(_type.isNull())
647       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationDbleInformation !");
648     tinyInfo.clear();
649     timeDiscrSafe()->getTinySerializationDbleInformation(tinyInfo);
650     std::vector<double> tinyInfo2;
651     _type->getTinySerializationDbleInformation(tinyInfo2);
652     tinyInfo.insert(tinyInfo.end(),tinyInfo2.begin(),tinyInfo2.end());
653     tinyInfo.push_back((int)tinyInfo2.size());//very bad, lack of time to improve it
654   }
655
656   /*!
657    * This method has to be called to the new instance filled by CORBA, MPI, File...
658    * @param tinyInfoI is the value retrieves from distant result of getTinySerializationIntInformation on source instance to be copied.
659    * @param dataInt out parameter. If not null the pointer is already owned by \a this after the call of this method. In this case no decrRef must be applied.
660    * @param arrays out parameter is a vector resized to the right size. The pointers in the vector is already owned by \a this after the call of this method.
661    *               No decrRef must be applied to every instances in returned vector.
662    * \sa checkForUnserialization
663    */
664   template<class T>
665   void MEDCouplingFieldT<T>::resizeForUnserialization(const std::vector<mcIdType>& tinyInfoI, DataArrayIdType *&dataInt, std::vector<typename Traits<T>::ArrayType *>& arrays)
666   {
667     if(_type.isNull())
668       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform resizeForUnserialization !");
669     dataInt=0;
670     std::vector<mcIdType> tinyInfoITmp(tinyInfoI);
671     mcIdType sz=tinyInfoITmp.back();
672     tinyInfoITmp.pop_back();
673     std::vector<mcIdType> tinyInfoITmp2(tinyInfoITmp.begin(),tinyInfoITmp.end()-sz);
674     std::vector<mcIdType> tinyInfoI2(tinyInfoITmp2.begin()+3,tinyInfoITmp2.end());
675     timeDiscrSafe()->resizeForUnserialization(tinyInfoI2,arrays);
676     std::vector<mcIdType> tinyInfoITmp3(tinyInfoITmp.end()-sz,tinyInfoITmp.end());
677     _type->resizeForUnserialization(tinyInfoITmp3,dataInt);
678   }
679
680   /*!
681    * This method is extremely close to resizeForUnserialization except that here the arrays in \a dataInt and in \a arrays are attached in \a this
682    * after having checked that size is correct. This method is used in python pickeling context to avoid copy of data.
683    * \sa resizeForUnserialization
684    */
685   template<class T>
686   void MEDCouplingFieldT<T>::checkForUnserialization(const std::vector<mcIdType>& tinyInfoI, const DataArrayIdType *dataInt, const std::vector<typename Traits<T>::ArrayType *>& arrays)
687   {
688     if(_type.isNull())
689       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform resizeForUnserialization !");
690     std::vector<mcIdType> tinyInfoITmp(tinyInfoI);
691     mcIdType sz=tinyInfoITmp.back();
692     tinyInfoITmp.pop_back();
693     std::vector<mcIdType> tinyInfoITmp2(tinyInfoITmp.begin(),tinyInfoITmp.end()-sz);
694     std::vector<mcIdType> tinyInfoI2(tinyInfoITmp2.begin()+3,tinyInfoITmp2.end());
695     timeDiscrSafe()->checkForUnserialization(tinyInfoI2,arrays);
696     std::vector<mcIdType> tinyInfoITmp3(tinyInfoITmp.end()-sz,tinyInfoITmp.end());
697     _type->checkForUnserialization(tinyInfoITmp3,dataInt);
698   }
699
700   template<class T>
701   void MEDCouplingFieldT<T>::finishUnserialization(const std::vector<mcIdType>& tinyInfoI, const std::vector<double>& tinyInfoD, const std::vector<std::string>& tinyInfoS)
702   {
703     if(_type.isNull())
704       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform finishUnserialization !");
705     std::vector<mcIdType> tinyInfoI2(tinyInfoI.begin()+3,tinyInfoI.end());
706     //
707     std::vector<double> tmp(tinyInfoD);
708     mcIdType sz=ToIdType(tinyInfoD.back());//very bad, lack of time to improve it
709     tmp.pop_back();
710     std::vector<double> tmp1(tmp.begin(),tmp.end()-sz);
711     std::vector<double> tmp2(tmp.end()-sz,tmp.end());
712     //
713     timeDiscrSafe()->finishUnserialization(tinyInfoI2,tmp1,tinyInfoS);
714     _nature=(NatureOfField)tinyInfoI[2];
715     _type->finishUnserialization(tmp2);
716     std::size_t nbOfElemS=tinyInfoS.size();
717     _name=tinyInfoS[nbOfElemS-3];
718     _desc=tinyInfoS[nbOfElemS-2];
719     setTimeUnit(tinyInfoS[nbOfElemS-1]);
720   }
721
722   /*!
723    * Contrary to MEDCouplingPointSet class the returned arrays are \b not the responsibilities of the caller.
724    * The values returned must be consulted only in readonly mode.
725    */
726   template<class T>
727   void MEDCouplingFieldT<T>::serialize(DataArrayIdType *&dataInt, std::vector<typename Traits<T>::ArrayType *>& arrays) const
728   {
729     if(_type.isNull())
730       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform serialize !");
731     timeDiscrSafe()->getArrays(arrays);
732     _type->getSerializationIntArray(dataInt);
733   }
734 }
735
736 #endif