Back to index

salome-med  6.5.0
create_mesh_c3h8.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";
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   char nomfam[MED_NAME_SIZE+1];
00100   med_int numfam;
00101   //char attdesMED_TAILLE_DESC+1];
00102   med_int natt;
00103   med_int attide;
00104   med_int attval;
00105   med_int ngro;
00106   char gro[MED_LNAME_SIZE+1];
00107   char dtunitp3[MED_LNAME_SIZE+1]="";
00108   int i;
00109   int nfame = 1; 
00110   int nfamn = 1;
00111 
00112   /*
00113     Some fields : 2 on nodes : one int and one double , one on cells : double
00114   */
00115 
00116   char champ1[MED_NAME_SIZE+1]="fieldnodeint" ;
00117   char champ1_comp[MED_SNAME_SIZE+1]="comp1           " ;
00118   char champ1_unit[MED_SNAME_SIZE+1]="M               " ;
00119   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};
00120 
00121   char champ2[MED_NAME_SIZE+1]="fieldnodedouble" ;
00122   char champ2_comp[MED_SNAME_SIZE+1]="comp1           " ;
00123   char champ2_unit[MED_SNAME_SIZE+1]="J               " ;
00124   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.};
00125   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.};
00126 
00127   char champ3[MED_NAME_SIZE+1]="fieldcelldouble" ;
00128   char champ3_comp[MED_SNAME_SIZE*3+1]="comp1           comp2           comp3           " ;
00129   char champ3_unit[MED_SNAME_SIZE*3+1]="M/S             m/s             m/s             " ;
00130   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.};
00131 
00132   /***************************************************************************/
00133   fid = MEDfileOpen("cube_hexa8.med",MED_ACC_RDWR);
00134   if (fid < 0)
00135     ret = -1;
00136   else
00137     ret = 0;
00138   printf("MEDouvrir : %d\n",ret);
00139 
00140   /***************************************************************************/
00141   if (ret == 0)
00142     ret = MEDmeshCr(fid,maa,mdim,mdim,MED_UNSTRUCTURED_MESH,maadesc,dtunitp3,
00143                     MED_SORT_DTIT,MED_CARTESIAN,nomcoo,unicoo);
00144   printf("MEDmaaCr : %d\n",ret);
00145   if (ret == 0)
00146     ret = MEDunvCr(fid,maa);
00147   printf("MEDunvCr : %d\n",ret);
00148 
00149   /***************************************************************************/
00150   if (ret == 0)
00151     ret = MEDmeshNodeCoordinateWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,MED_FULL_INTERLACE,nnoe,coo);
00152   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NODE,0,nnoe,nufano);
00153   printf("MEDnoeudsEcr : %d\n",ret);
00154 
00155   /* ecriture des mailles MED_HEXA8 :
00156      - connectivite
00157      - noms (optionnel) 
00158      - numeros (optionnel)
00159      - numeros des familles */
00160   if (ret == 0) 
00161     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
00162                                        MED_CELL,MED_HEXA8,MED_NODAL,MED_FULL_INTERLACE,
00163                                        nhexa8,hexa8);
00164   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_HEXA8,nhexa8,nufahexa8);
00165   printf("MEDelementsEcr : %d \n",ret);
00166 
00167   /***************************************************************************/
00168   /* ecriture des familles */
00169   /* Conventions :
00170      - toujours creer une famille de numero 0 ne comportant aucun attribut
00171        ni groupe (famille de reference pour les noeuds ou les elements
00172        qui ne sont rattaches a aucun groupe ni attribut)
00173      - les numeros de familles de noeuds sont > 0
00174      - les numeros de familles des elements sont < 0
00175      - rien d'imposer sur les noms de familles
00176    */ 
00177 
00178   /* la famille 0 */
00179   if (ret == 0)
00180     {
00181       strcpy(nomfam,"FAMILLE_0");
00182       numfam = 0;
00183       ret = MEDfamilyCr(fid,maa,nomfam,numfam,0,gro);
00184     }
00185   printf("%d \n",ret);
00186 
00187   /* on cree :
00188       - 2 familles d'elements (-1,-2) et 
00189       - 3 familles de noeuds (1,2,3) 
00190   */
00191 
00192   if (ret == 0)
00193     {
00194       nfame = 2;
00195       for (i=0;i<nfame;i++)
00196         {
00197           if (ret == 0)
00198             {
00199               strcpy(nomfam,"FAMILLE_ELEMENT_");
00200               numfam = -(i+1);
00201               sprintf(nomfam,"%s%d",nomfam,-numfam);
00202               attide = 1;
00203               attval = numfam*100;
00204               natt = 1;
00205               //strcpy(attdes,"description attribut");
00206               strcpy(gro,"groupe1");
00207               ngro = 1;
00208 
00209               /*printf("nomfam : %s - numfam : %d - attide : %d - attval : %d - ngro : %d \n",nomfam,numfam,attide,attval,ngro);*/
00210 
00211               ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
00212               printf("MEDfamCr : %d\n",ret);
00213             }
00214         }
00215     }
00216   
00217   if (ret == 0)
00218     {
00219       nfamn = 3;
00220       for (i=0;i<nfamn;i++)
00221         {
00222           if(ret == 0)
00223             {
00224               strcpy(nomfam,"FAMILLE_NOEUD_");
00225               numfam = i+1;
00226               sprintf(nomfam,"%s%d",nomfam,numfam);
00227               attide = 1;
00228               attval = numfam*100;
00229               natt = 1;
00230               //strcpy(attdes,"description attribut");
00231               strcpy(gro,"groupe2");
00232               ngro = 1;
00233               ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
00234               printf("MEDfamCr : %d\n",ret);
00235             }
00236         }
00237     }
00238 
00239   /***************************************************************************/
00240   /*
00241     Les champs
00242   */
00243   if (ret == 0)
00244     {
00245       ret = MEDfieldCr(fid,champ1,MED_INT32,1,champ1_comp,champ1_unit,dtunitp3,maa);
00246       printf("MEDchampCr : %d \n",ret);
00247       if (ret == 0) {
00248         ret = MEDfieldValueWr(fid, champ1,MED_NO_DT,MED_NO_IT,0.,MED_NODE,0,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodeint);
00249         
00250         printf("MEDchampEcr : %d \n",ret);
00251       }
00252     }
00253   
00254   if (ret == 0)
00255     {
00256       ret = MEDfieldCr(fid,champ2,MED_FLOAT64,1,champ2_comp,champ2_unit,dtunitp3,maa);
00257       printf("MEDchampCr : %d \n",ret);
00258       if (ret == 0) {
00259         ret = MEDfieldValueWr(fid, champ2,1,MED_NO_IT,1.1 ,MED_NODE,0,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
00260         printf("MEDchampEcr1 : %d \n",ret);
00261         ret = MEDfieldValueWr(fid, champ2,2,MED_NO_IT,1.2 ,MED_NODE,0,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble2);
00262         printf("MEDchampEcr2 : %d \n",ret);
00263       }
00264     }
00265   
00266   /* on met champ2 sans pas de temps pour pouvoir le lire aussi par defaut !*/
00267   if (ret == 0)
00268     {
00269       ret = MEDfieldValueWr(fid, champ2,MED_NO_DT,MED_NO_IT,0. ,MED_NODE,0,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
00270       printf("MEDchampEcr : %d \n",ret); 
00271     }
00272 
00273   if (ret == 0)
00274     {
00275       ret = MEDfieldCr(fid,champ3,MED_FLOAT64,3,champ3_comp,champ3_unit,dtunitp3,maa);
00276       printf("MEDchampCr : %d \n",ret);
00277       if (ret == 0) {
00278         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);
00279         printf("MEDchampEcr : %d \n",ret);
00280       }
00281     }
00282 
00283   /***************************************************************************/
00284 
00285   ret = MEDfermer(fid);
00286   printf("%d\n",ret);
00287   
00288   if ( getenv("srcdir") ) 
00289     /* we are in 'make check' */
00290     remove( "cube_hexa8.med" );
00291 
00292   return 0;
00293 }