Back to index

salome-med  6.5.0
MEDCalculatorBasicsTest.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 "MEDCalculatorBasicsTest.hxx"
00021 #include "MEDCalculatorBrowserLiteStruct.hxx"
00022 #include "MEDCalculatorDBRangeSelection.hxx"
00023 #include "MEDCalculatorDBField.hxx"
00024 
00025 #include "SPythonParser.hxx"
00026 #include "SPythonInterpreter.hxx"
00027 
00028 #include "MEDLoader.hxx"
00029 
00030 #include "MEDCouplingMemArray.hxx"
00031 #include "MEDCouplingUMesh.hxx"
00032 #include "MEDCouplingFieldDouble.hxx"
00033 
00034 #include <iostream>
00035 
00036 void ParaMEDMEM::MEDCalculatorBasicsTest::testLightStruct1()
00037 {
00038   const char fname[]="/export/home/geay/MEDStdAlone/Salome5/V5_1_main/MED/MED_INSTALL_DEBUG/share/salome/resources/med/TimeStamps_import22.med";
00039   MEDCalculatorBrowserLiteStruct lt(fname);
00040   lt.selectAllFields();
00041   //std::cout << lt.str() << std::endl;
00042 }
00043 
00044 void ParaMEDMEM::MEDCalculatorBasicsTest::testRangeSelection1()
00045 {
00046   MEDCalculatorDBRangeSelection sel1(":");
00047   std::vector<int> v=sel1.getIds(6);
00048   CPPUNIT_ASSERT_EQUAL(6,(int)v.size());
00049   const int expected1[6]={0,1,2,3,4,5};
00050   CPPUNIT_ASSERT(std::equal(expected1,expected1+6,v.begin()));
00051   MEDCalculatorDBRangeSelection sel2(":-1");
00052   v=sel2.getIds(6);
00053   CPPUNIT_ASSERT_EQUAL(5,(int)v.size());
00054   CPPUNIT_ASSERT(std::equal(expected1,expected1+5,v.begin()));
00055   MEDCalculatorDBRangeSelection sel3("2:4");
00056   v=sel3.getIds(6);
00057   CPPUNIT_ASSERT_EQUAL(2,(int)v.size());
00058   CPPUNIT_ASSERT(std::equal(expected1+2,expected1+4,v.begin()));
00059   MEDCalculatorDBRangeSelection sel4(":3");
00060   v=sel4.getIds(6);
00061   CPPUNIT_ASSERT_EQUAL(3,(int)v.size());
00062   CPPUNIT_ASSERT(std::equal(expected1,expected1+3,v.begin()));
00063   //
00064   bool exc=false;
00065   MEDCalculatorDBRangeSelection sel5("2");
00066   try {  sel5="3:1"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
00067   CPPUNIT_ASSERT(exc); exc=false;
00068   try {  sel5="4:5:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
00069   CPPUNIT_ASSERT(exc); exc=false;
00070   try {  sel5="4:5r"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
00071   CPPUNIT_ASSERT(exc); exc=false;
00072   try {  sel5="4:ry5"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
00073   CPPUNIT_ASSERT(exc); exc=false;
00074   try {  sel5="4r:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
00075   CPPUNIT_ASSERT(exc); exc=false;
00076   try {  sel5="r4:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
00077   CPPUNIT_ASSERT(exc); exc=false;
00078   sel5="3:7";
00079   try {  v=sel5.getIds(6); } catch(INTERP_KERNEL::Exception& e) { exc=true; }
00080   CPPUNIT_ASSERT(exc); exc=false;
00081 }
00082 
00083 void ParaMEDMEM::MEDCalculatorBasicsTest::testDBField1()
00084 {
00085   const char fName[]="hfile1.med";
00086   generateAFile1(fName);
00087   MEDCalculatorBrowserLiteStruct lt(fName);
00088   lt.selectAllFields();
00089   MEDCalculatorDBFieldReal *Power=new MEDCalculatorDBFieldReal(lt.getField(0));
00090   MEDCalculatorDBFieldReal *Power2=new MEDCalculatorDBFieldReal(lt.getField(0));
00091   MEDCalculatorDBFieldReal *Power3=(MEDCalculatorDBFieldReal *)((*Power)+(*Power2));
00092   Power3->decrRef();
00093   Power2->decrRef();
00094   Power->decrRef();
00095   //
00096   MEDCalculatorDBRangeSelection t1("2:4"),t2("6:8");
00097   MEDCalculatorDBRangeSelection p;
00098   MEDCalculatorDBRangeSelection c1,c2;
00099   //
00100   Power=new MEDCalculatorDBFieldReal(lt.getField(0));
00101   Power2=(*Power)(t1,p,c1);
00102   Power3=(*Power)(t2,p,c1);
00103   MEDCalculatorDBField *Power4=(*Power2)+(*Power3);
00104   Power4->decrRef();
00105   Power3->decrRef();
00106   Power2->decrRef();
00107   Power->decrRef();
00108 }
00109 
00110 void ParaMEDMEM::MEDCalculatorBasicsTest::generateAFile1(const char *fName)
00111 {
00112   double targetCoords[18]={-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 };
00113   int targetConn[18]={0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4};
00114   MEDCouplingUMesh *m=MEDCouplingUMesh::New();
00115   m->setName("AMesh");
00116   m->setMeshDimension(2);
00117   m->allocateCells(5);
00118   m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn);
00119   m->insertNextCell(INTERP_KERNEL::NORM_TRI3,3,targetConn+4);
00120   m->insertNextCell(INTERP_KERNEL::NORM_TRI3,3,targetConn+7);
00121   m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn+10);
00122   m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn+14);
00123   m->finishInsertingCells();
00124   DataArrayDouble *myCoords=DataArrayDouble::New();
00125   myCoords->alloc(9,2);
00126   myCoords->setInfoOnComponent(0,"x [cm]");
00127   myCoords->setInfoOnComponent(1,"y [cm]");
00128   std::copy(targetCoords,targetCoords+18,myCoords->getPointer());
00129   m->setCoords(myCoords);
00130   myCoords->decrRef();
00131   //
00132   MEDLoader::WriteUMesh(fName,m,true);
00133   static const int nbOfTimeSteps=10;
00134   static const int nbOfComponents=7;
00135   MEDCouplingFieldDouble *f=MEDCouplingFieldDouble::New(ON_CELLS,ONE_TIME);
00136   f->setName("Power");
00137   f->setMesh(m);
00138   DataArrayDouble *da=DataArrayDouble::New();
00139   da->alloc(5,7);
00140   f->setArray(da);
00141   da->setInfoOnComponent(0,"aaa [a]"); da->setInfoOnComponent(1,"bbb [b]"); da->setInfoOnComponent(2,"ccc [c]");
00142   da->setInfoOnComponent(3,"ddd [d]"); da->setInfoOnComponent(4,"eee [e]"); da->setInfoOnComponent(5,"fff [f]");
00143   da->setInfoOnComponent(6,"ggg [g]");
00144   f->checkCoherency();
00145   for(int i=0;i<nbOfTimeSteps;i++)
00146     {
00147       double *pt=da->getPointer();
00148       for(int j=0;j<5;j++)
00149         for(int k=0;k<nbOfComponents;k++,pt++)
00150           *pt=(i+1)*100.+(j+1)*10.+k+1;
00151       f->setTime(i*0.1,i,-i);
00152       MEDLoader::WriteFieldUsingAlreadyWrittenMesh(fName,f);
00153     }
00154   da->decrRef();
00155   f->decrRef();
00156   m->decrRef();
00157 }
00158 
00159 void ParaMEDMEM::MEDCalculatorBasicsTest::testSPython1()
00160 {
00161   std::string s1,s2;
00162   bool b;
00163   s1="12:3";
00164   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00165   CPPUNIT_ASSERT(!b);
00166   CPPUNIT_ASSERT_EQUAL(s2,s1);
00167   s1="12:";
00168   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00169   CPPUNIT_ASSERT(!b);
00170   CPPUNIT_ASSERT_EQUAL(s2,s1);
00171   s1="12";
00172   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00173   CPPUNIT_ASSERT(b);
00174   CPPUNIT_ASSERT_EQUAL(s2,s1);
00175   s1="";
00176   CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00177   s1="          ";
00178   CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00179   s1=":";
00180   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00181   CPPUNIT_ASSERT(!b);
00182   CPPUNIT_ASSERT_EQUAL(s2,s1);
00183   s1=":12";
00184   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00185   CPPUNIT_ASSERT(!b);
00186   CPPUNIT_ASSERT_EQUAL(s2,s1);
00187   s1=":12:";
00188   CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00189   s1=":12   23";
00190   CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00191   // some ' ' char
00192   s1="  12  : 3  ";
00193   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00194   CPPUNIT_ASSERT(!b);
00195   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
00196   s1="  12  : 3";
00197   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00198   CPPUNIT_ASSERT(!b);
00199   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
00200   s1="  12  :3  ";
00201   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00202   CPPUNIT_ASSERT(!b);
00203   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
00204   s1="  12: 3  ";
00205   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00206   CPPUNIT_ASSERT(!b);
00207   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
00208   s1="12  : 3  ";
00209   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
00210   CPPUNIT_ASSERT(!b);
00211   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
00212 }
00213 
00214 void ParaMEDMEM::MEDCalculatorBasicsTest::testSPython2()
00215 {
00216   std::string s1,s2;
00217   s1="  12:3  , : ,4  ";
00218   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isParenthesisMatching(s1,s2));
00219   CPPUNIT_ASSERT_EQUAL(s2,std::string("\"12:3\",\":\",4"));
00220 }
00221 
00222 void ParaMEDMEM::MEDCalculatorBasicsTest::testSPython3()
00223 {
00224   std::string s1,s2;
00225   s1="(  12:3  , : ,4  )";
00226   s2=ParaMEDMEM::SPythonInterpreter::strip(s1);
00227   CPPUNIT_ASSERT_EQUAL(s2,std::string("(12:3,:,4)"));
00228   s1=" (  12:3  , : ,4  )  ";
00229   s2=ParaMEDMEM::SPythonInterpreter::strip(s1);
00230   CPPUNIT_ASSERT_EQUAL(s2,std::string("(12:3,:,4)"));
00231 }