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