Salome HOME
feat: new crackAlong method
[tools/medcoupling.git] / src / MEDLoader / MEDFileMeshElt.cxx
1 // Copyright (C) 2007-2024  CEA, EDF
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 (CEA/DEN)
20
21 #include "MEDFileMeshElt.hxx"
22 #include "MEDFileSafeCaller.txx"
23 #include "MEDFileMeshReadSelector.hxx"
24 #include "MEDFileBasis.hxx"
25
26 #include "MEDCouplingUMesh.hxx"
27
28 #include "InterpKernelException.hxx"
29 #include "InterpKernelAutoPtr.hxx"
30 #include "CellModel.hxx"
31
32 #include "MEDFilterEntity.hxx"
33
34 #include <iostream>
35
36
37 // From MEDLOader.cxx TU
38 extern med_geometry_type typmai3[INTERP_KERNEL::NORM_MAXTYPE];
39
40 using namespace MEDCoupling;
41
42 MEDFileUMeshPerTypeCommon *MEDFileUMeshPerTypeCommon::New()
43 {
44   return new MEDFileUMeshPerTypeCommon;
45 }
46
47 void MEDFileUMeshPerTypeCommon::loadCommonPart(med_idt fid, const char *mName, int dt, int it, mcIdType curNbOfElem, med_geometry_type geoElt,
48                                                med_entity_type entity, MEDFileMeshReadSelector *mrs)
49 {
50   med_bool changement,transformation;
51   _fam=0;
52   if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
53     {    
54       if(!mrs || mrs->isCellFamilyFieldReading())
55         {
56           MCAuto<DataArrayMedInt> miFam=DataArrayMedInt::New();
57           miFam->alloc(curNbOfElem,1);
58           if(MEDmeshEntityFamilyNumberRd(fid,mName,dt,it,entity,geoElt,miFam->getPointer())!=0)
59             std::fill(miFam->getPointer(),miFam->getPointer()+curNbOfElem,0);
60           _fam=FromMedIntArray<mcIdType>( miFam );
61         }
62     }
63   _num=0;
64   if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
65     {
66       if(!mrs || mrs->isCellNumFieldReading())
67         {
68           MCAuto<DataArrayMedInt> miNum=DataArrayMedInt::New();
69           miNum->alloc(curNbOfElem,1);
70           if(MEDmeshEntityNumberRd(fid,mName,dt,it,entity,geoElt,miNum->getPointer())!=0)
71             miNum=0;
72           _num=FromMedIntArray<mcIdType>(miNum);
73         }
74     }
75   _names=0;
76   if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_NAME,MED_NODAL,&changement,&transformation)>0)
77     {
78       if(!mrs || mrs->isCellNameFieldReading())
79         {
80           _names=DataArrayAsciiChar::New();
81           _names->alloc(curNbOfElem+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
82           if(MEDmeshEntityNameRd(fid,mName,dt,it,entity,geoElt,_names->getPointer())!=0)
83             _names=0;
84           else
85             _names->reAlloc(curNbOfElem);//not a bug to avoid the memory corruption due to last \0 at the end
86         }
87     }
88 }
89
90 std::size_t MEDFileUMeshPerTypeCommon::getHeapMemorySizeWithoutChildren() const
91 {
92   return 0;
93 }
94
95 std::vector<const BigMemoryObject *> MEDFileUMeshPerTypeCommon::getDirectChildrenWithNull() const
96 {
97   std::vector<const BigMemoryObject *> ret;
98   ret.push_back((const DataArrayIdType *)_num);
99   ret.push_back((const DataArrayIdType *)_fam);
100   ret.push_back((const DataArrayAsciiChar *)_names);
101   return ret;
102 }
103
104 MEDFileUMeshPerType *MEDFileUMeshPerType::New(med_idt fid, const char *mName, int dt, int it, int mdim, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType geoElt2, MEDFileMeshReadSelector *mrs)
105 {
106   med_entity_type whichEntity;
107   if(!isExisting(fid,mName,dt,it,geoElt,whichEntity))
108     return 0;
109   return new MEDFileUMeshPerType(fid,mName,dt,it,mdim,geoElt,geoElt2,whichEntity,mrs);
110 }
111
112
113 MEDFileUMeshPerType *MEDFileUMeshPerType::NewPart(med_idt fid, const char *mName, int dt, int it, int mdim, INTERP_KERNEL::NormalizedCellType geoElt2, mcIdType strt, mcIdType stp, mcIdType step, MEDFileMeshReadSelector *mrs)
114 {
115   int geoElt2i((int)geoElt2);
116   if(geoElt2i<0 || geoElt2i>=INTERP_KERNEL::NORM_MAXTYPE)
117     throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::NewPart : Not recognized MEDCoupling/MEDLoader geometric type !");
118   med_geometry_type geoElt(typmai3[geoElt2]);
119   med_entity_type whichEntity;
120   if(!isExisting(fid,mName,dt,it,geoElt,whichEntity))
121     throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::NewPart : The specified geo type is not present in the specified mesh !");
122   MCAuto<MEDFileUMeshPerType> ret(new MEDFileUMeshPerType);
123   ret->loadPart(fid,mName,dt,it,mdim,geoElt,geoElt2,whichEntity,strt,stp,step,mrs);
124   return ret.retn();
125 }
126
127 MEDFileUMeshPerType *MEDFileUMeshPerType::NewPart(med_idt fid, const char *mName, int dt, int it, int mdim, INTERP_KERNEL::NormalizedCellType geoElt2, const std::vector<mcIdType>& distrib, MEDFileMeshReadSelector *mrs)
128 {
129   int geoElt2i((int)geoElt2);
130   if(geoElt2i<0 || geoElt2i>=INTERP_KERNEL::NORM_MAXTYPE)
131     throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::NewPart : Not recognized MEDCoupling/MEDLoader geometric type !");
132   med_geometry_type geoElt(typmai3[geoElt2]);
133   med_entity_type whichEntity;
134   if(!isExisting(fid,mName,dt,it,geoElt,whichEntity))
135     throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::NewPart : The specified geo type is not present in the specified mesh !");
136   MCAuto<MEDFileUMeshPerType> ret(new MEDFileUMeshPerType);
137   ret->loadPart(fid,mName,dt,it,mdim,geoElt,geoElt2,whichEntity,distrib,mrs);
138   return ret.retn();
139 }
140
141 std::size_t MEDFileUMeshPerType::getHeapMemorySizeWithoutChildren() const
142 {
143   return MEDFileUMeshPerTypeCommon::getHeapMemorySizeWithoutChildren()+0;
144 }
145
146 std::vector<const BigMemoryObject *> MEDFileUMeshPerType::getDirectChildrenWithNull() const
147 {
148   std::vector<const BigMemoryObject *> ret(MEDFileUMeshPerTypeCommon::getDirectChildrenWithNull());
149   ret.push_back((const MEDCoupling1GTUMesh *)_m);
150   return ret;
151 }
152
153 bool MEDFileUMeshPerType::isExisting(med_idt fid, const char *mName, int dt, int it, med_geometry_type geoElt, med_entity_type& whichEntity)
154 {
155   static const med_entity_type entities[3]={MED_CELL,MED_DESCENDING_FACE,MED_DESCENDING_EDGE};
156   mcIdType nbOfElt=0;
157   for(int i=0;i<3;i++)
158     {
159       med_bool changement,transformation;
160       mcIdType tmp(MEDmeshnEntity(fid,mName,dt,it,entities[i],geoElt,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
161       if(tmp>nbOfElt)
162         {
163           nbOfElt=tmp;
164           whichEntity=entities[i];
165           if(i>0)
166             std::cerr << "WARNING : MEDFile has been detected to be noncompliant with MED 3 : Please change entity in MEDFile for geotype " <<  geoElt << std::endl;
167         }
168     }
169   return nbOfElt>0;
170 }
171
172 int MEDFileUMeshPerType::getDim() const
173 {
174   return _m->getMeshDimension();
175 }
176
177 MEDFileUMeshPerType::MEDFileUMeshPerType()
178 {
179 }
180
181 MEDFileUMeshPerType::MEDFileUMeshPerType(med_idt fid, const char *mName, int dt, int it, int mdim, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType type,
182                                          med_entity_type entity, MEDFileMeshReadSelector *mrs)
183 {
184   med_bool changement,transformation;
185   mcIdType curNbOfElem(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
186   const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel(type));
187   if(!cm.isDynamic())
188     {
189       loadFromStaticType(fid,mName,dt,it,mdim,curNbOfElem,geoElt,type,entity,mrs);
190       return;
191     }
192   if(type==INTERP_KERNEL::NORM_POLYGON || type==INTERP_KERNEL::NORM_QPOLYG)
193     {
194       loadPolyg(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
195       return;
196     }
197   //if(type==INTERP_KERNEL::NORM_POLYHED)
198   loadPolyh(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
199 }
200
201 void MEDFileUMeshPerType::loadPart(med_idt fid, const char *mName, int dt, int it, int mdim, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType type,
202                                    med_entity_type entity, const std::vector<mcIdType>& distrib, MEDFileMeshReadSelector *mrs)
203 {
204   med_bool changement,transformation;
205   mcIdType curNbOfElem(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
206   const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel(type));
207   MCAuto<DataArrayIdType> listOfIds=DataArrayIdType::New();
208   listOfIds->useArray(distrib.data(),false,DeallocType::C_DEALLOC,distrib.size(),1);
209   _pd=PartDefinition::New(listOfIds);
210   if(!cm.isDynamic())
211     {
212       loadPartStaticType(fid,mName,dt,it,mdim,curNbOfElem,geoElt,type,entity,mrs);
213     }
214   else
215     throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::loadPart : not implemented yet for the dynamic type !");
216 }
217
218 void MEDFileUMeshPerType::loadPart(med_idt fid, const char *mName, int dt, int it, int mdim, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType type,
219                                    med_entity_type entity, mcIdType strt, mcIdType end, mcIdType step, MEDFileMeshReadSelector *mrs)
220 {
221   med_bool changement,transformation;
222   mcIdType curNbOfElem(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
223   const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel(type));
224   _pd=PartDefinition::New(strt,end,step);
225   if(!cm.isDynamic())
226     {
227       loadPartStaticType(fid,mName,dt,it,mdim,curNbOfElem,geoElt,type,entity,mrs);
228     }
229   else
230     throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::loadPart : not implemented yet for the dynamic type !");
231 }
232
233
234 void MEDFileUMeshPerType::loadFromStaticType(med_idt fid, const char *mName, int dt, int it, int mdim, mcIdType curNbOfElem, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType type,
235                                              med_entity_type entity, MEDFileMeshReadSelector *mrs)
236 {
237   _m=MEDCoupling1SGTUMesh::New(mName,type);
238   MEDCoupling1SGTUMesh *mc(dynamic_cast<MEDCoupling1SGTUMesh *>((MEDCoupling1GTUMesh *)_m));
239   MCAuto<DataArrayMedInt> conn(DataArrayMedInt::New());
240   mcIdType nbOfNodesPerCell(mc->getNumberOfNodesPerCell());
241   conn->alloc(nbOfNodesPerCell*curNbOfElem,1);
242   MEDFILESAFECALLERRD0(MEDmeshElementConnectivityRd,(fid,mName,dt,it,entity,geoElt,MED_NODAL,MED_FULL_INTERLACE,conn->getPointer()));
243   std::transform(conn->begin(),conn->end(),conn->getPointer(),std::bind(std::plus<med_int>(),std::placeholders::_1,-1));
244   mc->setNodalConnectivity(FromMedIntArray<mcIdType>(conn));
245   loadCommonPart(fid,mName,dt,it,curNbOfElem,geoElt,entity,mrs);
246 }
247
248 void MEDFileUMeshPerType::loadPartStaticType(med_idt fid, const char *mName, int dt, int it, int mdim, mcIdType curNbOfElem, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType type,
249                                              med_entity_type entity, MEDFileMeshReadSelector *mrs)
250 {
251   _m=MEDCoupling1SGTUMesh::New(mName,type);
252   MEDCoupling1SGTUMesh *mc(dynamic_cast<MEDCoupling1SGTUMesh *>((MEDCoupling1GTUMesh *)_m));
253   MCAuto<DataArrayMedInt> conn(DataArrayMedInt::New());
254   mcIdType nbOfNodesPerCell(mc->getNumberOfNodesPerCell());
255   if(!_pd)
256     throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::loadPartStaticType : no part definition !");
257   mcIdType nbOfEltsToLoad(_pd->getNumberOfElems());
258   conn->alloc(nbOfNodesPerCell*nbOfEltsToLoad,1);
259   {
260     MEDFilterEntity filter;
261     filter.fill(fid,/*nentity*/curNbOfElem,/*nvaluesperentity*/1,/*nconstituentpervalue*/nbOfNodesPerCell,
262         MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,MED_NO_PROFILE,
263         _pd);
264     MEDFILESAFECALLERRD0(MEDmeshElementConnectivityAdvancedRd,(fid,mName,dt,it,entity,geoElt,MED_NODAL,filter.getPtr(),conn->getPointer()));
265   }
266   std::transform(conn->begin(),conn->end(),conn->getPointer(),std::bind(std::plus<med_int>(),std::placeholders::_1,-1));
267   mc->setNodalConnectivity(FromMedIntArray<mcIdType>(conn));
268   loadPartOfCellCommonPart(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
269 }
270
271
272 void MEDFileUMeshPerType::loadPartOfCellCommonPart(med_idt fid, const char *mName, int dt, int it, int mdim, mcIdType curNbOfElem, med_geometry_type geoElt, med_entity_type entity, MEDFileMeshReadSelector *mrs)
273 {
274   med_bool changement,transformation;
275   if(!_pd)
276     throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::loadPartOfCellCommonPart : no part definition !");
277   mcIdType nbOfEltsToLoad(_pd->getNumberOfElems());
278   _fam=0;
279   if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
280     {
281       if(!mrs || mrs->isCellFamilyFieldReading())
282         {
283           MCAuto<DataArrayMedInt> miFam(DataArrayMedInt::New());
284           miFam->alloc(nbOfEltsToLoad,1);
285           MEDFilterEntity filter;
286           filter.fill(fid,/*nentity*/curNbOfElem,/*nvaluesperentity*/1,/*nconstituentpervalue*/1,
287               MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,MED_NO_PROFILE,
288               _pd);
289           if(MEDmeshEntityAttributeAdvancedRd(fid,mName,MED_FAMILY_NUMBER,dt,it,entity,geoElt,filter.getPtr(),miFam->getPointer())!=0)
290             miFam->fillWithZero();
291           _fam=FromMedIntArray<mcIdType>(miFam);
292         }
293     }
294   _num=0;
295   if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
296     {
297       if(!mrs || mrs->isCellNumFieldReading())
298         {
299           MCAuto<DataArrayMedInt> miNum(DataArrayMedInt::New());
300           miNum->alloc(nbOfEltsToLoad,1);
301           MEDFilterEntity filter;
302           filter.fill(fid,/*nentity*/curNbOfElem,/*nvaluesperentity*/1,/*nconstituentpervalue*/1,
303               MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,MED_NO_PROFILE,
304               _pd);
305           if(MEDmeshEntityAttributeAdvancedRd(fid,mName,MED_NUMBER,dt,it,entity,geoElt,filter.getPtr(),miNum->getPointer())!=0)
306             miNum->fillWithZero();
307           _num=FromMedIntArray<mcIdType>(miNum);
308         }
309     }
310   _names=0;
311   if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_NAME,MED_NODAL,&changement,&transformation)>0)
312     {
313       if(!mrs || mrs->isCellNameFieldReading())
314         {
315           _names=DataArrayAsciiChar::New();
316           _names->alloc(nbOfEltsToLoad+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
317           MEDFilterEntity filter;
318           filter.fill(fid,/*nentity*/curNbOfElem,/*nvaluesperentity*/1,/*nconstituentpervalue*/1,
319               MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,MED_NO_PROFILE,
320               _pd);
321           if(MEDmeshEntityAttributeAdvancedRd(fid,mName,MED_NAME,dt,it,entity,geoElt,filter.getPtr(),_names->getPointer())!=0)
322             _names=0;
323           else
324             _names->reAlloc(nbOfEltsToLoad);//not a bug to avoid the memory corruption due to last \0 at the end
325         }
326     }
327 }
328
329 void MEDFileUMeshPerType::loadPolyg(med_idt fid, const char *mName, int dt, int it, int mdim, mcIdType arraySize, med_geometry_type geoElt,
330                                     med_entity_type entity, MEDFileMeshReadSelector *mrs)
331 {
332   med_bool changement,transformation;
333   mcIdType curNbOfElem(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_INDEX_NODE,MED_NODAL,&changement,&transformation)-1);
334   _m=MEDCoupling1DGTUMesh::New(mName,geoElt==MED_POLYGON?INTERP_KERNEL::NORM_POLYGON:INTERP_KERNEL::NORM_QPOLYG);
335   MCAuto<MEDCoupling1DGTUMesh> mc(DynamicCast<MEDCoupling1GTUMesh,MEDCoupling1DGTUMesh>(_m));
336   MCAuto<DataArrayMedInt> conn(DataArrayMedInt::New()),connI(DataArrayMedInt::New());
337   conn->alloc(arraySize,1); connI->alloc(curNbOfElem+1,1);
338   MEDFILESAFECALLERRD0(MEDmeshPolygon2Rd,(fid,mName,dt,it,MED_CELL,geoElt,MED_NODAL,connI->getPointer(),conn->getPointer()));
339   std::transform(conn->begin(),conn->end(),conn->getPointer(),std::bind(std::plus<med_int>(),std::placeholders::_1,-1));
340   std::transform(connI->begin(),connI->end(),connI->getPointer(),std::bind(std::plus<med_int>(),std::placeholders::_1,-1));
341   mc->setNodalConnectivity(FromMedIntArray<mcIdType>(conn),FromMedIntArray<mcIdType>(connI));
342   loadCommonPart(fid,mName,dt,it,curNbOfElem,geoElt,entity,mrs);
343 }
344
345 void MEDFileUMeshPerType::loadPolyh(med_idt fid, const char *mName, int dt, int it, int mdim, mcIdType connFaceLgth, med_geometry_type geoElt,
346                                     med_entity_type entity, MEDFileMeshReadSelector *mrs)
347 {
348   med_bool changement,transformation;
349   med_int indexFaceLgth(MEDmeshnEntity(fid,mName,dt,it,MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation));
350   mcIdType curNbOfElem(MEDmeshnEntity(fid,mName,dt,it,MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,&changement,&transformation)-1);
351   _m=MEDCoupling1DGTUMesh::New(mName,INTERP_KERNEL::NORM_POLYHED);
352   MCAuto<MEDCoupling1DGTUMesh> mc(DynamicCastSafe<MEDCoupling1GTUMesh,MEDCoupling1DGTUMesh>(_m));
353   INTERP_KERNEL::AutoPtr<med_int> index=new med_int[curNbOfElem+1];
354   INTERP_KERNEL::AutoPtr<med_int> indexFace=new med_int[indexFaceLgth];
355   INTERP_KERNEL::AutoPtr<med_int> locConn=new med_int[connFaceLgth];
356   MEDFILESAFECALLERRD0(MEDmeshPolyhedronRd,(fid,mName,dt,it,MED_CELL,MED_NODAL,index,indexFace,locConn));
357   MCAuto<DataArrayIdType> conn(DataArrayIdType::New()),connI(DataArrayIdType::New());
358   mcIdType arraySize=connFaceLgth;
359   for(mcIdType i=0;i<curNbOfElem;i++)
360     arraySize+=index[i+1]-index[i]-1;
361   conn=DataArrayIdType::New();
362   conn->alloc(arraySize,1);
363   mcIdType *wFinalConn=conn->getPointer();
364   connI->alloc(curNbOfElem+1,1);
365   mcIdType *finalIndex(connI->getPointer());
366   finalIndex[0]=0;
367   for(mcIdType i=0;i<curNbOfElem;i++)
368     {
369       finalIndex[i+1]=finalIndex[i]+index[i+1]-index[i]-1+indexFace[index[i+1]-1]-indexFace[index[i]-1];
370       wFinalConn=std::transform(locConn+indexFace[index[i]-1]-1,locConn+indexFace[index[i]]-1,wFinalConn,std::bind(std::plus<mcIdType>(),std::placeholders::_1,-1));
371       for(mcIdType j=index[i];j<index[i+1]-1;j++)
372         {
373           *wFinalConn++=-1;
374           wFinalConn=std::transform(locConn+indexFace[j]-1,locConn+indexFace[j+1]-1,wFinalConn,std::bind(std::plus<mcIdType>(),std::placeholders::_1,-1));
375         }
376     }
377   mc->setNodalConnectivity(conn,connI);
378   loadCommonPart(fid,mName,dt,it,curNbOfElem,MED_POLYHEDRON,entity,mrs);
379 }
380
381 void MEDFileUMeshPerType::Write(med_idt fid, const std::string& mname, int mdim, const MEDCoupling1GTUMesh *m, const DataArrayIdType *fam, const DataArrayIdType *num, const DataArrayAsciiChar *names)
382 {
383   mcIdType nbOfCells=m->getNumberOfCells();
384   if(nbOfCells<1)
385     return ;
386   int dt,it;
387   double timm=m->getTime(dt,it);
388   INTERP_KERNEL::NormalizedCellType ikt=m->getTypeOfCell(0);
389   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(ikt);
390   med_geometry_type curMedType=typmai3[(int)ikt];
391   if(!cm.isDynamic())
392     {
393       const MEDCoupling1SGTUMesh *m0(dynamic_cast<const MEDCoupling1SGTUMesh *>(m));
394       if(!m0)
395         throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::Write : internal error #1 !");
396       MCAuto<DataArrayMedInt> arr(DataArrayMedInt_Copy(m0->getNodalConnectivity()));
397       std::transform(arr->begin(),arr->end(),arr->getPointer(),std::bind(std::plus<med_int>(),std::placeholders::_1,1));
398       MEDFILESAFECALLERWR0(MEDmeshElementConnectivityWr,(fid,mname.c_str(),dt,it,timm,MED_CELL,curMedType,MED_NODAL,MED_FULL_INTERLACE,ToMedInt(nbOfCells),arr->begin()));
399     }
400   else
401     {
402       const MEDCoupling1DGTUMesh *m0(dynamic_cast<const MEDCoupling1DGTUMesh *>(m));
403       if(!m0)
404         throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::Write : internal error #2 !");
405       if(ikt==INTERP_KERNEL::NORM_POLYGON || ikt==INTERP_KERNEL::NORM_QPOLYG)
406         {
407           MCAuto<DataArrayMedInt> arr(DataArrayMedInt_Copy(m0->getNodalConnectivity())),arrI(DataArrayMedInt_Copy(m0->getNodalConnectivityIndex()));
408           std::transform(arr->begin(),arr->end(),arr->getPointer(),std::bind(std::plus<med_int>(),std::placeholders::_1,1));
409           std::transform(arrI->begin(),arrI->end(),arrI->getPointer(),std::bind(std::plus<med_int>(),std::placeholders::_1,1));
410           MEDFILESAFECALLERWR0(MEDmeshPolygon2Wr,(fid,mname.c_str(),dt,it,timm,MED_CELL,ikt==INTERP_KERNEL::NORM_POLYGON?MED_POLYGON:MED_POLYGON2,MED_NODAL,ToMedInt(nbOfCells+1),arrI->begin(),arr->begin()));
411         }
412       else
413         {
414           const mcIdType *conn(m0->getNodalConnectivity()->begin()),*connI(m0->getNodalConnectivityIndex()->begin());
415           mcIdType meshLgth=m0->getNodalConnectivityLength();
416           mcIdType nbOfFaces=ToIdType(std::count(conn,conn+meshLgth,-1)+nbOfCells);
417           INTERP_KERNEL::AutoPtr<med_int> tab1=new med_int[nbOfCells+1];
418           med_int *w1=tab1; *w1=1;
419           INTERP_KERNEL::AutoPtr<med_int> tab2=new med_int[nbOfFaces+1];
420           med_int *w2=tab2; *w2=1;
421           INTERP_KERNEL::AutoPtr<med_int> bigtab=new med_int[meshLgth];
422           med_int *bt=bigtab;
423           for(mcIdType i=0;i<nbOfCells;i++,w1++)
424             {
425               int nbOfFaces2=0;
426               for(const mcIdType *w=conn+connI[i];w!=conn+connI[i+1];w2++)
427                 {
428                   const mcIdType *wend=std::find(w,conn+connI[i+1],-1);
429                   bt=std::transform(w,wend,bt,std::bind(std::plus<mcIdType>(),std::placeholders::_1,1));
430                   std::size_t nbOfNode=std::distance(w,wend);
431                   w2[1]=w2[0]+(med_int)nbOfNode;
432                   if(wend!=conn+connI[i+1])
433                     w=wend+1;
434                   else
435                     w=wend;
436                   nbOfFaces2++;
437                 }
438               w1[1]=w1[0]+nbOfFaces2;
439             }
440           MEDFILESAFECALLERWR0(MEDmeshPolyhedronWr,(fid,mname.c_str(),dt,it,timm,MED_CELL,MED_NODAL,ToMedInt(nbOfCells+1),tab1,ToMedInt(nbOfFaces+1),tab2,bigtab));
441         }
442     }
443   if(fam)
444     MEDFILESAFECALLERWR0(MEDmeshEntityFamilyNumberWr,(fid,mname.c_str(),dt,it,MED_CELL,curMedType,ToMedInt(nbOfCells),ToMedIntArray<mcIdType>(fam)->getConstPointer()));
445   if(num)
446     MEDFILESAFECALLERWR0(MEDmeshEntityNumberWr,(fid,mname.c_str(),dt,it,MED_CELL,curMedType,ToMedInt(nbOfCells),ToMedIntArray<mcIdType>(num)->getConstPointer()));
447   if(names)
448     {
449       if(names->getNumberOfComponents()!=MED_SNAME_SIZE)
450         {
451           std::ostringstream oss; oss << "MEDFileUMeshPerType::write : expected a name field on cells with number of components set to " << MED_SNAME_SIZE;
452           oss << " ! The array has " << names->getNumberOfComponents() << " components !";
453           throw INTERP_KERNEL::Exception(oss.str().c_str());
454         }
455       MEDFILESAFECALLERWR0(MEDmeshEntityNameWr,(fid,mname.c_str(),dt,it,MED_CELL,curMedType,ToMedInt(nbOfCells),names->getConstPointer()));
456     }
457 }