Пример #1
0
    def convert_fem_mesh_obj_to_table(self):
        def ro(item):
            item.setFlags(~QtCore.Qt.ItemIsEditable & ~QtCore.Qt.ItemIsEnabled)
            return item

        gmshgroups = importToolsFem.get_FemMeshObjectMeshGroups(
            self.fem_mesh_obj)
        fem_mesh = self.fem_mesh_obj.FemMesh

        self.form.tableGroups.setRowCount(0)
        self.form.tableGroups.setRowCount(len(gmshgroups))

        for (ind, gind) in enumerate(gmshgroups):
            # group number
            self.form.tableGroups.setItem(
                ind, 0, ro(QtGui.QTableWidgetItem(str(gind))))
            # group name
            self.form.tableGroups.setItem(
                ind, 1,
                ro(QtGui.QTableWidgetItem(fem_mesh.getGroupName(gind))))
            # group elements
            self.form.tableGroups.setItem(
                ind, 2,
                ro(QtGui.QTableWidgetItem(fem_mesh.getGroupElementType(gind))))
            # default value for not marked elements
            self.form.tableGroups.setItem(ind, 3,
                                          QtGui.QTableWidgetItem(str(-1)))
            # default value for marked elements
            self.form.tableGroups.setItem(ind, 4,
                                          QtGui.QTableWidgetItem(str(gind)))
Пример #2
0
def export(objectslist, fileString):
    "called when freecad exports a file"
    if len(objectslist) != 1:
        FreeCAD.Console.PrintError("This exporter can only export one object.\n")
        return
    obj = objectslist[0]
    if not obj.isDerivedFrom("Fem::FemMeshObject"):
        FreeCAD.Console.PrintError("No FEM mesh object selected.\n")
        return

    if fileString != "":
        fileName, fileExtension = os.path.splitext(fileString)
        if fileExtension.lower() == '.xml':
            writeFenicsXML.write_fenics_mesh_xml(obj, fileString)
        elif fileExtension.lower() == '.xdmf':
            if importToolsFem.get_FemMeshObjectMeshGroups(obj) is not ():
                # if there are groups found, make task panel available
                panel = WriteXDMFTaskPanel(obj, fileString)
                FreeCADGui.Control.showDialog(panel)
            else:
                writeFenicsXDMF.write_fenics_mesh_xdmf(obj, fileString)
Пример #3
0
def export(objectslist, fileString):
    "called when freecad exports a file"
    if len(objectslist) != 1:
        FreeCAD.Console.PrintError(
            "This exporter can only export one object.\n")
        return
    obj = objectslist[0]
    if not obj.isDerivedFrom("Fem::FemMeshObject"):
        FreeCAD.Console.PrintError("No FEM mesh object selected.\n")
        return

    if fileString != "":
        fileName, fileExtension = os.path.splitext(fileString)
        if fileExtension.lower() == '.xml':
            writeFenicsXML.write_fenics_mesh_xml(obj, fileString)
        elif fileExtension.lower() == '.xdmf':
            if importToolsFem.get_FemMeshObjectMeshGroups(obj) is not ():
                # if there are groups found, make task panel available
                panel = WriteXDMFTaskPanel(obj, fileString)
                FreeCADGui.Control.showDialog(panel)
            else:
                writeFenicsXDMF.write_fenics_mesh_xdmf(obj, fileString)
Пример #4
0
    def convert_fem_mesh_obj_to_table(self):

        def ro(item):
            item.setFlags(~QtCore.Qt.ItemIsEditable & ~QtCore.Qt.ItemIsEnabled)
            return item

        gmshgroups = importToolsFem.get_FemMeshObjectMeshGroups(self.fem_mesh_obj)
        fem_mesh = self.fem_mesh_obj.FemMesh

        self.form.tableGroups.setRowCount(0)
        self.form.tableGroups.setRowCount(len(gmshgroups))

        for (ind, gind) in enumerate(gmshgroups):
            # group number
            self.form.tableGroups.setItem(ind, 0, ro(QtGui.QTableWidgetItem(str(gind))))
            # group name
            self.form.tableGroups.setItem(ind, 1, ro(QtGui.QTableWidgetItem(fem_mesh.getGroupName(gind))))
            # group elements
            self.form.tableGroups.setItem(ind, 2, ro(QtGui.QTableWidgetItem(fem_mesh.getGroupElementType(gind))))
            # default value for not marked elements
            self.form.tableGroups.setItem(ind, 3, QtGui.QTableWidgetItem(str(-1)))
            # default value for marked elements
            self.form.tableGroups.setItem(ind, 4, QtGui.QTableWidgetItem(str(gind)))
