MED fichier

UsesCase_MEDmesh_7.c
Aller à la documentation de ce fichier.
1 /* This file is part of MED.
2 *
3 * COPYRIGHT (C) 1999 - 2021 EDF R&D, CEA/DEN
4 * MED is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * MED is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with MED. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * Use case 7 : read a 2D unstructured mesh with nodes coordinates modifications
20 */
21
22 #include <med.h>
23 #define MESGERR 1
24 #include <med_utils.h>
25
26 #include <string.h>
27
28 int main (int argc, char **argv) {
29 med_idt fid;
30 const char meshname[MED_NAME_SIZE+1] = "2D unstructured mesh";
31 char meshdescription[MED_COMMENT_SIZE+1]="";
32 med_int meshdim;
33 med_int spacedim;
34 med_sorting_type sortingtype;
35 med_int nstep;
36 med_mesh_type meshtype;
37 med_axis_type axistype;
38 char axisname[2*MED_SNAME_SIZE+1]="";
39 char unitname[2*MED_SNAME_SIZE+1]="";
40 char dtunit[MED_SNAME_SIZE+1]="";
41 med_float *coordinates = NULL;
42 med_int nnodes = 0;
43 med_int *triaconnectivity = NULL;
44 med_int ntria3 = 0;
45 med_int *quadconnectivity = NULL;
46 med_int nquad4 = 0;
47 med_bool coordinatechangement;
48 med_bool geotransformation;
49 int i, it;
50 med_int profilesize;
51 char profilename[MED_NAME_SIZE+1]="";
52 med_int numdt, numit;
53 med_float dt;
54 int ret=-1;
55
56 /* open MED file with READ ONLY access mode */
57 fid = MEDfileOpen("UsesCase_MEDmesh_6.med",MED_ACC_RDONLY);
58 if (fid < 0) {
59 MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
60 goto ERROR;
61 }
62
63 /*
64 * ... we know that the MED file has only one mesh,
65 * a real code would check ...
66 */
67
68 /* read mesh informations : mesh dimension, space dimension ... */
69 if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
70 dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
71 MESSAGE("ERROR : mesh info ...");
72 goto ERROR;
73 }
74
75 /* read how many nodes in the mesh */
76 if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
77 MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
78 &geotransformation)) < 0) {
79 MESSAGE("ERROR : number of nodes ...");
80 goto ERROR;
81 }
82
83 /*
84 * ... we know that we only have MED_TRIA3 and MED_QUAD4 in the mesh,
85 * a real code would check all MED geometry cell types ...
86 */
87
88 /* read how many triangular cells in the mesh */
89 if ((ntria3 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_TRIA3,
90 MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
91 &geotransformation)) < 0) {
92 MESSAGE("ERROR : number of MED_TRIA3 ...");
93 goto ERROR;
94 }
95
96 /* read how many quadrangular cells in the mesh */
97 if ((nquad4 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_QUAD4,
98 MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
99 &geotransformation)) < 0) {
100 MESSAGE("ERROR : number of MED_QUAD4 ...");
101 goto ERROR;
102 }
103
104 /* read mesh nodes coordinates in the initial mesh */
105 if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
106 MESSAGE("ERROR : memory allocation ...");
107 goto ERROR;
108 }
109
110 if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
111 coordinates) < 0) {
112 MESSAGE("ERROR : nodes coordinates ...");
113 free(coordinates);
114 goto ERROR;
115 }
116
117 /* read cells connectivity in the initial mesh */
118 if ((triaconnectivity = (med_int *) malloc(sizeof(med_int)*ntria3*3)) == NULL) {
119 MESSAGE("ERROR : memory allocation ...");
120 goto ERROR;
121 }
122 if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
123 MED_TRIA3, MED_NODAL, MED_FULL_INTERLACE, triaconnectivity) < 0) {
124 MESSAGE("ERROR : MED_TRIA3 connectivity ...");
125 free(triaconnectivity);
126 goto ERROR;
127 }
128 free(triaconnectivity);
129
130 if ((quadconnectivity = (med_int *) malloc(sizeof(med_int)*nquad4*4)) == NULL) {
131 MESSAGE("ERROR : memory allocation ...");
132 goto ERROR;
133 }
134 if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
135 MED_QUAD4, MED_NODAL, MED_FULL_INTERLACE, quadconnectivity) < 0) {
136 MESSAGE("ERROR : MED_QUAD4 connectivity ...");
137 free(quadconnectivity);
138 goto ERROR;
139 }
140 free(quadconnectivity);
141
142 /*
143 * ... we know that the family number of nodes and elements is 0, a real code would check ...
144 */
145
146 /* read nodes coordinates changements step by step */
147 for (it=1;it<nstep;it++) {
148
149 if (MEDmeshComputationStepInfo(fid, meshname, it+1,
150 &numdt, &numit, &dt) < 0) {
151 MESSAGE("ERROR : Computing step info ...");
152 SSCRUTE(meshname);
153 goto ERROR;
154 }
155
156 /* test changement : for nodes coordinates */
157 if ((nnodes = MEDmeshnEntityWithProfile(fid, meshname, numdt, numit,
158 MED_NODE, MED_NO_GEOTYPE,
159 MED_COORDINATE, MED_NO_CMODE,
160 MED_GLOBAL_STMODE, profilename, &profilesize,
161 &coordinatechangement, &geotransformation)) < 0) {
162 MESSAGE("ERROR : number of nodes ...");
163 goto ERROR;
164 }
165
166 /* if coordinates have changed, then read the new coordinates */
167 if (coordinatechangement) {
168 if (MEDmeshNodeCoordinateWithProfileRd(fid, meshname, numdt, numit,
169 MED_GLOBAL_STMODE,profilename,
170 MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,
171 coordinates) < 0) {
172 MESSAGE("ERROR : nodes coordinates ...");
173 free(coordinates);
174 goto ERROR;
175 }
176 }
177
178 }
179
180 free(coordinates);
181
182 ret=0;
183 ERROR:
184
185 /* close MED file */
186 if (MEDfileClose(fid) < 0) {
187 MESSAGE("ERROR : close file");
188 ret=-1;
189 }
190
191
192 return ret;
193 }
194
MED_TRIA3
#define MED_TRIA3
Definition: med.h:203
MED_FULL_INTERLACE
Definition: med.h:96
MED_COMMENT_SIZE
#define MED_COMMENT_SIZE
Definition: med.h:79
MEDmeshnEntity
MEDC_EXPORT med_int MEDmeshnEntity(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_entity_type entitype, const med_geometry_type geotype, const med_data_type datatype, const med_connectivity_mode cmode, med_bool *const changement, med_bool *const transformation)
Cette routine permet de lire le nombre d'entités dans un maillage pour une étape de calcul donnée.
Definition: MEDmeshnEntity.c:44
MEDmeshComputationStepInfo
MEDC_EXPORT med_err MEDmeshComputationStepInfo(const med_idt fid, const char *const meshname, const int csit, med_int *const numdt, med_int *const numit, med_float *const dt)
Cette routine permet de lire les informations relatives à une étape de calcul d'un maillage.
Definition: MEDmeshComputationStepInfo.c:38
MEDfileOpen
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition: MEDfileOpen.c:42
MED_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:82
MED_NODAL
Definition: med.h:255
MED_NO_GEOTYPE
#define MED_NO_GEOTYPE
Definition: med.h:232
med_idt
hid_t med_idt
Definition: med.h:331
main
int main(int argc, char **argv)
Definition: UsesCase_MEDmesh_7.c:28
MED_ACC_RDONLY
Definition: med.h:120
med_sorting_type
med_sorting_type
Definition: med.h:309
MED_NO_CMODE
Definition: med.h:255
MED_CELL
Definition: med.h:143
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:324
med_int
int med_int
Definition: med.h:342
med.h
med_bool
med_bool
Definition: med.h:260
med_float
double med_float
Definition: med.h:336
MED_NO_DT
#define MED_NO_DT
Definition: med.h:320
MED_CONNECTIVITY
Definition: med.h:149
MEDmeshElementConnectivityRd
MEDC_EXPORT med_err MEDmeshElementConnectivityRd(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_entity_type entitype, const med_geometry_type geotype, const med_connectivity_mode cmode, const med_switch_mode switchmode, med_int *const connectivity)
Cette routine permet de lire dans un maillage le tableau des connectivités pour un type géométrique d...
Definition: MEDmeshElementConnectivityRd.c:40
MED_NODE
Definition: med.h:143
med_mesh_type
med_mesh_type
Definition: med.h:131
SSCRUTE
#define SSCRUTE(chaine)
Definition: med_utils.h:323
MED_GLOBAL_STMODE
Definition: med.h:109
MEDmeshNodeCoordinateRd
MEDC_EXPORT med_err MEDmeshNodeCoordinateRd(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_switch_mode switchmode, med_float *const coordinates)
Cette routine permet de lire dans un maillage le tableau des coordonnées des noeuds,...
Definition: MEDmeshNodeCoordinateRd.c:37
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:81
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
MED_ALL_CONSTITUENT
#define MED_ALL_CONSTITUENT
Definition: med.h:299
MEDmeshnEntityWithProfile
MEDC_EXPORT med_int MEDmeshnEntityWithProfile(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_entity_type entitype, const med_geometry_type geotype, const med_data_type datatype, const med_connectivity_mode cmode, const med_storage_mode storagemode, char *const profilename, med_int *const profilesize, med_bool *const changement, med_bool *const transformation)
Cette routine permet de lire le nombre d'entités dans un maillage pour une étape de calcul et un prof...
Definition: MEDmeshnEntityWithProfile.c:47
MEDmeshInfoByName
MEDC_EXPORT med_err MEDmeshInfoByName(const med_idt fid, const char *const meshname, med_int *const spacedim, med_int *const meshdim, med_mesh_type *const meshtype, char *const description, char *const dtunit, med_sorting_type *const sortingtype, med_int *const nstep, med_axis_type *const axistype, char *const axisname, char *const axisunit)
Cette routine permet de lire les informations relatives à un maillage en précisant son nom.
Definition: MEDmeshInfoByName.c:42
med_axis_type
med_axis_type
Definition: med.h:258
MED_NO_IT
#define MED_NO_IT
Definition: med.h:321
MED_COORDINATE
Definition: med.h:149
MEDmeshNodeCoordinateWithProfileRd
MEDC_EXPORT med_err MEDmeshNodeCoordinateWithProfileRd(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_storage_mode storagemode, const char *const profilename, const med_switch_mode switchmode, const med_int dimselect, med_float *const coordinates)
Cette routine permet de lire dans un maillage le tableau des coordonnées des noeuds,...
Definition: MEDmeshNodeCoordinateWithProfileRd.c:40
med_utils.h
MED_QUAD4
#define MED_QUAD4
Definition: med.h:204