コード例 #1
0
ファイル: xdmf_utils.py プロジェクト: pyfsi/Kratos
def CreateXdmfSpatialGrid(h5_model_part):
    """Return an XDMF Grid object corresponding to a mesh in an HDF5 file.

    Keyword arguments:
    h5_model_part -- the HDF5 group containing the model part

    Expects:
    - element connectivities in h5_model_part["Xdmf/Elements/<element-name>"].
      Each connectivities has attributes "Dimension" and "NumberOfNodes".  For
      example, "Element2D3N" has "Dimension" 2 and "NumberOfNodes" 3.  The
      connectivities differ from the normal mdpa connectivities in that they
      directly index the array of nodal coordinates.  Currently there is
      no other way to post-process the mesh with Xdmf.

    See:
    - core.operations.ModelPartOutput,
    - core.operations.PartitionedModelPartOutput,
    - RenumberConnectivitiesForXdmf.
    """
    sgrid = SpatialGrid()
    geom = Geometry(HDF5UniformDataItem(
        h5_model_part["Nodes/Local/Coordinates"]))
    for name, value in h5_model_part["Xdmf/Elements"].items():
        cell_type = TopologyCellType(
            value.attrs["Dimension"], value.attrs["NumberOfNodes"])
        connectivities = HDF5UniformDataItem(value["Connectivities"])
        topology = UniformMeshTopology(cell_type, connectivities)
        sgrid.add_grid(UniformGrid(name, geom, topology))
    return sgrid
コード例 #2
0
ファイル: xdmf_utils.py プロジェクト: KlausBSautter/Kratos
def CreateXdmfSpatialGrid(h5_model_part):
    """Return an XDMF Grid object corresponding to a mesh in an HDF5 file.

    Keyword arguments:
    h5_model_part -- the HDF5 group containing the model part

    Expects:
    - element connectivities in h5_model_part["Xdmf/Elements/<element-name>"].
      Each connectivities has attributes "Dimension" and "NumberOfNodes".  For
      example, "Element2D3N" has "Dimension" 2 and "NumberOfNodes" 3.  The
      connectivities differ from the normal mdpa connectivities in that they
      directly index the array of nodal coordinates.  Currently there is
      no other way to post-process the mesh with Xdmf.

    See:
    - core.operations.ModelPartOutput,
    - core.operations.PartitionedModelPartOutput,
    - RenumberConnectivitiesForXdmf.
    """
    sgrid = SpatialGrid()
    geom = Geometry(
        HDF5UniformDataItem(h5_model_part["Nodes/Local/Coordinates"]))

    spatial_grids_list = []
    GetListOfSpatialGrids(spatial_grids_list, h5_model_part["Xdmf"],
                          "RootModelPart")

    for spatial_grid in spatial_grids_list:
        spatial_grid_location = spatial_grid[0]
        spatial_grid_name = spatial_grid[1]
        current_h5_item = h5_model_part[spatial_grid_location]
        if (isinstance(current_h5_item, h5py.Dataset)):
            cell_type = TopologyCellType(3, 1)
            points = HDF5UniformDataItem(current_h5_item)
            topology = UniformMeshTopology(cell_type, points)
            sgrid.add_grid(
                UniformGrid(spatial_grid_name + "." + name, geom, topology))
            KratosMultiphysics.Logger.PrintInfo(
                "XDMF", "Added " + spatial_grid_name + " spatial grid.")
        else:
            for name, value in current_h5_item.items():
                cell_type = TopologyCellType(value.attrs["Dimension"],
                                             value.attrs["NumberOfNodes"])
                connectivities = HDF5UniformDataItem(value["Connectivities"])
                topology = UniformMeshTopology(cell_type, connectivities)
                sgrid.add_grid(
                    UniformGrid(spatial_grid_name + "." + name, geom,
                                topology))
                KratosMultiphysics.Logger.PrintInfo(
                    "XDMF", "Added " + spatial_grid_name + "." + name +
                    " spatial grid.")

    return sgrid
