Debian Codegraph @

packages/nox/test/tpetra/Tpetra_OperatorTests.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // NOX: An Object-Oriented Nonlinear Solver Package
4 //
5 // Copyright 2002 NTESS and the NOX contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_ScalarTraits.hpp"
11 #include "Teuchos_Comm.hpp"
12 #include "Teuchos_RCP.hpp"
13
14 #include "Tpetra_Core.hpp"
15 #include "Tpetra_Vector.hpp"
16 #include "Tpetra_MultiVector.hpp"
17 #include "Tpetra_CrsMatrix.hpp"
18 #include "Thyra_VectorStdOps.hpp"
19 #include "Thyra_MultiVectorStdOps.hpp"
20 #include "Thyra_TpetraThyraWrappers.hpp"
21 #include "Thyra_StateFuncModelEvaluatorBase.hpp"
22 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
23
24 #include "NOX_Thyra.H"
25 #include "NOX_Thyra_Vector.H"
26 #include "NOX_Thyra_MultiVector.H"
27
28 // Typedefs
29
30 typedef Tpetra::Vector<>::scalar_type Scalar;
31 typedef Tpetra::Vector<>::local_ordinal_type LO;
32 typedef Tpetra::Vector<>::global_ordinal_type GO;
33 typedef Tpetra::Vector<>::node_type Node;
34
35 typedef Teuchos::ScalarTraits<Scalar> ST;
36 typedef Tpetra::Map<LO,GO,Node> Map;
37 typedef Tpetra::Vector<Scalar,LO,GO,Node> TV;
38 typedef Tpetra::MultiVector<Scalar,LO,GO,Node> TMV;
39 typedef Tpetra::CrsMatrix<Scalar,LO,GO,Node> CRSM;
40 typedef Thyra::VectorSpaceBase<Scalar> TVSB;
41 typedef Thyra::VectorBase<Scalar> TVB;
42 typedef Thyra::MultiVectorBase<Scalar> TMVB;
43 typedef Thyra::LinearOpBase<Scalar> TLOB;
44 typedef Thyra::TpetraOperatorVectorExtraction<Scalar,LO,GO,Node> TOVE;
45 typedef NOX::Thyra::Vector NTV;
46 typedef NOX::Thyra::MultiVector NTMV;
47 typedef typename TMV::mag_type mag_type;
48
49 #if defined HAVE_TPETRACORE_CUDA
50 #define NUM_LOCAL 10000
51 #else
52 #define NUM_LOCAL 100
53 #endif
54 const std::size_t numLocalElements = NUM_LOCAL;
55
56 // Model evaluator for returning precomputed Jacobian operator
57
58 class JacobianEvaluator
59 : public Thyra::StateFuncModelEvaluatorBase<Scalar>
60 {
61 public:
62 JacobianEvaluator(const Teuchos::RCP<TLOB>& lop) :
63 lop_(lop)
64 {
65 typedef Thyra::ModelEvaluatorBase MEB;
66 MEB::InArgsSetup<Scalar> inArgs;
67 inArgs.setModelEvalDescription(this->description());
68 inArgs.setSupports(MEB::IN_ARG_x);
69 inArgs_ = inArgs;
70
71 MEB::OutArgsSetup<Scalar> outArgs;
72 outArgs.setModelEvalDescription(this->description());
73 outArgs.setSupports(MEB::OUT_ARG_f);
74 outArgs.setSupports(MEB::OUT_ARG_W_op);
75 outArgs_ = outArgs;
76
77 Teuchos::RCP<TVB> x0 = Thyra::createMember(lop_->domain());
78 Thyra::assign(x0.ptr(), ST::zero());
79 nominalValues_ = inArgs;
80 nominalValues_.set_x(x0);
81 }
82
83 Teuchos::RCP<const TVSB> get_x_space() const { return lop_->domain(); }
84 Teuchos::RCP<const TVSB> get_f_space() const { return lop_->range(); }
85 Thyra::ModelEvaluatorBase::InArgs<Scalar> getNominalValues() const { return nominalValues_; }
86 Teuchos::RCP<TLOB> create_W_op() const { return lop_; }
87 Thyra::ModelEvaluatorBase::InArgs<Scalar> createInArgs() const { return inArgs_; }
88
89 private:
90 Thyra::ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const { return outArgs_; }
91 void evalModelImpl(
92 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs,
93 const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs
94 ) const
95 { if (nonnull(outArgs.get_W_op())) outArgs.get_W_op() = lop_; }
96
97 Teuchos::RCP<TLOB> lop_;
98 Thyra::ModelEvaluatorBase::InArgs<Scalar> nominalValues_;
99 Thyra::ModelEvaluatorBase::InArgs<Scalar> inArgs_;
100 Thyra::ModelEvaluatorBase::OutArgs<Scalar> outArgs_;
101 };
102
103 // Tpetra matrix creation
104
105 Teuchos::RCP<CRSM> createTpetraOp(const Teuchos::RCP<const Map>& map)
106 {
107 Teuchos::RCP<CRSM> mat = Teuchos::rcp(new CRSM(map, 2));
108
109 Scalar posOne = ST::one();
110 Scalar negOne = -1.0*posOne;
111 for (std::size_t i = 0; i < map->getLocalNumElements(); ++i) {
112 const GO row = map->getGlobalElement(static_cast<LO>(i));
113 if (row == 0) {
114 mat->insertGlobalValues(row,
115 Teuchos::tuple(row+1),
116 Teuchos::tuple(posOne));
117 } else if (row == static_cast<GO>(map->getGlobalNumElements()-1)) {
118 mat->insertGlobalValues(row,
119 Teuchos::tuple(row-1),
120 Teuchos::tuple(negOne));
121 } else {
122 mat->insertGlobalValues(row,
123 Teuchos::tuple(row-1, row+1),
124 Teuchos::tuple(negOne, posOne));
125 }
126 }
127
128 mat->fillComplete();
129 return mat;
130 }
131
132 // Routines for checking solution
133
134 void checkMultiVectors(const Teuchos::RCP<TMV>& a,
135 const Teuchos::RCP<TMV>& b,
136 const Teuchos::ArrayView<mag_type>& expectedNorms,
137 Teuchos::FancyOStream& out,
138 bool& success)
139 {
140 TEUCHOS_ASSERT(a->getNumVectors() == b->getNumVectors());
141 TEUCHOS_ASSERT(static_cast<size_t>(a->getNumVectors()) == static_cast<size_t>(expectedNorms.size()));
142 Scalar one = ST::one();
143 b->update(-1.0*one, *a, one);
144 std::vector<ST::magnitudeType> norms(expectedNorms.size());
145 b->norm2(Teuchos::arrayViewFromVector(norms));
146
147 ST::magnitudeType tol = 1.0e-14;
148 for (auto norm = norms.begin(); norm != norms.end(); ++norm) {
149 TEUCHOS_TEST_EQUALITY(*norm < tol, true, out, success);
150 }
151 a->norm2(Teuchos::arrayViewFromVector(norms));
152 auto normIter = norms.begin();
153 auto expNormIter = expectedNorms.begin();
154 for (; normIter != norms.end(); ++normIter, ++expNormIter) {
155 TEUCHOS_TEST_FLOATING_EQUALITY(*normIter, *expNormIter, tol, out, success);
156 }
157 }
158
159 // Unit tests
160
161 TEUCHOS_UNIT_TEST(Tpetra_OpTests, VecNoTrans)
162 {
163 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
164 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
165
166 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
167 Teuchos::RCP<const TVSB> space = Thyra::createVectorSpace<Scalar,LO,GO,Node>(map);
168
169 // Create vector objects
170 Teuchos::RCP<TVB> x = Thyra::createMember(space);
171 Teuchos::RCP<TVB> y = Thyra::createMember(space);
172 Teuchos::RCP<TVB> z = Thyra::createMember(space);
173
174 Scalar one = ST::one();
175 Thyra::assign(x.ptr(), one);
176 Thyra::assign(y.ptr(), one);
177 Thyra::assign(z.ptr(), one);
178
179 Teuchos::RCP<TV> x_tpetra = TOVE::getTpetraVector(x);
180 Teuchos::RCP<TV> y_tpetra = TOVE::getTpetraVector(y);
181 Teuchos::RCP<TV> z_tpetra = TOVE::getTpetraVector(z);
182
183 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y));
184 Teuchos::RCP<NTV> z_nox = Teuchos::rcp(new NTV(z));
185
186 // Create operator objects
187 Teuchos::RCP<CRSM> op_tpetra = createTpetraOp(map);
188 Teuchos::RCP<TLOB> op = Thyra::tpetraLinearOp<Scalar,LO,GO,Node>(space, space, op_tpetra);
189 TEUCHOS_TEST_EQUALITY(Thyra::opSupported(*op, Thyra::NOTRANS), true, out, success);
190
191 // Apply op with Tpetra directly
192 op_tpetra->apply(*z_tpetra, *x_tpetra, Teuchos::NO_TRANS);
193
194 // Apply op through NOX Group interface
195 Teuchos::RCP<JacobianEvaluator> model = Teuchos::rcp(new JacobianEvaluator(op));
196 // Need to pass a lows factory to the group to call computeJacobian
197 Stratimikos::DefaultLinearSolverBuilder builder;
198 Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList();
199 p->set("Linear Solver Type", "Belos");
200 p->set("Preconditioner Type", "None");
201 builder.setParameterList(p);
202 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
203 lowsf = builder.createLinearSolveStrategy("");
204 Teuchos::RCP<NOX::Thyra::Group> group =
205 Teuchos::rcp(new NOX::Thyra::Group(*z_nox, model, op, lowsf, Teuchos::null, Teuchos::null, Teuchos::null));
206 group->computeJacobian();
207 group->applyJacobian(*z_nox, *y_nox);
208
209 // Check for correct results
210 mag_type val = static_cast<mag_type>(ST::squareroot(static_cast<Scalar>(2.0)));
211 auto ans = Teuchos::tuple(val);
212 checkMultiVectors(x_tpetra, y_tpetra, ans, out, success);
213
214 }
215
216 TEUCHOS_UNIT_TEST(Tpetra_OpTests, MultiVecNoTrans)
217 {
218 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
219 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
220 const std::size_t numCols = 5;
221
222 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
223 Teuchos::RCP<const TVSB> space = Thyra::createVectorSpace<Scalar,LO,GO,Node>(map);
224
225 // Create vector objects
226 Teuchos::RCP<TMVB> x = Thyra::createMembers(space, numCols);
227 Teuchos::RCP<TMVB> y = Thyra::createMembers(space, numCols);
228 Teuchos::RCP<TMVB> z = Thyra::createMembers(space, numCols);
229
230 Scalar one = ST::one();
231 Thyra::assign(x.ptr(), one);
232 Thyra::assign(y.ptr(), one);
233 Thyra::assign(z.ptr(), one);
234
235 Teuchos::RCP<TMV> x_tpetra = TOVE::getTpetraMultiVector(x);
236 Teuchos::RCP<TMV> y_tpetra = TOVE::getTpetraMultiVector(y);
237 Teuchos::RCP<TMV> z_tpetra = TOVE::getTpetraMultiVector(z);
238
239 Teuchos::RCP<NTMV> y_nox = Teuchos::rcp(new NTMV(y));
240 Teuchos::RCP<NTMV> z_nox = Teuchos::rcp(new NTMV(z));
241
242 auto scales = Teuchos::tuple(one, 2.0*one, 3.0*one, 4.0*one, 5.0*one);
243 x_tpetra->scale(scales);
244 y_tpetra->scale(scales);
245 z_tpetra->scale(scales);
246
247 // Create operator objects
248 Teuchos::RCP<CRSM> op_tpetra = createTpetraOp(map);
249 Teuchos::RCP<TLOB> op = Thyra::tpetraLinearOp<Scalar,LO,GO,Node>(space, space, op_tpetra);
250 TEUCHOS_TEST_EQUALITY(Thyra::opSupported(*op, Thyra::NOTRANS), true, out, success);
251
252 // Apply op with Tpetra directly
253 op_tpetra->apply(*z_tpetra, *x_tpetra, Teuchos::NO_TRANS);
254
255 // Apply op through NOX Group interface
256 Teuchos::RCP<JacobianEvaluator> model = Teuchos::rcp(new JacobianEvaluator(op));
257 // Need to pass a lows factory to the group to call computeJacobian
258 Stratimikos::DefaultLinearSolverBuilder builder;
259 Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList();
260 p->set("Linear Solver Type", "Belos");
261 p->set("Preconditioner Type", "None");
262 builder.setParameterList(p);
263 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
264 lowsf = builder.createLinearSolveStrategy("");
265 Teuchos::RCP<NOX::Thyra::Group> group =
266 Teuchos::rcp(new NOX::Thyra::Group(dynamic_cast<NTV&>((*z_nox)[0]), model, op, lowsf, Teuchos::null, Teuchos::null, Teuchos::null));
267 group->computeJacobian();
268 group->applyJacobianMultiVector(*z_nox, *y_nox);
269
270 // Check for correct results
271 mag_type val = static_cast<mag_type>(ST::squareroot(static_cast<Scalar>(2.0)));
272 auto ans = Teuchos::tuple(val, 2.0*val, 3.0*val, 4.0*val, 5.0*val);
273 checkMultiVectors(x_tpetra, y_tpetra, ans, out, success);
274 }
275
276 TEUCHOS_UNIT_TEST(Tpetra_OpTests, VecTrans)
277 {
278 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
279 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
280
281 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
282 Teuchos::RCP<const TVSB> space = Thyra::createVectorSpace<Scalar,LO,GO,Node>(map);
283
284 // Create vector objects
285 Teuchos::RCP<TVB> x = Thyra::createMember(space);
286 Teuchos::RCP<TVB> y = Thyra::createMember(space);
287 Teuchos::RCP<TVB> z = Thyra::createMember(space);
288
289 Scalar one = ST::one();
290 Thyra::assign(x.ptr(), one);
291 Thyra::assign(y.ptr(), one);
292 Thyra::assign(z.ptr(), one);
293
294 Teuchos::RCP<TV> x_tpetra = TOVE::getTpetraVector(x);
295 Teuchos::RCP<TV> y_tpetra = TOVE::getTpetraVector(y);
296 Teuchos::RCP<TV> z_tpetra = TOVE::getTpetraVector(z);
297
298 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y));
299 Teuchos::RCP<NTV> z_nox = Teuchos::rcp(new NTV(z));
300
301 // Create operator objects
302 Teuchos::RCP<CRSM> op_tpetra = createTpetraOp(map);
303 Teuchos::RCP<TLOB> op = Thyra::tpetraLinearOp<Scalar,LO,GO,Node>(space, space, op_tpetra);
304 TEUCHOS_TEST_EQUALITY(Thyra::opSupported(*op, Thyra::TRANS), true, out, success);
305
306 // Apply op with Tpetra directly
307 op_tpetra->apply(*z_tpetra, *x_tpetra, Teuchos::TRANS);
308
309 // Apply op through NOX Group interface
310 Teuchos::RCP<JacobianEvaluator> model = Teuchos::rcp(new JacobianEvaluator(op));
311 // Need to pass a lows factory to the group to call computeJacobian
312 Stratimikos::DefaultLinearSolverBuilder builder;
313 Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList();
314 p->set("Linear Solver Type", "Belos");
315 p->set("Preconditioner Type", "None");
316 builder.setParameterList(p);
317 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
318 lowsf = builder.createLinearSolveStrategy("");
319 Teuchos::RCP<NOX::Thyra::Group> group =
320 Teuchos::rcp(new NOX::Thyra::Group(*z_nox, model, op, lowsf, Teuchos::null, Teuchos::null, Teuchos::null));
321 group->computeJacobian();
322 // Need this to get the group to call updateLOWS, or else shared_jacobian is unitialized
323 (void) group->getJacobian();
324 group->applyJacobianTranspose(*z_nox, *y_nox);
325
326 // Check for correct results
327 mag_type val = static_cast<mag_type>(ST::squareroot(static_cast<Scalar>(2.0)));
328 auto ans = Teuchos::tuple(val);
329 checkMultiVectors(x_tpetra, y_tpetra, ans, out, success);
330 }
331
332 TEUCHOS_UNIT_TEST(Tpetra_OpTests, MultiVecTrans)
333 {
334 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
335 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
336 const std::size_t numCols = 5;
337
338 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
339 Teuchos::RCP<const TVSB> space = Thyra::createVectorSpace<Scalar,LO,GO,Node>(map);
340
341 // Create vector objects
342 Teuchos::RCP<TMVB> x = Thyra::createMembers(space, numCols);
343 Teuchos::RCP<TMVB> y = Thyra::createMembers(space, numCols);
344 Teuchos::RCP<TMVB> z = Thyra::createMembers(space, numCols);
345
346 Scalar one = ST::one();
347 Thyra::assign(x.ptr(), one);
348 Thyra::assign(y.ptr(), one);
349 Thyra::assign(z.ptr(), one);
350
351 Teuchos::RCP<TMV> x_tpetra = TOVE::getTpetraMultiVector(x);
352 Teuchos::RCP<TMV> y_tpetra = TOVE::getTpetraMultiVector(y);
353 Teuchos::RCP<TMV> z_tpetra = TOVE::getTpetraMultiVector(z);
354
355 Teuchos::RCP<NTMV> y_nox = Teuchos::rcp(new NTMV(y));
356 Teuchos::RCP<NTMV> z_nox = Teuchos::rcp(new NTMV(z));
357
358 auto scales = Teuchos::tuple(one, 2.0*one, 3.0*one, 4.0*one, 5.0*one);
359 x_tpetra->scale(scales);
360 y_tpetra->scale(scales);
361 z_tpetra->scale(scales);
362
363 // Create operator objects
364 Teuchos::RCP<CRSM> op_tpetra = createTpetraOp(map);
365 Teuchos::RCP<TLOB> op = Thyra::tpetraLinearOp<Scalar,LO,GO,Node>(space, space, op_tpetra);
366 TEUCHOS_TEST_EQUALITY(Thyra::opSupported(*op, Thyra::TRANS), true, out, success);
367
368 // Apply op with Tpetra directly
369 op_tpetra->apply(*z_tpetra, *x_tpetra, Teuchos::TRANS);
370
371 // Apply op through NOX Group interface
372 Teuchos::RCP<JacobianEvaluator> model = Teuchos::rcp(new JacobianEvaluator(op));
373 // Need to pass a lows factory to the group to call computeJacobian even though it isn't used
374 Stratimikos::DefaultLinearSolverBuilder builder;
375 Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList();
376 p->set("Linear Solver Type", "Belos");
377 p->set("Preconditioner Type", "None");
378 builder.setParameterList(p);
379 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
380 lowsf = builder.createLinearSolveStrategy("");
381 Teuchos::RCP<NOX::Thyra::Group> group =
382 Teuchos::rcp(new NOX::Thyra::Group(dynamic_cast<NTV&>((*z_nox)[0]), model, op, lowsf, Teuchos::null, Teuchos::null, Teuchos::null));
383 group->computeJacobian();
384 // Need this to get the group to call updateLOWS, or else shared_jacobian is unitialized
385 (void) group->getJacobian();
386 group->applyJacobianTransposeMultiVector(*z_nox, *y_nox);
387
388 // Check for correct results
389 mag_type val = static_cast<mag_type>(ST::squareroot(static_cast<Scalar>(2.0)));
390 auto ans = Teuchos::tuple(val, 2.0*val, 3.0*val, 4.0*val, 5.0*val);
391 checkMultiVectors(x_tpetra, y_tpetra, ans, out, success);
392 }