# UsesCase_MEDfield_14.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 #include <med.h>
    00025 #define MESGERR 1
    00026 #include <med_utils.h>
    00027
    00028 #include <string.h>
    00029
    00030 /*
    00031  * Field use case 14 : read a field on nodes elements (generic approach) in a MED file
    00032  */
    00033
    00034
    00035 int main (int argc, char **argv) {
    00036   med_idt fid;
    00037   med_idt nfield, i, j;
    00038   char meshname[MED_NAME_SIZE+1]="";
    00039   med_bool localmesh;
    00040   char fieldname[MED_NAME_SIZE+1]="";
    00041   med_field_type fieldtype;
    00042   char *componentname = NULL;
    00043   char *componentunit = NULL;
    00044   char dtunit[MED_SNAME_SIZE+1]="";
    00045   med_float *values = NULL;
    00046   med_int nstep, nvalues;
    00047   med_int ncomponent;
    00048   med_int csit, numit, numdt, it;
    00049   med_float dt;
    00050   med_geometry_type geotype;
    00051   med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
    00052   med_int nprofile, pit, profilesize;
    00053   char profilename[MED_NAME_SIZE+1]="";
    00054   med_int nintegrationpoint;
    00055   char localizationname[MED_NAME_SIZE+1]="";
    00056   int k;
    00057   int ret=-1;
    00058
    00059   /* open file */
    00060   fid = MEDfileOpen("UsesCase_MEDfield_13.med",MED_ACC_RDONLY);
    00061   if (fid < 0) {
    00062     MESSAGE("ERROR : open file ...");
    00063     goto ERROR;
    00064   }
    00065
    00066   /*
    00067    * generic approach : how many fields in the file and identification
    00068    * of each field.
    00069    */
    00070   if ((nfield = MEDnField(fid)) < 0) {
    00071     MESSAGE("ERROR : How many fields in the file ...");
    00072     goto ERROR;
    00073   }
    00074
    00075   /*
    00076    * read values for each field
    00077    */
    00078   for (i=0; i<nfield; i++) {
    00079
    00080     if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
    00081       MESSAGE("ERROR : number of field component ...");
    00082       goto ERROR;
    00083     }
    00084
    00085     if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00086       MESSAGE("ERROR : memory allocation ...");
    00087       goto ERROR;
    00088     }
    00089
    00090     if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
    00091       MESSAGE("ERROR : memory allocation ...");
    00092       goto ERROR;
    00093     }
    00094
    00095     if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
    00096                      componentname, componentunit, dtunit, &nstep) < 0) {
    00097       MESSAGE("ERROR : Field info ...");
    00098       free(componentname);
    00099       free(componentunit);
    00100       goto ERROR;
    00101     }
    00102
    00103     free(componentname);
    00104     free(componentunit);
    00105
    00106     /*
    00107      * Read field values for each computing step
    00108      */
    00109     for (csit=0; csit<nstep; csit++) {
    00110
    00111       if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
    00112         MESSAGE("ERROR : Computing step info ...");
    00113         goto ERROR;
    00114       }
    00115
    00116       /*
    00117        * ... In our case, we suppose that the field values are only defined on nodes element ...
    00118        */
    00119       for (it=1; it<= MED_N_CELL_FIXED_GEO; it++) {
    00120
    00121         geotype = geotypes[it];
    00122         /*
    00123          * How many profile for each geometry type ?
    00124          */
    00125         if ((nprofile = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_NODE_ELEMENT, geotype,
    00126                                          profilename, localizationname)) < 0) {
    00127           MESSAGE("ERROR : read number of profile ");
    00128           goto ERROR;
    00129         }
    00130
    00131         /*
    00132          * Read values for each profile
    00133          */
    00134         for (pit=0; pit<nprofile; pit++) {
    00135
    00136           if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_NODE_ELEMENT, geotype,
    00137                                                    pit+1, MED_COMPACT_PFLMODE, profilename, &profilesize,
    00138                                                    localizationname, &nintegrationpoint)) < 0) {
    00139             MESSAGE("ERROR : read number of values with a profile ...");
    00140             goto ERROR;
    00141           }
    00142
    00143           if (nvalues) {
    00144             if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent*nintegrationpoint)) == NULL) {
    00145               MESSAGE("ERROR : memory allocation ...");
    00146               goto ERROR;
    00147             }
    00148             if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_NODE_ELEMENT, geotype,
    00149                                            MED_COMPACT_PFLMODE, profilename,
    00150                                            MED_FULL_INTERLACE, MED_ALL_CONSTITUENT,
    00151                                            (unsigned char*) values) < 0) {
    00152               MESSAGE("ERROR : read fields values for cells ...");
    00153               free(values);
    00154               goto ERROR;
    00155             }
    00156             free(values);
    00157           }
    00158         }
    00159       }
    00160     }
    00161   }
    00162
    00163   ret=0;
    00164  ERROR:
    00165
    00166   /* close file */
    00167   if (MEDfileClose(fid) < 0) {
    00168     MESSAGE("ERROR : close file ...");
    00169     ret=-1;
    00170   }
    00171
    00172   return ret;
    00173 }