Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
random_field_example.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 // random_field_example
43 //
44 // usage:
45 // random_field_example
46 //
47 // output:
48 // prints out KL expansion for an exponential random field
49 
50 #include "Stokhos_ConfigDefs.h"
52 
53 // Functor for evaluating random field on the device
54 template <typename Value, typename Device = Kokkos::DefaultExecutionSpace>
55 struct RF {
56  typedef Value value_type;
57  typedef Device execution_space;
58 
60  typedef Kokkos::View<value_type*,execution_space> point_type;
61  typedef Kokkos::View<value_type*,execution_space> rv_type;
62  typedef Kokkos::View<value_type*,execution_space> result_type;
63  const rf_type rf;
64  const point_type x;
65  const rv_type rv;
66  const result_type y;
67 
68  RF(const rf_type& rf_, const point_type& x_, const rv_type& rv_) :
69  rf(rf_), x(x_), rv(rv_), y("y",1)
70  {
71  Kokkos::parallel_for(1, *this);
72  }
73 
74  KOKKOS_INLINE_FUNCTION
75  void operator() (const unsigned i) const {
76  y(0) = rf.evaluate(x, rv);
77  }
78 };
79 
80 int main(int argc, char **argv)
81 {
82  Kokkos::initialize();
83 
84  try {
85 
86  // Create random field
87  int M = 10;
88  Teuchos::ParameterList solverParams;
89  solverParams.set("Number of KL Terms", M);
90  solverParams.set("Mean", 1.0);
91  solverParams.set("Standard Deviation", 0.1);
92  int ndim = 3;
93  Teuchos::Array<double> domain_upper(ndim), domain_lower(ndim),
94  correlation_length(ndim);
95  for (int i=0; i<ndim; i++) {
96  domain_upper[i] = 1.0;
97  domain_lower[i] = 0.0;
98  correlation_length[i] = 10.0;
99  }
100  solverParams.set("Domain Upper Bounds", domain_upper);
101  solverParams.set("Domain Lower Bounds", domain_lower);
102  solverParams.set("Correlation Lengths", correlation_length);
104  rf.print(std::cout);
105 
106  // Evaluate random field at a point
107  Teuchos::Array<double> x(ndim);
108  for (int i=0; i<ndim; i++)
109  x[i] = (domain_upper[i] + domain_lower[i])/2.0 +
110  0.1*(domain_upper[i] - domain_lower[i])/2.0;
111  Teuchos::Array<double> rvar(M);
112  for (int i=0; i<M; i++)
113  rvar[i] = 1.5;
114  double result = rf.evaluate(x, rvar);
115  std::cout << "result (host) = " << result << std::endl;
116 
117  // Evaluate random field in a functor on device
118  typedef Kokkos::View<double*> view_type;
119  typedef view_type::HostMirror host_view_type;
120  view_type x_view("x", ndim);
121  host_view_type host_x = Kokkos::create_mirror_view(x_view);
122  for (int i=0; i<ndim; i++)
123  host_x(i) = x[i];
124  Kokkos::deep_copy(x_view, host_x);
125  view_type rvar_view("rvar", M);
126  host_view_type host_rvar = Kokkos::create_mirror_view(rvar_view);
127  for (int i=0; i<M; i++)
128  host_rvar(i) = rvar[i];
129  Kokkos::deep_copy(rvar_view, host_rvar);
130  RF<double> rf_func(rf, x_view, rvar_view);
131  host_view_type host_y = Kokkos::create_mirror_view(rf_func.y);
132  Kokkos::deep_copy(host_y, rf_func.y);
133  double result2 = host_y(0);
134  std::cout << "result (device)= " << result2 << std::endl;
135  }
136  catch (std::exception& e) {
137  std::cout << e.what() << std::endl;
138  }
139 
140  Kokkos::finalize();
141 }
KOKKOS_INLINE_FUNCTION void operator()(const unsigned i) const
KOKKOS_INLINE_FUNCTION Teuchos::PromotionTraits< typename rv_type::value_type, value_type >::promote evaluate(const point_type &point, const rv_type &random_variables) const
Evaluate random field at a point.
const rv_type rv
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void print(std::ostream &os) const
Print KL expansion.
Kokkos::View< value_type *, execution_space > point_type
const rf_type rf
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
Kokkos::View< value_type *, execution_space > result_type
int main(int argc, char **argv)
const point_type x
Stokhos::KL::ExponentialRandomField< value_type, execution_space > rf_type
Value value_type
Class representing a KL expansion of an exponential random field.
const result_type y
RF(const rf_type &rf_, const point_type &x_, const rv_type &rv_)
Device execution_space
Stokhos::CrsMatrix< ValueType, Device, Layout >::HostMirror create_mirror_view(const Stokhos::CrsMatrix< ValueType, Device, Layout > &A)
Kokkos::View< value_type *, execution_space > rv_type