Salome HOME
buildInnerBoundaryAlongM1Group: bug fix for singular point in 3D
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingFieldTemplate.cxx
1 // Copyright (C) 2007-2020  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 (EDF R&D)
20
21 #include "MEDCouplingFieldTemplate.hxx"
22 #include "MEDCouplingMesh.hxx"
23 #include "MEDCouplingFieldInt32.hxx"
24 #include "MEDCouplingFieldInt64.hxx"
25 #include "MEDCouplingFieldFloat.hxx"
26 #include "MEDCouplingFieldDouble.hxx"
27 #include "MEDCouplingFieldDiscretization.hxx"
28
29 #include <sstream>
30
31 using namespace MEDCoupling;
32
33 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::New(const MEDCouplingFieldDouble& f)
34 {
35   return new MEDCouplingFieldTemplate(f,true);
36 }
37
38 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::New(const MEDCouplingFieldFloat& f)
39 {
40   return new MEDCouplingFieldTemplate(f,true);
41 }
42
43 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::New(const MEDCouplingFieldInt32& f)
44 {
45   return new MEDCouplingFieldTemplate(f,true);
46 }
47
48 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::New(const MEDCouplingFieldInt64& f)
49 {
50   return new MEDCouplingFieldTemplate(f,true);
51 }
52
53 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::NewWithoutCheck(const MEDCouplingFieldDouble& f)
54 {
55   return new MEDCouplingFieldTemplate(f,false);
56 }
57
58 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::NewWithoutCheck(const MEDCouplingFieldFloat& f)
59 {
60   return new MEDCouplingFieldTemplate(f,false);
61 }
62
63 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::NewWithoutCheck(const MEDCouplingFieldInt32& f)
64 {
65   return new MEDCouplingFieldTemplate(f,false);
66 }
67
68 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::NewWithoutCheck(const MEDCouplingFieldInt64& f)
69 {
70   return new MEDCouplingFieldTemplate(f,false);
71 }
72
73 bool MEDCouplingFieldTemplate::isEqualIfNotWhy(const MEDCouplingFieldTemplate *other, double meshPrec, std::string& reason) const
74 {
75   return isEqualIfNotWhyProtected(other,meshPrec,reason);
76 }
77
78 bool MEDCouplingFieldTemplate::isEqual(const MEDCouplingFieldTemplate *other, double meshPrec) const
79 {
80   std::string tmp;
81   return isEqualIfNotWhyProtected(other,meshPrec,tmp);
82 }
83
84 bool MEDCouplingFieldTemplate::isEqualWithoutConsideringStr(const MEDCouplingFieldTemplate *other, double meshPrec) const
85 {
86   return isEqualWithoutConsideringStrProtected(other,meshPrec);
87 }
88
89 /*!
90  * The user should \b not use this method. Only useful for CORBA serialization/unserialization.
91  */
92 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::New(TypeOfField type)
93 {
94   return new MEDCouplingFieldTemplate(type);
95 }
96
97 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldDouble& f, bool isChecked):MEDCouplingField(f,false) 
98 {
99   forceTimeOfThis(f);
100   if(isChecked)
101     checkConsistencyLight();
102 }
103
104 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldFloat& f, bool isChecked):MEDCouplingField(f,false) 
105 {
106   forceTimeOfThis(f);
107   if(isChecked)
108     checkConsistencyLight();
109 }
110
111 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldInt32& f, bool isChecked):MEDCouplingField(f,false) 
112 {
113   forceTimeOfThis(f);
114   if(isChecked)
115     checkConsistencyLight();
116 }
117
118 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldInt64& f, bool isChecked):MEDCouplingField(f,false) 
119 {
120   forceTimeOfThis(f);
121   if(isChecked)
122     checkConsistencyLight();
123 }
124
125 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(TypeOfField type):MEDCouplingField(type)
126 {
127 }
128
129 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldTemplate& other, bool deepCopy):MEDCouplingField(other,deepCopy)
130 {
131 }
132
133 void MEDCouplingFieldTemplate::checkConsistencyLight() const
134 {
135   if(_mesh==0)
136     throw INTERP_KERNEL::Exception("MEDCouplingFieldTemplate::checkConsistencyLight : Empty mesh !");
137 }
138
139 std::string MEDCouplingFieldTemplate::simpleRepr() const
140 {
141   std::ostringstream ret;
142   ret << "FieldTemplate with name : \"" << getName() << "\"\n";
143   ret << "Description of field is : \"" << getDescription() << "\"\n";
144   if(_type)
145     { ret << "FieldTemplate space discretization is : " << _type->getStringRepr() << "\n"; }
146   else
147     { ret << "FieldTemplate has no spatial discretization !\n"; }
148   ret << "FieldTemplate nature of field is : \"" << MEDCouplingNatureOfField::GetReprNoThrow(_nature) << "\"\n";
149   if(_mesh)
150     ret << "Mesh support information :\n__________________________\n" << _mesh->simpleRepr();
151   else
152     ret << "Mesh support information : No mesh set !\n";
153   return ret.str();
154 }
155
156 std::string MEDCouplingFieldTemplate::advancedRepr() const
157 {
158   return simpleRepr();
159 }
160
161 void MEDCouplingFieldTemplate::getTinySerializationIntInformation(std::vector<mcIdType>& tinyInfo) const
162 {
163   if(!((const MEDCouplingFieldDiscretization *)_type))
164     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationIntInformation !");
165   tinyInfo.clear();
166   tinyInfo.push_back(ToIdType(_type->getEnum()));
167   tinyInfo.push_back(ToIdType(_nature));
168   std::vector<mcIdType> tinyInfo2;
169   _type->getTinySerializationIntInformation(tinyInfo2);
170   tinyInfo.insert(tinyInfo.end(),tinyInfo2.begin(),tinyInfo2.end());
171   tinyInfo.push_back(ToIdType(tinyInfo2.size()));
172 }
173
174 void MEDCouplingFieldTemplate::getTinySerializationDbleInformation(std::vector<double>& tinyInfo) const
175 {
176   if(!((const MEDCouplingFieldDiscretization *)_type))
177     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationDbleInformation !");
178   tinyInfo.clear();
179   _type->getTinySerializationDbleInformation(tinyInfo);
180 }
181
182 void MEDCouplingFieldTemplate::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
183 {
184   tinyInfo.clear();
185   tinyInfo.push_back(_name);
186   tinyInfo.push_back(_desc);
187 }
188
189 void MEDCouplingFieldTemplate::resizeForUnserialization(const std::vector<mcIdType>& tinyInfoI, DataArrayIdType *&dataInt)
190 {
191   if(!((const MEDCouplingFieldDiscretization *)_type))
192     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform resizeForUnserialization !");
193   dataInt=0;
194   std::vector<mcIdType> tinyInfoITmp(tinyInfoI.begin()+2,tinyInfoI.end());
195   _type->resizeForUnserialization(tinyInfoITmp,dataInt);
196 }
197
198 void MEDCouplingFieldTemplate::finishUnserialization(const std::vector<mcIdType>& tinyInfoI, const std::vector<double>& tinyInfoD, const std::vector<std::string>& tinyInfoS)
199 {
200   if(!((const MEDCouplingFieldDiscretization *)_type))
201     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform finishUnserialization !");
202   _nature=(NatureOfField)tinyInfoI[1];
203   _type->finishUnserialization(tinyInfoD);
204   _name=tinyInfoS[0];
205   _desc=tinyInfoS[1];
206 }
207
208 void MEDCouplingFieldTemplate::serialize(DataArrayIdType *&dataInt) const
209 {
210   _type->getSerializationIntArray(dataInt);
211 }
212
213 void MEDCouplingFieldTemplate::reprQuickOverview(std::ostream& stream) const
214 {
215   stream << "MEDCouplingFieldTemplate C++ instance at " << this << ". Name : \"" << _name << "\"." << std::endl;
216   const char *nat=0;
217   try
218   {
219       nat=MEDCouplingNatureOfField::GetRepr(_nature);
220       stream << "Nature of field template : " << nat << ".\n";
221   }
222   catch(INTERP_KERNEL::Exception& /*e*/)
223   {  }
224   const MEDCouplingFieldDiscretization *fd(_type);
225   if(!fd)
226     stream << "No spatial discretization set !";
227   else
228     fd->reprQuickOverview(stream);
229   stream << std::endl;
230   if(!_mesh)
231     stream << "\nNo mesh support defined !";
232   else
233     {
234       std::ostringstream oss;
235       _mesh->reprQuickOverview(oss);
236       std::string tmp(oss.str());
237       stream << "\nMesh info : " << tmp.substr(0,tmp.find('\n'));
238     }
239 }
240
241 MCAuto<MEDCouplingFieldTemplate> MEDCouplingFieldTemplate::clone(bool recDeepCpy) const
242 {
243   MCAuto<MEDCouplingFieldTemplate> ret(new MEDCouplingFieldTemplate(*this,recDeepCpy));
244   return ret;
245 }