Debian Codegraph @

packages/trilinoscouplings/examples/fenl/fenl.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #ifndef KOKKOS_EXAMPLE_FENL_HPP
18 #define KOKKOS_EXAMPLE_FENL_HPP
19
20 #include "TrilinosCouplings_config.h"
21 #ifdef HAVE_TRILINOSCOUPLINGS_SACADO
22 #include "Sacado.hpp"
23 #include "Sacado_mpl_apply.hpp"
24 #endif
25
26 #include <iostream>
27
28 #include <stdlib.h>
29 #include <BoxElemPart.hpp>
30
31 #include <Teuchos_Comm.hpp>
32 #include <Teuchos_GlobalMPISession.hpp>
33 #include <Tpetra_KokkosCompat_ClassicNodeAPI_Wrapper.hpp>
34
35 #include <Kokkos_Core.hpp>
36 #include <KokkosSparse_CrsMatrix.hpp>
37 #include <Kokkos_ArithTraits.hpp>
38
39 #include <SGPreconditioner.hpp>
40
41 namespace Kokkos {
42 namespace Example {
43 namespace FENL {
44
45 inline
46 double maximum( const Teuchos::Comm<int>& comm , double local )
47 {
48 double global = 0 ;
49 Teuchos::reduceAll( comm , Teuchos::REDUCE_MAX , 1 , & local , & global );
50 return global ;
51 }
52
53 // Struct storing performance statistics
54 struct Perf {
55 size_t uq_count ;
56 size_t global_elem_count ;
57 size_t global_node_count ;
58 size_t newton_iter_count ;
59 size_t cg_iter_count ;
60 double map_ratio ;
61 double fill_node_set ;
62 double scan_node_count ;
63 double fill_graph_entries ;
64 double sort_graph_entries ;
65 double fill_element_graph ;
66 double create_sparse_matrix ;
67 double fill_time ;
68 double tangent_fill_time ;
69 double import_time ;
70 double bc_time ;
71 double mat_vec_time ;
72 double cg_iter_time ;
73 double prec_setup_time ;
74 double prec_apply_time ;
75 double cg_total_time ;
76 double newton_total_time ;
77 double newton_residual ;
78 double error_max ;
79 double response_mean ;
80 double response_std_dev ;
81
82 std::vector<size_t> ensemble_cg_iter_count;
83
84 Perf() : uq_count(1) ,
85 global_elem_count(0) ,
86 global_node_count(0) ,
87 newton_iter_count(0) ,
88 cg_iter_count(0) ,
89 map_ratio(0) ,
90 fill_node_set(0) ,
91 scan_node_count(0) ,
92 fill_graph_entries(0) ,
93 sort_graph_entries(0) ,
94 fill_element_graph(0) ,
95 create_sparse_matrix(0) ,
96 fill_time(0) ,
97 tangent_fill_time(0) ,
98 import_time(0) ,
99 bc_time(0) ,
100 mat_vec_time(0) ,
101 cg_iter_time(0) ,
102 prec_setup_time(0) ,
103 prec_apply_time(0) ,
104 cg_total_time(0) ,
105 newton_total_time(0) ,
106 newton_residual(0) ,
107 error_max(0) ,
108 response_mean(0) ,
109 response_std_dev(0),
110 ensemble_cg_iter_count() {}
111
112 void increment(const Perf& p, const bool accumulate_solve_times) {
113 global_elem_count = p.global_elem_count;
114 global_node_count = p.global_node_count;
115
116 newton_iter_count += p.newton_iter_count;
117 cg_iter_count += p.cg_iter_count;
118 map_ratio += p.map_ratio;
119 fill_node_set += p.fill_node_set;
120 scan_node_count += p.scan_node_count;
121 fill_graph_entries += p.fill_graph_entries;
122 sort_graph_entries += p.sort_graph_entries;
123 fill_element_graph += p.fill_element_graph;
124 create_sparse_matrix += p.create_sparse_matrix;
125 fill_time += p.fill_time;
126 tangent_fill_time += p.tangent_fill_time;
127 import_time += p.import_time;
128 bc_time += p.bc_time ;
129 newton_total_time += p.newton_total_time ;
130 newton_residual += p.newton_residual ;
131 error_max += p.error_max;
132
133 const int n = p.ensemble_cg_iter_count.size();
134 ensemble_cg_iter_count.resize(n);
135 for (int i=0; i<n; ++i)
136 ensemble_cg_iter_count[i] += p.ensemble_cg_iter_count[i];
137
138 if (accumulate_solve_times) {
139 mat_vec_time += p.mat_vec_time;
140 cg_iter_time += p.cg_iter_time;
141 prec_setup_time += p.prec_setup_time;
142 prec_apply_time += p.prec_apply_time;
143 cg_total_time += p.cg_total_time;
144 }
145 else {
146 mat_vec_time = p.mat_vec_time;
147 cg_iter_time = p.cg_iter_time;
148 prec_setup_time = p.prec_setup_time;
149 prec_apply_time = p.prec_apply_time;
150 cg_total_time = p.cg_total_time;
151 }
152 }
153
154 void min(const Perf& p) {
155 map_ratio = std::min( map_ratio , p. map_ratio );
156 fill_node_set = std::min( fill_node_set , p.fill_node_set );
157 scan_node_count = std::min( scan_node_count , p.scan_node_count );
158 fill_graph_entries = std::min( fill_graph_entries , p.fill_graph_entries );
159 sort_graph_entries = std::min( sort_graph_entries , p.sort_graph_entries );
160 fill_element_graph = std::min( fill_element_graph , p.fill_element_graph );
161 create_sparse_matrix= std::min( create_sparse_matrix , p.create_sparse_matrix );
162 import_time = std::min( import_time , p.import_time );
163 fill_time = std::min( fill_time , p.fill_time );
164 tangent_fill_time = std::min( tangent_fill_time , p.tangent_fill_time );
165 bc_time = std::min( bc_time , p.bc_time );
166 mat_vec_time = std::min( mat_vec_time , p.mat_vec_time );
167 cg_iter_time = std::min( cg_iter_time , p.cg_iter_time );
168 prec_setup_time = std::min( prec_setup_time , p.prec_setup_time );
169 prec_apply_time = std::min( prec_apply_time , p.prec_apply_time );
170 cg_total_time = std::min( cg_total_time , p.cg_total_time );
171 newton_total_time = std::min( newton_total_time , p.newton_total_time );
172 }
173
174 void reduceMax(const Teuchos::Comm<int>& comm) {
175 map_ratio = maximum( comm , map_ratio);
176 fill_node_set = maximum( comm , fill_node_set);
177 scan_node_count = maximum( comm , scan_node_count);
178 fill_graph_entries = maximum( comm , fill_graph_entries);
179 sort_graph_entries = maximum( comm , sort_graph_entries);
180 fill_element_graph = maximum( comm , fill_element_graph);
181 create_sparse_matrix = maximum( comm , create_sparse_matrix);
182 import_time = maximum( comm , import_time );
183 fill_time = maximum( comm , fill_time );
184 tangent_fill_time = maximum( comm , tangent_fill_time );
185 bc_time = maximum( comm , bc_time );
186 mat_vec_time = maximum( comm , mat_vec_time );
187 cg_iter_time = maximum( comm , cg_iter_time );
188 prec_setup_time = maximum( comm , prec_setup_time );
189 prec_apply_time = maximum( comm , prec_apply_time );
190 cg_total_time = maximum( comm , cg_total_time );
191 newton_total_time = maximum( comm , newton_total_time );
192 }
193 };
194
195 inline double scalar_norm(double x) { return x; }
196
197 // Struct for storing UQ quadrature data
198 template <typename Device>
199 struct QuadratureData {
200 typedef Kokkos::View<double*, Kokkos::LayoutLeft, Device> quad_weights_type;
201 typedef Kokkos::View<double**, Kokkos::LayoutLeft, Device> quad_values_type;
202 quad_weights_type weights_view;
203 quad_values_type points_view;
204 quad_values_type values_view;
205
206 QuadratureData() {}
207 };
208
209 //----------------------------------------------------------------------------
210
211 // Traits class for creating strided local views for embedded ensemble-based,
212 // specialized for ensemble UQ scalar type
213 template <typename ViewType, typename Enabled = void>
214 struct LocalViewTraits {
215 typedef ViewType view_type;
216 // typedef Kokkos::View<typename view_type::data_type,
217 // typename view_type::array_layout,
218 // typename view_type::execution_space,
219 // Kokkos::MemoryUnmanaged> local_view_type;
220 typedef const view_type& local_view_type;
221 typedef typename view_type::value_type local_value_type;
222 static const bool use_team = false;
223 KOKKOS_INLINE_FUNCTION
224 static local_view_type create_local_view(const view_type& v,
225 const unsigned local_rank)
226 { return v; }
227 };
228
229 // Traits class for replacing a nested scalar type with the local scalar type
230 template <typename ScalarType, typename LocalScalarType,
231 typename Enabled = void>
232 struct ReplaceLocalScalarType {
233 typedef LocalScalarType type;
234 };
235
236 #ifdef HAVE_TRILINOSCOUPLINGS_SACADO
237 template <typename ScalarType, typename LocalScalarType>
238 struct ReplaceLocalScalarType<ScalarType,LocalScalarType,typename std::enable_if< Sacado::IsFad<ScalarType>::value >::type> {
239 typedef typename Sacado::mpl::apply<ScalarType,LocalScalarType>::type type;
240 };
241 #endif
242
243 // Compute DeviceConfig struct's based on scalar type
244 template <typename ScalarType>
245 struct CreateDeviceConfigs {
246 static void eval( KokkosSparse::DeviceConfig& dev_config_elem,
247 KokkosSparse::DeviceConfig& dev_config_gath,
248 KokkosSparse::DeviceConfig& dev_config_bc ) {
249 dev_config_elem = KokkosSparse::DeviceConfig( 0 , 1 , 1 );
250 dev_config_gath = KokkosSparse::DeviceConfig( 0 , 1 , 1 );
251 dev_config_bc = KokkosSparse::DeviceConfig( 0 , 1 , 1 );
252 }
253 };
254
255 //----------------------------------------------------------------------------
256 // Diffusion coefficients
257 //----------------------------------------------------------------------------
258
259 // Constant
260 struct ElementComputationConstantCoefficient {
261 enum { is_constant = true };
262
263 const double coeff_k ;
264
265 KOKKOS_INLINE_FUNCTION
266 double operator()( const double point[], unsigned ensemble_rank ) const
267 { return coeff_k ; }
268
269 ElementComputationConstantCoefficient( const double val )
270 : coeff_k( val ) {}
271
272 ElementComputationConstantCoefficient( const ElementComputationConstantCoefficient & rhs )
273 : coeff_k( rhs.coeff_k ) {}
274 };
275
276 //----------------------------------------------------------------------------
277
278 // Linear
279 struct ElementComputationLinearCoefficient {
280 enum { is_constant = false };
281
282 const double a, b ;
283
284 KOKKOS_INLINE_FUNCTION
285 double operator()( const double point[], unsigned ensemble_rank ) const
286 { return a*point[0] + b ; }
287
288 ElementComputationLinearCoefficient( const double a_, const double b_ )
289 : a(a_), b(b_) {}
290
291 ElementComputationLinearCoefficient( const ElementComputationLinearCoefficient & rhs )
292 : a(rhs.a), b(rhs.b) {}
293 };
294
295 //----------------------------------------------------------------------------
296
297 // KL-like expansion from Webster & Gunzburger
298 template < typename Scalar, typename MeshScalar, typename Device >
299 class ElementComputationKLCoefficient {
300 public:
301
302 enum { is_constant = false };
303 typedef Kokkos::View<Scalar*, Kokkos::LayoutLeft, Device> RandomVariableView;
304 typedef Kokkos::View<MeshScalar*, Device> EigenView;
305 typedef typename RandomVariableView::size_type size_type;
306
307 typedef LocalViewTraits< RandomVariableView > local_rv_view_traits;
308 typedef typename local_rv_view_traits::local_view_type local_rv_view_type;
309 typedef typename local_rv_view_traits::local_value_type local_scalar_type;
310
311 const MeshScalar m_mean; // Mean of random field
312 const MeshScalar m_variance; // Variance of random field
313 const MeshScalar m_corr_len; // Correlation length of random field
314 const size_type m_num_rv; // Number of random variables
315 RandomVariableView m_rv; // KL random variables
316 const EigenView m_eig; // KL eigenvalues
317 const MeshScalar m_pi; // 3.1415...
318
319 public:
320
321 ElementComputationKLCoefficient( const MeshScalar mean ,
322 const MeshScalar variance ,
323 const MeshScalar correlation_length ,
324 const size_type num_rv ) :
325 m_mean( mean ),
326 m_variance( variance ),
327 m_corr_len( correlation_length ),
328 m_num_rv( num_rv ),
329 m_rv( "KL Random Variables", m_num_rv ),
330 m_eig( "KL Eigenvalues", m_num_rv ),
331 m_pi( 4.0*std::atan(1.0) )
332 {
333 typename EigenView::HostMirror host_eig =
334 Kokkos::create_mirror_view( m_eig );
335
336 const MeshScalar a = std::sqrt( std::sqrt(m_pi)*m_corr_len );
337
338 if (m_num_rv > 0)
339 host_eig(0) = a / std::sqrt( MeshScalar(2) );
340
341 for ( size_type i=1; i<m_num_rv; ++i ) {
342 const MeshScalar b = (i+1)/2; // floor((i+1)/2)
343 const MeshScalar c = b * m_pi * m_corr_len;
344 host_eig(i) = a * std::exp( -c*c / MeshScalar(8) );
345 }
346
347 Kokkos::deep_copy( m_eig , host_eig );
348 }
349
350 ElementComputationKLCoefficient( const ElementComputationKLCoefficient & rhs )
351 : m_mean( rhs.m_mean ) ,
352 m_variance( rhs.m_variance ) ,
353 m_corr_len( rhs.m_corr_len ) ,
354 m_num_rv( rhs.m_num_rv ) ,
355 m_rv( rhs.m_rv ) ,
356 m_eig( rhs.m_eig ) ,
357 m_pi( rhs.m_pi ) {}
358
359 KOKKOS_INLINE_FUNCTION
360 void setRandomVariables( const RandomVariableView& rv) { m_rv = rv; }
361
362 KOKKOS_INLINE_FUNCTION
363 RandomVariableView getRandomVariables() const { return m_rv; }
364
365 KOKKOS_INLINE_FUNCTION
366 local_scalar_type operator() ( const MeshScalar point[],
367 const size_type ensemble_rank ) const
368 {
369 local_rv_view_type local_rv =
370 local_rv_view_traits::create_local_view(m_rv, ensemble_rank);
371
372 local_scalar_type val = 0.0;
373 MeshScalar x = point[0];
374 if (m_num_rv > 0)
375 val += m_eig(0) * local_rv(0);
376 for ( size_type i=1; i<m_num_rv; i+=2 ) {
377 const MeshScalar b = (i+1)/2; // floor((i+1)/2)
378 val += m_eig(i) * std::sin( b * m_pi * x ) * local_rv(i);
379 }
380 for ( size_type i=2; i<m_num_rv; i+=2 ) {
381 const MeshScalar b = (i+1)/2; // floor((i+1)/2)
382 val += m_eig(i) * std::cos( b * m_pi * x ) * local_rv(i);
383 }
384
385 val = m_mean + m_variance * val;
386
387 return val;
388 }
389 };
390
391 template <typename CoeffType, typename FadType>
392 struct FadCoeffFunctionTraits {
393 typedef CoeffType type;
394 static type eval(const CoeffType& coeff_function) {
395 return coeff_function;
396 }
397 };
398
399 } /* namespace FENL */
400 } /* namespace Example */
401 } /* namespace Kokkos */
402
403 //----------------------------------------------------------------------------
404
405 #include "TrilinosCouplings_config.h"
406 #ifdef HAVE_TRILINOSCOUPLINGS_STOKHOS
407
408 #include "Stokhos_KL_ExponentialRandomField.hpp"
409
410 namespace Kokkos {
411 namespace Example {
412 namespace FENL {
413
414 template <typename T>
415 struct EnsembleTraits {
416 static const int size = 1;
417 typedef T value_type;
418 KOKKOS_INLINE_FUNCTION
419 static const value_type& coeff(const T& x, int i) { return x; }
420 KOKKOS_INLINE_FUNCTION
421 static value_type& coeff(T& x, int i) { return x; }
422 };
423
424 // Exponential KL from Stokhos
425 template < typename Scalar, typename MeshScalar, typename Device >
426 class ExponentialKLCoefficient {
427 public:
428
429 // Turn into a meta-function class usable with Sacado::mpl::apply
430 template <typename T1, typename T2 = MeshScalar, typename T3 = Device>
431 struct apply {
432 typedef ExponentialKLCoefficient<T1,T2,T3> type;
433 };
434
435 enum { is_constant = false };
436 typedef Kokkos::View<Scalar*, Kokkos::LayoutLeft, Device> RandomVariableView;
437 typedef typename RandomVariableView::size_type size_type;
438
439 typedef LocalViewTraits< RandomVariableView > local_rv_view_traits;
440 typedef typename local_rv_view_traits::local_view_type local_rv_view_type;
441 typedef typename local_rv_view_traits::local_value_type local_scalar_type;
442 typedef Stokhos::KL::ExponentialRandomField<MeshScalar, Device> rf_type;
443
444 rf_type m_rf; // Exponential random field
445 const MeshScalar m_mean; // Mean of random field
446 const MeshScalar m_variance; // Variance of random field
447 const MeshScalar m_corr_len; // Correlation length of random field
448 const size_type m_num_rv; // Number of random variables
449 const bool m_use_exp; // Take exponential of random field
450 const MeshScalar m_exp_shift; // Shift of exponential of random field
451 const MeshScalar m_exp_scale; // Scale of exponential of random field
452 const bool m_use_disc_exp_scale; // Use discontinuous exponential scale
453 RandomVariableView m_rv; // KL random variables
454
455 public:
456
457 ExponentialKLCoefficient(
458 const MeshScalar mean ,
459 const MeshScalar variance ,
460 const MeshScalar correlation_length ,
461 const size_type num_rv,
462 const bool use_exp,
463 const MeshScalar exp_shift,
464 const MeshScalar exp_scale,
465 const bool use_disc_exp_scale,
466 const bool init_random_variables = true) :
467 m_mean( mean ),
468 m_variance( variance ),
469 m_corr_len( correlation_length ),
470 m_num_rv( num_rv ),
471 m_use_exp( use_exp ),
472 m_exp_shift( exp_shift ),
473 m_exp_scale( exp_scale ),
474 m_use_disc_exp_scale( use_disc_exp_scale )
475 {
476 Teuchos::ParameterList solverParams;
477 solverParams.set("Number of KL Terms", int(num_rv));
478 solverParams.set("Mean", mean);
479 solverParams.set("Standard Deviation", std::sqrt(variance));
480 int ndim = 3;
481 Teuchos::Array<double> domain_upper(ndim, 1.0), domain_lower(ndim, 0.0),
482 correlation_lengths(ndim, correlation_length);
483 solverParams.set("Domain Upper Bounds", domain_upper);
484 solverParams.set("Domain Lower Bounds", domain_lower);
485 solverParams.set("Correlation Lengths", correlation_lengths);
486
487 m_rf = rf_type(solverParams);
488
489 if (init_random_variables)
490 m_rv = RandomVariableView( "KL Random Variables", m_num_rv );
491 }
492
493 ExponentialKLCoefficient( const ExponentialKLCoefficient & rhs ) :
494 m_rf( rhs.m_rf ) ,
495 m_mean( rhs.m_mean ) ,
496 m_variance( rhs.m_variance ) ,
497 m_corr_len( rhs.m_corr_len ) ,
498 m_num_rv( rhs.m_num_rv ) ,
499 m_use_exp( rhs.m_use_exp ) ,
500 m_exp_shift( rhs.m_exp_shift ) ,
501 m_exp_scale( rhs.m_exp_scale ) ,
502 m_use_disc_exp_scale( rhs.m_use_disc_exp_scale ),
503 m_rv( rhs.m_rv ) {}
504
505 KOKKOS_INLINE_FUNCTION
506 void setRandomVariables( const RandomVariableView& rv) { m_rv = rv; }
507
508 KOKKOS_INLINE_FUNCTION
509 RandomVariableView getRandomVariables() const { return m_rv; }
510
511 KOKKOS_INLINE_FUNCTION
512 local_scalar_type operator() ( const MeshScalar point[],
513 const size_type ensemble_rank ) const
514 {
515 local_rv_view_type local_rv =
516 local_rv_view_traits::create_local_view(m_rv, ensemble_rank);
517
518 local_scalar_type val = m_rf.evaluate(point, local_rv);
519
520 if (m_use_exp) {
521 local_scalar_type exp_scale = m_exp_scale;
522 if (m_use_disc_exp_scale) {
523 MeshScalar D = std::sqrt(3.0);
524 local_scalar_type r = 0.0;
525 for (size_type i=0; i<m_num_rv; ++i)
526 r += local_rv(i)*local_rv(i);
527 r = std::sqrt(r);
528 typedef EnsembleTraits<local_scalar_type> ET;
529 const int ensemble_size = ET::size;
530 for (int j=0; j<ensemble_size; ++j) {
531 typename ET::value_type rj = ET::coeff(r,j);
532 if (rj < D/4.0)
533 ET::coeff(exp_scale,j) = 1.0;
534 else if (rj >= D/4.0 && rj < D/2.0)
535 ET::coeff(exp_scale,j) = 100.0;
536 else
537 ET::coeff(exp_scale,j) = 10.0;
538 }
539 }
540 val = m_exp_shift + exp_scale * std::exp(val);
541 }
542
543 return val;
544 }
545 };
546
547 #ifdef HAVE_TRILINOSCOUPLINGS_SACADO
548 template <typename Scalar, typename MeshScalar, typename Device,
549 typename FadType>
550 struct FadCoeffFunctionTraits<
551 ExponentialKLCoefficient<Scalar,MeshScalar,Device>, FadType> {
552 typedef ExponentialKLCoefficient<Scalar,MeshScalar,Device> coeff_type;
553 typedef ExponentialKLCoefficient<FadType,MeshScalar,Device> type;
554 static type eval(const coeff_type& coeff_function) {
555 typedef typename type::RandomVariableView FadRV;
556 type fad_coeff_function(coeff_function.m_mean,
557 coeff_function.m_variance,
558 coeff_function.m_corr_len,
559 coeff_function.m_num_rv,
560 coeff_function.m_use_exp,
561 coeff_function.m_exp_shift,
562 coeff_function.m_exp_scale,
563 coeff_function.m_use_disc_exp_scale,
564 false);
565 FadRV fad_rv("Fad KL Random Variables",
566 coeff_function.m_num_rv, coeff_function.m_num_rv+1 );
567 for (unsigned i=0; i<coeff_function.m_num_rv; ++i) {
568 fad_rv(i).val() = coeff_function.m_rv(i);
569 fad_rv(i).fastAccessDx(i) = 1.0;
570 }
571 fad_coeff_function.setRandomVariables(fad_rv);
572 return fad_coeff_function;
573 }
574 };
575 #endif
576
577 } /* namespace FENL */
578 } /* namespace Example */
579 } /* namespace Kokkos */
580
581 #endif
582
583 #endif /* #ifndef KOKKOS_EXAMPLE_FENL_HPP */