Back to index

salome-med  6.5.0
test_operation_fieldint.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 #include "MEDMEM_Exception.hxx"
00023 #include "MEDMEM_Mesh.hxx"
00024 #include "MEDMEM_Family.hxx"
00025 #include "MEDMEM_Group.hxx"
00026 
00027 #include "MEDMEM_MedMeshDriver.hxx"
00028 #include "MEDMEM_MedFieldDriver.hxx"
00029 #include "MEDMEM_Support.hxx"
00030 #include "MEDMEM_Field.hxx"
00031 #include "MEDMEM_define.hxx"
00032 
00033 #include <string>
00034 #include <iostream>
00035 #include <iomanip>
00036 #include <cmath>
00037 
00038 using namespace MEDMEM;
00039 using namespace MED_EN;
00040 
00041 int myfunction1(int x);
00042 int myfunction1(int x)
00043 {
00044   return 2*x;
00045 }
00046 
00047 int myfunction2(int x);
00048 int myfunction2(int x)
00049 {
00050   return x/2;
00051 }
00052 
00053 using namespace std;
00054 static void affiche_field_(FIELD_ * myField, const SUPPORT * mySupport)
00055 {
00056   cout << "Field "<< myField->getName() << " : " <<myField->getDescription() <<  endl ;
00057   int NumberOfComponents = myField->getNumberOfComponents() ;
00058   cout << "- Nombre de composantes : "<< NumberOfComponents << endl ;
00059   cout << "- Nombre de valeurs     : "<< myField->getNumberOfValues() << endl ;
00060   for (int i=1; i<NumberOfComponents+1; i++) 
00061     {
00062       cout << "  - composante "<<i<<" :"<<endl ;
00063       cout << "      - nom         : "<<myField->getComponentName(i)<< endl;
00064       cout << "      - description : "<<myField->getComponentDescription(i) << endl;
00065       cout << "      - units       : "<<myField->getMEDComponentUnit(i) << endl;
00066     }
00067   cout << "- iteration :" << endl ;
00068   cout << "    - numero : " << myField->getIterationNumber()<< endl  ;
00069   cout << "    - ordre  : " << myField->getOrderNumber()<< endl  ;
00070   cout << "    - temps  : " << myField->getTime()<< endl  ;
00071 
00072   cout << "- Type : " << myField->getValueType()<< endl;
00073 
00074   cout << "- Adresse support : " << mySupport << endl;
00075 }
00076 
00077 static void affiche_fieldT(FIELD<int> * myField, const SUPPORT * mySupport)
00078 {
00079   affiche_field_((FIELD_ *) myField, mySupport);
00080 
00081   cout << "- Valeurs :"<<endl;
00082   int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
00083   int NumberOfComponents = myField->getNumberOfComponents() ;
00084 
00085   for (int i=1; i<NumberOf+1; i++) 
00086     {
00087       const int * value = myField->getRow(i) ;
00088       for (int j=0; j<NumberOfComponents; j++)
00089         cout << value[j]<< " ";
00090       cout<<endl;
00091     }
00092   std::cout << std::endl;
00093   std::cout << "Norme euclidienne : " << myField->norm2() << endl;
00094   std::cout << "Norme max         : " << myField->normMax() << endl;
00095   try
00096     {
00097       for (int i=1; i<=myField->getNumberOfComponents(); ++i)
00098         std::cout << "Norme L2 - comp=" << i << " : " << myField->normL2(i) << endl;
00099       std::cout << "Norme L2          : " << myField->normL2() << endl;
00100 
00101       for (int i=1; i<=myField->getNumberOfComponents(); ++i)
00102         std::cout << "Norme L1 - comp=" << i << " : " << myField->normL1(i) << endl;
00103       std::cout << "Norme L1          : " << myField->normL1() << endl;
00104     }
00105   catch (MEDEXCEPTION &ex)
00106     {
00107       std::cout << ex.what() << std::endl;
00108     }
00109 }
00110 
00111 static void affiche_valeur_field(const char * intitule, const int taille, const FIELD<int>& f)
00112 {
00113   const int * value=f.getValue();
00114   std::cout << endl << intitule;
00115   for(int i=0;i<taille;i++)
00116     std::cout << setw(3) << value[i] << " ";
00117 }
00118 
00119 int main (int argc, char ** argv)
00120 {
00121   if (argc != 4)
00122     {
00123       cerr << "Usage : " << argv[0] 
00124            << " filename meshname fieldname" << endl << endl;
00125       exit(-1);
00126     }
00127   string filename = argv[1] ;
00128   string meshname = argv[2] ;
00129   string fieldname = argv[3];
00130 
00131   MESH * myMesh = new MESH(MED_DRIVER,filename,meshname);
00132   const SUPPORT * mySupport;
00133   FIELD<int> * myField1;
00134   try
00135     {
00136       /* read MESH, SUPPORT and FIELD */
00137       mySupport = myMesh->getSupportOnAll(MED_CELL);
00138       myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
00139     }
00140   catch (MEDEXCEPTION &ex)
00141     {
00142       mySupport = myMesh->getSupportOnAll(MED_NODE);
00143       try
00144         {
00145           myField1 = new FIELD<int>(mySupport,MED_DRIVER,filename,fieldname) ;
00146           myField1->setValueIJ(10,1,-9); // pour tester les normes max avec une valeur negative
00147         }
00148       catch (...)
00149         {
00150           cout << "Field int " << fieldname << " not found !!!" << endl ;
00151           exit (-1) ;
00152         }
00153     }
00154 
00155     FIELD<int> * myField2 = new FIELD<int>(* myField1);
00156     //myField1->setNumberOfValues(16); // PROVISOIRE !! BUG
00157     //myField2->setNumberOfValues(16); // PROVISOIRE !! BUG
00158 //      FIELD<int>* myField1_vol=myField1->getSupport()->getMesh()->getVolume(myField1->getSupport());
00159 //      affiche_fieldT(myField1_vol, myField1->getSupport());
00160 
00161     affiche_fieldT(myField1, myField1->getSupport());
00162     std::cout <<  endl << string(60,'-') << endl;
00163     affiche_fieldT(myField2, myField2->getSupport());
00164 
00165     // Verifie plusieurs cas de non compatibilité 
00166 
00167     // Unites non compatibles
00168     const string unite=myField1->getMEDComponentUnit(1);
00169     myField1->setMEDComponentUnit(1,string("UniteBidon"));
00170     try
00171     {
00172         std::cout << endl << string(60,'-') << endl;
00173         std::cout<< "Test incompatibilité d'unité :" << endl;
00174         FIELD<int> *myFieldPlus = *myField1 + *myField2;
00175         myFieldPlus->removeReference();
00176     }
00177     catch (MEDEXCEPTION & ex)
00178     {
00179         std::cout << "MEDEXCEPTION : " << ex.what() << endl;
00180         myField1->setMEDComponentUnit(1,unite);
00181     }
00182 
00183     // numberOfComponents non compatibles
00184     const int numberOfComponents =myField1->getNumberOfComponents();
00185     myField1->setNumberOfComponents(4);
00186     try
00187     {
00188         std::cout << endl << string(60,'-') << endl;
00189         std::cout<< "Test incompatibilité nombre de composantes :" << endl;
00190         FIELD<int> *myFieldPlus = *myField1 + *myField2;
00191         myFieldPlus->removeReference();
00192     }
00193     catch (MEDEXCEPTION & ex)
00194     {
00195         std::cout << ex.what() << endl;
00196         myField1->setNumberOfComponents(numberOfComponents);
00197     }
00198 
00199     // supports non compatibles
00200     const SUPPORT *mySupport2=myMesh->getSupportOnAll(MED_NODE);
00201     myField1->setSupport(mySupport2);
00202     try
00203     {
00204         std::cout << endl << string(60,'-') << endl;
00205         std::cout<< "Test incompatibilité des supports :" << endl;
00206         FIELD<int> *myFieldPlus = *myField1 + *myField2;
00207         myFieldPlus->removeReference();
00208     }
00209     catch (MEDEXCEPTION & ex)
00210     {
00211         std::cout << ex.what() << endl;
00212         myField1->setSupport( myField2->getSupport() );
00213     }
00214 
00215     // champs de taille nulle
00216     myField1->setNumberOfComponents(0);
00217     myField2->setNumberOfComponents(0);
00218     try
00219     {
00220         std::cout << endl << string(60,'-') << endl;
00221         std::cout<< "Test incompatibilité taille nulle :" << endl;
00222         FIELD<int> *myFieldPlus = *myField1 + *myField2;
00223         myFieldPlus->removeReference();
00224     }
00225     catch (MEDEXCEPTION & ex)
00226     {
00227         std::cout << ex.what() << endl;
00228     }
00229     try
00230     {
00231         myField1->norm2();
00232     }
00233     catch (MEDEXCEPTION & ex)
00234     {
00235         std::cout << ex.what() << endl;
00236         myField1->setNumberOfComponents(numberOfComponents);
00237         myField2->setNumberOfComponents(numberOfComponents);
00238     }
00239 
00240     // Apres toutes ces exceptions, des opérations qui marchent!
00241 
00242     FIELD<int> *myFieldPlus = *myField1 + *myField2;
00243     FIELD<int> *myFieldMoins = *myField1 - *myField2;
00244     FIELD<int> *myFieldNeg = -(*myField1);
00245     FIELD<int> *myFieldFois = *myField1 * *myField2;
00246     FIELD<int> *myFieldDiv = *myField1 / *myField2;
00247     FIELD<int> *myFieldAsso = (*myField1)+*((*myField2)*(*myField2));
00248     FIELD<int>* myFieldadd = FIELD<int>::add(*myField1, *myField2);
00249     FIELD<int>* myFieldsub = FIELD<int>::sub(*myField1, *myField2);
00250     FIELD<int>* myFieldmul = FIELD<int>::mul(*myField1, *myField2);
00251     FIELD<int>* myFielddiv = FIELD<int>::div(*myField1, *myField2);
00252     FIELD<int>* myFieldDot = FIELD<int>::scalarProduct(*myField1, *myField2);
00253 
00254     std::cout <<  endl << string(60,'-') << endl << "f1+f2 :" << endl << endl;
00255     affiche_fieldT(myFieldPlus, myFieldPlus->getSupport());
00256     std::cout <<  endl << string(60,'-') << endl << "add(f1,f2) :" << endl << endl;
00257     affiche_fieldT(myFieldadd, myFieldadd->getSupport());
00258     std::cout <<  endl << string(60,'-') << endl << "scalarProduct(f1,f2) :" << endl << endl;
00259     affiche_fieldT(myFieldDot, myFieldDot->getSupport());
00260     std::cout <<  endl << string(60,'-') << endl << " - f1 :" << endl << endl;
00261     affiche_fieldT(myFieldNeg, myFieldNeg->getSupport());
00262     int size=myFieldPlus->getNumberOfValues()*myFieldPlus->getNumberOfComponents();
00263   
00264     std::cout <<  endl << string(60,'-') << endl << "Tests opérations :" << endl << endl;
00265     affiche_valeur_field("  f1    :", size, *myField1);
00266     affiche_valeur_field("  f2    :", size, *myField2);
00267     std::cout << endl << "        " << string(4*size,'-');
00268 
00269     affiche_valeur_field("  +     :", size, *myFieldPlus);
00270     affiche_valeur_field(" add    :", size, *myFieldadd);
00271     affiche_valeur_field("  -     :", size, *myFieldMoins);
00272     affiche_valeur_field(" sub    :", size, *myFieldsub);
00273     affiche_valeur_field("  *     :", size, *myFieldFois);
00274     affiche_valeur_field(" mul    :", size, *myFieldmul);
00275     affiche_valeur_field("  /     :", size, *myFieldDiv);
00276     affiche_valeur_field(" div    :", size, *myFielddiv);
00277     affiche_valeur_field("f1+f2*f1:", size, *myFieldAsso);
00278     affiche_valeur_field("  - f1  :", size, *myFieldNeg);
00279 
00280     myFieldPlus->removeReference();
00281     myFieldMoins->removeReference();
00282     myFieldFois->removeReference();
00283     myFieldDiv->removeReference();
00284     myFieldAsso->removeReference();
00285     myFieldNeg->removeReference();
00286 
00287     // Test applyLin
00288     std::cout << endl;
00289     myField1->applyLin(1,1);
00290     affiche_valeur_field(" f1+1 :", size, *myField1);
00291     myField1->applyLin(1,-1);
00292     affiche_valeur_field(" -> f1  :", size, *myField1);
00293     
00294     // Test applyFunc
00295     std::cout << endl;
00296     myField1->applyFunc<myfunction1>();
00297     affiche_valeur_field(" CB 2f1 :", size, *myField1);
00298     myField1->applyFunc<myfunction2>();
00299     affiche_valeur_field(" -> f1  :", size, *myField1);
00300 
00301     // Test operateur +=
00302     std::cout << endl;
00303     *myField1 += *myField2;
00304     affiche_valeur_field(" f1+=f2 :", size, *myField1);
00305 
00306     // Test operateur *=
00307     *myField1 *= *myField2;
00308     affiche_valeur_field(" f1*=f2 :", size, *myField1);
00309 
00310     // Test operateur /=
00311     *myField1 /= *myField2;
00312     affiche_valeur_field(" f1/=f2 :", size, *myField1);
00313 
00314     // Test operateur -=
00315     *myField1 -= *myField2;
00316     affiche_valeur_field(" f1-=f2 :", size, *myField1);
00317 
00318     std::cout << endl << endl; 
00319 
00320 
00321     myFieldadd->removeReference();
00322     myFieldsub->removeReference();
00323     myFieldmul->removeReference();
00324     myFielddiv->removeReference();
00325     myFieldDot->removeReference();
00326 //    myField1_vol->removeReference();
00327 
00328     myField1->removeReference();
00329     myField2->removeReference();
00330     mySupport->removeReference();
00331     myMesh->removeReference();
00332     return 0;
00333 }