Salome HOME
805b4a7c595cadaf29bb122ea82bf394f2829c4a
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingField.cxx
1 // Copyright (C) 2007-2012  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 TypeOfField MEDCouplingField::getTypeOfField() const
131 {
132   return _type->getEnum();
133 }
134
135 /*!
136  * This method returns the nature of field. This information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
137  * 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.
138  * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref NatureOfField "here".
139  */
140 NatureOfField MEDCouplingField::getNature() const
141 {
142   return _nature;
143 }
144
145 /*!
146  * This method set the nature of field in \b this.This  information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
147  * 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.
148  * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref TableNatureOfField "here".
149  */
150 void MEDCouplingField::setNature(NatureOfField nat) throw(INTERP_KERNEL::Exception)
151 {
152   _nature=nat;
153 }
154
155 /*!
156  * This method returns is case of success an instance of DataArrayDouble the user is in reponsability to deal with.
157  * If 'this->_mesh' is not set an exception will be thrown.
158  * For a field on node the array of coords will be returned. For a field on cell a ParaMEDMEM::DataArrayDouble instance
159  * containing the barycenter of cells will be returned. And for a field on gauss point the explicit position of gauss points.
160  */
161 DataArrayDouble *MEDCouplingField::getLocalizationOfDiscr() const throw(INTERP_KERNEL::Exception)
162 {
163   if(!_mesh)
164     throw INTERP_KERNEL::Exception("MEDCouplingField::getLocalizationOfDiscr : No mesh set !");
165   return _type->getLocalizationOfDiscValues(_mesh);
166 }
167
168 /*!
169  * This method retrieves the measure field of 'this'. If no '_mesh' is defined an exception will be thrown.
170  * Warning the retrieved field life cycle is the responsability of caller.
171  */
172 MEDCouplingFieldDouble *MEDCouplingField::buildMeasureField(bool isAbs) const throw(INTERP_KERNEL::Exception)
173 {
174   if(_mesh==0)
175     throw INTERP_KERNEL::Exception("MEDCouplingField::getMeasureField : no mesh defined !!!");
176   return _type->getMeasureField(_mesh,isAbs);
177 }
178
179 void MEDCouplingField::setMesh(const MEDCouplingMesh *mesh)
180 {
181   if(mesh!=_mesh)
182     {
183       if(_mesh)
184         _mesh->decrRef();
185       _mesh=mesh;
186       if(_mesh)
187         {
188           _mesh->incrRef();
189           updateTimeWith(*_mesh);
190         }
191     }
192 }
193
194 /*!
195  * This method sets gauss localization by geometric type.
196  * @param type geometric type on which the gauss localization will be set.
197  * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
198  * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
199  * @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.
200  * @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.
201  */
202 void MEDCouplingField::setGaussLocalizationOnType(INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
203                                                   const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
204 {
205   if(!_mesh)
206     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnType method !");
207   _type->setGaussLocalizationOnType(_mesh,type,refCoo,gsCoo,wg);
208 }
209
210 /*!
211  * 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.
212  * If an incoherence appears an exception will be thrown and no seting will be performed.
213  * An exception is thrown too if [begin,end) has a size lesser than 1.
214  * 
215  * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
216  * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
217  * @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.
218  * @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.
219  */
220 void MEDCouplingField::setGaussLocalizationOnCells(const int *begin, const int *end, const std::vector<double>& refCoo,
221                                                    const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
222 {
223   if(!_mesh)
224     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnCells method !");
225   _type->setGaussLocalizationOnCells(_mesh,begin,end,refCoo,gsCoo,wg);
226 }
227
228 /*!
229  * This method resets all Gauss loalizations if any.
230  */
231 void MEDCouplingField::clearGaussLocalizations()
232 {
233   if(!_mesh)
234     throw INTERP_KERNEL::Exception("Mesh has to be set before calling clearGaussLocalizations method !");
235   _type->clearGaussLocalizations();
236 }
237
238 /*!
239  * This method returns reference to the Gauss localization object corresponding to 'locId' id.
240  * 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
241  * MEDCouplingField::getNbOfGaussLocalization method.
242  * Warning this method is not const, so the returned object could be modified without any problem.
243  */
244 MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) throw(INTERP_KERNEL::Exception)
245 {
246   if(!_mesh)
247     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
248   return _type->getGaussLocalization(locId);
249 }
250
251 /*!
252  * This method returns reference to the Gauss localization object corresponding to 'locId' id.
253  * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) and if several localization ids have been found
254  * for a type.
255  */
256 int MEDCouplingField::getGaussLocalizationIdOfOneType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
257 {
258   if(!_mesh)
259     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneType method !");
260   return _type->getGaussLocalizationIdOfOneType(type);
261 }
262
263 std::set<int> MEDCouplingField::getGaussLocalizationIdsOfOneType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
264 {
265   if(!_mesh)
266     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdsOfOneType method !");
267   return _type->getGaussLocalizationIdsOfOneType(type);
268 }
269
270 /*!
271  * This method returns number of Gauss localization available. Implicitely all ids in [0,getNbOfGaussLocalization()) is a valid Gauss localisation id.
272  * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss)
273  */
274 int MEDCouplingField::getNbOfGaussLocalization() const throw(INTERP_KERNEL::Exception)
275 {
276   if(!_mesh)
277     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getNbOfGaussLocalization method !");
278   return _type->getNbOfGaussLocalization();
279 }
280
281 /*!
282  * This method returns an id of Gauss localization in [0,getNbOfGaussLocalization()) that corresponds to the localization of the cell specified by its cellId.
283  * 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
284  * Gauss localization has been set.
285  */
286 int MEDCouplingField::getGaussLocalizationIdOfOneCell(int cellId) const throw(INTERP_KERNEL::Exception)
287 {
288   if(!_mesh)
289     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
290   return _type->getGaussLocalizationIdOfOneCell(cellId);
291 }
292
293 /*!
294  * This method returns all cellIds that share the same Gauss localization given by 'locId' parameter (in range [0,getNbOfGaussLocalization()) ).
295  * If no cells fit the Gauss localization given by 'locId' cellIds will be returned empty.
296  * @param locId input that specifies the id of Gauss localization.
297  * @param cellIds output parameter, that will contain the result if this method succeds. This parameter is systematically cleared when called.
298  * @throw  if there is no mesh, invalid FieldDescription (different from Gauss) or if locId not in [0,getNbOfGaussLocalization())
299  */
300 void MEDCouplingField::getCellIdsHavingGaussLocalization(int locId, std::vector<int>& cellIds) const throw(INTERP_KERNEL::Exception)
301 {
302   cellIds.clear();
303   if(!_mesh)
304     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
305   _type->getCellIdsHavingGaussLocalization(locId,cellIds);
306 }
307
308 /*!
309  * This method returns reference to the Gauss localization object corresponding to 'locId' id.
310  * 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
311  * MEDCouplingField::getNbOfGaussLocalization method.
312  * Warning this method is const.
313  */
314 const MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) const throw(INTERP_KERNEL::Exception)
315 {
316   if(!_mesh)
317     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
318   return _type->getGaussLocalization(locId);
319 }
320
321 MEDCouplingField::~MEDCouplingField()
322 {
323   if(_mesh)
324     _mesh->decrRef();
325 }
326
327 MEDCouplingField::MEDCouplingField(MEDCouplingFieldDiscretization *type, NatureOfField nature):_nature(nature),_mesh(0),_type(type)
328 {
329 }
330
331 MEDCouplingField::MEDCouplingField(TypeOfField type):_nature(NoNature),_mesh(0),_type(MEDCouplingFieldDiscretization::New(type))
332 {
333 }
334
335 MEDCouplingField::MEDCouplingField(const MEDCouplingField& other):RefCountObject(other),_name(other._name),_desc(other._desc),_nature(other._nature),
336                                                                   _mesh(0),_type(other._type->clone())
337 {
338   if(other._mesh)
339     {
340       _mesh=other._mesh;
341       _mesh->incrRef();
342     }
343 }
344
345 /*!
346  * This method returns a submesh of 'mesh' instance constituting cell ids contained in array defined as an interval [start;end).
347  * @param di is an array returned that specifies entity ids (nodes, cells ids...) in mesh 'mesh' of entity in returned submesh.
348  */
349 MEDCouplingMesh *MEDCouplingField::buildSubMeshData(const int *start, const int *end, DataArrayInt *&di) const
350 {
351   return _type->buildSubMeshData(_mesh,start,end,di);
352 }
353
354 /*!
355  * This method returns tuples ids implied by the mesh selection of the  cell ids contained in array defined as an interval [start;end).
356  * \return a newly allocated DataArrayInt instance containing tuples ids.
357  */
358 DataArrayInt *MEDCouplingField::computeTupleIdsToSelectFromCellIds(const int *startCellIds, const int *endCellIds) const
359 {
360   return _type->computeTupleIdsToSelectFromCellIds(_mesh,startCellIds,endCellIds);
361 }
362
363 /*!
364  * This method returns number of tuples expected regarding its discretization and its _mesh attribute.
365  * This method expected a not null _mesh instance. If null, an exception will be thrown.
366  */
367 int MEDCouplingField::getNumberOfTuplesExpected() const throw(INTERP_KERNEL::Exception)
368 {
369   if(_mesh)
370     return _type->getNumberOfTuples(_mesh);
371   else
372     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfTuplesExpected : Empty mesh !");
373 }
374
375 /*!
376  * This method returns number of mesh placed expected regarding its discretization and its _mesh attribute.
377  * This method expected a not null _mesh instance. If null, an exception will be thrown.
378  */
379 int MEDCouplingField::getNumberOfMeshPlacesExpected() const throw(INTERP_KERNEL::Exception)
380 {
381   if(_mesh)
382     return _type->getNumberOfMeshPlaces(_mesh);
383   else
384     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfMeshPlacesExpected : Empty mesh !");
385 }