Debian Codegraph @

packages/nox/test/tpetra/1DFEM_Functors.hpp

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 #ifndef NOX_TPETRA_1DFEM_FUNCTORS_HPP
10 #define NOX_TPETRA_1DFEM_FUNCTORS_HPP
11
12 #include "Kokkos_Core.hpp"
13 #include "Tpetra_Details_OrdinalTraits.hpp"
14
15 template<class TpetraVectorType>
16 struct MeshFillFunctor
17 {
18 typedef typename TpetraVectorType::impl_scalar_type Scalar;
19 typedef typename TpetraVectorType::local_ordinal_type LO;
20 typedef typename TpetraVectorType::global_ordinal_type GO;
21 typedef typename TpetraVectorType::dual_view_type::t_dev ViewType;
22
23 ViewType coordsView_;
24 const Scalar zMin_;
25 const Scalar dz_;
26 const GO minGID_;
27
28 MeshFillFunctor(TpetraVectorType& coords,
29 const Scalar& zMin,
30 const Scalar& dz,
31 const GO& minGID) :
32 coordsView_(coords.getLocalViewDevice(Tpetra::Access::ReadWrite)),
33 zMin_(zMin),
34 dz_(dz),
35 minGID_(minGID)
36 {}
37
38 KOKKOS_INLINE_FUNCTION
39 void operator() (const LO node) const
40 {
41 coordsView_(node,0) = zMin_ + static_cast<Scalar>(minGID_ + node) * dz_;
42 }
43
44 };
45
46 //==================================================================
47
48 template <class size_type, class LO>
49 struct RowCountsFunctor
50 {
51 const Kokkos::View<size_type*> counts_;
52 const LO numMyNodes_;
53 const int numProcs_;
54 const int myRank_;
55
56 RowCountsFunctor(const Kokkos::View<size_type*>& counts,
57 const LO& numMyNodes,
58 const int& numProcs,
59 const int& myRank) :
60 counts_(counts),
61 numMyNodes_(numMyNodes),
62 numProcs_(numProcs),
63 myRank_(myRank)
64 {}
65
66 KOKKOS_INLINE_FUNCTION
67 void operator() (const LO localRow, std::size_t& curNumLocalEntries) const
68 {
69 // Add a diagonal matrix entry
70 Kokkos::atomic_inc(&counts_(localRow));
71 ++curNumLocalEntries;
72 // Contribute a matrix entry to the previous row
73 if (localRow > 0) {
74 Kokkos::atomic_inc(&counts_(localRow-1));
75 ++curNumLocalEntries;
76 }
77 // Contribute a matrix entry to the next row
78 if (localRow < numMyNodes_-1) {
79 Kokkos::atomic_inc(&counts_(localRow+1));
80 ++curNumLocalEntries;
81 }
82 // MPI process to the left sends us an entry
83 if ((myRank_ > 0) && (localRow == 0)) {
84 Kokkos::atomic_inc(&counts_(localRow));
85 ++curNumLocalEntries;
86 }
87 // MPI process to the right sends us an entry
88 if ((myRank_ < numProcs_-1) && (localRow == numMyNodes_-1)) {
89 Kokkos::atomic_inc(&counts_(localRow));
90 ++curNumLocalEntries;
91 }
92 }
93
94 };
95
96 //==================================================================
97
98 template <class size_type, class LO>
99 struct RowOffsetsFunctor
100 {
101 const Kokkos::View<size_type*> offsets_;
102 const Kokkos::View<size_type*> counts_;
103 const LO numMyNodes_;
104
105 RowOffsetsFunctor(const Kokkos::View<size_type*>& offsets,
106 const Kokkos::View<size_type*>& counts,
107 const LO& numMyNodes) :
108 offsets_(offsets),
109 counts_(counts),
110 numMyNodes_(numMyNodes)
111 {}
112
113 KOKKOS_INLINE_FUNCTION
114 void operator() (const LO localRow, size_type& update, const bool final) const
115 {
116 if (final) {
117 offsets_(localRow) = update;
118 }
119 if (localRow < numMyNodes_) {
120 update += counts_(localRow);
121 }
122 }
123
124 };
125
126 //==================================================================
127
128 template <class size_type, class LO>
129 struct ColumnIndexCompFunctor
130 {
131 const Kokkos::View<LO*> indices_;
132 const Kokkos::View<size_type*> offsets_;
133 const Kokkos::View<size_type*> counts_;
134 const LO numMyNodes_;
135 const int numProcs_;
136 const int myRank_;
137
138 ColumnIndexCompFunctor(const Kokkos::View<LO*> indices,
139 const Kokkos::View<size_type*>& offsets,
140 const Kokkos::View<size_type*>& counts,
141 const LO& numMyNodes,
142 const int& numProcs,
143 const int& myRank) :
144 indices_(indices),
145 offsets_(offsets),
146 counts_(counts),
147 numMyNodes_(numMyNodes),
148 numProcs_(numProcs),
149 myRank_(myRank)
150 {}
151
152 KOKKOS_INLINE_FUNCTION
153 void operator() (const LO localRow) const
154 {
155 // Get local column index for diagonal entry
156 LO localCol = (myRank_ > 0) ? (localRow + 1) : localRow;
157
158 // Add a diagonal matrix entry
159 size_type negOne = static_cast<size_type>(-1);
160 {
161 // Decrementing row counts back to 0
162 size_type count = Kokkos::atomic_fetch_add(&counts_(localRow), negOne) + negOne;
163 indices_(offsets_(localRow) + count) = localCol;
164 }
165 // Contribute a matrix entry to the previous row
166 if (localRow > 0) {
167 size_type count = Kokkos::atomic_fetch_add(&counts_(localRow-1), negOne) + negOne;
168 indices_(offsets_(localRow-1) + count) = localCol;
169 }
170 // Contribute a matrix entry to the next row
171 if (localRow < numMyNodes_-1) {
172 size_type count = Kokkos::atomic_fetch_add(&counts_(localRow+1), negOne) + negOne;
173 indices_(offsets_(localRow+1) + count) = localCol;
174 }
175 // MPI process to the left sends us an entry
176 if ((myRank_ > 0) && (localRow == 0)) {
177 size_type count = Kokkos::atomic_fetch_add(&counts_(localRow), negOne) + negOne;
178 indices_(offsets_(localRow) + count) = localCol - 1;
179 }
180 // MPI process to the right sends us an entry
181 if ((myRank_ < numProcs_-1) && (localRow == numMyNodes_-1)) {
182 size_type count = Kokkos::atomic_fetch_add(&counts_(localRow), negOne) + negOne;
183 indices_(offsets_(localRow) + count) = localCol + 1;
184 }
185
186 }
187
188 };
189
190 //==================================================================
191
192 // Finite Element Basis Object
193 template <class Scalar, class LO>
194 class Basis {
195
196 public:
197 // Constructor
198 KOKKOS_INLINE_FUNCTION
199 Basis():
200 uu(0.0),
201 zz(0.0),
202 duu(0.0),
203 eta(0.0),
204 wt(0.0),
205 dz(0.0),
206 uuold(0.0),
207 duuold(0.0)
208 {}
209
210 // Destructor
211 KOKKOS_INLINE_FUNCTION
212 ~Basis()
213 {}
214
215 // Calculates the values of z and u at the specified Gauss point
216 KOKKOS_INLINE_FUNCTION
217 void computeBasis(LO gp, Scalar* z, Scalar* u, Scalar* uold = 0) {
218 if (gp==0) {eta=-1.0/sqrt(3.0); wt=1.0;}
219 if (gp==1) {eta=1.0/sqrt(3.0); wt=1.0;}
220
221 // Calculate basis function and derivatives at Gauss point
222 phi[0]=(1.0-eta)/2.0;
223 phi[1]=(1.0+eta)/2.0;
224 dphide[0]=-0.5;
225 dphide[1]=0.5;
226
227 // Caculate function and derivative approximations at GP.
228 dz=0.5*(z[1]-z[0]);
229 zz=0.0;
230 uu=0.0;
231 duu=0.0;
232 uuold=0.0;
233 duuold=0.0;
234 for (LO i=0; i < 2; i++) {
235 zz += z[i] * phi[i];
236 uu += u[i] * phi[i];
237 duu += u[i] * dphide[i];
238 if (uold) {
239 uuold += uold[i] * phi[i];
240 duuold += uold[i] * dphide[i];
241 }
242 }
243 }
244
245 public:
246 // Variables that are calculated at the Gauss point
247 Scalar phi[2];
248 Scalar dphide[2];
249 Scalar uu;
250 Scalar zz;
251 Scalar duu;
252 Scalar eta;
253 Scalar wt;
254 Scalar dz;
255 // These are only needed for transient
256 Scalar uuold;
257 Scalar duuold;
258 };
259
260 //==================================================================
261
262 template <class TpetraVectorType>
263 struct ResidualEvaluatorFunctor
264 {
265 typedef typename TpetraVectorType::impl_scalar_type scalar_type;
266 typedef typename TpetraVectorType::local_ordinal_type local_ordinal_type;
267 typedef typename TpetraVectorType::map_type map_type;
268 typedef typename map_type::local_map_type local_map_type;
269 typedef typename TpetraVectorType::dual_view_type dual_view_type;
270 typedef typename dual_view_type::t_dev view_type;
271 typedef typename view_type::const_type const_view_type;
272
273 const view_type f_view_;
274 const const_view_type x_view_;
275 const const_view_type u_view_;
276 const local_map_type row_map_;
277 const local_map_type col_map_;
278 const int myRank_;
279 const scalar_type k_;
280 const scalar_type p4_;
281
282 ResidualEvaluatorFunctor(TpetraVectorType& f,
283 const TpetraVectorType& x,
284 const TpetraVectorType& u,
285 const int& myRank,
286 const typename TpetraVectorType::impl_scalar_type& k,
287 const typename TpetraVectorType::impl_scalar_type& p4) :
288 f_view_(f.getLocalViewDevice(Tpetra::Access::ReadWrite)),
289 x_view_(x.getLocalViewDevice(Tpetra::Access::ReadOnly)),
290 u_view_(u.getLocalViewDevice(Tpetra::Access::ReadOnly)),
291 row_map_(f.getMap()->getLocalMap()),
292 col_map_(u.getMap()->getLocalMap()),
293 myRank_(myRank),
294 k_(k),
295 p4_(p4)
296 {}
297
298 // Adds the contribution from element elt to the residual vector
299 KOKKOS_INLINE_FUNCTION
300 void operator() (const local_ordinal_type elt) const
301 {
302 // Get the solution and coordinates at the nodes
303 scalar_type xx[2];
304 xx[0] = x_view_(elt,0);
305 xx[1] = x_view_(elt+1,0);
306
307 scalar_type uu[2];
308 uu[0] = u_view_(elt,0);
309 uu[1] = u_view_(elt+1,0);
310
311 Basis<scalar_type, local_ordinal_type> basis;
312
313 // Loop Over Gauss Points
314 for(local_ordinal_type gp = 0; gp < 2; ++gp) {
315 // Calculate the basis function at the gauss point
316 basis.computeBasis(gp, xx, uu);
317
318 // Loop over nodes in element
319 for (local_ordinal_type i = 0; i < 2; ++i) {
320 local_ordinal_type localRow = row_map_.getLocalElement(col_map_.getGlobalElement(elt+i));
321 const local_ordinal_type invalid = Tpetra::Details::OrdinalTraits<local_ordinal_type>::invalid();
322
323 if (localRow != invalid) {
324 scalar_type value = basis.wt * basis.dz * (basis.uu * basis.uu * basis.phi[i] * k_
325 + (basis.duu * basis.dphide[i])/(basis.dz * basis.dz));
326 Kokkos::atomic_add(&f_view_(localRow,0), value);
327 }
328 }
329 }
330
331 // Correct for Dirichlet BCs
332 if ((myRank_ == 0) && (elt == 0)) {
333 scalar_type value = u_view_(0,0) - p4_;
334 Kokkos::atomic_exchange(&f_view_(0,0), value);
335 }
336
337 }
338
339 };
340
341 //==================================================================
342
343 template <class TpetraVectorType, class TpetraMatrixType>
344 struct JacobianEvaluatorFunctor
345 {
346 typedef typename TpetraVectorType::impl_scalar_type scalar_type;
347 typedef typename TpetraVectorType::local_ordinal_type local_ordinal_type;
348 typedef typename TpetraVectorType::map_type map_type;
349 typedef typename map_type::local_map_type local_map_type;
350 typedef typename TpetraVectorType::dual_view_type dual_view_type;
351 typedef typename dual_view_type::t_dev::const_type const_view_type;
352 typedef typename TpetraMatrixType::local_matrix_device_type local_matrix_type;
353
354 const local_matrix_type J_local_;
355 const const_view_type x_view_;
356 const const_view_type u_view_;
357 const local_map_type row_map_;
358 const local_map_type col_map_;
359 const int myRank_;
360 const scalar_type k_;
361
362 JacobianEvaluatorFunctor(const TpetraMatrixType& J,
363 const TpetraVectorType& x,
364 const TpetraVectorType& u,
365 const int& myRank,
366 const typename TpetraVectorType::impl_scalar_type& k) :
367 J_local_(J.getLocalMatrixDevice()),
368 x_view_(x.getLocalViewDevice(Tpetra::Access::ReadOnly)),
369 u_view_(u.getLocalViewDevice(Tpetra::Access::ReadOnly)),
370 row_map_(J.getRowMap()->getLocalMap()),
371 col_map_(J.getColMap()->getLocalMap()),
372 myRank_(myRank),
373 k_(k)
374 {}
375
376 // Adds the contribution from element elt to the Jacobian matrix
377 KOKKOS_INLINE_FUNCTION
378 void operator() (const local_ordinal_type elt) const
379 {
380 // Get the solution and coordinates at the nodes
381 scalar_type xx[2];
382 xx[0]=x_view_(elt,0);
383 xx[1]=x_view_(elt+1,0);
384
385 scalar_type uu[2];
386 uu[0]=u_view_(elt,0);
387 uu[1]=u_view_(elt+1,0);
388
389 Basis<scalar_type, local_ordinal_type> basis;
390
391 // Loop Over Gauss Points
392 for(local_ordinal_type gp = 0; gp < 2; ++gp) {
393 // Calculate the basis function at the gauss point
394 basis.computeBasis(gp, xx, uu);
395
396 // Loop over nodes in element
397 for (local_ordinal_type i = 0; i < 2; ++i) {
398 local_ordinal_type localRow = row_map_.getLocalElement(col_map_.getGlobalElement(elt+i));
399 const local_ordinal_type invalid = Tpetra::Details::OrdinalTraits<local_ordinal_type>::invalid();
400
401 if (localRow != invalid) {
402 // Loop over trial functions
403 for(local_ordinal_type j = 0; j < 2; ++j) {
404 const local_ordinal_type localColumn = elt + j;
405 scalar_type value = basis.wt * basis.dz
406 * ((basis.dphide[j]*basis.dphide[i])/(basis.dz*basis.dz)
407 + 2.0*basis.uu*basis.phi[j]*basis.phi[i]*k_);
408
409 J_local_.sumIntoValues(localRow, &localColumn, 1, &value, false, true);
410 }
411 }
412 }
413
414 // Correct for Dirichlet BCs
415 if ((myRank_ == 0) && (elt == 0)) {
416 local_ordinal_type row = 0;
417 local_ordinal_type column = 0;
418 scalar_type value = 1.0;
419 J_local_.replaceValues(row, &column, 1, &value);
420 column = 1;
421 value = 0.0;
422 J_local_.replaceValues(row, &column, 1, &value);
423 }
424
425 }
426 }
427
428 };
429
430 //==================================================================
431
432 template <class TpetraVectorType, class TpetraMatrixType>
433 struct PreconditionerEvaluatorFunctor
434 {
435 typedef typename TpetraVectorType::impl_scalar_type scalar_type;
436 typedef typename TpetraVectorType::local_ordinal_type local_ordinal_type;
437 typedef typename TpetraVectorType::map_type map_type;
438 typedef typename map_type::local_map_type local_map_type;
439 typedef typename TpetraVectorType::dual_view_type dual_view_type;
440 typedef typename dual_view_type::t_dev::const_type const_view_type;
441 typedef typename TpetraMatrixType::local_matrix_device_type local_matrix_type;
442
443 const local_matrix_type M_local_;
444 const const_view_type x_view_;
445 const const_view_type u_view_;
446 const local_map_type row_map_;
447 const local_map_type col_map_;
448 const int myRank_;
449 const scalar_type k_;
450
451 PreconditionerEvaluatorFunctor(const TpetraMatrixType& M,
452 const TpetraVectorType& x,
453 const TpetraVectorType& u,
454 const int& myRank,
455 const typename TpetraVectorType::impl_scalar_type& k) :
456 M_local_(M.getLocalMatrixDevice()),
457 x_view_(x.getLocalViewDevice(Tpetra::Access::ReadOnly)),
458 u_view_(u.getLocalViewDevice(Tpetra::Access::ReadOnly)),
459 row_map_(M.getRowMap()->getLocalMap()),
460 col_map_(M.getColMap()->getLocalMap()),
461 myRank_(myRank),
462 k_(k)
463 {}
464
465 // Adds the contribution from element elt to the preconditioner matrix
466 KOKKOS_INLINE_FUNCTION
467 void operator() (const local_ordinal_type elt) const
468 {
469 // Get the solution and coordinates at the nodes
470 scalar_type xx[2];
471 xx[0]=x_view_(elt,0);
472 xx[1]=x_view_(elt+1,0);
473
474 scalar_type uu[2];
475 uu[0]=u_view_(elt,0);
476 uu[1]=u_view_(elt+1,0);
477
478 // Loop Over Gauss Points
479 for(local_ordinal_type gp = 0; gp < 2; ++gp) {
480 // Calculate the basis function at the gauss point
481 Basis<scalar_type, local_ordinal_type> basis;
482 basis.computeBasis(gp, xx, uu);
483
484 // Loop over nodes in element
485 for (local_ordinal_type i = 0; i < 2; ++i) {
486 local_ordinal_type localRow = row_map_.getLocalElement(col_map_.getGlobalElement(elt+i));
487 const local_ordinal_type invalid = Tpetra::Details::OrdinalTraits<local_ordinal_type>::invalid();
488
489 // Loop over trial functions
490 if (localRow != invalid) {
491 for(local_ordinal_type j = 0; j < 2; ++j) {
492 const local_ordinal_type localColumn = elt + j;
493 if (row_map_.getGlobalElement(localRow) == col_map_.getGlobalElement(localColumn)) {
494 scalar_type value = basis.wt * basis.dz
495 * ((basis.dphide[j]*basis.dphide[i])/(basis.dz*basis.dz)
496 + 2.0*basis.uu*basis.phi[j]*basis.phi[i]*k_);
497 // Do we need to set force_atomic true in this?
498 M_local_.sumIntoValues(localRow, &localColumn, 1, &value);
499 }
500 }
501 }
502 }
503 }
504
505 // Correct for Dirichlet BCs
506 if ((myRank_ == 0) && (elt == 0)) {
507 local_ordinal_type row = 0;
508 local_ordinal_type column = 0;
509 scalar_type value = 1.0;
510 M_local_.replaceValues(row, &column, 1, &value);
511 }
512
513 }
514
515 };
516
517 //==================================================================
518
519 template <class TpetraVectorType>
520 struct DfDp2EvaluatorFunctor
521 {
522 typedef typename TpetraVectorType::impl_scalar_type scalar_type;
523 typedef typename TpetraVectorType::local_ordinal_type local_ordinal_type;
524 typedef typename TpetraVectorType::map_type map_type;
525 typedef typename map_type::local_map_type local_map_type;
526 typedef typename TpetraVectorType::dual_view_type dual_view_type;
527 typedef typename dual_view_type::t_dev view_type;
528 typedef typename dual_view_type::t_dev::const_type const_view_type;
529
530 const view_type f_view_;
531 const const_view_type x_view_;
532 const const_view_type u_view_;
533 const local_map_type row_map_;
534 const local_map_type col_map_;
535 const int myRank_;
536 const scalar_type k_;
537
538 DfDp2EvaluatorFunctor(TpetraVectorType& f,
539 const TpetraVectorType& x,
540 const TpetraVectorType& u,
541 const int& myRank,
542 const typename TpetraVectorType::impl_scalar_type& k) :
543 f_view_(f.getLocalViewDevice(Tpetra::Access::ReadWrite)),
544 x_view_(x.getLocalViewDevice(Tpetra::Access::ReadOnly)),
545 u_view_(u.getLocalViewDevice(Tpetra::Access::ReadOnly)),
546 row_map_(f.getMap()->getLocalMap()),
547 col_map_(u.getMap()->getLocalMap()),
548 myRank_(myRank),
549 k_(k)
550 {}
551
552 // Adds the contribution from element elt to the residual vector
553 KOKKOS_INLINE_FUNCTION
554 void operator() (const local_ordinal_type elt) const
555 {
556 // Get the solution and coordinates at the nodes
557 scalar_type xx[2];
558 xx[0] = x_view_(elt,0);
559 xx[1] = x_view_(elt+1,0);
560
561 scalar_type uu[2];
562 uu[0] = u_view_(elt,0);
563 uu[1] = u_view_(elt+1,0);
564
565 Basis<scalar_type, local_ordinal_type> basis;
566
567 // Loop Over Gauss Points
568 for(local_ordinal_type gp = 0; gp < 2; ++gp) {
569 // Calculate the basis function at the gauss point
570 basis.computeBasis(gp, xx, uu);
571
572 // Loop over nodes in element
573 for (local_ordinal_type i = 0; i < 2; ++i) {
574 local_ordinal_type localRow = row_map_.getLocalElement(col_map_.getGlobalElement(elt+i));
575 const local_ordinal_type invalid = Tpetra::Details::OrdinalTraits<local_ordinal_type>::invalid();
576
577 if (localRow != invalid) {
578 scalar_type value = basis.wt * basis.dz * (basis.uu * basis.uu * basis.phi[i]);
579 // + (basis.duu * basis.dphide[i])/(basis.dz * basis.dz));
580 Kokkos::atomic_add(&f_view_(localRow,0), value);
581 }
582 }
583 }
584
585 // Correct for Dirichlet BCs
586 if ((myRank_ == 0) && (elt == 0)) {
587 scalar_type value = 0.0;
588 Kokkos::atomic_exchange(&f_view_(0,0), value);
589 }
590
591 }
592
593 };
594
595 #endif