Salome HOME
079e7f92640dec2b4b9fc1ee07134a4eba2d7884
[modules/med.git] / src / MEDMEMBinTest / create_poly3D.c
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
23 /*
24   creation d'une geometrie 3d : 
25   maillĂ© en :
26   - 2 polyedres
27     ayant pour faces 3 polygones
28                      9 quadrangles
29                      6 triangles
30   - 1 tetra4
31
32 */
33
34 #include <med.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 int main (int argc, char **argv)
39 {
40   med_err ret;
41   med_idt fid;
42   char maa[MED_NAME_SIZE+1] = "poly3D";
43   char maadesc[MED_COMMENT_SIZE+1] = "Example de maillage non structure 3D avec 2 polyedres+1tetra4 comme mailles et 3 polygones comme faces";
44   med_int mdim = 3;
45   med_int nnoe = 19;
46   /*
47     les noeuds:
48   */
49   med_float coo[57] = {
50     2.0, 3.0, 2.0,
51     3.0, 2.0, 2.0,
52     4.0, 1.0, 2.0,
53     2.0, 0.0, 2.0,
54     0.0, 1.0, 2.0,
55     1.0, 2.0, 2.0,
56     2.0, 3.0, 1.0,
57     3.0, 2.0, 0.0,
58     4.0, 1.0, 0.0,
59     2.0, 0.0, 0.0,
60     0.0, 1.0, 0.0,
61     1.0, 2.0, 0.0,
62     5.0, 3.0, 2.0,
63     7.0, 2.0, 2.0,
64     6.0, 0.0, 2.0,
65     6.0, 3.0, 0.0,
66     7.0, 2.0, 0.0,
67     6.0, 0.0, -1.0,
68     5.0, 1.0, -3.0 
69   };
70   char nomcoo[3*MED_SNAME_SIZE+1] = "x               y               z               ";
71   char unicoo[3*MED_SNAME_SIZE+1] = "cm              cm              cm              ";
72   /*  char nomnoe[18*MED_TAILLE_PNOM+1] = "nom1    nom2    nom3    nom4";*/
73   //char *nomnoe ;
74 //  med_int numnoe[19] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
75   med_int nufano[19] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
76   /*
77     les elements:
78   */
79   /* Faces QUAD4 */
80   med_int nquad4 = 8;
81   med_int quad4[32] = {
82     1, 7, 8, 2,
83     2, 8, 9, 3,
84     4, 3, 9, 10,
85     5, 4, 10, 11,
86     6, 5, 11, 12,
87     1, 6, 12, 7,
88     14, 13, 16, 17,
89     8, 9, 17, 16
90   };
91  // char nomquad4[MED_TAILLE_PNOM*8+1] = "quad1           quad2           quad3           quad4           quad5           quad6           quad7           quad8           ";
92  // med_int numquad4[9] = {2,3,4,5,6,7,12,17};
93   med_int nufaquad4[9] = {-1,-1,-1,-1,-1,-1,-1,-1};
94
95   /* Faces TRIA3 */
96   med_int ntria3 = 6;
97   med_int tria3[18] = {
98     7, 12, 8,
99     15, 14, 17,
100     15, 17, 18,
101     15, 18, 9,
102     3, 15, 9,
103     18, 17, 9
104   };
105  // char nomtria3[MED_TAILLE_PNOM*6+1] = "tria1           tria2           tria3           tria4           tria5           tria6           ";
106  // med_int numtria3[6] = {8,13,14,15,16,18};
107   med_int nufatria3[6] = {-2,-2,-2,-2,-2,-2};
108
109   /* Faces POLYGONE */
110   med_int npolygon = 3;
111   med_int nindexpolygon = 4;
112   med_int indexpolygon [4] = {
113     1, 7, 12, 17
114   };
115   med_int polygon[16] = {
116     1, 2, 3, 4, 5, 6,
117     10, 9, 8, 12, 11,
118     13, 14, 15, 3, 2
119   };
120   //char nompolygon[MED_SNAME_SIZE*3+1] = "polygon1        polygon2        polygon3        ";
121 //  med_int numpolygon[3] = {1,9,10};
122   med_int nufapolygon[3] = {-3,-3,-3};
123
124   /* Mailles POLYEDRE */
125   med_int npolyhedron = 2;
126   med_int npolyhedronfaces = 19;
127   med_int polyhedronconnectivity[74] = {1,2,3,4,5,6,1,7,8,2,2,8,9,3,4,3,9,10,5,4,10,11,6,5,11,12,1,6,12,7,7,12,8,10,9,8,12,11,
128                                         13,14,15,3,2,13,2,8,16,14,13,16,17,15,14,17,15,17,18,15,18,9,3,15,9,2,3,9,8,8,9,17,16,9,18,17};
129   med_int polyhedronfacesindex[20] = {1,7,11,15,19,23,27,31,34,
130                                       39,44,48,52,55,58,61,64,68,72,75};
131   med_int polyhedronindex[3] = {1,10,20};
132   //char nompolyhedron[MED_SNAME_SIZE*2+1] = "polyhedron1     polyhedron2     ";
133 //  med_int numpolyhedron[2] = {2,3};
134   med_int nufapolyhedron[2] = {-4,-4};
135
136   /* Mailles TETRA4 */
137   med_int ntetra4 = 1;
138   med_int tetra4[4] = {
139     17, 9, 18, 19
140   };
141  // char nomtetra4[MED_SNAME_SIZE*1+1] = "tetra1          ";
142  // med_int numtetra4[1] = {1};
143   med_int nufatetra4[1] = {-5};
144
145
146   char nomfam[MED_NAME_SIZE+1];
147   med_int numfam;
148   //char attdesMED_TAILLE_DESC+1];
149   //med_int attide;
150   //med_int attval;
151   char gro[MED_LNAME_SIZE+1];
152   char dtunitp3[MED_LNAME_SIZE+1]="";
153
154   /*
155     Some fields : 2 on nodes : one int and one double , one on cells : double
156    */
157   char champ1[MED_NAME_SIZE+1]="fieldnodeint" ;
158   char champ1_comp[MED_SNAME_SIZE+1]="comp1           " ;
159   char champ1_unit[MED_SNAME_SIZE+1]="M               " ;
160   med_int     fieldnodeint[19]    = {1,1,3,2,2,3,4,4,5,6,6,7,8,8,9,9,9,10,5};
161
162   char champ2[MED_NAME_SIZE+1]="fieldnodedouble" ;
163   char champ2_comp[MED_SNAME_SIZE+1]="comp1           " ;
164   char champ2_unit[MED_SNAME_SIZE+1]="J               " ;
165   med_float   fieldnodedouble1[19] = {1.,3.,4.,1.,3.,4.,3.,2.,5.,6.,4.,3.,1.,5.,6.,4.,3.,7.,3.};
166   med_float   fieldnodedouble2[19] = {1.,2.,2.,3.,3.,3.,4.,4.,5.,2.,8.,9.,6.,7.,1.,2.,5.,8.,4.};
167
168   char champ3[MED_NAME_SIZE+1]="fieldfacedouble" ;
169   char champ3_comp[MED_SNAME_SIZE*2+1]="comp1           comp2           " ;
170   char champ3_unit[MED_SNAME_SIZE*2+1]="M/S             m/s             " ;
171   med_float   fieldfacedouble[6*2] = {0.,1.,1.,1.,1.,2.,2.,3.,3.,4.,4.,6.};
172
173   /***************************************************************************/
174   fid = MEDfileOpen("poly3D.med",MED_ACC_RDWR);
175   if (fid < 0)
176     ret = -1;
177   else
178     ret = 0;
179   printf("MEDouvrir : %d\n",ret);
180
181   /***************************************************************************/
182   if (ret == 0)
183     ret = MEDmeshCr(fid,maa,mdim,mdim,MED_UNSTRUCTURED_MESH,maadesc,dtunitp3,
184                     MED_SORT_DTIT,MED_CARTESIAN,nomcoo,unicoo);
185   printf("MEDmaaCr : %d\n",ret);
186   if (ret == 0)
187     ret = MEDunvCr(fid,maa);
188   printf("MEDunvCr : %d\n",ret);
189
190   /***************************************************************************/
191   if (ret == 0)
192     ret = MEDmeshNodeCoordinateWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,MED_FULL_INTERLACE,nnoe,coo);
193   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NODE,0,nnoe,nufano);
194   printf("MEDnoeudsEcr : %d\n",ret);
195
196   /* ecriture des faces MED_QUAD4 :
197      - connectivite
198      - noms (optionnel) 
199      - numeros (optionnel)
200      - numeros des familles */
201   if (ret == 0) 
202     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
203                                        MED_CELL,MED_QUAD4,MED_NODAL,MED_FULL_INTERLACE,
204                                        nquad4,quad4);
205   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_QUAD4,nquad4,nufaquad4);
206   printf("MEDelementsEcr for quad : %d \n",ret);
207
208   /* ecriture des faces MED_TRIA3 :
209      - connectivite
210      - noms (optionnel) 
211      - numeros (optionnel)
212      - numeros des familles */
213   if (ret == 0) 
214     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
215                                        MED_CELL,MED_TRIA3,MED_NODAL,MED_FULL_INTERLACE,
216                                        ntria3,tria3);
217   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_TRIA3,ntria3,nufatria3);
218   printf("MEDelementsEcr for tria : %d \n",ret);
219
220   /* ecriture des faces MED_POLYGONE :
221      - connectivite
222      - noms (optionnel) 
223      - numeros (optionnel)
224      - numeros des familles
225      Dans ce cas il n existe pas de routine globale (Warning !!) */
226   if (ret == 0)
227     ret = MEDmeshPolygonWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT, MED_CELL,MED_NODAL, nindexpolygon,indexpolygon,polygon);
228   printf("MEDpolygoneConnEcr: %d \n",ret);
229
230 /*   if (ret == 0) */
231 /*     MEDnomEcr(fid,maa,nompolygon,npolygon,MED_CELL,MED_POLYGONE); */
232 /*   printf("MEDnomEcr for polygon : %d \n",ret); */
233
234 /*   if (ret == 0) */
235 /*     MEDnumEcr(fid,maa,numpolygon,npolygon,MED_CELL,MED_POLYGONE); */
236 /*   printf("MEDnumEcr for polygon : %d \n",ret); */
237
238   if (ret == 0)
239     MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT, MED_CELL,MED_POLYGON, npolygon,nufapolygon);
240   printf("MEDfamEcr for polygon : %d \n",ret);
241
242   /* ecriture des mailles MED_POLYEDRE :
243      - connectivite
244      - noms (optionnel) 
245      - numeros (optionnel)
246      - numeros des familles
247      Dans ce cas il n existe pas de routine globale (Warning !!) */
248   if (ret == 0)
249     ret = MEDmeshPolyhedronWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT, MED_CELL,MED_NODAL, npolyhedronfaces+1,polyhedronfacesindex,npolyhedron+1,polyhedronindex,polyhedronconnectivity);
250   printf("MEDpolyedreConnEcr: %d \n",ret);
251
252 /*   if (ret == 0) */
253 /*     ret = MEDnomEcr(fid,maa,nompolyhedron,npolyhedron,MED_CELL,MED_POLYEDRE); */
254 /*   printf("MEDnomEcr for polyhedron : %d \n",ret); */
255
256 /*   if (ret == 0) */
257 /*     ret = MEDnumEcr(fid,maa,numpolyhedron,npolyhedron,MED_CELL,MED_POLYEDRE); */
258 /*   printf("MEDnumEcr for polyhedron : %d \n",ret); */
259
260   if (ret == 0)
261     ret = MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT, MED_CELL,MED_POLYHEDRON, npolyhedron,nufapolyhedron);
262   printf("MEDfamEcr for polyhedron : %d \n",ret);
263
264   /* ecriture des mailles TETRA4 :
265      - connectivite
266      - noms (optionnel) 
267      - numeros (optionnel)
268      - numeros des familles */
269   if (ret == 0)
270     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
271                                        MED_CELL,MED_TETRA4,MED_NODAL,MED_FULL_INTERLACE,
272                                        ntetra4,tetra4);
273   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_TETRA4,ntetra4,nufatetra4);
274   printf("MEDelementsEcr for tetra : %d \n",ret);
275
276   /***************************************************************************/
277   /* ecriture des familles */
278   /* Conventions :
279      - toujours creer une famille de numero 0 ne comportant aucun attribut
280        ni groupe (famille de reference pour les noeuds ou les elements
281        qui ne sont rattaches a aucun groupe ni attribut)
282      - les numeros de familles de noeuds sont > 0
283      - les numeros de familles des elements sont < 0
284      - rien d'imposer sur les noms de familles
285    */ 
286
287   /* la famille 0 */
288   if (ret == 0)
289     {
290       strcpy(nomfam,"FAMILLE_0");
291       numfam = 0;
292       ret = MEDfamilyCr(fid,maa,nomfam,numfam,0,gro);
293     }
294   printf("MEDfamCr : %d \n",ret);
295
296   /* on cree :
297       - 2 familles d'elements de dimension (d)
298         en fait de face (-10)
299   */
300   /*
301   if (ret == 0)
302     {
303       numfam = -1;
304       strcpy(nomfam,"FAMILLE_FACE_QUAD4");
305       sprintf(nomfam,"%s%d",nomfam,-numfam);
306       attide = 1;
307       attval = numfam*100;
308       natt = 1;
309       //strcpy(attdes,"description attribut");
310       strcpy(gro,"groupe0");
311       ngro = 1;
312
313       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
314       printf("MEDfamCr : %d\n",ret);
315
316       numfam = -2;
317       strcpy(nomfam,"FAMILLE_FACE_TRIA3");
318       sprintf(nomfam,"%s%d",nomfam,-numfam);
319       attide = 1;
320       attval = numfam*100;
321       natt = 1;
322       //strcpy(attdes,"description attribut");
323       strcpy(gro,"groupe0");
324       ngro = 1;
325
326       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
327       printf("MEDfamCr : %d\n",ret);
328
329       numfam = -3;
330       strcpy(nomfam,"FAMILLE_FACE_POLYGONS");
331       sprintf(nomfam,"%s%d",nomfam,-numfam);
332       attide = 1;
333       attval = numfam*100;
334       natt = 1;
335       //strcpy(attdes,"description attribut");
336       strcpy(gro,"groupe0");
337       ngro = 1;
338
339       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
340       printf("MEDfamCr : %d\n",ret);
341
342       numfam = -4;
343       strcpy(nomfam,"FAMILLE_MAILLE_POLYHEDRON");
344       sprintf(nomfam,"%s%d",nomfam,-numfam);
345       attide = 1;
346       attval = numfam*100;
347       natt = 1;
348       //strcpy(attdes,"description attribut");
349       strcpy(gro,"groupe0");
350       ngro = 1;
351
352       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
353       printf("MEDfamCr : %d\n",ret);
354
355       numfam = -5;
356       strcpy(nomfam,"FAMILLE_MAILLE_TETRA4");
357       sprintf(nomfam,"%s%d",nomfam,-numfam);
358       attide = 1;
359       attval = numfam*100;
360       natt = 1;
361       //strcpy(attdes,"description attribut");
362       strcpy(gro,"groupe0");
363       ngro = 1;
364
365       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
366       printf("MEDfamCr : %d\n",ret);
367     }
368   */
369   /***************************************************************************/
370   /*
371     Les champs
372   */
373   if (ret == 0)
374     {
375       ret = MEDfieldCr(fid,champ1,MED_INT32,1,champ1_comp,champ1_unit,dtunitp3,maa);
376       printf("MEDchampCr : %d \n",ret);
377       if (ret == 0) {
378         ret = MEDfieldValueWr(fid, champ1,MED_NO_DT,MED_NO_IT,0.,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodeint);
379         
380         printf("MEDchampEcr : %d \n",ret);
381       }
382     }
383   
384   if (ret == 0)
385     {
386       ret = MEDfieldCr(fid,champ2,MED_FLOAT64,1,champ2_comp,champ2_unit,dtunitp3,maa);
387       printf("MEDchampCr : %d \n",ret);
388       if (ret == 0) {
389         ret = MEDfieldValueWr(fid, champ2,1,MED_NO_IT,1.1 ,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
390         printf("MEDchampEcr1 : %d \n",ret);
391         ret = MEDfieldValueWr(fid, champ2,2,MED_NO_IT,1.2 ,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble2);
392         printf("MEDchampEcr2 : %d \n",ret);
393       }
394     }
395   
396   /*/ on met champ2 sans pas de temps pour pouvoir le lire aussi par defaut ! */
397   if (ret == 0)
398     {
399       ret = MEDfieldValueWr(fid, champ2,MED_NO_DT,MED_NO_IT,0. ,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
400       printf("MEDchampEcr : %d \n",ret); 
401     }
402
403   if (ret == 0)
404     {
405       ret = MEDfieldCr(fid,champ3,MED_FLOAT64,2,champ3_comp,champ3_unit,dtunitp3,maa);
406       printf("MEDchampCr : %d \n",ret);
407       if (ret == 0) {
408         ret = MEDfieldValueWr(fid, champ3,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_TRIA3,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,ntria3, (unsigned char *)fieldfacedouble);
409         printf("MEDchampEcr : %d \n",ret);
410       }
411     }
412   /***************************************************************************/
413
414   ret = MEDfermer(fid);
415   printf("MEDfermer : %d\n",ret);
416   
417   if ( getenv("srcdir") ) 
418     /* we are in 'make check' */
419     remove( "poly3D.med" );
420
421   return 0;
422 }
423