Skip to content

Commit

Permalink
Check if parameters exist before setting/getting them.
Browse files Browse the repository at this point in the history
  • Loading branch information
pelesh committed Dec 15, 2024
1 parent 6ac0bbd commit 993782c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
9 changes: 9 additions & 0 deletions resolve/LinSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}


Expand Down
4 changes: 3 additions & 1 deletion resolve/LinSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ namespace ReSolve
return 1;
}

protected:
protected:
int getParamId(std::string id);

matrix::Sparse* A_{nullptr};

MatrixHandler* matrix_handler_{nullptr};
Expand Down
27 changes: 14 additions & 13 deletions resolve/LinSolverIterativeFGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -398,27 +398,28 @@ 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;
}

int LinSolverIterativeFGMRES::getCliParam(const std::string id, index_type& value)
{
switch (params_list_[id])
switch (getParamId(id))
{
case MAXIT:
value = getMaxit();
Expand All @@ -430,43 +431,43 @@ 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;
}

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;
}

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;
}

int LinSolverIterativeFGMRES::printCliParam(const std::string id)
{
switch (params_list_[id])
switch (getParamId(id))
{
case TOL:
std::cout << getTol() << "\n";
Expand All @@ -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;
Expand Down
27 changes: 14 additions & 13 deletions resolve/LinSolverIterativeRandFGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -520,27 +520,28 @@ 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;
}

int LinSolverIterativeRandFGMRES::getCliParam(const std::string id, index_type& value)
{
switch (params_list_[id])
switch (getParamId(id))
{
case MAXIT:
value = getMaxit();
Expand All @@ -552,43 +553,43 @@ 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;
}

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;
}

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;
}

int LinSolverIterativeRandFGMRES::printCliParam(const std::string id)
{
switch (params_list_[id])
switch (getParamId(id))
{
case TOL:
std::cout << getTol() << "\n";
Expand All @@ -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;
Expand Down

0 comments on commit 993782c

Please sign in to comment.