1 // Programme de test des operations sur les champs
8 #include "MEDMEM_Exception.hxx"
9 #include "MEDMEM_Mesh.hxx"
10 #include "MEDMEM_Family.hxx"
11 #include "MEDMEM_Group.hxx"
13 #include "MEDMEM_MedMeshDriver.hxx"
14 #include "MEDMEM_MedFieldDriver.hxx"
15 #include "MEDMEM_Support.hxx"
16 #include "MEDMEM_Field.hxx"
17 #include "MEDMEM_define.hxx"
19 double myfunction1(double x)
26 void affiche_field_(FIELD_ * myField, const SUPPORT * mySupport)
28 cout << "Field "<< myField->getName() << " : " <<myField->getDescription() << endl ;
29 int NumberOfComponents = myField->getNumberOfComponents() ;
30 cout << "- Nombre de composantes : "<< NumberOfComponents << endl ;
31 cout << "- Nombre de valeurs : "<< myField->getNumberOfValues() << endl ;
32 for (int i=1; i<NumberOfComponents+1; i++) {
33 cout << " - composante "<<i<<" :"<<endl ;
34 cout << " - nom : "<<myField->getComponentName(i)<< endl;
35 cout << " - description : "<<myField->getComponentDescription(i) << endl;
36 cout << " - units : "<<myField->getMEDComponentUnit(i) << endl;
38 cout << "- iteration :" << endl ;
39 cout << " - numero : " << myField->getIterationNumber()<< endl ;
40 cout << " - ordre : " << myField->getOrderNumber()<< endl ;
41 cout << " - temps : " << myField->getTime()<< endl ;
43 cout << "- Type : " << myField->getValueType()<< endl;
45 cout << "- Adresse support : " << mySupport << endl;
48 void affiche_fieldT(FIELD<double> * myField, const SUPPORT * mySupport)
50 affiche_field_((FIELD_ *) myField, mySupport);
52 cout << "- Valeurs :"<<endl;
53 int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
54 int NumberOfComponents = myField->getNumberOfComponents() ;
56 for (int i=1; i<NumberOf+1; i++) {
57 const double * value = myField->getValueI(MED_FULL_INTERLACE,i) ;
58 for (int j=0; j<NumberOfComponents; j++)
59 cout << value[j]<< " ";
63 cout << "Norme euclidienne : " << myField->norm2() << endl;
64 cout << "Norme max : " << myField->normMax() << endl;
67 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
68 cout << "Norme L2 - comp=" << i << " : " << myField->normL2(i) << endl;
69 cout << "Norme L2 : " << myField->normL2() << endl;
71 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
72 cout << "Norme L1 - comp=" << i << " : " << myField->normL1(i) << endl;
73 cout << "Norme L1 : " << myField->normL1() << endl;
75 catch (MEDEXCEPTION &ex)
77 cout << ex.what() << endl;
81 void affiche_valeur_field(const FIELD<double>& f)
83 const int tailleMax=12;
84 const int taille=f.getNumberOfValues()*f.getNumberOfComponents();
85 const double * value=f.getValue(f.getvalue()->getMode());
87 for(int i=0;i<taille;i++)
88 cout << setw(3) << value[i] << " ";
91 for(int i=0; i<tailleMax/2; ++i)
92 cout << setw(3) << value[i] << " ";
94 for(int i=taille-tailleMax/2 ; i<taille; ++i)
95 cout << setw(3) << value[i] << " ";
99 void checkOperation(const FIELD<double>& resOp, const FIELD<double>& f1, const FIELD<double>& f2,
100 char Op, const char* intitule, int verbose)
104 // get pointers to inside arrays of values
105 medModeSwitch mode=resOp.getvalue()->getMode();
106 const double * value=resOp.getValue(mode);
107 const double * value1=f1.getValue(mode);
108 const double * value2=f2.getValue(mode);
109 const int size=f1.getNumberOfValues()*f1.getNumberOfComponents(); // size of field1
111 // check size compatibility
112 if(f1.getNumberOfValues()*f1.getNumberOfComponents()!=size ||
113 resOp.getNumberOfValues()*resOp.getNumberOfComponents()!=size)
121 for(int i=0; i!=size; ++i)
122 if(value[i]!=value1[i]+value2[i])
126 for(int i=0; i!=size; ++i)
127 if(value[i]!=value1[i]-value2[i])
131 for(int i=0; i!=size; ++i)
132 if(value[i]!=-value1[i])
136 for(int i=0; i!=size; ++i)
137 if(value[i]!=value1[i]*value2[i])
141 for(int i=0; i!=size; ++i)
143 if(value[i]!=value1[i]/value2[i])
147 for(int i=0; i!=size; ++i)
148 if(value[i]!=value2[i])
152 for(int i=0; i!=size; ++i)
153 if(value[i]!=value1[i]+value2[i]*value2[i])
161 cout << endl << intitule << "[";
166 affiche_valeur_field(resOp);
172 int main (int argc, char ** argv)
174 /* process the arguments */
175 int verbose=0; // verbose=1 if the verbose mode is selected
176 int res=0; // unit test result
177 int ntest=0; // numéro du test
179 if (argc>=2 && !strcmp(argv[1],"-v"))
182 if (argc != 4+verbose)
184 cerr << "Usage : " << argv[0]
185 << "[-v] filename meshname fieldname" << endl << endl
186 << "-> tests field's operations on the FIELD<double> fieldname" << endl
187 << "Use optional option -v to select verbose mode" << endl;
190 string filename = argv[verbose+1];
191 string meshname = argv[verbose+2];
192 string fieldname = argv[verbose+3];
194 /* read MESH, SUPPORT and FIELDS */
195 MESH * myMesh = new MESH(MED_DRIVER,filename,meshname);
197 FIELD<double> * myField1;
200 mySupport = new SUPPORT(myMesh,"Support on all Cells",MED_CELL);
201 myField1 = new FIELD<double>(mySupport,MED_DRIVER,filename,fieldname) ;
202 myField1->setValueType(MED_REEL64);
204 catch (MEDEXCEPTION &ex)
206 // field wasn't found on cells, try on nodes
208 mySupport = new SUPPORT(myMesh,"On_all_node",MED_NODE);
211 myField1 = new FIELD<double>(mySupport,MED_DRIVER,filename,fieldname) ;
215 cout << "Field double " << fieldname << " not found !!!" << endl ;
219 FIELD<double> * myField2 = new FIELD<double>(* myField1);
220 FIELD<double> myFieldPlus = *myField1 + *myField2;
223 // affichage des nprmes,des champs f1, f2, scalarProduct(f1,f2) et f1+f2
224 FIELD<double>* myField1_vol=myField1->getSupport()->getMesh()->getVolume(myField1->getSupport());
225 cout << "Norme L2 calculée en fournissant le volume : " << myField1->normL2(myField1_vol) << endl;
226 for (int i=1; i<=myField1->getNumberOfComponents(); ++i)
227 cout << "Norme L2 - comp=" << i << " : " << myField1->normL2(i,myField1_vol) << endl;
228 cout << "Norme L1 calculée en fournissant le volume : " << myField1->normL1(myField1_vol) << endl;
229 for (int i=1; i<=myField1->getNumberOfComponents(); ++i)
230 cout << "Norme L1 - comp=" << i << " : " << myField1->normL1(i,myField1_vol) << endl;
233 affiche_fieldT(myField1, myField1->getSupport());
234 cout << endl << string(60,'-') << endl;
235 affiche_fieldT(myField2, myField2->getSupport());
236 cout << endl << string(60,'-') << endl;
238 FIELD<double>* myFieldDot = FIELD<double>::scalarProduct(*myField1, *myField2);
239 affiche_fieldT(myFieldDot, myFieldDot->getSupport());
241 cout << endl << string(60,'-') << endl ;
242 affiche_fieldT(&myFieldPlus, myFieldPlus.getSupport());
243 cout << endl << string(60,'-') << endl << endl ;
247 // Verifie plusieurs cas de non compatibilité
249 // test 1 : Unites non compatibles
250 const string unite=myField1->getMEDComponentUnit(1);
251 myField1->setMEDComponentUnit(1,string("UniteBidon"));
255 FIELD<double> myFieldPlus = *myField1 + *myField2;
258 cout << endl << string(60,'-') << endl;
259 cout<< "Test " << ntest << " : incompatibilité d'unité : " << endl << endl;
262 catch (MEDEXCEPTION & ex)
266 cout << ex.what() << endl;
267 myField1->setMEDComponentUnit(1,unite);
271 // test 2 : numberOfComponents non compatibles
272 const int numberOfComponents =myField1->getNumberOfComponents();
273 myField1->setNumberOfComponents(13);
279 cout << endl << string(60,'-') << endl;
280 cout<< "Test " << ntest << " : incompatibilité nombre de composantes : " << endl << endl;
282 FIELD<double> myFieldPlus = *myField1 + *myField2;
284 catch (MEDEXCEPTION & ex)
288 cout << endl << ex.what() << endl << endl;
289 myField1->setNumberOfComponents(numberOfComponents);
293 // test 3 : supports non compatibles
294 const SUPPORT mySupport2(myMesh,"On_all_node",MED_NODE);
295 myField1->setSupport(&mySupport2);
300 cout << endl << string(60,'-') << endl << "Test " << ntest << " : incompatibilité des supports" << endl << endl;
301 FIELD<double> myFieldPlus = *myField1 + *myField2;
303 catch (MEDEXCEPTION & ex)
307 cout << ex.what() << endl << endl << endl;
308 myField1->setSupport(mySupport);
312 // test 4 : champs de taille nulle
313 myField1->setNumberOfComponents(0);
314 myField2->setNumberOfComponents(0);
319 cout<< endl << string(60,'-') << endl << "Test " << ntest << " : incompatibilité taille nulle" << endl << endl;
320 FIELD<double> myFieldPlus = *myField1 + *myField2;
322 catch (MEDEXCEPTION & ex)
326 cout << ex.what() << endl << endl ;
330 double mynorm2=myField1->norm2();
332 catch (MEDEXCEPTION & ex)
336 cout << ex.what() << endl << endl ;
337 myField1->setNumberOfComponents(numberOfComponents);
338 myField2->setNumberOfComponents(numberOfComponents);
342 // Apres toutes ces exceptions, des opérations qui marchent!
346 cout<< endl << string(60,'-') << endl << "Test " << ++ntest << " : Operations arithmétiques" << endl;
347 cout << endl << " f1 : "; affiche_valeur_field(*myField1);
348 cout << endl << " f2 : "; affiche_valeur_field(*myField2);
349 cout << endl << string(140,'-');
352 // Test du résultats de certaines opérations et affichage si verbose
353 checkOperation(myFieldPlus, *myField1, *myField2, '+', " f1+f2 ", verbose);
354 FIELD<double>* myFieldadd = FIELD<double>::add(*myField1, *myField2);
355 checkOperation( *myFieldadd, *myField1, *myField2, '+', "add(f1,f2)", verbose);
358 FIELD<double> myFieldMoins = *myField1 - *myField2;
359 checkOperation(myFieldMoins, *myField1, *myField2, '-', " f1-f2 ", verbose);
360 FIELD<double>* myFieldsub = FIELD<double>::sub(*myField1, *myField2);
361 checkOperation( *myFieldsub, *myField1, *myField2, '-', "sub(f1,f2)", verbose);
363 FIELD<double> myFieldNeg = -(*myField1);
364 checkOperation(myFieldNeg, *myField1, *myField1, 'n', " -f1 ", verbose);
366 FIELD<double> myFieldFois = *myField1 * *myField2;
367 checkOperation(myFieldFois, *myField1, *myField2, '*', " f1*f2 ", verbose);
368 FIELD<double>* myFieldmul = FIELD<double>::mul(*myField1, *myField2);
369 checkOperation( *myFieldmul, *myField1, *myField2, '*', "mul(f1,f2)", verbose);
371 FIELD<double> myFieldDiv = *myField1 / *myField2;
372 checkOperation(myFieldDiv, *myField1, *myField2, '/', " f1/f2 ", verbose);
373 FIELD<double>* myFielddiv = FIELD<double>::div(*myField1, *myField2);
374 checkOperation( *myFielddiv, *myField1, *myField2, '/', "div(f1,f2)", verbose);
377 FIELD<double> myFieldAsso = (*myField1)+(*myField2)*(*myField2);
378 checkOperation(myFieldAsso, *myField1, *myField2, 'a', " f1+f2*f2 ", verbose);
380 myField1->applyLin(4.0,1.0);
381 checkOperation(*myField1, *myField2, *myField2, 'l', " 4.f1 + 1 ", verbose);
382 myField1->applyFunc<myfunction1>();
383 checkOperation( *myField1, *myField2, *myField1, '=', "CB : ->f1)", verbose);
385 *myField1 += *myField2;
386 checkOperation(*myField1, *myField2, *myField2, '+', " f1+=f2 ", verbose);
388 *myField1 -= *myField2;
389 checkOperation(*myField1, *myField2, *myField2, '=', " f1-=f2 ", verbose);
391 *myField1 *= *myField2;
392 checkOperation(*myField1, *myField2, *myField2, '*', " f1*=f2 ", verbose);
393 *myField1 /= *myField2;
394 checkOperation(*myField1, *myFieldmul, *myField2, '/', " f1/=f2 ", verbose);