Salome HOME
Code factorization in DataArrays
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingField.cxx
1 // Copyright (C) 2007-2016  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 (CEA/DEN)
20
21 #include "MEDCouplingField.hxx"
22 #include "MEDCouplingMesh.hxx"
23 #include "MEDCouplingFieldDiscretization.hxx"
24
25 #include <sstream>
26
27 using namespace MEDCoupling;
28
29 void MEDCouplingField::checkConsistencyLight() const
30 {
31   if(!_mesh)
32     throw INTERP_KERNEL::Exception("Field invalid because no mesh specified !");
33   if(_type.isNull())
34     throw INTERP_KERNEL::Exception("MEDCouplingField::checkConsistencyLight : no spatial discretization !");
35 }
36
37 bool MEDCouplingField::isEqualIfNotWhy(const MEDCouplingField *other, double meshPrec, double valsPrec, std::string& reason) const
38 {
39   if(!other)
40     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualIfNotWhy : other instance is NULL !");
41   std::ostringstream oss; oss.precision(15);
42   if(_name!=other->_name)
43     {
44       oss << "Field names differ : this name = \"" << _name << "\" and other name = \"" << other->_name << "\" !";
45       reason=oss.str();
46       return false;
47     }
48   if(_desc!=other->_desc)
49     {
50       oss << "Field descriptions differ : this description = \"" << _desc << "\" and other description = \"" << other->_desc << "\" !";
51       reason=oss.str();
52       return false;
53     }
54   if(_nature!=other->_nature)
55     {
56       oss << "Field nature differ : this nature = \"" << MEDCouplingNatureOfField::GetRepr(_nature) << "\" and other nature = \"" << MEDCouplingNatureOfField::GetRepr(other->_nature) << "\" !";
57       reason=oss.str();
58       return false;
59     }
60   if(!_type->isEqualIfNotWhy(other->_type,valsPrec,reason))
61     {
62       reason.insert(0,"Spatial discretizations differ :");
63       return false;
64     }
65   if(_mesh==0 && other->_mesh==0)
66     return true;
67   if(_mesh==0 || other->_mesh==0)
68     {
69       reason="Only one field between the two this and other has its underlying mesh defined !";
70       return false;
71     }
72   if(_mesh==other->_mesh)
73     return true;
74   bool ret=_mesh->isEqualIfNotWhy(other->_mesh,meshPrec,reason);
75   if(!ret)
76     reason.insert(0,"Underlying meshes of fields differ for the following reason : ");
77   return ret;
78 }
79
80 /*!
81  * Checks if \a this and another MEDCouplingField are fully equal.
82  *  \param [in] other - the field to compare with \a this one.
83  *  \param [in] meshPrec - precision used to compare node coordinates of the underlying mesh.
84  *  \param [in] valsPrec - precision used to compare field values.
85  *  \return bool - \c true if the two fields are equal, \c false else.
86  *  \throw If \a other is NULL.
87  */
88 bool MEDCouplingField::isEqual(const MEDCouplingField *other, double meshPrec, double valsPrec) const
89 {
90   std::string tmp;
91   return isEqualIfNotWhy(other,meshPrec,valsPrec,tmp);
92 }
93
94 /*!
95  * Checks if \a this and another MEDCouplingField are equal. The textual
96  * information like names etc. is not considered.
97  *  \param [in] other - the field to compare with \a this one.
98  *  \param [in] meshPrec - precision used to compare node coordinates of the underlying mesh.
99  *  \param [in] valsPrec - precision used to compare field values.
100  *  \return bool - \c true if the two fields are equal, \c false else.
101  *  \throw If \a other is NULL.
102  *  \throw If the spatial discretization of \a this field is NULL.
103  */
104 bool MEDCouplingField::isEqualWithoutConsideringStr(const MEDCouplingField *other, double meshPrec, double valsPrec) const
105 {
106   if(!other)
107     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualWithoutConsideringStr : input field is NULL !");
108   if(!_type)
109     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualWithoutConsideringStr : spatial discretization of this is NULL !");
110   if(!_type->isEqualWithoutConsideringStr(other->_type,valsPrec))
111     return false;
112   if(_nature!=other->_nature)
113     return false;
114   if(_mesh==0 && other->_mesh==0)
115     return true;
116   if(_mesh==0 || other->_mesh==0)
117     return false;
118   if(_mesh==other->_mesh)
119     return true;
120   return _mesh->isEqualWithoutConsideringStr(other->_mesh,meshPrec);
121 }
122
123 /*!
124  * This method states if 'this' and 'other' are compatibles each other before performing any treatment.
125  * This method is good for methods like : mergeFields.
126  * This method is not very demanding compared to areStrictlyCompatible that is better for operation on fields.
127  */
128 bool MEDCouplingField::areCompatibleForMerge(const MEDCouplingField *other) const
129 {
130   if(!other)
131     throw INTERP_KERNEL::Exception("MEDCouplingField::areCompatibleForMerge : input field is NULL !");
132   if(!_type->isEqual(other->_type,1.))
133     return false;
134   if(_nature!=other->_nature)
135     return false;
136   if(_mesh==other->_mesh)
137     return true;
138   return _mesh->areCompatibleForMerge(other->_mesh);
139 }
140
141 /*!
142  * This method is more strict than MEDCouplingField::areCompatibleForMerge method.
143  * This method is used for operation on fields to operate a first check before attempting operation.
144  */
145 bool MEDCouplingField::areStrictlyCompatible(const MEDCouplingField *other) const
146 {
147   if(!other)
148     throw INTERP_KERNEL::Exception("MEDCouplingField::areStrictlyCompatible : input field is NULL !");
149   if(!_type->isEqual(other->_type,1.e-12))
150     return false;
151   if(_nature!=other->_nature)
152     return false;
153   return _mesh==other->_mesh;
154 }
155
156 /*!
157  * This method is less strict than MEDCouplingField::areStrictlyCompatible method.
158  * The difference is that the nature is not checked.
159  * This method is used for multiplication and division on fields to operate a first check before attempting operation.
160  */
161 bool MEDCouplingField::areStrictlyCompatibleForMulDiv(const MEDCouplingField *other) const
162 {
163   if(!other)
164     throw INTERP_KERNEL::Exception("MEDCouplingField::areStrictlyCompatible : input field is NULL !");
165   if(!_type->isEqual(other->_type,1.e-12))
166     return false;
167   return _mesh==other->_mesh;
168 }
169
170
171 void MEDCouplingField::updateTime() const
172 {
173   if(_mesh)
174     updateTimeWith(*_mesh);
175   if(_type)
176     updateTimeWith(*_type);
177 }
178
179 std::size_t MEDCouplingField::getHeapMemorySizeWithoutChildren() const
180 {
181   std::size_t ret=0;
182   ret+=_name.capacity();
183   ret+=_desc.capacity();
184   return ret;
185 }
186
187 std::vector<const BigMemoryObject *> MEDCouplingField::getDirectChildrenWithNull() const
188 {
189   std::vector<const BigMemoryObject *> ret;
190   ret.push_back(_mesh);
191   ret.push_back((const MEDCouplingFieldDiscretization *)_type);
192   return ret;
193 }
194
195 /*!
196  * Returns a type of \ref MEDCouplingSpatialDisc "spatial discretization" of \a this
197  * field in terms of enum MEDCoupling::TypeOfField. 
198  * \return MEDCoupling::TypeOfField - the type of \a this field.
199  * \throw If the geometric type is empty.
200  */
201 TypeOfField MEDCouplingField::getTypeOfField() const
202 {
203   if(!((const MEDCouplingFieldDiscretization *)_type))
204     throw INTERP_KERNEL::Exception("MEDCouplingField::getTypeOfField : spatial discretization is null !");
205   return _type->getEnum();
206 }
207
208 /*!
209  * Returns the nature of \a this field. This information is very important during
210  * interpolation process using MEDCoupling::MEDCouplingRemapper or MEDCoupling::InterpKernelDEC.
211  * In other context than the two mentioned above, this attribute is unimportant. This
212  * attribute is not stored in the MED file.
213  * For more information of the semantics and the influence of this attribute to the
214  * result of interpolation, see
215  * - \ref NatureOfField
216  * - \ref TableNatureOfField "How interpolation coefficients depend on Field Nature"
217  */
218 NatureOfField MEDCouplingField::getNature() const
219 {
220   return _nature;
221 }
222
223 /*!
224  * Sets the nature of \a this field. This information is very important during
225  * interpolation process using MEDCoupling::MEDCouplingRemapper or MEDCoupling::InterpKernelDEC.
226  * In other context than the two mentioned above, this attribute is unimportant. This
227  * attribute is not stored in the MED file.
228  * For more information of the semantics and the influence of this attribute to the
229  * result of interpolation, see
230  * - \ref NatureOfField
231  * - \ref TableNatureOfField "How interpolation coefficients depend on Field Nature"
232  *
233  *  \param [in] nat - the nature of \a this field.
234  *  \throw If \a nat has an invalid value.
235  */
236 void MEDCouplingField::setNature(NatureOfField nat)
237 {
238   MEDCouplingNatureOfField::GetRepr(nat);//generate a throw if nat not recognized
239   _nature=nat;
240 }
241
242 /*!
243  * Returns coordinates of field location points that depend on 
244  * \ref MEDCouplingSpatialDisc "spatial discretization" of \a this field.
245  * - For a field on nodes, returns coordinates of nodes.
246  * - For a field on cells, returns barycenters of cells.
247  * - For a field on gauss points, returns coordinates of gauss points.
248  * 
249  *  \return DataArrayDouble * - a new instance of DataArrayDouble. The caller is to
250  *          delete this array using decrRef() as it is no more needed. 
251  *  \throw If the spatial discretization of \a this field is NULL.
252  *  \throw If the mesh is not set.
253  */
254 DataArrayDouble *MEDCouplingField::getLocalizationOfDiscr() const
255 {
256   if(!_mesh)
257     throw INTERP_KERNEL::Exception("MEDCouplingField::getLocalizationOfDiscr : No mesh set !");
258   if(!((const MEDCouplingFieldDiscretization *)_type))
259     throw INTERP_KERNEL::Exception("MEDCouplingField::getLocalizationOfDiscr : No spatial discretization set !");
260   return _type->getLocalizationOfDiscValues(_mesh);
261 }
262
263 /*!
264  * Returns a new MEDCouplingFieldDouble containing volumes of cells of a dual mesh whose
265  * cells are constructed around field location points (getLocalizationOfDiscr()) of \a this
266  * field. (In case of a field on cells, the dual mesh coincides with the underlying mesh).<br>
267  * For 1D cells, the returned field contains lengths.<br>
268  * For 2D cells, the returned field contains areas.<br>
269  * For 3D cells, the returned field contains volumes.
270  *  \param [in] isAbs - if \c true, the computed cell volume does not reflect cell
271  *         orientation, i.e. the volume is always positive.
272  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble.
273  *          The caller is to delete this array using decrRef() as
274  *          it is no more needed.
275  *  \throw If the mesh is not set.
276  *  \throw If the spatial discretization of \a this field is NULL.
277  *  \throw If the spatial discretization of \a this field is not well defined.
278  */
279
280 MEDCouplingFieldDouble *MEDCouplingField::buildMeasureField(bool isAbs) const
281 {
282   if(!_mesh)
283     throw INTERP_KERNEL::Exception("MEDCouplingField::buildMeasureField : no mesh defined !");
284   if(!((const MEDCouplingFieldDiscretization *)_type))
285     throw INTERP_KERNEL::Exception("MEDCouplingField::buildMeasureField : No spatial discretization set !");
286   return _type->getMeasureField(_mesh,isAbs);
287 }
288
289 /*!
290  * Sets the underlying mesh of \a this field.
291  * For examples of field construction, see \ref MEDCouplingFirstSteps3.
292  *  \param [in] mesh - the new underlying mesh.
293  */
294 void MEDCouplingField::setMesh(const MEDCouplingMesh *mesh)
295 {
296   if(mesh!=_mesh)
297     {
298       if(_mesh)
299         _mesh->decrRef();
300       _mesh=mesh;
301       declareAsNew();
302       if(_mesh)
303         {
304           _mesh->incrRef();
305           updateTimeWith(*_mesh);
306         }
307     }
308 }
309
310 /*!
311  * Sets localization of Gauss points for a given geometric type of cell.
312  *  \param [in] type - the geometric type of cell for which the Gauss localization is set.
313  *  \param [in] refCoo - coordinates of points of the reference cell. Size of this vector
314  *         must be \c nbOfNodesPerCell * \c dimOfType. 
315  *  \param [in] gsCoo - coordinates of Gauss points on the reference cell. Size of this vector
316  *         must be  _wg_.size() * \c dimOfType.
317  *  \param [in] wg - the weights of Gauss points.
318  *  \throw If \a this field is not on Gauss points.
319  *  \throw If the spatial discretization of \a this field is NULL.
320  *  \throw If the mesh is not set.
321  *  \throw If size of any vector do not match the \a type.
322  */
323 void MEDCouplingField::setGaussLocalizationOnType(INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
324                                                   const std::vector<double>& gsCoo, const std::vector<double>& wg)
325 {
326   if(!_mesh)
327     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnType method !");
328   if(!((const MEDCouplingFieldDiscretization *)_type))
329     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call setGaussLocalizationOnType method !");
330   _type->setGaussLocalizationOnType(_mesh,type,refCoo,gsCoo,wg);
331 }
332
333 /*!
334  * Sets localization of Gauss points for given cells specified by their ids.
335  *  \param [in] begin - an array of cell ids of interest.
336  *  \param [in] end - the end of \a begin, i.e. a pointer to its (last+1)-th element.
337  *  \param [in] refCoo - coordinates of points of the reference cell. Size of this vector
338  *         must be \c nbOfNodesPerCell * \c dimOfType. 
339  *  \param [in] gsCoo - coordinates of Gauss points on the reference cell. Size of this vector
340  *         must be  _wg_.size() * \c dimOfType.
341  *  \param [in] wg - the weights of Gauss points.
342  *  \throw If \a this field is not on Gauss points.
343  *  \throw If the spatial discretization of \a this field is NULL.
344  *  \throw If the mesh is not set.
345  *  \throw If size of any vector do not match the type of cell # \a begin[0].
346  *  \throw If type of any cell in \a begin differs from that of cell # \a begin[0].
347  *  \throw If the range [_begin_,_end_) is empty.
348  */
349 void MEDCouplingField::setGaussLocalizationOnCells(const int *begin, const int *end, const std::vector<double>& refCoo,
350                                                    const std::vector<double>& gsCoo, const std::vector<double>& wg)
351 {
352   if(!_mesh)
353     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnCells method !");
354   if(!((const MEDCouplingFieldDiscretization *)_type))
355     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call setGaussLocalizationOnCells method !");
356   _type->setGaussLocalizationOnCells(_mesh,begin,end,refCoo,gsCoo,wg);
357 }
358
359 /*!
360  * Clears data on Gauss points localization.
361  *  \throw If \a this field is not on Gauss points.
362  *  \throw If the spatial discretization of \a this field is NULL.
363  */
364 void MEDCouplingField::clearGaussLocalizations()
365 {
366   if(!((const MEDCouplingFieldDiscretization *)_type))
367     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call clearGaussLocalizations method !");
368   _type->clearGaussLocalizations();
369 }
370
371 /*!
372  * Returns a reference to the Gauss localization object by its id.
373  * \warning This method is not const, so the returned object can be modified without any
374  *          problem.
375  *  \param [in] locId - the id of the Gauss localization object of interest.
376  *         It must be in range <em> 0 <= locId < getNbOfGaussLocalization() </em>.
377  *  \return \ref MEDCoupling::MEDCouplingGaussLocalization "MEDCouplingGaussLocalization" & - the
378  *  Gauss localization object.
379  *  \throw If \a this field is not on Gauss points.
380  *  \throw If \a locId is not within the valid range.
381  *  \throw If the spatial discretization of \a this field is NULL.
382  */
383 MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId)
384 {
385   if(!((const MEDCouplingFieldDiscretization *)_type))
386     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalization method !");
387   return _type->getGaussLocalization(locId);
388 }
389
390 /*!
391  * Returns an id of the Gauss localization object corresponding to a given cell type.
392  *  \param [in] type - the cell type of interest.
393  *  \return int - the id of the Gauss localization object.
394  *  \throw If \a this field is not on Gauss points.
395  *  \throw If the spatial discretization of \a this field is NULL.
396  *  \throw If no Gauss localization object found for the given cell \a type.
397  *  \throw If more than one Gauss localization object found for the given cell \a type.
398  */
399 int MEDCouplingField::getGaussLocalizationIdOfOneType(INTERP_KERNEL::NormalizedCellType type) const
400 {
401   if(!((const MEDCouplingFieldDiscretization *)_type))
402     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalizationIdOfOneType method !");
403   return _type->getGaussLocalizationIdOfOneType(type);
404 }
405
406 /*!
407  * Returns ids of Gauss localization objects corresponding to a given cell type.
408  *  \param [in] type - the cell type of interest.
409  *  \return std::set<int> - ids of the Gauss localization object.
410  *  \throw If \a this field is not on Gauss points.
411  *  \throw If the spatial discretization of \a this field is NULL
412  */
413 std::set<int> MEDCouplingField::getGaussLocalizationIdsOfOneType(INTERP_KERNEL::NormalizedCellType type) const
414 {
415   if(!((const MEDCouplingFieldDiscretization *)_type))
416     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalizationIdsOfOneType method !");
417   return _type->getGaussLocalizationIdsOfOneType(type);
418 }
419
420 /*!
421  * Returns number of Gauss localization objects available. Implicitly all ids in
422  * [0,getNbOfGaussLocalization()) are valid Gauss localization ids. 
423  *  \return int - the number of available Gauss localization objects.
424  *  \throw If \a this field is not on Gauss points.
425  *  \throw If the spatial discretization of \a this field is NULL.
426  */
427 int MEDCouplingField::getNbOfGaussLocalization() const
428 {
429   if(!((const MEDCouplingFieldDiscretization *)_type))
430     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getNbOfGaussLocalization method !");
431   return _type->getNbOfGaussLocalization();
432 }
433
434 /*!
435  * Returns an id of the Gauss localization object corresponding to a type of a given cell.
436  *  \param [in] cellId - an id of the cell of interest.
437  *  \return int - the id of the Gauss localization object.
438  *  \throw If \a this field is not on Gauss points.
439  *  \throw If the spatial discretization of \a this field is NULL.
440  *  \throw If no Gauss localization object found for the given cell.
441  */
442 int MEDCouplingField::getGaussLocalizationIdOfOneCell(int cellId) const
443 {
444   if(!((const MEDCouplingFieldDiscretization *)_type))
445     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalizationIdOfOneCell method !");
446   return _type->getGaussLocalizationIdOfOneCell(cellId);
447 }
448
449 /*!
450  * Returns ids of cells that share the same Gauss localization given by its id.
451  *  \param [in] locId - the id of the Gauss localization object of interest. 
452  *         It must be in range <em> 0 <= locId < getNbOfGaussLocalization() </em>.
453  *  \param [in,out] cellIds - a vector returning ids of found cells. It is cleared before
454  *         filling in. It remains empty if no cells found.
455  *  \throw If \a this field is not on Gauss points.
456  *  \throw If \a locId is not within the valid range.
457  *  \throw If the spatial discretization of \a this field is NULL.
458  */
459 void MEDCouplingField::getCellIdsHavingGaussLocalization(int locId, std::vector<int>& cellIds) const
460 {
461   cellIds.clear();
462   if(!((const MEDCouplingFieldDiscretization *)_type))
463     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getCellIdsHavingGaussLocalization method !");
464   _type->getCellIdsHavingGaussLocalization(locId,cellIds);
465 }
466
467 /*!
468  * Returns a reference to the Gauss localization object by its id.
469  * \warning This method is const, so the returned object is not apt for modification.
470  *  \param [in] locId - the id of the Gauss localization object of interest.
471  *         It must be in range <em> 0 <= locId < getNbOfGaussLocalization() </em>.
472  *  \return const \ref MEDCouplingGaussLocalization & - the Gauss localization object.
473  *  \throw If \a this field is not on Gauss points.
474  *  \throw If \a locId is not within the valid range.
475  *  \throw If the spatial discretization of \a this field is NULL.
476  */
477 const MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) const
478 {
479   if(!((const MEDCouplingFieldDiscretization *)_type))
480     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalization method !");
481   return _type->getGaussLocalization(locId);
482 }
483
484 MEDCouplingField::~MEDCouplingField()
485 {
486   if(_mesh)
487     _mesh->decrRef();
488 }
489
490 MEDCouplingField::MEDCouplingField(MEDCouplingFieldDiscretization *type, NatureOfField nature):_nature(nature),_mesh(0),_type(type)
491 {
492 }
493
494 MEDCouplingField::MEDCouplingField(TypeOfField type):_nature(NoNature),_mesh(0),_type(MEDCouplingFieldDiscretization::New(type))
495 {
496 }
497
498 MEDCouplingField::MEDCouplingField(const MEDCouplingField& other, bool deepCopy):RefCountObject(other),_name(other._name),_desc(other._desc),_nature(other._nature),
499     _mesh(0),_type(0)
500 {
501   if(other._mesh)
502     {
503       _mesh=other._mesh;
504       _mesh->incrRef();
505     }
506   if(deepCopy)
507     _type=other._type->clone();
508   else
509     _type=other._type;
510 }
511
512 /*!
513  * Returns a new MEDCouplingMesh constituted by some cells of the underlying mesh of \a
514  * this field, and returns ids of entities (nodes, cells, Gauss points) lying on the
515  * specified cells. The cells to include to the result mesh are specified by an array of
516  * cell ids. The new mesh shares the coordinates array with the underlying mesh. 
517  *  \param [in] start - an array of cell ids to include to the result mesh.
518  *  \param [in] end - specifies the end of the array \a start, so that
519  *              the last value of \a start is \a end[ -1 ].
520  *  \param [out] di - a new instance of DataArrayInt holding the ids of entities (nodes,
521  *         cells, Gauss points). The caller is to delete this array using decrRef() as it
522  *         is no more needed.  
523  *  \return MEDCouplingMesh * - a new instance of MEDCouplingMesh. The caller is to
524  *         delete this mesh using decrRef() as it is no more needed. 
525  *  \throw If the spatial discretization of \a this field is NULL.
526  *  \throw If the mesh is not set.
527  * \sa buildSubMeshDataRange()
528  */
529 MEDCouplingMesh *MEDCouplingField::buildSubMeshData(const int *start, const int *end, DataArrayInt *&di) const
530 {
531   if(!((const MEDCouplingFieldDiscretization *)_type))
532     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call buildSubMeshData method !");
533   return _type->buildSubMeshData(_mesh,start,end,di);
534 }
535
536 /*!
537  * This method returns a submesh of 'mesh' instance constituting cell ids defined by a range given by the 3 following inputs \a begin, \a end and \a step.
538  * 
539  * \param [out] beginOut Valid only if \a di is NULL
540  * \param [out] endOut Valid only if \a di is NULL
541  * \param [out] stepOut Valid only if \a di is NULL
542  * \param [out] di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array if no output range is foundable.
543  * 
544  * \sa MEDCouplingField::buildSubMeshData
545  */
546 MEDCouplingMesh *MEDCouplingField::buildSubMeshDataRange(int begin, int end, int step, int& beginOut, int& endOut, int& stepOut, DataArrayInt *&di) const
547 {
548   if(!((const MEDCouplingFieldDiscretization *)_type))
549     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call buildSubMeshDataRange method !");
550   return _type->buildSubMeshDataRange(_mesh,begin,end,step,beginOut,endOut,stepOut,di);
551 }
552
553 /*!
554  * This method returns tuples ids implied by the mesh selection of the  cell ids contained in array defined as an interval [start;end).
555  * \return a newly allocated DataArrayInt instance containing tuples ids.
556  */
557 DataArrayInt *MEDCouplingField::computeTupleIdsToSelectFromCellIds(const int *startCellIds, const int *endCellIds) const
558 {
559   if(!((const MEDCouplingFieldDiscretization *)_type))
560     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call computeTupleIdsToSelectFromCellIds method !");
561   return _type->computeTupleIdsToSelectFromCellIds(_mesh,startCellIds,endCellIds);
562 }
563
564 /*!
565  * Returns number of tuples expected regarding the spatial discretization of \a this
566  * field and number of entities in the underlying mesh. This method behaves exactly as MEDCouplingFieldDouble::getNumberOfTuples.
567  *  \return int - the number of expected tuples.
568  *  \throw If the spatial discretization of \a this field is NULL.
569  *  \throw If the mesh is not set.
570  */
571 int MEDCouplingField::getNumberOfTuplesExpected() const
572 {
573   if(!((const MEDCouplingFieldDiscretization *)_type))
574     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getNumberOfTuplesExpected method !");
575   if(_mesh)
576     return _type->getNumberOfTuples(_mesh);
577   else
578     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfTuplesExpected : Empty mesh !");
579 }
580
581 void MEDCouplingField::setDiscretization(MEDCouplingFieldDiscretization *newDisc)
582 {
583   bool needUpdate=(const MEDCouplingFieldDiscretization *)_type!=newDisc;
584   _type=newDisc;
585   if(newDisc)
586     newDisc->incrRef();
587   if(needUpdate)
588     declareAsNew();
589 }
590
591 /*!
592  * Returns number of mesh entities in the underlying mesh of \a this field regarding the
593  * spatial discretization.
594  *  \return int - the number of mesh entities porting the field values.
595  *  \throw If the spatial discretization of \a this field is NULL.
596  *  \throw If the mesh is not set.
597  */
598 int MEDCouplingField::getNumberOfMeshPlacesExpected() const
599 {
600   if(!((const MEDCouplingFieldDiscretization *)_type))
601     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getNumberOfMeshPlacesExpected method !");
602   if(_mesh)
603     return _type->getNumberOfMeshPlaces(_mesh);
604   else
605     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfMeshPlacesExpected : Empty mesh !");
606 }
607
608 /*!
609  * Copy tiny info (component names, name, description) but warning the underlying mesh is not renamed (for safety reason).
610  */
611 void MEDCouplingField::copyTinyStringsFrom(const MEDCouplingField *other)
612 {
613   if(other)
614     {
615       setName(other->_name);
616       setDescription(other->_desc);    
617     }
618 }
619
620 /*!
621  * This method computes the number of tuples a DataArrayDouble instance should have to build a correct MEDCouplingFieldDouble instance starting from a 
622  * submesh of a virtual mesh on which a substraction per type had been applied regarding the spatial discretization in \a this.
623  * 
624  * For spatial discretization \b not equal to ON_GAUSS_NE this method will make the hypothesis that any positive entity id in \a code \a idsPerType is valid.
625  * So in those cases attribute \a _mesh of \a this is ignored.
626  * 
627  * For spatial discretization equal to ON_GAUSS_NE \a _mesh attribute will be taken into account.
628  *
629  * The input code is those implemented in MEDCouplingUMesh::splitProfilePerType.
630  *
631  * \param [in] code - a code with format described above.
632  * \param [in] idsPerType - a list of subparts
633  * \throw If \a this has no spatial discretization set.
634  * \throw If input code point to invalid zones in spatial discretization.
635  * \throw If spatial discretization in \a this requires a mesh and those mesh is invalid (null,...)
636  */
637 int MEDCouplingField::getNumberOfTuplesExpectedRegardingCode(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
638 {
639   const MEDCouplingFieldDiscretization *t(_type);
640   if(!t)
641     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfTuplesExpectedRegardingCode : no spatial discretization set !");
642   return t->getNumberOfTuplesExpectedRegardingCode(code,idsPerType);
643 }