Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_ApproxJacobiPreconditioner.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_TimeMonitor.hpp"
13 
17  const Teuchos::RCP<const Stokhos::OrthogPolyBasis<int,double> >& sg_basis_,
19  const Teuchos::RCP<const Epetra_Map>& base_map_,
20  const Teuchos::RCP<const Epetra_Map>& sg_map_,
22  const Teuchos::RCP<Teuchos::ParameterList>& params_) :
23  label("Stokhos Approximate Jacobi Preconditioner"),
24  sg_comm(sg_comm_),
25  sg_basis(sg_basis_),
26  epetraCijk(epetraCijk_),
27  base_map(base_map_),
28  sg_map(sg_map_),
29  prec_factory(prec_factory_),
30  mean_prec(),
31  useTranspose(false),
32  num_iter(2),
33  sg_op(),
34  sg_poly(),
35  rhs_block()
36 {
37  num_iter = params_->get("Number of Jacobi Iterations", 2);
38 
40  Teuchos::rcp(&(params_->sublist("Jacobi SG Operator")), false);
41  sgOpParams->set("Include Mean", false);
42  if (!sgOpParams->isParameter("Only Use Linear Terms"))
43  sgOpParams->set("Only Use Linear Terms", true);
44 
45  // Build new parallel Cijk if we are only using the linear terms, Cijk
46  // is distributed over proc's, and Cijk includes more than just the linear
47  // terms (so we have the right column map; otherwise we will be importing
48  // much more than necessary)
49  if (sgOpParams->get<bool>("Only Use Linear Terms") &&
50  epetraCijk->isStochasticParallel()) {
51  int dim = sg_basis->dimension();
52  if (epetraCijk->getKEnd() > dim+1)
53  epetraCijk =
55 
56  }
57  Stokhos::SGOperatorFactory sg_op_factory(sgOpParams);
58  mat_free_op = sg_op_factory.build(sg_comm, sg_basis, epetraCijk,
60 }
61 
64 {
65 }
66 
67 void
70  const Epetra_Vector& x)
71 {
72  sg_op = sg_op_;
73  sg_poly = sg_op->getSGPolynomial();
74  mean_prec = prec_factory->compute(sg_poly->getCoeffPtr(0));
75  label = std::string("Stokhos Approximate Jacobi Preconditioner:\n") +
76  std::string(" ***** ") +
77  std::string(mean_prec->Label());
78  mat_free_op->setupOperator(sg_poly);
79 }
80 
81 int
83 SetUseTranspose(bool UseTranspose)
84 {
85  useTranspose = UseTranspose;
86  sg_op->SetUseTranspose(UseTranspose);
87  mat_free_op->SetUseTranspose(UseTranspose);
88  mean_prec->SetUseTranspose(UseTranspose);
89 
90  return 0;
91 }
92 
93 int
95 Apply(const Epetra_MultiVector& Input, Epetra_MultiVector& Result) const
96 {
97  return sg_op->Apply(Input, Result);
98 }
99 
100 int
103 {
104 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
105  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Approximate Jacobi Time");
106 #endif
107 
108  // We have to be careful if Input and Result are the same vector.
109  // If this is the case, the only possible solution is to make a copy
110  const Epetra_MultiVector *input = &Input;
111  bool made_copy = false;
112  if (Input.Values() == Result.Values()) {
113  input = new Epetra_MultiVector(Input);
114  made_copy = true;
115  }
116 
117  int m = input->NumVectors();
118  if (rhs_block == Teuchos::null || rhs_block->NumVectors() != m)
119  rhs_block =
120  Teuchos::rcp(new EpetraExt::BlockMultiVector(*base_map, *sg_map, m));
121 
122  // Extract blocks
123  EpetraExt::BlockMultiVector input_block(View, *base_map, *input);
124  EpetraExt::BlockMultiVector result_block(View, *base_map, Result);
125 
126  int myBlockRows = epetraCijk->numMyRows();
127  result_block.PutScalar(0.0);
128  for (int iter=0; iter<num_iter; iter++) {
129 
130  // Compute RHS
131  if (iter == 0)
132  rhs_block->Update(1.0, input_block, 0.0);
133  else {
134  mat_free_op->Apply(result_block, *rhs_block);
135  rhs_block->Update(1.0, input_block, -1.0);
136  }
137 
138  // Apply deterministic preconditioner
139  for(int i=0; i<myBlockRows; i++) {
140 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
141  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total AJ Deterministic Preconditioner Time");
142 #endif
143  mean_prec->ApplyInverse(*(rhs_block->GetBlock(i)),
144  *(result_block.GetBlock(i)));
145  }
146 
147  }
148 
149  if (made_copy)
150  delete input;
151 
152  return 0;
153 }
154 
155 double
157 NormInf() const
158 {
159  return sg_op->NormInf();
160 }
161 
162 
163 const char*
165 Label () const
166 {
167  return const_cast<char*>(label.c_str());
168 }
169 
170 bool
173 {
174  return useTranspose;
175 }
176 
177 bool
179 HasNormInf() const
180 {
181  return sg_op->HasNormInf();
182 }
183 
184 const Epetra_Comm &
186 Comm() const
187 {
188  return *sg_comm;
189 }
190 const Epetra_Map&
193 {
194  return *sg_map;
195 }
196 
197 const Epetra_Map&
200 {
201  return *sg_map;
202 }
virtual const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this matrix operator. ...
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
virtual const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with the range of this matrix operator.
Teuchos::RCP< const EpetraExt::MultiComm > sg_comm
Stores SG parallel communicator.
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 ...
T & get(ParameterList &l, const std::string &name)
Teuchos::RCP< const Epetra_Map > sg_map
Stores SG map.
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.
RCP< ParameterList > sublist(const RCP< ParameterList > &paramList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Teuchos::RCP< const Epetra_Map > base_map
Stores base map.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Teuchos::RCP< Stokhos::SGOperator > mat_free_op
SG operator to implement SG mat-vec.
virtual ordinal_type dimension() const =0
Return dimension of basis.
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual Teuchos::RCP< Stokhos::SGOperator > build(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)
Build preconditioner operator.
virtual Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > getSGPolynomial()=0
Get SG polynomial.
virtual int SetUseTranspose(bool UseTranspose)
Set to true if the transpose of the operator is requested.
virtual void setupPreconditioner(const Teuchos::RCP< Stokhos::SGOperator > &sg_op, const Epetra_Vector &x)
Setup preconditioner.
virtual bool HasNormInf() const
Returns true if the this object can provide an approximate Inf-norm, false otherwise.
Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > sg_basis
Stochastic Galerking basis.
ApproxJacobiPreconditioner(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 > &base_map, const Teuchos::RCP< const Epetra_Map > &sg_map, const Teuchos::RCP< Stokhos::AbstractPreconditionerFactory > &prec_factory, const Teuchos::RCP< Teuchos::ParameterList > &params)
Constructor.
Factory for generating stochastic Galerkin preconditioners.
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 ...
virtual bool UseTranspose() const
Returns the current UseTranspose setting.
virtual const char * Label() const
Returns a character string describing the operator.
virtual double NormInf() const
Returns an approximate infinity norm of the operator matrix.