UsesCase_MEDstructElement_3.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 #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 dynamically struct element name for each struct element model,
00098      then for each type read the connectivity if a support mesh exist and
00099      finaly the attribute(s) */
00100   for (i=0;i<nmodels;i++) {
00101 
00102     /* read the geotype */
00103     if (MEDmeshEntityInfo(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT,i+1,elementname,(geotype+i)) < 0) {
00104       MESSAGE("ERROR : name and type of MED_STRUCT_ELEMENT ...");
00105       goto ERROR;
00106     }
00107 
00108     /* read how many MED_STRUCT_ELEMENT of type *(geotype+i) there is in the mesh */
00109     if ((nentities = MEDmeshnEntity(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT,*(geotype+i),
00110                                     MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
00111                                     &geotransformation)) < 0) {
00112       MESSAGE("ERROR : number of MED_STRUCT_ELEMENT ...");
00113       goto ERROR;
00114     }
00115 
00116     /* read informations about the model */
00117     if (MEDstructElementInfoByName(fid, elementname, geotype+i, &elementdim,
00118                                    supportmeshname, &entitype, &nnode, &ncell,
00119                                    &geocelltype, &nconstatt, &anyprofile, nvaratt+i) < 0) {
00120       MESSAGE("ERROR : struct element models information ...");
00121       goto ERROR;
00122     }
00123     
00124     /* read support mesh  */
00125     if (strcmp(supportmeshname,"")) {
00126       if ( MEDsupportMeshInfoByName(fid, supportmeshname, &spacedim, &meshdim, description,
00127                                     &axistype, axisname, axisunit) < 0 ) {
00128         MESSAGE("ERROR : read information about mesh support ...");
00129         goto ERROR;
00130       }
00131 
00132       ISCRUTE(nnode);
00133       /* read how many nodes in the support mesh */
00134       if ((nnode = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
00135                                   MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
00136                                   &geotransformation)) < 0) {
00137         MESSAGE("ERROR : read number of nodes ...");
00138         goto ERROR;
00139       }
00140      ISCRUTE(nnode);
00141 
00142       /* read mesh nodes coordinates */
00143       coordinates = (med_float*) malloc(sizeof(med_float)*nnode*spacedim);
00144 
00145       if (MEDmeshNodeCoordinateRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
00146                                   coordinates) < 0) {
00147         MESSAGE("ERROR : read nodes coordinates ...");
00148         free(coordinates);
00149         goto ERROR;
00150       }
00151 
00152       /* free memory */
00153       free(coordinates);
00154 
00155       /* read how many MED_SEG2 cells in the mesh */
00156       if ((nseg2 = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,geocelltype,
00157                                   MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
00158                                   &geotransformation)) < 0) {
00159         MESSAGE("ERROR : number of MED_SEG2 ...");
00160         goto ERROR;
00161       }
00162      ISCRUTE(nseg2);
00163      ISCRUTE(ncell);
00164       /* read MED_SEG2 connectivity if necessary */
00165       if (nseg2 > 0) {
00166         seg2connectivity = (med_int *) malloc(sizeof(med_int)*nseg2*2);
00167 
00168         if (MEDmeshElementConnectivityRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,
00169                                          geocelltype, MED_NODAL, MED_FULL_INTERLACE, seg2connectivity) < 0) {
00170           MESSAGE("ERROR : MED_SEG2 connectivity ...");
00171           free(seg2connectivity);
00172           goto ERROR;
00173         }
00174         free(seg2connectivity);
00175       }
00176     }
00177 
00178     /* read constant attribute(s) */
00179     for (j=0; j<nconstatt; j++) {
00180       if ( MEDstructElementConstAttInfo(fid, elementname, j+1,
00181                                         attname, &atttype, &nattcomp, &attentitype,
00182                                         profilename, &profilesize) < 0) {
00183         MESSAGE("ERROR : const attribute information ...");
00184         goto ERROR;
00185       }
00186 
00187       /* if there is a profile => read the profile, see UsesCase_MEDprofile_2 */
00188 
00189       /* memory allocation */
00190       if (profilesize != 0)
00191         size = profilesize*nattcomp*MEDstructElementAttSizeof(atttype);
00192       else
00193         if (attentitype== MED_NODE)
00194           size = nnode*nattcomp*MEDstructElementAttSizeof(atttype);
00195         else
00196           size = ncell*nattcomp*MEDstructElementAttSizeof(atttype);
00197       if ( atttype == MED_ATT_NAME) ++size;
00198       ISCRUTE(size);
00199       value = (med_float *) malloc(size);
00200 
00201 
00202       /* read attribute(s) value(s) */
00203       if ( MEDstructElementConstAttRd(fid, elementname, attname, (unsigned char *)value ) < 0 ) {
00204         MESSAGE("ERROR : const attribute value ...");
00205         free(value);
00206         goto ERROR;
00207       }
00208 
00209       /* memory deallocation */
00210       free(value);
00211     }
00212 
00213     /* read variable attribute(s) */
00214     for (j=0; j<*(nvaratt+i); j++) {
00215 
00216       /* read informations about the attribute */
00217       if ( MEDstructElementVarAttInfo(fid, elementname, j+1, 
00218                                       attname, &atttype, &nattcomp) < 0) {
00219             MESSAGE("ERROR : var attribute information ...");
00220             goto ERROR;
00221       }
00222       
00223       /* memory allocation */
00224       size = nentities*nattcomp*MEDstructElementAttSizeof(atttype);
00225       if ( atttype == MED_ATT_NAME) ++size;
00226       ISCRUTE(size);
00227       value = (med_float *) malloc((size)*sizeof(char));
00228       
00229       /* read attribute values */
00230       if (MEDmeshStructElementVarAttRd(fid, computmeshname, MED_NO_DT, MED_NO_IT,
00231                                        *(geotype+i), attname, value ) < 0) {
00232         MESSAGE("ERROR : read variable attributes values ...");
00233         free(value);
00234         goto ERROR;
00235       }
00236 
00237       /* free memory */
00238       free(value);
00239       
00240     }
00241     
00242   }
00243 
00244   ret=0;
00245  ERROR:
00246 
00247   /* free memory */
00248   free(geotype);
00249   free(nvaratt);
00250 
00251   /* close file */
00252   if (MEDfileClose(fid) < 0) {
00253     MESSAGE("ERROR : file closing ...");
00254     ret=-1;
00255   }
00256 
00257   return ret;
00258 }
00259 

Généré le Mon May 16 17:10:24 2011 pour MED fichier par  doxygen 1.6.1