Beispiel #1
0
def get_data(self):
    """Import mesh and generate MeshMat object

    Parameters
    ----------
    self : ImportData
        An ImportData object

    Returns
    -------
    mesh: MeshMat
        The generated MeshMat object

    """

    # Get mesh data (nodes and elements)
    if splitext(self.file_path)[1] == ".unv":
        nodes, elements = ImportMeshUnv(self.file_path).get_data()
    else:
        raise Exception(
            splitext(self.file_path)[1] + " files are not supported")

    # Define MeshMat object
    if min(nodes[:, 0]) == 0 and max(nodes[:, 0]) == len(nodes[:, 0]) - 1:
        is_renum = False
    else:
        is_renum = True

    mesh = MeshMat(_is_renum=is_renum)
    mesh.label = "Imported mesh"

    # Define NodeMat object
    mesh.node = NodeMat(
        coordinate=nodes[:, 1:],
        nb_node=nodes.shape[0],
        indice=nodes[:, 0],
    )

    # Define CellMat objects
    for elt_type, elt in elements.items():
        mesh.cell[elt_type] = CellMat(
            connectivity=elt[:, 1:],
            nb_cell=elt.shape[0],
            nb_node_per_cell=elt.shape[1] - 1,
            indice=elt[:, 0],
        )

    return mesh
Beispiel #2
0
def get_group(self, group_names):
    """Return all attributes of a MeshSolution object with only the cells, points
    and corresponding solutions of the group. Solutions are converted as SolutionMat.

     Parameters
     ----------
     self : MeshSolution
         an MeshSolution object
     group_name : str
         the name of the group (e.g. "stator")

     Returns
     -------
     grp_cells: dict
         a dict sorted by cell type containing connectivity of the group

    """

    is_same_mesh = self.is_same_mesh
    dimension = self.dimension

    group_indices = list()
    label = ""
    is_interface = False

    # 1) get the indices of all targeted cell corresponding to group(s)
    sep_list = list()
    if isinstance(group_names, list):
        for grp in group_names:
            if grp == "/":
                # The groups before and after "/" are stored in different lists
                # to perform the interface.
                is_interface = True
                group_indices_init = group_indices.copy()
                group_indices = list()
                sep_list.append(group_indices_init)
            else:
                group_indices.extend(self.group[grp])
                label = label + grp + "_"
    elif isinstance(group_names, str):
        if group_names not in self.group:
            raise KeyError(group_names +
                           " group doesn't exist (available groups: " +
                           str(list(self.group.keys())) + ")")
        group_indices.extend(self.group[group_names])
        label = label + group_names

    sep_list.append(group_indices)

    # 2) extract the corresponding connectivity and create a new mesh
    mesh_init = self.get_mesh()
    point_init = mesh_init.get_point()
    mesh_list = list()
    for sep in sep_list:
        connect_dict, nb_cell, indice_dict = mesh_init.get_cell(sep)

        node_indice = list()
        mesh_new = MeshMat()
        for key in connect_dict:
            node_indice.extend(np.unique(connect_dict[key]))
            mesh_new.cell[key] = CellMat(
                connectivity=connect_dict[key],
                nb_cell=len(connect_dict[key]),
                nb_pt_per_cell=mesh_init.cell[key].nb_pt_per_cell,
                indice=indice_dict[key],
                interpolation=mesh_init.cell[key].interpolation,
            )
        node_indice = np.unique(node_indice)

        mesh_new.point = PointMat(init_dict=mesh_init.point.as_dict())
        mesh_new.label = label

        mesh_list.append(mesh_new)

    # 3) if interface, create the corresponding new mesh (e.g. with triangle mesh,
    #    it creates only segment cells)
    if is_interface:
        mesh_interface = mesh_list[0].interface(mesh_list[1])
        connect_interface, nb_cell_interf, indices_interf = mesh_interface.get_cell(
        )
        node_indice_interf = list()
        for key in connect_interface:
            node_indice_interf.extend(np.unique(connect_interface[key]))

        node_indice = np.unique(node_indice_interf)

    # 4) select the corresponding solutions
    sol_list = list()
    for sol in self.solution:
        type_cell_sol = sol.type_cell

        new_sol = None
        if type_cell_sol == "point":
            new_sol = sol.get_solution(indice=node_indice)
        elif not is_interface:  # Interface is only available for point solution.
            new_sol = sol.get_solution(indice=indice_dict[type_cell_sol])

        if new_sol is not None:
            sol_list.append(new_sol)

    # 5) Create the corresponding MeshSolution object
    if is_interface:
        mesh_interface.renum()
        mesh = mesh_interface
    else:
        mesh_new.renum()
        mesh = mesh_new

    meshsol_grp = self.copy()
    meshsol_grp.label = label
    meshsol_grp.mesh = [mesh]
    meshsol_grp.is_same_mesh = is_same_mesh
    meshsol_grp.solution = sol_list
    meshsol_grp.dimension = dimension

    return meshsol_grp
