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 "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::testMed2Sauv()
57 std::string file = getResourceFile("pointe.med");
58 MEDCouplingAutoRefCountObjectPtr<MEDFileData> pointeMed=MEDFileData::New(file.c_str());
60 // add 3 faces to pointeMed
61 MEDFileUMesh* pointeMedMesh = static_cast<MEDFileUMesh*>(pointeMed->getMeshes()->getMeshAtPos(0));
62 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> pointeM1D = MEDCouplingUMesh::New();
63 DataArrayDouble *coords = pointeMedMesh->getCoords();
64 pointeM1D->setCoords( coords );
65 pointeM1D->setMeshDimension( 2 );
66 pointeM1D->allocateCells( 3 );
69 0,1,2, 0,1,3, 10,11,12,13
71 pointeM1D->insertNextCell( INTERP_KERNEL::NORM_TRI3, 3, conn);
72 pointeM1D->insertNextCell( INTERP_KERNEL::NORM_TRI3, 3, conn+3);
73 pointeM1D->insertNextCell( INTERP_KERNEL::NORM_QUAD4, 4, conn+6);
74 pointeM1D->finishInsertingCells();
75 pointeMedMesh->setMeshAtLevel( -1, pointeM1D );
76 pointeMed->getMeshes()->setMeshAtPos( 0, pointeMedMesh );
78 // add a field on 2 faces to pointeMed
79 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> ff1=MEDFileFieldMultiTS::New();
80 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f1=MEDCouplingFieldDouble::New(ON_GAUSS_NE,ONE_TIME);
81 f1->setMesh( pointeM1D );
82 f1->setName("Field on 2 faces");
83 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> d=DataArrayDouble::New();
85 d->setInfoOnComponent(0,"sigX [MPa]");
86 d->setInfoOnComponent(1,"sigY [GPa]");
87 double vals[2*(3+4)] =
89 311,312,321,322,331,332,411,412,421,422,431,432,441,442
91 std::copy(vals,vals+d->getNbOfElems(),d->getPointer());
93 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=DataArrayInt::New();
99 std::copy(ids,ids+da->getNbOfElems(),da->getPointer());
101 ff1->appendFieldProfile(f1,pointeMedMesh,-1,da);
102 pointeMed->getFields()->pushField( ff1 );
104 // remove "fieldnodeint"
105 MEDFileFields* pointeFields = pointeMed->getFields();
106 for ( int i = 0; i < pointeFields->getNumberOfFields(); ++i )
108 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> ts = pointeFields->getFieldAtPos(i);
109 if ( std::string("fieldnodeint") == ts->getName())
111 pointeFields->destroyFieldAtPos( i );
115 // write pointeMed to SAUV
116 const char* sauvFile = "pointe.sauv";
117 MEDCouplingAutoRefCountObjectPtr<SauvWriter> sw=SauvWriter::New();
118 sw->setMEDFileDS(pointeMed);
121 // read SAUV and check
122 MEDCouplingAutoRefCountObjectPtr<SauvReader> sr=SauvReader::New(sauvFile);
123 MEDCouplingAutoRefCountObjectPtr<MEDFileData> d2=sr->loadInMEDFileDS();
124 CPPUNIT_ASSERT_EQUAL(1,d2->getNumberOfMeshes());
125 CPPUNIT_ASSERT_EQUAL(4,d2->getNumberOfFields());
126 MEDFileUMesh * m = static_cast<MEDFileUMesh*>( d2->getMeshes()->getMeshAtPos(0) );
127 CPPUNIT_ASSERT_EQUAL(std::string("maa1"),std::string(m->getName() ));
128 CPPUNIT_ASSERT_EQUAL(3,m->getMeshDimension());
129 std::vector<std::string > groups = m->getGroupsNames();
130 CPPUNIT_ASSERT_EQUAL(6,(int)groups.size());
131 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe1") != groups.end() );
132 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe2") != groups.end() );
133 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe3") != groups.end() );
134 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe4") != groups.end() );
135 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe5") != groups.end() );
136 CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"maa1") != groups.end() );
137 CPPUNIT_ASSERT_EQUAL(16,m->getSizeAtLevel(0));
138 MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> um0 = m->getGenMeshAtLevel(0);
139 CPPUNIT_ASSERT_EQUAL(12, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_TETRA4 ));
140 CPPUNIT_ASSERT_EQUAL(2, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_PYRA5 ));
141 CPPUNIT_ASSERT_EQUAL(2, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_HEXA8 ));
142 MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> um1 = m->getGenMeshAtLevel(-1);
143 CPPUNIT_ASSERT_EQUAL(2, um1->getNumberOfCellsWithType( INTERP_KERNEL::NORM_TRI3 ));
144 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> pointeUM0 =
145 static_cast<MEDCouplingUMesh*>( pointeMedMesh->getGenMeshAtLevel(0));
146 DataArrayDouble *coo = m->getCoords();
147 DataArrayDouble *pointeCoo = pointeMedMesh->getCoords();
148 CPPUNIT_ASSERT(coo->isEqualWithoutConsideringStr(*pointeCoo,1e-12));
149 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> vol = um0->getMeasureField(0);
150 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> pointeVol = pointeUM0->getMeasureField(0);
151 CPPUNIT_ASSERT_DOUBLES_EQUAL( vol->accumulate(0), pointeVol->accumulate(0),1e-12);
154 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldnodedoubleTS1 =
155 pointeMed->getFields()->getFieldWithName("fieldnodedouble");
156 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldnodedoubleTS2 =
157 d2->getFields()->getFieldWithName("fieldnodedouble");
158 CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getInfo().size(), fieldnodedoubleTS2->getInfo().size());
159 for ( size_t i = 0; i < fieldnodedoubleTS1->getInfo().size(); ++i )
160 CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getInfo()[i], fieldnodedoubleTS2->getInfo()[i]);
161 CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getNumberOfTS(), fieldnodedoubleTS2->getNumberOfTS());
162 std::vector< std::pair<int,int> > io1 = fieldnodedoubleTS1->getIterations();
163 std::vector< std::pair<int,int> > io2 = fieldnodedoubleTS2->getIterations();
164 for ( int i =0; i < fieldnodedoubleTS1->getNumberOfTS(); ++i )
166 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd1 =
167 fieldnodedoubleTS1->getFieldOnMeshAtLevel(ON_NODES, io1[i].first,io1[i].second,pointeUM0);
168 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd2 =
169 fieldnodedoubleTS2->getFieldOnMeshAtLevel(ON_NODES, io2[i].first,io2[i].second,um0);
170 CPPUNIT_ASSERT( fnd1->getArray()->isEqual( *fnd2->getArray(), 1e-12 ));
172 // fieldcelldoublevector
173 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldcelldoublevectorTS1 =
174 pointeMed->getFields()->getFieldWithName("fieldcelldoublevector");
175 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldcelldoublevectorTS2 =
176 d2->getFields()->getFieldWithName("fieldcelldoublevector");
177 CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getInfo().size(), fieldcelldoublevectorTS2->getInfo().size());
178 for ( size_t i = 0; i < fieldcelldoublevectorTS1->getInfo().size(); ++i )
179 CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getInfo()[i], fieldcelldoublevectorTS2->getInfo()[i]);
180 CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getNumberOfTS(), fieldcelldoublevectorTS2->getNumberOfTS());
181 io1 = fieldcelldoublevectorTS1->getIterations();
182 io2 = fieldcelldoublevectorTS2->getIterations();
183 for ( int i =0; i < fieldcelldoublevectorTS1->getNumberOfTS(); ++i )
185 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd1 =
186 fieldcelldoublevectorTS1->getFieldOnMeshAtLevel(ON_CELLS, io1[i].first,io1[i].second,pointeUM0);
187 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd2 =
188 fieldcelldoublevectorTS2->getFieldOnMeshAtLevel(ON_CELLS, io2[i].first,io2[i].second,um0);
189 CPPUNIT_ASSERT_DOUBLES_EQUAL( fnd1->accumulate(0), fnd2->accumulate(0), 1e-12 );
190 CPPUNIT_ASSERT_DOUBLES_EQUAL( fnd1->accumulate(1), fnd2->accumulate(1), 1e-12 );
191 CPPUNIT_ASSERT_DOUBLES_EQUAL( fnd1->accumulate(2), fnd2->accumulate(2), 1e-12 );
193 // "Field on 2 faces"
194 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldOnFaces =
195 d2->getFields()->getFieldWithName(f1->getName());
196 io1 = fieldOnFaces->getIterations();
197 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fof =
198 fieldOnFaces->getFieldOnMeshAtLevel(f1->getTypeOfField(),io1[0].first,io1[0].second,um1);
199 CPPUNIT_ASSERT( d->isEqual( *fof->getArray(), 1e-12 ));
202 void SauvLoaderTest::tearDown()
204 const int nbFilesToRemove = 2;
205 const char* fileToRemove[nbFilesToRemove] = { "allPillesTest.med", "pointe.sauv" };
206 for ( int i = 0; i < nbFilesToRemove; ++i )
209 if (GetFileAttributes(fileToRemove[i]) != INVALID_FILE_ATTRIBUTES)
211 if (access(fileToRemove[i], F_OK) == 0)
213 remove(fileToRemove[i]);
217 std::string SauvLoaderTest::getResourceFile( const std::string& filename )
219 std::string resourceFile = "";
221 if ( getenv("top_srcdir") ) {
222 // we are in 'make check' step
223 resourceFile = getenv("top_srcdir");
224 resourceFile += "/resources/";
226 else if ( getenv("MED_ROOT_DIR") ) {
227 // use MED_ROOT_DIR env.var
228 resourceFile = getenv("MED_ROOT_DIR");
229 resourceFile += "/share/salome/resources/med/";
231 resourceFile += filename;
233 std::string fixedpath = resourceFile;
234 for ( int i=0; i < fixedpath.length(); ++i )
235 if (fixedpath[i] == '/')