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