Salome HOME
LOT7: fix compilation at med_int==int64
[modules/med.git] / src / MEDCalculator / Test / MEDCalculatorBasicsTest.cxx
1 // Copyright (C) 2007-2019  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, or (at your option) any later version.
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 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCalculatorBasicsTest.hxx"
22 #include "MEDCalculatorBrowserLiteStruct.hxx"
23 #include "MEDCalculatorDBRangeSelection.hxx"
24 #include "MEDCalculatorDBField.hxx"
25
26 #include "SPythonParser.hxx"
27 #include "SPythonInterpreter.hxx"
28
29 #include "MEDLoader.hxx"
30
31 #include "MEDCouplingMemArray.hxx"
32 #include "MEDCouplingUMesh.hxx"
33 #include "MEDCouplingFieldDouble.hxx"
34
35 #include <iostream>
36
37 void MEDCoupling::MEDCalculatorBasicsTest::testLightStruct1()
38 {
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);
41   lt.selectAllFields();
42   //std::cout << lt.str() << std::endl;
43 }
44
45 void MEDCoupling::MEDCalculatorBasicsTest::testRangeSelection1()
46 {
47   MEDCalculatorDBRangeSelection sel1(":");
48   std::vector<std::size_t> 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");
53   v=sel2.getIds(6);
54   CPPUNIT_ASSERT_EQUAL(5,(int)v.size());
55   CPPUNIT_ASSERT(std::equal(expected1,expected1+5,v.begin()));
56   MEDCalculatorDBRangeSelection sel3("2:4");
57   v=sel3.getIds(6);
58   CPPUNIT_ASSERT_EQUAL(2,(int)v.size());
59   CPPUNIT_ASSERT(std::equal(expected1+2,expected1+4,v.begin()));
60   MEDCalculatorDBRangeSelection sel4(":3");
61   v=sel4.getIds(6);
62   CPPUNIT_ASSERT_EQUAL(3,(int)v.size());
63   CPPUNIT_ASSERT(std::equal(expected1,expected1+3,v.begin()));
64   //
65   bool exc=false;
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;
79   sel5="3:7";
80   try {  v=sel5.getIds(6); } catch(INTERP_KERNEL::Exception& e) { exc=true; }
81   CPPUNIT_ASSERT(exc); exc=false;
82 }
83
84 void MEDCoupling::MEDCalculatorBasicsTest::testDBField1()
85 {
86   const char fName[]="hfile1.med";
87   generateAFile1(fName);
88   MEDCalculatorBrowserLiteStruct lt(fName);
89   lt.selectAllFields();
90   MEDCalculatorDBFieldReal *Power=new MEDCalculatorDBFieldReal(lt.getField(0));
91   MEDCalculatorDBFieldReal *Power2=new MEDCalculatorDBFieldReal(lt.getField(0));
92   MEDCalculatorDBFieldReal *Power3=(MEDCalculatorDBFieldReal *)((*Power)+(*Power2));
93   Power3->decrRef();
94   Power2->decrRef();
95   Power->decrRef();
96   //
97   MEDCalculatorDBRangeSelection t1("2:4"),t2("6:8");
98   MEDCalculatorDBRangeSelection p;
99   MEDCalculatorDBRangeSelection c1,c2;
100   //
101   Power=new MEDCalculatorDBFieldReal(lt.getField(0));
102   Power2=(*Power)(t1,p,c1);
103   Power3=(*Power)(t2,p,c1);
104   MEDCalculatorDBField *Power4=(*Power2)+(*Power3);
105   Power4->decrRef();
106   Power3->decrRef();
107   Power2->decrRef();
108   Power->decrRef();
109 }
110
111 void MEDCoupling::MEDCalculatorBasicsTest::generateAFile1(const char *fName)
112 {
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   mcIdType 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();
116   m->setName("AMesh");
117   m->setMeshDimension(2);
118   m->allocateCells(5);
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);
131   myCoords->decrRef();
132   //
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);
137   f->setName("Power");
138   f->setMesh(m);
139   DataArrayDouble *da=DataArrayDouble::New();
140   da->alloc(5,7);
141   f->setArray(da);
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++)
147     {
148       double *pt=da->getPointer();
149       for(int j=0;j<5;j++)
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);
154     }
155   da->decrRef();
156   f->decrRef();
157   m->decrRef();
158 }
159
160 void MEDCoupling::MEDCalculatorBasicsTest::testSPython1()
161 {
162   std::string s1,s2;
163   bool b;
164   s1="12:3";
165   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
166   CPPUNIT_ASSERT(!b);
167   CPPUNIT_ASSERT_EQUAL(s2,s1);
168   s1="12:";
169   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
170   CPPUNIT_ASSERT(!b);
171   CPPUNIT_ASSERT_EQUAL(s2,s1);
172   s1="12";
173   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
174   CPPUNIT_ASSERT(b);
175   CPPUNIT_ASSERT_EQUAL(s2,s1);
176   s1="";
177   CPPUNIT_ASSERT(!MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
178   s1="          ";
179   CPPUNIT_ASSERT(!MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
180   s1=":";
181   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
182   CPPUNIT_ASSERT(!b);
183   CPPUNIT_ASSERT_EQUAL(s2,s1);
184   s1=":12";
185   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
186   CPPUNIT_ASSERT(!b);
187   CPPUNIT_ASSERT_EQUAL(s2,s1);
188   s1=":12:";
189   CPPUNIT_ASSERT(!MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
190   s1=":12   23";
191   CPPUNIT_ASSERT(!MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
192   // some ' ' char
193   s1="  12  : 3  ";
194   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
195   CPPUNIT_ASSERT(!b);
196   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
197   s1="  12  : 3";
198   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
199   CPPUNIT_ASSERT(!b);
200   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
201   s1="  12  :3  ";
202   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
203   CPPUNIT_ASSERT(!b);
204   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
205   s1="  12: 3  ";
206   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
207   CPPUNIT_ASSERT(!b);
208   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
209   s1="12  : 3  ";
210   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isElementInParenthesisMatching(s1,s2,b));
211   CPPUNIT_ASSERT(!b);
212   CPPUNIT_ASSERT_EQUAL(s2,std::string("12:3"));
213 }
214
215 void MEDCoupling::MEDCalculatorBasicsTest::testSPython2()
216 {
217   std::string s1,s2;
218   s1="  12:3  , : ,4  ";
219   CPPUNIT_ASSERT(MEDCoupling::SPythonParser::isParenthesisMatching(s1,s2));
220   CPPUNIT_ASSERT_EQUAL(s2,std::string("\"12:3\",\":\",4"));
221 }
222
223 void MEDCoupling::MEDCalculatorBasicsTest::testSPython3()
224 {
225   std::string s1,s2;
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)"));
232 }