Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEMBinTest / create_mesh_c3h8.c
1 //  Copyright (C) 2007-2008  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   creation d'une geometrie 3d : un cube [0,1]^3
24   maillĂ© uniformement en hexahedres reguliers;
25   avec en plus une partie des faces (une partie
26   des faces de la frontiere) du maillage.
27   ATTENTION : 3 noeuds dans chaque direction
28 */
29
30 #include <med.h>
31 #include <string.h>
32
33 int main (int argc, char **argv)
34 {
35   med_err ret;
36   med_idt fid;
37   char maa[MED_TAILLE_NOM+1] = "CUBE_EN_HEXA8";
38   char maadesc[MED_TAILLE_DESC+1] = "Example de maillage non structure 3D";
39   med_int mdim = 3;
40   med_int nnoe = 27;
41   /*
42     les noeuds:
43   */
44   med_float coo[81] = {
45     0.0, 0.0, 0.0, 
46     0.5, 0.0, 0.0, 
47     1.0, 0.0, 0.0, 
48     0.0, 0.5, 0.0, 
49     0.5, 0.5, 0.0, 
50     1.0, 0.5, 0.0, 
51     0.0, 1.0, 0.0, 
52     0.5, 1.0, 0.0, 
53     1.0, 1.0, 0.0, 
54     0.0, 0.0, 0.5, 
55     0.5, 0.0, 0.5, 
56     1.0, 0.0, 0.5, 
57     0.0, 0.5, 0.5, 
58     0.5, 0.5, 0.5, 
59     1.0, 0.5, 0.5, 
60     0.0, 1.0, 0.5, 
61     0.5, 1.0, 0.5, 
62     1.0, 1.0, 0.5, 
63     0.0, 0.0, 1.0, 
64     0.5, 0.0, 1.0, 
65     1.0, 0.0, 1.0, 
66     0.0, 0.5, 1.0, 
67     0.5, 0.5, 1.0, 
68     1.0, 0.5, 1.0, 
69     0.0, 1.0, 1.0, 
70     0.5, 1.0, 1.0, 
71     1.0, 1.0, 1.0
72   };
73   char nomcoo[3*MED_TAILLE_PNOM+1] = "x               y               z               ";
74   char unicoo[3*MED_TAILLE_PNOM+1] = "cm              cm              cm              ";
75   /*  char nomnoe[19*MED_TAILLE_PNOM+1] = "nom1    nom2    nom3    nom4";*/
76   char *nomnoe ;
77   med_int numnoe[27] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27};
78   med_int nufano[27] = {1,1,1,1,1,1,1,1,1,3,3,3,3,0,3,3,3,3,2,2,2,2,2,2,2,2,2};
79   /*
80     les elements:
81   */
82   med_int nhexa8 = 8;
83   med_int hexa8[64] = {
84     4, 13, 14, 5, 1, 10, 11, 2,
85     5, 14, 15, 6, 2, 11, 12, 3,
86     7, 16, 17, 8, 4, 13, 14, 5,
87     8, 17, 18, 9, 5, 14, 15, 6,
88     13, 22, 23, 14, 10, 19, 20, 11,
89     14, 23, 24, 15, 11, 20, 21, 12,
90     16, 25, 26, 17, 13, 22, 23, 14,
91     17, 26, 27, 18, 14, 23, 24, 15
92   };
93   char nomhexa8[MED_TAILLE_PNOM*8+1] = "hexa1           hexa2           hexa3           hexa4           hexa5           hexa6           hexa7           hexa8           ";
94   med_int numhexa8[8] = {1,2,3,4,5,6,7,8};
95   med_int nufahexa8[8] = {-1,-1,-1,-1,-2,-2,-2,-2};
96
97   char nomfam[MED_TAILLE_NOM+1];
98   med_int numfam;
99   char attdes[MED_TAILLE_DESC+1];
100   med_int natt;
101   med_int attide;
102   med_int attval;
103   med_int ngro;
104   char gro[MED_TAILLE_LNOM+1];
105   int i;
106   int nfame = 1; 
107   int nfamn = 1;
108   int nfamf = 1;
109
110   /*
111     Some fields : 2 on nodes : one int and one double , one on cells : double
112   */
113
114   char champ1[MED_TAILLE_NOM+1]="fieldnodeint" ;
115   char champ1_comp[MED_TAILLE_PNOM+1]="comp1           " ;
116   char champ1_unit[MED_TAILLE_PNOM+1]="M               " ;
117   med_int fieldnodeint[27] = {1,1,3,2,2,3,4,4,5,1,1,3,2,2,3,4,4,5,1,1,3,2,2,3,4,4,5};
118
119   char champ2[MED_TAILLE_NOM+1]="fieldnodedouble" ;
120   char champ2_comp[MED_TAILLE_PNOM+1]="comp1           " ;
121   char champ2_unit[MED_TAILLE_PNOM+1]="J               " ;
122   med_float fieldnodedouble1[27] = {1.,3.,4.,1.,3.,4.,3.,2.,5.,1.,3.,4.,1.,3.,4.,3.,2.,5.,1.,3.,4.,1.,3.,4.,3.,2.,5.};
123   med_float fieldnodedouble2[27] = {1.,2.,2.,3.,3.,3.,4.,4.,5.,1.,2.,2.,3.,3.,3.,4.,4.,5.,1.,2.,2.,3.,3.,3.,4.,4.,5.};
124
125   char champ3[MED_TAILLE_NOM+1]="fieldcelldouble" ;
126   char champ3_comp[MED_TAILLE_PNOM*3+1]="comp1           comp2           comp3           " ;
127   char champ3_unit[MED_TAILLE_PNOM*3+1]="M/S             m/s             m/s             " ;
128   med_float fieldcelldouble[8*3] = {0.,1.,1.,1.,1.,2.,2.,3.,0.,1.,1.,1.,1.,2.,2.,3.,0.,1.,1.,1.,1.,2.,2.,3.};
129
130   /***************************************************************************/
131   fid = MEDouvrir("cube_hexa8.med",MED_LECTURE_ECRITURE);
132   if (fid < 0)
133     ret = -1;
134   else
135     ret = 0;
136   printf("MEDouvrir : %d\n",ret);
137
138   /***************************************************************************/
139   if (ret == 0)
140     ret = MEDmaaCr(fid,maa,mdim,MED_NON_STRUCTURE,maadesc);
141   printf("MEDmaaCr : %d\n",ret);
142   if (ret == 0)
143     ret = MEDunvCr(fid,maa);
144   printf("MEDunvCr : %d\n",ret);
145
146   /***************************************************************************/
147   if (ret == 0)
148     ret = MEDnoeudsEcr(fid,maa,mdim,coo,MED_FULL_INTERLACE,MED_CART,
149                        nomcoo,unicoo,nomnoe,MED_FAUX,numnoe,MED_VRAI,
150                        nufano,nnoe);
151   printf("MEDnoeudsEcr : %d\n",ret);
152
153   /* ecriture des mailles MED_HEXA8 :
154      - connectivite
155      - noms (optionnel) 
156      - numeros (optionnel)
157      - numeros des familles */
158   if (ret == 0) 
159     ret = MEDelementsEcr(fid,maa,mdim,hexa8,MED_FULL_INTERLACE,
160                          nomhexa8,MED_FAUX,numhexa8,MED_VRAI,nufahexa8,nhexa8,
161                          MED_MAILLE,MED_HEXA8,MED_NOD);
162   printf("MEDelementsEcr : %d \n",ret);
163
164   /***************************************************************************/
165   /* ecriture des familles */
166   /* Conventions :
167      - toujours creer une famille de numero 0 ne comportant aucun attribut
168        ni groupe (famille de reference pour les noeuds ou les elements
169        qui ne sont rattaches a aucun groupe ni attribut)
170      - les numeros de familles de noeuds sont > 0
171      - les numeros de familles des elements sont < 0
172      - rien d'imposer sur les noms de familles
173    */ 
174
175   /* la famille 0 */
176   if (ret == 0)
177     {
178       strcpy(nomfam,"FAMILLE_0");
179       numfam = 0;
180       ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,0,
181                      gro,0);
182     }
183   printf("%d \n",ret);
184
185   /* on cree :
186       - 2 familles d'elements (-1,-2) et 
187       - 3 familles de noeuds (1,2,3) 
188   */
189
190   if (ret == 0)
191     {
192       nfame = 2;
193       for (i=0;i<nfame;i++)
194         {
195           if (ret == 0)
196             {
197               strcpy(nomfam,"FAMILLE_ELEMENT_");
198               numfam = -(i+1);
199               sprintf(nomfam,"%s%d",nomfam,-numfam);
200               attide = 1;
201               attval = numfam*100;
202               natt = 1;
203               strcpy(attdes,"description attribut");
204               strcpy(gro,"groupe1");
205               ngro = 1;
206
207               /*printf("nomfam : %s - numfam : %d - attide : %d - attval : %d - ngro : %d \n",nomfam,numfam,attide,attval,ngro);*/
208
209               ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,
210                              natt,gro,ngro);
211               printf("MEDfamCr : %d\n",ret);
212             }
213         }
214     }
215   
216   if (ret == 0)
217     {
218       nfamn = 3;
219       for (i=0;i<nfamn;i++)
220         {
221           if(ret == 0)
222             {
223               strcpy(nomfam,"FAMILLE_NOEUD_");
224               numfam = i+1;
225               sprintf(nomfam,"%s%d",nomfam,numfam);
226               attide = 1;
227               attval = numfam*100;
228               natt = 1;
229               strcpy(attdes,"description attribut");
230               strcpy(gro,"groupe2");
231               ngro = 1;
232               ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,
233                              natt,gro,ngro);
234               printf("MEDfamCr : %d\n",ret);
235             }
236         }
237     }
238
239   /***************************************************************************/
240   /*
241     Les champs
242   */
243   if (ret == 0)
244     {
245       ret = MEDchampCr(fid,champ1,MED_INT32,champ1_comp,champ1_unit,1);
246       printf("MEDchampCr : %d \n",ret);
247       if (ret == 0) {
248         ret = MEDchampEcr(fid, maa, champ1, (unsigned char *)fieldnodeint,
249                           MED_FULL_INTERLACE, nnoe, MED_NOGAUSS, MED_ALL,
250                           MED_NOPFL, MED_NO_PFLMOD, MED_NOEUD, 0,
251                           MED_NOPDT,"        ", 0., MED_NONOR);
252         
253         printf("MEDchampEcr : %d \n",ret);
254       }
255     }
256   
257   if (ret == 0)
258     {
259       ret = MEDchampCr(fid,champ2,MED_FLOAT64,champ2_comp,champ2_unit,1);
260       printf("MEDchampCr : %d \n",ret);
261       if (ret == 0) {
262         ret = MEDchampEcr(fid, maa, champ2, (unsigned char *)fieldnodedouble1,
263                           MED_FULL_INTERLACE, nnoe, MED_NOGAUSS, MED_ALL,
264                           MED_NOPFL, MED_NO_PFLMOD, MED_NOEUD, 0,
265                           1,"S       ", 1.1 , MED_NONOR);
266         printf("MEDchampEcr1 : %d \n",ret);
267         ret = MEDchampEcr(fid, maa, champ2, (unsigned char *)fieldnodedouble2,
268                           MED_FULL_INTERLACE, nnoe, MED_NOGAUSS, MED_ALL,
269                           MED_NOPFL, MED_NO_PFLMOD, MED_NOEUD, 0,
270                           2,"S       ", 1.2 , MED_NONOR);
271         printf("MEDchampEcr2 : %d \n",ret);
272       }
273     }
274   
275   /* on met champ2 sans pas de temps pour pouvoir le lire aussi par defaut !*/
276   if (ret == 0)
277     {
278       ret = MEDchampEcr(fid, maa, champ2, (unsigned char *)fieldnodedouble1,
279                         MED_FULL_INTERLACE, nnoe, MED_NOGAUSS, MED_ALL,
280                         MED_NOPFL, MED_NO_PFLMOD, MED_NOEUD, 0,
281                         MED_NOPDT,"        ", 0. , MED_NONOR);
282       printf("MEDchampEcr : %d \n",ret); 
283     }
284
285   if (ret == 0)
286     {
287       ret = MEDchampCr(fid,champ3,MED_FLOAT64,champ3_comp,champ3_unit,3);
288       printf("MEDchampCr : %d \n",ret);
289       if (ret == 0) {
290         ret = MEDchampEcr(fid, maa, champ3, (unsigned char *)fieldcelldouble,
291                           MED_FULL_INTERLACE, nhexa8, MED_NOGAUSS, MED_ALL,
292                           MED_NOPFL, MED_NO_PFLMOD, MED_MAILLE, MED_HEXA8,
293                           MED_NOPDT,"        ", 0., MED_NONOR);
294         printf("MEDchampEcr : %d \n",ret);
295       }
296     }
297
298   /***************************************************************************/
299
300   ret = MEDfermer(fid);
301   printf("%d\n",ret);
302   
303   if ( getenv("srcdir") ) 
304     /* we are in 'make check' */
305     remove( "cube_hexa8.med" );
306
307   return 0;
308 }