Back to index

salome-med  6.5.0
create_mesh_c3t4.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 3d : un cube [0,1]^3
00025   maillé uniformement en tetrahedres 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   cube_tetra_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] = "cube_tetra4";
00047   med_int mdim = 3;
00048   int dimTot;
00049 
00050   int nnoe_dir;
00051   int nelt_dir;
00052   med_int nnoe;
00053   int i, j, k, ijk;
00054 
00055   med_float * coo;
00056   med_int * numnoe;
00057   med_int * nufano;
00058 
00059   med_float hxsize;
00060   med_float hysize;
00061   med_float hzsize;
00062 
00063   med_float * DbleVectNode;
00064   med_float * DbleVectCell;
00065 
00066   time_t t1;
00067 
00068   /*
00069     Le maillage
00070    */
00071 
00072   char nomcoo[3*MED_TAILLE_PNOM+1] = "x       y       z       ";
00073   char unicoo[3*MED_TAILLE_PNOM+1] = "cm      cm      cm      ";
00074   /*  char nomnoe[19*MED_TAILLE_PNOM+1] = "nom1    nom2    nom3    nom4";*/
00075   char *nomnoe ;
00076 
00077   med_int ntetra4;
00078   med_int * tetra4;
00079   med_int * numtetra4;
00080   med_int * nufatetra4;
00081 
00082   char * nomtetra4;
00083   int indexN1, indexN2, indexN3, indexN4, indexN5, indexN6, indexN7, indexN8;
00084 
00085   char nomfam[MED_TAILLE_NOM+1];
00086   med_int numfam;
00087   char attdes[MED_TAILLE_DESC+1];
00088   med_int natt;
00089   med_int attide;
00090   med_int attval;
00091   med_int ngro;
00092   char gro[MED_TAILLE_LNOM+1];
00093   int nfame = 0; 
00094   int nfamn = 0;
00095 
00096   char MedFile[100] = "cube_tetra4_";
00097   char buff[100];
00098 
00099   /*
00100     Les champs
00101   */
00102 
00103   char champDbleVectNode[MED_TAILLE_NOM+1] = "DbleVectNode";
00104   char compDbleVectNode[MED_TAILLE_PNOM*3+1] = "comp1   comp2   comp3   " ;
00105   char unitDbleVectNode[MED_TAILLE_PNOM*3+1] = "unit1   unit2   unit3   " ;
00106 
00107   char champDbleVectCell[MED_TAILLE_NOM+1] = "DbleVectCell";
00108   char compDbleVectCell[MED_TAILLE_PNOM*3+1] = "comp1   comp2   comp3   " ;
00109   char unitDbleVectCell[MED_TAILLE_PNOM*3+1] = "unit1   unit2   unit3   " ;
00110 
00111   if (argc != 2)
00112     {
00113       printf("Usage: %s <n> \n",argv[0]);
00114       printf("       where\n");
00115       printf("       - n is the number of nodes in each direction.\n");
00116       printf("\n");
00117       printf("This program will produce a MED file cube_tetra4_n.med\n");
00118       exit(0);
00119     }
00120 
00121   nnoe_dir = atoi(argv[1]);
00122   nelt_dir = nnoe_dir-1;
00123   nnoe = (med_int) nnoe_dir*nnoe_dir*nnoe_dir;
00124 
00125   dimTot = (int) mdim*nnoe*sizeof(med_float);
00126   coo = malloc(dimTot);
00127 
00128   dimTot = (int) (MED_TAILLE_PNOM*nnoe+1)*sizeof(char);
00129   nomnoe = malloc(dimTot);
00130 
00131   dimTot = (int) nnoe*sizeof(med_int);
00132   numnoe = malloc(dimTot);
00133   nufano = malloc(dimTot);
00134 
00135   hxsize = 1./((med_float) (nnoe_dir - 1));
00136   hysize = hxsize;
00137   hzsize = hxsize;
00138 
00139   ntetra4 = (med_int) 6*nelt_dir*nelt_dir*nelt_dir;
00140 
00141   dimTot = (int) 4*ntetra4*sizeof(med_int);
00142   tetra4 = malloc(dimTot);
00143 
00144   dimTot = (int) (MED_TAILLE_PNOM*ntetra4+1)*sizeof(char);
00145   nomtetra4 = malloc(dimTot);
00146 
00147   dimTot = (int) ntetra4*sizeof(med_int);
00148   numtetra4 = malloc(dimTot);
00149   nufatetra4 = malloc(dimTot);
00150 
00151   dimTot = (int) mdim*nnoe*sizeof(med_float);
00152   DbleVectNode = malloc(dimTot);
00153 
00154   dimTot = (int) mdim*ntetra4*sizeof(med_float);
00155   DbleVectCell = malloc(dimTot);
00156 
00157   /*
00158     les noeuds:
00159   */
00160 
00161   for(k=0;k<nnoe_dir;k++)
00162     {
00163       for(j=0;j<nnoe_dir;j++)
00164         {
00165           for (i=0;i<nnoe_dir;i++)
00166             {
00167               int ijk = k*nnoe_dir*nnoe_dir+j*nnoe_dir+i;
00168 
00169               numnoe[ijk] = ijk+1;
00170               nufano[ijk] = 0;
00171 
00172               coo[mdim*ijk] = ((med_float) i)*hxsize;
00173               coo[mdim*ijk+1] = ((med_float) j)*hysize;
00174               coo[mdim*ijk+2] = ((med_float) k)*hzsize;
00175 
00176               /*
00177               printf("Coordonnées %d   X = %lf  Y = %lf  Z = %lf\n",(ijk+1),coo[mdim*ijk],coo[mdim*ijk+1],coo[mdim*ijk+2]);
00178               */
00179             }
00180         }
00181     }
00182 
00183   /*
00184     les elements:
00185   */
00186 
00187   for(k=0;k<nelt_dir;k++)
00188     {
00189       for(j=0;j<nelt_dir;j++)
00190         {
00191           for (i=0;i<nelt_dir;i++)
00192             {
00193               int ijk = k*nelt_dir*nelt_dir+j*nelt_dir+i;
00194               int ijk1 = 6*ijk;
00195               int ijk2 = ijk1+1;
00196               int ijk3 = ijk2+1;
00197               int ijk4 = ijk3+1;
00198               int ijk5 = ijk4+1;
00199               int ijk6 = ijk5+1;
00200 
00201               numtetra4[ijk1] = ijk1+1;
00202               numtetra4[ijk2] = ijk2+1;
00203               numtetra4[ijk3] = ijk3+1;
00204               numtetra4[ijk4] = ijk4+1;
00205               numtetra4[ijk5] = ijk5+1;
00206               numtetra4[ijk6] = ijk6+1;
00207 
00208               nufatetra4[ijk1] = 0;
00209               nufatetra4[ijk2] = 0;
00210               nufatetra4[ijk3] = 0;
00211               nufatetra4[ijk4] = 0;
00212               nufatetra4[ijk5] = 0;
00213               nufatetra4[ijk6] = 0;
00214 
00215               indexN5 = k*nnoe_dir*nnoe_dir+j*nnoe_dir+i+1;
00216               indexN8 = indexN5+1;
00217               indexN1 = indexN5+nnoe_dir;
00218               indexN4 = indexN8+nnoe_dir;
00219 
00220               indexN6 = indexN5+nnoe_dir*nnoe_dir;
00221               indexN7 = indexN8+nnoe_dir*nnoe_dir;
00222               indexN2 = indexN1+nnoe_dir*nnoe_dir;
00223               indexN3 = indexN4+nnoe_dir*nnoe_dir;
00224 
00225               tetra4[4*ijk1] = indexN1;
00226               tetra4[4*ijk1+1] = indexN3;
00227               tetra4[4*ijk1+2] = indexN4;
00228               tetra4[4*ijk1+3] = indexN5;
00229 
00230               tetra4[4*ijk2] = indexN3;
00231               tetra4[4*ijk2+1] = indexN7;
00232               tetra4[4*ijk2+2] = indexN4;
00233               tetra4[4*ijk2+3] = indexN5;
00234 
00235               tetra4[4*ijk3] = indexN4;
00236               tetra4[4*ijk3+1] = indexN7;
00237               tetra4[4*ijk3+2] = indexN8;
00238               tetra4[4*ijk3+3] = indexN5;
00239 
00240               tetra4[4*ijk4] = indexN1;
00241               tetra4[4*ijk4+1] = indexN2;
00242               tetra4[4*ijk4+2] = indexN3;
00243               tetra4[4*ijk4+3] = indexN5;
00244 
00245               tetra4[4*ijk5] = indexN2;
00246               tetra4[4*ijk5+1] = indexN6;
00247               tetra4[4*ijk5+2] = indexN3;
00248               tetra4[4*ijk5+3] = indexN5;
00249 
00250               tetra4[4*ijk6] = indexN3;
00251               tetra4[4*ijk6+1] = indexN6;
00252               tetra4[4*ijk6+2] = indexN7;
00253               tetra4[4*ijk6+3] = indexN5;
00254             }
00255         }
00256     }
00257 
00258   /*
00259     Les champs
00260   */
00261 
00262   (void) time(&t1);
00263   
00264    srand((int) t1); /* use time in seconds to set seed */  
00265 
00266    for(i=0;i<nnoe;i++)
00267      {
00268        DbleVectNode[mdim*i] = (med_float)
00269          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00270 
00271        DbleVectNode[mdim*i+1] = (med_float)
00272          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00273 
00274        DbleVectNode[mdim*i+2] = (med_float)
00275          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00276 
00277        /*
00278          printf("i %d DbleVectNode %lf %lf %lf\n",i,DbleVectNode[mdim*i],
00279          DbleVectNode[mdim*i+1],DbleVectNode[mdim*i+2]);
00280        */
00281      }
00282 
00283    for(i=0;i<ntetra4;i++)
00284      {
00285        DbleVectCell[mdim*i] = (med_float)
00286          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00287 
00288        DbleVectCell[mdim*i+1] = (med_float)
00289          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00290 
00291        DbleVectCell[mdim*i+2] = (med_float)
00292          (1+(int) (100.0*rand()/(RAND_MAX+1.0)));
00293 
00294        /*
00295          printf("i %d DbleVectCell %lf %lf %lf\n",i,DbleVectCell[mdim*i],
00296          DbleVectCell[mdim*i+1],DbleVectCell[mdim*i+2]);
00297        */
00298      }
00299 
00300   /***************************************************************************/
00301 
00302   sprintf(buff,"%d",nnoe_dir);
00303   strcat(MedFile,buff);
00304   strcat(MedFile,".med");
00305 
00306   fid = MEDouvrir(MedFile,RDWR);
00307 
00308   if (fid < 0)
00309     ret = -1;
00310   else
00311     ret = 0;
00312   printf("%d\n",ret);
00313 
00314   /***************************************************************************/
00315 
00316   if (ret == 0)
00317     ret = MEDmaaCr(fid,maa,mdim);
00318   printf("%d\n",ret);
00319 
00320   if (ret == 0)
00321     ret = MEDunvCr(fid,maa);
00322   printf("%d\n",ret);
00323 
00324   /***************************************************************************/
00325 
00326   if (ret == 0)
00327     ret = MEDnoeudsEcr(fid,maa,mdim,coo,MED_FULL_INTERLACE,MED_CART,
00328                        nomcoo,unicoo,nomnoe,MED_FAUX,numnoe,MED_VRAI,
00329                        nufano,nnoe,WRONLY);
00330   printf("%d\n",ret);
00331 
00332   /*
00333     ecriture des mailles MED_TETRA4 :
00334     - connectivite
00335     - noms (optionnel) 
00336     - numeros (optionnel)
00337     - numeros des familles
00338   */
00339 
00340   if (ret == 0) 
00341     ret = MEDelementsEcr(fid,maa,mdim,tetra4,MED_FULL_INTERLACE,
00342                          nomtetra4,MED_FAUX,numtetra4,MED_VRAI,nufatetra4,
00343                          ntetra4,MED_MAILLE,MED_TETRA4,MED_NOD,WRONLY);
00344   printf("%d \n",ret);
00345 
00346   /***************************************************************************/
00347   /* ecriture des familles */
00348   /* Conventions :
00349      - toujours creer une famille de numero 0 ne comportant aucun attribut
00350        ni groupe (famille de reference pour les noeuds ou les elements
00351        qui ne sont rattaches a aucun groupe ni attribut)
00352      - les numeros de familles de noeuds sont > 0
00353      - les numeros de familles des elements sont < 0
00354      - rien d'imposer sur les noms de familles
00355    */ 
00356 
00357   /* la famille 0 */
00358   if (ret == 0)
00359     {
00360       strcpy(nomfam,"FAMILLE_0");
00361       numfam = 0;
00362       ret = MEDfamCr(fid,maa,nomfam,numfam,&attide,&attval,attdes,0,
00363                      gro,0);
00364     }
00365   printf("%d \n",ret);
00366 
00367   /***************************************************************************/
00368   /*
00369     Les Champs
00370   */
00371 
00372   if (ret == 0)
00373     {
00374       ret = MEDchampCr(fid,champDbleVectNode,MED_REEL64,compDbleVectNode,
00375                        unitDbleVectNode,mdim);
00376 
00377       printf("MEDchampCr DbleVectNode : %d \n",ret);
00378 
00379       if (ret == 0)
00380         {
00381           dimTot = (int) nnoe;
00382 
00383           ret = MEDchampEcr(fid, maa, champDbleVectNode,
00384                             (unsigned char *) DbleVectNode,
00385                             MED_NO_INTERLACE, dimTot,
00386                             MED_NOPG, MED_ALL, MED_NOPFL, WRONLY, MED_NOEUD, 
00387                             0, MED_NOPDT,"        ", 0., MED_NONOR);
00388         
00389           printf("MEDchampEcr DbleVectNode : %d \n",ret);
00390         }
00391     }
00392 
00393   if (ret == 0)
00394     {
00395       ret = MEDchampCr(fid,champDbleVectCell,MED_REEL64,compDbleVectCell,
00396                        unitDbleVectCell,mdim);
00397 
00398       printf("MEDchampCr DbleVectCell : %d \n",ret);
00399 
00400       if (ret == 0)
00401         {
00402           dimTot = (int) ntetra4;
00403 
00404           ret = MEDchampEcr(fid, maa, champDbleVectCell,
00405                             (unsigned char *) DbleVectCell,
00406                             MED_NO_INTERLACE, dimTot,
00407                             MED_NOPG, MED_ALL, MED_NOPFL, WRONLY, MED_MAILLE,
00408                             MED_TETRA4, MED_NOPDT,"        ", 0., MED_NONOR);
00409         
00410           printf("MEDchampEcr DbleVectCell : %d \n",ret);
00411         }
00412     }
00413 
00414   /***************************************************************************/
00415 
00416   ret = MEDfermer(fid);
00417   printf("%d\n",ret);
00418 
00419   free(coo);
00420   free(numnoe);
00421   free(nufano);
00422   free(nomnoe);
00423   free(tetra4);
00424   free(numtetra4);
00425   free(nufatetra4);
00426   free(nomtetra4);
00427   free(DbleVectNode);
00428   free(DbleVectCell);
00429 
00430   return 0;
00431 }