# UsesCase_MEDstructElement_2.c

Aller à la documentation de ce fichier.

    00001 /*  This file is part of MED.
    00002  *
    00003  *  COPYRIGHT (C) 1999 - 2012  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 #include <med.h>
    00019 #define MESGERR 1
    00020 #include <med_utils.h>
    00021
    00022 #include <string.h>
    00023
    00024 /*
    00025  * StructElement use case 2 : read struct element models in a file
    00026  * Classical iteration approach
    00027  * STEP 1 : read suppport mesh
    00028  * STEP 2 : read struct element model
    00029  * STEP 3 : read in a computation mesh
    00030  * A access from the computation mesh is defined in StructElement use case 3.
    00031  */
    00032
    00033 int main (int argc, char **argv) {
    00034   med_idt fid;
    00035   med_int nmodels, nsmesh;
    00036   int i,j,k;
    00037   char       elementname    [MED_NAME_SIZE+1]="";
    00038   char       supportmeshname[MED_NAME_SIZE+1]="";
    00039   const char computmeshname [MED_NAME_SIZE+1]="COMPUT_MESH";
    00040   med_geometry_type *geotype;
    00041   med_geometry_type geocelltype;
    00042
    00043   med_entity_type entitype;
    00044   med_int elementdim,nnode,ncell;
    00045   med_bool anyprofile=0;
    00046   med_int nconstatt, *nvaratt;
    00047   char attname    [MED_NAME_SIZE+1]="";
    00048   char profilename[MED_NAME_SIZE+1]="";
    00049   med_attribute_type atttype;
    00050   med_int nattcomp;
    00051   med_entity_type attentitype;
    00052   med_int profilesize;
    00053   unsigned char *value;
    00054   med_int size=0;
    00055   med_int meshdim, spacedim;
    00056   char description[MED_COMMENT_SIZE+1]="";
    00057   char axisname   [3*MED_SNAME_SIZE+1]="";
    00058   char axisunit   [3*MED_SNAME_SIZE+1]="";
    00059   med_axis_type axistype;
    00060   med_float *coordinates;
    00061   med_bool coordinatechangement, geotransformation;
    00062   med_int nseg2, *seg2connectivity;
    00063   med_int nentities=0;
    00064   med_sorting_type sortingtype;
    00065   med_mesh_type    meshtype;
    00066   med_int nstep;
    00067   char dtunit  [MED_SNAME_SIZE+1]="";
    00068   char unitname[2*MED_SNAME_SIZE+1]="";
    00069   char tmp     [MED_NAME_SIZE+1]="";
    00070   int ret=-1;
    00071
    00072   /* open file */
    00073   fid = MEDfileOpen("UsesCase_MEDstructElement_1.med",MED_ACC_RDONLY);
    00074   if (fid < 0) {
    00075     MESSAGE("ERROR : file creation ...");
    00076     goto ERROR;
    00077   }
    00078
    00079   /* STEP 1 */
    00080
    00081   /* how many support mesh in the file ? */
    00082   if ((nsmesh = MEDnSupportMesh(fid)) < 0 ) {
    00083     MESSAGE("ERROR : read number of support mesh ...");
    00084     goto ERROR;
    00085   }
    00086
    00087   /* read each support mesh */
    00088   for (i=0; i<nsmesh; i++) {
    00089     if ( MEDsupportMeshInfo(fid, i+1, supportmeshname, &spacedim, &meshdim, description,
    00090                             &axistype, axisname, axisunit) < 0 ) {
    00091       MESSAGE("ERROR : read information about mesh support ...");
    00092       goto ERROR;
    00093     }
    00094
    00095     /* read how many nodes in the mesh */
    00096     if ((nnode = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
    00097                                 MED_COORDINATE, MED_NO_CMODE, &coordinatechangement,
    00098                                 &geotransformation)) < 0) {
    00099       MESSAGE("ERROR : read number of nodes ...");
    00100       goto ERROR;
    00101     }
    00102
    00103     /* read mesh nodes coordinates */
    00104     coordinates = (med_float*) malloc(sizeof(med_float)*nnode*spacedim);
    00105
    00106     if (MEDmeshNodeCoordinateRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
    00107                                 coordinates) < 0) {
    00108       MESSAGE("ERROR : read nodes coordinates ...");
    00109       free(coordinates);
    00110       goto ERROR;
    00111     }
    00112
    00113     /* free memory */
    00114     free(coordinates);
    00115
    00116     /* ... In this case, we suppose that we have only MED_SEG2
    00117      *     as cell elements in our support meshes
    00118      *     a real code working would check ...  */
    00119     if ((nseg2 = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_SEG2,
    00120                                 MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
    00121                                 &geotransformation)) < 0) {
    00122       MESSAGE("ERROR : number of MED_SEG2 ...");
    00123       goto ERROR;
    00124     }
    00125
    00126     /* read MED_SEG2 connectivity if necessary */
    00127     if (nseg2 > 0) {
    00128       seg2connectivity = (med_int *) malloc(sizeof(med_int)*nseg2*2);
    00129
    00130       if (MEDmeshElementConnectivityRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,
    00131                                        MED_SEG2, MED_NODAL, MED_FULL_INTERLACE, seg2connectivity) < 0) {
    00132         MESSAGE("ERROR : MED_SEG2 connectivity ...");
    00133         free(seg2connectivity);
    00134         goto ERROR;
    00135       }
    00136
    00137       free(seg2connectivity);
    00138     }
    00139
    00140   }
    00141   /* STEP 2 */
    00142   /* how many struct element models ? */
    00143   if ((nmodels =  MEDnStructElement(fid)) < 0) {
    00144     MESSAGE("ERROR : read number of struct element models ...");
    00145     goto ERROR;
    00146   }
    00147
    00148   geotype = (med_geometry_type *) malloc(sizeof(med_geometry_type)*nmodels);
    00149   nvaratt = (med_int *) malloc(sizeof(med_int)*nmodels);
    00150
    00151
    00152   /* read each model */
    00153   for (i=0; i<nmodels; i++) {
    00154         if (MEDstructElementInfo(fid, i+1, elementname, geotype+i, &elementdim,
    00155                                  supportmeshname, &entitype, &nnode, &ncell,
    00156                                  &geocelltype, &nconstatt, &anyprofile, nvaratt+i) < 0) {
    00157           MESSAGE("ERROR : struct element models information ...");
    00158           goto ERROR;
    00159         }
    00160
    00161         /* read constant attribute(s) */
    00162         for (j=0; j<nconstatt; j++) {
    00163           if ( MEDstructElementConstAttInfo(fid, elementname, j+1,
    00164                                             attname, &atttype, &nattcomp, &attentitype,
    00165                                             profilename, &profilesize) < 0) {
    00166             MESSAGE("ERROR : const attribute information ...");
    00167             goto ERROR;
    00168           }
    00169
    00170           /* memory allocation */
    00171           if (profilesize != 0)
    00172             size = profilesize*nattcomp*MEDstructElementAttSizeof(atttype);
    00173           else
    00174             if (entitype == MED_NODE)
    00175               size = nnode*nattcomp*MEDstructElementAttSizeof(atttype);
    00176             else
    00177               size = ncell*nattcomp*MEDstructElementAttSizeof(atttype);
    00178           if ( atttype == MED_ATT_NAME) ++size;
    00179           value = (unsigned char *) malloc(size);
    00180
    00181           /* read attribute(s) value(s) */
    00182           if ( MEDstructElementConstAttRd(fid, elementname, attname, (unsigned char *)value ) < 0 ) {
    00183             MESSAGE("ERROR : const attribute value ...");
    00184             free(value);
    00185             goto ERROR;
    00186           }
    00187
    00188           free(value);
    00189
    00190         }
    00191
    00192         /* read variable attribute(s)                              */
    00193         /* values must be read in a computation mesh => see STEP 3 */
    00194   }
    00195
    00196   /* STEP 3 */
    00197
    00198   /*
    00199    * ... In this case, we know that the MED file has only one mesh,
    00200    * a real code working would check ...
    00201    */
    00202   /* read mesh informations : mesh dimension, space dimension ... */
    00203   if (MEDmeshInfoByName(fid, computmeshname, &spacedim, &meshdim, &meshtype, description,
    00204                         dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
    00205     MESSAGE("ERROR : mesh info ...");
    00206     goto ERROR;
    00207   }
    00208
    00209
    00210   /* Get dynamically struct element name for each struct element model,
    00211      then for each type read the connectivity if a support mesh exist and
    00212      finaly the variable(s) attribute(s) */
    00213   for (i=0;i<nmodels;i++) {
    00214
    00215     /* read how many MED_STRUCT_ELEMENT of type *(geotype+i) there is in the mesh */
    00216     if ((nentities = MEDmeshnEntity(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT,*(geotype+i),
    00217                                     MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
    00218                                     &geotransformation)) < 0) {
    00219       MESSAGE("ERROR : number of MED_STRUCT_ELEMENT ...");
    00220       goto ERROR;
    00221     }
    00222
    00223     if (MEDstructElementName(fid,*(geotype+i),elementname) < 0) {
    00224           MESSAGE("ERROR : get element name ...");
    00225           goto ERROR;
    00226     }
    00227
    00228     for (j=0; j<*(nvaratt+i); j++) {
    00229
    00230       /* read informations about each attribute */
    00231       if ( MEDstructElementVarAttInfo(fid, elementname, j+1,
    00232                                       attname, &atttype, &nattcomp) < 0) {
    00233             MESSAGE("ERROR : var attribute information ...");
    00234             goto ERROR;
    00235       }
    00236
    00237       /* memory allocation */
    00238       if (entitype == MED_NODE)
    00239         size = nattcomp*nentities*MEDstructElementAttSizeof(atttype);
    00240       else
    00241         size = nattcomp*nentities*MEDstructElementAttSizeof(atttype);
    00242       if ( atttype == MED_ATT_NAME) ++size;
    00243       value = (unsigned char *) malloc(size);
    00244
    00245       /* read attribute values */
    00246       if (MEDmeshStructElementVarAttRd(fid, computmeshname, MED_NO_DT, MED_NO_IT,
    00247                                        *(geotype+i), attname, value ) < 0) {
    00248         MESSAGE("ERROR : read variable attributes values ...");
    00249         free(value);
    00250         goto ERROR;
    00251       }
    00252
    00253       /*TODO : Lire les connectivit�s des �l�ments de structures */
    00254
    00255       free(value);
    00256
    00257     }
    00258   }
    00259
    00260   ret=0;
    00261  ERROR:
    00262
    00263   free(geotype);
    00264   free(nvaratt);
    00265
    00266   /* close file */
    00267   if (MEDfileClose(fid) < 0) {
    00268     MESSAGE("ERROR : file closing ...");
    00269     ret=-1;
    00270   }
    00271
    00272   return ret;
    00273 }
    00274