From 993782c6f5e42b9b1515ad1f6ef660cdb5a7a96d Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Sun, 15 Dec 2024 01:14:06 -0500 Subject: [PATCH] Check if parameters exist before setting/getting them. --- resolve/LinSolver.cpp | 9 ++++++++ resolve/LinSolver.hpp | 4 +++- resolve/LinSolverIterativeFGMRES.cpp | 27 ++++++++++++------------ resolve/LinSolverIterativeRandFGMRES.cpp | 27 ++++++++++++------------ 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/resolve/LinSolver.cpp b/resolve/LinSolver.cpp index ccb5e72d..d8b11fd1 100644 --- a/resolve/LinSolver.cpp +++ b/resolve/LinSolver.cpp @@ -31,6 +31,15 @@ namespace ReSolve return 1.0; } + int LinSolver::getParamId(std::string id) + { + auto it = params_list_.find(id); + if (it == params_list_.end()) { + out::error() << "Unknown parameter " << id << ".\n"; + return 999; + } + return (*it).second; + } } diff --git a/resolve/LinSolver.hpp b/resolve/LinSolver.hpp index 31c53fa2..2d536985 100644 --- a/resolve/LinSolver.hpp +++ b/resolve/LinSolver.hpp @@ -79,7 +79,9 @@ namespace ReSolve return 1; } - protected: + protected: + int getParamId(std::string id); + matrix::Sparse* A_{nullptr}; MatrixHandler* matrix_handler_{nullptr}; diff --git a/resolve/LinSolverIterativeFGMRES.cpp b/resolve/LinSolverIterativeFGMRES.cpp index b71a0786..6318aa11 100644 --- a/resolve/LinSolverIterativeFGMRES.cpp +++ b/resolve/LinSolverIterativeFGMRES.cpp @@ -383,7 +383,7 @@ namespace ReSolve int LinSolverIterativeFGMRES::setCliParam(const std::string id, const std::string value) { - switch (params_list_[id]) + switch (getParamId(id)) { case TOL: setTol(atof(value.c_str())); @@ -398,19 +398,20 @@ namespace ReSolve setConvCond(atoi(value.c_str())); break; case FLEXIBLE: - bool is_flexible = (value == "yes"); - setFlexible(is_flexible); + setFlexible(value == "yes"); break; + default: + std::cout << "Setting parameter failed!\n"; } return 0; } int LinSolverIterativeFGMRES::getCliParam(const std::string id, std::string& /* value */) { - switch (params_list_[id]) + switch (getParamId(id)) { default: - out::error() << "Unknown string parameter " << id << "\n"; + out::error() << "Trying to get unknown string parameter " << id << "\n"; return 1; } return 0; @@ -418,7 +419,7 @@ namespace ReSolve int LinSolverIterativeFGMRES::getCliParam(const std::string id, index_type& value) { - switch (params_list_[id]) + switch (getParamId(id)) { case MAXIT: value = getMaxit(); @@ -430,7 +431,7 @@ namespace ReSolve value = getConvCond(); break; default: - out::error() << "Unknown integer parameter " << id << "\n"; + out::error() << "Trying to get unknown integer parameter " << id << "\n"; return 1; } return 0; @@ -438,13 +439,13 @@ namespace ReSolve int LinSolverIterativeFGMRES::getCliParam(const std::string id, real_type& value) { - switch (params_list_[id]) + switch (getParamId(id)) { case TOL: value = getTol(); break; default: - out::error() << "Unknown real parameter " << id << "\n"; + out::error() << "Trying to get unknown real parameter " << id << "\n"; return 1; } return 0; @@ -452,13 +453,13 @@ namespace ReSolve int LinSolverIterativeFGMRES::getCliParam(const std::string id, bool& value) { - switch (params_list_[id]) + switch (getParamId(id)) { case FLEXIBLE: value = getFlexible(); break; default: - out::error() << "Unknown boolean parameter " << id << "\n"; + out::error() << "Trying to get unknown boolean parameter " << id << "\n"; return 1; } return 0; @@ -466,7 +467,7 @@ namespace ReSolve int LinSolverIterativeFGMRES::printCliParam(const std::string id) { - switch (params_list_[id]) + switch (getParamId(id)) { case TOL: std::cout << getTol() << "\n"; @@ -484,7 +485,7 @@ namespace ReSolve std::cout << getFlexible() << "\n"; break; default: - out::error() << "Unknown parameter " << id << "\n"; + out::error() << "Trying to print unknown parameter " << id << "\n"; return 1; } return 0; diff --git a/resolve/LinSolverIterativeRandFGMRES.cpp b/resolve/LinSolverIterativeRandFGMRES.cpp index 92798b75..de86f70e 100644 --- a/resolve/LinSolverIterativeRandFGMRES.cpp +++ b/resolve/LinSolverIterativeRandFGMRES.cpp @@ -505,7 +505,7 @@ namespace ReSolve int LinSolverIterativeRandFGMRES::setCliParam(const std::string id, const std::string value) { - switch (params_list_[id]) + switch (getParamId(id)) { case TOL: setTol(atof(value.c_str())); @@ -520,19 +520,20 @@ namespace ReSolve setConvCond(atoi(value.c_str())); break; case FLEXIBLE: - bool is_flexible = (value == "yes"); - setFlexible(is_flexible); + setFlexible(value == "yes"); break; + default: + std::cout << "Setting parameter failed!\n"; } return 0; } int LinSolverIterativeRandFGMRES::getCliParam(const std::string id, std::string& /* value */) { - switch (params_list_[id]) + switch (getParamId(id)) { default: - out::error() << "Unknown string parameter " << id << "\n"; + out::error() << "Trying to get unknown string parameter " << id << "\n"; return 1; } return 0; @@ -540,7 +541,7 @@ namespace ReSolve int LinSolverIterativeRandFGMRES::getCliParam(const std::string id, index_type& value) { - switch (params_list_[id]) + switch (getParamId(id)) { case MAXIT: value = getMaxit(); @@ -552,7 +553,7 @@ namespace ReSolve value = getConvCond(); break; default: - out::error() << "Unknown integer parameter " << id << "\n"; + out::error() << "Trying to get unknown integer parameter " << id << "\n"; return 1; } return 0; @@ -560,13 +561,13 @@ namespace ReSolve int LinSolverIterativeRandFGMRES::getCliParam(const std::string id, real_type& value) { - switch (params_list_[id]) + switch (getParamId(id)) { case TOL: value = getTol(); break; default: - out::error() << "Unknown real parameter " << id << "\n"; + out::error() << "Trying to get unknown real parameter " << id << "\n"; return 1; } return 0; @@ -574,13 +575,13 @@ namespace ReSolve int LinSolverIterativeRandFGMRES::getCliParam(const std::string id, bool& value) { - switch (params_list_[id]) + switch (getParamId(id)) { case FLEXIBLE: value = getFlexible(); break; default: - out::error() << "Unknown boolean parameter " << id << "\n"; + out::error() << "Trying to get unknown boolean parameter " << id << "\n"; return 1; } return 0; @@ -588,7 +589,7 @@ namespace ReSolve int LinSolverIterativeRandFGMRES::printCliParam(const std::string id) { - switch (params_list_[id]) + switch (getParamId(id)) { case TOL: std::cout << getTol() << "\n"; @@ -600,7 +601,7 @@ namespace ReSolve std::cout << getRestart() << "\n"; break; default: - out::error() << "Unknown parameter " << id << "\n"; + out::error() << "Trying to print unknown parameter " << id << "\n"; return 1; } return 0;