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