Back to index

salome-med  6.5.0
create_mesh_c3h8q4.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 hexahedres reguliers;
00026   avec en plus une partie des faces (une partie
00027   des faces de la frontiere) du maillage.
00028   ATTENTION : 3 noeuds dans chaque direction
00029 */
00030 
00031 #include <med.h>
00032 #include <string.h>
00033 #include <stdlib.h>
00034 
00035 int main (int argc, char **argv)
00036 {
00037   med_err ret;
00038   med_idt fid;
00039   char maa[MED_NAME_SIZE+1] = "CUBE_EN_HEXA8_QUAD4";
00040   char maadesc[MED_COMMENT_SIZE+1] = "Example de maillage non structure 3D";
00041   med_int mdim = 3;
00042   med_int nnoe = 27;
00043   /*
00044     les noeuds:
00045   */
00046   med_float coo[81] = {
00047     0.0, 0.0, 0.0, 
00048     0.5, 0.0, 0.0, 
00049     1.0, 0.0, 0.0, 
00050     0.0, 0.5, 0.0, 
00051     0.5, 0.5, 0.0, 
00052     1.0, 0.5, 0.0, 
00053     0.0, 1.0, 0.0, 
00054     0.5, 1.0, 0.0, 
00055     1.0, 1.0, 0.0, 
00056     0.0, 0.0, 0.5, 
00057     0.5, 0.0, 0.5, 
00058     1.0, 0.0, 0.5, 
00059     0.0, 0.5, 0.5, 
00060     0.5, 0.5, 0.5, 
00061     1.0, 0.5, 0.5, 
00062     0.0, 1.0, 0.5, 
00063     0.5, 1.0, 0.5, 
00064     1.0, 1.0, 0.5, 
00065     0.0, 0.0, 1.0, 
00066     0.5, 0.0, 1.0, 
00067     1.0, 0.0, 1.0, 
00068     0.0, 0.5, 1.0, 
00069     0.5, 0.5, 1.0, 
00070     1.0, 0.5, 1.0, 
00071     0.0, 1.0, 1.0, 
00072     0.5, 1.0, 1.0, 
00073     1.0, 1.0, 1.0
00074   };
00075   char nomcoo[3*MED_SNAME_SIZE+1] = "x               y               z               ";
00076   char unicoo[3*MED_SNAME_SIZE+1] = "cm              cm              cm              ";
00077   /*  char nomnoe[19*MED_TAILLE_PNOM+1] = "nom1    nom2    nom3    nom4";*/
00078   //char *nomnoe ;
00079 //  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};
00080   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};
00081   /*
00082     les elements:
00083   */
00084   med_int nhexa8 = 8;
00085   med_int hexa8[64] = {
00086     4, 13, 14, 5, 1, 10, 11, 2,
00087     5, 14, 15, 6, 2, 11, 12, 3,
00088     7, 16, 17, 8, 4, 13, 14, 5,
00089     8, 17, 18, 9, 5, 14, 15, 6,
00090     13, 22, 23, 14, 10, 19, 20, 11,
00091     14, 23, 24, 15, 11, 20, 21, 12,
00092     16, 25, 26, 17, 13, 22, 23, 14,
00093     17, 26, 27, 18, 14, 23, 24, 15
00094   };
00095  // char nomhexa8[MED_TAILLE_PNOM*8+1] = "hexa1           hexa2           hexa3           hexa4           hexa5           hexa6           hexa7           hexa8           ";
00096  // med_int numhexa8[8] = {1,2,3,4,5,6,7,8};
00097   med_int nufahexa8[8] = {-1,-1,-1,-1,-2,-2,-2,-2};
00098 
00099   /*
00100     Les Faces qui dans ce cas (2D) sont des arretes
00101   */
00102 
00103   med_int nquad4 = 8;
00104   med_int quad4[40] = {
00105     20, 21, 24, 23, 0,
00106     22, 23, 26, 25, 0,
00107     19, 20, 23, 22, 0,
00108     23, 24, 27, 26, 0,
00109     1 , 4, 5, 2, 0,
00110     5, 8, 9, 6, 0,
00111     2, 5, 6, 3, 0,
00112     7, 8, 5, 4, 0
00113   };
00114 
00115  // char nomquad4[MED_TAILLE_PNOM*8+1] = "quad1           quad2           quad3           quad4           quad5           quad6           quad7           quad8          ";
00116  // med_int numquad4[8] = {1,2,3,4,5,6,7,8};
00117   med_int nufaquad4[8] = {-3,-3,-3,-3,-4, -4, -4 , -4};
00118 
00119   char nomfam[MED_NAME_SIZE+1];
00120   med_int numfam;
00121   //char attdesMED_TAILLE_DESC+1];
00122   med_int natt;
00123   med_int attide;
00124   med_int attval;
00125   med_int ngro;
00126   char gro[MED_LNAME_SIZE+1];
00127   char dtunitp3[MED_LNAME_SIZE+1]="";
00128   int i;
00129   int nfame = 1; 
00130   int nfamn = 1;
00131   int nfamf = 1;
00132 
00133   /*
00134     Some fields : 2 on nodes : one int and one double , one on cells : double
00135   */
00136 
00137   char champ1[MED_NAME_SIZE+1]="fieldnodeint" ;
00138   char champ1_comp[MED_SNAME_SIZE+1]="comp1           " ;
00139   char champ1_unit[MED_SNAME_SIZE+1]="M               " ;
00140   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};
00141 
00142   char champ2[MED_NAME_SIZE+1]="fieldnodedouble" ;
00143   char champ2_comp[MED_SNAME_SIZE+1]="comp1           " ;
00144   char champ2_unit[MED_SNAME_SIZE+1]="J               " ;
00145   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.};
00146   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.};
00147 
00148   char champ3[MED_NAME_SIZE+1]="fieldcelldouble" ;
00149   char champ3_comp[MED_SNAME_SIZE*3+1]="comp1           comp2           comp3           " ;
00150   char champ3_unit[MED_SNAME_SIZE*3+1]="M/S             m/s             m/s             " ;
00151   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.};
00152 
00153   /***************************************************************************/
00154   fid = MEDfileOpen("cube_hexa8_quad4.med",MED_ACC_RDWR);
00155   if (fid < 0)
00156     ret = -1;
00157   else
00158     ret = 0;
00159   printf("MEDouvrir : %d\n",ret);
00160 
00161   /***************************************************************************/
00162   if (ret == 0)
00163     ret = MEDmeshCr(fid,maa,mdim,mdim,MED_UNSTRUCTURED_MESH,maadesc,dtunitp3,
00164                     MED_SORT_DTIT,MED_CARTESIAN,nomcoo,unicoo);
00165   printf("MEDmaaCr : %d\n",ret);
00166   if (ret == 0)
00167     ret = MEDunvCr(fid,maa);
00168   printf("MEDunvCr : %d\n",ret);
00169 
00170   /***************************************************************************/
00171   if (ret == 0)
00172     ret = MEDmeshNodeCoordinateWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,MED_FULL_INTERLACE,nnoe,coo);
00173   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NODE,0,nnoe,nufano);
00174   printf("MEDnoeudsEcr : %d\n",ret);
00175 
00176   /* ecriture des mailles MED_HEXA8 :
00177      - connectivite
00178      - noms (optionnel) 
00179      - numeros (optionnel)
00180      - numeros des familles */
00181   if (ret == 0) 
00182     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
00183                                        MED_CELL,MED_HEXA8,MED_NODAL,MED_FULL_INTERLACE,
00184                                        nhexa8,hexa8);
00185   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_HEXA8,nhexa8,nufahexa8);
00186   printf("MEDelementsEcr : %d \n",ret);
00187 
00188   /* ecriture des mailles MED_QUAD4 :
00189      - connectivite
00190      - noms (optionnel) 
00191      - numeros (optionnel)
00192      - numeros des familles */
00193   if (ret == 0) 
00194     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
00195                                        MED_CELL,MED_QUAD4,MED_NODAL,MED_FULL_INTERLACE,
00196                                        nquad4,quad4);
00197   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_QUAD4,nquad4,nufaquad4);
00198   printf("%d \n",ret);
00199 
00200   /***************************************************************************/
00201   /* ecriture des familles */
00202   /* Conventions :
00203      - toujours creer une famille de numero 0 ne comportant aucun attribut
00204        ni groupe (famille de reference pour les noeuds ou les elements
00205        qui ne sont rattaches a aucun groupe ni attribut)
00206      - les numeros de familles de noeuds sont > 0
00207      - les numeros de familles des elements sont < 0
00208      - rien d'imposer sur les noms de familles
00209    */ 
00210 
00211   /* la famille 0 */
00212   if (ret == 0)
00213     {
00214       strcpy(nomfam,"FAMILLE_0");
00215       numfam = 0;
00216       ret = MEDfamilyCr(fid,maa,nomfam,numfam,0,gro);
00217     }
00218   printf("%d \n",ret);
00219 
00220   /* on cree :
00221       - 2 familles d'elements (-1,-2) et 
00222       - 3 familles de noeuds (1,2,3) 
00223       - 1 famille(s) d'elements de dimension (d-1)
00224         en fait de face (-3)
00225   */
00226 
00227   if (ret == 0)
00228     {
00229       nfame = 2;
00230       for (i=0;i<nfame;i++)
00231         {
00232           if (ret == 0)
00233             {
00234               strcpy(nomfam,"FAMILLE_ELEMENT_");
00235               numfam = -(i+1);
00236               sprintf(nomfam,"%s%d",nomfam,-numfam);
00237               attide = 1;
00238               attval = numfam*100;
00239               natt = 1;
00240               //strcpy(attdes,"description attribut");
00241               strcpy(gro,"groupe1");
00242               ngro = 1;
00243 
00244               /*printf("nomfam : %s - numfam : %d - attide : %d - attval : %d - ngro : %d \n",nomfam,numfam,attide,attval,ngro);*/
00245 
00246               ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
00247               printf("MEDfamCr : %d\n",ret);
00248             }
00249         }
00250     }
00251   
00252   if (ret == 0)
00253     {
00254       nfamn = 3;
00255       for (i=0;i<nfamn;i++)
00256         {
00257           if(ret == 0)
00258             {
00259               strcpy(nomfam,"FAMILLE_NOEUD_");
00260               numfam = i+1;
00261               sprintf(nomfam,"%s%d",nomfam,numfam);
00262               attide = 1;
00263               attval = numfam*100;
00264               natt = 1;
00265               //strcpy(attdes,"description attribut");
00266               strcpy(gro,"groupe2");
00267               ngro = 1;
00268               ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
00269               printf("MEDfamCr : %d\n",ret);
00270             }
00271         }
00272     }
00273 
00274   if (ret == 0)
00275     {
00276       nfamf = 2;
00277       for (i=0;i<nfamf;i++)
00278         {
00279           if (ret == 0)
00280             {
00281               strcpy(nomfam,"FAMILLE_FACE_");
00282               numfam = -(i+3);
00283               sprintf(nomfam,"%s%d",nomfam,-numfam);
00284               attide = 1;
00285               attval = numfam*100;
00286               natt = 1;
00287               //strcpy(attdes,"description attribut");
00288               strcpy(gro,"groupe3");
00289               ngro = 1;
00290               /*printf("nomfam : %s - numfam : %d - attide : %d - attval : %d - ngro : %d \n",nomfam,numfam,attide,attval,ngro);*/
00291               ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
00292               printf("MEDfamCr : %d\n",ret);
00293             }
00294         }
00295     }
00296 
00297   /***************************************************************************/
00298   /*
00299     Les champs
00300   */
00301   if (ret == 0)
00302     {
00303       ret = MEDfieldCr(fid,champ1,MED_INT32,1,champ1_comp,champ1_unit,dtunitp3,maa);
00304       printf("MEDchampCr : %d \n",ret);
00305       if (ret == 0) {
00306         ret = MEDfieldValueWr(fid, champ1,MED_NO_DT,MED_NO_IT,0.,MED_NODE,0,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodeint);
00307         
00308         printf("MEDchampEcr : %d \n",ret);
00309       }
00310     }
00311   
00312   if (ret == 0)
00313     {
00314       ret = MEDfieldCr(fid,champ2,MED_FLOAT64,1,champ2_comp,champ2_unit,dtunitp3,maa);
00315       printf("MEDchampCr : %d \n",ret);
00316       if (ret == 0) {
00317         ret = MEDfieldValueWr(fid, champ2,1,MED_NO_IT,1.1 ,MED_NODE,0,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
00318         printf("MEDchampEcr1 : %d \n",ret);
00319         ret = MEDfieldValueWr(fid, champ2,2,MED_NO_IT,1.2 ,MED_NODE,0,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble2);
00320         printf("MEDchampEcr2 : %d \n",ret);
00321       }
00322     }
00323   
00324   /* on met champ2 sans pas de temps pour pouvoir le lire aussi par defaut !*/
00325   if (ret == 0)
00326     {
00327       ret = MEDfieldValueWr(fid, champ2,MED_NO_DT,MED_NO_IT,0. ,MED_NODE,0,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
00328       printf("MEDchampEcr : %d \n",ret); 
00329     }
00330 
00331   if (ret == 0)
00332     {
00333       ret = MEDfieldCr(fid,champ3,MED_FLOAT64,3,champ3_comp,champ3_unit,dtunitp3,maa);
00334       printf("MEDchampCr : %d \n",ret);
00335       if (ret == 0) {
00336         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);
00337         printf("MEDchampEcr : %d \n",ret);
00338       }
00339     }
00340 
00341   /***************************************************************************/
00342 
00343   ret = MEDfermer(fid);
00344   printf("%d\n",ret);
00345   
00346   if ( getenv("srcdir") ) 
00347     /* we are in 'make check' */
00348     remove( "cube_hexa8_quad4.med" );
00349 
00350   return 0;
00351 }