Back to index

salome-med  6.5.0
create_mesh_c2t3.c
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 /*
00024   creation d'une geometrie 2d : un cube [0,1]^2
00025   maillé uniformement en triangles reguliers;
00026   avec n (=argv[1]) noeuds dans chaque direction.
00027   2 champs:
00028   - DbleVectNode champ vectoriel reel sur les noeuds
00029   - DbleVectCell champ vectoriel reel sur les cellules
00030 
00031   En sortie, il y aura production d'un fichier MED
00032   carre_tria3_n.med qui contiendra un seul maillage et 2 champs
00033   avec une seule famille la FAMILLE_0
00034 */
00035 
00036 #include <med.h>
00037 #include <string.h>
00038 #include <stdlib.h>
00039 
00040 #include <time.h>
00041 
00042 int main (int argc, char **argv)
00043 {
00044   med_err ret;
00045   med_idt fid;
00046   char maa[MED_TAILLE_NOM+1] = "carre_tria3";
00047   med_int mdim = 2;
00048   int nnoe_dir;
00049   int nelt_dir;
00050   med_int nnoe;
00051   int i, j, ij;
00052 
00053   med_float * coo;
00054   med_int * numnoe;
00055   med_int * nufano;
00056 
00057   med_float hxsize;
00058   med_float hysize;
00059 
00060   med_float * DbleVectNode;
00061   med_float * DbleVectCell;
00062 
00063   time_t t1;
00064 
00065   /*
00066     Le maillage
00067    */
00068 
00069   char nomcoo[2*MED_TAILLE_PNOM+1] = "x       y       ";
00070   char unicoo[2*MED_TAILLE_PNOM+1] = "cm      cm      ";
00071   /*  char nomnoe[19*MED_TAILLE_PNOM+1] = "nom1    nom2    nom3    nom4";*/
00072   char *nomnoe ;
00073 
00074   med_int ntria3;
00075   med_int * tria3;
00076   med_int * numtria3;
00077   med_int * nufatria3;
00078   char * nomtria3;
00079   int indexN1, indexN2, indexN3, indexN4;
00080 
00081   char nomfam[MED_TAILLE_NOM+1];
00082   med_int numfam;
00083   char attdes[MED_TAILLE_DESC+1];
00084   med_int natt;
00085   med_int attide;
00086   med_int attval;
00087   med_int ngro;
00088   char gro[MED_TAILLE_LNOM+1];
00089   int nfame = 0; 
00090   int nfamn = 0;
00091 
00092   char MedFile[100] = "carre_tria3_";
00093   char buff[100];
00094 
00095   /*
00096     Les champs
00097   */
00098 
00099   char champDbleVectNode[MED_TAILLE_NOM+1] = "DbleVectNode";
00100   char compDbleVectNode[MED_TAILLE_PNOM*2+1] = "comp1   comp2   " ;
00101   char unitDbleVectNode[MED_TAILLE_PNOM*2+1] = "unit1   unit2   " ;
00102 
00103   char champDbleVectCell[MED_TAILLE_NOM+1] = "DbleVectCell";
00104   char compDbleVectCell[MED_TAILLE_PNOM*2+1] = "comp1   comp2   " ;
00105   char unitDbleVectCell[MED_TAILLE_PNOM*2+1] = "unit1   unit2   " ;
00106 
00107   if (argc != 2)
00108     {
00109       printf("Usage: %s <n> \n",argv[0]);
00110       printf("       where\n");
00111       printf("       - n is the number of nodes in each direction.\n");
00112       printf("\n");
00113       printf("This program will produce a MED file carre_tria3_n.med\n");
00114       exit(0);
00115     }
00116 
00117   nnoe_dir = atoi(argv[1]);
00118   nelt_dir = nnoe_dir-1;
00119   nnoe = nnoe_dir*nnoe_dir;
00120 
00121   coo = malloc(mdim*nnoe*sizeof(med_float));
00122   numnoe = malloc(nnoe*sizeof(med_int));
00123   nufano = malloc(nnoe*sizeof(med_int));
00124   nomnoe = malloc((MED_TAILLE_PNOM*nnoe+1)*sizeof(char));
00125 
00126   hxsize = 1./((med_float) (nnoe_dir - 1));
00127   hysize = hxsize;
00128 
00129   ntria3 = 2*nelt_dir*nelt_dir;
00130   tria3 = malloc(3*ntria3*sizeof(med_int));
00131   numtria3 = malloc(ntria3*sizeof(med_int));
00132   nufatria3 = malloc(ntria3*sizeof(med_int));
00133   nomtria3 = malloc((MED_TAILLE_PNOM*ntria3+1)*sizeof(char));
00134 
00135   DbleVectNode = malloc(mdim*nnoe*sizeof(med_float));
00136   DbleVectCell = malloc(mdim*ntria3*sizeof(med_float));
00137 
00138   /*
00139     les noeuds:
00140   */
00141 
00142   for(j=0;j<nnoe_dir;j++)
00143     {
00144       for (i=0;i<nnoe_dir;i++)
00145         {
00146           int ij = j*nnoe_dir+i;
00147 
00148           numnoe[ij] = ij+1;
00149           nufano[ij] = 0;
00150 
00151           coo[mdim*ij] = ((med_float) i)*hxsize;
00152           coo[mdim*ij+1] = ((med_float) j)*hysize;
00153 
00154           /*
00155           printf("Coordonnées %d   X = %lf  Y = %lf\n",(ij+1),coo[mdim*ij],coo[mdim*ij+1]);
00156           */
00157         }
00158     }
00159 
00160   /*
00161     les elements:
00162   */
00163 
00164   for(j=0;j<nelt_dir;j++)
00165     {
00166       for (i=0;i<nelt_dir;i++)
00167         {
00168           int ij = j*nelt_dir+i;
00169           int ij1 = 2*ij;
00170           int ij2 = ij1+1;
00171 
00172           numtria3[ij1] = ij1+1;
00173           numtria3[ij2] = ij2+1;
00174 
00175           nufatria3[ij1] = 0;
00176           nufatria3[ij2] = 0;
00177 
00178           indexN4 = j*nnoe_dir+i+1;
00179           indexN3 = indexN4+1;
00180           indexN1 = indexN4+nnoe_dir;
00181           indexN2 = indexN3+nnoe_dir;
00182 
00183           tria3[3*ij1] = indexN1;
00184           tria3[3*ij1+1] = indexN2;
00185           tria3[3*ij1+2] = indexN3;
00186 
00187           tria3[3*ij2] = indexN1;
00188           tria3[3*ij2+1] = indexN3;
00189           tria3[3*ij2+2] = indexN4;
00190         }
00191     }
00192 
00193   /*
00194   for (i=0;i<ntria3;i++)
00195     {
00196       printf("Connectivitée %d  i1 = %d  i2 = %d  i3 = %d\n",(i+1),tria3[3*i],tria3[3*i+1],tria3[3*i+2]);
00197     }
00198   */
00199 
00200   /*
00201     Les champs
00202   */
00203 
00204   (void) time(&t1);
00205   
00206    srand((int) t1); /* use time in seconds to set seed */  
00207 
00208    for(i=0;i<nnoe;i++)
00209      {
00210        DbleVectNode[mdim*i] =
00211          (med_float) (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00212 
00213        DbleVectNode[mdim*i+1] =
00214          (med_float) (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00215 
00216        /*
00217          printf("i %d DbleVectNode %lf %lf\n",i,DbleVectNode[mdim*i],
00218          DbleVectNode[mdim*i+1]);
00219        */
00220      }
00221 
00222    for(i=0;i<ntria3;i++)
00223      {
00224        DbleVectCell[mdim*i] = (med_float)
00225          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00226 
00227        DbleVectCell[mdim*i+1] = (med_float)
00228          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00229 
00230        /*
00231          printf("i %d DbleVectCell %lf %lf\n",i,DbleVectCell[mdim*i],
00232          DbleVectCell[mdim*i+1]);
00233        */
00234      }
00235 
00236   /***************************************************************************/
00237 
00238   sprintf(buff,"%d",nnoe_dir);
00239   strcat(MedFile,buff);
00240   strcat(MedFile,".med");
00241 
00242   fid = MEDouvrir(MedFile,RDWR);
00243 
00244   if (fid < 0)
00245     ret = -1;
00246   else
00247     ret = 0;
00248   printf("%d\n",ret);
00249 
00250   /***************************************************************************/
00251   if (ret == 0)
00252     ret = MEDmaaCr(fid,maa,mdim);
00253   printf("%d\n",ret);
00254 
00255   if (ret == 0)
00256     ret = MEDunvCr(fid,maa);
00257   printf("%d\n",ret);
00258 
00259   /***************************************************************************/
00260 
00261   if (ret == 0)
00262     ret = MEDnoeudsEcr(fid,maa,mdim,coo,MED_FULL_INTERLACE,MED_CART,
00263                        nomcoo,unicoo,nomnoe,MED_FAUX,numnoe,MED_VRAI,
00264                        nufano,nnoe,WRONLY);
00265   printf("%d\n",ret);
00266 
00267   /*
00268     ecriture des mailles MED_TRIA3 :
00269     - connectivite
00270     - noms (optionnel) 
00271     - numeros (optionnel)
00272     - numeros des familles
00273   */
00274 
00275   if (ret == 0) 
00276     ret = MEDelementsEcr(fid,maa,mdim,tria3,MED_FULL_INTERLACE,
00277                          nomtria3,MED_FAUX,numtria3,MED_VRAI,nufatria3,ntria3,
00278                          MED_MAILLE,MED_TRIA3,MED_NOD,WRONLY);
00279   printf("%d \n",ret);
00280 
00281   /***************************************************************************/
00282   /* ecriture des familles */
00283   /* Conventions :
00284      - toujours creer une famille de numero 0 ne comportant aucun attribut
00285        ni groupe (famille de reference pour les noeuds ou les elements
00286        qui ne sont rattaches a aucun groupe ni attribut)
00287      - les numeros de familles de noeuds sont > 0
00288      - les numeros de familles des elements sont < 0
00289      - rien d'imposer sur les noms de familles
00290    */ 
00291 
00292   /* la famille 0 */
00293   if (ret == 0)
00294     {
00295       strcpy(nomfam,"FAMILLE_0");
00296       numfam = 0;
00297       ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,0,
00298                      gro,0);
00299     }
00300   printf("%d \n",ret);
00301 
00302   /***************************************************************************/
00303   /*
00304     Les Champs
00305   */
00306 
00307   if (ret == 0)
00308     {
00309       ret = MEDchampCr(fid,champDbleVectNode,MED_REEL64,compDbleVectNode,
00310                        unitDbleVectNode,mdim);
00311 
00312       printf("MEDchampCr DbleVectNode : %d \n",ret);
00313 
00314       if (ret == 0)
00315         {
00316           ret = MEDchampEcr(fid, maa, champDbleVectNode,
00317                             (unsigned char *)DbleVectNode,
00318                             MED_NO_INTERLACE, nnoe,
00319                             MED_NOPG, MED_ALL, MED_NOPFL, WRONLY, MED_NOEUD, 
00320                             0, MED_NOPDT,"        ", 0., MED_NONOR);
00321         
00322           printf("MEDchampEcr DbleVectNode : %d \n",ret);
00323         }
00324     }
00325 
00326 
00327   if (ret == 0)
00328     {
00329       ret = MEDchampCr(fid,champDbleVectCell,MED_REEL64,compDbleVectCell,
00330                        unitDbleVectCell,mdim);
00331 
00332       printf("MEDchampCr DbleVectCell : %d \n",ret);
00333 
00334       if (ret == 0)
00335         {
00336           ret = MEDchampEcr(fid, maa, champDbleVectCell,
00337                             (unsigned char *)DbleVectCell,
00338                             MED_NO_INTERLACE, ntria3,
00339                             MED_NOPG, MED_ALL, MED_NOPFL, WRONLY, MED_MAILLE,
00340                             MED_TRIA3, MED_NOPDT,"        ", 0., MED_NONOR);
00341         
00342           printf("MEDchampEcr DbleVectCell : %d \n",ret);
00343         }
00344     }
00345 
00346   /***************************************************************************/
00347 
00348   ret = MEDfermer(fid);
00349   printf("%d\n",ret);
00350 
00351   free(coo);
00352   free(numnoe);
00353   free(nufano);
00354   free(nomnoe);
00355   free(tria3);
00356   free(numtria3);
00357   free(nufatria3);
00358   free(nomtria3);
00359   free(DbleVectNode);
00360   free(DbleVectCell);
00361 
00362   return 0;
00363 }