Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MedClient / src / create_mesh_c2t3.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 2d : un cube [0,1]^2
24   maillé uniformement en triangles reguliers;
25   avec n (=argv[1]) noeuds dans chaque direction.
26   2 champs:
27   - DbleVectNode champ vectoriel reel sur les noeuds
28   - DbleVectCell champ vectoriel reel sur les cellules
29
30   En sortie, il y aura production d'un fichier MED
31   carre_tria3_n.med qui contiendra un seul maillage et 2 champs
32   avec une seule famille la FAMILLE_0
33 */
34
35 #include <med.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 #include <time.h>
40
41 int main (int argc, char **argv)
42 {
43   med_err ret;
44   med_idt fid;
45   char maa[MED_TAILLE_NOM+1] = "carre_tria3";
46   med_int mdim = 2;
47   int nnoe_dir;
48   int nelt_dir;
49   med_int nnoe;
50   int i, j, ij;
51
52   med_float * coo;
53   med_int * numnoe;
54   med_int * nufano;
55
56   med_float hxsize;
57   med_float hysize;
58
59   med_float * DbleVectNode;
60   med_float * DbleVectCell;
61
62   time_t t1;
63
64   /*
65     Le maillage
66    */
67
68   char nomcoo[2*MED_TAILLE_PNOM+1] = "x       y       ";
69   char unicoo[2*MED_TAILLE_PNOM+1] = "cm      cm      ";
70   /*  char nomnoe[19*MED_TAILLE_PNOM+1] = "nom1    nom2    nom3    nom4";*/
71   char *nomnoe ;
72
73   med_int ntria3;
74   med_int * tria3;
75   med_int * numtria3;
76   med_int * nufatria3;
77   char * nomtria3;
78   int indexN1, indexN2, indexN3, indexN4;
79
80   char nomfam[MED_TAILLE_NOM+1];
81   med_int numfam;
82   char attdes[MED_TAILLE_DESC+1];
83   med_int natt;
84   med_int attide;
85   med_int attval;
86   med_int ngro;
87   char gro[MED_TAILLE_LNOM+1];
88   int nfame = 0; 
89   int nfamn = 0;
90
91   char MedFile[100] = "carre_tria3_";
92   char buff[100];
93
94   /*
95     Les champs
96   */
97
98   char champDbleVectNode[MED_TAILLE_NOM+1] = "DbleVectNode";
99   char compDbleVectNode[MED_TAILLE_PNOM*2+1] = "comp1   comp2   " ;
100   char unitDbleVectNode[MED_TAILLE_PNOM*2+1] = "unit1   unit2   " ;
101
102   char champDbleVectCell[MED_TAILLE_NOM+1] = "DbleVectCell";
103   char compDbleVectCell[MED_TAILLE_PNOM*2+1] = "comp1   comp2   " ;
104   char unitDbleVectCell[MED_TAILLE_PNOM*2+1] = "unit1   unit2   " ;
105
106   if (argc != 2)
107     {
108       printf("Usage: %s <n> \n",argv[0]);
109       printf("       where\n");
110       printf("       - n is the number of nodes in each direction.\n");
111       printf("\n");
112       printf("This program will produce a MED file carre_tria3_n.med\n");
113       exit(0);
114     }
115
116   nnoe_dir = atoi(argv[1]);
117   nelt_dir = nnoe_dir-1;
118   nnoe = nnoe_dir*nnoe_dir;
119
120   coo = malloc(mdim*nnoe*sizeof(med_float));
121   numnoe = malloc(nnoe*sizeof(med_int));
122   nufano = malloc(nnoe*sizeof(med_int));
123   nomnoe = malloc((MED_TAILLE_PNOM*nnoe+1)*sizeof(char));
124
125   hxsize = 1./((med_float) (nnoe_dir - 1));
126   hysize = hxsize;
127
128   ntria3 = 2*nelt_dir*nelt_dir;
129   tria3 = malloc(3*ntria3*sizeof(med_int));
130   numtria3 = malloc(ntria3*sizeof(med_int));
131   nufatria3 = malloc(ntria3*sizeof(med_int));
132   nomtria3 = malloc((MED_TAILLE_PNOM*ntria3+1)*sizeof(char));
133
134   DbleVectNode = malloc(mdim*nnoe*sizeof(med_float));
135   DbleVectCell = malloc(mdim*ntria3*sizeof(med_float));
136
137   /*
138     les noeuds:
139   */
140
141   for(j=0;j<nnoe_dir;j++)
142     {
143       for (i=0;i<nnoe_dir;i++)
144         {
145           int ij = j*nnoe_dir+i;
146
147           numnoe[ij] = ij+1;
148           nufano[ij] = 0;
149
150           coo[mdim*ij] = ((med_float) i)*hxsize;
151           coo[mdim*ij+1] = ((med_float) j)*hysize;
152
153           /*
154           printf("Coordonnées %d   X = %lf  Y = %lf\n",(ij+1),coo[mdim*ij],coo[mdim*ij+1]);
155           */
156         }
157     }
158
159   /*
160     les elements:
161   */
162
163   for(j=0;j<nelt_dir;j++)
164     {
165       for (i=0;i<nelt_dir;i++)
166         {
167           int ij = j*nelt_dir+i;
168           int ij1 = 2*ij;
169           int ij2 = ij1+1;
170
171           numtria3[ij1] = ij1+1;
172           numtria3[ij2] = ij2+1;
173
174           nufatria3[ij1] = 0;
175           nufatria3[ij2] = 0;
176
177           indexN4 = j*nnoe_dir+i+1;
178           indexN3 = indexN4+1;
179           indexN1 = indexN4+nnoe_dir;
180           indexN2 = indexN3+nnoe_dir;
181
182           tria3[3*ij1] = indexN1;
183           tria3[3*ij1+1] = indexN2;
184           tria3[3*ij1+2] = indexN3;
185
186           tria3[3*ij2] = indexN1;
187           tria3[3*ij2+1] = indexN3;
188           tria3[3*ij2+2] = indexN4;
189         }
190     }
191
192   /*
193   for (i=0;i<ntria3;i++)
194     {
195       printf("Connectivitée %d  i1 = %d  i2 = %d  i3 = %d\n",(i+1),tria3[3*i],tria3[3*i+1],tria3[3*i+2]);
196     }
197   */
198
199   /*
200     Les champs
201   */
202
203   (void) time(&t1);
204   
205    srand((int) t1); /* use time in seconds to set seed */  
206
207    for(i=0;i<nnoe;i++)
208      {
209        DbleVectNode[mdim*i] =
210          (med_float) (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
211
212        DbleVectNode[mdim*i+1] =
213          (med_float) (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
214
215        /*
216          printf("i %d DbleVectNode %lf %lf\n",i,DbleVectNode[mdim*i],
217          DbleVectNode[mdim*i+1]);
218        */
219      }
220
221    for(i=0;i<ntria3;i++)
222      {
223        DbleVectCell[mdim*i] = (med_float)
224          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
225
226        DbleVectCell[mdim*i+1] = (med_float)
227          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
228
229        /*
230          printf("i %d DbleVectCell %lf %lf\n",i,DbleVectCell[mdim*i],
231          DbleVectCell[mdim*i+1]);
232        */
233      }
234
235   /***************************************************************************/
236
237   sprintf(buff,"%d",nnoe_dir);
238   strcat(MedFile,buff);
239   strcat(MedFile,".med");
240
241   fid = MEDouvrir(MedFile,RDWR);
242
243   if (fid < 0)
244     ret = -1;
245   else
246     ret = 0;
247   printf("%d\n",ret);
248
249   /***************************************************************************/
250   if (ret == 0)
251     ret = MEDmaaCr(fid,maa,mdim);
252   printf("%d\n",ret);
253
254   if (ret == 0)
255     ret = MEDunvCr(fid,maa);
256   printf("%d\n",ret);
257
258   /***************************************************************************/
259
260   if (ret == 0)
261     ret = MEDnoeudsEcr(fid,maa,mdim,coo,MED_FULL_INTERLACE,MED_CART,
262                        nomcoo,unicoo,nomnoe,MED_FAUX,numnoe,MED_VRAI,
263                        nufano,nnoe,WRONLY);
264   printf("%d\n",ret);
265
266   /*
267     ecriture des mailles MED_TRIA3 :
268     - connectivite
269     - noms (optionnel) 
270     - numeros (optionnel)
271     - numeros des familles
272   */
273
274   if (ret == 0) 
275     ret = MEDelementsEcr(fid,maa,mdim,tria3,MED_FULL_INTERLACE,
276                          nomtria3,MED_FAUX,numtria3,MED_VRAI,nufatria3,ntria3,
277                          MED_MAILLE,MED_TRIA3,MED_NOD,WRONLY);
278   printf("%d \n",ret);
279
280   /***************************************************************************/
281   /* ecriture des familles */
282   /* Conventions :
283      - toujours creer une famille de numero 0 ne comportant aucun attribut
284        ni groupe (famille de reference pour les noeuds ou les elements
285        qui ne sont rattaches a aucun groupe ni attribut)
286      - les numeros de familles de noeuds sont > 0
287      - les numeros de familles des elements sont < 0
288      - rien d'imposer sur les noms de familles
289    */ 
290
291   /* la famille 0 */
292   if (ret == 0)
293     {
294       strcpy(nomfam,"FAMILLE_0");
295       numfam = 0;
296       ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,0,
297                      gro,0);
298     }
299   printf("%d \n",ret);
300
301   /***************************************************************************/
302   /*
303     Les Champs
304   */
305
306   if (ret == 0)
307     {
308       ret = MEDchampCr(fid,champDbleVectNode,MED_REEL64,compDbleVectNode,
309                        unitDbleVectNode,mdim);
310
311       printf("MEDchampCr DbleVectNode : %d \n",ret);
312
313       if (ret == 0)
314         {
315           ret = MEDchampEcr(fid, maa, champDbleVectNode,
316                             (unsigned char *)DbleVectNode,
317                             MED_NO_INTERLACE, nnoe,
318                             MED_NOPG, MED_ALL, MED_NOPFL, WRONLY, MED_NOEUD, 
319                             0, MED_NOPDT,"        ", 0., MED_NONOR);
320         
321           printf("MEDchampEcr DbleVectNode : %d \n",ret);
322         }
323     }
324
325
326   if (ret == 0)
327     {
328       ret = MEDchampCr(fid,champDbleVectCell,MED_REEL64,compDbleVectCell,
329                        unitDbleVectCell,mdim);
330
331       printf("MEDchampCr DbleVectCell : %d \n",ret);
332
333       if (ret == 0)
334         {
335           ret = MEDchampEcr(fid, maa, champDbleVectCell,
336                             (unsigned char *)DbleVectCell,
337                             MED_NO_INTERLACE, ntria3,
338                             MED_NOPG, MED_ALL, MED_NOPFL, WRONLY, MED_MAILLE,
339                             MED_TRIA3, MED_NOPDT,"        ", 0., MED_NONOR);
340         
341           printf("MEDchampEcr DbleVectCell : %d \n",ret);
342         }
343     }
344
345   /***************************************************************************/
346
347   ret = MEDfermer(fid);
348   printf("%d\n",ret);
349
350   free(coo);
351   free(numnoe);
352   free(nufano);
353   free(nomnoe);
354   free(tria3);
355   free(numtria3);
356   free(nufatria3);
357   free(nomtria3);
358   free(DbleVectNode);
359   free(DbleVectCell);
360
361   return 0;
362 }