39 for (
int qp=0; qp<m; qp++) {
44 A(0,1) = s2[qp]/(2.*
h) + 1.0/(
h*
h);
45 for (
int i=1; i<n-1; i++) {
47 A(i,i-1) = -s2[qp]/(2.*
h) + 1.0/(h*h);
48 A(i,i+1) = s2[qp]/(2.*
h) + 1.0/(h*h);
50 A(n-1,n-2) = -s2[qp]/(2.*
h) + 1.0/(h*h);
51 A(n-1,n-1) = -2.0/(h*
h);
62 for (
int i=0; i<n; i++)
83 int nqp = weights.
size();
84 int sz = basis.
size();
88 for (
int qp=0; qp<nqp; qp++) {
89 s2[qp] = xi2.
evaluate(points[qp], basis_vals[qp]);
90 g[qp] = gamma.
evaluate(points[qp], basis_vals[qp]);
98 for (
int qp=0; qp<nqp; qp++) {
99 for (
int i=0; i<sz; i++)
100 rho[i] += r[qp]*weights[qp]*basis_vals[qp][i]/norms[i];
121 for (
int qp=0; qp<m; qp++) {
125 A(0,0) = -s1[qp]*2.0/(
h*
h) + rho[qp];
126 A(0,1) = s1[qp]/(
h*
h);
127 for (
int i=1; i<n-1; i++) {
128 A(i,i) = -s1[qp]*2.0/(
h*
h) + rho[qp];
129 A(i,i-1) = s1[qp]/(
h*
h);
130 A(i,i+1) = s1[qp]/(
h*
h);
132 A(n-1,n-2) = s1[qp]/(
h*
h);
133 A(n-1,n-1) = -s1[qp]*2.0/(
h*
h) + rho[qp];
137 for (
int i=0; i<n; i++) {
148 for (
int i=0; i<n; i++)
149 gamma[qp] +=
b(i)*
b(i);
150 gamma[qp] *= 100.0*
h;
169 int nqp = weights.
size();
170 int sz = basis.
size();
174 for (
int qp=0; qp<nqp; qp++) {
175 s1[qp] = xi1.
evaluate(points[qp], basis_vals[qp]);
176 r[qp] = rho.
evaluate(points[qp], basis_vals[qp]);
184 for (
int qp=0; qp<nqp; qp++) {
185 for (
int i=0; i<sz; i++)
186 gamma[i] += g[qp]*weights[qp]*basis_vals[qp][i]/norms[i];
200 double tol_,
int max_it_,
204 void solve(
double xi1,
double xi2,
double& rho,
double& gamma) {
207 double rho_error = 1.0;
208 double gamma_error = 1.0;
215 while( (rho_error >
tol || gamma_error >
tol) && it <
max_it) {
235 throw std::string(
"model didn't converge!");
252 double tol,
int max_it,
278 int nqp = weights.
size();
279 for (
int qp=0; qp<nqp; qp++) {
280 double s1 = xi1.
evaluate(points[qp], basis_vals[qp]);
281 double s2 = xi2.
evaluate(points[qp], basis_vals[qp]);
287 for (
int i=0; i<sz; i++) {
288 rho_pce[i] += rho*weights[qp]*basis_vals[qp][i]/norms[i];
289 gamma_pce[i] += gamma*weights[qp]*basis_vals[qp][i]/norms[i];
302 double tol_,
int max_it_,
316 double rho_error = 1.0;
317 double gamma_error = 1.0;
321 while( (rho_error >
tol || gamma_error >
tol) && it <
max_it) {
332 for (
int i=0; i<sz; i++) {
334 double ge =
rel_error(gamma[i],gamma0[i]);
337 if (ge > gamma_error)
341 std::cout <<
"it = " << it
342 <<
" rho_error = " << rho_error
343 <<
" gamma_error = " << gamma_error << std::endl;
353 throw std::string(
"model didn't converge!");
367 double tol_,
int max_it_,
390 double rho_error = 1.0;
391 double gamma_error = 1.0;
393 int nqp = weights.
size();
396 bool use_pce_quad_points =
false;
397 bool normalize =
false;
398 bool project_integrals =
false;
402 if (project_integrals)
411 while( (rho_error >
tol || gamma_error >
tol) && it <
max_it) {
415 rho_bases[0] = coordinate_bases[1];
420 normalize, project_integrals, Cijk));
431 gamma_rho(rho_basis), rho_rho(rho_basis);
432 xi2_rho.term(0, 0) = xi2.
term(1,0);
433 xi2_rho.term(0, 1) = xi2.
term(1,1);
434 gamma_rho.term(1, 0) = gamma.
mean();
436 gamma_rho.term(1, 1) = 1.0;
449 for (
int qp=0; qp<nqp; qp++) {
450 rho_point[0] = points[qp][1];
451 rho_point[1] = gamma.
evaluate(points[qp], basis_vals[qp]);
452 rho_basis->evaluateBases(rho_point, rho_basis_vals);
453 double r = rho_rho.
evaluate(rho_point ,rho_basis_vals);
454 for (
int i=0; i<sz; i++)
455 rho[i] += r*weights[qp]*basis_vals[qp][i]/norms[i];
460 gamma_bases[0] = coordinate_bases[0];
465 normalize, project_integrals, Cijk));
476 gamma_gamma(gamma_basis), rho_gamma(gamma_basis);
477 xi1_gamma.term(0, 0) = xi1.
term(0,0);
478 xi1_gamma.term(0, 1) = xi1.
term(0,1);
481 rho_gamma.
term(1, 1) = 1.0;
485 rho_gamma, gamma_gamma);
491 for (
int qp=0; qp<nqp; qp++) {
492 gamma_point[0] = points[qp][0];
493 gamma_point[1] = rho.
evaluate(points[qp], basis_vals[qp]);
494 gamma_basis->evaluateBases(gamma_point, gamma_basis_vals);
495 double g = gamma_gamma.evaluate(gamma_point, gamma_basis_vals);
496 for (
int i=0; i<sz; i++)
497 gamma[i] += g*weights[qp]*basis_vals[qp][i]/norms[i];
503 for (
int i=0; i<sz; i++) {
505 double ge =
rel_error(gamma[i],gamma0[i]);
508 if (ge > gamma_error)
512 std::cout <<
"it = " << it
513 <<
" rho_error = " << rho_error
514 <<
" gamma_error = " << gamma_error << std::endl;
524 throw std::string(
"model didn't converge!");
540 const double h = 1.0/(n+1);
543 const double tol = 1.0e-7;
544 const int max_it = 20;
545 CoupledSolver coupledSolver(1e-12, max_it, rhoModel, gammaModel);
551 for (
int i=0; i<d; i++)
564 x1.term(0,0) = (0.5+0.2)/2.0;
565 x1.term(0,1) = (0.5-0.2)/2.0;
568 x2.term(1,0) = (40.0+0)/2.0;
569 x2.term(1,1) = (40.0-0)/2.0;
575 double s1 = x1.evaluate(point);
576 double s2 = x2.evaluate(point);
578 coupledSolver.
solve(s1, s2, rho0, gamma0);
579 std::cout <<
"s1 = " << s1 <<
" s2 = " << s2 << std::endl;
584 nispSolver.
solve(x1, x2, rho_nisp, gamma_nisp);
585 double rho1 = rho_nisp.evaluate(point);
586 double gamma1 = gamma_nisp.
evaluate(point);
588 std::cout <<
"rho_nisp = " << rho1
589 <<
" rho0 = " << rho0 <<
" error = "
591 std::cout <<
"gamma_nisp = " << gamma1
592 <<
" gamma0 = " << gamma0 <<
" error = "
598 for (
int i=0; i<basis->size(); i++) {
602 siSolver.
solve(x1, x2, rho_si, gamma_si);
603 double rho2 = rho_si.evaluate(point);
604 double gamma2 = gamma_si.
evaluate(point);
606 std::cout <<
"rho_si = " << rho2
607 <<
" rho0 = " << rho0 <<
" error = "
609 std::cout <<
"gamma_si = " << gamma2
610 <<
" gamma0 = " << gamma0 <<
" error = "
616 for (
int i=0; i<basis->size(); i++) {
622 stSolver.
solve(x1, x2, rho_st, gamma_st);
623 double rho3 = rho_st.evaluate(point);
624 double gamma3 = gamma_st.
evaluate(point);
626 std::cout <<
"rho_st = " << rho3
627 <<
" rho0 = " << rho0 <<
" error = "
629 std::cout <<
"gamma_st = " << gamma3
630 <<
" gamma0 = " << gamma0 <<
" error = "
633 int num_samples = 100;
634 double h_grid = 2.0/(num_samples - 1);
635 std::ofstream coupled(
"coupled.txt");
636 std::ofstream nisp(
"nisp.txt");
637 std::ofstream si(
"si.txt");
638 std::ofstream st(
"st.txt");
639 for (
int i=0; i<num_samples; i++) {
640 point[0] = -1.0 + h_grid*i;
641 for (
int j=0;
j<num_samples;
j++) {
642 point[1] = -1.0 + h_grid*
j;
644 std::cout <<
"i = " << i <<
"j = " << j << std::endl;
646 double s1 = x1.evaluate(point);
647 double s2 = x2.evaluate(point);
648 coupledSolver.
solve(s1, s2, rho0, gamma0);
649 coupled << s1 <<
" " << s2 <<
" " << rho0 <<
" " << gamma0 << std::endl;
651 rho1 = rho_nisp.evaluate(point);
652 gamma1 = gamma_nisp.
evaluate(point);
653 nisp << s1 <<
" " << s2 <<
" " << rho1 <<
" " << gamma1 << std::endl;
655 rho2 = rho_si.evaluate(point);
657 si << s1 <<
" " << s2 <<
" " << rho2 <<
" " << gamma2 << std::endl;
659 rho3 = rho_st.evaluate(point);
661 st << s1 <<
" " << s2 <<
" " << rho3 <<
" " << gamma3 << std::endl;
670 catch (std::string& s) {
671 std::cout <<
"caught exception: " << s << std::endl;
673 catch (std::exception& e) {
674 std::cout << e.what() << std::endl;
ScalarType * values() const
void computeRho(const Teuchos::Array< double > &s2, const Teuchos::Array< double > &gamma, Teuchos::Array< double > &rho)
void solve(const Stokhos::OrthogPolyApprox< int, double > &xi1, const Stokhos::OrthogPolyApprox< int, double > &xi2, Stokhos::OrthogPolyApprox< int, double > &rho, Stokhos::OrthogPolyApprox< int, double > &gamma)
value_type evaluate(const Teuchos::Array< value_type > &point) const
Evaluate polynomial approximation at a point.
Teuchos::LAPACK< int, double > lapack
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const =0
Compute triple product tensor.
GammaModel(int n, double dx)
StieltjesCoupledSolver(double tol_, int max_it_, RhoModel &rhoModel_, GammaModel &gammaModel_, const Teuchos::RCP< const Stokhos::ProductBasis< int, double > > &basis_, const Teuchos::RCP< const Stokhos::Quadrature< int, double > > &quad_)
virtual ordinal_type order() const =0
Return order of basis.
Teuchos::RCP< const Stokhos::Quadrature< int, double > > quad
SemiIntrusiveCoupledSolver(double tol_, int max_it_, RhoModel &rhoModel_, GammaModel &gammaModel_, const Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > &basis_, const Teuchos::RCP< const Stokhos::Quadrature< int, double > > &quad_)
Teuchos::SerialDenseMatrix< int, double > A
void init(const value_type &v)
Initialize coefficients to value.
void solve(const Stokhos::OrthogPolyApprox< int, double > &xi1, const Stokhos::OrthogPolyApprox< int, double > &xi2, Stokhos::OrthogPolyApprox< int, double > &rho, Stokhos::OrthogPolyApprox< int, double > &gamma)
virtual const Teuchos::Array< Teuchos::Array< value_type > > & getBasisAtQuadPoints() const =0
Get values of basis at quadrature points.
double rel_error(double a, double b)
Teuchos::RCP< const Stokhos::Quadrature< int, double > > quad
void computeGamma(const Teuchos::Array< double > &s1, const Teuchos::Array< double > &rho, Teuchos::Array< double > &gamma)
Teuchos::LAPACK< int, double > lapack
void computeGammaPCE(const Stokhos::OrthogPolyBasis< int, double > &basis, const Stokhos::Quadrature< int, double > &quad, const Stokhos::OrthogPolyApprox< int, double > &xi1, const Stokhos::OrthogPolyApprox< int, double > &rho, Stokhos::OrthogPolyApprox< int, double > &gamma)
Stokhos::LegendreBasis< int, double > basis_type
virtual const Teuchos::Array< value_type > & getQuadWeights() const =0
Get quadrature weights.
Generates three-term recurrence using the Discretized Stieltjes procedure applied to a polynomial cha...
virtual const Teuchos::Array< Teuchos::Array< value_type > > & getQuadPoints() const =0
Get quadrature points.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::Array< int > piv
void solve(double xi1, double xi2, double &rho, double &gamma)
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
KOKKOS_INLINE_FUNCTION PCE< Storage > max(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
OrdinalType length() const
CoupledSolver coupledSolver
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
CoupledSolver(double tol_, int max_it_, RhoModel &rhoModel_, GammaModel &gammaModel_)
RhoModel(int n, double dx)
value_type mean() const
Compute mean of expansion.
virtual const Teuchos::Array< value_type > & norm_squared() const =0
Return array storing norm-squared of each basis polynomial.
Teuchos::RCP< const Stokhos::Quadrature< int, double > > quad
void computeRhoPCE(const Stokhos::OrthogPolyBasis< int, double > &basis, const Stokhos::Quadrature< int, double > &quad, const Stokhos::OrthogPolyApprox< int, double > &xi2, const Stokhos::OrthogPolyApprox< int, double > &gamma, Stokhos::OrthogPolyApprox< int, double > &rho)
KOKKOS_INLINE_FUNCTION PCE< Storage > atan(const PCE< Storage > &a)
virtual Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > getCoordinateBases() const =0
Return array of coordinate bases.
Legendre polynomial basis.
int main(int argc, char **argv)
Teuchos::SerialDenseVector< int, double > b
void GESV(const OrdinalType &n, const OrdinalType &nrhs, ScalarType *A, const OrdinalType &lda, OrdinalType *IPIV, ScalarType *B, const OrdinalType &ldb, OrdinalType *info) const
Teuchos::Array< int > piv
KOKKOS_INLINE_FUNCTION PCE< Storage > sin(const PCE< Storage > &a)
Teuchos::RCP< const Stokhos::Quadrature< int, double > > quad
Teuchos::SerialDenseMatrix< int, double > A
Teuchos::RCP< const Stokhos::ProductBasis< int, double > > basis
Teuchos::SerialDenseVector< int, double > b
void solve(const Stokhos::OrthogPolyApprox< int, double > &xi1, const Stokhos::OrthogPolyApprox< int, double > &xi2, Stokhos::OrthogPolyApprox< int, double > &rho_pce, Stokhos::OrthogPolyApprox< int, double > &gamma_pce)
Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > basis
NISPCoupledSolver(double tol, int max_it, RhoModel &rhoModel, GammaModel &gammaModel, const Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > &basis_, const Teuchos::RCP< const Stokhos::Quadrature< int, double > > &quad_)
Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > basis
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...
virtual ordinal_type size() const =0
Return total size of basis.
ScalarType g(const Teuchos::Array< ScalarType > &x, const ScalarType &y)
Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > basis
reference term(ordinal_type dimension, ordinal_type order)
Get coefficient term for given dimension and order.