CppNoddy  0.92
Loading...
Searching...
No Matches
Classes | Namespaces | Functions
ExceptionChecks.cpp File Reference

We do some obviously dumb things to induce a handful of common failures. More...

#include <Newton_bundle.h>
#include <DenseLinearSystem.h>

Go to the source code of this file.

Classes

class  CppNoddy::Example::Cube_root_problem
 The problem to be solved, here z^3 -1 in complex form. More...
 

Namespaces

namespace  CppNoddy
 A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechanics.
 
namespace  CppNoddy::Example
 

Functions

int main ()
 

Detailed Description

We do some obviously dumb things to induce a handful of common failures.

The failures should result in exceptions being thrown, which should be caught. The test is passed if the required number of exceptions are successfully caught.

Definition in file ExceptionChecks.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 39 of file ExceptionChecks.cpp.

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
99 Example::Cube_root_problem problem;
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}
A linear system class for vector right-hand sides.
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 vector NEWTON iteration class.
Definition: Newton.h:25
double A(1.0)
initial hump amplitude
std::complex< double > D_complex
A complex double precision number using std::complex.
Definition: Types.h:98

References CppNoddy::DenseMatrix< _Type >::add(), CppNoddy::Newton< _Type >::iterate(), and CppNoddy::DenseLinearSystem< _Type >::solve().

© 2012

R.E. Hewitt