Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_Amesos2LinearOpWithSolve_def.hpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Stratimikos: Thyra-based strategies for linear solvers
6 // Copyright (2006) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 #include "Thyra_MultiVectorStdOps.hpp"
46 #include "Teuchos_TimeMonitor.hpp"
47 #include <Teuchos_RCP.hpp>
48 
49 
50 namespace Thyra {
51 
52 
53 // Constructors/initializers/accessors
54 
55 
56 template<typename Scalar>
58 {}
59 
60 
61 template<typename Scalar>
63  const Teuchos::RCP<const LinearOpBase<Scalar> > &fwdOp,
64  const Teuchos::RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
65  const Teuchos::RCP< Solver > &amesos2Solver,
66  const EOpTransp /* amesos2SolverTransp */,
67  const Scalar /* amesos2SolverScalar */
68  )
69 {
70  this->initialize(fwdOp,fwdOpSrc,amesos2Solver);
71 }
72 
73 
74 template<typename Scalar>
76  const Teuchos::RCP<const LinearOpBase<Scalar> > &fwdOp,
77  const Teuchos::RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
78  const Teuchos::RCP< Solver > &amesos2Solver
79  )
80 {
81  fwdOp_ = fwdOp;
82  fwdOpSrc_ = fwdOpSrc;
83  amesos2Solver_ = amesos2Solver;
84  const std::string fwdOpLabel = fwdOp_->getObjectLabel();
85  if(fwdOpLabel.length())
86  this->setObjectLabel( "lows("+fwdOpLabel+")" );
87 }
88 
89 
90 template<typename Scalar>
93 {
95  _fwdOpSrc = fwdOpSrc_;
96  fwdOpSrc_ = Teuchos::null;
97  return _fwdOpSrc;
98 }
99 
100 
101 // Overridden from LinearOpBase
102 
103 
104 template<typename Scalar>
107 {
108  return ( fwdOp_.get() ? fwdOp_->range() : Teuchos::null );
109 }
110 
111 
112 template<typename Scalar>
115 {
116  return ( fwdOp_.get() ? fwdOp_->domain() : Teuchos::null );
117 }
118 
119 
120 template<typename Scalar>
123 {
124  return Teuchos::null; // Not supported yet but could be
125 }
126 
127 
128 // Overridden from Teuchos::Describable
129 
130 
131 template<typename Scalar>
133 {
134  std::ostringstream oss;
136  if(!is_null(amesos2Solver_)) {
137  oss
138  << "{fwdOp="<<fwdOp_->description()
139  << ",amesos2Solver="<<typeName(*amesos2Solver_)<<"}";
140  }
141  return oss.str();
142 }
143 
144 
145 template<typename Scalar>
148  const Teuchos::EVerbosityLevel verbLevel
149  ) const
150 {
151  using Teuchos::OSTab;
152  using Teuchos::typeName;
153  using Teuchos::describe;
154  switch(verbLevel) {
156  case Teuchos::VERB_LOW:
157  out << this->description() << std::endl;
158  break;
160  case Teuchos::VERB_HIGH:
162  {
163  out
165  << "rangeDim=" << this->range()->dim()
166  << ",domainDim="<< this->domain()->dim() << "}\n";
167  OSTab tab(out);
168  if(!is_null(fwdOp_)) {
169  out << "fwdOp = " << describe(*fwdOp_,verbLevel);
170  }
171  if(!is_null(amesos2Solver_)) {
172  out << "amesos2Solver=" << typeName(*amesos2Solver_) << "\n";
173  }
174  break;
175  }
176  default:
177  TEUCHOS_TEST_FOR_EXCEPT(true); // Should never get here!
178  }
179 }
180 
181 
182 // protected
183 
184 
185 // Overridden from LinearOpBase
186 
187 
188 template<typename Scalar>
190 {
191  return ::Thyra::opSupported(*fwdOp_,M_trans);
192 }
193 
194 
195 template<typename Scalar>
197  const EOpTransp M_trans,
198  const MultiVectorBase<Scalar> &X,
199  const Ptr<MultiVectorBase<Scalar> > &Y,
200  const Scalar alpha,
201  const Scalar beta
202  ) const
203 {
204  Thyra::apply( *fwdOp_, M_trans, X, Y, alpha, beta );
205 }
206 
207 
208 // Overridden from LinearOpWithSolveBase
209 
210 template<typename Scalar>
212 {
213  if (Thyra::real_trans(M_trans) == Thyra::NOTRANS) {
214  // Assume every amesos2 solver supports a basic forward solve!
215  return true;
216  }
217  // dai: not sure how to query amesos2 transpose capability
218  return false;
219 }
220 
221 
222 template<typename Scalar>
224  EOpTransp /* M_trans */, const SolveMeasureType& /* solveMeasureType */
225  ) const
226 {
227  return true; // I am a direct solver so I should be able to do it all!
228 }
229 
230 
231 template<typename Scalar>
232 SolveStatus<Scalar>
234  const EOpTransp /* M_trans */,
235  const MultiVectorBase<Scalar> &B,
236  const Ptr<MultiVectorBase<Scalar> > &X,
237  const Ptr<const SolveCriteria<Scalar> > /* solveCriteria */
238  ) const
239 {
240  auto Btpetra = ConverterT::getConstTpetraMultiVector(Teuchos::rcpFromRef(B));
241 
242  auto Xtpetra = ConverterT::getTpetraMultiVector(Teuchos::rcpFromPtr(X));
243 
244  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
245  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
246 
247  if(out.get() && static_cast<int>(verbLevel) > static_cast<int>(Teuchos::VERB_NONE))
248  *out << "\nSolving system using Amesos2 solver "
249  << typeName(*amesos2Solver_) << " ...\n\n";
250 
251  amesos2Solver_->solve(Xtpetra.ptr(), Btpetra.ptr());
252 
253  SolveStatus<Scalar> solveStatus;
254  solveStatus.solveStatus = SOLVE_STATUS_CONVERGED;
255  return solveStatus;
256 }
257 
258 
259 } // end namespace Thyra
virtual bool solveSupportsSolveMeasureTypeImpl(EOpTransp M_trans, const SolveMeasureType &solveMeasureType) const
void initialize(const Teuchos::RCP< const LinearOpBase< Scalar > > &fwdOp, const Teuchos::RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, const Teuchos::RCP< Solver > &amesos2Solver)
Initialize after construction.
std::string typeName(const T &t)
SolveStatus< Scalar > solveImpl(const EOpTransp M_trans, const MultiVectorBase< Scalar > &B, const Ptr< MultiVectorBase< Scalar > > &X, const Ptr< const SolveCriteria< Scalar > > solveCriteria) const
bool is_null(const boost::shared_ptr< T > &p)
virtual void applyImpl(const EOpTransp M_trans, const MultiVectorBase< Scalar > &X, const Ptr< MultiVectorBase< Scalar > > &Y, const Scalar alpha, const Scalar beta) const
T * get() const
virtual bool opSupportedImpl(EOpTransp M_trans) const
basic_OSTab< char > OSTab
Teuchos::RCP< const VectorSpaceBase< Scalar > > domain() const
Teuchos::RCP< const LinearOpBase< Scalar > > clone() const
virtual std::string description() const
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Teuchos::RCP< const VectorSpaceBase< Scalar > > range() const
virtual bool solveSupportsImpl(EOpTransp M_trans) const
Amesos2LinearOpWithSolve()
Construct to uninitialized.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
Teuchos::RCP< const LinearOpSourceBase< Scalar > > extract_fwdOpSrc()
Extract the forward LinearOpSourceBase&lt;double&gt; object so that it can be modified and remove it from t...