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 using namespace MEDMEM;
27 using namespace MED_EN;
29 void affiche_field_(FIELD_ * myField, const SUPPORT * mySupport)
31 cout << "Field "<< myField->getName() << " : " <<myField->getDescription() << endl ;
32 int NumberOfComponents = myField->getNumberOfComponents() ;
33 cout << "- Nombre de composantes : "<< NumberOfComponents << endl ;
34 cout << "- Nombre de valeurs : "<< myField->getNumberOfValues() << endl ;
35 for (int i=1; i<NumberOfComponents+1; i++) {
36 cout << " - composante "<<i<<" :"<<endl ;
37 cout << " - nom : "<<myField->getComponentName(i)<< endl;
38 cout << " - description : "<<myField->getComponentDescription(i) << endl;
39 cout << " - units : "<<myField->getMEDComponentUnit(i) << endl;
41 cout << "- iteration :" << endl ;
42 cout << " - numero : " << myField->getIterationNumber()<< endl ;
43 cout << " - ordre : " << myField->getOrderNumber()<< endl ;
44 cout << " - temps : " << myField->getTime()<< endl ;
46 cout << "- Type : " << myField->getValueType()<< endl;
48 cout << "- Adresse support : " << mySupport << endl;
51 void affiche_fieldT(FIELD<double> * myField, const SUPPORT * mySupport)
53 affiche_field_((FIELD_ *) myField, mySupport);
55 cout << "- Valeurs :"<<endl;
56 int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
57 int NumberOfComponents = myField->getNumberOfComponents() ;
59 for (int i=1; i<NumberOf+1; i++) {
60 const double * value = myField->getValueI(MED_FULL_INTERLACE,i) ;
61 for (int j=0; j<NumberOfComponents; j++)
62 cout << value[j]<< " ";
66 cout << "Norme euclidienne : " << myField->norm2() << endl;
67 cout << "Norme max : " << myField->normMax() << endl;
70 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
71 cout << "Norme L2 - comp=" << i << " : " << myField->normL2(i) << endl;
72 cout << "Norme L2 : " << myField->normL2() << endl;
74 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
75 cout << "Norme L1 - comp=" << i << " : " << myField->normL1(i) << endl;
76 cout << "Norme L1 : " << myField->normL1() << endl;
78 catch (MEDEXCEPTION &ex)
80 cout << ex.what() << endl;
84 void affiche_valeur_field(const FIELD<double>& f)
86 const int tailleMax=12;
87 const int taille=f.getNumberOfValues()*f.getNumberOfComponents();
88 const double * value=f.getValue(f.getvalue()->getMode());
90 for(int i=0;i<taille;i++)
91 cout << setw(3) << value[i] << " ";
94 for(int i=0; i<tailleMax/2; ++i)
95 cout << setw(3) << value[i] << " ";
97 for(int i=taille-tailleMax/2 ; i<taille; ++i)
98 cout << setw(3) << value[i] << " ";
102 void checkOperation(const FIELD<double>& resOp, const FIELD<double>& f1, const FIELD<double>& f2,
103 char Op, const char* intitule, int verbose)
107 // get pointers to inside arrays of values
108 medModeSwitch mode=resOp.getvalue()->getMode();
109 const double * value=resOp.getValue(mode);
110 const double * value1=f1.getValue(mode);
111 const double * value2=f2.getValue(mode);
112 const int size=f1.getNumberOfValues()*f1.getNumberOfComponents(); // size of field1
114 // check size compatibility
115 if(f1.getNumberOfValues()*f1.getNumberOfComponents()!=size ||
116 resOp.getNumberOfValues()*resOp.getNumberOfComponents()!=size)
124 for(int i=0; i!=size; ++i)
125 if(value[i]!=value1[i]+value2[i])
129 for(int i=0; i!=size; ++i)
130 if(value[i]!=value1[i]-value2[i])
134 for(int i=0; i!=size; ++i)
135 if(value[i]!=-value1[i])
139 for(int i=0; i!=size; ++i)
140 if(value[i]!=value1[i]*value2[i])
144 for(int i=0; i!=size; ++i)
146 if(value[i]!=value1[i]/value2[i])
150 for(int i=0; i!=size; ++i)
151 if(value[i]!=value2[i])
155 for(int i=0; i!=size; ++i)
156 if(value[i]!=value1[i]+value2[i]*value2[i])
164 cout << endl << intitule << "[";
169 affiche_valeur_field(resOp);
175 int main (int argc, char ** argv)
177 /* process the arguments */
178 int verbose=0; // verbose=1 if the verbose mode is selected
179 int res=0; // unit test result
180 int ntest=0; // numéro du test
182 if (argc>=2 && !strcmp(argv[1],"-v"))
185 if (argc != 4+verbose)
187 cerr << "Usage : " << argv[0]
188 << "[-v] filename meshname fieldname" << endl << endl
189 << "-> tests field's operations on the FIELD<double> fieldname" << endl
190 << "Use optional option -v to select verbose mode" << endl;
193 string filename = argv[verbose+1];
194 string meshname = argv[verbose+2];
195 string fieldname = argv[verbose+3];
197 /* read MESH, SUPPORT and FIELDS */
198 MESH * myMesh = new MESH(MED_DRIVER,filename,meshname);
200 FIELD<double> * myField1;
203 mySupport = new SUPPORT(myMesh,"Support on all Cells",MED_CELL);
204 myField1 = new FIELD<double>(mySupport,MED_DRIVER,filename,fieldname) ;
205 myField1->setValueType(MED_REEL64);
207 catch (MEDEXCEPTION &ex)
209 // field wasn't found on cells, try on nodes
211 mySupport = new SUPPORT(myMesh,"On_all_node",MED_NODE);
214 myField1 = new FIELD<double>(mySupport,MED_DRIVER,filename,fieldname) ;
218 cout << "Field double " << fieldname << " not found !!!" << endl ;
222 FIELD<double> * myField2 = new FIELD<double>(* myField1);
223 FIELD<double> myFieldPlus = *myField1 + *myField2;
226 // affichage des nprmes,des champs f1, f2, scalarProduct(f1,f2) et f1+f2
227 FIELD<double>* myField1_vol=myField1->getSupport()->getMesh()->getVolume(myField1->getSupport());
228 cout << "Norme L2 calculée en fournissant le volume : " << myField1->normL2(myField1_vol) << endl;
229 for (int i=1; i<=myField1->getNumberOfComponents(); ++i)
230 cout << "Norme L2 - comp=" << i << " : " << myField1->normL2(i,myField1_vol) << endl;
231 cout << "Norme L1 calculée en fournissant le volume : " << myField1->normL1(myField1_vol) << endl;
232 for (int i=1; i<=myField1->getNumberOfComponents(); ++i)
233 cout << "Norme L1 - comp=" << i << " : " << myField1->normL1(i,myField1_vol) << endl;
236 affiche_fieldT(myField1, myField1->getSupport());
237 cout << endl << string(60,'-') << endl;
238 affiche_fieldT(myField2, myField2->getSupport());
239 cout << endl << string(60,'-') << endl;
241 FIELD<double>* myFieldDot = FIELD<double>::scalarProduct(*myField1, *myField2);
242 affiche_fieldT(myFieldDot, myFieldDot->getSupport());
244 cout << endl << string(60,'-') << endl ;
245 affiche_fieldT(&myFieldPlus, myFieldPlus.getSupport());
246 cout << endl << string(60,'-') << endl << endl ;
250 // Verifie plusieurs cas de non compatibilité
252 // test 1 : Unites non compatibles
253 const string unite=myField1->getMEDComponentUnit(1);
254 myField1->setMEDComponentUnit(1,string("UniteBidon"));
258 FIELD<double> myFieldPlus = *myField1 + *myField2;
261 cout << endl << string(60,'-') << endl;
262 cout<< "Test " << ntest << " : incompatibilité d'unité : " << endl << endl;
265 catch (MEDEXCEPTION & ex)
269 cout << ex.what() << endl;
270 myField1->setMEDComponentUnit(1,unite);
274 // test 2 : numberOfComponents non compatibles
275 const int numberOfComponents =myField1->getNumberOfComponents();
276 myField1->setNumberOfComponents(13);
282 cout << endl << string(60,'-') << endl;
283 cout<< "Test " << ntest << " : incompatibilité nombre de composantes : " << endl << endl;
285 FIELD<double> myFieldPlus = *myField1 + *myField2;
287 catch (MEDEXCEPTION & ex)
291 cout << endl << ex.what() << endl << endl;
292 myField1->setNumberOfComponents(numberOfComponents);
296 // test 3 : supports non compatibles
297 const SUPPORT mySupport2(myMesh,"On_all_node",MED_NODE);
298 myField1->setSupport(&mySupport2);
303 cout << endl << string(60,'-') << endl << "Test " << ntest << " : incompatibilité des supports" << endl << endl;
304 FIELD<double> myFieldPlus = *myField1 + *myField2;
306 catch (MEDEXCEPTION & ex)
310 cout << ex.what() << endl << endl << endl;
311 myField1->setSupport(mySupport);
315 // test 4 : champs de taille nulle
316 myField1->setNumberOfComponents(0);
317 myField2->setNumberOfComponents(0);
322 cout<< endl << string(60,'-') << endl << "Test " << ntest << " : incompatibilité taille nulle" << endl << endl;
323 FIELD<double> myFieldPlus = *myField1 + *myField2;
325 catch (MEDEXCEPTION & ex)
329 cout << ex.what() << endl << endl ;
333 double mynorm2=myField1->norm2();
335 catch (MEDEXCEPTION & ex)
339 cout << ex.what() << endl << endl ;
340 myField1->setNumberOfComponents(numberOfComponents);
341 myField2->setNumberOfComponents(numberOfComponents);
345 // Apres toutes ces exceptions, des opérations qui marchent!
349 cout<< endl << string(60,'-') << endl << "Test " << ++ntest << " : Operations arithmétiques" << endl;
350 cout << endl << " f1 : "; affiche_valeur_field(*myField1);
351 cout << endl << " f2 : "; affiche_valeur_field(*myField2);
352 cout << endl << string(140,'-');
355 // Test du résultats de certaines opérations et affichage si verbose
356 checkOperation(myFieldPlus, *myField1, *myField2, '+', " f1+f2 ", verbose);
357 FIELD<double>* myFieldadd = FIELD<double>::add(*myField1, *myField2);
358 checkOperation( *myFieldadd, *myField1, *myField2, '+', "add(f1,f2)", verbose);
361 FIELD<double> myFieldMoins = *myField1 - *myField2;
362 checkOperation(myFieldMoins, *myField1, *myField2, '-', " f1-f2 ", verbose);
363 FIELD<double>* myFieldsub = FIELD<double>::sub(*myField1, *myField2);
364 checkOperation( *myFieldsub, *myField1, *myField2, '-', "sub(f1,f2)", verbose);
366 FIELD<double> myFieldNeg = -(*myField1);
367 checkOperation(myFieldNeg, *myField1, *myField1, 'n', " -f1 ", verbose);
369 FIELD<double> myFieldFois = *myField1 * *myField2;
370 checkOperation(myFieldFois, *myField1, *myField2, '*', " f1*f2 ", verbose);
371 FIELD<double>* myFieldmul = FIELD<double>::mul(*myField1, *myField2);
372 checkOperation( *myFieldmul, *myField1, *myField2, '*', "mul(f1,f2)", verbose);
374 FIELD<double> myFieldDiv = *myField1 / *myField2;
375 checkOperation(myFieldDiv, *myField1, *myField2, '/', " f1/f2 ", verbose);
376 FIELD<double>* myFielddiv = FIELD<double>::div(*myField1, *myField2);
377 checkOperation( *myFielddiv, *myField1, *myField2, '/', "div(f1,f2)", verbose);
380 FIELD<double> myFieldAsso = (*myField1)+(*myField2)*(*myField2);
381 checkOperation(myFieldAsso, *myField1, *myField2, 'a', " f1+f2*f2 ", verbose);
383 myField1->applyLin(4.0,1.0);
384 checkOperation(*myField1, *myField2, *myField2, 'l', " 4.f1 + 1 ", verbose);
385 myField1->applyFunc<myfunction1>();
386 checkOperation( *myField1, *myField2, *myField1, '=', "CB : ->f1)", verbose);
388 *myField1 += *myField2;
389 checkOperation(*myField1, *myField2, *myField2, '+', " f1+=f2 ", verbose);
391 *myField1 -= *myField2;
392 checkOperation(*myField1, *myField2, *myField2, '=', " f1-=f2 ", verbose);
394 *myField1 *= *myField2;
395 checkOperation(*myField1, *myField2, *myField2, '*', " f1*=f2 ", verbose);
396 *myField1 /= *myField2;
397 checkOperation(*myField1, *myFieldmul, *myField2, '/', " f1/=f2 ", verbose);