| 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 |
# * How to create an unstructured mesh with polyhedrons |
| 22 |
# * |
| 23 |
# * Use case 16 : read a 3D unstructured mesh with 2 polyhedrons |
| 24 |
# * |
| 25 |
# *****************************************************************************/ |
| 26 |
|
| 27 |
import sys |
| 28 |
|
| 29 |
import med |
| 30 |
|
| 31 |
def doCleanup(fid, status) : |
| 32 |
try : |
| 33 |
med.MEDfileClose(fid) |
| 34 |
except RuntimeError as ex : |
| 35 |
print("ERROR : close file ...\n%s" % ex) |
| 36 |
status = -1 |
| 37 |
finally : |
| 38 |
if status != 0 : |
| 39 |
sys.exit(status) |
| 40 |
|
| 41 |
# max size of meshname : med.MED_NAME_SIZE+1 |
| 42 |
meshname = "3D Unstructured Mesh With 2 polyhedrons" |
| 43 |
|
| 44 |
# open MED file with READ ONLY access mode |
| 45 |
try : |
| 46 |
fid = med.MEDfileOpen("UsesCase_MEDmesh_15.med", med.MED_ACC_RDONLY) |
| 47 |
except RuntimeError as ex : |
| 48 |
print("ERROR : open file in READ ONLY ACCESS mode ...\n%s" % ex) |
| 49 |
sys.exit(-1) |
| 50 |
|
| 51 |
# ... we know that this MED file has only one mesh, |
| 52 |
# a real code would check ... |
| 53 |
|
| 54 |
# read mesh informations : mesh dimension, space dimension ... |
| 55 |
try : |
| 56 |
meshinfo = med.MEDmeshInfoByName(fid, meshname) |
| 57 |
except RuntimeError as ex : |
| 58 |
print("ERROR : mesh info ...\n%s" % ex) |
| 59 |
doCleanup(fid, -1) |
| 60 |
|
| 61 |
if (not meshinfo) or (len(meshinfo) != 10) : |
| 62 |
print("ERROR : no mesh info created from MEDmeshInfoByName") |
| 63 |
doCleanup(fid, -1) |
| 64 |
else : |
| 65 |
spacedim,meshdim,meshtype,description,dtunit,sortingtype,nstep,axistype,axisname,axisunit=meshinfo |
| 66 |
|
| 67 |
# read how many nodes in the mesh |
| 68 |
try : |
| 69 |
nnodes, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, |
| 70 |
med.MED_NO_IT, med.MED_NODE, med.MED_POINT1, |
| 71 |
med.MED_COORDINATE, med.MED_NO_CMODE) |
| 72 |
except RuntimeError as ex : |
| 73 |
print("ERROR : number of nodes ...\n%s" % ex) |
| 74 |
doCleanup(fid, -1) |
| 75 |
|
| 76 |
# ... we know that we only have MED_POLYHEDRON cells in the mesh, |
| 77 |
# a real code would check all MED geometry cell types ... |
| 78 |
|
| 79 |
# We read how many polyhedrons are in the mesh (using nodal connectivity mode) |
| 80 |
# We get the size of the polyhedrons/face index array. |
| 81 |
# As an index of the face index array give the location of the first face and so the |
| 82 |
# number of polyhedrons |
| 83 |
try : |
| 84 |
faceindexsize, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, |
| 85 |
med.MED_NO_IT, med.MED_CELL, med.MED_POLYHEDRON, |
| 86 |
med.MED_INDEX_FACE, med.MED_NODAL) |
| 87 |
except RuntimeError as ex : |
| 88 |
print("ERROR : read number of polyhedrons faceindex ...\n%s" % ex) |
| 89 |
doCleanup(fid, -1) |
| 90 |
npoly = faceindexsize - 1 |
| 91 |
|
| 92 |
try : |
| 93 |
nodeindexsize, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,med.MED_CELL, |
| 94 |
med.MED_POLYHEDRON, med.MED_INDEX_NODE, med.MED_NODAL) |
| 95 |
except RuntimeError as ex : |
| 96 |
print("ERROR : read number of polyhedrons nodeindex ...\n%s" % ex) |
| 97 |
doCleanup(fid, -1) |
| 98 |
|
| 99 |
# how many nodes for the polyhedron connectivity ? |
| 100 |
try : |
| 101 |
connectivitysize, chgt, trsf = med.MEDmeshnEntity(fid, meshname, |
| 102 |
med.MED_NO_DT, med.MED_NO_IT, med.MED_CELL, |
| 103 |
med.MED_POLYHEDRON, med.MED_CONNECTIVITY, med.MED_NODAL) |
| 104 |
except RuntimeError as ex : |
| 105 |
print("ERROR : read connectivity size ...\n%s" % ex) |
| 106 |
doCleanup(fid, -1) |
| 107 |
|
| 108 |
# read mesh nodes coordinates |
| 109 |
coordinates = med.MEDFLOAT([0.] * nnodes * spacedim) |
| 110 |
try : |
| 111 |
med.MEDmeshNodeCoordinateRd(fid, meshname, med.MED_NO_DT, med.MED_NO_IT, |
| 112 |
med.MED_FULL_INTERLACE, coordinates) |
| 113 |
except RuntimeError as ex : |
| 114 |
print("ERROR : nodes coordinates ...\n%s" % ex) |
| 115 |
doCleanup(fid, -1) |
| 116 |
|
| 117 |
for i in range(0, nnodes * spacedim) : |
| 118 |
sys.stdout.write(str(coordinates[i])) |
| 119 |
if (i + 1 != nnodes * spacedim) : |
| 120 |
sys.stdout.write(" - ") |
| 121 |
else : |
| 122 |
print("") |
| 123 |
|
| 124 |
# read polyhedron connectivity |
| 125 |
faceindex = med.MEDINT([0] * faceindexsize) |
| 126 |
nodeindex = med.MEDINT([0] * nodeindexsize) |
| 127 |
connectivity = med.MEDINT([0] * connectivitysize) |
| 128 |
|
| 129 |
try : |
| 130 |
med.MEDmeshPolyhedronRd(fid, meshname, med.MED_NO_DT, med.MED_NO_IT, |
| 131 |
med.MED_CELL, med.MED_NODAL, faceindex, |
| 132 |
nodeindex, connectivity) |
| 133 |
except RuntimeError as ex : |
| 134 |
print("ERROR : read polyhedron connectivity ...\n%s" % ex) |
| 135 |
doCleanup(fid, -1) |
| 136 |
|
| 137 |
for i in range(0, npoly) : |
| 138 |
print(">> MED_POLYHEDRON %s :" % str(i+1)) |
| 139 |
print("---- Face Index ----- : [") |
| 140 |
ind1 = faceindex[i]-1 |
| 141 |
ind2 = faceindex[i+1]-1 |
| 142 |
for k in range(ind1, ind2) : |
| 143 |
sys.stdout.write(str(nodeindex[k]) + " ") |
| 144 |
print(" ] ") |
| 145 |
print("---- Connectivity ----- : [") |
| 146 |
for k in range(ind1, ind2) : |
| 147 |
jind1 = nodeindex[k]-1 |
| 148 |
jind2 = nodeindex[k+1]-1 |
| 149 |
for j in range(jind1, jind2) : |
| 150 |
sys.stdout.write(str(connectivity[j]) + " ") |
| 151 |
print("") |
| 152 |
print(" ]") |
| 153 |
|
| 154 |
# ... we know that the family number of nodes and elements is 0, a real code would check ... |
| 155 |
|
| 156 |
# close MED file |
| 157 |
doCleanup(fid, 0) |