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"
20 #include "LocalTraceCollector.hxx"
21 #endif /* ifdef _DEBUG_*/
23 using namespace MEDMEM;
24 using namespace MED_EN;
26 int myfunction1(int x)
31 int myfunction2(int x)
37 void affiche_field_(FIELD_ * myField, const SUPPORT * mySupport)
39 cout << "Field "<< myField->getName() << " : " <<myField->getDescription() << endl ;
40 int NumberOfComponents = myField->getNumberOfComponents() ;
41 cout << "- Nombre de composantes : "<< NumberOfComponents << endl ;
42 cout << "- Nombre de valeurs : "<< myField->getNumberOfValues() << endl ;
43 for (int i=1; i<NumberOfComponents+1; i++) {
44 cout << " - composante "<<i<<" :"<<endl ;
45 cout << " - nom : "<<myField->getComponentName(i)<< endl;
46 cout << " - description : "<<myField->getComponentDescription(i) << endl;
47 cout << " - units : "<<myField->getMEDComponentUnit(i) << endl;
49 cout << "- iteration :" << endl ;
50 cout << " - numero : " << myField->getIterationNumber()<< endl ;
51 cout << " - ordre : " << myField->getOrderNumber()<< endl ;
52 cout << " - temps : " << myField->getTime()<< endl ;
54 cout << "- Type : " << myField->getValueType()<< endl;
56 cout << "- Adresse support : " << mySupport << endl;
59 void affiche_fieldT(FIELD<int> * myField, const SUPPORT * mySupport)
61 affiche_field_((FIELD_ *) myField, mySupport);
63 cout << "- Valeurs :"<<endl;
64 int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
65 int NumberOfComponents = myField->getNumberOfComponents() ;
67 for (int i=1; i<NumberOf+1; i++) {
68 const int * value = myField->getValueI(MED_FULL_INTERLACE,i) ;
69 for (int j=0; j<NumberOfComponents; j++)
70 cout << value[j]<< " ";
73 std::cout << std::endl;
74 std::cout << "Norme euclidienne : " << myField->norm2() << endl;
75 std::cout << "Norme max : " << myField->normMax() << endl;
78 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
79 std::cout << "Norme L2 - comp=" << i << " : " << myField->normL2(i) << endl;
80 std::cout << "Norme L2 : " << myField->normL2() << endl;
82 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
83 std::cout << "Norme L1 - comp=" << i << " : " << myField->normL1(i) << endl;
84 std::cout << "Norme L1 : " << myField->normL1() << endl;
86 catch (MEDEXCEPTION &ex)
88 std::cout << ex.what() << std::endl;
92 void affiche_valeur_field(const char * intitule, const int taille, const FIELD<int>& f)
94 const int * value=f.getValue(f.getvalue()->getMode());
95 std::cout << endl << intitule;
96 for(int i=0;i<taille;i++)
97 std::cout << setw(3) << value[i] << " ";
100 int main (int argc, char ** argv)
104 cerr << "Usage : " << argv[0]
105 << " filename meshname fieldname" << endl << endl;
110 LocalTraceCollector::instance();
111 #endif /* ifdef _DEBUG_*/
113 string filename = argv[1] ;
114 string meshname = argv[2] ;
115 string fieldname = argv[3];
117 MESH * myMesh = new MESH(MED_DRIVER,filename,meshname);
119 FIELD<int> * myField1;
122 /* read MESH, SUPPORT and FIELD */
123 mySupport = new SUPPORT(myMesh,"Support on all Cells",MED_CELL);
124 myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
125 myField1->setValueType(MED_REEL64);
127 catch (MEDEXCEPTION &ex)
130 mySupport = new SUPPORT(myMesh,"On_all_node",MED_NODE);
133 myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
134 myField1->setValueType(MED_INT32);
135 myField1->setValueIJ(10,1,-9); // pour tester les normes max avec une valeur negative
139 cout << "Field int " << fieldname << " not found !!!" << endl ;
144 FIELD<int> * myField2 = new FIELD<int>(* myField1);
145 //myField1->setNumberOfValues(16); // PROVISOIRE !! BUG
146 //myField2->setNumberOfValues(16); // PROVISOIRE !! BUG
147 // FIELD<int>* myField1_vol=myField1->getSupport()->getMesh()->getVolume(myField1->getSupport());
148 // affiche_fieldT(myField1_vol, myField1->getSupport());
150 affiche_fieldT(myField1, myField1->getSupport());
151 std::cout << endl << string(60,'-') << endl;
152 affiche_fieldT(myField2, myField2->getSupport());
154 // Verifie plusieurs cas de non compatibilité
156 // Unites non compatibles
157 const string unite=myField1->getMEDComponentUnit(1);
158 myField1->setMEDComponentUnit(1,string("UniteBidon"));
161 std::cout << endl << string(60,'-') << endl;
162 std::cout<< "Test incompatibilité d'unité :" << endl;
163 FIELD<int> myFieldPlus = *myField1 + *myField2;
165 catch (MEDEXCEPTION & ex)
167 std::cout << "MEDEXCEPTION : " << ex.what() << endl;
168 myField1->setMEDComponentUnit(1,unite);
171 // numberOfComponents non compatibles
172 const int numberOfComponents =myField1->getNumberOfComponents();
173 myField1->setNumberOfComponents(4);
176 std::cout << endl << string(60,'-') << endl;
177 std::cout<< "Test incompatibilité nombre de composantes :" << endl;
178 FIELD<int> myFieldPlus = *myField1 + *myField2;
180 catch (MEDEXCEPTION & ex)
182 std::cout << ex.what() << endl;
183 myField1->setNumberOfComponents(numberOfComponents);
186 // supports non compatibles
187 const SUPPORT mySupport2(myMesh,"On_all_node",MED_NODE);
188 myField1->setSupport(&mySupport2);
191 std::cout << endl << string(60,'-') << endl;
192 std::cout<< "Test incompatibilité des supports :" << endl;
193 FIELD<int> myFieldPlus = *myField1 + *myField2;
195 catch (MEDEXCEPTION & ex)
197 std::cout << ex.what() << endl;
198 myField1->setSupport(mySupport);
201 // champs de taille nulle
202 myField1->setNumberOfComponents(0);
203 myField2->setNumberOfComponents(0);
206 std::cout << endl << string(60,'-') << endl;
207 std::cout<< "Test incompatibilité taille nulle :" << endl;
208 FIELD<int> myFieldPlus = *myField1 + *myField2;
210 catch (MEDEXCEPTION & ex)
212 std::cout << ex.what() << endl;
216 double mynorm2=myField1->norm2();
218 catch (MEDEXCEPTION & ex)
220 std::cout << ex.what() << endl;
221 myField1->setNumberOfComponents(numberOfComponents);
222 myField2->setNumberOfComponents(numberOfComponents);
225 // Apres toutes ces exceptions, des opérations qui marchent!
227 FIELD<int> myFieldPlus = *myField1 + *myField2;
228 FIELD<int> myFieldMoins = *myField1 - *myField2;
229 FIELD<int> myFieldNeg = -(*myField1);
230 FIELD<int> myFieldFois = *myField1 * *myField2;
231 FIELD<int> myFieldDiv = *myField1 / *myField2;
232 FIELD<int> myFieldAsso = (*myField1)+(*myField2)*(*myField2);
233 FIELD<int>* myFieldadd = FIELD<int>::add(*myField1, *myField2);
234 FIELD<int>* myFieldsub = FIELD<int>::sub(*myField1, *myField2);
235 FIELD<int>* myFieldmul = FIELD<int>::mul(*myField1, *myField2);
236 FIELD<int>* myFielddiv = FIELD<int>::div(*myField1, *myField2);
237 FIELD<int>* myFieldDot = FIELD<int>::scalarProduct(*myField1, *myField2);
239 std::cout << endl << string(60,'-') << endl << "f1+f2 :" << endl << endl;
240 affiche_fieldT(&myFieldPlus, myFieldPlus.getSupport());
241 std::cout << endl << string(60,'-') << endl << "add(f1,f2) :" << endl << endl;
242 affiche_fieldT(myFieldadd, myFieldadd->getSupport());
243 std::cout << endl << string(60,'-') << endl << "scalarProduct(f1,f2) :" << endl << endl;
244 affiche_fieldT(myFieldDot, myFieldDot->getSupport());
245 std::cout << endl << string(60,'-') << endl << " - f1 :" << endl << endl;
246 affiche_fieldT(&myFieldNeg, myFieldNeg.getSupport());
248 medModeSwitch mode=myFieldPlus.getvalue()->getMode();
249 int size=myFieldPlus.getNumberOfValues()*myFieldPlus.getNumberOfComponents();
251 std::cout << endl << string(60,'-') << endl << "Tests opérations :" << endl << endl;
252 affiche_valeur_field(" f1 :", size, *myField1);
253 affiche_valeur_field(" f2 :", size, *myField2);
254 std::cout << endl << " " << string(4*size,'-');
256 affiche_valeur_field(" + :", size, myFieldPlus);
257 affiche_valeur_field(" add :", size, *myFieldadd);
258 affiche_valeur_field(" - :", size, myFieldMoins);
259 affiche_valeur_field(" sub :", size, *myFieldsub);
260 affiche_valeur_field(" * :", size, myFieldFois);
261 affiche_valeur_field(" mul :", size, *myFieldmul);
262 affiche_valeur_field(" / :", size, myFieldDiv);
263 affiche_valeur_field(" div :", size, *myFielddiv);
264 affiche_valeur_field("f1+f2*f1:", size, myFieldAsso);
265 affiche_valeur_field(" - f1 :", size, myFieldNeg);
269 myField1->applyLin(1,1);
270 affiche_valeur_field(" f1+1 :", size, *myField1);
271 myField1->applyLin(1,-1);
272 affiche_valeur_field(" -> f1 :", size, *myField1);
276 myField1->applyFunc<myfunction1>();
277 affiche_valeur_field(" CB 2f1 :", size, *myField1);
278 myField1->applyFunc<myfunction2>();
279 affiche_valeur_field(" -> f1 :", size, *myField1);
283 *myField1 += *myField2;
284 affiche_valeur_field(" f1+=f2 :", size, *myField1);
287 *myField1 *= *myField2;
288 affiche_valeur_field(" f1*=f2 :", size, *myField1);
291 *myField1 /= *myField2;
292 affiche_valeur_field(" f1/=f2 :", size, *myField1);
295 *myField1 -= *myField2;
296 affiche_valeur_field(" f1-=f2 :", size, *myField1);
298 std::cout << endl << endl;
306 // delete myField1_vol;