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