Salome HOME
4d071116636ffb3628d3f239ea04c595146495b4
[modules/med.git] / src / MEDMEM / test_operation_fielddouble.cxx
1 // Programme de test des operations sur les champs
2
3 #include <string>
4 #include <iostream>
5 #include <iomanip>
6 #include <cmath>
7
8 #include "MEDMEM_Exception.hxx"
9 #include "MEDMEM_Mesh.hxx"
10 #include "MEDMEM_Family.hxx"
11 #include "MEDMEM_Group.hxx"
12
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"
18
19 double myfunction1(double x)
20 {
21     return 0.25*(x-1.0);
22 }
23
24
25 using namespace std;
26 using namespace MEDMEM;
27 using namespace MED_EN;
28
29 void affiche_field_(FIELD_ * myField, const SUPPORT * mySupport)
30 {
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;
40   }
41   cout << "- iteration :" << endl ;
42   cout << "    - numero : " << myField->getIterationNumber()<< endl  ;
43   cout << "    - ordre  : " << myField->getOrderNumber()<< endl  ;
44   cout << "    - temps  : " << myField->getTime()<< endl  ;
45
46   cout << "- Type : " << myField->getValueType()<< endl;
47
48   cout << "- Adresse support : " << mySupport << endl;
49 }
50
51 void affiche_fieldT(FIELD<double> * myField, const SUPPORT * mySupport)
52 {
53   affiche_field_((FIELD_ *) myField, mySupport);
54
55   cout << "- Valeurs :"<<endl;
56   int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
57   int NumberOfComponents = myField->getNumberOfComponents() ;
58
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]<< " ";
63     cout<<endl;
64   }
65   cout << endl;
66   cout << "Norme euclidienne : " << myField->norm2() << endl;
67   cout << "Norme max         : " << myField->normMax() << endl;
68   try
69   {
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;
73
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;
77   }
78   catch (MEDEXCEPTION &ex)
79   {
80       cout << ex.what() << endl;
81   }
82 }
83
84 void affiche_valeur_field(const FIELD<double>& f)
85 {
86     const int tailleMax=12;
87     const int taille=f.getNumberOfValues()*f.getNumberOfComponents();
88     const double * value=f.getValue(f.getvalue()->getMode());
89     if(taille<=tailleMax)
90         for(int i=0;i<taille;i++)
91             cout << setw(3) << value[i] << " ";
92     else
93     {
94         for(int i=0; i<tailleMax/2; ++i)
95             cout << setw(3) << value[i] << " ";
96         cout << "    ...    ";
97         for(int i=taille-tailleMax/2 ; i<taille; ++i)
98             cout << setw(3) << value[i] << " ";
99     }
100 }
101
102 void checkOperation(const FIELD<double>& resOp, const FIELD<double>& f1, const FIELD<double>& f2, 
103         char Op, const char* intitule, int verbose)
104 {
105     int res=0;
106
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
113  
114     // check size compatibility
115     if(f1.getNumberOfValues()*f1.getNumberOfComponents()!=size ||
116             resOp.getNumberOfValues()*resOp.getNumberOfComponents()!=size)
117         res=1;
118
119     if(!res)
120     {
121         switch(Op)
122         {
123             case '+':
124                 for(int i=0; i!=size; ++i)
125                     if(value[i]!=value1[i]+value2[i])   
126                         res+=1;
127                 break;
128             case '-':
129                 for(int i=0; i!=size; ++i)
130                     if(value[i]!=value1[i]-value2[i])   
131                         res+=1;
132                 break;
133             case 'n':
134                 for(int i=0; i!=size; ++i)
135                     if(value[i]!=-value1[i])
136                         res+=1;
137                 break;
138             case '*':
139                 for(int i=0; i!=size; ++i)
140                     if(value[i]!=value1[i]*value2[i])   
141                         res+=1;
142                 break;
143             case '/':
144                 for(int i=0; i!=size; ++i)
145                     if(value2[i]!=0.0)
146                         if(value[i]!=value1[i]/value2[i])       
147                             res+=1;
148                 break;
149             case '=':
150                 for(int i=0; i!=size; ++i)
151                     if(value[i]!=value2[i])
152                         res+=1;
153                 break;
154             case 'a':
155                 for(int i=0; i!=size; ++i)
156                     if(value[i]!=value1[i]+value2[i]*value2[i]) 
157                         res+=1;
158                 break;
159         }
160             
161     }
162
163     if (verbose)
164         cout << endl << intitule << "[";
165     cout << res;
166     if (verbose)
167     {
168         cout << "] : ";
169         affiche_valeur_field(resOp);
170     }
171     else 
172         cout << endl;
173 }
174
175 int main (int argc, char ** argv)
176 {
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
181
182     if (argc>=2 && !strcmp(argv[1],"-v"))
183         verbose=1;
184
185     if (argc != 4+verbose) 
186     {
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;
191         exit(-1);
192     }
193     string filename  = argv[verbose+1];
194     string meshname  = argv[verbose+2];
195     string fieldname = argv[verbose+3];
196
197     /* read MESH, SUPPORT and FIELDS */
198     MESH * myMesh = new MESH(MED_DRIVER,filename,meshname);
199     SUPPORT * mySupport;
200     FIELD<double> * myField1;
201     try
202     {
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);
206     }
207     catch (MEDEXCEPTION &ex)
208     {
209         // field wasn't found on cells, try on nodes
210         delete mySupport ;
211         mySupport = new SUPPORT(myMesh,"On_all_node",MED_NODE);
212         try 
213         {
214             myField1 = new FIELD<double>(mySupport,MED_DRIVER,filename,fieldname) ;
215         }
216         catch (...) 
217         {
218             cout << "Field double " << fieldname << " not found !!!" << endl ;
219             exit (-1) ;
220         }
221     }
222     FIELD<double> * myField2 = new FIELD<double>(* myField1);
223     FIELD<double> myFieldPlus = *myField1 + *myField2;
224     if(verbose)
225     {
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;
234         delete myField1_vol;
235
236         affiche_fieldT(myField1, myField1->getSupport());
237         cout <<  endl << string(60,'-') << endl;
238         affiche_fieldT(myField2, myField2->getSupport());
239         cout << endl << string(60,'-') << endl;
240         
241         FIELD<double>* myFieldDot = FIELD<double>::scalarProduct(*myField1, *myField2);
242         affiche_fieldT(myFieldDot, myFieldDot->getSupport());
243         delete myFieldDot;
244         cout <<  endl << string(60,'-') << endl ;
245         affiche_fieldT(&myFieldPlus, myFieldPlus.getSupport());
246         cout <<  endl << string(60,'-') << endl << endl ;
247     }
248
249
250     // Verifie plusieurs cas de non compatibilité 
251
252     // test 1 : Unites non compatibles
253     const string unite=myField1->getMEDComponentUnit(1);
254     myField1->setMEDComponentUnit(1,string("UniteBidon"));
255     ntest++; res=1;
256     try
257     {
258         FIELD<double> myFieldPlus = *myField1 + *myField2;
259         if(verbose) 
260         {
261             cout << endl << string(60,'-') << endl;
262             cout<< "Test " << ntest << " : incompatibilité d'unité : " << endl << endl;
263         }
264     }
265     catch (MEDEXCEPTION & ex)
266     {
267         res=0;
268         if(verbose) 
269             cout << ex.what() << endl;
270         myField1->setMEDComponentUnit(1,unite);
271     }
272     cout << res << endl;
273
274     // test 2 : numberOfComponents non compatibles
275     const int numberOfComponents =myField1->getNumberOfComponents();
276     myField1->setNumberOfComponents(13);
277     ntest++; res=1;
278     try
279     {
280         if(verbose) 
281         {
282             cout << endl << string(60,'-') << endl;
283             cout<< "Test " << ntest << " : incompatibilité nombre de composantes : " << endl << endl;
284         }
285         FIELD<double> myFieldPlus = *myField1 + *myField2;
286     }
287     catch (MEDEXCEPTION & ex)
288     {
289         res=0;
290         if(verbose)
291             cout << endl << ex.what() << endl << endl;
292         myField1->setNumberOfComponents(numberOfComponents);
293     }
294     cout << res << endl;
295
296     // test 3 : supports non compatibles
297     const SUPPORT mySupport2(myMesh,"On_all_node",MED_NODE);
298     myField1->setSupport(&mySupport2);
299     ntest++; res=1;
300     try
301     {
302         if(verbose)
303             cout << endl << string(60,'-') << endl << "Test " << ntest << " : incompatibilité des supports"  << endl << endl;
304         FIELD<double> myFieldPlus = *myField1 + *myField2;
305     }
306     catch (MEDEXCEPTION & ex)
307     {
308         res=0;
309         if(verbose)
310             cout << ex.what() << endl << endl << endl;
311         myField1->setSupport(mySupport);
312     }
313     cout << res << endl;
314
315     // test 4 : champs de taille nulle
316     myField1->setNumberOfComponents(0);
317     myField2->setNumberOfComponents(0);
318     ntest++; res=2;
319     try
320     {
321         if(verbose)
322             cout<< endl << string(60,'-') << endl << "Test " << ntest << " : incompatibilité taille nulle" << endl << endl;
323         FIELD<double> myFieldPlus = *myField1 + *myField2;
324     }
325     catch (MEDEXCEPTION & ex)
326     {
327         --res;
328         if(verbose) 
329             cout << ex.what() << endl << endl ;
330     }
331     try
332     {
333         double mynorm2=myField1->norm2();
334     }
335     catch (MEDEXCEPTION & ex)
336     {
337         --res;
338         if(verbose) 
339             cout << ex.what() << endl << endl ;
340         myField1->setNumberOfComponents(numberOfComponents);
341         myField2->setNumberOfComponents(numberOfComponents);
342     }
343     cout << res << endl;
344
345     // Apres toutes ces exceptions, des opérations qui marchent!
346
347     if(verbose)
348     {
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,'-');
353     }
354
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);
359     delete myFieldadd;
360
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);
365     delete myFieldsub;
366     FIELD<double> myFieldNeg = -(*myField1);
367     checkOperation(myFieldNeg, *myField1, *myField1, 'n', " -f1      ", verbose);
368     
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);
373     
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);
378     delete myFielddiv;
379
380     FIELD<double> myFieldAsso = (*myField1)+(*myField2)*(*myField2);
381     checkOperation(myFieldAsso, *myField1, *myField2, 'a', " f1+f2*f2 ", verbose);
382
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);
387
388     *myField1 += *myField2;
389     checkOperation(*myField1, *myField2, *myField2, '+', " f1+=f2   ", verbose);
390
391     *myField1 -= *myField2;
392     checkOperation(*myField1, *myField2, *myField2, '=', " f1-=f2   ", verbose);
393
394     *myField1 *= *myField2;
395     checkOperation(*myField1, *myField2, *myField2, '*', " f1*=f2   ", verbose);
396     *myField1 /= *myField2;
397     checkOperation(*myField1, *myFieldmul, *myField2, '/', " f1/=f2   ", verbose);
398     delete myFieldmul;
399
400
401     delete myField1;
402     delete myField2;
403     delete mySupport ;
404     delete myMesh ;
405     return 0;
406 }