Salome HOME
Copyright update 2021
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingFieldT.txx
1 // Copyright (C) 2016-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 #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    *
454    * 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].
455    * Then the returned field will lie on mesh having 3 cells and will contain 3 tuples.
456    *- 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().
457    *- 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().
458    *- 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().
459    *
460    * 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].
461    * 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
462    * will contain 6 tuples and \a this field will lie on this restricted mesh. 
463    *
464    * \param [in] partBg - start (included) of input range of cell ids to select [ \a partBg, \a partEnd )
465    * \param [in] partEnd - end (not included) of input range of cell ids to select [ \a partBg, \a partEnd )
466    * \return a newly allocated field the caller should deal with.
467    * 
468    * \throw if there is presence of an invalid cell id in [ \a partBg, \a partEnd ) regarding the number of cells of \a this->getMesh().
469    *
470    * \if ENABLE_EXAMPLES
471    * \ref cpp_mcfielddouble_subpart1 "Here a C++ example."<br>
472    * \ref py_mcfielddouble_subpart1 "Here a Python example."
473    * \endif
474    * \sa MEDCoupling::MEDCouplingFieldDouble::buildSubPart(const DataArrayInt *) const, MEDCouplingFieldDouble::buildSubPartRange
475    */
476   template<class T>
477   typename Traits<T>::FieldType *MEDCouplingFieldT<T>::buildSubPart(const mcIdType *partBg, const mcIdType *partEnd) const
478   {
479     if(_type.isNull())
480       throw INTERP_KERNEL::Exception("MEDCouplingFieldT::buildSubPart : Expecting a not NULL spatial discretization !");
481     DataArrayIdType *arrSelect;
482     MCAuto<MEDCouplingMesh> m=_type->buildSubMeshData(_mesh,partBg,partEnd,arrSelect);
483     MCAuto<DataArrayIdType> arrSelect2(arrSelect);
484     MCAuto< typename Traits<T>::FieldType > ret(clone(false));//quick shallow copy.
485     const MEDCouplingFieldDiscretization *disc=getDiscretization();
486     if(disc)
487       ret->setDiscretization(MCAuto<MEDCouplingFieldDiscretization>(disc->clonePart(partBg,partEnd)));
488     ret->setMesh(m);
489     std::vector<typename Traits<T>::ArrayType *> arrays;
490     timeDiscrSafe()->getArrays(arrays);
491     std::vector<typename Traits<T>::ArrayType *> arrs;
492     std::vector< MCAuto< typename Traits<T>::ArrayType > > arrsSafe;
493     const mcIdType *arrSelBg=arrSelect->begin();
494     const mcIdType *arrSelEnd=arrSelect->end();
495     for(typename std::vector<typename Traits<T>::ArrayType *>::const_iterator iter=arrays.begin();iter!=arrays.end();iter++)
496       {
497         typename Traits<T>::ArrayType *arr(0);
498         if(*iter)
499           arr=(*iter)->selectByTupleIdSafe(arrSelBg,arrSelEnd);
500         arrs.push_back(arr); arrsSafe.push_back(arr);
501       }
502     ret->timeDiscrSafe()->setArrays(arrs,0);
503     return ret.retn();
504   }
505
506   /*!
507    * Builds a newly created field, that the caller will have the responsibility to deal with (decrRef()).
508    * This method makes the assumption that the field is correctly defined when this method is called, no check of this will be done.
509    * 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.
510    * Parameter \a part specifies **cell ids whatever the spatial discretization of this** (
511    * \ref MEDCoupling::ON_CELLS "ON_CELLS", 
512    * \ref MEDCoupling::ON_NODES "ON_NODES",
513    * \ref MEDCoupling::ON_GAUSS_PT "ON_GAUSS_PT", 
514    * \ref MEDCoupling::ON_GAUSS_NE "ON_GAUSS_NE",
515    * \ref MEDCoupling::ON_NODES_KR "ON_NODES_KR").
516    *
517    * 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].
518    * Then the returned field will lie on mesh having 3 cells and the returned field will contain 3 tuples.<br>
519    * 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>
520    * 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>
521    * 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().
522    *
523    * 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].
524    * 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
525    * will contain 6 tuples and \a this field will lie on this restricted mesh. 
526    *
527    *  \param [in] part - an array of cell ids to include to the result field.
528    *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble. The caller is to delete this field using decrRef() as it is no more needed.
529    *
530    *  \if ENABLE_EXAMPLES
531    *  \ref cpp_mcfielddouble_subpart1 "Here is a C++ example".<br>
532    *  \ref  py_mcfielddouble_subpart1 "Here is a Python example".
533    *  \endif
534    *  \sa MEDCouplingFieldDouble::buildSubPartRange
535    */
536   template<class T>
537   typename Traits<T>::FieldType *MEDCouplingFieldT<T>::buildSubPart(const DataArrayIdType *part) const
538   {
539     if(part==0)
540       throw INTERP_KERNEL::Exception("MEDCouplingFieldT::buildSubPart : not empty array must be passed to this method !");
541     return buildSubPart(part->begin(),part->end());
542   }
543   
544   /*!
545    * This method is equivalent to MEDCouplingFieldDouble::buildSubPart, the only difference is that the input range of cell ids is
546    * given using a range given \a begin, \a end and \a step to optimize the part computation.
547    * 
548    * \sa MEDCouplingFieldDouble::buildSubPart
549    */
550   template<class T>
551   typename Traits<T>::FieldType *MEDCouplingFieldT<T>::buildSubPartRange(mcIdType begin, mcIdType end, mcIdType step) const
552   {
553     if(_type.isNull())
554       throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : Expecting a not NULL spatial discretization !");
555     DataArrayIdType *arrSelect;
556     mcIdType beginOut,endOut,stepOut;
557     MCAuto<MEDCouplingMesh> m(_type->buildSubMeshDataRange(_mesh,begin,end,step,beginOut,endOut,stepOut,arrSelect));
558     MCAuto<DataArrayIdType> arrSelect2(arrSelect);
559     MCAuto< typename Traits<T>::FieldType > ret(clone(false));//quick shallow copy.
560     const MEDCouplingFieldDiscretization *disc=getDiscretization();
561     if(disc)
562       ret->setDiscretization(MCAuto<MEDCouplingFieldDiscretization>(disc->clonePartRange(begin,end,step)));
563     ret->setMesh(m);
564     std::vector<typename Traits<T>::ArrayType *> arrays;
565     timeDiscrSafe()->getArrays(arrays);
566     std::vector<typename Traits<T>::ArrayType *> arrs;
567     std::vector< MCAuto< typename Traits<T>::ArrayType > > arrsSafe;
568     for(typename std::vector<typename Traits<T>::ArrayType *>::const_iterator iter=arrays.begin();iter!=arrays.end();iter++)
569       {
570         typename Traits<T>::ArrayType *arr(0);
571         if(*iter)
572           {
573             if(arrSelect)
574               {
575                 const mcIdType *arrSelBg=arrSelect->begin();
576                 const mcIdType *arrSelEnd=arrSelect->end();
577                 arr=(*iter)->selectByTupleIdSafe(arrSelBg,arrSelEnd);
578               }
579             else
580               arr=(*iter)->selectByTupleIdSafeSlice(beginOut,endOut,stepOut);
581           }
582         arrs.push_back(arr); arrsSafe.push_back(arr);
583       }
584     ret->timeDiscrSafe()->setArrays(arrs,0);
585     return ret.retn();
586   }
587   
588   template<class T>
589   const MEDCouplingTimeDiscretizationTemplate<T> *MEDCouplingFieldT<T>::timeDiscrSafe() const
590   {
591     const MEDCouplingTimeDiscretizationTemplate<T> *ret(_time_discr);
592     if(!ret)
593       throw INTERP_KERNEL::Exception("const FieldT : Null type of time discr !");
594     return ret;
595   }
596   
597   template<class T>
598   MEDCouplingTimeDiscretizationTemplate<T> *MEDCouplingFieldT<T>::timeDiscrSafe()
599   {
600     MEDCouplingTimeDiscretizationTemplate<T> *ret(_time_discr);
601     if(!ret)
602       throw INTERP_KERNEL::Exception("const FieldT : Null type of time discr !");
603     return ret;
604   }
605
606   template<class T>
607   void MEDCouplingFieldT<T>::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
608   {
609     tinyInfo.clear();
610     timeDiscrSafe()->getTinySerializationStrInformation(tinyInfo);
611     tinyInfo.push_back(_name);
612     tinyInfo.push_back(_desc);
613     tinyInfo.push_back(getTimeUnit());
614   }
615
616   /*!
617    * This method retrieves some critical values to resize and prepare remote instance.
618    * The first two elements returned in tinyInfo correspond to the parameters to give in constructor.
619    * @param tinyInfo out parameter resized correctly after the call. The length of this vector is tiny.
620    */
621   template<class T>
622   void MEDCouplingFieldT<T>::getTinySerializationIntInformation(std::vector<mcIdType>& tinyInfo) const
623   {
624     if(_type.isNull())
625       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationIntInformation !");
626     tinyInfo.clear();
627     tinyInfo.push_back(ToIdType(_type->getEnum()));
628     tinyInfo.push_back(ToIdType(timeDiscrSafe()->getEnum()));
629     tinyInfo.push_back(ToIdType(_nature));
630     timeDiscrSafe()->getTinySerializationIntInformation(tinyInfo);
631     std::vector<mcIdType> tinyInfo2;
632     _type->getTinySerializationIntInformation(tinyInfo2);
633     tinyInfo.insert(tinyInfo.end(),tinyInfo2.begin(),tinyInfo2.end());
634     tinyInfo.push_back(ToIdType(tinyInfo2.size()));
635   }
636
637   /*!
638    * This method retrieves some critical values to resize and prepare remote instance.
639    * @param tinyInfo out parameter resized correctly after the call. The length of this vector is tiny.
640    */
641   template<class T>
642   void MEDCouplingFieldT<T>::getTinySerializationDbleInformation(std::vector<double>& tinyInfo) const
643   {
644     if(_type.isNull())
645       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationDbleInformation !");
646     tinyInfo.clear();
647     timeDiscrSafe()->getTinySerializationDbleInformation(tinyInfo);
648     std::vector<double> tinyInfo2;
649     _type->getTinySerializationDbleInformation(tinyInfo2);
650     tinyInfo.insert(tinyInfo.end(),tinyInfo2.begin(),tinyInfo2.end());
651     tinyInfo.push_back((int)tinyInfo2.size());//very bad, lack of time to improve it
652   }
653
654   /*!
655    * This method has to be called to the new instance filled by CORBA, MPI, File...
656    * @param tinyInfoI is the value retrieves from distant result of getTinySerializationIntInformation on source instance to be copied.
657    * @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.
658    * @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.
659    *               No decrRef must be applied to every instances in returned vector.
660    * \sa checkForUnserialization
661    */
662   template<class T>
663   void MEDCouplingFieldT<T>::resizeForUnserialization(const std::vector<mcIdType>& tinyInfoI, DataArrayIdType *&dataInt, std::vector<typename Traits<T>::ArrayType *>& arrays)
664   {
665     if(_type.isNull())
666       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform resizeForUnserialization !");
667     dataInt=0;
668     std::vector<mcIdType> tinyInfoITmp(tinyInfoI);
669     mcIdType sz=tinyInfoITmp.back();
670     tinyInfoITmp.pop_back();
671     std::vector<mcIdType> tinyInfoITmp2(tinyInfoITmp.begin(),tinyInfoITmp.end()-sz);
672     std::vector<mcIdType> tinyInfoI2(tinyInfoITmp2.begin()+3,tinyInfoITmp2.end());
673     timeDiscrSafe()->resizeForUnserialization(tinyInfoI2,arrays);
674     std::vector<mcIdType> tinyInfoITmp3(tinyInfoITmp.end()-sz,tinyInfoITmp.end());
675     _type->resizeForUnserialization(tinyInfoITmp3,dataInt);
676   }
677
678   /*!
679    * This method is extremely close to resizeForUnserialization except that here the arrays in \a dataInt and in \a arrays are attached in \a this
680    * after having checked that size is correct. This method is used in python pickeling context to avoid copy of data.
681    * \sa resizeForUnserialization
682    */
683   template<class T>
684   void MEDCouplingFieldT<T>::checkForUnserialization(const std::vector<mcIdType>& tinyInfoI, const DataArrayIdType *dataInt, const std::vector<typename Traits<T>::ArrayType *>& arrays)
685   {
686     if(_type.isNull())
687       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform resizeForUnserialization !");
688     std::vector<mcIdType> tinyInfoITmp(tinyInfoI);
689     mcIdType sz=tinyInfoITmp.back();
690     tinyInfoITmp.pop_back();
691     std::vector<mcIdType> tinyInfoITmp2(tinyInfoITmp.begin(),tinyInfoITmp.end()-sz);
692     std::vector<mcIdType> tinyInfoI2(tinyInfoITmp2.begin()+3,tinyInfoITmp2.end());
693     timeDiscrSafe()->checkForUnserialization(tinyInfoI2,arrays);
694     std::vector<mcIdType> tinyInfoITmp3(tinyInfoITmp.end()-sz,tinyInfoITmp.end());
695     _type->checkForUnserialization(tinyInfoITmp3,dataInt);
696   }
697
698   template<class T>
699   void MEDCouplingFieldT<T>::finishUnserialization(const std::vector<mcIdType>& tinyInfoI, const std::vector<double>& tinyInfoD, const std::vector<std::string>& tinyInfoS)
700   {
701     if(_type.isNull())
702       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform finishUnserialization !");
703     std::vector<mcIdType> tinyInfoI2(tinyInfoI.begin()+3,tinyInfoI.end());
704     //
705     std::vector<double> tmp(tinyInfoD);
706     mcIdType sz=ToIdType(tinyInfoD.back());//very bad, lack of time to improve it
707     tmp.pop_back();
708     std::vector<double> tmp1(tmp.begin(),tmp.end()-sz);
709     std::vector<double> tmp2(tmp.end()-sz,tmp.end());
710     //
711     timeDiscrSafe()->finishUnserialization(tinyInfoI2,tmp1,tinyInfoS);
712     _nature=(NatureOfField)tinyInfoI[2];
713     _type->finishUnserialization(tmp2);
714     std::size_t nbOfElemS=tinyInfoS.size();
715     _name=tinyInfoS[nbOfElemS-3];
716     _desc=tinyInfoS[nbOfElemS-2];
717     setTimeUnit(tinyInfoS[nbOfElemS-1]);
718   }
719
720   /*!
721    * Contrary to MEDCouplingPointSet class the returned arrays are \b not the responsibilities of the caller.
722    * The values returned must be consulted only in readonly mode.
723    */
724   template<class T>
725   void MEDCouplingFieldT<T>::serialize(DataArrayIdType *&dataInt, std::vector<typename Traits<T>::ArrayType *>& arrays) const
726   {
727     if(_type.isNull())
728       throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform serialize !");
729     timeDiscrSafe()->getArrays(arrays);
730     _type->getSerializationIntArray(dataInt);
731   }
732 }
733
734 #endif