|
| 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 |
0 commit comments