Back to index

salome-med  6.5.0
test_MEDMEM_nArray.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 <stdlib.h>
00023 #include <iostream>
00024 #include <stdio.h>
00025 
00026 #include "MEDMEM_ArrayInterface.hxx"
00027 #include "MEDMEM_Field.hxx"
00028 #include "MEDMEM_IndexCheckingPolicy.hxx"
00029 #include "MEDMEM_ArrayConvert.hxx"
00030 
00031 using namespace MEDMEM;
00032 
00033 int main(void) {
00034 
00035   const int mdim = 2;
00036   const int nbelem1 = 20;
00037 
00038   // TEST n° 1  FullInterlace et NoGauss
00039   std::cout << std::endl;
00040   std::cout << "Test de MEDMEM_Array en FullInterlace et NoGauss : " << std::endl;
00041   std::cout << std::endl;
00042 
00043   bool test1ok = true;
00044   const double * myArray1Ptr = 0;
00045   const double array1Ref[]  = { 11 , 12 , 21 , 22 , 31 , 32 , 41 , 42 , 51 , 52 ,
00046                                61 , 62 , 71 , 72 , 81 , 82 , 91 , 92 , 101 , 102 ,
00047                                111 , 112 , 121 , 122 , 131 , 132 , 141 , 142 ,
00048                                151 , 152 , 161 , 162 , 171 , 172 , 181 , 182 ,
00049                                191 , 192 , 201 , 202 };
00050 
00051   const double array2Ref[] = { 11 , 21 , 31 , 41 , 51 , 61 , 71 , 81 , 91 , 101
00052                                , 111 , 121 , 131 , 141 , 151 , 161 , 171 , 181
00053                                , 191 , 201 ,
00054                                12 , 22 , 32 , 42 , 52 , 62 , 72 , 82 , 92 , 102
00055                                , 112 , 122 , 132 , 142 , 152 , 162 , 172 , 182
00056                                , 192 , 202 };
00057 
00058   MEDMEM_Array<double>  myArray1(mdim,nbelem1);
00059 
00060   for (int i =1; i <= nbelem1; i++)
00061     for (int j =1; j <= mdim; j++)
00062       myArray1.setIJ(i,j,i*10+j);
00063 
00064   myArray1Ptr = myArray1.getPtr();
00065   for (int i =0; i < mdim*nbelem1; i++)
00066         test1ok = test1ok && ( myArray1Ptr[i] == array1Ref[i] );
00067 
00068   std::cout << "- Egualité de myArray1 et de myArray1Ref : " << test1ok << std::endl ;
00069 
00070   test1ok = true;
00071   MEDMEM_Array<double> myArray1bis(myArray1, false);
00072   myArray1Ptr = myArray1bis.getPtr();
00073   for (int i =0; i < mdim*nbelem1; i++)
00074         test1ok = test1ok && ( myArray1Ptr[i] == array1Ref[i] );
00075   std::cout << "- Constructeur par recopie en FullInterlace et NoGauss : " << test1ok << std::endl;
00076 
00077   test1ok = true;
00078   MEDMEM_Array<double> myArray1ter;
00079   myArray1ter = myArray1;
00080   myArray1Ptr = myArray1ter.getPtr();
00081   for (int i =0; i < mdim*nbelem1; i++)
00082         test1ok = test1ok && ( myArray1Ptr[i] == array1Ref[i] );
00083   std::cout << "- Opérateur d'affectation en FullInterlace et NoGauss : " << test1ok << std::endl;
00084 
00085 
00086   test1ok = true;
00087   MEDMEM_Array<double> myArray1qua(mdim,nbelem1);
00088   myArray1Ptr = myArray1qua.getPtr();
00089 
00090   for (int i =1; i <= nbelem1; i++)
00091     myArray1qua.setRow(i,&array1Ref[(i-1)*mdim]);
00092 
00093   for (int i =0; i < mdim*nbelem1; i++)
00094         test1ok = test1ok && ( myArray1Ptr[i] == array1Ref[i] );
00095   std::cout << "- Méthode setRow en FullInterlace et NoGauss : " << test1ok << std::endl;
00096 
00097   std::cout << "- Test de levée d'exception pour getColumn : ";
00098   try {
00099     myArray1qua.getColumn(1);
00100   }
00101   catch (MEDMEM::MEDEXCEPTION &m) {
00102     std::cout << m.what() << endl ;
00103   }
00104 
00105   test1ok = true;
00106   MEDMEM_Array<double,NoInterlaceNoGaussPolicy> * myArray1cin = ArrayConvert(myArray1);
00107   myArray1Ptr = myArray1cin->getPtr();
00108   int elemno = 0;
00109   for (int j=1; j <= mdim; j++)
00110     for (int  i=1; i <= nbelem1 ; i++ ) {
00111       test1ok = test1ok
00112         && (  myArray1cin->getIJ(i,j) ==  array2Ref[elemno] )
00113         && (  myArray1Ptr[elemno]     ==  array2Ref[elemno] );
00114       elemno++;
00115     }
00116   std::cout << "- Convertion du mode FullInterlaceNoGaussPolicy au mode NoInterlaceNoGaussPolicy : "
00117             << test1ok << std::endl;
00118 
00119 
00120 
00121   // TEST n°2   NoInterlace et NoGauss
00122   std::cout << std::endl;
00123   std::cout << "Test de MEDMEM_Array en NoInterlace et NoGauss : " << std::endl;
00124   std::cout << std::endl;
00125 
00126   bool test2ok = true;
00127   const double * myArray2Ptr = 0;
00128 
00129   MEDMEM_Array<double,NoInterlaceNoGaussPolicy> myArray2(mdim,nbelem1);
00130 
00131   for (int i =1; i <= nbelem1; i++)
00132     for (int j =1; j <= mdim; j++)
00133       myArray2.setIJ(i,j,i*10+j);
00134 
00135   myArray2Ptr = myArray2.getPtr();
00136   for (int i =0; i < mdim*nbelem1; i++)
00137         test2ok = test2ok && ( myArray2Ptr[i] == array2Ref[i] );
00138 
00139   std::cout << "- Egualité de myArray2 et de myArray2Ref : " << test2ok << std::endl ;
00140 
00141   test2ok = true;
00142   MEDMEM_Array<double,NoInterlaceNoGaussPolicy> myArray2bis(myArray2, false);
00143   myArray2Ptr = myArray2bis.getPtr();
00144   for (int i =0; i < mdim*nbelem1; i++)
00145         test2ok = test2ok && ( myArray2Ptr[i] == array2Ref[i] );
00146   std::cout << "- Constructeur par recopie en NoInterlace et NoGauss : " << test2ok << std::endl;
00147 
00148   test2ok = true;
00149   MEDMEM_Array<double,NoInterlaceNoGaussPolicy> myArray2ter;
00150   myArray2ter = myArray2;
00151   myArray2Ptr = myArray2ter.getPtr();
00152   for (int i =0; i < mdim*nbelem1; i++)
00153         test2ok = test2ok && ( myArray2Ptr[i] == array2Ref[i] );
00154   std::cout << "- Opérateur d'affectation en NoInterlace et NoGauss : " << test2ok << std::endl;
00155 
00156   test2ok = true;
00157   MEDMEM_Array<double,NoInterlaceNoGaussPolicy> myArray2qua(mdim,nbelem1);
00158   myArray2Ptr = myArray2qua.getPtr();
00159 
00160   for (int j =1; j <= mdim; j++)
00161     myArray2qua.setColumn(j,&array2Ref[nbelem1*(j-1)]);
00162 
00163   for (int i =0; i < mdim*nbelem1; i++)
00164         test2ok = test2ok && ( myArray2Ptr[i] == array2Ref[i] );
00165   std::cout << "- Méthode setColumn en NoInterlace et NoGauss : " << test2ok << std::endl;
00166 
00167   std::cout << "- Test de levée d'exception pour getRow : " ;
00168   try {
00169     myArray2qua.getRow(1);
00170   }
00171   catch (MEDMEM::MEDEXCEPTION &m) {
00172     std::cout << m.what() << endl ;
00173   }
00174 
00175   test2ok = true;
00176   MEDMEM_Array<double,FullInterlaceNoGaussPolicy> * myArray2cin = ArrayConvert(myArray2);
00177   myArray2Ptr = myArray2cin->getPtr();
00178   elemno = 0;
00179   for (int  i=1; i <= nbelem1 ; i++ )
00180     for (int j=1; j <= mdim; j++) {
00181       test2ok = test2ok
00182         && (  myArray2cin->getIJ(i,j) ==  array1Ref[elemno] )
00183         && (  myArray2Ptr[elemno]     ==  array1Ref[elemno] );
00184       elemno++;
00185     }
00186   std::cout << "- Convertion du mode NoInterlaceNoGaussPolicy au mode FullInterlaceNoGaussPolicy : "
00187             << test2ok << std::endl;
00188 
00189 
00190   // TEST n°3   FullInterlace et Gauss
00191   const int nbelem2 = 5;
00192   const int nbtypegeo = 2;
00193   const int nbelgeoc[nbtypegeo+1]   = {1,3,6};
00194   const int nbgaussgeo[nbtypegeo+1] = {-1,2,3};
00195  
00196   std::cout << std::endl;
00197   std::cout << "Test de MEDMEM_Array en FullInterlace et Gauss : " << std::endl;
00198   std::cout << std::endl;
00199 
00200   bool test3ok = true;
00201   const double * myArray3Ptr = 0;
00202   const double array3Ref[] = {1.11 , 1.12 , 1.21 , 1.22 , 
00203                               2.11 , 2.12 , 2.21 , 2.22 , 
00204                               13.11 , 13.12 , 13.21 , 13.22 , 13.31 , 13.32 , 
00205                               14.11 , 14.12 , 14.21 , 14.22 , 14.31 , 14.32 , 
00206                               15.11 , 15.12 , 15.21 , 15.22 , 15.31 , 15.32 };
00207 
00208   const double array4Ref[] = { 1.11 , 1.21 , 2.11 , 2.21,
00209                                13.11 , 13.21 , 13.31 , 14.11 , 14.21 , 14.31 ,
00210                                15.11 , 15.21 , 15.31 ,
00211                                1.12 , 1.22 , 2.12 , 2.22 ,
00212                                13.12 , 13.22 , 13.32 , 14.12 , 14.22 , 14.32 ,
00213                                15.12 , 15.22 , 15.32 };
00214 
00215   MEDMEM_Array<double,FullInterlaceGaussPolicy> myArray3(mdim,nbelem2,nbtypegeo,nbelgeoc,nbgaussgeo);
00216 
00217   elemno = 1;
00218    for (int ntyp=1; ntyp <= nbtypegeo; ntyp++ ) {
00219     for (int  i=0; i < (nbelgeoc[ntyp]-nbelgeoc[ntyp-1]) ; i++ ) {
00220       for (int k=1; k <= nbgaussgeo[ntyp]; k++)
00221         for (int j=1; j <= mdim; j++) {
00222           myArray3.setIJK(elemno,j,k,elemno+(ntyp-1)*10+0.1*k+0.01*j);
00223         }
00224       elemno++;
00225     }
00226   }
00227   myArray3Ptr = myArray3.getPtr();
00228   for (int i =0; i < myArray3.getArraySize(); i++)
00229     test3ok = test3ok && ( myArray3Ptr[i] == array3Ref[i] );
00230 
00231   std::cout << "- Egualité de myArray3 et de myArray3Ref : " << test3ok << std::endl ;
00232 
00233   test3ok = true;
00234   MEDMEM_Array<double,FullInterlaceGaussPolicy> myArray3bis(myArray3, false);
00235   myArray3Ptr = myArray3bis.getPtr();
00236 
00237   elemno = 0;
00238   for (int  i=1; i < nbelgeoc[nbtypegeo] ; i++ )
00239     for (int k=1; k <= myArray3bis.getNbGauss(i); k++)
00240       for (int j=1; j <= mdim; j++) {
00241         test3ok = test3ok
00242           && (  myArray3bis.getIJK(i,j,k)    ==  array3Ref[elemno] )
00243           && (  myArray3Ptr[elemno]          ==  array3Ref[elemno] );
00244         elemno++;
00245       }
00246   std::cout << "- Constructeur par recopie en FullInterlace et Gauss : " << test3ok << std::endl;
00247 
00248   test3ok = true;
00249   MEDMEM_Array<double,FullInterlaceGaussPolicy> myArray3ter;
00250   myArray3ter = myArray3; //Opérateur d'affectation
00251   myArray3Ptr = myArray3ter.getPtr();
00252 
00253   elemno = 0;
00254   for (int  i=1; i < nbelgeoc[nbtypegeo] ; i++ )
00255     for (int k=1; k <= myArray3ter.getNbGauss(i); k++)
00256       for (int j=1; j <= mdim; j++) {
00257         test3ok = test3ok
00258           && (  myArray3ter.getIJK(i,j,k)    ==  array3Ref[elemno] )
00259           && (  myArray3Ptr[elemno]          ==  array3Ref[elemno] );
00260         elemno++;
00261       }
00262 
00263   std::cout << "- Opérateur d'affectation en FullInterlace et Gauss : " << test3ok << std::endl;
00264 
00265 
00266   test3ok = true;
00267   MEDMEM_Array<double,FullInterlaceGaussPolicy> myArray3qua(mdim,nbelem2,
00268                                                             nbtypegeo,nbelgeoc,nbgaussgeo);
00269   myArray3Ptr = myArray3qua.getPtr();
00270 
00271   int cumul = 0;
00272   for (int i =1; i <= nbelem2; i++){
00273     myArray3qua.setRow(i,&array3Ref[cumul]);
00274     cumul+=myArray3qua.getNbGauss(i)*mdim;
00275   };
00276 
00277   for (int i =0; i < myArray3qua.getArraySize(); i++)
00278         test3ok = test3ok && ( myArray3Ptr[i] == array3Ref[i] );
00279   std::cout << "- Méthode setRow en FullInterlace et Gauss : " << test3ok << std::endl;
00280 
00281   std::cout << "- Test de levée d'exception pour getColumn : " ;
00282   try {
00283     myArray3qua.getColumn(1);
00284   }
00285   catch (MEDMEM::MEDEXCEPTION &m) {
00286     std::cout << m.what() << endl ;
00287   }
00288 
00289   test3ok = true;
00290   MEDMEM_Array<double,NoInterlaceGaussPolicy> * myArray3cin = ArrayConvert(myArray3);
00291   myArray3Ptr = myArray3cin->getPtr();
00292   elemno = 0;
00293   for (int j=1; j <= mdim; j++)
00294     for (int  i=1; i < nbelgeoc[nbtypegeo] ; i++ )
00295       for (int k=1; k <= myArray3cin->getNbGauss(i); k++) {
00296         test3ok = test3ok
00297           && (  myArray3cin->getIJK(i,j,k) ==  array4Ref[elemno] )
00298           && (  myArray3Ptr[elemno]        ==  array4Ref[elemno] );
00299         elemno++;
00300       }
00301   std::cout << "- Convertion du mode FullInterlaceGaussPolicy au mode NoInterlaceGaussPolicy : " 
00302             << test3ok << std::endl;
00303 
00304 
00305   // TEST n°4   NoInterlace et Gauss
00306   std::cout << std::endl;
00307   std::cout << "Test de MEDMEM_Array en NoInterlace et Gauss : " << std::endl;
00308   std::cout << std::endl;
00309 
00310   bool test4ok = true;
00311   const double * myArray4Ptr = 0;
00312 
00313   MEDMEM_Array<double,NoInterlaceGaussPolicy> myArray4(mdim,nbelem2,nbtypegeo,nbelgeoc,nbgaussgeo);
00314 
00315   elemno = 1;
00316    for (int ntyp=1; ntyp <= nbtypegeo; ntyp++ ) {
00317     for (int  i=0; i < (nbelgeoc[ntyp]-nbelgeoc[ntyp-1]) ; i++ ) {
00318       for (int k=1; k <= nbgaussgeo[ntyp]; k++)
00319         for (int j=1; j <= mdim; j++) {
00320           myArray4.setIJK(elemno,j,k,elemno+(ntyp-1)*10+0.1*k+0.01*j);
00321         }
00322       elemno++;
00323     }
00324   }
00325   myArray4Ptr = myArray4.getPtr();
00326   for (int i =0; i < myArray4.getArraySize(); i++) {
00327     test4ok = test4ok && ( myArray4Ptr[i] == array4Ref[i] );
00328   }
00329   std::cout << "- Egualité de myArray4 et de myArray4Ref : " << test4ok << std::endl ;
00330 
00331   test4ok = true;
00332   MEDMEM_Array<double,NoInterlaceGaussPolicy> myArray4bis(myArray4, false);
00333   myArray4Ptr = myArray4bis.getPtr();
00334 
00335   elemno = 0;
00336   for (int j=1; j <= mdim; j++)
00337     for (int  i=1; i < nbelgeoc[nbtypegeo] ; i++ )
00338       for (int k=1; k <= myArray4bis.getNbGauss(i); k++) {
00339         test4ok = test4ok
00340           && (  myArray4bis.getIJK(i,j,k)    ==  array4Ref[elemno] )
00341           && (  myArray4Ptr[elemno]          ==  array4Ref[elemno] );
00342         elemno++;
00343       }
00344   std::cout << "- Constructeur par recopie en NoInterlace et Gauss : " << test4ok << std::endl;
00345 
00346   test4ok = true;
00347   MEDMEM_Array<double,NoInterlaceGaussPolicy> myArray4ter;
00348   myArray4ter = myArray4; // Opérateur d'affectation
00349   myArray4Ptr = myArray4ter.getPtr();
00350 
00351   elemno = 0;
00352   for (int j=1; j <= mdim; j++)
00353     for (int  i=1; i < nbelgeoc[nbtypegeo] ; i++ )
00354       for (int k=1; k <= myArray4ter.getNbGauss(i); k++) {
00355         test4ok = test4ok
00356           && (  myArray4ter.getIJK(i,j,k)    ==  array4Ref[elemno] )
00357           && (  myArray4Ptr[elemno]          ==  array4Ref[elemno] );
00358         elemno++;
00359       }
00360 
00361   std::cout << "- Opérateur d'affectation en NoInterlace et Gauss : " << test4ok << std::endl;
00362 
00363 
00364   test4ok = true;
00365   MEDMEM_Array<double,NoInterlaceGaussPolicy> myArray4qua(mdim,nbelem2,nbtypegeo,nbelgeoc,nbgaussgeo);
00366   myArray4Ptr = myArray4qua.getPtr();
00367 
00368   for (int j =1; j <= mdim; j++)
00369     myArray4qua.setColumn(j,&array4Ref[(myArray4qua.getArraySize()/mdim)*(j-1)]);
00370 
00371   for (int i =0; i < myArray4qua.getArraySize(); i++)
00372         test4ok = test4ok && ( myArray4Ptr[i] == array4Ref[i] );
00373   std::cout << "- Méthode setColumn en NoInterlace et Gauss : " << test4ok << std::endl;
00374 
00375   std::cout << "- Test de levée d'exception pour getRow : " ;
00376   try {
00377     myArray4qua.getRow(1);
00378   }
00379   catch (MEDMEM::MEDEXCEPTION &m) {
00380     std::cout << m.what() << endl ;
00381   }
00382 
00383 
00384   test4ok = true;
00385   MEDMEM_Array<double,FullInterlaceGaussPolicy> * myArray4cin = ArrayConvert(myArray4);
00388   // typedef MEDMEM_Array<double,FullInterlaceGaussPolicy> MyArray;
00389   //   MyArray myArray4cin MyArray(myArray4);
00390   //   myArray4cin.convert(myArray4);
00391   //  MEDMEM_Array<double,FullInterlaceGaussPolicy> myArray4cin(myArray4);
00392   myArray4Ptr = myArray4cin->getPtr();
00393   elemno = 0;
00394   for (int  i=1; i < nbelgeoc[nbtypegeo] ; i++ )
00395     for (int k=1; k <= myArray4cin->getNbGauss(i); k++)
00396       for (int j=1; j <= mdim; j++) {
00397         test4ok = test4ok
00398           && (  myArray4cin->getIJK(i,j,k)   ==  array3Ref[elemno] )
00399           && (  myArray4cin->getIJK(i,j,k)   ==  myArray3.getIJK(i,j,k) )
00400           && (  myArray4Ptr[elemno]          ==  array3Ref[elemno] );
00401         elemno++;
00402       }
00403 
00404   std::cout << "- Convertion du mode NoInterlaceGaussPolicy au mode FullInterlaceGaussPolicy : " 
00405             << test4ok << std::endl;
00406 
00407 
00408   //TEST DES METHODES D'INTERROGATION en NoInterlace et Gauss :
00409   //  MEDMEM_ArrayInterface<double,NoInterlace,Gauss>::Array & myArray4bis = myField4bis.getArray();
00410   
00411   std::cout << std::endl;
00412   std::cout << "Test de la méthode getDim : " << myArray4bis.getDim() << std::endl;
00413   std::cout << "Test de la méthode getNbelem : " << myArray4bis.getNbElem() << std::endl;
00414   std::cout << "Test de la méthode getArraySize : " << myArray4bis.getArraySize() << std::endl;
00415   std::cout << "Test de la méthode getInterlacingType : " << myArray4bis.getInterlacingType() << std::endl;
00416 
00417 
00418   // Test de levée d'exception si MED_ARRAY compilée avec une politique de checking
00419   std::cout << std::endl;
00420   std::cout << "Test de levée d'exception pour indice incohérent " << std::endl;
00421   try {
00422     myArray4.getIJ(0,2); //L'exception génère des blocks perdus (A voir)
00423   }
00424   catch (MEDMEM::MEDEXCEPTION &m) {
00425     std::cout << m.what() << endl ;
00426   }
00427 
00428  
00429 
00430   return 0;
00431 
00432 }
00433