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 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 patched_truth_compute_output(self_): other_truth_problem.compute_output() assign(self.truth_problem._output, other_truth_problem._output) assign(self.truth_problem._output_over_time, other_truth_problem._output_over_time) return self.truth_problem._output_over_time
def get_residual_norm_squared(self): residual_norm_squared_over_time = TimeSeries(self._solution_over_time) assert len(self._solution_over_time) == len(self._solution_dot_over_time) for (k, t) in enumerate(self._solution_over_time.stored_times()): if not isclose(t, self.t0, self.dt/2.): # Set current time self.set_time(t) # Set current solution and solution_dot assign(self._solution, self._solution_over_time[k]) assign(self._solution_dot, self._solution_dot_over_time[k]) # Compute the numerator of the error bound at the current time, first # by computing residual of elliptic part elliptic_residual_norm_squared = ParabolicCoerciveRBReducedProblem_Base.get_residual_norm_squared(self) # ... and then adding terms related to time derivative N = self._solution.N theta_m = self.compute_theta("m") theta_a = self.compute_theta("a") theta_f = self.compute_theta("f") residual_norm_squared_over_time.append( elliptic_residual_norm_squared + 2.0*(transpose(self._solution_dot)*sum(product(theta_m, self.error_estimation_operator["m", "f"][:N], theta_f))) + 2.0*(transpose(self._solution_dot)*sum(product(theta_m, self.error_estimation_operator["m", "a"][:N, :N], theta_a))*self._solution) + transpose(self._solution_dot)*sum(product(theta_m, self.error_estimation_operator["m", "m"][:N, :N], theta_m))*self._solution_dot ) else: # Error estimator on initial condition does not use the residual residual_norm_squared_over_time.append(0.) return residual_norm_squared_over_time
def _solve(self): if self.multiply_by_theta: assert isinstance(self.term, str) # method untested otherwise O = sum(product(self.truth_problem.compute_theta(self.term), self.operator)) # noqa else: assert isinstance(self.term, tuple) # method untested otherwise assert len(self.operator) == 1 O = self.operator[0] # noqa assert len(self.inner_product) == 1 inner_product = self.inner_product[0] if self.truth_problem.dirichlet_bc is not None: dirichlet_bcs_sum = sum(product((0., )*len(self.truth_problem.dirichlet_bc), self.truth_problem.dirichlet_bc)) eigensolver = EigenSolver(self.truth_problem.V, O, inner_product, dirichlet_bcs_sum) else: eigensolver = EigenSolver(self.truth_problem.V, O, inner_product) eigensolver_parameters = dict() eigensolver_parameters["problem_type"] = "gen_hermitian" assert self.spectrum is "largest" or self.spectrum is "smallest" eigensolver_parameters["spectrum"] = self.spectrum + " real" if self.eigensolver_parameters is not None: if "spectral_transform" in self.eigensolver_parameters and self.eigensolver_parameters["spectral_transform"] == "shift-and-invert": eigensolver_parameters["spectrum"] = "target real" eigensolver_parameters.update(self.eigensolver_parameters) eigensolver.set_parameters(eigensolver_parameters) eigensolver.solve(1) r, c = eigensolver.get_eigenvalue(0) # real and complex part of the eigenvalue r_vector, c_vector = eigensolver.get_eigenvector(0) # real and complex part of the eigenvectors assert isclose(c, 0), "The required eigenvalue is not real" self._eigenvalue = r assign(self._eigenvector, r_vector)
def solve(self): problem = self.problem solver = TimeStepping(self, problem._solution, problem._solution_dot) solver.set_parameters(problem._time_stepping_parameters) (_, problem._solution_over_time, problem._solution_dot_over_time) = solver.solve() assign(problem._solution, problem._solution_over_time[-1]) assign(problem._solution_dot, problem._solution_dot_over_time[-1])
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): assert self.operator["stability_factor_left_hand_matrix"] is not None if self.expansion_index is None: A = sum(product( self.truth_problem.compute_theta("stability_factor_left_hand_matrix"), self.operator["stability_factor_left_hand_matrix"])) else: assert len(self.operator["stability_factor_left_hand_matrix"]) == 1 A = self.operator["stability_factor_left_hand_matrix"][0] assert self.operator["stability_factor_right_hand_matrix"] is not None assert len(self.operator["stability_factor_right_hand_matrix"]) == 1 B = self.operator["stability_factor_right_hand_matrix"][0] if self.dirichlet_bc is not None: dirichlet_bcs_sum = sum(product((0., ) * len(self.dirichlet_bc), self.dirichlet_bc)) eigensolver = EigenSolver(self.truth_problem.stability_factor_V, A, B, dirichlet_bcs_sum) else: eigensolver = EigenSolver(self.truth_problem.stability_factor_V, A, B) eigensolver_parameters = dict() assert self.spectrum == "largest" or self.spectrum == "smallest" eigensolver_parameters["spectrum"] = self.spectrum + " real" eigensolver_parameters.update(self.eigensolver_parameters) eigensolver.set_parameters(eigensolver_parameters) eigensolver.solve(1) r, c = eigensolver.get_eigenvalue(0) # real and complex part of the eigenvalue r_vector, c_vector = eigensolver.get_eigenvector(0) # real and complex part of the eigenvectors assert isclose(c, 0.), "The required eigenvalue is not real" self._eigenvalue = r assign(self._eigenvector, r_vector)
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 export_error(self, folder=None, filename=None, component=None, suffix=None, **kwargs): self.truth_problem.solve(**kwargs) assert len(self.truth_problem._solution_over_time) == len(self._solution_over_time) for (k, t) in enumerate(self.truth_problem._solution_over_time.stored_times()): self.set_time(t) assign(self._solution, self._solution_over_time[k]) assign(self.truth_problem._solution, self.truth_problem._solution_over_time[k]) assert suffix is None self.truth_problem.export_solution(folder, filename, self.truth_problem._solution - self.basis_functions[:self._solution.N]*self._solution, component=component, suffix=k)
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 _compute_relative_error(self, absolute_error_over_time, **kwargs): relative_error_over_time = TimeSeries(self._solution_over_time) assert len(self._solution_over_time) == len(absolute_error_over_time) for (k, t) in enumerate(self.truth_problem._solution_over_time.stored_times()): self.set_time(t) assign(self.truth_problem._solution, self.truth_problem._solution_over_time[k]) absolute_error = self._convert_error_at_time(k, absolute_error_over_time) relative_error = ParametrizedReducedDifferentialProblem_DerivedClass._compute_relative_error(self, absolute_error, **kwargs) relative_error_over_time.append(relative_error) relative_error_over_time = self._convert_error_over_time(relative_error_over_time) return relative_error_over_time
def _compute_error(self, **kwargs): error_over_time = list() for (k, (truth_solution, reduced_solution)) in enumerate( zip(self.truth_problem._solution_over_time, self._solution_over_time)): self.set_time(k * self.dt) assign(self._solution, reduced_solution) assign(self.truth_problem._solution, truth_solution) error = ParametrizedReducedDifferentialProblem_DerivedClass._compute_error( self, **kwargs) error_over_time.append(error) error_over_time = self._convert_error_over_time(error_over_time) return error_over_time
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(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) # will also add to cache delattr(self, "_is_solving") return self._solution
def patched_truth_solve(self_, **kwargs_): other_truth_problem.solve(**kwargs_) assign(self.truth_problem._solution, other_truth_problem._solution) assign(self.truth_problem._solution_dot, other_truth_problem._solution_dot) assign(self.truth_problem._solution_over_time, other_truth_problem._solution_over_time) assign(self.truth_problem._solution_dot_over_time, other_truth_problem._solution_dot_over_time) return self.truth_problem._solution_over_time
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 _compute_relative_error(self, absolute_error_over_time, **kwargs): relative_error_over_time = list() for (k, truth_solution) in enumerate( self.truth_problem._solution_over_time): self.set_time(k * self.dt) assign(self.truth_problem._solution, truth_solution) absolute_error = self._convert_error_at_time( k, absolute_error_over_time) relative_error = ParametrizedReducedDifferentialProblem_DerivedClass._compute_relative_error( self, absolute_error, **kwargs) relative_error_over_time.append(relative_error) relative_error_over_time = self._convert_error_over_time( relative_error_over_time) return relative_error_over_time
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_over_time.clear() self._solution_dot = OnlineFunction(N) self._solution_dot_over_time.clear() if N == 0: # trivial case self._solution_over_time.extend([ self._solution for _ in self._solution_over_time.expected_times() ]) self._solution_dot_over_time.extend([ self._solution_dot for _ in self._solution_dot_over_time.expected_times() ]) 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") 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_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 compute_output(self): N = self._solution.N kwargs = self._latest_solve_kwargs try: assign(self._output_over_time, self._output_over_time_cache[self.mu, N, kwargs]) # **kwargs is not supported by __getitem__ except KeyError: try: self._compute_output(N) except ValueError: # raised by compute_theta if output computation is optional self._output_over_time.clear() self._output_over_time.extend([NotImplemented]*len(self._solution_over_time)) self._output = NotImplemented self._output_over_time_cache[self.mu, N, kwargs] = self._output_over_time else: self._output = self._output_over_time[-1] return self._output_over_time
def _solve(self, N, **kwargs): assert self.operator["stability_factor_left_hand_matrix"] is not None A = sum(product(self.reduced_problem.compute_theta("stability_factor_left_hand_matrix"), self.operator["stability_factor_left_hand_matrix"][:N, :N])) assert self.operator["stability_factor_right_hand_matrix"] is not None assert len(self.operator["stability_factor_right_hand_matrix"]) == 1 B = self.operator["stability_factor_right_hand_matrix"][0][:N, :N] eigensolver = OnlineEigenSolver(self.reduced_problem.stability_factor_basis_functions, A, B) eigensolver_parameters = dict() assert self.spectrum == "largest" or self.spectrum == "smallest" eigensolver_parameters["spectrum"] = self.spectrum + " real" eigensolver_parameters.update(self.eigensolver_parameters) eigensolver.set_parameters(eigensolver_parameters) eigensolver.solve(1) r, c = eigensolver.get_eigenvalue(0) # real and complex part of the eigenvalue r_vector, c_vector = eigensolver.get_eigenvector(0) # real and complex part of the eigenvectors assert isclose(c, 0.), "The required eigenvalue is not real" self._eigenvalue = r assign(self._eigenvector, r_vector)
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): """ 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 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 _assign(object_to: TimeSeries, object_from: TimeSeries): if object_from is not object_to: from rbnics.backends import assign assign(object_to._list, object_from._list)
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, **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 patched_truth_solve(self_, **kwargs_): other_truth_problem.solve(**kwargs_) assign(self.truth_problem._solution, other_truth_problem._solution) return self.truth_problem._solution