Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEMBinTest / test_copie_connectivity.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 CONNECTIVITY de MEDMEM
23    jroy - 19/12/2002 */
24
25 #include <string>
26
27 #include <math.h>
28 #include <stdlib.h>
29
30 #include "MEDMEM_Exception.hxx"
31 #include "MEDMEM_Mesh.hxx"
32 #include "MEDMEM_Family.hxx"
33 #include "MEDMEM_Group.hxx"
34
35 #include "MEDMEM_MedMeshDriver.hxx"
36 #include "MEDMEM_MedFieldDriver.hxx"
37 #include "MEDMEM_Support.hxx"
38 #include "MEDMEM_Field.hxx"
39 #include "MEDMEM_define.hxx"
40
41 using namespace std;
42 using namespace MEDMEM;
43 using namespace MED_EN;
44
45 static void affiche_connectivity(const CONNECTIVITY * myConnectivity, MESH * myMesh)
46 {
47   int MeshDimension  = myMesh->getMeshDimension() ;
48   int NumberOfNodes  = myMesh->getNumberOfNodes() ;
49
50   int NumberOfTypes                 = myMesh->getNumberOfTypes(MED_CELL) ;
51   const medGeometryElement  * Types = myMesh->getTypes(MED_CELL) ;
52
53   cout << "Show Connectivity (Nodal) :" << endl ;
54   for (int i=0; i<NumberOfTypes; i++) {
55     cout << "For type " << Types[i] << " : " << endl ;
56     int NumberOfElements = myMesh->getNumberOfElements(MED_CELL,Types[i]);
57     const int * connectivity =  myMesh->getConnectivity(MED_NODAL,MED_CELL,Types[i]);
58     int NomberOfNodesPerCell = Types[i]%100 ;
59     for (int j=0;j<NumberOfElements;j++){
60       cout << "Element "<< j+1 <<" : " ;
61       for (int k=0;k<NomberOfNodesPerCell;k++)
62         cout << connectivity[j*NomberOfNodesPerCell+k]<<" ";
63       cout << endl ;
64     }
65   }
66
67   cout << "Show Reverse Nodal Connectivity :" << endl ;
68   const int * ReverseNodalConnectivity = myMesh->getReverseConnectivity(MED_NODAL) ;
69   const int * ReverseNodalConnectivityIndex = myMesh->getReverseConnectivityIndex(MED_NODAL) ;
70   for (int i=0; i<NumberOfNodes; i++) {
71     cout << "Node "<<i+1<<" : " ;
72     for (int j=ReverseNodalConnectivityIndex[i];j<ReverseNodalConnectivityIndex[i+1];j++)
73       cout << ReverseNodalConnectivity[j-1] << " " ;
74     cout << endl ;
75   }
76
77   cout << "Show Connectivity (Descending) :" << endl ;
78   int NumberOfElements ;
79   const int * connectivity ;
80   const int * connectivity_index ;
81   myMesh->calculateConnectivity(MED_DESCENDING,MED_CELL);
82   try {
83     NumberOfElements = myMesh->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS);
84     connectivity =  myMesh->getConnectivity(MED_DESCENDING,MED_CELL,MED_ALL_ELEMENTS);
85     connectivity_index =  myMesh->getConnectivityIndex(MED_DESCENDING,MED_CELL);
86   }
87   catch (MEDEXCEPTION& m) {
88     cout << m.what() << endl ;
89     exit (-1) ;
90   }
91   for (int j=0;j<NumberOfElements;j++) {
92     cout << "Element "<<j+1<<" : " ;
93     for (int k=connectivity_index[j];k<connectivity_index[j+1];k++)
94       cout << connectivity[k-1]<<" ";
95     cout << endl ;
96   }
97
98   cout << "Show Reverse Descending Connectivity :" << endl ;
99   const int * ReverseDescendingConnectivity = myMesh->getReverseConnectivity(MED_DESCENDING) ;
100   const int * ReverseDescendingConnectivityIndex = myMesh->getReverseConnectivityIndex(MED_DESCENDING) ;
101
102   int NumberOfConstituents  = 0;
103   string constituent ;
104   medEntityMesh constituentEntity ;
105
106   if (MeshDimension==3) {
107     constituent = "Face" ;
108     constituentEntity = MED_FACE ;
109   }
110
111   if (MeshDimension==2) {
112     constituent = "Edge" ;
113     constituentEntity = MED_EDGE ;
114   }
115
116   if (MeshDimension==1) {
117     MESSAGE_MED("ERROR : MeshDimension = 1 !");
118     MESSAGE_MED("We could not see Reverse Descending Connectivity.") ;
119   } else {
120     NumberOfConstituents = myMesh->getNumberOfElements (constituentEntity,MED_ALL_ELEMENTS);
121     for (int i=0; i<NumberOfConstituents; i++) {
122       cout << constituent <<i+1<<" : " ;
123       for (int j=ReverseDescendingConnectivityIndex[i];j<ReverseDescendingConnectivityIndex[i+1];j++)
124         cout << ReverseDescendingConnectivity[j-1] << " " ;
125       cout << endl ;
126     }
127   }
128   cout << "Show "<<constituent<<" Connectivity (Nodal) :" << endl ;
129   const int * face_connectivity =  myMesh->getConnectivity(MED_NODAL,constituentEntity,MED_ALL_ELEMENTS);
130   const int * face_connectivity_index =  myMesh->getConnectivityIndex(MED_NODAL,constituentEntity);
131   for (int i=0; i<NumberOfConstituents; i++) {
132     cout << constituent <<i+1<<" : " ;
133     for (int j=face_connectivity_index[i];j<face_connectivity_index[i+1];j++)
134       cout << face_connectivity[j-1]<<" ";
135     cout << endl ;
136   }
137 }
138
139
140 int main (int argc, char ** argv) {
141
142   if (argc <3) { // after 3, ignored !
143     cerr << "Usage : " << argv[0] 
144          << " filename meshname" << endl << endl;
145     exit(-1);
146   }
147
148   string filename = argv[1] ;
149   string meshname = argv[2] ;
150
151   //Construction d'un maillage
152   MESH * myMesh= new MESH;
153   myMesh->setName(meshname);
154   MED_MESH_RDONLY_DRIVER myMeshDriver(filename,myMesh) ;
155   myMeshDriver.setMeshName(meshname);
156   myMeshDriver.open() ;
157   myMeshDriver.read() ; //A partir d'ici la connectivité est construite
158   myMeshDriver.close() ;
159
160   const CONNECTIVITY * myConnectivity = myMesh->getConnectivityptr();
161   affiche_connectivity(myConnectivity, myMesh);
162
163   CONNECTIVITY * myConnectivity2 = new CONNECTIVITY(* myConnectivity);
164   affiche_connectivity(myConnectivity2, myMesh);
165
166   CONNECTIVITY * myConnectivity3 = new CONNECTIVITY(* myConnectivity2);
167   delete myConnectivity2;
168   affiche_connectivity(myConnectivity3, myMesh);
169   delete myConnectivity3;
170
171   myMesh->removeReference();
172
173   return 0;
174 }