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