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