Skip to content

Commit

Permalink
Set/get CLI parameters (first stab).
Browse files Browse the repository at this point in the history
  • Loading branch information
pelesh committed Apr 9, 2024
1 parent 790e7d0 commit db597be
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 16 deletions.
1 change: 1 addition & 0 deletions resolve/LinSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @brief Implementation of linear solver base class.
*
*/

#include <resolve/matrix/Sparse.hpp>
#include <resolve/utilities/logger/Logger.hpp>

Expand Down
38 changes: 36 additions & 2 deletions resolve/LinSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/
#pragma once

#include <map>
#include <string>

#include "Common.hpp"

namespace ReSolve
Expand All @@ -29,6 +31,8 @@ namespace ReSolve

// Forward declaration of MatrixHandler class
class MatrixHandler;

class SolverParameters;

/**
* @brief Base class for all linear solvers.
Expand All @@ -44,14 +48,44 @@ namespace ReSolve
virtual ~LinSolver();

real_type evaluateResidual();

virtual int setCliParam(const std::string /* id */, const std::string /* value */)
{
return 1;
}

virtual int getCliParam(const std::string /* id */, std::string& /* value */)
{
return 1;
}

virtual int getCliParam(const std::string /* id */, index_type& /* value */)
{
return 1;
}

virtual int getCliParam(const std::string /* id */, real_type& /* value */)
{
return 1;
}

virtual int getCliParam(const std::string /* id */, bool& /* value */)
{
return 1;
}

virtual int printCliParam(const std::string /* id */)
{
return 1;
}

protected:
matrix::Sparse* A_{nullptr};
real_type* rhs_{nullptr};
real_type* sol_{nullptr};

MatrixHandler* matrix_handler_{nullptr};
VectorHandler* vector_handler_{nullptr};

std::map<std::string, int> params_list_;
};

} // namespace ReSolve
146 changes: 135 additions & 11 deletions resolve/LinSolverIterativeFGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace ReSolve
vector_handler_ = vector_handler;
GS_ = nullptr;
setMemorySpace();
initParamList();
}

LinSolverIterativeFGMRES::LinSolverIterativeFGMRES(MatrixHandler* matrix_handler,
Expand All @@ -34,6 +35,7 @@ namespace ReSolve
vector_handler_ = vector_handler;
GS_ = gs;
setMemorySpace();
initParamList();
}

LinSolverIterativeFGMRES::LinSolverIterativeFGMRES(index_type restart,
Expand All @@ -55,6 +57,7 @@ namespace ReSolve
vector_handler_ = vector_handler;
GS_ = gs;
setMemorySpace();
initParamList();
}

LinSolverIterativeFGMRES::~LinSolverIterativeFGMRES()
Expand Down Expand Up @@ -143,18 +146,31 @@ namespace ReSolve
tolrel = 1e-16;
}
}
int exit_cond = 0;
if (conv_cond_ == 0) {
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON));
} else {
if (conv_cond_ == 1) {
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < tol_));
} else {
if (conv_cond_ == 2) {
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < (tol_*bnorm)));
}
}

bool exit_cond = false;
switch (conv_cond_)
{
case 0:
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON));
break;
case 1:
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < tol_));
break;
case 2:
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < (tol_*bnorm)));
break;
}
// if (conv_cond_ == 0) {
// exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON));
// } else {
// if (conv_cond_ == 1) {
// exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < tol_));
// } else {
// if (conv_cond_ == 2) {
// exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < (tol_*bnorm)));
// }
// }
// }
if (exit_cond) {
outer_flag = 0;
final_residual_norm_ = rnorm;
Expand Down Expand Up @@ -383,6 +399,105 @@ namespace ReSolve
return 0;
}

int LinSolverIterativeFGMRES::setCliParam(const std::string id, const std::string value)
{
switch (params_list_[id])
{
case TOL:
setTol(atof(value.c_str()));
break;
case MAXIT:
setMaxit(atoi(value.c_str()));
break;
case RESTART:
setRestart(atoi(value.c_str()));
break;
case CONV_COND:
setRestart(atoi(value.c_str()));
break;
case FLEXIBLE:
bool is_flexible = (value == "yes");
setFlexible(is_flexible);
break;
}
return 0;
}

int LinSolverIterativeFGMRES::getCliParam(const std::string id, std::string& /* value */)
{
switch (params_list_[id])
{
default:
out::error() << "Unknown string parameter " << id << "\n";
return 1;
}
return 0;
}

int LinSolverIterativeFGMRES::getCliParam(const std::string id, index_type& value)
{
switch (params_list_[id])
{
case MAXIT:
value = getMaxit();
break;
case RESTART:
value = getRestart();
break;
case CONV_COND:
value = getConvCond();
break;
default:
out::error() << "Unknown integer parameter " << id << "\n";
return 1;
}
return 0;
}

int LinSolverIterativeFGMRES::getCliParam(const std::string id, real_type& value)
{
switch (params_list_[id])
{
case TOL:
value = getTol();
break;
default:
out::error() << "Unknown real parameter " << id << "\n";
return 1;
}
return 0;
}

int LinSolverIterativeFGMRES::getCliParam(const std::string id, bool& value)
{
switch (params_list_[id])
{
case FLEXIBLE:
value = getFlexible();
default:
out::error() << "Unknown boolean parameter " << id << "\n";
return 1;
}
return 0;
}

int LinSolverIterativeFGMRES::printCliParam(const std::string id)
{
switch (params_list_[id])
{
case TOL:
std::cout << getTol() << "\n";
break;
case MAXIT:
std::cout << getMaxit() << "\n";
break;
default:
out::error() << "Unknown parameter " << id << "\n";
return 1;
}
return 0;
}

//
// Private methods
//
Expand Down Expand Up @@ -449,4 +564,13 @@ namespace ReSolve
}
}

void LinSolverIterativeFGMRES::initParamList()
{
params_list_["tol"] = TOL;
params_list_["maxit"] = MAXIT;
params_list_["restart"] = RESTART;
params_list_["conv_cond"] = CONV_COND;
params_list_["flexible"] = FLEXIBLE;
}

} // namespace
11 changes: 11 additions & 0 deletions resolve/LinSolverIterativeFGMRES.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,21 @@ namespace ReSolve
int setRestart(index_type restart) override;
int setFlexible(bool is_flexible) override;

int setCliParam(const std::string id, const std::string value) override;
int getCliParam(const std::string id, std::string& value) override;
int getCliParam(const std::string id, index_type& value) override;
int getCliParam(const std::string id, real_type& value) override;
int getCliParam(const std::string id, bool& value) override;
int printCliParam(const std::string id) override;

private:
enum ParamaterIDs {TOL=0, MAXIT, RESTART, CONV_COND, FLEXIBLE};

private:
int allocateSolverData();
int freeSolverData();
void setMemorySpace();
void initParamList();
void precV(vector_type* rhs, vector_type* x); ///< Apply preconditioner

memory::MemorySpace memspace_;
Expand Down
Loading

0 comments on commit db597be

Please sign in to comment.