Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Stokhos_MeanBasedPreconditioner.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
43 #include "EpetraExt_BlockMultiVector.h"
44 
47  const Teuchos::RCP<const EpetraExt::MultiComm>& sg_comm_,
48  const Teuchos::RCP<const Stokhos::OrthogPolyBasis<int,double> >& sg_basis_,
49  const Teuchos::RCP<const Stokhos::EpetraSparse3Tensor>& epetraCijk_,
50  const Teuchos::RCP<const Epetra_Map>& base_map_,
51  const Teuchos::RCP<const Epetra_Map>& sg_map_,
52  const Teuchos::RCP<Stokhos::AbstractPreconditionerFactory>& prec_factory_,
53  const Teuchos::RCP<Teuchos::ParameterList>& params_) :
54  label("Stokhos Mean-Based Preconditioner"),
55  sg_comm(sg_comm_),
56  sg_basis(sg_basis_),
57  epetraCijk(epetraCijk_),
58  base_map(base_map_),
59  sg_map(sg_map_),
60  num_blocks(0),
61  prec_factory(prec_factory_),
62  mean_prec(),
63  useTranspose(false),
64  use_block_apply(true)
65 {
66  use_block_apply = params_->get("Use Block Apply", true);
67 }
68 
71 {
72 }
73 
74 void
76 setupPreconditioner(const Teuchos::RCP<Stokhos::SGOperator>& sg_op,
77  const Epetra_Vector& x)
78 {
79  TEUCHOS_TEST_FOR_EXCEPTION(prec_factory == Teuchos::null, std::logic_error,
80  "Error! setupPreconditioner() cannot be called when " <<
81  "prec_factory is null!" << std::endl);
82 
83  Teuchos::RCP<Stokhos::EpetraOperatorOrthogPoly > sg_poly =
84  sg_op->getSGPolynomial();
85  mean_prec = prec_factory->compute(sg_poly->getCoeffPtr(0));
86  label = std::string("Stokhos Mean-Based Preconditioner:\n") +
87  std::string(" ***** ") +
88  std::string(mean_prec->Label());
89  num_blocks = sg_basis()->size();
90 }
91 
92 int
94 SetUseTranspose(bool UseTheTranspose)
95 {
96  useTranspose = UseTheTranspose;
97  mean_prec->SetUseTranspose(useTranspose);
98 
99  return 0;
100 }
101 
102 int
104 Apply(const Epetra_MultiVector& Input, Epetra_MultiVector& Result) const
105 {
106  int myBlockRows = epetraCijk->numMyRows();
107 
108  if (!use_block_apply) {
109  EpetraExt::BlockMultiVector sg_input(View, *base_map, Input);
110  EpetraExt::BlockMultiVector sg_result(View, *base_map, Result);
111  for (int i=0; i<myBlockRows; i++) {
112  mean_prec->Apply(*(sg_input.GetBlock(i)), *(sg_result.GetBlock(i)));
113  }
114  }
115 
116  else {
117  int m = Input.NumVectors();
118  Epetra_MultiVector input_block(
119  View, *base_map, Input.Values(), base_map->NumMyElements(),
120  m*myBlockRows);
121  Epetra_MultiVector result_block(
122  View, *base_map, Result.Values(), base_map->NumMyElements(),
123  m*myBlockRows);
124  mean_prec->Apply(input_block, result_block);
125  }
126 
127  return 0;
128 }
129 
130 int
133 {
134  int myBlockRows = epetraCijk->numMyRows();
135 
136  if (!use_block_apply) {
137  EpetraExt::BlockMultiVector sg_input(View, *base_map, Input);
138  EpetraExt::BlockMultiVector sg_result(View, *base_map, Result);
139  for (int i=0; i<myBlockRows; i++) {
140  mean_prec->ApplyInverse(*(sg_input.GetBlock(i)),
141  *(sg_result.GetBlock(i)));
142  }
143  }
144 
145  else {
146  int m = Input.NumVectors();
147  Epetra_MultiVector input_block(
148  View, *base_map, Input.Values(), base_map->NumMyElements(),
149  m*myBlockRows);
150  Epetra_MultiVector result_block(
151  View, *base_map, Result.Values(), base_map->NumMyElements(),
152  m*myBlockRows);
153  mean_prec->ApplyInverse(input_block, result_block);
154  }
155 
156  return 0;
157 }
158 
159 double
161 NormInf() const
162 {
163  return mean_prec->NormInf();
164 }
165 
166 
167 const char*
169 Label () const
170 {
171  return const_cast<char*>(label.c_str());
172 }
173 
174 bool
177 {
178  return useTranspose;
179 }
180 
181 bool
183 HasNormInf() const
184 {
185  return true;
186 }
187 
188 const Epetra_Comm &
190 Comm() const
191 {
192  return *sg_comm;
193 }
194 const Epetra_Map&
197 {
198  return *sg_map;
199 }
200 
201 const Epetra_Map&
204 {
205  return *sg_map;
206 }
double * Values() const
MeanBasedPreconditioner(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.
virtual const char * Label() const
Returns a character string describing the operator.
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.
virtual double NormInf() const
Returns an approximate infinity norm of the operator matrix.
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
virtual bool UseTranspose() const
Returns the current UseTranspose setting.
int NumVectors() const
bool use_block_apply
Flag indicating whether to use apply all vectors simultaneously.
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 ...
virtual int SetUseTranspose(bool UseTranspose)
Set to true if the transpose of the operator is requested.
virtual const Epetra_Comm & Comm() const
Returns a reference to the Epetra_Comm communicator associated with this 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 ...
virtual const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this matrix operator. ...
virtual const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with the range of this matrix operator.