1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "MEDFileMeshElt.hxx"
22 #include "MEDCouplingUMesh.hxx"
24 #include "InterpKernelAutoPtr.hxx"
25 #include "CellModel.hxx"
29 extern med_geometry_type typmai3[32];
31 using namespace ParaMEDMEM;
33 MEDFileUMeshPerType *MEDFileUMeshPerType::New(med_idt fid, const char *mName, int dt, int it, int mdim, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType geoElt2)
35 med_entity_type whichEntity;
36 if(!isExisting(fid,mName,dt,it,geoElt,whichEntity))
38 return new MEDFileUMeshPerType(fid,mName,dt,it,mdim,geoElt,geoElt2,whichEntity);
41 bool MEDFileUMeshPerType::isExisting(med_idt fid, const char *mName, int dt, int it, med_geometry_type geoElt, med_entity_type& whichEntity)
43 static const med_entity_type entities[3]={MED_CELL,MED_DESCENDING_FACE,MED_DESCENDING_EDGE};
47 med_bool changement,transformation;
48 int tmp=MEDmeshnEntity(fid,mName,dt,it,entities[i],geoElt,MED_CONNECTIVITY,MED_NODAL,
49 &changement,&transformation);
53 whichEntity=entities[i];
55 std::cerr << "WARNING : MEDFile has been detected to be no compilant with MED 3 : Please change entity in MEDFile for geotype " << geoElt << std::endl;
61 int MEDFileUMeshPerType::getDim() const
63 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
64 return cm.getDimension();
67 MEDFileUMeshPerType::MEDFileUMeshPerType(med_idt fid, const char *mName, int dt, int it, int mdim, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType type,
68 med_entity_type entity):_type(type),_entity(entity)
70 med_bool changement,transformation;
71 int curNbOfElem=MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_CONNECTIVITY,MED_NODAL,
72 &changement,&transformation);
73 if(type!=INTERP_KERNEL::NORM_POLYGON && type!=INTERP_KERNEL::NORM_POLYHED)
75 loadFromStaticType(fid,mName,dt,it,mdim,curNbOfElem,geoElt,type,entity);
78 if(type==INTERP_KERNEL::NORM_POLYGON)
80 loadPolyg(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity);
83 //if(type==INTERP_KERNEL::NORM_POLYHED)
84 loadPolyh(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity);
87 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,
88 med_entity_type entity)
90 _conn=DataArrayInt::New();
91 int nbOfNodesPerCell=(geoElt%100);
92 _conn->alloc((nbOfNodesPerCell+1)*curNbOfElem,1);
93 _conn_index=DataArrayInt::New();
94 _conn_index->alloc(curNbOfElem+1,1);
95 INTERP_KERNEL::AutoPtr<int> connTab=new int[(nbOfNodesPerCell)*curNbOfElem];
96 _num=DataArrayInt::New();
97 _num->alloc(curNbOfElem,1);
98 _fam=DataArrayInt::New();
99 _fam->alloc(curNbOfElem,1);
100 med_bool changement,transformation;
101 INTERP_KERNEL::AutoPtr<char> noms=new char[MED_SNAME_SIZE*(std::size_t)curNbOfElem+1];
102 MEDmeshElementConnectivityRd(fid,mName,dt,it,entity,geoElt,MED_NODAL,MED_FULL_INTERLACE,connTab);
103 if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
105 if(MEDmeshEntityFamilyNumberRd(fid,mName,dt,it,entity,geoElt,_fam->getPointer())!=0)
106 std::fill(_fam->getPointer(),_fam->getPointer()+curNbOfElem,0);
109 std::fill(_fam->getPointer(),_fam->getPointer()+curNbOfElem,0);
110 if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
112 _num=DataArrayInt::New();
113 _num->alloc(curNbOfElem,1);
114 if(MEDmeshEntityNumberRd(fid,mName,dt,it,entity,geoElt,_num->getPointer())!=0)
119 int *w1=_conn->getPointer();
120 int *w2=_conn_index->getPointer();
122 const int *wi=connTab;
123 for(int i=0;i<curNbOfElem;i++,wi+=nbOfNodesPerCell,w2++)
126 w1=std::transform(wi,wi+nbOfNodesPerCell,w1,std::bind2nd(std::plus<int>(),-1));
127 *w2=w2[-1]+nbOfNodesPerCell+1;
131 void MEDFileUMeshPerType::loadPolyg(med_idt fid, const char *mName, int dt, int it, int mdim, int arraySize, med_geometry_type geoElt,
132 med_entity_type entity)
134 med_bool changement,transformation;
135 med_int curNbOfElem=MEDmeshnEntity(fid,mName,dt,it,entity,MED_POLYGON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation)-1;
136 _conn_index=DataArrayInt::New();
137 _conn_index->alloc(curNbOfElem+1,1);
138 _conn=DataArrayInt::New();
139 _conn->alloc(arraySize+curNbOfElem,1);
140 _num=DataArrayInt::New();
141 _num->alloc(curNbOfElem,1);
142 _fam=DataArrayInt::New();
143 _fam->alloc(curNbOfElem,1);
144 INTERP_KERNEL::AutoPtr<int> locConn=new int[arraySize];
145 MEDmeshPolygonRd(fid,mName,dt,it,MED_CELL,MED_NODAL,_conn_index->getPointer(),locConn);
146 int *w1=_conn->getPointer();
147 int *w2=_conn_index->getPointer();
148 const int *wi=locConn;
149 for(int i=0;i<curNbOfElem;i++,w2++)
151 *w1++=(int)INTERP_KERNEL::NORM_POLYGON;
152 const int *wi2=wi+(w2[1]-w2[0]);
153 w1=std::transform(wi,wi2,w1,std::bind2nd(std::plus<int>(),-1));
157 *w2=*w2-1+curNbOfElem;
158 if(MEDmeshnEntity(fid,mName,dt,it,entity,MED_POLYGON,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
160 if(MEDmeshEntityFamilyNumberRd(fid,mName,dt,it,entity,MED_POLYGON,_fam->getPointer())!=0)
161 std::fill(_fam->getPointer(),_fam->getPointer()+curNbOfElem,0);
164 std::fill(_fam->getPointer(),_fam->getPointer()+curNbOfElem,0);
165 if(MEDmeshnEntity(fid,mName,dt,it,entity,MED_POLYGON,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
167 if(MEDmeshEntityNumberRd(fid,mName,dt,it,entity,MED_POLYGON,_num->getPointer())!=0)
174 void MEDFileUMeshPerType::loadPolyh(med_idt fid, const char *mName, int dt, int it, int mdim, int connFaceLgth, med_geometry_type geoElt,
175 med_entity_type entity)
177 med_bool changement,transformation;
178 med_int indexFaceLgth=MEDmeshnEntity(fid,mName,dt,it,MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation);
179 int curNbOfElem=MEDmeshnEntity(fid,mName,dt,it,MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,&changement,&transformation)-1;
180 INTERP_KERNEL::AutoPtr<int> index=new int[curNbOfElem+1];
181 INTERP_KERNEL::AutoPtr<int> indexFace=new int[indexFaceLgth];
182 INTERP_KERNEL::AutoPtr<int> locConn=new int[connFaceLgth];
183 _fam=DataArrayInt::New();
184 _fam->alloc(curNbOfElem,1);
185 MEDmeshPolyhedronRd(fid,mName,dt,it,MED_CELL,MED_NODAL,index,indexFace,locConn);
186 if(MEDmeshnEntity(fid,mName,dt,it,entity,MED_POLYHEDRON,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
188 if(MEDmeshEntityFamilyNumberRd(fid,mName,dt,it,entity,MED_POLYHEDRON,_fam->getPointer())!=0)
189 std::fill(_fam->getPointer(),_fam->getPointer()+curNbOfElem,0);
192 std::fill(_fam->getPointer(),_fam->getPointer()+curNbOfElem,0);
193 int arraySize=connFaceLgth;
194 for(int i=0;i<curNbOfElem;i++)
195 arraySize+=index[i+1]-index[i]-1;
196 _conn=DataArrayInt::New();
197 _conn->alloc(arraySize+curNbOfElem,1);
198 int *wFinalConn=_conn->getPointer();
199 _conn_index=DataArrayInt::New();
200 _conn_index->alloc(curNbOfElem+1,1);
201 int *finalIndex=_conn_index->getPointer();
203 for(int i=0;i<curNbOfElem;i++)
205 *wFinalConn++=(int)INTERP_KERNEL::NORM_POLYHED;
206 finalIndex[i+1]=finalIndex[i]+index[i+1]-index[i]-1+indexFace[index[i+1]-1]-indexFace[index[i]-1]+1;
207 wFinalConn=std::transform(locConn+indexFace[index[i]-1]-1,locConn+indexFace[index[i]]-1,wFinalConn,std::bind2nd(std::plus<int>(),-1));
208 for(int j=index[i];j<index[i+1]-1;j++)
211 wFinalConn=std::transform(locConn+indexFace[j]-1,locConn+indexFace[j+1]-1,wFinalConn,std::bind2nd(std::plus<int>(),-1));
214 _num=DataArrayInt::New();
215 _num->alloc(curNbOfElem,1);
216 if(MEDmeshnEntity(fid,mName,dt,it,entity,MED_POLYHEDRON,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
218 if(MEDmeshEntityNumberRd(fid,mName,dt,it,entity,MED_POLYHEDRON,_num->getPointer())!=0)
225 void MEDFileUMeshPerType::write(med_idt fid, const char *mname, int mdim, const MEDCouplingUMesh *m, const DataArrayInt *fam, const DataArrayInt *num)
227 int nbOfCells=m->getNumberOfCells();
231 double timm=m->getTime(dt,it);
232 INTERP_KERNEL::NormalizedCellType ikt=m->getTypeOfCell(0);
233 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(ikt);
234 med_geometry_type curMedType=typmai3[(int)ikt];
235 const int *conn=m->getNodalConnectivity()->getConstPointer();
236 const int *connI=m->getNodalConnectivityIndex()->getConstPointer();
237 if(ikt!=INTERP_KERNEL::NORM_POLYGON && ikt!=INTERP_KERNEL::NORM_POLYHED)
239 int nbNodesPerCell=cm.getNumberOfNodes();
240 INTERP_KERNEL::AutoPtr<int> tab=new int[nbNodesPerCell*nbOfCells];
242 for(int i=0;i<nbOfCells;i++)
243 w=std::transform(conn+connI[i]+1,conn+connI[i+1],w,std::bind2nd(std::plus<int>(),1));
244 MEDmeshElementConnectivityWr(fid,mname,dt,it,timm,MED_CELL,curMedType,MED_NODAL,MED_FULL_INTERLACE,nbOfCells,tab);
248 if(ikt==INTERP_KERNEL::NORM_POLYGON)
250 INTERP_KERNEL::AutoPtr<int> tab1=new int[nbOfCells+1];
251 INTERP_KERNEL::AutoPtr<int> tab2=new int[m->getMeshLength()];
254 for(int i=0;i<nbOfCells;i++,wI++)
256 wI[1]=wI[0]+connI[i+1]-connI[i]-1;
257 w=std::transform(conn+connI[i]+1,conn+connI[i+1],w,std::bind2nd(std::plus<int>(),1));
259 MEDmeshPolygonWr(fid,mname,dt,it,timm,MED_CELL,MED_NODAL,nbOfCells+1,tab1,tab2);
263 int meshLgth=m->getMeshLength();
264 int nbOfFaces=std::count(conn,conn+meshLgth,-1)+nbOfCells;
265 INTERP_KERNEL::AutoPtr<int> tab1=new int[nbOfCells+1];
267 INTERP_KERNEL::AutoPtr<int> tab2=new int[nbOfFaces+1];
269 INTERP_KERNEL::AutoPtr<int> bigtab=new int[meshLgth-nbOfCells];
271 for(int i=0;i<nbOfCells;i++,w1++)
274 for(const int *w=conn+connI[i]+1;w!=conn+connI[i+1];w2++)
276 const int *wend=std::find(w,conn+connI[i+1],-1);
277 bt=std::transform(w,wend,bt,std::bind2nd(std::plus<int>(),1));
278 int nbOfNode=std::distance(w,wend);
279 w2[1]=w2[0]+nbOfNode;
280 if(wend!=conn+connI[i+1])
286 w1[1]=w1[0]+nbOfFaces2;
288 MEDmeshPolyhedronWr(fid,mname,dt,it,timm,MED_CELL,MED_NODAL,nbOfCells+1,tab1,nbOfFaces+1,tab2,bigtab);
292 MEDmeshEntityFamilyNumberWr(fid,mname,dt,it,MED_CELL,curMedType,nbOfCells,fam->getConstPointer());
294 MEDmeshEntityNumberWr(fid,mname,dt,it,MED_CELL,curMedType,nbOfCells,num->getConstPointer());