13 #include "EpetraExt_MatrixMatrix.h"
20 struct KL_Diffusion_Func {
25 KL_Diffusion_Func(
double xyLeft,
double xyRight,
26 double mean_,
double std_dev,
27 double L,
int num_KL) : mean(mean_), point(2)
30 rfParams.
set(
"Number of KL Terms", num_KL);
31 rfParams.
set(
"Mean", mean);
32 rfParams.
set(
"Standard Deviation", std_dev);
35 correlation_length(ndim);
36 for (
int i=0; i<ndim; i++) {
37 domain_upper[i] = xyRight;
38 domain_lower[i] = xyLeft;
39 correlation_length[i] = L;
41 rfParams.
set(
"Domain Upper Bounds", domain_upper);
42 rfParams.
set(
"Domain Lower Bounds", domain_lower);
43 rfParams.
set(
"Correlation Lengths", correlation_length);
49 double operator() (
double x,
double y,
int k)
const {
54 return rf->evaluate_eigenfunction(point, k-1);
60 template <
typename DiffusionFunc>
61 struct Normalized_KL_Diffusion_Func {
62 const DiffusionFunc& func;
66 Normalized_KL_Diffusion_Func(
67 const DiffusionFunc& func_,
75 for(
int k=0; k<d; k++) {
83 double operator() (
double x,
double y,
int k)
const {
85 double val = func(x, y, 0);
86 for (
int i=1; i<=d; i++)
87 val -= psi_0[i]/(psi_1[i]-psi_0[i])*func(x, y, i);
92 return 1.0/(psi_1[k]-psi_0[k])*func(x, y, k);
97 template <
typename DiffusionFunc>
98 struct LogNormal_Diffusion_Func {
100 const DiffusionFunc& func;
103 LogNormal_Diffusion_Func(
105 const DiffusionFunc& func_,
107 : mean(mean_), func(func_), prodbasis(prodbasis_) {}
109 double operator() (
double x,
double y,
int k)
const {
110 int d = prodbasis->dimension();
113 double sum_g = 0.0, efval;
114 for (
int l=0; l<d; l++) {
117 efval =
std::exp(mean + 0.5*sum_g)/norms[k];
118 for (
int l=0; l<d; l++) {
119 efval *=
std::pow(func(x,y,l+1),multiIndex[l]);
133 bool eliminate_bcs_) :
136 log_normal(log_normal_),
137 eliminate_bcs(eliminate_bcs_)
150 h = (xyRight - xyLeft)/((
double)(n-1));
152 for (
int j=0;
j<n;
j++) {
153 double y = xyLeft +
j*
h;
154 for (
int i=0; i<n; i++) {
155 double x = xyLeft + i*
h;
159 if (i == 0 || i == n-1 ||
j == 0 ||
j == n-1)
160 mesh[idx].boundary =
true;
162 mesh[idx].left = idx-1;
164 mesh[idx].right = idx+1;
166 mesh[idx].down = idx-n;
168 mesh[idx].up = idx+n;
175 int n_global_dof = global_dof_indices.
size();
177 int proc_id = comm->
MyPID();
178 int n_my_dof = n_global_dof / n_proc;
179 if (proc_id == n_proc-1)
180 n_my_dof += n_global_dof % n_proc;
181 int *my_dof = global_dof_indices.
getRawPtr() + proc_id*(n_global_dof / n_proc);
201 for (
int i=0;i<d;i++) {
202 std::stringstream ss;
203 ss <<
"KL Random Variable " << i+1;
204 (*p_names)[i] = ss.str();
208 int NumMyElements = x_map->NumMyElements();
209 int *MyGlobalElements = x_map->MyGlobalElements();
211 for (
int i=0; i<NumMyElements; ++i ) {
214 int global_idx = MyGlobalElements[i];
217 if (!
mesh[global_idx].boundary) {
245 Normalized_KL_Diffusion_Func<KL_Diffusion_Func> nklFunc(klFunc, *basis);
251 int sz = basis->
size();
264 for(
int i=0 ; i<NumMyElements; ++i ) {
265 int global_idx = MyGlobalElements[i];
266 if (
mesh[global_idx].boundary)
301 "Error! twoD_diffusion_problem::get_p_map(): " <<
302 "Invalid parameter index l = " << l << std::endl);
314 "Error! twoD_diffusion_problem::get_g_map(): " <<
315 "Invalid parameter index l = " << l << std::endl);
327 "Error! twoD_diffusion_problem::get_p_names(): " <<
328 "Invalid parameter index l = " << l << std::endl);
347 "Error! twoD_diffusion_problem::get_p_init(): " <<
348 "Invalid parameter index l = " << l << std::endl);
373 f.Update(-1.0, *
b, 1.0);
398 g[0] *=
double(x.GlobalLength()) /
double(
mesh.size());
415 for (
int i=0;i<basis->
size();i++) {
421 Cijk_type::k_iterator k_begin = Cijk->k_begin();
422 Cijk_type::k_iterator k_end = Cijk->k_end();
423 for (Cijk_type::k_iterator k_it=k_begin; k_it!=k_end; ++k_it) {
424 int k = Stokhos::index(k_it);
425 for (Cijk_type::kj_iterator j_it = Cijk->j_begin(k_it);
426 j_it != Cijk->j_end(k_it); ++j_it) {
427 int j = Stokhos::index(j_it);
430 for (Cijk_type::kj_iterator j_it = Cijk->j_begin(k_it);
431 j_it != Cijk->j_end(k_it); ++j_it) {
432 int j = Stokhos::index(j_it);
433 for (Cijk_type::kji_iterator i_it = Cijk->i_begin(j_it);
434 i_it != Cijk->i_end(j_it); ++i_it) {
435 int i = Stokhos::index(i_it);
436 double c = Stokhos::value(i_it);
441 f_sg[0].Update(-1.0,*
b,1.0);
450 for (
int i=0; i<J_sg.
size(); i++) {
455 jac->OptimizeStorage();
465 int sz = x_sg.
size();
466 for (
int i=0; i<sz; i++) {
467 x_sg[i].MeanValue(&g_sg[i][0]);
472 template <
typename FuncT>
483 for (
int k=0; k<sz; k++) {
485 for(
int i=0 ; i<NumMyElements; ++i ) {
488 int global_idx = MyGlobalElements[i];
489 if (
mesh[global_idx].boundary) {
494 A_k[k]->ReplaceGlobalValues(global_idx, 1, &val, &global_idx);
498 -func(
mesh[global_idx].x,
mesh[global_idx].y-h/2.0, k)/h2;
500 -func(
mesh[global_idx].x-h/2.0,
mesh[global_idx].y, k)/h2;
502 -func(
mesh[global_idx].x+h/2.0,
mesh[global_idx].y, k)/h2;
504 -func(
mesh[global_idx].x,
mesh[global_idx].y+h/2.0, k)/h2;
507 val = -(a_down + a_left + a_right + a_up);
508 A_k[k]->ReplaceGlobalValues(global_idx, 1, &val, &global_idx);
512 A_k[k]->ReplaceGlobalValues(global_idx, 1, &a_down,
513 &
mesh[global_idx].down);
517 A_k[k]->ReplaceGlobalValues(global_idx, 1, &a_left,
518 &
mesh[global_idx].left);
522 A_k[k]->ReplaceGlobalValues(global_idx, 1, &a_right,
523 &
mesh[global_idx].right);
527 A_k[k]->ReplaceGlobalValues(global_idx, 1, &a_up,
528 &
mesh[global_idx].up);
531 A_k[k]->FillComplete();
532 A_k[k]->OptimizeStorage();
546 EpetraExt::MatrixMatrix::Add((*
A_k[k]),
false,
basis_vals[k], *
A, 1.0);
551 EpetraExt::MatrixMatrix::Add((*
A_k[k]),
false, p[k-1], *
A, 1.0);
554 A->OptimizeStorage();
A container class storing an orthogonal polynomial whose coefficients are vectors, operators, or in general any type that would have an expensive copy constructor.
void computeSGJacobian(const Stokhos::EpetraVectorOrthogPoly &x_sg, const Stokhos::EpetraVectorOrthogPoly &p_sg, Stokhos::EpetraOperatorOrthogPoly &J_sg)
Compute Jacobian.
Teuchos::Array< Teuchos::RCP< Epetra_CrsMatrix > > A_k
KL coefficients of operator.
Teuchos::RCP< Epetra_Map > x_map
Solution vector map.
int MyGlobalElements(int *MyGlobalElementList) const
Teuchos::RCP< Epetra_Operator > create_W() const
Create W = alpha*M + beta*J matrix.
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
Teuchos::RCP< const Epetra_Map > get_g_map(int j) const
Return response function map.
void init(const value_type &val)
Initialize coefficients.
Teuchos::RCP< const Epetra_Map > get_p_map(int l) const
Return parameter vector map.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< Epetra_CrsMatrix > A
Matrix to store deterministic operator.
void computeResidual(const Epetra_Vector &x, const Epetra_Vector &p, Epetra_Vector &f)
Compute residual.
void computeResponse(const Epetra_Vector &x, const Epetra_Vector &p, Epetra_Vector &g)
Compute response.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Teuchos::RCP< const Epetra_Vector > get_p_init(int l) const
Return initial parameters.
virtual void evaluateBases(const Teuchos::ArrayView< const value_type > &point, Teuchos::Array< value_type > &basis_vals) const =0
Evaluate basis polynomials at given point point.
Teuchos::RCP< LogNormal_Diffusion_Func< KL_Diffusion_Func > > lnFunc
void computeSGResponse(const Stokhos::EpetraVectorOrthogPoly &x_sg, const Stokhos::EpetraVectorOrthogPoly &p_sg, Stokhos::EpetraVectorOrthogPoly &g_sg)
Compute SG response.
Teuchos::RCP< Epetra_Map > g_map
Response vector map.
int InsertGlobalIndices(int_type GlobalRow, int NumIndices, int_type *Indices)
virtual int MyPID() const =0
int FillComplete(bool OptimizeDataStorage=true)
Teuchos::RCP< Epetra_Map > p_map
Parameter vector map.
Teuchos::RCP< const Epetra_Map > get_x_map() const
Return solution vector map.
void computeJacobian(const Epetra_Vector &x, const Epetra_Vector &p, Epetra_Operator &J)
Compute Jacobian.
Teuchos::RCP< Epetra_Vector > p_init
Initial parameters.
Abstract base class for orthogonal polynomial-based expansions.
int NumMyElements() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::Array< MeshPoint > mesh
A container class storing an orthogonal polynomial whose coefficients are vectors, operators, or in general any type that would have an expensive copy constructor.
Teuchos::RCP< KL_Diffusion_Func > klFunc
Teuchos::Array< double > basis_vals
Array to store values of basis at a point.
Teuchos::RCP< const Epetra_Vector > get_x_init() const
Return initial solution.
Teuchos::RCP< Epetra_Vector > x_init
Initial guess.
Teuchos::RCP< const Epetra_Map > get_f_map() const
Return residual vector map.
Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > basis
Teuchos::Array< Teuchos::RCP< Epetra_Vector > > sg_kx_vec_all
Vectors to store matrix-vector products in SG residual calculation.
void resize(size_type new_size, const value_type &x=value_type())
twoD_diffusion_problem(const Teuchos::RCP< const Epetra_Comm > &comm, int n, int d, double s=0.1, double mu=0.2, const Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > &basis=Teuchos::null, bool log_normal=false, bool eliminate_bcs=false)
Constructor.
virtual const Teuchos::Array< value_type > & norm_squared() const =0
Return array storing norm-squared of each basis polynomial.
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
Stokhos::Sparse3Tensor< int, double > Cijk_type
Teuchos::RCP< coeff_type > getCoeffPtr(ordinal_type i)
Return ref-count pointer to coefficient i.
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int l) const
Return array of parameter names.
void push_back(const value_type &x)
virtual int NumProc() const =0
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
Teuchos::Array< double > point
Array to store a point for basis evaluation.
Class representing a KL expansion of an exponential random field.
Teuchos::RCP< Epetra_Vector > b
Deterministic RHS.
void compute_A(const Epetra_Vector &p)
Compute A matrix.
void fillMatrices(const FuncT &func, int sz)
Fill coefficient matrix given function to evaluate diffusion coefficient.
Teuchos::RCP< Teuchos::Array< std::string > > p_names
Parameter names.
Teuchos::RCP< Epetra_CrsGraph > graph
Jacobian graph.
ordinal_type size() const
Return size.
virtual ordinal_type size() const =0
Return total size of basis.
ScalarType g(const Teuchos::Array< ScalarType > &x, const ScalarType &y)
void computeSGResidual(const Stokhos::EpetraVectorOrthogPoly &x_sg, const Stokhos::EpetraVectorOrthogPoly &p_sg, Stokhos::OrthogPolyExpansion< int, double > &expn, Stokhos::EpetraVectorOrthogPoly &f_sg)
Compute SG residual.
virtual Teuchos::RCP< const Sparse3Tensor< ordinal_type, value_type > > getTripleProduct() const =0
Get triple product.