1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDFileMeshElt.hxx"
22 #include "MEDFileMeshReadSelector.hxx"
24 #include "MEDCouplingUMesh.hxx"
26 #include "InterpKernelAutoPtr.hxx"
27 #include "CellModel.hxx"
31 extern med_geometry_type typmai3[34];
33 using namespace ParaMEDMEM;
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)
37 med_entity_type whichEntity;
38 if(!isExisting(fid,mName,dt,it,geoElt,whichEntity))
40 return new MEDFileUMeshPerType(fid,mName,dt,it,mdim,geoElt,geoElt2,whichEntity,mrs);
43 std::size_t MEDFileUMeshPerType::getHeapMemorySizeWithoutChildren() const
48 std::vector<const BigMemoryObject *> MEDFileUMeshPerType::getDirectChildrenWithNull() const
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);
58 bool MEDFileUMeshPerType::isExisting(med_idt fid, const char *mName, int dt, int it, med_geometry_type geoElt, med_entity_type& whichEntity)
60 static const med_entity_type entities[3]={MED_CELL,MED_DESCENDING_FACE,MED_DESCENDING_EDGE};
64 med_bool changement,transformation;
65 int tmp=MEDmeshnEntity(fid,mName,dt,it,entities[i],geoElt,MED_CONNECTIVITY,MED_NODAL,
66 &changement,&transformation);
70 whichEntity=entities[i];
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;
78 int MEDFileUMeshPerType::getDim() const
80 return _m->getMeshDimension();
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)
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);
92 loadFromStaticType(fid,mName,dt,it,mdim,curNbOfElem,geoElt,type,entity,mrs);
95 if(type==INTERP_KERNEL::NORM_POLYGON || type==INTERP_KERNEL::NORM_QPOLYG)
97 loadPolyg(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
100 //if(type==INTERP_KERNEL::NORM_POLYHED)
101 loadPolyh(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
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)
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);
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)
121 med_bool changement,transformation;
123 if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
125 if(!mrs || mrs->isCellFamilyFieldReading())
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);
134 if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
136 if(!mrs || mrs->isCellNumFieldReading())
138 _num=DataArrayInt::New();
139 _num->alloc(curNbOfElem,1);
140 if(MEDmeshEntityNumberRd(fid,mName,dt,it,entity,geoElt,_num->getPointer())!=0)
145 if(MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_NAME,MED_NODAL,&changement,&transformation)>0)
147 if(!mrs || mrs->isCellNameFieldReading())
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)
154 _names->reAlloc(curNbOfElem);//not a bug to avoid the memory corruption due to last \0 at the end
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)
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);
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)
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());
197 for(int i=0;i<curNbOfElem;i++)
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++)
204 wFinalConn=std::transform(locConn+indexFace[j]-1,locConn+indexFace[j+1]-1,wFinalConn,std::bind2nd(std::plus<int>(),-1));
207 mc->setNodalConnectivity(conn,connI);
208 loadCommonPart(fid,mName,dt,it,mdim,curNbOfElem,MED_POLYHEDRON,entity,mrs);
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)
213 int nbOfCells=m->getNumberOfCells();
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];
223 const MEDCoupling1SGTUMesh *m0(dynamic_cast<const MEDCoupling1SGTUMesh *>(m));
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());
232 const MEDCoupling1DGTUMesh *m0(dynamic_cast<const MEDCoupling1DGTUMesh *>(m));
234 throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::Write : internal error #2 !");
235 if(ikt==INTERP_KERNEL::NORM_POLYGON || ikt==INTERP_KERNEL::NORM_QPOLYG)
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());
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];
249 INTERP_KERNEL::AutoPtr<int> tab2=new int[nbOfFaces+1];
251 INTERP_KERNEL::AutoPtr<int> bigtab=new int[meshLgth];
253 for(int i=0;i<nbOfCells;i++,w1++)
256 for(const int *w=conn+connI[i];w!=conn+connI[i+1];w2++)
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])
268 w1[1]=w1[0]+nbOfFaces2;
270 MEDmeshPolyhedronWr(fid,mname.c_str(),dt,it,timm,MED_CELL,MED_NODAL,nbOfCells+1,tab1,nbOfFaces+1,tab2,bigtab);
274 MEDmeshEntityFamilyNumberWr(fid,mname.c_str(),dt,it,MED_CELL,curMedType,nbOfCells,fam->getConstPointer());
276 MEDmeshEntityNumberWr(fid,mname.c_str(),dt,it,MED_CELL,curMedType,nbOfCells,num->getConstPointer());
279 if(names->getNumberOfComponents()!=MED_SNAME_SIZE)
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());
285 MEDmeshEntityNameWr(fid,mname.c_str(),dt,it,MED_CELL,curMedType,nbOfCells,names->getConstPointer());