Salome HOME
Synchronize adm files
[modules/med.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 "InterpKernelAutoPtr.hxx"
27 #include "CellModel.hxx"
28
29 #include <iostream>
30
31 extern med_geometry_type typmai3[34];
32
33 using namespace ParaMEDMEM;
34
35 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)
36 {
37   med_entity_type whichEntity;
38   if(!isExisting(fid,mName,dt,it,geoElt,whichEntity))
39     return 0;
40   return new MEDFileUMeshPerType(fid,mName,dt,it,mdim,geoElt,geoElt2,whichEntity,mrs);
41 }
42
43 std::size_t MEDFileUMeshPerType::getHeapMemorySizeWithoutChildren() const
44 {
45   return 0;
46 }
47
48 std::vector<const BigMemoryObject *> MEDFileUMeshPerType::getDirectChildrenWithNull() const
49 {
50   std::vector<const BigMemoryObject *> ret;
51   ret.push_back((const MEDCoupling1GTUMesh *)_m);
52   ret.push_back((const DataArrayInt *)_num);
53   ret.push_back((const DataArrayInt *)_fam);
54   ret.push_back((const DataArrayAsciiChar *)_names);
55   return ret;
56 }
57
58 bool MEDFileUMeshPerType::isExisting(med_idt fid, const char *mName, int dt, int it, med_geometry_type geoElt, med_entity_type& whichEntity)
59 {
60   static const med_entity_type entities[3]={MED_CELL,MED_DESCENDING_FACE,MED_DESCENDING_EDGE};
61   int nbOfElt=0;
62   for(int i=0;i<3;i++)
63     {
64       med_bool changement,transformation;
65       int tmp=MEDmeshnEntity(fid,mName,dt,it,entities[i],geoElt,MED_CONNECTIVITY,MED_NODAL,
66           &changement,&transformation);
67       if(tmp>nbOfElt)
68         {
69           nbOfElt=tmp;
70           whichEntity=entities[i];
71           if(i>0)
72             std::cerr << "WARNING : MEDFile has been detected to be no compilant with MED 3 : Please change entity in MEDFile for geotype " <<  geoElt << std::endl;
73         }
74     }
75   return nbOfElt>0;
76 }
77
78 int MEDFileUMeshPerType::getDim() const
79 {
80   return _m->getMeshDimension();
81 }
82
83 MEDFileUMeshPerType::MEDFileUMeshPerType(med_idt fid, const char *mName, int dt, int it, int mdim, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType type,
84                                          med_entity_type entity, MEDFileMeshReadSelector *mrs):_entity(entity)
85 {
86   med_bool changement,transformation;
87   int curNbOfElem=MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_CONNECTIVITY,MED_NODAL,
88       &changement,&transformation);
89   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
90   if(!cm.isDynamic())
91     {
92       loadFromStaticType(fid,mName,dt,it,mdim,curNbOfElem,geoElt,type,entity,mrs);
93       return;
94     }
95   if(type==INTERP_KERNEL::NORM_POLYGON || type==INTERP_KERNEL::NORM_QPOLYG)
96     {
97       loadPolyg(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
98       return;
99     }
100   //if(type==INTERP_KERNEL::NORM_POLYHED)
101   loadPolyh(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
102 }
103
104 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,
105                                              med_entity_type entity, MEDFileMeshReadSelector *mrs)
106 {
107   _m=MEDCoupling1SGTUMesh::New(mName,type);
108   MEDCoupling1SGTUMesh *mc(dynamic_cast<MEDCoupling1SGTUMesh *>((MEDCoupling1GTUMesh *)_m));
109   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
110   int nbOfNodesPerCell=mc->getNumberOfNodesPerCell();
111   conn->alloc(nbOfNodesPerCell*curNbOfElem,1);
112   MEDmeshElementConnectivityRd(fid,mName,dt,it,entity,geoElt,MED_NODAL,MED_FULL_INTERLACE,conn->getPointer());
113   std::transform(conn->begin(),conn->end(),conn->getPointer(),std::bind2nd(std::plus<int>(),-1));
114   mc->setNodalConnectivity(conn);
115   loadCommonPart(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
116 }
117
118 void MEDFileUMeshPerType::loadCommonPart(med_idt fid, const char *mName, int dt, int it, int mdim, int curNbOfElem, med_geometry_type geoElt,
119                                          med_entity_type entity, MEDFileMeshReadSelector *mrs)
120 {
121   med_bool changement,transformation;
122   _fam=0;
123   if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
124     {    
125       if(!mrs || mrs->isCellFamilyFieldReading())
126         {
127           _fam=DataArrayInt::New();
128           _fam->alloc(curNbOfElem,1);
129           if(MEDmeshEntityFamilyNumberRd(fid,mName,dt,it,entity,geoElt,_fam->getPointer())!=0)
130             std::fill(_fam->getPointer(),_fam->getPointer()+curNbOfElem,0);
131         }
132     }
133   _num=0;
134   if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
135     {
136       if(!mrs || mrs->isCellNumFieldReading())
137         {
138           _num=DataArrayInt::New();
139           _num->alloc(curNbOfElem,1);
140           if(MEDmeshEntityNumberRd(fid,mName,dt,it,entity,geoElt,_num->getPointer())!=0)
141             _num=0;
142         }
143     }
144   _names=0;
145   if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_NAME,MED_NODAL,&changement,&transformation)>0)
146     {
147       if(!mrs || mrs->isCellNameFieldReading())
148         {
149           _names=DataArrayAsciiChar::New();
150           _names->alloc(curNbOfElem+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
151           if(MEDmeshEntityNameRd(fid,mName,dt,it,entity,geoElt,_names->getPointer())!=0)
152             _names=0;
153           else
154             _names->reAlloc(curNbOfElem);//not a bug to avoid the memory corruption due to last \0 at the end
155         }
156     }
157 }
158
159 void MEDFileUMeshPerType::loadPolyg(med_idt fid, const char *mName, int dt, int it, int mdim, int arraySize, med_geometry_type geoElt,
160                                     med_entity_type entity, MEDFileMeshReadSelector *mrs)
161 {
162   med_bool changement,transformation;
163   med_int curNbOfElem=MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_INDEX_NODE,MED_NODAL,&changement,&transformation)-1;
164   _m=MEDCoupling1DGTUMesh::New(mName,geoElt==MED_POLYGON?INTERP_KERNEL::NORM_POLYGON:INTERP_KERNEL::NORM_QPOLYG);
165   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1DGTUMesh> mc(DynamicCast<MEDCoupling1GTUMesh,MEDCoupling1DGTUMesh>(_m));
166   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New()),connI(DataArrayInt::New());
167   conn->alloc(arraySize,1); connI->alloc(curNbOfElem+1,1);
168   MEDmeshPolygon2Rd(fid,mName,dt,it,MED_CELL,geoElt,MED_NODAL,connI->getPointer(),conn->getPointer());
169   std::transform(conn->begin(),conn->end(),conn->getPointer(),std::bind2nd(std::plus<int>(),-1));
170   std::transform(connI->begin(),connI->end(),connI->getPointer(),std::bind2nd(std::plus<int>(),-1));
171   mc->setNodalConnectivity(conn,connI);
172   loadCommonPart(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
173 }
174
175 void MEDFileUMeshPerType::loadPolyh(med_idt fid, const char *mName, int dt, int it, int mdim, int connFaceLgth, med_geometry_type geoElt,
176                                     med_entity_type entity, MEDFileMeshReadSelector *mrs)
177 {
178   med_bool changement,transformation;
179   med_int indexFaceLgth=MEDmeshnEntity(fid,mName,dt,it,MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation);
180   int curNbOfElem=MEDmeshnEntity(fid,mName,dt,it,MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,&changement,&transformation)-1;
181   _m=MEDCoupling1DGTUMesh::New(mName,INTERP_KERNEL::NORM_POLYHED);
182   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1DGTUMesh> mc(DynamicCastSafe<MEDCoupling1GTUMesh,MEDCoupling1DGTUMesh>(_m));
183   INTERP_KERNEL::AutoPtr<int> index=new int[curNbOfElem+1];
184   INTERP_KERNEL::AutoPtr<int> indexFace=new int[indexFaceLgth];
185   INTERP_KERNEL::AutoPtr<int> locConn=new int[connFaceLgth];
186   MEDmeshPolyhedronRd(fid,mName,dt,it,MED_CELL,MED_NODAL,index,indexFace,locConn);
187   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New()),connI(DataArrayInt::New());
188   int arraySize=connFaceLgth;
189   for(int i=0;i<curNbOfElem;i++)
190     arraySize+=index[i+1]-index[i]-1;
191   conn=DataArrayInt::New();
192   conn->alloc(arraySize,1);
193   int *wFinalConn=conn->getPointer();
194   connI->alloc(curNbOfElem+1,1);
195   int *finalIndex(connI->getPointer());
196   finalIndex[0]=0;
197   for(int i=0;i<curNbOfElem;i++)
198     {
199       finalIndex[i+1]=finalIndex[i]+index[i+1]-index[i]-1+indexFace[index[i+1]-1]-indexFace[index[i]-1];
200       wFinalConn=std::transform(locConn+indexFace[index[i]-1]-1,locConn+indexFace[index[i]]-1,wFinalConn,std::bind2nd(std::plus<int>(),-1));
201       for(int j=index[i];j<index[i+1]-1;j++)
202         {
203           *wFinalConn++=-1;
204           wFinalConn=std::transform(locConn+indexFace[j]-1,locConn+indexFace[j+1]-1,wFinalConn,std::bind2nd(std::plus<int>(),-1));
205         }
206     }
207   mc->setNodalConnectivity(conn,connI);
208   loadCommonPart(fid,mName,dt,it,mdim,curNbOfElem,MED_POLYHEDRON,entity,mrs);
209 }
210
211 void MEDFileUMeshPerType::Write(med_idt fid, const std::string& mname, int mdim, const MEDCoupling1GTUMesh *m, const DataArrayInt *fam, const DataArrayInt *num, const DataArrayAsciiChar *names)
212 {
213   int nbOfCells=m->getNumberOfCells();
214   if(nbOfCells<1)
215     return ;
216   int dt,it;
217   double timm=m->getTime(dt,it);
218   INTERP_KERNEL::NormalizedCellType ikt=m->getTypeOfCell(0);
219   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(ikt);
220   med_geometry_type curMedType=typmai3[(int)ikt];
221   if(!cm.isDynamic())
222     {
223       const MEDCoupling1SGTUMesh *m0(dynamic_cast<const MEDCoupling1SGTUMesh *>(m));
224       if(!m0)
225         throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::Write : internal error #1 !");
226       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(m0->getNodalConnectivity()->deepCpy());
227       std::transform(arr->begin(),arr->end(),arr->getPointer(),std::bind2nd(std::plus<int>(),1));
228       MEDmeshElementConnectivityWr(fid,mname.c_str(),dt,it,timm,MED_CELL,curMedType,MED_NODAL,MED_FULL_INTERLACE,nbOfCells,arr->begin());
229     }
230   else
231     {
232       const MEDCoupling1DGTUMesh *m0(dynamic_cast<const MEDCoupling1DGTUMesh *>(m));
233       if(!m0)
234         throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::Write : internal error #2 !");
235       if(ikt==INTERP_KERNEL::NORM_POLYGON || ikt==INTERP_KERNEL::NORM_QPOLYG)
236         {
237           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(m0->getNodalConnectivity()->deepCpy()),arrI(m0->getNodalConnectivityIndex()->deepCpy());
238           std::transform(arr->begin(),arr->end(),arr->getPointer(),std::bind2nd(std::plus<int>(),1));
239           std::transform(arrI->begin(),arrI->end(),arrI->getPointer(),std::bind2nd(std::plus<int>(),1));
240           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());
241         }
242       else
243         {
244           const int *conn(m0->getNodalConnectivity()->begin()),*connI(m0->getNodalConnectivityIndex()->begin());
245           int meshLgth=m0->getNodalConnectivityLength();
246           int nbOfFaces=std::count(conn,conn+meshLgth,-1)+nbOfCells;
247           INTERP_KERNEL::AutoPtr<int> tab1=new int[nbOfCells+1];
248           int *w1=tab1; *w1=1;
249           INTERP_KERNEL::AutoPtr<int> tab2=new int[nbOfFaces+1];
250           int *w2=tab2; *w2=1;
251           INTERP_KERNEL::AutoPtr<int> bigtab=new int[meshLgth];
252           int *bt=bigtab;
253           for(int i=0;i<nbOfCells;i++,w1++)
254             {
255               int nbOfFaces2=0;
256               for(const int *w=conn+connI[i];w!=conn+connI[i+1];w2++)
257                 {
258                   const int *wend=std::find(w,conn+connI[i+1],-1);
259                   bt=std::transform(w,wend,bt,std::bind2nd(std::plus<int>(),1));
260                   int nbOfNode=std::distance(w,wend);
261                   w2[1]=w2[0]+nbOfNode;
262                   if(wend!=conn+connI[i+1])
263                     w=wend+1;
264                   else
265                     w=wend;
266                   nbOfFaces2++;
267                 }
268               w1[1]=w1[0]+nbOfFaces2;
269             }
270           MEDmeshPolyhedronWr(fid,mname.c_str(),dt,it,timm,MED_CELL,MED_NODAL,nbOfCells+1,tab1,nbOfFaces+1,tab2,bigtab);
271         }
272     }
273   if(fam)
274     MEDmeshEntityFamilyNumberWr(fid,mname.c_str(),dt,it,MED_CELL,curMedType,nbOfCells,fam->getConstPointer());
275   if(num)
276     MEDmeshEntityNumberWr(fid,mname.c_str(),dt,it,MED_CELL,curMedType,nbOfCells,num->getConstPointer());
277   if(names)
278     {
279       if(names->getNumberOfComponents()!=MED_SNAME_SIZE)
280         {
281           std::ostringstream oss; oss << "MEDFileUMeshPerType::write : expected a name field on cells with number of components set to " << MED_SNAME_SIZE;
282           oss << " ! The array has " << names->getNumberOfComponents() << " components !";
283           throw INTERP_KERNEL::Exception(oss.str().c_str());
284         }
285       MEDmeshEntityNameWr(fid,mname.c_str(),dt,it,MED_CELL,curMedType,nbOfCells,names->getConstPointer());
286     }
287 }