From c08c7dce65feac67c51f3d8396045c2eaa228c27 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Mon, 2 Oct 2023 08:34:45 -0700 Subject: [PATCH 01/16] Add example results --- examples/prom/dg_advection_global_rom.cpp | 36 +++++++++-------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/examples/prom/dg_advection_global_rom.cpp b/examples/prom/dg_advection_global_rom.cpp index 6855b8743..5a24e3c3e 100644 --- a/examples/prom/dg_advection_global_rom.cpp +++ b/examples/prom/dg_advection_global_rom.cpp @@ -10,14 +10,21 @@ // libROM MFEM Example: DG Advection (adapted from ex9p.cpp) // +// ================================================================================= +// // Compile with: make dg_advection_global_rom // -// For ROM (reproductive case): +// Sample runs and results for reproductive case: // dg_advection_global_rom -offline // dg_advection_global_rom -merge -ns 1 // dg_advection_global_rom -online // -// For ROM (global rom): +// Output: +// Relative l2 error of ROM solution 0.0005641839849343575 +// +// ================================================================================= +// +// Sample runs and results for global ROM: // Offline phase: dg_advection_global_rom -offline -ff 1.0 -id 0 // dg_advection_global_rom -offline -ff 1.1 -id 1 // dg_advection_global_rom -offline -ff 1.2 -id 2 @@ -28,19 +35,10 @@ // // Online phase: dg_advection_global_rom -online -ff 1.15 // -// Sample runs: -// mpirun -np 4 dg_advection_global_rom -p 0 -dt 0.005 -// mpirun -np 4 dg_advection_global_rom -p 0 -dt 0.01 -// mpirun -np 4 dg_advection_global_rom -p 1 -dt 0.005 -tf 9 -// mpirun -np 4 dg_advection_global_rom -p 1 -rp 1 -dt 0.002 -tf 9 -// mpirun -np 4 dg_advection_global_rom -p 1 -rp 1 -dt 0.02 -s 13 -tf 9 -// mpirun -np 4 dg_advection_global_rom -p 1 -rp 1 -dt 0.004 -tf 9 -// mpirun -np 4 dg_advection_global_rom -p 1 -rp 1 -dt 0.005 -tf 9 -// mpirun -np 4 dg_advection_global_rom -p 3 -rp 2 -dt 0.0025 -tf 9 -vs 20 -// mpirun -np 4 dg_advection_global_rom -p 0 -o 2 -rp 1 -dt 0.01 -tf 8 -// mpirun -np 4 dg_advection_global_rom -p 0 -rs 2 -dt 0.005 -tf 2 -// mpirun -np 4 dg_advection_global_rom -p 0 -rs 1 -o 2 -tf 2 -// mpirun -np 3 dg_advection_global_rom -p 1 -rs 1 -rp 0 -dt 0.005 -tf 0.5 +// Output: +// Relative l2 error of ROM solution 0.0004333183604809453 +// +// ================================================================================= // // Description: This example code solves the time-dependent advection equation // du/dt + v.grad(u) = 0, where v is a given fluid velocity, and @@ -128,10 +126,7 @@ class AIR_prec : public Solver delete AIR_solver; AIR_solver = new HypreBoomerAMG(A_s); AIR_solver->SetAdvectiveOptions(1, "", "FA"); - AIR_solver->SetPrintLevel( - 0); // 6. Define the parallel mesh by a partitioning of the serial mesh. Refine - // this mesh further in parallel to increase the resolution. Once the - // parallel mesh is defined, the serial mesh can be deleted. + AIR_solver->SetPrintLevel(0); AIR_solver->SetMaxLevels(50); } @@ -464,9 +459,6 @@ int main(int argc, char *argv[]) { cout << "Unknown ODE solver type: " << ode_solver_type << '\n'; } - delete mesh; // 6. Define the parallel mesh by a partitioning of the serial mesh. Refine - // this mesh further in parallel to increase the resolution. Once the - // parallel mesh is defined, the serial mesh can be deleted. return 3; } From 90393b0b53c13cc0daa11f0d9c645c503fcad8f8 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Mon, 4 Dec 2023 14:03:04 -0800 Subject: [PATCH 02/16] Count DMD prediction timer only at final time --- examples/dmd/dg_advection.cpp | 8 +++----- examples/dmd/dg_euler.cpp | 8 +++----- examples/dmd/heat_conduction.cpp | 8 +++----- examples/dmd/heat_conduction_dmdc.cpp | 8 +++----- examples/dmd/nonlinear_elasticity.cpp | 28 +++++++++++++-------------- examples/dmd/wave_equation.cpp | 6 +++--- 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/examples/dmd/dg_advection.cpp b/examples/dmd/dg_advection.cpp index c75c2339f..31bf61c59 100644 --- a/examples/dmd/dg_advection.cpp +++ b/examples/dmd/dg_advection.cpp @@ -710,9 +710,7 @@ int main(int argc, char *argv[]) Vector true_solution_u(U->Size()); true_solution_u = U->GetData(); - dmd_prediction_timer.Start(); - - // 14. Predict the state at t_final using DMD. + // 14. Predict using DMD. if (myid == 0) { std::cout << "Predicting solution using DMD" << std::endl; @@ -755,9 +753,9 @@ int main(int argc, char *argv[]) } } - dmd_prediction_timer.Stop(); - + dmd_prediction_timer.Start(); result_u = dmd_U.predict(t_final); + dmd_prediction_timer.Stop(); // 15. Calculate the relative error between the DMD final solution and the true solution. Vector dmd_solution_u(result_u->getData(), result_u->dim()); diff --git a/examples/dmd/dg_euler.cpp b/examples/dmd/dg_euler.cpp index 159ef76f2..43129362e 100644 --- a/examples/dmd/dg_euler.cpp +++ b/examples/dmd/dg_euler.cpp @@ -574,9 +574,7 @@ int main(int argc, char *argv[]) Vector true_solution_e(u_block.GetBlock(3).Size()); true_solution_e = u_block.GetBlock(3).GetData(); - dmd_prediction_timer.Start(); - - // 14. Predict the state at t_final using DMD. + // 14. Predict using DMD. if (mpi.WorldRank() == 0) { std::cout << "Predicting density, momentum, and energy using DMD" << std::endl; @@ -639,12 +637,12 @@ int main(int argc, char *argv[]) } } - dmd_prediction_timer.Stop(); - + dmd_prediction_timer.Start(); result_dens = dmd_dens->predict(t_final); result_x_mom = dmd_x_mom->predict(t_final); result_y_mom = dmd_y_mom->predict(t_final); result_e = dmd_e->predict(t_final); + dmd_prediction_timer.Stop(); // 15. Calculate the relative error between the DMD final solution and the true solution. Vector dmd_solution_dens(result_dens->getData(), result_dens->dim()); diff --git a/examples/dmd/heat_conduction.cpp b/examples/dmd/heat_conduction.cpp index 34cb44329..5c12a9ae7 100644 --- a/examples/dmd/heat_conduction.cpp +++ b/examples/dmd/heat_conduction.cpp @@ -498,9 +498,7 @@ int main(int argc, char *argv[]) Vector true_solution_u(u.Size()); true_solution_u = u.GetData(); - dmd_prediction_timer.Start(); - - // 14. Predict the state at t_final using DMD. + // 14. Predict using DMD. if (myid == 0) { std::cout << "Predicting temperature using DMD" << std::endl; @@ -540,9 +538,9 @@ int main(int argc, char *argv[]) } } - dmd_prediction_timer.Stop(); - + dmd_prediction_timer.Start(); result_u = dmd_u->predict(t_final); + dmd_prediction_timer.Stop(); // 15. Calculate the relative error between the DMD final solution and the true solution. Vector dmd_solution_u(result_u->getData(), result_u->dim()); diff --git a/examples/dmd/heat_conduction_dmdc.cpp b/examples/dmd/heat_conduction_dmdc.cpp index 7d76568c2..4b4672b85 100644 --- a/examples/dmd/heat_conduction_dmdc.cpp +++ b/examples/dmd/heat_conduction_dmdc.cpp @@ -534,9 +534,7 @@ int main(int argc, char *argv[]) Vector true_solution_u(u.Size()); true_solution_u = u.GetData(); - dmd_prediction_timer.Start(); - - // 14. Predict the state at t_final using DMDc. + // 14. Predict using DMDc. if (myid == 0) { std::cout << "Predicting temperature using DMDc" << std::endl; @@ -576,9 +574,9 @@ int main(int argc, char *argv[]) } } - dmd_prediction_timer.Stop(); - + dmd_prediction_timer.Start(); result_u = dmd_u->predict(t_final); + dmd_prediction_timer.Stop(); // 15. Calculate the relative error between the DMDc final solution and the true solution. Vector dmd_solution_u(result_u->getData(), result_u->dim()); diff --git a/examples/dmd/nonlinear_elasticity.cpp b/examples/dmd/nonlinear_elasticity.cpp index 5bca8b44b..5f8c43384 100644 --- a/examples/dmd/nonlinear_elasticity.cpp +++ b/examples/dmd/nonlinear_elasticity.cpp @@ -515,6 +515,12 @@ int main(int argc, char *argv[]) if (last_step || (ti % windowNumSamples) == 0) { + // Calculate DMD modes + if (myid == 0 && rdim != -1 && ef != -1) + { + std::cout << "Both rdim and ef are set. ef will be ignored." << std::endl; + } + if (rdim != -1) { if (myid == 0) @@ -608,10 +614,10 @@ int main(int argc, char *argv[]) w_gf.Save(ee_ofs); } - // 13. Calculate the DMD modes. - if (myid == 0 && rdim != -1 && ef != -1) + // 13. Predict using DMD. + if (myid == 0) { - std::cout << "Both rdim and ef are set. ef will be ignored." << std::endl; + std::cout << "Predicting position and velocity using DMD" << std::endl; } Vector true_solution_x(x_gf.GetTrueVector().Size()); @@ -620,14 +626,6 @@ int main(int argc, char *argv[]) Vector true_solution_v(v_gf.GetTrueVector().Size()); true_solution_v = v_gf.GetTrueVector(); - dmd_prediction_timer.Start(); - - // 14. Predict the state at t_final using DMD. - if (myid == 0) - { - std::cout << "Predicting position and velocity using DMD" << std::endl; - } - curr_window = 0; CAROM::Vector* result_x = dmd_x[curr_window]->predict(ts[0]); CAROM::Vector* result_v = dmd_v[curr_window]->predict(ts[0]); @@ -687,12 +685,12 @@ int main(int argc, char *argv[]) } } - dmd_prediction_timer.Stop(); - + dmd_prediction_timer.Start(); result_x = dmd_x[curr_window]->predict(t_final); result_v = dmd_v[curr_window]->predict(t_final); + dmd_prediction_timer.Stop(); - // 15. Calculate the relative error between the DMD final solution and the true solution. + // 14. Calculate the relative error between the DMD final solution and the true solution. Vector dmd_solution_x(result_x->getData(), result_x->dim()); Vector diff_x(true_solution_x.Size()); subtract(dmd_solution_x, true_solution_x, diff_x); @@ -722,7 +720,7 @@ int main(int argc, char *argv[]) dmd_prediction_timer.RealTime()); } - // 16. Free the used memory. + // 15. Free the used memory. delete ode_solver; delete pmesh; delete result_x; diff --git a/examples/dmd/wave_equation.cpp b/examples/dmd/wave_equation.cpp index 7bc6ae3fe..d59c06b83 100644 --- a/examples/dmd/wave_equation.cpp +++ b/examples/dmd/wave_equation.cpp @@ -525,8 +525,7 @@ int main(int argc, char *argv[]) dudt_gf.Save(osol); } - // 10. Predict the state at t_final using DMD. - dmd_prediction_timer.Start(); + // 10. Predict using DMD. cout << "Predicting temperature using DMD" << endl; CAROM::Vector* result_u = nullptr; VisItDataCollection dmd_visit_dc("DMD_Wave_Equation", mesh); @@ -564,8 +563,9 @@ int main(int argc, char *argv[]) } } } - dmd_prediction_timer.Stop(); + dmd_prediction_timer.Start(); result_u = dmd_u[curr_window]->predict(t_final); + dmd_prediction_timer.Stop(); // 11. Calculate the relative error between the DMD final solution and the true solution. Vector dmd_solution_u(result_u->getData(), result_u->dim()); From ffe333ead70167b5c643ce9f446d6ca78f55652b Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Mon, 4 Dec 2023 18:58:53 -0800 Subject: [PATCH 03/16] Move final summary for basis generator into library --- examples/prom/mixed_nonlinear_diffusion.cpp | 51 +----------------- .../prom/nonlinear_elasticity_global_rom.cpp | 52 +------------------ lib/linalg/BasisGenerator.cpp | 51 ++++++++++++++++++ lib/linalg/BasisGenerator.h | 15 ++++++ 4 files changed, 69 insertions(+), 100 deletions(-) diff --git a/examples/prom/mixed_nonlinear_diffusion.cpp b/examples/prom/mixed_nonlinear_diffusion.cpp index 8b6f7916f..970bef672 100644 --- a/examples/prom/mixed_nonlinear_diffusion.cpp +++ b/examples/prom/mixed_nonlinear_diffusion.cpp @@ -427,54 +427,6 @@ CAROM::Matrix* GetFirstColumns(const int N, const CAROM::Matrix* A) } // TODO: move this to the library? -void BasisGeneratorFinalSummary(CAROM::BasisGenerator* bg, - const double energyFraction, int & cutoff, const std::string cutoffOutputPath) -{ - const int rom_dim = bg->getSpatialBasis()->numColumns(); - const CAROM::Vector* sing_vals = bg->getSingularValues(); - - MFEM_VERIFY(rom_dim <= sing_vals->dim(), ""); - - double sum = 0.0; - for (int sv = 0; sv < sing_vals->dim(); ++sv) { - sum += (*sing_vals)(sv); - } - - vector energy_fractions = {0.9999, 0.999, 0.99, 0.9}; - bool reached_cutoff = false; - - ofstream outfile(cutoffOutputPath); - - double partialSum = 0.0; - for (int sv = 0; sv < sing_vals->dim(); ++sv) { - partialSum += (*sing_vals)(sv); - for (int i = energy_fractions.size() - 1; i >= 0; i--) - { - if (partialSum / sum > energy_fractions[i]) - { - outfile << "For energy fraction: " << energy_fractions[i] << ", take first " - << sv+1 << " of " << sing_vals->dim() << " basis vectors" << endl; - energy_fractions.pop_back(); - } - else - { - break; - } - } - - if (!reached_cutoff && partialSum / sum > energyFraction) - { - cutoff = sv+1; - reached_cutoff = true; - } - } - - if (!reached_cutoff) cutoff = sing_vals->dim(); - outfile << "Take first " << cutoff << " of " << sing_vals->dim() << - " basis vectors" << endl; - outfile.close(); -} - void MergeBasis(const int dimFOM, const int nparam, const int max_num_snapshots, std::string name) { @@ -496,9 +448,10 @@ void MergeBasis(const int dimFOM, const int nparam, const int max_num_snapshots, generator.endSamples(); // save the merged basis file int cutoff = 0; - BasisGeneratorFinalSummary(&generator, 0.9999, cutoff, "mergedSV_" + name); + generator.FinalSummary(0.9999, cutoff, "mergedSV_" + name); } +// TODO: move this to the library? const CAROM::Matrix* GetSnapshotMatrix(const int dimFOM, const int nparam, const int max_num_snapshots, std::string name) { diff --git a/examples/prom/nonlinear_elasticity_global_rom.cpp b/examples/prom/nonlinear_elasticity_global_rom.cpp index 984f1624d..40018ad3f 100644 --- a/examples/prom/nonlinear_elasticity_global_rom.cpp +++ b/examples/prom/nonlinear_elasticity_global_rom.cpp @@ -273,55 +273,6 @@ CAROM::Matrix* GetFirstColumns(const int N, const CAROM::Matrix* A) } // TODO: move this to the library? -void BasisGeneratorFinalSummary(CAROM::BasisGenerator* bg, - const double energyFraction, int& cutoff, - const std::string cutoffOutputPath) -{ - const int rom_dim = bg->getSpatialBasis()->numColumns(); - const CAROM::Vector* sing_vals = bg->getSingularValues(); - - MFEM_VERIFY(rom_dim <= sing_vals->dim(), ""); - - double sum = 0.0; - for (int sv = 0; sv < sing_vals->dim(); ++sv) { - sum += (*sing_vals)(sv); - } - - vector energy_fractions = { 0.9999999, 0.999999, 0.99999, 0.9999, 0.999, 0.99, 0.9 }; - bool reached_cutoff = false; - - ofstream outfile(cutoffOutputPath); - - double partialSum = 0.0; - for (int sv = 0; sv < sing_vals->dim(); ++sv) { - partialSum += (*sing_vals)(sv); - for (int i = energy_fractions.size() - 1; i >= 0; i--) - { - if (partialSum / sum > energy_fractions[i]) - { - outfile << "For energy fraction: " << energy_fractions[i] << ", take first " - << sv + 1 << " of " << sing_vals->dim() << " basis vectors" << endl; - energy_fractions.pop_back(); - } - else - { - break; - } - } - - if (!reached_cutoff && partialSum / sum > energyFraction) - { - cutoff = sv + 1; - reached_cutoff = true; - } - } - - if (!reached_cutoff) cutoff = sing_vals->dim(); - outfile << "Take first " << cutoff << " of " << sing_vals->dim() << - " basis vectors" << endl; - outfile.close(); -} - void MergeBasis(const int dimFOM, const int nparam, const int max_num_snapshots, std::string name) { @@ -343,8 +294,7 @@ void MergeBasis(const int dimFOM, const int nparam, const int max_num_snapshots, generator.endSamples(); // save the merged basis file int cutoff = 0; - BasisGeneratorFinalSummary(&generator, 0.9999999, cutoff, - "mergedSV_" + name + ".txt"); + generator.FinalSummary(0.9999999, cutoff, "mergedSV_" + name + ".txt"); } // TODO: remove this by making online computation serial? diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index bfd46bbb7..166910582 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -310,6 +310,57 @@ BasisGenerator::resetDt( } } +void +BasisGenerator::FinalSummary( + const double energyFraction, + int & cutoff, + const std::string cutoffOutputPath, + const int first_sv) +{ + const int rom_dim = getSpatialBasis()->numColumns(); + const Vector* sing_vals = getSingularValues(); + + CAROM_VERIFY(rom_dim <= sing_vals->dim()); + + double sum = 0.0; + for (int sv = first_sv; sv < sing_vals->dim(); ++sv) { + sum += (*sing_vals)(sv); + } + + std::vector energy_fractions = {0.99999999, 0.9999999, 0.999999, 0.99999, 0.9999, 0.999, 0.99, 0.9}; + cutoff = first_sv; + bool reached_cutoff = false; + double partialSum = 0.0; + + std::ofstream outfile(cutoffOutputPath); + for (int sv = first_sv; sv < sing_vals->dim(); ++sv) { + partialSum += (*sing_vals)(sv); + for (int i = energy_fractions.size() - 1; i >= 0; i--) + { + if (partialSum / sum > energy_fractions[i]) + { + outfile << "For energy fraction: " << energy_fractions[i] << ", take first " + << sv+1 << " of " << sing_vals->dim() << " basis vectors" << std::endl; + energy_fractions.pop_back(); + } + else + { + break; + } + } + if (!reached_cutoff && partialSum / sum > energyFraction) + { + cutoff = sv+1; + reached_cutoff = true; + } + } + + if (!reached_cutoff) cutoff = sing_vals->dim(); + outfile << "For energy fraction: " << energyFraction << ", take first " + << cutoff << " of " << sing_vals->dim() << " basis vectors" << std::endl; + outfile.close(); +} + BasisGenerator::~BasisGenerator() { if (d_basis_writer) { diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index cbd5febcc..bc009e606 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -23,6 +23,7 @@ #include "mpi.h" #include +#include /* Use C++11 built-in shared pointers if available; else fallback to Boost. */ #if __cplusplus >= 201103L @@ -263,6 +264,20 @@ class BasisGenerator return d_svd->getNumSamples(); } + /** + * @brief Prints the summary of recommended numbers of basis vectors. + * + * @param[in] energyFraction Energy Fraction. + * @param[in] cutoff Number of basis vectors selected. + * @param[in] cutoffOutputPath Path of the summary file. + * @param[in] first_sv First singular vector in the calculaton of energy. + */ + void FinalSummary( + const double energyFraction, + int & cutoff, + const std::string cutoffOutputPath, + const int first_sv = 0); + protected: /** * @brief Writer of basis vectors. From b8d115684c1e3d9b1880540507a48494cb97fedf Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Mon, 4 Dec 2023 19:01:22 -0800 Subject: [PATCH 04/16] Stylize --- examples/prom/dg_advection_global_rom.cpp | 2 +- lib/linalg/BasisGenerator.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/prom/dg_advection_global_rom.cpp b/examples/prom/dg_advection_global_rom.cpp index 5a24e3c3e..1fa9f8b70 100644 --- a/examples/prom/dg_advection_global_rom.cpp +++ b/examples/prom/dg_advection_global_rom.cpp @@ -21,7 +21,7 @@ // // Output: // Relative l2 error of ROM solution 0.0005641839849343575 -// +// // ================================================================================= // // Sample runs and results for global ROM: diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index 166910582..697b975a6 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -310,7 +310,7 @@ BasisGenerator::resetDt( } } -void +void BasisGenerator::FinalSummary( const double energyFraction, int & cutoff, From c52cd4f2c6be567b6c92f17c8f0f1d6cd1079f1e Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Thu, 7 Dec 2023 10:10:51 -0800 Subject: [PATCH 05/16] Add finalSummary to combine_samples --- examples/misc/combine_samples.cpp | 3 +++ examples/prom/mixed_nonlinear_diffusion.cpp | 2 +- .../prom/nonlinear_elasticity_global_rom.cpp | 2 +- lib/linalg/BasisGenerator.cpp | 27 ++++++++++++++----- lib/linalg/BasisGenerator.h | 4 +-- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/examples/misc/combine_samples.cpp b/examples/misc/combine_samples.cpp index 4b083e807..9444bbd24 100644 --- a/examples/misc/combine_samples.cpp +++ b/examples/misc/combine_samples.cpp @@ -127,12 +127,14 @@ int main(int argc, char* argv[]) std::endl; static_basis_generator->writeSnapshot(); + int rdim; if (!subtract_mean && !subtract_offset) { /*-- Compute SVD and save file --*/ if (rank==0) std::cout << "Computing SVD" << std::endl; int rom_dim = static_basis_generator->getSpatialBasis()->numColumns(); if (rank==0) std::cout << "U ROM Dimension: " << rom_dim << std::endl; static_basis_generator->endSamples(); + static_basis_generator->finalSummary(0.99999999, rdim); } else { /*-- load data from hdf5 file to find the mean and subtract it --*/ @@ -188,6 +190,7 @@ int main(int argc, char* argv[]) int rom_dim = static_basis_generator2->getSpatialBasis()->numColumns(); if (rank==0) std::cout << "U ROM Dimension: " << rom_dim << std::endl; static_basis_generator2->endSamples(); + static_basis_generator2->finalSummary(0.99999999, rdim); static_basis_generator2 = nullptr; } diff --git a/examples/prom/mixed_nonlinear_diffusion.cpp b/examples/prom/mixed_nonlinear_diffusion.cpp index 970bef672..e73c39b29 100644 --- a/examples/prom/mixed_nonlinear_diffusion.cpp +++ b/examples/prom/mixed_nonlinear_diffusion.cpp @@ -448,7 +448,7 @@ void MergeBasis(const int dimFOM, const int nparam, const int max_num_snapshots, generator.endSamples(); // save the merged basis file int cutoff = 0; - generator.FinalSummary(0.9999, cutoff, "mergedSV_" + name); + generator.finalSummary(0.9999, cutoff, "mergedSV_" + name); } // TODO: move this to the library? diff --git a/examples/prom/nonlinear_elasticity_global_rom.cpp b/examples/prom/nonlinear_elasticity_global_rom.cpp index 40018ad3f..9e460d6c5 100644 --- a/examples/prom/nonlinear_elasticity_global_rom.cpp +++ b/examples/prom/nonlinear_elasticity_global_rom.cpp @@ -294,7 +294,7 @@ void MergeBasis(const int dimFOM, const int nparam, const int max_num_snapshots, generator.endSamples(); // save the merged basis file int cutoff = 0; - generator.FinalSummary(0.9999999, cutoff, "mergedSV_" + name + ".txt"); + generator.finalSummary(0.9999999, cutoff, "mergedSV_" + name + ".txt"); } // TODO: remove this by making online computation serial? diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index 697b975a6..fa13c7a64 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -21,6 +21,8 @@ #include "svd/IncrementalSVDFastUpdate.h" #include "svd/IncrementalSVDBrand.h" +#include + namespace CAROM { BasisGenerator::BasisGenerator( @@ -311,7 +313,7 @@ BasisGenerator::resetDt( } void -BasisGenerator::FinalSummary( +BasisGenerator::finalSummary( const double energyFraction, int & cutoff, const std::string cutoffOutputPath, @@ -327,21 +329,31 @@ BasisGenerator::FinalSummary( sum += (*sing_vals)(sv); } - std::vector energy_fractions = {0.99999999, 0.9999999, 0.999999, 0.99999, 0.9999, 0.999, 0.99, 0.9}; + int p = std::floor(-std::log10(1.0 - energyFraction)); + std::vector energy_fractions(p); + + for (int i = 0; i < p; ++i) { + energy_fractions[i] = 1 - std::pow(10, -1 - i); + } + cutoff = first_sv; bool reached_cutoff = false; double partialSum = 0.0; + int count = 0; std::ofstream outfile(cutoffOutputPath); + for (int sv = first_sv; sv < sing_vals->dim(); ++sv) { partialSum += (*sing_vals)(sv); - for (int i = energy_fractions.size() - 1; i >= 0; i--) + for (int i = count; i < p; ++i) { - if (partialSum / sum > energy_fractions[i]) + if (partialSum / sum > 1.0 - std::pow(10, -1 - i)) { - outfile << "For energy fraction: " << energy_fractions[i] << ", take first " - << sv+1 << " of " << sing_vals->dim() << " basis vectors" << std::endl; - energy_fractions.pop_back(); + outfile << "For energy fraction: 0."; + for (int j = 0; j < i+1; ++j) outfile << "9"; + outfile << ", take first " << sv+1 << " of " << sing_vals->dim() << + " basis vectors" << std::endl; + count += 1; } else { @@ -356,6 +368,7 @@ BasisGenerator::FinalSummary( } if (!reached_cutoff) cutoff = sing_vals->dim(); + outfile << std::fixed << std::setprecision(p+1); outfile << "For energy fraction: " << energyFraction << ", take first " << cutoff << " of " << sing_vals->dim() << " basis vectors" << std::endl; outfile.close(); diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index bc009e606..c9281d060 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -272,10 +272,10 @@ class BasisGenerator * @param[in] cutoffOutputPath Path of the summary file. * @param[in] first_sv First singular vector in the calculaton of energy. */ - void FinalSummary( + void finalSummary( const double energyFraction, int & cutoff, - const std::string cutoffOutputPath, + const std::string cutoffOutputPath = "bg_summary.txt", const int first_sv = 0); protected: From 0505fbba815be58344f287162e8136b6ba44d512 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Sat, 16 Dec 2023 20:59:44 -0800 Subject: [PATCH 06/16] Enable capabilities in combine_samples.cpp --- examples/misc/combine_samples.cpp | 29 +++++++++++++++---- lib/linalg/BasisGenerator.cpp | 48 ++++++++++++++++++++++--------- lib/linalg/BasisGenerator.h | 25 ++++++++++++++-- 3 files changed, 81 insertions(+), 21 deletions(-) diff --git a/examples/misc/combine_samples.cpp b/examples/misc/combine_samples.cpp index 9444bbd24..8ee32d9e5 100644 --- a/examples/misc/combine_samples.cpp +++ b/examples/misc/combine_samples.cpp @@ -53,15 +53,19 @@ int main(int argc, char* argv[]) std::vector sample_names; int snaps = 0; int dim = 0; + int col_min = 1; + int col_max = 1e9; bool subtract_mean = false; bool subtract_offset = false; + std::string generator_filename = "total"; std::string kind = "snapshot"; std::string offset_file; bool offset_arg = false; if (argc >= 2) { - for (int i = 1; i < argc; i++) { + int i = 1; + while (i < argc) { if (!strcmp(argv[i], "basis") || !strcmp(argv[i], "-b")) { if (rank==0) std::cout << "Argument " << i << " identified as basis or -b" << std::endl; @@ -80,9 +84,25 @@ int main(int argc, char* argv[]) if (rank==0) std::cout << "Will subtract mean" << std::endl; subtract_mean = true; } + else if (!strcmp(argv[i], "file") || !strcmp(argv[i], "-f")) { + i += 1; + generator_filename = argv[i]; + if (rank==0) std::cout << "Output prefix = " << generator_filename << std::endl; + } + else if (!strcmp(argv[i], "col_min") || !strcmp(argv[i], "-cmin")) { + i += 1; + col_min = std::stoi(argv[i]); + if (rank==0) std::cout << "First column to read = " << col_min << std::endl; + } + else if (!strcmp(argv[i], "col_max") || !strcmp(argv[i], "-cmax")) { + i += 1; + col_max = std::stoi(argv[i]); + if (rank==0) std::cout << "Last column to read = " << col_max << std::endl; + } else { sample_names.push_back(argv[i]); } + i += 1; } } else { @@ -112,7 +132,6 @@ int main(int argc, char* argv[]) CAROM_VERIFY((snaps > 0) && (dim > 0)); /*-- Load data from input files --*/ - std::string generator_filename = "total"; std::unique_ptr static_basis_generator; static_basis_generator.reset(new CAROM::BasisGenerator( CAROM::Options(dim, snaps).setMaxBasisDimension(snaps), false, @@ -120,7 +139,7 @@ int main(int argc, char* argv[]) if (rank==0) std::cout << "Loading data from " << kind << std::endl; for(const auto& sample_name: sample_names) { - static_basis_generator->loadSamples(sample_name, kind); + static_basis_generator->loadSampleRange(sample_name, kind, col_min, col_max); } if (rank==0) std::cout << "Saving data uploaded as a snapshot matrix" << @@ -134,7 +153,7 @@ int main(int argc, char* argv[]) int rom_dim = static_basis_generator->getSpatialBasis()->numColumns(); if (rank==0) std::cout << "U ROM Dimension: " << rom_dim << std::endl; static_basis_generator->endSamples(); - static_basis_generator->finalSummary(0.99999999, rdim); + if (rank==0) static_basis_generator->finalSummary(0.99999999, rdim); } else { /*-- load data from hdf5 file to find the mean and subtract it --*/ @@ -190,7 +209,7 @@ int main(int argc, char* argv[]) int rom_dim = static_basis_generator2->getSpatialBasis()->numColumns(); if (rank==0) std::cout << "U ROM Dimension: " << rom_dim << std::endl; static_basis_generator2->endSamples(); - static_basis_generator2->finalSummary(0.99999999, rdim); + if (rank==0) static_basis_generator2->finalSummary(0.99999999, rdim); static_basis_generator2 = nullptr; } diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index fa13c7a64..ed92363e9 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -163,9 +163,10 @@ BasisGenerator::takeSample( } void -BasisGenerator::loadSamples(const std::string& base_file_name, +BasisGenerator::loadSampleRange(const std::string& base_file_name, const std::string& kind, - int cut_off, + int col_min, + int col_max, Database::formats db_format) { CAROM_ASSERT(!base_file_name.empty()); @@ -188,10 +189,12 @@ BasisGenerator::loadSamples(const std::string& base_file_name, int num_rows = mat->numRows(); int num_cols = mat->numColumns(); - int max_cols = num_cols; - if (cut_off < num_cols) max_cols = cut_off; + if (col_min < 1) col_min = 1; + if (col_max > num_cols) col_max = num_cols; + + CAROM_VERIFY(col_max >= col_min); - for (int j = 0; j < max_cols; j++) { + for (int j = col_min-1; j < col_max; j++) { double* u_in = new double[num_rows]; for (int i = 0; i < num_rows; i++) { if (kind == "basis") { @@ -206,6 +209,15 @@ BasisGenerator::loadSamples(const std::string& base_file_name, } } +void +BasisGenerator::loadSamples(const std::string& base_file_name, + const std::string& kind, + int cutoff, + Database::formats db_format) +{ + loadSampleRange(base_file_name, kind, 1, cutoff, db_format); +} + double BasisGenerator::computeNextSampleTime( double* u_in, @@ -341,7 +353,13 @@ BasisGenerator::finalSummary( double partialSum = 0.0; int count = 0; - std::ofstream outfile(cutoffOutputPath); + std::ostream* output_stream; + + if (!cutoffOutputPath.empty()) { + output_stream = new std::ofstream(cutoffOutputPath); + } else { + output_stream = &std::cout; + } for (int sv = first_sv; sv < sing_vals->dim(); ++sv) { partialSum += (*sing_vals)(sv); @@ -349,10 +367,10 @@ BasisGenerator::finalSummary( { if (partialSum / sum > 1.0 - std::pow(10, -1 - i)) { - outfile << "For energy fraction: 0."; - for (int j = 0; j < i+1; ++j) outfile << "9"; - outfile << ", take first " << sv+1 << " of " << sing_vals->dim() << - " basis vectors" << std::endl; + *output_stream << "For energy fraction: 0."; + for (int j = 0; j < i+1; ++j) *output_stream << "9"; + *output_stream << ", take first " << sv+1 << " of " + << sing_vals->dim() << " basis vectors" << std::endl; count += 1; } else @@ -368,10 +386,14 @@ BasisGenerator::finalSummary( } if (!reached_cutoff) cutoff = sing_vals->dim(); - outfile << std::fixed << std::setprecision(p+1); - outfile << "For energy fraction: " << energyFraction << ", take first " + *output_stream << std::fixed << std::setprecision(p+1); + *output_stream << "For energy fraction: " << energyFraction << ", take first " << cutoff << " of " << sing_vals->dim() << " basis vectors" << std::endl; - outfile.close(); + + if (!cutoffOutputPath.empty()) { + static_cast(output_stream)->close(); + delete output_stream; + } } BasisGenerator::~BasisGenerator() diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index c9281d060..c8e7af7a4 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -143,6 +143,25 @@ class BasisGenerator } } + /** + * @brief Load previously saved sample (basis or state) + * within a column range. + * + * @param[in] base_file_name The base part of the name of the files + * holding the basis / snapshot vectors. + * @param[in] kind A string equal to "basis" or "snapshot", representing + * which kind of data to load. + * @param[in] col_min The first basis or snapshot to read. + * @param[in] col_max The last basis or snapshot to read. + * @param[in] db_format Format of the file to read. + */ + void + loadSampleRange(const std::string& base_file_name, + const std::string& kind = "basis", + int col_min = 1, + int col_max = 1e9, + Database::formats db_format = Database::HDF5); + /** * @brief Load previously saved sample (basis or state). * @@ -150,13 +169,13 @@ class BasisGenerator * holding the basis / snapshot vectors. * @param[in] kind A string equal to "basis" or "snapshot", representing * which kind of data to load. - * @param[in] cut_off The maximum number of bases or snapshots to read. + * @param[in] cutoff The maximum number of bases or snapshots to read. * @param[in] db_format Format of the file to read. */ void loadSamples(const std::string& base_file_name, const std::string& kind = "basis", - int cut_off = 1e9, + int cutoff = 1e9, Database::formats db_format = Database::HDF5); /** @@ -275,7 +294,7 @@ class BasisGenerator void finalSummary( const double energyFraction, int & cutoff, - const std::string cutoffOutputPath = "bg_summary.txt", + const std::string cutoffOutputPath = "", const int first_sv = 0); protected: From bf6150cff7e868b2e464e0b75e9da65b239037b7 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Mon, 18 Dec 2023 13:15:28 -0800 Subject: [PATCH 07/16] Astyle --- lib/linalg/BasisGenerator.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index ed92363e9..d978cbac5 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -164,10 +164,10 @@ BasisGenerator::takeSample( void BasisGenerator::loadSampleRange(const std::string& base_file_name, - const std::string& kind, - int col_min, - int col_max, - Database::formats db_format) + const std::string& kind, + int col_min, + int col_max, + Database::formats db_format) { CAROM_ASSERT(!base_file_name.empty()); CAROM_VERIFY(kind == "basis" || kind == "snapshot"); @@ -369,7 +369,7 @@ BasisGenerator::finalSummary( { *output_stream << "For energy fraction: 0."; for (int j = 0; j < i+1; ++j) *output_stream << "9"; - *output_stream << ", take first " << sv+1 << " of " + *output_stream << ", take first " << sv+1 << " of " << sing_vals->dim() << " basis vectors" << std::endl; count += 1; } @@ -388,7 +388,7 @@ BasisGenerator::finalSummary( if (!reached_cutoff) cutoff = sing_vals->dim(); *output_stream << std::fixed << std::setprecision(p+1); *output_stream << "For energy fraction: " << energyFraction << ", take first " - << cutoff << " of " << sing_vals->dim() << " basis vectors" << std::endl; + << cutoff << " of " << sing_vals->dim() << " basis vectors" << std::endl; if (!cutoffOutputPath.empty()) { static_cast(output_stream)->close(); From e2872d82870ca6effef4384241b98a0c2a2c8943 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Mon, 18 Dec 2023 14:04:11 -0800 Subject: [PATCH 08/16] Remove spaces --- lib/linalg/BasisGenerator.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index c8e7af7a4..5f078471e 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -157,10 +157,10 @@ class BasisGenerator */ void loadSampleRange(const std::string& base_file_name, - const std::string& kind = "basis", - int col_min = 1, - int col_max = 1e9, - Database::formats db_format = Database::HDF5); + const std::string& kind = "basis", + int col_min = 1, + int col_max = 1e9, + Database::formats db_format = Database::HDF5); /** * @brief Load previously saved sample (basis or state). From b57ec2be3bcd8d61d07e437540151aa2d28d4a66 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Thu, 15 Feb 2024 09:23:26 -0800 Subject: [PATCH 09/16] Run astyle --- lib/linalg/BasisGenerator.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index d0179f611..6baa7ebc5 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -144,7 +144,7 @@ class BasisGenerator } /** - * @brief Load previously saved sample (basis or state) + * @brief Load previously saved sample (basis or state) * within a column range. * * @param[in] base_file_name The base part of the name of the files @@ -284,7 +284,7 @@ class BasisGenerator } /** - * @brief Prints the summary of recommended numbers of basis vectors. + * @brief Prints the summary of recommended numbers of basis vectors. * * @param[in] energyFraction Energy Fraction. * @param[in] cutoff Number of basis vectors selected. @@ -293,8 +293,8 @@ class BasisGenerator */ void finalSummary( const double energyFraction, - int & cutoff, - const std::string cutoffOutputPath = "", + int & cutoff, + const std::string cutoffOutputPath = "", const int first_sv = 0); protected: From ff4baf0cf58187173c1f0e69a83a1e4453fe8d67 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Thu, 7 Mar 2024 08:34:23 -0800 Subject: [PATCH 10/16] Change to 1-base --- examples/misc/combine_samples.cpp | 2 +- lib/linalg/BasisGenerator.cpp | 9 +++++---- lib/linalg/BasisGenerator.h | 3 +-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/misc/combine_samples.cpp b/examples/misc/combine_samples.cpp index dc726acee..9a2ae2f3e 100644 --- a/examples/misc/combine_samples.cpp +++ b/examples/misc/combine_samples.cpp @@ -139,7 +139,7 @@ int main(int argc, char* argv[]) if (rank==0) std::cout << "Loading data from " << kind << std::endl; for(const auto& sample_name: sample_names) { - static_basis_generator->loadSampleRange(sample_name, kind, col_min, col_max); + static_basis_generator->loadSampleRange(sample_name, kind, col_min-1, col_max-1); } if (rank==0) std::cout << "Saving data uploaded as a snapshot matrix" << diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index 9a5e4fa12..c005132ca 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -22,6 +22,7 @@ #include "svd/IncrementalSVDBrand.h" #include +#include namespace CAROM { @@ -189,12 +190,12 @@ BasisGenerator::loadSampleRange(const std::string& base_file_name, int num_rows = mat->numRows(); int num_cols = mat->numColumns(); - if (col_min < 1) col_min = 1; - if (col_max > num_cols) col_max = num_cols; + if (col_min < 0) col_min = 0; + if (col_max > num_cols-1) col_max = num_cols-1; CAROM_VERIFY(col_max >= col_min); - for (int j = col_min-1; j < col_max; j++) { + for (int j = col_min; j <= col_max; j++) { double* u_in = new double[num_rows]; for (int i = 0; i < num_rows; i++) { if (kind == "basis") { @@ -215,7 +216,7 @@ BasisGenerator::loadSamples(const std::string& base_file_name, int cutoff, Database::formats db_format) { - loadSampleRange(base_file_name, kind, 1, cutoff, db_format); + loadSampleRange(base_file_name, kind, 0, cutoff-1, db_format); } double diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index 6baa7ebc5..4fbcfb1e6 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -23,7 +23,6 @@ #include "mpi.h" #include -#include /* Use C++11 built-in shared pointers if available; else fallback to Boost. */ #if __cplusplus >= 201103L @@ -158,7 +157,7 @@ class BasisGenerator void loadSampleRange(const std::string& base_file_name, const std::string& kind = "basis", - int col_min = 1, + int col_min = 0, int col_max = 1e9, Database::formats db_format = Database::HDF5); From 0602769cd7352545babdc70e9596d90d42c0550a Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Thu, 7 Mar 2024 08:35:57 -0800 Subject: [PATCH 11/16] Change comments --- lib/linalg/BasisGenerator.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index 4fbcfb1e6..749ce90c1 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -147,11 +147,11 @@ class BasisGenerator * within a column range. * * @param[in] base_file_name The base part of the name of the files - * holding the basis / snapshot vectors. + * holding the basis/snapshot vectors. * @param[in] kind A string equal to "basis" or "snapshot", representing * which kind of data to load. - * @param[in] col_min The first basis or snapshot to read. - * @param[in] col_max The last basis or snapshot to read. + * @param[in] col_min The first basis/snapshot vector to read. + * @param[in] col_max The last basis/snapshot vector to read. * @param[in] db_format Format of the file to read. */ void @@ -165,10 +165,10 @@ class BasisGenerator * @brief Load previously saved sample (basis or state). * * @param[in] base_file_name The base part of the name of the files - * holding the basis / snapshot vectors. + * holding the basis/snapshot vectors. * @param[in] kind A string equal to "basis" or "snapshot", representing * which kind of data to load. - * @param[in] cutoff The maximum number of bases or snapshots to read. + * @param[in] cutoff The maximum number of basis/snapshot vectors to read. * @param[in] db_format Format of the file to read. */ void From ba33c97e1e981a721415291bdd7c8287e4d01f48 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Thu, 7 Mar 2024 08:51:46 -0800 Subject: [PATCH 12/16] Change to energy fraction threshold --- examples/misc/combine_samples.cpp | 4 ++-- examples/prom/mixed_nonlinear_diffusion.cpp | 2 +- examples/prom/nonlinear_elasticity_global_rom.cpp | 2 +- lib/linalg/BasisGenerator.cpp | 8 ++++---- lib/linalg/BasisGenerator.h | 11 ++++++----- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/examples/misc/combine_samples.cpp b/examples/misc/combine_samples.cpp index 9a2ae2f3e..c2e2ef427 100644 --- a/examples/misc/combine_samples.cpp +++ b/examples/misc/combine_samples.cpp @@ -153,7 +153,7 @@ int main(int argc, char* argv[]) int rom_dim = static_basis_generator->getSpatialBasis()->numColumns(); if (rank==0) std::cout << "U ROM Dimension: " << rom_dim << std::endl; static_basis_generator->endSamples(); - if (rank==0) static_basis_generator->finalSummary(0.99999999, rdim); + if (rank==0) static_basis_generator->finalSummary(1e-8, rdim); } else { /*-- load data from hdf5 file to find the mean and subtract it --*/ @@ -209,7 +209,7 @@ int main(int argc, char* argv[]) int rom_dim = static_basis_generator2->getSpatialBasis()->numColumns(); if (rank==0) std::cout << "U ROM Dimension: " << rom_dim << std::endl; static_basis_generator2->endSamples(); - if (rank==0) static_basis_generator2->finalSummary(0.99999999, rdim); + if (rank==0) static_basis_generator2->finalSummary(1e-8, rdim); static_basis_generator2 = nullptr; } diff --git a/examples/prom/mixed_nonlinear_diffusion.cpp b/examples/prom/mixed_nonlinear_diffusion.cpp index 2079e7899..5d1e15f59 100644 --- a/examples/prom/mixed_nonlinear_diffusion.cpp +++ b/examples/prom/mixed_nonlinear_diffusion.cpp @@ -448,7 +448,7 @@ void MergeBasis(const int dimFOM, const int nparam, const int max_num_snapshots, generator.endSamples(); // save the merged basis file int cutoff = 0; - generator.finalSummary(0.9999, cutoff, "mergedSV_" + name); + generator.finalSummary(1e-4, cutoff, "mergedSV_" + name); } // TODO: move this to the library? diff --git a/examples/prom/nonlinear_elasticity_global_rom.cpp b/examples/prom/nonlinear_elasticity_global_rom.cpp index b79316317..911596035 100644 --- a/examples/prom/nonlinear_elasticity_global_rom.cpp +++ b/examples/prom/nonlinear_elasticity_global_rom.cpp @@ -328,7 +328,7 @@ void MergeBasis(const int dimFOM, const int nparam, const int max_num_snapshots, generator.endSamples(); // save the merged basis file int cutoff = 0; - generator.finalSummary(0.9999999, cutoff, "mergedSV_" + name + ".txt"); + generator.finalSummary(1e-7, cutoff, "mergedSV_" + name + ".txt"); } const CAROM::Matrix *GetSnapshotMatrix(const int dimFOM, const int nparam, diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index c005132ca..2dd581657 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -327,7 +327,7 @@ BasisGenerator::resetDt( void BasisGenerator::finalSummary( - const double energyFraction, + const double energyFractionThreshold, int & cutoff, const std::string cutoffOutputPath, const int first_sv) @@ -342,7 +342,7 @@ BasisGenerator::finalSummary( sum += (*sing_vals)(sv); } - int p = std::floor(-std::log10(1.0 - energyFraction)); + int p = std::floor(-std::log10(energyFractionThreshold)); std::vector energy_fractions(p); for (int i = 0; i < p; ++i) { @@ -379,7 +379,7 @@ BasisGenerator::finalSummary( break; } } - if (!reached_cutoff && partialSum / sum > energyFraction) + if (!reached_cutoff && partialSum / sum > 1.0 - energyFractionThreshold) { cutoff = sv+1; reached_cutoff = true; @@ -388,7 +388,7 @@ BasisGenerator::finalSummary( if (!reached_cutoff) cutoff = sing_vals->dim(); *output_stream << std::fixed << std::setprecision(p+1); - *output_stream << "For energy fraction: " << energyFraction << ", take first " + *output_stream << "For energy fraction: " << 1.0 - energyFractionThreshold << ", take first " << cutoff << " of " << sing_vals->dim() << " basis vectors" << std::endl; if (!cutoffOutputPath.empty()) { diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index 749ce90c1..7eac2a077 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -285,13 +285,14 @@ class BasisGenerator /** * @brief Prints the summary of recommended numbers of basis vectors. * - * @param[in] energyFraction Energy Fraction. - * @param[in] cutoff Number of basis vectors selected. - * @param[in] cutoffOutputPath Path of the summary file. - * @param[in] first_sv First singular vector in the calculaton of energy. + * @param[in] energyFractionThreshold Energy Fraction threshold + * (difference of energy difference from 1.0). + * @param[in] cutoff Number of basis vectors selected. + * @param[in] cutoffOutputPath Path of the summary file. + * @param[in] first_sv First singular vector in the calculaton of energy. */ void finalSummary( - const double energyFraction, + const double energyFractionThreshold, int & cutoff, const std::string cutoffOutputPath = "", const int first_sv = 0); From d774492525fe6208090a7c0c9184abcac61534f6 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Thu, 7 Mar 2024 08:54:35 -0800 Subject: [PATCH 13/16] Pass the string by reference --- lib/linalg/BasisGenerator.cpp | 2 +- lib/linalg/BasisGenerator.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index 2dd581657..ae8326deb 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -329,7 +329,7 @@ void BasisGenerator::finalSummary( const double energyFractionThreshold, int & cutoff, - const std::string cutoffOutputPath, + const std::string & cutoffOutputPath, const int first_sv) { const int rom_dim = getSpatialBasis()->numColumns(); diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index 7eac2a077..202af25f5 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -294,7 +294,7 @@ class BasisGenerator void finalSummary( const double energyFractionThreshold, int & cutoff, - const std::string cutoffOutputPath = "", + const std::string & cutoffOutputPath = "", const int first_sv = 0); protected: From 6d4f7e5d49121ac253f6b47204e4b1aa54e09305 Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Thu, 7 Mar 2024 08:56:19 -0800 Subject: [PATCH 14/16] Astyle --- examples/misc/combine_samples.cpp | 3 ++- lib/linalg/BasisGenerator.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/misc/combine_samples.cpp b/examples/misc/combine_samples.cpp index c2e2ef427..8ab5f19e1 100644 --- a/examples/misc/combine_samples.cpp +++ b/examples/misc/combine_samples.cpp @@ -139,7 +139,8 @@ int main(int argc, char* argv[]) if (rank==0) std::cout << "Loading data from " << kind << std::endl; for(const auto& sample_name: sample_names) { - static_basis_generator->loadSampleRange(sample_name, kind, col_min-1, col_max-1); + static_basis_generator->loadSampleRange(sample_name, kind, col_min-1, + col_max-1); } if (rank==0) std::cout << "Saving data uploaded as a snapshot matrix" << diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index ae8326deb..c1a94a11b 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -388,7 +388,8 @@ BasisGenerator::finalSummary( if (!reached_cutoff) cutoff = sing_vals->dim(); *output_stream << std::fixed << std::setprecision(p+1); - *output_stream << "For energy fraction: " << 1.0 - energyFractionThreshold << ", take first " + *output_stream << "For energy fraction: " << 1.0 - energyFractionThreshold << + ", take first " << cutoff << " of " << sing_vals->dim() << " basis vectors" << std::endl; if (!cutoffOutputPath.empty()) { From 7ba3a8f1851428a980a22cd57a98b47111c7ab9f Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Sun, 10 Mar 2024 22:27:41 -0700 Subject: [PATCH 15/16] Modify brief --- lib/linalg/BasisGenerator.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index 202af25f5..88a600b5f 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -286,7 +286,7 @@ class BasisGenerator * @brief Prints the summary of recommended numbers of basis vectors. * * @param[in] energyFractionThreshold Energy Fraction threshold - * (difference of energy difference from 1.0). + * (energy fraction = 1.0 - energyFractionThreshold). * @param[in] cutoff Number of basis vectors selected. * @param[in] cutoffOutputPath Path of the summary file. * @param[in] first_sv First singular vector in the calculaton of energy. From 751887af88769afb3c4dc3e808ef49176cd2d2cc Mon Sep 17 00:00:00 2001 From: Siu Wun Cheung Date: Mon, 11 Mar 2024 08:38:06 -0700 Subject: [PATCH 16/16] Run astyle --- lib/linalg/BasisGenerator.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index 88a600b5f..2857db8d3 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -285,7 +285,7 @@ class BasisGenerator /** * @brief Prints the summary of recommended numbers of basis vectors. * - * @param[in] energyFractionThreshold Energy Fraction threshold + * @param[in] energyFractionThreshold Energy Fraction threshold * (energy fraction = 1.0 - energyFractionThreshold). * @param[in] cutoff Number of basis vectors selected. * @param[in] cutoffOutputPath Path of the summary file.