Salome HOME
a7768c55753ce24c7dea313182ad8c2c859ef77b
[modules/med.git] / src / MEDMEMBinTest / test_copie_fieldT.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /* Programme de test du constructeur de copies de la classe FIELD_ de MEDMEM
23    jroy - 12/12/2002 */
24
25 #include "MEDMEM_Exception.hxx"
26 #include "MEDMEM_Mesh.hxx"
27 #include "MEDMEM_Family.hxx"
28 #include "MEDMEM_Group.hxx"
29
30 #include "MEDMEM_MedMeshDriver.hxx"
31 #include "MEDMEM_MedFieldDriver.hxx"
32 #include "MEDMEM_Support.hxx"
33 #include "MEDMEM_Field.hxx"
34 #include "MEDMEM_FieldConvert.hxx"
35 #include "MEDMEM_define.hxx"
36
37 #include<string>
38
39 #include <math.h>
40 #include <stdlib.h>
41
42 using namespace std;
43 using namespace MEDMEM;
44 using namespace MED_EN;
45
46
47 static void affiche_field_(FIELD_ * myField, const SUPPORT * mySupport)
48 {
49   cout << "Field "<< myField->getName() << " : " <<myField->getDescription() <<  endl ;
50   int NumberOfComponents = myField->getNumberOfComponents() ;
51   cout << "- Nombre de composantes : "<< NumberOfComponents << endl ;
52   for (int i=1; i<NumberOfComponents+1; i++) {
53     cout << "  - composante "<<i<<" :"<<endl ;
54     cout << "      - nom         : "<<myField->getComponentName(i)<< endl;
55     cout << "      - description : "<<myField->getComponentDescription(i) << endl;
56     cout << "      - units       : "<<myField->getMEDComponentUnit(i) << endl;
57   }
58   cout << "- iteration :" << endl ;
59   cout << "    - numero : " << myField->getIterationNumber()<< endl  ;
60   cout << "    - ordre  : " << myField->getOrderNumber()<< endl  ;
61   cout << "    - temps  : " << myField->getTime()<< endl  ;
62
63   cout << "- Type : " << myField->getValueType()<< endl;
64
65   cout << "- Adresse support : " << mySupport << endl;
66 }
67
68 template <class INTERLACING_TAG>
69 void affiche_fieldT(FIELD<double, INTERLACING_TAG> * myField,
70                     const SUPPORT * mySupport)
71 {
72   affiche_field_((FIELD_ *) myField, mySupport);
73
74   cout << "- Valeurs :"<<endl;
75   int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
76   int NumberOfComponents = myField->getNumberOfComponents() ;
77
78   if ( myField->getInterlacingType() == MED_EN::MED_FULL_INTERLACE ) {
79     for (int i=1; i<NumberOf+1; i++) {
80       const double * value = myField->getRow(i) ;
81       for (int j=0; j<NumberOfComponents; j++)
82         cout << value[j]<< " ";
83       cout<<endl;
84     }
85   }
86   else {
87     for (int j=1; j<NumberOfComponents+1; j++) {
88       const double * value = myField->getColumn(j) ;
89       for (int i=0; i<NumberOf; i++)
90         cout << value[i]<< " ";
91       cout<<endl;
92     }
93   }
94 }
95
96 int main (int argc, char ** argv)
97 {
98   if ((argc !=3) && (argc != 4))
99     {
100       cerr << "Usage : " << argv[0] 
101            << " filename meshname fieldname" << endl << endl;
102       exit(-1);
103     }
104
105   string filename = argv[1] ;
106   string meshname = argv[2] ;
107
108   MESH * myMesh= new MESH;
109   myMesh->setName(meshname);
110   MED_MESH_RDONLY_DRIVER myMeshDriver(filename,myMesh) ;
111   myMeshDriver.setMeshName(meshname);
112   int current = myMesh->addDriver(myMeshDriver);
113   myMesh->read(current);
114
115   // read field :
116   if (argc != 4) exit(0) ;
117   // else we have a field !
118   string fieldname = argv[3];
119
120   FIELD<double> * myField ;
121   const SUPPORT * mySupport = myMesh->getSupportOnAll(MED_CELL);
122   try
123     {
124       myField = new FIELD<double>(mySupport,MED_DRIVER,filename,fieldname) ;
125     }
126   catch (...)
127     {
128       mySupport = myMesh->getSupportOnAll(MED_NODE);
129       try
130         {
131           myField = new FIELD<double>(mySupport,MED_DRIVER,filename,fieldname) ;
132         }
133       catch (...)
134         {
135           cout << "Field double " << fieldname << " not found !!!" << endl ;
136           exit (-1) ;
137         }
138     }
139
140   affiche_fieldT(myField, mySupport);
141   FIELD<double> * myField2 = new FIELD<double>(* myField); // Contructeur par recopie, sauf SUPPORT
142   myField->removeReference(); // Ne détruit pas le Support 
143   affiche_fieldT(myField2, myField2->getSupport());
144   FIELD<double,NoInterlace>   * myField3  = FieldConvert( *myField2 );
145   myField2->removeReference();
146
147   affiche_fieldT(myField3, myField3->getSupport());
148   FIELD<double,FullInterlace> * myField4  = FieldConvert( *myField3 );
149   myField3->removeReference();
150   affiche_fieldT(myField4, myField4->getSupport());
151   myField4->removeReference();
152
153   FIELD<double,NoInterlace> * myField5 = new FIELD<double,NoInterlace>(mySupport,MED_DRIVER,filename,fieldname) ;
154   affiche_fieldT(myField5, myField5->getSupport());
155   myField5->removeReference();
156
157   myMesh->removeReference();
158
159   return 0;
160 }