Salome HOME
249f192067c66d59ff7d77ca18579bf0d4ac957f
[modules/med.git] / src / MEDLoader / Test / SauvLoaderTest.cxx
1 // Copyright (C) 2007-2012  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.
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
20 #include "SauvLoaderTest.hxx"
21
22 #include "SauvReader.hxx"
23 #include "SauvWriter.hxx"
24 #include "MEDFileData.hxx"
25 #include "MEDCouplingFieldDouble.hxx"
26 #include "MEDCouplingMemArray.hxx"
27
28 #ifdef WNT
29 # include <windows.h>
30 #else
31 # include <unistd.h>
32 #endif
33
34 #include <vector>
35 #include <string>
36
37 using namespace ParaMEDMEM;
38
39 void SauvLoaderTest::testSauv2Med()
40 {
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();
45   // write MED
46   d2->write("allPillesTest.med",0);
47   // check 
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()));
52 }
53
54 void SauvLoaderTest::testMed2Sauv()
55 {
56   // read pointe.med
57   std::string file = getResourceFile("pointe.med");
58   MEDCouplingAutoRefCountObjectPtr<MEDFileData> pointeMed=MEDFileData::New(file.c_str());
59
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 );
67   int conn[]=
68     {
69       0,1,2, 0,1,3, 10,11,12,13
70     };
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 );
77
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();
84   d->alloc(3+4,2);
85   d->setInfoOnComponent(0,"sigX [MPa]");
86   d->setInfoOnComponent(1,"sigY [GPa]");
87   double vals[2*(3+4)] =
88     {
89       311,312,321,322,331,332,411,412,421,422,431,432,441,442
90     };
91   std::copy(vals,vals+d->getNbOfElems(),d->getPointer());
92   f1->setArray(d);
93   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=DataArrayInt::New();
94   int ids[] =
95     {
96       0,2
97     };
98   da->alloc(2,1);
99   std::copy(ids,ids+da->getNbOfElems(),da->getPointer());
100   da->setName("sup2");
101   ff1->appendFieldProfile(f1,pointeMedMesh,-1,da);
102   pointeMed->getFields()->pushField( ff1 );
103
104   // remove "fieldnodeint"
105   MEDFileFields* pointeFields = pointeMed->getFields();
106   for ( int i = 0; i < pointeFields->getNumberOfFields(); ++i )
107     {
108       MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> ts = pointeFields->getFieldAtPos(i);
109       if ( std::string("fieldnodeint") == ts->getName())
110         {
111           pointeFields->destroyFieldAtPos( i );
112           break;
113         }
114     }
115   // write pointeMed to SAUV
116   const char* sauvFile = "pointe.sauv";
117   MEDCouplingAutoRefCountObjectPtr<SauvWriter> sw=SauvWriter::New();
118   sw->setMEDFileDS(pointeMed);
119   sw->write(sauvFile);
120
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);
152   // check fields
153   // fieldnodedouble
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 )
165     {
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 ));
171     }
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 )
184     {
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 );
192     }
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 ));
200 }
201
202 void SauvLoaderTest::tearDown()
203 {
204   const int nbFilesToRemove = 2;
205   const char* fileToRemove[nbFilesToRemove] = { "allPillesTest.med", "pointe.sauv" };
206   for ( int i = 0; i < nbFilesToRemove; ++i )
207     {
208 #ifdef WNT
209       if (GetFileAttributes(fileToRemove[i]) != INVALID_FILE_ATTRIBUTES)
210 #else
211         if (access(fileToRemove[i], F_OK) == 0)
212 #endif
213       remove(fileToRemove[i]);
214   }
215 }
216
217 std::string SauvLoaderTest::getResourceFile( const std::string& filename )
218 {
219   std::string resourceFile = "";
220
221   if ( getenv("top_srcdir") ) {
222     // we are in 'make check' step
223     resourceFile = getenv("top_srcdir");
224     resourceFile += "/resources/";
225   }
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/";
230   }
231   resourceFile += filename;
232 #ifdef WNT
233   std::string fixedpath = resourceFile;
234   for ( int i=0; i < fixedpath.length(); ++i )
235     if (fixedpath[i] == '/')
236       fixedpath[i] = '\\';
237   return fixedpath;
238 #endif
239   return resourceFile;
240 }