Salome HOME
Copyrights update 2015.
[modules/med.git] / src / MEDCoupling / MEDCouplingFieldTemplate.cxx
1 // Copyright (C) 2007-2015  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 "MEDCouplingFieldDouble.hxx"
24 #include "MEDCouplingFieldDiscretization.hxx"
25
26 #include <sstream>
27
28 using namespace ParaMEDMEM;
29
30 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::New(const MEDCouplingFieldDouble& f)
31 {
32   return new MEDCouplingFieldTemplate(f);
33 }
34
35 /*!
36  * The user should \b not use this method. Only useful for CORBA serialization/unserialization.
37  */
38 MEDCouplingFieldTemplate *MEDCouplingFieldTemplate::New(TypeOfField type)
39 {
40   return new MEDCouplingFieldTemplate(type);
41 }
42
43 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(const MEDCouplingFieldDouble& f):MEDCouplingField(f,false) 
44 {
45   forceTimeOfThis(f);
46   checkCoherency();
47 }
48
49 MEDCouplingFieldTemplate::MEDCouplingFieldTemplate(TypeOfField type):MEDCouplingField(type)
50 {
51 }
52
53 void MEDCouplingFieldTemplate::checkCoherency() const
54 {
55   if(_mesh==0)
56     throw INTERP_KERNEL::Exception("MEDCouplingFieldTemplate::checkCoherency : Empty mesh !");
57 }
58
59 std::string MEDCouplingFieldTemplate::simpleRepr() const
60 {
61   std::ostringstream ret;
62   ret << "FieldTemplate with name : \"" << getName() << "\"\n";
63   ret << "Description of field is : \"" << getDescription() << "\"\n";
64   if(_type)
65     { ret << "FieldTemplate space discretization is : " << _type->getStringRepr() << "\n"; }
66   else
67     { ret << "FieldTemplate has no spatial discretization !\n"; }
68   ret << "FieldTemplate nature of field is : \"" << MEDCouplingNatureOfField::GetReprNoThrow(_nature) << "\"\n";
69   if(_mesh)
70     ret << "Mesh support information :\n__________________________\n" << _mesh->simpleRepr();
71   else
72     ret << "Mesh support information : No mesh set !\n";
73   return ret.str();
74 }
75
76 std::string MEDCouplingFieldTemplate::advancedRepr() const
77 {
78   return simpleRepr();
79 }
80
81 void MEDCouplingFieldTemplate::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
82 {
83   if(!((const MEDCouplingFieldDiscretization *)_type))
84     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationIntInformation !");
85   tinyInfo.clear();
86   tinyInfo.push_back((int)_type->getEnum());
87   tinyInfo.push_back((int)_nature);
88   std::vector<int> tinyInfo2;
89   _type->getTinySerializationIntInformation(tinyInfo2);
90   tinyInfo.insert(tinyInfo.end(),tinyInfo2.begin(),tinyInfo2.end());
91   tinyInfo.push_back((int)tinyInfo2.size());
92 }
93
94 void MEDCouplingFieldTemplate::getTinySerializationDbleInformation(std::vector<double>& tinyInfo) const
95 {
96   if(!((const MEDCouplingFieldDiscretization *)_type))
97     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform getTinySerializationDbleInformation !");
98   tinyInfo.clear();
99   _type->getTinySerializationDbleInformation(tinyInfo);
100 }
101
102 void MEDCouplingFieldTemplate::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
103 {
104   tinyInfo.clear();
105   tinyInfo.push_back(_name);
106   tinyInfo.push_back(_desc);
107 }
108
109 void MEDCouplingFieldTemplate::resizeForUnserialization(const std::vector<int>& tinyInfoI, DataArrayInt *&dataInt)
110 {
111   if(!((const MEDCouplingFieldDiscretization *)_type))
112     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform resizeForUnserialization !");
113   dataInt=0;
114   std::vector<int> tinyInfoITmp(tinyInfoI.begin()+2,tinyInfoI.end());
115   _type->resizeForUnserialization(tinyInfoITmp,dataInt);
116 }
117
118 void MEDCouplingFieldTemplate::finishUnserialization(const std::vector<int>& tinyInfoI, const std::vector<double>& tinyInfoD, const std::vector<std::string>& tinyInfoS)
119 {
120   if(!((const MEDCouplingFieldDiscretization *)_type))
121     throw INTERP_KERNEL::Exception("No spatial discretization underlying this field to perform finishUnserialization !");
122   _nature=(NatureOfField)tinyInfoI[1];
123   _type->finishUnserialization(tinyInfoD);
124   _name=tinyInfoS[0];
125   _desc=tinyInfoS[1];
126 }
127
128 void MEDCouplingFieldTemplate::serialize(DataArrayInt *&dataInt) const
129 {
130   _type->getSerializationIntArray(dataInt);
131 }
132
133 void MEDCouplingFieldTemplate::reprQuickOverview(std::ostream& stream) const
134 {
135   stream << "MEDCouplingFieldTemplate C++ instance at " << this << ". Name : \"" << _name << "\"." << std::endl;
136   const char *nat=0;
137   try
138   {
139       nat=MEDCouplingNatureOfField::GetRepr(_nature);
140       stream << "Nature of field template : " << nat << ".\n";
141   }
142   catch(INTERP_KERNEL::Exception& /*e*/)
143   {  }
144   const MEDCouplingFieldDiscretization *fd(_type);
145   if(!fd)
146     stream << "No spatial discretization set !";
147   else
148     fd->reprQuickOverview(stream);
149   stream << std::endl;
150   if(!_mesh)
151     stream << "\nNo mesh support defined !";
152   else
153     {
154       std::ostringstream oss;
155       _mesh->reprQuickOverview(oss);
156       std::string tmp(oss.str());
157       stream << "\nMesh info : " << tmp.substr(0,tmp.find('\n'));
158     }
159 }