예제 #1
0
    class SolutionFile(SolutionFile_Base):
        # DOLFIN 2018.1.0.dev added (throughout the developement cycle) an optional append
        # attribute to XDMFFile.write_checkpoint, which should be set to its non-default value,
        # thus breaking backwards compatibility
        append_attribute = XDMFFile.write_checkpoint.__doc__.find(
            "append: bool") > -1

        def __init__(self, directory, filename):
            SolutionFile_Base.__init__(self, directory, filename)
            self._visualization_file = XDMFFile(self._full_filename + ".xdmf")
            self._visualization_file.parameters["flush_output"] = True
            self._restart_file = XDMFFile(self._full_filename +
                                          "_checkpoint.xdmf")
            self._restart_file.parameters["flush_output"] = True

        @staticmethod
        def remove_files(directory, filename):
            SolutionFile_Base.remove_files(directory, filename)
            #
            full_filename = os.path.join(str(directory), filename)
            if is_io_process() and os.path.exists(full_filename + ".xdmf"):
                os.remove(full_filename + ".xdmf")
                os.remove(full_filename + ".h5")
                os.remove(full_filename + "_checkpoint.xdmf")
                os.remove(full_filename + "_checkpoint.h5")

        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")

        def read(self, function, name, index):
            if index <= self._last_index:
                time = float(index)
                self._restart_file.read_checkpoint(function, name, index)
                self._update_function_container(function)
                self._visualization_file.write(self._function_container, time)
            else:
                raise OSError
