1 // Copyright (C) 2007-2013 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 "SauvLoaderTest.hxx"
22 #include "SauvReader.hxx"
23 #include "SauvWriter.hxx"
24 #include "MEDFileData.hxx"
25 #include "MEDCouplingFieldDouble.hxx"
26 #include "MEDCouplingMemArray.hxx"
37 using namespace ParaMEDMEM;
39 void SauvLoaderTest::testSauv2Med()
41 // read a file containing all types of readable piles
42 std::string file = getResourceFile("allPillesTest.sauv");
43 MEDCouplingAutoRefCountObjectPtr<SauvReader> sr=SauvReader::New(file.c_str());
44 MEDCouplingAutoRefCountObjectPtr<MEDFileData> d2=sr->loadInMEDFileDS();
46 d2->write("allPillesTest.med",0);
48 CPPUNIT_ASSERT_EQUAL(1,d2->getNumberOfMeshes());
49 CPPUNIT_ASSERT_EQUAL(8+97,d2->getNumberOfFields());
50 MEDFileMesh * m = d2->getMeshes()->getMeshAtPos(0);
51 CPPUNIT_ASSERT_EQUAL(17,int(m->getGroupsNames().size()));
54 void SauvLoaderTest::testMed2SauvOnAMeshWithVoidFamily()
56 // Create a mesh with 2 quads.
57 const int spaceDim = 2;
58 const int nbOfNodes = 6;
59 double coords[nbOfNodes*spaceDim] = {0,0, 1,0, 1,1, 0,1, 2,0, 2,1};
60 int conn[8]={0,1,2,3, 1,4,5,2};
61 MEDCouplingUMesh *mesh2d=MEDCouplingUMesh::New("Mesh",spaceDim);
62 mesh2d->allocateCells(2);
63 mesh2d->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,conn);
64 mesh2d->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,conn+4);
65 mesh2d->finishInsertingCells();
66 DataArrayDouble *myCoords=DataArrayDouble::New();
67 myCoords->alloc(nbOfNodes,spaceDim);
68 std::copy(coords,coords+nbOfNodes*spaceDim,myCoords->getPointer());
69 mesh2d->setCoords(myCoords);
72 // create a MedFileUMesh
73 MEDFileUMesh* m= MEDFileUMesh::New();
74 m->setMeshAtLevel(0,mesh2d);
76 // Create families and groups
78 DataArrayInt *fam = DataArrayInt::New();
80 int elemsFams[2] = {-2,-3};
81 std::copy(elemsFams,elemsFams+2,fam->getPointer());
83 m->setFamilyFieldArr(0,fam);
85 std::map<std::string,int> theFamilies;
86 theFamilies["FAM_-1"]=-1;
87 theFamilies["FAM_-2"]=-2;
88 theFamilies["FAM_-3"]=-3;
90 std::map<std::string, std::vector<std::string> > theGroups;
91 theGroups["Group1"].push_back("FAM_-2");
92 theGroups["Group2"].push_back("FAM_-3");
93 theGroups["Grouptot"].push_back("FAM_-1");
94 theGroups["Grouptot"].push_back("FAM_-2");
95 theGroups["Grouptot"].push_back("FAM_-3");
96 m->setFamilyInfo(theFamilies);
97 m->setGroupInfo(theGroups);
99 // write to MED for visual check
100 //const char* medFile = "mesh_with_void_family.med";
101 //m->write(medFile, 2);
104 const char* sauvFile = "mesh_with_void_family.sauv";
105 MEDFileData* medData = MEDFileData::New();
106 MEDFileMeshes* medMeshes = MEDFileMeshes::New();
107 SauvWriter* sw=SauvWriter::New();
108 medMeshes->setMeshAtPos(0, m);
109 medData->setMeshes(medMeshes);
110 sw->setMEDFileDS(medData);
113 // read SAUV and check groups
114 MEDCouplingAutoRefCountObjectPtr<SauvReader> sr=SauvReader::New(sauvFile);
115 MEDCouplingAutoRefCountObjectPtr<MEDFileData> d2=sr->loadInMEDFileDS();
116 MEDFileUMesh * m2 = static_cast<MEDFileUMesh*>( d2->getMeshes()->getMeshAtPos(0) );
117 std::vector<std::string > groups = m->getGroupsNames();
118 std::cout << "Number of groups: " << groups.size() << std::endl;
119 CPPUNIT_ASSERT_EQUAL(3,(int)groups.size());
120 MEDCouplingUMesh * grp1 = m2->getGroup(0, "Group1");
121 CPPUNIT_ASSERT_EQUAL(1,(int)grp1->getNumberOfCells());
122 MEDCouplingUMesh * grp2 = m2->getGroup(0, "Group2");
123 CPPUNIT_ASSERT_EQUAL(1,(int)grp2->getNumberOfCells());
124 MEDCouplingUMesh * grptot = m2->getGroup(0, "Grouptot");
125 CPPUNIT_ASSERT_EQUAL(2,(int)grptot->getNumberOfCells());
130 medMeshes->decrRef();
139 void SauvLoaderTest::testMed2Sauv()
142 std::string file = getResourceFile("pointe.med");
143 MEDCouplingAutoRefCountObjectPtr<MEDFileData> pointeMed=MEDFileData::New(file.c_str());
145 // add 3 faces to pointeMed
146 MEDFileUMesh* pointeMedMesh = static_cast<MEDFileUMesh*>(pointeMed->getMeshes()->getMeshAtPos(0));
147 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> pointeM1D = MEDCouplingUMesh::New();
148 DataArrayDouble *coords = pointeMedMesh->getCoords();
149 pointeM1D->setCoords( coords );
150 pointeM1D->setMeshDimension( 2 );
151 pointeM1D->allocateCells( 3 );
154 0,1,2, 0,1,3, 10,11,12,13
156 pointeM1D->insertNextCell( INTERP_KERNEL::NORM_TRI3, 3, conn);
157 pointeM1D->insertNextCell( INTERP_KERNEL::NORM_TRI3, 3, conn+3);
158 pointeM1D->insertNextCell( INTERP_KERNEL::NORM_QUAD4, 4, conn+6);
159 pointeM1D->finishInsertingCells();
160 pointeMedMesh->setMeshAtLevel( -1, pointeM1D );
161 pointeMed->getMeshes()->setMeshAtPos( 0, pointeMedMesh );
163 // add a field on 2 faces to pointeMed
164 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> ff1=MEDFileFieldMultiTS::New();
165 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f1=MEDCouplingFieldDouble::New(ON_GAUSS_NE,ONE_TIME);
166 f1->setMesh( pointeM1D );
167 f1->setName("Field on 2 faces");
168 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> d=DataArrayDouble::New();
170 d->setInfoOnComponent(0,"sigX [MPa]");
171 d->setInfoOnComponent(1,"sigY [GPa]");
172 double vals[2*(3+4)] =
174 311,312,321,322,331,332,411,412,421,422,431,432,441,442
176 std::copy(vals,vals+d->getNbOfElems(),d->getPointer());
178 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=DataArrayInt::New();
184 std::copy(ids,ids+da->getNbOfElems(),da->getPointer());
186 ff1->appendFieldProfile(f1,pointeMedMesh,-1,da);
187 pointeMed->getFields()->pushField( ff1 );
189 // remove "fieldnodeint"
190 MEDFileFields* pointeFields = pointeMed->getFields();
191 for ( int i = 0; i < pointeFields->getNumberOfFields(); ++i )
193 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> ts = pointeFields->getFieldAtPos(i);
194 if ( std::string("fieldnodeint") == ts->getName())
196 pointeFields->destroyFieldAtPos( i );
200 // write pointeMed to SAUV
201 const char* sauvFile = "pointe.sauv";
202 MEDCouplingAutoRefCountObjectPtr<SauvWriter> sw=SauvWriter::New();
203 sw->setMEDFileDS(pointeMed);
206 // read SAUV and check
207 MEDCouplingAutoRefCountObjectPtr<SauvReader> sr=SauvReader::New(sauvFile);
208 MEDCouplingAutoRefCountObjectPtr<MEDFileData> d2=sr->loadInMEDFileDS();
209 CPPUNIT_ASSERT_EQUAL(1,d2->getNumberOfMeshes());
210 CPPUNIT_ASSERT_EQUAL(4,d2->getNumberOfFields());
211 MEDFileUMesh * m = static_cast<MEDFileUMesh*>( d2->getMeshes()->getMeshAtPos(0) );
212 CPPUNIT_ASSERT_EQUAL(std::string("maa1"),std::string(m->getName() ));
213 CPPUNIT_ASSERT_EQUAL(3,m->getMeshDimension());
214 std::vector<std::string > groups = m->getGroupsNames();
215 CPPUNIT_ASSERT_EQUAL(6,(int)groups.size());
216 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe1") != groups.end() );
217 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe2") != groups.end() );
218 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe3") != groups.end() );
219 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe4") != groups.end() );
220 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe5") != groups.end() );
221 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"maa1") != groups.end() );
222 CPPUNIT_ASSERT_EQUAL(16,m->getSizeAtLevel(0));
223 MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> um0 = m->getGenMeshAtLevel(0);
224 CPPUNIT_ASSERT_EQUAL(12, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_TETRA4 ));
225 CPPUNIT_ASSERT_EQUAL(2, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_PYRA5 ));
226 CPPUNIT_ASSERT_EQUAL(2, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_HEXA8 ));
227 MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> um1 = m->getGenMeshAtLevel(-1);
228 CPPUNIT_ASSERT_EQUAL(2, um1->getNumberOfCellsWithType( INTERP_KERNEL::NORM_TRI3 ));
229 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> pointeUM0 =
230 static_cast<MEDCouplingUMesh*>( pointeMedMesh->getGenMeshAtLevel(0));
231 DataArrayDouble *coo = m->getCoords();
232 DataArrayDouble *pointeCoo = pointeMedMesh->getCoords();
233 CPPUNIT_ASSERT(coo->isEqualWithoutConsideringStr(*pointeCoo,1e-12));
234 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> vol = um0->getMeasureField(0);
235 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> pointeVol = pointeUM0->getMeasureField(0);
236 CPPUNIT_ASSERT_DOUBLES_EQUAL( vol->accumulate(0), pointeVol->accumulate(0),1e-12);
239 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldnodedoubleTS1 =
240 pointeMed->getFields()->getFieldWithName("fieldnodedouble");
241 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldnodedoubleTS2 =
242 d2->getFields()->getFieldWithName("fieldnodedouble");
243 CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getInfo().size(), fieldnodedoubleTS2->getInfo().size());
244 for ( size_t i = 0; i < fieldnodedoubleTS1->getInfo().size(); ++i )
245 CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getInfo()[i], fieldnodedoubleTS2->getInfo()[i]);
246 CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getNumberOfTS(), fieldnodedoubleTS2->getNumberOfTS());
247 std::vector< std::pair<int,int> > io1 = fieldnodedoubleTS1->getIterations();
248 std::vector< std::pair<int,int> > io2 = fieldnodedoubleTS2->getIterations();
249 for ( int i =0; i < fieldnodedoubleTS1->getNumberOfTS(); ++i )
251 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd1 =
252 fieldnodedoubleTS1->getFieldOnMeshAtLevel(ON_NODES, io1[i].first,io1[i].second,pointeUM0);
253 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd2 =
254 fieldnodedoubleTS2->getFieldOnMeshAtLevel(ON_NODES, io2[i].first,io2[i].second,um0);
255 CPPUNIT_ASSERT( fnd1->getArray()->isEqual( *fnd2->getArray(), 1e-12 ));
257 // fieldcelldoublevector
258 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldcelldoublevectorTS1 =
259 pointeMed->getFields()->getFieldWithName("fieldcelldoublevector");
260 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldcelldoublevectorTS2 =
261 d2->getFields()->getFieldWithName("fieldcelldoublevector");
262 CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getInfo().size(), fieldcelldoublevectorTS2->getInfo().size());
263 for ( size_t i = 0; i < fieldcelldoublevectorTS1->getInfo().size(); ++i )
264 CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getInfo()[i], fieldcelldoublevectorTS2->getInfo()[i]);
265 CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getNumberOfTS(), fieldcelldoublevectorTS2->getNumberOfTS());
266 io1 = fieldcelldoublevectorTS1->getIterations();
267 io2 = fieldcelldoublevectorTS2->getIterations();
268 for ( int i =0; i < fieldcelldoublevectorTS1->getNumberOfTS(); ++i )
270 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd1 =
271 fieldcelldoublevectorTS1->getFieldOnMeshAtLevel(ON_CELLS, io1[i].first,io1[i].second,pointeUM0);
272 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd2 =
273 fieldcelldoublevectorTS2->getFieldOnMeshAtLevel(ON_CELLS, io2[i].first,io2[i].second,um0);
274 CPPUNIT_ASSERT_DOUBLES_EQUAL( fnd1->accumulate(0), fnd2->accumulate(0), 1e-12 );
275 CPPUNIT_ASSERT_DOUBLES_EQUAL( fnd1->accumulate(1), fnd2->accumulate(1), 1e-12 );
276 CPPUNIT_ASSERT_DOUBLES_EQUAL( fnd1->accumulate(2), fnd2->accumulate(2), 1e-12 );
278 // "Field on 2 faces"
279 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldOnFaces =
280 d2->getFields()->getFieldWithName(f1->getName());
281 io1 = fieldOnFaces->getIterations();
282 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fof =
283 fieldOnFaces->getFieldOnMeshAtLevel(f1->getTypeOfField(),io1[0].first,io1[0].second,um1);
284 CPPUNIT_ASSERT( d->isEqual( *fof->getArray(), 1e-12 ));
287 void SauvLoaderTest::tearDown()
289 const int nbFilesToRemove = 3;
290 const char* fileToRemove[nbFilesToRemove] = { "allPillesTest.med", "pointe.sauv", "mesh_with_void_family.sauv" };
291 for ( int i = 0; i < nbFilesToRemove; ++i )
294 if (GetFileAttributes(fileToRemove[i]) != INVALID_FILE_ATTRIBUTES)
296 if (access(fileToRemove[i], F_OK) == 0)
298 remove(fileToRemove[i]);
302 std::string SauvLoaderTest::getResourceFile( const std::string& filename )
304 std::string resourceFile = "";
306 if ( getenv("top_srcdir") ) {
307 // we are in 'make check' step
308 resourceFile = getenv("top_srcdir");
309 resourceFile += "/resources/";
311 else if ( getenv("MED_ROOT_DIR") ) {
312 // use MED_ROOT_DIR env.var
313 resourceFile = getenv("MED_ROOT_DIR");
314 resourceFile += "/share/salome/resources/med/";
316 resourceFile += filename;
318 std::string fixedpath = resourceFile;
319 for ( int i=0; i < fixedpath.length(); ++i )
320 if (fixedpath[i] == '/')