From b6a9ac6d61341ad7e74f6ede6c48abe12e00248e Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Tue, 16 Jan 2024 18:09:42 -0800 Subject: [PATCH 01/37] BasisWriter::writeBasis create/open a file and closes the file at the end. --- lib/linalg/BasisReader.cpp | 1 - lib/linalg/BasisWriter.cpp | 45 +++++++++++++++++++------------------- lib/utils/CSVDatabase.cpp | 2 ++ lib/utils/CSVDatabase.h | 4 ++-- lib/utils/Database.cpp | 23 +++++++++++++++++++ lib/utils/Database.h | 6 +++-- lib/utils/HDFDatabase.cpp | 4 ++++ lib/utils/HDFDatabase.h | 4 ++-- 8 files changed, 59 insertions(+), 30 deletions(-) diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index 1b5c8583c..d2f0464aa 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -48,7 +48,6 @@ 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; diff --git a/lib/linalg/BasisWriter.cpp b/lib/linalg/BasisWriter.cpp index eee5c3a09..7c4b14402 100644 --- a/lib/linalg/BasisWriter.cpp +++ b/lib/linalg/BasisWriter.cpp @@ -53,20 +53,19 @@ BasisWriter::BasisWriter( char tmp2[100]; sprintf(tmp2, "_snapshot.%06d", rank); snap_file_name = base_file_name + tmp2; + + // create and open snapshot/basis database + // TODO(kevin): can it be CSV at all? we might want to remove if statement here. + if (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; - } + if (d_database) delete d_database; + if (d_snap_database) delete d_snap_database; } void @@ -81,13 +80,10 @@ BasisWriter::writeBasis(const std::string& kind) 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); + if (fileExists(full_file_name)) + d_database->open(full_file_name, "wr"); + else + d_database->create(full_file_name); d_database->putDouble(tmp, time_interval_start_time); @@ -122,15 +118,15 @@ BasisWriter::writeBasis(const std::string& kind) ++d_num_intervals_written; + d_database->putInteger("num_time_intervals", 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); + if (fileExists(snap_file_name)) + d_snap_database->open(snap_file_name, "wr"); + else + d_snap_database->create(snap_file_name); d_snap_database->putDouble(tmp, time_interval_start_time); @@ -143,6 +139,9 @@ BasisWriter::writeBasis(const std::string& kind) d_snap_database->putInteger(tmp, num_cols); sprintf(tmp, "snapshot_matrix_%06d", d_num_intervals_written); d_snap_database->putDoubleArray(tmp, &snapshots->item(0,0), num_rows*num_cols); + + d_snap_database->putInteger("num_time_intervals", d_num_intervals_written); + d_snap_database->close(); } } 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..53e240dd9 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..673723cca 100644 --- a/lib/utils/HDFDatabase.cpp +++ b/lib/utils/HDFDatabase.cpp @@ -44,6 +44,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 +65,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. From e5c777918f8dd6f2cb096932cd5ebbf5c8c3e29c Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Tue, 16 Jan 2024 18:23:41 -0800 Subject: [PATCH 02/37] stylization. --- lib/utils/Database.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/utils/Database.cpp b/lib/utils/Database.cpp index 53e240dd9..49c6c9eb7 100644 --- a/lib/utils/Database.cpp +++ b/lib/utils/Database.cpp @@ -18,9 +18,9 @@ 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. + std::ifstream f(name.c_str()); + return f.good(); + // ifstream f will be closed upon the end of the function. } Database::Database() From fd53d75a79af9c4845594c1f897980673284b0ba Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Tue, 16 Jan 2024 18:48:53 -0800 Subject: [PATCH 03/37] HDFDatabase::putIntegerArray - overwrites if the dataset exists. --- lib/linalg/BasisWriter.cpp | 3 ++- lib/utils/HDFDatabase.cpp | 35 +++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/linalg/BasisWriter.cpp b/lib/linalg/BasisWriter.cpp index 7c4b14402..3a054786a 100644 --- a/lib/linalg/BasisWriter.cpp +++ b/lib/linalg/BasisWriter.cpp @@ -80,7 +80,8 @@ BasisWriter::writeBasis(const std::string& kind) sprintf(tmp, "time_%06d", d_num_intervals_written); if (kind == "basis") { - if (fileExists(full_file_name)) + bool file_exists = fileExists(full_file_name); + if (file_exists) d_database->open(full_file_name, "wr"); else d_database->create(full_file_name); diff --git a/lib/utils/HDFDatabase.cpp b/lib/utils/HDFDatabase.cpp index 673723cca..a40d3b8ee 100644 --- a/lib/utils/HDFDatabase.cpp +++ b/lib/utils/HDFDatabase.cpp @@ -12,6 +12,7 @@ #include "HDFDatabase.h" #include "Utilities.h" +#include namespace CAROM { @@ -123,21 +124,31 @@ HDFDatabase::putIntegerArray( hid_t space = H5Screate_simple(1, dim, 0); CAROM_VERIFY(space >= 0); + const bool key_exists = (H5Lexists(d_group_id, key.c_str(), H5P_DEFAULT) > 0); + hid_t dataset; + if (key_exists) + { + std::cout << "HDF5Database: overwriting dataset " << key << std::endl; + dataset = H5Dopen(d_group_id, key.c_str(), H5P_DEFAULT); + } + else + { #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6)) - hid_t dataset = H5Dcreate(d_group_id, - key.c_str(), - H5T_STD_I32BE, - space, - H5P_DEFAULT, - H5P_DEFAULT, - H5P_DEFAULT); + dataset = H5Dcreate(d_group_id, + key.c_str(), + H5T_STD_I32BE, + space, + H5P_DEFAULT, + H5P_DEFAULT, + H5P_DEFAULT); #else - hid_t dataset = H5Dcreate(d_group_id, - key.c_str(), - H5T_STD_I32BE, - space, - H5P_DEFAULT); + dataset = H5Dcreate(d_group_id, + key.c_str(), + H5T_STD_I32BE, + space, + H5P_DEFAULT); #endif + } CAROM_VERIFY(dataset >= 0); herr_t errf = H5Dwrite(dataset, From 302823aac51a5b6a98e29a392ab3d088a6bef68b Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 8 Feb 2024 16:36:27 -0800 Subject: [PATCH 04/37] enforce single time interval in Options. --- lib/linalg/Options.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/linalg/Options.h b/lib/linalg/Options.h index a7c6a1fb7..6b6b15f11 100644 --- a/lib/linalg/Options.h +++ b/lib/linalg/Options.h @@ -50,7 +50,15 @@ class Options samples_per_time_interval(samples_per_time_interval_), max_time_intervals(max_time_intervals_), update_right_SV(update_right_SV_), - write_snapshots(write_snapshots_) {}; + write_snapshots(write_snapshots_) + { + if (max_time_intervals > 1) + { + printf("time interval is obsolete and will be removed in the future. Set max_time_intervals=%d to 1 or -1!\n", + max_time_intervals); + } + CAROM_VERIFY(max_time_intervals <= 1); + }; /** * @brief Sets the maximum basis dimension of the SVD algorithm. From 14cbb89ec749490b93fdbf00435b9b80debb09e3 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 8 Feb 2024 16:47:43 -0800 Subject: [PATCH 05/37] HDFDatabase::putIntegerArray does not allow overwrite. --- lib/utils/HDFDatabase.cpp | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/lib/utils/HDFDatabase.cpp b/lib/utils/HDFDatabase.cpp index a40d3b8ee..60bd0ad69 100644 --- a/lib/utils/HDFDatabase.cpp +++ b/lib/utils/HDFDatabase.cpp @@ -124,31 +124,21 @@ HDFDatabase::putIntegerArray( hid_t space = H5Screate_simple(1, dim, 0); CAROM_VERIFY(space >= 0); - const bool key_exists = (H5Lexists(d_group_id, key.c_str(), H5P_DEFAULT) > 0); - hid_t dataset; - if (key_exists) - { - std::cout << "HDF5Database: overwriting dataset " << key << std::endl; - dataset = H5Dopen(d_group_id, key.c_str(), H5P_DEFAULT); - } - else - { #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6)) - dataset = H5Dcreate(d_group_id, - key.c_str(), - H5T_STD_I32BE, - space, - H5P_DEFAULT, - H5P_DEFAULT, - H5P_DEFAULT); + hid_t dataset = H5Dcreate(d_group_id, + key.c_str(), + H5T_STD_I32BE, + space, + H5P_DEFAULT, + H5P_DEFAULT, + H5P_DEFAULT); #else - dataset = H5Dcreate(d_group_id, - key.c_str(), - H5T_STD_I32BE, - space, - H5P_DEFAULT); + hid_t dataset = H5Dcreate(d_group_id, + key.c_str(), + H5T_STD_I32BE, + space, + H5P_DEFAULT); #endif - } CAROM_VERIFY(dataset >= 0); herr_t errf = H5Dwrite(dataset, From b7993175bd647a94cad1c92a97e0c17143c17f9f Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 8 Feb 2024 16:53:11 -0800 Subject: [PATCH 06/37] BasisWriter::writeBasis always create the file, which will overwrite the exisiting file. --- lib/linalg/BasisWriter.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/linalg/BasisWriter.cpp b/lib/linalg/BasisWriter.cpp index 3a054786a..cdde9b731 100644 --- a/lib/linalg/BasisWriter.cpp +++ b/lib/linalg/BasisWriter.cpp @@ -81,10 +81,7 @@ BasisWriter::writeBasis(const std::string& kind) if (kind == "basis") { bool file_exists = fileExists(full_file_name); - if (file_exists) - d_database->open(full_file_name, "wr"); - else - d_database->create(full_file_name); + d_database->create(full_file_name); d_database->putDouble(tmp, time_interval_start_time); @@ -124,10 +121,7 @@ BasisWriter::writeBasis(const std::string& kind) } if (kind == "snapshot") { - if (fileExists(snap_file_name)) - d_snap_database->open(snap_file_name, "wr"); - else - d_snap_database->create(snap_file_name); + d_snap_database->create(snap_file_name); d_snap_database->putDouble(tmp, time_interval_start_time); From 6d2067a9f06e25606176491aba09cf4e028ea8a8 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 8 Feb 2024 17:03:38 -0800 Subject: [PATCH 07/37] add a header and stylization. --- lib/linalg/Options.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/linalg/Options.h b/lib/linalg/Options.h index 6b6b15f11..5f8b5060d 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 { /** @@ -55,7 +57,7 @@ class Options if (max_time_intervals > 1) { printf("time interval is obsolete and will be removed in the future. Set max_time_intervals=%d to 1 or -1!\n", - max_time_intervals); + max_time_intervals); } CAROM_VERIFY(max_time_intervals <= 1); }; From dc6304216a9cc94e95089e64dcc08d1a7c91780b Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 8 Feb 2024 17:13:33 -0800 Subject: [PATCH 08/37] remove increase time interval test, as time interval is fixed to 1. --- unit_tests/test_SVD.cpp | 14 -------------- 1 file changed, 14 deletions(-) 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); From 8d84c1b08c6d517a98bd94eded5fa28c80399fe6 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 15 Feb 2024 11:57:55 -0800 Subject: [PATCH 09/37] add an error message for a guidance. --- lib/linalg/svd/SVD.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/linalg/svd/SVD.h b/lib/linalg/svd/SVD.h index 9ce034843..c56ef47d1 100644 --- a/lib/linalg/svd/SVD.h +++ b/lib/linalg/svd/SVD.h @@ -164,6 +164,7 @@ class SVD void increaseTimeInterval() { + CAROM_ERROR("SVD::increaseTimeInterval- Time interval is obsolete and will be removed in the future. You received this error presumably because the number of samples reached its limit.\n"); int num_time_intervals = static_cast(d_time_interval_start_times.size()); CAROM_VERIFY(d_max_time_intervals == -1 || From 85aa02120786bd3dc2340d43469c80053a9f1603 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 15 Feb 2024 12:27:15 -0800 Subject: [PATCH 10/37] remove test_SVD from ci workflow. --- .github/workflows/run_tests/action.yml | 1 - lib/linalg/Options.h | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/run_tests/action.yml b/.github/workflows/run_tests/action.yml index 34471de9b..7a7ee5b2b 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 diff --git a/lib/linalg/Options.h b/lib/linalg/Options.h index 5f8b5060d..be36bff52 100644 --- a/lib/linalg/Options.h +++ b/lib/linalg/Options.h @@ -44,7 +44,7 @@ class Options */ Options(int dim_, int samples_per_time_interval_, - int max_time_intervals_ = -1, + int max_time_intervals_ = 1, bool update_right_SV_ = false, bool write_snapshots_ = false ): dim(dim_), @@ -56,10 +56,10 @@ class Options { if (max_time_intervals > 1) { - printf("time interval is obsolete and will be removed in the future. Set max_time_intervals=%d to 1 or -1!\n", + printf("time interval is obsolete and will be removed in the future. Set max_time_intervals=%d to 1!\n", max_time_intervals); } - CAROM_VERIFY(max_time_intervals <= 1); + CAROM_VERIFY(max_time_intervals == 1); }; /** From 3c3c6670dc0ca70e7fc2e7f4f0e5a90914a73d42 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 15 Feb 2024 12:45:08 -0800 Subject: [PATCH 11/37] SVD::increaseTimeInterval - allow the initial time interval. --- lib/linalg/svd/SVD.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/linalg/svd/SVD.h b/lib/linalg/svd/SVD.h index c56ef47d1..cb9fb1fbf 100644 --- a/lib/linalg/svd/SVD.h +++ b/lib/linalg/svd/SVD.h @@ -164,11 +164,11 @@ class SVD void increaseTimeInterval() { - CAROM_ERROR("SVD::increaseTimeInterval- Time interval is obsolete and will be removed in the future. You received this error presumably because the number of samples reached its limit.\n"); 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); + CAROM_VERIFY(d_max_time_intervals == 1); + if (num_time_intervals > 0) + CAROM_ERROR("SVD::increaseTimeInterval- Time interval is obsolete and will be removed in the future. You received this error presumably because the number of samples reached its limit.\n"); d_time_interval_start_times.resize( static_cast(num_time_intervals) + 1); } From 8371e85c99e5065b35afd74b51292ca37e4064d6 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Thu, 15 Feb 2024 13:19:14 -0800 Subject: [PATCH 12/37] minor fix in test_IncrementalSVDBrand. --- unit_tests/test_IncrementalSVDBrand.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unit_tests/test_IncrementalSVDBrand.cpp b/unit_tests/test_IncrementalSVDBrand.cpp index 014c05021..a18d842d7 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, 1, true) .setMaxBasisDimension(num_total_rows) .setIncrementalSVD(1e-1, 1e-1, From bf1e126afc18803f6d9b6f2d64d518e53a4df970 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Fri, 16 Feb 2024 09:32:52 -0800 Subject: [PATCH 13/37] reflecting the comments. --- lib/linalg/BasisWriter.cpp | 18 ++++++++++-------- lib/linalg/Options.h | 4 ++-- lib/linalg/svd/SVD.h | 5 ++++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/linalg/BasisWriter.cpp b/lib/linalg/BasisWriter.cpp index cdde9b731..3e3402a73 100644 --- a/lib/linalg/BasisWriter.cpp +++ b/lib/linalg/BasisWriter.cpp @@ -55,17 +55,15 @@ BasisWriter::BasisWriter( snap_file_name = base_file_name + tmp2; // create and open snapshot/basis database - // TODO(kevin): can it be CSV at all? we might want to remove if statement here. - if (db_format_ == Database::HDF5) { - d_snap_database = new HDFDatabase(); - d_database = new HDFDatabase(); - } + CAROM_VERIFY(db_format_ == Database::HDF5); + d_snap_database = new HDFDatabase(); + d_database = new HDFDatabase(); } BasisWriter::~BasisWriter() { - if (d_database) delete d_database; - if (d_snap_database) delete d_snap_database; + delete d_database; + delete d_snap_database; } void @@ -80,7 +78,6 @@ BasisWriter::writeBasis(const std::string& kind) sprintf(tmp, "time_%06d", d_num_intervals_written); if (kind == "basis") { - bool file_exists = fileExists(full_file_name); d_database->create(full_file_name); d_database->putDouble(tmp, time_interval_start_time); @@ -116,6 +113,7 @@ BasisWriter::writeBasis(const std::string& kind) ++d_num_intervals_written; + CAROM_VERIFY(d_num_intervals_written == 1); d_database->putInteger("num_time_intervals", d_num_intervals_written); d_database->close(); } @@ -135,6 +133,10 @@ BasisWriter::writeBasis(const std::string& kind) sprintf(tmp, "snapshot_matrix_%06d", d_num_intervals_written); d_snap_database->putDoubleArray(tmp, &snapshots->item(0,0), num_rows*num_cols); + // NOTE(kevin): number of interval will be removed in future. + // Until then, we keep the current practice of + // un-increased number of interval, that is 0. + CAROM_VERIFY(d_num_intervals_written == 0); d_snap_database->putInteger("num_time_intervals", d_num_intervals_written); d_snap_database->close(); } diff --git a/lib/linalg/Options.h b/lib/linalg/Options.h index be36bff52..816002d2b 100644 --- a/lib/linalg/Options.h +++ b/lib/linalg/Options.h @@ -56,8 +56,8 @@ class Options { if (max_time_intervals > 1) { - printf("time interval is obsolete and will be removed in the future. Set max_time_intervals=%d to 1!\n", - max_time_intervals); + printf("time interval is obsolete and will be removed in the future." + " Set max_time_intervals=%d to 1!\n", max_time_intervals); } CAROM_VERIFY(max_time_intervals == 1); }; diff --git a/lib/linalg/svd/SVD.h b/lib/linalg/svd/SVD.h index cb9fb1fbf..de2c6f389 100644 --- a/lib/linalg/svd/SVD.h +++ b/lib/linalg/svd/SVD.h @@ -168,7 +168,10 @@ class SVD static_cast(d_time_interval_start_times.size()); CAROM_VERIFY(d_max_time_intervals == 1); if (num_time_intervals > 0) - CAROM_ERROR("SVD::increaseTimeInterval- Time interval is obsolete and will be removed in the future. You received this error presumably because the number of samples reached its limit.\n"); + CAROM_ERROR("SVD::increaseTimeInterval- Time interval is obsolete" + " and will be removed in the future. " + "You received this error presumably " + "because the number of samples reached its limit.\n"); d_time_interval_start_times.resize( static_cast(num_time_intervals) + 1); } From cf6f27c77be0ba5adea4c24f88e96b537f9705dd Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sat, 17 Feb 2024 12:00:34 -0800 Subject: [PATCH 14/37] removed the concept of time interval in BasisReader. time argument remains for backward compatibility. --- lib/linalg/BasisReader.cpp | 222 ++++++++++++------------------------- lib/linalg/BasisReader.h | 147 ++++++++++++------------ 2 files changed, 150 insertions(+), 219 deletions(-) diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index d2f0464aa..60d8c44a1 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -49,16 +49,6 @@ BasisReader::BasisReader( } 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() @@ -71,24 +61,15 @@ Matrix* BasisReader::getSpatialBasis( double time) { - 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); + d_last_basis_idx = 0; + 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, + + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + d_database->getDoubleArray("spatial_basis_000000", &spatial_basis_vectors->item(0, 0), num_rows*num_cols); return spatial_basis_vectors; @@ -99,7 +80,7 @@ BasisReader::getSpatialBasis( double time, int n) { - return getSpatialBasis(time, 1, n); + return getSpatialBasis(1, n); } Matrix* @@ -108,19 +89,9 @@ BasisReader::getSpatialBasis( 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); + d_last_basis_idx = 0; + int num_rows = getDim("basis"); + int num_cols = getNumSamples("basis"); char tmp[100]; CAROM_VERIFY(0 < start_col <= num_cols); @@ -128,7 +99,9 @@ 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); + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + sprintf(tmp, "spatial_basis_000000"); d_database->getDoubleArray(tmp, &spatial_basis_vectors->item(0, 0), num_rows*num_cols_to_read, @@ -143,7 +116,7 @@ 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++) @@ -163,30 +136,22 @@ BasisReader::getSpatialBasis( } delete sv; - return getSpatialBasis(time, num_used_singular_values); + return getSpatialBasis(num_used_singular_values); } Matrix* BasisReader::getTemporalBasis( double time) { - 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); + d_last_basis_idx = 0; + 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); + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + sprintf(tmp, "temporal_basis_000000"); d_database->getDoubleArray(tmp, &temporal_basis_vectors->item(0, 0), num_rows*num_cols); @@ -198,7 +163,7 @@ BasisReader::getTemporalBasis( double time, int n) { - return getTemporalBasis(time, 1, n); + return getTemporalBasis(1, n); } Matrix* @@ -207,19 +172,9 @@ BasisReader::getTemporalBasis( 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); + d_last_basis_idx = 0; + int num_rows = getDim("temporal_basis"); + int num_cols = getNumSamples("temporal_basis"); char tmp[100]; CAROM_VERIFY(0 < start_col <= num_cols); @@ -227,7 +182,9 @@ 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); + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + sprintf(tmp, "temporal_basis_000000"); d_database->getDoubleArray(tmp, &temporal_basis_vectors->item(0, 0), num_rows*num_cols_to_read, @@ -242,7 +199,7 @@ 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++) @@ -262,31 +219,24 @@ 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; + d_last_basis_idx = 0; char tmp[100]; int size; - sprintf(tmp, "singular_value_size_%06d", i); + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + sprintf(tmp, "singular_value_size_000000"); d_database->getInteger(tmp, size); Vector* singular_values = new Vector(size, false); - sprintf(tmp, "singular_value_%06d", i); + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + sprintf(tmp, "singular_value_000000"); d_database->getDoubleArray(tmp, &singular_values->item(0), size); @@ -298,7 +248,7 @@ 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++) @@ -330,60 +280,46 @@ 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; + d_last_basis_idx = 0; 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); + + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + if (kind == "basis") sprintf(tmp, "spatial_basis_num_rows_000000"); + else if (kind == "snapshot") sprintf(tmp, "snapshot_matrix_num_rows_000000"); + else if (kind == "temporal_basis") sprintf(tmp, + "temporal_basis_num_rows_000000"); + 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; + d_last_basis_idx = 0; 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); + + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + if (kind == "basis") sprintf(tmp, "spatial_basis_num_cols_000000"); + else if (kind == "snapshot") sprintf(tmp, "snapshot_matrix_num_cols_000000"); + else if (kind == "temporal_basis") sprintf(tmp, + "temporal_basis_num_cols_000000"); + d_database->getInteger(tmp, num_cols); return num_cols; } @@ -392,23 +328,15 @@ Matrix* BasisReader::getSnapshotMatrix( double time) { - 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; + d_last_basis_idx = 0; int num_rows = getDim("snapshot",time); int num_cols = getNumSamples("snapshot",time); char tmp[100]; Matrix* snapshots = new Matrix(num_rows, num_cols, false); - sprintf(tmp, "snapshot_matrix_%06d", i); + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + sprintf(tmp, "snapshot_matrix_000000"); d_database->getDoubleArray(tmp, &snapshots->item(0, 0), num_rows*num_cols); @@ -420,7 +348,7 @@ BasisReader::getSnapshotMatrix( double time, int n) { - return getSnapshotMatrix(time, 1, n); + return getSnapshotMatrix(1, n); } Matrix* @@ -429,19 +357,9 @@ BasisReader::getSnapshotMatrix( 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); + d_last_basis_idx = 0; + 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); @@ -449,7 +367,9 @@ BasisReader::getSnapshotMatrix( char tmp[100]; Matrix* snapshots = new Matrix(num_rows, num_cols_to_read, false); - sprintf(tmp, "snapshot_matrix_%06d", i); + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. + sprintf(tmp, "snapshot_matrix_000000"); 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..7a01ddb4e 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -58,10 +58,9 @@ class 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 - * * @param[in] time Time at which we are interested in the basis vectors. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * * @return True if the basis vectors at the requested time are different * from the last requested basis vectors. @@ -70,24 +69,7 @@ class BasisReader { 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; + return (d_last_basis_idx == -1); } /** @@ -95,10 +77,9 @@ class BasisReader { * @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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * * @return The spatial basis vectors for the requested time. */ @@ -111,11 +92,11 @@ class BasisReader { * @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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * @param[in] n The number of spatial basis vectors desired. * * @return The spatial basis vectors for the requested time. @@ -130,12 +111,12 @@ 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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * @param[in] start_col The starting column desired. * @param[in] end_col The starting column desired. * @@ -152,11 +133,11 @@ 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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * @param[in] ef The desired energy fraction. * * @return The spatial basis vectors for the requested time. @@ -171,10 +152,9 @@ 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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * * @return The temporal basis vectors for the requested time. */ @@ -187,11 +167,11 @@ class BasisReader { * @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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * @param[in] n The number of temporal basis vectors desired. * * @return The temporal basis vectors for the requested time. @@ -206,12 +186,12 @@ 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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * @param[in] start_col The starting column desired. * @param[in] end_col The starting column desired. * @@ -228,11 +208,11 @@ 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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * @param[in] ef The desired energy fraction. * * @return The temporal basis vectors for the requested time. @@ -246,27 +226,44 @@ class BasisReader { * * @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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. + * + * @return The temporal basis vectors for the requested time. + */ + Vector* + getSingularValues(); + + /** + * + * @brief Returns the singular values for the requested time. + * NOTE: this function is obsolete and remains only for backward compatibility. + * Will be removed in future. * * @param[in] time Time for which we want the basis vectors. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * * @return The temporal basis vectors for the requested time. */ Vector* getSingularValues( - double time); + double time) + { + return 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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * @param[in] ef The desired energy fraction. * * @return The temporal basis vectors for the requested time. @@ -283,9 +280,24 @@ class BasisReader { * @return The dimension of the system on this processor. */ int + getDim( + const std::string kind); + + /** + * + * @brief Returns the dimension of the system on this processor. + * NOTE: this function is obsolete and remains only for backward compatibility. + * Will be removed in future. + * + * @return The dimension of the system on this processor. + */ + int getDim( const std::string kind, - double time); + double time) + { + return getDim(kind); + } /** * @@ -294,18 +306,33 @@ class BasisReader { * @return The number of samples in file. */ int + getNumSamples( + const std::string kind); + + /** + * + * @brief Returns the number of samples (columns) in file. + * NOTE: this function is obsolete and remains only for backward compatibility. + * Will be removed in future. + * + * @return The number of samples in file. + */ + int getNumSamples( const std::string kind, - double time); + double time) + { + return getNumSamples(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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * * @return The snapshot matrix for the requested time. */ @@ -317,11 +344,11 @@ class BasisReader { * * @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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * @param[in] n The number of basis vectors desired. * * @return The snapshot matrix for the requested time. @@ -335,12 +362,12 @@ class BasisReader { * * @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. + * NOTE: this argument is obsolete and remains only for backward compatibility. + * Will be removed in future. * @param[in] start_col The starting column desired. * @param[in] end_col The starting column desired. * @@ -371,22 +398,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. */ From 3153eaf80a6322ac9e43b29b82a9750693270a30 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sat, 17 Feb 2024 12:08:14 -0800 Subject: [PATCH 15/37] BasisWriter: removed the concept of time interval. --- lib/linalg/BasisWriter.cpp | 42 ++++++++++++-------------------------- lib/linalg/BasisWriter.h | 6 ------ 2 files changed, 13 insertions(+), 35 deletions(-) diff --git a/lib/linalg/BasisWriter.cpp b/lib/linalg/BasisWriter.cpp index 3e3402a73..712d5f0c7 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), @@ -69,75 +68,60 @@ BasisWriter::~BasisWriter() 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); + // This 0 index is the remainder from time interval concept. + // This remains only for backward compatibility purpose. if (kind == "basis") { 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_000000"); 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_000000"); d_database->putInteger(tmp, num_cols); - sprintf(tmp, "spatial_basis_%06d", d_num_intervals_written); + sprintf(tmp, "spatial_basis_000000"); 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_000000"); 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_000000"); d_database->putInteger(tmp, num_cols); - sprintf(tmp, "temporal_basis_%06d", d_num_intervals_written); + sprintf(tmp, "temporal_basis_000000"); 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_000000"); d_database->putInteger(tmp, sv_dim); - sprintf(tmp, "singular_value_%06d", d_num_intervals_written); + sprintf(tmp, "singular_value_000000"); d_database->putDoubleArray(tmp, &sv->item(0), sv_dim); - ++d_num_intervals_written; - - CAROM_VERIFY(d_num_intervals_written == 1); - d_database->putInteger("num_time_intervals", d_num_intervals_written); d_database->close(); } if (kind == "snapshot") { 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_000000"); 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_000000"); d_snap_database->putInteger(tmp, num_cols); - sprintf(tmp, "snapshot_matrix_%06d", d_num_intervals_written); + sprintf(tmp, "snapshot_matrix_000000"); d_snap_database->putDoubleArray(tmp, &snapshots->item(0,0), num_rows*num_cols); - // NOTE(kevin): number of interval will be removed in future. - // Until then, we keep the current practice of - // un-increased number of interval, that is 0. - CAROM_VERIFY(d_num_intervals_written == 0); - d_snap_database->putInteger("num_time_intervals", d_num_intervals_written); 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; }; } From 6ffa9082b7d74cb8a6ef51c68ba8f8ee3d6f49ac Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sat, 17 Feb 2024 12:31:43 -0800 Subject: [PATCH 16/37] minor fix in BasisReader. --- lib/linalg/BasisReader.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index 60d8c44a1..0bf3dd074 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -80,7 +80,7 @@ BasisReader::getSpatialBasis( double time, int n) { - return getSpatialBasis(1, n); + return getSpatialBasis(time, 1, n); } Matrix* @@ -136,7 +136,7 @@ BasisReader::getSpatialBasis( } delete sv; - return getSpatialBasis(num_used_singular_values); + return getSpatialBasis(time, num_used_singular_values); } Matrix* @@ -163,7 +163,7 @@ BasisReader::getTemporalBasis( double time, int n) { - return getTemporalBasis(1, n); + return getTemporalBasis(time, 1, n); } Matrix* @@ -219,7 +219,7 @@ BasisReader::getTemporalBasis( } delete sv; - return getTemporalBasis(num_used_singular_values); + return getTemporalBasis(time, num_used_singular_values); } Vector* @@ -348,7 +348,7 @@ BasisReader::getSnapshotMatrix( double time, int n) { - return getSnapshotMatrix(1, n); + return getSnapshotMatrix(time, 1, n); } Matrix* From 8aa426d0ff9e8a8a4b6fc39de244a88360d71780 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sat, 17 Feb 2024 13:48:57 -0800 Subject: [PATCH 17/37] SVD: removed the concept of time intervals. --- CMakeLists.txt | 1 - lib/linalg/BasisGenerator.cpp | 18 +- lib/linalg/BasisGenerator.h | 31 --- lib/linalg/svd/IncrementalSVD.cpp | 17 +- lib/linalg/svd/IncrementalSVD.h | 6 +- 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 | 6 +- lib/linalg/svd/SVD.h | 78 +------ lib/linalg/svd/StaticSVD.cpp | 49 ++--- lib/linalg/svd/StaticSVD.h | 9 +- unit_tests/test_IncrementalSVD.cpp | 3 +- unit_tests/test_SVD.cpp | 227 -------------------- 18 files changed, 47 insertions(+), 475 deletions(-) delete mode 100644 unit_tests/test_SVD.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 35919a836..9a2b489f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -270,7 +270,6 @@ if(GTEST_FOUND) GNAT QDEIM S_OPT - SVD StaticSVD RandomizedSVD IncrementalSVD diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index 7c1925a78..36ba53aeb 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -135,6 +135,7 @@ BasisGenerator::takeSample( { 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); @@ -144,20 +145,7 @@ BasisGenerator::takeSample( 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 @@ -199,7 +187,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..fee00f4ee 100644 --- a/lib/linalg/BasisGenerator.h +++ b/lib/linalg/BasisGenerator.h @@ -222,37 +222,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/svd/IncrementalSVD.cpp b/lib/linalg/svd/IncrementalSVD.cpp index 807e8d473..c65f2a8b0 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 && (!isNewSample())) { // 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 (isNewSample()) { + 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..144781597 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); /** @@ -118,8 +115,7 @@ class IncrementalSVD : public SVD 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..e2a994b35 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 && (!isNewSample())) { // 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..9ce73649d 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 && (!isNewSample())) { // 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..de9cb3fe6 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 && (!isNewSample())) { // 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..fdf3ef214 100644 --- a/lib/linalg/svd/SVD.cpp +++ b/lib/linalg/svd/SVD.cpp @@ -19,20 +19,16 @@ 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.samples_per_time_interval), 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); } diff --git a/lib/linalg/svd/SVD.h b/lib/linalg/svd/SVD.h index de2c6f389..25a4662b0 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; /** @@ -110,39 +107,6 @@ class SVD const Matrix* getSnapshotMatrix() = 0; - /** - * @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 static_cast(d_time_interval_start_times.size()); - } - - /** - * @brief Returns the start time for the requested time interval. - * - * @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 - { - 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]; - } - /** * @brief Returns true if the next sample will result in a new time * interval. @@ -151,38 +115,27 @@ class SVD * interval. */ bool - isNewTimeInterval() const + isNewSample() 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 + * @brief Get the number of samples taken. * */ - void - increaseTimeInterval() + int getNumSamples() const { - int num_time_intervals = - static_cast(d_time_interval_start_times.size()); - CAROM_VERIFY(d_max_time_intervals == 1); - if (num_time_intervals > 0) - CAROM_ERROR("SVD::increaseTimeInterval- Time interval is obsolete" - " and will be removed in the future. " - "You received this error presumably " - "because the number of samples reached its limit.\n"); - d_time_interval_start_times.resize( - static_cast(num_time_intervals) + 1); + return d_num_samples; } /** - * @brief Get the number of samples taken. + * @brief Get the maximum number of samples that can be taken. * */ - int getNumSamples() const + int getMaxNumSamples() const { - return d_num_samples; + return d_max_num_samples; } protected: @@ -202,15 +155,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. @@ -260,11 +207,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..47e8344e1 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,14 @@ StaticSVD::takeSample( return false; } - if (isNewTimeInterval()) { + if (isNewSample()) { // We have a new time interval. 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 +134,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 +143,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 +159,7 @@ StaticSVD::getSpatialBasis() else { CAROM_ASSERT(d_basis != 0); } - CAROM_ASSERT(thisIntervalBasisCurrent()); + CAROM_ASSERT(isBasisCurrent()); return d_basis; } @@ -187,7 +168,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 +184,7 @@ StaticSVD::getTemporalBasis() else { CAROM_ASSERT(d_basis_right != 0); } - CAROM_ASSERT(thisIntervalBasisCurrent()); + CAROM_ASSERT(isBasisCurrent()); return d_basis_right; } @@ -212,7 +193,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 +209,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 +344,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..a650936f2 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); /** @@ -128,9 +125,9 @@ class StaticSVD : public SVD * date. */ bool - thisIntervalBasisCurrent() + isBasisCurrent() { - return d_this_interval_basis_current; + return d_basis_is_current; } /** @@ -147,7 +144,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/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_SVD.cpp b/unit_tests/test_SVD.cpp deleted file mode 100644 index daa17f5bb..000000000 --- a/unit_tests/test_SVD.cpp +++ /dev/null @@ -1,227 +0,0 @@ -/****************************************************************************** - * - * 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) - * - *****************************************************************************/ - -// Description: This source file is a test runner that uses the Google Test -// Framework to run unit tests on the CAROM::SVD class. - -#include - -#ifdef CAROM_HAS_GTEST -#include -#include -#include "linalg/Options.h" -#include "linalg/svd/SVD.h" - -/** - * Simple smoke test to make sure Google Test is properly linked - */ -TEST(GoogleTestFramework, GoogleTestFrameworkFound) { - SUCCEED(); -} - -/** - * Fake SVD to test parts of the SVD abstract base class. (For those - * unfamiliar, "fake" or "mock" is a term of art in unit testing - * referring to an implementation that simulates the behavior of - * real objects.) - */ - -class FakeSVD : public CAROM::SVD -{ -public: - - FakeSVD(CAROM::Options options) - : SVD(options) - { - } - - ~FakeSVD() - { - } - - /** - * Stub implementations of methods not really testable from the - * abstract base class, because there is no meaningful data we could - * return without implementing an actual singular value decomposition. - * - */ - virtual const CAROM::Matrix* getSpatialBasis() { - return NULL; - } - virtual const CAROM::Matrix* getTemporalBasis() { - return NULL; - } - virtual const CAROM::Vector* getSingularValues() { - return NULL; - } - virtual const CAROM::Matrix* getSnapshotMatrix() { - return NULL; - } - - /** - * The only testable methods from the SVD abstract base class are - * those with concrete implementations, namely: - * - * int getDim() const; - * - * int getNumBasisTimeIntervals() const; - * - * double getBasisIntervalStartTime(int) const; - * - * bool isNewTimeInterval() const; - */ - - /** - * This method needs to do a few things: - * - * 1) If there are no sample time intervals stored, create one and - * record its start time. Set the number of samples in the current - * time interval to zero. - * - * 2) If adding another sample to the current sample time interval - * would exceed the number of samples per time interval set in - * the constructor, create a new time interval and record its - * start time. Set the number of samples in the current time interval - * to zero. - * - * 3) Increment the number of samples in the current sample time - * interval. - * - * Implementing this behavior suffices for testing the abstract base class. - * - */ - bool takeSample - (__attribute__((unused)) double* u_in, - double time, - __attribute__((unused)) bool add_without_increase) - { - /** - If a new time interval is needed, add one and reset the number - of samples counter to zero. - */ - if (isNewTimeInterval()) - { - int num_time_intervals = - static_cast(d_time_interval_start_times.size()); - increaseTimeInterval(); - d_time_interval_start_times[num_time_intervals] = time; - d_num_samples = 0; - } - - /* Increment the number of samples in the current time interval */ - d_num_samples++; - - /** - This method should almost always succeed because it does not - do anything likely to fail, so it should return true. - */ - return true; - } - -}; - -TEST(SVDSerialTest, Test_getDim) -{ - FakeSVD svd(CAROM::Options(5, 2)); - EXPECT_EQ(svd.getDim(), 5); -} - -TEST(SVDSerialTest, Test_isNewTimeInterval) -{ - FakeSVD svd(CAROM::Options(5, 2)); - - /* 0 samples, so taking a sample will create a new time interval */ - EXPECT_TRUE(svd.isNewTimeInterval()); - - /* 1 sample; limit is 2, taking a sample won't create a new time interval */ - svd.takeSample(NULL, 0, true); - EXPECT_FALSE(svd.isNewTimeInterval()); - - /* 2 samples; limit is 2, taking a sample will create a new time interval */ - svd.takeSample(NULL, 0.5, true); - EXPECT_TRUE(svd.isNewTimeInterval()); - - /* 1 sample; limit is 2, taking a sample won't create a new time interval */ - svd.takeSample(NULL, 1, true); - EXPECT_FALSE(svd.isNewTimeInterval()); - - /* 2 samples; limit is 2, taking a sample will create a new time interval */ - svd.takeSample(NULL, 1.5, true); - EXPECT_TRUE(svd.isNewTimeInterval()); - - /* 1 sample; limit is 2, taking a sample won't create a new time interval */ - svd.takeSample(NULL, 2, true); - EXPECT_FALSE(svd.isNewTimeInterval()); -} - -TEST(SVDSerialTest, Test_getNumBasisTimeIntervals) -{ - FakeSVD svd(CAROM::Options(5, 2)); - - /* Number of time intervals starts at zero. */ - EXPECT_EQ(svd.getNumBasisTimeIntervals(), 0); - - /* Creates new time interval; number of intervals = 1 */ - svd.takeSample(NULL, 0, true); - EXPECT_EQ(svd.getNumBasisTimeIntervals(), 1); - svd.takeSample(NULL, 0.5, true); - EXPECT_EQ(svd.getNumBasisTimeIntervals(), 1); - - /* Creates new time interval; number of intervals = 2 */ - svd.takeSample(NULL, 1, true); - EXPECT_EQ(svd.getNumBasisTimeIntervals(), 2); - svd.takeSample(NULL, 1.5, true); - EXPECT_EQ(svd.getNumBasisTimeIntervals(), 2); - - /* Creates new time interval; number of intervals = 3 */ - svd.takeSample(NULL, 2, true); - EXPECT_EQ(svd.getNumBasisTimeIntervals(), 3); -} - -TEST(SVDSerialTest, Test_getBasisIntervalStartTime) -{ - FakeSVD svd(CAROM::Options(5, 2)); - - /* 1st time interval starts at time 0 */ - svd.takeSample(NULL, 0, true); - EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); - - svd.takeSample(NULL, 0.5, true); - EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); - - /* 2nd time interval starts at time 1 */ - svd.takeSample(NULL, 1, true); - EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); - EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(1), 1); - - svd.takeSample(NULL, 1.5, true); - EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); - EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(1), 1); - - /* 3rd time interval starts at time 2 */ - svd.takeSample(NULL, 2, true); - EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); - EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(1), 1); - EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(2), 2); -} - -int main(int argc, char* argv[]) -{ - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} -#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 From b7cb8e2b89e5d7530cd29d3ae1e63ac6cc864b4a Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sat, 17 Feb 2024 14:07:37 -0800 Subject: [PATCH 18/37] BasisGenerator: removed the concept of time interval. --- lib/linalg/BasisGenerator.cpp | 13 +++++------ lib/linalg/BasisReader.cpp | 13 +++++------ lib/linalg/BasisReader.h | 42 ++++++++++++++++++++++++++++++++--- lib/linalg/Options.h | 15 +++++++------ lib/linalg/svd/SVD.cpp | 4 ++-- 5 files changed, 59 insertions(+), 28 deletions(-) diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index 36ba53aeb..dcf59d5b0 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -34,7 +34,7 @@ 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); @@ -134,14 +134,12 @@ BasisGenerator::takeSample( 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; } @@ -160,16 +158,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(); diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index 0bf3dd074..4cecaae45 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -58,8 +58,7 @@ BasisReader::~BasisReader() } Matrix* -BasisReader::getSpatialBasis( - double time) +BasisReader::getSpatialBasis() { d_last_basis_idx = 0; int num_rows = getDim("basis"); @@ -140,8 +139,7 @@ BasisReader::getSpatialBasis( } Matrix* -BasisReader::getTemporalBasis( - double time) +BasisReader::getTemporalBasis() { d_last_basis_idx = 0; int num_rows = getDim("temporal_basis"); @@ -325,12 +323,11 @@ BasisReader::getNumSamples( } Matrix* -BasisReader::getSnapshotMatrix( - double time) +BasisReader::getSnapshotMatrix() { d_last_basis_idx = 0; - 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); diff --git a/lib/linalg/BasisReader.h b/lib/linalg/BasisReader.h index 7a01ddb4e..f1c209574 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -72,10 +72,21 @@ class BasisReader { return (d_last_basis_idx == -1); } + /** + * + * @brief Returns the spatial basis vectors as a Matrix. + * + * @return The spatial basis vectors. + */ + Matrix* + getSpatialBasis(); + /** * * @brief Returns the spatial basis vectors for the requested time as a * Matrix. + * NOTE: this function is obsolete and remains only for backward compatibility. + * Will be removed in future. * * @param[in] time Time for which we want the basis vectors. * NOTE: this argument is obsolete and remains only for backward compatibility. @@ -85,7 +96,8 @@ class BasisReader { */ Matrix* getSpatialBasis( - double time); + double time) + { return getSpatialBasis(); } /** * @@ -152,6 +164,18 @@ class BasisReader { * @brief Returns the temporal basis vectors for the requested time as * a Matrix. * + * @return The temporal basis vectors for the requested time. + */ + Matrix* + getTemporalBasis(); + + /** + * + * @brief Returns the temporal basis vectors for the requested time as + * a Matrix. + * NOTE: this function is obsolete and remains only for backward compatibility. + * Will be removed in future. + * * @param[in] time Time for which we want the basis vectors. * NOTE: this argument is obsolete and remains only for backward compatibility. * Will be removed in future. @@ -160,7 +184,8 @@ class BasisReader { */ Matrix* getTemporalBasis( - double time); + double time) + { return getTemporalBasis(); } /** * @@ -329,6 +354,16 @@ class BasisReader { * * @brief Returns the snapshot matrix for the requested time. * + * @return The snapshot matrix for the requested time. + */ + Matrix* + getSnapshotMatrix(); + + /** + * + * @brief Returns the snapshot matrix for the requested time. + * NOTE: this function is obsolete and remains only for backward compatibility. + * Will be removed in future. * * @param[in] time Time for which we want the basis vectors. * NOTE: this argument is obsolete and remains only for backward compatibility. @@ -338,7 +373,8 @@ class BasisReader { */ Matrix* getSnapshotMatrix( - double time); + double time) + { return getSnapshotMatrix(); } /** * diff --git a/lib/linalg/Options.h b/lib/linalg/Options.h index 816002d2b..87aa8cfac 100644 --- a/lib/linalg/Options.h +++ b/lib/linalg/Options.h @@ -30,11 +30,11 @@ class Options * @brief Constructor. * * @pre dim_ > 0 - * @pre samples_per_time_interval_ > 0 + * @pre max_num_samples_ > 0 * @pre max_time_intervals == -1 || max_time_intervals > 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. @@ -43,13 +43,13 @@ class Options * */ Options(int dim_, - int samples_per_time_interval_, + int max_num_samples_, int max_time_intervals_ = 1, 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_basis_dimension(max_num_samples_), + max_num_samples(max_num_samples_), max_time_intervals(max_time_intervals_), update_right_SV(update_right_SV_), write_snapshots(write_snapshots_) @@ -226,12 +226,13 @@ class Options int dim = -1; /** - * @brief The number of samples per time interval. + * @brief The maximum number of samples. */ - int samples_per_time_interval = -1; + int max_num_samples = -1; /** * @brief The maximum number of time intervals. + * NOTE: this variable is obsolte and will be removed in future. */ int max_time_intervals = -1; diff --git a/lib/linalg/svd/SVD.cpp b/lib/linalg/svd/SVD.cpp index fdf3ef214..fe6547625 100644 --- a/lib/linalg/svd/SVD.cpp +++ b/lib/linalg/svd/SVD.cpp @@ -19,7 +19,7 @@ SVD::SVD( Options options) : d_dim(options.dim), d_num_samples(0), - d_max_num_samples(options.samples_per_time_interval), + d_max_num_samples(options.max_num_samples), d_basis(NULL), d_basis_right(NULL), d_U(NULL), @@ -29,7 +29,7 @@ SVD::SVD( d_debug_algorithm(options.debug_algorithm) { CAROM_VERIFY(options.dim > 0); - CAROM_VERIFY(options.samples_per_time_interval > 0); + CAROM_VERIFY(options.max_num_samples > 0); } SVD::~SVD() From 3d43de04644614cad30a09cc833badf2ea925851 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sat, 17 Feb 2024 14:24:42 -0800 Subject: [PATCH 19/37] add test_SVD.cpp for resolving conflict. --- unit_tests/test_SVD.cpp | 241 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 unit_tests/test_SVD.cpp diff --git a/unit_tests/test_SVD.cpp b/unit_tests/test_SVD.cpp new file mode 100644 index 000000000..68a7c70fc --- /dev/null +++ b/unit_tests/test_SVD.cpp @@ -0,0 +1,241 @@ +/****************************************************************************** + * + * 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) + * + *****************************************************************************/ + +// Description: This source file is a test runner that uses the Google Test +// Framework to run unit tests on the CAROM::SVD class. + +#include + +#ifdef CAROM_HAS_GTEST +#include +#include +#include "linalg/Options.h" +#include "linalg/svd/SVD.h" + +/** + * Simple smoke test to make sure Google Test is properly linked + */ +TEST(GoogleTestFramework, GoogleTestFrameworkFound) { + SUCCEED(); +} + +/** + * Fake SVD to test parts of the SVD abstract base class. (For those + * unfamiliar, "fake" or "mock" is a term of art in unit testing + * referring to an implementation that simulates the behavior of + * real objects.) + */ + +class FakeSVD : public CAROM::SVD +{ +public: + + FakeSVD(CAROM::Options options) + : SVD(options) + { + } + + ~FakeSVD() + { + } + + /** + * Stub implementations of methods not really testable from the + * abstract base class, because there is no meaningful data we could + * return without implementing an actual singular value decomposition. + * + */ + virtual const CAROM::Matrix* getSpatialBasis() { + return NULL; + } + virtual const CAROM::Matrix* getTemporalBasis() { + return NULL; + } + virtual const CAROM::Vector* getSingularValues() { + return NULL; + } + virtual const CAROM::Matrix* getSnapshotMatrix() { + return NULL; + } + + /** + * The only testable methods from the SVD abstract base class are + * those with concrete implementations, namely: + * + * int getDim() const; + * + * int getNumBasisTimeIntervals() const; + * + * double getBasisIntervalStartTime(int) const; + * + * bool isNewTimeInterval() const; + */ + + /** + * This method needs to do a few things: + * + * 1) If there are no sample time intervals stored, create one and + * record its start time. Set the number of samples in the current + * time interval to zero. + * + * 2) If adding another sample to the current sample time interval + * would exceed the number of samples per time interval set in + * the constructor, create a new time interval and record its + * start time. Set the number of samples in the current time interval + * to zero. + * + * 3) Increment the number of samples in the current sample time + * interval. + * + * Implementing this behavior suffices for testing the abstract base class. + * + */ + bool takeSample + (__attribute__((unused)) double* u_in, + double time, + __attribute__((unused)) bool add_without_increase) + { + /** + If a new time interval is needed, add one and reset the number + of samples counter to zero. + */ + if (isNewTimeInterval()) + { + int num_time_intervals = + static_cast(d_time_interval_start_times.size()); + increaseTimeInterval(); + d_time_interval_start_times[num_time_intervals] = time; + d_num_samples = 0; + } + + /* Increment the number of samples in the current time interval */ + d_num_samples++; + + /** + This method should almost always succeed because it does not + do anything likely to fail, so it should return true. + */ + return true; + } + +}; + +TEST(SVDSerialTest, Test_getDim) +{ + FakeSVD svd(CAROM::Options(5, 2)); + EXPECT_EQ(svd.getDim(), 5); +} + +TEST(SVDSerialTest, Test_isNewTimeInterval) +{ + FakeSVD svd(CAROM::Options(5, 2)); + + /* 0 samples, so taking a sample will create a new time interval */ + EXPECT_TRUE(svd.isNewTimeInterval()); + + /* 1 sample; limit is 2, taking a sample won't create a new time interval */ + svd.takeSample(NULL, 0, true); + EXPECT_FALSE(svd.isNewTimeInterval()); + + /* 2 samples; limit is 2, taking a sample will create a new time interval */ + svd.takeSample(NULL, 0.5, true); + EXPECT_TRUE(svd.isNewTimeInterval()); + + /* 1 sample; limit is 2, taking a sample won't create a new time interval */ + svd.takeSample(NULL, 1, true); + EXPECT_FALSE(svd.isNewTimeInterval()); + + /* 2 samples; limit is 2, taking a sample will create a new time interval */ + svd.takeSample(NULL, 1.5, true); + EXPECT_TRUE(svd.isNewTimeInterval()); + + /* 1 sample; limit is 2, taking a sample won't create a new time interval */ + svd.takeSample(NULL, 2, true); + EXPECT_FALSE(svd.isNewTimeInterval()); +} + +TEST(SVDSerialTest, Test_getNumBasisTimeIntervals) +{ + FakeSVD svd(CAROM::Options(5, 2)); + + /* Number of time intervals starts at zero. */ + EXPECT_EQ(svd.getNumBasisTimeIntervals(), 0); + + /* Creates new time interval; number of intervals = 1 */ + svd.takeSample(NULL, 0, true); + EXPECT_EQ(svd.getNumBasisTimeIntervals(), 1); + svd.takeSample(NULL, 0.5, true); + EXPECT_EQ(svd.getNumBasisTimeIntervals(), 1); + + /* Creates new time interval; number of intervals = 2 */ + svd.takeSample(NULL, 1, true); + EXPECT_EQ(svd.getNumBasisTimeIntervals(), 2); + svd.takeSample(NULL, 1.5, true); + EXPECT_EQ(svd.getNumBasisTimeIntervals(), 2); + + /* Creates new time interval; number of intervals = 3 */ + svd.takeSample(NULL, 2, true); + EXPECT_EQ(svd.getNumBasisTimeIntervals(), 3); +} + +TEST(SVDSerialTest, Test_getBasisIntervalStartTime) +{ + FakeSVD svd(CAROM::Options(5, 2)); + + /* 1st time interval starts at time 0 */ + svd.takeSample(NULL, 0, true); + EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); + + svd.takeSample(NULL, 0.5, true); + EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); + + /* 2nd time interval starts at time 1 */ + svd.takeSample(NULL, 1, true); + EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); + EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(1), 1); + + svd.takeSample(NULL, 1.5, true); + EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); + EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(1), 1); + + /* 3rd time interval starts at time 2 */ + svd.takeSample(NULL, 2, true); + EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(0), 0); + EXPECT_DOUBLE_EQ(svd.getBasisIntervalStartTime(1), 1); + 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); + return RUN_ALL_TESTS(); +} +#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 From 9fdc4535a7e70ab742ddf34fe55af03b9617cd92 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sat, 17 Feb 2024 14:32:55 -0800 Subject: [PATCH 20/37] stylization. --- lib/linalg/BasisReader.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/linalg/BasisReader.h b/lib/linalg/BasisReader.h index f1c209574..c56f75f70 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -97,7 +97,9 @@ class BasisReader { Matrix* getSpatialBasis( double time) - { return getSpatialBasis(); } + { + return getSpatialBasis(); + } /** * @@ -185,7 +187,9 @@ class BasisReader { Matrix* getTemporalBasis( double time) - { return getTemporalBasis(); } + { + return getTemporalBasis(); + } /** * @@ -374,7 +378,9 @@ class BasisReader { Matrix* getSnapshotMatrix( double time) - { return getSnapshotMatrix(); } + { + return getSnapshotMatrix(); + } /** * From 3acb60630951a4256e661767635461f10a893051 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Wed, 21 Feb 2024 13:03:39 -0800 Subject: [PATCH 21/37] changed function signature of BasisGenerator::takeSample. --- examples/misc/combine_samples.cpp | 2 +- examples/prom/dg_advection_global_rom.cpp | 4 +-- .../dg_advection_local_rom_matrix_interp.cpp | 4 +-- .../prom/linear_elasticity_global_rom.cpp | 2 +- examples/prom/maxwell_global_rom.cpp | 2 +- examples/prom/mixed_nonlinear_diffusion.cpp | 16 +++++----- .../prom/nonlinear_elasticity_global_rom.cpp | 16 ++++++---- examples/prom/poisson_global_rom.cpp | 2 +- examples/prom/poisson_local_rom_greedy.cpp | 2 +- lib/linalg/BasisGenerator.cpp | 14 ++++++-- lib/linalg/BasisGenerator.h | 4 --- unit_tests/random_test.cpp | 4 +-- unit_tests/smoke_static.cpp | 8 ++--- unit_tests/smoke_test.cpp | 4 +-- unit_tests/test_IncrementalSVDBrand.cpp | 6 ++-- unit_tests/test_RandomizedSVD.cpp | 32 +++++++++---------- unit_tests/test_StaticSVD.cpp | 18 +++++------ unit_tests/test_include.cpp | 4 +-- unit_tests/uneven_dist.cpp | 4 +-- unit_tests/weak_scaling.cpp | 2 +- 20 files changed, 79 insertions(+), 71 deletions(-) diff --git a/examples/misc/combine_samples.cpp b/examples/misc/combine_samples.cpp index 77987ab37..d4f10d4ce 100644 --- a/examples/misc/combine_samples.cpp +++ b/examples/misc/combine_samples.cpp @@ -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/dg_advection_global_rom.cpp b/examples/prom/dg_advection_global_rom.cpp index 4f28c57d9..b4fa575b5 100644 --- a/examples/prom/dg_advection_global_rom.cpp +++ b/examples/prom/dg_advection_global_rom.cpp @@ -697,7 +697,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()); } // 11. The merge phase @@ -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..7b7e73d43 100644 --- a/examples/prom/dg_advection_local_rom_matrix_interp.cpp +++ b/examples/prom/dg_advection_local_rom_matrix_interp.cpp @@ -711,7 +711,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 (online) @@ -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/linear_elasticity_global_rom.cpp b/examples/prom/linear_elasticity_global_rom.cpp index f84974ebf..a5fde9e94 100644 --- a/examples/prom/linear_elasticity_global_rom.cpp +++ b/examples/prom/linear_elasticity_global_rom.cpp @@ -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; diff --git a/examples/prom/maxwell_global_rom.cpp b/examples/prom/maxwell_global_rom.cpp index f9de7d849..dd6beabab 100644 --- a/examples/prom/maxwell_global_rom.cpp +++ b/examples/prom/maxwell_global_rom.cpp @@ -348,7 +348,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; diff --git a/examples/prom/mixed_nonlinear_diffusion.cpp b/examples/prom/mixed_nonlinear_diffusion.cpp index 19ab15dd9..c543b0b43 100644 --- a/examples/prom/mixed_nonlinear_diffusion.cpp +++ b/examples/prom/mixed_nonlinear_diffusion.cpp @@ -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..5d4eb6085 100644 --- a/examples/prom/nonlinear_elasticity_global_rom.cpp +++ b/examples/prom/nonlinear_elasticity_global_rom.cpp @@ -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..0f45d1c39 100644 --- a/examples/prom/poisson_global_rom.cpp +++ b/examples/prom/poisson_global_rom.cpp @@ -341,7 +341,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; diff --git a/examples/prom/poisson_local_rom_greedy.cpp b/examples/prom/poisson_local_rom_greedy.cpp index 72a7eb271..1145563a8 100644 --- a/examples/prom/poisson_local_rom_greedy.cpp +++ b/examples/prom/poisson_local_rom_greedy.cpp @@ -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; diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index dcf59d5b0..942efa9b2 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -129,8 +129,6 @@ BasisGenerator::isNextSample( bool BasisGenerator::takeSample( double* u_in, - double time, - double dt, bool add_without_increase) { CAROM_VERIFY(u_in != 0); @@ -143,6 +141,18 @@ BasisGenerator::takeSample( return false; } + /* + Note for previous implementation: + Previously with multiple time interval, + there was an input argument (double dt), + which is only used to reset d_dt for new time interval. + Assuming only single interval is used in practice, + resetDt(dt) was never used in takeSample, + and options.initial_dt is used for incremental svd. + */ + // if (d_svd->isNewSample()) + // resetDt(dt); + return d_svd->takeSample(u_in, add_without_increase); } diff --git a/lib/linalg/BasisGenerator.h b/lib/linalg/BasisGenerator.h index fee00f4ee..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); /** diff --git a/unit_tests/random_test.cpp b/unit_tests/random_test.cpp index b6c759c87..104f01e5e 100644 --- a/unit_tests/random_test.cpp +++ b/unit_tests/random_test.cpp @@ -190,7 +190,7 @@ main( bool status = true; for (int i = 0; i < num_samples; ++i) { if (inc_basis_generator.isNextSample(0.01*i)) { - status = inc_basis_generator.takeSample(M[i], 0.01*i, 0.01); + status = inc_basis_generator.takeSample(M[i]); if (!status) { break; } @@ -198,7 +198,7 @@ main( } if (i < num_lin_indep_samples && static_basis_generator.isNextSample(0.01*i)) { - status = static_basis_generator.takeSample(M[i], 0.01*i, 0.01); + status = static_basis_generator.takeSample(M[i]); if (!status) { break; } diff --git a/unit_tests/smoke_static.cpp b/unit_tests/smoke_static.cpp index 316bd72f8..856dc71c7 100644 --- a/unit_tests/smoke_static.cpp +++ b/unit_tests/smoke_static.cpp @@ -74,7 +74,7 @@ main( "static_smoke1")); // Take the first sample. - static_basis_generator->takeSample(&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_IncrementalSVDBrand.cpp b/unit_tests/test_IncrementalSVDBrand.cpp index a18d842d7..a55345523 100644 --- a/unit_tests/test_IncrementalSVDBrand.cpp +++ b/unit_tests/test_IncrementalSVDBrand.cpp @@ -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..e1b1b6c81 100644 --- a/unit_tests/test_RandomizedSVD.cpp +++ b/unit_tests/test_RandomizedSVD.cpp @@ -74,9 +74,9 @@ TEST(RandomizedSVDTest, Test_RandomizedSVD) 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(); @@ -154,11 +154,11 @@ TEST(RandomizedSVDTest, Test_RandomizedSVDTransposed) 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(); @@ -232,9 +232,9 @@ TEST(RandomizedSVDTest, Test_RandomizedSVDSmallerSubspace) 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(); @@ -311,11 +311,11 @@ TEST(RandomizedSVDTest, Test_RandomizedSVDTransposedSmallerSubspace) 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_StaticSVD.cpp b/unit_tests/test_StaticSVD.cpp index ddda7bc4c..dde4c9694 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(); @@ -290,9 +290,9 @@ TEST(StaticSVDTest, Test_StaticSVDClass) 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(); @@ -370,11 +370,11 @@ TEST(StaticSVDTest, Test_StaticSVDTranspose) 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_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; } From 79702a25ffbae0696bc91766e7bfa072b2605ba7 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Wed, 21 Feb 2024 13:35:19 -0800 Subject: [PATCH 22/37] rebased to resolve conflict. --- examples/prom/de_parametric_maxwell_greedy.cpp | 2 +- examples/prom/grad_div_global_rom.cpp | 2 +- examples/prom/maxwell_local_rom_greedy.cpp | 2 +- unit_tests/test_SVD.cpp | 14 -------------- 4 files changed, 3 insertions(+), 17 deletions(-) diff --git a/examples/prom/de_parametric_maxwell_greedy.cpp b/examples/prom/de_parametric_maxwell_greedy.cpp index a0a1a2b0d..7f8720229 100644 --- a/examples/prom/de_parametric_maxwell_greedy.cpp +++ b/examples/prom/de_parametric_maxwell_greedy.cpp @@ -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; diff --git a/examples/prom/grad_div_global_rom.cpp b/examples/prom/grad_div_global_rom.cpp index 22a944854..331423204 100644 --- a/examples/prom/grad_div_global_rom.cpp +++ b/examples/prom/grad_div_global_rom.cpp @@ -358,7 +358,7 @@ int main(int argc, char *argv[]) // 18. take and write snapshot for ROM if (offline) { - generator->takeSample(X.GetData(), 0.0, 0.01); + generator->takeSample(X.GetData()); generator->writeSnapshot(); delete generator; delete options; diff --git a/examples/prom/maxwell_local_rom_greedy.cpp b/examples/prom/maxwell_local_rom_greedy.cpp index 8c75a0f7b..3bf4c3283 100644 --- a/examples/prom/maxwell_local_rom_greedy.cpp +++ b/examples/prom/maxwell_local_rom_greedy.cpp @@ -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; 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); From 588930929019fcd4235520dd816bcd4f7956c4dd Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Wed, 21 Feb 2024 14:03:04 -0800 Subject: [PATCH 23/37] changed function signature for BasisReader::getSpatialBasis. --- .../prom/de_parametric_maxwell_greedy.cpp | 2 +- examples/prom/dg_advection_global_rom.cpp | 4 +- .../dg_advection_local_rom_matrix_interp.cpp | 6 +-- examples/prom/grad_div_global_rom.cpp | 2 +- .../prom/linear_elasticity_global_rom.cpp | 2 +- examples/prom/maxwell_global_rom.cpp | 2 +- examples/prom/maxwell_local_rom_greedy.cpp | 2 +- examples/prom/mixed_nonlinear_diffusion.cpp | 8 ++-- .../prom/nonlinear_elasticity_global_rom.cpp | 6 +-- examples/prom/poisson_global_rom.cpp | 2 +- examples/prom/poisson_local_rom_greedy.cpp | 2 +- lib/linalg/BasisReader.cpp | 7 +--- lib/linalg/BasisReader.h | 39 +------------------ regression_tests/basisComparator.cpp | 8 ++-- 14 files changed, 26 insertions(+), 66 deletions(-) diff --git a/examples/prom/de_parametric_maxwell_greedy.cpp b/examples/prom/de_parametric_maxwell_greedy.cpp index 7f8720229..78e226553 100644 --- a/examples/prom/de_parametric_maxwell_greedy.cpp +++ b/examples/prom/de_parametric_maxwell_greedy.cpp @@ -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(); diff --git a/examples/prom/dg_advection_global_rom.cpp b/examples/prom/dg_advection_global_rom.cpp index b4fa575b5..8c3b1bac1 100644 --- a/examples/prom/dg_advection_global_rom.cpp +++ b/examples/prom/dg_advection_global_rom.cpp @@ -731,11 +731,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(); diff --git a/examples/prom/dg_advection_local_rom_matrix_interp.cpp b/examples/prom/dg_advection_local_rom_matrix_interp.cpp index 7b7e73d43..cf052998c 100644 --- a/examples/prom/dg_advection_local_rom_matrix_interp.cpp +++ b/examples/prom/dg_advection_local_rom_matrix_interp.cpp @@ -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); diff --git a/examples/prom/grad_div_global_rom.cpp b/examples/prom/grad_div_global_rom.cpp index 331423204..5ba867afe 100644 --- a/examples/prom/grad_div_global_rom.cpp +++ b/examples/prom/grad_div_global_rom.cpp @@ -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 a5fde9e94..1e44ef7b3 100644 --- a/examples/prom/linear_elasticity_global_rom.cpp +++ b/examples/prom/linear_elasticity_global_rom.cpp @@ -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 dd6beabab..68d9abaea 100644 --- a/examples/prom/maxwell_global_rom.cpp +++ b/examples/prom/maxwell_global_rom.cpp @@ -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 3bf4c3283..41d784fcb 100644 --- a/examples/prom/maxwell_local_rom_greedy.cpp +++ b/examples/prom/maxwell_local_rom_greedy.cpp @@ -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, diff --git a/examples/prom/mixed_nonlinear_diffusion.cpp b/examples/prom/mixed_nonlinear_diffusion.cpp index c543b0b43..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) { diff --git a/examples/prom/nonlinear_elasticity_global_rom.cpp b/examples/prom/nonlinear_elasticity_global_rom.cpp index 5d4eb6085..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) diff --git a/examples/prom/poisson_global_rom.cpp b/examples/prom/poisson_global_rom.cpp index 0f45d1c39..6ec67cdec 100644 --- a/examples/prom/poisson_global_rom.cpp +++ b/examples/prom/poisson_global_rom.cpp @@ -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 1145563a8..89fd50297 100644 --- a/examples/prom/poisson_local_rom_greedy.cpp +++ b/examples/prom/poisson_local_rom_greedy.cpp @@ -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, diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index 4cecaae45..874099ccd 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -76,15 +76,13 @@ 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) { @@ -112,7 +110,6 @@ BasisReader::getSpatialBasis( Matrix* BasisReader::getSpatialBasis( - double time, double ef) { Vector* sv = getSingularValues(); @@ -135,7 +132,7 @@ BasisReader::getSpatialBasis( } delete sv; - return getSpatialBasis(time, num_used_singular_values); + return getSpatialBasis(num_used_singular_values); } Matrix* diff --git a/lib/linalg/BasisReader.h b/lib/linalg/BasisReader.h index c56f75f70..75ca0edc3 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -58,16 +58,11 @@ class BasisReader { * @brief Returns true if the basis vectors at requested time are * different from the last requested basis vectors. * - * @param[in] time Time at which we are interested in the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. - * * @return True if the basis vectors at the requested time are different * from the last requested basis vectors. */ bool - isNewBasis( - double time) + isNewBasis() { return (d_last_basis_idx == -1); } @@ -81,26 +76,6 @@ class BasisReader { Matrix* getSpatialBasis(); - /** - * - * @brief Returns the spatial basis vectors for the requested time as a - * Matrix. - * NOTE: this function is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @return The spatial basis vectors for the requested time. - */ - Matrix* - getSpatialBasis( - double time) - { - return getSpatialBasis(); - } - /** * * @brief Returns the first n spatial basis vectors for the requested time @@ -108,16 +83,12 @@ class BasisReader { * * @pre 0 < n <= numColumns() * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. * @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); /** @@ -128,9 +99,6 @@ class BasisReader { * @pre 0 < start_col <= numColumns() * @pre start_col <= end_col <= numColumns() * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. * @param[in] start_col The starting column desired. * @param[in] end_col The starting column desired. * @@ -138,7 +106,6 @@ class BasisReader { */ Matrix* getSpatialBasis( - double time, int start_col, int end_col); @@ -149,16 +116,12 @@ class BasisReader { * * @pre 0 <= ef <= 1.0 * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. * @param[in] ef The desired energy fraction. * * @return The spatial basis vectors for the requested time. */ Matrix* getSpatialBasis( - double time, double ef); /** 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(); From 5e3a418caa0e2b4c7e99355c942d54bead1e8915 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Wed, 21 Feb 2024 14:06:41 -0800 Subject: [PATCH 24/37] changed function signature for BasisReader::getTemporalBasis. --- lib/linalg/BasisReader.cpp | 7 ++----- lib/linalg/BasisReader.h | 36 ------------------------------------ 2 files changed, 2 insertions(+), 41 deletions(-) diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index 874099ccd..eeda217a2 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -155,15 +155,13 @@ 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) { @@ -191,7 +189,6 @@ BasisReader::getTemporalBasis( Matrix* BasisReader::getTemporalBasis( - double time, double ef) { Vector* sv = getSingularValues(); @@ -214,7 +211,7 @@ BasisReader::getTemporalBasis( } delete sv; - return getTemporalBasis(time, num_used_singular_values); + return getTemporalBasis(num_used_singular_values); } Vector* diff --git a/lib/linalg/BasisReader.h b/lib/linalg/BasisReader.h index 75ca0edc3..aedfe632b 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -134,26 +134,6 @@ class BasisReader { Matrix* getTemporalBasis(); - /** - * - * @brief Returns the temporal basis vectors for the requested time as - * a Matrix. - * NOTE: this function is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @return The temporal basis vectors for the requested time. - */ - Matrix* - getTemporalBasis( - double time) - { - return getTemporalBasis(); - } - /** * * @brief Returns the first n temporal basis vectors for the requested time @@ -161,16 +141,12 @@ class BasisReader { * * @pre 0 < n <= numColumns() * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. * @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); /** @@ -181,9 +157,6 @@ class BasisReader { * @pre 0 < start_col <= numColumns() * @pre start_col <= end_col <= numColumns() * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. * @param[in] start_col The starting column desired. * @param[in] end_col The starting column desired. * @@ -191,7 +164,6 @@ class BasisReader { */ Matrix* getTemporalBasis( - double time, int start_col, int end_col); @@ -202,26 +174,18 @@ class BasisReader { * * @pre 0 <= ef <= 1.0 * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. * @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. * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. - * * @return The temporal basis vectors for the requested time. */ Vector* From 99beadbc5e8679074988a7d926c3b1095099e5ef Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Wed, 21 Feb 2024 14:08:12 -0800 Subject: [PATCH 25/37] changed function signature for BasisReader::getSingularValues. --- lib/linalg/BasisReader.cpp | 1 - lib/linalg/BasisReader.h | 23 ----------------------- 2 files changed, 24 deletions(-) diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index eeda217a2..ad5013779 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -237,7 +237,6 @@ BasisReader::getSingularValues() Vector* BasisReader::getSingularValues( - double time, double ef) { Vector* sv = getSingularValues(); diff --git a/lib/linalg/BasisReader.h b/lib/linalg/BasisReader.h index aedfe632b..481fbb4b2 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -191,25 +191,6 @@ class BasisReader { Vector* getSingularValues(); - /** - * - * @brief Returns the singular values for the requested time. - * NOTE: this function is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @return The temporal basis vectors for the requested time. - */ - Vector* - getSingularValues( - double time) - { - return getSingularValues(); - } - /** * * @brief Returns the largest singular values for the requested time @@ -217,16 +198,12 @@ class BasisReader { * * @pre 0 <= ef <= 1.0 * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. * @param[in] ef The desired energy fraction. * * @return The temporal basis vectors for the requested time. */ Vector* getSingularValues( - double time, double ef); /** From 30ff8558d16aeb1acb2849031835c78e00c218dc Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Wed, 21 Feb 2024 14:11:06 -0800 Subject: [PATCH 26/37] changed function signature for BasisReader::getSnapshotMatrix. --- examples/misc/combine_samples.cpp | 4 +-- lib/linalg/BasisReader.cpp | 4 +-- lib/linalg/BasisReader.h | 59 ------------------------------- 3 files changed, 3 insertions(+), 64 deletions(-) diff --git a/examples/misc/combine_samples.cpp b/examples/misc/combine_samples.cpp index d4f10d4ce..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 == diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index ad5013779..cb00e2309 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -335,15 +335,13 @@ 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) { diff --git a/lib/linalg/BasisReader.h b/lib/linalg/BasisReader.h index 481fbb4b2..5a7090802 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -216,22 +216,6 @@ class BasisReader { getDim( const std::string kind); - /** - * - * @brief Returns the dimension of the system on this processor. - * NOTE: this function is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @return The dimension of the system on this processor. - */ - int - getDim( - const std::string kind, - double time) - { - return getDim(kind); - } - /** * * @brief Returns the number of samples (columns) in file. @@ -242,22 +226,6 @@ class BasisReader { getNumSamples( const std::string kind); - /** - * - * @brief Returns the number of samples (columns) in file. - * NOTE: this function is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @return The number of samples in file. - */ - int - getNumSamples( - const std::string kind, - double time) - { - return getNumSamples(kind); - } - /** * * @brief Returns the snapshot matrix for the requested time. @@ -267,41 +235,18 @@ class BasisReader { Matrix* getSnapshotMatrix(); - /** - * - * @brief Returns the snapshot matrix for the requested time. - * NOTE: this function is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. - * - * @return The snapshot matrix for the requested time. - */ - Matrix* - getSnapshotMatrix( - double time) - { - return getSnapshotMatrix(); - } - /** * * @brief Returns the first n columns of the snapshot matrix for the requested time. * * @pre 0 < n <= numColumns() * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. * @param[in] n The number of basis vectors desired. * * @return The snapshot matrix for the requested time. */ Matrix* getSnapshotMatrix( - double time, int n); /** @@ -311,9 +256,6 @@ class BasisReader { * @pre 0 < start_col <= numColumns() * @pre start_col <= end_col <= numColumns() * - * @param[in] time Time for which we want the basis vectors. - * NOTE: this argument is obsolete and remains only for backward compatibility. - * Will be removed in future. * @param[in] start_col The starting column desired. * @param[in] end_col The starting column desired. * @@ -321,7 +263,6 @@ class BasisReader { */ Matrix* getSnapshotMatrix( - double time, int start_col, int end_col); From 45005c606d5474dcad9e7ca625343c10aba827ba Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Fri, 23 Feb 2024 16:28:10 -0800 Subject: [PATCH 27/37] removed Options::max_time_intervals --- .../prom/de_parametric_maxwell_greedy.cpp | 4 ++-- examples/prom/dg_advection_global_rom.cpp | 4 ++-- .../dg_advection_local_rom_matrix_interp.cpp | 2 +- examples/prom/grad_div_global_rom.cpp | 4 ++-- .../prom/linear_elasticity_global_rom.cpp | 4 ++-- examples/prom/maxwell_global_rom.cpp | 4 ++-- examples/prom/maxwell_local_rom_greedy.cpp | 4 ++-- examples/prom/poisson_global_rom.cpp | 4 ++-- examples/prom/poisson_local_rom_greedy.cpp | 4 ++-- lib/linalg/BasisGenerator.cpp | 2 -- lib/linalg/Options.h | 19 +------------------ unit_tests/random_test.cpp | 2 +- unit_tests/test_IncrementalSVDBrand.cpp | 2 +- unit_tests/test_RandomizedSVD.cpp | 8 ++++---- unit_tests/test_StaticSVD.cpp | 4 ++-- 15 files changed, 26 insertions(+), 45 deletions(-) diff --git a/examples/prom/de_parametric_maxwell_greedy.cpp b/examples/prom/de_parametric_maxwell_greedy.cpp index 78e226553..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); @@ -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 8c3b1bac1..e438766e8 100644 --- a/examples/prom/dg_advection_global_rom.cpp +++ b/examples/prom/dg_advection_global_rom.cpp @@ -692,7 +692,7 @@ 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()); @@ -704,7 +704,7 @@ int main(int argc, char *argv[]) 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; paramIDSize(), 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()); diff --git a/examples/prom/grad_div_global_rom.cpp b/examples/prom/grad_div_global_rom.cpp index 5ba867afe..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; paramIDGetTrueVSize(), 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) diff --git a/examples/prom/maxwell_global_rom.cpp b/examples/prom/maxwell_global_rom.cpp index 68d9abaea..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; paramIDGetTrueVSize(), 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); @@ -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/poisson_global_rom.cpp b/examples/prom/poisson_global_rom.cpp index 6ec67cdec..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; paramID 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) { diff --git a/lib/linalg/Options.h b/lib/linalg/Options.h index 87aa8cfac..7bb1d7c00 100644 --- a/lib/linalg/Options.h +++ b/lib/linalg/Options.h @@ -31,12 +31,10 @@ class Options * * @pre dim_ > 0 * @pre max_num_samples_ > 0 - * @pre max_time_intervals == -1 || max_time_intervals > 0 * * @param[in] dim_ The dimension of the system on this processor. * @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. @@ -44,23 +42,14 @@ class Options */ Options(int dim_, int max_num_samples_, - int max_time_intervals_ = 1, bool update_right_SV_ = false, bool write_snapshots_ = false ): dim(dim_), max_basis_dimension(max_num_samples_), max_num_samples(max_num_samples_), - max_time_intervals(max_time_intervals_), update_right_SV(update_right_SV_), write_snapshots(write_snapshots_) - { - if (max_time_intervals > 1) - { - printf("time interval is obsolete and will be removed in the future." - " Set max_time_intervals=%d to 1!\n", max_time_intervals); - } - CAROM_VERIFY(max_time_intervals == 1); - }; + {}; /** * @brief Sets the maximum basis dimension of the SVD algorithm. @@ -230,12 +219,6 @@ class Options */ int max_num_samples = -1; - /** - * @brief The maximum number of time intervals. - * NOTE: this variable is obsolte and will be removed in future. - */ - int max_time_intervals = -1; - /** * @brief Whether to update the right singular values. */ diff --git a/unit_tests/random_test.cpp b/unit_tests/random_test.cpp index 104f01e5e..3d03bdcb0 100644 --- a/unit_tests/random_test.cpp +++ b/unit_tests/random_test.cpp @@ -132,7 +132,7 @@ main( // static sampler. CAROM::BasisGenerator static_basis_generator( - CAROM::Options(dim, num_samples, -1, true), false + CAROM::Options(dim, num_samples, true), false ); // Initialize random number generator. diff --git a/unit_tests/test_IncrementalSVDBrand.cpp b/unit_tests/test_IncrementalSVDBrand.cpp index a55345523..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, diff --git a/unit_tests/test_RandomizedSVD.cpp b/unit_tests/test_RandomizedSVD.cpp index e1b1b6c81..1679392f0 100644 --- a/unit_tests/test_RandomizedSVD.cpp +++ b/unit_tests/test_RandomizedSVD.cpp @@ -69,7 +69,7 @@ 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); @@ -149,7 +149,7 @@ 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); @@ -227,7 +227,7 @@ 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); @@ -306,7 +306,7 @@ 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); diff --git a/unit_tests/test_StaticSVD.cpp b/unit_tests/test_StaticSVD.cpp index dde4c9694..c1420344b 100644 --- a/unit_tests/test_StaticSVD.cpp +++ b/unit_tests/test_StaticSVD.cpp @@ -285,7 +285,7 @@ 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); @@ -365,7 +365,7 @@ 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); From 9d77b7a143ca6076727c6a9c307ff32fecf685a6 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Fri, 23 Feb 2024 16:31:51 -0800 Subject: [PATCH 28/37] SVD::isNewSample -> isFirstSample --- lib/linalg/BasisGenerator.cpp | 2 +- lib/linalg/svd/IncrementalSVD.cpp | 4 ++-- lib/linalg/svd/IncrementalSVDBrand.cpp | 2 +- lib/linalg/svd/IncrementalSVDFastUpdate.cpp | 2 +- lib/linalg/svd/IncrementalSVDStandard.cpp | 2 +- lib/linalg/svd/SVD.h | 26 ++++++++++----------- lib/linalg/svd/StaticSVD.cpp | 2 +- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index 3a7114236..7d5aeacf1 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -148,7 +148,7 @@ BasisGenerator::takeSample( resetDt(dt) was never used in takeSample, and options.initial_dt is used for incremental svd. */ - // if (d_svd->isNewSample()) + // if (d_svd->isFirstSample()) // resetDt(dt); return d_svd->takeSample(u_in, add_without_increase); diff --git a/lib/linalg/svd/IncrementalSVD.cpp b/lib/linalg/svd/IncrementalSVD.cpp index c65f2a8b0..5c5e912c4 100644 --- a/lib/linalg/svd/IncrementalSVD.cpp +++ b/lib/linalg/svd/IncrementalSVD.cpp @@ -138,7 +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 && (!isNewSample())) { + if (d_save_state && (!isFirstSample())) { // Save d_U. int num_rows = d_U->numRows(); d_state_database->putInteger("U_num_rows", num_rows); @@ -186,7 +186,7 @@ IncrementalSVD::takeSample( // If this is the first SVD then build it. Otherwise add this sample to the // system. bool result = true; - if (isNewSample()) { + if (isFirstSample()) { buildInitialSVD(u_in); } else { diff --git a/lib/linalg/svd/IncrementalSVDBrand.cpp b/lib/linalg/svd/IncrementalSVDBrand.cpp index e2a994b35..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 && (!isNewSample())) { + if (d_save_state && (!isFirstSample())) { // Create state database file. d_state_database = new HDFDatabase(); d_state_database->create(d_state_file_name); diff --git a/lib/linalg/svd/IncrementalSVDFastUpdate.cpp b/lib/linalg/svd/IncrementalSVDFastUpdate.cpp index 9ce73649d..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 && (!isNewSample())) { + if (d_save_state && (!isFirstSample())) { // Create state database file. d_state_database = new HDFDatabase(); d_state_database->create(d_state_file_name); diff --git a/lib/linalg/svd/IncrementalSVDStandard.cpp b/lib/linalg/svd/IncrementalSVDStandard.cpp index de9cb3fe6..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 && (!isNewSample())) { + if (d_save_state && (!isFirstSample())) { // Create state database file. d_state_database = new HDFDatabase(); d_state_database->create(d_state_file_name); diff --git a/lib/linalg/svd/SVD.h b/lib/linalg/svd/SVD.h index 25a4662b0..0f8018db6 100644 --- a/lib/linalg/svd/SVD.h +++ b/lib/linalg/svd/SVD.h @@ -107,19 +107,6 @@ class SVD const Matrix* getSnapshotMatrix() = 0; - /** - * @brief Returns true if the next sample will result in a new time - * interval. - * - * @return True if the next sample results in the creation of a new time - * interval. - */ - bool - isNewSample() const - { - return (d_num_samples == 0); - } - /** * @brief Get the number of samples taken. * @@ -139,6 +126,19 @@ class SVD } protected: + /** + * @brief Returns true if the next sample will result in a new time + * interval. + * + * @return True if the next sample results in the creation of a new time + * interval. + */ + bool + isFirstSample() const + { + return (d_num_samples == 0); + } + /** * @brief Dimension of the system. */ diff --git a/lib/linalg/svd/StaticSVD.cpp b/lib/linalg/svd/StaticSVD.cpp index 47e8344e1..e342469ca 100644 --- a/lib/linalg/svd/StaticSVD.cpp +++ b/lib/linalg/svd/StaticSVD.cpp @@ -113,7 +113,7 @@ StaticSVD::takeSample( return false; } - if (isNewSample()) { + if (isFirstSample()) { // We have a new time interval. delete_factorizer(); d_num_samples = 0; From 7fb574d34e043b82c7424f590a917e5ac4521781 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Fri, 23 Feb 2024 16:33:16 -0800 Subject: [PATCH 29/37] update comments for StaticSVD::isBasisCurrent --- lib/linalg/svd/StaticSVD.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/linalg/svd/StaticSVD.h b/lib/linalg/svd/StaticSVD.h index a650936f2..9dd331a30 100644 --- a/lib/linalg/svd/StaticSVD.h +++ b/lib/linalg/svd/StaticSVD.h @@ -118,11 +118,9 @@ 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 isBasisCurrent() From 92267b30c5cb3c31099c522842bd070ebbd2170a Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Fri, 23 Feb 2024 16:39:21 -0800 Subject: [PATCH 30/37] minor comment updates. --- lib/linalg/svd/SVD.h | 3 ++- lib/linalg/svd/StaticSVD.cpp | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/linalg/svd/SVD.h b/lib/linalg/svd/SVD.h index 0f8018db6..ed701222a 100644 --- a/lib/linalg/svd/SVD.h +++ b/lib/linalg/svd/SVD.h @@ -118,7 +118,8 @@ class SVD /** * @brief Get the maximum number of samples that can be taken. - * + * SVD class will return an error if the number of samples exceed the maximum. + * */ int getMaxNumSamples() const { diff --git a/lib/linalg/svd/StaticSVD.cpp b/lib/linalg/svd/StaticSVD.cpp index e342469ca..6eff9b9a8 100644 --- a/lib/linalg/svd/StaticSVD.cpp +++ b/lib/linalg/svd/StaticSVD.cpp @@ -114,7 +114,6 @@ StaticSVD::takeSample( } if (isFirstSample()) { - // We have a new time interval. delete_factorizer(); d_num_samples = 0; d_basis = nullptr; From cdf7945153671c1c386cfcacb0468b25f92e826f Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Fri, 23 Feb 2024 16:43:08 -0800 Subject: [PATCH 31/37] stylization. --- lib/linalg/svd/SVD.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linalg/svd/SVD.h b/lib/linalg/svd/SVD.h index ed701222a..37bbf9fc4 100644 --- a/lib/linalg/svd/SVD.h +++ b/lib/linalg/svd/SVD.h @@ -119,7 +119,7 @@ class SVD /** * @brief Get the maximum number of samples that can be taken. * SVD class will return an error if the number of samples exceed the maximum. - * + * */ int getMaxNumSamples() const { From 40beb4b92c93627a372882fa873c6cb55e02dc31 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Mon, 26 Feb 2024 12:01:55 -0800 Subject: [PATCH 32/37] update dataset names and added python script for dataset name update. --- .github/workflows/run_tests/action.yml | 8 ++ CMakeLists.txt | 3 +- lib/linalg/BasisReader.cpp | 48 ++----- lib/linalg/BasisWriter.cpp | 24 ++-- 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/test_basis_conversion.cpp | 124 ++++++++++++++++++ 8 files changed, 187 insertions(+), 48 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 7a7ee5b2b..e1d376e22 100644 --- a/.github/workflows/run_tests/action.yml +++ b/.github/workflows/run_tests/action.yml @@ -23,6 +23,14 @@ runs: mpirun -n 3 --oversubscribe tests/test_StaticSVD ./tests/test_IncrementalSVDBrand mpirun -n 3 --oversubscribe tests/test_IncrementalSVDBrand + - 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 + ./tests/test_basis_conversion shell: bash diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a2b489f5..8356f3e92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -274,7 +274,8 @@ if(GTEST_FOUND) 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/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index cb00e2309..fcaa07ac4 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -66,9 +66,7 @@ BasisReader::getSpatialBasis() Matrix* spatial_basis_vectors = new Matrix(num_rows, num_cols, true); - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - d_database->getDoubleArray("spatial_basis_000000", + d_database->getDoubleArray("spatial_basis", &spatial_basis_vectors->item(0, 0), num_rows*num_cols); return spatial_basis_vectors; @@ -96,9 +94,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); - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - sprintf(tmp, "spatial_basis_000000"); + sprintf(tmp, "spatial_basis"); d_database->getDoubleArray(tmp, &spatial_basis_vectors->item(0, 0), num_rows*num_cols_to_read, @@ -144,9 +140,7 @@ BasisReader::getTemporalBasis() char tmp[100]; Matrix* temporal_basis_vectors = new Matrix(num_rows, num_cols, true); - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - sprintf(tmp, "temporal_basis_000000"); + sprintf(tmp, "temporal_basis"); d_database->getDoubleArray(tmp, &temporal_basis_vectors->item(0, 0), num_rows*num_cols); @@ -175,9 +169,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); - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - sprintf(tmp, "temporal_basis_000000"); + sprintf(tmp, "temporal_basis"); d_database->getDoubleArray(tmp, &temporal_basis_vectors->item(0, 0), num_rows*num_cols_to_read, @@ -220,15 +212,11 @@ BasisReader::getSingularValues() d_last_basis_idx = 0; char tmp[100]; int size; - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - sprintf(tmp, "singular_value_size_000000"); + sprintf(tmp, "singular_value_size"); d_database->getInteger(tmp, size); Vector* singular_values = new Vector(size, false); - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - sprintf(tmp, "singular_value_000000"); + sprintf(tmp, "singular_value"); d_database->getDoubleArray(tmp, &singular_values->item(0), size); @@ -281,12 +269,10 @@ BasisReader::getDim( char tmp[100]; int num_rows; - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - if (kind == "basis") sprintf(tmp, "spatial_basis_num_rows_000000"); - else if (kind == "snapshot") sprintf(tmp, "snapshot_matrix_num_rows_000000"); + 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_000000"); + "temporal_basis_num_rows"); d_database->getInteger(tmp, num_rows); return num_rows; @@ -304,12 +290,10 @@ BasisReader::getNumSamples( char tmp[100]; int num_cols; - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - if (kind == "basis") sprintf(tmp, "spatial_basis_num_cols_000000"); - else if (kind == "snapshot") sprintf(tmp, "snapshot_matrix_num_cols_000000"); + 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_000000"); + "temporal_basis_num_cols"); d_database->getInteger(tmp, num_cols); return num_cols; @@ -324,9 +308,7 @@ BasisReader::getSnapshotMatrix() char tmp[100]; Matrix* snapshots = new Matrix(num_rows, num_cols, false); - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - sprintf(tmp, "snapshot_matrix_000000"); + sprintf(tmp, "snapshot_matrix"); d_database->getDoubleArray(tmp, &snapshots->item(0, 0), num_rows*num_cols); @@ -355,9 +337,7 @@ BasisReader::getSnapshotMatrix( char tmp[100]; Matrix* snapshots = new Matrix(num_rows, num_cols_to_read, false); - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. - sprintf(tmp, "snapshot_matrix_000000"); + sprintf(tmp, "snapshot_matrix"); d_database->getDoubleArray(tmp, &snapshots->item(0, 0), num_rows*num_cols_to_read, diff --git a/lib/linalg/BasisWriter.cpp b/lib/linalg/BasisWriter.cpp index 712d5f0c7..b2bfd0ffd 100644 --- a/lib/linalg/BasisWriter.cpp +++ b/lib/linalg/BasisWriter.cpp @@ -71,39 +71,37 @@ BasisWriter::writeBasis(const std::string& kind) CAROM_ASSERT(kind == "basis" || kind == "snapshot"); char tmp[100]; - // This 0 index is the remainder from time interval concept. - // This remains only for backward compatibility purpose. if (kind == "basis") { d_database->create(full_file_name); const Matrix* basis = d_basis_generator->getSpatialBasis(); int num_rows = basis->numRows(); - sprintf(tmp, "spatial_basis_num_rows_000000"); + sprintf(tmp, "spatial_basis_num_rows"); d_database->putInteger(tmp, num_rows); int num_cols = basis->numColumns(); - sprintf(tmp, "spatial_basis_num_cols_000000"); + sprintf(tmp, "spatial_basis_num_cols"); d_database->putInteger(tmp, num_cols); - sprintf(tmp, "spatial_basis_000000"); + 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_000000"); + sprintf(tmp, "temporal_basis_num_rows"); d_database->putInteger(tmp, num_rows); num_cols = tbasis->numColumns(); - sprintf(tmp, "temporal_basis_num_cols_000000"); + sprintf(tmp, "temporal_basis_num_cols"); d_database->putInteger(tmp, num_cols); - sprintf(tmp, "temporal_basis_000000"); + 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_000000"); + sprintf(tmp, "singular_value_size"); d_database->putInteger(tmp, sv_dim); - sprintf(tmp, "singular_value_000000"); + sprintf(tmp, "singular_value"); d_database->putDoubleArray(tmp, &sv->item(0), sv_dim); d_database->close(); @@ -114,12 +112,12 @@ BasisWriter::writeBasis(const std::string& kind) const Matrix* snapshots = d_basis_generator->getSnapshotMatrix(); int num_rows = snapshots->numRows(); // d_dim - sprintf(tmp, "snapshot_matrix_num_rows_000000"); + 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_000000"); + sprintf(tmp, "snapshot_matrix_num_cols"); d_snap_database->putInteger(tmp, num_cols); - sprintf(tmp, "snapshot_matrix_000000"); + sprintf(tmp, "snapshot_matrix"); d_snap_database->putDoubleArray(tmp, &snapshots->item(0,0), num_rows*num_cols); d_snap_database->close(); 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&d +#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 From 6b7f7be5f828b5b55b6572c0e95cadee89ba2739 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Mon, 26 Feb 2024 12:08:46 -0800 Subject: [PATCH 33/37] fixed the ci workflow --- .github/workflows/run_tests/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run_tests/action.yml b/.github/workflows/run_tests/action.yml index e1d376e22..9d068e29c 100644 --- a/.github/workflows/run_tests/action.yml +++ b/.github/workflows/run_tests/action.yml @@ -23,6 +23,7 @@ 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 @@ -31,7 +32,6 @@ runs: python3 update_basis_format.py test_basis.000000 python3 update_basis_format.py test_basis_snapshot.000000 ./tests/test_basis_conversion - shell: bash - name: Run regression tests From 08aa57beaee2281c6f07c294fb1bd2044a3f3322 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Mon, 26 Feb 2024 12:16:05 -0800 Subject: [PATCH 34/37] minor fix in ci workflow. --- .github/workflows/run_tests/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run_tests/action.yml b/.github/workflows/run_tests/action.yml index 9d068e29c..17a5cf769 100644 --- a/.github/workflows/run_tests/action.yml +++ b/.github/workflows/run_tests/action.yml @@ -31,7 +31,7 @@ runs: 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 - ./tests/test_basis_conversion + ./test_basis_conversion shell: bash - name: Run regression tests From 718baaec1b01a0eb7a0710cc42d17c8cb4d66018 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Mon, 26 Feb 2024 13:07:24 -0800 Subject: [PATCH 35/37] minor fix --- lib/linalg/BasisGenerator.cpp | 12 ------------ lib/linalg/BasisReader.cpp | 20 ++++++++++---------- lib/linalg/BasisReader.h | 14 ++++++-------- lib/linalg/svd/SVD.h | 2 +- 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index 7d5aeacf1..945c34bda 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -139,18 +139,6 @@ BasisGenerator::takeSample( return false; } - /* - Note for previous implementation: - Previously with multiple time interval, - there was an input argument (double dt), - which is only used to reset d_dt for new time interval. - Assuming only single interval is used in practice, - resetDt(dt) was never used in takeSample, - and options.initial_dt is used for incremental svd. - */ - // if (d_svd->isFirstSample()) - // resetDt(dt); - return d_svd->takeSample(u_in, add_without_increase); } diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index fcaa07ac4..67c24517f 100644 --- a/lib/linalg/BasisReader.cpp +++ b/lib/linalg/BasisReader.cpp @@ -22,7 +22,7 @@ namespace CAROM { BasisReader::BasisReader( const std::string& base_file_name, Database::formats db_format) : - d_last_basis_idx(-1), + d_file_loaded(false), full_file_name(""), base_file_name_(base_file_name) { @@ -60,7 +60,7 @@ BasisReader::~BasisReader() Matrix* BasisReader::getSpatialBasis() { - d_last_basis_idx = 0; + d_file_loaded = true; int num_rows = getDim("basis"); int num_cols = getNumSamples("basis"); @@ -84,7 +84,7 @@ BasisReader::getSpatialBasis( int start_col, int end_col) { - d_last_basis_idx = 0; + d_file_loaded = true; int num_rows = getDim("basis"); int num_cols = getNumSamples("basis"); @@ -134,7 +134,7 @@ BasisReader::getSpatialBasis( Matrix* BasisReader::getTemporalBasis() { - d_last_basis_idx = 0; + d_file_loaded = true; int num_rows = getDim("temporal_basis"); int num_cols = getNumSamples("temporal_basis"); @@ -159,7 +159,7 @@ BasisReader::getTemporalBasis( int start_col, int end_col) { - d_last_basis_idx = 0; + d_file_loaded = true; int num_rows = getDim("temporal_basis"); int num_cols = getNumSamples("temporal_basis"); @@ -209,7 +209,7 @@ BasisReader::getTemporalBasis( Vector* BasisReader::getSingularValues() { - d_last_basis_idx = 0; + d_file_loaded = true; char tmp[100]; int size; sprintf(tmp, "singular_value_size"); @@ -265,7 +265,7 @@ BasisReader::getDim( (kind == "snapshot") || (kind == "temporal_basis")); - d_last_basis_idx = 0; + d_file_loaded = true; char tmp[100]; int num_rows; @@ -286,7 +286,7 @@ BasisReader::getNumSamples( (kind == "snapshot") || (kind == "temporal_basis")); - d_last_basis_idx = 0; + d_file_loaded = true; char tmp[100]; int num_cols; @@ -302,7 +302,7 @@ BasisReader::getNumSamples( Matrix* BasisReader::getSnapshotMatrix() { - d_last_basis_idx = 0; + d_file_loaded = true; int num_rows = getDim("snapshot"); int num_cols = getNumSamples("snapshot"); @@ -327,7 +327,7 @@ BasisReader::getSnapshotMatrix( int start_col, int end_col) { - d_last_basis_idx = 0; + d_file_loaded = true; int num_rows = getDim("snapshot"); int num_cols = getNumSamples("snapshot"); diff --git a/lib/linalg/BasisReader.h b/lib/linalg/BasisReader.h index 5a7090802..ab1ca0f2b 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -55,16 +55,14 @@ class BasisReader { ~BasisReader(); /** - * @brief Returns true if the basis vectors at requested time are - * different from the last requested basis vectors. + * @brief Returns true if this already loaded either a snapshot or basis file. * - * @return True if the basis vectors at the requested time are different - * from the last requested basis vectors. + * @return True if either a snapshot or basis file is already loaded. */ bool - isNewBasis() + isFileLoaded() { - return (d_last_basis_idx == -1); + return d_file_loaded; } /** @@ -301,9 +299,9 @@ class BasisReader { std::string full_file_name; /** - * @brief The last time at which basis vectors were requested. + * @brief Whether or not a file is loaded. */ - int d_last_basis_idx; + bool d_file_loaded; }; } diff --git a/lib/linalg/svd/SVD.h b/lib/linalg/svd/SVD.h index 37bbf9fc4..7ddbc21b8 100644 --- a/lib/linalg/svd/SVD.h +++ b/lib/linalg/svd/SVD.h @@ -118,7 +118,7 @@ class SVD /** * @brief Get the maximum number of samples that can be taken. - * SVD class will return an error if the number of samples exceed the maximum. + * SVD class will return an error if the number of samples exceeds the maximum. * */ int getMaxNumSamples() const From 4e04f251b5a213f30c2bf6eef123732708b7729b Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Mon, 26 Feb 2024 13:32:29 -0800 Subject: [PATCH 36/37] removed BasisReader:isNewBasis --- lib/linalg/BasisReader.cpp | 10 ---------- lib/linalg/BasisReader.h | 16 ---------------- 2 files changed, 26 deletions(-) diff --git a/lib/linalg/BasisReader.cpp b/lib/linalg/BasisReader.cpp index 67c24517f..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_file_loaded(false), full_file_name(""), base_file_name_(base_file_name) { @@ -60,7 +59,6 @@ BasisReader::~BasisReader() Matrix* BasisReader::getSpatialBasis() { - d_file_loaded = true; int num_rows = getDim("basis"); int num_cols = getNumSamples("basis"); @@ -84,7 +82,6 @@ BasisReader::getSpatialBasis( int start_col, int end_col) { - d_file_loaded = true; int num_rows = getDim("basis"); int num_cols = getNumSamples("basis"); @@ -134,7 +131,6 @@ BasisReader::getSpatialBasis( Matrix* BasisReader::getTemporalBasis() { - d_file_loaded = true; int num_rows = getDim("temporal_basis"); int num_cols = getNumSamples("temporal_basis"); @@ -159,7 +155,6 @@ BasisReader::getTemporalBasis( int start_col, int end_col) { - d_file_loaded = true; int num_rows = getDim("temporal_basis"); int num_cols = getNumSamples("temporal_basis"); @@ -209,7 +204,6 @@ BasisReader::getTemporalBasis( Vector* BasisReader::getSingularValues() { - d_file_loaded = true; char tmp[100]; int size; sprintf(tmp, "singular_value_size"); @@ -265,7 +259,6 @@ BasisReader::getDim( (kind == "snapshot") || (kind == "temporal_basis")); - d_file_loaded = true; char tmp[100]; int num_rows; @@ -286,7 +279,6 @@ BasisReader::getNumSamples( (kind == "snapshot") || (kind == "temporal_basis")); - d_file_loaded = true; char tmp[100]; int num_cols; @@ -302,7 +294,6 @@ BasisReader::getNumSamples( Matrix* BasisReader::getSnapshotMatrix() { - d_file_loaded = true; int num_rows = getDim("snapshot"); int num_cols = getNumSamples("snapshot"); @@ -327,7 +318,6 @@ BasisReader::getSnapshotMatrix( int start_col, int end_col) { - d_file_loaded = true; int num_rows = getDim("snapshot"); int num_cols = getNumSamples("snapshot"); diff --git a/lib/linalg/BasisReader.h b/lib/linalg/BasisReader.h index ab1ca0f2b..eb17998a2 100644 --- a/lib/linalg/BasisReader.h +++ b/lib/linalg/BasisReader.h @@ -54,17 +54,6 @@ class BasisReader { */ ~BasisReader(); - /** - * @brief Returns true if this already loaded either a snapshot or basis file. - * - * @return True if either a snapshot or basis file is already loaded. - */ - bool - isFileLoaded() - { - return d_file_loaded; - } - /** * * @brief Returns the spatial basis vectors as a Matrix. @@ -297,11 +286,6 @@ class BasisReader { * @brief Full file name of database including rank. */ std::string full_file_name; - - /** - * @brief Whether or not a file is loaded. - */ - bool d_file_loaded; }; } From 57eb4e1e2b8306e7e9db9b5b34de6d3137c27c78 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Wed, 28 Feb 2024 09:48:19 -0800 Subject: [PATCH 37/37] minor doxygen comment update --- lib/linalg/svd/IncrementalSVD.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/linalg/svd/IncrementalSVD.h b/lib/linalg/svd/IncrementalSVD.h index 144781597..5b293e45a 100644 --- a/lib/linalg/svd/IncrementalSVD.h +++ b/lib/linalg/svd/IncrementalSVD.h @@ -107,10 +107,8 @@ 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