21 #include "Ifpack2_Factory.hpp"
22 #include "BelosLinearProblem.hpp"
23 #include "BelosTpetraAdapter.hpp"
24 #include "BelosPseudoBlockCGSolMgr.hpp"
25 #include "BelosPseudoBlockGmresSolMgr.hpp"
26 #include "Tpetra_Core.hpp"
27 #include "MatrixMarket_Tpetra.hpp"
37 typedef double MeshScalar;
38 typedef double BasisScalar;
48 LocalOrdinal num_KL = 2;
52 bool nonlinear_expansion =
false;
54 bool symmetric =
false;
58 MPI_Init(&argc,&argv);
69 RCP<const Epetra_Comm> globalComm;
75 MyPID = globalComm->MyPID();
79 for (LocalOrdinal i=0; i<num_KL; i++)
81 RCP<const Stokhos::CompletePolynomialBasis<LocalOrdinal,BasisScalar> > basis =
84 LocalOrdinal sz = basis->size();
85 RCP<Stokhos::Sparse3Tensor<LocalOrdinal,BasisScalar> > Cijk;
86 if (nonlinear_expansion)
87 Cijk = basis->computeTripleProductTensor();
89 Cijk = basis->computeLinearTripleProductTensor();
90 RCP<Stokhos::OrthogPolyExpansion<LocalOrdinal,BasisScalar> > expansion =
94 std::cout <<
"Stochastic Galerkin expansion size = " << sz << std::endl;
97 LocalOrdinal num_spatial_procs = -1;
99 num_spatial_procs = std::atoi(argv[1]);
100 ParameterList parallelParams;
101 parallelParams.set(
"Number of Spatial Processors", num_spatial_procs);
106 RCP<Stokhos::ParallelData> sg_parallel_data =
109 RCP<const EpetraExt::MultiComm> sg_comm =
110 sg_parallel_data->getMultiComm();
111 RCP<const Epetra_Comm> app_comm =
112 sg_parallel_data->getSpatialComm();
115 RCP< Teuchos::Comm<int> > teuchos_app_comm;
117 RCP<const Epetra_MpiComm> app_mpi_comm =
119 RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > raw_mpi_comm =
120 Teuchos::opaqueWrapper(app_mpi_comm->Comm());
121 teuchos_app_comm =
rcp(
new Teuchos::MpiComm<int>(raw_mpi_comm));
128 RCP<problem_type> model =
129 rcp(
new problem_type(teuchos_app_comm, n, num_KL, s, mu,
130 nonlinear_expansion, symmetric));
134 typedef problem_type::Tpetra_Vector Tpetra_Vector;
135 typedef problem_type::Tpetra_CrsMatrix Tpetra_CrsMatrix;
136 typedef Tpetra::MatrixMarket::Writer<Tpetra_CrsMatrix> Writer;
137 RCP<const Tpetra_Vector> p = model->get_p_init(0);
138 RCP<Tpetra_Vector> x = Tpetra::createVector<Scalar>(model->get_x_map());
140 RCP<Tpetra_Vector>
f = Tpetra::createVector<Scalar>(model->get_f_map());
141 RCP<Tpetra_Vector>
dx = Tpetra::createVector<Scalar>(model->get_x_map());
142 RCP<Tpetra_CrsMatrix> J = model->create_W();
146 std::string prec_name =
"RILUK";
147 precParams.
set(
"fact: iluk level-of-fill", 1);
148 precParams.
set(
"fact: iluk level-of-overlap", 0);
149 typedef Ifpack2::Preconditioner<Scalar,LocalOrdinal,GlobalOrdinal,Node> Tprec;
151 Ifpack2::Factory factory;
152 M = factory.create<Tpetra_CrsMatrix>(prec_name, J);
153 M->setParameters(precParams);
156 model->computeResidual(*x, *p, *f);
157 model->computeJacobian(*x, *p, *J);
163 f->norm2(Teuchos::arrayView(&norm_f,1));
165 std::cout <<
"\nInitial residual norm = " << norm_f << std::endl;
168 RCP<ParameterList> belosParams =
rcp(
new ParameterList);
169 belosParams->set(
"Num Blocks", 20);
170 belosParams->set(
"Convergence Tolerance", 1e-12);
171 belosParams->set(
"Maximum Iterations", 1000);
172 belosParams->set(
"Verbosity", 33);
173 belosParams->set(
"Output Style", 1);
174 belosParams->set(
"Output Frequency", 1);
175 typedef Tpetra::Operator<Scalar,LocalOrdinal,GlobalOrdinal,Node> OP;
176 typedef Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> MV;
177 typedef Belos::OperatorTraits<Scalar,MV,OP> BOPT;
178 typedef Belos::MultiVecTraits<Scalar,MV> BMVT;
179 typedef Belos::LinearProblem<Scalar,MV,OP> BLinProb;
180 RCP< BLinProb > problem =
rcp(
new BLinProb(J, dx, f));
181 problem->setRightPrec(M);
182 problem->setProblem();
183 RCP<Belos::SolverManager<Scalar,MV,OP> > solver;
185 solver =
rcp(
new Belos::PseudoBlockCGSolMgr<Scalar,MV,OP>(problem,
188 solver =
rcp(
new Belos::PseudoBlockGmresSolMgr<Scalar,MV,OP>(problem,
192 Belos::ReturnType ret = solver->solve();
195 x->update(-1.0, *dx, 1.0);
198 RCP<Tpetra_Vector>
g = Tpetra::createVector<Scalar>(model->get_g_map(0));
200 model->computeResidual(*x, *p, *f);
201 model->computeResponse(*x, *p, *g);
204 f->norm2(Teuchos::arrayView(&norm_f,1));
206 std::cout <<
"\nFinal residual norm = " << norm_f << std::endl;
209 std::cout <<
"\nResponse = " << std::endl;
210 Writer::writeDense(std::cout, g);
219 catch (std::exception& e) {
220 std::cout << e.what() << std::endl;
223 std::cout << s << std::endl;
226 std::cout << s << std::endl;
229 std::cout <<
"Caught unknown exception!" <<std:: endl;
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Orthogonal polynomial expansions limited to algebraic operations.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Legendre polynomial basis.
int main(int argc, char **argv)
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
Tpetra::KokkosClassic::DefaultNode::DefaultNodeType Node
ScalarType g(const Teuchos::Array< ScalarType > &x, const ScalarType &y)
static void zeroOutTimers()
A linear 2-D diffusion problem.