Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_DiagnosticLinearOp.cpp
1 /*
2 // @HEADER
3 //
4 // ***********************************************************************
5 //
6 // Teko: A package for block and physics based preconditioning
7 // Copyright 2010 Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
40 //
41 // ***********************************************************************
42 //
43 // @HEADER
44 
45 */
46 
48 
49 #include "Teuchos_TimeMonitor.hpp"
50 
51 #include "Thyra_MultiVectorStdOps.hpp"
52 
53 namespace Teko {
54 
59 DiagnosticLinearOp::DiagnosticLinearOp(const Teuchos::RCP<std::ostream>& ostrm,
60  const ModifiableLinearOp& A,
61  const std::string& diagnosticString)
62  : outputStream_(ostrm),
63  wrapOpA_(A),
64  wrapOpA_lo_(A),
65  diagString_(diagnosticString),
66  timer_(diagnosticString) {}
67 
68 DiagnosticLinearOp::DiagnosticLinearOp(const Teuchos::RCP<std::ostream>& ostrm, const LinearOp& A,
69  const std::string& diagnosticString)
70  : outputStream_(ostrm),
71  wrapOpA_(Teuchos::null),
72  wrapOpA_lo_(A),
73  diagString_(diagnosticString),
74  timer_(diagnosticString) {}
75 
80 DiagnosticLinearOp::DiagnosticLinearOp(const Teuchos::RCP<std::ostream>& ostrm,
81  const LinearOp& fwdOp, const ModifiableLinearOp& A,
82  const std::string& diagnosticString)
83  : outputStream_(ostrm),
84  wrapOpA_(A),
85  wrapOpA_lo_(A),
86  fwdOp_(fwdOp),
87  diagString_(diagnosticString),
88  timer_(diagnosticString) {}
89 
91  double elapsedTime = totalTime();
92  int applications = numApplications();
93 
94  (*outputStream_) << "DiagnosticLinearOp \"" << diagString_ << "\": "
95  << "elapsed = " << elapsedTime << ", "
96  << "applications = " << applications << ", ";
97  if (applications > 0)
98  (*outputStream_) << "timer/app = " << elapsedTime / double(applications) << std::endl;
99  else
100  (*outputStream_) << "timer/app = "
101  << "none" << std::endl;
102 }
103 
116 void DiagnosticLinearOp::implicitApply(const MultiVector& x, MultiVector& y, const double alpha,
117  const double beta) const {
118  Teko_DEBUG_SCOPE("DiagnosticLinearOp::implicityApply", 10);
119 
120  // start timer on construction, end on destruction
121  Teuchos::TimeMonitor monitor(timer_, false);
122 
123  MultiVector z; // for temporary storage dealing with nozero beta
124  if (beta != 0.0) z = deepcopy(y);
125 
126  wrapOpA_lo_->apply(Thyra::NOTRANS, *x, y.ptr(), alpha, beta);
127 
128  // print residual if there is a fwd Op
129  bool printResidual = (fwdOp_ != Teuchos::null);
130  if (printResidual) {
131  // compute residual
132  MultiVector residual = Teko::deepcopy(x);
133  // fwdOp_->apply(Thyra::NOTRANS,*y,residual.ptr(),-1.0,1.0);
134 
135  fwdOp_->apply(Thyra::NOTRANS, *y, residual.ptr(), -1.0, alpha);
136  if (beta != 0.0) fwdOp_->apply(Thyra::NOTRANS, *z, residual.ptr(), beta, 1.0);
137 
138  // calculate norms
139  std::vector<double> norms(y->domain()->dim()); // size of column count
140  std::vector<double> rhsNorms(x->domain()->dim()); // size of column count
141  Thyra::norms_2<double>(*residual, Teuchos::arrayViewFromVector(norms));
142  Thyra::norms_2<double>(*x, Teuchos::arrayViewFromVector(rhsNorms));
143 
144  // print out residual norms
145  (*outputStream_) << "DiagnosticLinearOp \"" << diagString_ << "\": residual = [";
146  for (std::size_t i = 0; i < norms.size(); ++i)
147  (*outputStream_) << " " << std::scientific << std::setprecision(4)
148  << norms[i] / rhsNorms[i]; // << " (" <<rhsNorms[i]<<") ";
149  (*outputStream_) << " ]" << std::endl;
150 
151  residualNorm_ = norms[0];
152  }
153 }
154 
155 } // 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.