From 785d660dd78fdc7dfd643a88b7f2bf3d54b3e7cc Mon Sep 17 00:00:00 2001 From: joha2 Date: Tue, 18 Jul 2017 23:46:12 +0000 Subject: [PATCH] FEM: Fenics xdmf: added writeout of gmsh groups as cell functions --- src/Mod/Fem/writeFenicsXDMF.py | 36 ++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Mod/Fem/writeFenicsXDMF.py b/src/Mod/Fem/writeFenicsXDMF.py index 0996f26ffc..ffa51c15d1 100644 --- a/src/Mod/Fem/writeFenicsXDMF.py +++ b/src/Mod/Fem/writeFenicsXDMF.py @@ -139,16 +139,16 @@ def write_fenics_mesh_volumes_xdmf(fem_mesh_obj, topologynode, rd, encoding=ENCO pass -def write_fenics_mesh_cellfunctions(fem_mesh_obj, mycellvalues, attributenode, encoding=ENCODING_ASCII): +def write_fenics_mesh_scalar_cellfunctions(name, cell_array, attributenode, encoding=ENCODING_ASCII): attributenode.set("AttributeType", "Scalar") attributenode.set("Center", "Cell") - attributenode.set("Name", "f") + attributenode.set("Name", name) - (num_cells, name_cell, dim_cell) = get_MaxDimElementFromList(get_FemMeshObjectElementTypes(fem_mesh_obj)) + (num_cells, num_dims) = np.shape(cell_array) if encoding == ENCODING_ASCII: - dataitem = ET.SubElement(attributenode, "DataItem", Dimensions="%d %d" % (num_cells, 1), Format="XML") - dataitem.text = numpy_array_to_str(np.random.random((num_cells, 1))) + dataitem = ET.SubElement(attributenode, "DataItem", Dimensions="%d %d" % (num_cells, num_dims), Format="XML") + dataitem.text = numpy_array_to_str(cell_array) elif encoding == ENCODING_HDF5: pass @@ -186,11 +186,35 @@ def write_fenics_mesh_xdmf(fem_mesh_obj, outputfile, encoding=ENCODING_ASCII): topology = ET.SubElement(grid, "Topology") geometry = ET.SubElement(grid, "Geometry") - # attribute = etree.SubElement(grid, "Attribute") # for cell functions recalc_dict = write_fenics_mesh_points_xdmf(fem_mesh_obj, geometry, encoding=encoding) write_fenics_mesh_volumes_xdmf(fem_mesh_obj, topology, recalc_dict, encoding=encoding) + fem_mesh = fem_mesh_obj.FemMesh + try: + gmshgroups = fem_mesh.Groups + except: + gmshgroups = () + + elem_dict = {} + for g in gmshgroups: + print('found mesh groups') + mesh_function_type = fem_mesh.getGroupElementType(g) + print('group id: %d with element type %s' % (g, mesh_function_type)) + if mesh_function_type == 'Volume': + + for e in fem_mesh.getGroupElements(g): + elem_dict[e] = g + + attribute = ET.SubElement(grid, "Attribute") # for cell functions + name = "cell_function" + + + val_array = np.array([elem_dict.get(e, 0) for e in fem_mesh.Volumes]) + cell_array = np.vstack((val_array,)).T + write_fenics_mesh_scalar_cellfunctions(name, cell_array, attribute, encoding=ENCODING_ASCII) + + # TODO: improve cell functions support # write_fenics_mesh_cellfunctions(fem_mesh_obj, {}, attribute, encoding=encoding)