1 // Copyright (C) 2007-2013 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.
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 (CEA/DEN)
21 #include "MEDCouplingField.hxx"
22 #include "MEDCouplingMesh.hxx"
23 #include "MEDCouplingFieldDiscretization.hxx"
27 using namespace ParaMEDMEM;
29 bool MEDCouplingField::isEqualIfNotWhy(const MEDCouplingField *other, double meshPrec, double valsPrec, std::string& reason) const throw(INTERP_KERNEL::Exception)
32 throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualIfNotWhy : other instance is NULL !");
33 std::ostringstream oss; oss.precision(15);
34 if(_name!=other->_name)
36 oss << "Field names differ : this name = \"" << _name << "\" and other name = \"" << other->_name << "\" !";
40 if(_desc!=other->_desc)
42 oss << "Field descriptions differ : this description = \"" << _desc << "\" and other description = \"" << other->_desc << "\" !";
46 if(_nature!=other->_nature)
48 oss << "Field nature differ : this nature = \"" << MEDCouplingNatureOfField::GetRepr(_nature) << "\" and other nature = \"" << MEDCouplingNatureOfField::GetRepr(other->_nature) << "\" !";
52 if(!_type->isEqualIfNotWhy(other->_type,valsPrec,reason))
54 reason.insert(0,"Spatial discretizations differ :");
57 if(_mesh==0 && other->_mesh==0)
59 if(_mesh==0 || other->_mesh==0)
61 reason="Only one field between the two this and other has its underlying mesh defined !";
64 if(_mesh==other->_mesh)
66 bool ret=_mesh->isEqualIfNotWhy(other->_mesh,meshPrec,reason);
68 reason.insert(0,"Underlying meshes of fields differ for the following reason : ");
72 bool MEDCouplingField::isEqual(const MEDCouplingField *other, double meshPrec, double valsPrec) const
75 return isEqualIfNotWhy(other,meshPrec,valsPrec,tmp);
78 bool MEDCouplingField::isEqualWithoutConsideringStr(const MEDCouplingField *other, double meshPrec, double valsPrec) const
81 throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualWithoutConsideringStr : input field is NULL !");
83 throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualWithoutConsideringStr : spatial discretization of this is NULL !");
84 if(!_type->isEqualWithoutConsideringStr(other->_type,valsPrec))
86 if(_nature!=other->_nature)
88 if(_mesh==0 && other->_mesh==0)
90 if(_mesh==0 || other->_mesh==0)
92 if(_mesh==other->_mesh)
94 return _mesh->isEqualWithoutConsideringStr(other->_mesh,meshPrec);
98 * This method states if 'this' and 'other' are compatibles each other before performing any treatment.
99 * This method is good for methods like : mergeFields.
100 * This method is not very demanding compared to areStrictlyCompatible that is better for operation on fields.
102 bool MEDCouplingField::areCompatibleForMerge(const MEDCouplingField *other) const
105 throw INTERP_KERNEL::Exception("MEDCouplingField::areCompatibleForMerge : input field is NULL !");
106 if(!_type->isEqual(other->_type,1.))
108 if(_nature!=other->_nature)
110 if(_mesh==other->_mesh)
112 return _mesh->areCompatibleForMerge(other->_mesh);
116 * This method is more strict than MEDCouplingField::areCompatibleForMerge method.
117 * This method is used for operation on fields to operate a first check before attempting operation.
119 bool MEDCouplingField::areStrictlyCompatible(const MEDCouplingField *other) const
122 throw INTERP_KERNEL::Exception("MEDCouplingField::areStrictlyCompatible : input field is NULL !");
123 if(!_type->isEqual(other->_type,1.e-12))
125 if(_nature!=other->_nature)
127 return _mesh==other->_mesh;
130 void MEDCouplingField::updateTime() const
133 updateTimeWith(*_mesh);
135 updateTimeWith(*_type);
138 std::size_t MEDCouplingField::getHeapMemorySize() const
141 ret+=_name.capacity();
142 ret+=_desc.capacity();
144 ret+=_mesh->getHeapMemorySize();
145 if((const MEDCouplingFieldDiscretization *)_type)
146 ret+=_type->getHeapMemorySize();
150 TypeOfField MEDCouplingField::getTypeOfField() const
152 return _type->getEnum();
156 * This method returns the nature of field. This information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
157 * In other context than the two mentioned before this attribute of the field is not sensitive. This attribute is not store in MED file in MEDLoader.
158 * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref NatureOfField "here".
160 NatureOfField MEDCouplingField::getNature() const
166 * This method set the nature of field in \b this.This information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
167 * In other context than the two mentioned before this attribute of the field is not sensitive. This attribute is not store in MED file in MEDLoader.
168 * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref TableNatureOfField "here".
170 void MEDCouplingField::setNature(NatureOfField nat) throw(INTERP_KERNEL::Exception)
172 MEDCouplingNatureOfField::GetRepr(nat);//generate a throw if nat not recognized
177 * This method returns is case of success an instance of DataArrayDouble the user is in reponsability to deal with.
178 * If 'this->_mesh' is not set an exception will be thrown.
179 * For a field on node the array of coords will be returned. For a field on cell a ParaMEDMEM::DataArrayDouble instance
180 * containing the barycenter of cells will be returned. And for a field on gauss point the explicit position of gauss points.
182 DataArrayDouble *MEDCouplingField::getLocalizationOfDiscr() const throw(INTERP_KERNEL::Exception)
185 throw INTERP_KERNEL::Exception("MEDCouplingField::getLocalizationOfDiscr : No mesh set !");
186 if(!((const MEDCouplingFieldDiscretization *)_type))
187 throw INTERP_KERNEL::Exception("MEDCouplingField::getLocalizationOfDiscr : No spatial discretization set !");
188 return _type->getLocalizationOfDiscValues(_mesh);
192 * This method retrieves the measure field of 'this'. If no '_mesh' is defined an exception will be thrown.
193 * Warning the retrieved field life cycle is the responsability of caller.
195 MEDCouplingFieldDouble *MEDCouplingField::buildMeasureField(bool isAbs) const throw(INTERP_KERNEL::Exception)
198 throw INTERP_KERNEL::Exception("MEDCouplingField::buildMeasureField : no mesh defined !");
199 if(!((const MEDCouplingFieldDiscretization *)_type))
200 throw INTERP_KERNEL::Exception("MEDCouplingField::buildMeasureField : No spatial discretization set !");
201 return _type->getMeasureField(_mesh,isAbs);
204 void MEDCouplingField::setMesh(const MEDCouplingMesh *mesh)
215 updateTimeWith(*_mesh);
221 * This method sets gauss localization by geometric type.
222 * @param type geometric type on which the gauss localization will be set.
223 * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
224 * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
225 * @param wg are the weights on Gauss points. The size of this array is used to determine the number of Gauss point in the element.
226 * @throw when size of 'RefCoo' is not valid regarding 'type' parameter, it throws too when the mesh is not set before or if it is not a field on Gauss points.
228 void MEDCouplingField::setGaussLocalizationOnType(INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
229 const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
232 throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnType method !");
233 if(!((const MEDCouplingFieldDiscretization *)_type))
234 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call setGaussLocalizationOnType method !");
235 _type->setGaussLocalizationOnType(_mesh,type,refCoo,gsCoo,wg);
239 * This method sets on ids defined by [begin;end) their gauss localization. This method checks the coherency of cells ids in [begin;end) and 'refCoo' size.
240 * If an incoherence appears an exception will be thrown and no seting will be performed.
241 * An exception is thrown too if [begin,end) has a size lesser than 1.
243 * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
244 * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
245 * @param wg are the weights on Gauss points. The size of this array is used to determine the number of Gauss point in the element.
246 * @throw when size of 'RefCoo' is not valid regarding cells in [begin,end) parameters, it throws too when the mesh is not set before or if it is not a field on Gauss points.
248 void MEDCouplingField::setGaussLocalizationOnCells(const int *begin, const int *end, const std::vector<double>& refCoo,
249 const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
252 throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnCells method !");
253 if(!((const MEDCouplingFieldDiscretization *)_type))
254 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call setGaussLocalizationOnCells method !");
255 _type->setGaussLocalizationOnCells(_mesh,begin,end,refCoo,gsCoo,wg);
259 * This method resets all Gauss loalizations if any.
261 void MEDCouplingField::clearGaussLocalizations()
264 throw INTERP_KERNEL::Exception("Mesh has to be set before calling clearGaussLocalizations method !");
265 if(!((const MEDCouplingFieldDiscretization *)_type))
266 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call clearGaussLocalizations method !");
267 _type->clearGaussLocalizations();
271 * This method returns reference to the Gauss localization object corresponding to 'locId' id.
272 * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) and if 'locId' is invalid because out of range given by
273 * MEDCouplingField::getNbOfGaussLocalization method.
274 * Warning this method is not const, so the returned object could be modified without any problem.
276 MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) throw(INTERP_KERNEL::Exception)
279 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
280 if(!((const MEDCouplingFieldDiscretization *)_type))
281 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalization method !");
282 return _type->getGaussLocalization(locId);
286 * This method returns reference to the Gauss localization object corresponding to 'locId' id.
287 * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) and if several localization ids have been found
290 int MEDCouplingField::getGaussLocalizationIdOfOneType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
293 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneType method !");
294 if(!((const MEDCouplingFieldDiscretization *)_type))
295 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalizationIdOfOneType method !");
296 return _type->getGaussLocalizationIdOfOneType(type);
299 std::set<int> MEDCouplingField::getGaussLocalizationIdsOfOneType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
302 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdsOfOneType method !");
303 if(!((const MEDCouplingFieldDiscretization *)_type))
304 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalizationIdsOfOneType method !");
305 return _type->getGaussLocalizationIdsOfOneType(type);
309 * This method returns number of Gauss localization available. Implicitely all ids in [0,getNbOfGaussLocalization()) is a valid Gauss localisation id.
310 * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss)
312 int MEDCouplingField::getNbOfGaussLocalization() const throw(INTERP_KERNEL::Exception)
315 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getNbOfGaussLocalization method !");
316 if(!((const MEDCouplingFieldDiscretization *)_type))
317 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getNbOfGaussLocalization method !");
318 return _type->getNbOfGaussLocalization();
322 * This method returns an id of Gauss localization in [0,getNbOfGaussLocalization()) that corresponds to the localization of the cell specified by its cellId.
323 * This methods throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) or if at the cell with id 'cellId' in this->_mesh no
324 * Gauss localization has been set.
326 int MEDCouplingField::getGaussLocalizationIdOfOneCell(int cellId) const throw(INTERP_KERNEL::Exception)
329 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
330 if(!((const MEDCouplingFieldDiscretization *)_type))
331 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalizationIdOfOneCell method !");
332 return _type->getGaussLocalizationIdOfOneCell(cellId);
336 * This method returns all cellIds that share the same Gauss localization given by 'locId' parameter (in range [0,getNbOfGaussLocalization()) ).
337 * If no cells fit the Gauss localization given by 'locId' cellIds will be returned empty.
338 * @param locId input that specifies the id of Gauss localization.
339 * @param cellIds output parameter, that will contain the result if this method succeds. This parameter is systematically cleared when called.
340 * @throw if there is no mesh, invalid FieldDescription (different from Gauss) or if locId not in [0,getNbOfGaussLocalization())
342 void MEDCouplingField::getCellIdsHavingGaussLocalization(int locId, std::vector<int>& cellIds) const throw(INTERP_KERNEL::Exception)
346 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
347 if(!((const MEDCouplingFieldDiscretization *)_type))
348 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getCellIdsHavingGaussLocalization method !");
349 _type->getCellIdsHavingGaussLocalization(locId,cellIds);
353 * This method returns reference to the Gauss localization object corresponding to 'locId' id.
354 * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) and if 'locId' is invalid because out of range given by
355 * MEDCouplingField::getNbOfGaussLocalization method.
356 * Warning this method is const.
358 const MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) const throw(INTERP_KERNEL::Exception)
361 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
362 if(!((const MEDCouplingFieldDiscretization *)_type))
363 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalization method !");
364 return _type->getGaussLocalization(locId);
367 MEDCouplingField::~MEDCouplingField()
373 MEDCouplingField::MEDCouplingField(MEDCouplingFieldDiscretization *type, NatureOfField nature):_nature(nature),_mesh(0),_type(type)
377 MEDCouplingField::MEDCouplingField(TypeOfField type):_nature(NoNature),_mesh(0),_type(MEDCouplingFieldDiscretization::New(type))
381 MEDCouplingField::MEDCouplingField(const MEDCouplingField& other, bool deepCopy):RefCountObject(other),_name(other._name),_desc(other._desc),_nature(other._nature),
390 _type=other._type->clone();
396 * This method returns a submesh of 'mesh' instance constituting cell ids contained in array defined as an interval [start;end).
397 * @param di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array.
399 * \sa MEDCouplingField::buildSubMeshDataRange
401 MEDCouplingMesh *MEDCouplingField::buildSubMeshData(const int *start, const int *end, DataArrayInt *&di) const
403 if(!((const MEDCouplingFieldDiscretization *)_type))
404 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call buildSubMeshData method !");
405 return _type->buildSubMeshData(_mesh,start,end,di);
409 * 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.
411 * \param [out] beginOut Valid only if \a di is NULL
412 * \param [out] endOut Valid only if \a di is NULL
413 * \param [out] stepOut Valid only if \a di is NULL
414 * \param [out] di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array if no output range is foundable.
416 * \sa MEDCouplingField::buildSubMeshData
418 MEDCouplingMesh *MEDCouplingField::buildSubMeshDataRange(int begin, int end, int step, int& beginOut, int& endOut, int& stepOut, DataArrayInt *&di) const
420 if(!((const MEDCouplingFieldDiscretization *)_type))
421 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call buildSubMeshDataRange method !");
422 return _type->buildSubMeshDataRange(_mesh,begin,end,step,beginOut,endOut,stepOut,di);
426 * This method returns tuples ids implied by the mesh selection of the cell ids contained in array defined as an interval [start;end).
427 * \return a newly allocated DataArrayInt instance containing tuples ids.
429 DataArrayInt *MEDCouplingField::computeTupleIdsToSelectFromCellIds(const int *startCellIds, const int *endCellIds) const
431 if(!((const MEDCouplingFieldDiscretization *)_type))
432 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call computeTupleIdsToSelectFromCellIds method !");
433 return _type->computeTupleIdsToSelectFromCellIds(_mesh,startCellIds,endCellIds);
437 * This method returns number of tuples expected regarding its discretization and its _mesh attribute.
438 * This method expected a not null _mesh instance. If null, an exception will be thrown.
440 int MEDCouplingField::getNumberOfTuplesExpected() const throw(INTERP_KERNEL::Exception)
442 if(!((const MEDCouplingFieldDiscretization *)_type))
443 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getNumberOfTuplesExpected method !");
445 return _type->getNumberOfTuples(_mesh);
447 throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfTuplesExpected : Empty mesh !");
450 void MEDCouplingField::setDiscretization(MEDCouplingFieldDiscretization *newDisc)
452 bool needUpdate=(const MEDCouplingFieldDiscretization *)_type!=newDisc;
461 * This method returns number of mesh placed expected regarding its discretization and its _mesh attribute.
462 * This method expected a not null _mesh instance. If null, an exception will be thrown.
464 int MEDCouplingField::getNumberOfMeshPlacesExpected() const throw(INTERP_KERNEL::Exception)
466 if(!((const MEDCouplingFieldDiscretization *)_type))
467 throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getNumberOfMeshPlacesExpected method !");
469 return _type->getNumberOfMeshPlaces(_mesh);
471 throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfMeshPlacesExpected : Empty mesh !");
475 * Copy tiny info (component names, name, description) but warning the underlying mesh is not renamed (for safety reason).
477 void MEDCouplingField::copyTinyStringsFrom(const MEDCouplingField *other) throw(INTERP_KERNEL::Exception)
481 setName(other->_name.c_str());
482 setDescription(other->_desc.c_str());