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