From 1f6df17cdd560a2faacb9da21709255a7b7ab07b Mon Sep 17 00:00:00 2001 From: Dylan Copeland Date: Thu, 18 Jul 2024 18:27:59 -0700 Subject: [PATCH] More elimination of raw pointers in Vector class. --- examples/dmd/de_dg_advection_greedy.cpp | 23 +- .../de_parametric_heat_conduction_greedy.cpp | 17 +- examples/dmd/dg_advection.cpp | 6 +- examples/dmd/dg_euler.cpp | 21 +- examples/dmd/heat_conduction.cpp | 7 +- examples/dmd/local_dw_csv.cpp | 18 +- examples/dmd/local_tw_csv.cpp | 16 +- examples/dmd/nonlinear_elasticity.cpp | 14 +- examples/dmd/parametric_dw_csv.cpp | 33 +- examples/dmd/parametric_heat_conduction.cpp | 8 +- examples/dmd/parametric_tw_csv.cpp | 27 +- examples/dmd/wave_equation.cpp | 7 +- lib/algo/AdaptiveDMD.cpp | 11 +- lib/algo/AdaptiveDMD.h | 4 +- lib/algo/DMD.cpp | 29 +- lib/algo/DMD.h | 11 +- lib/algo/DMDc.cpp | 16 +- lib/algo/DMDc.h | 4 +- lib/algo/NonuniformDMD.cpp | 6 +- lib/algo/NonuniformDMD.h | 2 +- lib/algo/SnapshotDMD.cpp | 2 +- lib/algo/SnapshotDMD.h | 7 +- .../manifold_interp/PCHIPInterpolator.cpp | 15 +- lib/algo/manifold_interp/PCHIPInterpolator.h | 11 +- .../manifold_interp/VectorInterpolator.cpp | 19 +- lib/algo/manifold_interp/VectorInterpolator.h | 16 +- lib/linalg/BasisGenerator.cpp | 6 +- lib/linalg/Vector.cpp | 85 +--- lib/linalg/Vector.h | 148 +------ lib/linalg/svd/IncrementalSVD.cpp | 8 +- lib/linalg/svd/IncrementalSVDBrand.cpp | 10 +- unit_tests/test_DMD.cpp | 8 +- unit_tests/test_PCHIPInterpolator.cpp | 7 +- unit_tests/test_Vector.cpp | 397 +----------------- 34 files changed, 176 insertions(+), 843 deletions(-) diff --git a/examples/dmd/de_dg_advection_greedy.cpp b/examples/dmd/de_dg_advection_greedy.cpp index ad7191b20..0f57b95bf 100644 --- a/examples/dmd/de_dg_advection_greedy.cpp +++ b/examples/dmd/de_dg_advection_greedy.cpp @@ -644,10 +644,9 @@ double simulation() } dmd_prediction_timer.Start(); - CAROM::Vector* result_U = dmd_U->predict(t_final); + std::shared_ptr result_U = dmd_U->predict(t_final); dmd_prediction_timer.Stop(); - // 21. Calculate the relative error between the DMD final solution and the true solution. Vector dmd_solution_U(result_U->getData(), result_U->dim()); @@ -668,8 +667,6 @@ double simulation() dmd_prediction_timer.RealTime()); } - delete result_U; - return rel_diff; } @@ -740,8 +737,7 @@ double simulation() // THIS IS LIKE COMPUTING A RESIDUAL. t = t_final - 10.*dt; - CAROM::Vector* carom_tf_u_minus_some = dmd_U->predict(t); - + std::shared_ptr carom_tf_u_minus_some = dmd_U->predict(t); for(int i = 0; i < carom_tf_u_minus_some->dim(); i++) { @@ -749,8 +745,6 @@ double simulation() } u_gf->SetFromTrueDofs(*U); - - delete carom_tf_u_minus_some; } ts.push_back(t); @@ -925,8 +919,7 @@ double simulation() } - CAROM::Vector* result_U = dmd_U->predict(t_final); - + std::shared_ptr result_U = dmd_U->predict(t_final); Vector dmd_solution_U(result_U->getData(), result_U->dim()); @@ -945,8 +938,6 @@ double simulation() << rel_diff << std::endl; } - delete result_U; - if (myid == 0) { printf("Elapsed time for training DMD: %e second\n", @@ -956,7 +947,7 @@ double simulation() else if (online) { dmd_prediction_timer.Start(); - CAROM::Vector* result_U = dmd_U->predict(ts[0]); + std::shared_ptr result_U = dmd_U->predict(ts[0]); Vector initial_dmd_solution_U(result_U->getData(), result_U->dim()); u_gf->SetFromTrueDofs(initial_dmd_solution_U); @@ -972,8 +963,6 @@ double simulation() dmd_visit_dc.Save(); } - delete result_U; - if (visit) { for (int i = 1; i < ts.size(); i++) @@ -993,8 +982,6 @@ double simulation() dmd_visit_dc.SetCycle(i); dmd_visit_dc.SetTime(ts[i]); dmd_visit_dc.Save(); - - delete result_U; } } } @@ -1039,8 +1026,6 @@ double simulation() tot_true_solution_u_norm << std::endl; fout.close(); } - - delete result_U; } // 22. Calculate the relative error as commanded by the greedy algorithm. if (offline) diff --git a/examples/dmd/de_parametric_heat_conduction_greedy.cpp b/examples/dmd/de_parametric_heat_conduction_greedy.cpp index 60e532d87..68db41fad 100644 --- a/examples/dmd/de_parametric_heat_conduction_greedy.cpp +++ b/examples/dmd/de_parametric_heat_conduction_greedy.cpp @@ -467,15 +467,13 @@ double simulation() // compare the final FOM solution to the DMD predicted solution. t = t_final - 10.0 * dt; - CAROM::Vector* carom_tf_u_minus_some = dmd_u->predict(t); + std::shared_ptr carom_tf_u_minus_some = dmd_u->predict(t); Vector tf_u_minus_some(carom_tf_u_minus_some->getData(), carom_tf_u_minus_some->dim()); u = tf_u_minus_some; u_gf.SetFromTrueDofs(u); - - delete carom_tf_u_minus_some; } ts.push_back(t); @@ -683,7 +681,8 @@ double simulation() *true_solution_u, *true_solution_u)); } } - CAROM::Vector* result_u = dmd_u->predict(t_final); + + std::shared_ptr result_u = dmd_u->predict(t_final); Vector dmd_solution_u(result_u->getData(), result_u->dim()); Vector diff_u(true_solution_u->Size()); @@ -699,8 +698,6 @@ double simulation() << rel_diff << std::endl; } - delete result_u; - if (!de && myid == 0) { printf("Elapsed time for training DMD: %e second\n", @@ -730,7 +727,7 @@ double simulation() std::cout << "Predicting temperature using DMD at: " << ts[0] << std::endl; } - CAROM::Vector* result_u = dmd_u->predict(ts[0]); + std::shared_ptr result_u = dmd_u->predict(ts[0]); Vector initial_dmd_solution_u(result_u->getData(), result_u->dim()); u_gf.SetFromTrueDofs(initial_dmd_solution_u); @@ -746,8 +743,6 @@ double simulation() dmd_visit_dc.Save(); } - delete result_u; - if (visit) { for (int i = 1; i < ts.size(); i++) @@ -766,8 +761,6 @@ double simulation() dmd_visit_dc.SetCycle(i); dmd_visit_dc.SetTime(ts[i]); dmd_visit_dc.Save(); - - delete result_u; } } } @@ -792,8 +785,6 @@ double simulation() printf("Elapsed time for predicting DMD: %e second\n", dmd_prediction_timer.RealTime()); } - - delete result_u; } // 19. Calculate the relative error as commanded by the greedy algorithm. diff --git a/examples/dmd/dg_advection.cpp b/examples/dmd/dg_advection.cpp index 1abdfef3d..008bac3dc 100644 --- a/examples/dmd/dg_advection.cpp +++ b/examples/dmd/dg_advection.cpp @@ -716,7 +716,7 @@ int main(int argc, char *argv[]) std::cout << "Predicting solution using DMD" << std::endl; } - CAROM::Vector* result_u = dmd_U.predict(ts[0]); + std::shared_ptr result_u = dmd_U.predict(ts[0]); Vector initial_dmd_solution_u(result_u->getData(), result_u->dim()); u->SetFromTrueDofs(initial_dmd_solution_u); @@ -733,8 +733,6 @@ int main(int argc, char *argv[]) dmd_dc->Save(); } - delete result_u; - if (visit) { for (int i = 1; i < ts.size(); i++) @@ -748,7 +746,6 @@ int main(int argc, char *argv[]) dmd_dc->SetCycle(i); dmd_dc->SetTime(ts[i]); dmd_dc->Save(); - delete result_u; } } } @@ -788,7 +785,6 @@ int main(int argc, char *argv[]) delete pmesh; delete ode_solver; delete pd; - delete result_u; #ifdef MFEM_USE_ADIOS2 if (adios2) { diff --git a/examples/dmd/dg_euler.cpp b/examples/dmd/dg_euler.cpp index f88d03813..cf5c7eb10 100644 --- a/examples/dmd/dg_euler.cpp +++ b/examples/dmd/dg_euler.cpp @@ -582,10 +582,10 @@ int main(int argc, char *argv[]) std::cout << "Predicting density, momentum, and energy using DMD" << std::endl; } - CAROM::Vector* result_dens = dmd_dens->predict(ts[0]); - CAROM::Vector* result_x_mom = dmd_x_mom->predict(ts[0]); - CAROM::Vector* result_y_mom = dmd_y_mom->predict(ts[0]); - CAROM::Vector* result_e = dmd_e->predict(ts[0]); + std::shared_ptr result_dens = dmd_dens->predict(ts[0]); + std::shared_ptr result_x_mom = dmd_x_mom->predict(ts[0]); + std::shared_ptr result_y_mom = dmd_y_mom->predict(ts[0]); + std::shared_ptr result_e = dmd_e->predict(ts[0]); Vector initial_dmd_solution_dens(result_dens->getData(), result_dens->dim()); Vector initial_dmd_solution_x_mom(result_x_mom->getData(), result_x_mom->dim()); Vector initial_dmd_solution_y_mom(result_y_mom->getData(), result_y_mom->dim()); @@ -604,10 +604,6 @@ int main(int argc, char *argv[]) dmd_visit_dc.Save(); } - delete result_dens; - delete result_x_mom; - delete result_y_mom; - delete result_e; if (visit) { for (int i = 1; i < ts.size(); i++) @@ -630,11 +626,6 @@ int main(int argc, char *argv[]) dmd_visit_dc.SetCycle(i); dmd_visit_dc.SetTime(ts[i]); dmd_visit_dc.Save(); - - delete result_dens; - delete result_x_mom; - delete result_y_mom; - delete result_e; } } } @@ -701,10 +692,6 @@ int main(int argc, char *argv[]) // Free the used memory. delete ode_solver; - delete result_dens; - delete result_x_mom; - delete result_y_mom; - delete result_e; delete dmd_dens; delete dmd_x_mom; delete dmd_y_mom; diff --git a/examples/dmd/heat_conduction.cpp b/examples/dmd/heat_conduction.cpp index 3b229a87d..ca95e72dd 100644 --- a/examples/dmd/heat_conduction.cpp +++ b/examples/dmd/heat_conduction.cpp @@ -504,7 +504,7 @@ int main(int argc, char *argv[]) std::cout << "Predicting temperature using DMD" << std::endl; } - CAROM::Vector* result_u = dmd_u->predict(ts[0]); + std::shared_ptr result_u = dmd_u->predict(ts[0]); Vector initial_dmd_solution_u(result_u->getData(), result_u->dim()); u_gf.SetFromTrueDofs(initial_dmd_solution_u); @@ -517,8 +517,6 @@ int main(int argc, char *argv[]) dmd_visit_dc.Save(); } - delete result_u; - if (visit) { for (int i = 1; i < ts.size(); i++) @@ -532,8 +530,6 @@ int main(int argc, char *argv[]) dmd_visit_dc.SetCycle(i); dmd_visit_dc.SetTime(ts[i]); dmd_visit_dc.Save(); - - delete result_u; } } } @@ -566,7 +562,6 @@ int main(int argc, char *argv[]) // 16. Free the used memory. delete ode_solver; delete pmesh; - delete result_u; MPI_Finalize(); diff --git a/examples/dmd/local_dw_csv.cpp b/examples/dmd/local_dw_csv.cpp index 9a79c6c16..ba9afc30f 100644 --- a/examples/dmd/local_dw_csv.cpp +++ b/examples/dmd/local_dw_csv.cpp @@ -578,7 +578,7 @@ int main(int argc, char *argv[]) for (int k = 0; k < f_snapshots->numColumns(); ++k) { f_snapshots->getColumn(k, interp_snap); - CAROM::Vector* result = admd->predict(t_init+k*dtc); + std::shared_ptr result = admd->predict(t_init+k*dtc); Vector dmd_solution(result->getData(), dim); Vector true_solution(interp_snap.getData(), dim); @@ -600,7 +600,6 @@ int main(int argc, char *argv[]) cout << "Relative error of DMD prediction for interpolated snapshot #" << k << " is " << rel_error << endl; } - delete result; } if (myid == 0) { @@ -677,7 +676,7 @@ int main(int argc, char *argv[]) << " is read." << endl; } - CAROM::Vector* init_cond = nullptr; + std::shared_ptr init_cond; if (min_idx_snap == -1) { double curr_indicator_val = (indicator_idx.size() == 0) ? tval : @@ -697,13 +696,12 @@ int main(int argc, char *argv[]) cout << "Projecting initial condition at t = " << tval << " for DMD model #0." << endl; } - init_cond = new CAROM::Vector(dim, true); + init_cond.reset(new CAROM::Vector(dim, true)); for (int i = 0; i < dim; ++i) { init_cond->item(i) = sample[i]; } - dmd[curr_window]->projectInitialCondition(init_cond, tval); - delete init_cond; + dmd[curr_window]->projectInitialCondition(init_cond.get(), tval); dmd_preprocess_timer.Stop(); } else @@ -723,7 +721,7 @@ int main(int argc, char *argv[]) " using DMD model #" << curr_window << endl; } dmd_prediction_timer.Start(); - CAROM::Vector* result = dmd[curr_window]->predict(tval); + std::shared_ptr result = dmd[curr_window]->predict(tval); dmd_prediction_timer.Stop(); double curr_indicator_val = (indicator_idx.size() == 0) ? tval : result->item( @@ -764,7 +762,6 @@ int main(int argc, char *argv[]) { t_left = t_offset; } - delete init_cond; } t_offset = (t_left + t_right) / 2.0; } @@ -794,10 +791,8 @@ int main(int argc, char *argv[]) } init_cond = dmd[curr_window]->predict(t_offset); - dmd[curr_window+1]->projectInitialCondition(init_cond, t_offset); - delete init_cond; + dmd[curr_window+1]->projectInitialCondition(init_cond.get(), t_offset); - delete result; curr_window += 1; result = dmd[curr_window]->predict(tval); curr_indicator_val = (indicator_idx.size() == 0) ? tval : result->item( @@ -837,7 +832,6 @@ int main(int argc, char *argv[]) } } } - delete result; if (curr_window == numWindows-1 && curr_indicator_val >= indicator_val[numWindows]) diff --git a/examples/dmd/local_tw_csv.cpp b/examples/dmd/local_tw_csv.cpp index d6356f260..c11ed9bb2 100644 --- a/examples/dmd/local_tw_csv.cpp +++ b/examples/dmd/local_tw_csv.cpp @@ -612,7 +612,7 @@ int main(int argc, char *argv[]) for (int k = 0; k < f_snapshots->numColumns(); ++k) { f_snapshots->getColumn(k, interp_snap); - CAROM::Vector* result = admd->predict(t_init+k*dtc); + std::shared_ptr result = admd->predict(t_init+k*dtc); Vector dmd_solution(result->getData(), dim); Vector true_solution(interp_snap.getData(), dim); @@ -634,7 +634,6 @@ int main(int argc, char *argv[]) cout << "Relative error of DMD prediction for interpolated snapshot #" << k << " is " << rel_error << endl; } - delete result; } if (myid == 0) { @@ -762,7 +761,7 @@ int main(int argc, char *argv[]) if (idx_snap == 0) { dmd_preprocess_timer.Start(); - CAROM::Vector* init_cond = nullptr; + std::shared_ptr init_cond; for (int window = 0; window < numWindows; ++window) { if (myid == 0) @@ -772,7 +771,7 @@ int main(int argc, char *argv[]) } if (window == 0) { - init_cond = new CAROM::Vector(dim, true); + init_cond.reset(new CAROM::Vector(dim, true)); for (int i = 0; i < dim; ++i) { init_cond->item(i) = sample[i]; @@ -782,8 +781,7 @@ int main(int argc, char *argv[]) { init_cond = dmd[window-1]->predict(indicator_val[window]); } - dmd[window]->projectInitialCondition(init_cond); - delete init_cond; + dmd[window]->projectInitialCondition(init_cond.get()); } dmd_preprocess_timer.Stop(); } @@ -801,7 +799,7 @@ int main(int argc, char *argv[]) << " using DMD model #" << curr_window << endl; } dmd_prediction_timer.Start(); - CAROM::Vector* result = dmd[curr_window]->predict(t_final); + std::shared_ptr result = dmd[curr_window]->predict(t_final); dmd_prediction_timer.Stop(); if (myid == 0) { @@ -810,7 +808,6 @@ int main(int argc, char *argv[]) result->getData(), dim); } idx_snap = snap_bound[1]+1; // escape for-loop over idx_snap - delete result; } else // Verify DMD prediction results against dataset { @@ -824,7 +821,7 @@ int main(int argc, char *argv[]) << tval << " using DMD model #" << curr_window << endl; } dmd_prediction_timer.Start(); - CAROM::Vector* result = dmd[curr_window]->predict(tval); + std::shared_ptr result = dmd[curr_window]->predict(tval); dmd_prediction_timer.Stop(); // Calculate the relative error between the DMD final solution and the true solution. @@ -860,7 +857,6 @@ int main(int argc, char *argv[]) } } } - delete result; } } if (myid == 0 && t_final <= 0.0) diff --git a/examples/dmd/nonlinear_elasticity.cpp b/examples/dmd/nonlinear_elasticity.cpp index e409c5b9e..1791f42e8 100644 --- a/examples/dmd/nonlinear_elasticity.cpp +++ b/examples/dmd/nonlinear_elasticity.cpp @@ -627,8 +627,8 @@ int main(int argc, char *argv[]) true_solution_v = v_gf.GetTrueVector(); curr_window = 0; - CAROM::Vector* result_x = dmd_x[curr_window]->predict(ts[0]); - CAROM::Vector* result_v = dmd_v[curr_window]->predict(ts[0]); + std::shared_ptr result_x = dmd_x[curr_window]->predict(ts[0]); + std::shared_ptr result_v = dmd_v[curr_window]->predict(ts[0]); Vector initial_dmd_solution_x(result_x->getData(), result_x->dim()); Vector initial_dmd_solution_v(result_v->getData(), result_v->dim()); x_gf.SetFromTrueDofs(initial_dmd_solution_x); @@ -648,14 +648,11 @@ int main(int argc, char *argv[]) dmd_dc->Save(); } - delete result_x; - delete result_v; - for (int i = 1; i < ts.size(); i++) { if (i == ts.size() - 1 || (i % vis_steps) == 0) { - if(visit) + if (visit) { result_x = dmd_x[curr_window]->predict(ts[i]); result_v = dmd_v[curr_window]->predict(ts[i]); @@ -671,9 +668,6 @@ int main(int argc, char *argv[]) dmd_dc->SetTime(ts[i]); dmd_dc->Save(); pmesh->SwapNodes(nodes, owns_nodes); - - delete result_x; - delete result_v; } if (i % windowNumSamples == 0 && i < ts.size()-1) @@ -723,8 +717,6 @@ int main(int argc, char *argv[]) // 15. Free the used memory. delete ode_solver; delete pmesh; - delete result_x; - delete result_v; delete dc; delete dmd_dc; delete dmd_x[curr_window]; diff --git a/examples/dmd/parametric_dw_csv.cpp b/examples/dmd/parametric_dw_csv.cpp index 7ca9ce830..21c993a40 100644 --- a/examples/dmd/parametric_dw_csv.cpp +++ b/examples/dmd/parametric_dw_csv.cpp @@ -55,7 +55,7 @@ using namespace mfem; void getInterpolatedTimeWindows(CAROM::Vector*& testing_twep, std::vector& parameter_points, - std::vector& training_twep, + std::vector>& training_twep, CAROM::Vector* desired_point, std::string rbf, double closest_rbf_val); @@ -268,7 +268,8 @@ int main(int argc, char *argv[]) vector par_dir_list; // DATASET name vector num_train_snap; // DATASET size vector indicator_init, indicator_last; // DATASET indicator range - vector training_twep; // DATASET temporal endpoint + vector> + training_twep; // DATASET temporal endpoint csv_db.getStringVector(string(list_dir) + "/" + train_list + ".csv", training_par_list, false); @@ -534,7 +535,7 @@ int main(int argc, char *argv[]) } CAROM_VERIFY(numWindows == curr_window+1); - training_twep.push_back(twep); + training_twep.push_back(std::shared_ptr(twep)); csv_db.putDoubleArray(outputPath + "/" + par_dir + "_twep.csv", twep->getData(), numWindows+1); @@ -585,7 +586,7 @@ int main(int argc, char *argv[]) CAROM::Vector* twep = new CAROM::Vector(numWindows+1, false); csv_db.getDoubleArray(outputPath + "/" + par_dir + "_twep.csv", twep->getData(), numWindows+1); - training_twep.push_back(twep); + training_twep.push_back(std::shared_ptr(twep)); } par_dir_list.clear(); @@ -722,7 +723,7 @@ int main(int argc, char *argv[]) << " is read." << endl; } - CAROM::Vector* init_cond = nullptr; + std::shared_ptr init_cond; if (min_idx_snap == -1) { if (tval >= twep->item(0)) @@ -742,13 +743,12 @@ int main(int argc, char *argv[]) cout << "Projecting initial condition at t = " << tval << " for DMD model #0." << endl; } - init_cond = new CAROM::Vector(dim, true); + init_cond.reset(new CAROM::Vector(dim, true)); for (int i = 0; i < dim; ++i) { init_cond->item(i) = sample[i]; } - dmd[idx_dataset][curr_window]->projectInitialCondition(init_cond, tval); - delete init_cond; + dmd[idx_dataset][curr_window]->projectInitialCondition(init_cond.get(), tval); dmd_preprocess_timer.Stop(); } else @@ -768,7 +768,8 @@ int main(int argc, char *argv[]) " using DMD model #" << curr_window << endl; } dmd_prediction_timer.Start(); - CAROM::Vector* result = dmd[idx_dataset][curr_window]->predict(tval); + std::shared_ptr result = + dmd[idx_dataset][curr_window]->predict(tval); dmd_prediction_timer.Stop(); while (curr_window+1 < numWindows @@ -806,10 +807,9 @@ int main(int argc, char *argv[]) } init_cond = dmd[idx_dataset][curr_window]->predict(t_offset); - dmd[idx_dataset][curr_window+1]->projectInitialCondition(init_cond, t_offset); - delete init_cond; + dmd[idx_dataset][curr_window+1]->projectInitialCondition(init_cond.get(), + t_offset); - delete result; curr_window += 1; result = dmd[idx_dataset][curr_window]->predict(tval); } @@ -847,7 +847,6 @@ int main(int argc, char *argv[]) } } } - delete result; if (curr_window == numWindows-1 && tval >= twep->item(numWindows)) @@ -893,13 +892,7 @@ int main(int argc, char *argv[]) } } - delete[] sample; - for (int idx_dataset = 0; idx_dataset < training_twep.size(); ++idx_dataset) - { - delete training_twep[idx_dataset]; - } - for (int idx_dataset = 0; idx_dataset < npar; ++idx_dataset) { delete testing_par_vectors[idx_dataset]; @@ -914,7 +907,7 @@ int main(int argc, char *argv[]) void getInterpolatedTimeWindows(CAROM::Vector*& testing_twep, std::vector& parameter_points, - std::vector& training_twep, + std::vector>& training_twep, CAROM::Vector* desired_point, std::string rbf = "G", double closest_rbf_val = 0.9) diff --git a/examples/dmd/parametric_heat_conduction.cpp b/examples/dmd/parametric_heat_conduction.cpp index e50d329c3..2c5ff2412 100644 --- a/examples/dmd/parametric_heat_conduction.cpp +++ b/examples/dmd/parametric_heat_conduction.cpp @@ -796,7 +796,7 @@ int main(int argc, char *argv[]) std::cout << "Predicting temperature using DMD at: " << ts[0] << std::endl; } - CAROM::Vector* result_u = dmd_u->predict(ts[0]); + std::shared_ptr result_u = dmd_u->predict(ts[0]); Vector initial_dmd_solution_u(result_u->getData(), result_u->dim()); u_gf.SetFromTrueDofs(initial_dmd_solution_u); @@ -812,8 +812,6 @@ int main(int argc, char *argv[]) dmd_visit_dc.Save(); } - delete result_u; - if (visit) { for (int i = 1; i < ts.size(); i++) @@ -832,8 +830,6 @@ int main(int argc, char *argv[]) dmd_visit_dc.SetCycle(i); dmd_visit_dc.SetTime(ts[i]); dmd_visit_dc.Save(); - - delete result_u; } } } @@ -858,8 +854,6 @@ int main(int argc, char *argv[]) printf("Elapsed time for predicting DMD: %e second\n", dmd_prediction_timer.RealTime()); } - - delete result_u; } if (myid == 0) diff --git a/examples/dmd/parametric_tw_csv.cpp b/examples/dmd/parametric_tw_csv.cpp index 1b132f8d9..babcf8987 100644 --- a/examples/dmd/parametric_tw_csv.cpp +++ b/examples/dmd/parametric_tw_csv.cpp @@ -761,10 +761,9 @@ int main(int argc, char *argv[]) << indicator_val[window] + offset_indicator * tvec[snap_bound[0]] << " for DMD model #" << window << endl; } - CAROM::Vector* init_cond = dmd[idx_dataset][window-1]->predict( - indicator_val[window]); - dmd[idx_dataset][window]->projectInitialCondition(init_cond); - delete init_cond; + std::shared_ptr init_cond = dmd[idx_dataset][window-1]->predict( + indicator_val[window]); + dmd[idx_dataset][window]->projectInitialCondition(init_cond.get()); } // Make a directory for this window, only on the first parameter. @@ -978,10 +977,10 @@ int main(int argc, char *argv[]) << indicator_val[window] + offset_indicator * tvec[snap_bound[0]] << " for DMD model #" << window << endl; } - CAROM::Vector* init_cond = nullptr; + std::shared_ptr init_cond; if (window == 0) { - init_cond = new CAROM::Vector(dim, true); + init_cond.reset(new CAROM::Vector(dim, true)); for (int i = 0; i < dim; ++i) { init_cond->item(i) = sample[i]; @@ -991,7 +990,7 @@ int main(int argc, char *argv[]) { init_cond = dmd[idx_dataset][window-1]->predict(indicator_val[window]); } - dmd[idx_dataset][window]->projectInitialCondition(init_cond); + dmd[idx_dataset][window]->projectInitialCondition(init_cond.get()); const double norm_init_cond = init_cond->norm(); if (myid == 0) @@ -1001,8 +1000,6 @@ int main(int argc, char *argv[]) << window - 1 << endl; } - delete init_cond; - if (window > 0 && indicator_val[window] < t_final) { // To save memory, delete dmd[idx_dataset][window] for windows @@ -1142,8 +1139,8 @@ int main(int argc, char *argv[]) } dmd_prediction_timer.Start(); - CAROM::Vector* result = dmd[idx_dataset][curr_window]->predict( - t_final - offset_indicator * tvec[snap_bound[0]]); + std::shared_ptr result = dmd[idx_dataset][curr_window]->predict( + t_final - offset_indicator * tvec[snap_bound[0]]); dmd_prediction_timer.Stop(); if (myid == 0) @@ -1159,7 +1156,6 @@ int main(int argc, char *argv[]) } idx_snap = snap_bound[1]+1; // escape for-loop over idx_snap - delete result; } else // Verify DMD prediction results against dataset { @@ -1177,8 +1173,9 @@ int main(int argc, char *argv[]) } dmd_prediction_timer.Start(); - CAROM::Vector* result = dmd[idx_dataset][curr_window]->predict( - tval - offset_indicator * tvec[snap_bound[0]]); + std::shared_ptr result = + dmd[idx_dataset][curr_window]->predict( + tval - offset_indicator * tvec[snap_bound[0]]); dmd_prediction_timer.Stop(); // Calculate the relative error between the DMD final solution and the true solution. @@ -1227,8 +1224,6 @@ int main(int argc, char *argv[]) { hdf_db->putDoubleArray(snap, result->getData(), dim); } - - delete result; } } if (myid == 0 && t_final <= 0.0) diff --git a/examples/dmd/wave_equation.cpp b/examples/dmd/wave_equation.cpp index d90316863..a05bf9205 100644 --- a/examples/dmd/wave_equation.cpp +++ b/examples/dmd/wave_equation.cpp @@ -597,7 +597,7 @@ int main(int argc, char *argv[]) // 10. Predict using DMD. cout << "Predicting temperature using DMD" << endl; - CAROM::Vector* result_u = nullptr; + std::shared_ptr result_u; VisItDataCollection dmd_visit_dc(io_dir+"/DMD_Wave_Equation", mesh); dmd_visit_dc.RegisterField("solution", &u_gf); curr_window = 0; @@ -608,7 +608,6 @@ int main(int argc, char *argv[]) dmd_visit_dc.SetCycle(0); dmd_visit_dc.SetTime(0.0); dmd_visit_dc.Save(); - delete result_u; } for (int i = 1; i < ts.size(); i++) @@ -623,7 +622,6 @@ int main(int argc, char *argv[]) dmd_visit_dc.SetCycle(i); dmd_visit_dc.SetTime(ts[i]); dmd_visit_dc.Save(); - delete result_u; } if (i % windowNumSamples == 0 && i < ts.size()-1) @@ -632,7 +630,7 @@ int main(int argc, char *argv[]) { result_u = dmd_u[curr_window]->predict(ts[i]); cout << "Projecting solution for new window at " << ts[i] << endl; - dmd_u[curr_window+1]->projectInitialCondition(result_u, ts[i]); + dmd_u[curr_window+1]->projectInitialCondition(result_u.get(), ts[i]); } delete dmd_u[curr_window]; curr_window++; @@ -662,7 +660,6 @@ int main(int argc, char *argv[]) // 12. Free the used memory. delete ode_solver; delete mesh; - delete result_u; delete dmd_u[curr_window]; return 0; } diff --git a/lib/algo/AdaptiveDMD.cpp b/lib/algo/AdaptiveDMD.cpp index a2d765019..7619ca1b7 100644 --- a/lib/algo/AdaptiveDMD.cpp +++ b/lib/algo/AdaptiveDMD.cpp @@ -36,14 +36,6 @@ AdaptiveDMD::AdaptiveDMD(int dim, double desired_dt, std::string rbf, d_closest_rbf_val = closest_rbf_val; } -AdaptiveDMD::~AdaptiveDMD() -{ - for (auto interp_snapshot : d_interp_snapshots) - { - delete interp_snapshot; - } -} - void AdaptiveDMD::train(double energy_fraction, const Matrix* W0, double linearity_tol) { @@ -139,7 +131,8 @@ void AdaptiveDMD::interpolateSnapshots() // Obtain the interpolated snapshot. CAROM::Vector* curr_interpolated_snapshot = obtainInterpolatedVector( d_snapshots, f_T, d_interp_method, rbf); - d_interp_snapshots.push_back(curr_interpolated_snapshot); + d_interp_snapshots.push_back(std::shared_ptr + (curr_interpolated_snapshot)); delete point; } diff --git a/lib/algo/AdaptiveDMD.h b/lib/algo/AdaptiveDMD.h index 1a7598928..b7166a476 100644 --- a/lib/algo/AdaptiveDMD.h +++ b/lib/algo/AdaptiveDMD.h @@ -62,7 +62,7 @@ class AdaptiveDMD : public DMD /** * @brief Destroy the AdaptiveDMD object */ - ~AdaptiveDMD(); + ~AdaptiveDMD() { } /** * @param[in] energy_fraction The energy fraction to keep after doing SVD. @@ -126,7 +126,7 @@ class AdaptiveDMD : public DMD /** * @brief std::vector holding the interpolated snapshots. */ - std::vector d_interp_snapshots; + std::vector> d_interp_snapshots; /** * @brief Internal function to obtain the interpolated snapshots. diff --git a/lib/algo/DMD.cpp b/lib/algo/DMD.cpp index 2ea11e77e..ad025b3b0 100644 --- a/lib/algo/DMD.cpp +++ b/lib/algo/DMD.cpp @@ -129,10 +129,6 @@ DMD::DMD(std::vector> eigs, Matrix* phi_real, DMD::~DMD() { - for (auto snapshot : d_snapshots) - { - delete snapshot; - } for (auto sampled_time : d_sampled_times) { delete sampled_time; @@ -144,7 +140,6 @@ DMD::~DMD() delete d_phi_imaginary; delete d_phi_real_squared_inverse; delete d_phi_imaginary_squared_inverse; - delete d_projected_init_imaginary; } void DMD::setOffset(Vector* offset_vector, int order) @@ -177,8 +172,6 @@ void DMD::takeSample(double* u_in, double t) { if (d_rank == 0) std::cout << "Removing existing snapshot at time: " << d_t_offset + d_sampled_times.back()->item(0) << std::endl; - Vector* last_snapshot = d_snapshots.back(); - delete last_snapshot; d_snapshots.pop_back(); d_sampled_times.pop_back(); } @@ -193,7 +186,7 @@ void DMD::takeSample(double* u_in, double t) CAROM_VERIFY(d_sampled_times.back()->item(0) < t); } - d_snapshots.push_back(sample); + d_snapshots.push_back(std::shared_ptr(sample)); Vector* sampled_time = new Vector(&t, 1, false); d_sampled_times.push_back(sampled_time); @@ -616,7 +609,7 @@ DMD::projectInitialCondition(const Vector* init, double t_offset) Vector* d_projected_init_imaginary_2 = d_phi_imaginary_squared_inverse->mult( *rhs_real); d_projected_init_imaginary = d_projected_init_imaginary_2->minus( - d_projected_init_imaginary_1); + *d_projected_init_imaginary_1); delete d_phi_real_squared_2; delete d_projected_init_real_1; @@ -640,7 +633,7 @@ DMD::projectInitialCondition(const Vector* init, double t_offset) d_init_projected = true; } -Vector* +std::shared_ptr DMD::predict(double t, int deg) { CAROM_VERIFY(d_trained); @@ -658,9 +651,10 @@ DMD::predict(double t, int deg) Vector* d_predicted_state_real_2 = d_phi_mult_eigs_imaginary->mult( *d_projected_init_imaginary); - Vector* d_predicted_state_real = d_predicted_state_real_1->minus( - d_predicted_state_real_2); - addOffset(d_predicted_state_real, t, deg); + std::shared_ptr d_predicted_state_real = + d_predicted_state_real_1->minus( + *d_predicted_state_real_2); + addOffset(*d_predicted_state_real, t, deg); delete d_phi_mult_eigs_real; delete d_phi_mult_eigs_imaginary; @@ -671,11 +665,11 @@ DMD::predict(double t, int deg) } void -DMD::addOffset(Vector*& result, double t, int deg) +DMD::addOffset(Vector & result, double t, int deg) { if (d_state_offset) { - *result += *d_state_offset; + result += *d_state_offset; } } @@ -731,7 +725,8 @@ DMD::getSnapshotMatrix() } const Matrix* -DMD::createSnapshotMatrix(std::vector snapshots) +DMD::createSnapshotMatrix(const std::vector> & + snapshots) { CAROM_VERIFY(snapshots.size() > 0); CAROM_VERIFY(snapshots[0]->dim() > 0); @@ -824,7 +819,7 @@ DMD::load(std::string base_file_name) d_projected_init_real->read(full_file_name); full_file_name = base_file_name + "_projected_init_imaginary"; - d_projected_init_imaginary = new Vector(); + d_projected_init_imaginary.reset(new Vector()); d_projected_init_imaginary->read(full_file_name); full_file_name = base_file_name + "_state_offset"; diff --git a/lib/algo/DMD.h b/lib/algo/DMD.h index d5cd7b5f9..e573b54f5 100644 --- a/lib/algo/DMD.h +++ b/lib/algo/DMD.h @@ -151,7 +151,7 @@ class DMD * @param[in] t The time of the output state * @param[in] deg The derivative degree of the output state */ - Vector* predict(double t, int deg = 0); + std::shared_ptr predict(double t, int deg = 0); /** * @brief Get the time offset contained within d_t_offset. @@ -320,12 +320,13 @@ class DMD /** * @brief Add the appropriate offset when predicting the solution. */ - virtual void addOffset(Vector*& result, double t = 0.0, int deg = 0); + virtual void addOffset(Vector & result, double t = 0.0, int deg = 0); /** * @brief Get the snapshot matrix contained within d_snapshots. */ - const Matrix* createSnapshotMatrix(std::vector snapshots); + const Matrix* createSnapshotMatrix(const std::vector> & + snapshots); /** * @brief The rank of the process this object belongs to. @@ -355,7 +356,7 @@ class DMD /** * @brief std::vector holding the snapshots. */ - std::vector d_snapshots; + std::vector> d_snapshots; /** * @brief The stored times of each sample. @@ -440,7 +441,7 @@ class DMD /** * @brief The imaginary part of the projected initial condition. */ - Vector* d_projected_init_imaginary = NULL; + std::shared_ptr d_projected_init_imaginary = NULL; /** * @brief A vector holding the complex eigenvalues of the eigenmodes. diff --git a/lib/algo/DMDc.cpp b/lib/algo/DMDc.cpp index d92da293d..09097e415 100644 --- a/lib/algo/DMDc.cpp +++ b/lib/algo/DMDc.cpp @@ -150,7 +150,6 @@ DMDc::~DMDc() delete d_phi_imaginary; delete d_phi_real_squared_inverse; delete d_phi_imaginary_squared_inverse; - delete d_projected_init_imaginary; } void DMDc::setOffset(Vector* offset_vector) @@ -668,7 +667,7 @@ DMDc::project(const Vector* init, const Matrix* controls, double t_offset) Vector* d_projected_init_imaginary_2 = d_phi_imaginary_squared_inverse->mult( *init_real); d_projected_init_imaginary = d_projected_init_imaginary_2->minus( - d_projected_init_imaginary_1); + *d_projected_init_imaginary_1); delete init_real; delete init_imaginary; @@ -733,9 +732,9 @@ DMDc::predict(double t) Vector* d_predicted_state_real_2 = d_phi_mult_eigs_imaginary->mult( *d_projected_init_imaginary); - Vector* d_predicted_state_real = d_predicted_state_real_1->minus( - d_predicted_state_real_2); - addOffset(d_predicted_state_real); + std::unique_ptr d_predicted_state_real = + d_predicted_state_real_1->minus(*d_predicted_state_real_2); + addOffset(*d_predicted_state_real); delete d_phi_mult_eigs_real; delete d_phi_mult_eigs_imaginary; @@ -768,15 +767,14 @@ DMDc::predict(double t) delete f_control_real; delete f_control_imaginary; - return d_predicted_state_real; } void -DMDc::addOffset(Vector*& result) +DMDc::addOffset(Vector & result) { if (d_state_offset) { - *result += *d_state_offset; + result += *d_state_offset; } } @@ -925,7 +923,7 @@ DMDc::load(std::string base_file_name) d_projected_init_real->read(full_file_name); full_file_name = base_file_name + "_projected_init_imaginary"; - d_projected_init_imaginary = new Vector(); + d_projected_init_imaginary.reset(new Vector()); d_projected_init_imaginary->read(full_file_name); full_file_name = base_file_name + "_state_offset"; diff --git a/lib/algo/DMDc.h b/lib/algo/DMDc.h index 7a8c4e642..6dc604256 100644 --- a/lib/algo/DMDc.h +++ b/lib/algo/DMDc.h @@ -291,7 +291,7 @@ class DMDc /** * @brief Add the state offset when predicting the solution. */ - virtual void addOffset(Vector*& result); + virtual void addOffset(Vector & result); /** * @brief Get the snapshot matrix contained within d_snapshots. @@ -421,7 +421,7 @@ class DMDc /** * @brief The imaginary part of the projected initial condition. */ - Vector* d_projected_init_imaginary = NULL; + std::shared_ptr d_projected_init_imaginary; /** * @brief The real part of the projected controls. diff --git a/lib/algo/NonuniformDMD.cpp b/lib/algo/NonuniformDMD.cpp index 9a0454322..72b25b276 100644 --- a/lib/algo/NonuniformDMD.cpp +++ b/lib/algo/NonuniformDMD.cpp @@ -115,7 +115,7 @@ NonuniformDMD::computeEigExp(std::complex eig, double t) } void -NonuniformDMD::addOffset(Vector*& result, double t, int deg) +NonuniformDMD::addOffset(Vector & result, double t, int deg) { CAROM_VERIFY(deg == 0 || deg == 1); if (deg == 0) @@ -123,14 +123,14 @@ NonuniformDMD::addOffset(Vector*& result, double t, int deg) DMD::addOffset(result); if (d_derivative_offset) { - result->plusAx(t, *d_derivative_offset); + result.plusAx(t, *d_derivative_offset); } } else { if (d_derivative_offset) { - *result += *d_derivative_offset; + result += *d_derivative_offset; } } } diff --git a/lib/algo/NonuniformDMD.h b/lib/algo/NonuniformDMD.h index 50761c6b2..bd56532c9 100644 --- a/lib/algo/NonuniformDMD.h +++ b/lib/algo/NonuniformDMD.h @@ -171,7 +171,7 @@ class NonuniformDMD : public DMD /** * @brief Add the appropriate offset when predicting the solution. */ - void addOffset(Vector*& result, double t, int deg) override; + void addOffset(Vector & result, double t, int deg) override; /** * @brief Derivative offset in snapshot. diff --git a/lib/algo/SnapshotDMD.cpp b/lib/algo/SnapshotDMD.cpp index cc7e78007..b3915061d 100644 --- a/lib/algo/SnapshotDMD.cpp +++ b/lib/algo/SnapshotDMD.cpp @@ -51,7 +51,7 @@ void SnapshotDMD::train(double energy_fraction, const Matrix* W0, void SnapshotDMD::interpolateToNSnapshots(int n) { PCHIPInterpolator* interp = new PCHIPInterpolator(); - std::vector new_snapshots; + std::vector> new_snapshots; std::vector new_times; interp->interpolate(d_sampled_times,d_snapshots,n,new_times,new_snapshots); diff --git a/lib/algo/SnapshotDMD.h b/lib/algo/SnapshotDMD.h index 404147a42..bc010f052 100644 --- a/lib/algo/SnapshotDMD.h +++ b/lib/algo/SnapshotDMD.h @@ -72,10 +72,9 @@ class SnapshotDMD : public DMD /** * @brief Returns a copy of the current snapshot vector "d_snapshots" */ - std::vector getSnapshotVectors() + std::vector> getSnapshotVectors() { - std::vector return_snapshots(d_snapshots); - return return_snapshots; + return d_snapshots; } protected: /** @@ -128,4 +127,4 @@ class SnapshotDMD : public DMD private: }; } -#endif \ No newline at end of file +#endif diff --git a/lib/algo/manifold_interp/PCHIPInterpolator.cpp b/lib/algo/manifold_interp/PCHIPInterpolator.cpp index dc0191a32..8dc07f81d 100644 --- a/lib/algo/manifold_interp/PCHIPInterpolator.cpp +++ b/lib/algo/manifold_interp/PCHIPInterpolator.cpp @@ -33,9 +33,9 @@ using namespace std; namespace CAROM { void PCHIPInterpolator::interpolate(std::vector& snapshot_ts, - std::vector& snapshots, + std::vector>& snapshots, std::vector& output_ts, - std::vector& output_snapshots) + std::vector>& output_snapshots) { CAROM_VERIFY(snapshot_ts.size() == snapshots.size()); CAROM_VERIFY(snapshot_ts.size() > 2); @@ -60,7 +60,7 @@ void PCHIPInterpolator::interpolate(std::vector& snapshot_ts, { Vector* temp_snapshot = new Vector(snapshots[0]->dim(), snapshots[0]->distributed()); - output_snapshots.push_back(temp_snapshot); + output_snapshots.push_back(std::shared_ptr(temp_snapshot)); } for(int i = 0; i < n_dim; ++i) @@ -134,12 +134,11 @@ void PCHIPInterpolator::interpolate(std::vector& snapshot_ts, } } -void PCHIPInterpolator::interpolate(std::vector& - snapshot_ts, - std::vector& snapshots, +void PCHIPInterpolator::interpolate(std::vector& snapshot_ts, + std::vector>& snapshots, int n_out, std::vector& output_ts, - std::vector& output_snapshots) + std::vector>& output_snapshots) { CAROM_VERIFY(snapshot_ts.size() == snapshots.size()); CAROM_VERIFY(snapshot_ts.size() > 0); @@ -219,4 +218,4 @@ int PCHIPInterpolator::sign(double a) const return 0; } -} \ No newline at end of file +} diff --git a/lib/algo/manifold_interp/PCHIPInterpolator.h b/lib/algo/manifold_interp/PCHIPInterpolator.h index 4fd2e7014..e0415d207 100644 --- a/lib/algo/manifold_interp/PCHIPInterpolator.h +++ b/lib/algo/manifold_interp/PCHIPInterpolator.h @@ -3,6 +3,7 @@ #include #include +#include namespace CAROM { @@ -38,9 +39,9 @@ class PCHIPInterpolator * from snapshot_ts */ void interpolate(std::vector& snapshot_ts, - std::vector& snapshots, + std::vector>& snapshots, std::vector& output_ts, - std::vector&output_snapshots); + std::vector>& output_snapshots); /** * @brief Compute new snapshots interpolated from snapshot_ts to @@ -56,10 +57,10 @@ class PCHIPInterpolator * from snapshot_ts */ void interpolate(std::vector& snapshot_ts, - std::vector& snapshots, + std::vector>& snapshots, int n_out, std::vector& output_ts, - std::vector& output_snapshots); + std::vector>& output_snapshots); private: @@ -74,4 +75,4 @@ class PCHIPInterpolator }; } -#endif \ No newline at end of file +#endif diff --git a/lib/algo/manifold_interp/VectorInterpolator.cpp b/lib/algo/manifold_interp/VectorInterpolator.cpp index 7b09dcd62..3b984cb64 100644 --- a/lib/algo/manifold_interp/VectorInterpolator.cpp +++ b/lib/algo/manifold_interp/VectorInterpolator.cpp @@ -84,9 +84,6 @@ VectorInterpolator::~VectorInterpolator() delete d_rotated_reduced_vectors[i]; } } - - for (auto v : d_gammas) - delete v; } void VectorInterpolator::obtainLambda() @@ -116,13 +113,13 @@ std::shared_ptr VectorInterpolator::interpolate(Vector* point) { Vector* gamma = new Vector(d_rotated_reduced_vectors[d_ref_point]->dim(), d_rotated_reduced_vectors[d_ref_point]->distributed()); - d_gammas.push_back(gamma); + d_gammas.push_back(std::shared_ptr(gamma)); } else { // Gamma is Y - X - Vector* gamma = d_rotated_reduced_vectors[i]->minus( - *d_rotated_reduced_vectors[d_ref_point]); + std::shared_ptr gamma = d_rotated_reduced_vectors[i]->minus( + *d_rotated_reduced_vectors[d_ref_point]); d_gammas.push_back(gamma); } } @@ -146,7 +143,8 @@ std::shared_ptr VectorInterpolator::interpolate(Vector* point) return interpolated_vector; } -Vector* obtainInterpolatedVector(std::vector data, Matrix* f_T, +Vector* obtainInterpolatedVector(const std::vector> & + data, Matrix* f_T, std::string interp_method, std::vector& rbf) { Vector* interpolated_vector = new Vector(data[0]->dim(), @@ -187,9 +185,10 @@ Vector* obtainInterpolatedVector(std::vector data, Matrix* f_T, return interpolated_vector; } -Matrix* solveLinearSystem(std::vector parameter_points, - std::vector data, std::string interp_method, - std::string rbf, double& epsilon) +Matrix* solveLinearSystem(const std::vector & parameter_points, + const std::vector> & data, + std::string interp_method, + std::string rbf, double & epsilon) { int mpi_init, rank; MPI_Initialized(&mpi_init); diff --git a/lib/algo/manifold_interp/VectorInterpolator.h b/lib/algo/manifold_interp/VectorInterpolator.h index fb213ff5c..f00c1223f 100644 --- a/lib/algo/manifold_interp/VectorInterpolator.h +++ b/lib/algo/manifold_interp/VectorInterpolator.h @@ -116,16 +116,18 @@ class VectorInterpolator : public Interpolator /** * @brief The reduced elements in tangential space. */ - std::vector d_gammas; + std::vector> d_gammas; }; -Vector* obtainInterpolatedVector(std::vector data, Matrix* f_T, - std::string interp_method, std::vector& rbf); - -Matrix* solveLinearSystem(std::vector parameter_points, - std::vector data, std::string interp_method, - std::string rbf, double& epsilon); +Vector* obtainInterpolatedVector(const std::vector> & + data, + Matrix* f_T, std::string interp_method, + std::vector& rbf); +Matrix* solveLinearSystem(const std::vector & parameter_points, + const std::vector> & data, + std::string interp_method, + std::string rbf, double & epsilon); } #endif diff --git a/lib/linalg/BasisGenerator.cpp b/lib/linalg/BasisGenerator.cpp index fa5cda5f6..acb10c26e 100644 --- a/lib/linalg/BasisGenerator.cpp +++ b/lib/linalg/BasisGenerator.cpp @@ -230,7 +230,7 @@ BasisGenerator::computeNextSampleTime( Vector* basisl = basis->mult(*l); // Compute u - basisl. - Vector* eta = u_vec.minus(basisl); + std::unique_ptr eta = u_vec.minus(*basisl); delete l; delete basisl; @@ -243,7 +243,7 @@ BasisGenerator::computeNextSampleTime( basisl = basis->mult(*l); // Compute rhs - basisl. - Vector* eta_dot = rhs_vec.minus(basisl); + std::unique_ptr eta_dot = rhs_vec.minus(*basisl); delete l; delete basisl; @@ -257,8 +257,6 @@ BasisGenerator::computeNextSampleTime( local_norm = val; } } - delete eta; - delete eta_dot; if (d_num_procs == 1) { global_norm = local_norm; } diff --git a/lib/linalg/Vector.cpp b/lib/linalg/Vector.cpp index 7cb11ff6b..21f3e58fe 100644 --- a/lib/linalg/Vector.cpp +++ b/lib/linalg/Vector.cpp @@ -121,7 +121,7 @@ Vector& Vector::operator += ( const Vector& rhs) { - CAROM_VERIFY(d_dim == rhs.d_dim); + CAROM_ASSERT(d_dim == rhs.d_dim); for(int i=0; idistributed() == distributed()); - CAROM_ASSERT(distributed() == other.distributed()); - CAROM_VERIFY(dim() == other.dim()); - - // If the result has not been allocated then do so. Otherwise size it - // correctly. - if (result == 0) { - result = new Vector(d_dim, d_distributed); - } - else { - result->setSize(d_dim); - } - - // Do the addition. - for (int i = 0; i < d_dim; ++i) { - result->d_vec[i] = d_vec[i] + factor*other.d_vec[i]; - } -} - void Vector::plusAx( double factor, @@ -280,7 +255,7 @@ Vector::plusAx( { CAROM_ASSERT(result.distributed() == distributed()); CAROM_ASSERT(distributed() == other.distributed()); - CAROM_VERIFY(dim() == other.dim()); + CAROM_ASSERT(dim() == other.dim()); // Size result correctly. result.setSize(d_dim); @@ -297,7 +272,7 @@ Vector::plusEqAx( const Vector& other) { CAROM_ASSERT(distributed() == other.distributed()); - CAROM_VERIFY(dim() == other.dim()); + CAROM_ASSERT(dim() == other.dim()); // Do the addition. for (int i = 0; i < d_dim; ++i) { @@ -305,30 +280,6 @@ Vector::plusEqAx( } } -void -Vector::minus( - const Vector& other, - Vector*& result) const -{ - CAROM_ASSERT(result == 0 || result->distributed() == distributed()); - CAROM_ASSERT(distributed() == other.distributed()); - CAROM_VERIFY(dim() == other.dim()); - - // If the result has not been allocated then do so. Otherwise size it - // correctly. - if (result == 0) { - result = new Vector(d_dim, d_distributed); - } - else { - result->setSize(d_dim); - } - - // Do the subtraction. - for (int i = 0; i < d_dim; ++i) { - result->d_vec[i] = d_vec[i] - other.d_vec[i]; - } -} - void Vector::minus( const Vector& other, @@ -336,7 +287,7 @@ Vector::minus( { CAROM_ASSERT(result.distributed() == distributed()); CAROM_ASSERT(distributed() == other.distributed()); - CAROM_VERIFY(dim() == other.dim()); + CAROM_ASSERT(dim() == other.dim()); // Size result correctly. result.setSize(d_dim); @@ -347,28 +298,6 @@ Vector::minus( } } -void -Vector::mult( - double factor, - Vector*& result) const -{ - CAROM_ASSERT(result == 0 || result->distributed() == distributed()); - - // If the result has not been allocated then do so. Otherwise size it - // correctly. - if (result == 0) { - result = new Vector(d_dim, d_distributed); - } - else { - result->setSize(d_dim); - } - - // Do the multiplication. - for (int i = 0; i < d_dim; ++i) { - result->d_vec[i] = factor*d_vec[i]; - } -} - void Vector::mult( double factor, diff --git a/lib/linalg/Vector.h b/lib/linalg/Vector.h index 72d3b6c94..0361aa491 100644 --- a/lib/linalg/Vector.h +++ b/lib/linalg/Vector.h @@ -300,7 +300,7 @@ class Vector normalize(); /** - * @brief Adds other and this and returns the result, reference version. + * @brief Adds other and this and returns the result. * * @pre distributed() == other.distributed() * @pre dim() == other.dim() @@ -335,8 +335,7 @@ class Vector Vector& result) const; /** - * @brief Adds factor*other and this and returns the result, reference - * version. + * @brief Adds factor*other and this and returns the result. * * @pre distributed() == other.distributed() * @pre dim() == other.dim() @@ -346,57 +345,16 @@ class Vector * * @return this + factor*other */ - Vector* - plusAx( - double factor, - const Vector& other) - { - Vector* result = 0; - plusAx(factor, other, result); - return result; - } - - /** - * @brief Adds factor*other and this and returns the result, pointer - * version. - * - * @pre distributed() == other->distributed() - * @pre dim() == other->dim() - * - * @param[in] factor Multiplicative factor applied to other. - * @param[in] other The other summand. - * - * @return this + factor*other - */ - Vector* + std::unique_ptr plusAx( double factor, - const Vector* other) + const Vector& other) const { - CAROM_VERIFY(other != 0); - return plusAx(factor, *other); + Vector *result = new Vector(d_dim, d_distributed); + plusAx(factor, other, *result); + return std::unique_ptr(result); } - /** - * @brief Adds factor*other and this and fills result with the answer. - * - * Result will be allocated if unallocated or resized appropriately if - * already allocated. - * - * @pre result == 0 || result->distributed() == distributed() - * @pre distributed() == other.distributed() - * @pre dim() == other.dim() - * - * @param[in] factor Multiplicative factor applied to other. - * @param[in] other The other summand. - * @param[out] result this + factor*other - */ - void - plusAx( - double factor, - const Vector& other, - Vector*& result) const; - /** * @brief Adds factor*other and this and fills result with the answer. * @@ -417,7 +375,7 @@ class Vector Vector& result) const; /** - * @brief Adds factor*other to this, reference version. + * @brief Adds factor*other to this. * * @pre distributed() == other.distributed() * @pre dim() == other.dim() @@ -431,27 +389,7 @@ class Vector const Vector& other); /** - * @brief Adds factor*other to this, pointer version. - * - * @pre other != 0 - * @pre distributed() == other->distributed() - * @pre dim() == other->dim() - * - * @param[in] factor Multiplicative factor applied to other. - * @param[in] other The other summand. - */ - void - plusEqAx( - double factor, - const Vector* other) - { - CAROM_VERIFY(other != 0); - plusEqAx(factor, *other); - } - - /** - * @brief Subtracts other and this and returns the result, reference - * version. + * @brief Subtracts other and this and returns the result. * * @pre distributed() == other.distributed() * @pre dim() == other.dim() @@ -460,53 +398,15 @@ class Vector * * @return this - other */ - Vector* + std::unique_ptr minus( const Vector& other) const { - Vector* result = 0; - minus(other, result); - return result; - } - - /** - * @brief Subtracts other and this and returns the result, pointer - * version. - * - * @pre other != 0 - * @pre distributed() == other->distributed() - * @pre dim() == other->dim() - * - * @param[in] other The other subtrahand. - * - * @return this - other - */ - Vector* - minus( - const Vector* other) const - { - CAROM_VERIFY(other != 0); - return minus(*other); + Vector *result = new Vector(d_dim, d_distributed); + minus(other, *result); + return std::unique_ptr(result); } - /** - * @brief Subtracts other and this and fills result with the answer. - * - * Result will be allocated if unallocated or resized appropriately if - * already allocated. - * - * @pre result == 0 || result->distributed() == distributed() - * @pre distributed() == other.distributed() - * @pre dim() == other.dim() - * - * @param[in] other The other subtrahend. - * @param[out] result this - other - */ - void - minus( - const Vector& other, - Vector*& result) const; - /** * @brief Subtracts other and this and fills result with the answer. * @@ -532,29 +432,15 @@ class Vector * * @return factor*this */ - Vector* + std::unique_ptr mult( double factor) const { - Vector* result = 0; - mult(factor, result); - return result; + Vector *result = new Vector(d_dim, d_distributed); + mult(factor, *result); + return std::unique_ptr(result); } - /** - * @brief Multiplies this by the supplied constant and fills result with - * the answer. - * - * @pre result == 0 || result->distributed() == distributed() - * - * @param[in] factor Factor to multiply by. - * @param[out] result factor*this - */ - void - mult( - double factor, - Vector*& result) const; - /** * @brief Multiplies this by the supplied constant and fills result with * the answer. diff --git a/lib/linalg/svd/IncrementalSVD.cpp b/lib/linalg/svd/IncrementalSVD.cpp index 556a411aa..cfd505440 100644 --- a/lib/linalg/svd/IncrementalSVD.cpp +++ b/lib/linalg/svd/IncrementalSVD.cpp @@ -293,9 +293,8 @@ IncrementalSVD::buildIncrementalSVD( // Computing as k = sqrt(u.u - 2.0*l.l + basisl.basisl) // results in catastrophic cancellation, and must be avoided. // Instead we compute as k = sqrt((u-basisl).(u-basisl)). - Vector* e_proj = u_vec.minus(basisl); + std::unique_ptr e_proj = u_vec.minus(*basisl); double k = e_proj->inner_product(*e_proj); - delete e_proj; if (k <= 0) { if(d_rank == 0) printf("linearly dependent sample!\n"); @@ -363,15 +362,14 @@ IncrementalSVD::buildIncrementalSVD( // This sample is not linearly dependent. // Compute j - Vector* j = u_vec.minus(basisl); + std::unique_ptr j = u_vec.minus(*basisl); for (int i = 0; i < d_dim; ++i) { j->item(i) /= k; } // addNewSample will assign sigma to d_S hence it should not be // deleted upon return. - addNewSample(j, A, W, sigma); - delete j; + addNewSample(j.get(), A, W, sigma); } delete basisl; delete A; diff --git a/lib/linalg/svd/IncrementalSVDBrand.cpp b/lib/linalg/svd/IncrementalSVDBrand.cpp index 7b683de59..bab939f20 100644 --- a/lib/linalg/svd/IncrementalSVDBrand.cpp +++ b/lib/linalg/svd/IncrementalSVDBrand.cpp @@ -150,12 +150,14 @@ IncrementalSVDBrand::buildIncrementalSVD( Vector e_proj(u, d_dim, true); Vector *tmp = d_U->transposeMult(e_proj); - - e_proj -= *(d_U->mult(*tmp)); // Gram-Schmidt + Vector *tmp2 = d_U->mult(*tmp); + e_proj -= *tmp2; // Gram-Schmidt + delete tmp2; delete tmp; tmp = d_U->transposeMult(e_proj); - e_proj -= *(d_U->mult(*tmp)); // Re-orthogonalization - + tmp2 = d_U->mult(*tmp); + e_proj -= *tmp2; // Re-orthogonalization + delete tmp2; delete tmp; double k = e_proj.inner_product(e_proj); diff --git a/unit_tests/test_DMD.cpp b/unit_tests/test_DMD.cpp index 393538f7d..a2e956018 100644 --- a/unit_tests/test_DMD.cpp +++ b/unit_tests/test_DMD.cpp @@ -74,7 +74,7 @@ TEST(DMDTest, Test_DMD) dmd.takeSample(&sample3[row_offset[d_rank]], 2.0); dmd.train(2); - CAROM::Vector* result = dmd.predict(3.0); + std::shared_ptr result = dmd.predict(3.0); for (int i = 0; i < d_num_rows; i++) { EXPECT_NEAR(result->item(i), prediction_baseline[row_offset[d_rank] + i], 1e-3); @@ -82,11 +82,11 @@ TEST(DMDTest, Test_DMD) dmd.save("test_DMD"); CAROM::DMD dmd_load("test_DMD"); - CAROM::Vector* result_load = dmd_load.predict(3.0); + std::shared_ptr result_load = dmd_load.predict(3.0); for (int i = 0; i < d_num_rows; i++) { - EXPECT_NEAR(result_load->item(i), prediction_baseline[row_offset[d_rank] + i], - 1e-3); + EXPECT_NEAR(result_load->item(i), + prediction_baseline[row_offset[d_rank] + i], 1e-3); } } diff --git a/unit_tests/test_PCHIPInterpolator.cpp b/unit_tests/test_PCHIPInterpolator.cpp index 2d2b2d15d..c9dcf547c 100644 --- a/unit_tests/test_PCHIPInterpolator.cpp +++ b/unit_tests/test_PCHIPInterpolator.cpp @@ -60,8 +60,8 @@ TEST(InterpolationTest,test_accuracy) -0.077580693288345, -0.623537711025344, -0.971758328554163, -0.890773577229575, -0.536572918000435, -0.041614069121016, 0.560852411851254, 0.957953731078007, 0.938810668593539, 0.650287840157117}; - std::vector snapshots; - std::vector out_snapshots; + std::vector> snapshots; + std::vector> out_snapshots; std::vector reference_snapshots; std::vector times; std::vector out_times; @@ -73,7 +73,7 @@ TEST(InterpolationTest,test_accuracy) CAROM::Vector* temp_y = new CAROM::Vector(2,false); temp_y->item(0) = y[i]; temp_y->item(1) = y2[i]; - snapshots.push_back(temp_y); + snapshots.push_back(std::shared_ptr(temp_y)); } for(int i = 0; i < tq.size(); ++i) @@ -89,7 +89,6 @@ TEST(InterpolationTest,test_accuracy) CAROM::PCHIPInterpolator* interp = new CAROM::PCHIPInterpolator(); - interp->interpolate(times,snapshots,out_times,out_snapshots); for(int i = 0; i < out_snapshots.size(); ++i) diff --git a/unit_tests/test_Vector.cpp b/unit_tests/test_Vector.cpp index 2c4d5a688..b480f8c5b 100644 --- a/unit_tests/test_Vector.cpp +++ b/unit_tests/test_Vector.cpp @@ -474,7 +474,7 @@ TEST(VectorSerialTest, Test_plus_const_reference_reference) TODO(oxberry1@llnl.gov): Test with double argument set to something other than 1. */ -TEST(VectorSerialTest, Test_plusAx_const_reference) +TEST(VectorSerialTest, Test_plusAx_const_pointer) { CAROM::Vector v(2, false); v(0) = 1; @@ -489,7 +489,7 @@ TEST(VectorSerialTest, Test_plusAx_const_reference) y(0) = 5; y(1) = 12; - CAROM::Vector *result; + std::unique_ptr result; /* ( 1, 1) + ( 1, 1) = ( 2, 2) */ result = v.plusAx(1.0, v); @@ -497,8 +497,6 @@ TEST(VectorSerialTest, Test_plusAx_const_reference) EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 2); EXPECT_DOUBLE_EQ((*result)(1), 2); - delete result; - result = NULL; /* ( 1, 1) + (-1, 1) = ( 0, 2) */ result = v.plusAx(1.0, w); @@ -506,8 +504,6 @@ TEST(VectorSerialTest, Test_plusAx_const_reference) EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 0); EXPECT_DOUBLE_EQ((*result)(1), 2); - delete result; - result = NULL; /* ( 1, 1) + ( 3, 4) = ( 4, 5) */ result = v.plusAx(1.0, x); @@ -515,8 +511,6 @@ TEST(VectorSerialTest, Test_plusAx_const_reference) EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 4); EXPECT_DOUBLE_EQ((*result)(1), 5); - delete result; - result = NULL; /* ( 1, 1) + ( 5, 12) = ( 6, 13) */ result = v.plusAx(1.0, y); @@ -524,136 +518,6 @@ TEST(VectorSerialTest, Test_plusAx_const_reference) EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 6); EXPECT_DOUBLE_EQ((*result)(1), 13); - delete result; - result = NULL; -} - -/* - TODO(oxberry1@llnl.gov): Test with double argument set to - something other than 1. -*/ -TEST(VectorSerialTest, Test_plusAx_const_pointer) -{ - CAROM::Vector *v = new CAROM::Vector(2, false); - (*v)(0) = 1; - (*v)(1) = 1; - CAROM::Vector *w = new CAROM::Vector(2, false); - (*w)(0) = -1; - (*w)(1) = 1; - CAROM::Vector *x = new CAROM::Vector(2, false); - (*x)(0) = 3; - (*x)(1) = 4; - CAROM::Vector *y = new CAROM::Vector(2, false); - (*y)(0) = 5; - (*y)(1) = 12; - - CAROM::Vector *result; - - /* ( 1, 1) + ( 1, 1) = ( 2, 2) */ - result = v->plusAx(1.0, v); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 2); - EXPECT_DOUBLE_EQ((*result)(1), 2); - delete result; - result = NULL; - - /* ( 1, 1) + (-1, 1) = ( 0, 2) */ - result = v->plusAx(1.0, w); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 0); - EXPECT_DOUBLE_EQ((*result)(1), 2); - delete result; - result = NULL; - - /* ( 1, 1) + ( 3, 4) = ( 4, 5) */ - result = v->plusAx(1.0, x); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 4); - EXPECT_DOUBLE_EQ((*result)(1), 5); - delete result; - result = NULL; - - /* ( 1, 1) + ( 5, 12) = ( 6, 13) */ - result = v->plusAx(1.0, y); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 6); - EXPECT_DOUBLE_EQ((*result)(1), 13); - delete result; - result = NULL; - - delete v; - delete w; - delete x; - delete y; -} - -/* - TODO(oxberry1@llnl.gov): Test with double argument set to - something other than 1. -*/ -/* TODO(oxberry1@llnl.gov): Test cases where pointer already allocated */ -TEST(VectorSerialTest, Test_plusAx_const_reference_pointer) -{ - CAROM::Vector v(2, false); - v(0) = 1; - v(1) = 1; - CAROM::Vector w(2, false); - w(0) = -1; - w(1) = 1; - CAROM::Vector x(2, false); - x(0) = 3; - x(1) = 4; - CAROM::Vector y(2, false); - y(0) = 5; - y(1) = 12; - - /* - NOTE(oxberry1@llnl.gov): if assignment omitted, pointer has - indeterminate value that is probably non-NULL, so - CAROM::Vector::plus tries to assign to that memory, resulting in a - segfault. - */ - CAROM::Vector *result = NULL; - - /* ( 1, 1) + ( 1, 1) = ( 2, 2) */ - v.plusAx(1.0, v, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 2); - EXPECT_DOUBLE_EQ((*result)(1), 2); - delete result; - result = NULL; - - /* ( 1, 1) + (-1, 1) = ( 0, 2) */ - v.plusAx(1.0, w, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 0); - EXPECT_DOUBLE_EQ((*result)(1), 2); - delete result; - result = NULL; - - /* ( 1, 1) + ( 3, 4) = ( 4, 5) */ - v.plusAx(1.0, x, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 4); - EXPECT_DOUBLE_EQ((*result)(1), 5); - delete result; - result = NULL; - - /* ( 1, 1) + ( 5, 12) = ( 6, 13) */ - v.plusAx(1.0, y, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 6); - EXPECT_DOUBLE_EQ((*result)(1), 13); - delete result; - result = NULL; } /* @@ -663,7 +527,7 @@ TEST(VectorSerialTest, Test_plusAx_const_reference_pointer) /* TODO(oxberry1@llnl.gov): Test cases where output vector must be resized. */ -TEST(VectorSerialTest, Test_plusAx_const_reference_reference) +TEST(VectorSerialTest, Test_plusAx_const_reference) { CAROM::Vector v(2, false); v(0) = 1; @@ -713,7 +577,7 @@ TEST(VectorSerialTest, Test_plusAx_const_reference_reference) TODO(oxberry1@llnl.gov): Test with double argument set to something other than 1. */ -TEST(VectorSerialTest, Test_plusEqAx_const_reference) +TEST(VectorSerialTest, Test_plusEqAx_const) { CAROM::Vector v(2, false); v(0) = 1; @@ -763,66 +627,7 @@ TEST(VectorSerialTest, Test_plusEqAx_const_reference) EXPECT_DOUBLE_EQ(v(1), 13); } -/* - TODO(oxberry1@llnl.gov): Test with double argument set to - something other than 1. -*/ -TEST(VectorSerialTest, Test_plusEqAx_const_pointer) -{ - CAROM::Vector *v = new CAROM::Vector(2, false); - (*v)(0) = 1; - (*v)(1) = 1; - CAROM::Vector *w = new CAROM::Vector(2, false); - (*w)(0) = -1; - (*w)(1) = 1; - CAROM::Vector *x = new CAROM::Vector(2, false); - (*x)(0) = 3; - (*x)(1) = 4; - CAROM::Vector *y = new CAROM::Vector(2, false); - (*y)(0) = 5; - (*y)(1) = 12; - - /* ( 1, 1) + ( 1, 1) = ( 2, 2) */ - v->plusEqAx(1.0, v); - EXPECT_FALSE(v->distributed()); - EXPECT_EQ(v->dim(), 2); - EXPECT_DOUBLE_EQ((*v)(0), 2); - EXPECT_DOUBLE_EQ((*v)(1), 2); - - /* ( 1, 1) + (-1, 1) = ( 0, 2) */ - (*v)(0) = 1; - (*v)(1) = 1; - v->plusEqAx(1.0, w); - EXPECT_FALSE(v->distributed()); - EXPECT_EQ(v->dim(), 2); - EXPECT_DOUBLE_EQ((*v)(0), 0); - EXPECT_DOUBLE_EQ((*v)(1), 2); - - /* ( 1, 1) + ( 3, 4) = ( 4, 5) */ - (*v)(0) = 1; - (*v)(1) = 1; - v->plusEqAx(1.0, x); - EXPECT_FALSE(v->distributed()); - EXPECT_EQ(v->dim(), 2); - EXPECT_DOUBLE_EQ((*v)(0), 4); - EXPECT_DOUBLE_EQ((*v)(1), 5); - - /* ( 1, 1) + ( 5, 12) = ( 6, 13) */ - (*v)(0) = 1; - (*v)(1) = 1; - v->plusEqAx(1.0, y); - EXPECT_FALSE(v->distributed()); - EXPECT_EQ(v->dim(), 2); - EXPECT_DOUBLE_EQ((*v)(0), 6); - EXPECT_DOUBLE_EQ((*v)(1), 13); - - delete v; - delete w; - delete x; - delete y; -} - -TEST(VectorSerialTest, Test_minus_const_reference) +TEST(VectorSerialTest, Test_minus_const_pointer) { CAROM::Vector v(2, false); v(0) = 1; @@ -837,7 +642,7 @@ TEST(VectorSerialTest, Test_minus_const_reference) y(0) = 5; y(1) = 12; - CAROM::Vector *result; + std::unique_ptr result; /* ( 1, 1) - ( 1, 1) = ( 0, 0) */ result = v.minus(v); @@ -845,8 +650,6 @@ TEST(VectorSerialTest, Test_minus_const_reference) EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 0); EXPECT_DOUBLE_EQ((*result)(1), 0); - delete result; - result = NULL; /* ( 1, 1) - (-1, 1) = ( 2, 0) */ result = v.minus(w); @@ -854,8 +657,6 @@ TEST(VectorSerialTest, Test_minus_const_reference) EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 2); EXPECT_DOUBLE_EQ((*result)(1), 0); - delete result; - result = NULL; /* ( 1, 1) - ( 3, 4) = (-2, -3) */ result = v.minus(x); @@ -863,8 +664,6 @@ TEST(VectorSerialTest, Test_minus_const_reference) EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), -2); EXPECT_DOUBLE_EQ((*result)(1), -3); - delete result; - result = NULL; /* ( 1, 1) - ( 5, 12) = (-4, -11) */ result = v.minus(y); @@ -872,134 +671,12 @@ TEST(VectorSerialTest, Test_minus_const_reference) EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), -4); EXPECT_DOUBLE_EQ((*result)(1),-11); - delete result; - result = NULL; -} - -TEST(VectorSerialTest, Test_minus_const_pointer) -{ - CAROM::Vector *v = new CAROM::Vector(2, false); - (*v)(0) = 1; - (*v)(1) = 1; - CAROM::Vector *w = new CAROM::Vector(2, false); - (*w)(0) = -1; - (*w)(1) = 1; - CAROM::Vector *x = new CAROM::Vector(2, false); - (*x)(0) = 3; - (*x)(1) = 4; - CAROM::Vector *y = new CAROM::Vector(2, false); - (*y)(0) = 5; - (*y)(1) = 12; - - CAROM::Vector *result; - - /* ( 1, 1) - ( 1, 1) = ( 0, 0) */ - result = v->minus(v); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 0); - EXPECT_DOUBLE_EQ((*result)(1), 0); - delete result; - result = NULL; - - /* ( 1, 1) - (-1, 1) = ( 2, 0) */ - result = v->minus(w); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 2); - EXPECT_DOUBLE_EQ((*result)(1), 0); - delete result; - result = NULL; - - /* ( 1, 1) - ( 3, 4) = (-2, -3) */ - result = v->minus(x); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), -2); - EXPECT_DOUBLE_EQ((*result)(1), -3); - delete result; - result = NULL; - - /* ( 1, 1) - ( 5, 12) = (-4, -11) */ - result = v->minus(y); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), -4); - EXPECT_DOUBLE_EQ((*result)(1),-11); - delete result; - result = NULL; - - delete v; - delete w; - delete x; - delete y; -} - -/* TODO(oxberry1@llnl.gov): Test cases where pointer already allocated */ -TEST(VectorSerialTest, Test_minus_const_reference_pointer) -{ - CAROM::Vector v(2, false); - v(0) = 1; - v(1) = 1; - CAROM::Vector w(2, false); - w(0) = -1; - w(1) = 1; - CAROM::Vector x(2, false); - x(0) = 3; - x(1) = 4; - CAROM::Vector y(2, false); - y(0) = 5; - y(1) = 12; - - /* - NOTE(oxberry1@llnl.gov): if assignment omitted, pointer has - indeterminate value that is probably non-NULL, so - CAROM::Vector::minus tries to assign to that memory, resulting in a - segfault. - */ - CAROM::Vector *result = NULL; - - /* ( 1, 1) - ( 1, 1) = ( 0, 0) */ - v.minus(v, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 0); - EXPECT_DOUBLE_EQ((*result)(1), 0); - delete result; - result = NULL; - - /* ( 1, 1) - (-1, 1) = ( 2, 0) */ - v.minus(w, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 2); - EXPECT_DOUBLE_EQ((*result)(1), 0); - delete result; - result = NULL; - - /* ( 1, 1) - ( 3, 4) = (-2, -3) */ - v.minus(x, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), -2); - EXPECT_DOUBLE_EQ((*result)(1), -3); - delete result; - result = NULL; - - /* ( 1, 1) - ( 5, 12) = (-4, -11) */ - v.minus(y, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), -4); - EXPECT_DOUBLE_EQ((*result)(1),-11); - delete result; - result = NULL; } /* TODO(oxberry1@llnl.gov): Test cases where output vector must be resized. */ -TEST(VectorSerialTest, Test_minus_const_reference_reference) +TEST(VectorSerialTest, Test_minus_const_reference) { CAROM::Vector v(2, false); v(0) = 1; @@ -1060,89 +737,31 @@ TEST(VectorSerialTest, Test_mult_double) y(0) = 5; y(1) = 12; - CAROM::Vector *result; + std::unique_ptr result; result = v.mult(2); EXPECT_FALSE(result->distributed()); EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 2); EXPECT_DOUBLE_EQ((*result)(1), 2); - delete result; - result = NULL; result = w.mult(-5); EXPECT_FALSE(result->distributed()); EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 5); EXPECT_DOUBLE_EQ((*result)(1), -5); - delete result; - result = NULL; result = x.mult(3); EXPECT_FALSE(result->distributed()); EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 9); EXPECT_DOUBLE_EQ((*result)(1), 12); - delete result; - result = NULL; result = y.mult(0.5); EXPECT_FALSE(result->distributed()); EXPECT_EQ(result->dim(), 2); EXPECT_DOUBLE_EQ((*result)(0), 2.5); EXPECT_DOUBLE_EQ((*result)(1), 6); - delete result; - result = NULL; -} - -TEST(VectorSerialTest, Test_mult_double_pointer) -{ - CAROM::Vector v(2, false); - v(0) = 1; - v(1) = 1; - CAROM::Vector w(2, false); - w(0) = -1; - w(1) = 1; - CAROM::Vector x(2, false); - x(0) = 3; - x(1) = 4; - CAROM::Vector y(2, false); - y(0) = 5; - y(1) = 12; - - CAROM::Vector *result = NULL; - - v.mult(2, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 2); - EXPECT_DOUBLE_EQ((*result)(1), 2); - delete result; - result = NULL; - - w.mult(-5, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 5); - EXPECT_DOUBLE_EQ((*result)(1), -5); - delete result; - result = NULL; - - x.mult(3, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 9); - EXPECT_DOUBLE_EQ((*result)(1), 12); - delete result; - result = NULL; - - y.mult(0.5, result); - EXPECT_FALSE(result->distributed()); - EXPECT_EQ(result->dim(), 2); - EXPECT_DOUBLE_EQ((*result)(0), 2.5); - EXPECT_DOUBLE_EQ((*result)(1), 6); - delete result; - result = NULL; } TEST(VectorSerialTest, Test_mult_double_reference)