def solve(self, mu, c, snapshot): print("Performing L^2 projection for mu =", mu, "and component", c) #quit() projected_snapshot_N = OnlineFunction(self.N) solver = OnlineLinearSolver( self.inner_product_N, projected_snapshot_N, transpose(self.basis_functions) * self.inner_product * snapshot) solver.solve() return projected_snapshot_N.vector().__array__()
def reduced_solve(mu, N): normalize_inputs = NormalizeInputs(mu_range) mu_torch = normalize_inputs(mu) mu_torch = mu_torch.view(mu_torch.shape[1], -1) reduced_solution = dict() for c in components: network = Network(len(mu), c, N) network.load_state_dict( torch.load(os.path.join("networks", "network_" + c + "_" + str(N)))) normalize_outputs = NormalizeOutputs( os.path.join("networks", "output_normalization_" + c + "_" + str(N))) reduced_solution_c = OnlineFunction(N) reduced_solution_c.vector()[:] = normalize_outputs.inv( network(mu_torch).detach().numpy()[0]) reduced_solution[c] = reduced_solution_c return reduced_solution
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)