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: object): problem = self.problem solver = LinearSolver( problem._riesz_solve_inner_product, problem._riesz_solve_storage, rhs, problem._riesz_solve_homogeneous_dirichlet_bc) solver.set_parameters(problem._linear_solver_parameters) solver.solve() return problem._riesz_solve_storage
def solve(self, rhs: object): problem = self.problem args = (problem._riesz_solve_inner_product, problem._riesz_solve_storage, rhs, problem._riesz_solve_homogeneous_dirichlet_bc) if not self.delay: solver = LinearSolver(*args) solver.set_parameters(problem._linear_solver_parameters) solver.solve() return problem._riesz_solve_storage else: solver = DelayedLinearSolver(*args) solver.set_parameters(problem._linear_solver_parameters) return solver
def _solve(self, N, **kwargs): EllipticCoerciveReducedProblem_DerivedClass._solve( self, N, **kwargs) if kwargs["online_rectification"]: q = self.online_solve_kwargs_with_rectification.index(kwargs) intermediate_solution = OnlineFunction(N) solver = LinearSolver( self.operator["projection_reduced_snapshots"][:N, :N][q], intermediate_solution, self._solution.vector()) solver.set_parameters(self._linear_solver_parameters) solver.solve() self._solution.vector( )[:] = self.operator["projection_truth_snapshots"][:N, :N][ 0] * intermediate_solution
def _solve_supremizer(self, solution): assert len(self.inner_product["s"]) == 1 # the affine expansion storage contains only the inner product matrix assembled_operator_lhs = self.inner_product["s"][0] assembled_operator_bt = sum(product(self.compute_theta("bt_restricted"), self.operator["bt_restricted"])) assembled_operator_rhs = assembled_operator_bt*solution if self.dirichlet_bc["s"] is not None: assembled_dirichlet_bc = sum(product(self.compute_theta("dirichlet_bc_s"), self.dirichlet_bc["s"])) else: assembled_dirichlet_bc = None solver = LinearSolver( assembled_operator_lhs, self._supremizer, assembled_operator_rhs, assembled_dirichlet_bc ) solver.set_parameters(self._linear_solver_parameters) solver.solve()
def _solve(self, N, **kwargs): # Temporarily change value of stabilized attribute in truth problem bak_stabilized = self.truth_problem.stabilized self.truth_problem.stabilized = kwargs["online_stabilization"] # Solve reduced problem if kwargs["online_vanishing_viscosity"]: assembled_operator = dict() assembled_operator["a"] = ( sum(product(self.compute_theta("a"), self.operator["a"][:N, :N])) + self.operator["vanishing_viscosity"][:N, :N][0] ) assembled_operator["f"] = sum(product(self.compute_theta("f"), self.operator["f"][:N])) self._solution = OnlineFunction(N) solver = LinearSolver(assembled_operator["a"], self._solution, assembled_operator["f"]) solver.set_parameters(self._linear_solver_parameters) solver.solve() else: EllipticCoerciveReducedProblem_DerivedClass._solve(self, N, **kwargs) # Restore original value of stabilized attribute in truth problem self.truth_problem.stabilized = bak_stabilized
def solve(self): problem = self.problem solver = LinearSolver(self.matrix_eval(), problem._solution, self.vector_eval(), self.bc_eval()) solver.set_parameters(problem._linear_solver_parameters) solver.solve()
def solve(self): problem = self.problem solver = LinearSolver(self, problem._solution) solver.set_parameters(problem._linear_solver_parameters) solver.solve()
def assemble_operator(self, term, current_stage="online"): if term == "projection_truth_snapshots": assert current_stage in ( "online", "offline_rectification_postprocessing") if current_stage == "online": # load from file self.operator["projection_truth_snapshots"].load( self.folder["reduced_operators"], "projection_truth_snapshots") return self.operator["projection_truth_snapshots"] elif current_stage == "offline_rectification_postprocessing": assert len( self.truth_problem.inner_product ) == 1 # the affine expansion storage contains only the inner product matrix inner_product = self.truth_problem.inner_product[0] for n in range(1, self.N + 1): assert len( self.inner_product ) == 1 # the affine expansion storage contains only the inner product matrix inner_product_n = self.inner_product[:n, :n][0] basis_functions_n = self.basis_functions[:n] projection_truth_snapshots_expansion = OnlineAffineExpansionStorage( 1) projection_truth_snapshots = OnlineMatrix(n, n) for (i, snapshot_i) in enumerate(self.snapshots[:n]): projected_truth_snapshot_i = OnlineFunction(n) solver = LinearSolver( inner_product_n, projected_truth_snapshot_i, transpose(basis_functions_n) * inner_product * snapshot_i) solver.set_parameters( self._linear_solver_parameters) solver.solve() for j in range(n): projection_truth_snapshots[ j, i] = projected_truth_snapshot_i.vector()[j] projection_truth_snapshots_expansion[ 0] = projection_truth_snapshots print("\tcondition number for n = " + str(n) + ": " + str(cond(projection_truth_snapshots))) self.operator[ "projection_truth_snapshots"][:n, : n] = projection_truth_snapshots_expansion # Save self.operator["projection_truth_snapshots"].save( self.folder["reduced_operators"], "projection_truth_snapshots") return self.operator["projection_truth_snapshots"] else: raise ValueError("Invalid stage in assemble_operator().") elif term == "projection_reduced_snapshots": assert current_stage in ( "online", "offline_rectification_postprocessing") if current_stage == "online": # load from file self.operator["projection_reduced_snapshots"].load( self.folder["reduced_operators"], "projection_reduced_snapshots") return self.operator["projection_reduced_snapshots"] elif current_stage == "offline_rectification_postprocessing": # Backup mu bak_mu = self.mu # Prepare rectification for all possible online solve arguments for n in range(1, self.N + 1): print("\tcondition number for n = " + str(n)) projection_reduced_snapshots_expansion = OnlineAffineExpansionStorage( len(self.online_solve_kwargs_without_rectification) ) for (q, online_solve_kwargs) in enumerate( self.online_solve_kwargs_without_rectification ): projection_reduced_snapshots = OnlineMatrix(n, n) for (i, mu_i) in enumerate(self.snapshots_mu[:n]): self.set_mu(mu_i) projected_reduced_snapshot_i = self.solve( n, **online_solve_kwargs) for j in range(n): projection_reduced_snapshots[ j, i] = projected_reduced_snapshot_i.vector( )[j] projection_reduced_snapshots_expansion[ q] = projection_reduced_snapshots print("\t\tonline solve options " + str( dict(self. online_solve_kwargs_with_rectification[q]) ) + ": " + str(cond(projection_reduced_snapshots))) self.operator[ "projection_reduced_snapshots"][:n, : n] = projection_reduced_snapshots_expansion # Save and restore previous mu self.set_mu(bak_mu) self.operator["projection_reduced_snapshots"].save( self.folder["reduced_operators"], "projection_reduced_snapshots") return self.operator["projection_reduced_snapshots"] else: raise ValueError("Invalid stage in assemble_operator().") else: return EllipticCoerciveReducedProblem_DerivedClass.assemble_operator( self, term, current_stage)