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 _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 _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
# -*- coding: utf-8 -*- """ Checks that this version of FEniCS/DOLFIN was compiled with parallel HDF5 and a few extra preconditoners and solvers. """ import dolfin as df assert df.has_hdf5_parallel() assert df.has_krylov_solver_preconditioner('hypre_amg') assert df.has_krylov_solver_preconditioner('hypre_euclid') assert df.has_krylov_solver_preconditioner('hypre_parasails') assert df.has_lu_solver_method('mumps') assert df.has_petsc() assert df.has_petsc4py()
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)