[P]arallel [Hi]gh-order [Li]brary for [P]DEs  Latest
Parallel High-Order Library for PDEs through hp-adaptive Discontinuous Galerkin methods
HROM_error_post_sampling.cpp
1 #include "HROM_error_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 "rom_import_helper_functions.h"
14 #include <eigen/Eigen/Dense>
15 #include <iostream>
16 #include <filesystem>
17 #include <Epetra_CrsMatrix.h>
18 #include <Epetra_Map.h>
19 #include <Epetra_Vector.h>
20 
21 namespace PHiLiP {
22 namespace Tests {
23 
24 
25 template <int dim, int nstate>
27  const dealii::ParameterHandler &parameter_handler_input)
28  : TestsBase::TestsBase(parameters_input)
29  , parameter_handler(parameter_handler_input)
30 {}
31 
32 template <int dim, int nstate>
34  // Copy all parameters
36 
37  parameters.reduced_order_param.path_to_search = path;
38  return parameters;
39 }
40 
41 template <int dim, int nstate>
43  bool file_found = false;
44  Epetra_MpiComm epetra_comm(MPI_COMM_WORLD);
45  VectorXd weights_eig;
46  int rows = 0;
48 
49  std::vector<std::filesystem::path> files_in_directory;
50  std::copy(std::filesystem::directory_iterator(path), std::filesystem::directory_iterator(), std::back_inserter(files_in_directory));
51  std::sort(files_in_directory.begin(), files_in_directory.end()); //Sort files so that the order is the same as for the sensitivity basis
52 
53  for (const auto & entry : files_in_directory){
54  if(std::string(entry.filename()).std::string::find("weights") != std::string::npos){
55  pcout << "Processing " << entry << std::endl;
56  file_found = true;
57  std::ifstream myfile(entry);
58  if(!myfile)
59  {
60  pcout << "Error opening file." << std::endl;
61  std::abort();
62  }
63  std::string line;
64 
65  while(std::getline(myfile, line)){ //for each line
66  std::istringstream stream(line);
67  std::string field;
68  while (getline(stream, field,' ')) { //parse data values on each line
69  if (field.empty()) {
70  continue;
71  }
72  else {
73  try{
74  std::stod(field);
75  rows++;
76  } catch (...){
77  continue;
78  }
79  }
80  }
81  }
82 
83  weights_eig.resize(rows);
84  int row = 0;
85  myfile.clear();
86  myfile.seekg(0); //Bring back to beginning of file
87  //Second loop set to build solutions matrix
88  while(std::getline(myfile, line)){ //for each line
89  std::istringstream stream(line);
90  std::string field;
91  while (getline(stream, field,' ')) { //parse data values on each line
92  if (field.empty()) {
93  continue;
94  }
95  else {
96  try{
97  double num_string = std::stod(field);
98  weights_eig(row) = num_string;
99  row++;
100  } catch (...){
101  continue;
102  }
103  }
104  }
105  }
106  myfile.close();
107  }
108  }
109 
110  Epetra_CrsMatrix epetra_system_matrix = dg->system_matrix.trilinos_matrix();
111  const int n_quad_pts = dg->volume_quadrature_collection[dg->all_parameters->flow_solver_param.poly_degree].size();
112  const int length = epetra_system_matrix.NumMyRows()/(nstate*n_quad_pts);
113  int *local_elements = new int[length];
114  int ctr = 0;
115  for (const auto &cell : dg->dof_handler.active_cell_iterators())
116  {
117  if (cell->is_locally_owned()){
118  local_elements[ctr] = cell->active_cell_index();
119  ctr +=1;
120  }
121  }
122 
123  Epetra_Map ColMap(rows, length, local_elements, 0, epetra_comm);
124  ColMap.Print(std::cout);
125  Epetra_Vector weights(ColMap);
126  for(int i = 0; i < length; i++){
127  int global_ind = local_elements[i];
128  weights[i] = weights_eig(global_ind);
129  }
130 
131  ptr_weights = std::make_shared<Epetra_Vector>(weights);
132  return file_found;
133 }
134 
135 template <int dim, int nstate>
137 {
138  pcout << "Starting error analysis for HROM..." << std::endl;
139 
140  // Create POD Petrov-Galerkin ROM with Hyper-reduction
141  std::unique_ptr<FlowSolver::FlowSolver<dim,nstate>> flow_solver_hyper_reduced_petrov_galerkin = FlowSolver::FlowSolverFactory<dim,nstate>::select_flow_case(all_parameters, parameter_handler);
142 
143  std::shared_ptr<ProperOrthogonalDecomposition::OfflinePOD<dim>> pod_petrov_galerkin = std::make_shared<ProperOrthogonalDecomposition::OfflinePOD<dim>>(flow_solver_hyper_reduced_petrov_galerkin->dg);
144  std::shared_ptr<HyperreducedAdaptiveSampling<dim,nstate>> hyper_reduced_ROM_solver = std::make_unique<HyperreducedAdaptiveSampling<dim,nstate>>(all_parameters, parameter_handler);
145  hyper_reduced_ROM_solver->current_pod->basis = pod_petrov_galerkin->basis;
146  hyper_reduced_ROM_solver->current_pod->referenceState = pod_petrov_galerkin->referenceState;
147  hyper_reduced_ROM_solver->current_pod->snapshotMatrix = pod_petrov_galerkin->snapshotMatrix;
148  snapshot_parameters(0,0);
149  std::string path = all_parameters->reduced_order_param.path_to_search; //Search specified directory for files containing "solutions_table"
150  bool snap_found = getSnapshotParamsFromFile(snapshot_parameters, path);
151  if (snap_found){
152  hyper_reduced_ROM_solver->snapshot_parameters = snapshot_parameters;
153  std::cout << "snapshot_parameters" << std::endl;
154  std::cout << snapshot_parameters << std::endl;
155  }
156  else{
157  std::cout << "File with snapshots not found in folder" << std::endl;
158  return -1;
159  }
160  getROMPoints(rom_points, all_parameters);
161  std::cout << "ROM Locations" << std::endl;
162  std::cout << rom_points << std::endl;
163 
164  bool weights_found = getWeightsFromFile(flow_solver_hyper_reduced_petrov_galerkin->dg);
165  if (weights_found){
166  std::cout << "ECSW Weights" << std::endl;
167  std::cout << *ptr_weights << std::endl;
168  }
169  else{
170  std::cout << "File with weights not found in folder" << std::endl;
171  return -1;
172  }
173 
174  hyper_reduced_ROM_solver->trueErrorROM(rom_points, *ptr_weights);
175 
176  return 0;
177 }
178 
179 #if PHILIP_DIM==1
181 #endif
182 
183 #if PHILIP_DIM!=1
185 #endif
186 } // Tests namespace
187 } // PHiLiP namespace
HROMErrorPostSampling(const Parameters::AllParameters *const parameters_input, const dealii::ParameterHandler &parameter_handler_input)
Constructor.
Files for the baseline physics.
Definition: ADTypes.hpp:10
ReducedOrderModelParam reduced_order_param
Contains parameters for the Reduced-Order model.
std::string path_to_search
Path to search for snapshots or saved POD basis.
Main parameter class that contains the various other sub-parameter classes.
const dealii::ParameterHandler & parameter_handler
Dummy parameter handler because flowsolver requires it.
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
MatrixXd snapshot_parameters
Matrix of snapshot parameters.
MatrixXd rom_points
Matrix of error sampling points.
bool getWeightsFromFile(std::shared_ptr< DGBase< dim, double >> &dg) const
Read ECSW weights from the text file.
int run_test() const override
Evaluate and output the "true" error at ROM Points.
dealii::ConditionalOStream pcout
ConditionalOStream.
Definition: tests.h:45
Parameters::AllParameters reinit_params(std::string path) const
Reinitialize parameters.
std::shared_ptr< Epetra_Vector > ptr_weights
Ptr vector of ECSW Weights.
DGBase is independent of the number of state variables.
Definition: dg_base.hpp:82
Base class of all the tests.
Definition: tests.h:17