# UsesCase_MEDmesh_12.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 12 : read a 2D unstructured mesh with moving grid (generic approach)
    00020  */
    00021
    00022 #include <med.h>
    00023 #define MESGERR 1
    00024 #include <med_utils.h>
    00025
    00026 #include <string.h>
    00027
    00028 int main (int argc, char **argv) {
    00029   med_idt fid;
    00030   med_int nmesh;
    00031   char meshname[MED_NAME_SIZE+1]="";
    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;
    00040   char *unitname;
    00041   char dtunit[MED_SNAME_SIZE+1]="";
    00042   med_float *coordinates = NULL;
    00043   med_int ngeo = 0;
    00044   med_int nnodes = 0;
    00045   med_int *connectivity = NULL;
    00046   med_bool coordinatechangement;
    00047   med_bool geotransformation;
    00048   med_bool matrixtransformation;
    00049   med_int matrixsize;
    00050   med_float matrix[7]={ 0, 0, 0, 0, 0, 0, 0};
    00051   int i, it, j;
    00052   med_int profilesize;
    00053   char profilename[MED_NAME_SIZE+1]="";
    00054   med_int numdt, numit;
    00055   med_float dt;
    00056   med_geometry_type geotype;
    00057   med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
    00058   int ret=-1;
    00059
    00060   /* open MED file with READ ONLY access mode */
    00061   fid = MEDfileOpen("UsesCase_MEDmesh_9.med",MED_ACC_RDONLY);
    00062   if (fid < 0) {
    00063     MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
    00064     goto ERROR;
    00065   }
    00066
    00067
    00068   /* read how many mesh in the file */
    00069   if ((nmesh = MEDnMesh(fid)) < 0) {
    00070     MESSAGE("ERROR : read how many mesh ...");
    00071     goto ERROR;
    00072   }
    00073
    00074   for (i=0;i<nmesh;i++) {
    00075
    00076     /* read computation space dimension */
    00077     if ((spacedim = MEDmeshnAxis(fid, i+1)) < 0) {
    00078       MESSAGE("ERROR : read computation space dimension ...");
    00079       goto ERROR;
    00080     }
    00081
    00082     /* memory allocation */
    00083     if ((axisname  = (char*) malloc(MED_SNAME_SIZE*spacedim+1)) == NULL) {
    00084       MESSAGE("ERROR : memory allocation ...");
    00085       goto ERROR;
    00086     }
    00087     if ((unitname  = (char*) malloc(MED_SNAME_SIZE*spacedim+1)) == NULL) {
    00088       MESSAGE("ERROR : memory allocation ...");
    00089       goto ERROR;
    00090     }
    00091
    00092     /* read mesh informations : meshname, mesh dimension, mesh type ... */
    00093     if (MEDmeshInfo(fid, i+1, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
    00094                     dtunit, &sortingtype, &nstep,
    00095                     &axistype, axisname, unitname) < 0) {
    00096       MESSAGE("ERROR : mesh info ...");
    00097       free(axisname);
    00098       free(unitname);
    00099       goto ERROR;
    00100     }
    00101     free(axisname);
    00102     free(unitname);
    00103
    00104
    00105     /* read how many nodes in the mesh */
    00106     if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
    00107                                  MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
    00108                                  &geotransformation)) < 0) {
    00109       MESSAGE("ERROR : number of nodes ...");
    00110       goto ERROR;
    00111     }
    00112
    00113     /* read mesh nodes coordinates */
    00114     if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
    00115       MESSAGE("ERROR : memory allocation ...");
    00116       goto ERROR;
    00117     }
    00118
    00119     if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
    00120                                 coordinates) < 0) {
    00121       MESSAGE("ERROR : nodes coordinates ...");
    00122       free(coordinates);
    00123       goto ERROR;
    00124     }
    00125
    00126     /* read all MED geometry cell types */
    00127     for (it=1; it<= MED_N_CELL_FIXED_GEO; it++) {
    00128
    00129       geotype = geotypes[it];
    00130
    00131       if ((ngeo = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,geotype,
    00132                                  MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
    00133                                  &geotransformation)) < 0) {
    00134         MESSAGE("ERROR : number of cell ...");
    00135         ISCRUTE(geotype);
    00136         goto ERROR;
    00137       }
    00138
    00139       if (ngeo) {
    00140         /* read cells connectivity in the mesh */
    00141         if ((connectivity = (med_int *) malloc(sizeof(med_int)*ngeo*(geotype%100))) == NULL) {
    00142           MESSAGE("ERROR : memory allocation ...");
    00143           goto ERROR;
    00144         }
    00145
    00146         if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
    00147                                          geotype, MED_NODAL, MED_FULL_INTERLACE, connectivity) < 0) {
    00148           MESSAGE("ERROR : cellconnectivity ...");
    00149           ISCRUTE(geotype);
    00150           free(connectivity);
    00151           goto ERROR;
    00152         }
    00153
    00154         /* memory deallocation */
    00155         free(connectivity);
    00156         connectivity = NULL;
    00157       }
    00158     }
    00159
    00160
    00161     /* read nodes coordinates changements step by step */
    00162     for (it=1;it<nstep;it++) {
    00163
    00164       if (MEDmeshComputationStepInfo(fid, meshname, it+1,
    00165                                      &numdt, &numit, &dt) < 0) {
    00166         MESSAGE("ERROR : Computing step info ...");
    00167         SSCRUTE(meshname);
    00168         goto ERROR;
    00169       }
    00170
    00171       /* test changement : for nodes coordinates */
    00172       if ((nnodes = MEDmeshnEntityWithProfile(fid, meshname, numdt, numit,
    00173                                               MED_NODE, MED_NONE,
    00174                                               MED_COORDINATE, MED_NO_CMODE,
    00175                                               MED_GLOBAL_PFLMODE, profilename, &profilesize,
    00176                                               &coordinatechangement, &geotransformation)) < 0) {
    00177         MESSAGE("ERROR : number of nodes ...");
    00178         goto ERROR;
    00179       }
    00180
    00181       /* if only coordinates have changed, then read the new coordinates */
    00182       if (coordinatechangement && geotransformation) {
    00183         if (MEDmeshNodeCoordinateWithProfileRd(fid, meshname, numdt, numit,
    00184                                                MED_GLOBAL_PFLMODE,profilename,
    00185                                                MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,
    00186                                                coordinates) < 0) {
    00187           MESSAGE("ERROR : nodes coordinates ...");
    00188           free(coordinates);
    00189           goto ERROR;
    00190         }
    00191       }
    00192
    00193       if (coordinatechangement && ! geotransformation) {
    00194
    00195         matrixsize = MEDmeshnEntity(fid,meshname,numdt,numit,
    00196                                     MED_NODE,MED_NONE,MED_COORDINATE_TRSF,MED_NODAL,&coordinatechangement,
    00197                                     &matrixtransformation);
    00198
    00199         if (matrixsize < 0) {
    00200           MESSAGE("ERROR : matrix transformation ...");
    00201           goto ERROR;
    00202         }
    00203
    00204         if (matrixtransformation) {
    00205           if ( MEDmeshNodeCoordinateTrsfRd(fid, meshname, numdt, numit, matrix) < 0 ) {
    00206             MESSAGE("ERROR : read transformation matrix ...");
    00207             goto ERROR;
    00208           }
    00209         }
    00210       }
    00211     }
    00212   }
    00213   free(coordinates);
    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
    00225   return ret;
    00226 }
    00227