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 int myfunction1(int x)
24 int myfunction2(int x)
30 void affiche_field_(FIELD_ * myField, const SUPPORT * mySupport)
32 cout << "Field "<< myField->getName() << " : " <<myField->getDescription() << endl ;
33 int NumberOfComponents = myField->getNumberOfComponents() ;
34 cout << "- Nombre de composantes : "<< NumberOfComponents << endl ;
35 cout << "- Nombre de valeurs : "<< myField->getNumberOfValues() << endl ;
36 for (int i=1; i<NumberOfComponents+1; i++) {
37 cout << " - composante "<<i<<" :"<<endl ;
38 cout << " - nom : "<<myField->getComponentName(i)<< endl;
39 cout << " - description : "<<myField->getComponentDescription(i) << endl;
40 cout << " - units : "<<myField->getMEDComponentUnit(i) << endl;
42 cout << "- iteration :" << endl ;
43 cout << " - numero : " << myField->getIterationNumber()<< endl ;
44 cout << " - ordre : " << myField->getOrderNumber()<< endl ;
45 cout << " - temps : " << myField->getTime()<< endl ;
47 cout << "- Type : " << myField->getValueType()<< endl;
49 cout << "- Adresse support : " << mySupport << endl;
52 void affiche_fieldT(FIELD<int> * myField, const SUPPORT * mySupport)
54 affiche_field_((FIELD_ *) myField, mySupport);
56 cout << "- Valeurs :"<<endl;
57 int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
58 int NumberOfComponents = myField->getNumberOfComponents() ;
60 for (int i=1; i<NumberOf+1; i++) {
61 const int * value = myField->getValueI(MED_FULL_INTERLACE,i) ;
62 for (int j=0; j<NumberOfComponents; j++)
63 cout << value[j]<< " ";
66 std::cout << std::endl;
67 std::cout << "Norme euclidienne : " << myField->norm2() << endl;
68 std::cout << "Norme max : " << myField->normMax() << endl;
71 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
72 std::cout << "Norme L2 - comp=" << i << " : " << myField->normL2(i) << endl;
73 std::cout << "Norme L2 : " << myField->normL2() << endl;
75 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
76 std::cout << "Norme L1 - comp=" << i << " : " << myField->normL1(i) << endl;
77 std::cout << "Norme L1 : " << myField->normL1() << endl;
79 catch (MEDEXCEPTION &ex)
81 std::cout << ex.what() << std::endl;
85 void affiche_valeur_field(const char * intitule, const int taille, const FIELD<int>& f)
87 const int * value=f.getValue(f.getvalue()->getMode());
88 std::cout << endl << intitule;
89 for(int i=0;i<taille;i++)
90 std::cout << setw(3) << value[i] << " ";
93 int main (int argc, char ** argv)
97 cerr << "Usage : " << argv[0]
98 << " filename meshname fieldname" << endl << endl;
101 string filename = argv[1] ;
102 string meshname = argv[2] ;
103 string fieldname = argv[3];
105 MESH * myMesh = new MESH(MED_DRIVER,filename,meshname);
107 FIELD<int> * myField1;
110 /* read MESH, SUPPORT and FIELD */
111 mySupport = new SUPPORT(myMesh,"Support on all Cells",MED_CELL);
112 myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
113 myField1->setValueType(MED_REEL64);
115 catch (MEDEXCEPTION &ex)
118 mySupport = new SUPPORT(myMesh,"On_all_node",MED_NODE);
121 myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
122 myField1->setValueType(MED_INT32);
123 myField1->setValueIJ(10,1,-9); // pour tester les normes max avec une valeur negative
127 cout << "Field int " << fieldname << " not found !!!" << endl ;
132 FIELD<int> * myField2 = new FIELD<int>(* myField1);
133 //myField1->setNumberOfValues(16); // PROVISOIRE !! BUG
134 //myField2->setNumberOfValues(16); // PROVISOIRE !! BUG
135 // FIELD<int>* myField1_vol=myField1->getSupport()->getMesh()->getVolume(myField1->getSupport());
136 // affiche_fieldT(myField1_vol, myField1->getSupport());
138 affiche_fieldT(myField1, myField1->getSupport());
139 std::cout << endl << string(60,'-') << endl;
140 affiche_fieldT(myField2, myField2->getSupport());
142 // Verifie plusieurs cas de non compatibilité
144 // Unites non compatibles
145 const string unite=myField1->getMEDComponentUnit(1);
146 myField1->setMEDComponentUnit(1,string("UniteBidon"));
149 std::cout << endl << string(60,'-') << endl;
150 std::cout<< "Test incompatibilité d'unité :" << endl;
151 FIELD<int> myFieldPlus = *myField1 + *myField2;
153 catch (MEDEXCEPTION & ex)
155 std::cout << "MEDEXCEPTION : " << ex.what() << endl;
156 myField1->setMEDComponentUnit(1,unite);
159 // numberOfComponents non compatibles
160 const int numberOfComponents =myField1->getNumberOfComponents();
161 myField1->setNumberOfComponents(4);
164 std::cout << endl << string(60,'-') << endl;
165 std::cout<< "Test incompatibilité nombre de composantes :" << endl;
166 FIELD<int> myFieldPlus = *myField1 + *myField2;
168 catch (MEDEXCEPTION & ex)
170 std::cout << ex.what() << endl;
171 myField1->setNumberOfComponents(numberOfComponents);
174 // supports non compatibles
175 const SUPPORT mySupport2(myMesh,"On_all_node",MED_NODE);
176 myField1->setSupport(&mySupport2);
179 std::cout << endl << string(60,'-') << endl;
180 std::cout<< "Test incompatibilité des supports :" << endl;
181 FIELD<int> myFieldPlus = *myField1 + *myField2;
183 catch (MEDEXCEPTION & ex)
185 std::cout << ex.what() << endl;
186 myField1->setSupport(mySupport);
189 // champs de taille nulle
190 myField1->setNumberOfComponents(0);
191 myField2->setNumberOfComponents(0);
194 std::cout << endl << string(60,'-') << endl;
195 std::cout<< "Test incompatibilité taille nulle :" << endl;
196 FIELD<int> myFieldPlus = *myField1 + *myField2;
198 catch (MEDEXCEPTION & ex)
200 std::cout << ex.what() << endl;
204 double mynorm2=myField1->norm2();
206 catch (MEDEXCEPTION & ex)
208 std::cout << ex.what() << endl;
209 myField1->setNumberOfComponents(numberOfComponents);
210 myField2->setNumberOfComponents(numberOfComponents);
213 // Apres toutes ces exceptions, des opérations qui marchent!
215 FIELD<int> myFieldPlus = *myField1 + *myField2;
216 FIELD<int> myFieldMoins = *myField1 - *myField2;
217 FIELD<int> myFieldNeg = -(*myField1);
218 FIELD<int> myFieldFois = *myField1 * *myField2;
219 FIELD<int> myFieldDiv = *myField1 / *myField2;
220 FIELD<int> myFieldAsso = (*myField1)+(*myField2)*(*myField2);
221 FIELD<int>* myFieldadd = FIELD<int>::add(*myField1, *myField2);
222 FIELD<int>* myFieldsub = FIELD<int>::sub(*myField1, *myField2);
223 FIELD<int>* myFieldmul = FIELD<int>::mul(*myField1, *myField2);
224 FIELD<int>* myFielddiv = FIELD<int>::div(*myField1, *myField2);
225 FIELD<int>* myFieldDot = FIELD<int>::scalarProduct(*myField1, *myField2);
227 std::cout << endl << string(60,'-') << endl << "f1+f2 :" << endl << endl;
228 affiche_fieldT(&myFieldPlus, myFieldPlus.getSupport());
229 std::cout << endl << string(60,'-') << endl << "add(f1,f2) :" << endl << endl;
230 affiche_fieldT(myFieldadd, myFieldadd->getSupport());
231 std::cout << endl << string(60,'-') << endl << "scalarProduct(f1,f2) :" << endl << endl;
232 affiche_fieldT(myFieldDot, myFieldDot->getSupport());
233 std::cout << endl << string(60,'-') << endl << " - f1 :" << endl << endl;
234 affiche_fieldT(&myFieldNeg, myFieldNeg.getSupport());
236 medModeSwitch mode=myFieldPlus.getvalue()->getMode();
237 int size=myFieldPlus.getNumberOfValues()*myFieldPlus.getNumberOfComponents();
239 std::cout << endl << string(60,'-') << endl << "Tests opérations :" << endl << endl;
240 affiche_valeur_field(" f1 :", size, *myField1);
241 affiche_valeur_field(" f2 :", size, *myField2);
242 std::cout << endl << " " << string(4*size,'-');
244 affiche_valeur_field(" + :", size, myFieldPlus);
245 affiche_valeur_field(" add :", size, *myFieldadd);
246 affiche_valeur_field(" - :", size, myFieldMoins);
247 affiche_valeur_field(" sub :", size, *myFieldsub);
248 affiche_valeur_field(" * :", size, myFieldFois);
249 affiche_valeur_field(" mul :", size, *myFieldmul);
250 affiche_valeur_field(" / :", size, myFieldDiv);
251 affiche_valeur_field(" div :", size, *myFielddiv);
252 affiche_valeur_field("f1+f2*f1:", size, myFieldAsso);
253 affiche_valeur_field(" - f1 :", size, myFieldNeg);
257 myField1->applyLin(1,1);
258 affiche_valeur_field(" f1+1 :", size, *myField1);
259 myField1->applyLin(1,-1);
260 affiche_valeur_field(" -> f1 :", size, *myField1);
264 myField1->applyFunc<myfunction1>();
265 affiche_valeur_field(" CB 2f1 :", size, *myField1);
266 myField1->applyFunc<myfunction2>();
267 affiche_valeur_field(" -> f1 :", size, *myField1);
271 *myField1 += *myField2;
272 affiche_valeur_field(" f1+=f2 :", size, *myField1);
275 *myField1 *= *myField2;
276 affiche_valeur_field(" f1*=f2 :", size, *myField1);
279 *myField1 /= *myField2;
280 affiche_valeur_field(" f1/=f2 :", size, *myField1);
283 *myField1 -= *myField2;
284 affiche_valeur_field(" f1-=f2 :", size, *myField1);
286 std::cout << endl << endl;
294 // delete myField1_vol;