45 #include "EpetraExt_MatrixMatrix.h"
52 struct KL_Diffusion_Func {
57 KL_Diffusion_Func(
double xyLeft,
double xyRight,
58 double mean_,
double std_dev,
59 double L,
int num_KL) : mean(mean_), point(2)
62 rfParams.
set(
"Number of KL Terms", num_KL);
63 rfParams.
set(
"Mean", mean);
64 rfParams.
set(
"Standard Deviation", std_dev);
67 correlation_length(ndim);
68 for (
int i=0; i<ndim; i++) {
69 domain_upper[i] = xyRight;
70 domain_lower[i] = xyLeft;
71 correlation_length[i] = L;
73 rfParams.
set(
"Domain Upper Bounds", domain_upper);
74 rfParams.
set(
"Domain Lower Bounds", domain_lower);
75 rfParams.
set(
"Correlation Lengths", correlation_length);
81 double operator() (
double x,
double y,
int k)
const {
86 return rf->evaluate_eigenfunction(point, k-1);
92 template <
typename DiffusionFunc>
93 struct Normalized_KL_Diffusion_Func {
94 const DiffusionFunc& func;
98 Normalized_KL_Diffusion_Func(
99 const DiffusionFunc& func_,
102 d(basis.dimension()),
107 for(
int k=0; k<d; k++) {
115 double operator() (
double x,
double y,
int k)
const {
117 double val = func(x, y, 0);
118 for (
int i=1; i<=d; i++)
119 val -= psi_0[i]/(psi_1[i]-psi_0[i])*func(x, y, i);
124 return 1.0/(psi_1[k]-psi_0[k])*func(x, y, k);
129 template <
typename DiffusionFunc>
130 struct LogNormal_Diffusion_Func {
132 const DiffusionFunc& func;
135 LogNormal_Diffusion_Func(
137 const DiffusionFunc& func_,
139 : mean(mean_), func(func_), prodbasis(prodbasis_) {}
141 double operator() (
double x,
double y,
int k)
const {
142 int d = prodbasis->dimension();
145 double sum_g = 0.0, efval;
146 for (
int l=0; l<d; l++) {
149 efval =
std::exp(mean + 0.5*sum_g)/norms[k];
150 for (
int l=0; l<d; l++) {
151 efval *=
std::pow(func(x,y,l+1),multiIndex[l]);
165 bool eliminate_bcs_) :
168 log_normal(log_normal_),
169 eliminate_bcs(eliminate_bcs_)
182 h = (xyRight - xyLeft)/((
double)(n-1));
184 for (
int j=0;
j<n;
j++) {
185 double y = xyLeft +
j*
h;
186 for (
int i=0; i<n; i++) {
187 double x = xyLeft + i*
h;
191 if (i == 0 || i == n-1 ||
j == 0 ||
j == n-1)
192 mesh[idx].boundary =
true;
194 mesh[idx].left = idx-1;
196 mesh[idx].right = idx+1;
198 mesh[idx].down = idx-n;
200 mesh[idx].up = idx+n;
207 int n_global_dof = global_dof_indices.
size();
209 int proc_id = comm->
MyPID();
210 int n_my_dof = n_global_dof / n_proc;
211 if (proc_id == n_proc-1)
212 n_my_dof += n_global_dof % n_proc;
213 int *my_dof = global_dof_indices.
getRawPtr() + proc_id*(n_global_dof / n_proc);
233 for (
int i=0;i<d;i++) {
234 std::stringstream ss;
235 ss <<
"KL Random Variable " << i+1;
236 (*p_names)[i] = ss.str();
240 int NumMyElements = x_map->NumMyElements();
241 int *MyGlobalElements = x_map->MyGlobalElements();
243 for (
int i=0; i<NumMyElements; ++i ) {
246 int global_idx = MyGlobalElements[i];
249 if (!
mesh[global_idx].boundary) {
277 Normalized_KL_Diffusion_Func<KL_Diffusion_Func> nklFunc(klFunc, *basis);
283 int sz = basis->
size();
296 for(
int i=0 ; i<NumMyElements; ++i ) {
297 int global_idx = MyGlobalElements[i];
298 if (
mesh[global_idx].boundary)
333 "Error! twoD_diffusion_problem::get_p_map(): " <<
334 "Invalid parameter index l = " << l << std::endl);
346 "Error! twoD_diffusion_problem::get_g_map(): " <<
347 "Invalid parameter index l = " << l << std::endl);
359 "Error! twoD_diffusion_problem::get_p_names(): " <<
360 "Invalid parameter index l = " << l << std::endl);
379 "Error! twoD_diffusion_problem::get_p_init(): " <<
380 "Invalid parameter index l = " << l << std::endl);
405 f.Update(-1.0, *
b, 1.0);
430 g[0] *=
double(x.GlobalLength()) /
double(
mesh.size());
447 for (
int i=0;i<basis->
size();i++) {
453 Cijk_type::k_iterator k_begin = Cijk->k_begin();
454 Cijk_type::k_iterator k_end = Cijk->k_end();
455 for (Cijk_type::k_iterator k_it=k_begin; k_it!=k_end; ++k_it) {
456 int k = Stokhos::index(k_it);
457 for (Cijk_type::kj_iterator j_it = Cijk->j_begin(k_it);
458 j_it != Cijk->j_end(k_it); ++j_it) {
459 int j = Stokhos::index(j_it);
462 for (Cijk_type::kj_iterator j_it = Cijk->j_begin(k_it);
463 j_it != Cijk->j_end(k_it); ++j_it) {
464 int j = Stokhos::index(j_it);
465 for (Cijk_type::kji_iterator i_it = Cijk->i_begin(j_it);
466 i_it != Cijk->i_end(j_it); ++i_it) {
467 int i = Stokhos::index(i_it);
468 double c = Stokhos::value(i_it);
473 f_sg[0].Update(-1.0,*
b,1.0);
482 for (
int i=0; i<J_sg.
size(); i++) {
487 jac->OptimizeStorage();
497 int sz = x_sg.
size();
498 for (
int i=0; i<sz; i++) {
499 x_sg[i].MeanValue(&g_sg[i][0]);
504 template <
typename FuncT>
515 for (
int k=0; k<sz; k++) {
517 for(
int i=0 ; i<NumMyElements; ++i ) {
520 int global_idx = MyGlobalElements[i];
521 if (
mesh[global_idx].boundary) {
526 A_k[k]->ReplaceGlobalValues(global_idx, 1, &val, &global_idx);
530 -func(
mesh[global_idx].x,
mesh[global_idx].y-h/2.0, k)/h2;
532 -func(
mesh[global_idx].x-h/2.0,
mesh[global_idx].y, k)/h2;
534 -func(
mesh[global_idx].x+h/2.0,
mesh[global_idx].y, k)/h2;
536 -func(
mesh[global_idx].x,
mesh[global_idx].y+h/2.0, k)/h2;
539 val = -(a_down + a_left + a_right + a_up);
540 A_k[k]->ReplaceGlobalValues(global_idx, 1, &val, &global_idx);
544 A_k[k]->ReplaceGlobalValues(global_idx, 1, &a_down,
545 &
mesh[global_idx].down);
549 A_k[k]->ReplaceGlobalValues(global_idx, 1, &a_left,
550 &
mesh[global_idx].left);
554 A_k[k]->ReplaceGlobalValues(global_idx, 1, &a_right,
555 &
mesh[global_idx].right);
559 A_k[k]->ReplaceGlobalValues(global_idx, 1, &a_up,
560 &
mesh[global_idx].up);
563 A_k[k]->FillComplete();
564 A_k[k]->OptimizeStorage();
578 EpetraExt::MatrixMatrix::Add((*
A_k[k]),
false,
basis_vals[k], *
A, 1.0);
583 EpetraExt::MatrixMatrix::Add((*
A_k[k]),
false, p[k-1], *
A, 1.0);
586 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)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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.
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.