FEM: code formating, flake8 in import Fenics mesh

This commit is contained in:
Bernd Hahnebach 2017-02-28 11:51:40 +01:00 committed by wmayer
parent 10dd719631
commit e98d6321bb

View File

@ -31,7 +31,8 @@ __url__ = "http://www.freecadweb.org"
import FreeCAD import FreeCAD
import os import os
from lxml import etree # parsing xml files and exporting from lxml import etree # parsing xml files and exporting
# Template copied from importZ88Mesh.py. Thanks Bernd! # Template copied from importZ88Mesh.py. Thanks Bernd!
########## generic FreeCAD import and export methods ########## ########## generic FreeCAD import and export methods ##########
@ -72,10 +73,9 @@ def export(objectslist, filename):
write_fenics_mesh(obj, filename) write_fenics_mesh(obj, filename)
########## module specific methods ########## ########## module specific methods ##########
# Helper # Helper
def get_FemMeshObjectDimension(fem_mesh_obj): def get_FemMeshObjectDimension(fem_mesh_obj):
""" Count all entities in an abstract sense, to distinguish which dimension the mesh is """ Count all entities in an abstract sense, to distinguish which dimension the mesh is
(i.e. linemesh, facemesh, volumemesh) (i.e. linemesh, facemesh, volumemesh)
@ -93,12 +93,14 @@ def get_FemMeshObjectDimension(fem_mesh_obj):
return dim return dim
def get_FemMeshObjectElementTypes(fem_mesh_obj, remove_zero_element_entries=True): def get_FemMeshObjectElementTypes(fem_mesh_obj, remove_zero_element_entries=True):
""" """
Spit out all elements in the mesh with their appropriate dimension. Spit out all elements in the mesh with their appropriate dimension.
""" """
FreeCAD_element_names = ["Node", "Edge", "Hexa", "Polygon", "Polyhedron", FreeCAD_element_names = [
"Prism", "Pyramid", "Quadrangle", "Tetra", "Triangle"] "Node", "Edge", "Hexa", "Polygon", "Polyhedron",
"Prism", "Pyramid", "Quadrangle", "Tetra", "Triangle"]
FreeCAD_element_dims = [0, 1, 3, 2, 3, 3, 3, 2, 3, 2] FreeCAD_element_dims = [0, 1, 3, 2, 3, 3, 3, 2, 3, 2]
elements_list_with_zero = [(eval("fem_mesh_obj.FemMesh." + s + "Count"), s, d) for (s, d) in zip(FreeCAD_element_names, FreeCAD_element_dims)] elements_list_with_zero = [(eval("fem_mesh_obj.FemMesh." + s + "Count"), s, d) for (s, d) in zip(FreeCAD_element_names, FreeCAD_element_dims)]
@ -108,9 +110,9 @@ def get_FemMeshObjectElementTypes(fem_mesh_obj, remove_zero_element_entries=True
else: else:
elements_list = elements_list_with_zero elements_list = elements_list_with_zero
return elements_list return elements_list
def get_MaxDimElementFromList(elem_list): def get_MaxDimElementFromList(elem_list):
""" """
Gets element with the maximal dimension in the mesh to determine cells. Gets element with the maximal dimension in the mesh to determine cells.
@ -118,6 +120,7 @@ def get_MaxDimElementFromList(elem_list):
elem_list.sort(key=lambda (num, s, d): d) elem_list.sort(key=lambda (num, s, d): d)
return elem_list[-1] return elem_list[-1]
def write_fenics_mesh(fem_mesh_obj, outputfile): def write_fenics_mesh(fem_mesh_obj, outputfile):
""" """
For the export, we only have to use the highest dimensional entities and their For the export, we only have to use the highest dimensional entities and their
@ -126,19 +129,17 @@ def write_fenics_mesh(fem_mesh_obj, outputfile):
# TODO: check for second order elements # TODO: check for second order elements
# TODO: export mesh functions (to be defined, cell functions, vertex functions, facet functions) # TODO: export mesh functions (to be defined, cell functions, vertex functions, facet functions)
FreeCAD_to_Fenics_dict = { FreeCAD_to_Fenics_dict = {
"Triangle": "triangle", "Triangle": "triangle",
"Tetra": "tetrahedron", "Tetra": "tetrahedron",
"Hexa": "hexahedron", "Hexa": "hexahedron",
"Edge": "interval", "Edge": "interval",
"Node": "point", "Node": "point",
"Quadrangle": "quadrilateral", "Quadrangle": "quadrilateral",
"Polygon": "unknown", "Polyhedron": "unknown",
"Prism": "unknown", "Pyramid": "unknown",
}
"Polygon": "unknown", "Polyhedron": "unknown",
"Prism": "unknown", "Pyramid": "unknown",
}
print("Converting " + fem_mesh_obj.Label + " to fenics XML File") print("Converting " + fem_mesh_obj.Label + " to fenics XML File")
print("Dimension of mesh: %d" % (get_FemMeshObjectDimension(fem_mesh_obj),)) print("Dimension of mesh: %d" % (get_FemMeshObjectDimension(fem_mesh_obj),))
@ -148,16 +149,17 @@ def write_fenics_mesh(fem_mesh_obj, outputfile):
celltype_in_mesh = get_MaxDimElementFromList(elements_in_mesh) celltype_in_mesh = get_MaxDimElementFromList(elements_in_mesh)
(num_cells, cellname_fc, dim_cell) = celltype_in_mesh (num_cells, cellname_fc, dim_cell) = celltype_in_mesh
cellname_fenics = FreeCAD_to_Fenics_dict[cellname_fc] cellname_fenics = FreeCAD_to_Fenics_dict[cellname_fc]
print("Celltype in mesh -> %s and its Fenics name: %s" % (str(celltype_in_mesh),cellname_fenics)) print("Celltype in mesh -> %s and its Fenics name: %s" % (str(celltype_in_mesh), cellname_fenics))
root = etree.Element("dolfin", dolfin="http://fenicsproject.org") root = etree.Element("dolfin", dolfin="http://fenicsproject.org")
meshchild = etree.SubElement(root, "mesh", celltype=cellname_fenics, dim=str(dim_cell)) meshchild = etree.SubElement(root, "mesh", celltype=cellname_fenics, dim=str(dim_cell))
vertices = etree.SubElement(meshchild, "vertices", size=str(fem_mesh_obj.FemMesh.NodeCount)) vertices = etree.SubElement(meshchild, "vertices", size=str(fem_mesh_obj.FemMesh.NodeCount))
for (nodeind, fc_vec) in fem_mesh_obj.FemMesh.Nodes.iteritems(): # python2 for (nodeind, fc_vec) in fem_mesh_obj.FemMesh.Nodes.iteritems(): # python2
etree.SubElement(vertices, "vertex", index=str(nodeind-1), etree.SubElement(
# FC starts from 1, fenics starts from 0 to size-1 vertices, "vertex", index=str(nodeind - 1),
x=str(fc_vec[0]), y=str(fc_vec[1]), z=str(fc_vec[2])) # 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 = etree.SubElement(meshchild, "cells", size=str(num_cells)) cells = etree.SubElement(meshchild, "cells", size=str(num_cells))
if dim_cell == 3: if dim_cell == 3: