Salome HOME
Mesh::getNumberOfCells returns std::size_t
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingFieldTemplate.cxx
1 // Copyright (C) 2007-2016  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 /*!
63  * The user should \b not use this method. Only useful for CORBA serialization/unserialization.
64  */
65 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::New(TypeOfField type)
66 {
67   return new MEDCouplingFieldTemplate(type);
68 }
69
70 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldDouble& f, bool isChecked):MEDCouplingField(f,false) 
71 {
72   forceTimeOfThis(f);
73   if(isChecked)
74     checkConsistencyLight();
75 }
76
77 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldFloat& f, bool isChecked):MEDCouplingField(f,false) 
78 {
79   forceTimeOfThis(f);
80   if(isChecked)
81     checkConsistencyLight();
82 }
83
84 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldInt& f, bool isChecked):MEDCouplingField(f,false) 
85 {
86   forceTimeOfThis(f);
87   if(isChecked)
88     checkConsistencyLight();
89 }
90
91 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(TypeOfField type):MEDCouplingField(type)
92 {
93 }
94
95 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldTemplate& other, bool deepCopy):MEDCouplingField(other,deepCopy)
96 {
97 }
98
99 void MEDCouplingFieldTemplate::checkConsistencyLight() const
100 {
101   if(_mesh==0)
102     throw INTERP_KERNEL::Exception("MEDCouplingFieldTemplate::checkConsistencyLight : Empty mesh !");
103 }
104
105 std::string MEDCouplingFieldTemplate::simpleRepr() const
106 {
107   std::ostringstream ret;
108   ret << "FieldTemplate with name : \"" << getName() << "\"\n";
109   ret << "Description of field is : \"" << getDescription() << "\"\n";
110   if(_type)
111     { ret << "FieldTemplate space discretization is : " << _type->getStringRepr() << "\n"; }
112   else
113     { ret << "FieldTemplate has no spatial discretization !\n"; }
114   ret << "FieldTemplate nature of field is : \"" << MEDCouplingNatureOfField::GetReprNoThrow(_nature) << "\"\n";
115   if(_mesh)
116     ret << "Mesh support information :\n__________________________\n" << _mesh->simpleRepr();
117   else
118     ret << "Mesh support information : No mesh set !\n";
119   return ret.str();
120 }
121
122 std::string MEDCouplingFieldTemplate::advancedRepr() const
123 {
124   return simpleRepr();
125 }
126
127 void MEDCouplingFieldTemplate::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
128 {
129   if(!((const MEDCouplingFieldDiscretization *)_type))
130     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationIntInformation !");
131   tinyInfo.clear();
132   tinyInfo.push_back((int)_type->getEnum());
133   tinyInfo.push_back((int)_nature);
134   std::vector<int> tinyInfo2;
135   _type->getTinySerializationIntInformation(tinyInfo2);
136   tinyInfo.insert(tinyInfo.end(),tinyInfo2.begin(),tinyInfo2.end());
137   tinyInfo.push_back((int)tinyInfo2.size());
138 }
139
140 void MEDCouplingFieldTemplate::getTinySerializationDbleInformation(std::vector<double>& tinyInfo) const
141 {
142   if(!((const MEDCouplingFieldDiscretization *)_type))
143     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationDbleInformation !");
144   tinyInfo.clear();
145   _type->getTinySerializationDbleInformation(tinyInfo);
146 }
147
148 void MEDCouplingFieldTemplate::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
149 {
150   tinyInfo.clear();
151   tinyInfo.push_back(_name);
152   tinyInfo.push_back(_desc);
153 }
154
155 void MEDCouplingFieldTemplate::resizeForUnserialization(const std::vector<int>& tinyInfoI, DataArrayInt *&dataInt)
156 {
157   if(!((const MEDCouplingFieldDiscretization *)_type))
158     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform resizeForUnserialization !");
159   dataInt=0;
160   std::vector<int> tinyInfoITmp(tinyInfoI.begin()+2,tinyInfoI.end());
161   _type->resizeForUnserialization(tinyInfoITmp,dataInt);
162 }
163
164 void MEDCouplingFieldTemplate::finishUnserialization(const std::vector<int>& tinyInfoI, const std::vector<double>& tinyInfoD, const std::vector<std::string>& tinyInfoS)
165 {
166   if(!((const MEDCouplingFieldDiscretization *)_type))
167     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform finishUnserialization !");
168   _nature=(NatureOfField)tinyInfoI[1];
169   _type->finishUnserialization(tinyInfoD);
170   _name=tinyInfoS[0];
171   _desc=tinyInfoS[1];
172 }
173
174 void MEDCouplingFieldTemplate::serialize(DataArrayInt *&dataInt) const
175 {
176   _type->getSerializationIntArray(dataInt);
177 }
178
179 void MEDCouplingFieldTemplate::reprQuickOverview(std::ostream& stream) const
180 {
181   stream << "MEDCouplingFieldTemplate C++ instance at " << this << ". Name : \"" << _name << "\"." << std::endl;
182   const char *nat=0;
183   try
184   {
185       nat=MEDCouplingNatureOfField::GetRepr(_nature);
186       stream << "Nature of field template : " << nat << ".\n";
187   }
188   catch(INTERP_KERNEL::Exception& /*e*/)
189   {  }
190   const MEDCouplingFieldDiscretization *fd(_type);
191   if(!fd)
192     stream << "No spatial discretization set !";
193   else
194     fd->reprQuickOverview(stream);
195   stream << std::endl;
196   if(!_mesh)
197     stream << "\nNo mesh support defined !";
198   else
199     {
200       std::ostringstream oss;
201       _mesh->reprQuickOverview(oss);
202       std::string tmp(oss.str());
203       stream << "\nMesh info : " << tmp.substr(0,tmp.find('\n'));
204     }
205 }
206
207 MCAuto<MEDCouplingFieldTemplate> MEDCouplingFieldTemplate::clone(bool recDeepCpy) const
208 {
209   MCAuto<MEDCouplingFieldTemplate> ret(new MEDCouplingFieldTemplate(*this,recDeepCpy));
210   return ret;
211 }