Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_DiagnosticLinearOp.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Teko: A package for block and physics based preconditioning
4 //
5 // Copyright 2010 NTESS and the Teko contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 
12 #include "Teuchos_TimeMonitor.hpp"
13 
14 #include "Thyra_MultiVectorStdOps.hpp"
15 
16 namespace Teko {
17 
22 DiagnosticLinearOp::DiagnosticLinearOp(const Teuchos::RCP<std::ostream>& ostrm,
23  const ModifiableLinearOp& A,
24  const std::string& diagnosticString)
25  : outputStream_(ostrm),
26  wrapOpA_(A),
27  wrapOpA_lo_(A),
28  diagString_(diagnosticString),
29  timer_(diagnosticString) {}
30 
31 DiagnosticLinearOp::DiagnosticLinearOp(const Teuchos::RCP<std::ostream>& ostrm, const LinearOp& A,
32  const std::string& diagnosticString)
33  : outputStream_(ostrm),
34  wrapOpA_(Teuchos::null),
35  wrapOpA_lo_(A),
36  diagString_(diagnosticString),
37  timer_(diagnosticString) {}
38 
43 DiagnosticLinearOp::DiagnosticLinearOp(const Teuchos::RCP<std::ostream>& ostrm,
44  const LinearOp& fwdOp, const ModifiableLinearOp& A,
45  const std::string& diagnosticString)
46  : outputStream_(ostrm),
47  wrapOpA_(A),
48  wrapOpA_lo_(A),
49  fwdOp_(fwdOp),
50  diagString_(diagnosticString),
51  timer_(diagnosticString) {}
52 
54  double elapsedTime = totalTime();
55  int applications = numApplications();
56 
57  (*outputStream_) << "DiagnosticLinearOp \"" << diagString_ << "\": "
58  << "elapsed = " << elapsedTime << ", "
59  << "applications = " << applications << ", ";
60  if (applications > 0)
61  (*outputStream_) << "timer/app = " << elapsedTime / double(applications) << std::endl;
62  else
63  (*outputStream_) << "timer/app = "
64  << "none" << std::endl;
65 }
66 
79 void DiagnosticLinearOp::implicitApply(const MultiVector& x, MultiVector& y, const double alpha,
80  const double beta) const {
81  Teko_DEBUG_SCOPE("DiagnosticLinearOp::implicityApply", 10);
82 
83  // start timer on construction, end on destruction
84  Teuchos::TimeMonitor monitor(timer_, false);
85 
86  MultiVector z; // for temporary storage dealing with nozero beta
87  if (beta != 0.0) z = deepcopy(y);
88 
89  wrapOpA_lo_->apply(Thyra::NOTRANS, *x, y.ptr(), alpha, beta);
90 
91  // print residual if there is a fwd Op
92  bool printResidual = (fwdOp_ != Teuchos::null);
93  if (printResidual) {
94  // compute residual
95  MultiVector residual = Teko::deepcopy(x);
96  // fwdOp_->apply(Thyra::NOTRANS,*y,residual.ptr(),-1.0,1.0);
97 
98  fwdOp_->apply(Thyra::NOTRANS, *y, residual.ptr(), -1.0, alpha);
99  if (beta != 0.0) fwdOp_->apply(Thyra::NOTRANS, *z, residual.ptr(), beta, 1.0);
100 
101  // calculate norms
102  std::vector<double> norms(y->domain()->dim()); // size of column count
103  std::vector<double> rhsNorms(x->domain()->dim()); // size of column count
104  Thyra::norms_2<double>(*residual, Teuchos::arrayViewFromVector(norms));
105  Thyra::norms_2<double>(*x, Teuchos::arrayViewFromVector(rhsNorms));
106 
107  // print out residual norms
108  (*outputStream_) << "DiagnosticLinearOp \"" << diagString_ << "\": residual = [";
109  for (std::size_t i = 0; i < norms.size(); ++i)
110  (*outputStream_) << " " << std::scientific << std::setprecision(4)
111  << norms[i] / rhsNorms[i]; // << " (" <<rhsNorms[i]<<") ";
112  (*outputStream_) << " ]" << std::endl;
113 
114  residualNorm_ = norms[0];
115  }
116 }
117 
118 } // end namespace Teko
virtual void implicitApply(const MultiVector &x, MultiVector &y, const double alpha=1.0, const double beta=0.0) const
Perform a matrix vector multiply with this operator.
virtual ~DiagnosticLinearOp()
Destructor prints out timing information about this operator.