CppNoddy  0.92
Loading...
Searching...
No Matches
Residual.h
Go to the documentation of this file.
1/// \file Residual.h
2/// A specification of a (double/complex) VECTOR residual class. To be
3/// inherited by anything that is to be passed to the Newton object.
4
5#ifndef RESIDUAL_H
6#define RESIDUAL_H
7
8#include <Timer.h>
9#include <DenseVector.h>
10#include <DenseMatrix.h>
11
12namespace CppNoddy {
13 /// A base class to be inherited by objects that define residuals
14 template <typename _Type>
15 class Residual {
16 public:
17 /// Constructor for a 'square' residual object
18 /// that is, N residuals for N unknowns.
19 /// \param order The order of the residual vector
20 Residual(const unsigned& order);
21
22 /// Constructor for a 'non-square' residual object
23 /// that is, there are less residual constraints than unknowns.
24 /// \param order The number of residuals
25 /// \param nvars The number of unknowns/variables
26 Residual(const unsigned& order, const unsigned& nvars);
27
28 /// An empty destructor, virtual since we have virtual methods.
29 virtual ~Residual();
30
31 /// Update the Residual object for the current set of state variables
32 /// \param state The state at which to set the residual object
33 void update(const DenseVector<_Type>& state);
34
35 /// Return a handle to the residuals corresponding
36 /// to the last update state
38
39 /// Retrun a handle to the Jacobian of the residual
40 /// corresponding to the last update state
42
43 /// \return A handle to the step size used when finite-differencing
44 /// the residual vector to obtain the Jacobian
45 _Type& delta();
46
47 /// \return A handle to the step size used when finite-differencing
48 /// the residual vector to obtain the Jacobian
49 const _Type& delta() const;
50
51 /// Get the order of the residual vector
52 /// \return The order/length of the residual vector
53 unsigned get_order() const;
54
55 /// Get the number of variables that this residual condition
56 /// is defined for.
57 /// \return The number of variables for the residual
58 unsigned get_number_of_vars() const;
59
60 /// A blank virtual residual function method.
61 /// \param state The unknown variable.
62 /// \param f The residual function f(x).
63 virtual void residual_fn(const DenseVector<_Type>& state, DenseVector<_Type>& f) const {
64 std::string problem;
65 problem = "The Residual::residual_fn method has not been implemented.\n";
66 problem += "You have to implement this method to define the residual.\n";
67 throw ExceptionRuntime(problem);
68 }
69
70 protected:
71 /// Because the residual evaluation at the current state is assumed
72 /// to have already been done by the 'update' method, this routine is
73 /// protected. This default uses a finite-differenced Jacobian.
74 /// You can overload this to provide an analytic Jacobian if you wish
75 /// \param state Dummy state vector ... this is only here to make overloading
76 /// easier, the 'last_x' member data is assumed in the default FD method.
77 /// \param jac The NxN matrix Jacobian where the equation_fn
78 /// is a vector function of length N.
79 virtual void jacobian(const DenseVector<_Type>& state, DenseMatrix<_Type>& jac) const;
80
81 /// Jacobian for the last state vector
83 /// Residual for the last state vector
85 /// The last state vector
87 /// A default step for FD computation of the Jacobian
88 _Type DELTA;
89 /// The order of the system of equations
91 /// The number of elements in the state vector
93#ifdef TIME
94 Timer T_UPDATER;
95#endif
96 }
97 ; // end class
98
99 template <typename _Type>
101#ifdef TIME
102 T_UPDATER.start();
103#endif
104 LAST_STATE = x;
105 residual_fn(LAST_STATE, FN_AT_LAST_STATE);
106 jacobian(LAST_STATE, JAC_AT_LAST_STATE);
107#ifdef TIME
108 T_UPDATER.stop();
109#endif
110 }
111
112 template <typename _Type>
114 return FN_AT_LAST_STATE;
115 }
116
117 template <typename _Type>
119 return JAC_AT_LAST_STATE;
120 }
121
122 template <typename _Type>
123 inline unsigned Residual<_Type>::get_order() const {
124 return ORDER_OF_SYSTEM;
125 }
126
127 template <typename _Type>
128 inline unsigned Residual<_Type>::get_number_of_vars() const {
129 return NUMBER_OF_VARS;
130 }
131
132 template <typename _Type>
133 inline _Type& Residual<_Type>::delta() {
134 return DELTA;
135 }
136
137 template <typename _Type>
138 inline const _Type& Residual<_Type>::delta() const {
139 return DELTA;
140 }
141
142} // end namespace
143
144#endif
@ f
Definition: BVPBerman.cpp:15
A matrix class that constructs a DENSE matrix as an STL Vector of DenseVectors.
Specification for a templated DenseVector class – a dense, dynamic, vector object.
A spec for the CppNoddy Timer object.
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
Definition: DenseMatrix.h:25
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A generic runtime exception.
Definition: Exceptions.h:158
A base class to be inherited by objects that define residuals.
Definition: Residual.h:15
const DenseMatrix< _Type > & jacobian() const
Retrun a handle to the Jacobian of the residual corresponding to the last update state.
Definition: Residual.h:118
_Type DELTA
A default step for FD computation of the Jacobian.
Definition: Residual.h:88
const DenseVector< _Type > & residual() const
Return a handle to the residuals corresponding to the last update state.
Definition: Residual.h:113
unsigned get_number_of_vars() const
Get the number of variables that this residual condition is defined for.
Definition: Residual.h:128
const _Type & delta() const
Definition: Residual.h:138
virtual void residual_fn(const DenseVector< _Type > &state, DenseVector< _Type > &f) const
A blank virtual residual function method.
Definition: Residual.h:63
unsigned get_order() const
Get the order of the residual vector.
Definition: Residual.h:123
virtual ~Residual()
An empty destructor, virtual since we have virtual methods.
Definition: Residual.cpp:37
DenseVector< _Type > FN_AT_LAST_STATE
Residual for the last state vector.
Definition: Residual.h:84
_Type & delta()
Definition: Residual.h:133
unsigned NUMBER_OF_VARS
The number of elements in the state vector.
Definition: Residual.h:92
DenseVector< _Type > LAST_STATE
The last state vector.
Definition: Residual.h:86
DenseMatrix< _Type > JAC_AT_LAST_STATE
Jacobian for the last state vector.
Definition: Residual.h:82
unsigned ORDER_OF_SYSTEM
The order of the system of equations.
Definition: Residual.h:90
void update(const DenseVector< _Type > &state)
Update the Residual object for the current set of state variables.
Definition: Residual.h:100
A simple CPU-clock-tick timer for timing metods.
Definition: Timer.h:19
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt