Salome HOME
8478acdf53a7c5a3c36883f550bf18a946723b5d
[modules/med.git] / src / MEDCoupling / MEDCouplingField.cxx
1 // Copyright (C) 2007-2013  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.
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 ParaMEDMEM;
28
29 bool MEDCouplingField::isEqualIfNotWhy(const MEDCouplingField *other, double meshPrec, double valsPrec, std::string& reason) const throw(INTERP_KERNEL::Exception)
30 {
31   if(!other)
32     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualIfNotWhy : other instance is NULL !");
33   std::ostringstream oss; oss.precision(15);
34   if(_name!=other->_name)
35     {
36       oss << "Field names differ : this name = \"" << _name << "\" and other name = \"" << other->_name << "\" !";
37       reason=oss.str();
38       return false;
39     }
40   if(_desc!=other->_desc)
41     {
42       oss << "Field descriptions differ : this description = \"" << _desc << "\" and other description = \"" << other->_desc << "\" !";
43       reason=oss.str();
44       return false;
45     }
46   if(_nature!=other->_nature)
47     {
48       oss << "Field nature differ : this nature = \"" << MEDCouplingNatureOfField::GetRepr(_nature) << "\" and other nature = \"" << MEDCouplingNatureOfField::GetRepr(other->_nature) << "\" !";
49       reason=oss.str();
50       return false;
51     }
52   if(!_type->isEqualIfNotWhy(other->_type,valsPrec,reason))
53     {
54       reason.insert(0,"Spatial discretizations differ :");
55       return false;
56     }
57   if(_mesh==0 && other->_mesh==0)
58     return true;
59   if(_mesh==0 || other->_mesh==0)
60     {
61       reason="Only one field between the two this and other has its underlying mesh defined !";
62       return false;
63     }
64   if(_mesh==other->_mesh)
65     return true;
66   bool ret=_mesh->isEqualIfNotWhy(other->_mesh,meshPrec,reason);
67   if(!ret)
68     reason.insert(0,"Underlying meshes of fields differ for the following reason : ");
69   return ret;
70 }
71
72 bool MEDCouplingField::isEqual(const MEDCouplingField *other, double meshPrec, double valsPrec) const
73 {
74   std::string tmp;
75   return isEqualIfNotWhy(other,meshPrec,valsPrec,tmp);
76 }
77
78 bool MEDCouplingField::isEqualWithoutConsideringStr(const MEDCouplingField *other, double meshPrec, double valsPrec) const
79 {
80   if(!_type->isEqualWithoutConsideringStr(other->_type,valsPrec))
81     return false;
82   if(_nature!=other->_nature)
83     return false;
84   if(_mesh==0 && other->_mesh==0)
85     return true;
86   if(_mesh==0 || other->_mesh==0)
87     return false;
88   if(_mesh==other->_mesh)
89     return true;
90   return _mesh->isEqualWithoutConsideringStr(other->_mesh,meshPrec);
91 }
92
93 /*!
94  * This method states if 'this' and 'other' are compatibles each other before performing any treatment.
95  * This method is good for methods like : mergeFields.
96  * This method is not very demanding compared to areStrictlyCompatible that is better for operation on fields.
97  */
98 bool MEDCouplingField::areCompatibleForMerge(const MEDCouplingField *other) const
99 {
100   if(!_type->isEqual(other->_type,1.))
101     return false;
102   if(_nature!=other->_nature)
103     return false;
104   if(_mesh==other->_mesh)
105     return true;
106   return _mesh->areCompatibleForMerge(other->_mesh);
107 }
108
109 /*!
110  * This method is more strict than MEDCouplingField::areCompatibleForMerge method.
111  * This method is used for operation on fields to operate a first check before attempting operation.
112  */
113 bool MEDCouplingField::areStrictlyCompatible(const MEDCouplingField *other) const
114 {
115   if(!_type->isEqual(other->_type,1.e-12))
116     return false;
117   if(_nature!=other->_nature)
118     return false;
119   return _mesh==other->_mesh;
120 }
121
122 void MEDCouplingField::updateTime() const
123 {
124   if(_mesh)
125     updateTimeWith(*_mesh);
126   if(_type)
127     updateTimeWith(*_type);
128 }
129
130 std::size_t MEDCouplingField::getHeapMemorySize() const
131 {
132   std::size_t ret=0;
133   ret+=_name.capacity();
134   ret+=_desc.capacity();
135   if(_mesh)
136     ret+=_mesh->getHeapMemorySize();
137   if((const MEDCouplingFieldDiscretization *)_type)
138     ret+=_type->getHeapMemorySize();
139   return ret;
140 }
141
142 TypeOfField MEDCouplingField::getTypeOfField() const
143 {
144   return _type->getEnum();
145 }
146
147 /*!
148  * This method returns the nature of field. This information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
149  * 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.
150  * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref NatureOfField "here".
151  */
152 NatureOfField MEDCouplingField::getNature() const
153 {
154   return _nature;
155 }
156
157 /*!
158  * This method set the nature of field in \b this.This  information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
159  * 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.
160  * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref TableNatureOfField "here".
161  */
162 void MEDCouplingField::setNature(NatureOfField nat) throw(INTERP_KERNEL::Exception)
163 {
164   _nature=nat;
165 }
166
167 /*!
168  * This method returns is case of success an instance of DataArrayDouble the user is in reponsability to deal with.
169  * If 'this->_mesh' is not set an exception will be thrown.
170  * For a field on node the array of coords will be returned. For a field on cell a ParaMEDMEM::DataArrayDouble instance
171  * containing the barycenter of cells will be returned. And for a field on gauss point the explicit position of gauss points.
172  */
173 DataArrayDouble *MEDCouplingField::getLocalizationOfDiscr() const throw(INTERP_KERNEL::Exception)
174 {
175   if(!_mesh)
176     throw INTERP_KERNEL::Exception("MEDCouplingField::getLocalizationOfDiscr : No mesh set !");
177   return _type->getLocalizationOfDiscValues(_mesh);
178 }
179
180 /*!
181  * This method retrieves the measure field of 'this'. If no '_mesh' is defined an exception will be thrown.
182  * Warning the retrieved field life cycle is the responsability of caller.
183  */
184 MEDCouplingFieldDouble *MEDCouplingField::buildMeasureField(bool isAbs) const throw(INTERP_KERNEL::Exception)
185 {
186   if(_mesh==0)
187     throw INTERP_KERNEL::Exception("MEDCouplingField::getMeasureField : no mesh defined !!!");
188   return _type->getMeasureField(_mesh,isAbs);
189 }
190
191 void MEDCouplingField::setMesh(const MEDCouplingMesh *mesh)
192 {
193   if(mesh!=_mesh)
194     {
195       if(_mesh)
196         _mesh->decrRef();
197       _mesh=mesh;
198       if(_mesh)
199         {
200           _mesh->incrRef();
201           updateTimeWith(*_mesh);
202         }
203     }
204 }
205
206 /*!
207  * This method sets gauss localization by geometric type.
208  * @param type geometric type on which the gauss localization will be set.
209  * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
210  * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
211  * @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.
212  * @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.
213  */
214 void MEDCouplingField::setGaussLocalizationOnType(INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
215                                                   const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
216 {
217   if(!_mesh)
218     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnType method !");
219   _type->setGaussLocalizationOnType(_mesh,type,refCoo,gsCoo,wg);
220 }
221
222 /*!
223  * 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.
224  * If an incoherence appears an exception will be thrown and no seting will be performed.
225  * An exception is thrown too if [begin,end) has a size lesser than 1.
226  * 
227  * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
228  * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
229  * @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.
230  * @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.
231  */
232 void MEDCouplingField::setGaussLocalizationOnCells(const int *begin, const int *end, const std::vector<double>& refCoo,
233                                                    const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
234 {
235   if(!_mesh)
236     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnCells method !");
237   _type->setGaussLocalizationOnCells(_mesh,begin,end,refCoo,gsCoo,wg);
238 }
239
240 /*!
241  * This method resets all Gauss loalizations if any.
242  */
243 void MEDCouplingField::clearGaussLocalizations()
244 {
245   if(!_mesh)
246     throw INTERP_KERNEL::Exception("Mesh has to be set before calling clearGaussLocalizations method !");
247   _type->clearGaussLocalizations();
248 }
249
250 /*!
251  * This method returns reference to the Gauss localization object corresponding to 'locId' id.
252  * 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
253  * MEDCouplingField::getNbOfGaussLocalization method.
254  * Warning this method is not const, so the returned object could be modified without any problem.
255  */
256 MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) throw(INTERP_KERNEL::Exception)
257 {
258   if(!_mesh)
259     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
260   return _type->getGaussLocalization(locId);
261 }
262
263 /*!
264  * This method returns reference to the Gauss localization object corresponding to 'locId' id.
265  * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) and if several localization ids have been found
266  * for a type.
267  */
268 int MEDCouplingField::getGaussLocalizationIdOfOneType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
269 {
270   if(!_mesh)
271     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneType method !");
272   return _type->getGaussLocalizationIdOfOneType(type);
273 }
274
275 std::set<int> MEDCouplingField::getGaussLocalizationIdsOfOneType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
276 {
277   if(!_mesh)
278     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdsOfOneType method !");
279   return _type->getGaussLocalizationIdsOfOneType(type);
280 }
281
282 /*!
283  * This method returns number of Gauss localization available. Implicitely all ids in [0,getNbOfGaussLocalization()) is a valid Gauss localisation id.
284  * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss)
285  */
286 int MEDCouplingField::getNbOfGaussLocalization() const throw(INTERP_KERNEL::Exception)
287 {
288   if(!_mesh)
289     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getNbOfGaussLocalization method !");
290   return _type->getNbOfGaussLocalization();
291 }
292
293 /*!
294  * This method returns an id of Gauss localization in [0,getNbOfGaussLocalization()) that corresponds to the localization of the cell specified by its cellId.
295  * 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
296  * Gauss localization has been set.
297  */
298 int MEDCouplingField::getGaussLocalizationIdOfOneCell(int cellId) const throw(INTERP_KERNEL::Exception)
299 {
300   if(!_mesh)
301     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
302   return _type->getGaussLocalizationIdOfOneCell(cellId);
303 }
304
305 /*!
306  * This method returns all cellIds that share the same Gauss localization given by 'locId' parameter (in range [0,getNbOfGaussLocalization()) ).
307  * If no cells fit the Gauss localization given by 'locId' cellIds will be returned empty.
308  * @param locId input that specifies the id of Gauss localization.
309  * @param cellIds output parameter, that will contain the result if this method succeds. This parameter is systematically cleared when called.
310  * @throw  if there is no mesh, invalid FieldDescription (different from Gauss) or if locId not in [0,getNbOfGaussLocalization())
311  */
312 void MEDCouplingField::getCellIdsHavingGaussLocalization(int locId, std::vector<int>& cellIds) const throw(INTERP_KERNEL::Exception)
313 {
314   cellIds.clear();
315   if(!_mesh)
316     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
317   _type->getCellIdsHavingGaussLocalization(locId,cellIds);
318 }
319
320 /*!
321  * This method returns reference to the Gauss localization object corresponding to 'locId' id.
322  * 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
323  * MEDCouplingField::getNbOfGaussLocalization method.
324  * Warning this method is const.
325  */
326 const MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) const throw(INTERP_KERNEL::Exception)
327 {
328   if(!_mesh)
329     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
330   return _type->getGaussLocalization(locId);
331 }
332
333 MEDCouplingField::~MEDCouplingField()
334 {
335   if(_mesh)
336     _mesh->decrRef();
337 }
338
339 MEDCouplingField::MEDCouplingField(MEDCouplingFieldDiscretization *type, NatureOfField nature):_nature(nature),_mesh(0),_type(type)
340 {
341 }
342
343 MEDCouplingField::MEDCouplingField(TypeOfField type):_nature(NoNature),_mesh(0),_type(MEDCouplingFieldDiscretization::New(type))
344 {
345 }
346
347 MEDCouplingField::MEDCouplingField(const MEDCouplingField& other, bool deepCopy):RefCountObject(other),_name(other._name),_desc(other._desc),_nature(other._nature),
348                                                                                  _mesh(0),_type(0)
349 {
350   if(other._mesh)
351     {
352       _mesh=other._mesh;
353       _mesh->incrRef();
354     }
355   if(deepCopy)
356     _type=other._type->clone();
357   else
358     _type=other._type;
359 }
360
361 /*!
362  * This method returns a submesh of 'mesh' instance constituting cell ids contained in array defined as an interval [start;end).
363  * @param di is an array returned that specifies entity ids (nodes, cells ids...) in mesh 'mesh' of entity in returned submesh.
364  */
365 MEDCouplingMesh *MEDCouplingField::buildSubMeshData(const int *start, const int *end, DataArrayInt *&di) const
366 {
367   return _type->buildSubMeshData(_mesh,start,end,di);
368 }
369
370 /*!
371  * This method returns tuples ids implied by the mesh selection of the  cell ids contained in array defined as an interval [start;end).
372  * \return a newly allocated DataArrayInt instance containing tuples ids.
373  */
374 DataArrayInt *MEDCouplingField::computeTupleIdsToSelectFromCellIds(const int *startCellIds, const int *endCellIds) const
375 {
376   return _type->computeTupleIdsToSelectFromCellIds(_mesh,startCellIds,endCellIds);
377 }
378
379 /*!
380  * This method returns number of tuples expected regarding its discretization and its _mesh attribute.
381  * This method expected a not null _mesh instance. If null, an exception will be thrown.
382  */
383 int MEDCouplingField::getNumberOfTuplesExpected() const throw(INTERP_KERNEL::Exception)
384 {
385   if(_mesh)
386     return _type->getNumberOfTuples(_mesh);
387   else
388     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfTuplesExpected : Empty mesh !");
389 }
390
391 void MEDCouplingField::setDiscretization(MEDCouplingFieldDiscretization *newDisc)
392 {
393   _type=newDisc;
394   if(newDisc)
395     newDisc->incrRef();
396 }
397
398 /*!
399  * This method returns number of mesh placed expected regarding its discretization and its _mesh attribute.
400  * This method expected a not null _mesh instance. If null, an exception will be thrown.
401  */
402 int MEDCouplingField::getNumberOfMeshPlacesExpected() const throw(INTERP_KERNEL::Exception)
403 {
404   if(_mesh)
405     return _type->getNumberOfMeshPlaces(_mesh);
406   else
407     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfMeshPlacesExpected : Empty mesh !");
408 }
409
410 /*!
411  * Copy tiny info (component names, name, description) but warning the underlying mesh is not renamed (for safety reason).
412  */
413 void MEDCouplingField::copyTinyStringsFrom(const MEDCouplingField *other) throw(INTERP_KERNEL::Exception)
414 {
415   if(other)
416     {
417       setName(other->_name.c_str());
418       setDescription(other->_desc.c_str());    
419     }
420 }