1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDFileData.hxx"
23 using namespace ParaMEDMEM;
25 MEDFileData *MEDFileData::New(const char *fileName) throw(INTERP_KERNEL::Exception)
27 return new MEDFileData(fileName);
30 MEDFileData *MEDFileData::New()
32 return new MEDFileData;
35 MEDFileData *MEDFileData::deepCpy() const throw(INTERP_KERNEL::Exception)
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;
51 std::size_t MEDFileData::getHeapMemorySize() const
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();
63 MEDFileFields *MEDFileData::getFields() const
65 return const_cast<MEDFileFields *>(static_cast<const MEDFileFields *>(_fields));
68 MEDFileMeshes *MEDFileData::getMeshes() const
70 return const_cast<MEDFileMeshes *>(static_cast<const MEDFileMeshes *>(_meshes));
73 MEDFileParameters *MEDFileData::getParams() const
75 return const_cast<MEDFileParameters *>(static_cast<const MEDFileParameters *>(_params));
78 void MEDFileData::setFields(MEDFileFields *fields) throw(INTERP_KERNEL::Exception)
85 void MEDFileData::setMeshes(MEDFileMeshes *meshes) throw(INTERP_KERNEL::Exception)
92 void MEDFileData::setParams(MEDFileParameters *params) throw(INTERP_KERNEL::Exception)
99 int MEDFileData::getNumberOfFields() const throw(INTERP_KERNEL::Exception)
101 const MEDFileFields *f=_fields;
103 throw INTERP_KERNEL::Exception("MEDFileData::getNumberOfFields : no fields set !");
104 return f->getNumberOfFields();
107 int MEDFileData::getNumberOfMeshes() const throw(INTERP_KERNEL::Exception)
109 const MEDFileMeshes *m=_meshes;
111 throw INTERP_KERNEL::Exception("MEDFileData::getNumberOfMeshes : no meshes set !");
112 return m->getNumberOfMeshes();
115 int MEDFileData::getNumberOfParams() const throw(INTERP_KERNEL::Exception)
117 const MEDFileParameters *p=_params;
119 throw INTERP_KERNEL::Exception("MEDFileData::getNumberOfParams : no params set !");
120 return p->getNumberOfParams();
123 std::string MEDFileData::simpleRepr() const
125 std::ostringstream oss;
126 oss << "(***************)\n(* MEDFileData *)\n(***************)\n\nFields part :\n*************\n\n";
127 const MEDFileFields *tmp=_fields;
130 tmp->simpleRepr(0,oss);
134 oss << "No fields set !!!\n\n";
135 oss << "Meshes part :\n*************\n\n";
136 const MEDFileMeshes *tmp2=_meshes;
139 tmp2->simpleReprWithoutHeader(oss);
142 oss << "No meshes set !!!\n\n";
143 oss << "Params part :\n*************\n\n";
144 const MEDFileParameters *tmp3=_params;
147 tmp3->simpleReprWithoutHeader(oss);
150 oss << "No params set !!!\n";
154 bool MEDFileData::changeMeshNames(const std::vector< std::pair<std::string,std::string> >& modifTab) throw(INTERP_KERNEL::Exception)
157 MEDFileFields *fields=_fields;
159 ret=fields->changeMeshNames(modifTab) || ret;
160 MEDFileMeshes *meshes=_meshes;
162 ret=meshes->changeNames(modifTab) || ret;
166 bool MEDFileData::changeMeshName(const char *oldMeshName, const char *newMeshName) throw(INTERP_KERNEL::Exception)
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);
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)
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.
181 bool MEDFileData::unPolyzeMeshes() throw(INTERP_KERNEL::Exception)
183 MEDFileMeshes *ms=_meshes;
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++)
192 MEDFileMesh *m=ms->getMeshAtPos(i);
195 std::vector<int> oldCode,newCode;
196 DataArrayInt *o2nRenumCell=0;
197 bool modif=m->unPolyze(oldCode,newCode,o2nRenumCell);
200 renumParamsOfMeshImpacted.push_back(o2nRenumCell); memSaverIfThrow.push_back(o2nRenumCell);
201 oldCodeOfMeshImpacted.push_back(oldCode);
202 newCodeOfMeshImpacted.push_back(newCode);
203 meshesImpacted.push_back(m);
206 if(!meshesImpacted.empty())
208 MEDFileFields *fs=_fields;
210 for(std::size_t i=0;i<meshesImpacted.size();i++)
211 fs->renumberEntitiesLyingOnMesh(meshesImpacted[i]->getName(),oldCodeOfMeshImpacted[i],newCodeOfMeshImpacted[i],renumParamsOfMeshImpacted[i]);
213 return !meshesImpacted.empty();
216 MEDFileData::MEDFileData()
220 MEDFileData::MEDFileData(const char *fileName) throw(INTERP_KERNEL::Exception)
223 _fields=MEDFileFields::New(fileName);
224 _meshes=MEDFileMeshes::New(fileName);
225 _params=MEDFileParameters::New(fileName);
227 catch(INTERP_KERNEL::Exception& e)
232 void MEDFileData::write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception)
234 med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
235 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,medmod);
236 const MEDFileMeshes *ms=_meshes;
239 const MEDFileFields *fs=_fields;
242 const MEDFileParameters *ps=_params;