Пример #5
0
def write_fenics_mesh_xdmf(fem_mesh_obj, outputfile, group_values_dict={}, encoding=ENCODING_ASCII):
    """
        For the export of xdmf.
    """

    FreeCAD_to_Fenics_dict = {
        "Triangle": "triangle",
        "Tetra": "tetrahedron",
        "Hexa": "hexahedron",
        "Edge": "interval",
        "Node": "point",
        "Quadrangle": "quadrilateral",

        "Polygon": "unknown", "Polyhedron": "unknown",
        "Prism": "unknown", "Pyramid": "unknown",
    }

    print("Converting " + fem_mesh_obj.Label + " to fenics XDMF File")
    print("Dimension of mesh: %d" % (get_FemMeshObjectDimension(fem_mesh_obj),))

    elements_in_mesh = get_FemMeshObjectElementTypes(fem_mesh_obj)
    print("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]
    print("Celltype in mesh -> %s and its Fenics dolfin name: %s" % (str(celltype_in_mesh), cellname_fenics))

    root = ET.Element("Xdmf", version="3.0")
    domain = ET.SubElement(root, "Domain")
    base_grid = ET.SubElement(domain, "Grid", Name="base_mesh", GridType="Uniform")
    base_topology = ET.SubElement(base_grid, "Topology")
    base_geometry = ET.SubElement(base_grid, "Geometry")

    # TODO: for the general mesh: write out topology and geometry in grid node
    # TOOD: for every marked group write own grid node with topology (ref if cells)
    #       geometry ref, attribute

    #####################################
    # write base topo and geometry
    nodes_dict = write_fenics_mesh_points_xdmf(fem_mesh_obj, base_geometry, encoding=encoding)
    write_fenics_mesh_codim_xdmf(fem_mesh_obj, base_topology, nodes_dict, codim=0, encoding=encoding)
    #####################################

    fem_mesh = fem_mesh_obj.FemMesh
    gmshgroups = get_FemMeshObjectMeshGroups(fem_mesh_obj)

    if gmshgroups is not ():
        print('found mesh groups')

    for g in gmshgroups:
        mesh_function_type = fem_mesh.getGroupElementType(g)
        mesh_function_codim = dim_cell - FreeCAD_Group_Dimensions[mesh_function_type]
        mesh_function_name = fem_mesh.getGroupName(g)

        print('group id: %d (label: %s) with element type %s and codim %d'
              % (g, mesh_function_name, mesh_function_type, mesh_function_codim))

        mesh_function_grid = ET.SubElement(domain, "Grid", Name=mesh_function_name + "_mesh", GridType="Uniform")
        mesh_function_topology = ET.SubElement(mesh_function_grid, "Topology")

        mesh_function_topology_description = write_fenics_mesh_codim_xdmf(fem_mesh_obj,
                                                                          mesh_function_topology,
                                                                          nodes_dict,
                                                                          codim=mesh_function_codim, encoding=encoding)

        mesh_function_geometry = ET.SubElement(mesh_function_grid, "Geometry", Reference="XML")
        mesh_function_geometry.text = "/Xdmf/Domain/Grid/Geometry"
        mesh_function_attribute = ET.SubElement(mesh_function_grid, "Attribute")

        elem_dict = {}
        (elem_mark_group, elem_mark_default) = group_values_dict.get(g, (g, -1))

        # TODO: is it better to save all groups each at once or collect all codim equal
        # groups to put them into one function?
        # TODO: nevertheless there has to be a dialog which fixes the default value and the mark value

        for e in fem_mesh.getGroupElements(g):
            elem_dict[e] = elem_mark_group

        val_array = np.array([elem_dict.get(e, elem_mark_default) for e in mesh_function_topology_description])
        topo_array = np.vstack((val_array,)).T
        write_fenics_mesh_scalar_cellfunctions(mesh_function_name,
                                               topo_array,
                                               mesh_function_attribute, encoding=ENCODING_ASCII)

    # TODO: improve cell functions support

    fp = open(outputfile, "w")
    fp.write('''<?xml version="1.0"?>\n<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>\n''')
    fp.write(ET.tostring(root))
    # xml core functionality does not support pretty printing
    # so the output file looks quite ugly
    fp.close()
Пример #6
0
def write_fenics_mesh_xdmf(fem_mesh_obj, outputfile, group_values_dict={}, encoding=ENCODING_ASCII):
    """
        For the export of xdmf.
    """

    FreeCAD_to_Fenics_dict = {
        "Triangle": "triangle",
        "Tetra": "tetrahedron",
        "Hexa": "hexahedron",
        "Edge": "interval",
        "Node": "point",
        "Quadrangle": "quadrilateral",

        "Polygon": "unknown", "Polyhedron": "unknown",
        "Prism": "unknown", "Pyramid": "unknown",
    }

    print("Converting " + fem_mesh_obj.Label + " to fenics XDMF File")
    print("Dimension of mesh: %d" % (get_FemMeshObjectDimension(fem_mesh_obj),))

    elements_in_mesh = get_FemMeshObjectElementTypes(fem_mesh_obj)
    print("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]
    print("Celltype in mesh -> %s and its Fenics dolfin name: %s" % (str(celltype_in_mesh), cellname_fenics))

    root = ET.Element("Xdmf", version="3.0")
    domain = ET.SubElement(root, "Domain")
    base_grid = ET.SubElement(domain, "Grid", Name="base_mesh", GridType="Uniform")
    base_topology = ET.SubElement(base_grid, "Topology")
    base_geometry = ET.SubElement(base_grid, "Geometry")

    # TODO: for the general mesh: write out topology and geometry in grid node
    # TOOD: for every marked group write own grid node with topology (ref if cells)
    #       geometry ref, attribute

    #####################################
    # write base topo and geometry
    nodes_dict = write_fenics_mesh_points_xdmf(fem_mesh_obj, base_geometry, encoding=encoding)
    write_fenics_mesh_codim_xdmf(fem_mesh_obj, base_topology, nodes_dict, codim=0, encoding=encoding)
    #####################################

    fem_mesh = fem_mesh_obj.FemMesh
    gmshgroups = get_FemMeshObjectMeshGroups(fem_mesh_obj)

    if gmshgroups is not ():
        print('found mesh groups')

    for g in gmshgroups:
        mesh_function_type = fem_mesh.getGroupElementType(g)
        mesh_function_codim = dim_cell - FreeCAD_Group_Dimensions[mesh_function_type]
        mesh_function_name = fem_mesh.getGroupName(g)

        print('group id: %d (label: %s) with element type %s and codim %d'
              % (g, mesh_function_name, mesh_function_type, mesh_function_codim))

        mesh_function_grid = ET.SubElement(domain, "Grid", Name=mesh_function_name + "_mesh", GridType="Uniform")
        mesh_function_topology = ET.SubElement(mesh_function_grid, "Topology")

        mesh_function_topology_description = write_fenics_mesh_codim_xdmf(fem_mesh_obj,
                                                                          mesh_function_topology,
                                                                          nodes_dict,
                                                                          codim=mesh_function_codim, encoding=encoding)

        mesh_function_geometry = ET.SubElement(mesh_function_grid, "Geometry", Reference="XML")
        mesh_function_geometry.text = "/Xdmf/Domain/Grid/Geometry"
        mesh_function_attribute = ET.SubElement(mesh_function_grid, "Attribute")

        elem_dict = {}
        (elem_mark_group, elem_mark_default) = group_values_dict.get(g, (g, -1))

        # TODO: is it better to save all groups each at once or collect all codim equal
        # groups to put them into one function?
        # TODO: nevertheless there has to be a dialog which fixes the default value and the mark value

        for e in fem_mesh.getGroupElements(g):
            elem_dict[e] = elem_mark_group

        val_array = np.array([elem_dict.get(e, elem_mark_default) for e in mesh_function_topology_description])
        topo_array = np.vstack((val_array,)).T
        write_fenics_mesh_scalar_cellfunctions(mesh_function_name,
                                               topo_array,
                                               mesh_function_attribute, encoding=ENCODING_ASCII)

    # TODO: improve cell functions support

    fp = open(outputfile, "w")
    fp.write('''<?xml version="1.0"?>\n<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>\n''')
    fp.write(ET.tostring(root))
    # xml core functionality does not support pretty printing
    # so the output file looks quite ugly
    fp.close()