X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDLoader%2FTest%2FSauvLoaderTest.cxx;h=4fda6a2464aea2596ca99041441f55218a6df43c;hb=8c3dcf5940836637ac7bcb000049e833c5581eb7;hp=249f192067c66d59ff7d77ca18579bf0d4ac957f;hpb=10f37bf6f33a762626d7f1093b2f5450c1688667;p=tools%2Fmedcoupling.git diff --git a/src/MEDLoader/Test/SauvLoaderTest.cxx b/src/MEDLoader/Test/SauvLoaderTest.cxx index 249f19206..4fda6a246 100644 --- a/src/MEDLoader/Test/SauvLoaderTest.cxx +++ b/src/MEDLoader/Test/SauvLoaderTest.cxx @@ -1,9 +1,9 @@ -// Copyright (C) 2007-2012 CEA/DEN, EDF R&D +// Copyright (C) 2007-2015 CEA/DEN, EDF R&D // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either -// version 2.1 of the License. +// version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -24,9 +24,10 @@ #include "MEDFileData.hxx" #include "MEDCouplingFieldDouble.hxx" #include "MEDCouplingMemArray.hxx" +#include "TestInterpKernelUtils.hxx" // getResourceFile() -#ifdef WNT -# include +#ifdef WIN32 +#include #else # include #endif @@ -39,22 +40,136 @@ using namespace ParaMEDMEM; void SauvLoaderTest::testSauv2Med() { // read a file containing all types of readable piles - std::string file = getResourceFile("allPillesTest.sauv"); + std::string file = INTERP_TEST::getResourceFile("allPillesTest.sauv", 3); MEDCouplingAutoRefCountObjectPtr sr=SauvReader::New(file.c_str()); MEDCouplingAutoRefCountObjectPtr d2=sr->loadInMEDFileDS(); // write MED d2->write("allPillesTest.med",0); - // check + // check CPPUNIT_ASSERT_EQUAL(1,d2->getNumberOfMeshes()); CPPUNIT_ASSERT_EQUAL(8+97,d2->getNumberOfFields()); MEDFileMesh * m = d2->getMeshes()->getMeshAtPos(0); CPPUNIT_ASSERT_EQUAL(17,int(m->getGroupsNames().size())); } +void SauvLoaderTest::testMed2SauvOnAMeshWithVoidFamily() +{ + // Create a mesh with 2 quads. + const int spaceDim = 2; + const int nbOfNodes = 6; + double coords[nbOfNodes*spaceDim] = {0,0, 1,0, 1,1, 0,1, 2,0, 2,1}; + int conn[8]={0,1,2,3, 1,4,5,2}; + MEDCouplingAutoRefCountObjectPtr mesh2d=MEDCouplingUMesh::New("Mesh",spaceDim); + mesh2d->allocateCells(2); + mesh2d->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,conn); + mesh2d->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,conn+4); + mesh2d->finishInsertingCells(); + MEDCouplingAutoRefCountObjectPtr myCoords=DataArrayDouble::New(); + myCoords->alloc(nbOfNodes,spaceDim); + std::copy(coords,coords+nbOfNodes*spaceDim,myCoords->getPointer()); + mesh2d->setCoords(myCoords); + + // create a MedFileUMesh + MEDCouplingAutoRefCountObjectPtr m= MEDFileUMesh::New(); + m->setMeshAtLevel(0,mesh2d); + + // Create families and groups + + MEDCouplingAutoRefCountObjectPtr fam = DataArrayInt::New(); + fam->alloc(2,1); + int elemsFams[2] = {-2,-3}; + std::copy(elemsFams,elemsFams+2,fam->getPointer()); + + m->setFamilyFieldArr(0,fam); + + std::map theFamilies; + theFamilies["FAM_-1"]=-1; + theFamilies["FAM_-2"]=-2; + theFamilies["FAM_-3"]=-3; + + std::map > theGroups; + theGroups["Group1"].push_back("FAM_-2"); + theGroups["Group2"].push_back("FAM_-3"); + theGroups["Grouptot"].push_back("FAM_-1"); + theGroups["Grouptot"].push_back("FAM_-2"); + theGroups["Grouptot"].push_back("FAM_-3"); + m->setFamilyInfo(theFamilies); + m->setGroupInfo(theGroups); + + // write to MED for visual check + //const char* medFile = "mesh_with_void_family.med"; + //m->write(medFile, 2); + + // write to SAUV + const char* sauvFile = "mesh_with_void_family.sauv"; + MEDCouplingAutoRefCountObjectPtr medData = MEDFileData::New(); + MEDCouplingAutoRefCountObjectPtr medMeshes = MEDFileMeshes::New(); + MEDCouplingAutoRefCountObjectPtr sw=SauvWriter::New(); + medMeshes->setMeshAtPos(0, m); + medData->setMeshes(medMeshes); + sw->setMEDFileDS(medData); + sw->write(sauvFile); + + // read SAUV and check groups + MEDCouplingAutoRefCountObjectPtr sr=SauvReader::New(sauvFile); + MEDCouplingAutoRefCountObjectPtr d2=sr->loadInMEDFileDS(); + MEDFileUMesh* m2 = static_cast( d2->getMeshes()->getMeshAtPos(0) ); + MEDCouplingAutoRefCountObjectPtr group1 = m2->getGroup(0, "Group1"); + CPPUNIT_ASSERT_EQUAL(1,(int)group1->getNumberOfCells()); + MEDCouplingAutoRefCountObjectPtr group2 = m2->getGroup(0, "Group2"); + CPPUNIT_ASSERT_EQUAL(1,(int)group2->getNumberOfCells()); + MEDCouplingAutoRefCountObjectPtr grptot = m2->getGroup(0, "Grouptot"); + CPPUNIT_ASSERT_EQUAL(2,(int)grptot->getNumberOfCells()); +} + +void SauvLoaderTest::testSauv2MedOnA3SubsField() +{ + // read SAUV + std::string sauvFile = INTERP_TEST::getResourceFile("portico_3subs.sauv", 3); + MEDCouplingAutoRefCountObjectPtr sr=SauvReader::New(sauvFile.c_str()); + MEDCouplingAutoRefCountObjectPtr d2=sr->loadInMEDFileDS(); + // check mesh + MEDFileUMesh* m2 = static_cast(d2->getMeshes()->getMeshAtPos(0)); + MEDCouplingAutoRefCountObjectPtr mesh1d = m2->getMeshAtLevel(0); + MEDCouplingAutoRefCountObjectPtr length1dField = mesh1d->getMeasureField(0); + std::cout << "Length of 1d elements: " << length1dField->accumulate(0) << std::endl; + CPPUNIT_ASSERT_DOUBLES_EQUAL(3, length1dField->accumulate(0), 1e-12); + // check field + MEDCouplingAutoRefCountObjectPtr field = + dynamic_cast(d2->getFields()->getFieldWithName("CHAM1D")); + std::cout << "Number of components in field: " << field->getInfo().size() << std::endl; + CPPUNIT_ASSERT_EQUAL(6,(int)field->getInfo().size()); + std::vector< std::pair > timesteps = field->getIterations(); + + MEDCouplingAutoRefCountObjectPtr field1d = + field->getFieldOnMeshAtLevel(ON_GAUSS_NE, timesteps[0].first, timesteps[0].second, 0, m2); + + // Check first component of the field + // 2 gauss points per element => 12 values + double values[12] = { + -7.687500000000e-03, + -7.687500000000e-03, + -4.562500000000e-03, + -4.562500000000e-03, + -8.208333333333e-03, + -8.208333333333e-03, + -6.125000000000e-03, + -6.125000000000e-03, + -4.041666666666e-03, + -4.041666666666e-03, + -6.111413346910e-07, + -6.111413346910e-07}; + + for (int i=0; i < field1d->getNumberOfTuples(); i++) + { + CPPUNIT_ASSERT_DOUBLES_EQUAL( values[i], field1d->getIJ(i, 0), 1e-12 ); + } +} + void SauvLoaderTest::testMed2Sauv() { // read pointe.med - std::string file = getResourceFile("pointe.med"); + std::string file = INTERP_TEST::getResourceFile("pointe.med", 3); MEDCouplingAutoRefCountObjectPtr pointeMed=MEDFileData::New(file.c_str()); // add 3 faces to pointeMed @@ -105,7 +220,7 @@ void SauvLoaderTest::testMed2Sauv() MEDFileFields* pointeFields = pointeMed->getFields(); for ( int i = 0; i < pointeFields->getNumberOfFields(); ++i ) { - MEDCouplingAutoRefCountObjectPtr ts = pointeFields->getFieldAtPos(i); + MEDCouplingAutoRefCountObjectPtr ts = pointeFields->getFieldAtPos(i); if ( std::string("fieldnodeint") == ts->getName()) { pointeFields->destroyFieldAtPos( i ); @@ -135,14 +250,14 @@ void SauvLoaderTest::testMed2Sauv() CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"groupe5") != groups.end() ); CPPUNIT_ASSERT( std::find(groups.begin(),groups.end(),"maa1") != groups.end() ); CPPUNIT_ASSERT_EQUAL(16,m->getSizeAtLevel(0)); - MEDCouplingAutoRefCountObjectPtr um0 = m->getGenMeshAtLevel(0); + MEDCouplingAutoRefCountObjectPtr um0 = m->getMeshAtLevel(0); CPPUNIT_ASSERT_EQUAL(12, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_TETRA4 )); CPPUNIT_ASSERT_EQUAL(2, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_PYRA5 )); CPPUNIT_ASSERT_EQUAL(2, um0->getNumberOfCellsWithType( INTERP_KERNEL::NORM_HEXA8 )); - MEDCouplingAutoRefCountObjectPtr um1 = m->getGenMeshAtLevel(-1); + MEDCouplingAutoRefCountObjectPtr um1 = m->getMeshAtLevel(-1); CPPUNIT_ASSERT_EQUAL(2, um1->getNumberOfCellsWithType( INTERP_KERNEL::NORM_TRI3 )); MEDCouplingAutoRefCountObjectPtr pointeUM0 = - static_cast( pointeMedMesh->getGenMeshAtLevel(0)); + static_cast( pointeMedMesh->getMeshAtLevel(0)); DataArrayDouble *coo = m->getCoords(); DataArrayDouble *pointeCoo = pointeMedMesh->getCoords(); CPPUNIT_ASSERT(coo->isEqualWithoutConsideringStr(*pointeCoo,1e-12)); @@ -152,9 +267,9 @@ void SauvLoaderTest::testMed2Sauv() // check fields // fieldnodedouble MEDCouplingAutoRefCountObjectPtr fieldnodedoubleTS1 = - pointeMed->getFields()->getFieldWithName("fieldnodedouble"); + dynamic_cast(pointeMed->getFields()->getFieldWithName("fieldnodedouble")); MEDCouplingAutoRefCountObjectPtr fieldnodedoubleTS2 = - d2->getFields()->getFieldWithName("fieldnodedouble"); + dynamic_cast(d2->getFields()->getFieldWithName("fieldnodedouble")); CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getInfo().size(), fieldnodedoubleTS2->getInfo().size()); for ( size_t i = 0; i < fieldnodedoubleTS1->getInfo().size(); ++i ) CPPUNIT_ASSERT_EQUAL( fieldnodedoubleTS1->getInfo()[i], fieldnodedoubleTS2->getInfo()[i]); @@ -171,9 +286,9 @@ void SauvLoaderTest::testMed2Sauv() } // fieldcelldoublevector MEDCouplingAutoRefCountObjectPtr fieldcelldoublevectorTS1 = - pointeMed->getFields()->getFieldWithName("fieldcelldoublevector"); + dynamic_cast(pointeMed->getFields()->getFieldWithName("fieldcelldoublevector")); MEDCouplingAutoRefCountObjectPtr fieldcelldoublevectorTS2 = - d2->getFields()->getFieldWithName("fieldcelldoublevector"); + dynamic_cast(d2->getFields()->getFieldWithName("fieldcelldoublevector")); CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getInfo().size(), fieldcelldoublevectorTS2->getInfo().size()); for ( size_t i = 0; i < fieldcelldoublevectorTS1->getInfo().size(); ++i ) CPPUNIT_ASSERT_EQUAL( fieldcelldoublevectorTS1->getInfo()[i], fieldcelldoublevectorTS2->getInfo()[i]); @@ -192,7 +307,7 @@ void SauvLoaderTest::testMed2Sauv() } // "Field on 2 faces" MEDCouplingAutoRefCountObjectPtr fieldOnFaces = - d2->getFields()->getFieldWithName(f1->getName()); + dynamic_cast(d2->getFields()->getFieldWithName(f1->getName().c_str())); io1 = fieldOnFaces->getIterations(); MEDCouplingAutoRefCountObjectPtr fof = fieldOnFaces->getFieldOnMeshAtLevel(f1->getTypeOfField(),io1[0].first,io1[0].second,um1); @@ -201,11 +316,11 @@ void SauvLoaderTest::testMed2Sauv() void SauvLoaderTest::tearDown() { - const int nbFilesToRemove = 2; - const char* fileToRemove[nbFilesToRemove] = { "allPillesTest.med", "pointe.sauv" }; + const int nbFilesToRemove = 3; + const char* fileToRemove[nbFilesToRemove] = { "allPillesTest.med", "pointe.sauv", "mesh_with_void_family.sauv" }; for ( int i = 0; i < nbFilesToRemove; ++i ) { -#ifdef WNT +#ifdef WIN32 if (GetFileAttributes(fileToRemove[i]) != INVALID_FILE_ATTRIBUTES) #else if (access(fileToRemove[i], F_OK) == 0) @@ -213,28 +328,3 @@ void SauvLoaderTest::tearDown() remove(fileToRemove[i]); } } - -std::string SauvLoaderTest::getResourceFile( const std::string& filename ) -{ - std::string resourceFile = ""; - - if ( getenv("top_srcdir") ) { - // we are in 'make check' step - resourceFile = getenv("top_srcdir"); - resourceFile += "/resources/"; - } - else if ( getenv("MED_ROOT_DIR") ) { - // use MED_ROOT_DIR env.var - resourceFile = getenv("MED_ROOT_DIR"); - resourceFile += "/share/salome/resources/med/"; - } - resourceFile += filename; -#ifdef WNT - std::string fixedpath = resourceFile; - for ( int i=0; i < fixedpath.length(); ++i ) - if (fixedpath[i] == '/') - fixedpath[i] = '\\'; - return fixedpath; -#endif - return resourceFile; -}