From fde345cde6976115746c82d11c91080e3feb5c7c Mon Sep 17 00:00:00 2001 From: "Kevin\" Seung Whan Chung" Date: Thu, 29 Feb 2024 10:41:13 -0800 Subject: [PATCH] Removal of time interval and closing files within BasisWriter::writeBasis (#261) * BasisWriter::writeBasis create/open a file and closes the file at the end. * stylization. * HDFDatabase::putIntegerArray - overwrites if the dataset exists. * enforce single time interval in Options. * HDFDatabase::putIntegerArray does not allow overwrite. * BasisWriter::writeBasis always create the file, which will overwrite the exisiting file. * add a header and stylization. * remove increase time interval test, as time interval is fixed to 1. * add an error message for a guidance. * remove test_SVD from ci workflow. * SVD::increaseTimeInterval - allow the initial time interval. * minor fix in test_IncrementalSVDBrand. * reflecting the comments. * removed the concept of time interval in BasisReader. time argument remains for backward compatibility. * BasisWriter: removed the concept of time interval. * minor fix in BasisReader. * SVD: removed the concept of time intervals. * BasisGenerator: removed the concept of time interval. * add test_SVD.cpp for resolving conflict. * stylization. * changed function signature of BasisGenerator::takeSample. * rebased to resolve conflict. * changed function signature for BasisReader::getSpatialBasis. * changed function signature for BasisReader::getTemporalBasis. * changed function signature for BasisReader::getSingularValues. * changed function signature for BasisReader::getSnapshotMatrix. * removed Options::max_time_intervals * SVD::isNewSample -> isFirstSample * update comments for StaticSVD::isBasisCurrent * minor comment updates. * stylization. * update dataset names and added python script for dataset name update. * fixed the ci workflow * minor fix in ci workflow. * minor fix * removed BasisReader:isNewBasis * minor doxygen comment update --- .github/workflows/run_tests/action.yml | 11 +- CMakeLists.txt | 4 +- examples/misc/combine_samples.cpp | 6 +- .../prom/de_parametric_maxwell_greedy.cpp | 8 +- examples/prom/dg_advection_global_rom.cpp | 12 +- .../dg_advection_local_rom_matrix_interp.cpp | 12 +- examples/prom/grad_div_global_rom.cpp | 8 +- .../prom/linear_elasticity_global_rom.cpp | 8 +- examples/prom/maxwell_global_rom.cpp | 8 +- examples/prom/maxwell_local_rom_greedy.cpp | 8 +- examples/prom/mixed_nonlinear_diffusion.cpp | 24 +- .../prom/nonlinear_elasticity_global_rom.cpp | 22 +- examples/prom/poisson_global_rom.cpp | 8 +- examples/prom/poisson_local_rom_greedy.cpp | 8 +- lib/linalg/BasisGenerator.cpp | 35 +-- lib/linalg/BasisGenerator.h | 35 --- lib/linalg/BasisReader.cpp | 217 ++++-------------- lib/linalg/BasisReader.h | 136 +---------- lib/linalg/BasisWriter.cpp | 64 ++---- lib/linalg/BasisWriter.h | 6 - lib/linalg/Options.h | 28 +-- lib/linalg/svd/IncrementalSVD.cpp | 17 +- lib/linalg/svd/IncrementalSVD.h | 8 +- lib/linalg/svd/IncrementalSVDBrand.cpp | 22 +- lib/linalg/svd/IncrementalSVDBrand.h | 3 +- lib/linalg/svd/IncrementalSVDFastUpdate.cpp | 22 +- lib/linalg/svd/IncrementalSVDFastUpdate.h | 3 +- lib/linalg/svd/IncrementalSVDStandard.cpp | 22 +- lib/linalg/svd/IncrementalSVDStandard.h | 3 +- lib/linalg/svd/RandomizedSVD.cpp | 2 +- lib/linalg/svd/SVD.cpp | 8 +- lib/linalg/svd/SVD.h | 77 +------ lib/linalg/svd/StaticSVD.cpp | 50 ++-- lib/linalg/svd/StaticSVD.h | 15 +- lib/utils/CSVDatabase.cpp | 2 + lib/utils/CSVDatabase.h | 4 +- lib/utils/Database.cpp | 23 ++ lib/utils/Database.h | 6 +- lib/utils/HDFDatabase.cpp | 5 + lib/utils/HDFDatabase.h | 4 +- regression_tests/basisComparator.cpp | 8 +- scripts/data/update_basis_format.py | 28 +++ .../basis_conversion/test_basis.000000 | Bin 0 -> 5984 bytes .../test_basis_snapshot.000000 | Bin 0 -> 4272 bytes unit_tests/random_test.cpp | 6 +- unit_tests/smoke_static.cpp | 8 +- unit_tests/smoke_test.cpp | 4 +- unit_tests/test_IncrementalSVD.cpp | 3 +- unit_tests/test_IncrementalSVDBrand.cpp | 8 +- unit_tests/test_RandomizedSVD.cpp | 40 ++-- unit_tests/test_SVD.cpp | 14 -- unit_tests/test_StaticSVD.cpp | 22 +- unit_tests/test_basis_conversion.cpp | 124 ++++++++++ unit_tests/test_include.cpp | 4 +- unit_tests/uneven_dist.cpp | 4 +- unit_tests/weak_scaling.cpp | 2 +- 56 files changed, 465 insertions(+), 774 deletions(-) create mode 100644 scripts/data/update_basis_format.py create mode 100644 unit_tests/baselines/basis_conversion/test_basis.000000 create mode 100644 unit_tests/baselines/basis_conversion/test_basis_snapshot.000000 create mode 100644 unit_tests/test_basis_conversion.cpp diff --git a/.github/workflows/run_tests/action.yml b/.github/workflows/run_tests/action.yml index 34471de9b..17a5cf769 100644 --- a/.github/workflows/run_tests/action.yml +++ b/.github/workflows/run_tests/action.yml @@ -4,7 +4,6 @@ runs: - name: Run unit tests run: | cd ${GITHUB_WORKSPACE}/build - ./tests/test_SVD ./tests/test_Vector ./tests/test_Matrix mpirun -n 3 --oversubscribe ./tests/test_Matrix @@ -24,7 +23,15 @@ runs: mpirun -n 3 --oversubscribe tests/test_StaticSVD ./tests/test_IncrementalSVDBrand mpirun -n 3 --oversubscribe tests/test_IncrementalSVDBrand - + shell: bash + - name: Basis dataset update test + run: | + cd ${GITHUB_WORKSPACE}/build/tests + cp ${GITHUB_WORKSPACE}/unit_tests/baselines/basis_conversion/* ./ + cp ${GITHUB_WORKSPACE}/scripts/data/update_basis_format.py ./ + python3 update_basis_format.py test_basis.000000 + python3 update_basis_format.py test_basis_snapshot.000000 + ./test_basis_conversion shell: bash - name: Run regression tests diff --git a/CMakeLists.txt b/CMakeLists.txt index 98e84260d..443d82d0a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -271,12 +271,12 @@ if(GTEST_FOUND) GNAT QDEIM S_OPT - SVD StaticSVD RandomizedSVD IncrementalSVD IncrementalSVDBrand - GreedyCustomSampler) + GreedyCustomSampler + basis_conversion) foreach(stem IN LISTS unit_test_stems) add_executable(test_${stem} unit_tests/test_${stem}.cpp) target_link_libraries(test_${stem} PRIVATE ROM diff --git a/examples/misc/combine_samples.cpp b/examples/misc/combine_samples.cpp index 77987ab37..f191368f6 100644 --- a/examples/misc/combine_samples.cpp +++ b/examples/misc/combine_samples.cpp @@ -101,8 +101,8 @@ int main(int argc, char* argv[]) for (const auto& sample_name: sample_names) { CAROM::BasisReader reader(sample_name); - dim = reader.getDim(kind, 0); - snaps += reader.getNumSamples(kind, 0); + dim = reader.getDim(kind); + snaps += reader.getNumSamples(kind); if (dimFirst == 0) dimFirst = dim; CAROM_VERIFY(dim == @@ -180,7 +180,7 @@ int main(int argc, char* argv[]) CAROM::Vector snap_cur(num_rows, true); for (int col = 0; col < num_cols; col++) { snap_cur = *snapshots->getColumn(col); - static_basis_generator2->takeSample(snap_cur.getData(), 0.0, false); + static_basis_generator2->takeSample(snap_cur.getData(), false); } /*-- Compute SVD and save file --*/ diff --git a/examples/prom/de_parametric_maxwell_greedy.cpp b/examples/prom/de_parametric_maxwell_greedy.cpp index a0a1a2b0d..56e3db22c 100644 --- a/examples/prom/de_parametric_maxwell_greedy.cpp +++ b/examples/prom/de_parametric_maxwell_greedy.cpp @@ -221,7 +221,7 @@ double simulation() // 12. Set BasisGenerator if offline if (offline) { - options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, update_right_SV); if (myid == 0) cout << "Saving basis to: " << saveBasisName << endl; generator = new CAROM::BasisGenerator(*options, isIncremental, saveBasisName); @@ -316,7 +316,7 @@ double simulation() // 19. take and write snapshot for ROM if (offline) { - bool addSample = generator->takeSample(X.GetData(), 0.0, 0.01); + bool addSample = generator->takeSample(X.GetData()); generator->writeSnapshot(); basisIdentifiers.push_back(saveBasisName); delete generator; @@ -329,7 +329,7 @@ double simulation() // 21. read the reduced basis assembleTimer.Start(); CAROM::BasisReader reader(loadBasisName); - spatialbasis = reader.getSpatialBasis(0.0); + spatialbasis = reader.getSpatialBasis(); numRowRB = spatialbasis->numRows(); numColumnRB = spatialbasis->numColumns(); @@ -513,7 +513,7 @@ double simulation() { mergeTimer.Start(); std::unique_ptr basis_generator; - options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, loadBasisName); for (int i = 0; i < basisIdentifiers.size(); ++i) diff --git a/examples/prom/dg_advection_global_rom.cpp b/examples/prom/dg_advection_global_rom.cpp index 4f28c57d9..e438766e8 100644 --- a/examples/prom/dg_advection_global_rom.cpp +++ b/examples/prom/dg_advection_global_rom.cpp @@ -692,19 +692,19 @@ int main(int argc, char *argv[]) // 10. Set BasisGenerator if offline if (offline) { - options = new CAROM::Options(U->Size(), max_num_snapshots, 1, update_right_SV); + options = new CAROM::Options(U->Size(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisFileName); Vector u_curr(*U); Vector u_centered(U->Size()); subtract(u_curr, u_init, u_centered); - bool addSample = generator->takeSample(u_centered.GetData(), t, dt); + bool addSample = generator->takeSample(u_centered.GetData()); } // 11. The merge phase if (merge) { mergeTimer.Start(); - options = new CAROM::Options(U->Size(), max_num_snapshots, 1, update_right_SV); + options = new CAROM::Options(U->Size(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisName); for (int paramID=0; paramIDnumRows(); numColumnRB = spatialbasis->numColumns(); @@ -831,7 +831,7 @@ int main(int argc, char *argv[]) Vector u_curr(*U); Vector u_centered(U->Size()); subtract(u_curr, u_init, u_centered); - bool addSample = generator->takeSample(u_centered.GetData(), t, dt); + bool addSample = generator->takeSample(u_centered.GetData()); } if (done || ti % vis_steps == 0) diff --git a/examples/prom/dg_advection_local_rom_matrix_interp.cpp b/examples/prom/dg_advection_local_rom_matrix_interp.cpp index 1341c63ac..93f8236e7 100644 --- a/examples/prom/dg_advection_local_rom_matrix_interp.cpp +++ b/examples/prom/dg_advection_local_rom_matrix_interp.cpp @@ -706,12 +706,12 @@ int main(int argc, char *argv[]) // 10. Set BasisGenerator if offline if (offline) { - options = new CAROM::Options(U->Size(), max_num_snapshots, 1, update_right_SV); + options = new CAROM::Options(U->Size(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisName); Vector u_curr(*U); Vector u_centered(U->Size()); subtract(u_curr, u_init, u_centered); - bool addSample = generator->takeSample(u_centered.GetData(), t, dt); + bool addSample = generator->takeSample(u_centered.GetData()); } if (online) @@ -723,11 +723,11 @@ int main(int argc, char *argv[]) CAROM::BasisReader reader(basisName); if (rdim != -1) { - spatialbasis = reader.getSpatialBasis(0.0, rdim); + spatialbasis = reader.getSpatialBasis(rdim); } else { - spatialbasis = reader.getSpatialBasis(0.0, ef); + spatialbasis = reader.getSpatialBasis(ef); } numRowRB = spatialbasis->numRows(); numColumnRB = spatialbasis->numColumns(); @@ -819,7 +819,7 @@ int main(int argc, char *argv[]) CAROM::BasisReader reader(parametricBasisName); MFEM_VERIFY(rdim != -1, "rdim must be used for interpolation."); - CAROM::Matrix* parametricSpatialBasis = reader.getSpatialBasis(0.0, rdim); + CAROM::Matrix* parametricSpatialBasis = reader.getSpatialBasis(rdim); numRowRB = parametricSpatialBasis->numRows(); numColumnRB = parametricSpatialBasis->numColumns(); bases.push_back(parametricSpatialBasis); @@ -932,7 +932,7 @@ int main(int argc, char *argv[]) Vector u_curr(*U); Vector u_centered(U->Size()); subtract(u_curr, u_init, u_centered); - bool addSample = generator->takeSample(u_centered.GetData(), t, dt); + bool addSample = generator->takeSample(u_centered.GetData()); } if (done || ti % vis_steps == 0) diff --git a/examples/prom/grad_div_global_rom.cpp b/examples/prom/grad_div_global_rom.cpp index 22a944854..ea9fc777e 100644 --- a/examples/prom/grad_div_global_rom.cpp +++ b/examples/prom/grad_div_global_rom.cpp @@ -225,7 +225,7 @@ int main(int argc, char *argv[]) // 10. Set BasisGenerator if offline if (offline) { - options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisFileName); } @@ -234,7 +234,7 @@ int main(int argc, char *argv[]) if (merge) { mergeTimer.Start(); - options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisName); for (int paramID=0; paramIDtakeSample(X.GetData(), 0.0, 0.01); + generator->takeSample(X.GetData()); generator->writeSnapshot(); delete generator; delete options; @@ -370,7 +370,7 @@ int main(int argc, char *argv[]) // 20. read the reduced basis assembleTimer.Start(); CAROM::BasisReader reader(basisName); - const CAROM::Matrix* spatialbasis = reader.getSpatialBasis(0.0); + const CAROM::Matrix* spatialbasis = reader.getSpatialBasis(); const int numRowRB = spatialbasis->numRows(); const int numColumnRB = spatialbasis->numColumns(); if (myid == 0) printf("spatial basis dimension is %d x %d\n", numRowRB, diff --git a/examples/prom/linear_elasticity_global_rom.cpp b/examples/prom/linear_elasticity_global_rom.cpp index f84974ebf..cc404e0c3 100644 --- a/examples/prom/linear_elasticity_global_rom.cpp +++ b/examples/prom/linear_elasticity_global_rom.cpp @@ -235,7 +235,7 @@ int main(int argc, char* argv[]) // 10. Set BasisGenerator if offline if (offline) { - options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisFileName); } @@ -244,7 +244,7 @@ int main(int argc, char* argv[]) if (merge) { mergeTimer.Start(); - options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisName); for (int paramID = 0; paramID < nsets; ++paramID) @@ -367,7 +367,7 @@ int main(int argc, char* argv[]) // 18. take and write snapshot for ROM if (offline) { - bool addSample = generator->takeSample(X.GetData(), 0.0, 0.01); + bool addSample = generator->takeSample(X.GetData()); generator->writeSnapshot(); delete generator; delete options; @@ -379,7 +379,7 @@ int main(int argc, char* argv[]) // 20. read the reduced basis assembleTimer.Start(); CAROM::BasisReader reader(basisName); - const CAROM::Matrix* spatialbasis = reader.getSpatialBasis(0.0); + const CAROM::Matrix* spatialbasis = reader.getSpatialBasis(); numRowRB = spatialbasis->numRows(); numColumnRB = spatialbasis->numColumns(); printf("On rank %d, spatial basis dimension is %d x %d\n", myid, diff --git a/examples/prom/maxwell_global_rom.cpp b/examples/prom/maxwell_global_rom.cpp index f9de7d849..69d462583 100644 --- a/examples/prom/maxwell_global_rom.cpp +++ b/examples/prom/maxwell_global_rom.cpp @@ -229,7 +229,7 @@ int main(int argc, char *argv[]) // 10. Set BasisGenerator if offline if (offline) { - options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisFileName); } @@ -238,7 +238,7 @@ int main(int argc, char *argv[]) if (merge) { mergeTimer.Start(); - options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisName); for (int paramID=0; paramIDtakeSample(X.GetData(), 0.0, 0.01); + bool addSample = generator->takeSample(X.GetData()); generator->writeSnapshot(); delete generator; delete options; @@ -360,7 +360,7 @@ int main(int argc, char *argv[]) // 20. read the reduced basis assembleTimer.Start(); CAROM::BasisReader reader(basisName); - spatialbasis = reader.getSpatialBasis(0.0); + spatialbasis = reader.getSpatialBasis(); numRowRB = spatialbasis->numRows(); numColumnRB = spatialbasis->numColumns(); if (myid == 0) printf("spatial basis dimension is %d x %d\n", numRowRB, diff --git a/examples/prom/maxwell_local_rom_greedy.cpp b/examples/prom/maxwell_local_rom_greedy.cpp index 8c75a0f7b..9c5b973ea 100644 --- a/examples/prom/maxwell_local_rom_greedy.cpp +++ b/examples/prom/maxwell_local_rom_greedy.cpp @@ -374,7 +374,7 @@ int main(int argc, char *argv[]) // 12. Set BasisGenerator if offline if (offline) { - options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, update_right_SV); if (myid == 0) cout << "Saving basis to: " << saveBasisName << endl; generator = new CAROM::BasisGenerator(*options, isIncremental, saveBasisName); @@ -469,7 +469,7 @@ int main(int argc, char *argv[]) // 19. take and write snapshot for ROM if (offline) { - bool addSample = generator->takeSample(X.GetData(), 0.0, 0.01); + bool addSample = generator->takeSample(X.GetData()); generator->writeSnapshot(); basisIdentifiers.push_back(saveBasisName); delete generator; @@ -482,7 +482,7 @@ int main(int argc, char *argv[]) // 21. read the reduced basis assembleTimer.Start(); CAROM::BasisReader reader(loadBasisName); - spatialbasis = reader.getSpatialBasis(0.0); + spatialbasis = reader.getSpatialBasis(); numRowRB = spatialbasis->numRows(); numColumnRB = spatialbasis->numColumns(); if (myid == 0) printf("spatial basis dimension is %d x %d\n", numRowRB, @@ -635,7 +635,7 @@ int main(int argc, char *argv[]) { mergeTimer.Start(); std::unique_ptr basis_generator; - options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace->GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, loadBasisName); for (int i = 0; i < basisIdentifiers.size(); ++i) diff --git a/examples/prom/mixed_nonlinear_diffusion.cpp b/examples/prom/mixed_nonlinear_diffusion.cpp index 19ab15dd9..47f01f284 100644 --- a/examples/prom/mixed_nonlinear_diffusion.cpp +++ b/examples/prom/mixed_nonlinear_diffusion.cpp @@ -918,7 +918,7 @@ int main(int argc, char *argv[]) if (online) { CAROM::BasisReader readerR("basisR"); - BR_librom = readerR.getSpatialBasis(0.0); + BR_librom = readerR.getSpatialBasis(); if (rrdim == -1) rrdim = BR_librom->numColumns(); else @@ -931,7 +931,7 @@ int main(int argc, char *argv[]) printf("reduced R dim = %d\n",rrdim); CAROM::BasisReader readerW("basisW"); - BW_librom = readerW.getSpatialBasis(0.0); + BW_librom = readerW.getSpatialBasis(); if (rwdim == -1) rwdim = BW_librom->numColumns(); else @@ -953,7 +953,7 @@ int main(int argc, char *argv[]) */ CAROM::BasisReader readerFR("basisFR"); - FR_librom = readerFR.getSpatialBasis(0.0); + FR_librom = readerFR.getSpatialBasis(); if (nldim == -1) { @@ -1041,7 +1041,7 @@ int main(int argc, char *argv[]) if (hyperreduce_source) { readerS = new CAROM::BasisReader("basisS"); - S_librom = readerS->getSpatialBasis(0.0); + S_librom = readerS->getSpatialBasis(); if (nsdim == -1) { @@ -1214,7 +1214,7 @@ int main(int argc, char *argv[]) if (sampleW && hyperreduce_source) { oper.GetSource(source); - basis_generator_S->takeSample(source.GetData(), t, dt); + basis_generator_S->takeSample(source.GetData()); // TODO: dfdt? In this example, one can implement the exact formula. // In general, one can use finite differences in time (dpdt is computed that way). //basis_generator_S->computeNextSampleTime(p.GetData(), dfdt.GetData(), t); @@ -1224,21 +1224,21 @@ int main(int argc, char *argv[]) { oper.CopyDpDt(dpdt); - basis_generator_R->takeSample(p.GetData(), t, dt); + basis_generator_R->takeSample(p.GetData()); basis_generator_R->computeNextSampleTime(p.GetData(), dpdt.GetData(), t); Vector p_R(p.GetData(), N1); Vector Mp(N1); oper.SetParameters(p); oper.Mult_Mmat(p_R, Mp); - basis_generator_FR->takeSample(Mp.GetData(), t, dt); + basis_generator_FR->takeSample(Mp.GetData()); } if (sampleW) { oper.CopyDpDt_W(dpdt); - basis_generator_W->takeSample(p_W->GetData(), t, dt); + basis_generator_W->takeSample(p_W->GetData()); basis_generator_W->computeNextSampleTime(p_W->GetData(), dpdt.GetData(), t); } } @@ -1403,13 +1403,13 @@ int main(int argc, char *argv[]) oper.CopyDpDt(dpdt); // R space - basis_generator_R->takeSample(p.GetData(), t, dt); + basis_generator_R->takeSample(p.GetData()); Vector p_R(p.GetData(), N1); Vector Mp(N1); oper.SetParameters(p); oper.Mult_Mmat(p_R, Mp); - basis_generator_FR->takeSample(Mp.GetData(), t, dt); + basis_generator_FR->takeSample(Mp.GetData()); // Terminate the sampling and write out information. basis_generator_R->writeSnapshot(); @@ -1418,14 +1418,14 @@ int main(int argc, char *argv[]) // W space // TODO: why call computeNextSampleTime if you just do takeSample on every step anyway? - basis_generator_W->takeSample(p_W->GetData(), t, dt); + basis_generator_W->takeSample(p_W->GetData()); basis_generator_W->writeSnapshot(); oper.GetSource(source); if (hyperreduce_source) { - basis_generator_S->takeSample(source.GetData(), t, dt); + basis_generator_S->takeSample(source.GetData()); basis_generator_S->writeSnapshot(); } diff --git a/examples/prom/nonlinear_elasticity_global_rom.cpp b/examples/prom/nonlinear_elasticity_global_rom.cpp index 7936d143c..677aff4ee 100644 --- a/examples/prom/nonlinear_elasticity_global_rom.cpp +++ b/examples/prom/nonlinear_elasticity_global_rom.cpp @@ -900,7 +900,7 @@ int main(int argc, char *argv[]) readerV = new CAROM::BasisReader("basisV"); } - BV_librom = readerV->getSpatialBasis(0.0); + BV_librom = readerV->getSpatialBasis(); if (rvdim == -1) // Change rvdim rvdim = BV_librom->numColumns(); @@ -914,7 +914,7 @@ int main(int argc, char *argv[]) printf("reduced V dim = %d\n", rvdim); CAROM::BasisReader readerX("basisX"); - BX_librom = readerX.getSpatialBasis(0.0); + BX_librom = readerX.getSpatialBasis(); if (rxdim == -1) // Change rxdim rxdim = BX_librom->numColumns(); @@ -929,7 +929,7 @@ int main(int argc, char *argv[]) // Hyper reduce H CAROM::BasisReader readerH("basisH"); - H_librom = readerH.getSpatialBasis(0.0); + H_librom = readerH.getSpatialBasis(); // Compute sample points if (hdim == -1) @@ -1262,23 +1262,25 @@ int main(int argc, char *argv[]) } // Take samples + // NOTE(kevin): I don't know why this example checks next sample time. + // IncrementalSVD is never turned on in this example and isNextSample is always true. if (x_base_only == false && basis_generator_v->isNextSample(t)) { - basis_generator_v->takeSample(vx_diff.GetBlock(0), t, dt); + basis_generator_v->takeSample(vx_diff.GetBlock(0)); basis_generator_v->computeNextSampleTime(vx_diff.GetBlock(0), dvdt.GetData(), t); - basis_generator_H->takeSample(oper.H_sp.GetData(), t, dt); + basis_generator_H->takeSample(oper.H_sp.GetData()); } if (basis_generator_x->isNextSample(t)) { - basis_generator_x->takeSample(vx_diff.GetBlock(1), t, dt); + basis_generator_x->takeSample(vx_diff.GetBlock(1)); basis_generator_x->computeNextSampleTime(vx_diff.GetBlock(1), dxdt.GetData(), t); if (x_base_only == true) { - basis_generator_H->takeSample(oper.H_sp.GetData(), t, dt); + basis_generator_H->takeSample(oper.H_sp.GetData()); } } } @@ -1363,16 +1365,16 @@ int main(int argc, char *argv[]) // Take samples if (x_base_only == false) { - basis_generator_v->takeSample(vx_diff.GetBlock(0), t, dt); + basis_generator_v->takeSample(vx_diff.GetBlock(0)); basis_generator_v->writeSnapshot(); delete basis_generator_v; } - basis_generator_H->takeSample(oper.H_sp.GetData(), t, dt); + basis_generator_H->takeSample(oper.H_sp.GetData()); basis_generator_H->writeSnapshot(); delete basis_generator_H; - basis_generator_x->takeSample(vx_diff.GetBlock(1), t, dt); + basis_generator_x->takeSample(vx_diff.GetBlock(1)); basis_generator_x->writeSnapshot(); delete basis_generator_x; diff --git a/examples/prom/poisson_global_rom.cpp b/examples/prom/poisson_global_rom.cpp index 8406466ce..f417539ce 100644 --- a/examples/prom/poisson_global_rom.cpp +++ b/examples/prom/poisson_global_rom.cpp @@ -238,7 +238,7 @@ int main(int argc, char *argv[]) // 10. Set BasisGenerator if offline if (offline) { - options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisFileName); } @@ -247,7 +247,7 @@ int main(int argc, char *argv[]) if (merge) { mergeTimer.Start(); - options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, basisName); for (int paramID=0; paramIDtakeSample(X.GetData(), 0.0, 0.01); + bool addSample = generator->takeSample(X.GetData()); generator->writeSnapshot(); delete generator; delete options; @@ -353,7 +353,7 @@ int main(int argc, char *argv[]) // 20. read the reduced basis assembleTimer.Start(); CAROM::BasisReader reader(basisName); - spatialbasis = reader.getSpatialBasis(0.0); + spatialbasis = reader.getSpatialBasis(); numRowRB = spatialbasis->numRows(); numColumnRB = spatialbasis->numColumns(); if (myid == 0) printf("spatial basis dimension is %d x %d\n", numRowRB, diff --git a/examples/prom/poisson_local_rom_greedy.cpp b/examples/prom/poisson_local_rom_greedy.cpp index 72a7eb271..46398dd8c 100644 --- a/examples/prom/poisson_local_rom_greedy.cpp +++ b/examples/prom/poisson_local_rom_greedy.cpp @@ -376,7 +376,7 @@ int main(int argc, char *argv[]) // 12. Set BasisGenerator if offline if (offline) { - options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, update_right_SV); if (myid == 0) cout << "Saving basis to: " << saveBasisName << endl; generator = new CAROM::BasisGenerator(*options, isIncremental, saveBasisName); @@ -454,7 +454,7 @@ int main(int argc, char *argv[]) // 19. take and write snapshot for ROM if (offline) { - bool addSample = generator->takeSample(X.GetData(), 0.0, 0.01); + bool addSample = generator->takeSample(X.GetData()); generator->writeSnapshot(); basisIdentifiers.push_back(saveBasisName); delete generator; @@ -467,7 +467,7 @@ int main(int argc, char *argv[]) // 21. read the reduced basis assembleTimer.Start(); CAROM::BasisReader reader(loadBasisName); - spatialbasis = reader.getSpatialBasis(0.0); + spatialbasis = reader.getSpatialBasis(); numRowRB = spatialbasis->numRows(); numColumnRB = spatialbasis->numColumns(); if (myid == 0) printf("spatial basis dimension is %d x %d\n", numRowRB, @@ -617,7 +617,7 @@ int main(int argc, char *argv[]) if (calc_rel_error || (offline && basisIdentifiers.size() == 1)) { mergeTimer.Start(); - options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, 1, + options = new CAROM::Options(fespace.GetTrueVSize(), max_num_snapshots, update_right_SV); generator = new CAROM::BasisGenerator(*options, isIncremental, loadBasisName); for (int i = 0; i < basisIdentifiers.size(); ++i) diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index 7c1925a78..945c34bda 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -34,10 +34,8 @@ BasisGenerator::BasisGenerator( d_write_snapshots(options.write_snapshots) { CAROM_VERIFY(options.dim > 0); - CAROM_VERIFY(options.samples_per_time_interval > 0); + CAROM_VERIFY(options.max_num_samples > 0); CAROM_VERIFY(options.singular_value_tol >= 0); - CAROM_VERIFY(options.max_time_intervals == -1 - || options.max_time_intervals > 0); CAROM_VERIFY(options.max_basis_dimension > 0); if (incremental) { @@ -129,35 +127,19 @@ BasisGenerator::isNextSample( bool BasisGenerator::takeSample( double* u_in, - double time, - double dt, bool add_without_increase) { CAROM_VERIFY(u_in != 0); - CAROM_VERIFY(time >= 0); + CAROM_VERIFY(d_svd->getNumSamples() < d_svd->getMaxNumSamples()); // Check that u_in is not non-zero. Vector u_vec(u_in, getDim(), true); if (u_vec.norm() == 0.0) { - printf("WARNING: BasisGenerator::takeSample skipped trivial sample at time %.4E\n", - time); + printf("WARNING: BasisGenerator::takeSample skipped trivial sample.\n"); return false; } - if (getNumBasisTimeIntervals() > 0 && - d_svd->isNewTimeInterval()) { - resetDt(dt); - if (d_basis_writer) { - if (d_write_snapshots) { - writeSnapshot(); - } - else { - d_basis_writer->writeBasis("basis"); - } - } - } - - return d_svd->takeSample(u_in, time, add_without_increase); + return d_svd->takeSample(u_in, add_without_increase); } void @@ -172,16 +154,15 @@ BasisGenerator::loadSamples(const std::string& base_file_name, if (d_basis_reader) delete d_basis_reader; d_basis_reader = new BasisReader(base_file_name, db_format); - double time = 0.0; const Matrix* mat; const Vector* singular_vals; if (kind == "basis") { - mat = d_basis_reader->getSpatialBasis(time); - singular_vals = d_basis_reader->getSingularValues(time); + mat = d_basis_reader->getSpatialBasis(); + singular_vals = d_basis_reader->getSingularValues(); } else if (kind == "snapshot") { - mat = d_basis_reader->getSnapshotMatrix(time); + mat = d_basis_reader->getSnapshotMatrix(); } int num_rows = mat->numRows(); @@ -199,7 +180,7 @@ BasisGenerator::loadSamples(const std::string& base_file_name, u_in[i] = mat->item(i,j); } } - d_svd->takeSample(u_in, time, false); + d_svd->takeSample(u_in, false); delete[] u_in; } } diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index ebad59495..7ba7b02c5 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -102,8 +102,6 @@ class BasisGenerator * @pre time >= 0.0 * * @param[in] u_in The state at the specified time. - * @param[in] time The simulation time for the state. - * @param[in] dt The current simulation dt. * @param[in] add_without_increase If true, the addLinearlyDependent is * invoked. This only applies to incremental * SVD. @@ -113,8 +111,6 @@ class BasisGenerator bool takeSample( double* u_in, - double time, - double dt, bool add_without_increase = false); /** @@ -222,37 +218,6 @@ class BasisGenerator return d_svd->getSnapshotMatrix(); } - /** - * @brief Returns the number of time intervals on which different sets of - * basis vectors are defined. - * - * @return The number of time intervals on which there are basis vectors. - */ - int - getNumBasisTimeIntervals() const - { - return d_svd->getNumBasisTimeIntervals(); - } - - /** - * @brief Returns the start time for the requested time interval. - * - * @pre 0 <= which_interval - * @pre which_interval < getNumBasisTimeIntervals() - * - * @param[in] which_interval Time interval whose start time is needed. - * - * @return The start time for the requested time interval. - */ - double - getBasisIntervalStartTime( - int which_interval) const - { - CAROM_VERIFY(0 <= which_interval); - CAROM_VERIFY(which_interval < getNumBasisTimeIntervals()); - return d_svd->getBasisIntervalStartTime(which_interval); - } - /** * @brief Returns the number of samples taken. * diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index 1b5c8583c..b974b3cd1 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -22,7 +22,6 @@ namespace CAROM { BasisReader::BasisReader( const std::string& base_file_name, Database::formats db_format) : - d_last_basis_idx(-1), full_file_name(""), base_file_name_(base_file_name) { @@ -48,18 +47,7 @@ BasisReader::BasisReader( d_database = new CSVDatabase(); } - std::cout << "Opening file: " << full_file_name << std::endl; d_database->open(full_file_name, "r"); - - int num_time_intervals; - double foo; - d_database->getDouble("num_time_intervals", foo); - num_time_intervals = static_cast(foo); - d_time_interval_start_times.resize(num_time_intervals); - for (int i = 0; i < num_time_intervals; ++i) { - sprintf(tmp, "time_%06d", i); - d_database->getDouble(tmp, d_time_interval_start_times[i]); - } } BasisReader::~BasisReader() @@ -69,27 +57,14 @@ BasisReader::~BasisReader() } Matrix* -BasisReader::getSpatialBasis( - double time) +BasisReader::getSpatialBasis() { - CAROM_ASSERT(0 < numTimeIntervals()); - CAROM_ASSERT(0 <= time); - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - d_last_basis_idx = i; - int num_rows = getDim("basis",time); - int num_cols = getNumSamples("basis",time); + int num_rows = getDim("basis"); + int num_cols = getNumSamples("basis"); - char tmp[100]; Matrix* spatial_basis_vectors = new Matrix(num_rows, num_cols, true); - sprintf(tmp, "spatial_basis_%06d", i); - d_database->getDoubleArray(tmp, + + d_database->getDoubleArray("spatial_basis", &spatial_basis_vectors->item(0, 0), num_rows*num_cols); return spatial_basis_vectors; @@ -97,31 +72,18 @@ BasisReader::getSpatialBasis( Matrix* BasisReader::getSpatialBasis( - double time, int n) { - return getSpatialBasis(time, 1, n); + return getSpatialBasis(1, n); } Matrix* BasisReader::getSpatialBasis( - double time, int start_col, int end_col) { - CAROM_ASSERT(0 < numTimeIntervals()); - CAROM_ASSERT(0 <= time); - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - d_last_basis_idx = i; - int num_rows = getDim("basis",time); - int num_cols = getNumSamples("basis",time); + int num_rows = getDim("basis"); + int num_cols = getNumSamples("basis"); char tmp[100]; CAROM_VERIFY(0 < start_col <= num_cols); @@ -129,7 +91,7 @@ BasisReader::getSpatialBasis( int num_cols_to_read = end_col - start_col + 1; Matrix* spatial_basis_vectors = new Matrix(num_rows, num_cols_to_read, true); - sprintf(tmp, "spatial_basis_%06d", i); + sprintf(tmp, "spatial_basis"); d_database->getDoubleArray(tmp, &spatial_basis_vectors->item(0, 0), num_rows*num_cols_to_read, @@ -141,10 +103,9 @@ BasisReader::getSpatialBasis( Matrix* BasisReader::getSpatialBasis( - double time, double ef) { - Vector* sv = getSingularValues(time); + Vector* sv = getSingularValues(); double total_energy = 0.0; double energy = 0.0; for (int i = 0; i < sv->dim(); i++) @@ -164,30 +125,18 @@ BasisReader::getSpatialBasis( } delete sv; - return getSpatialBasis(time, num_used_singular_values); + return getSpatialBasis(num_used_singular_values); } Matrix* -BasisReader::getTemporalBasis( - double time) +BasisReader::getTemporalBasis() { - CAROM_ASSERT(0 < numTimeIntervals()); - CAROM_ASSERT(0 <= time); - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - d_last_basis_idx = i; - int num_rows = getDim("temporal_basis",time); - int num_cols = getNumSamples("temporal_basis",time); + int num_rows = getDim("temporal_basis"); + int num_cols = getNumSamples("temporal_basis"); char tmp[100]; Matrix* temporal_basis_vectors = new Matrix(num_rows, num_cols, true); - sprintf(tmp, "temporal_basis_%06d", i); + sprintf(tmp, "temporal_basis"); d_database->getDoubleArray(tmp, &temporal_basis_vectors->item(0, 0), num_rows*num_cols); @@ -196,31 +145,18 @@ BasisReader::getTemporalBasis( Matrix* BasisReader::getTemporalBasis( - double time, int n) { - return getTemporalBasis(time, 1, n); + return getTemporalBasis(1, n); } Matrix* BasisReader::getTemporalBasis( - double time, int start_col, int end_col) { - CAROM_ASSERT(0 < numTimeIntervals()); - CAROM_ASSERT(0 <= time); - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - d_last_basis_idx = i; - int num_rows = getDim("temporal_basis",time); - int num_cols = getNumSamples("temporal_basis",time); + int num_rows = getDim("temporal_basis"); + int num_cols = getNumSamples("temporal_basis"); char tmp[100]; CAROM_VERIFY(0 < start_col <= num_cols); @@ -228,7 +164,7 @@ BasisReader::getTemporalBasis( int num_cols_to_read = end_col - start_col + 1; Matrix* temporal_basis_vectors = new Matrix(num_rows, num_cols_to_read, true); - sprintf(tmp, "temporal_basis_%06d", i); + sprintf(tmp, "temporal_basis"); d_database->getDoubleArray(tmp, &temporal_basis_vectors->item(0, 0), num_rows*num_cols_to_read, @@ -240,10 +176,9 @@ BasisReader::getTemporalBasis( Matrix* BasisReader::getTemporalBasis( - double time, double ef) { - Vector* sv = getSingularValues(time); + Vector* sv = getSingularValues(); double total_energy = 0.0; double energy = 0.0; for (int i = 0; i < sv->dim(); i++) @@ -263,31 +198,19 @@ BasisReader::getTemporalBasis( } delete sv; - return getTemporalBasis(time, num_used_singular_values); + return getTemporalBasis(num_used_singular_values); } Vector* -BasisReader::getSingularValues( - double time) +BasisReader::getSingularValues() { - CAROM_ASSERT(0 < numTimeIntervals()); - CAROM_ASSERT(0 <= time); - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - d_last_basis_idx = i; char tmp[100]; int size; - sprintf(tmp, "singular_value_size_%06d", i); + sprintf(tmp, "singular_value_size"); d_database->getInteger(tmp, size); Vector* singular_values = new Vector(size, false); - sprintf(tmp, "singular_value_%06d", i); + sprintf(tmp, "singular_value"); d_database->getDoubleArray(tmp, &singular_values->item(0), size); @@ -296,10 +219,9 @@ BasisReader::getSingularValues( Vector* BasisReader::getSingularValues( - double time, double ef) { - Vector* sv = getSingularValues(time); + Vector* sv = getSingularValues(); double total_energy = 0.0; double energy = 0.0; for (int i = 0; i < sv->dim(); i++) @@ -331,85 +253,53 @@ BasisReader::getSingularValues( int BasisReader::getDim( - const std::string kind, - double time) + const std::string kind) { - CAROM_ASSERT(0 < numTimeIntervals()); - CAROM_ASSERT(0 <= time); CAROM_ASSERT((kind == "basis") || (kind == "snapshot") || (kind == "temporal_basis")); - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - d_last_basis_idx = i; char tmp[100]; int num_rows; - if (kind == "basis") sprintf(tmp, "spatial_basis_num_rows_%06d", i); - else if (kind == "snapshot") sprintf(tmp, "snapshot_matrix_num_rows_%06d", i); - else if (kind == "temporal_basis") sprintf(tmp, "temporal_basis_num_rows_%06d", - i); + + if (kind == "basis") sprintf(tmp, "spatial_basis_num_rows"); + else if (kind == "snapshot") sprintf(tmp, "snapshot_matrix_num_rows"); + else if (kind == "temporal_basis") sprintf(tmp, + "temporal_basis_num_rows"); + d_database->getInteger(tmp, num_rows); return num_rows; } int BasisReader::getNumSamples( - const std::string kind, - double time) + const std::string kind) { - CAROM_ASSERT(0 < numTimeIntervals()); - CAROM_ASSERT(0 <= time); CAROM_ASSERT((kind == "basis") || (kind == "snapshot") || (kind == "temporal_basis")); - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - d_last_basis_idx = i; char tmp[100]; int num_cols; - if (kind == "basis") sprintf(tmp, "spatial_basis_num_cols_%06d", i); - else if (kind == "snapshot") sprintf(tmp, "snapshot_matrix_num_cols_%06d", i); - else if (kind == "temporal_basis") sprintf(tmp, "temporal_basis_num_cols_%06d", - i); + + if (kind == "basis") sprintf(tmp, "spatial_basis_num_cols"); + else if (kind == "snapshot") sprintf(tmp, "snapshot_matrix_num_cols"); + else if (kind == "temporal_basis") sprintf(tmp, + "temporal_basis_num_cols"); + d_database->getInteger(tmp, num_cols); return num_cols; } Matrix* -BasisReader::getSnapshotMatrix( - double time) +BasisReader::getSnapshotMatrix() { - CAROM_ASSERT(0 < numTimeIntervals()); - CAROM_ASSERT(0 <= time); - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - d_last_basis_idx = i; - int num_rows = getDim("snapshot",time); - int num_cols = getNumSamples("snapshot",time); + int num_rows = getDim("snapshot"); + int num_cols = getNumSamples("snapshot"); char tmp[100]; Matrix* snapshots = new Matrix(num_rows, num_cols, false); - sprintf(tmp, "snapshot_matrix_%06d", i); + sprintf(tmp, "snapshot_matrix"); d_database->getDoubleArray(tmp, &snapshots->item(0, 0), num_rows*num_cols); @@ -418,31 +308,18 @@ BasisReader::getSnapshotMatrix( Matrix* BasisReader::getSnapshotMatrix( - double time, int n) { - return getSnapshotMatrix(time, 1, n); + return getSnapshotMatrix(1, n); } Matrix* BasisReader::getSnapshotMatrix( - double time, int start_col, int end_col) { - CAROM_ASSERT(0 < numTimeIntervals()); - CAROM_ASSERT(0 <= time); - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - d_last_basis_idx = i; - int num_rows = getDim("snapshot",time); - int num_cols = getNumSamples("snapshot",time); + int num_rows = getDim("snapshot"); + int num_cols = getNumSamples("snapshot"); CAROM_VERIFY(0 < start_col <= num_cols); CAROM_VERIFY(start_col <= end_col && end_col <= num_cols); @@ -450,7 +327,7 @@ BasisReader::getSnapshotMatrix( char tmp[100]; Matrix* snapshots = new Matrix(num_rows, num_cols_to_read, false); - sprintf(tmp, "snapshot_matrix_%06d", i); + sprintf(tmp, "snapshot_matrix"); d_database->getDoubleArray(tmp, &snapshots->item(0, 0), num_rows*num_cols_to_read, diff --git a/lib/linalg/BasisReader.h b/lib/linalg/BasisReader.h index 5cbfb27ef..eb17998a2 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -55,74 +55,27 @@ class BasisReader { ~BasisReader(); /** - * @brief Returns true if the basis vectors at requested time are - * different from the last requested basis vectors. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time + * @brief Returns the spatial basis vectors as a Matrix. * - * @param[in] time Time at which we are interested in the basis vectors. - * - * @return True if the basis vectors at the requested time are different - * from the last requested basis vectors. - */ - bool - isNewBasis( - double time) - { - CAROM_VERIFY(0 < numTimeIntervals()); - CAROM_VERIFY(0 <= time); - bool result = false; - if (d_last_basis_idx == -1) { - result = true; - } - else { - int num_time_intervals = numTimeIntervals(); - int i; - for (i = 0; i < num_time_intervals-1; ++i) { - if (d_time_interval_start_times[i] <= time && - time < d_time_interval_start_times[i+1]) { - break; - } - } - result = i != d_last_basis_idx; - } - return result; - } - - /** - * - * @brief Returns the spatial basis vectors for the requested time as a - * Matrix. - * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time - * - * @param[in] time Time for which we want the basis vectors. - * - * @return The spatial basis vectors for the requested time. + * @return The spatial basis vectors. */ Matrix* - getSpatialBasis( - double time); + getSpatialBasis(); /** * * @brief Returns the first n spatial basis vectors for the requested time * as a Matrix. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time * @pre 0 < n <= numColumns() * - * @param[in] time Time for which we want the basis vectors. * @param[in] n The number of spatial basis vectors desired. * * @return The spatial basis vectors for the requested time. */ Matrix* getSpatialBasis( - double time, int n); /** @@ -130,12 +83,9 @@ class BasisReader { * @brief Returns spatial basis vectors from start_col to end_col for the * requested time as a Matrix. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time * @pre 0 < start_col <= numColumns() * @pre start_col <= end_col <= numColumns() * - * @param[in] time Time for which we want the basis vectors. * @param[in] start_col The starting column desired. * @param[in] end_col The starting column desired. * @@ -143,7 +93,6 @@ class BasisReader { */ Matrix* getSpatialBasis( - double time, int start_col, int end_col); @@ -152,18 +101,14 @@ class BasisReader { * @brief Returns the first n spatial basis vectors for the requested time * as a Matrix that capture the given energy fraction. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time * @pre 0 <= ef <= 1.0 * - * @param[in] time Time for which we want the basis vectors. * @param[in] ef The desired energy fraction. * * @return The spatial basis vectors for the requested time. */ Matrix* getSpatialBasis( - double time, double ef); /** @@ -171,34 +116,24 @@ class BasisReader { * @brief Returns the temporal basis vectors for the requested time as * a Matrix. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time - * - * @param[in] time Time for which we want the basis vectors. - * * @return The temporal basis vectors for the requested time. */ Matrix* - getTemporalBasis( - double time); + getTemporalBasis(); /** * * @brief Returns the first n temporal basis vectors for the requested time * as a Matrix. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time * @pre 0 < n <= numColumns() * - * @param[in] time Time for which we want the basis vectors. * @param[in] n The number of temporal basis vectors desired. * * @return The temporal basis vectors for the requested time. */ Matrix* getTemporalBasis( - double time, int n); /** @@ -206,12 +141,9 @@ class BasisReader { * @brief Returns temporal basis vectors from start_col to end_col for the * requested time as a Matrix. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time * @pre 0 < start_col <= numColumns() * @pre start_col <= end_col <= numColumns() * - * @param[in] time Time for which we want the basis vectors. * @param[in] start_col The starting column desired. * @param[in] end_col The starting column desired. * @@ -219,7 +151,6 @@ class BasisReader { */ Matrix* getTemporalBasis( - double time, int start_col, int end_col); @@ -228,52 +159,38 @@ class BasisReader { * @brief Returns the first n temporal basis vectors for the requested time * as a Matrix that capture the given energy fraction. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time * @pre 0 <= ef <= 1.0 * - * @param[in] time Time for which we want the basis vectors. * @param[in] ef The desired energy fraction. * * @return The temporal basis vectors for the requested time. */ Matrix* getTemporalBasis( - double time, double ef); /** * * @brief Returns the singular values for the requested time. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time - * - * @param[in] time Time for which we want the basis vectors. - * * @return The temporal basis vectors for the requested time. */ Vector* - getSingularValues( - double time); + getSingularValues(); /** * * @brief Returns the largest singular values for the requested time * that capture the given energy fraction. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time * @pre 0 <= ef <= 1.0 * - * @param[in] time Time for which we want the basis vectors. * @param[in] ef The desired energy fraction. * * @return The temporal basis vectors for the requested time. */ Vector* getSingularValues( - double time, double ef); /** @@ -284,8 +201,7 @@ class BasisReader { */ int getDim( - const std::string kind, - double time); + const std::string kind); /** * @@ -295,52 +211,38 @@ class BasisReader { */ int getNumSamples( - const std::string kind, - double time); + const std::string kind); /** * * @brief Returns the snapshot matrix for the requested time. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time - * - * @param[in] time Time for which we want the basis vectors. - * * @return The snapshot matrix for the requested time. */ Matrix* - getSnapshotMatrix( - double time); + getSnapshotMatrix(); /** * * @brief Returns the first n columns of the snapshot matrix for the requested time. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time * @pre 0 < n <= numColumns() * - * @param[in] time Time for which we want the basis vectors. * @param[in] n The number of basis vectors desired. * * @return The snapshot matrix for the requested time. */ Matrix* getSnapshotMatrix( - double time, int n); /** * * @brief Returns the snapshot matrix from start_col to end_col for the requested time. * - * @pre 0 < numTimeIntervals() - * @pre 0 <= time * @pre 0 < start_col <= numColumns() * @pre start_col <= end_col <= numColumns() * - * @param[in] time Time for which we want the basis vectors. * @param[in] start_col The starting column desired. * @param[in] end_col The starting column desired. * @@ -348,7 +250,6 @@ class BasisReader { */ Matrix* getSnapshotMatrix( - double time, int start_col, int end_col); @@ -371,22 +272,6 @@ class BasisReader { operator = ( const BasisReader& rhs); - /** - * @brief Number of time intervals. - * - * @return The number of time intervals. - */ - int - numTimeIntervals() - { - return static_cast(d_time_interval_start_times.size()); - } - - /** - * @brief The start time of each time interval. - */ - std::vector d_time_interval_start_times; - /** * @brief The database being read from. */ @@ -401,11 +286,6 @@ class BasisReader { * @brief Full file name of database including rank. */ std::string full_file_name; - - /** - * @brief The last time at which basis vectors were requested. - */ - int d_last_basis_idx; }; } diff --git a/lib/linalg/BasisWriter.cpp b/lib/linalg/BasisWriter.cpp index eee5c3a09..b2bfd0ffd 100644 --- a/lib/linalg/BasisWriter.cpp +++ b/lib/linalg/BasisWriter.cpp @@ -26,7 +26,6 @@ BasisWriter::BasisWriter( const std::string& base_file_name, Database::formats db_format) : d_basis_generator(basis_generator), - d_num_intervals_written(0), full_file_name(""), snap_file_name(""), db_format_(db_format), @@ -53,96 +52,75 @@ BasisWriter::BasisWriter( char tmp2[100]; sprintf(tmp2, "_snapshot.%06d", rank); snap_file_name = base_file_name + tmp2; + + // create and open snapshot/basis database + CAROM_VERIFY(db_format_ == Database::HDF5); + d_snap_database = new HDFDatabase(); + d_database = new HDFDatabase(); } BasisWriter::~BasisWriter() { - if (d_database) { - d_database->putInteger("num_time_intervals", d_num_intervals_written); - d_database->close(); - delete d_database; - } - if (d_snap_database) { - d_snap_database->putInteger("num_time_intervals", d_num_intervals_written); - d_snap_database->close(); - delete d_snap_database; - } + delete d_database; + delete d_snap_database; } void BasisWriter::writeBasis(const std::string& kind) { - CAROM_ASSERT(kind == "basis" || kind == "snapshot"); char tmp[100]; - double time_interval_start_time = - d_basis_generator->getBasisIntervalStartTime(d_num_intervals_written); - sprintf(tmp, "time_%06d", d_num_intervals_written); if (kind == "basis") { - - // create and open basis database - if (db_format_ == Database::HDF5) { - d_database = new HDFDatabase(); - } - std::cout << "Creating file: " << full_file_name << std::endl; d_database->create(full_file_name); - d_database->putDouble(tmp, time_interval_start_time); - const Matrix* basis = d_basis_generator->getSpatialBasis(); int num_rows = basis->numRows(); - sprintf(tmp, "spatial_basis_num_rows_%06d", d_num_intervals_written); + sprintf(tmp, "spatial_basis_num_rows"); d_database->putInteger(tmp, num_rows); int num_cols = basis->numColumns(); - sprintf(tmp, "spatial_basis_num_cols_%06d", d_num_intervals_written); + sprintf(tmp, "spatial_basis_num_cols"); d_database->putInteger(tmp, num_cols); - sprintf(tmp, "spatial_basis_%06d", d_num_intervals_written); + sprintf(tmp, "spatial_basis"); d_database->putDoubleArray(tmp, &basis->item(0, 0), num_rows*num_cols); if(d_basis_generator->updateRightSV()) { const Matrix* tbasis = d_basis_generator->getTemporalBasis(); num_rows = tbasis->numRows(); - sprintf(tmp, "temporal_basis_num_rows_%06d", d_num_intervals_written); + sprintf(tmp, "temporal_basis_num_rows"); d_database->putInteger(tmp, num_rows); num_cols = tbasis->numColumns(); - sprintf(tmp, "temporal_basis_num_cols_%06d", d_num_intervals_written); + sprintf(tmp, "temporal_basis_num_cols"); d_database->putInteger(tmp, num_cols); - sprintf(tmp, "temporal_basis_%06d", d_num_intervals_written); + sprintf(tmp, "temporal_basis"); d_database->putDoubleArray(tmp, &tbasis->item(0, 0), num_rows*num_cols); } const Vector* sv = d_basis_generator->getSingularValues(); int sv_dim = sv->dim(); - sprintf(tmp, "singular_value_size_%06d", d_num_intervals_written); + sprintf(tmp, "singular_value_size"); d_database->putInteger(tmp, sv_dim); - sprintf(tmp, "singular_value_%06d", d_num_intervals_written); + sprintf(tmp, "singular_value"); d_database->putDoubleArray(tmp, &sv->item(0), sv_dim); - ++d_num_intervals_written; - + d_database->close(); } if (kind == "snapshot") { - // create and open snapshot database - if (db_format_ == Database::HDF5) { - d_snap_database = new HDFDatabase(); - } - std::cout << "Creating file: " << snap_file_name << std::endl; d_snap_database->create(snap_file_name); - d_snap_database->putDouble(tmp, time_interval_start_time); - const Matrix* snapshots = d_basis_generator->getSnapshotMatrix(); int num_rows = snapshots->numRows(); // d_dim - sprintf(tmp, "snapshot_matrix_num_rows_%06d", d_num_intervals_written); + sprintf(tmp, "snapshot_matrix_num_rows"); d_snap_database->putInteger(tmp, num_rows); int num_cols = snapshots->numColumns(); // d_num_samples - sprintf(tmp, "snapshot_matrix_num_cols_%06d", d_num_intervals_written); + sprintf(tmp, "snapshot_matrix_num_cols"); d_snap_database->putInteger(tmp, num_cols); - sprintf(tmp, "snapshot_matrix_%06d", d_num_intervals_written); + sprintf(tmp, "snapshot_matrix"); d_snap_database->putDoubleArray(tmp, &snapshots->item(0,0), num_rows*num_cols); + + d_snap_database->close(); } } diff --git a/lib/linalg/BasisWriter.h b/lib/linalg/BasisWriter.h index 8611c1ead..d495b6ef0 100644 --- a/lib/linalg/BasisWriter.h +++ b/lib/linalg/BasisWriter.h @@ -99,12 +99,6 @@ class BasisWriter { */ std::string full_file_name; std::string snap_file_name; - - /** - * @brief Number of time intervals for which basis vectors have been - * written. - */ - int d_num_intervals_written; }; } diff --git a/lib/linalg/Options.h b/lib/linalg/Options.h index a7c6a1fb7..7bb1d7c00 100644 --- a/lib/linalg/Options.h +++ b/lib/linalg/Options.h @@ -14,6 +14,8 @@ #ifndef included_Options_h #define included_Options_h +#include "utils/Utilities.h" + namespace CAROM { /** @@ -28,29 +30,26 @@ class Options * @brief Constructor. * * @pre dim_ > 0 - * @pre samples_per_time_interval_ > 0 - * @pre max_time_intervals == -1 || max_time_intervals > 0 + * @pre max_num_samples_ > 0 * * @param[in] dim_ The dimension of the system on this processor. - * @param[in] samples_per_time_interval_ The maximum number of samples in + * @param[in] max_num_samples_ The maximum number of samples in * each time interval. - * @param[in] max_time_intervals_ The maximum number of time intervals. * @param[in] update_right_SV_ Whether to update the right SV or not. * @param[in] write_snapshots_ Whether to automatically write snapshots matrices * instead of basis matrices. * */ Options(int dim_, - int samples_per_time_interval_, - int max_time_intervals_ = -1, + int max_num_samples_, bool update_right_SV_ = false, bool write_snapshots_ = false ): dim(dim_), - max_basis_dimension(samples_per_time_interval_), - samples_per_time_interval(samples_per_time_interval_), - max_time_intervals(max_time_intervals_), + max_basis_dimension(max_num_samples_), + max_num_samples(max_num_samples_), update_right_SV(update_right_SV_), - write_snapshots(write_snapshots_) {}; + write_snapshots(write_snapshots_) + {}; /** * @brief Sets the maximum basis dimension of the SVD algorithm. @@ -216,14 +215,9 @@ class Options int dim = -1; /** - * @brief The number of samples per time interval. - */ - int samples_per_time_interval = -1; - - /** - * @brief The maximum number of time intervals. + * @brief The maximum number of samples. */ - int max_time_intervals = -1; + int max_num_samples = -1; /** * @brief Whether to update the right singular values. diff --git a/lib/linalg/svd/IncrementalSVD.cpp b/lib/linalg/svd/IncrementalSVD.cpp index 807e8d473..5c5e912c4 100644 --- a/lib/linalg/svd/IncrementalSVD.cpp +++ b/lib/linalg/svd/IncrementalSVD.cpp @@ -93,12 +93,6 @@ IncrementalSVD::IncrementalSVD( d_state_database = new HDFDatabase(); bool is_good = d_state_database->open(d_state_file_name, "r"); if (is_good) { - // Read time interval start time. - double time; - d_state_database->getDouble("time", time); - d_time_interval_start_times.resize(1); - d_time_interval_start_times[0] = time; - // Read d_U. int num_rows; d_state_database->getInteger("U_num_rows", num_rows); @@ -144,10 +138,7 @@ IncrementalSVD::~IncrementalSVD() // // If there are multiple time intervals then saving and restoring the state // does not make sense as there is not one, all encompassing, basis. - if (d_save_state && d_time_interval_start_times.size() == 1) { - // Save the time interval start time. - d_state_database->putDouble("time", d_time_interval_start_times[0]); - + if (d_save_state && (!isFirstSample())) { // Save d_U. int num_rows = d_U->numRows(); d_state_database->putInteger("U_num_rows", num_rows); @@ -182,11 +173,9 @@ IncrementalSVD::~IncrementalSVD() bool IncrementalSVD::takeSample( double* u_in, - double time, bool add_without_increase) { CAROM_VERIFY(u_in != 0); - CAROM_VERIFY(time >= 0.0); // Check that u_in is not non-zero. Vector u_vec(u_in, d_dim, true); @@ -197,8 +186,8 @@ IncrementalSVD::takeSample( // If this is the first SVD then build it. Otherwise add this sample to the // system. bool result = true; - if (isNewTimeInterval()) { - buildInitialSVD(u_in, time); + if (isFirstSample()) { + buildInitialSVD(u_in); } else { result = buildIncrementalSVD(u_in,add_without_increase); diff --git a/lib/linalg/svd/IncrementalSVD.h b/lib/linalg/svd/IncrementalSVD.h index 213151aaa..5b293e45a 100644 --- a/lib/linalg/svd/IncrementalSVD.h +++ b/lib/linalg/svd/IncrementalSVD.h @@ -52,10 +52,8 @@ class IncrementalSVD : public SVD * @brief Sample new state, u_in, at the given time. * * @pre u_in != 0 - * @pre time >= 0.0 * * @param[in] u_in The state at the specified time. - * @param[in] time The simulation time for the state. * @param[in] add_without_increase If true, addLinearlyDependent is invoked. * * @return True if the sampling was successful. @@ -64,7 +62,6 @@ class IncrementalSVD : public SVD bool takeSample( double* u_in, - double time, bool add_without_increase = false); /** @@ -110,16 +107,13 @@ class IncrementalSVD : public SVD * @brief Constructs the first SVD. * * @pre u != 0 - * @pre time >= 0.0 * * @param[in] u The first state. - * @param[in] time The simulation time for the first state. */ virtual void buildInitialSVD( - double* u, - double time) = 0; + double* u) = 0; /** * @brief Adds the new sampled the state vector, u, to the system. diff --git a/lib/linalg/svd/IncrementalSVDBrand.cpp b/lib/linalg/svd/IncrementalSVDBrand.cpp index 32cd41ca1..b4648ab58 100644 --- a/lib/linalg/svd/IncrementalSVDBrand.cpp +++ b/lib/linalg/svd/IncrementalSVDBrand.cpp @@ -65,7 +65,7 @@ IncrementalSVDBrand::~IncrementalSVDBrand() // // If there are multiple time intervals then saving and restoring the state // does not make sense as there is not one, all encompassing, basis. - if (d_save_state && d_time_interval_start_times.size() == 1) { + if (d_save_state && (!isFirstSample())) { // Create state database file. d_state_database = new HDFDatabase(); d_state_database->create(d_state_file_name); @@ -106,27 +106,9 @@ IncrementalSVDBrand::getTemporalBasis() void IncrementalSVDBrand::buildInitialSVD( - double* u, - double time) + double* u) { CAROM_VERIFY(u != 0); - CAROM_VERIFY(time >= 0.0); - - // We have a new time interval. - - // If this is not the first time interval then delete d_basis, d_U, d_Up, - // and d_S of the just completed time interval. - int num_time_intervals = - static_cast(d_time_interval_start_times.size()); - if (num_time_intervals > 0) { - delete d_basis; - delete d_U; - delete d_Up; - delete d_S; - delete d_W; - } - increaseTimeInterval(); - d_time_interval_start_times[num_time_intervals] = time; // Build d_S for this new time interval. d_S = new Vector(1, false); diff --git a/lib/linalg/svd/IncrementalSVDBrand.h b/lib/linalg/svd/IncrementalSVDBrand.h index 153120c62..6b7e54482 100644 --- a/lib/linalg/svd/IncrementalSVDBrand.h +++ b/lib/linalg/svd/IncrementalSVDBrand.h @@ -98,8 +98,7 @@ class IncrementalSVDBrand : public IncrementalSVD virtual void buildInitialSVD( - double* u, - double time); + double* u); /** * @brief Adds the new sampled the state vector, u, to the system. diff --git a/lib/linalg/svd/IncrementalSVDFastUpdate.cpp b/lib/linalg/svd/IncrementalSVDFastUpdate.cpp index cbed4d250..6fa22442d 100644 --- a/lib/linalg/svd/IncrementalSVDFastUpdate.cpp +++ b/lib/linalg/svd/IncrementalSVDFastUpdate.cpp @@ -65,7 +65,7 @@ IncrementalSVDFastUpdate::~IncrementalSVDFastUpdate() // // If there are multiple time intervals then saving and restoring the state // does not make sense as there is not one, all encompassing, basis. - if (d_save_state && d_time_interval_start_times.size() == 1) { + if (d_save_state && (!isFirstSample())) { // Create state database file. d_state_database = new HDFDatabase(); d_state_database->create(d_state_file_name); @@ -88,27 +88,9 @@ IncrementalSVDFastUpdate::~IncrementalSVDFastUpdate() void IncrementalSVDFastUpdate::buildInitialSVD( - double* u, - double time) + double* u) { CAROM_VERIFY(u != 0); - CAROM_VERIFY(time >= 0.0); - - // We have a new time interval. - - // If this is not the first time interval then delete d_basis, d_U, d_Up, - // and d_S of the just completed time interval. - int num_time_intervals = - static_cast(d_time_interval_start_times.size()); - if (num_time_intervals > 0) { - delete d_basis; - delete d_U; - delete d_Up; - delete d_S; - delete d_W; - } - increaseTimeInterval(); - d_time_interval_start_times[num_time_intervals] = time; // Build d_S for this new time interval. d_S = new Vector(1, false); diff --git a/lib/linalg/svd/IncrementalSVDFastUpdate.h b/lib/linalg/svd/IncrementalSVDFastUpdate.h index 2069e86e5..6d7e1f41c 100644 --- a/lib/linalg/svd/IncrementalSVDFastUpdate.h +++ b/lib/linalg/svd/IncrementalSVDFastUpdate.h @@ -80,8 +80,7 @@ class IncrementalSVDFastUpdate : public IncrementalSVD virtual void buildInitialSVD( - double* u, - double time); + double* u); /** * @brief Computes the current basis vectors. diff --git a/lib/linalg/svd/IncrementalSVDStandard.cpp b/lib/linalg/svd/IncrementalSVDStandard.cpp index c9c9dd491..d063d0a8f 100644 --- a/lib/linalg/svd/IncrementalSVDStandard.cpp +++ b/lib/linalg/svd/IncrementalSVDStandard.cpp @@ -52,7 +52,7 @@ IncrementalSVDStandard::~IncrementalSVDStandard() // // If there are multiple time intervals then saving and restoring the state // does not make sense as there is not one, all encompassing, basis. - if (d_save_state && d_time_interval_start_times.size() == 1) { + if (d_save_state && (!isFirstSample())) { // Create state database file. d_state_database = new HDFDatabase(); d_state_database->create(d_state_file_name); @@ -61,27 +61,9 @@ IncrementalSVDStandard::~IncrementalSVDStandard() void IncrementalSVDStandard::buildInitialSVD( - double* u, - double time) + double* u) { CAROM_VERIFY(u != 0); - CAROM_VERIFY(time >= 0.0); - - // We have a new time interval. - - // If this is not the first time interval then write the basis vectors for - // the just completed interval. Delete d_basis and d_S of the just - // completed time interval. - int num_time_intervals = - static_cast(d_time_interval_start_times.size()); - if (num_time_intervals > 0) { - delete d_basis; - delete d_U; - delete d_S; - delete d_W; - } - increaseTimeInterval(); - d_time_interval_start_times[num_time_intervals] = time; // Build d_S for this new time interval. d_S = new Vector(1, false); diff --git a/lib/linalg/svd/IncrementalSVDStandard.h b/lib/linalg/svd/IncrementalSVDStandard.h index f514bb5b8..ac10b1ee9 100644 --- a/lib/linalg/svd/IncrementalSVDStandard.h +++ b/lib/linalg/svd/IncrementalSVDStandard.h @@ -79,8 +79,7 @@ class IncrementalSVDStandard : public IncrementalSVD virtual void buildInitialSVD( - double* u, - double time); + double* u); /** * @brief Computes the current basis vectors. diff --git a/lib/linalg/svd/RandomizedSVD.cpp b/lib/linalg/svd/RandomizedSVD.cpp index 9d9271ebc..c5ea51f13 100644 --- a/lib/linalg/svd/RandomizedSVD.cpp +++ b/lib/linalg/svd/RandomizedSVD.cpp @@ -196,7 +196,7 @@ RandomizedSVD::computeSVD() d_basis_right->gather(); } - d_this_interval_basis_current = true; + d_basis_is_current = true; delete Q; release_context(&svd_input); diff --git a/lib/linalg/svd/SVD.cpp b/lib/linalg/svd/SVD.cpp index d81825f94..fe6547625 100644 --- a/lib/linalg/svd/SVD.cpp +++ b/lib/linalg/svd/SVD.cpp @@ -19,21 +19,17 @@ SVD::SVD( Options options) : d_dim(options.dim), d_num_samples(0), - d_samples_per_time_interval(options.samples_per_time_interval), - d_max_time_intervals(options.max_time_intervals), + d_max_num_samples(options.max_num_samples), d_basis(NULL), d_basis_right(NULL), d_U(NULL), d_W(NULL), d_S(NULL), d_snapshots(NULL), - d_time_interval_start_times(0), d_debug_algorithm(options.debug_algorithm) { CAROM_VERIFY(options.dim > 0); - CAROM_VERIFY(options.max_time_intervals == -1 - || options.max_time_intervals > 0); - CAROM_VERIFY(options.samples_per_time_interval > 0); + CAROM_VERIFY(options.max_num_samples > 0); } SVD::~SVD() diff --git a/lib/linalg/svd/SVD.h b/lib/linalg/svd/SVD.h index 9ce034843..7ddbc21b8 100644 --- a/lib/linalg/svd/SVD.h +++ b/lib/linalg/svd/SVD.h @@ -47,10 +47,8 @@ class SVD * @brief Collect the new sample, u_in at supplied time. * * @pre u_in != 0 - * @pre time >= 0.0 * * @param[in] u_in The new sample. - * @param[in] time The simulation time of the new sample. * @param[in] add_without_increase If true, the addLinearlyDependent is invoked. * This only applies to incremental SVD. * @@ -60,7 +58,6 @@ class SVD bool takeSample( double* u_in, - double time, bool add_without_increase) = 0; /** @@ -111,38 +108,25 @@ class SVD getSnapshotMatrix() = 0; /** - * @brief Returns the number of time intervals on which different sets - * of basis vectors are defined. + * @brief Get the number of samples taken. * - * @return The number of time intervals on which there are basis vectors. */ - int - getNumBasisTimeIntervals() const + int getNumSamples() const { - return static_cast(d_time_interval_start_times.size()); + return d_num_samples; } /** - * @brief Returns the start time for the requested time interval. + * @brief Get the maximum number of samples that can be taken. + * SVD class will return an error if the number of samples exceeds the maximum. * - * @pre 0 <= which_interval - * @pre which_interval < getNumBasisTimeIntervals() - * - * @param[in] which_interval The time interval of interest. - * - * @return The start time for the requested time interval. */ - double - getBasisIntervalStartTime( - int which_interval) const + int getMaxNumSamples() const { - CAROM_VERIFY(0 <= which_interval); - CAROM_VERIFY(which_interval < getNumBasisTimeIntervals()); - - std::size_t i = static_cast(which_interval); - return d_time_interval_start_times[i]; + return d_max_num_samples; } +protected: /** * @brief Returns true if the next sample will result in a new time * interval. @@ -151,37 +135,11 @@ class SVD * interval. */ bool - isNewTimeInterval() const + isFirstSample() const { - return (d_num_samples == 0) || - (d_num_samples >= d_samples_per_time_interval); + return (d_num_samples == 0); } - /** - * @brief Increase the number of time intervals by one - * - */ - void - increaseTimeInterval() - { - int num_time_intervals = - static_cast(d_time_interval_start_times.size()); - CAROM_VERIFY(d_max_time_intervals == -1 || - num_time_intervals < d_max_time_intervals); - d_time_interval_start_times.resize( - static_cast(num_time_intervals) + 1); - } - - /** - * @brief Get the number of samples taken. - * - */ - int getNumSamples() const - { - return d_num_samples; - } - -protected: /** * @brief Dimension of the system. */ @@ -198,15 +156,9 @@ class SVD int d_num_rows_of_W; /** - * @brief The maximum number of samples to be collected for a time - * interval. - */ - const int d_samples_per_time_interval; - - /** - * @brief The maximum number of time intervals. + * @brief The maximum number of samples. */ - const int d_max_time_intervals; + const int d_max_num_samples; /** * @brief The globalized basis vectors for the current time interval. @@ -256,11 +208,6 @@ class SVD */ Matrix* d_snapshots; - /** - * @brief The simulation time at which each time interval starts. - */ - std::vector d_time_interval_start_times; - /** * @brief Flag to indicate if results of algorithm should be printed for * debugging purposes. diff --git a/lib/linalg/svd/StaticSVD.cpp b/lib/linalg/svd/StaticSVD.cpp index 085336f10..6eff9b9a8 100644 --- a/lib/linalg/svd/StaticSVD.cpp +++ b/lib/linalg/svd/StaticSVD.cpp @@ -36,7 +36,7 @@ StaticSVD::StaticSVD( Options options) : SVD(options), d_samples(new SLPK_Matrix), d_factorizer(new SVDManager), - d_this_interval_basis_current(false), + d_basis_is_current(false), d_max_basis_dimension(options.max_basis_dimension), d_singular_value_tol(options.singular_value_tol), d_preserve_snapshot(options.static_svd_preserve_snapshot) @@ -60,7 +60,7 @@ StaticSVD::StaticSVD( d_blocksize += 1; } - initialize_matrix(d_samples.get(), d_total_dim, d_samples_per_time_interval, + initialize_matrix(d_samples.get(), d_total_dim, d_max_num_samples, d_nprow, d_npcol, d_blocksize, d_blocksize); // TODO: should nb = 1? d_factorizer->A = nullptr; } @@ -100,12 +100,12 @@ void StaticSVD::delete_factorizer() bool StaticSVD::takeSample( double* u_in, - double time, bool add_without_increase) { CAROM_VERIFY(u_in != 0); - CAROM_VERIFY(time >= 0.0); CAROM_NULL_USE(add_without_increase); + CAROM_VERIFY(0 <= d_num_samples); + CAROM_VERIFY(d_num_samples < d_max_num_samples); // Check the u_in is not non-zero. Vector u_vec(u_in, d_dim, true); @@ -113,33 +113,13 @@ StaticSVD::takeSample( return false; } - if (isNewTimeInterval()) { - // We have a new time interval. + if (isFirstSample()) { delete_factorizer(); - int num_time_intervals = - static_cast(d_time_interval_start_times.size()); - if (num_time_intervals > 0) { - delete d_basis; - d_basis = nullptr; - delete d_basis_right; - d_basis_right = nullptr; - delete d_U; - d_U = nullptr; - delete d_S; - d_S = nullptr; - delete d_W; - d_W = nullptr; - delete d_snapshots; - d_snapshots = nullptr; - } d_num_samples = 0; - increaseTimeInterval(); - d_time_interval_start_times[static_cast(num_time_intervals)] = - time; d_basis = nullptr; d_basis_right = nullptr; // Set the N in the global matrix so BLACS won't complain. - d_samples->n = d_samples_per_time_interval; + d_samples->n = d_max_num_samples; } broadcast_sample(u_in); ++d_num_samples; @@ -153,7 +133,7 @@ StaticSVD::takeSample( // firstrow, 1, nrows, // d_num_samples, rank); // } - d_this_interval_basis_current = false; + d_basis_is_current = false; return true; } @@ -162,7 +142,7 @@ StaticSVD::getSpatialBasis() { // If this basis is for the last time interval then it may not be up to date // so recompute it. - if (!thisIntervalBasisCurrent()) { + if (!isBasisCurrent()) { delete d_basis; d_basis = nullptr; delete d_basis_right; @@ -178,7 +158,7 @@ StaticSVD::getSpatialBasis() else { CAROM_ASSERT(d_basis != 0); } - CAROM_ASSERT(thisIntervalBasisCurrent()); + CAROM_ASSERT(isBasisCurrent()); return d_basis; } @@ -187,7 +167,7 @@ StaticSVD::getTemporalBasis() { // If this basis is for the last time interval then it may not be up to date // so recompute it. - if (!thisIntervalBasisCurrent()) { + if (!isBasisCurrent()) { delete d_basis; d_basis = nullptr; delete d_basis_right; @@ -203,7 +183,7 @@ StaticSVD::getTemporalBasis() else { CAROM_ASSERT(d_basis_right != 0); } - CAROM_ASSERT(thisIntervalBasisCurrent()); + CAROM_ASSERT(isBasisCurrent()); return d_basis_right; } @@ -212,7 +192,7 @@ StaticSVD::getSingularValues() { // If these singular values are for the last time interval then they may not // be up to date so recompute them. - if (!thisIntervalBasisCurrent()) { + if (!isBasisCurrent()) { delete d_basis; d_basis = nullptr; delete d_basis_right; @@ -228,14 +208,14 @@ StaticSVD::getSingularValues() else { CAROM_ASSERT(d_S != 0); } - CAROM_ASSERT(thisIntervalBasisCurrent()); + CAROM_ASSERT(isBasisCurrent()); return d_S; } const Matrix* StaticSVD::getSnapshotMatrix() { - if ((!d_preserve_snapshot) && (thisIntervalBasisCurrent()) && (d_basis != 0)) + if ((!d_preserve_snapshot) && (isBasisCurrent()) && (d_basis != 0)) CAROM_ERROR("StaticSVD: snapshot matrix is modified after computeSVD." " To preserve the snapshots, set Options::static_svd_preserve_snapshot to be true!\n"); @@ -363,7 +343,7 @@ StaticSVD::computeSVD() } for (int i = 0; i < ncolumns; ++i) d_S->item(i) = d_factorizer->S[static_cast(i)]; - d_this_interval_basis_current = true; + d_basis_is_current = true; if (d_debug_algorithm) { if (d_rank == 0) { diff --git a/lib/linalg/svd/StaticSVD.h b/lib/linalg/svd/StaticSVD.h index bb8d29cbf..9dd331a30 100644 --- a/lib/linalg/svd/StaticSVD.h +++ b/lib/linalg/svd/StaticSVD.h @@ -41,10 +41,8 @@ class StaticSVD : public SVD * @brief Collect the new sample, u_in at the supplied time. * * @pre u_in != 0 - * @pre time >= 0.0 * * @param[in] u_in The new sample. - * @param[in] time The simulation time of the new sample. * @param[in] add_without_increase If true, then addLinearlyDependent will be invoked * * @return True if the sampling was successful. @@ -53,7 +51,6 @@ class StaticSVD : public SVD bool takeSample( double* u_in, - double time, bool add_without_increase = false); /** @@ -121,16 +118,14 @@ class StaticSVD : public SVD computeSVD(); /** - * @brief Checks if the basis vectors for this time interval are up to - * date. + * @brief Checks if the basis vectors are computed from the current snapshot. * - * @return True if the basis vectors for this time interval are up to - * date. + * @return True if the basis vectors are computed from the current snapshot. */ bool - thisIntervalBasisCurrent() + isBasisCurrent() { - return d_this_interval_basis_current; + return d_basis_is_current; } /** @@ -147,7 +142,7 @@ class StaticSVD : public SVD * @brief Flag to indicate if the basis vectors for the current time * interval are up to date. */ - bool d_this_interval_basis_current; + bool d_basis_is_current; /** * @brief The rank of the process this object belongs to. diff --git a/lib/utils/CSVDatabase.cpp b/lib/utils/CSVDatabase.cpp index 6ecf74cd8..99547d6a7 100644 --- a/lib/utils/CSVDatabase.cpp +++ b/lib/utils/CSVDatabase.cpp @@ -30,6 +30,7 @@ bool CSVDatabase::create( const std::string& file_name) { + Database::create(file_name); return true; } @@ -38,6 +39,7 @@ CSVDatabase::open( const std::string& file_name, const std::string& type) { + Database::open(file_name, type); return true; } diff --git a/lib/utils/CSVDatabase.h b/lib/utils/CSVDatabase.h index f4cfdb465..9eefa1f8a 100644 --- a/lib/utils/CSVDatabase.h +++ b/lib/utils/CSVDatabase.h @@ -47,7 +47,7 @@ class CSVDatabase : public Database virtual bool create( - const std::string& file_name); + const std::string& file_name) override; /** * @brief Opens an existing CSV database file with the supplied name. @@ -61,7 +61,7 @@ class CSVDatabase : public Database bool open( const std::string& file_name, - const std::string& type); + const std::string& type) override; /** * @brief Closes the currently open CSV database file. diff --git a/lib/utils/Database.cpp b/lib/utils/Database.cpp index ef46d3ad4..49c6c9eb7 100644 --- a/lib/utils/Database.cpp +++ b/lib/utils/Database.cpp @@ -11,9 +11,18 @@ // Description: The abstract database class defines interface for databases. #include "Database.h" +#include +#include namespace CAROM { +bool fileExists(const std::string& name) +{ + std::ifstream f(name.c_str()); + return f.good(); + // ifstream f will be closed upon the end of the function. +} + Database::Database() { } @@ -22,4 +31,18 @@ Database::~Database() { } +bool +Database::create(const std::string& file_name) +{ + std::cout << "Creating file: " << file_name << std::endl; +} + +bool +Database::open( + const std::string& file_name, + const std::string& type) +{ + std::cout << "Opening file: " << file_name << std::endl; +} + } diff --git a/lib/utils/Database.h b/lib/utils/Database.h index dafdb4056..e839da863 100644 --- a/lib/utils/Database.h +++ b/lib/utils/Database.h @@ -18,6 +18,8 @@ namespace CAROM { +bool fileExists(const std::string& name); + /** * Class Database is an abstract base class that provides basic ability to * write to and read from a file. It's capabilities are limited to what the @@ -47,7 +49,7 @@ class Database virtual bool create( - const std::string& file_name) = 0; + const std::string& file_name); /** * @brief Opens an existing database file with the supplied name. @@ -61,7 +63,7 @@ class Database bool open( const std::string& file_name, - const std::string& type) = 0; + const std::string& type); /** * @brief Closes the currently open database file. diff --git a/lib/utils/HDFDatabase.cpp b/lib/utils/HDFDatabase.cpp index 7f98f4e11..60bd0ad69 100644 --- a/lib/utils/HDFDatabase.cpp +++ b/lib/utils/HDFDatabase.cpp @@ -12,6 +12,7 @@ #include "HDFDatabase.h" #include "Utilities.h" +#include namespace CAROM { @@ -44,6 +45,8 @@ bool HDFDatabase::create( const std::string& file_name) { + Database::create(file_name); + CAROM_VERIFY(!file_name.empty()); hid_t file_id = H5Fcreate(file_name.c_str(), H5F_ACC_TRUNC, @@ -63,6 +66,8 @@ HDFDatabase::open( const std::string& file_name, const std::string& type) { + Database::open(file_name, type); + CAROM_VERIFY(!file_name.empty()); CAROM_VERIFY(type == "r" || type == "wr"); hid_t file_id; diff --git a/lib/utils/HDFDatabase.h b/lib/utils/HDFDatabase.h index 35cbc0070..d91e707d6 100644 --- a/lib/utils/HDFDatabase.h +++ b/lib/utils/HDFDatabase.h @@ -46,7 +46,7 @@ class HDFDatabase : public Database virtual bool create( - const std::string& file_name); + const std::string& file_name) override; /** * @brief Opens an existing HDF5 database file with the supplied name. @@ -60,7 +60,7 @@ class HDFDatabase : public Database bool open( const std::string& file_name, - const std::string& type); + const std::string& type) override; /** * @brief Closes the currently open HDF5 database file. diff --git a/regression_tests/basisComparator.cpp b/regression_tests/basisComparator.cpp index d4aca8fad..9ae74a840 100755 --- a/regression_tests/basisComparator.cpp +++ b/regression_tests/basisComparator.cpp @@ -37,13 +37,13 @@ void compareBasis(string &baselineFile, string &targetFile, double errorBound, CAROM::BasisReader baselineReader(baselineFile); CAROM::Matrix *baselineBasis = - (CAROM::Matrix*) baselineReader.getSpatialBasis(0.0); + (CAROM::Matrix*) baselineReader.getSpatialBasis(); CAROM::Vector *baselineSV = - (CAROM::Vector*) baselineReader.getSingularValues(0.0); + (CAROM::Vector*) baselineReader.getSingularValues(); CAROM::BasisReader targetReader(targetFile); - CAROM::Matrix *targetBasis = (CAROM::Matrix*) targetReader.getSpatialBasis(0.0); + CAROM::Matrix *targetBasis = (CAROM::Matrix*) targetReader.getSpatialBasis(); CAROM::BasisReader diffReader(baselineFile); - CAROM::Matrix *diffBasis = (CAROM::Matrix*) diffReader.getSpatialBasis(0.0); + CAROM::Matrix *diffBasis = (CAROM::Matrix*) diffReader.getSpatialBasis(); // Get basis dimensions int baselineNumRows = baselineBasis->numRows(); diff --git a/scripts/data/update_basis_format.py b/scripts/data/update_basis_format.py new file mode 100644 index 000000000..6bc7fc8fb --- /dev/null +++ b/scripts/data/update_basis_format.py @@ -0,0 +1,28 @@ +import numpy +import h5py +import argparse + +parser = argparse.ArgumentParser( + prog='Basis data format converter', + description='This python script converts the out-dated dataset names of ' + 'a basis hdf5 file. 6 digits of integer following ' + 'the name of all datasets will be removed.') +parser.add_argument('filename', type=str, + help='the basis hdf5 file name to update the dataset names.') # positional argument + +if __name__ == "__main__": + args = parser.parse_args() + print('Filename: %s' % args.filename) + with h5py.File(args.filename, 'r+') as f: + for key in f.keys(): + if ((key[-7] == '_') and (key[-6:].isdigit())): + print('Detected an outdated dataset name: %s' % key) + if (int(key[-6:]) != 0): + print('libROM only supports single time interval, skipping this dataset.') + continue + newname = key[:-7] + print('Converting to: %s' % newname) + f[newname] = f[key] + del f[key] + + diff --git a/unit_tests/baselines/basis_conversion/test_basis.000000 b/unit_tests/baselines/basis_conversion/test_basis.000000 new file mode 100644 index 0000000000000000000000000000000000000000..a1b32487ce5397d7292aa48d051dde8b700f968c GIT binary patch literal 5984 zcmeD5aB<`1lHy_j0S*oZ76t(@6Gr@pf&_7h2#gPtPk=HQp>zk7Ucm%mFfxE31A_!q zTo7tLy1I}cS62q0N|^aD8mf)KfCa)zfC-G!BPs+uTpa^I9!`KJv=30oZ+XBbl3h ze$|bHBGo|vEN_iVdj~|)9f#lT=9F_ z%wqrIx%lFla^d!mLg)RTH_04imV)idVj28ShbsB;Q*sa5092%6&}FuG8n*U^7|1T z&~WiU$tZ)hA5mP8Sdy8T6Q7h=oLL;7SDG7NlwV#PZvX~BA7IKS=jY&*hihXfNzE=v@ee? b46FPH>xCg`a{KXDpy2`;kAubVV9S31l0GJR literal 0 HcmV?d00001 diff --git a/unit_tests/baselines/basis_conversion/test_basis_snapshot.000000 b/unit_tests/baselines/basis_conversion/test_basis_snapshot.000000 new file mode 100644 index 0000000000000000000000000000000000000000..541a8d630bfb3c334d750f04ec37b0d2dba6d9d0 GIT binary patch literal 4272 zcmeD5aB<`1lHy_j0S*oZ76t(@6Gr@pf(=k%7#}8|0A(;j=?*Bpf(gQ4WB@@11_`LR zAk=(xbs<5nt_(nxF!NzFR2zc<3xvf16BwmOR0w#uItG9|yaDR#1SpM`Tw$VAqZt|4 z!RZ%5fKn(VB{MQYQami(!)RupgFtKnP6l9J0s#du$;!Y17H48&0xRW!n#0TplmG*0 z0*7!IgrLG8#e#3|r1CHrfMpmNSQ&VLav_xksUQK6JzxWo)Puw!Vjz|Wh@ct>_VagP zWMGDy+QEk3Xbq@z1uK4auzan+j9=Ywk)?gQVi{8Dicom)Ln9tWGXwJvIBzK0LsPy3 zg9$f-$$x12!JXfLl^hsUpd=+q^Bcl22Ly9yus}JJ+5Vx)wS|dKzVH9?ChY3@Rp<79 zooTR-_pPS=-|*#ahtwMP-##E^_UN^Y1BZL%vaQ_w2bhFd_b@Kbv%mUQqvtvE2K(n` zRs0jC?XiD7U8N+r@3RBXf(g10c%}DmP!9@8Se0sjCEY@{cE5)MoBD43H(}T8pWVCE zajw96Xav!yD@Q|MGz3ONU^E0qLtuD@KyhATL2*WYNqlZ%Nl|7+d|qj8d{KURal8Q- XFkq8U&dtakeSample(&vals0[dim*rank],0,0.1); + static_basis_generator->takeSample(&vals0[dim*rank]); std::cout << "Writing sample 1" << std::endl; static_basis_generator->writeSnapshot(); static_basis_generator->endSamples(); @@ -87,7 +87,7 @@ main( "static_smoke2")); // Take the second sample. - static_basis_generator2->takeSample(&vals1[dim*rank],0,0.1); + static_basis_generator2->takeSample(&vals1[dim*rank]); static_basis_generator2->writeSnapshot(); // "_snapshot" will be added to the base file name static_basis_generator2->endSamples(); @@ -120,8 +120,8 @@ main( static_svd_options, false, "static_smoke_check")); - static_basis_generator4->takeSample(&vals0[dim*rank],0,0.1); - static_basis_generator4->takeSample(&vals1[dim*rank],0,0.1); + static_basis_generator4->takeSample(&vals0[dim*rank]); + static_basis_generator4->takeSample(&vals1[dim*rank]); static_basis_generator4->endSamples(); static_basis_generator4 = nullptr; diff --git a/unit_tests/smoke_test.cpp b/unit_tests/smoke_test.cpp index feca85ebe..0c6237ef3 100644 --- a/unit_tests/smoke_test.cpp +++ b/unit_tests/smoke_test.cpp @@ -73,7 +73,7 @@ main( // Take the first sample. if (inc_basis_generator.isNextSample(0.0)) { - status = inc_basis_generator.takeSample(&vals0[dim*rank], 0.0, 0.11); + status = inc_basis_generator.takeSample(&vals0[dim*rank]); if (status) { inc_basis_generator.computeNextSampleTime(&vals0[dim*rank], &vals0[dim*rank], @@ -83,7 +83,7 @@ main( // Take the second sample. if (status && inc_basis_generator.isNextSample(0.11)) { - status = inc_basis_generator.takeSample(&vals1[dim*rank], 0.11, 0.11); + status = inc_basis_generator.takeSample(&vals1[dim*rank]); if (status) { inc_basis_generator.computeNextSampleTime(&vals1[dim*rank], &vals1[dim*rank], diff --git a/unit_tests/test_IncrementalSVD.cpp b/unit_tests/test_IncrementalSVD.cpp index 5aaa31279..2ca19d1c0 100644 --- a/unit_tests/test_IncrementalSVD.cpp +++ b/unit_tests/test_IncrementalSVD.cpp @@ -69,8 +69,7 @@ class FakeIncrementalSVD : public CAROM::IncrementalSVD } void buildInitialSVD - (__attribute__((unused)) double* u, - __attribute__((unused)) double time) + (__attribute__((unused)) double* u) { /* Do nothing */ } diff --git a/unit_tests/test_IncrementalSVDBrand.cpp b/unit_tests/test_IncrementalSVDBrand.cpp index 014c05021..5e1402a63 100644 --- a/unit_tests/test_IncrementalSVDBrand.cpp +++ b/unit_tests/test_IncrementalSVDBrand.cpp @@ -82,7 +82,7 @@ TEST(IncrementalSVDBrandTest, Test_IncrementalSVDBrand) bool fast_update = true; bool fast_update_brand = true; - CAROM::Options incremental_svd_options = CAROM::Options(d_num_rows, 3, -1, true) + CAROM::Options incremental_svd_options = CAROM::Options(d_num_rows, 3, true) .setMaxBasisDimension(num_total_rows) .setIncrementalSVD(1e-1, 1e-1, @@ -96,9 +96,9 @@ TEST(IncrementalSVDBrandTest, Test_IncrementalSVDBrand) incremental_svd_options, true, "irrelevant.txt"); - sampler.takeSample(&sample1[row_offset[d_rank]], 0, 1e-1); - sampler.takeSample(&sample2[row_offset[d_rank]], 0, 1e-1); - sampler.takeSample(&sample3[row_offset[d_rank]], 0, 1e-1); + sampler.takeSample(&sample1[row_offset[d_rank]]); + sampler.takeSample(&sample2[row_offset[d_rank]]); + sampler.takeSample(&sample3[row_offset[d_rank]]); const CAROM::Matrix* d_basis = sampler.getSpatialBasis(); const CAROM::Matrix* d_basis_right = sampler.getTemporalBasis(); diff --git a/unit_tests/test_RandomizedSVD.cpp b/unit_tests/test_RandomizedSVD.cpp index 0f52b495c..1679392f0 100644 --- a/unit_tests/test_RandomizedSVD.cpp +++ b/unit_tests/test_RandomizedSVD.cpp @@ -69,14 +69,14 @@ TEST(RandomizedSVDTest, Test_RandomizedSVD) 4.84486375065219387892E+00, 3.66719976398777269821E+00, 2.69114625366671811335E+00 }; - CAROM::Options randomized_svd_options = CAROM::Options(d_num_rows, 3, 1); + CAROM::Options randomized_svd_options = CAROM::Options(d_num_rows, 3); randomized_svd_options.setMaxBasisDimension(num_total_rows); randomized_svd_options.setDebugMode(true); randomized_svd_options.setRandomizedSVD(true); CAROM::BasisGenerator sampler(randomized_svd_options, false); - sampler.takeSample(&sample1[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample2[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample3[row_offset[d_rank]], 0, 0); + sampler.takeSample(&sample1[row_offset[d_rank]]); + sampler.takeSample(&sample2[row_offset[d_rank]]); + sampler.takeSample(&sample3[row_offset[d_rank]]); const CAROM::Matrix* d_basis = sampler.getSpatialBasis(); const CAROM::Matrix* d_basis_right = sampler.getTemporalBasis(); @@ -149,16 +149,16 @@ TEST(RandomizedSVDTest, Test_RandomizedSVDTransposed) 4.84486375065219387892E+00, 3.66719976398777269821E+00, 2.69114625366671811335E+00, }; - CAROM::Options randomized_svd_options = CAROM::Options(d_num_rows, 5, 1); + CAROM::Options randomized_svd_options = CAROM::Options(d_num_rows, 5); randomized_svd_options.setMaxBasisDimension(num_total_rows); randomized_svd_options.setDebugMode(true); randomized_svd_options.setRandomizedSVD(true); CAROM::BasisGenerator sampler(randomized_svd_options, false); - sampler.takeSample(&sample1[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample2[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample3[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample4[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample5[row_offset[d_rank]], 0, 0); + sampler.takeSample(&sample1[row_offset[d_rank]]); + sampler.takeSample(&sample2[row_offset[d_rank]]); + sampler.takeSample(&sample3[row_offset[d_rank]]); + sampler.takeSample(&sample4[row_offset[d_rank]]); + sampler.takeSample(&sample5[row_offset[d_rank]]); const CAROM::Matrix* d_basis = sampler.getSpatialBasis(); const CAROM::Matrix* d_basis_right = sampler.getTemporalBasis(); @@ -227,14 +227,14 @@ TEST(RandomizedSVDTest, Test_RandomizedSVDSmallerSubspace) 4.80607940538476441361E+00, 3.21443716375044896694E+00, }; - CAROM::Options randomized_svd_options = CAROM::Options(d_num_rows, 3, 1); + CAROM::Options randomized_svd_options = CAROM::Options(d_num_rows, 3); randomized_svd_options.setMaxBasisDimension(num_total_rows); randomized_svd_options.setDebugMode(true); randomized_svd_options.setRandomizedSVD(true, 2); CAROM::BasisGenerator sampler(randomized_svd_options, false); - sampler.takeSample(&sample1[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample2[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample3[row_offset[d_rank]], 0, 0); + sampler.takeSample(&sample1[row_offset[d_rank]]); + sampler.takeSample(&sample2[row_offset[d_rank]]); + sampler.takeSample(&sample3[row_offset[d_rank]]); const CAROM::Matrix* d_basis = sampler.getSpatialBasis(); const CAROM::Matrix* d_basis_right = sampler.getTemporalBasis(); @@ -306,16 +306,16 @@ TEST(RandomizedSVDTest, Test_RandomizedSVDTransposedSmallerSubspace) 4.80607940538476441361E+00, 3.21443716375044896694E+00, }; - CAROM::Options randomized_svd_options = CAROM::Options(d_num_rows, 5, 1); + CAROM::Options randomized_svd_options = CAROM::Options(d_num_rows, 5); randomized_svd_options.setMaxBasisDimension(num_total_rows); randomized_svd_options.setDebugMode(true); randomized_svd_options.setRandomizedSVD(true, reduced_rows); CAROM::BasisGenerator sampler(randomized_svd_options, false); - sampler.takeSample(&sample1[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample2[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample3[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample4[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample5[row_offset[d_rank]], 0, 0); + sampler.takeSample(&sample1[row_offset[d_rank]]); + sampler.takeSample(&sample2[row_offset[d_rank]]); + sampler.takeSample(&sample3[row_offset[d_rank]]); + sampler.takeSample(&sample4[row_offset[d_rank]]); + sampler.takeSample(&sample5[row_offset[d_rank]]); const CAROM::Matrix* d_basis = sampler.getSpatialBasis(); const CAROM::Matrix* d_basis_right = sampler.getTemporalBasis(); diff --git a/unit_tests/test_SVD.cpp b/unit_tests/test_SVD.cpp index 68a7c70fc..daa17f5bb 100644 --- a/unit_tests/test_SVD.cpp +++ b/unit_tests/test_SVD.cpp @@ -212,20 +212,6 @@ TEST(SVDSerialTest, Test_getBasisIntervalStartTime) EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(2), 2); } - -TEST(SVDSerialTest, Test_increaseTimeInterval) -{ - FakeSVD svd(CAROM::Options(5, 2, 2)); - - ASSERT_NO_THROW(svd.takeSample(NULL, 0, true)); - ASSERT_NO_THROW(svd.takeSample(NULL, 0.5, true)); - ASSERT_NO_THROW(svd.takeSample(NULL, 1, true)); - ASSERT_NO_THROW(svd.takeSample(NULL, 1.5, true)); - - /* The maximum number of time intervals is surpassed */ - EXPECT_DEATH(svd.takeSample(NULL, 2, true), ".*"); -} - int main(int argc, char* argv[]) { ::testing::InitGoogleTest(&argc, argv); diff --git a/unit_tests/test_StaticSVD.cpp b/unit_tests/test_StaticSVD.cpp index ddda7bc4c..c1420344b 100644 --- a/unit_tests/test_StaticSVD.cpp +++ b/unit_tests/test_StaticSVD.cpp @@ -78,7 +78,7 @@ TEST(StaticSVDTest, Test_StaticSVD) std::vector similar(columns[j]); for (unsigned i = 0; i < 12; ++i) similar[i] *= sigmas[j]; - sampler.takeSample(similar.data(), 0, 0); + sampler.takeSample(similar.data()); } auto distU = sampler.getSpatialBasis(); @@ -285,14 +285,14 @@ TEST(StaticSVDTest, Test_StaticSVDClass) 4.84486375065219387892E+00, 3.66719976398777269821E+00, 2.69114625366671811335E+00 }; - CAROM::Options svd_options = CAROM::Options(d_num_rows, 3, 1); + CAROM::Options svd_options = CAROM::Options(d_num_rows, 3); svd_options.setMaxBasisDimension(num_total_rows); svd_options.setDebugMode(true); svd_options.setRandomizedSVD(false); CAROM::BasisGenerator sampler(svd_options, false); - sampler.takeSample(&sample1[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample2[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample3[row_offset[d_rank]], 0, 0); + sampler.takeSample(&sample1[row_offset[d_rank]]); + sampler.takeSample(&sample2[row_offset[d_rank]]); + sampler.takeSample(&sample3[row_offset[d_rank]]); const CAROM::Matrix* d_basis = sampler.getSpatialBasis(); const CAROM::Matrix* d_basis_right = sampler.getTemporalBasis(); @@ -365,16 +365,16 @@ TEST(StaticSVDTest, Test_StaticSVDTranspose) 4.84486375065219387892E+00, 3.66719976398777269821E+00, 2.69114625366671811335E+00, }; - CAROM::Options svd_options = CAROM::Options(d_num_rows, num_total_cols, 1); + CAROM::Options svd_options = CAROM::Options(d_num_rows, num_total_cols); svd_options.setMaxBasisDimension(num_total_rows); svd_options.setDebugMode(true); svd_options.setRandomizedSVD(false); CAROM::BasisGenerator sampler(svd_options, false); - sampler.takeSample(&sample1[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample2[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample3[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample4[row_offset[d_rank]], 0, 0); - sampler.takeSample(&sample5[row_offset[d_rank]], 0, 0); + sampler.takeSample(&sample1[row_offset[d_rank]]); + sampler.takeSample(&sample2[row_offset[d_rank]]); + sampler.takeSample(&sample3[row_offset[d_rank]]); + sampler.takeSample(&sample4[row_offset[d_rank]]); + sampler.takeSample(&sample5[row_offset[d_rank]]); const CAROM::Matrix* d_basis = sampler.getSpatialBasis(); const CAROM::Matrix* d_basis_right = sampler.getTemporalBasis(); diff --git a/unit_tests/test_basis_conversion.cpp b/unit_tests/test_basis_conversion.cpp new file mode 100644 index 000000000..2a4285398 --- /dev/null +++ b/unit_tests/test_basis_conversion.cpp @@ -0,0 +1,124 @@ +/****************************************************************************** + * + * Copyright (c) 2013-2024, Lawrence Livermore National Security, LLC + * and other libROM project developers. See the top-level COPYRIGHT + * file for details. + * + * SPDX-License-Identifier: (Apache-2.0 OR MIT) + * + *****************************************************************************/ + +#ifdef CAROM_HAS_GTEST + +#include +#include "linalg/BasisReader.h" +#include "linalg/Matrix.h" +#include +#include +#include +#include // for memcpy +#include +#include "mpi.h" +#include "utils/mpi_utils.h" + +/** + * Simple smoke test to make sure Google Test is properly linked + */ +TEST(GoogleTestFramework, GoogleTestFrameworkFound) { + SUCCEED(); +} + +TEST(StaticSVDTest, Test_StaticSVDClass) +{ + // Get the rank of this process, and the number of processors. + int mpi_init, d_rank, d_num_procs; + MPI_Initialized(&mpi_init); + if (mpi_init == 0) { + MPI_Init(nullptr, nullptr); + } + + MPI_Comm_rank(MPI_COMM_WORLD, &d_rank); + MPI_Comm_size(MPI_COMM_WORLD, &d_num_procs); + + // This test is designed only for single process. + EXPECT_EQ(d_num_procs, 1); + + /* This snapshot/basis is from test_StaticSVD */ + constexpr int num_total_rows = 5; + int d_num_rows = CAROM::split_dimension(num_total_rows, MPI_COMM_WORLD); + std::vector row_offset(d_num_procs + 1); + const int total_rows = CAROM::get_global_offsets(d_num_rows, row_offset, + MPI_COMM_WORLD); + EXPECT_EQ(total_rows, num_total_rows); + + double* sample1 = new double[5] {0.5377, 1.8339, -2.2588, 0.8622, 0.3188}; + double* sample2 = new double[5] {-1.3077, -0.4336, 0.3426, 3.5784, 2.7694}; + double* sample3 = new double[5] {-1.3499, 3.0349, 0.7254, -0.0631, 0.7147}; + + double* basis_true_ans = new double[15] { + 3.08158946098238906153E-01, -9.49897947980619661301E-02, -4.50691774108525788911E-01, + -1.43697905723455976457E-01, 9.53289043424090820622E-01, 8.77767692937209131898E-02, + -2.23655845793717528158E-02, -2.10628953513210204207E-01, 8.42235962392685943989E-01, + -7.29903965154318323805E-01, -1.90917141788945754488E-01, -2.77280930877637610266E-01, + -5.92561353877168350834E-01, -3.74570084880578441089E-02, 5.40928141934190823137E-02 + }; + + double* basis_right_true_ans = new double[9] { + -1.78651649346571794741E-01, 5.44387957786310106023E-01, -8.19588518467042281834E-01, + -9.49719639253861602768E-01, -3.13100149275943651084E-01, -9.50441422536040881122E-04, + -2.57130696341890396805E-01, 7.78209514167382598870E-01, 5.72951792961765460355E-01 + }; + + double* sv_true_ans = new double[3] { + 4.84486375065219387892E+00, 3.66719976398777269821E+00, 2.69114625366671811335E+00 + }; + + CAROM::BasisReader basis("test_basis"); + CAROM::BasisReader snapshot("test_basis_snapshot"); + const CAROM::Matrix* d_snapshot = snapshot.getSnapshotMatrix(); + const CAROM::Matrix* d_basis = basis.getSpatialBasis(); + const CAROM::Matrix* d_basis_right = basis.getTemporalBasis(); + const CAROM::Vector* sv = basis.getSingularValues(); + + EXPECT_EQ(d_basis->numRows(), d_num_rows); + EXPECT_EQ(d_basis->numColumns(), 3); + EXPECT_EQ(d_basis_right->numRows(), 3); + EXPECT_EQ(d_basis_right->numColumns(), 3); + EXPECT_EQ(sv->dim(), 3); + + double* d_basis_vals = d_basis->getData(); + double* d_basis_right_vals = d_basis_right->getData(); + + for (int i = 0; i < d_num_rows * 3; i++) { + EXPECT_NEAR(abs(d_basis_vals[i]), + abs(basis_true_ans[row_offset[d_rank] * 3 + i]), 1e-7); + } + + for (int i = 0; i < 9; i++) { + EXPECT_NEAR(abs(d_basis_right_vals[i]), abs(basis_right_true_ans[i]), 1e-7); + } + + for (int i = 0; i < 3; i++) { + EXPECT_NEAR(sv->item(i), sv_true_ans[i], 1e-7); + } +} + +int main(int argc, char* argv[]) +{ + ::testing::InitGoogleTest(&argc, argv); + MPI_Init(&argc, &argv); + int result = RUN_ALL_TESTS(); + MPI_Finalize(); + return result; +} + +#else // #ifndef CAROM_HAS_GTEST + +int main() +{ + std::cout << "libROM was compiled without Google Test support, so unit " + << "tests have been disabled. To enable unit tests, compile " + << "libROM with Google Test support." << std::endl; +} + +#endif // #endif CAROM_HAS_GTEST \ No newline at end of file diff --git a/unit_tests/test_include.cpp b/unit_tests/test_include.cpp index 8cbb6b3bb..2396067d3 100644 --- a/unit_tests/test_include.cpp +++ b/unit_tests/test_include.cpp @@ -73,7 +73,7 @@ main( // Take the first sample. if (inc_basis_generator.isNextSample(0.0)) { - status = inc_basis_generator.takeSample(&vals0[dim*rank], 0.0, 0.11); + status = inc_basis_generator.takeSample(&vals0[dim*rank]); if (status) { inc_basis_generator.computeNextSampleTime(&vals0[dim*rank], &vals0[dim*rank], @@ -83,7 +83,7 @@ main( // Take the second sample. if (status && inc_basis_generator.isNextSample(0.11)) { - status = inc_basis_generator.takeSample(&vals1[dim*rank], 0.11, 0.11); + status = inc_basis_generator.takeSample(&vals1[dim*rank]); if (status) { inc_basis_generator.computeNextSampleTime(&vals1[dim*rank], &vals1[dim*rank], diff --git a/unit_tests/uneven_dist.cpp b/unit_tests/uneven_dist.cpp index 7da2dfb41..6281e61e1 100644 --- a/unit_tests/uneven_dist.cpp +++ b/unit_tests/uneven_dist.cpp @@ -136,7 +136,7 @@ main( // Take the first sample. if (inc_basis_generator.isNextSample(0.0)) { - status = inc_basis_generator.takeSample(&vals0[offset], 0.0, 0.11); + status = inc_basis_generator.takeSample(&vals0[offset]); if (status) { inc_basis_generator.computeNextSampleTime(&vals0[offset], &vals0[offset], @@ -146,7 +146,7 @@ main( // Take the second sample. if (status && inc_basis_generator.isNextSample(0.11)) { - status = inc_basis_generator.takeSample(&vals1[offset], 0.11, 0.11); + status = inc_basis_generator.takeSample(&vals1[offset]); if (status) { inc_basis_generator.computeNextSampleTime(&vals1[offset], &vals1[offset], diff --git a/unit_tests/weak_scaling.cpp b/unit_tests/weak_scaling.cpp index b0e232564..34a4c2fe5 100644 --- a/unit_tests/weak_scaling.cpp +++ b/unit_tests/weak_scaling.cpp @@ -76,7 +76,7 @@ main( int samples_taken = 0; for (int i = 0; i < num_samples; ++i) { if (basis_generator.isNextSample(0.01*i)) { - status = basis_generator.takeSample(M[i], 0.01*i, 0.01); + status = basis_generator.takeSample(M[i]); if (!status) { break; }