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