[P]arallel [Hi]gh-order [Li]brary for [P]DEs  Latest
Parallel High-Order Library for PDEs through hp-adaptive Discontinuous Galerkin methods
hyper_reduction_post_sampling.cpp
1 #include "hyper_reduction_post_sampling.h"
2 #include "reduced_order/pod_basis_offline.h"
3 #include "parameters/all_parameters.h"
4 #include "flow_solver/flow_solver.h"
5 #include "flow_solver/flow_solver_factory.h"
6 #include "ode_solver/ode_solver_factory.h"
7 #include "reduced_order/assemble_ECSW_residual.h"
8 #include "reduced_order/assemble_ECSW_jacobian.h"
9 #include "linear_solver/NNLS_solver.h"
10 #include "linear_solver/helper_functions.h"
11 #include "reduced_order/pod_adaptive_sampling.h"
12 #include "reduced_order/hyper_reduced_adaptive_sampling.h"
13 #include "reduced_order/multi_core_helper_functions.h"
14 #include "rom_import_helper_functions.h"
15 #include <eigen/Eigen/Dense>
16 #include <iostream>
17 #include <filesystem>
18 
19 namespace PHiLiP {
20 namespace Tests {
21 
22 
23 template <int dim, int nstate>
25  const dealii::ParameterHandler &parameter_handler_input)
26  : TestsBase::TestsBase(parameters_input)
27  , parameter_handler(parameter_handler_input)
28 {}
29 
30 template <int dim, int nstate>
32  // Copy all parameters
34 
35  parameters.ode_solver_param.nonlinear_max_iterations = max_iter;
36  return parameters;
37 }
38 
39 template <int dim, int nstate>
41 {
42  pcout << "Starting hyperreduction test..." << std::endl;
43 
44  Epetra_MpiComm Comm( MPI_COMM_WORLD );
45  // Create POD Petrov-Galerkin ROM with Hyperreduction
46  std::unique_ptr<FlowSolver::FlowSolver<dim,nstate>> flow_solver_hyper_reduced_petrov_galerkin = FlowSolver::FlowSolverFactory<dim,nstate>::select_flow_case(all_parameters, parameter_handler);
47  auto ode_solver_type = Parameters::ODESolverParam::ODESolverEnum::hyper_reduced_petrov_galerkin_solver;
48 
49  // Run Adaptive Sampling to choose snapshot locations and create POD basis
50  std::shared_ptr<AdaptiveSampling<dim,nstate>> parameter_sampling = std::make_unique<AdaptiveSampling<dim,nstate>>(all_parameters, parameter_handler);
51  parameter_sampling->run_sampling();
52 
53  // Find C and d for NNLS Problem
54  std::cout << "Construct instance of Assembler..."<< std::endl;
55  std::shared_ptr<HyperReduction::AssembleECSWBase<dim,nstate>> constructor_NNLS_problem;
56  if (this->all_parameters->hyper_reduction_param.training_data == "residual")
57  constructor_NNLS_problem = std::make_shared<HyperReduction::AssembleECSWRes<dim,nstate>>(all_parameters, parameter_handler, flow_solver_hyper_reduced_petrov_galerkin->dg, parameter_sampling->current_pod, parameter_sampling->snapshot_parameters, ode_solver_type, Comm);
58  else {
59  constructor_NNLS_problem = std::make_shared<HyperReduction::AssembleECSWJac<dim,nstate>>(all_parameters, parameter_handler, flow_solver_hyper_reduced_petrov_galerkin->dg, parameter_sampling->current_pod, parameter_sampling->snapshot_parameters, ode_solver_type, Comm);
60  }
61  for (unsigned int j = 0 ; j < parameter_sampling->fom_locations.size() ; j++ ){
62  constructor_NNLS_problem->update_snapshots(parameter_sampling->fom_locations[j]);
63  }
64  std::cout << "Build Problem..."<< std::endl;
65  constructor_NNLS_problem->build_problem();
66 
67  // Transfer b vector (RHS of NNLS problem) to Epetra structure
68  const int rank = Comm.MyPID();
69  int rows = (constructor_NNLS_problem->A_T->trilinos_matrix()).NumGlobalCols();
70  Epetra_Map bMap(rows, (rank == 0) ? rows: 0, 0, Comm);
71  Epetra_Vector b_Epetra(bMap);
72  auto b = constructor_NNLS_problem->b;
73  unsigned int local_length = bMap.NumMyElements();
74  for(unsigned int i = 0 ; i < local_length ; i++){
75  b_Epetra[i] = b(i);
76  }
77 
78  // Solve NNLS Problem for ECSW weights
79  std::cout << "Create NNLS problem..."<< std::endl;
80  NNLSSolver NNLS_prob(all_parameters, parameter_handler, constructor_NNLS_problem->A_T->trilinos_matrix(), true, Comm, b_Epetra);
81  std::cout << "Solve NNLS problem..."<< std::endl;
82  bool exit_con = NNLS_prob.solve();
83  std::cout << exit_con << std::endl;
84 
85  std::shared_ptr<Epetra_Vector> ptr_weights = std::make_shared<Epetra_Vector>(NNLS_prob.get_solution());
86  std::cout << "ECSW Weights"<< std::endl;
87  std::cout << *ptr_weights << std::endl;
88 
89  Epetra_Vector local_weights = allocate_vector_to_single_core(*ptr_weights);
90  std::unique_ptr<dealii::TableHandler> weights_table = std::make_unique<dealii::TableHandler>();
91  for(int i = 0 ; i < local_weights.MyLength() ; i++){
92  weights_table->add_value("ECSW Weights", local_weights[i]);
93  weights_table->set_precision("ECSW Weights", 16);
94  }
95  std::ofstream weights_table_file("weights_table_iteration_HROM_post_sampling.txt");
96  weights_table->write_text(weights_table_file, dealii::TableHandler::TextOutputFormat::org_mode_table);
97  weights_table_file.close();
98 
99  // Solve for the DWR Error at the ROM points with the hyperreduced weights
100  std::shared_ptr<HyperreducedAdaptiveSampling<dim,nstate>> hyper_reduced_ROM_solver = std::make_unique<HyperreducedAdaptiveSampling<dim,nstate>>(all_parameters, parameter_handler);
101  hyper_reduced_ROM_solver->current_pod = parameter_sampling->current_pod;
102  hyper_reduced_ROM_solver->snapshot_parameters = parameter_sampling->snapshot_parameters;
103  MatrixXd rom_points(0, hyper_reduced_ROM_solver->snapshot_parameters.cols());
104  for(auto it = parameter_sampling->rom_locations.begin(); it != parameter_sampling->rom_locations.end(); ++it){
105  rom_points.conservativeResize(rom_points.rows()+1, rom_points.cols());
106  Eigen::RowVectorXd rom = it->get()->parameter;
107  rom_points.row(rom_points.rows()-1) = rom;
108  }
109  hyper_reduced_ROM_solver->placeROMLocations(rom_points, *ptr_weights);
110  hyper_reduced_ROM_solver->outputIterationData("HROM_post_sampling");
111 
112  // True Error for ROM and HROM at 20 or 400 evenly distributed points in a 1-param and 2-param design space, respectively
113  MatrixXd rom_true_error_points(0,0);
114  getROMPoints(rom_true_error_points, all_parameters);
115  parameter_sampling->trueErrorROM(rom_true_error_points);
116  hyper_reduced_ROM_solver->trueErrorROM(rom_true_error_points, *ptr_weights);
117 
118  return 0;
119 }
120 
121 #if PHILIP_DIM==1
123 #endif
124 
125 #if PHILIP_DIM!=1
127 #endif
128 } // Tests namespace
129 } // PHiLiP namespace
const dealii::ParameterHandler & parameter_handler
Dummy parameter handler because flowsolver requires it.
int run_test() const override
Conduct hyperreduction and evaluate HROM at ROM points.
Files for the baseline physics.
Definition: ADTypes.hpp:10
HyperReductionPostSampling(const Parameters::AllParameters *const parameters_input, const dealii::ParameterHandler &parameter_handler_input)
Constructor.
Main parameter class that contains the various other sub-parameter classes.
ODESolverParam ode_solver_param
Contains parameters for ODE solver.
static std::unique_ptr< FlowSolver< dim, nstate > > select_flow_case(const Parameters::AllParameters *const parameters_input, const dealii::ParameterHandler &parameter_handler_input)
Factory to return the correct flow solver given input file.
const Parameters::AllParameters *const all_parameters
Pointer to all parameters.
Definition: tests.h:20
Parameters::AllParameters reinit_params(const int max_iter) const
Reinitialize parameters.
dealii::ConditionalOStream pcout
ConditionalOStream.
Definition: tests.h:45
bool solve()
Call to solve NNLS problem.
unsigned int nonlinear_max_iterations
Maximum number of iterations.
Base class of all the tests.
Definition: tests.h:17