Skip to content

Commit

Permalink
Add unit test for setting/getting solver params.
Browse files Browse the repository at this point in the history
  • Loading branch information
pelesh committed Sep 28, 2024
1 parent e3268a8 commit 5356946
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 26 deletions.
6 changes: 1 addition & 5 deletions resolve/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ namespace ReSolve {
constexpr double EPSMAC = 1.0e-16;


// TODO: let cmake manage these. combined with the todo above relating to cstdint
// this is related to resolve/lusol/lusol_precision.f90. whatever is here should
// have an equivalent there

// NOTE: i'd love to make this std::float64_t but we're not on c++23
/// @todo Provide CMake option to se these types at config time
using real_type = double;
using index_type = std::int32_t;

Expand Down
18 changes: 7 additions & 11 deletions resolve/LinSolverIterativeFGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,7 @@ namespace ReSolve
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 @@ -495,6 +485,12 @@ namespace ReSolve
case RESTART:
std::cout << getRestart() << "\n";
break;
case CONV_COND:
std::cout << getConvCond() << "\n";
break;
case FLEXIBLE:
std::cout << getFlexible() << "\n";
break;
default:
out::error() << "Unknown parameter " << id << "\n";
return 1;
Expand Down
27 changes: 17 additions & 10 deletions resolve/LinSolverIterativeRandFGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,21 @@ 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 (exit_cond) {
outer_flag = 0;
final_residual_norm_ = rnorm;
Expand Down Expand Up @@ -268,9 +275,9 @@ namespace ReSolve
h_H_[i * (restart_ + 1) + k] = -h_s_[k1] * t + h_c_[k1] * h_H_[i * (restart_ + 1) + k];
}
} // if (i != 0)
double Hii = h_H_[i * (restart_ + 1) + i];
double Hii1 = h_H_[(i) * (restart_ + 1) + i + 1];
double gam = std::sqrt(Hii * Hii + Hii1 * Hii1);
real_type Hii = h_H_[i * (restart_ + 1) + i];
real_type Hii1 = h_H_[(i) * (restart_ + 1) + i + 1];
real_type gam = std::sqrt(Hii * Hii + Hii1 * Hii1);

if(std::abs(gam - ZERO) <= EPSILON) {
gam = EPSMAC;
Expand Down
1 change: 1 addition & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ add_subdirectory(matrix)
add_subdirectory(vector)
add_subdirectory(utilities)
add_subdirectory(memory)
add_subdirectory(params)
18 changes: 18 additions & 0 deletions tests/unit/params/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[[
@brief Build ReSolve solver configuration parameter management unit tests
@author Slaven Peles <[email protected]>
]]

# Build logger tests
add_executable(runParamTests.exe runParamTests.cpp)
target_link_libraries(runParamTests.exe PRIVATE ReSolve)

# Install tests
set(installable_tests runParamTests.exe)
install(TARGETS ${installable_tests}
RUNTIME DESTINATION bin/resolve/tests/unit)

add_test(NAME param_test COMMAND $<TARGET_FILE:runParamTests.exe>)
122 changes: 122 additions & 0 deletions tests/unit/params/ParamTests.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* @file ParamTests.hpp
* @brief Contains definition of ParamTests class.
* @author Slaven Peles <[email protected]>
*/

#pragma once
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <resolve/workspace/LinAlgWorkspace.hpp>
#include <resolve/matrix/MatrixHandler.hpp>
#include <resolve/vector/VectorHandler.hpp>
#include <resolve/GramSchmidt.hpp>
#include <resolve/LinSolverIterativeFGMRES.hpp>
#include <tests/unit/TestBase.hpp>

namespace ReSolve { namespace tests {
/**
* @brief Class implementing unit tests for Param class.
*
* The ParamTests class is implemented entirely in this header file.
* Adding new unit test requires simply adding another method to this
* class.
*/
class ParamTests : TestBase
{
public:
ParamTests(){}
virtual ~ParamTests(){}

TestOutcome paramSetGet()
{
TestStatus success;

success = true;

index_type restart = -1;
real_type tol = -1.0;
index_type maxit = -1;
index_type conv_cond = -1;

LinAlgWorkspaceCpu workspace;
workspace.initializeHandles();

MatrixHandler matrix_handler(&workspace);
VectorHandler vector_handler(&workspace);

GramSchmidt gs(&vector_handler, GramSchmidt::cgs2);

// Constructor sets parameters
LinSolverIterativeFGMRES solver(restart,
tol,
maxit,
conv_cond,
&matrix_handler,
&vector_handler,
&gs);

index_type restart_out = 0;
real_type tol_out = 0.0;
index_type maxit_out = 0;
index_type conv_cond_out = 0;
bool flexible_out = false;

// Use getters to read parameters set by the constructor
solver.getCliParam("restart", restart_out );
solver.getCliParam("tol", tol_out );
solver.getCliParam("maxit", maxit_out );
solver.getCliParam("conv_cond", conv_cond_out);
solver.getCliParam("flexible", flexible_out );

// Check getters
success *= (restart == restart_out);
success *= (maxit == maxit_out);
success *= (conv_cond == conv_cond_out);
success *= isEqual(tol, tol_out);
success *= flexible_out; // Default is flexible = true

// Pick different parameter values from the input
std::string restart_in = "2";
std::string tol_in = "2.0";
std::string maxit_in = "2";
std::string conv_cond_in = "2";
std::string flexible_in = "no";

restart = atoi(restart_in.c_str());
tol = atof(tol_in.c_str());
maxit = atoi(maxit_in.c_str());
conv_cond = atoi(conv_cond_in.c_str());

// Use setters to change FGMRES solver parameters
solver.setCliParam("restart", restart_in );
solver.setCliParam("tol", tol_in );
solver.setCliParam("maxit", maxit_in );
solver.setCliParam("conv_cond", conv_cond_in);
solver.setCliParam("flexible", flexible_in );

// Read new values
solver.getCliParam("restart", restart_out );
solver.getCliParam("tol", tol_out );
solver.getCliParam("maxit", maxit_out );
solver.getCliParam("conv_cond", conv_cond_out);
solver.getCliParam("flexible", flexible_out );

// Check setters
success *= (restart == restart_out);
success *= (maxit == maxit_out);
success *= (conv_cond == conv_cond_out);
success *= isEqual(tol, tol_out);
success *= !flexible_out; // flexible was set to "no"

return success.report(__func__);
}


private:

}; // class ParamTests

}} // namespace ReSolve::tests
28 changes: 28 additions & 0 deletions tests/unit/params/runParamTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @file runParamTests.cpp
* @brief Driver for Param class tests.
* @author Slaven Peles <[email protected]>
*/

#include <iostream>
#include <ostream>

#include <resolve/Common.hpp>
#include "ParamTests.hpp"

int main()
{
using namespace ReSolve::io;

// Create ParamTests object
ReSolve::tests::ParamTests test;

// Create test results accounting object
ReSolve::tests::TestingResults result;

// Run tests
result += test.paramSetGet();

// Return tests summary
return result.summary();
}

0 comments on commit 5356946

Please sign in to comment.