Salome HOME
Final version of the V2_2_0 in the main trunk of the CVS tree.
[modules/med.git] / src / MEDMEM / MEDMEM_Meshing.cxx
1 /*
2   File MEDMEM_Meshing.cxx
3   $Header$
4 */
5
6 #include <string>
7
8 #include "utilities.h"
9 #include "MEDMEM_STRING.hxx"
10 #include "MEDMEM_Exception.hxx"
11 #include "MEDMEM_define.hxx"
12
13 #include "MEDMEM_Meshing.hxx"
14 #include "MEDMEM_Group.hxx"
15
16 using namespace std;
17 using namespace MEDMEM;
18 using namespace MED_EN;
19
20 /*! Create an empty MESH. */
21 MESHING::MESHING(): MESH()
22 {
23   MESSAGE("MESHING::MESHING()");
24   SCRUTE(_coordinate);
25   SCRUTE(_connectivity);
26 }
27
28 MESHING::~MESHING()
29 {
30   MESSAGE("Deletinh MESHING !!");
31 }
32
33 /*! Set the dimension of the space */
34 void MESHING::setSpaceDimension(const int SpaceDimension)
35 {
36   _spaceDimension = SpaceDimension ;
37 }
38
39 /* Set the dimension of the MESHING */
40 // void MESHING::setMeshDimension(const int MeshDimension)
41 // {
42 //    _meshDimension = MeshDimension ;
43 // }
44
45 /*! Set the number of nodes used in the MESH */
46 void MESHING::setNumberOfNodes(const int NumberOfNodes)
47 {
48   _numberOfNodes = NumberOfNodes ;
49 }
50
51 /*! 
52   Set the whole coordinates array in a given system and interlacing mode.
53   The system coordinates are :
54     - "CARTESIAN"
55     - "CYLINDRICAL"
56     - "SPHERICAL"
57   The interlacing mode are :
58     - MED_NO_INTERLACE   :  X1 X2 Y1 Y2 Z1 Z2
59     - MED_FULL_INTERLACE :  X1 Y1 Z1 X2 Y2 Z2
60 */
61 void MESHING::setCoordinates(const int SpaceDimension,
62                              const int NumberOfNodes,
63                              const double * Coordinates,
64                              const string System,
65                              const medModeSwitch Mode)
66 {
67   setSpaceDimension(SpaceDimension);
68   setNumberOfNodes(NumberOfNodes);
69
70   SCRUTE(_coordinate);
71   SCRUTE(_connectivity);
72   //if (NULL != _coordinate) delete _coordinate;
73
74   _coordinate = new COORDINATE(SpaceDimension,
75                                NumberOfNodes,
76                                Mode);
77   _coordinate->setCoordinates(Mode,Coordinates);
78   _coordinate->setCoordinatesSystem(System);
79 }
80
81 /*! Set the system in which coordinates are given (CARTESIAN,CYLINDRICAL,SPHERICAL) __??MED_CART??__. */
82 void MESHING::setCoordinatesSystem(const string System)
83   throw (MEDEXCEPTION)
84 {
85   if (NULL == _coordinate)
86     throw MEDEXCEPTION(LOCALIZED("MESHING::setCoordinatesSystem : no coordinates defined"));
87   _coordinate->setCoordinatesSystem(System);
88 }
89
90 /*! Set the coordinate names array ("x       ","y       ","z       ")
91   of size n*MED_TAILLE_PNOM
92 */
93 void MESHING::setCoordinatesNames(const string * name)
94 {
95 //    int SpaceDimension = getSpaceDimension() ;
96 //    _coordinate->setCoordinatesNames(SpaceDimension,name);
97   _coordinate->setCoordinatesNames(name);
98 }
99
100 /*!
101   Set the (i+1)^th component of coordinate names array
102   ("x       ","y       ","z       ") of size n*MED_TAILLE_PNOM
103 */
104 void MESHING::setCoordinateName(const string name, const int i)
105 {
106   _coordinate->setCoordinateName(name,i);
107 }
108
109 /*! Set the coordinate unit names array ("cm       ","cm       ","cm       ")
110   of size n*MED_TAILLE_PNOM
111 */
112 void MESHING::setCoordinatesUnits(const string * units)
113 {
114 //    int SpaceDimension = getSpaceDimension() ;
115 //    _coordinate->setCoordinatesUnits(SpaceDimension,units);
116   _coordinate->setCoordinatesUnits(units);
117 }
118
119 /*!
120   Set the (i+1)^th component of the coordinate unit names array
121   ("cm       ","cm       ","cm       ") of size n*MED_TAILLE_PNOM
122 */
123 void MESHING::setCoordinateUnit(const string unit, const int i)
124 {
125   _coordinate->setCoordinateUnit(unit,i);
126 }
127
128 /*!
129   Create a new connectivity object with the given number of type and
130   entity. If a connectivity already exist, delete it !
131
132   For exemple setNumberOfTypes(3,MED_CELL) create a connectivity with 3 
133   medGeometryElement in MESH for MED_CELL entity (like MED_TETRA4, 
134   MED_PYRA5 and MED_HEXA6 for example).
135
136   Return an exception if could not create the connectivity (as if we set 
137   MED_FACE connectivity before MED_CELL).
138 */
139 void MESHING::setNumberOfTypes(const int NumberOfTypes,
140                                const medEntityMesh Entity) 
141   throw (MEDEXCEPTION)
142 {
143   const char * LOC = "MESHING::setNumberOfTypes( medEntityMesh ) : ";
144
145   // No defined for MED_NODE !
146   if (Entity == MED_NODE)
147     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Not required with MED_NODE !"));
148
149   if (MED_CELL == Entity) {
150     SCRUTE(_connectivity);
151 //     if (_connectivity != (CONNECTIVITY *) NULL)
152 //       delete _connectivity ;
153     _connectivity = new CONNECTIVITY(NumberOfTypes,Entity) ;
154
155   } else {
156
157     if (_connectivity == NULL) // we must have defined MED_CELL connectivity
158       throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_CELL defined !"));
159
160     if (MED_FACE == Entity)
161       if (3 != getSpaceDimension())
162         throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_FACE could be defined in non 3D space !"));
163     
164     if (MED_EDGE == Entity)
165       if (3 == getSpaceDimension()) {
166         if (!_connectivity->existConnectivity(MED_NODAL,MED_FACE))
167           throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_FACE defined !"));
168       } else {
169         if (2 != getSpaceDimension())
170           throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Could not set connectivity on MED_EDGE !"));
171       }
172     // all rigth, we could create connectivity !
173     CONNECTIVITY * myConnectivity = new CONNECTIVITY(NumberOfTypes,Entity) ;
174     _connectivity->setConstituent(myConnectivity);
175   }
176 }
177
178 /*!
179   Set the list of geometric types used by a given entity.
180   medEntityMesh entity could be : MED_CELL, MED_FACE, MED_EDGE
181
182   REM : Don't use MED_NODE and MED_ALL_ENTITIES
183
184   If entity is not defined, throw an exception.
185 */
186 void MESHING::setTypes(const medGeometryElement * Types,
187                        const medEntityMesh entity)
188   throw (MEDEXCEPTION)
189 {
190   if (entity == MED_NODE)
191     throw MEDEXCEPTION(LOCALIZED("MESHING::setTypes : Not defined with MED_NODE entity !"));
192
193   if (_connectivity == NULL)
194     throw MEDEXCEPTION(LOCALIZED("MESHING::setTypes : No connectivity defined !"));
195
196   _connectivity->setGeometricTypes(Types,entity) ;
197 }
198
199 /*!
200   Set the number of elements for each geometric type of given entity.
201
202   Example : setNumberOfElements({12,23},MED_FACE)
203   if we have two type of face (MED_TRIA3 and MED_QUAD4), 
204   we set 12 triangles and 23 quadrangles.
205 */
206 void MESHING::setNumberOfElements(const int * NumberOfElements,
207                                   const medEntityMesh Entity)
208   throw (MEDEXCEPTION)
209 {
210   const char * LOC = "MESHING::setNumberOfElements(const int *, medEntityMesh) : " ;
211
212   if (Entity==MED_NODE)
213     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Not defined with MED_NODE entity !"));
214
215   if (_connectivity == (CONNECTIVITY*)NULL)
216     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity defined !"));
217
218   int NumberOfTypes = getNumberOfTypes(Entity) ;
219   int * Count = new int[NumberOfTypes+1] ;
220   Count[0]=1 ;
221   for (int i=0; i<NumberOfTypes; i++)
222     Count[i+1]=Count[i]+NumberOfElements[i] ;
223   _connectivity->setCount(Count,Entity) ;
224   delete[] Count ;
225 }
226
227 /*!
228   Set the nodal connectivity for one geometric type of the given entity.
229
230   Example : setConnectivity({1,2,3,1,4,2},MED_FACE,MED_TRIA3)
231   Define 2 triangles face defined with nodes 1,2,3 and 1,4,2.
232 */
233 void MESHING::setConnectivity(const int * Connectivity,
234                               const medEntityMesh Entity,
235                               const medGeometryElement Type)
236   throw (MEDEXCEPTION)
237 {
238   const char * LOC = "MESHING::setConnectivity : " ;
239
240   if (Entity==MED_NODE)
241     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Not defined with MED_NODE entity !"));
242
243   if (_connectivity == (CONNECTIVITY*)NULL)
244     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity defined !"));
245
246   _connectivity->setNodal(Connectivity,Entity,Type) ;
247 }
248
249 /*!
250   NOT YET IMPLEMENTED !! WARNING
251 */
252 void MESHING::setConnectivities (const int * ConnectivityIndex,
253                                  const int * ConnectivityValue,
254                                  const medConnectivity ConnectivityType,
255                                  const medEntityMesh Entity)
256   throw (MEDEXCEPTION)
257 {
258   const char * LOC = "MESHING::setConnectivities : " ;
259   SCRUTE(Entity);
260   SCRUTE(ConnectivityType);
261   SCRUTE(ConnectivityValue);
262   SCRUTE(ConnectivityIndex);
263
264   throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Not Yet Implemented :: Warning !"));
265 }
266
267 /*!
268 */
269
270 // void MESHING::setGroup(const string name,
271 //                     const string description,
272 //                     const int NumberOfElements,
273 //                     const int * ElementsNumbers,
274 //                     const medEntityMesh Entity)
275 // {
276 //   GROUP * myGroup = new GROUP() ;
277 //   myGroup->setMesh(*this) ;
278 //   myGroup->setName(name) ;
279 //   myGroup->setDescription(description) ;
280 //   myGroup->setEntity(Entity) ;
281 //   // medEntityMesh and medGeometryElement ???
282 //   myGroup->setNumberOfGeometricType(NumberOfType) ;
283 //   myGroup->setGeometricType(Type) ;
284 //   myGroup->setNumberOfGaussPoint(NumberOfGaussPoint) ;
285 //   myGroup->setNumberOfElements(NumberOfElements) ;
286 //   myGroup->setNumber(Number) ;
287 // }
288
289 void MESHING::addGroup(const GROUP & Group)
290   throw (MEDEXCEPTION)
291 {
292   const char * LOC = "MESHING::addGroup : " ;
293
294   GROUP * myGroup = new GROUP(Group) ;
295   switch(Group.getEntity()){
296   case MED_CELL : {
297     _groupCell.push_back(myGroup);
298     break;
299   }
300   case MED_FACE : {
301      _groupFace.push_back(myGroup);
302     break;
303   }
304   case MED_EDGE : {
305      _groupEdge.push_back(myGroup);
306     break;
307   }
308   case MED_NODE : {
309      _groupNode.push_back(myGroup);
310     break;
311   }
312   default :
313     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Bad Entity !"));
314   }
315 }