Salome HOME
merge conflict
[modules/med.git] / src / medtool / src / MEDLoader / MEDFileData.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 "MEDFileData.hxx"
22
23 using namespace ParaMEDMEM;
24
25 MEDFileData *MEDFileData::New(const std::string& fileName)
26 {
27   return new MEDFileData(fileName);
28 }
29
30 MEDFileData *MEDFileData::New()
31 {
32   return new MEDFileData;
33 }
34
35 MEDFileData *MEDFileData::deepCpy() const
36 {
37   MEDCouplingAutoRefCountObjectPtr<MEDFileFields> fields;
38   if((const MEDFileFields *)_fields)
39     fields=_fields->deepCpy();
40   MEDCouplingAutoRefCountObjectPtr<MEDFileMeshes> meshes;
41   if((const MEDFileMeshes *)_meshes)
42     meshes=_meshes->deepCpy();
43   MEDCouplingAutoRefCountObjectPtr<MEDFileParameters> params;
44   if((const MEDFileParameters *)_params)
45     params=_params->deepCpy();
46   MEDCouplingAutoRefCountObjectPtr<MEDFileJoints> joints;
47   MEDCouplingAutoRefCountObjectPtr<MEDFileData> ret=MEDFileData::New();
48   ret->_fields=fields; ret->_meshes=meshes; ret->_params=params;
49   return ret.retn();
50 }
51
52 std::size_t MEDFileData::getHeapMemorySizeWithoutChildren() const
53 {
54   return 0;
55 }
56
57 std::vector<const BigMemoryObject *> MEDFileData::getDirectChildrenWithNull() const
58 {
59   std::vector<const BigMemoryObject *> ret;
60   ret.push_back((const MEDFileFields *)_fields);
61   ret.push_back((const MEDFileMeshes *)_meshes);
62   ret.push_back((const MEDFileParameters *)_params);
63   return ret;
64
65 }
66
67 /** Return a borrowed reference (caller is not responsible for object destruction) */
68 MEDFileFields *MEDFileData::getFields() const
69 {
70   return const_cast<MEDFileFields *>(static_cast<const MEDFileFields *>(_fields));
71 }
72
73 /** Return a borrowed reference (caller is not responsible for object destruction) */
74 MEDFileMeshes *MEDFileData::getMeshes() const
75 {
76   return const_cast<MEDFileMeshes *>(static_cast<const MEDFileMeshes *>(_meshes));
77 }
78
79 /** Return a borrowed reference (caller is not responsible for object destruction) */
80 MEDFileParameters *MEDFileData::getParams() const
81 {
82   return const_cast<MEDFileParameters *>(static_cast<const MEDFileParameters *>(_params));
83 }
84
85 void MEDFileData::setFields(MEDFileFields *fields)
86 {
87   if(fields)
88     fields->incrRef();
89   _fields=fields;
90 }
91
92 void MEDFileData::setMeshes(MEDFileMeshes *meshes)
93 {
94   if(meshes)
95     meshes->incrRef();
96   _meshes=meshes;
97 }
98
99 void MEDFileData::setParams(MEDFileParameters *params)
100 {
101   if(params)
102     params->incrRef();
103   _params=params;
104 }
105
106 int MEDFileData::getNumberOfFields() const
107 {
108   const MEDFileFields *f=_fields;
109   if(!f)
110     throw INTERP_KERNEL::Exception("MEDFileData::getNumberOfFields : no fields set !");
111   return f->getNumberOfFields();
112 }
113
114 int MEDFileData::getNumberOfMeshes() const
115 {
116   const MEDFileMeshes *m=_meshes;
117   if(!m)
118     throw INTERP_KERNEL::Exception("MEDFileData::getNumberOfMeshes : no meshes set !");
119   return m->getNumberOfMeshes();
120 }
121
122 int MEDFileData::getNumberOfParams() const
123 {
124   const MEDFileParameters *p=_params;
125   if(!p)
126     throw INTERP_KERNEL::Exception("MEDFileData::getNumberOfParams : no params set !");
127   return p->getNumberOfParams();
128 }
129
130 std::string MEDFileData::simpleRepr() const
131 {
132   std::ostringstream oss;
133   oss << "(***************)\n(* MEDFileData *)\n(***************)\n\nFields part :\n*************\n\n";
134   const MEDFileFields *tmp=_fields;
135   if(tmp)
136     {
137       tmp->simpleRepr(0,oss);
138       oss << std::endl;
139     }
140   else
141     oss << "No fields set !!!\n\n";
142   oss << "Meshes part :\n*************\n\n";
143   const MEDFileMeshes *tmp2=_meshes;
144   if(tmp2)
145     {
146       tmp2->simpleReprWithoutHeader(oss);
147     }
148   else
149     oss << "No meshes set !!!\n\n";
150   oss << "Params part :\n*************\n\n";
151   const MEDFileParameters *tmp3=_params;
152   if(tmp3)
153     {
154       tmp3->simpleReprWithoutHeader(oss);
155     }
156   else
157     oss << "No params set !!!\n";
158   return oss.str();
159 }
160
161 bool MEDFileData::changeMeshNames(const std::vector< std::pair<std::string,std::string> >& modifTab)
162 {
163   bool ret=false;
164   MEDFileFields *fields=_fields;
165   if(fields)
166     ret=fields->changeMeshNames(modifTab) || ret;
167   MEDFileMeshes *meshes=_meshes;
168   if(meshes)
169     ret=meshes->changeNames(modifTab) || ret;
170   return ret;
171 }
172
173 bool MEDFileData::changeMeshName(const std::string& oldMeshName, const std::string& newMeshName)
174 {
175   std::string oldName(oldMeshName);
176   std::vector< std::pair<std::string,std::string> > v(1);
177   v[0].first=oldName; v[0].second=newMeshName;
178   return changeMeshNames(v);
179 }
180
181 /*!
182  * This method performs unpolyzation in meshes in \a this and if it leads to a modification to one or more than one meshes in \a this 
183  * the fields are automatically renumbered (for those impacted, that is to say here fields on cells and fields on gauss points on impacted fields)
184  *
185  * \return If true is returned it means that some meshes in \a this has been modified and eventually fields have been renumbered.
186  *         \n If false \a this remains unchanged.
187  */
188 bool MEDFileData::unPolyzeMeshes()
189 {
190   MEDFileMeshes *ms=_meshes;
191   if(!ms)
192     return false;
193   std::vector< MEDFileMesh * > meshesImpacted;
194   std::vector< DataArrayInt * > renumParamsOfMeshImpacted;//same size as meshesImpacted
195   std::vector< std::vector<int> > oldCodeOfMeshImpacted,newCodeOfMeshImpacted;//same size as meshesImpacted
196   std::vector<MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > memSaverIfThrow;//same size as meshesImpacted
197   for(int i=0;i<ms->getNumberOfMeshes();i++)
198     {
199       MEDFileMesh *m=ms->getMeshAtPos(i);
200       if(m)
201         {
202           std::vector<int> oldCode,newCode;
203           DataArrayInt *o2nRenumCell=0;
204           bool modif=m->unPolyze(oldCode,newCode,o2nRenumCell);
205           if(!modif)
206             continue;
207           renumParamsOfMeshImpacted.push_back(o2nRenumCell); memSaverIfThrow.push_back(o2nRenumCell);
208           oldCodeOfMeshImpacted.push_back(oldCode);
209           newCodeOfMeshImpacted.push_back(newCode);
210           meshesImpacted.push_back(m);
211         }
212     }
213   if(!meshesImpacted.empty())
214     {
215       MEDFileFields *fs=_fields;
216       if(fs)
217         for(std::size_t i=0;i<meshesImpacted.size();i++)
218           fs->renumberEntitiesLyingOnMesh(meshesImpacted[i]->getName(),oldCodeOfMeshImpacted[i],newCodeOfMeshImpacted[i],renumParamsOfMeshImpacted[i]);
219     }
220   return !meshesImpacted.empty();
221 }
222
223 MEDFileData::MEDFileData()
224 {
225 }
226
227 MEDFileData::MEDFileData(const std::string& fileName)
228 try
229 {
230     _fields=MEDFileFields::New(fileName);
231     _meshes=MEDFileMeshes::New(fileName);
232     _params=MEDFileParameters::New(fileName);
233 }
234 catch(INTERP_KERNEL::Exception& e)
235 {
236     throw e;
237 }
238
239 void MEDFileData::write(const std::string& fileName, int mode) const
240 {
241   med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
242   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),medmod);
243   const MEDFileMeshes *ms=_meshes;
244   if(ms)
245     ms->write(fid);
246   const MEDFileFields *fs=_fields;
247   if(fs)
248     fs->writeLL(fid);
249   const MEDFileParameters *ps=_params;
250   if(ps)
251     ps->writeLL(fid);
252 }