1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDCalculatorBasicsTest.hxx"
22 #include "MEDCalculatorBrowserLiteStruct.hxx"
23 #include "MEDCalculatorDBRangeSelection.hxx"
24 #include "MEDCalculatorDBField.hxx"
26 #include "SPythonParser.hxx"
27 #include "SPythonInterpreter.hxx"
29 #include "MEDLoader.hxx"
31 #include "MEDCouplingMemArray.hxx"
32 #include "MEDCouplingUMesh.hxx"
33 #include "MEDCouplingFieldDouble.hxx"
37 void MEDCoupling::MEDCalculatorBasicsTest::testLightStruct1()
39 const char fname[]="/export/home/geay/MEDStdAlone/Salome5/V5_1_main/MED/MED_INSTALL_DEBUG/share/salome/resources/med/TimeStamps_import22.med";
40 MEDCalculatorBrowserLiteStruct lt(fname);
42 //std::cout << lt.str() << std::endl;
45 void MEDCoupling::MEDCalculatorBasicsTest::testRangeSelection1()
47 MEDCalculatorDBRangeSelection sel1(":");
48 std::vector<int> v=sel1.getIds(6);
49 CPPUNIT_ASSERT_EQUAL(6,(int)v.size());
50 const int expected1[6]={0,1,2,3,4,5};
51 CPPUNIT_ASSERT(std::equal(expected1,expected1+6,v.begin()));
52 MEDCalculatorDBRangeSelection sel2(":-1");
54 CPPUNIT_ASSERT_EQUAL(5,(int)v.size());
55 CPPUNIT_ASSERT(std::equal(expected1,expected1+5,v.begin()));
56 MEDCalculatorDBRangeSelection sel3("2:4");
58 CPPUNIT_ASSERT_EQUAL(2,(int)v.size());
59 CPPUNIT_ASSERT(std::equal(expected1+2,expected1+4,v.begin()));
60 MEDCalculatorDBRangeSelection sel4(":3");
62 CPPUNIT_ASSERT_EQUAL(3,(int)v.size());
63 CPPUNIT_ASSERT(std::equal(expected1,expected1+3,v.begin()));
66 MEDCalculatorDBRangeSelection sel5("2");
67 try { sel5="3:1"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
68 CPPUNIT_ASSERT(exc); exc=false;
69 try { sel5="4:5:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
70 CPPUNIT_ASSERT(exc); exc=false;
71 try { sel5="4:5r"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
72 CPPUNIT_ASSERT(exc); exc=false;
73 try { sel5="4:ry5"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
74 CPPUNIT_ASSERT(exc); exc=false;
75 try { sel5="4r:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
76 CPPUNIT_ASSERT(exc); exc=false;
77 try { sel5="r4:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
78 CPPUNIT_ASSERT(exc); exc=false;
80 try { v=sel5.getIds(6); } catch(INTERP_KERNEL::Exception& e) { exc=true; }
81 CPPUNIT_ASSERT(exc); exc=false;
84 void MEDCoupling::MEDCalculatorBasicsTest::testDBField1()
86 const char fName[]="hfile1.med";
87 generateAFile1(fName);
88 MEDCalculatorBrowserLiteStruct lt(fName);
90 MEDCalculatorDBFieldReal *Power=new MEDCalculatorDBFieldReal(lt.getField(0));
91 MEDCalculatorDBFieldReal *Power2=new MEDCalculatorDBFieldReal(lt.getField(0));
92 MEDCalculatorDBFieldReal *Power3=(MEDCalculatorDBFieldReal *)((*Power)+(*Power2));
97 MEDCalculatorDBRangeSelection t1("2:4"),t2("6:8");
98 MEDCalculatorDBRangeSelection p;
99 MEDCalculatorDBRangeSelection c1,c2;
101 Power=new MEDCalculatorDBFieldReal(lt.getField(0));
102 Power2=(*Power)(t1,p,c1);
103 Power3=(*Power)(t2,p,c1);
104 MEDCalculatorDBField *Power4=(*Power2)+(*Power3);
111 void MEDCoupling::MEDCalculatorBasicsTest::generateAFile1(const char *fName)
113 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 };
114 int targetConn[18]={0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4};
115 MEDCouplingUMesh *m=MEDCouplingUMesh::New();
117 m->setMeshDimension(2);
119 m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn);
120 m->insertNextCell(INTERP_KERNEL::NORM_TRI3,3,targetConn+4);
121 m->insertNextCell(INTERP_KERNEL::NORM_TRI3,3,targetConn+7);
122 m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn+10);
123 m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn+14);
124 m->finishInsertingCells();
125 DataArrayDouble *myCoords=DataArrayDouble::New();
126 myCoords->alloc(9,2);
127 myCoords->setInfoOnComponent(0,"x [cm]");
128 myCoords->setInfoOnComponent(1,"y [cm]");
129 std::copy(targetCoords,targetCoords+18,myCoords->getPointer());
130 m->setCoords(myCoords);
133 WriteUMesh(fName,m,true);
134 static const int nbOfTimeSteps=10;
135 static const int nbOfComponents=7;
136 MEDCouplingFieldDouble *f=MEDCouplingFieldDouble::New(ON_CELLS,ONE_TIME);
139 DataArrayDouble *da=DataArrayDouble::New();
142 da->setInfoOnComponent(0,"aaa [a]"); da->setInfoOnComponent(1,"bbb [b]"); da->setInfoOnComponent(2,"ccc [c]");
143 da->setInfoOnComponent(3,"ddd [d]"); da->setInfoOnComponent(4,"eee [e]"); da->setInfoOnComponent(5,"fff [f]");
144 da->setInfoOnComponent(6,"ggg [g]");
145 f->checkConsistencyLight();
146 for(int i=0;i<nbOfTimeSteps;i++)
148 double *pt=da->getPointer();
150 for(int k=0;k<nbOfComponents;k++,pt++)
151 *pt=(i+1)*100.+(j+1)*10.+k+1;
152 f->setTime(i*0.1,i,-i);
153 WriteFieldUsingAlreadyWrittenMesh(fName,f);
160 void MEDCoupling::MEDCalculatorBasicsTest::testSPython1()
165 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
167 CPPUNIT_ASSERT_EQUAL(s2,s1);
169 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
171 CPPUNIT_ASSERT_EQUAL(s2,s1);
173 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
175 CPPUNIT_ASSERT_EQUAL(s2,s1);
177 CPPUNIT_ASSERT(!MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
179 CPPUNIT_ASSERT(!MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
181 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
183 CPPUNIT_ASSERT_EQUAL(s2,s1);
185 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
187 CPPUNIT_ASSERT_EQUAL(s2,s1);
189 CPPUNIT_ASSERT(!MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
191 CPPUNIT_ASSERT(!MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
194 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
196 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
198 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
200 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
202 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
204 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
206 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
208 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
210 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
212 CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
215 void MEDCoupling::MEDCalculatorBasicsTest::testSPython2()
219 CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isParenthesisMatching(s1,s2));
220 CPPUNIT_ASSERT_EQUAL(s2,std::string("\"12:3\",\":\",4"));
223 void MEDCoupling::MEDCalculatorBasicsTest::testSPython3()
226 s1="( 12:3 , : ,4 )";
227 s2=MEDCoupling::SPythonInterpreter::strip(s1);
228 CPPUNIT_ASSERT_EQUAL(s2,std::string("(12:3,:,4)"));
229 s1=" ( 12:3 , : ,4 ) ";
230 s2=MEDCoupling::SPythonInterpreter::strip(s1);
231 CPPUNIT_ASSERT_EQUAL(s2,std::string("(12:3,:,4)"));