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