Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for libROM smart pointers #192

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions rom/laghos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,9 +1389,7 @@ int main(int argc, char *argv[])
CAROM::DMD* dmd_V = NULL;
CAROM::DMD* dmd_E = NULL;

CAROM::Vector* result_X = NULL;
CAROM::Vector* result_V = NULL;
CAROM::Vector* result_E = NULL;
std::unique_ptr<CAROM::Vector> result_X, result_V, result_E;
if (rom_restore)
{
if (romOptions.dmd)
Expand Down Expand Up @@ -1438,9 +1436,9 @@ int main(int argc, char *argv[])
}
}

if (result_X != NULL) delete result_X;
if (result_V != NULL) delete result_V;
if (result_E != NULL) delete result_E;
result_X.release();
result_V.release();
result_E.release();

if (myid == 0) cout << "Predicting time t " << curr_time << " using DMD window " << curr_window << " with initial start time " << window_start_time << std::endl;

Expand Down Expand Up @@ -2630,9 +2628,6 @@ int main(int argc, char *argv[])
delete dmd_X;
delete dmd_V;
delete dmd_E;
delete result_X;
delete result_V;
delete result_E;
}

return 0;
Expand Down
8 changes: 4 additions & 4 deletions rom/laghos_eqp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ void ComputeElementRowOfG_E(const IntegrationRule *ir,
void ROM_Basis::SetupEQP_Force(std::vector<const CAROM::Matrix*> snapX,
std::vector<const CAROM::Matrix*> snapV,
std::vector<const CAROM::Matrix*> snapE,
const CAROM::Matrix* basisV,
const CAROM::Matrix* basisE,
const std::shared_ptr<CAROM::Matrix> &basisV,
const std::shared_ptr<CAROM::Matrix> &basisE,
ROM_Options const& input, std::set<int> & elems)
{
MFEM_VERIFY(basisV->numRows() == input.H1FESpace->GetTrueVSize(), "");
Expand Down Expand Up @@ -451,8 +451,8 @@ void ROM_Basis::SetupEQP_Force(std::vector<const CAROM::Matrix*> snapX,
void ROM_Basis::SetupEQP_Force_Eq(std::vector<const CAROM::Matrix*> snapX,
std::vector<const CAROM::Matrix*> snapV,
std::vector<const CAROM::Matrix*> snapE,
const CAROM::Matrix* basisV,
const CAROM::Matrix* basisE,
const std::shared_ptr<CAROM::Matrix> &basisV,
const std::shared_ptr<CAROM::Matrix> &basisE,
ROM_Options const& input,
bool equationE,
std::set<int> & elems)
Expand Down
175 changes: 73 additions & 102 deletions rom/laghos_rom.cpp

Large diffs are not rendered by default.

76 changes: 26 additions & 50 deletions rom/laghos_rom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ class DMD_Sampler
if (offsetInit)
{
std::string path_init = (input.offsetType == interpolateOffset) ? basename + "/ROMoffset" + input.basisIdentifier + "/param" + std::to_string(parameterID) + "_init" : basename + "/ROMoffset" + input.basisIdentifier + "/init";
initX = new CAROM::Vector(tH1size, true);
initV = new CAROM::Vector(tH1size, true);
initE = new CAROM::Vector(tL2size, true);
initX.reset(new CAROM::Vector(tH1size, true));
initV.reset(new CAROM::Vector(tH1size, true));
initE.reset(new CAROM::Vector(tL2size, true));
Xdiff.SetSize(tH1size);
Ediff.SetSize(tL2size);

Expand Down Expand Up @@ -360,25 +360,22 @@ class DMD_Sampler
else
{
first_sv = input.sv_shift;
initX = NULL;
initV = NULL;
initE = NULL;
}

if (input.dmd_nonuniform)
{
dmd_X = new CAROM::NonuniformDMD(tH1size, false, initX, initV);
dmd_X = new CAROM::NonuniformDMD(tH1size, initX, initV, false);
dmd_V = new CAROM::NonuniformDMD(tH1size);
dmd_E = new CAROM::NonuniformDMD(tL2size);
}
else
{
dmd_X = new CAROM::AdaptiveDMD(tH1size, input.desired_dt, "G", "LS",
input.dmd_closest_rbf, initX);
input.dmd_closest_rbf, false, initX);
dmd_V = new CAROM::AdaptiveDMD(tH1size, input.desired_dt, "G", "LS",
input.dmd_closest_rbf, initV);
input.dmd_closest_rbf, false, initV);
dmd_E = new CAROM::AdaptiveDMD(tL2size, input.desired_dt, "G", "LS",
input.dmd_closest_rbf, initE);
input.dmd_closest_rbf, false, initE);
}
}

Expand Down Expand Up @@ -426,9 +423,7 @@ class DMD_Sampler
Vector X, X0, Xdiff, Ediff, dXdt, V, V0, dVdt, E, E0, dEdt;

