# UsesCase_MEDmesh_11.c

Aller à la documentation de ce fichier.

    00001 /*  This file is part of MED.
    00002  *
    00003  *  COPYRIGHT (C) 1999 - 2011  EDF R&D, CEA/DEN
    00004  *  MED is free software: you can redistribute it and/or modify
    00005  *  it under the terms of the GNU Lesser General Public License as published by
    00006  *  the Free Software Foundation, either version 3 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  MED is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Lesser General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU Lesser General Public License
    00015  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
    00016  */
    00017
    00018 /*
    00019  *  Use case 11 : read a 2D unstructured mesh with 15 nodes, 8 triangular cells, 4 quadragular cells with
    00020  *  nodes families
    00021  */
    00022
    00023 #include <med.h>
    00024 #define MESGERR 1
    00025 #include <med_utils.h>
    00026
    00027 #include <string.h>
    00028
    00029 int main (int argc, char **argv) {
    00030   med_idt fid;
    00031   const char meshname[MED_NAME_SIZE+1] = "2D unstructured mesh";
    00032   char meshdescription[MED_COMMENT_SIZE+1]="";
    00033   med_int meshdim;
    00034   med_int spacedim;
    00035   med_sorting_type sortingtype;
    00036   med_int nstep;
    00037   med_mesh_type meshtype;
    00038   med_axis_type axistype;
    00039   char axisname[2*MED_SNAME_SIZE+1]="";
    00040   char unitname[2*MED_SNAME_SIZE+1]="";
    00041   char dtunit[MED_SNAME_SIZE+1];
    00042   med_float *coordinates = NULL;
    00043   med_int nnodes = 0;
    00044   med_int *triaconnectivity = NULL;
    00045   med_int ntria3 = 0;
    00046   med_int *quadconnectivity = NULL;
    00047   med_int nquad4 = 0;
    00048   med_bool coordinatechangement;
    00049   med_bool geotransformation;
    00050   int i;
    00051   med_int nfamily, ngroup;
    00052   med_int familynumber;
    00053   char *groupname=NULL;
    00054   char familyname[MED_NAME_SIZE+1]="";
    00055   med_int *familynumbers = NULL;
    00056   int ret=-1;
    00057
    00058   /* open MED file with READ ONLY access mode */
    00059   fid = MEDfileOpen("UsesCase_MEDmesh_10.med",MED_ACC_RDONLY);
    00060   if (fid < 0) {
    00061     MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
    00062     goto ERROR;
    00063   }
    00064
    00065   /*
    00066    * ... we know that the MED file has only one mesh,
    00067    * a real code working would check ...
    00068    */
    00069
    00070   /* read mesh informations : mesh dimension, space dimension ... */
    00071   if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
    00072                         dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
    00073     MESSAGE("ERROR : mesh info ...");
    00074     goto ERROR;
    00075   }
    00076
    00077   /* read how many nodes in the mesh */
    00078   if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
    00079                                MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
    00080                                &geotransformation)) < 0) {
    00081     MESSAGE("ERROR : number of nodes ...");
    00082     goto ERROR;
    00083   }
    00084
    00085   /*
    00086    * ... we know that we only have MED_TRIA3 and MED_QUAD4 in the mesh,
    00087    * a real code working would check all MED geometry cell types ...
    00088    */
    00089
    00090   /* read how many triangular cells in the mesh */
    00091   if ((ntria3 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_TRIA3,
    00092                                MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
    00093                                &geotransformation)) < 0) {
    00094     MESSAGE("ERROR : number of MED_TRIA3 ...");
    00095     goto ERROR;
    00096   }
    00097
    00098   /* read how many quadrangular cells in the mesh */
    00099   if ((nquad4 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_QUAD4,
    00100                                MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
    00101                                &geotransformation)) < 0) {
    00102     MESSAGE("ERROR : number of MED_QUAD4 ...");
    00103     goto ERROR;
    00104   }
    00105
    00106   /* read mesh nodes coordinates */
    00107   if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
    00108     MESSAGE("ERROR : memory allocation ...");
    00109     goto ERROR;
    00110   }
    00111
    00112   if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
    00113                               coordinates) < 0) {
    00114     MESSAGE("ERROR : nodes coordinates ...");
    00115     free(coordinates);
    00116     goto ERROR;
    00117   }
    00118
    00119   free(coordinates);
    00120
    00121   /* read cells connectivity in the mesh */
    00122   if ((triaconnectivity = (med_int *) malloc(sizeof(med_int)*ntria3*3)) == NULL) {
    00123     MESSAGE("ERROR : memory allocation ...");
    00124     goto ERROR;
    00125   }
    00126   if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
    00127                                    MED_TRIA3, MED_NODAL, MED_FULL_INTERLACE, triaconnectivity) < 0) {
    00128     MESSAGE("ERROR : MED_TRIA3 connectivity ...");
    00129     free(triaconnectivity);
    00130     goto ERROR;
    00131   }
    00132   free(triaconnectivity);
    00133
    00134   if ((quadconnectivity = (med_int *) malloc(sizeof(med_int)*nquad4*4)) == NULL) {
    00135     MESSAGE("ERROR : memory allocation ...");
    00136     goto ERROR;
    00137   }
    00138   if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
    00139                                    MED_QUAD4, MED_NODAL, MED_FULL_INTERLACE, quadconnectivity) < 0) {
    00140     MESSAGE("ERROR : MED_TRIA3 connectivity ...");
    00141     free(quadconnectivity);
    00142     goto ERROR;
    00143   }
    00144   free(quadconnectivity);
    00145
    00146   /*
    00147    * read families of entities...
    00148    */
    00149   if ((nfamily = MEDnFamily(fid,meshname)) < 0) {
    00150     MESSAGE("ERROR : read number of family ...");
    00151     goto ERROR;
    00152   }
    00153
    00154   for (i=0; i<nfamily ; i++) {
    00155
    00156     if ((ngroup = MEDnFamilyGroup(fid, meshname, i+1)) < 0) {
    00157       MESSAGE("ERROR : read number of group in a family ...");
    00158       goto ERROR;
    00159     }
    00160
    00161     if (ngroup > 0) {
    00162       if ((groupname = (char*) malloc(sizeof(char)*MED_LNAME_SIZE*ngroup+1)) == NULL) {
    00163         MESSAGE("ERROR : memory allocation ...");
    00164         goto ERROR;
    00165       }
    00166
    00167       if (MEDfamilyInfo(fid, meshname, i+1, familyname, &familynumber, groupname) < 0) {
    00168         MESSAGE("ERROR : family info ...");
    00169         free(groupname);
    00170         goto ERROR;
    00171       }
    00172       free(groupname);
    00173     }
    00174
    00175   }
    00176
    00177   /* read family numbers for nodes */
    00178   /* By convention, if there is no numbers in the file, it means that 0 is the family
    00179      number of all nodes */
    00180   if ((familynumbers = (med_int *) malloc(sizeof(med_int)*nnodes)) == NULL) {
    00181     MESSAGE("ERROR : memory allocation ...");
    00182     goto ERROR;
    00183   }
    00184   if (MEDmeshEntityFamilyNumberRd(fid, meshname, MED_NO_DT, MED_NO_IT,
    00185                                   MED_NODE,MED_NONE,familynumbers ) < 0)
    00186     for (i=0; i<nnodes; i++) *(familynumbers+i) = 0;
    00187
    00188   for (i=0; i<nnodes; i++) printf("%d - ", *(familynumbers+i));
    00189
    00190   if (familynumbers)
    00191     free (familynumbers);
    00192
    00193   /* read family numbers for cells */
    00194   if ((familynumbers = (med_int *) malloc(sizeof(med_int)*ntria3)) == NULL) {
    00195     MESSAGE("ERROR : memory allocation ...");
    00196     goto ERROR;
    00197   }
    00198   if (MEDmeshEntityFamilyNumberRd(fid, meshname, MED_NO_DT, MED_NO_IT,
    00199                                   MED_CELL,MED_TRIA3,familynumbers ) < 0)
    00200     for (i=0; i<ntria3; i++) *(familynumbers+i) = 0;
    00201
    00202     free (familynumbers);
    00203
    00204
    00205   if ((familynumbers = (med_int *) malloc(sizeof(med_int)*nquad4)) == NULL) {
    00206     MESSAGE("ERROR : memory allocation ...");
    00207     goto ERROR;
    00208   }
    00209   if (MEDmeshEntityFamilyNumberRd(fid, meshname, MED_NO_DT, MED_NO_IT,
    00210                                   MED_CELL,MED_QUAD4,familynumbers ) < 0)
    00211     for (i=0; i<nquad4; i++) *(familynumbers+i) = 0;
    00212
    00213   free (familynumbers);
    00214
    00215   ret=0;
    00216  ERROR:
    00217
    00218   /* close MED file */
    00219   if (MEDfileClose(fid) < 0) {
    00220     MESSAGE("ERROR : close file");
    00221     ret= -1;
    00222   }
    00223
    00224   return ret;
    00225 }