def function_load(fun, directory, filename, suffix=None): fun_V = fun.function_space() if (not has_hdf5() or not has_hdf5_parallel() or fun_V.mesh().geometry().dim() is 1 # due to DOLFIN issue #892 TODO ): return _read_from_xml_file(fun, directory, filename, suffix) else: if hasattr(fun_V, "_index_to_components") and len( fun_V._index_to_components) > 1: for (index, components) in fun_V._index_to_components.items(): sub_fun_V = get_function_subspace(fun_V, components) sub_fun = Function(sub_fun_V) if not _read_from_xdmf_file(sub_fun, directory, filename, suffix, components): return False else: extended_sub_fun = function_extend_or_restrict( sub_fun, None, fun_V, components[0], weight=None, copy=True) fun.vector().add_local( extended_sub_fun.vector().get_local()) fun.vector().apply("add") return True else: return _read_from_xdmf_file(fun, directory, filename, suffix)
def _write_to_file(fun, directory, filename, suffix, components=None): if components is not None: filename = filename + "_component_" + "".join(components) function_name = "function_" + "".join(components) else: function_name = "function" fun_V_element = fun.function_space().ufl_element() if isinstance(fun_V_element, MixedElement) and not isinstance( fun_V_element, (TensorElement, VectorElement)): funs = fun.split(deepcopy=True) for (i, fun_i) in enumerate(funs): if components is not None: filename_i = filename + "_subcomponent_" + str(i) else: filename_i = filename + "_component_" + str(i) _write_to_file(fun_i, directory, filename_i, suffix, None) else: if fun_V_element.family() == "Real": SolutionFile = SolutionFileXML else: if has_hdf5() and has_hdf5_parallel(): SolutionFile = SolutionFileXDMF else: SolutionFile = SolutionFileXML if suffix is not None: if suffix == 0: # Remove existing files if any, as new functions should not be appended, # but rather overwrite existing functions SolutionFile.remove_files(directory, filename) # Remove from storage and re-create try: del _all_solution_files[(directory, filename)] except KeyError: pass _all_solution_files[(directory, filename)] = SolutionFile( directory, filename) file_ = _all_solution_files[(directory, filename)] file_.write(fun, function_name, suffix) else: # Remove existing files if any, as new functions should not be appended, # but rather overwrite existing functions SolutionFile.remove_files(directory, filename) # Write function to file file_ = SolutionFile(directory, filename) file_.write(fun, function_name, 0)
def __load_mesh_function_hdf5(mesh_function, mesh, mf_dim=None): """ Load a dolfin mesh function from an HDF5 file. Parameters ---------- mesh_function : str Name of the file containing the mesh function information Returns ------- mesh_func : dolfin.cpp.mesh.MeshFunctionSizet This function returns a dolfin mesh function object. """ # Check dolfin for HDF5 support if not dlf.has_hdf5(): msg = 'The current installation of dolfin does not support HDF5 files.' raise SoftwareNotAvailable(msg) # Check file extension if mesh_function[-3:] == '.h5': f = dlf.HDF5File(MPI_COMM_WORLD, mesh_function, 'r') # Assume the mesh function has 1 less dimension than mesh. if mf_dim is None: mf_dim = mesh.geometry().dim() - 1 mesh_func = dlf.MeshFunction('size_t', mesh, mf_dim) f.read(mesh_func, 'boundaries') f.close() else: msg = 'The file extension provided must be \'.h5\'.' raise ValueError(msg) return mesh_func
def _read_from_file(fun, directory, filename, suffix, components=None): if components is not None: filename = filename + "_component_" + "".join(components) function_name = "function_" + "".join(components) else: function_name = "function" fun_V_element = fun.function_space().ufl_element() if isinstance(fun_V_element, MixedElement) and not isinstance( fun_V_element, (TensorElement, VectorElement)): funs = fun.split(deepcopy=True) for (i, fun_i) in enumerate(funs): if components is not None: filename_i = filename + "_subcomponent_" + str(i) else: filename_i = filename + "_component_" + str(i) _read_from_file(fun_i, directory, filename_i, suffix, None) assign(fun.sub(i), fun_i) else: if fun_V_element.family() == "Real": SolutionFile = SolutionFileXML else: if has_hdf5() and has_hdf5_parallel(): SolutionFile = SolutionFileXDMF else: SolutionFile = SolutionFileXML if suffix is not None: if suffix == 0: # Remove from storage and re-create try: del _all_solution_files[(directory, filename)] except KeyError: pass _all_solution_files[(directory, filename)] = SolutionFile( directory, filename) file_ = _all_solution_files[(directory, filename)] file_.read(fun, function_name, suffix) else: file_ = SolutionFile(directory, filename) file_.read(fun, function_name, 0)
def function_save(fun, directory, filename, suffix=None): fun_V = fun.function_space() if (not has_hdf5() or not has_hdf5_parallel() or fun_V.mesh().geometry().dim() is 1 # due to DOLFIN issue #892 TODO ): if hasattr(fun_V, "_index_to_components") and len( fun_V._index_to_components) > 1: for (index, components) in fun_V._index_to_components.items(): sub_fun = function_extend_or_restrict(fun, components[0], get_function_subspace( fun_V, components[0]), None, weight=None, copy=True) _write_to_pvd_file(sub_fun, directory, filename, suffix, components) else: _write_to_pvd_file(fun, directory, filename, suffix) _write_to_xml_file(fun, directory, filename, suffix) else: if hasattr(fun_V, "_index_to_components") and len( fun_V._index_to_components) > 1: for (index, components) in fun_V._index_to_components.items(): sub_fun = function_extend_or_restrict(fun, components[0], get_function_subspace( fun_V, components[0]), None, weight=None, copy=True) _write_to_xdmf_file(sub_fun, directory, filename, suffix, components) else: _write_to_xdmf_file(fun, directory, filename, suffix)
def __load_mesh_hdf5(mesh_file): """ Load dolfin mesh from an HDF5 file. Parameters ---------- mesh_file : str Name of the file containing the mesh information Returns ------- mesh : dolfin.cpp.mesh.Mesh This function returns a dolfin mesh object. """ # Check dolfin for HDF5 support if not dlf.has_hdf5(): msg = 'The current installation of dolfin does not support HDF5 files.' raise SoftwareNotAvailable(msg) # Check file extension if mesh_file[-3:] == '.h5': f = dlf.HDF5File(MPI_COMM_WORLD, mesh_file, 'r') mesh = dlf.Mesh() f.read(mesh, 'mesh', False) f.close() else: msg = 'The file extension provided must be \'.h5\'.' raise ValueError(msg) return mesh
def _init_last_index(self): if IndexIO.exists_file(self._directory, self._filename + "_index.sfx"): self._last_index = IndexIO.load_file(self._directory, self._filename + "_index.sfx") else: self._last_index = -1 def _write_last_index(self, index): self._last_index = index # Write out current index IndexIO.save_file(index, self._directory, self._filename + "_index.sfx") if not has_hdf5() or not has_hdf5_parallel(): class SolutionFile(SolutionFile_Base): def __init__(self, directory, filename): SolutionFile_Base.__init__(self, directory, filename) self._visualization_file = PVDFile(self._full_filename + ".pvd", "compressed") @staticmethod def remove_files(directory, filename): SolutionFile_Base.remove_files(directory, filename) # No need to remove further files, PVD and XML will get automatically truncated def write(self, function, name, index): assert index in (self._last_index, self._last_index + 1) if index == self._last_index + 1: # writing out solutions after time stepping
# Copyright (C) 2014 Chris Richardson # # This file is part of DOLFIN (https://www.fenicsproject.org) # # SPDX-License-Identifier: LGPL-3.0-or-later import os from dolfin import (UnitSquareMesh, MPI, FunctionSpace, Function, Expression, has_hdf5) import dolfin.cpp as cpp from dolfin_utils.test import (skip_if_not_HDF5, tempdir, xfail_if_complex, xfail_with_serial_hdf5_in_parallel) if has_hdf5(): from dolfin.io import HDF5File assert (tempdir) @xfail_if_complex @skip_if_not_HDF5 @xfail_with_serial_hdf5_in_parallel def test_save_and_read_function_timeseries(tempdir): filename = os.path.join(tempdir, "function.h5") mesh = UnitSquareMesh(MPI.comm_world, 10, 10) Q = FunctionSpace(mesh, "CG", 3) F0 = Function(Q) F1 = Function(Q) E = Expression("t*x[0]", t=0.0, degree=1) F0.interpolate(E) # Save to HDF5 File
def invalid_config(encoding): return (not has_hdf5() and encoding == XDMFFile.Encoding.HDF5) \ or (encoding == XDMFFile.Encoding.ASCII and MPI.size(MPI.comm_world) > 1) \ or (not has_hdf5_parallel() and MPI.size(MPI.comm_world) > 1)