Beispiel #3
0
def get_group(self, group_names):
    """Return all attributes of a MeshSolution object with only the cells, points and corresponding solutions of
    the group. Solutions are converted as SolutionMat.

     Parameters
     ----------
     self : MeshSolution
         an MeshSolution object
     group_name : str
         the name of the group (e.g. "stator")

     Returns
     -------
     grp_cells: dict
         a dict sorted by cell type containing connectivity of the group

    """

    is_same_mesh = self.is_same_mesh
    dimension = self.dimension

    group_indices = list()
    label = ""
    is_interface = False

    # 1) get the indices of all targeted cell corresponding to group(s)
    sep_list = list()
    if isinstance(group_names, list):
        for grp in group_names:
            if (
                    grp == "/"
            ):  # The groups before and after "/" are stored in different lists to perform the interface.
                is_interface = True
                group_indices_init = group_indices.copy()
                group_indices = list()
                sep_list.append(group_indices_init)
            else:
                group_indices.extend(self.group[grp])
                label = label + grp + "_"
    elif isinstance(group_names, str):
        group_indices.extend(self.group[group_names])
        label = label + group_names

    sep_list.append(group_indices)

    # 2) extract the corresponding connectivity and create a new mesh
    mesh_init = self.get_mesh()
    point_init = mesh_init.get_point()
    mesh_list = list()
    for sep in sep_list:
        connect_dict, nb_cell, indice_dict = mesh_init.get_cell(sep)

        node_indice = list()
        mesh_new = MeshMat()
        for key in connect_dict:
            node_indice.extend(np.unique(connect_dict[key]))
            mesh_new.cell[key] = CellMat(
                connectivity=connect_dict[key],
                nb_cell=len(connect_dict[key]),
                nb_pt_per_cell=mesh_init.cell[key].nb_pt_per_cell,
                indice=indice_dict[key],
                interpolation=mesh_init.cell[key].interpolation,
            )
        node_indice = np.unique(node_indice)

        mesh_new.point = PointMat(init_dict=mesh_init.point.as_dict())
        mesh_new.label = label

        mesh_list.append(mesh_new)

    # 3) if interface, create the corresponding new mesh (e.g. with triangle mesh, it creates only segment cells)
    if is_interface:
        mesh_interface = mesh_list[0].interface(mesh_list[1])
        connect_interface, nb_cell_interf, indices_interf = mesh_interface.get_cell(
        )
        node_indice_interf = list()
        for key in connect_interface:
            node_indice_interf.extend(np.unique(connect_interface[key]))

        node_indice = np.unique(node_indice_interf)

    # 4) select the corresponding solutions
    sol_list = list()
    for sol in self.solution:
        label_sol = sol.label
        type_cell_sol = sol.type_cell
        field_sol = sol.get_field()
        axis_name, axis_size = sol.get_axes_list()

        if type_cell_sol == "point":
            Iindice = np.where(axis_name == "indice")[0]
            axis_size[Iindice] = len(node_indice)

            new_field_sol = field_sol[:, node_indice, :]
            new_sol = SolutionMat(
                label=label_sol,
                type_cell=type_cell_sol,
                field=new_field_sol,
                indice=node_indice,
                axis_name=axis_name,
                axis_size=axis_size,
            )
            sol_list.append(new_sol)

        elif not is_interface:  # Interface is only available for point solution.

            ind_cell = indice_dict[type_cell_sol]

            if "component" in axis_name:
                new_field_sol = field_sol[:, ind_cell, :]
            else:
                new_field_sol = field_sol[:, ind_cell]

            axis_size[axis_name.index("indice")] = len(ind_cell)

            new_sol = SolutionMat(
                label=label_sol,
                type_cell=type_cell_sol,
                field=new_field_sol,
                indice=ind_cell,
                axis_name=axis_name,
                axis_size=axis_size,
            )

            sol_list.append(new_sol)

    # 5) Create the corresponding MeshSolution object
    if is_interface:
        mesh_interface.renum()
        mesh = mesh_interface
    else:
        mesh_new.renum()
        mesh = mesh_new

    meshsol_grp = self.copy()
    meshsol_grp.label = label
    meshsol_grp.mesh = [mesh]
    meshsol_grp.is_same_mesh = is_same_mesh
    meshsol_grp.solution = sol_list
    meshsol_grp.dimension = dimension

    return meshsol_grp
Beispiel #4
0
def perm_coord(
    self,
    perm_coord_list=[0, 1, 2],
    path_meshVTK=None,
):
    """Returns the current MeshVTK object with permuted coordinates

    Parameters
    ----------
    self : MeshVTK
        a MeshVTK object
    perm_coord_list : list
        list of the coordinates to be permuted
    path_meshVTK : str
        full path to the MeshVTK file

    Returns
    -------
    mesh: MeshVTK
        The MeshVTK object with permuted coordinates

    """
    # convert into MeshMat object
    mesh_mat = self.convert(meshtype="MeshMat", scale=1)

    # extract nodes en elements
    mesh_mat_node = mesh_mat.get_node()
    mesh_mat_cell = mesh_mat.get_cell()

    # swap axis
    mesh_mat_node = mesh_mat_node.T[perm_coord_list].T

    # create new object
    # 1. create NodeMat
    nb_node = len(mesh_mat_node)
    nodemat = NodeMat(coordinate=mesh_mat_node, nb_node=nb_node)

    # 2. create CellMat
    cellMat = CellMat()
    CellMatDict = dict()

    for key in mesh_mat_cell[0]:

        cellMat = CellMat(
            connectivity=mesh_mat_cell[0][key],
            nb_cell=len(mesh_mat_cell[0][key]),
        )
        CellMatDict[key] = cellMat

    # 3. create MeshMat
    meshmat = MeshMat(cell=CellMatDict, node=nodemat)

    # convert and save into vtk
    mesh_pv = meshmat.get_mesh_pv()
    if path_meshVTK != None:
        mesh_pv.save(path_meshVTK)
    else:
        mesh_pv.save(self.path + "/" + self.name + ".vtk")

    self.mesh = mesh_pv

    return self