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