/
writeFenicsXML.py
124 lines (104 loc) · 5.33 KB
/
writeFenicsXML.py
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
# ***************************************************************************
# * *
# * Copyright (c) 2017 Johannes Hartung <j.hartung@gmx.net> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
__title__ = "FreeCAD Fenics XML mesh writer"
__author__ = "Johannes Hartung"
__url__ = "http://www.freecadweb.org"
## @package exportFenicsXML
# \ingroup FEM
# \brief FreeCAD Fenics Mesh XML writer for FEM workbench
from FreeCAD import Console
from .importToolsFem import get_FemMeshObjectDimension
from .importToolsFem import get_FemMeshObjectElementTypes
from .importToolsFem import get_MaxDimElementFromList
from xml.etree import ElementTree as ET # parsing xml files and exporting
def write_fenics_mesh_xml(fem_mesh_obj, outputfile):
"""
For the export, we only have to use the highest dimensional entities and their
vertices to be exported.
For second order elements, we have to delete the mid element nodes.
"""
# TODO: check for second order elements
# (reduce element order would be ok - all elements have at least the first
# necessary nodes)
FreeCAD_to_Fenics_dict = {
"Triangle": "triangle",
"Tetra": "tetrahedron",
"Hexa": "hexahedron",
"Edge": "interval",
"Node": "point",
"Quadrangle": "quadrilateral",
"Polygon": "unknown", "Polyhedron": "unknown",
"Prism": "unknown", "Pyramid": "unknown",
}
XML_Number_of_Nodes_dict = {
"point": 1,
"interval": 2,
"triangle": 3,
"quadrilateral": 4,
"tetrahedron": 4,
"hexahedron": 8
}
Console.PrintMessage(u"Converting " + fem_mesh_obj.Label + u" to fenics XML File\n")
Console.PrintMessage(u"Dimension of mesh: %d\n" % (get_FemMeshObjectDimension(fem_mesh_obj),))
elements_in_mesh = get_FemMeshObjectElementTypes(fem_mesh_obj)
Console.PrintMessage(u"Elements appearing in mesh: %s" % (str(elements_in_mesh),))
celltype_in_mesh = get_MaxDimElementFromList(elements_in_mesh)
(num_cells, cellname_fc, dim_cell) = celltype_in_mesh
cellname_fenics = FreeCAD_to_Fenics_dict[cellname_fc]
num_verts_cell = XML_Number_of_Nodes_dict[cellname_fenics]
Console.PrintMessage(
u"Celltype in mesh -> %s and its Fenics name: %s\n"
% (str(celltype_in_mesh), cellname_fenics)
)
root = ET.Element("dolfin", dolfin="http://fenicsproject.org")
meshchild = ET.SubElement(root, "mesh", celltype=cellname_fenics, dim=str(dim_cell))
vertices = ET.SubElement(meshchild, "vertices", size=str(fem_mesh_obj.FemMesh.NodeCount))
for (nodeind, fc_vec) in list(fem_mesh_obj.FemMesh.Nodes.items()):
ET.SubElement(
vertices, "vertex", index=str(nodeind - 1),
# FC starts from 1, fenics starts from 0 to size-1
x=str(fc_vec[0]), y=str(fc_vec[1]), z=str(fc_vec[2]))
cells = ET.SubElement(meshchild, "cells", size=str(num_cells))
if dim_cell == 3:
fc_cells = fem_mesh_obj.FemMesh.Volumes
elif dim_cell == 2:
fc_cells = fem_mesh_obj.FemMesh.Faces
elif dim_cell == 1:
fc_cells = fem_mesh_obj.FemMesh.Edges
else:
fc_cells = ()
for (fen_ind, fc_volume_ind) in enumerate(fc_cells):
# FC starts after all other entities, fenics start from 0 to size-1
nodeindices = fem_mesh_obj.FemMesh.getElementNodes(fc_volume_ind)
cell_args = {}
for (vi, ni) in enumerate(nodeindices):
if vi < num_verts_cell: # XML only supports first order meshs
cell_args["v" + str(vi)] = str(ni - 1)
# generate as many v entries in dict as nodes are listed in cell
# works only for first order elements
ET.SubElement(cells, cellname_fenics, index=str(fen_ind), **cell_args)
ET.SubElement(meshchild, "data")
fp = open(outputfile, "wb")
fp.write(ET.tostring(root))
# xml core functionality does not support pretty printing
# so the output file looks quite ugly
fp.close()