1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "MEDMEM_Exception.hxx"
23 #include "MEDMEM_Mesh.hxx"
24 #include "MEDMEM_Family.hxx"
25 #include "MEDMEM_Group.hxx"
27 #include "MEDMEM_MedMeshDriver.hxx"
28 #include "MEDMEM_MedFieldDriver.hxx"
29 #include "MEDMEM_Support.hxx"
30 #include "MEDMEM_Field.hxx"
31 #include "MEDMEM_define.hxx"
38 using namespace MEDMEM;
39 using namespace MED_EN;
41 int myfunction1(int x);
42 int myfunction1(int x)
47 int myfunction2(int x);
48 int myfunction2(int x)
54 static void affiche_field_(FIELD_ * myField, const SUPPORT * mySupport)
56 cout << "Field "<< myField->getName() << " : " <<myField->getDescription() << endl ;
57 int NumberOfComponents = myField->getNumberOfComponents() ;
58 cout << "- Nombre de composantes : "<< NumberOfComponents << endl ;
59 cout << "- Nombre de valeurs : "<< myField->getNumberOfValues() << endl ;
60 for (int i=1; i<NumberOfComponents+1; i++)
62 cout << " - composante "<<i<<" :"<<endl ;
63 cout << " - nom : "<<myField->getComponentName(i)<< endl;
64 cout << " - description : "<<myField->getComponentDescription(i) << endl;
65 cout << " - units : "<<myField->getMEDComponentUnit(i) << endl;
67 cout << "- iteration :" << endl ;
68 cout << " - numero : " << myField->getIterationNumber()<< endl ;
69 cout << " - ordre : " << myField->getOrderNumber()<< endl ;
70 cout << " - temps : " << myField->getTime()<< endl ;
72 cout << "- Type : " << myField->getValueType()<< endl;
74 cout << "- Adresse support : " << mySupport << endl;
77 static void affiche_fieldT(FIELD<int> * myField, const SUPPORT * mySupport)
79 affiche_field_((FIELD_ *) myField, mySupport);
81 cout << "- Valeurs :"<<endl;
82 int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
83 int NumberOfComponents = myField->getNumberOfComponents() ;
85 for (int i=1; i<NumberOf+1; i++)
87 const int * value = myField->getRow(i) ;
88 for (int j=0; j<NumberOfComponents; j++)
89 cout << value[j]<< " ";
92 std::cout << std::endl;
93 std::cout << "Norme euclidienne : " << myField->norm2() << endl;
94 std::cout << "Norme max : " << myField->normMax() << endl;
97 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
98 std::cout << "Norme L2 - comp=" << i << " : " << myField->normL2(i) << endl;
99 std::cout << "Norme L2 : " << myField->normL2() << endl;
101 for (int i=1; i<=myField->getNumberOfComponents(); ++i)
102 std::cout << "Norme L1 - comp=" << i << " : " << myField->normL1(i) << endl;
103 std::cout << "Norme L1 : " << myField->normL1() << endl;
105 catch (MEDEXCEPTION &ex)
107 std::cout << ex.what() << std::endl;
111 static void affiche_valeur_field(const char * intitule, const int taille, const FIELD<int>& f)
113 const int * value=f.getValue();
114 std::cout << endl << intitule;
115 for(int i=0;i<taille;i++)
116 std::cout << setw(3) << value[i] << " ";
119 int main (int argc, char ** argv)
123 cerr << "Usage : " << argv[0]
124 << " filename meshname fieldname" << endl << endl;
127 string filename = argv[1] ;
128 string meshname = argv[2] ;
129 string fieldname = argv[3];
131 MESH * myMesh = new MESH(MED_DRIVER,filename,meshname);
132 const SUPPORT * mySupport;
133 FIELD<int> * myField1;
136 /* read MESH, SUPPORT and FIELD */
137 mySupport = myMesh->getSupportOnAll(MED_CELL);
138 myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
140 catch (MEDEXCEPTION &ex)
142 mySupport = myMesh->getSupportOnAll(MED_NODE);
145 myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
146 myField1->setValueIJ(10,1,-9); // pour tester les normes max avec une valeur negative
150 cout << "Field int " << fieldname << " not found !!!" << endl ;
155 FIELD<int> * myField2 = new FIELD<int>(* myField1);
156 //myField1->setNumberOfValues(16); // PROVISOIRE !! BUG
157 //myField2->setNumberOfValues(16); // PROVISOIRE !! BUG
158 // FIELD<int>* myField1_vol=myField1->getSupport()->getMesh()->getVolume(myField1->getSupport());
159 // affiche_fieldT(myField1_vol, myField1->getSupport());
161 affiche_fieldT(myField1, myField1->getSupport());
162 std::cout << endl << string(60,'-') << endl;
163 affiche_fieldT(myField2, myField2->getSupport());
165 // Verifie plusieurs cas de non compatibilité
167 // Unites non compatibles
168 const string unite=myField1->getMEDComponentUnit(1);
169 myField1->setMEDComponentUnit(1,string("UniteBidon"));
172 std::cout << endl << string(60,'-') << endl;
173 std::cout<< "Test incompatibilité d'unité :" << endl;
174 FIELD<int> *myFieldPlus = *myField1 + *myField2;
175 myFieldPlus->removeReference();
177 catch (MEDEXCEPTION & ex)
179 std::cout << "MEDEXCEPTION : " << ex.what() << endl;
180 myField1->setMEDComponentUnit(1,unite);
183 // numberOfComponents non compatibles
184 const int numberOfComponents =myField1->getNumberOfComponents();
185 myField1->setNumberOfComponents(4);
188 std::cout << endl << string(60,'-') << endl;
189 std::cout<< "Test incompatibilité nombre de composantes :" << endl;
190 FIELD<int> *myFieldPlus = *myField1 + *myField2;
191 myFieldPlus->removeReference();
193 catch (MEDEXCEPTION & ex)
195 std::cout << ex.what() << endl;
196 myField1->setNumberOfComponents(numberOfComponents);
199 // supports non compatibles
200 const SUPPORT *mySupport2=myMesh->getSupportOnAll(MED_NODE);
201 myField1->setSupport(mySupport2);
204 std::cout << endl << string(60,'-') << endl;
205 std::cout<< "Test incompatibilité des supports :" << endl;
206 FIELD<int> *myFieldPlus = *myField1 + *myField2;
207 myFieldPlus->removeReference();
209 catch (MEDEXCEPTION & ex)
211 std::cout << ex.what() << endl;
212 myField1->setSupport( myField2->getSupport() );
215 // champs de taille nulle
216 myField1->setNumberOfComponents(0);
217 myField2->setNumberOfComponents(0);
220 std::cout << endl << string(60,'-') << endl;
221 std::cout<< "Test incompatibilité taille nulle :" << endl;
222 FIELD<int> *myFieldPlus = *myField1 + *myField2;
223 myFieldPlus->removeReference();
225 catch (MEDEXCEPTION & ex)
227 std::cout << ex.what() << endl;
233 catch (MEDEXCEPTION & ex)
235 std::cout << ex.what() << endl;
236 myField1->setNumberOfComponents(numberOfComponents);
237 myField2->setNumberOfComponents(numberOfComponents);
240 // Apres toutes ces exceptions, des opérations qui marchent!
242 FIELD<int> *myFieldPlus = *myField1 + *myField2;
243 FIELD<int> *myFieldMoins = *myField1 - *myField2;
244 FIELD<int> *myFieldNeg = -(*myField1);
245 FIELD<int> *myFieldFois = *myField1 * *myField2;
246 FIELD<int> *myFieldDiv = *myField1 / *myField2;
247 FIELD<int> *myFieldAsso = (*myField1)+*((*myField2)*(*myField2));
248 FIELD<int>* myFieldadd = FIELD<int>::add(*myField1, *myField2);
249 FIELD<int>* myFieldsub = FIELD<int>::sub(*myField1, *myField2);
250 FIELD<int>* myFieldmul = FIELD<int>::mul(*myField1, *myField2);
251 FIELD<int>* myFielddiv = FIELD<int>::div(*myField1, *myField2);
252 FIELD<int>* myFieldDot = FIELD<int>::scalarProduct(*myField1, *myField2);
254 std::cout << endl << string(60,'-') << endl << "f1+f2 :" << endl << endl;
255 affiche_fieldT(myFieldPlus, myFieldPlus->getSupport());
256 std::cout << endl << string(60,'-') << endl << "add(f1,f2) :" << endl << endl;
257 affiche_fieldT(myFieldadd, myFieldadd->getSupport());
258 std::cout << endl << string(60,'-') << endl << "scalarProduct(f1,f2) :" << endl << endl;
259 affiche_fieldT(myFieldDot, myFieldDot->getSupport());
260 std::cout << endl << string(60,'-') << endl << " - f1 :" << endl << endl;
261 affiche_fieldT(myFieldNeg, myFieldNeg->getSupport());
262 int size=myFieldPlus->getNumberOfValues()*myFieldPlus->getNumberOfComponents();
264 std::cout << endl << string(60,'-') << endl << "Tests opérations :" << endl << endl;
265 affiche_valeur_field(" f1 :", size, *myField1);
266 affiche_valeur_field(" f2 :", size, *myField2);
267 std::cout << endl << " " << string(4*size,'-');
269 affiche_valeur_field(" + :", size, *myFieldPlus);
270 affiche_valeur_field(" add :", size, *myFieldadd);
271 affiche_valeur_field(" - :", size, *myFieldMoins);
272 affiche_valeur_field(" sub :", size, *myFieldsub);
273 affiche_valeur_field(" * :", size, *myFieldFois);
274 affiche_valeur_field(" mul :", size, *myFieldmul);
275 affiche_valeur_field(" / :", size, *myFieldDiv);
276 affiche_valeur_field(" div :", size, *myFielddiv);
277 affiche_valeur_field("f1+f2*f1:", size, *myFieldAsso);
278 affiche_valeur_field(" - f1 :", size, *myFieldNeg);
280 myFieldPlus->removeReference();
281 myFieldMoins->removeReference();
282 myFieldFois->removeReference();
283 myFieldDiv->removeReference();
284 myFieldAsso->removeReference();
285 myFieldNeg->removeReference();
289 myField1->applyLin(1,1);
290 affiche_valeur_field(" f1+1 :", size, *myField1);
291 myField1->applyLin(1,-1);
292 affiche_valeur_field(" -> f1 :", size, *myField1);
296 myField1->applyFunc<myfunction1>();
297 affiche_valeur_field(" CB 2f1 :", size, *myField1);
298 myField1->applyFunc<myfunction2>();
299 affiche_valeur_field(" -> f1 :", size, *myField1);
303 *myField1 += *myField2;
304 affiche_valeur_field(" f1+=f2 :", size, *myField1);
307 *myField1 *= *myField2;
308 affiche_valeur_field(" f1*=f2 :", size, *myField1);
311 *myField1 /= *myField2;
312 affiche_valeur_field(" f1/=f2 :", size, *myField1);
315 *myField1 -= *myField2;
316 affiche_valeur_field(" f1-=f2 :", size, *myField1);
318 std::cout << endl << endl;
321 myFieldadd->removeReference();
322 myFieldsub->removeReference();
323 myFieldmul->removeReference();
324 myFielddiv->removeReference();
325 myFieldDot->removeReference();
326 // myField1_vol->removeReference();
328 myField1->removeReference();
329 myField2->removeReference();
330 mySupport->removeReference();
331 myMesh->removeReference();