예제 #1
0
 def write(self, function, name, index):
     time = float(index)
     # Write visualization file (no append available, will overwrite)
     self._update_function_container(function)
     self._visualization_file.write(self._function_container, time)
     # Write restart file. It might be possible that the solution was written to file in a previous run
     # and the execution was interrupted before last written index was updated. In this corner case
     # there would be two functions corresponding to the same time, with two consecutive indices.
     # For now the inelegant way is to try to read: if that works, assume that we are in the corner case;
     # otherwise, we are in the standard case and we should write to file.
     try:
         self._restart_file.read_checkpoint(self._function_container,
                                            name, index)
     except RuntimeError:
         self._update_function_container(function)
         bak_log_level = get_log_level()
         set_log_level(int(WARNING) + 1)  # disable xdmf logs
         if self.append_attribute:
             self._restart_file.write_checkpoint(
                 self._function_container, name, time, append=True)
         else:
             self._restart_file.write_checkpoint(
                 self._function_container, name, time)
         set_log_level(bak_log_level)
     # Once solutions have been written to file, update last written index
     self._write_last_index(index)
예제 #2
0
 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
         self._update_function_container(function)
         time = float(index)
         self._visualization_file.write(self._function_container, time)
         bak_log_level = get_log_level()
         set_log_level(int(WARNING) + 1)  # disable xdmf logs)
         if self.append_attribute:
             self._restart_file.write_checkpoint(
                 self._function_container, name, time, append=True)
         else:
             self._restart_file.write_checkpoint(
                 self._function_container, name, time)
         set_log_level(bak_log_level)
         # Once solutions have been written to file, update last written index
         self._write_last_index(index)
     elif index == self._last_index:
         # corner case for problems with two (or more) unknowns which are written separately to file;
         # one unknown was written to file, while the other was not: since the problem might be coupled,
         # a recomputation of both is required, but there is no need to update storage
         pass
     else:
         raise ValueError("Invalid index")
예제 #3
0
from functools import reduce
import numpy as np
import petsc4py
from petsc4py import PETSc
from dolfin import MPI
import matplotlib.pyplot as plt
from dolfin.cpp.log import log, LogLevel, get_log_level
import mpi4py
import yaml

from dolfin import derivative, assemble

comm = mpi4py.MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
log_level = get_log_level()


class EigenSolver(object):
    def __init__(
            self,
            a_k,
            u,
            a_m=None,  # optional, for eigpb of the type (K-lambda M)x=0
            bcs=None,
            restricted_dofs_is=None,
            slepc_options='',
            option_prefix='eigen_',
            comm=MPI.comm_world,
            slepc_eigensolver=None,
            initial_guess=None):
예제 #4
0
def _write_to_xdmf_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_rank = fun.value_rank()
    fun_dim = product(fun.value_shape())
    assert fun_rank <= 2
    if ((fun_rank is 1 and fun_dim not in (2, 3))
            or (fun_rank is 2 and fun_dim not in (4, 9))):
        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_xdmf_file(fun_i, directory, filename_i, suffix, None)
    else:
        full_filename_visualization = os.path.join(str(directory),
                                                   filename + ".xdmf")
        full_filename_checkpoint = os.path.join(str(directory),
                                                filename + "_checkpoint.xdmf")
        if suffix is not None:
            if full_filename_checkpoint in _all_xdmf_files:
                assert _all_xdmf_latest_suffix[
                    full_filename_checkpoint] == suffix - 1
                _all_xdmf_latest_suffix[full_filename_checkpoint] = suffix
            else:
                assert suffix == 0
                # Remove existing files if any, as new functions should not be appended,
                # but rather overwrite existing functions
                if is_io_process() and os.path.exists(
                        full_filename_checkpoint):
                    os.remove(full_filename_checkpoint)
                    os.remove(full_filename_checkpoint.replace(".xdmf", ".h5"))
                _all_xdmf_files[full_filename_visualization] = XDMFFile(
                    full_filename_visualization)
                _all_xdmf_files[full_filename_checkpoint] = XDMFFile(
                    full_filename_checkpoint)
                _all_xdmf_latest_suffix[
                    full_filename_checkpoint] = 0  # don't store these twice for both visualization
                _all_xdmf_functions[full_filename_checkpoint] = fun.copy(
                    deepcopy=True)  # and checkpoint, as they are the same!
            # Make sure to always use the same function, otherwise dolfin
            # changes the numbering and visualization is difficult in ParaView
            assign(_all_xdmf_functions[full_filename_checkpoint], fun)
            _all_xdmf_files[full_filename_visualization].write(
                _all_xdmf_functions[full_filename_checkpoint], float(suffix))
            bak_log_level = get_log_level()
            set_log_level(int(WARNING) + 1)  # disable xdmf logs
            _all_xdmf_files[full_filename_checkpoint].write_checkpoint(
                _all_xdmf_functions[full_filename_checkpoint], function_name,
                float(suffix))
            set_log_level(bak_log_level)
            # Write out current suffix as well
            SuffixIO.save_file(suffix, directory, filename + "_suffix")
        else:
            # Remove existing files if any, as new functions should not be appended,
            # but rather overwrite existing functions
            if is_io_process() and os.path.exists(full_filename_checkpoint):
                os.remove(full_filename_checkpoint)
                os.remove(full_filename_checkpoint.replace(".xdmf", ".h5"))
            with XDMFFile(full_filename_visualization) as file_visualization:
                file_visualization.write(fun, 0.)
            with XDMFFile(full_filename_checkpoint) as file_checkpoint:
                file_checkpoint.write_checkpoint(fun, function_name, 0.)