| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Belos: Block Linear Solvers Package |
| 4 |
// |
| 5 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include "Teuchos_UnitTestHarness.hpp" |
| 11 |
#include "BelosTpetraAdapter.hpp" |
| 12 |
#include "MatrixMarket_Tpetra.hpp" |
| 13 |
#include "BelosSolverFactory.hpp" |
| 14 |
#include "TpetraCore_ETIHelperMacros.h" |
| 15 |
|
| 16 |
// mfh 13 Oct 2017: Test Belos::CustomSolverFactory, for the Tpetra |
| 17 |
// specialization of Belos::SolverFactory. |
| 18 |
// |
| 19 |
// 1. Write a custom Belos::SolverManager subclass. It doesn't need |
| 20 |
// to do anything (we're not actually trying to solve with it; we |
| 21 |
// just want to test that we created an instance of it). |
| 22 |
// |
| 23 |
// 2. Write a custom Belos::CustomSolverFactory subclass, that can |
| 24 |
// create instances of the above solver. |
| 25 |
// |
| 26 |
// 3. Add our custom factory to Belos::SolverFactory. |
| 27 |
// |
| 28 |
// 4. Verify that Belos::SolverFactory can create instances of our |
| 29 |
// custom solver. |
| 30 |
|
| 31 |
namespace { // (anonymous) |
| 32 |
|
| 33 |
// Trivial Belos::SolverManager subclass for testing |
| 34 |
// Belos::SolverFactory::addFactory. It doesn't need to do anything, |
| 35 |
// but it must be possible to construct and destruct instances of it. |
| 36 |
template<class SC, class MV, class OP> |
| 37 |
class FooSolver : public Belos::SolverManager<SC, MV, OP> |
| 38 |
{ |
| 39 |
public: |
| 40 |
virtual ~FooSolver () {} |
| 41 |
|
| 42 |
Teuchos::RCP<Belos::SolverManager<SC, MV, OP> > clone () const override { |
| 43 |
return Teuchos::rcp(new FooSolver<SC, MV, OP>); |
| 44 |
} |
| 45 |
|
| 46 |
virtual const Belos::LinearProblem<SC, MV, OP>& |
| 47 |
getProblem () const override { |
| 48 |
return linearProblem_; |
| 49 |
} |
| 50 |
|
| 51 |
virtual Teuchos::RCP<const Teuchos::ParameterList> |
| 52 |
getValidParameters () const override { |
| 53 |
return Teuchos::null; |
| 54 |
} |
| 55 |
|
| 56 |
virtual Teuchos::RCP<const Teuchos::ParameterList> |
| 57 |
getCurrentParameters () const override { |
| 58 |
return Teuchos::null; |
| 59 |
} |
| 60 |
|
| 61 |
virtual int getNumIters () const override { |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
virtual bool isLOADetected () const override { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
virtual void |
| 70 |
setProblem (const Teuchos::RCP<Belos::LinearProblem<SC, MV, OP> >& /* problem */) override |
| 71 |
{} |
| 72 |
|
| 73 |
virtual void |
| 74 |
setParameters (const Teuchos::RCP<Teuchos::ParameterList>& /* params */) override |
| 75 |
{} |
| 76 |
|
| 77 |
virtual void |
| 78 |
reset (const Belos::ResetType /* type */) override |
| 79 |
{} |
| 80 |
|
| 81 |
virtual Belos::ReturnType solve () override { |
| 82 |
return Belos::Unconverged; |
| 83 |
} |
| 84 |
|
| 85 |
private: |
| 86 |
// Empty and trivial linear problem. Exists only to avoid warnings |
| 87 |
// in getProblem(). |
| 88 |
Belos::LinearProblem<SC, MV, OP> linearProblem_; |
| 89 |
}; |
| 90 |
|
| 91 |
// Belos::CustomSolverFactory subclass for testing |
| 92 |
// Belos::SolverFactory::addFactory. This subclass knows how to |
| 93 |
// create FooSolver instances (see above). |
| 94 |
template<class SC, class MV, class OP> |
| 95 |
class FooSolverFactory : public Belos::CustomSolverFactory<SC, MV, OP> |
| 96 |
{ |
| 97 |
public: |
| 98 |
|
| 99 |
virtual ~FooSolverFactory () {} |
| 100 |
|
| 101 |
virtual Teuchos::RCP<Belos::SolverManager<SC, MV, OP> > |
| 102 |
getSolver (const std::string& solverName, |
| 103 |
const Teuchos::RCP<Teuchos::ParameterList>& solverParams) |
| 104 |
{ |
| 105 |
using Teuchos::rcp; |
| 106 |
using Teuchos::rcp_implicit_cast; |
| 107 |
typedef Belos::SolverManager<SC, MV, OP> base_solver_type; |
| 108 |
typedef FooSolver<SC, MV, OP> solver_type; |
| 109 |
|
| 110 |
if (solverName == "FOO") { |
| 111 |
return rcp_implicit_cast<base_solver_type> (rcp (new solver_type)); |
| 112 |
} |
| 113 |
else { |
| 114 |
return Teuchos::null; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
virtual int numSupportedSolvers () const { |
| 119 |
return 1; |
| 120 |
} |
| 121 |
|
| 122 |
virtual std::vector<std::string> supportedSolverNames () const { |
| 123 |
return {"FOO"}; |
| 124 |
} |
| 125 |
|
| 126 |
virtual bool isSupported (const std::string& solverName) const { |
| 127 |
return solverName == "FOO"; |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
// |
| 132 |
// The actual unit test. |
| 133 |
// |
| 134 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL( CustomSolverFactory, AddFactory, SC, LO, GO, NT ) |
| 135 |
{ |
| 136 |
using Teuchos::RCP; |
| 137 |
using Teuchos::rcp; |
| 138 |
using Teuchos::rcp_static_cast; |
| 139 |
using std::endl; |
| 140 |
typedef Tpetra::MultiVector<SC,LO,GO,NT> MV; |
| 141 |
typedef Tpetra::Operator<SC,LO,GO,NT> OP; |
| 142 |
typedef Belos::SolverManager<SC, MV, OP> solver_type; |
| 143 |
typedef Belos::CustomSolverFactory<SC, MV, OP> custom_factory_type; |
| 144 |
|
| 145 |
Belos::SolverFactory<SC, MV, OP> factory; |
| 146 |
|
| 147 |
// Sanity check for factory; can it create a GMRES solver? |
| 148 |
RCP<solver_type> solver = factory.create ("GMRES", Teuchos::null); |
| 149 |
TEST_ASSERT( ! solver.is_null () ); |
| 150 |
solver = Teuchos::null; |
| 151 |
|
| 152 |
RCP<Teuchos::ParameterList> params (new Teuchos::ParameterList); |
| 153 |
solver = factory.create ("GMRES", params); |
| 154 |
TEST_ASSERT( ! solver.is_null () ); |
| 155 |
solver = Teuchos::null; |
| 156 |
|
| 157 |
// At this point, 'factory' should NOT support a solver called "FOO". |
| 158 |
TEST_ASSERT( ! factory.isSupported ("FOO") ); |
| 159 |
|
| 160 |
// At this point, 'factory' should NOT support a solver called "BAZ". |
| 161 |
TEST_ASSERT( ! factory.isSupported ("BAZ") ); |
| 162 |
|
| 163 |
RCP<FooSolverFactory<SC, MV, OP> > fooFactory = rcp(new FooSolverFactory<SC, MV, OP>); |
| 164 |
RCP<custom_factory_type> customFactory = |
| 165 |
rcp_static_cast<custom_factory_type> (fooFactory); |
| 166 |
// Add an instance of our custom factory to the main factory. |
| 167 |
factory.addFactory (customFactory); |
| 168 |
|
| 169 |
// At this point, 'factory' should support a solver called "FOO". |
| 170 |
TEST_ASSERT( factory.isSupported ("FOO") ); |
| 171 |
|
| 172 |
// At this point, 'factory' should NOT support a solver called "BAZ". |
| 173 |
TEST_ASSERT( ! factory.isSupported ("BAZ") ); |
| 174 |
|
| 175 |
// Attempt to create an instance of our custom solver from 'factory'. |
| 176 |
solver = factory.create ("FOO", Teuchos::null); |
| 177 |
TEST_ASSERT( ! solver.is_null () ); |
| 178 |
RCP<FooSolver<SC, MV, OP> > fooSolver = |
| 179 |
Teuchos::rcp_dynamic_cast<FooSolver<SC, MV, OP> > (solver); |
| 180 |
TEST_ASSERT( ! fooSolver.is_null () ); |
| 181 |
fooSolver = Teuchos::null; |
| 182 |
|
| 183 |
// Create a new Belos::SolverFactory instance. The above addFactory |
| 184 |
// call should have changed _all_ Belos::SolverFactory instances. |
| 185 |
Belos::SolverFactory<SC, MV, OP> factory2; |
| 186 |
TEST_ASSERT( factory2.isSupported ("FOO") ); |
| 187 |
solver = factory2.create ("FOO", Teuchos::null); |
| 188 |
TEST_ASSERT( ! solver.is_null () ); |
| 189 |
fooSolver = Teuchos::rcp_dynamic_cast<FooSolver<SC, MV, OP> > (solver); |
| 190 |
TEST_ASSERT( ! fooSolver.is_null () ); |
| 191 |
|
| 192 |
// Clear the custom solver factory, which is shared by all solver factories |
| 193 |
factory.clearFactories(); |
| 194 |
|
| 195 |
// Check that the custom solver is not available anymore from another |
| 196 |
// Belos::SolverFactory instance. |
| 197 |
TEST_ASSERT( ! factory2.isSupported ("FOO") ); |
| 198 |
} |
| 199 |
|
| 200 |
// Define typedefs that make the Tpetra macros work. |
| 201 |
TPETRA_ETI_MANGLING_TYPEDEFS() |
| 202 |
|
| 203 |
// Macro that instantiates the unit test |
| 204 |
#define LCLINST( SC, LO, GO, NT ) \ |
| 205 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT( CustomSolverFactory, AddFactory, SC, LO, GO, NT ) |
| 206 |
|
| 207 |
// Tpetra's ETI will instantiate the unit test for all enabled type |
| 208 |
// combinations. |
| 209 |
TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR( LCLINST ) |
| 210 |
|
| 211 |
} // namespace (anonymous) |