Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDCalculator / Test / MEDCalculatorBasicsTest.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 "MEDCalculatorBasicsTest.hxx"
21 #include "MEDCalculatorBrowserLiteStruct.hxx"
22 #include "MEDCalculatorDBRangeSelection.hxx"
23 #include "MEDCalculatorDBField.hxx"
24
25 #include "SPythonParser.hxx"
26 #include "SPythonInterpreter.hxx"
27
28 #include "MEDLoader.hxx"
29
30 #include "MEDCouplingMemArray.hxx"
31 #include "MEDCouplingUMesh.hxx"
32 #include "MEDCouplingFieldDouble.hxx"
33
34 #include <iostream>
35
36 void ParaMEDMEM::MEDCalculatorBasicsTest::testLightStruct1()
37 {
38   const char fname[]="/export/home/geay/MEDStdAlone/Salome5/V5_1_main/MED/MED_INSTALL_DEBUG/share/salome/resources/med/TimeStamps_import22.med";
39   MEDCalculatorBrowserLiteStruct lt(fname);
40   lt.selectAllFields();
41   //std::cout << lt.str() << std::endl;
42 }
43
44 void ParaMEDMEM::MEDCalculatorBasicsTest::testRangeSelection1()
45 {
46   MEDCalculatorDBRangeSelection sel1(":");
47   std::vector<int> v=sel1.getIds(6);
48   CPPUNIT_ASSERT_EQUAL(6,(int)v.size());
49   const int expected1[6]={0,1,2,3,4,5};
50   CPPUNIT_ASSERT(std::equal(expected1,expected1+6,v.begin()));
51   MEDCalculatorDBRangeSelection sel2(":-1");
52   v=sel2.getIds(6);
53   CPPUNIT_ASSERT_EQUAL(5,(int)v.size());
54   CPPUNIT_ASSERT(std::equal(expected1,expected1+5,v.begin()));
55   MEDCalculatorDBRangeSelection sel3("2:4");
56   v=sel3.getIds(6);
57   CPPUNIT_ASSERT_EQUAL(2,(int)v.size());
58   CPPUNIT_ASSERT(std::equal(expected1+2,expected1+4,v.begin()));
59   MEDCalculatorDBRangeSelection sel4(":3");
60   v=sel4.getIds(6);
61   CPPUNIT_ASSERT_EQUAL(3,(int)v.size());
62   CPPUNIT_ASSERT(std::equal(expected1,expected1+3,v.begin()));
63   //
64   bool exc=false;
65   MEDCalculatorDBRangeSelection sel5("2");
66   try {  sel5="3:1"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
67   CPPUNIT_ASSERT(exc); exc=false;
68   try {  sel5="4:5:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
69   CPPUNIT_ASSERT(exc); exc=false;
70   try {  sel5="4:5r"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
71   CPPUNIT_ASSERT(exc); exc=false;
72   try {  sel5="4:ry5"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
73   CPPUNIT_ASSERT(exc); exc=false;
74   try {  sel5="4r:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
75   CPPUNIT_ASSERT(exc); exc=false;
76   try {  sel5="r4:7"; } catch(INTERP_KERNEL::Exception& e) { exc=true; }
77   CPPUNIT_ASSERT(exc); exc=false;
78   sel5="3:7";
79   try {  v=sel5.getIds(6); } catch(INTERP_KERNEL::Exception& e) { exc=true; }
80   CPPUNIT_ASSERT(exc); exc=false;
81 }
82
83 void ParaMEDMEM::MEDCalculatorBasicsTest::testDBField1()
84 {
85   const char fName[]="hfile1.med";
86   generateAFile1(fName);
87   MEDCalculatorBrowserLiteStruct lt(fName);
88   lt.selectAllFields();
89   MEDCalculatorDBFieldReal *Power=new MEDCalculatorDBFieldReal(lt.getField(0));
90   MEDCalculatorDBFieldReal *Power2=new MEDCalculatorDBFieldReal(lt.getField(0));
91   MEDCalculatorDBFieldReal *Power3=(MEDCalculatorDBFieldReal *)((*Power)+(*Power2));
92   Power3->decrRef();
93   Power2->decrRef();
94   Power->decrRef();
95   //
96   MEDCalculatorDBRangeSelection t1("2:4"),t2("6:8");
97   MEDCalculatorDBRangeSelection p;
98   MEDCalculatorDBRangeSelection c1,c2;
99   //
100   Power=new MEDCalculatorDBFieldReal(lt.getField(0));
101   Power2=(*Power)(t1,p,c1);
102   Power3=(*Power)(t2,p,c1);
103   MEDCalculatorDBField *Power4=(*Power2)+(*Power3);
104   Power4->decrRef();
105   Power3->decrRef();
106   Power2->decrRef();
107   Power->decrRef();
108 }
109
110 void ParaMEDMEM::MEDCalculatorBasicsTest::generateAFile1(const char *fName)
111 {
112   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 };
113   int targetConn[18]={0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4};
114   MEDCouplingUMesh *m=MEDCouplingUMesh::New();
115   m->setName("AMesh");
116   m->setMeshDimension(2);
117   m->allocateCells(5);
118   m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn);
119   m->insertNextCell(INTERP_KERNEL::NORM_TRI3,3,targetConn+4);
120   m->insertNextCell(INTERP_KERNEL::NORM_TRI3,3,targetConn+7);
121   m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn+10);
122   m->insertNextCell(INTERP_KERNEL::NORM_QUAD4,4,targetConn+14);
123   m->finishInsertingCells();
124   DataArrayDouble *myCoords=DataArrayDouble::New();
125   myCoords->alloc(9,2);
126   myCoords->setInfoOnComponent(0,"x [cm]");
127   myCoords->setInfoOnComponent(1,"y [cm]");
128   std::copy(targetCoords,targetCoords+18,myCoords->getPointer());
129   m->setCoords(myCoords);
130   myCoords->decrRef();
131   //
132   MEDLoader::WriteUMesh(fName,m,true);
133   static const int nbOfTimeSteps=10;
134   static const int nbOfComponents=7;
135   MEDCouplingFieldDouble *f=MEDCouplingFieldDouble::New(ON_CELLS,ONE_TIME);
136   f->setName("Power");
137   f->setMesh(m);
138   DataArrayDouble *da=DataArrayDouble::New();
139   da->alloc(5,7);
140   f->setArray(da);
141   da->setInfoOnComponent(0,"aaa [a]"); da->setInfoOnComponent(1,"bbb [b]"); da->setInfoOnComponent(2,"ccc [c]");
142   da->setInfoOnComponent(3,"ddd [d]"); da->setInfoOnComponent(4,"eee [e]"); da->setInfoOnComponent(5,"fff [f]");
143   da->setInfoOnComponent(6,"ggg [g]");
144   f->checkCoherency();
145   for(int i=0;i<nbOfTimeSteps;i++)
146     {
147       double *pt=da->getPointer();
148       for(int j=0;j<5;j++)
149         for(int k=0;k<nbOfComponents;k++,pt++)
150           *pt=(i+1)*100.+(j+1)*10.+k+1;
151       f->setTime(i*0.1,i,-i);
152       MEDLoader::WriteFieldUsingAlreadyWrittenMesh(fName,f);
153     }
154   da->decrRef();
155   f->decrRef();
156   m->decrRef();
157 }
158
159 void ParaMEDMEM::MEDCalculatorBasicsTest::testSPython1()
160 {
161   std::string s1,s2;
162   bool b;
163   s1="12:3";
164   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
165   CPPUNIT_ASSERT(!b);
166   CPPUNIT_ASSERT_EQUAL(s2,s1);
167   s1="12:";
168   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
169   CPPUNIT_ASSERT(!b);
170   CPPUNIT_ASSERT_EQUAL(s2,s1);
171   s1="12";
172   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
173   CPPUNIT_ASSERT(b);
174   CPPUNIT_ASSERT_EQUAL(s2,s1);
175   s1="";
176   CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
177   s1="          ";
178   CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
179   s1=":";
180   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
181   CPPUNIT_ASSERT(!b);
182   CPPUNIT_ASSERT_EQUAL(s2,s1);
183   s1=":12";
184   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
185   CPPUNIT_ASSERT(!b);
186   CPPUNIT_ASSERT_EQUAL(s2,s1);
187   s1=":12:";
188   CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
189   s1=":12   23";
190   CPPUNIT_ASSERT(!ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
191   // some ' ' char
192   s1="  12  : 3  ";
193   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
194   CPPUNIT_ASSERT(!b);
195   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
196   s1="  12  : 3";
197   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
198   CPPUNIT_ASSERT(!b);
199   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
200   s1="  12  :3  ";
201   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
202   CPPUNIT_ASSERT(!b);
203   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
204   s1="  12: 3  ";
205   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
206   CPPUNIT_ASSERT(!b);
207   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
208   s1="12  : 3  ";
209   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
210   CPPUNIT_ASSERT(!b);
211   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
212 }
213
214 void ParaMEDMEM::MEDCalculatorBasicsTest::testSPython2()
215 {
216   std::string s1,s2;
217   s1="  12:3  , : ,4  ";
218   CPPUNIT_ASSERT(ParaMEDMEM::SPythonParser::isParenthesisMatching(s1,s2));
219   CPPUNIT_ASSERT_EQUAL(s2,std::string("\"12:3\",\":\",4"));
220 }
221
222 void ParaMEDMEM::MEDCalculatorBasicsTest::testSPython3()
223 {
224   std::string s1,s2;
225   s1="(  12:3  , : ,4  )";
226   s2=ParaMEDMEM::SPythonInterpreter::strip(s1);
227   CPPUNIT_ASSERT_EQUAL(s2,std::string("(12:3,:,4)"));
228   s1=" (  12:3  , : ,4  )  ";
229   s2=ParaMEDMEM::SPythonInterpreter::strip(s1);
230   CPPUNIT_ASSERT_EQUAL(s2,std::string("(12:3,:,4)"));
231 }