Skip to content

Commit 6ac0bbd

Browse files
committed
Add unit test for setting/getting solver params.
1 parent 3206f99 commit 6ac0bbd

File tree

7 files changed

+194
-26
lines changed

7 files changed

+194
-26
lines changed

resolve/Common.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ namespace ReSolve {
1313
constexpr double EPSMAC = 1.0e-16;
1414

1515

16-
// TODO: let cmake manage these. combined with the todo above relating to cstdint
17-
// this is related to resolve/lusol/lusol_precision.f90. whatever is here should
18-
// have an equivalent there
19-
20-
// NOTE: i'd love to make this std::float64_t but we're not on c++23
16+
/// @todo Provide CMake option to se these types at config time
2117
using real_type = double;
2218
using index_type = std::int32_t;
2319

resolve/LinSolverIterativeFGMRES.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,7 @@ namespace ReSolve
152152
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < (tol_*bnorm)));
153153
break;
154154
}
155-
// if (conv_cond_ == 0) {
156-
// exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON));
157-
// } else {
158-
// if (conv_cond_ == 1) {
159-
// exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < tol_));
160-
// } else {
161-
// if (conv_cond_ == 2) {
162-
// exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < (tol_*bnorm)));
163-
// }
164-
// }
165-
// }
155+
166156
if (exit_cond) {
167157
outer_flag = 0;
168158
final_residual_norm_ = rnorm;
@@ -487,6 +477,12 @@ namespace ReSolve
487477
case RESTART:
488478
std::cout << getRestart() << "\n";
489479
break;
480+
case CONV_COND:
481+
std::cout << getConvCond() << "\n";
482+
break;
483+
case FLEXIBLE:
484+
std::cout << getFlexible() << "\n";
485+
break;
490486
default:
491487
out::error() << "Unknown parameter " << id << "\n";
492488
return 1;

resolve/LinSolverIterativeRandFGMRES.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,21 @@ namespace ReSolve
177177
tolrel = 1e-16;
178178
}
179179
}
180-
int exit_cond = 0;
181-
if (conv_cond_ == 0) {
182-
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON));
183-
} else if (conv_cond_ == 1) {
184-
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < tol_));
185-
} else if (conv_cond_ == 2) {
186-
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < (tol_*bnorm)));
180+
181+
bool exit_cond = false;
182+
switch (conv_cond_)
183+
{
184+
case 0:
185+
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON));
186+
break;
187+
case 1:
188+
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < tol_));
189+
break;
190+
case 2:
191+
exit_cond = ((std::abs(rnorm - ZERO) <= EPSILON) || (rnorm < (tol_*bnorm)));
192+
break;
187193
}
194+
188195
if (exit_cond) {
189196
outer_flag = 0;
190197
final_residual_norm_ = rnorm;
@@ -259,9 +266,9 @@ namespace ReSolve
259266
h_H_[i * (restart_ + 1) + k] = -h_s_[k1] * t + h_c_[k1] * h_H_[i * (restart_ + 1) + k];
260267
}
261268
} // if (i != 0)
262-
double Hii = h_H_[i * (restart_ + 1) + i];
263-
double Hii1 = h_H_[(i) * (restart_ + 1) + i + 1];
264-
double gam = std::sqrt(Hii * Hii + Hii1 * Hii1);
269+
real_type Hii = h_H_[i * (restart_ + 1) + i];
270+
real_type Hii1 = h_H_[(i) * (restart_ + 1) + i + 1];
271+
real_type gam = std::sqrt(Hii * Hii + Hii1 * Hii1);
265272

266273
if(std::abs(gam - ZERO) <= EPSILON) {
267274
gam = EPSMAC;

tests/unit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ add_subdirectory(matrix)
1010
add_subdirectory(vector)
1111
add_subdirectory(utilities)
1212
add_subdirectory(memory)
13+
add_subdirectory(params)

tests/unit/params/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#[[
2+
3+
@brief Build ReSolve solver configuration parameter management unit tests
4+
5+
@author Slaven Peles <[email protected]>
6+
7+
]]
8+
9+
# Build logger tests
10+
add_executable(runParamTests.exe runParamTests.cpp)
11+
target_link_libraries(runParamTests.exe PRIVATE ReSolve)
12+
13+
# Install tests
14+
set(installable_tests runParamTests.exe)
15+
install(TARGETS ${installable_tests}
16+
RUNTIME DESTINATION bin/resolve/tests/unit)
17+
18+
add_test(NAME param_test COMMAND $<TARGET_FILE:runParamTests.exe>)

