Salome HOME
Update copyrights
[tools/medcoupling.git] / src / MEDLoader / MEDFileStructureElement.cxx
1 // Copyright (C) 2007-2019  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 (EDF R&D)
20
21 #include "MEDFileStructureElement.hxx"
22 #include "MEDFileMeshSupport.hxx"
23 #include "MEDLoaderBase.hxx"
24 #include "MEDFileMeshLL.hxx"
25 #include "MEDFileSafeCaller.txx"
26
27 #include "InterpKernelAutoPtr.hxx"
28
29 using namespace MEDCoupling;
30
31
32 std::string MEDFileSEHolder::getModelName() const
33 {
34   return _father->getName();
35 }
36
37 std::string MEDFileSEHolder::getName() const
38 {
39   return _name;
40 }
41
42 void MEDFileSEHolder::setName(const std::string& name)
43 {
44   _name=name;
45 }
46
47 std::size_t MEDFileSEHolder::getHeapMemorySizeLoc() const
48 {
49   return _name.capacity();
50 }
51
52 ////////////////////
53
54 MEDFileSEConstAtt *MEDFileSEConstAtt::New(med_idt fid, MEDFileStructureElement *father, int idCstAtt, const MEDFileUMesh *mesh)
55 {
56   return new MEDFileSEConstAtt(fid,father,idCstAtt,mesh);
57 }
58
59 MEDFileSEConstAtt::MEDFileSEConstAtt(med_idt fid, MEDFileStructureElement *father, int idCstAtt, const MEDFileUMesh *mesh):MEDFileSEHolder(father)
60 {
61   std::string modelName(getModelName());
62   INTERP_KERNEL::AutoPtr<char> constattname(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE)),profilename(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
63   med_attribute_type constatttype;
64   int nbCompo;
65   med_entity_type met;
66   int pflSz;
67   MEDFILESAFECALLERRD0(MEDstructElementConstAttInfo,(fid,modelName.c_str(),idCstAtt+1,constattname,&constatttype,&nbCompo,&met,profilename,&pflSz));
68   std::string name(MEDLoaderBase::buildStringFromFortran(constattname,MED_NAME_SIZE));
69   setName(name);
70   setProfile(MEDLoaderBase::buildStringFromFortran(profilename,MED_NAME_SIZE));
71   _tof=MEDFileMesh::ConvertFromMEDFileEntity(met);
72   //
73   _val=MEDFileStructureElement::BuildFrom(constatttype);
74   nbCompo=MEDFileStructureElement::EffectiveNbCompo(constatttype,nbCompo);
75   if(pflSz==0 && getProfile().empty())
76     {
77       switch(met)
78         {
79         case MED_CELL:
80           {
81             std::vector<INTERP_KERNEL::NormalizedCellType> gt(mesh->getAllGeoTypes());
82             if(gt.size()!=1)
83               throw INTERP_KERNEL::Exception("MEDFileSEConstAtt constr : only one cell type expected !");
84             pflSz=mesh->getNumberOfCellsWithType(gt[0]);
85             break;
86           }
87         case MED_NODE:
88           {
89             pflSz=mesh->getNumberOfNodes();
90             break;
91           }
92         default:
93           throw INTERP_KERNEL::Exception("MEDFileSEConstAtt cstr : not recognized entity type !");
94         }
95     }
96   if(constatttype==MED_ATT_NAME)
97     pflSz++;
98   _val->alloc(pflSz,nbCompo);
99   MEDFILESAFECALLERRD0(MEDstructElementConstAttRd,(fid,modelName.c_str(),name.c_str(),_val->getVoidStarPointer()));
100   if(constatttype==MED_ATT_NAME)
101     { pflSz--; _val->reAlloc(pflSz); }
102 }
103
104 std::vector<const BigMemoryObject *> MEDFileSEConstAtt::getDirectChildrenWithNull() const
105 {
106   std::vector<const BigMemoryObject *> ret;
107   ret.push_back(_val);
108   return ret;
109 }
110
111 std::size_t MEDFileSEConstAtt::getHeapMemorySizeWithoutChildren() const
112 {
113   return getHeapMemorySizeLoc()+_pfl.capacity();
114 }
115
116 void MEDFileSEConstAtt::writeLL(med_idt fid) const
117 {
118 }
119
120 void MEDFileSEConstAtt::setProfile(const std::string& name)
121 {
122   _pfl=name;
123 }
124
125 std::string MEDFileSEConstAtt::getProfile() const
126 {
127   return _pfl;
128 }
129
130 ////////////////////
131
132 MEDFileSEVarAtt *MEDFileSEVarAtt::New(med_idt fid, MEDFileStructureElement *father, int idVarAtt)
133 {
134   return new MEDFileSEVarAtt(fid,father,idVarAtt);
135 }
136
137 MEDFileSEVarAtt::MEDFileSEVarAtt(med_idt fid, MEDFileStructureElement *father, int idVarAtt):MEDFileSEHolder(father)
138 {
139   std::string modelName(getModelName());
140   INTERP_KERNEL::AutoPtr<char> varattname(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE)),profilename(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
141   med_attribute_type varatttype;
142   {
143     int pflSz;
144     MEDFILESAFECALLERRD0(MEDstructElementVarAttInfo,(fid,modelName.c_str(),idVarAtt+1,varattname,&varatttype,&_nb_compo));
145   }
146   setName(MEDLoaderBase::buildStringFromFortran(varattname,MED_NAME_SIZE));
147   _gen=MEDFileStructureElement::BuildFrom(varatttype);
148   _gen->alloc(0,1);
149 }
150
151 std::vector<const BigMemoryObject *> MEDFileSEVarAtt::getDirectChildrenWithNull() const
152 {
153   return std::vector<const BigMemoryObject *>();
154 }
155
156 std::size_t MEDFileSEVarAtt::getHeapMemorySizeWithoutChildren() const
157 {
158   return getHeapMemorySizeLoc();
159 }
160
161 void MEDFileSEVarAtt::writeLL(med_idt fid) const
162 {
163 }
164
165 ////////////////////
166
167 MEDFileStructureElement *MEDFileStructureElement::New(med_idt fid, int idSE, const MEDFileMeshSupports *ms)
168 {
169   return new MEDFileStructureElement(fid,idSE,ms);
170 }
171
172 MEDFileStructureElement::MEDFileStructureElement(med_idt fid, int idSE, const MEDFileMeshSupports *ms)
173 {
174   INTERP_KERNEL::AutoPtr<char> modelName(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE)),supportMeshName(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
175   med_geometry_type sgeoType;
176   med_entity_type entiyType;
177   int nConsAttr(0),nVarAttr(0);
178   {
179     med_bool anyPfl;
180     int nnode(0),ncell(0);
181     MEDFILESAFECALLERRD0(MEDstructElementInfo,(fid,idSE+1,modelName,&_id_type,&_dim,supportMeshName,&entiyType,&nnode,&ncell,&sgeoType,&nConsAttr,&anyPfl,&nVarAttr));
182   }
183   _name=MEDLoaderBase::buildStringFromFortran(modelName,MED_NAME_SIZE);
184   _sup_mesh_name=MEDLoaderBase::buildStringFromFortran(supportMeshName,MED_NAME_SIZE);
185   _geo_type=MEDFileMesh::ConvertFromMEDFileGeoType(sgeoType);
186   _tof=MEDFileMesh::ConvertFromMEDFileEntity(entiyType);
187   _cst_att.resize(nConsAttr);
188   for(int i=0;i<nConsAttr;i++)
189     _cst_att[i]=MEDFileSEConstAtt::New(fid,this,i,ms->getSupMeshWithName(_sup_mesh_name));
190   _var_att.resize(nVarAttr);
191   for(int i=0;i<nVarAttr;i++)
192     _var_att[i]=MEDFileSEVarAtt::New(fid,this,i);
193 }
194
195 std::vector<const BigMemoryObject *> MEDFileStructureElement::getDirectChildrenWithNull() const
196 {
197   std::vector<const BigMemoryObject *> ret;
198   for(std::vector< MCAuto<MEDFileSEConstAtt> >::const_iterator it=_cst_att.begin();it!=_cst_att.end();it++)
199     ret.push_back(*it);
200   for(std::vector< MCAuto<MEDFileSEVarAtt> >::const_iterator it=_var_att.begin();it!=_var_att.end();it++)
201     ret.push_back(*it);
202   return ret;
203 }
204
205 std::size_t MEDFileStructureElement::getHeapMemorySizeWithoutChildren() const
206 {
207   return _name.capacity()+_cst_att.capacity()*sizeof(MCAuto<MEDFileSEConstAtt>)+_var_att.capacity()*sizeof(MCAuto<MEDFileSEVarAtt>);
208 }
209
210 void MEDFileStructureElement::writeLL(med_idt fid) const
211 {
212 }
213
214 std::string MEDFileStructureElement::getName() const
215 {
216   return _name;
217 }
218
219 MCAuto<DataArray> MEDFileStructureElement::BuildFrom(med_attribute_type mat)
220 {
221   MCAuto<DataArray> ret;
222   switch(mat)
223     {
224     case MED_ATT_INT:
225       {
226         ret=DataArrayInt::New();
227         break;
228       }
229     case MED_ATT_FLOAT64:
230       {
231         ret=DataArrayDouble::New();
232         break;
233       }
234     case MED_ATT_NAME:
235       {
236         ret=DataArrayAsciiChar::New();
237         break;
238       }
239     default:
240       throw INTERP_KERNEL::Exception("MEDFileStructureElement::BuildFrom : not recognized type ! Only INT and FLOAT64 !");
241     }
242   return ret;
243 }
244
245 int MEDFileStructureElement::EffectiveNbCompo(med_attribute_type mat, int nbCompo)
246 {
247   switch(mat)
248     {
249     case MED_ATT_INT:
250     case MED_ATT_FLOAT64:
251       return nbCompo;
252     case MED_ATT_NAME:
253       return nbCompo*MED_NAME_SIZE;
254     default:
255       throw INTERP_KERNEL::Exception("MEDFileStructureElement::BuildFrom : not recognized type ! Only INT and FLOAT64 !");
256     }
257 }
258
259 int MEDFileStructureElement::getDynGT() const
260 {
261   return _id_type;
262 }
263
264 TypeOfField MEDFileStructureElement::getEntity() const
265 {
266   return _tof;
267 }
268
269 std::string MEDFileStructureElement::getMeshName() const
270 {
271   return _sup_mesh_name;
272 }
273
274 std::vector<std::string> MEDFileStructureElement::getVarAtts() const
275 {
276   std::vector<std::string> ret;
277   for(std::vector< MCAuto<MEDFileSEVarAtt> >::const_iterator it=_var_att.begin();it!=_var_att.end();it++)
278     if((*it).isNotNull())
279       ret.push_back((*it)->getName());
280   return ret;
281 }
282
283 const MEDFileSEVarAtt *MEDFileStructureElement::getVarAtt(const std::string& varName) const
284 {
285   for(std::vector< MCAuto<MEDFileSEVarAtt> >::const_iterator it=_var_att.begin();it!=_var_att.end();it++)
286     if((*it).isNotNull())
287       if((*it)->getName()==varName)
288         return *it;
289   std::ostringstream oss; oss << "MEDFileStructureElement::getVarAtt : no var att with name \"" << varName << "\" !";
290   throw INTERP_KERNEL::Exception(oss.str());
291 }
292
293 ////////////////////
294
295 MEDFileStructureElements *MEDFileStructureElements::New(const std::string& fileName, const MEDFileMeshSupports *ms)
296 {
297   MEDFileUtilities::AutoFid fid(OpenMEDFileForRead(fileName));
298   return New(fid,ms);
299 }
300
301 MEDFileStructureElements *MEDFileStructureElements::New(med_idt fid, const MEDFileMeshSupports *ms)
302 {
303   return new MEDFileStructureElements(fid,ms);
304 }
305
306 MEDFileStructureElements *MEDFileStructureElements::New()
307 {
308   return new MEDFileStructureElements;
309 }
310
311 std::vector<const BigMemoryObject *> MEDFileStructureElements::getDirectChildrenWithNull() const
312 {
313   std::vector<const BigMemoryObject *> ret;
314   for(std::vector< MCAuto<MEDFileStructureElement> >::const_iterator it=_elems.begin();it!=_elems.end();it++)
315     ret.push_back(*it);
316   return ret;
317 }
318
319 std::size_t MEDFileStructureElements::getHeapMemorySizeWithoutChildren() const
320 {
321   return _elems.capacity()*sizeof(MEDFileStructureElement);
322 }
323
324 void MEDFileStructureElements::writeLL(med_idt fid) const
325 {
326 }
327
328 MEDFileStructureElements::MEDFileStructureElements(med_idt fid, const MEDFileMeshSupports *ms)
329 {
330   int nbSE(MEDnStructElement(fid));
331   _elems.resize(nbSE);
332   for(int i=0;i<nbSE;i++)
333     _elems[i]=MEDFileStructureElement::New(fid,i,ms);
334   _sup.takeRef(ms);
335 }
336
337 MEDFileStructureElements::MEDFileStructureElements()
338 {
339 }
340
341 MEDFileStructureElements::~MEDFileStructureElements()
342 {
343 }
344
345 int MEDFileStructureElements::getNumberOf() const
346 {
347   return _elems.size();
348 }
349
350 std::vector<int> MEDFileStructureElements::getDynGTAvail() const
351 {
352   std::vector<int> ret;
353   for(std::vector< MCAuto<MEDFileStructureElement> >::const_iterator it=_elems.begin();it!=_elems.end();it++)
354     {
355       const MEDFileStructureElement *elt(*it);
356       if(elt)
357         ret.push_back(elt->getDynGT());
358     }
359   return ret;
360 }
361
362 const MEDFileStructureElement *MEDFileStructureElements::getWithGT(int idGT) const
363 {
364   for(std::vector< MCAuto<MEDFileStructureElement> >::const_iterator it=_elems.begin();it!=_elems.end();it++)
365     if((*it).isNotNull())
366       {
367         if((*it)->getDynGT()==idGT)
368           return *it;
369       }
370   std::ostringstream oss; oss << "MEDFileStructureElements::getWithGT : no such geo type " << idGT << " !";
371   throw INTERP_KERNEL::Exception(oss.str());
372 }
373
374 int MEDFileStructureElements::getNumberOfNodesPerSE(const std::string& seName) const
375 {
376   if(seName=="MED_PARTICLE")
377     return 1;
378   const MEDFileStructureElement *se(getSEWithName(seName));
379   std::string meshName(se->getMeshName());
380   return _sup->getNumberOfNodesInConnOf(se->getEntity(),meshName);
381 }
382
383 const MEDFileStructureElement *MEDFileStructureElements::getSEWithName(const std::string& seName) const
384 {
385   for(std::vector< MCAuto<MEDFileStructureElement> >::const_iterator it=_elems.begin();it!=_elems.end();it++)
386     {
387       if((*it).isNotNull())
388         if((*it)->getName()==seName)
389           return *it;
390     }
391   std::ostringstream oss; oss << "MEDFileStructureElements::getSEWithName : no such structure element with name " << seName << " !";
392   throw INTERP_KERNEL::Exception(oss.str());
393 }
394
395 std::vector<std::string> MEDFileStructureElements::getVarAttsOf(const std::string& seName) const
396 {
397   const MEDFileStructureElement *se(getSEWithName(seName));
398   return se->getVarAtts();
399 }
400
401 const MEDFileSEVarAtt *MEDFileStructureElements::getVarAttOf(const std::string &seName, const std::string& varName) const
402 {
403   const MEDFileStructureElement *se(getSEWithName(seName));
404   return se->getVarAtt(varName);
405 }
406
407 const MEDFileUMesh *MEDFileStructureElements::getSupMeshWithName(const std::string& name) const
408 {
409   return _sup->getSupMeshWithName(name);
410 }