| 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 3 : read an unstructured mesh : generic approach |
| 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 |
# open MED file with READ ONLY access mode |
| 41 |
try : |
| 42 |
fid = med.MEDfileOpen("UsesCase_MEDmesh_1.med", med.MED_ACC_RDONLY) |
| 43 |
except RuntimeError as ex : |
| 44 |
print("ERROR : open file in READ ONLY ACCESS mode ...\n%s" % ex) |
| 45 |
sys.exit(-1) |
| 46 |
|
| 47 |
# read how many mesh in the file |
| 48 |
try : |
| 49 |
nmesh = med.MEDnMesh(fid) |
| 50 |
except RuntimeError as ex : |
| 51 |
print("ERROR : read how many mesh ...\n%s" % ex) |
| 52 |
doCleanup(fid, -1) |
| 53 |
|
| 54 |
for i in range(0, nmesh) : |
| 55 |
|
| 56 |
# read computation space dimension |
| 57 |
try : |
| 58 |
spacedim = med.MEDmeshnAxis(fid, i+1) |
| 59 |
except RuntimeError as ex : |
| 60 |
print("ERROR : read computation space dimension ...\n%s" % ex) |
| 61 |
doCleanup(fid, -1) |
| 62 |
|
| 63 |
# read mesh informations : meshname, mesh dimension, mesh type ... |
| 64 |
try : |
| 65 |
meshinfo = med.MEDmeshInfo(fid, i+1) |
| 66 |
except RuntimeError as ex : |
| 67 |
print("ERROR : mesh info ...\n%s" % ex) |
| 68 |
doCleanup(fid, -1) |
| 69 |
|
| 70 |
if (not meshinfo) or (len(meshinfo) != 11) : |
| 71 |
print("ERROR : no mesh info created from MEDmeshInfo") |
| 72 |
doCleanup(fid, -1) |
| 73 |
else : |
| 74 |
meshname,spacedim,meshdim,meshtype,description,dtunit,sortingtype,nstep,axistype,axisname,axisunit=meshinfo |
| 75 |
|
| 76 |
# read how many nodes in the mesh |
| 77 |
try : |
| 78 |
nnodes, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, |
| 79 |
med.MED_NO_IT, med.MED_NODE, med.MED_NO_GEOTYPE, |
| 80 |
med.MED_COORDINATE, med.MED_NO_CMODE) |
| 81 |
except RuntimeError as ex : |
| 82 |
print("ERROR : number of nodes ...\n%s" % ex) |
| 83 |
doCleanup(fid, -1) |
| 84 |
|
| 85 |
# read mesh nodes coordinates |
| 86 |
coordinates = med.MEDFLOAT([0.] * nnodes * spacedim) |
| 87 |
try : |
| 88 |
med.MEDmeshNodeCoordinateRd(fid, meshname, med.MED_NO_DT, med.MED_NO_IT, |
| 89 |
med.MED_FULL_INTERLACE, coordinates) |
| 90 |
except RuntimeError as ex : |
| 91 |
print("ERROR : nodes coordinates ...\n%s" % ex) |
| 92 |
doCleanup(fid, -1) |
| 93 |
|
| 94 |
# read number of geometrical types for cells |
| 95 |
try: |
| 96 |
ngeo, chgt2, trsf2 = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, |
| 97 |
med.MED_NO_IT, med.MED_CELL, med.MED_GEO_ALL, |
| 98 |
med.MED_CONNECTIVITY, med.MED_NODAL) |
| 99 |
except RuntimeError as ex : |
| 100 |
print("ERROR : number of geo types ...\n%s" % ex) |
| 101 |
doCleanup(fid, -1) |
| 102 |
|
| 103 |
for it in range(1, ngeo + 1) : |
| 104 |
|
| 105 |
# get geometry type |
| 106 |
try : |
| 107 |
geotypename,geotype = med.MEDmeshEntityInfo(fid, meshname, med.MED_NO_DT, |
| 108 |
med.MED_NO_IT, med.MED_CELL, it ) |
| 109 |
except RuntimeError as ex : |
| 110 |
print("ERROR : get geo type ...\n%s" % ex) |
| 111 |
doCleanup(fid, -1) |
| 112 |
|
| 113 |
# how many cells of type geotype ? |
| 114 |
try : |
| 115 |
nelt, chgt3, trsf3 = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, |
| 116 |
med.MED_NO_IT, med.MED_CELL, geotype, |
| 117 |
med.MED_CONNECTIVITY, med.MED_NODAL) |
| 118 |
except RuntimeError as ex : |
| 119 |
print("ERROR : number of cell of type geotype ...\n%s" % ex) |
| 120 |
doCleanup(fid, -1) |
| 121 |
|
| 122 |
# read cells connectivity in the mesh |
| 123 |
connectivity = med.MEDINT([0] * nelt * (geotype % 100)) |
| 124 |
try : |
| 125 |
med.MEDmeshElementConnectivityRd(fid, meshname, med.MED_NO_DT, |
| 126 |
med.MED_NO_IT, med.MED_CELL, geotype, |
| 127 |
med.MED_NODAL, med.MED_FULL_INTERLACE, connectivity) |
| 128 |
except RuntimeError as ex : |
| 129 |
print("ERROR : cell connectivity ...\n%s" % ex) |
| 130 |
doCleanup(fid, -1) |
| 131 |
|
| 132 |
# ... we know that the family number of nodes and elements is 0, a real code would check ... |
| 133 |
|
| 134 |
# close MED file |
| 135 |
doCleanup(fid, 0) |