Esempio n. 1
0
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
Esempio n. 2
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