Salome HOME
MEDMEM suppression
[modules/med.git] / src / MEDMEMBinTest / create_mesh.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 #include <med.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 int main (int argc, char **argv)
28 {
29   med_err ret;
30   med_idt fid;
31   char maa[MED_NAME_SIZE+1] = "maa1";
32   char maadesc[MED_COMMENT_SIZE+1] = "Example de maillage non structure 3D";
33   med_int mdim = 3;
34   med_int nnoe = 19;
35   med_float coo[57] = {
36     0.0, 0.0, 0.0, 
37     0.0, 0.0, 1.0, 
38     2.0, 0.0, 1.0, 
39     0.0, 2.0, 1.0, 
40     -2.0, 0.0, 1.0, 
41     0.0, -2.0, 1.0, 
42     1.0, 1.0, 2.0, 
43     -1.0, 1.0, 2.0, 
44     -1.0, -1.0, 2.0, 
45     1.0, -1.0, 2.0, 
46     1.0, 1.0, 3.0, 
47     -1.0, 1.0, 3.0, 
48     -1.0, -1.0, 3.0, 
49     1.0, -1.0, 3.0, 
50     1.0, 1.0, 4.0, 
51     -1.0, 1.0, 4.0, 
52     -1.0, -1.0, 4.0, 
53     1.0, -1.0, 4.0,
54     0.0, 0.0, 5.0
55   };
56   char nomcoo[3*MED_SNAME_SIZE+1] = "x               y               z               ";
57   char unicoo[3*MED_SNAME_SIZE+1] = "cm              cm              cm              ";
58   //char *nomnoe ;
59 //  med_int numnoe[19] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
60   med_int nufano[19] = {1,1,3,3,0,0,2,0,4,0,4,2,4,2,4,2,4,1,3};
61   
62   med_int ntet4 = 12;
63   med_int tet4[12*4] = {
64     1,2,3,6,
65     1,2,4,3,
66     1,2,5,4,
67     1,2,6,5,
68     2,7,4,3,
69     2,8,5,4,
70     2,9,6,5,
71     2,10,3,6,
72     2,7,3,10,
73     2,8,4,7,
74     2,9,5,8,
75     2,10,6,9
76   };
77   //char * nomtet4 ; 
78  // med_int numtet4[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
79   med_int nufatet4[12] = {-1,-1,-1,-1,0,0,0,0,0,0,-2,-3}; 
80
81   med_int npyra5 = 2;
82   med_int pyra5[5*2] = {
83     7,8,9,10,2,
84     15,18,17,16,19
85   };
86  // char nompyra5[MED_TAILLE_PNOM*2+1] = "pyra1           pyra2           ";
87  // med_int numpyra5[2] = {13,16};
88   med_int nufapyra5[2] = {0,-3};
89
90   med_int nhexa8 = 2;
91   med_int hexa8[8*2] = {
92     11,12,13,14,7,8,9,10,
93     15,16,17,18,11,12,13,14
94   };
95  // char nomhexa8[MED_TAILLE_PNOM*2+1] = "hexa1           hexa2           ";
96  // med_int numhexa8[2] = {14,15};
97   med_int nufahexa8[2] = {0,0};
98
99   char nomfam[MED_NAME_SIZE+1];
100   med_int numfam;
101   //char attdesMED_TAILLE_DESC+1];
102   med_int natt;
103   med_int attide;
104   med_int attval;
105   med_int ngro;
106   char gro[MED_LNAME_SIZE+1];
107   char gro2[MED_LNAME_SIZE*2+1];
108   char dtunitp3[MED_LNAME_SIZE+1]="";
109   int i;
110   int nfame = 3; 
111
112
113   /*
114     Some fields : 2 on nodes : one int and one double , one on cells : double
115    */
116   char champ1[MED_NAME_SIZE+1]="fieldnodeint" ;
117   char champ1_comp[MED_SNAME_SIZE+1]="comp1           " ;
118   char champ1_unit[MED_SNAME_SIZE+1]="M               " ;
119   med_int     fieldnodeint[19]    = {1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7};
120
121   char champ2[MED_NAME_SIZE+1]="fieldnodedouble" ;
122   char champ2_comp[MED_SNAME_SIZE+1]="comp1           " ;
123   char champ2_unit[MED_SNAME_SIZE+1]="J               " ;
124   med_float   fieldnodedouble1[19] = {1.,1.,1.,2.,2.,2.,3.,3.,3.,4.,4.,4.,5.,5.,5.,6.,6.,6.,7.};
125   med_float   fieldnodedouble2[19] = {1.,2.,2.,2.,3.,3.,3.,4.,4.,4.,5.,5.,5.,6.,6.,6.,7.,7.,7.};
126
127   char champ3[MED_NAME_SIZE+1]="fieldcelldoublevector" ;
128   char champ3_comp[MED_SNAME_SIZE*3+1]="comp1           comp2           comp3           " ;
129   char champ3_unit[MED_SNAME_SIZE*3+1]="m/s             m/s             m/s             " ;
130   med_float   fieldcelldouble1[12*3] = {1.,1.,1.,2.,2.,2.,3.,3.,3.,4.,4.,4.,0.,0.,0.,1.,1.,1.,0.,0.,0.,1.,1.,1.,1.,1.,1.,0.,0.,0.,1.,1.,1.,0.,0.,0.};
131   med_float   fieldcelldouble2[2*3]  = {5.,5.,0.,1.,0.,1.};
132   med_float   fieldcelldouble3[2*3]  = {6.,6.,1.,0.,1.,0.};
133
134   char champ4[MED_NAME_SIZE+1]="fieldcelldoublescalar" ;
135   char champ4_comp[MED_SNAME_SIZE+1]="comp1           " ;
136   char champ4_unit[MED_SNAME_SIZE+1]="m/s             " ;
137   med_float   fieldcelldouble4[12] = {1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.};
138   med_float   fieldcelldouble5[2]  = {2.,2.};
139   med_float   fieldcelldouble6[2]  = {3.,3.};
140
141   /***************************************************************************/
142   fid = MEDfileOpen("pointe.med",MED_ACC_RDWR);
143   if (fid < 0)
144     ret = -1;
145   else
146     ret = 0;
147   printf("MEDouvrir : %d\n",ret);
148
149   /***************************************************************************/
150   if (ret == 0)
151     //ret = MEDmeshCr(fid,maa,mdim,mdim,MED_UNSTRUCTURED_MESH,maadesc,dtunitp3,
152     //MED_SORT_DTIT,MED_CARTESIAN,nomcoo,unicoo);
153     ret = MEDmeshCr(fid,maa,mdim,mdim,MED_UNSTRUCTURED_MESH,maadesc,dtunitp3,
154                     MED_SORT_DTIT,MED_CARTESIAN,nomcoo,unicoo);
155   printf("MEDmaaCr : %d\n",ret);
156   if (ret == 0)
157     ret = MEDunvCr(fid,maa);
158   printf("MEDunvCr : %d\n",ret);
159
160   /***************************************************************************/
161   if (ret == 0)
162     ret = MEDmeshNodeCoordinateWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,MED_FULL_INTERLACE,nnoe,coo);
163   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NODE,0,nnoe,nufano);
164   printf("MEDnoeudsEcr : %d\n",ret);
165
166
167   /***************************************************************************/
168   /* ecriture des mailles MED_TETRA4 :
169      - connectivite
170      - noms (optionnel) 
171      - numeros (optionnel)
172      - numeros des familles */
173   if (ret == 0) 
174     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
175                                        MED_CELL,MED_TETRA4,MED_NODAL,MED_FULL_INTERLACE,
176                                        ntet4,tet4);
177   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_TETRA4,ntet4,nufatet4);
178   printf("MEDelementsEcr : %d \n",ret);
179
180   /* ecriture des mailles MED_PYRA5 :
181      - connectivite
182      - noms (optionnel) 
183      - numeros (optionnel)
184      - numeros des familles */
185   if (ret == 0) 
186     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
187                                        MED_CELL,MED_PYRA5,MED_NODAL,MED_FULL_INTERLACE,
188                                        npyra5,pyra5);
189   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_PYRA5,npyra5,nufapyra5);
190   printf("MEDelementsEcr : %d \n",ret);
191
192   /* ecriture des mailles MED_HEXA8 :
193      - connectivite
194      - noms (optionnel) 
195      - numeros (optionnel)
196      - numeros des familles */
197   if (ret == 0) 
198     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
199                                        MED_CELL,MED_HEXA8,MED_NODAL,MED_FULL_INTERLACE,
200                                        nhexa8,hexa8);
201   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_HEXA8,nhexa8,nufahexa8);
202   printf("MEDelementsEcr : %d \n",ret);
203
204   /***************************************************************************/
205   /* ecriture des familles */
206   /* Conventions :
207      - toujours creer une famille de numero 0 ne comportant aucun attribut
208        ni groupe (famille de reference pour les noeuds ou les elements
209        qui ne sont rattaches a aucun groupe ni attribut)
210      - les numeros de familles de noeuds sont > 0
211      - les numeros de familles des elements sont < 0
212      - rien d'imposer sur les noms de familles
213    */ 
214
215   /* la famille 0 */
216   if (ret == 0)
217     {
218       strcpy(nomfam,"FAMILLE_0");
219       numfam = 0;
220       ret = MEDfamilyCr(fid,maa,nomfam,numfam,0,gro);
221     }
222   printf("MEDfamCr : %d \n",ret);
223
224   /* on cree :
225       - 3 familles d'elements (-1,-2,-3) et 
226       - 2 familles de noeuds (1,2) 
227   */
228   if (ret == 0)
229     {
230       nfame = 3;
231       for (i=0;i<nfame;i++)
232         {
233           if (ret == 0)
234             {
235               strcpy(nomfam,"FAMILLE_ELEMENT_");
236               numfam = -(i+1);
237               sprintf(nomfam,"%s%d",nomfam,-numfam);
238               attide = 1;
239               attval = numfam*100;
240               natt = 1;
241               //strcpy(attdes,"description attribut");
242               strcpy(gro,"groupe1");
243               ngro = 1;
244               /*printf("nomfam : %s - numfam : %d - attide : %d - attval : %d - ngro : %d \n",nomfam,numfam,attide,attval,ngro);*/
245               ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
246               printf("MEDfamCr : %d\n",ret);
247             }
248         }
249     }
250   
251   if (ret == 0)
252     {
253       strcpy(nomfam,"FAMILLE_NOEUD_1");
254       numfam = 1;
255       attide = 1;
256       attval = numfam*100;
257       natt = 1;
258       //strcpy(attdes,"description attribut");
259       strcpy(gro2,"groupe2");
260       strcpy(gro2+MED_NAME_SIZE,"groupe3");
261       ngro = 2;
262       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro2);
263       printf("MEDfamCr : %d\n",ret);
264     }
265   if (ret == 0)
266     {
267       strcpy(nomfam,"FAMILLE_NOEUD_2");
268       numfam = 2;
269       attide = 1;
270       attval = numfam*100;
271       natt = 1;
272       //strcpy(attdes,"description attribut");
273       strcpy(gro2,"groupe3");
274       strcpy(gro2+MED_LNAME_SIZE,"groupe4");
275       ngro = 2;
276       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro2);
277       printf("MEDfamCr : %d\n",ret);
278     }
279   if (ret == 0)
280     {
281       strcpy(nomfam,"FAMILLE_NOEUD_3");
282       numfam = 3;
283       attide = 1;
284       attval = numfam*100;
285       natt = 1;
286       //strcpy(attdes,"description attribut");
287       strcpy(gro2,"groupe2");
288       strcpy(gro2+MED_LNAME_SIZE,"groupe4");
289       ngro = 2;
290       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro2);
291       printf("MEDfamCr : %d\n",ret);
292     }
293   if (ret == 0)
294     {
295       strcpy(nomfam,"FAMILLE_NOEUD_4");
296       numfam = 4;
297       attide = 1;
298       attval = numfam*100;
299       natt = 1;
300       //strcpy(attdes,"description attribut");
301       strcpy(gro,"groupe5");
302       ngro = 1;
303       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
304       printf("MEDfamCr : %d\n",ret);
305     }
306
307   /***************************************************************************/
308   /*
309     Les champs
310   */
311   if (ret == 0)
312     {
313       ret = MEDfieldCr(fid,champ1,MED_INT32,1,champ1_comp,champ1_unit,dtunitp3,maa);
314       printf("MEDchampCr : %d \n",ret);
315       if (ret == 0) {
316         ret = MEDfieldValueWr(fid, champ1,MED_NO_DT,MED_NO_IT,0.,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodeint);
317         
318         printf("MEDchampEcr : %d \n",ret);
319       }
320     }
321   
322   if (ret == 0)
323     {
324       ret = MEDfieldCr(fid,champ2,MED_FLOAT64,1,champ2_comp,champ2_unit,dtunitp3,maa);
325       printf("MEDchampCr : %d \n",ret);
326       if (ret == 0) {
327         ret = MEDfieldValueWr(fid, champ2,1,MED_NO_IT,1.1 ,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
328         printf("MEDchampEcr1 : %d \n",ret);
329         ret = MEDfieldValueWr(fid, champ2,2,MED_NO_IT,1.2 ,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble2);
330         printf("MEDchampEcr2 : %d \n",ret);
331       }
332     }
333   
334   /* on met champ2 sans pas de temps pour pouvoir le lire aussi par defaut !*/
335   if (ret == 0)
336     {
337 /*        ret = MEDfieldCr(fid,champ2,MED_REEL64,1,champ2_comp,champ2_unit,dtunitp3,maa); */
338 /*        printf("MEDchampCr : %d \n",ret); */
339       if (ret == 0) {
340         ret = MEDfieldValueWr(fid, champ2,MED_NO_DT,MED_NO_IT,0. ,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
341         printf("MEDchampEcr : %d \n",ret);
342       }
343     }
344
345   if (ret == 0)
346     {
347       ret = MEDfieldCr(fid,champ3,MED_FLOAT64,3,champ3_comp,champ3_unit,dtunitp3,maa);
348       printf("MEDchampCr : %d \n",ret);
349       if (ret == 0) {
350         ret = MEDfieldValueWr(fid, champ3,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_TETRA4,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,ntet4, (unsigned char *)fieldcelldouble1);
351         printf("MEDchampEcr : %d \n",ret);
352         ret = MEDfieldValueWr(fid, champ3,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_HEXA8,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nhexa8, (unsigned char *)fieldcelldouble3);
353         printf("MEDchampEcr : %d \n",ret);
354         ret = MEDfieldValueWr(fid, champ3,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_PYRA5,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,npyra5, (unsigned char *)fieldcelldouble2);
355         printf("MEDchampEcr : %d \n",ret);
356       }
357     }
358   
359   if (ret == 0)
360     {
361       ret = MEDfieldCr(fid,champ4,MED_FLOAT64,1,champ4_comp,champ4_unit,dtunitp3,maa);
362       printf("MEDchampCr : %d \n",ret);
363       if (ret == 0) {
364         ret = MEDfieldValueWr(fid, champ4,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_TETRA4,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,ntet4, (unsigned char *)fieldcelldouble4);
365         printf("MEDchampEcr : %d \n",ret);
366         ret = MEDfieldValueWr(fid, champ4,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_HEXA8,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nhexa8, (unsigned char *)fieldcelldouble6);
367         printf("MEDchampEcr : %d \n",ret);
368         ret = MEDfieldValueWr(fid, champ4,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_PYRA5,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,npyra5, (unsigned char *)fieldcelldouble5);
369         printf("MEDchampEcr : %d \n",ret);
370       }
371     }
372   
373   /***************************************************************************/
374   ret = MEDfermer(fid);
375   printf("MEDfermer : %d\n",ret);
376   
377   if ( getenv("srcdir") ) {
378     /* we are in 'make check' */
379     remove( "pointe.med" );
380     printf("Remove generated file");
381   }
382
383   return 0;
384 }
385