| 1 |
#! /usr/bin/env python |
| 2 |
# -*- coding:utf-8 -*- |
| 3 |
# /* This file is part of MED. |
| 4 |
# * |
| 5 |
# * COPYRIGHT (C) 1999 - 2020 EDF R&D, CEA/DEN |
| 6 |
# * MED is free software: you can redistribute it and/or modify |
| 7 |
# * it under the terms of the GNU Lesser General Public License as published by |
| 8 |
# * the Free Software Foundation, either version 3 of the License, or |
| 9 |
# * (at your option) any later version. |
| 10 |
# * |
| 11 |
# * MED is distributed in the hope that it will be useful, |
| 12 |
# * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# * GNU Lesser General Public License for more details. |
| 15 |
# * |
| 16 |
# * You should have received a copy of the GNU Lesser General Public License |
| 17 |
# * along with MED. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
# */ |
| 19 |
|
| 20 |
# /****************************************************************************** |
| 21 |
# * |
| 22 |
# * Use case 14 : read a 2D unstructured mesh with 2 polygons |
| 23 |
# * |
| 24 |
# *****************************************************************************/ |
| 25 |
|
| 26 |
import sys |
| 27 |
|
| 28 |
import med |
| 29 |
|
| 30 |
def doCleanup(fid, status) : |
| 31 |
try : |
| 32 |
med.MEDfileClose(fid) |
| 33 |
except RuntimeError as ex : |
| 34 |
print("ERROR : close file ...\n%s" % ex) |
| 35 |
status = -1 |
| 36 |
finally : |
| 37 |
if status != 0 : |
| 38 |
sys.exit(status) |
| 39 |
|
| 40 |
# max size of meshname : med.MED_NAME_SIZE+1 |
| 41 |
meshname = "2D unstructured mesh" |
| 42 |
|
| 43 |
# open MED file with READ ONLY access mode |
| 44 |
try : |
| 45 |
fid = med.MEDfileOpen("UsesCase_MEDmesh_13.med", med.MED_ACC_RDONLY) |
| 46 |
except RuntimeError as ex : |
| 47 |
print("ERROR : open file in READ ONLY ACCESS mode ...\n%s" % ex) |
| 48 |
sys.exit(-1) |
| 49 |
|
| 50 |
# ... we know that this MED file has only one mesh, |
| 51 |
# a real code would check ... |
| 52 |
|
| 53 |
# read mesh informations : mesh dimension, space dimension ... |
| 54 |
try : |
| 55 |
meshinfo = med.MEDmeshInfoByName(fid, meshname) |
| 56 |
except RuntimeError as ex : |
| 57 |
print("ERROR : mesh info ...\n%s" % ex) |
| 58 |
doCleanup(fid, -1) |
| 59 |
|
| 60 |
if (not meshinfo) or (len(meshinfo) != 10) : |
| 61 |
print("ERROR : no mesh info created from MEDmeshInfoByName") |
| 62 |
doCleanup(fid, -1) |
| 63 |
else : |
| 64 |
spacedim,meshdim,meshtype,description,dtunit,sortingtype,nstep,axistype,axisname,axisunit=meshinfo |
| 65 |
|
| 66 |
# read how many nodes in the mesh |
| 67 |
try : |
| 68 |
nnodes, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, |
| 69 |
med.MED_NO_IT, med.MED_NODE, med.MED_POINT1, |
| 70 |
med.MED_COORDINATE, med.MED_NO_CMODE) |
| 71 |
except RuntimeError as ex : |
| 72 |
print("ERROR : number of nodes ...\n%s" % ex) |
| 73 |
doCleanup(fid, -1) |
| 74 |
|
| 75 |
# ... we know that we only have MED_POLYGON cells in the mesh, |
| 76 |
# a real code would check all MED geometry cell types ... |
| 77 |
|
| 78 |
# How many polygon in the mesh in nodal connectivity mode |
| 79 |
# For the polygons, we get the size of array index |
| 80 |
try : |
| 81 |
indexsize, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, |
| 82 |
med.MED_NO_IT, med.MED_CELL, med.MED_POLYGON, |
| 83 |
med.MED_INDEX_NODE, med.MED_NODAL) |
| 84 |
except RuntimeError as ex : |
| 85 |
print("ERROR : read number of polygon ...\n%s" % ex) |
| 86 |
doCleanup(fid, -1) |
| 87 |
npoly = indexsize - 1 |
| 88 |
|
| 89 |
# how many nodes for the polygon connectivity ? |
| 90 |
try : |
| 91 |
connectivitysize, chgt, trsf = med.MEDmeshnEntity(fid, meshname, |
| 92 |
med.MED_NO_DT, med.MED_NO_IT, med.MED_CELL, |
| 93 |
med.MED_POLYGON, med.MED_CONNECTIVITY, med.MED_NODAL) |
| 94 |
except RuntimeError as ex : |
| 95 |
print("ERROR : read connectivity size ...\n%s" % ex) |
| 96 |
doCleanup(fid, -1) |
| 97 |
|
| 98 |
# read mesh nodes coordinates |
| 99 |
coordinates = med.MEDFLOAT([0.] * nnodes * spacedim) |
| 100 |
try : |
| 101 |
med.MEDmeshNodeCoordinateRd(fid, meshname, med.MED_NO_DT, med.MED_NO_IT, |
| 102 |
med.MED_FULL_INTERLACE, coordinates) |
| 103 |
except RuntimeError as ex : |
| 104 |
print("ERROR : nodes coordinates ...\n%s" % ex) |
| 105 |
doCleanup(fid, -1) |
| 106 |
|
| 107 |
# read polygons connectivity |
| 108 |
index = med.MEDINT([0] * indexsize) |
| 109 |
connectivity = med.MEDINT([0] * connectivitysize) |
| 110 |
|
| 111 |
try : |
| 112 |
med.MEDmeshPolygonRd(fid, meshname, med.MED_NO_DT, med.MED_NO_IT, |
| 113 |
med.MED_CELL, med.MED_NODAL, index, connectivity) |
| 114 |
except RuntimeError as ex : |
| 115 |
print("ERROR : read polygon connectivity ...\n%s" % ex) |
| 116 |
doCleanup(fid, -1) |
| 117 |
|
| 118 |
# ... we know that the family number of nodes and elements is 0, a real code would check ... |
| 119 |
|
| 120 |
# close MED file |
| 121 |
doCleanup(fid, 0) |