const bool offsetInit;
CAROM::Vector *initX = 0;
CAROM::Vector *initV = 0;
CAROM::Vector *initE = 0;
std::shared_ptr<CAROM::Vector> initX, initV, initE;

ParGridFunction gfH1, gfL2;

Expand Down Expand Up @@ -960,8 +955,8 @@ class ROM_Basis
return BEsp;
}

CAROM::Matrix *GetBXtBV() {
return basisX->transposeMult(basisV);
std::unique_ptr<CAROM::Matrix> GetBXtBV() {
return basisX->transposeMult(*basisV);
}

void ComputeReducedMatrices(bool sns1);
Expand All @@ -980,9 +975,9 @@ class ROM_Basis
CAROM::SampleMeshManager *smm = NULL;
CAROM::SampleDOFSelector *sampleSelector = NULL;

CAROM::Matrix* PiXtransPiV = 0; // TODO: make this private and use a function to access its mult
CAROM::Matrix* PiXtransPiX = 0; // TODO: make this private and use a function to access its mult
CAROM::Matrix* PiXtransPiXlag = 0; // TODO: make this private and use a function to access its mult
std::unique_ptr<CAROM::Matrix> PiXtransPiV; // TODO: make this private and use a function to access its mult
std::unique_ptr<CAROM::Matrix> PiXtransPiX; // TODO: make this private and use a function to access its mult
std::unique_ptr<CAROM::Matrix> PiXtransPiXlag; // TODO: make this private and use a function to access its mult

private:
const bool hyperreduce;
Expand All @@ -1005,11 +1000,7 @@ class ROM_Basis

std::string basisIdentifier;

CAROM::Matrix* basisX = 0;
CAROM::Matrix* basisV = 0;
CAROM::Matrix* basisE = 0;
CAROM::Matrix* basisFv = 0;
CAROM::Matrix* basisFe = 0;
std::shared_ptr<CAROM::Matrix> basisX, basisV, basisE, basisFv, basisFe;

std::string basename = "run";
std::string testing_parameter_basename = "run";
Expand Down Expand Up @@ -1052,21 +1043,15 @@ class ROM_Basis
CAROM::Vector *sV = NULL;
CAROM::Vector *sE = NULL;

CAROM::Matrix *BsinvX = NULL;
CAROM::Matrix *BsinvV = NULL;
CAROM::Matrix *BsinvE = NULL;

CAROM::Matrix *BwinX = NULL;
CAROM::Matrix *BwinV = NULL;
CAROM::Matrix *BwinE = NULL;
std::unique_ptr<CAROM::Matrix> BsinvX, BsinvV, BsinvE, BwinX, BwinV, BwinE;

CAROM::Vector *initX = 0;
CAROM::Vector *initV = 0;
CAROM::Vector *initE = 0;
CAROM::Vector *initXsp = 0;
CAROM::Vector *initVsp = 0;
CAROM::Vector *initEsp = 0;
CAROM::Vector *BX0 = NULL;
std::unique_ptr<CAROM::Vector> BX0;

CAROM::Vector *BtInitDiffX = 0; // TODO: destructor
CAROM::Vector *BtInitDiffV = 0;
Expand All @@ -1085,9 +1070,8 @@ class ROM_Basis
const bool Voffset;

const bool RK2AvgFormulation;
CAROM::Matrix *BXXinv = NULL;
CAROM::Matrix *BVVinv = NULL;
CAROM::Matrix *BEEinv = NULL;

std::unique_ptr<CAROM::Matrix> BXXinv, BVVinv, BEEinv;

double energyFraction_X;

Expand All @@ -1103,8 +1087,8 @@ class ROM_Basis
private:
void SetSpaceTimeInitialGuessComponent(Vector& st, std::string const& name,
ParFiniteElementSpace *fespace,
const CAROM::Matrix* basis,
const CAROM::Matrix* tbasis,
const CAROM::Matrix &basis,
const CAROM::Matrix &tbasis,
const int nt,
const int rdim) const;

Expand All @@ -1116,16 +1100,16 @@ class ROM_Basis
void SetupEQP_Force(std::vector<const CAROM::Matrix*> snapX,
std::vector<const CAROM::Matrix*> snapV,
std::vector<const CAROM::Matrix*> snapE,
const CAROM::Matrix* basisV,
const CAROM::Matrix* basisE,
const std::shared_ptr<CAROM::Matrix> &basisV,
const std::shared_ptr<CAROM::Matrix> &basisE,
ROM_Options const& input,
std::set<int> & elems);

void SetupEQP_Force_Eq(std::vector<const CAROM::Matrix*> snapX,
std::vector<const CAROM::Matrix*> snapV,
std::vector<const CAROM::Matrix*> snapE,
const CAROM::Matrix* basisV,
const CAROM::Matrix* basisE,
const std::shared_ptr<CAROM::Matrix> &basisV,
const std::shared_ptr<CAROM::Matrix> &basisE,
ROM_Options const& input,
bool equationE,
std::set<int> & elems);
Expand All @@ -1139,16 +1123,8 @@ class ROM_Basis
int VTos = 0; // Velocity temporal index offset, used for V and Fe. This fixes the issue that V and Fe are not sampled at t=0, since they are initially zero. This is valid for the Sedov test but not in general when the initial velocity is nonzero.
// TODO: generalize for nonzero initial velocity.

CAROM::Matrix* tbasisX = 0;
CAROM::Matrix* tbasisV = 0;
CAROM::Matrix* tbasisE = 0;
CAROM::Matrix* tbasisFv = 0;
CAROM::Matrix* tbasisFe = 0;

CAROM::Matrix* PiVtransPiFv = 0;
CAROM::Matrix* PiEtransPiFe = 0;

// TODO: delete the new pointers added for space-time
std::shared_ptr<CAROM::Matrix> tbasisX, tbasisV, tbasisE, tbasisFv, tbasisFe;
std::unique_ptr<CAROM::Matrix> PiVtransPiFv, PiEtransPiFe;

std::vector<int> timeSamples; // merged V and E time samples

Expand Down
4 changes: 2 additions & 2 deletions rom/laghos_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void BasisGeneratorFinalSummary(CAROM::BasisGenerator* bg, const int first_sv,
const bool printout, const bool squareSV)
{
const int rom_dim = bg->getSpatialBasis()->numColumns();
const CAROM::Vector* sing_vals = bg->getSingularValues();
const std::shared_ptr<const CAROM::Vector> sing_vals = bg->getSingularValues();

MFEM_VERIFY(rom_dim <= sing_vals->dim(), "");

Expand Down Expand Up @@ -262,7 +262,7 @@ void PrintSingularValues(const int rank, const std::string& basename,
const std::string& name, CAROM::BasisGenerator* bg,
const bool usingWindows, const int window)
{
const CAROM::Vector* sing_vals = bg->getSingularValues();
const std::shared_ptr<const CAROM::Vector> sing_vals = bg->getSingularValues();

const std::string rankStr = "." + GetRankString6(rank);
const std::string fullname = (usingWindows) ? basename + "/sVal" + name
Expand Down
5 changes: 2 additions & 3 deletions rom/merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void MergeSamplingWindow(const int rank, const int first_sv,
int num_cols = col_ub - col_lb + 1;
std::cout << num_cols << " columns read. Columns " << col_lb - 1
<< " to " << col_ub - 1 << std::endl;
CAROM::Matrix* mat = basis_reader->getSnapshotMatrix(col_lb, col_ub);
std::unique_ptr<CAROM::Matrix> mat = basis_reader->getSnapshotMatrix(col_lb, col_ub);
MFEM_VERIFY(dim == mat->numRows(), "Inconsistent snapshot size");
MFEM_VERIFY(num_cols == mat->numColumns(), "Inconsistent number of snapshots");

Expand Down Expand Up @@ -179,7 +179,6 @@ void MergeSamplingWindow(const int rank, const int first_sv,
}

delete init;
delete mat;
}

cout << "Computing SVD for " << varName << " in basis window " << basisWindow << endl;
Expand Down Expand Up @@ -236,7 +235,7 @@ void GetSnapshotDim(const int id, const std::string& basename, const std::string
std::string filename = basename + "/param" + std::to_string(id) + "_var" + varName + std::to_string(window) + basisIdentifier + "_snapshot";

CAROM::BasisReader reader(filename);
const CAROM::Matrix *S = reader.getSnapshotMatrix();
const std::unique_ptr<CAROM::Matrix> S = reader.getSnapshotMatrix();
varDim = S->numRows();
numSnapshots = S->numColumns();
}
Expand Down
8 changes: 4 additions & 4 deletions rom/tests/basisComparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ void compareBasis(string &baselineFile, string &targetFile, double errorBound, i
vector<double> reducedDiffVecNormL2;

CAROM::BasisReader baselineReader(baselineFile);
CAROM::Matrix *baselineBasis = (CAROM::Matrix*) baselineReader.getSpatialBasis(0.0);
CAROM::Vector *baselineSV = (CAROM::Vector*) baselineReader.getSingularValues(0.0);
std::unique_ptr<CAROM::Matrix> baselineBasis = baselineReader.getSpatialBasis(0.0);
std::unique_ptr<CAROM::Vector> baselineSV = baselineReader.getSingularValues(0.0);
CAROM::BasisReader targetReader(targetFile);
CAROM::Matrix *targetBasis = (CAROM::Matrix*) targetReader.getSpatialBasis(0.0);
std::unique_ptr<CAROM::Matrix> targetBasis = targetReader.getSpatialBasis(0.0);
CAROM::BasisReader diffReader(baselineFile);
CAROM::Matrix *diffBasis = (CAROM::Matrix*) diffReader.getSpatialBasis(0.0);
std::unique_ptr<CAROM::Matrix> diffBasis = diffReader.getSpatialBasis(0.0);

// Get basis dimensions
int baselineNumRows = baselineBasis->numRows();
Expand Down