コード例 #3
0
ファイル: xdmf_utils.py プロジェクト: FelipeFonsecaa14/Kratos
def CreateXdmfTemporalGridFromMultifile(list_of_h5_files, h5path_to_mesh,
                                        h5path_to_results):
    """Return an XDMF Grid object for a list of temporal results in HDF5 files.

    Keyword arguments:
    list_of_h5_files -- the list of HDF5 files to be parsed
    h5path_to_mesh -- the internal HDF5 file path to the mesh
    h5path_to_results -- the internal HDF5 file path to the results

    Expects:
    - each file corresponds to a separate time step
    - the first file includes a mesh.  Subsequent files may include their own
      meshes.  If a file does not contain a mesh, it is assumed to have the
      same mesh as the most recent file containing a mesh.
    - meshes include XDMF mesh connectivities under the internal HDF5 file path
      "<h5path_to_mesh>/Xdmf".  If XDMF connectivities are not found, the file is
      skipped.  See RenumberConnectivitiesForXdmf.
    - file names contain their time step as a substring. Optionally the first
      file may omit the time step in which case it is assumed to be zero.

    If a file cannot be opened, it is skipped.
    """
    tgrid = TemporalGrid()
    for path in list_of_h5_files:
        with TryOpenH5File(path, "r") as file_:
            if not file_:
                continue
            if h5path_to_mesh in file_:
                if not "Xdmf" in file_[h5path_to_mesh]:
                    continue
                sgrid = CreateXdmfSpatialGrid(file_[h5path_to_mesh])
            current_sgrid = SpatialGrid()
            for g in sgrid.grids:
                current_sgrid.add_grid(
                    UniformGrid(g.name, g.geometry, g.topology))
            if h5path_to_results in file_:
                for result in XdmfResults(file_[h5path_to_results]):
                    current_sgrid.add_attribute(result)
            time_label = TimeLabel(path)
            if time_label == "":
                time_label = "0.0"
            tgrid.add_grid(Time(time_label), current_sgrid)
    return tgrid
コード例 #4
0
def CreateXdmfTemporalGridFromSinglefile(h5_file_name, h5path_pattern_to_mesh,
                                         h5path_pattern_to_results):
    """Return an XDMF Grid object for a list of temporal results in a single HDF5 file.

    Keyword arguments:
    h5_file_name -- the HDF5 file to be parsed
    h5path_pattern_to_mesh -- the internal HDF5 file path pattern to the mesh [ only <step> flag is supported ]
    h5path_pattern_to_results -- the internal HDF5 file path pattern to the results [ only <step> flag is supported ]

    Expects:
    - In prefixes, <step> flag is used maximum of one time only
    - If single mesh description is found, it is considered as single mesh temporal output
    """
    tgrid = TemporalGrid()

    h5path_pattern_to_mesh_wild_cards = h5path_pattern_to_mesh.replace(
        "<step>", "\d*")
    h5path_patterns_to_mesh = h5path_pattern_to_mesh.split("<step>")
    if (len(h5path_patterns_to_mesh) > 2):
        raise RuntimeError("'<step>' flag can only be used once in a prefix")

    h5path_pattern_to_results_wild_cards = h5path_pattern_to_results.replace(
        "<step>", "\d*")
    h5path_patterns_to_results = h5path_pattern_to_results.split("<step>")
    if (len(h5path_patterns_to_results) > 2):
        raise RuntimeError("'<step>' flag can only be used once in a prefix")

    renumbering_mesh_paths = []
    with TryOpenH5File(h5_file_name, "r") as file_:
        if not file_:
            raise RuntimeError(
                "Unsupported h5 file provided [ file_name = {:s} ].".format(
                    h5_file_name))

        output_meshes_dict = {}
        file_.visit(lambda x: GetMatchingGroupNames(
            output_meshes_dict, x, h5path_patterns_to_mesh,
            h5path_pattern_to_mesh_wild_cards))

        for _, v in output_meshes_dict.items():
            if "Xdmf" not in file_[v]:
                renumbering_mesh_paths.append(v)

        if len(output_meshes_dict.keys()) == 0:
            raise RuntimeError(
                "No grid information is found in the given hdf5 file matching the given pattern [ file_name = {:s}, pattern = {:s} ]."
                .format(h5_file_name, h5path_pattern_to_mesh))

    # renumber xdmf connectivities
    for v in renumbering_mesh_paths:
        KratosHDF5.HDF5XdmfConnectivitiesWriterProcess(h5_file_name,
                                                       v).Execute()

    with TryOpenH5File(h5_file_name, "r") as file_:
        output_results_dict = {}
        file_.visit(lambda x: GetMatchingGroupNames(
            output_results_dict, x, h5path_patterns_to_results,
            h5path_pattern_to_results_wild_cards))

        if len(output_results_dict.keys()) == 0:
            raise RuntimeError(
                "No results data is found in the given hdf5 file matching the given pattern [ file_name = {:s}, pattern = {:s} ]."
                .format(h5_file_name, h5path_pattern_to_results))

        for k, v in output_results_dict.items():
            if k in output_meshes_dict:
                sgrid = CreateXdmfSpatialGrid(file_[output_meshes_dict[k]])

            current_sgrid = SpatialGrid()
            for g in sgrid.grids:
                current_sgrid.add_grid(
                    UniformGrid(g.name, g.geometry, g.topology))

            for result in XdmfResults(file_[v]):
                current_sgrid.add_attribute(result)

            tgrid.add_grid(Time(k), current_sgrid)

    return tgrid