Back to index

salome-med  6.5.0
SauvLoaderTest.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "SauvLoaderTest.hxx"
00021 
00022 #include "SauvReader.hxx"
00023 #include "SauvWriter.hxx"
00024 #include "MEDFileData.hxx"
00025 #include "MEDCouplingFieldDouble.hxx"
00026 #include "MEDCouplingMemArray.hxx"
00027 
00028 #ifdef WNT
00029 #include <windows.h>
00030 #endif
00031 
00032 #include <vector>
00033 #include <string>
00034 
00035 using namespace ParaMEDMEM;
00036 
00037 void SauvLoaderTest::testSauv2Med()
00038 {
00039   // read a file containing all types of readable piles
00040   std::string file = getResourceFile("allPillesTest.sauv");
00041   MEDCouplingAutoRefCountObjectPtr<SauvReader> sr=SauvReader::New(file.c_str());
00042   MEDCouplingAutoRefCountObjectPtr<MEDFileData> d2=sr->loadInMEDFileDS();
00043   // write MED
00044   d2->write("allPillesTest.med",0);
00045   // check 
00046   CPPUNIT_ASSERT_EQUAL(1,d2->getNumberOfMeshes());
00047   CPPUNIT_ASSERT_EQUAL(8+97,d2->getNumberOfFields());
00048   MEDFileMesh * m = d2->getMeshes()->getMeshAtPos(0);
00049   CPPUNIT_ASSERT_EQUAL(17,int(m->getGroupsNames().size()));
00050 }
00051 
00052 void SauvLoaderTest::testMed2Sauv()
00053 {
00054   // read pointe.med
00055   std::string file = getResourceFile("pointe.med");
00056   MEDCouplingAutoRefCountObjectPtr<MEDFileData> pointeMed=MEDFileData::New(file.c_str());
00057 
00058   // add 3 faces to pointeMed
00059   MEDFileUMesh* pointeMedMesh = static_cast<MEDFileUMesh*>(pointeMed->getMeshes()->getMeshAtPos(0));
00060   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> pointeM1D = MEDCouplingUMesh::New();
00061   DataArrayDouble     *coords = pointeMedMesh->getCoords();
00062   pointeM1D->setCoords( coords );
00063   pointeM1D->setMeshDimension( 2 );
00064   pointeM1D->allocateCells( 3 );
00065   int conn[]=
00066     {
00067       0,1,2, 0,1,3, 10,11,12,13
00068     };
00069   pointeM1D->insertNextCell( INTERP_KERNEL::NORM_TRI3, 3, conn);
00070   pointeM1D->insertNextCell( INTERP_KERNEL::NORM_TRI3, 3, conn+3);
00071   pointeM1D->insertNextCell( INTERP_KERNEL::NORM_QUAD4, 4, conn+6);
00072   pointeM1D->finishInsertingCells();
00073   pointeMedMesh->setMeshAtLevel( -1, pointeM1D );
00074   pointeMed->getMeshes()->setMeshAtPos( 0, pointeMedMesh );
00075 
00076   // add a field on 2 faces to pointeMed
00077   MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> ff1=MEDFileFieldMultiTS::New();
00078   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f1=MEDCouplingFieldDouble::New(ON_GAUSS_NE,ONE_TIME);
00079   f1->setMesh( pointeM1D );
00080   f1->setName("Field on 2 faces");
00081   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> d=DataArrayDouble::New();
00082   d->alloc(3+4,2);
00083   d->setInfoOnComponent(0,"sigX [MPa]");
00084   d->setInfoOnComponent(1,"sigY [GPa]");
00085   double vals[2*(3+4)] =
00086     {
00087       311,312,321,322,331,332,411,412,421,422,431,432,441,442
00088     };
00089   std::copy(vals,vals+d->getNbOfElems(),d->getPointer());
00090   f1->setArray(d);
00091   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=DataArrayInt::New();
00092   int ids[] =
00093     {
00094       0,2
00095     };
00096   da->alloc(2,1);
00097   std::copy(ids,ids+da->getNbOfElems(),da->getPointer());
00098   da->setName("sup2");
00099   ff1->appendFieldProfile(f1,pointeMedMesh,-1,da);
00100   pointeMed->getFields()->pushField( ff1 );
00101 
00102   // remove "fieldnodeint"
00103   MEDFileFields* pointeFields = pointeMed->getFields();
00104   for ( int i = 0; i < pointeFields->getNumberOfFields(); ++i )
00105     {
00106       MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> ts = pointeFields->getFieldAtPos(i);
00107       if ( std::string("fieldnodeint") == ts->getName())
00108         {
00109           pointeFields->destroyFieldAtPos( i );
00110           break;
00111         }
00112     }
00113   // write pointeMed to SAUV
00114   const char* sauvFile = "pointe.sauv";
00115   MEDCouplingAutoRefCountObjectPtr<SauvWriter> sw=SauvWriter::New();
00116   sw->setMEDFileDS(pointeMed);
00117   sw->write(sauvFile);
00118 
00119   // read SAUV and check
00120   MEDCouplingAutoRefCountObjectPtr<SauvReader> sr=SauvReader::New(sauvFile);
00121   MEDCouplingAutoRefCountObjectPtr<MEDFileData> d2=sr->loadInMEDFileDS();
00122   CPPUNIT_ASSERT_EQUAL(1,d2->getNumberOfMeshes());
00123   CPPUNIT_ASSERT_EQUAL(4,d2->getNumberOfFields());
00124   MEDFileUMesh * m = static_cast<MEDFileUMesh*>( d2->getMeshes()->getMeshAtPos(0) );
00125   CPPUNIT_ASSERT_EQUAL(std::string("maa1"),std::string(m->getName() ));
00126   CPPUNIT_ASSERT_EQUAL(3,m->getMeshDimension());
00127   std::vector<std::string > groups = m->getGroupsNames();
00128   CPPUNIT_ASSERT_EQUAL(5,(int)groups.size());
00129   CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe1") != groups.end() );
00130   CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe2") != groups.end() );
00131   CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe3") != groups.end() );
00132   CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe4") != groups.end() );
00133   CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe5") != groups.end() );
00134   CPPUNIT_ASSERT_EQUAL(16,m->getSizeAtLevel(0));
00135   MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> um0 = m->getGenMeshAtLevel(0);
00136   CPPUNIT_ASSERT_EQUAL(12, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_TETRA4 ));
00137   CPPUNIT_ASSERT_EQUAL(2,  um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_PYRA5 ));
00138   CPPUNIT_ASSERT_EQUAL(2,  um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_HEXA8 ));
00139   MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> um1 = m->getGenMeshAtLevel(-1);
00140   CPPUNIT_ASSERT_EQUAL(2, um1->getNumberOfCellsWithType( INTERP_KERNEL::NORM_TRI3 ));
00141   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> pointeUM0 =
00142     static_cast<MEDCouplingUMesh*>( pointeMedMesh->getGenMeshAtLevel(0));
00143   DataArrayDouble *coo = m->getCoords();
00144   DataArrayDouble *pointeCoo = pointeMedMesh->getCoords();
00145   CPPUNIT_ASSERT(coo->isEqualWithoutConsideringStr(*pointeCoo,1e-12));
00146   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> vol = um0->getMeasureField(0);
00147   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> pointeVol = pointeUM0->getMeasureField(0);
00148   CPPUNIT_ASSERT_DOUBLES_EQUAL( vol->accumulate(0), pointeVol->accumulate(0),1e-12);
00149   // check fields
00150   // fieldnodedouble
00151   MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldnodedoubleTS1 =
00152     pointeMed->getFields()->getFieldWithName("fieldnodedouble");
00153   MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldnodedoubleTS2 =
00154     d2->getFields()->getFieldWithName("fieldnodedouble");
00155   CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getInfo().size(), fieldnodedoubleTS2->getInfo().size());
00156   for ( size_t i = 0; i < fieldnodedoubleTS1->getInfo().size(); ++i )
00157     CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getInfo()[i], fieldnodedoubleTS2->getInfo()[i]);
00158   CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getNumberOfTS(), fieldnodedoubleTS2->getNumberOfTS());
00159   std::vector< std::pair<int,int> > io1 = fieldnodedoubleTS1->getIterations();
00160   std::vector< std::pair<int,int> > io2 = fieldnodedoubleTS2->getIterations();
00161   for ( int i =0; i < fieldnodedoubleTS1->getNumberOfTS(); ++i )
00162     {
00163       MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd1 =
00164         fieldnodedoubleTS1->getFieldOnMeshAtLevel(ON_NODES, io1[i].first,io1[i].second,pointeUM0);
00165       MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd2 =
00166         fieldnodedoubleTS2->getFieldOnMeshAtLevel(ON_NODES, io2[i].first,io2[i].second,um0);
00167       CPPUNIT_ASSERT( fnd1->getArray()->isEqual( *fnd2->getArray(), 1e-12 ));
00168     }
00169   // fieldcelldoublevector
00170   MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldcelldoublevectorTS1 =
00171     pointeMed->getFields()->getFieldWithName("fieldcelldoublevector");
00172   MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldcelldoublevectorTS2 =
00173     d2->getFields()->getFieldWithName("fieldcelldoublevector");
00174   CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getInfo().size(), fieldcelldoublevectorTS2->getInfo().size());
00175   for ( size_t i = 0; i < fieldcelldoublevectorTS1->getInfo().size(); ++i )
00176     CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getInfo()[i], fieldcelldoublevectorTS2->getInfo()[i]);
00177   CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getNumberOfTS(), fieldcelldoublevectorTS2->getNumberOfTS());
00178   io1 = fieldcelldoublevectorTS1->getIterations();
00179   io2 = fieldcelldoublevectorTS2->getIterations();
00180   for ( int i =0; i < fieldcelldoublevectorTS1->getNumberOfTS(); ++i )
00181     {
00182       MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd1 =
00183         fieldcelldoublevectorTS1->getFieldOnMeshAtLevel(ON_CELLS, io1[i].first,io1[i].second,pointeUM0);
00184       MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fnd2 =
00185         fieldcelldoublevectorTS2->getFieldOnMeshAtLevel(ON_CELLS, io2[i].first,io2[i].second,um0);
00186       CPPUNIT_ASSERT_DOUBLES_EQUAL( fnd1->accumulate(0), fnd2->accumulate(0), 1e-12 );
00187       CPPUNIT_ASSERT_DOUBLES_EQUAL( fnd1->accumulate(1), fnd2->accumulate(1), 1e-12 );
00188       CPPUNIT_ASSERT_DOUBLES_EQUAL( fnd1->accumulate(2), fnd2->accumulate(2), 1e-12 );
00189     }
00190   // "Field on 2 faces"
00191   MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fieldOnFaces =
00192     d2->getFields()->getFieldWithName(f1->getName());
00193   io1 = fieldOnFaces->getIterations();
00194   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> fof =
00195     fieldOnFaces->getFieldOnMeshAtLevel(f1->getTypeOfField(),io1[0].first,io1[0].second,um1);
00196   CPPUNIT_ASSERT( d->isEqual( *fof->getArray(), 1e-12 ));
00197 }
00198 
00199 void SauvLoaderTest::tearDown()
00200 {
00201   const int nbFilesToRemove = 2;
00202   const char* fileToRemove[nbFilesToRemove] = { "allPillesTest.med", "pointe.sauv" };
00203   for ( int i = 0; i < nbFilesToRemove; ++i )
00204     {
00205 #ifdef WNT
00206       if (GetFileAttributes(fileToRemove[i]) != INVALID_FILE_ATTRIBUTES)
00207 #else
00208         if (access(fileToRemove[i], F_OK) == 0)
00209 #endif
00210       remove(fileToRemove[i]);
00211   }
00212 }
00213 
00214 std::string SauvLoaderTest::getResourceFile( const std::string& filename )
00215 {
00216   std::string resourceFile = "";
00217 
00218   if ( getenv("top_srcdir") ) {
00219     // we are in 'make check' step
00220     resourceFile = getenv("top_srcdir");
00221     resourceFile += "/resources/";
00222   }
00223   else if ( getenv("MED_ROOT_DIR") ) {
00224     // use MED_ROOT_DIR env.var
00225     resourceFile = getenv("MED_ROOT_DIR");
00226     resourceFile += "/share/salome/resources/med/";
00227   }
00228   resourceFile += filename;
00229 #ifdef WNT
00230   std::string fixedpath = resourceFile;
00231   for ( int i=0; i < fixedpath.length(); ++i )
00232     if (fixedpath[i] == '/')
00233       fixedpath[i] = '\\';
00234   return fixedpath;
00235 #endif
00236   return resourceFile;
00237 }