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