def greedy(self): assert self.EIM_approximation.basis_generation == "Greedy" # Print some additional information on the consistency of the reduced basis if self.EIM_approximation.N > 0: # skip during initialization self.EIM_approximation.solve() self.EIM_approximation.snapshot = self.load_snapshot() error = self.EIM_approximation.snapshot - self.EIM_approximation.basis_functions * self.EIM_approximation._interpolation_coefficients error_on_interpolation_locations = evaluate( error, self.EIM_approximation.interpolation_locations) (maximum_error, _) = max(abs(error)) (maximum_error_on_interpolation_locations, _) = max(abs(error_on_interpolation_locations) ) # for consistency check, should be zero print("interpolation error for current mu =", abs(maximum_error)) print( "interpolation error on interpolation locations for current mu =", abs(maximum_error_on_interpolation_locations)) # Carry out the actual greedy search def solve_and_computer_error(mu): self.EIM_approximation.set_mu(mu) self.EIM_approximation.solve() self.EIM_approximation.snapshot = self.load_snapshot() (_, maximum_error, _) = self.EIM_approximation.compute_maximum_interpolation_error() return abs(maximum_error) if self.EIM_approximation.N == 0: print("find initial mu") else: print("find next mu") (error_max, error_argmax) = self.training_set.max(solve_and_computer_error) self.EIM_approximation.set_mu(self.training_set[error_argmax]) self.greedy_selected_parameters.append(self.training_set[error_argmax]) self.greedy_selected_parameters.save(self.folder["post_processing"], "mu_greedy") self.greedy_errors.append(error_max) self.greedy_errors.save(self.folder["post_processing"], "error_max") if abs(self.greedy_errors[0]) > 0.: return (abs(error_max), abs(error_max / self.greedy_errors[0])) else: # Trivial case, greedy should stop after one iteration after having store a zero basis function assert len(self.greedy_errors) in (1, 2) if len(self.greedy_errors) is 1: assert self.EIM_approximation.N == 0 # Tweak the tolerance to force getting in the greedy loop self.tol = -1. elif len(self.greedy_errors) is 2: assert error_max == 0. assert self.EIM_approximation.N == 1 # Tweak back the tolerance to force getting out of the greedy loop assert self.tol == -1. self.tol = 1. return (0., 0.)
def compute_maximum_interpolation_relative_error(self, N=None): (absolute_error, maximum_absolute_error, maximum_location) = self.compute_maximum_interpolation_error(N) (maximum_snapshot_value, _) = max(abs(self.snapshot)) return (absolute_error / maximum_snapshot_value, maximum_absolute_error / maximum_snapshot_value, maximum_location)
def _solve(self, rhs_, N=None): if N is None: N = self.N if N > 0: self._interpolation_coefficients = OnlineFunction(N) # Evaluate the parametrized expression at interpolation locations rhs = evaluate(rhs_, self.interpolation_locations[:N]) (max_abs_rhs, _) = max(abs(rhs)) if max_abs_rhs == 0.: # If the rhs is zero, then we are interpolating the zero function # and the default zero coefficients are enough. pass else: # Extract the interpolation matrix lhs = self.interpolation_matrix[0][:N, :N] # Solve the interpolation problem solver = OnlineLinearSolver(lhs, self._interpolation_coefficients, rhs) solver.solve() else: self._interpolation_coefficients = None # OnlineFunction
def compute_maximum_interpolation_relative_error(self, N=None): (absolute_error, maximum_absolute_error, maximum_location) = self.compute_maximum_interpolation_error(N) (maximum_snapshot_value, _) = max(abs(self.snapshot)) if maximum_snapshot_value != 0.: return (absolute_error/maximum_snapshot_value, maximum_absolute_error/maximum_snapshot_value, maximum_location) else: if maximum_absolute_error == 0.: return (absolute_error, maximum_absolute_error, maximum_location) # the first two arguments are a zero expression and zero scalar else: return (None, float("NaN"), maximum_location) # the first argument should be a NaN expression
def greedy(self): assert self.EIM_approximation.basis_generation == "Greedy" # Print some additional information on the consistency of the reduced basis self.EIM_approximation.solve() self.EIM_approximation.snapshot = self.load_snapshot() error = self.EIM_approximation.snapshot - self.EIM_approximation.basis_functions*self.EIM_approximation._interpolation_coefficients error_on_interpolation_locations = evaluate(error, self.EIM_approximation.interpolation_locations) (maximum_error, _) = max(abs(error)) (maximum_error_on_interpolation_locations, _) = max(abs(error_on_interpolation_locations)) # for consistency check, should be zero print("interpolation error for current mu =", abs(maximum_error)) print("interpolation error on interpolation locations for current mu =", abs(maximum_error_on_interpolation_locations)) # Carry out the actual greedy search def solve_and_computer_error(mu): self.EIM_approximation.set_mu(mu) self.EIM_approximation.solve() self.EIM_approximation.snapshot = self.load_snapshot() (_, maximum_error, _) = self.EIM_approximation.compute_maximum_interpolation_error() return abs(maximum_error) print("find next mu") (error_max, error_argmax) = self.training_set.max(solve_and_computer_error) self.EIM_approximation.set_mu(self.training_set[error_argmax]) self.greedy_selected_parameters.append(self.training_set[error_argmax]) self.greedy_selected_parameters.save(self.folder["post_processing"], "mu_greedy") self.greedy_errors.append(error_max) self.greedy_errors.save(self.folder["post_processing"], "error_max") if abs(self.greedy_errors[0]) > 0.: return (abs(error_max), abs(error_max/self.greedy_errors[0])) else: # Trivial case, greedy will stop at the first iteration assert len(self.greedy_errors) == 1 assert self.EIM_approximation.N == 1 return (0., 0.)
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)