Back to index

salome-med  6.5.0
create_mesh.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 #include <med.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026 
00027 int main (int argc, char **argv)
00028 {
00029   med_err ret;
00030   med_idt fid;
00031   char maa[MED_NAME_SIZE+1] = "maa1";
00032   char maadesc[MED_COMMENT_SIZE+1] = "Example de maillage non structure 3D";
00033   med_int mdim = 3;
00034   med_int nnoe = 19;
00035   med_float coo[57] = {
00036     0.0, 0.0, 0.0, 
00037     0.0, 0.0, 1.0, 
00038     2.0, 0.0, 1.0, 
00039     0.0, 2.0, 1.0, 
00040     -2.0, 0.0, 1.0, 
00041     0.0, -2.0, 1.0, 
00042     1.0, 1.0, 2.0, 
00043     -1.0, 1.0, 2.0, 
00044     -1.0, -1.0, 2.0, 
00045     1.0, -1.0, 2.0, 
00046     1.0, 1.0, 3.0, 
00047     -1.0, 1.0, 3.0, 
00048     -1.0, -1.0, 3.0, 
00049     1.0, -1.0, 3.0, 
00050     1.0, 1.0, 4.0, 
00051     -1.0, 1.0, 4.0, 
00052     -1.0, -1.0, 4.0, 
00053     1.0, -1.0, 4.0,
00054     0.0, 0.0, 5.0
00055   };
00056   char nomcoo[3*MED_SNAME_SIZE+1] = "x               y               z               ";
00057   char unicoo[3*MED_SNAME_SIZE+1] = "cm              cm              cm              ";
00058   //char *nomnoe ;
00059 //  med_int numnoe[19] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
00060   med_int nufano[19] = {1,1,3,3,0,0,2,0,4,0,4,2,4,2,4,2,4,1,3};
00061   
00062   med_int ntet4 = 12;
00063   med_int tet4[12*4] = {
00064     1,2,3,6,
00065     1,2,4,3,
00066     1,2,5,4,
00067     1,2,6,5,
00068     2,7,4,3,
00069     2,8,5,4,
00070     2,9,6,5,
00071     2,10,3,6,
00072     2,7,3,10,
00073     2,8,4,7,
00074     2,9,5,8,
00075     2,10,6,9
00076   };
00077   //char * nomtet4 ; 
00078  // med_int numtet4[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
00079   med_int nufatet4[12] = {-1,-1,-1,-1,0,0,0,0,0,0,-2,-3}; 
00080 
00081   med_int npyra5 = 2;
00082   med_int pyra5[5*2] = {
00083     7,8,9,10,2,
00084     15,18,17,16,19
00085   };
00086  // char nompyra5[MED_TAILLE_PNOM*2+1] = "pyra1           pyra2           ";
00087  // med_int numpyra5[2] = {13,16};
00088   med_int nufapyra5[2] = {0,-3};
00089 
00090   med_int nhexa8 = 2;
00091   med_int hexa8[8*2] = {
00092     11,12,13,14,7,8,9,10,
00093     15,16,17,18,11,12,13,14
00094   };
00095  // char nomhexa8[MED_TAILLE_PNOM*2+1] = "hexa1           hexa2           ";
00096  // med_int numhexa8[2] = {14,15};
00097   med_int nufahexa8[2] = {0,0};
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 gro2[MED_LNAME_SIZE*2+1];
00108   char dtunitp3[MED_LNAME_SIZE+1]="";
00109   int i;
00110   int nfame = 3; 
00111 
00112 
00113   /*
00114     Some fields : 2 on nodes : one int and one double , one on cells : double
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[19]    = {1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7};
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[19] = {1.,1.,1.,2.,2.,2.,3.,3.,3.,4.,4.,4.,5.,5.,5.,6.,6.,6.,7.};
00125   med_float   fieldnodedouble2[19] = {1.,2.,2.,2.,3.,3.,3.,4.,4.,4.,5.,5.,5.,6.,6.,6.,7.,7.,7.};
00126 
00127   char champ3[MED_NAME_SIZE+1]="fieldcelldoublevector" ;
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   fieldcelldouble1[12*3] = {1.,1.,1.,2.,2.,2.,3.,3.,3.,4.,4.,4.,0.,0.,0.,1.,1.,1.,0.,0.,0.,1.,1.,1.,1.,1.,1.,0.,0.,0.,1.,1.,1.,0.,0.,0.};
00131   med_float   fieldcelldouble2[2*3]  = {5.,5.,0.,1.,0.,1.};
00132   med_float   fieldcelldouble3[2*3]  = {6.,6.,1.,0.,1.,0.};
00133 
00134   char champ4[MED_NAME_SIZE+1]="fieldcelldoublescalar" ;
00135   char champ4_comp[MED_SNAME_SIZE+1]="comp1           " ;
00136   char champ4_unit[MED_SNAME_SIZE+1]="m/s             " ;
00137   med_float   fieldcelldouble4[12] = {1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.};
00138   med_float   fieldcelldouble5[2]  = {2.,2.};
00139   med_float   fieldcelldouble6[2]  = {3.,3.};
00140 
00141   /***************************************************************************/
00142   fid = MEDfileOpen("pointe.med",MED_ACC_RDWR);
00143   if (fid < 0)
00144     ret = -1;
00145   else
00146     ret = 0;
00147   printf("MEDouvrir : %d\n",ret);
00148 
00149   /***************************************************************************/
00150   if (ret == 0)
00151     //ret = MEDmeshCr(fid,maa,mdim,mdim,MED_UNSTRUCTURED_MESH,maadesc,dtunitp3,
00152     //MED_SORT_DTIT,MED_CARTESIAN,nomcoo,unicoo);
00153     ret = MEDmeshCr(fid,maa,mdim,mdim,MED_UNSTRUCTURED_MESH,maadesc,dtunitp3,
00154                     MED_SORT_DTIT,MED_CARTESIAN,nomcoo,unicoo);
00155   printf("MEDmaaCr : %d\n",ret);
00156   if (ret == 0)
00157     ret = MEDunvCr(fid,maa);
00158   printf("MEDunvCr : %d\n",ret);
00159 
00160   /***************************************************************************/
00161   if (ret == 0)
00162     ret = MEDmeshNodeCoordinateWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,MED_FULL_INTERLACE,nnoe,coo);
00163   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NODE,0,nnoe,nufano);
00164   printf("MEDnoeudsEcr : %d\n",ret);
00165 
00166 
00167   /***************************************************************************/
00168   /* ecriture des mailles MED_TETRA4 :
00169      - connectivite
00170      - noms (optionnel) 
00171      - numeros (optionnel)
00172      - numeros des familles */
00173   if (ret == 0) 
00174     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
00175                                        MED_CELL,MED_TETRA4,MED_NODAL,MED_FULL_INTERLACE,
00176                                        ntet4,tet4);
00177   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_TETRA4,ntet4,nufatet4);
00178   printf("MEDelementsEcr : %d \n",ret);
00179 
00180   /* ecriture des mailles MED_PYRA5 :
00181      - connectivite
00182      - noms (optionnel) 
00183      - numeros (optionnel)
00184      - numeros des familles */
00185   if (ret == 0) 
00186     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
00187                                        MED_CELL,MED_PYRA5,MED_NODAL,MED_FULL_INTERLACE,
00188                                        npyra5,pyra5);
00189   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_PYRA5,npyra5,nufapyra5);
00190   printf("MEDelementsEcr : %d \n",ret);
00191 
00192   /* ecriture des mailles MED_HEXA8 :
00193      - connectivite
00194      - noms (optionnel) 
00195      - numeros (optionnel)
00196      - numeros des familles */
00197   if (ret == 0) 
00198     ret = MEDmeshElementConnectivityWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_NO_DT,
00199                                        MED_CELL,MED_HEXA8,MED_NODAL,MED_FULL_INTERLACE,
00200                                        nhexa8,hexa8);
00201   MEDmeshEntityFamilyNumberWr(fid,maa,MED_NO_DT,MED_NO_IT,MED_CELL,MED_HEXA8,nhexa8,nufahexa8);
00202   printf("MEDelementsEcr : %d \n",ret);
00203 
00204   /***************************************************************************/
00205   /* ecriture des familles */
00206   /* Conventions :
00207      - toujours creer une famille de numero 0 ne comportant aucun attribut
00208        ni groupe (famille de reference pour les noeuds ou les elements
00209        qui ne sont rattaches a aucun groupe ni attribut)
00210      - les numeros de familles de noeuds sont > 0
00211      - les numeros de familles des elements sont < 0
00212      - rien d'imposer sur les noms de familles
00213    */ 
00214 
00215   /* la famille 0 */
00216   if (ret == 0)
00217     {
00218       strcpy(nomfam,"FAMILLE_0");
00219       numfam = 0;
00220       ret = MEDfamilyCr(fid,maa,nomfam,numfam,0,gro);
00221     }
00222   printf("MEDfamCr : %d \n",ret);
00223 
00224   /* on cree :
00225       - 3 familles d'elements (-1,-2,-3) et 
00226       - 2 familles de noeuds (1,2) 
00227   */
00228   if (ret == 0)
00229     {
00230       nfame = 3;
00231       for (i=0;i<nfame;i++)
00232         {
00233           if (ret == 0)
00234             {
00235               strcpy(nomfam,"FAMILLE_ELEMENT_");
00236               numfam = -(i+1);
00237               sprintf(nomfam,"%s%d",nomfam,-numfam);
00238               attide = 1;
00239               attval = numfam*100;
00240               natt = 1;
00241               //strcpy(attdes,"description attribut");
00242               strcpy(gro,"groupe1");
00243               ngro = 1;
00244               /*printf("nomfam : %s - numfam : %d - attide : %d - attval : %d - ngro : %d \n",nomfam,numfam,attide,attval,ngro);*/
00245               ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
00246               printf("MEDfamCr : %d\n",ret);
00247             }
00248         }
00249     }
00250   
00251   if (ret == 0)
00252     {
00253       strcpy(nomfam,"FAMILLE_NOEUD_1");
00254       numfam = 1;
00255       attide = 1;
00256       attval = numfam*100;
00257       natt = 1;
00258       //strcpy(attdes,"description attribut");
00259       strcpy(gro2,"groupe2");
00260       strcpy(gro2+MED_NAME_SIZE,"groupe3");
00261       ngro = 2;
00262       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro2);
00263       printf("MEDfamCr : %d\n",ret);
00264     }
00265   if (ret == 0)
00266     {
00267       strcpy(nomfam,"FAMILLE_NOEUD_2");
00268       numfam = 2;
00269       attide = 1;
00270       attval = numfam*100;
00271       natt = 1;
00272       //strcpy(attdes,"description attribut");
00273       strcpy(gro2,"groupe3");
00274       strcpy(gro2+MED_LNAME_SIZE,"groupe4");
00275       ngro = 2;
00276       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro2);
00277       printf("MEDfamCr : %d\n",ret);
00278     }
00279   if (ret == 0)
00280     {
00281       strcpy(nomfam,"FAMILLE_NOEUD_3");
00282       numfam = 3;
00283       attide = 1;
00284       attval = numfam*100;
00285       natt = 1;
00286       //strcpy(attdes,"description attribut");
00287       strcpy(gro2,"groupe2");
00288       strcpy(gro2+MED_LNAME_SIZE,"groupe4");
00289       ngro = 2;
00290       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro2);
00291       printf("MEDfamCr : %d\n",ret);
00292     }
00293   if (ret == 0)
00294     {
00295       strcpy(nomfam,"FAMILLE_NOEUD_4");
00296       numfam = 4;
00297       attide = 1;
00298       attval = numfam*100;
00299       natt = 1;
00300       //strcpy(attdes,"description attribut");
00301       strcpy(gro,"groupe5");
00302       ngro = 1;
00303       ret = MEDfamilyCr(fid,maa,nomfam,numfam,ngro,gro);
00304       printf("MEDfamCr : %d\n",ret);
00305     }
00306 
00307   /***************************************************************************/
00308   /*
00309     Les champs
00310   */
00311   if (ret == 0)
00312     {
00313       ret = MEDfieldCr(fid,champ1,MED_INT32,1,champ1_comp,champ1_unit,dtunitp3,maa);
00314       printf("MEDchampCr : %d \n",ret);
00315       if (ret == 0) {
00316         ret = MEDfieldValueWr(fid, champ1,MED_NO_DT,MED_NO_IT,0.,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodeint);
00317         
00318         printf("MEDchampEcr : %d \n",ret);
00319       }
00320     }
00321   
00322   if (ret == 0)
00323     {
00324       ret = MEDfieldCr(fid,champ2,MED_FLOAT64,1,champ2_comp,champ2_unit,dtunitp3,maa);
00325       printf("MEDchampCr : %d \n",ret);
00326       if (ret == 0) {
00327         ret = MEDfieldValueWr(fid, champ2,1,MED_NO_IT,1.1 ,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
00328         printf("MEDchampEcr1 : %d \n",ret);
00329         ret = MEDfieldValueWr(fid, champ2,2,MED_NO_IT,1.2 ,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble2);
00330         printf("MEDchampEcr2 : %d \n",ret);
00331       }
00332     }
00333   
00334   /* on met champ2 sans pas de temps pour pouvoir le lire aussi par defaut !*/
00335   if (ret == 0)
00336     {
00337 /*        ret = MEDfieldCr(fid,champ2,MED_REEL64,1,champ2_comp,champ2_unit,dtunitp3,maa); */
00338 /*        printf("MEDchampCr : %d \n",ret); */
00339       if (ret == 0) {
00340         ret = MEDfieldValueWr(fid, champ2,MED_NO_DT,MED_NO_IT,0. ,MED_NODE,0,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nnoe, (unsigned char *)fieldnodedouble1);
00341         printf("MEDchampEcr : %d \n",ret);
00342       }
00343     }
00344 
00345   if (ret == 0)
00346     {
00347       ret = MEDfieldCr(fid,champ3,MED_FLOAT64,3,champ3_comp,champ3_unit,dtunitp3,maa);
00348       printf("MEDchampCr : %d \n",ret);
00349       if (ret == 0) {
00350         ret = MEDfieldValueWr(fid, champ3,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_TETRA4,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,ntet4, (unsigned char *)fieldcelldouble1);
00351         printf("MEDchampEcr : %d \n",ret);
00352         ret = MEDfieldValueWr(fid, champ3,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_HEXA8,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nhexa8, (unsigned char *)fieldcelldouble3);
00353         printf("MEDchampEcr : %d \n",ret);
00354         ret = MEDfieldValueWr(fid, champ3,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_PYRA5,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,npyra5, (unsigned char *)fieldcelldouble2);
00355         printf("MEDchampEcr : %d \n",ret);
00356       }
00357     }
00358   
00359   if (ret == 0)
00360     {
00361       ret = MEDfieldCr(fid,champ4,MED_FLOAT64,1,champ4_comp,champ4_unit,dtunitp3,maa);
00362       printf("MEDchampCr : %d \n",ret);
00363       if (ret == 0) {
00364         ret = MEDfieldValueWr(fid, champ4,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_TETRA4,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,ntet4, (unsigned char *)fieldcelldouble4);
00365         printf("MEDchampEcr : %d \n",ret);
00366         ret = MEDfieldValueWr(fid, champ4,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_HEXA8,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,nhexa8, (unsigned char *)fieldcelldouble6);
00367         printf("MEDchampEcr : %d \n",ret);
00368         ret = MEDfieldValueWr(fid, champ4,MED_NO_DT,MED_NO_IT,0.,MED_CELL,MED_PYRA5,MED_NO_INTERLACE,MED_ALL_CONSTITUENT,npyra5, (unsigned char *)fieldcelldouble5);
00369         printf("MEDchampEcr : %d \n",ret);
00370       }
00371     }
00372   
00373   /***************************************************************************/
00374   ret = MEDfermer(fid);
00375   printf("MEDfermer : %d\n",ret);
00376   
00377   if ( getenv("srcdir") ) {
00378     /* we are in 'make check' */
00379     remove( "pointe.med" );
00380     printf("Remove generated file");
00381   }
00382 
00383   return 0;
00384 }
00385