1 // Copyright (C) 2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
21 #ifndef __MEDCOUPLINGFIELDT_TXX__
22 #define __MEDCOUPLINGFIELDT_TXX__
24 #include "MEDCouplingTimeDiscretization.hxx"
25 #include "MEDCouplingMesh.hxx"
30 MEDCouplingFieldT<T>::MEDCouplingFieldT(const MEDCouplingFieldT<T>& other, bool deepCopy):MEDCouplingField(other,deepCopy),_time_discr(other._time_discr->performCopyOrIncrRef(deepCopy))
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.
44 void MEDCouplingFieldT<T>::checkConsistencyLight() const
46 MEDCouplingField::checkConsistencyLight();
47 _time_discr->checkConsistencyLight();
48 _type->checkCoherencyBetween(_mesh,getArray());
52 MEDCouplingFieldT<T>::MEDCouplingFieldT(const MEDCouplingField& other, MEDCouplingTimeDiscretizationTemplate<T> *timeDiscr, bool deepCopy):MEDCouplingField(other,deepCopy),_time_discr(timeDiscr)
57 MEDCouplingFieldT<T>::MEDCouplingFieldT(TypeOfField type, MEDCouplingTimeDiscretizationTemplate<T> *timeDiscr):MEDCouplingField(type),_time_discr(timeDiscr)
62 MEDCouplingFieldT<T>::MEDCouplingFieldT(MEDCouplingFieldDiscretization *type, NatureOfField n, MEDCouplingTimeDiscretizationTemplate<T> *timeDiscr):MEDCouplingField(type,n),_time_discr(timeDiscr)
67 MEDCouplingFieldT<T>::~MEDCouplingFieldT()
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)
77 void MEDCouplingFieldT<T>::synchronizeTimeWithMesh()
80 throw INTERP_KERNEL::Exception("MEDCouplingFieldT::synchronizeTimeWithMesh : no mesh set in this !");
82 double val(_mesh->getTime(it,ordr));
83 std::string timeUnit(_mesh->getTimeUnit());
85 setTimeUnit(timeUnit);
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)
95 * - \ref MEDCouplingSpatialDisc "a spatial discretization".
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.
110 typename Traits<T>::FieldType *MEDCouplingFieldT<T>::cloneWithMesh(bool recDeepCpy) const
112 MCAuto< typename Traits<T>::FieldType > ret(clone(recDeepCpy));
115 MCAuto<MEDCouplingMesh> mCpy(_mesh->deepCopy());
122 bool MEDCouplingFieldT<T>::isEqualIfNotWhy(const MEDCouplingField *other, double meshPrec, double valsPrec, std::string& reason) const
125 throw INTERP_KERNEL::Exception("MEDCouplingFieldT::isEqualIfNotWhy : other instance is NULL !");
126 const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
129 reason="field given in input is not castable in MEDCouplingFieldT !";
132 if(!MEDCouplingField::isEqualIfNotWhy(other,meshPrec,valsPrec,reason))
134 if(!_time_discr->isEqualIfNotWhy(otherC->_time_discr,T(valsPrec),reason))
136 reason.insert(0,"In FieldT time discretizations differ :");
143 * Checks equality of \a this and \a other field. Only numeric data is considered,
144 * i.e. names, description etc are not compared.
145 * \param [in] other - the field to compare with.
146 * \param [in] meshPrec - a precision used to compare node coordinates of meshes.
147 * \param [in] valsPrec - a precision used to compare data arrays of the two fields.
148 * \return bool - \c true if the two fields are equal, \c false else.
149 * \throw If \a other == NULL.
150 * \throw If the spatial discretization of \a this field is NULL.
153 bool MEDCouplingFieldT<T>::isEqualWithoutConsideringStr(const MEDCouplingField *other, double meshPrec, double valsPrec) const
155 const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
158 if(!MEDCouplingField::isEqualWithoutConsideringStr(other,meshPrec,valsPrec))
160 if(!_time_discr->isEqualWithoutConsideringStr(otherC->_time_discr,T(valsPrec)))
166 * Copies tiny info (component names, name and description) from an \a other field to
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()
173 void MEDCouplingFieldT<T>::copyTinyStringsFrom(const MEDCouplingField *other)
175 MEDCouplingField::copyTinyStringsFrom(other);
176 const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
179 _time_discr->copyTinyStringsFrom(*otherC->_time_discr);
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()
191 void MEDCouplingFieldT<T>::copyTinyAttrFrom(const MEDCouplingFieldT<T> *other)
195 _time_discr->copyTinyAttrFrom(*other->_time_discr);
200 void MEDCouplingFieldT<T>::copyAllTinyAttrFrom(const MEDCouplingFieldT<T> *other)
202 copyTinyStringsFrom(other);
203 copyTinyAttrFrom(other);
207 * This method is more strict than MEDCouplingField::areCompatibleForMerge method.
208 * This method is used for operation on fields to operate a first check before attempting operation.
211 bool MEDCouplingFieldT<T>::areStrictlyCompatible(const MEDCouplingField *other) const
214 if(!MEDCouplingField::areStrictlyCompatible(other))
216 const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
219 if(!_time_discr->areStrictlyCompatible(otherC->_time_discr,tmp))
225 bool MEDCouplingFieldT<T>::areStrictlyCompatibleForMulDiv(const MEDCouplingField *other) const
227 if(!MEDCouplingField::areStrictlyCompatibleForMulDiv(other))
229 const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
232 if(!_time_discr->areStrictlyCompatibleForDiv(otherC->_time_discr))
238 * Method with same principle than MEDCouplingFieldDouble::areStrictlyCompatibleForMulDiv method except that
239 * number of components between \a this and 'other' can be different here (for operator/).
242 bool MEDCouplingFieldT<T>::areCompatibleForDiv(const MEDCouplingField *other) const
244 if(!MEDCouplingField::areStrictlyCompatibleForMulDiv(other))
246 const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
249 if(!_time_discr->areStrictlyCompatibleForDiv(otherC->_time_discr))
255 bool MEDCouplingFieldT<T>::areCompatibleForMul(const MEDCouplingField *other) const
257 if(!MEDCouplingField::areStrictlyCompatibleForMulDiv(other))
259 const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
262 if(!_time_discr->areStrictlyCompatibleForMul(otherC->_time_discr))
268 * Returns a string describing \a this field. This string is outputted by \c print
269 * Python command. The string includes info on
272 * - \ref MEDCouplingSpatialDisc "spatial discretization",
273 * - \ref MEDCouplingTemporalDisc "time discretization",
274 * - \ref NatureOfField,
278 * \return std::string - the string describing \a this field.
281 std::string MEDCouplingFieldT<T>::simpleRepr() const
283 std::ostringstream ret;
284 ret << Traits<T>::FieldTypeName << " with name : \"" << getName() << "\"\n";
285 ret << "Description of field is : \"" << getDescription() << "\"\n";
287 { ret << Traits<T>::FieldTypeName << " space discretization is : " << _type->getStringRepr() << "\n"; }
289 { ret << Traits<T>::FieldTypeName << " has no spatial discretization !\n"; }
291 { ret << Traits<T>::FieldTypeName << " time discretization is : " << _time_discr->getStringRepr() << "\n"; }
293 { ret << Traits<T>::FieldTypeName << " has no time discretization !\n"; }
294 ret << Traits<T>::FieldTypeName << " nature of field is : \"" << MEDCouplingNatureOfField::GetReprNoThrow(_nature) << "\"\n";
297 if(getArray()->isAllocated())
299 int nbOfCompo=getArray()->getNumberOfComponents();
300 ret << Traits<T>::FieldTypeName << " default array has " << nbOfCompo << " components and " << getArray()->getNumberOfTuples() << " tuples.\n";
301 ret << Traits<T>::FieldTypeName << " default array has following info on components : ";
302 for(int i=0;i<nbOfCompo;i++)
303 ret << "\"" << getArray()->getInfoOnComponent(i) << "\" ";
308 ret << "Array set but not allocated !\n";
312 ret << "Mesh support information :\n__________________________\n" << _mesh->simpleRepr();
314 ret << "Mesh support information : No mesh set !\n";
319 void MEDCouplingFieldT<T>::reprQuickOverview(std::ostream& stream) const
321 stream << Traits<T>::FieldTypeName << " C++ instance at " << this << ". Name : \"" << _name << "\"." << std::endl;
325 nat=MEDCouplingNatureOfField::GetRepr(_nature);
326 stream << "Nature of field : " << nat << ".\n";
328 catch(INTERP_KERNEL::Exception& e)
330 const MEDCouplingFieldDiscretization *fd(_type);
332 stream << "No spatial discretization set !";
334 fd->reprQuickOverview(stream);
337 stream << "\nNo mesh support defined !";
340 std::ostringstream oss;
341 _mesh->reprQuickOverview(oss);
342 std::string tmp(oss.str());
343 stream << "\nMesh info : " << tmp.substr(0,tmp.find('\n'));
347 const typename Traits<T>::ArrayType *arr(_time_discr->getArray());
350 stream << "\n\nArray info : ";
351 arr->reprQuickOverview(stream);
355 stream << "\n\nNo data array set !";
361 * Returns a type of \ref MEDCouplingTemporalDisc "time discretization" of \a this field.
362 * \return MEDCoupling::TypeOfTimeDiscretization - an enum item describing the time
363 * discretization type.
366 TypeOfTimeDiscretization MEDCouplingFieldT<T>::getTimeDiscretization() const
368 return _time_discr->getEnum();
372 * Builds a newly created field, that the caller will have the responsability to deal with.
373 * \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**.
374 * \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.
375 * \n Parameter [\a partBg, \a partEnd ) specifies **cell ids whatever the spatial discretization** of \a this (
376 * \ref MEDCoupling::ON_CELLS "ON_CELLS",
377 * \ref MEDCoupling::ON_NODES "ON_NODES",
378 * \ref MEDCoupling::ON_GAUSS_PT "ON_GAUSS_PT",
379 * \ref MEDCoupling::ON_GAUSS_NE "ON_GAUSS_NE",
380 * \ref MEDCoupling::ON_NODES_KR "ON_NODES_KR").
382 * 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].
383 * Then the returned field will lie on mesh having 3 cells and will contain 3 tuples.
384 *- 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().
385 *- 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().
386 *- 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().
388 * 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].
389 * 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
390 * will contain 6 tuples and \a this field will lie on this restricted mesh.
392 * \param [in] partBg - start (included) of input range of cell ids to select [ \a partBg, \a partEnd )
393 * \param [in] partEnd - end (not included) of input range of cell ids to select [ \a partBg, \a partEnd )
394 * \return a newly allocated field the caller should deal with.
396 * \throw if there is presence of an invalid cell id in [ \a partBg, \a partEnd ) regarding the number of cells of \a this->getMesh().
398 * \if ENABLE_EXAMPLES
399 * \ref cpp_mcfielddouble_subpart1 "Here a C++ example."<br>
400 * \ref py_mcfielddouble_subpart1 "Here a Python example."
402 * \sa MEDCoupling::MEDCouplingFieldDouble::buildSubPart(const DataArrayInt *) const, MEDCouplingFieldDouble::buildSubPartRange
405 typename Traits<T>::FieldType *MEDCouplingFieldT<T>::buildSubPart(const int *partBg, const int *partEnd) const
408 throw INTERP_KERNEL::Exception("MEDCouplingFieldT::buildSubPart : Expecting a not NULL spatial discretization !");
409 DataArrayInt *arrSelect;
410 MCAuto<MEDCouplingMesh> m=_type->buildSubMeshData(_mesh,partBg,partEnd,arrSelect);
411 MCAuto<DataArrayInt> arrSelect2(arrSelect);
412 MCAuto< typename Traits<T>::FieldType > ret(clone(false));//quick shallow copy.
413 const MEDCouplingFieldDiscretization *disc=getDiscretization();
415 ret->setDiscretization(MCAuto<MEDCouplingFieldDiscretization>(disc->clonePart(partBg,partEnd)));
417 std::vector<typename Traits<T>::ArrayType *> arrays;
418 timeDiscrSafe()->getArrays(arrays);
419 std::vector<typename Traits<T>::ArrayType *> arrs;
420 std::vector< MCAuto< typename Traits<T>::ArrayType > > arrsSafe;
421 const int *arrSelBg=arrSelect->begin();
422 const int *arrSelEnd=arrSelect->end();
423 for(typename std::vector<typename Traits<T>::ArrayType *>::const_iterator iter=arrays.begin();iter!=arrays.end();iter++)
425 typename Traits<T>::ArrayType *arr(0);
427 arr=(*iter)->selectByTupleIdSafe(arrSelBg,arrSelEnd);
428 arrs.push_back(arr); arrsSafe.push_back(arr);
430 ret->timeDiscrSafe()->setArrays(arrs,0);
435 * Builds a newly created field, that the caller will have the responsability to deal with (decrRef()).
436 * This method makes the assumption that the field is correctly defined when this method is called, no check of this will be done.
437 * 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.
438 * Parameter \a part specifies **cell ids whatever the spatial discretization of this** (
439 * \ref MEDCoupling::ON_CELLS "ON_CELLS",
440 * \ref MEDCoupling::ON_NODES "ON_NODES",
441 * \ref MEDCoupling::ON_GAUSS_PT "ON_GAUSS_PT",
442 * \ref MEDCoupling::ON_GAUSS_NE "ON_GAUSS_NE",
443 * \ref MEDCoupling::ON_NODES_KR "ON_NODES_KR").
445 * 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].
446 * Then the returned field will lie on mesh having 3 cells and the returned field will contain 3 tuples.<br>
447 * 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>
448 * 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>
449 * 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().
451 * 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].
452 * 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
453 * will contain 6 tuples and \a this field will lie on this restricted mesh.
455 * \param [in] part - an array of cell ids to include to the result field.
456 * \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble. The caller is to delete this field using decrRef() as it is no more needed.
458 * \if ENABLE_EXAMPLES
459 * \ref cpp_mcfielddouble_subpart1 "Here is a C++ example".<br>
460 * \ref py_mcfielddouble_subpart1 "Here is a Python example".
462 * \sa MEDCouplingFieldDouble::buildSubPartRange
465 typename Traits<T>::FieldType *MEDCouplingFieldT<T>::buildSubPart(const DataArrayInt *part) const
468 throw INTERP_KERNEL::Exception("MEDCouplingFieldT::buildSubPart : not empty array must be passed to this method !");
469 return buildSubPart(part->begin(),part->end());
473 * This method is equivalent to MEDCouplingFieldDouble::buildSubPart, the only difference is that the input range of cell ids is
474 * given using a range given \a begin, \a end and \a step to optimize the part computation.
476 * \sa MEDCouplingFieldDouble::buildSubPart
479 typename Traits<T>::FieldType *MEDCouplingFieldT<T>::buildSubPartRange(int begin, int end, int step) const
482 throw INTERP_KERNEL::Exception("MEDCouplingFieldDouble::buildSubPart : Expecting a not NULL spatial discretization !");
483 DataArrayInt *arrSelect;
484 int beginOut,endOut,stepOut;
485 MCAuto<MEDCouplingMesh> m(_type->buildSubMeshDataRange(_mesh,begin,end,step,beginOut,endOut,stepOut,arrSelect));
486 MCAuto<DataArrayInt> arrSelect2(arrSelect);
487 MCAuto< typename Traits<T>::FieldType > ret(clone(false));//quick shallow copy.
488 const MEDCouplingFieldDiscretization *disc=getDiscretization();
490 ret->setDiscretization(MCAuto<MEDCouplingFieldDiscretization>(disc->clonePartRange(begin,end,step)));
492 std::vector<typename Traits<T>::ArrayType *> arrays;
493 timeDiscrSafe()->getArrays(arrays);
494 std::vector<typename Traits<T>::ArrayType *> arrs;
495 std::vector< MCAuto< typename Traits<T>::ArrayType > > arrsSafe;
496 for(typename std::vector<typename Traits<T>::ArrayType *>::const_iterator iter=arrays.begin();iter!=arrays.end();iter++)
498 typename Traits<T>::ArrayType *arr(0);
503 const int *arrSelBg=arrSelect->begin();
504 const int *arrSelEnd=arrSelect->end();
505 arr=(*iter)->selectByTupleIdSafe(arrSelBg,arrSelEnd);
508 arr=(*iter)->selectByTupleIdSafeSlice(beginOut,endOut,stepOut);
510 arrs.push_back(arr); arrsSafe.push_back(arr);
512 ret->timeDiscrSafe()->setArrays(arrs,0);
517 const MEDCouplingTimeDiscretizationTemplate<T> *MEDCouplingFieldT<T>::timeDiscrSafe() const
519 const MEDCouplingTimeDiscretizationTemplate<T> *ret(_time_discr);
521 throw INTERP_KERNEL::Exception("const FieldT : Null type of time discr !");
526 MEDCouplingTimeDiscretizationTemplate<T> *MEDCouplingFieldT<T>::timeDiscrSafe()
528 MEDCouplingTimeDiscretizationTemplate<T> *ret(_time_discr);
530 throw INTERP_KERNEL::Exception("const FieldT : Null type of time discr !");