Salome HOME
sources v1.2
[modules/med.git] / src / MEDMEM / create_mesh.c
1 /*----------------------------------------------------------------------------
2 MED MEDMEM : MED files in memory
3
4  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
6
7  This library is free software; you can redistribute it and/or 
8  modify it under the terms of the GNU Lesser General Public 
9  License as published by the Free Software Foundation; either 
10  version 2.1 of the License. 
11
12  This library is distributed in the hope that it will be useful, 
13  but WITHOUT ANY WARRANTY; without even the implied warranty of 
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
15  Lesser General Public License for more details. 
16
17  You should have received a copy of the GNU Lesser General Public 
18  License along with this library; if not, write to the Free Software 
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
20
21  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
22
23
24
25   File   : create_mesh.c
26 Module : MED
27 ----------------------------------------------------------------------------*/
28
29 #include <med.h>
30 #include <string.h>
31
32 #define MED_NOPG   1                   /* -> pas de point de Gauss                    */
33 #define MED_NOPFL  ""                  /* -> pas de profils utilisateur               */
34 #define MED_NOPFLi "                                "  /* Variable Interne                      */
35 #define MED_NOPF   0                   /* -> pas de profils pour _MEDdataseNnumEcrire */
36 #define MED_NOPDT -1                   /* rem: pas de pas de temps negatifs           */
37 #define MED_NONOR -1                   /* rem: pas de nĀ°ordre negatif                 */
38 #define MED_DIM1   1                   /* PAS */
39
40 #define MED_ALL    0
41
42 int main (int argc, char **argv)
43 {
44   med_err ret;
45   med_idt fid;
46   char maa[MED_TAILLE_NOM+1] = "maa1";
47   med_int mdim = 3;
48   med_int nnoe = 19;
49   med_float coo[57] = {
50     0.0, 0.0, 0.0, 
51     0.0, 0.0, 1.0, 
52     2.0, 0.0, 1.0, 
53     0.0, 2.0, 1.0, 
54     -2.0, 0.0, 1.0, 
55     0.0, -2.0, 1.0, 
56     1.0, 1.0, 2.0, 
57     -1.0, 1.0, 2.0, 
58     -1.0, -1.0, 2.0, 
59     1.0, -1.0, 2.0, 
60     1.0, 1.0, 3.0, 
61     -1.0, 1.0, 3.0, 
62     -1.0, -1.0, 3.0, 
63     1.0, -1.0, 3.0, 
64     1.0, 1.0, 4.0, 
65     -1.0, 1.0, 4.0, 
66     -1.0, -1.0, 4.0, 
67     1.0, -1.0, 4.0,
68     0.0, 0.0, 5.0
69   };
70   char nomcoo[3*MED_TAILLE_PNOM+1] = "x       y       z       ";
71   char unicoo[3*MED_TAILLE_PNOM+1] = "cm      cm      cm      ";
72   /*  char nomnoe[19*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] = {1,1,3,3,0,0,2,0,4,0,4,2,4,2,4,2,4,1,3};
76   
77   med_int ntet4 = 12;
78   med_int tet4[12*4] = {
79     1,2,3,6,
80     1,2,4,3,
81     1,2,5,4,
82     1,2,6,5,
83     2,7,4,3,
84     2,8,5,4,
85     2,9,6,5,
86     2,10,3,6,
87     2,7,3,10,
88     2,8,4,7,
89     2,9,5,8,
90     2,10,6,9
91   };
92   char * nomtet4 ; 
93   med_int numtet4[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
94   med_int nufatet4[12] = {-1,-1,-1,-1,0,0,0,0,0,0,-2,-3}; 
95
96   med_int npyra5 = 2;
97   med_int pyra5[5*2] = {
98     7,8,9,10,2,
99     15,18,17,16,19
100   };
101   char nompyra5[MED_TAILLE_PNOM*2+1] = "pyra1   pyra2   ";
102   med_int numpyra5[2] = {13,16};
103   med_int nufapyra5[2] = {0,-3};
104
105   med_int nhexa8 = 2;
106   med_int hexa8[8*2] = {
107     11,12,13,14,7,8,9,10,
108     15,16,17,18,11,12,13,14
109   };
110   char nomhexa8[MED_TAILLE_PNOM*2+1] = "hexa1   hexa2   ";
111   med_int numhexa8[2] = {14,15};
112   med_int nufahexa8[2] = {0,0};
113
114   char nomfam[MED_TAILLE_NOM+1];
115   med_int numfam;
116   char attdes[MED_TAILLE_DESC+1];
117   med_int natt;
118   med_int attide;
119   med_int attval;
120   med_int ngro;
121   char gro[MED_TAILLE_LNOM+1];
122   char gro2[MED_TAILLE_LNOM*2+1];
123   char gro3[MED_TAILLE_LNOM*3+1];
124   int i;
125   int nfame = 3; 
126   int nfamn = 2;
127
128
129   /*
130     Some fields : 2 on nodes : one int and one double , one on cells : double
131    */
132   char champ1[MED_TAILLE_NOM+1]="fieldnodeint" ;
133   char champ1_comp[MED_TAILLE_PNOM+1]="comp1   " ;
134   char champ1_unit[MED_TAILLE_PNOM+1]="M       " ;
135   med_int     fieldnodeint[19]    = {1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7};
136
137   char champ2[MED_TAILLE_NOM+1]="fieldnodedouble" ;
138   char champ2_comp[MED_TAILLE_PNOM+1]="comp1   " ;
139   char champ2_unit[MED_TAILLE_PNOM+1]="J       " ;
140   med_float   fieldnodedouble1[19] = {1.,1.,1.,2.,2.,2.,3.,3.,3.,4.,4.,4.,5.,5.,5.,6.,6.,6.,7.};
141   med_float   fieldnodedouble2[19] = {1.,2.,2.,2.,3.,3.,3.,4.,4.,4.,5.,5.,5.,6.,6.,6.,7.,7.,7.};
142
143   char champ3[MED_TAILLE_NOM+1]="fieldcelldoublevector" ;
144   char champ3_comp[MED_TAILLE_PNOM*3+1]="comp1   comp2   comp3   " ;
145   char champ3_unit[MED_TAILLE_PNOM*3+1]="m/s     m/s     m/s     " ;
146   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.};
147   med_float   fieldcelldouble2[2*3]  = {5.,5.,0.,1.,0.,1.};
148   med_float   fieldcelldouble3[2*3]  = {6.,6.,1.,0.,1.,0.};
149
150   char champ4[MED_TAILLE_NOM+1]="fieldcelldoublescalar" ;
151   char champ4_comp[MED_TAILLE_PNOM+1]="comp1   " ;
152   char champ4_unit[MED_TAILLE_PNOM+1]="m/s     " ;
153   med_float   fieldcelldouble4[12] = {1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.};
154   med_float   fieldcelldouble5[2]  = {2.,2.};
155   med_float   fieldcelldouble6[2]  = {3.,3.};
156
157   /***************************************************************************/
158   fid = MEDouvrir("pointe.med",MED_REMP);
159   if (fid < 0)
160     ret = -1;
161   else
162     ret = 0;
163   printf("MEDouvrir : %d\n",ret);
164
165   /***************************************************************************/
166   if (ret == 0)
167     ret = MEDmaaCr(fid,maa,mdim);
168   printf("MEDmaaCr : %d\n",ret);
169   if (ret == 0)
170     ret = MEDunvCr(fid,maa);
171   printf("MEDunvCr : %d\n",ret);
172
173   /***************************************************************************/
174   if (ret == 0)
175     ret = MEDnoeudsEcr(fid,maa,mdim,coo,MED_FULL_INTERLACE,MED_CART,
176                        nomcoo,unicoo,nomnoe,MED_FAUX,numnoe,MED_VRAI,
177                        nufano,nnoe,MED_ECRI);
178   printf("MEDnoeudsEcr : %d\n",ret);
179
180
181   /***************************************************************************/
182   /* ecriture des mailles MED_TETRA4 :
183      - connectivite
184      - noms (optionnel) 
185      - numeros (optionnel)
186      - numeros des familles */
187   if (ret == 0) 
188     ret = MEDelementsEcr(fid,maa,mdim,tet4,MED_FULL_INTERLACE,
189                          nomtet4,MED_FAUX,numtet4,MED_VRAI,nufatet4,ntet4,
190                          MED_MAILLE,MED_TETRA4,MED_NOD,MED_ECRI);
191   printf("MEDelementsEcr : %d \n",ret);
192
193   /* ecriture des mailles MED_PYRA5 :
194      - connectivite
195      - noms (optionnel) 
196      - numeros (optionnel)
197      - numeros des familles */
198   if (ret == 0) 
199     ret = MEDelementsEcr(fid,maa,mdim,pyra5,MED_FULL_INTERLACE,
200                          nompyra5,MED_VRAI,numpyra5,MED_VRAI,nufapyra5,npyra5,
201                          MED_MAILLE,MED_PYRA5,MED_NOD,MED_ECRI);
202   printf("MEDelementsEcr : %d \n",ret);
203
204   /* ecriture des mailles MED_HEXA8 :
205      - connectivite
206      - noms (optionnel) 
207      - numeros (optionnel)
208      - numeros des familles */
209   if (ret == 0) 
210     ret = MEDelementsEcr(fid,maa,mdim,hexa8,MED_FULL_INTERLACE,
211                          nomhexa8,MED_FAUX,numhexa8,MED_VRAI,nufahexa8,nhexa8,
212                          MED_MAILLE,MED_HEXA8,MED_NOD,MED_ECRI);
213   printf("MEDelementsEcr : %d \n",ret);
214
215   /***************************************************************************/
216   /* ecriture des familles */
217   /* Conventions :
218      - toujours creer une famille de numero 0 ne comportant aucun attribut
219        ni groupe (famille de reference pour les noeuds ou les elements
220        qui ne sont rattaches a aucun groupe ni attribut)
221      - les numeros de familles de noeuds sont > 0
222      - les numeros de familles des elements sont < 0
223      - rien d'imposer sur les noms de familles
224    */ 
225
226   /* la famille 0 */
227   if (ret == 0)
228     {
229       strcpy(nomfam,"FAMILLE_0");
230       numfam = 0;
231       ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,0,
232                      gro,0);
233     }
234   printf("MEDfamCr : %d \n",ret);
235
236   /* on cree :
237       - 3 familles d'elements (-1,-2,-3) et 
238       - 2 familles de noeuds (1,2) 
239   */
240   if (ret == 0)
241     {
242       nfame = 3;
243       for (i=0;i<nfame;i++)
244         {
245           if (ret == 0)
246             {
247               strcpy(nomfam,"FAMILLE_ELEMENT_");
248               numfam = -(i+1);
249               sprintf(nomfam,"%s%d",nomfam,-numfam);
250               attide = 1;
251               attval = numfam*100;
252               natt = 1;
253               strcpy(attdes,"description attribut");
254               strcpy(gro,"groupe1");
255               ngro = 1;
256               /*printf("nomfam : %s - numfam : %d - attide : %d - attval : %d - ngro : %d \n",nomfam,numfam,attide,attval,ngro);*/
257               ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,
258                              natt,gro,ngro);
259               printf("MEDfamCr : %d\n",ret);
260             }
261         }
262     }
263   
264   if (ret == 0)
265     {
266       strcpy(nomfam,"FAMILLE_NOEUD_1");
267       numfam = 1;
268       attide = 1;
269       attval = numfam*100;
270       natt = 1;
271       strcpy(attdes,"description attribut");
272       strcpy(gro2,"groupe2");
273       strcpy(gro2+MED_TAILLE_LNOM,"groupe3");
274       ngro = 2;
275       ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,
276                      natt,gro2,ngro);
277       printf("MEDfamCr : %d\n",ret);
278     }
279   if (ret == 0)
280     {
281       strcpy(nomfam,"FAMILLE_NOEUD_2");
282       numfam = 2;
283       attide = 1;
284       attval = numfam*100;
285       natt = 1;
286       strcpy(attdes,"description attribut");
287       strcpy(gro2,"groupe3");
288       strcpy(gro2+MED_TAILLE_LNOM,"groupe4");
289       ngro = 2;
290       ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,
291                      natt,gro2,ngro);
292       printf("MEDfamCr : %d\n",ret);
293     }
294   if (ret == 0)
295     {
296       strcpy(nomfam,"FAMILLE_NOEUD_3");
297       numfam = 3;
298       attide = 1;
299       attval = numfam*100;
300       natt = 1;
301       strcpy(attdes,"description attribut");
302       strcpy(gro2,"groupe2");
303       strcpy(gro2+MED_TAILLE_LNOM,"groupe4");
304       ngro = 2;
305       ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,
306                      natt,gro2,ngro);
307       printf("MEDfamCr : %d\n",ret);
308     }
309   if (ret == 0)
310     {
311       strcpy(nomfam,"FAMILLE_NOEUD_4");
312       numfam = 4;
313       attide = 1;
314       attval = numfam*100;
315       natt = 1;
316       strcpy(attdes,"description attribut");
317       strcpy(gro,"groupe5");
318       ngro = 1;
319       ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,
320                      natt,gro,ngro);
321       printf("MEDfamCr : %d\n",ret);
322     }
323
324   /***************************************************************************/
325   /*
326     Les champs
327   */
328   if (ret == 0)
329     {
330       ret = MEDchampCr(fid,champ1,MED_INT32,champ1_comp,champ1_unit,1);
331       printf("MEDchampCr : %d \n",ret);
332       if (ret == 0) {
333         ret = MEDchampEcr(fid, maa, champ1, (unsigned char *)fieldnodeint,
334                           MED_NO_INTERLACE, nnoe,
335                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_NOEUD, 
336                           0, MED_NOPDT,"        ", 0., MED_NONOR);
337         
338         printf("MEDchampEcr : %d \n",ret);
339       }
340     }
341   
342   if (ret == 0)
343     {
344       ret = MEDchampCr(fid,champ2,MED_REEL64,champ2_comp,champ2_unit,1);
345       printf("MEDchampCr : %d \n",ret);
346       if (ret == 0) {
347         ret = MEDchampEcr(fid, maa, champ2, (unsigned char *)fieldnodedouble1,
348                           MED_NO_INTERLACE, nnoe,
349                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_NOEUD, 
350                           0, 1,"S       ", 1.1 , MED_NONOR);
351         printf("MEDchampEcr1 : %d \n",ret);
352         ret = MEDchampEcr(fid, maa, champ2, (unsigned char *)fieldnodedouble2,
353                           MED_NO_INTERLACE, nnoe,
354                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_NOEUD, 
355                           0, 2,"S       ", 1.2 , MED_NONOR);
356         printf("MEDchampEcr2 : %d \n",ret);
357       }
358     }
359   
360   // on met champ2 sans pas de temps pour pouvoir le lire aussi par defaut !
361   if (ret == 0)
362     {
363 /*        ret = MEDchampCr(fid,champ2,MED_REEL64,champ2_comp,champ2_unit,1); */
364 /*        printf("MEDchampCr : %d \n",ret); */
365       if (ret == 0) {
366         ret = MEDchampEcr(fid, maa, champ2, (unsigned char *)fieldnodedouble1,
367                           MED_NO_INTERLACE, nnoe,
368                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_NOEUD, 
369                           0, MED_NOPDT,"        ", 0. , MED_NONOR);
370         printf("MEDchampEcr : %d \n",ret);
371       }
372     }
373
374   if (ret == 0)
375     {
376       ret = MEDchampCr(fid,champ3,MED_REEL64,champ3_comp,champ3_unit,3);
377       printf("MEDchampCr : %d \n",ret);
378       if (ret == 0) {
379         ret = MEDchampEcr(fid, maa, champ3, (unsigned char *)fieldcelldouble1,
380                           MED_NO_INTERLACE, ntet4,
381                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_MAILLE, 
382                           MED_TETRA4, MED_NOPDT,"        ", 0., MED_NONOR);
383         printf("MEDchampEcr : %d \n",ret);
384         ret = MEDchampEcr(fid, maa, champ3, (unsigned char *)fieldcelldouble3,
385                           MED_NO_INTERLACE, nhexa8,
386                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_MAILLE, 
387                           MED_HEXA8, MED_NOPDT,"        ", 0., MED_NONOR);
388         printf("MEDchampEcr : %d \n",ret);
389         ret = MEDchampEcr(fid, maa, champ3, (unsigned char *)fieldcelldouble2,
390                           MED_NO_INTERLACE, npyra5,
391                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_MAILLE, 
392                           MED_PYRA5, MED_NOPDT,"        ", 0., MED_NONOR);
393         printf("MEDchampEcr : %d \n",ret);
394       }
395     }
396   
397   if (ret == 0)
398     {
399       ret = MEDchampCr(fid,champ4,MED_REEL64,champ4_comp,champ4_unit,1);
400       printf("MEDchampCr : %d \n",ret);
401       if (ret == 0) {
402         ret = MEDchampEcr(fid, maa, champ4, (unsigned char *)fieldcelldouble4,
403                           MED_NO_INTERLACE, ntet4,
404                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_MAILLE, 
405                           MED_TETRA4, MED_NOPDT,"        ", 0., MED_NONOR);
406         printf("MEDchampEcr : %d \n",ret);
407         ret = MEDchampEcr(fid, maa, champ4, (unsigned char *)fieldcelldouble6,
408                           MED_NO_INTERLACE, nhexa8,
409                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_MAILLE, 
410                           MED_HEXA8, MED_NOPDT,"        ", 0., MED_NONOR);
411         printf("MEDchampEcr : %d \n",ret);
412         ret = MEDchampEcr(fid, maa, champ4, (unsigned char *)fieldcelldouble5,
413                           MED_NO_INTERLACE, npyra5,
414                           MED_NOPG, MED_ALL, MED_NOPFL, MED_ECRI, MED_MAILLE, 
415                           MED_PYRA5, MED_NOPDT,"        ", 0., MED_NONOR);
416         printf("MEDchampEcr : %d \n",ret);
417       }
418     }
419   
420   /***************************************************************************/
421   ret = MEDfermer(fid);
422   printf("MEDfermer : %d\n",ret);
423   
424   return 0;
425 }
426