Ejemplo n.º 1
0
    def __init__(self, functional, controls, solver, parameters):
        # For consistency with the dolfin-adjoint API.
        self.scale = parameters.scale

        self.solver = solver
        if not isinstance(solver, Solver):
            raise ValueError, "solver argument of wrong type."

        self.functional = functional
        if not isinstance(functional, PrototypeFunctional):
            raise ValueError, "invalid functional argument."

        # Create the default parameters
        self.parameters = parameters

        # Hidden attributes
        self._solver_params = solver.parameters
        self._problem_params = solver.problem.parameters
        self._time_integrator = None
        self._automatic_scaling_factor = None

        # Caching variables that store which controls the last forward run was
        # performed
        self.last_m = None
        if self.solver.parameters.dump_period > 0:
            turbine_filename = os.path.join(solver.parameters.output_dir,
                                            "turbines.pvd")
            self.turbine_file = File(turbine_filename, "compressed")

            if self._solver_params.output_turbine_power:
                power_filename = os.path.join(solver.parameters.output_dir,
                                              "power.pvd")
                self.power_file = File(power_filename, "compressed")

        # dolfin-adjoint requires the ReducedFunctional to have a member
        # variable `parameter` which must be a list comprising an instance of a
        # class (here, TurbineFarmControl) which has a method named `data`
        # which returns a numpy.ndarray of the parameters used for optimisation,
        # e.g. the turbine frictions and positions.
        if not hasattr(controls, "__getitem__"):
            controls = [controls]
        self.controls = controls

        self._compute_functional_mem = MemoizeMutable(self._compute_functional)
        self._compute_gradient_mem = MemoizeMutable(self._compute_gradient)

        # Load checkpoints from file
        if self.parameters.load_checkpoints:
            self._load_checkpoint()

        if self._solver_params.print_individual_turbine_power:
            # if this is enabled, we need to instantiate the relevant helper
            # and pass this to the solver
            output_writer = helpers.OutputWriter(self.functional)
            self._solver_params.output_writer = output_writer
Ejemplo n.º 2
0
    def __init__(self, functional, controls, solver, parameters):
        # For consistency with the dolfin-adjoint API.
        self.scale = parameters.scale
        self.rf = self
        self.solver = solver
        if not isinstance(solver, Solver):
            raise ValueError, "solver argument of wrong type."

        self.functional = functional
        if not isinstance(functional, PrototypeFunctional):
            raise ValueError, "invalid functional argument."

        # Create the default parameters
        self.parameters = parameters

        # Hidden attributes
        self._solver_params = solver.parameters
        self._problem_params = solver.problem.parameters
        self._time_integrator = None
        self._automatic_scaling_factor = None

        # For storing the friction function for each time step as one changing
        # function and not as multiple functions
        if (isinstance(self.solver.problem.parameters.tidal_farm.\
                       friction_function, list)):
            self._friction_plot_function = self.solver.problem.parameters\
                                           .tidal_farm.friction_function[0]
        else:
            self._friction_plot_function = self.solver.problem.parameters\
                                           .tidal_farm.friction_function

        # Caching variables that store which controls the last forward run was
        # performed
        self.last_m = None
        if self.solver.parameters.dump_period > 0:
            turbine_filename = os.path.join(solver.parameters.output_dir, "turbines.pvd")
            self.turbine_file = File(turbine_filename, "compressed")

            if self._solver_params.output_turbine_power:
                power_filename = os.path.join(solver.parameters.output_dir, "power.pvd")
                self.power_file = File(power_filename, "compressed")

            # Just to clean any 'j.txt' files from previous simulations.
            if self._solver_params.output_j:
                dir = os.path.join(self.solver.parameters.output_dir,
                                   "iter_{}".format(self.solver.optimisation_iteration))
                if not os.path.exists(dir):
                    os.mkdir(dir)
                filename = os.path.join(dir, "j.txt")
                j_file = open(filename, 'w')
                j_file.close()

        # dolfin-adjoint requires the ReducedFunctional to have a member
        # variable `parameter` which must be a list comprising an instance of a
        # class (here, TurbineFarmControl) which has a method named `data`
        # which returns a numpy.ndarray of the parameters used for optimisation,
        # e.g. the turbine frictions and positions.
        if not hasattr(controls, "__getitem__"):
            controls = [controls]
        self.controls = controls

        self._compute_functional_mem = MemoizeMutable(self._compute_functional)
        self._compute_gradient_mem = MemoizeMutable(self._compute_gradient)

        # Load checkpoints from file
        if self.parameters.load_checkpoints:
            self._load_checkpoint()

        if (self._solver_params.print_individual_turbine_power
            or ((self.solver.parameters.dump_period > 0)
            and self._solver_params.output_turbine_power)):
            # if this is enabled, we need to instantiate the relevant helper
            # and pass this to the solver
            output_writer = helpers.OutputWriter(self.functional)
            self._solver_params.output_writer = output_writer