Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef PLAYA_NONLINEAROPERATORBASE_HPP
00043 #define PLAYA_NONLINEAROPERATORBASE_HPP
00044
00045 #include "PlayaDefs.hpp"
00046 #include "PlayaHandleable.hpp"
00047 #include "PlayaOut.hpp"
00048 #include "PlayaObjectWithVerbosity.hpp"
00049 #include "PlayaVectorDecl.hpp"
00050 #include "PlayaLinearOperatorDecl.hpp"
00051 #include "PlayaLinearCombinationDecl.hpp"
00052
00053 namespace Playa
00054 {
00055 using namespace Teuchos;
00056
00057
00058
00059
00060 template <class Scalar>
00061 class NonlinearOperatorBase
00062 : public Handleable<NonlinearOperatorBase<Scalar> >,
00063 public ObjectWithVerbosity
00064 {
00065 public:
00066
00067
00068 NonlinearOperatorBase()
00069 : domain_(), range_(),
00070 jacobianIsValid_(false),
00071 residualIsValid_(false),
00072 currentEvalPt_(),
00073 currentFunctionValue_(),
00074 currentJ_()
00075 {;}
00076
00077
00078 NonlinearOperatorBase(const VectorSpace<Scalar>& domain,
00079 const VectorSpace<Scalar>& range)
00080 : domain_(domain.ptr()), range_(range.ptr()),
00081 jacobianIsValid_(false),
00082 residualIsValid_(false),
00083 currentEvalPt_(),
00084 currentFunctionValue_(),
00085 currentJ_()
00086 {;}
00087
00088
00089 const RCP<const VectorSpaceBase<Scalar> >& domain() const
00090 {return domain_;}
00091
00092
00093 const RCP<const VectorSpaceBase<Scalar> >& range() const
00094 {return range_;}
00095
00096
00097 void setEvalPt(const Vector<Scalar>& x) const
00098 {
00099 if (this->verb() >= 1)
00100 {
00101 Out::os() << "NonlinearOperatorBase Setting new eval pt";
00102 if (this->verb() > 3)
00103 {
00104 Out::os() << " to " << std::endl ;
00105 x.print(Out::os());
00106 }
00107 Out::os() << std::endl;
00108 }
00109 jacobianIsValid_ = false;
00110 residualIsValid_ = false;
00111
00112 currentEvalPt_.acceptCopyOf(x);
00113 }
00114
00115
00116
00117 const Vector<double>& currentEvalPt() const {return currentEvalPt_;}
00118
00119
00120 LinearOperator<double> getJacobian() const
00121 {
00122 if (this->verb() > 1)
00123 {
00124 Out::os() << "NonlinearOperatorBase getting Jacobian" << std::endl;
00125 }
00126 if (!jacobianIsValid_)
00127 {
00128 if (this->verb() > 3)
00129 {
00130 Out::os() << "...computing new J and F" << std::endl;
00131 }
00132 currentJ_
00133 = computeJacobianAndFunction(currentFunctionValue_);
00134 jacobianIsValid_ = true;
00135 residualIsValid_ = true;
00136 }
00137 else
00138 {
00139 if (this->verb() > 1)
00140 {
00141 Out::os() << "...reusing valid J" << std::endl;
00142 }
00143 }
00144 if (this->verb() > 3)
00145 {
00146 Out::os() << "J is " << std::endl;
00147 currentJ_.print(Out::os());
00148 Out::os() << std::endl;
00149 }
00150 return currentJ_;
00151 }
00152
00153
00154
00155
00156 Vector<double> getFunctionValue() const
00157 {
00158 if (this->verb() > 1)
00159 {
00160 Out::os() << "NonlinearOperatorBase getting function value" << std::endl;
00161 }
00162 if (!residualIsValid_)
00163 {
00164 if (this->verb() > 1)
00165 {
00166 Out::os() << "...computing new F" << std::endl;
00167 }
00168 currentFunctionValue_ = computeFunctionValue();
00169 residualIsValid_ = true;
00170 }
00171 else
00172 {
00173 if (this->verb() > 1)
00174 {
00175 Out::os() << "...reusing valid F" << std::endl;
00176 }
00177 }
00178
00179 if (this->verb() > 3)
00180 {
00181 Out::os() << "F is " << std::endl;
00182 currentFunctionValue_.print(Out::os());
00183 Out::os() << std::endl;
00184 }
00185 return currentFunctionValue_;
00186 }
00187
00188
00189
00190 virtual Vector<double> getInitialGuess() const = 0 ;
00191
00192
00193 protected:
00194
00195
00196 virtual LinearOperator<Scalar> computeJacobianAndFunction(Vector<double>& functionValue) const = 0 ;
00197
00198
00199 virtual Vector<Scalar> computeFunctionValue() const
00200 {
00201 computeJacobianAndFunction(currentFunctionValue_);
00202 return currentFunctionValue_;
00203 }
00204
00205
00206
00207
00208 void setDomainAndRange(const VectorSpace<Scalar>& domain,
00209 const VectorSpace<Scalar>& range)
00210 {
00211 domain_ = domain.ptr();
00212 range_ = range.ptr();
00213 }
00214
00215
00216 private:
00217
00218 RCP<const VectorSpaceBase<Scalar> > domain_;
00219
00220
00221 RCP<const VectorSpaceBase<Scalar> > range_;
00222
00223
00224 mutable bool jacobianIsValid_;
00225
00226
00227 mutable bool residualIsValid_;
00228
00229
00230 mutable Vector<double> currentEvalPt_;
00231
00232
00233 mutable Vector<double> currentFunctionValue_;
00234
00235
00236 mutable LinearOperator<double> currentJ_;
00237 };
00238
00239
00240
00241
00242 }
00243
00244
00245 #endif