| 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 7 : read a 2D unstructured mesh with nodes coordinates modifications |
| 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_6.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_NO_GEOTYPE, |
| 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_TRIA3 and MED_QUAD4 in the mesh, |
| 76 |
# a real code would check all MED geometry cell types ... |
| 77 |
|
| 78 |
# read how many triangular cells in the mesh |
| 79 |
try : |
| 80 |
ntria3, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, |
| 81 |
med.MED_NO_IT, med.MED_CELL, med.MED_TRIA3, |
| 82 |
med.MED_CONNECTIVITY, med.MED_NODAL) |
| 83 |
except RuntimeError as ex : |
| 84 |
print("ERROR : number of MED_TRIA3 ...\n%s" % ex) |
| 85 |
doCleanup(fid, -1) |
| 86 |
|
| 87 |
# read how many quadrangular cells in the mesh |
| 88 |
try : |
| 89 |
nquad4, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT, |
| 90 |
med.MED_NO_IT, med.MED_CELL, med.MED_QUAD4, |
| 91 |
med.MED_CONNECTIVITY, med.MED_NODAL) |
| 92 |
except RuntimeError as ex : |
| 93 |
print("ERROR : number of MED_QUAD4 ...\n%s" % ex) |
| 94 |
doCleanup(fid, -1) |
| 95 |
|
| 96 |
# read mesh nodes coordinates in the initial mesh |
| 97 |
coordinates = med.MEDFLOAT([0.] * nnodes * spacedim) |
| 98 |
try : |
| 99 |
med.MEDmeshNodeCoordinateRd(fid, meshname, med.MED_NO_DT, med.MED_NO_IT, |
| 100 |
med.MED_FULL_INTERLACE, coordinates) |
| 101 |
except RuntimeError as ex : |
| 102 |
print("ERROR : initial nodes coordinates ...\n%s" % ex) |
| 103 |
doCleanup(fid, -1) |
| 104 |
|
| 105 |
# read cells connectivity in the mesh in the initial mesh |
| 106 |
triaconnectivity = med.MEDINT([0] * ntria3 * 3) |
| 107 |
try : |
| 108 |
med.MEDmeshElementConnectivityRd(fid, meshname, med.MED_NO_DT, |
| 109 |
med.MED_NO_IT, med.MED_CELL, med.MED_TRIA3, |
| 110 |
med.MED_NODAL, med.MED_FULL_INTERLACE, triaconnectivity) |
| 111 |
except RuntimeError as ex : |
| 112 |
print("ERROR : MED_TRIA3 connectivity ...\n%s" % ex) |
| 113 |
doCleanup(fid, -1) |
| 114 |
|
| 115 |
quadconnectivity = med.MEDINT([0] * nquad4 * 4) |
| 116 |
try : |
| 117 |
med.MEDmeshElementConnectivityRd(fid, meshname, med.MED_NO_DT, |
| 118 |
med.MED_NO_IT, med.MED_CELL, med.MED_QUAD4, |
| 119 |
med.MED_NODAL, med.MED_FULL_INTERLACE, quadconnectivity) |
| 120 |
except RuntimeError as ex : |
| 121 |
print("ERROR : MED_QUAD4 connectivity ...\n%s" % ex) |
| 122 |
doCleanup(fid, -1) |
| 123 |
|
| 124 |
# ... we know that the family number of nodes and elements is 0, a real code would check ... |
| 125 |
|
| 126 |
# read nodes coordinates changements step by step |
| 127 |
|
| 128 |
for it in range(1, nstep) : |
| 129 |
|
| 130 |
try : |
| 131 |
numdt, numit, dt = med.MEDmeshComputationStepInfo(fid, meshname, it + 1) |
| 132 |
except RuntimeError as ex : |
| 133 |
print("ERROR : Computing step info ...\n%s" % ex) |
| 134 |
doCleanup(fid, -1) |
| 135 |
|
| 136 |
# test changement : for nodes coordinates |
| 137 |
try : |
| 138 |
entityinfo = med.MEDmeshnEntityWithProfile(fid, meshname, numdt, |
| 139 |
numit, med.MED_NODE, med.MED_NO_GEOTYPE, |
| 140 |
med.MED_COORDINATE, med.MED_NO_CMODE, med.MED_GLOBAL_STMODE) |
| 141 |
except RuntimeError as ex : |
| 142 |
print("ERROR : nodes coordinates test changement ...\n%s" % ex) |
| 143 |
doCleanup(fid, -1) |
| 144 |
|
| 145 |
if (not entityinfo) or (len(entityinfo) != 5) : |
| 146 |
print("ERROR : nodes coordinates test changement return ...") |
| 147 |
doCleanup(fid, -1) |
| 148 |
else : |
| 149 |
nnodes,profilename,profilesize,chgt,trsf = entityinfo |
| 150 |
|
| 151 |
# if coordinates have changed, then read the new coordinates |
| 152 |
if (chgt) : |
| 153 |
try : |
| 154 |
med.MEDmeshNodeCoordinateWithProfileRd(fid, meshname, numdt, numit, |
| 155 |
med.MED_GLOBAL_STMODE, profilename, med.MED_FULL_INTERLACE, |
| 156 |
med.MED_ALL_CONSTITUENT, coordinates) |
| 157 |
except RuntimeError as ex : |
| 158 |
print("ERROR : nodes coordinates changement ...\n%s" % ex) |
| 159 |
doCleanup(fid, -1) |
| 160 |
|
| 161 |
# close MED file |
| 162 |
doCleanup(fid, 0) |