21 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
22 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
23 #include "Thyra_PreconditionerBase.hpp"
24 #include "Thyra_PreconditionerFactoryBase.hpp"
25 #include "Thyra_EpetraThyraWrappers.hpp"
26 #include "Thyra_EpetraLinearOp.hpp"
36 #include "EpetraExt_VectorOut.h"
60 const char *
sg_rf_names[] = {
"Uniform",
"CC-Uniform",
"Rys",
"Log-Normal" };
68 "Slow Restricted",
"Moderate Restricted",
"Unrestricted" };
74 MPI_Init(&argc,&argv);
85 int MyPID = Comm->
MyPID();
92 "This example runs a stochastic collocation method.\n");
95 CLP.
setOption(
"num_mesh", &n,
"Number of mesh points in each direction");
100 "Random field type");
106 CLP.
setOption(
"std_dev", &sigma,
"Standard deviation");
108 double weightCut = 1.0;
109 CLP.
setOption(
"weight_cut", &weightCut,
"Weight cut");
112 CLP.
setOption(
"num_kl", &num_KL,
"Number of KL terms");
115 CLP.
setOption(
"order", &p,
"Polynomial order");
117 bool normalize_basis =
true;
118 CLP.
setOption(
"normalize",
"unnormalize", &normalize_basis,
119 "Normalize PC basis");
122 CLP.
setOption(
"krylov_solver", &krylov_solver,
127 CLP.
setOption(
"krylov_method", &krylov_method,
132 CLP.
setOption(
"prec_strategy", &precStrategy,
134 "Preconditioner strategy");
137 CLP.
setOption(
"tol", &tol,
"Solver tolerance");
142 "Sparse grid growth rule");
144 CLP.
parse( argc, argv );
147 std::cout <<
"Summary of command line options:" << std::endl
148 <<
"\tnum_mesh = " << n << std::endl
149 <<
"\trand_field = " <<
sg_rf_names[randField] << std::endl
150 <<
"\tmean = " << mean << std::endl
151 <<
"\tstd_dev = " << sigma << std::endl
152 <<
"\tweight_cut = " << weightCut << std::endl
153 <<
"\tnum_kl = " << num_KL << std::endl
154 <<
"\torder = " << p << std::endl
155 <<
"\tnormalize_basis = " << normalize_basis << std::endl
162 <<
"\ttol = " << tol << std::endl
167 bool nonlinear_expansion =
false;
169 nonlinear_expansion =
true;
176 for (
int i=0; i<num_KL; i++) {
180 p, normalize_basis));
185 p, normalize_basis,
true));
187 else if (randField ==
RYS) {
189 p, weightCut, normalize_basis));
193 p, normalize_basis));
201 int sparse_grid_growth = Pecos::MODERATE_RESTRICTED_GROWTH;
203 sparse_grid_growth = Pecos::SLOW_RESTRICTED_GROWTH;
205 sparse_grid_growth = Pecos::MODERATE_RESTRICTED_GROWTH;
207 sparse_grid_growth = Pecos::UNRESTRICTED_GROWTH;
208 Stokhos::SparseGridQuadrature<int,double> quad(basis,p,1e-12,sg_growth);
210 quad.getQuadPoints();
212 quad.getQuadWeights();
213 int nqp = quad_weights.
size();
217 basis, nonlinear_expansion);
230 EpetraExt::ModelEvaluator::InArgs inArgs = model.
createInArgs();
231 EpetraExt::ModelEvaluator::OutArgs outArgs = model.
createOutArgs();
232 EpetraExt::ModelEvaluator::OutArgs outArgs2 = model.
createOutArgs();
244 if (krylov_solver ==
AZTECOO) {
245 stratParams.
set(
"Linear Solver Type",
"AztecOO");
247 stratParams.
sublist(
"Linear Solver Types").sublist(
"AztecOO").sublist(
"Forward Solve");
248 aztecParams.
set(
"Max Iterations", 1000);
249 aztecParams.
set(
"Tolerance", tol);
251 aztecParams.
sublist(
"AztecOO Settings");
252 if (krylov_method ==
GMRES)
253 aztecSettings.
set(
"Aztec Solver",
"GMRES");
254 else if (krylov_method ==
CG)
255 aztecSettings.
set(
"Aztec Solver",
"CG");
256 aztecSettings.
set(
"Aztec Preconditioner",
"none");
257 aztecSettings.
set(
"Size of Krylov Subspace", 100);
258 aztecSettings.
set(
"Convergence Test",
"r0");
259 aztecSettings.
set(
"Output Frequency", 10);
261 stratParams.
sublist(
"Linear Solver Types").sublist(
"AztecOO").sublist(
"VerboseObject");
262 verbParams.
set(
"Verbosity Level",
"none");
264 else if (krylov_solver ==
BELOS) {
265 stratParams.
set(
"Linear Solver Type",
"Belos");
267 stratParams.
sublist(
"Linear Solver Types").sublist(
"Belos");
269 if (krylov_method ==
GMRES || krylov_method ==
FGMRES) {
270 belosParams.
set(
"Solver Type",
"Block GMRES");
272 &(belosParams.
sublist(
"Solver Types").sublist(
"Block GMRES"));
273 if (krylov_method ==
FGMRES)
274 belosSolverParams->
set(
"Flexible Gmres",
true);
276 else if (krylov_method ==
RGMRES) {
277 belosParams.
set(
"Solver Type",
"GCRODR");
279 &(belosParams.
sublist(
"Solver Types").sublist(
"GCRODR"));
280 belosSolverParams->
set(
"Num Recycled Blocks", 10);
282 else if (krylov_method ==
CG) {
283 belosParams.
set(
"Solver Type",
"Block CG");
285 &(belosParams.
sublist(
"Solver Types").sublist(
"Block CG"));
288 belosSolverParams->
set(
"Convergence Tolerance", tol);
289 belosSolverParams->
set(
"Maximum Iterations", 1000);
290 belosSolverParams->
set(
"Num Blocks", 100);
291 belosSolverParams->
set(
"Output Frequency",10);
293 verbParams.
set(
"Verbosity Level",
"none");
295 stratParams.
set(
"Preconditioner Type",
"ML");
297 stratParams.
sublist(
"Preconditioner Types").sublist(
"ML").sublist(
"ML Settings");
298 precParams.
set(
"default values",
"SA");
299 precParams.
set(
"ML output", 0);
300 precParams.
set(
"max levels",5);
301 precParams.
set(
"increasing or decreasing",
"increasing");
302 precParams.
set(
"aggregation: type",
"Uncoupled");
303 precParams.
set(
"smoother: type",
"ML symmetric Gauss-Seidel");
304 precParams.
set(
"smoother: sweeps",2);
305 precParams.
set(
"smoother: pre or post",
"both");
306 precParams.
set(
"coarse: max size", 200);
307 #ifdef HAVE_ML_AMESOS
308 precParams.
set(
"coarse: type",
"Amesos-KLU");
310 precParams.
set(
"coarse: type",
"Jacobi");
314 Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
315 linearSolverBuilder.setParameterList(
Teuchos::rcp(&stratParams,
false));
320 linearSolverBuilder.createLinearSolveStrategy(
"");
322 Teuchos::VerboseObjectBase::getDefaultOStream();
323 lowsFactory->setOStream(out);
328 lowsFactory->createOp();
330 lowsFactory->getPreconditionerFactory();
332 precFactory->createPrec();
336 Thyra::epetraLinearOp(A);
338 rcp(
new Thyra::DefaultLinearOpSource<double>(A_thyra));
342 if (!(krylov_solver ==
BELOS && krylov_method ==
CG)) {
344 Thyra::SolveMeasureType solveMeasure(
345 Thyra::SOLVE_MEASURE_NORM_RESIDUAL,
346 Thyra::SOLVE_MEASURE_NORM_INIT_RESIDUAL);
348 Teuchos::rcp(
new Thyra::SolveCriteria<double>(solveMeasure, tol));
352 if (precStrategy ==
MEAN) {
355 precFactory->initializePrec(losb, M_thyra.
get());
356 Thyra::initializePreconditionedOp<double>(
357 *lowsFactory, A_thyra, M_thyra, lows.
ptr());
360 x_mean.PutScalar(0.0);
361 x_var.PutScalar(0.0);
363 for (
int qp=0; qp<nqp; qp++) {
367 for (
int i=0; i<num_KL; i++)
368 (*p)[i] = quad_points[qp][i];
382 Thyra::create_Vector(x, A_thyra->domain());
384 Thyra::create_Vector(f, A_thyra->range());
387 if (precStrategy !=
MEAN) {
389 precFactory->initializePrec(losb, M_thyra.
get());
390 Thyra::initializePreconditionedOp<double>(
391 *lowsFactory, A_thyra, M_thyra, lows.
ptr());
397 Thyra::SolveStatus<double> solveStatus =
398 lows->solve(Thyra::NOTRANS, *f_thyra, x_thyra.
ptr(),
399 solveCriteria.
ptr());
401 std::cout <<
"Collocation point " << qp+1 <<
'/' << nqp <<
": "
402 << solveStatus.message << std::endl;
410 outArgs2.set_g(0, g);
414 x2.Multiply(1.0, *x, *x, 0.0);
415 g2.Multiply(1.0, *g, *g, 0.0);
416 x_mean.Update(quad_weights[qp], *x, 1.0);
417 x_var.Update(quad_weights[qp], x2, 1.0);
418 g_mean.Update(quad_weights[qp], *g, 1.0);
419 g_var.Update(quad_weights[qp], g2, 1.0);
422 x2.Multiply(1.0, x_mean, x_mean, 0.0);
423 g2.Multiply(1.0, g_mean, g_mean, 0.0);
424 x_var.Update(-1.0, x2, 1.0);
425 g_var.Update(-1.0, g2, 1.0);
428 for (
int i=0; i<x_var.MyLength(); i++)
430 for (
int i=0; i<g_var.MyLength(); i++)
433 std::cout.precision(16);
434 std::cout <<
"\nResponse Mean = " << std::endl << g_mean << std::endl;
435 std::cout <<
"Response Std. Dev. = " << std::endl << g_var << std::endl;
438 EpetraExt::VectorToMatrixMarketFile(
"mean_col.mm", x_mean);
439 EpetraExt::VectorToMatrixMarketFile(
"std_dev_col.mm", x_var);
448 catch (std::exception& e) {
449 std::cout << e.what() << std::endl;
451 catch (std::string& s) {
452 std::cout << s << std::endl;
455 std::cout << s << std::endl;
458 std::cout <<
"Caught unknown exception!" <<std:: endl;
const char * prec_strategy_names[]
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
Hermite polynomial basis.
const Krylov_Method krylov_method_values[]
void evalModel(const InArgs &inArgs, const OutArgs &outArgs) const
Evaluate model on InArgs.
Teuchos::RCP< Epetra_CrsMatrix > get_mean() const
Get mean matrix.
const int num_prec_strategy
RCP< ParameterList > sublist(const RCP< ParameterList > ¶mList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
const char * krylov_solver_names[]
virtual int MyPID() const =0
const SG_GROWTH sg_growth_values[]
const char * sg_growth_names[]
ModelEvaluator for a linear 2-D diffusion problem.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
const int num_krylov_method
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)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
const SG_RF sg_rf_values[]
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
const int num_krylov_solver
Teuchos::RCP< const Epetra_Map > get_p_map(int l) const
Return parameter vector map.
Teuchos::RCP< const Epetra_Map > get_g_map(int j) const
Return response function map.
Legendre polynomial basis.
int main(int argc, char **argv)
Teuchos::RCP< Epetra_Operator > create_W() const
Create W = alpha*M + beta*J matrix.
InArgs createInArgs() const
Create InArgs.
const char * sg_rf_names[]
OutArgs createOutArgs() const
Create OutArgs.
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
Legendre polynomial basis using Clenshaw-Curtis quadrature points.
void setDocString(const char doc_string[])
Teuchos::RCP< const Epetra_Map > get_x_map() const
Return solution vector map.
const PrecStrategy prec_strategy_values[]
const Krylov_Solver krylov_solver_values[]
ScalarType g(const Teuchos::Array< ScalarType > &x, const ScalarType &y)
static void zeroOutTimers()
Teuchos::RCP< const Epetra_Map > get_f_map() const
Return residual vector map.
const char * krylov_method_names[]