42#include "EpetraExt_BlockMultiVector.h"
45#include "Teuchos_Assert.hpp"
46#include "Teuchos_TimeMonitor.hpp"
54 const Teuchos::RCP<const EpetraExt::MultiComm>& sg_comm_,
56 const Teuchos::RCP<const Stokhos::EpetraSparse3Tensor>& epetraCijk_,
57 const Teuchos::RCP<const Epetra_Map>& domain_base_map_,
58 const Teuchos::RCP<const Epetra_Map>& range_base_map_,
59 const Teuchos::RCP<const Epetra_Map>& domain_sg_map_,
60 const Teuchos::RCP<const Epetra_Map>& range_sg_map_,
61 const Teuchos::RCP<Teuchos::ParameterList>& params_) :
62 label(
"Stokhos KL Reduced Matrix Free Operator"),
93 const Teuchos::RCP<Stokhos::EpetraOperatorOrthogPoly >& ops)
99 mean = Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(
112Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly >
119Teuchos::RCP<const Stokhos::EpetraOperatorOrthogPoly >
154 throw "KLReducedMatrixFreeOperator::ApplyInverse not defined!";
170 return const_cast<char*
>(
label.c_str());
215#ifdef HAVE_STOKHOS_ANASAZI
216#ifdef STOKHOS_TEUCHOS_TIME_MONITOR
217 TEUCHOS_FUNC_TIME_MONITOR(
"Stokhos::KLReducedMatrixFreeOperator -- Calculation/setup of KL opeator");
220 mean = Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(
224 for (
int coeff=0; coeff<
num_blocks; coeff++) {
225 Teuchos::RCP<const Epetra_CrsMatrix> block_coeff =
226 Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>
229 for (
int i=0; i<
mean->NumMyRows(); i++) {
231 mean->NumMyRowEntries(i, num_col);
232 for (
int j=0;
j<num_col;
j++)
241 Teuchos::ParameterList anasazi_params = pceKL.getDefaultParams();
242 bool result = pceKL.computeKL(anasazi_params);
243 if (!result && myPID == 0)
244 std::cout <<
"KL Eigensolver did not converge!" << std::endl;
245 Teuchos::RCP<Epetra_MultiVector> evecs = pceKL.getEigenvectors();
246 Teuchos::Array<double> evals = pceKL.getEigenvalues();
249 std::cout <<
"num computed eigenvectors = "
250 << evecs->NumVectors() << std::endl;
251 double kl_tol =
params->get(
"KL Tolerance", 1e-6);
257 std::cout <<
"Can't achieve KL tolerance " << kl_tol
258 <<
". Smallest eigenvalue / largest eigenvalue = "
267 for (
int coeff=1; coeff <
num_blocks; coeff++) {
269 (*block_vec_poly)[coeff].Dot(*((*evecs)(rv)), &dot);
275 const Teuchos::Array<double>& norms =
sg_basis->norm_squared();
279 i_it!=
Cijk->i_end(); ++i_it) {
288 j_it !=
Cijk->j_end(l_it); ++j_it) {
291 i_it !=
Cijk->i_end(j_it); ++i_it) {
293 double c =
value(i_it);
305 bool save_tensor =
params->get(
"Save Sparse 3 Tensor To File",
false);
308 std::string basename =
params->get(
"Sparse 3 Tensor Base Filename",
310 std::stringstream ss;
311 ss << basename <<
"_" << idx++ <<
".mm";
313 *(
epetraCijk->getStochasticRowMap()), ss.str());
318 Teuchos::RCP<Epetra_BlockMap> kl_map =
330 for (
int i=0; i<
mean->NumMyRows(); i++) {
332 mean->NumMyRowEntries(i, num_col);
333 for (
int j=0;
j<num_col;
j++)
339 Teuchos::RCP<Stokhos::EpetraSparse3Tensor> reducedEpetraCijk =
344 reducedEpetraCijk->transformToLocal();
355 Teuchos::Array<double> point(
sg_basis->dimension());
356 for (
int i=0; i<
sg_basis->dimension(); i++)
358 Teuchos::Array<double> basis_vals(
sg_basis->size());
359 sg_basis->evaluateBases(point, basis_vals);
365 Teuchos::Array< Stokhos::OrthogPolyApprox<int,double> > rvs(
num_KL_computed);
372 val_rvs[rv] = rvs[rv].evaluate(point, basis_vals);
373 val_kl.
Update(val_rvs[rv], *((*evecs)(rv)), 1.0);
377 val.Update(-1.0, val_kl, 1.0);
381 std::cout <<
"Infinity norm of random field difference = " << diff/nrm
386 op_input.PutScalar(1.0);
391 op.
Apply(op_input, op_result);
392 this->
Apply(op_input, op_kl_result);
393 op_result.NormInf(&nrm);
394 op_result.Update(-1.0, op_kl_result, 1.0);
395 op_result.NormInf(&diff);
397 std::cout <<
"Infinity norm of operator difference = " << diff/nrm
402 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
403 "Stokhos::KLReducedMatrixFreeOperator is available " <<
404 "only when configured with Anasazi support!")
UnitTestSetup< Kokkos::Cuda > setup
int Update(double ScalarA, const Epetra_MultiVector &A, double ScalarThis)
A container class storing an orthogonal polynomial whose coefficients are vectors,...
A container class storing an orthogonal polynomial whose coefficients are vectors,...
virtual bool UseTranspose() const
Returns the current UseTranspose setting.
double drop_tolerance
Tolerance for dropping entries in sparse 3 tensor.
Teuchos::RCP< Epetra_Map > block_vec_map
Block map for vectorized-matrices.
Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > sg_basis
Stochastic Galerking basis.
Teuchos::RCP< Stokhos::Sparse3Tensor< int, double > > sparse_kl_coeffs
Sparse KL coefficients.
virtual void setupOperator(const Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > &poly)
Setup operator.
virtual bool HasNormInf() const
Returns true if the this object can provide an approximate Inf-norm, false otherwise.
virtual ~KLReducedMatrixFreeOperator()
Destructor.
virtual int Apply(const Epetra_MultiVector &Input, Epetra_MultiVector &Result) const
Returns the result of a Epetra_Operator applied to a Epetra_MultiVector Input in Result as described ...
int num_KL
Number of KL terms.
virtual int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of the inverse of the operator applied to a Epetra_MultiVector Input in Result as ...
Teuchos::RCP< Teuchos::ParameterList > params
Algorithmic parameters.
Teuchos::RCP< const Cijk_type > Cijk
Stores triple product tensor.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > block_vec_poly
Polynomial sorting vectorized matrix coefficients.
void setup()
Setup KL blocks.
Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > block_ops
Stores operators.
std::string label
Label for operator.
int expansion_size
Number of terms in expansion.
virtual const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this matrix operator.
Teuchos::RCP< const Epetra_Map > range_base_map
Stores range base map.
Teuchos::Array< Teuchos::Array< double > > dot_products
Dot products of KL eigenvectors and Jacobian blocks.
Teuchos::RCP< const EpetraExt::MultiComm > sg_comm
Stores SG parallel communicator.
virtual const Epetra_Comm & Comm() const
Returns a reference to the Epetra_Comm communicator associated with this operator.
Teuchos::RCP< const Stokhos::EpetraSparse3Tensor > epetraCijk
Stores Epetra Cijk tensor.
bool do_error_tests
Whether to do KL error tests (can be expensive)
virtual Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > getSGPolynomial()
Get SG polynomial.
int num_blocks
Number of blocks.
virtual double NormInf() const
Returns an approximate infinity norm of the operator matrix.
Teuchos::RCP< Stokhos::MatrixFreeOperator > kl_mat_free_op
Matrix-Free operator using KL operators.
KLReducedMatrixFreeOperator(const Teuchos::RCP< const EpetraExt::MultiComm > &sg_comm, const Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > &sg_basis, const Teuchos::RCP< const Stokhos::EpetraSparse3Tensor > &epetraCijk, const Teuchos::RCP< const Epetra_Map > &domain_base_map, const Teuchos::RCP< const Epetra_Map > &range_base_map, const Teuchos::RCP< const Epetra_Map > &domain_sg_map, const Teuchos::RCP< const Epetra_Map > &range_sg_map, const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Constructor.
virtual const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with the range of this matrix operator.
bool useTranspose
Flag indicating whether transpose was selected.
virtual const char * Label() const
Returns a character string describing the operator.
int num_KL_computed
Number of computed KL terms.
Teuchos::RCP< const Epetra_Map > range_sg_map
Stores range SG map.
Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > kl_ops
KL blocks as operators.
Teuchos::RCP< const Epetra_Map > domain_base_map
Stores domain base map.
Teuchos::RCP< Epetra_CrsMatrix > mean
Mean block.
Teuchos::Array< Teuchos::RCP< Epetra_CrsMatrix > > kl_blocks
KL blocks.
virtual int SetUseTranspose(bool UseTranspose)
Set to true if the transpose of the operator is requested.
Teuchos::RCP< const Epetra_Map > domain_sg_map
Stores domain SG map.
An Epetra operator representing the block stochastic Galerkin operator.
virtual void setupOperator(const Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > &poly)
Setup operator.
virtual int Apply(const Epetra_MultiVector &Input, Epetra_MultiVector &Result) const
Returns the result of a Epetra_Operator applied to a Epetra_MultiVector Input in Result as described ...
Abstract base class for multivariate orthogonal polynomials.
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
kji_sparse_array::const_iterator k_iterator
ikj_sparse_array::const_iterator i_iterator
SparseArrayIterator< index_iterator, value_iterator >::value_reference value(const SparseArrayIterator< index_iterator, value_iterator > &it)
SparseArrayIterator< index_iterator, value_iterator >::value_type index(const SparseArrayIterator< index_iterator, value_iterator > &it)
j_sparse_array::const_iterator kji_iterator
ji_sparse_array::const_iterator kj_iterator
void sparse3Tensor2MatrixMarket(const Stokhos::OrthogPolyBasis< ordinal_type, value_type > &basis, const Stokhos::Sparse3Tensor< ordinal_type, value_type > &Cijk, const Epetra_Comm &comm, const std::string &file)