예제 #2
0
class SolutionFileXDMF(SolutionFile_Base):
    # DOLFIN 2018.1.0.dev added (throughout the developement cycle) an optional append
    # attribute to XDMFFile.write_checkpoint, which should be set to its non-default value,
    # thus breaking backwards compatibility
    append_attribute = XDMFFile.write_checkpoint.__doc__.find("append: bool") > - 1
        
    def __init__(self, directory, filename):
        SolutionFile_Base.__init__(self, directory, filename)
        self._visualization_file = XDMFFile(self._full_filename + ".xdmf")
        self._visualization_file.parameters["flush_output"] = True
        self._restart_file = XDMFFile(self._full_filename + "_checkpoint.xdmf")
        self._restart_file.parameters["flush_output"] = True
            
    @staticmethod
    def remove_files(directory, filename):
        SolutionFile_Base.remove_files(directory, filename)
        #
        full_filename = os.path.join(str(directory), filename)
        def remove_files_task():
            if os.path.exists(full_filename + ".xdmf"):
                os.remove(full_filename + ".xdmf")
                os.remove(full_filename + ".h5")
                os.remove(full_filename + "_checkpoint.xdmf")
                os.remove(full_filename + "_checkpoint.h5")
        parallel_io(remove_files_task)
            
    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:
            from dolfin.cpp.log import get_log_level, LogLevel, set_log_level
            self._update_function_container(function)
            bak_log_level = get_log_level()
            set_log_level(int(LogLevel.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)
            
    def read(self, function, name, index):
        if index <= self._last_index:
            time = float(index)
            self._restart_file.read_checkpoint(function, name, index)
            self._update_function_container(function)
            self._visualization_file.write(self._function_container, time) # because there is no append option available
        else:
            raise OSError
예제 #3
0
def calc_value_differences(file_path1, file_path2, output_path, func_space,
                           t_func_space, parameters):
    f1 = XDMFFile(file_path1)
    f2 = XDMFFile(file_path2)
    f_out = XDMFFile(output_path)

    value_func1 = Function(func_space)
    value_func2 = Function(func_space)
    result = Function(t_func_space)

    f1.read_checkpoint(value_func1, 'value_func', 0)
    f2.read_checkpoint(value_func2, 'value_func', 0)
    result.vector()[:] \
        = value_func1.vector()[:] - value_func2.vector()[:]
    f_out.write_checkpoint(result, 'diff', parameters.T,
                           XDMFFile.Encoding.HDF5, True)

    i = 1
    dt = parameters.T / parameters.M
    for k in range(parameters.M - 1, -1, -1):
        if k % parameters.save_interval != 0:
            continue
        f1.read_checkpoint(value_func1, 'value_func', i)
        f2.read_checkpoint(value_func2, 'value_func', i)
        result.vector()[:] \
            = value_func1.vector()[:] - value_func2.vector()[:]
        f_out.write_checkpoint(result, 'diff', k * dt, XDMFFile.Encoding.HDF5,
                               True)
        i += 1
예제 #4
0
def vedoPlotter(pygmsh_ms):

    # Reading mesh data stored in .xdmf files.
    mesh = Mesh()
    with XDMFFile(pygmsh_ms) as infile:
        infile.read(mesh)

    # Define variational problem
    V = FunctionSpace(mesh, 'P', 1)
    u = Function(V)

    R_path = "Output_data"
    fU_in = XDMFFile(os.path.join(R_path, 'FuelRod_m.xdmf'))
    fU_in.read_checkpoint(u, "T", 0)

    axes_opts = dict(
        xyGrid=True,
        axesLineWidth=1,
        xTickColor='black',
        yTickColor='black',
        xMinorTicks=1,  # number of minor ticks btw two major ticks
        yMinorTicks=1,  # number of minor ticks btw two major ticks
        xLabelSize=0.02,  # size of the numeric labels along axis
        yLabelSize=0.02,  # offset of numeric labels
    )
    # nipy_spectral, gnuplot
    plot(u,
         interactive=True,
         cmap='hot',
         axes=0,
         lw=2,
         scalarbar='vertical',
         wireframe=True,
         alpha=10.,
         warpZfactor=0.)  # warpZfactor=0.01
    plot()
예제 #5
0
def heston_transform(parameters,
                     get_error=False,
                     file_path=None,
                     plot_deltas=False):
    experiment = parameters.experiment
    domain_name = parameters.domain
    mesh_name = parameters.mesh

    mesh = Mesh()
    mesh_path = parameters.get_mesh_path()
    with XDMFFile(mesh_path) as f:
        f.read(mesh)

    # Create functions in function spaces defined by both original and
    # transformed meshes and copy value function data from one to another
    t_mesh_path = f'meshes/{domain_name}/t_{domain_name}_{mesh_name}.xdmf'
    if not os.path.exists(t_mesh_path):
        mesh2 = get_original_mesh(t_mesh_path, mesh, parameters)
    else:
        mesh2 = Mesh()
        with XDMFFile(t_mesh_path) as f:
            f.read(mesh2)

    V = FunctionSpace(mesh, 'CG', 1)
    t_V = FunctionSpace(mesh2, 'CG', 1)
    value = Function(V)
    control = Function(V)
    t_value = Function(t_V)
    t_control = Function(t_V)
    if not file_path:
        f = XDMFFile(f'out/{experiment}/{domain_name}/{mesh_name}/v.xdmf')
        t_f = XDMFFile(f'out/{experiment}/{domain_name}/{mesh_name}/t_v.xdmf')
    else:
        f = XDMFFile(file_path)
        path, file_name = os.path.split(file_path)
        t_f = XDMFFile(os.path.join(path, f't_{file_name}'))
    # Save Final Time Condition
    f.read_checkpoint(value, 'value_func', 0)
    t_value.vector()[:] = value.vector()[:]
    t_f.write_checkpoint(t_value, 'value_func', parameters.T)

    # Iterate through remaining timesteps (only some of them are
    #  saved to the file)
    i = 1
    parameters.calculate_save_interval()
    dt = parameters.T / parameters.M
    for k in range(parameters.M - 1, -1, -1):
        if k % parameters.save_interval != 0:
            continue
        f.read_checkpoint(value, 'value_func', i)
        f.read_checkpoint(control, 'control', i - 1)
        t_value.vector()[:] = value.vector()[:]
        t_control.vector()[:] = control.vector()[:]
        t_f.write_checkpoint(t_value, 'value_func', k * dt,
                             XDMFFile.Encoding.HDF5, True)
        t_f.write_checkpoint(t_control, 'control', k * dt,
                             XDMFFile.Encoding.HDF5, True)
        if plot_deltas:
            delta_S = project(t_value.dx(0), t_V)
            t_f.write_checkpoint(delta_S, 'delta_S', k * dt,
                                 XDMFFile.Encoding.HDF5, True)
            delta_v = project(t_value.dx(1), t_V)
            t_f.write_checkpoint(delta_v, 'delta_v', k * dt,
                                 XDMFFile.Encoding.HDF5, True)
        i += 1

        if k == 0 and get_error:
            error_calc(t_value, parameters.t_v_e, mesh2, f't_{mesh_name}',
                       f'out/{experiment}/{domain_name}/errors.json')
예제 #6
0
 def initial_condition_from_file(self, path_u, path_p):
     f_in = XDMFFile(path_u)
     f_in.read_checkpoint(self.u_, "f", 0)
     f_in = XDMFFile(path_p)
     f_in.read_checkpoint(self.p_, "f", 0)
     return