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 using namespace MEDMEM;
20 using namespace MED_EN;
22 int myfunction1(int x)
27 int myfunction2(int x)
33 void affiche_field_(FIELD_ * myField, const SUPPORT * mySupport)
35 cout << "Field "<< myField->getName() << " : " <<myField->getDescription() << endl ;
36 int NumberOfComponents = myField->getNumberOfComponents() ;
37 cout << "- Nombre de composantes : "<< NumberOfComponents << endl ;
38 cout << "- Nombre de valeurs : "<< myField->getNumberOfValues() << endl ;
39 for (int i=1; i<NumberOfComponents+1; i++) {
40 cout << " - composante "<<i<<" :"<<endl ;
41 cout << " - nom : "<<myField->getComponentName(i)<< endl;
42 cout << " - description : "<<myField->getComponentDescription(i) << endl;
43 cout << " - units : "<<myField->getMEDComponentUnit(i) << endl;
45 cout << "- iteration :" << endl ;
46 cout << " - numero : " << myField->getIterationNumber()<< endl ;
47 cout << " - ordre : " << myField->getOrderNumber()<< endl ;
48 cout << " - temps : " << myField->getTime()<< endl ;
50 cout << "- Type : " << myField->getValueType()<< endl;
52 cout << "- Adresse support : " << mySupport << endl;
55 void affiche_fieldT(FIELD<int> * myField, const SUPPORT * mySupport)
57 affiche_field_((FIELD_ *) myField, mySupport);
59 cout << "- Valeurs :"<<endl;
60 int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
61 int NumberOfComponents = myField->getNumberOfComponents() ;
63 for (int i=1; i<NumberOf+1; i++) {
64 const int * value = myField->getValueI(MED_FULL_INTERLACE,i) ;
65 for (int j=0; j<NumberOfComponents; j++)
66 cout << value[j]<< " ";
69 std::cout << std::endl;
70 std::cout << "Norme euclidienne : " << myField->norm2() << endl;
71 std::cout << "Norme max : " << myField->normMax() << endl;
74 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
75 std::cout << "Norme L2 - comp=" << i << " : " << myField->normL2(i) << endl;
76 std::cout << "Norme L2 : " << myField->normL2() << endl;
78 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
79 std::cout << "Norme L1 - comp=" << i << " : " << myField->normL1(i) << endl;
80 std::cout << "Norme L1 : " << myField->normL1() << endl;
82 catch (MEDEXCEPTION &ex)
84 std::cout << ex.what() << std::endl;
88 void affiche_valeur_field(const char * intitule, const int taille, const FIELD<int>& f)
90 const int * value=f.getValue(f.getvalue()->getMode());
91 std::cout << endl << intitule;
92 for(int i=0;i<taille;i++)
93 std::cout << setw(3) << value[i] << " ";
96 int main (int argc, char ** argv)
100 cerr << "Usage : " << argv[0]
101 << " filename meshname fieldname" << endl << endl;
104 string filename = argv[1] ;
105 string meshname = argv[2] ;
106 string fieldname = argv[3];
108 MESH * myMesh = new MESH(MED_DRIVER,filename,meshname);
110 FIELD<int> * myField1;
113 /* read MESH, SUPPORT and FIELD */
114 mySupport = new SUPPORT(myMesh,"Support on all Cells",MED_CELL);
115 myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
116 myField1->setValueType(MED_REEL64);
118 catch (MEDEXCEPTION &ex)
121 mySupport = new SUPPORT(myMesh,"On_all_node",MED_NODE);
124 myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
125 myField1->setValueType(MED_INT32);
126 myField1->setValueIJ(10,1,-9); // pour tester les normes max avec une valeur negative
130 cout << "Field int " << fieldname << " not found !!!" << endl ;
135 FIELD<int> * myField2 = new FIELD<int>(* myField1);
136 //myField1->setNumberOfValues(16); // PROVISOIRE !! BUG
137 //myField2->setNumberOfValues(16); // PROVISOIRE !! BUG
138 // FIELD<int>* myField1_vol=myField1->getSupport()->getMesh()->getVolume(myField1->getSupport());
139 // affiche_fieldT(myField1_vol, myField1->getSupport());
141 affiche_fieldT(myField1, myField1->getSupport());
142 std::cout << endl << string(60,'-') << endl;
143 affiche_fieldT(myField2, myField2->getSupport());
145 // Verifie plusieurs cas de non compatibilité
147 // Unites non compatibles
148 const string unite=myField1->getMEDComponentUnit(1);
149 myField1->setMEDComponentUnit(1,string("UniteBidon"));
152 std::cout << endl << string(60,'-') << endl;
153 std::cout<< "Test incompatibilité d'unité :" << endl;
154 FIELD<int> myFieldPlus = *myField1 + *myField2;
156 catch (MEDEXCEPTION & ex)
158 std::cout << "MEDEXCEPTION : " << ex.what() << endl;
159 myField1->setMEDComponentUnit(1,unite);
162 // numberOfComponents non compatibles
163 const int numberOfComponents =myField1->getNumberOfComponents();
164 myField1->setNumberOfComponents(4);
167 std::cout << endl << string(60,'-') << endl;
168 std::cout<< "Test incompatibilité nombre de composantes :" << endl;
169 FIELD<int> myFieldPlus = *myField1 + *myField2;
171 catch (MEDEXCEPTION & ex)
173 std::cout << ex.what() << endl;
174 myField1->setNumberOfComponents(numberOfComponents);
177 // supports non compatibles
178 const SUPPORT mySupport2(myMesh,"On_all_node",MED_NODE);
179 myField1->setSupport(&mySupport2);
182 std::cout << endl << string(60,'-') << endl;
183 std::cout<< "Test incompatibilité des supports :" << endl;
184 FIELD<int> myFieldPlus = *myField1 + *myField2;
186 catch (MEDEXCEPTION & ex)
188 std::cout << ex.what() << endl;
189 myField1->setSupport(mySupport);
192 // champs de taille nulle
193 myField1->setNumberOfComponents(0);
194 myField2->setNumberOfComponents(0);
197 std::cout << endl << string(60,'-') << endl;
198 std::cout<< "Test incompatibilité taille nulle :" << endl;
199 FIELD<int> myFieldPlus = *myField1 + *myField2;
201 catch (MEDEXCEPTION & ex)
203 std::cout << ex.what() << endl;
207 double mynorm2=myField1->norm2();
209 catch (MEDEXCEPTION & ex)
211 std::cout << ex.what() << endl;
212 myField1->setNumberOfComponents(numberOfComponents);
213 myField2->setNumberOfComponents(numberOfComponents);
216 // Apres toutes ces exceptions, des opérations qui marchent!
218 FIELD<int> myFieldPlus = *myField1 + *myField2;
219 FIELD<int> myFieldMoins = *myField1 - *myField2;
220 FIELD<int> myFieldNeg = -(*myField1);
221 FIELD<int> myFieldFois = *myField1 * *myField2;
222 FIELD<int> myFieldDiv = *myField1 / *myField2;
223 FIELD<int> myFieldAsso = (*myField1)+(*myField2)*(*myField2);
224 FIELD<int>* myFieldadd = FIELD<int>::add(*myField1, *myField2);
225 FIELD<int>* myFieldsub = FIELD<int>::sub(*myField1, *myField2);
226 FIELD<int>* myFieldmul = FIELD<int>::mul(*myField1, *myField2);
227 FIELD<int>* myFielddiv = FIELD<int>::div(*myField1, *myField2);
228 FIELD<int>* myFieldDot = FIELD<int>::scalarProduct(*myField1, *myField2);
230 std::cout << endl << string(60,'-') << endl << "f1+f2 :" << endl << endl;
231 affiche_fieldT(&myFieldPlus, myFieldPlus.getSupport());
232 std::cout << endl << string(60,'-') << endl << "add(f1,f2) :" << endl << endl;
233 affiche_fieldT(myFieldadd, myFieldadd->getSupport());
234 std::cout << endl << string(60,'-') << endl << "scalarProduct(f1,f2) :" << endl << endl;
235 affiche_fieldT(myFieldDot, myFieldDot->getSupport());
236 std::cout << endl << string(60,'-') << endl << " - f1 :" << endl << endl;
237 affiche_fieldT(&myFieldNeg, myFieldNeg.getSupport());
239 medModeSwitch mode=myFieldPlus.getvalue()->getMode();
240 int size=myFieldPlus.getNumberOfValues()*myFieldPlus.getNumberOfComponents();
242 std::cout << endl << string(60,'-') << endl << "Tests opérations :" << endl << endl;
243 affiche_valeur_field(" f1 :", size, *myField1);
244 affiche_valeur_field(" f2 :", size, *myField2);
245 std::cout << endl << " " << string(4*size,'-');
247 affiche_valeur_field(" + :", size, myFieldPlus);
248 affiche_valeur_field(" add :", size, *myFieldadd);
249 affiche_valeur_field(" - :", size, myFieldMoins);
250 affiche_valeur_field(" sub :", size, *myFieldsub);
251 affiche_valeur_field(" * :", size, myFieldFois);
252 affiche_valeur_field(" mul :", size, *myFieldmul);
253 affiche_valeur_field(" / :", size, myFieldDiv);
254 affiche_valeur_field(" div :", size, *myFielddiv);
255 affiche_valeur_field("f1+f2*f1:", size, myFieldAsso);
256 affiche_valeur_field(" - f1 :", size, myFieldNeg);
260 myField1->applyLin(1,1);
261 affiche_valeur_field(" f1+1 :", size, *myField1);
262 myField1->applyLin(1,-1);
263 affiche_valeur_field(" -> f1 :", size, *myField1);
267 myField1->applyFunc<myfunction1>();
268 affiche_valeur_field(" CB 2f1 :", size, *myField1);
269 myField1->applyFunc<myfunction2>();
270 affiche_valeur_field(" -> f1 :", size, *myField1);
274 *myField1 += *myField2;
275 affiche_valeur_field(" f1+=f2 :", size, *myField1);
278 *myField1 *= *myField2;
279 affiche_valeur_field(" f1*=f2 :", size, *myField1);
282 *myField1 /= *myField2;
283 affiche_valeur_field(" f1/=f2 :", size, *myField1);
286 *myField1 -= *myField2;
287 affiche_valeur_field(" f1-=f2 :", size, *myField1);
289 std::cout << endl << endl;
297 // delete myField1_vol;