CppNoddy  0.92
Loading...
Searching...
No Matches
ExceptionChecks.cpp
Go to the documentation of this file.
1/// \file ExceptionChecks.cpp
2/// \ingroup Tests
3/// \ingroup Generic
4/// We do some obviously dumb things to induce a handful
5/// of common failures. The failures should result in exceptions
6/// being thrown, which should be caught. The test is passed if
7/// the required number of exceptions are successfully caught.
8
9#include <Newton_bundle.h>
10#include <DenseLinearSystem.h>
11
12namespace CppNoddy
13{
14 namespace Example
15 {
16 /// The problem to be solved, here z^3 -1 in complex form.
17 class Cube_root_problem : public Residual<D_complex>
18 {
19 public:
20
22 {}
23
24 /// The residual function for z^3 -1
25 /// \param z The independent variable.
26 /// \param f The value of the residual f(z).
28 {
29 f[ 0 ] = z[ 0 ] * z[ 0 ] * z[ 0 ] - 1.0;
30 }
31 };
32 } // end Example namespace
33} // end CppNoddy namespace
34
35
36using namespace CppNoddy;
37using namespace std;
38
39int main()
40{
41
42 cout << "\n";
43 cout << "=== Exception checks: testing forced failures =======\n";
44 cout << "\n";
45
46
47 int exceptions_caught( 0 );
48 int tests( 0 );
49
50 {
51 ++tests;
52 // set up noddy singular 2x2 problem
53 DenseMatrix<double> A( 2, 2, 0.0 );
54 DenseVector<double> B( 2, 0.0 );
55 A( 0, 0 ) = 1.;
56 A( 0, 1 ) = 2.;
57 A( 1, 0 ) = 2.; // row 1 = 2 * row 0 -- so singular
58 A( 1, 1 ) = 4.;
59 B[ 0 ] = 5.;
60 B[ 1 ] = 11.;
61
62 DenseLinearSystem<double> system( &A, &B, "native" );
63
64 // singular matrix should be caught in the native solver
65 try
66 {
67 system.solve();
68 }
69 catch ( const std::exception & e )
70 {
71 ++exceptions_caught;
72 cout << " Caught a runtime exception in the native linear solver \n\n";
73 }
74 }
75
76 {
77 // too many potential geometry errors to test ... just the add method here
78#ifdef PARANOID
79 ++tests;
80 // geometry checkes are only made under PARANOID flags
81 DenseMatrix<double> C( 3, 3, 1.0 );
82 DenseMatrix<double> D( 2, 2, 1.0 );
83 try
84 {
85 C.add( D );
86 }
87 catch ( std::exception & e )
88 {
89 ++exceptions_caught;
90 cout << " Caught a geometry exception when adding two matrices. \n\n";
91 }
92#endif
93
94 }
95
96 {
97 ++tests;
98 // set the tolerance & max iteration number too low
100 Newton<D_complex> newton( &problem, 8, 1.e-12 );
101 D_complex half( 0.5, 0.5 );
102 DenseVector<D_complex> guess( 1, half );
103 try
104 {
105 newton.iterate( guess );
106 }
107 catch ( std::exception & e )
108 {
109 ++exceptions_caught;
110 cout << " Caught an iteration exception in a scalar Newton problem. \n\n";
111 }
112 }
113
114 if ( tests != exceptions_caught )
115 {
116 cout << "\033[1;31;48m * FAILED \033[0m\n";
117 cout << tests << " checks were run but only " << exceptions_caught
118 << " exceptions were caught!\n";
119 return 1;
120 }
121 else
122 {
123 cout << "\033[1;32;48m * PASSED \033[0m\n";
124 return 0;
125 }
126
127}
@ f
Definition: BVPBerman.cpp:15
Specification of the linear system class.
int main()
A shorter bundled include file for Newton iteration problems.
A linear system class for vector right-hand sides.
void solve()
Solve the sparse system.
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
Definition: DenseMatrix.h:25
void add(const DenseMatrix< _Type > &b)
Add a DENSE matrix to this object.
Definition: DenseMatrix.cpp:76
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
The problem to be solved, here z^3 -1 in complex form.
void residual_fn(const DenseVector< D_complex > &z, DenseVector< D_complex > &f) const
The residual function for z^3 -1.
A vector NEWTON iteration class.
Definition: Newton.h:25
void iterate(DenseVector< _Type > &x)
The Newton iteration method.
Definition: Newton.cpp:35
A base class to be inherited by objects that define residuals.
Definition: Residual.h:15
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...
std::complex< double > D_complex
A complex double precision number using std::complex.
Definition: Types.h:98

© 2012

R.E. Hewitt