/usr/lib/python2.7/dist-packages/dolfin_utils/meshconvert/abaqus.py is in python-dolfin 1.3.0+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | """Module for converting the Abaqus mesh format."""
# Copyright (C) 2012 Arve Knudsen and Garth N/ Wells
#
# This file is part of DOLFIN.
#
# DOLFIN is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DOLFIN is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#
# Modified by Simon Funke (surface export)
import re
import csv
import xml_writer
import numpy as np
class State:
Init, Unknown, Invalid, ReadHeading, ReadNodes, ReadCells, \
ReadNodeSet, ReadCellSet, ReadSurfaceSet = range(9)
def convert(ifilename, handler):
""" Convert from Abaqus.
The Abaqus format first defines a node block, then there should be a number
of elements containing these nodes.
"""
# Dictionary of nodes (maps node id to coordinates)
nodes = {}
# Dictionary of elements (maps cell id to list of cell nodes)
elems = {}
# Lists of nodes for given name (key)
node_sets = {}
# Lists of cells for given name (key)
cell_sets = {}
# Lists of surfaces for given name (key) in the format:
# {'SS1': [set(['SS1_S1', 'S1']), set(['SS1_S4', 'S4'])]},
# where SS1 is the name of the surface, SS1_S1 is the name of the
# cell list whose first face is to be selected, ...
surface_sets = {}
# Open file Abaqus file
csv_file = csv.reader(open(ifilename, 'rb'), delimiter=',', skipinitialspace=True)
node_set_name = None
generate = None
# Set intial state state
state = State.Init
# Read data from input file
for l in csv_file:
# Sanity check
if (len(l) == 0): print "Ooops, zero length."
if l[0].startswith('**'): # Pass over comments
continue
elif l[0].startswith('*'): # Have a keyword
state = State.Unknown
if l[0].lower() == "*heading":
state = State.ReadHeading
elif l[0].lower() == "*part":
part_name = _read_part_name(l)
elif l[0].lower() == "*end part":
state = State.Invalid
elif l[0].lower() == "*node":
node_set_name = _create_node_list_entry(node_sets, l)
state = State.ReadNodes
elif l[0].lower() == "*element":
cell_type, cell_set_name = _read_element_keywords(cell_sets, l)
state = State.ReadCells
elif l[0].lower() == "*nset":
node_set_name, generate = _read_nset_keywords(node_sets, l)
state = State.ReadNodeSet
elif l[0].lower() == "*elset":
cell_set_name, generate = _read_elset_keywords(cell_sets, l)
if generate:
print "WARNING: generation of *elsets not tested."
state = State.ReadCellSet
elif l[0].lower() == "*surface":
surface_set_name, generate = _read_surface_keywords(surface_sets, l)
state = State.ReadSurfaceSet
else:
print "WARNING: unrecognised Abaqus input keyword:", l[0]
state = State.Unknown
else:
if state == State.ReadHeading:
model_name = _read_heading(l)
elif state == State.ReadNodes:
node_id = int(l[0]) - 1
coords = [float(c) for c in l[1:]]
nodes[node_id] = coords
if node_set_name is not None:
node_sets[node_set_name].add(node_id)
elif state == State.ReadCells:
cell_id = int(l[0]) - 1
cell_connectivity = [int(v) - 1 for v in l[1:]]
elems[cell_id] = cell_connectivity
if cell_set_name is not None:
cell_sets[cell_set_name].add(cell_id)
elif state == State.ReadNodeSet:
try:
if generate:
n0, n1, increment = l
node_range = range(int(n0) - 1, int(n1) - 1, int(increment))
node_range.append(int(n1) - 1)
node_sets[node_set_name].update(node_range)
else:
# Strip empty term at end of list, if present
if l[-1] == '': l.pop(-1)
node_range = [int(n) - 1 for n in l]
node_sets[node_set_name].update(node_range)
except:
print "WARNING: Non-integer node sets not yet supported."
elif state == State.ReadCellSet:
try:
if generate:
n0, n1, increment = l
cell_range = range(int(n0) - 1, int(n1) - 1, int(increment))
cell_range.append(int(n1) - 1)
cell_sets[cell_set_name].update(cell_range)
else:
# Strip empty term at end of list, if present
if l[-1] == '': l.pop(-1)
cell_range = [int(n) - 1 for n in l]
cell_sets[cell_set_name].update(cell_range)
except:
print "WARNING: Non-integer element sets not yet supported."
elif state == State.ReadSurfaceSet:
# Strip empty term at end of list, if present
if l[-1] == '': l.pop(-1)
surface_sets[surface_set_name].update([tuple(l)])
elif state == State.Invalid: # part
raise StandardError("Inavlid Abaqus parser state..")
# Close CSV object
del csv_file
# Write data to XML file
# Note that vertices/cells must be consecutively numbered, which
# isn't necessarily the case in Abaqus. Therefore we enumerate and
# translate original IDs to sequence indexes if gaps are present.
# FIXME
handler.set_mesh_type("tetrahedron", 3)
process_facets = len(surface_sets) > 0
if process_facets:
try:
from dolfin import MeshEditor, Mesh
except ImportError:
_error("DOLFIN must be installed to handle Abaqus boundary regions")
mesh = Mesh()
mesh_editor = MeshEditor()
mesh_editor.open(mesh, 3, 3)
node_ids_order = {}
# Check for gaps in vertex numbering
node_ids = nodes.keys()
if len(node_ids) > 0:
vertex_gap = (min(node_ids) != 0 or max(node_ids) != len(node_ids) - 1)
for x, y in enumerate(node_ids):
node_ids_order[y]= x # Maps Abaqus IDs to Dolfin IDs
else:
vertex_gap = True
# Check for gaps in cell numbering
elemids = elems.keys()
if len(elemids) > 0:
cell_gap = (min(elemids) != 0 or max(elemids) != len(elemids) - 1)
else:
cell_gap = True
# Write vertices to XML file
handler.start_vertices(len(nodes))
if process_facets:
mesh_editor.init_vertices (len(nodes))
if not vertex_gap:
for v_id, v_coords in nodes.items():
handler.add_vertex(v_id, v_coords)
if process_facets:
mesh_editor.add_vertex(v_id, np.array(v_coords, dtype=np.float_))
else:
for idx, (v_id, v_coords) in enumerate(nodes.items()):
handler.add_vertex(idx, v_coords)
if process_facets:
mesh_editor.add_vertex(idx, np.array(v_coords, dtype=np.float_))
handler.end_vertices()
# Write cells to XML file
handler.start_cells(len(elems))
if process_facets:
mesh_editor.init_cells(len(elems))
if not vertex_gap and not cell_gap:
for c_index, c_data in elems.items():
for v_id in c_data:
if not (0 <= v_id < len(nodes)):
handler.error("Element %s references non-existent node %s" % (c_index, v_id))
handler.add_cell(c_index, c_data)
if process_facets:
c_data_tmp = np.array(c_data)
c_data_tmp.sort()
mesh_editor.add_cell(c_index, np.array(c_data_tmp, dtype=np.uintp))
elif not vertex_gap and cell_gap:
for idx, (c_index, c_data) in enumerate(elems.items()):
for v_id in c_data:
if not (0 <= v_id < len(nodes)):
handler.error("Element %s references non-existent node %s" % (c_index, v_id))
handler.add_cell(idx, c_data)
if process_facets:
c_data_tmp = np.array(c_data)
c_data_tmp.sort()
mesh_editor.add_cell(idx, np.array(c_data_tmp, dtype=np.uintp))
else:
for idx, (c_id, c_data) in enumerate(elems.items()):
c_nodes = []
for v_id in c_data:
try: c_nodes.append(node_ids_order[v_id])
except ValueError:
handler.error("Element %s references non-existent node %s" % (c_id, v_id))
handler.add_cell(idx, c_nodes)
if process_facets:
c_nodes.sort()
mesh_editor.add_cell(idx, np.array(c_nodes, dtype=np.uintp))
handler.end_cells()
# Write MeshValueCollections to XML file
handler.start_domains()
# Build a abaqus node ID -> dolfin cell ID map (which is not unique but that is irrelevant here)
# and its local entity.
if len(node_sets.items()) > 0:
node_cell_map = {}
for c_dolfin_index, (c_index, c_data) in enumerate(elems.items()):
c_data_tmp = np.array(c_data)
c_data_tmp.sort()
for local_entity, n_index in enumerate(c_data_tmp):
node_cell_map[n_index] = (c_dolfin_index, local_entity)
# Write vertex/node sets
dim = 0
for value, (name, node_set) in enumerate(node_sets.items()):
handler.start_mesh_value_collection(name, dim, len(node_set), "uint")
for node in node_set:
try:
cell, local_entity = node_cell_map[node]
handler.add_entity_mesh_value_collection(dim, cell, value, local_entity=local_entity)
except KeyError:
print "Warning: Boundary references non-existent node %s" % node
handler.end_mesh_value_collection()
# Write cell/element sets
dim = 3
for name, s in cell_sets.items():
handler.start_mesh_value_collection(name, dim, len(s), "uint")
for cell in s:
handler.add_entity_mesh_value_collection(dim, cell, 0)
handler.end_mesh_value_collection()
# Write surface sets
if process_facets:
dim = 2
nodes_facet_map = _nodes_facet_map(mesh)
data = [int(0)] * mesh.num_facets()
S1 = [0, 1, 2]
S2 = [0, 3, 1]
S3 = [1, 3, 2]
S4 = [2, 3, 0]
node_selector = {'S1': S1,
'S2': S2,
'S3': S3,
'S4': S4,
}
for index, (name, s) in enumerate(surface_sets.items()):
cell_face_list = []
for cell_set_name, face_index in s:
cell_face_list += [(cell, face_index) for cell in cell_sets[cell_set_name]]
for cell, face in cell_face_list:
cell_nodes = elems[cell]
# Extract the face nodes
face_nodes = [cell_nodes[i] for i in node_selector[face]]
dolfin_face_nodes = [node_ids_order[n] for n in face_nodes]
dolfin_face_nodes.sort()
# Convert the face_nodes to dolfin IDs
face_id = nodes_facet_map[tuple(dolfin_face_nodes)]
data[face_id] = index + 1
# Create and initialise the mesh function
handler.start_meshfunction("facet_region", dim, mesh.num_facets() )
for index, physical_region in enumerate (data):
handler.add_entity_meshfunction(index, physical_region)
handler.end_meshfunction()
handler.end_domains()
def _nodes_facet_map(mesh):
# Now process the facet markers
dim = 2
mesh.init(dim, 0)
facets_as_nodes = mesh.topology()(dim, 0)().reshape(mesh.num_facets(), 3)
# Build the reverse map
nodes_as_facets = {}
for facet in range(mesh.num_facets()):
nodes_as_facets[tuple(facets_as_nodes[facet,:])] = facet
return nodes_as_facets
def _read_heading(l):
return l[0].strip()
def _read_part_name(l):
if (len(l) < 2): print "Ooops, length problem."
part_names = l[1].split('=')
if (len(part_names) < 2): print "Ooops, part names length problem."
return part_names[1].strip()
def _create_node_list_entry(node_sets, l):
# Check for node set name
node_set_name = None
if len(l) == 2:
set_data = l[1].split('=')
assert len(set_data) == 2, "wrong list length"
if set_data[0].lower() == "nset":
node_set_name = set_data[1]
if node_set_name not in node_sets:
node_sets[node_set_name] = set()
return node_set_name
def _read_element_keywords(cell_sets, l):
# Get element type and element set name
element_type = None
element_set_name = None
for key in l[1:]:
key_parts = key.split('=')
key_name = key_parts[0].lower().strip()
if key_name == "type":
element_type = key_parts[1].lower().strip()
elif key_name == "elset":
element_set_name = key_parts[1].strip()
# Test that element is supported
check_element_support(element_type)
# Add empty set to cell_sets dictionary
if element_set_name:
if element_set_name not in cell_sets:
cell_sets[element_set_name] = set()
return element_type, element_set_name
def _read_nset_keywords(node_sets, l):
node_set_name = None
generate = None
# Get set name and add to dict
set_data = l[1].split('=')
assert len(set_data) == 2, "wrong list length, set name missing"
assert set_data[0].lower() == "nset"
node_set_name = set_data[1]
if node_set_name not in node_sets:
node_sets[node_set_name] = set()
# Check for generate flag
if len(l) == 3:
if l[2].lower() == "generate":
generate = True
return node_set_name, generate
def _read_elset_keywords(sets, l):
set_name = None
generate = None
# Get set name and add to dict
set_data = l[1].split('=')
assert len(set_data) == 2, "wrong list length, set name missing"
assert set_data[0].lower() == "elset"
set_name = set_data[1]
if set_name not in sets: sets[set_name] = set()
# Check for generate flag
if len(l) == 3:
if l[2].lower() == "generate":
generate = True
return set_name, generate
def _read_surface_keywords(sets, l):
surface_name = None
generate = None
# Get surface name and add to dict
surface_data = l[1].split('=')
assert len(surface_data) == 2, "wrong list length, surface name missing"
assert surface_data[0].lower() == "name"
surface_name = surface_data[1]
if surface_name not in sets: sets[surface_name] = set()
generate = False
return surface_name, generate
def check_element_support(element_type):
supported_elements = ('c3d4',)
if element_type.lower() not in supported_elements:
raise StandardError("Element type not supported.")
|