tests/unit/params/ParamTests.hpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @file ParamTests.hpp
3+
* @brief Contains definition of ParamTests class.
4+
* @author Slaven Peles <[email protected]>
5+
*/
6+
7+
#pragma once
8+
#include <string>
9+
#include <vector>
10+
#include <sstream>
11+
#include <iterator>
12+
#include <resolve/workspace/LinAlgWorkspace.hpp>
13+
#include <resolve/matrix/MatrixHandler.hpp>
14+
#include <resolve/vector/VectorHandler.hpp>
15+
#include <resolve/GramSchmidt.hpp>
16+
#include <resolve/LinSolverIterativeFGMRES.hpp>
17+
#include <tests/unit/TestBase.hpp>
18+
19+
namespace ReSolve { namespace tests {
20+
/**
21+
* @brief Class implementing unit tests for Param class.
22+
*
23+
* The ParamTests class is implemented entirely in this header file.
24+
* Adding new unit test requires simply adding another method to this
25+
* class.
26+
*/
27+
class ParamTests : TestBase
28+
{
29+
public:
30+
ParamTests(){}
31+
virtual ~ParamTests(){}
32+
33+
TestOutcome paramSetGet()
34+
{
35+
TestStatus success;
36+
37+
success = true;
38+
39+
index_type restart = -1;
40+
real_type tol = -1.0;
41+
index_type maxit = -1;
42+
index_type conv_cond = -1;
43+
44+
LinAlgWorkspaceCpu workspace;
45+
workspace.initializeHandles();
46+
47+
MatrixHandler matrix_handler(&workspace);
48+
VectorHandler vector_handler(&workspace);
49+
50+
GramSchmidt gs(&vector_handler, GramSchmidt::cgs2);
51+
52+
// Constructor sets parameters
53+
LinSolverIterativeFGMRES solver(restart,
54+
tol,
55+
maxit,
56+
conv_cond,
57+
&matrix_handler,
58+
&vector_handler,
59+
&gs);
60+
61+
index_type restart_out = 0;
62+
real_type tol_out = 0.0;
63+
index_type maxit_out = 0;
64+
index_type conv_cond_out = 0;
65+
bool flexible_out = false;
66+
67+
// Use getters to read parameters set by the constructor
68+
solver.getCliParam("restart", restart_out );
69+
solver.getCliParam("tol", tol_out );
70+
solver.getCliParam("maxit", maxit_out );
71+
solver.getCliParam("conv_cond", conv_cond_out);
72+
solver.getCliParam("flexible", flexible_out );
73+
74+
// Check getters
75+
success *= (restart == restart_out);
76+
success *= (maxit == maxit_out);
77+
success *= (conv_cond == conv_cond_out);
78+
success *= isEqual(tol, tol_out);
79+
success *= flexible_out; // Default is flexible = true
80+
81+
// Pick different parameter values from the input
82+
std::string restart_in = "2";
83+
std::string tol_in = "2.0";
84+
std::string maxit_in = "2";
85+
std::string conv_cond_in = "2";
86+
std::string flexible_in = "no";
87+
88+
restart = atoi(restart_in.c_str());
89+
tol = atof(tol_in.c_str());
90+
maxit = atoi(maxit_in.c_str());
91+
conv_cond = atoi(conv_cond_in.c_str());
92+
93+
// Use setters to change FGMRES solver parameters
94+
solver.setCliParam("restart", restart_in );
95+
solver.setCliParam("tol", tol_in );
96+
solver.setCliParam("maxit", maxit_in );
97+
solver.setCliParam("conv_cond", conv_cond_in);
98+
solver.setCliParam("flexible", flexible_in );
99+
100+
// Read new values
101+
solver.getCliParam("restart", restart_out );
102+
solver.getCliParam("tol", tol_out );
103+
solver.getCliParam("maxit", maxit_out );
104+
solver.getCliParam("conv_cond", conv_cond_out);
105+
solver.getCliParam("flexible", flexible_out );
106+
107+
// Check setters
108+
success *= (restart == restart_out);
109+
success *= (maxit == maxit_out);
110+
success *= (conv_cond == conv_cond_out);
111+
success *= isEqual(tol, tol_out);
112+
success *= !flexible_out; // flexible was set to "no"
113+
114+
return success.report(__func__);
115+
}
116+
117+
118+
private:
119+
120+
}; // class ParamTests
121+
122+
}} // namespace ReSolve::tests
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @file runParamTests.cpp
3+
* @brief Driver for Param class tests.
4+
* @author Slaven Peles <[email protected]>
5+
*/
6+
7+
#include <iostream>
8+
#include <ostream>
9+
10+
#include <resolve/Common.hpp>
11+
#include "ParamTests.hpp"
12+
13+
int main()
14+
{
15+
using namespace ReSolve::io;
16+
17+
// Create ParamTests object
18+
ReSolve::tests::ParamTests test;
19+
20+
// Create test results accounting object
21+
ReSolve::tests::TestingResults result;
22+
23+
// Run tests
24+
result += test.paramSetGet();
25+
26+
// Return tests summary
27+
return result.summary();
28+
}

0 commit comments

Comments
 (0)