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