def evaluate(self): from rbnics.backends import evaluate, transpose args = self._arg._args assert len(args) in (2, 3) assert isinstance(args[0], AbstractBasisFunctionsMatrix) assert isinstance(args[1], AbstractParametrizedTensorFactory) if len(args) is 2: output = transpose(args[0]) * evaluate(args[1]) elif len(args) is 3: assert isinstance(args[2], AbstractBasisFunctionsMatrix) output = transpose(args[0]) * evaluate(args[1]) * args[2] else: raise ValueError("Invalid argument") assert not isinstance(output, DelayedTranspose) return output
def solve(self): from rbnics.backends import AffineExpansionStorage, evaluate, LinearSolver, product, sum assert self._lhs is not None assert self._solution is not None assert isinstance(self._rhs, DelayedSum) thetas = list() operators = list() for addend in self._rhs._args: assert isinstance(addend, DelayedProduct) assert len(addend._args) in (2, 3) assert isinstance(addend._args[0], Number) assert isinstance(addend._args[1], AbstractParametrizedTensorFactory) thetas.append(addend._args[0]) if len(addend._args) is 2: operators.append(addend._args[1]) elif len(addend._args) is 3: operators.append(addend._args[1] * addend._args[2]) else: raise ValueError("Invalid addend") thetas = tuple(thetas) operators = AffineExpansionStorage( tuple(evaluate(op) for op in operators)) rhs = sum(product(thetas, operators)) solver = LinearSolver(self._lhs, self._solution, rhs, self._bcs) solver.set_parameters(self._parameters) solver.solve()
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 update_interpolation_matrix(self): self.EIM_approximation.interpolation_matrix[0] = evaluate( self.EIM_approximation.basis_functions[:self.EIM_approximation.N], self.EIM_approximation.interpolation_locations) self.EIM_approximation.interpolation_matrix.save( self.EIM_approximation.folder["reduced_operators"], "interpolation_matrix")
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 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 __call__(self, thetas, operators, thetas2): from rbnics.backends import evaluate, product, sum, transpose assert operators._type in ("error_estimation_operators_11", "error_estimation_operators_21", "error_estimation_operators_22", "operators") if operators._type.startswith("error_estimation_operators"): assert operators.order() is 2 assert thetas2 is not None assert "inner_product_matrix" in operators._content assert "delayed_functions" in operators._content delayed_functions = operators._content["delayed_functions"] assert len(delayed_functions) is 2 output = transpose(sum(product(thetas, delayed_functions[0])))*operators._content["inner_product_matrix"]*sum(product(thetas2, delayed_functions[1])) # Return assert not isinstance(output, DelayedTranspose) return ProductOutput(output) elif operators._type == "operators": assert operators.order() is 1 assert thetas2 is None assert "truth_operators_as_expansion_storage" in operators._content sum_product_truth_operators = sum(product(thetas, operators._content["truth_operators_as_expansion_storage"])) assert isinstance(sum_product_truth_operators, (AbstractParametrizedTensorFactory, Number)) if isinstance(sum_product_truth_operators, AbstractParametrizedTensorFactory): sum_product_truth_operators = evaluate(sum_product_truth_operators) elif isinstance(sum_product_truth_operators, Number): pass else: raise TypeError("Invalid operator type") assert "basis_functions" in operators._content basis_functions = operators._content["basis_functions"] assert len(basis_functions) in (0, 1, 2) if len(basis_functions) is 0: output = sum_product_truth_operators elif len(basis_functions) is 1: output = transpose(basis_functions[0])*sum_product_truth_operators assert not isinstance(output, DelayedTranspose) elif len(basis_functions) is 2: output = transpose(basis_functions[0])*sum_product_truth_operators*basis_functions[1] assert not isinstance(output, DelayedTranspose) else: raise ValueError("Invalid length") # Return return ProductOutput(output) else: raise ValueError("Invalid type")
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 __add__(self, other): from rbnics.backends import evaluate assert len(self._spaces) == 0 return evaluate(self) + other
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)