UsesCase_MEDmesh_7.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2015  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 7 : read a 2D unstructured mesh with nodes coordinates modifications
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   const char meshname[MED_NAME_SIZE+1] = "2D unstructured mesh";
00031   char meshdescription[MED_COMMENT_SIZE+1]="";
00032   med_int meshdim;
00033   med_int spacedim;
00034   med_sorting_type sortingtype;
00035   med_int nstep;
00036   med_mesh_type meshtype;
00037   med_axis_type axistype;
00038   char axisname[2*MED_SNAME_SIZE+1]="";
00039   char unitname[2*MED_SNAME_SIZE+1]="";
00040   char dtunit[MED_SNAME_SIZE+1]="";
00041   med_float *coordinates = NULL;
00042   med_int nnodes = 0;
00043   med_int *triaconnectivity = NULL;
00044   med_int ntria3 = 0;
00045   med_int *quadconnectivity = NULL;
00046   med_int nquad4 = 0;
00047   med_bool coordinatechangement;
00048   med_bool geotransformation;
00049   int i, it;
00050   med_int profilesize;
00051   char profilename[MED_NAME_SIZE+1]="";
00052   med_int numdt, numit;
00053   med_float dt;
00054   int ret=-1;
00055 
00056   /* open MED file with READ ONLY access mode */
00057   fid = MEDfileOpen("UsesCase_MEDmesh_6.med",MED_ACC_RDONLY);
00058   if (fid < 0) {
00059     MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
00060     goto ERROR;
00061   }
00062 
00063   /* 
00064    * ... we know that the MED file has only one mesh, 
00065    * a real code working would check ... 
00066    */
00067 
00068   /* read mesh informations : mesh dimension, space dimension ... */
00069   if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription, 
00070                         dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
00071     MESSAGE("ERROR : mesh info ...");
00072     goto ERROR;
00073   }
00074 
00075   /* read how many nodes in the mesh */
00076   if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE,  MED_NO_GEOTYPE,
00077                                MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
00078                                &geotransformation)) < 0) {
00079     MESSAGE("ERROR : number of nodes ...");
00080     goto ERROR;
00081   }
00082   
00083   /* 
00084    * ... we know that we only have MED_TRIA3 and MED_QUAD4 in the mesh, 
00085    * a real code working would check all MED geometry cell types ... 
00086    */
00087 
00088   /* read how many triangular cells in the mesh */
00089   if ((ntria3 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_TRIA3,
00090                                MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
00091                                &geotransformation)) < 0) {
00092     MESSAGE("ERROR : number of MED_TRIA3 ...");
00093     goto ERROR;
00094   }
00095 
00096   /* read how many quadrangular cells in the mesh */
00097   if ((nquad4 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_QUAD4,
00098                                MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
00099                                &geotransformation)) < 0) {
00100     MESSAGE("ERROR : number of MED_QUAD4 ...");
00101     goto ERROR;
00102   }
00103 
00104   /* read mesh nodes coordinates in the initial mesh */
00105   if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
00106     MESSAGE("ERROR : memory allocation ...");
00107     goto ERROR;
00108   }
00109 
00110   if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
00111                               coordinates) < 0) {
00112     MESSAGE("ERROR : nodes coordinates ...");
00113     free(coordinates);
00114     goto ERROR;
00115   }
00116 
00117   /* read cells connectivity in the initial mesh */
00118   if ((triaconnectivity = (med_int *) malloc(sizeof(med_int)*ntria3*3)) == NULL) {
00119     MESSAGE("ERROR : memory allocation ...");
00120     goto ERROR;
00121   }
00122   if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
00123                                    MED_TRIA3, MED_NODAL, MED_FULL_INTERLACE, triaconnectivity) < 0) {
00124     MESSAGE("ERROR : MED_TRIA3 connectivity ...");
00125     free(triaconnectivity);
00126     goto ERROR;
00127   }
00128   free(triaconnectivity);
00129 
00130   if ((quadconnectivity = (med_int *) malloc(sizeof(med_int)*nquad4*4)) == NULL) {
00131     MESSAGE("ERROR : memory allocation ...");
00132     goto ERROR;
00133   }
00134   if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
00135                                    MED_QUAD4, MED_NODAL, MED_FULL_INTERLACE, quadconnectivity) < 0) {
00136     MESSAGE("ERROR : MED_QUAD4 connectivity ...");
00137     free(quadconnectivity);
00138     goto ERROR;
00139   }
00140   free(quadconnectivity);
00141 
00142   /* 
00143    * ... we know that the family number of nodes and elements is 0, a real working would check ...
00144    */
00145 
00146   /* read nodes coordinates changements step by step */
00147   for (it=1;it<nstep;it++) {
00148 
00149     if (MEDmeshComputationStepInfo(fid, meshname, it+1, 
00150                                    &numdt, &numit, &dt) < 0) {
00151       MESSAGE("ERROR : Computing step info ...");
00152       SSCRUTE(meshname);
00153       goto ERROR;
00154     }
00155   
00156     /* test changement : for nodes coordinates */
00157     if ((nnodes = MEDmeshnEntityWithProfile(fid, meshname, numdt, numit, 
00158                                             MED_NODE,  MED_NO_GEOTYPE,
00159                                             MED_COORDINATE, MED_NO_CMODE,
00160                                             MED_GLOBAL_PFLMODE, profilename, &profilesize,
00161                                             &coordinatechangement, &geotransformation)) < 0) {
00162       MESSAGE("ERROR : number of nodes ...");
00163       goto ERROR;
00164     }
00165 
00166     /* if coordinates have changed, then read the new coordinates */
00167     if (coordinatechangement) {
00168       if (MEDmeshNodeCoordinateWithProfileRd(fid, meshname, numdt, numit, 
00169                                              MED_GLOBAL_PFLMODE,profilename,
00170                                              MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,
00171                                              coordinates) < 0) {
00172         MESSAGE("ERROR : nodes coordinates ...");
00173         free(coordinates);
00174         goto ERROR;
00175       }
00176     }
00177       
00178   }
00179 
00180   free(coordinates);
00181   
00182   ret=0;
00183  ERROR:
00184 
00185   /* close MED file */
00186   if (MEDfileClose(fid) < 0) {
00187     MESSAGE("ERROR : close file");             
00188     ret=-1; 
00189   } 
00190 
00191 
00192   return ret;
00193 }
00194 

Généré le Thu Oct 8 14:26:17 2015 pour MED fichier par  doxygen 1.6.1