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