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