def solve(self, **kwargs): """ Perform a truth solve in case no precomputed solution is imported. """ (cache_key, cache_file) = self._cache_key_and_file_from_kwargs(**kwargs) if "RAM" in self.cache_config and cache_key in self._solution_cache: log(PROGRESS, "Loading truth solution from cache") assign(self._solution, self._solution_cache[cache_key]) elif "Disk" in self.cache_config and self.import_solution( self.folder["cache"], cache_file): log(PROGRESS, "Loading truth solution from file") if "RAM" in self.cache_config: self._solution_cache[cache_key] = copy(self._solution) else: # No precomputed solution available. Truth solve is performed. log(PROGRESS, "Solving truth problem") assert not hasattr(self, "_is_solving") self._is_solving = True assign(self._solution, Function(self.V)) self._solve(**kwargs) delattr(self, "_is_solving") if "RAM" in self.cache_config: self._solution_cache[cache_key] = copy(self._solution) self.export_solution( self.folder["cache"], cache_file ) # Note that we export to file regardless of config options, because they may change across different runs return self._solution
def solve(self, N=None, **kwargs): N, kwargs = self._online_size_from_kwargs(N, **kwargs) N += self.N_bc self._solution = OnlineFunction(N) self._solution_dot = OnlineFunction(N) cache_key = self._cache_key_from_N_and_kwargs(N, **kwargs) assert ((cache_key in self._solution_cache) == (cache_key in self._solution_dot_cache) == (cache_key in self._solution_over_time_cache) == (cache_key in self._solution_dot_over_time_cache)) if "RAM" in self.cache_config and cache_key in self._solution_cache: log(PROGRESS, "Loading reduced solution from cache") assign(self._solution, self._solution_cache[cache_key]) assign(self._solution_dot, self._solution_dot_cache[cache_key]) assign(self._solution_over_time, self._solution_over_time_cache[cache_key]) assign(self._solution_dot_over_time, self._solution_dot_over_time_cache[cache_key]) else: log(PROGRESS, "Solving reduced problem") assert not hasattr(self, "_is_solving") self._is_solving = True self._solve(N, **kwargs) delattr(self, "_is_solving") if "RAM" in self.cache_config: self._solution_cache[cache_key] = copy(self._solution) self._solution_dot_cache[cache_key] = copy( self._solution_dot) self._solution_over_time_cache[cache_key] = copy( self._solution_over_time) self._solution_dot_over_time_cache[cache_key] = copy( self._solution_dot_over_time) return self._solution_over_time
def solve_adjoint_supremizer(self, solution): (cache_key, cache_file) = self._supremizer_cache_key_and_file() if "RAM" in self.cache_config and cache_key in self._adjoint_supremizer_cache: log(PROGRESS, "Loading adjoint supremizer from cache") assign(self._adjoint_supremizer, self._adjoint_supremizer_cache[cache_key]) elif "Disk" in self.cache_config and self.import_supremizer( self.folder["cache"], cache_file, self._adjoint_supremizer, component="r"): log(PROGRESS, "Loading adjoint supremizer from file") if "RAM" in self.cache_config: self._adjoint_supremizer_cache[cache_key] = copy( self._adjoint_supremizer) else: # No precomputed adjoint supremizer available. Truth adjoint supremizer solve is performed. log(PROGRESS, "Solving adjoint supremizer problem") self._solve_adjoint_supremizer(solution) if "RAM" in self.cache_config: self._adjoint_supremizer_cache[cache_key] = copy( self._adjoint_supremizer) self.export_supremizer( self.folder["cache"], cache_file, self._adjoint_supremizer, component="r" ) # Note that we export to file regardless of config options, because they may change across different runs return self._adjoint_supremizer
def monitor(self, t, solution, solution_dot): problem = self.problem solution_copy = copy(solution) problem._solution_over_time.append(solution_copy) problem._solution_over_time_cache[problem.mu, self.N, self.kwargs].append(solution_copy) solution_dot_copy = copy(solution_dot) problem._solution_dot_over_time.append(solution_dot_copy) problem._solution_dot_over_time_cache[problem.mu, self.N, self.kwargs].append(solution_dot_copy)
def solve(self): problem = self.problem assert len(problem._solution_over_time) == 0 problem._solution_over_time_cache[problem.mu, self.N, self.kwargs] = copy(problem._solution_over_time) assert len(problem._solution_dot_over_time) == 0 problem._solution_dot_over_time_cache[ problem.mu, self.N, self.kwargs] = copy(problem._solution_dot_over_time) solver = OnlineTimeStepping(self, problem._solution, problem._solution_dot) solver.set_parameters(problem._time_stepping_parameters) solver.solve()
def solve(self): problem = self.problem problem._solution_over_time_cache[ problem.mu, self.kwargs] = copy(problem._solution_over_time) problem._solution_dot_over_time_cache[ problem.mu, self.kwargs] = copy(problem._solution_dot_over_time) solver = TimeStepping(self, problem._solution, problem._solution_dot) solver.set_parameters(problem._time_stepping_parameters) solver.solve()
def evaluate_parametrized_expression(self): (cache_key, cache_file) = self._cache_key_and_file() if "RAM" in self.cache_config and cache_key in self.snapshot_cache: self.snapshot = self.snapshot_cache[cache_key] elif "Disk" in self.cache_config and self.import_solution( self.folder["cache"], cache_file): if "RAM" in self.cache_config: self.snapshot_cache[cache_key] = copy(self.snapshot) else: self.snapshot = evaluate(self.parametrized_expression) if "RAM" in self.cache_config: self.snapshot_cache[cache_key] = copy(self.snapshot) self.export_solution( self.folder["cache"], cache_file ) # Note that we export to file regardless of config options, because they may change across different runs
def _supremizer_cache_import_impl(filename): supremizer = copy(self._supremizer[component]) self.import_supremizer(self.folder["cache"], filename, supremizer, component=component) return supremizer
def solve(self, N=None, **kwargs): """ Perform an online solve. self.N will be used as matrix dimension if the default value is provided for N. :param N : Dimension of the reduced problem :type N : integer :return: reduced solution """ N, kwargs = self._online_size_from_kwargs(N, **kwargs) N += self.N_bc self._latest_solve_kwargs = kwargs self._solution = OnlineFunction(N) if N == 0: # trivial case return self._solution try: assign(self._solution, self._solution_cache[ self.mu, N, kwargs]) # **kwargs is not supported by __getitem__ except KeyError: assert not hasattr(self, "_is_solving") self._is_solving = True self._solve(N, **kwargs) delattr(self, "_is_solving") self._solution_cache[self.mu, N, kwargs] = copy(self._solution) return self._solution
def copy(time_series): from rbnics.backends import copy time_series_copy = TimeSeries(time_series._time_interval, time_series._time_step_size) if len(time_series._list) > 0: time_series_copy._list = copy(time_series._list) return time_series_copy
def import_solution(self, folder=None, filename=None, solution_over_time=None, component=None, suffix=None): if folder is None: folder = self.folder_prefix if filename is None: filename = "solution" if solution_over_time is None: solution_over_time = self._solution_over_time if isinstance(solution_over_time, AbstractTimeSeries): solution = Function(self.V) assert suffix is None solution_over_time.clear() for (k, _) in enumerate( self._solution_over_time.expected_times()): ParametrizedDifferentialProblem_DerivedClass.import_solution( self, folder, filename, solution, component, suffix=k) solution_over_time.append(copy(solution)) else: # Used only for cache import solution = solution_over_time assert suffix is not None ParametrizedDifferentialProblem_DerivedClass.import_solution( self, folder, filename, solution, component=component, suffix=suffix)
def solve_supremizer(self, solution): kwargs = self._latest_solve_kwargs try: assign(self._supremizer, self._supremizer_cache[self.mu, kwargs]) # **kwargs is not supported by __getitem__ except KeyError: self._solve_supremizer(solution) self._supremizer_cache[self.mu, kwargs] = copy(self._supremizer) return self._supremizer
def solve(self): cache_key = self._cache_key() try: self._eigenvalue = self._eigenvalue_cache[cache_key] assign(self._eigenvector, self._eigenvector_cache[cache_key]) except KeyError: self._solve() self._eigenvalue_cache[cache_key] = self._eigenvalue self._eigenvector_cache[cache_key] = copy(self._eigenvector) return (self._eigenvalue, self._eigenvector)
def solve(self, N=None, **kwargs): N, kwargs = self.reduced_problem._online_size_from_kwargs(N, **kwargs) N += self.reduced_problem.N_bc self._eigenvector = OnlineFunction(N) cache_key = self._cache_key(**kwargs) try: self._eigenvalue = self._eigenvalue_cache[cache_key] assign(self._eigenvector, self._eigenvector_cache[cache_key]) except KeyError: self._solve(N, **kwargs) self._eigenvalue_cache[cache_key] = self._eigenvalue self._eigenvector_cache[cache_key] = copy(self._eigenvector) return (self._eigenvalue, self._eigenvector)
def solve_supremizer(self, solution): N_us = OnlineSizeDict(solution.N) # create a copy del N_us["p"] cache_key = self._supremizer_cache_key_from_N_and_kwargs(N_us) self._supremizer = OnlineFunction(N_us) if "RAM" in self.cache_config and cache_key in self._supremizer_cache: log(PROGRESS, "Loading reduced supremizer from cache") assign(self._supremizer, self._supremizer_cache[cache_key]) else: log(PROGRESS, "Solving supremizer reduced problem") self._solve_supremizer(solution) if "RAM" in self.cache_config: self._supremizer_cache[cache_key] = copy(self._supremizer) return self._supremizer
def solve_supremizer(self, solution): N_us = OnlineSizeDict(solution.N) # create a copy del N_us["p"] kwargs = self._latest_solve_kwargs self._supremizer = OnlineFunction(N_us) try: assign(self._supremizer, self._supremizer_cache[ self.mu, N_us, kwargs]) # **kwargs is not supported by __getitem__ except KeyError: self._solve_supremizer(solution) self._supremizer_cache[self.mu, N_us, kwargs] = copy(self._supremizer) return self._supremizer
def solve(self, **kwargs): (cache_key, cache_file) = self._cache_key_and_file_from_kwargs(**kwargs) assert ( (cache_key in self._solution_cache) == (cache_key in self._solution_dot_cache) == (cache_key in self._solution_over_time_cache) == (cache_key in self._solution_dot_over_time_cache) ) if "RAM" in self.cache_config and cache_key in self._solution_cache: log(PROGRESS, "Loading truth solution from cache") assign(self._solution, self._solution_cache[cache_key]) assign(self._solution_dot, self._solution_dot_cache[cache_key]) assign(self._solution_over_time, self._solution_over_time_cache[cache_key]) assign(self._solution_dot_over_time, self._solution_dot_over_time_cache[cache_key]) elif "Disk" in self.cache_config and ( self.import_solution(self.folder["cache"], cache_file + "_solution", self._solution_over_time) and self.import_solution(self.folder["cache"], cache_file + "_solution_dot", self._solution_dot_over_time) ): log(PROGRESS, "Loading truth solution from file") assign(self._solution, self._solution_over_time[-1]) assign(self._solution_dot, self._solution_dot_over_time[-1]) if "RAM" in self.cache_config: self._solution_cache[cache_key] = copy(self._solution) self._solution_dot_cache[cache_key] = copy(self._solution_dot) self._solution_over_time_cache[cache_key] = copy(self._solution_over_time) self._solution_dot_over_time_cache[cache_key] = copy(self._solution_dot_over_time) else: log(PROGRESS, "Solving truth problem") assert not hasattr(self, "_is_solving") self._is_solving = True assign(self._solution, Function(self.V)) assign(self._solution_dot, Function(self.V)) self._solve(**kwargs) delattr(self, "_is_solving") if "RAM" in self.cache_config: self._solution_cache[cache_key] = copy(self._solution) self._solution_dot_cache[cache_key] = copy(self._solution_dot) self._solution_over_time_cache[cache_key] = copy(self._solution_over_time) self._solution_dot_over_time_cache[cache_key] = copy(self._solution_dot_over_time) # Note that we export to file regardless of config options, because they may change across different runs self.export_solution(self.folder["cache"], cache_file + "_solution", self._solution_over_time) self.export_solution(self.folder["cache"], cache_file + "_solution_dot", self._solution_dot_over_time) return self._solution_over_time
def solve(self): (cache_key, cache_file) = self._cache_key_and_file() assert ( (cache_key in self._eigenvalue_cache) == (cache_key in self._eigenvector_cache) ) if "RAM" in self.cache_config and cache_key in self._eigenvalue_cache: log(PROGRESS, "Loading coercivity constant from cache") self._eigenvalue = self._eigenvalue_cache[cache_key] assign(self._eigenvector, self._eigenvector_cache[cache_key]) elif "Disk" in self.cache_config and self.import_solution(self.folder["cache"], cache_file): log(PROGRESS, "Loading coercivity constant from file") if "RAM" in self.cache_config: self._eigenvalue_cache[cache_key] = self._eigenvalue self._eigenvector_cache[cache_key] = copy(self._eigenvector) else: # No precomputed solution available. Truth solve is performed. log(PROGRESS, "Solving coercivity constant eigenproblem") self._solve() if "RAM" in self.cache_config: self._eigenvalue_cache[cache_key] = self._eigenvalue self._eigenvector_cache[cache_key] = copy(self._eigenvector) self.export_solution(self.folder["cache"], cache_file) # Note that we export to file regardless of config options, because they may change across different runs return (self._eigenvalue, self._eigenvector)
def solve(self, N=None, **kwargs): N, kwargs = self._online_size_from_kwargs(N, **kwargs) N += self.N_bc self._latest_solve_kwargs = kwargs self._solution = OnlineFunction(N) self._solution_dot = OnlineFunction(N) if N == 0: # trivial case time_interval = linspace(self.t0, self.T, (self.T - self.t0) / self.dt) self._solution_over_time = [ self._solution for _ in time_interval ] self._solution_dot_over_time = [ self._solution_dot for _ in time_interval ] return self._solution_over_time try: assign(self._solution_over_time, self._solution_over_time_cache[ self.mu, N, kwargs]) # **kwargs is not supported by __getitem__ assign(self._solution_dot_over_time, self._solution_dot_over_time_cache[self.mu, N, kwargs]) except KeyError: assert not hasattr(self, "_is_solving") self._is_solving = True self._solve(N, **kwargs) delattr(self, "_is_solving") self._solution_over_time_cache[self.mu, N, kwargs] = copy( self._solution_over_time) self._solution_dot_over_time_cache[self.mu, N, kwargs] = copy( self._solution_dot_over_time) else: assign(self._solution, self._solution_over_time[-1]) assign(self._solution_dot, self._solution_dot_over_time[-1]) return self._solution_over_time
def solve(self, **kwargs): self._latest_solve_kwargs = kwargs try: assign(self._solution_over_time, self._solution_over_time_cache[ self.mu, kwargs]) # **kwargs is not supported by __getitem__ assign(self._solution_dot_over_time, self._solution_dot_over_time_cache[self.mu, kwargs]) except KeyError: assert not hasattr(self, "_is_solving") self._is_solving = True assign(self._solution, Function(self.V)) assign(self._solution_dot, Function(self.V)) self._solve(**kwargs) delattr(self, "_is_solving") self._solution_over_time_cache[self.mu, kwargs] = copy( self._solution_over_time) self._solution_dot_over_time_cache[self.mu, kwargs] = copy( self._solution_dot_over_time) else: assign(self._solution, self._solution_over_time[-1]) assign(self._solution_dot, self._solution_dot_over_time[-1]) return self._solution_over_time
def compute_maximum_interpolation_error(self, N=None): if N is None: N = self.N # Compute the error (difference with the eim approximation) if N > 0: error = self.snapshot - self.basis_functions[:N]*self._interpolation_coefficients else: error = copy(self.snapshot) # need a copy because it will be rescaled # Get the location of the maximum error (maximum_error, maximum_location) = max(abs(error)) # Return return (error, maximum_error, maximum_location)
def solve(self, **kwargs): """ Perform a truth solve in case no precomputed solution is imported. """ self._latest_solve_kwargs = kwargs try: assign(self._solution, self._solution_cache[ self.mu, kwargs]) # **kwargs is not supported by __getitem__ except KeyError: assert not hasattr(self, "_is_solving") self._is_solving = True assign(self._solution, Function(self.V)) self._solve(**kwargs) delattr(self, "_is_solving") self._solution_cache[self.mu, kwargs] = copy(self._solution) return self._solution
def import_solution(self, folder=None, filename=None, solution_over_time=None, component=None, suffix=None): if folder is None: folder = self.folder_prefix if filename is None: filename = "solution" solution = Function(self.V) if solution_over_time is None: solution_over_time = self._solution_over_time assert suffix is None solution_over_time.clear() for (k, _) in enumerate(self._solution_over_time.expected_times()): ParametrizedDifferentialProblem_DerivedClass.import_solution( self, folder, filename, solution, component, suffix=k) solution_over_time.append(copy(solution))
def import_solution(self, folder=None, filename=None, solution_over_time=None, component=None, suffix=None): if folder is None: folder = self.folder_prefix if filename is None: filename = "solution" solution = Function(self.V) if solution_over_time is None: solution_over_time = self._solution_over_time assert suffix is None k = 0 self.t = 0 del solution_over_time[:] while self.t <= self.T: import_solution = ParametrizedDifferentialProblem_DerivedClass.import_solution(self, folder, filename, solution, component, suffix=k) if import_solution: solution_over_time.append(copy(solution)) k += 1 self.t += self.dt else: return False return True
def solve(self, N=None, **kwargs): """ Perform an online solve. self.N will be used as matrix dimension if the default value is provided for N. :param N : Dimension of the reduced problem :type N : integer :return: reduced solution """ N, kwargs = self._online_size_from_kwargs(N, **kwargs) N += self.N_bc cache_key = self._cache_key_from_N_and_kwargs(N, **kwargs) self._solution = OnlineFunction(N) if "RAM" in self.cache_config and cache_key in self._solution_cache: log(PROGRESS, "Loading reduced solution from cache") assign(self._solution, self._solution_cache[cache_key]) else: log(PROGRESS, "Solving reduced problem") assert not hasattr(self, "_is_solving") self._is_solving = True self._solve(N, **kwargs) delattr(self, "_is_solving") if "RAM" in self.cache_config: self._solution_cache[cache_key] = copy(self._solution) return self._solution
def _supremizer_cache_import(filename): supremizer = copy(self._supremizer) self.import_supremizer(self.folder["cache"], filename, supremizer) return supremizer
def solve_supremizer(self, solution): return copy(AbstractCFDUnsteadyProblem_Base.solve_supremizer(self, solution))
def evaluate_parametrized_expression(self): try: assign(self.snapshot, self._snapshot_cache[self.mu]) except KeyError: self.snapshot = evaluate(self.parametrized_expression) self._snapshot_cache[self.mu] = copy(self.snapshot)
def monitor(self, solution): problem = self.problem problem._solution_cache[problem.mu, self.kwargs] = copy(solution)
def _solution_cache_import(filename): solution = copy(self._solution) self.import_solution(self.folder["cache"], filename, solution) return solution