CppNoddy  0.92
Loading...
Searching...
No Matches
HYPAcousticImpedance.cpp
Go to the documentation of this file.
1/// \file HYPAcousticImpedance.cpp
2/// \ingroup Tests
3/// \ingroup HYP_1D
4/// Solve the constant coefficient linear sound wave problem
5/// \f[ p_t + K u_x = 0 \f]
6/// \f[ \rho u_t + p_x = 0 \f]
7/// for a right-propagating square pressure pulse in a medium
8/// with constant bulk modulus \f$K=4\f$ and density \f$\rho = 1\f$
9/// if \f$\vert x \vert < 1\f$ and \f$\rho = 4\f$ elsewhere.
10/// The (first) reflected wave's velocity amplitude is
11/// \f[ - \left ( \frac{\frac{K_1}{K_2}-\frac{c_1}{c_2}}{\frac{K_1}{K_2}+\frac{c_1}{c_2}} \right ) \f]
12/// where \f$ K_{1,2} \f$ and \f$c_{1,2}\f$ are the bulk modulus and wave speed
13/// before and after the density interface, with \f$c_i = \sqrt{ K_i \rho_i } \f$.
14
15#include <OneD_HYP_bundle.h>
16
17enum { p, u };
18
19namespace CppNoddy
20{
21 namespace Example
22 {
23 /// bulk modulus
24 double K( 4.0 );
25 /// higher density
26 double rho_big( 4.0 );
27 /// lower density
28 double rho_small( 1.0 );
29 /// Density function for the medium
30 double rho( const double &x )
31 {
32 if ( std::abs( x ) < 1.0 )
33 {
34 return rho_small;
35 }
36 else
37 {
38 return rho_big;
39 }
40 }
41
42 /// Define the system
44 {
45
46 public:
47
48 /// One dimemsional constant coefft acoustic problem
50 {}
51
52 /// Define the vector flux
53 void flux_fn( const double &x, const DenseVector<double> &q, DenseVector<double> &f ) const
54 {
55 f[ p ] = K * q[ u ];
56 f[ u ] = q[ p ] / rho( x );
57 }
58
59 /// Bound the shock speed
60 double max_charac_speed( const DenseVector<double> &q ) const
61 {
62 // sound speeds
63 double c = sqrt( K / rho_small );
64 // maximum shock speed
65 return c;
66 }
67
68 };
69
70 /// Set the initial state of the system
71 void Q_init( const double &x, DenseVector<double> &q )
72 {
73 if ( ( x < -1.5 ) && ( x > -2.5 ) )
74 {
75 q[ p ] = 1.0;
76 q[ u ] = q[ p ] / sqrt( K * rho( x ) );
77 }
78 else
79 {
80 q[ p ] = q[ u ] = 0.0;
81 }
82 }
83 } //end Example namespace
84
85} //end CppNoddy namespace
86
87
88using namespace CppNoddy;
89using namespace std;
90
91int main()
92{
93 cout << "\n";
94 cout << "=== Hyperbolic: 1D acoustic wave, impedance problem =\n";
95 cout << "\n";
96
97 // define the domain/mesh
98 const double left = -7.0;
99 const double right = 7.0;
100 const unsigned N = 800;
101 DenseVector<double> faces_x = Utility::uniform_node_vector( left, right, N );
102
103 // time
104 double t = 0.0;
105 double t_end = 5.0;
106
107 // hyperbolic problem
108 Example::Acoustic_1d conservative_problem;
109 OneD_TVDLF_Mesh Acoustic_mesh( faces_x, &conservative_problem, Example::Q_init );
110 Acoustic_mesh.set_limiter( 1 );
111
112 // mesh & test info
113 double I1 = Acoustic_mesh.integrate()[0];
114 int loop_counter( 4 );
115 int file_counter( 1 );
116
117 std::string dirname("./DATA");
118 mkdir( dirname.c_str(), S_IRWXU );
119 std::string filename_stub;
120 filename_stub = "./DATA/HYP_acoustic_imp";
121 TrackerFile my_file( 5 );
122 OneD_Node_Mesh<double> soln = Acoustic_mesh.get_soln();
123 my_file.push_ptr( &soln, "mesh" );
124
125 do
126 {
127 t += Acoustic_mesh.update( 0.499, t_end - t );
128
129 if ( loop_counter % 2 == 0 )
130 {
131 my_file.set_filename( filename_stub + Utility::stringify( file_counter ) + ".dat" );
132 soln = Acoustic_mesh.get_soln();
133 my_file.update();
134 file_counter += 1;
135 }
136 ++loop_counter;
137
138 }
139 while ( ( t < t_end ) && ( loop_counter < 1500 ) );
140
141 soln = Acoustic_mesh.get_soln();
142 double I2 = Acoustic_mesh.integrate()[0];
143 // the first reflected and first transmitted pulses can be determined explicitly.
144 // they have amplitude ratios of 1/3 and 8/9ths of the input amplitude for the pressure.
145 soln = Acoustic_mesh.get_soln();
146 double E1 = std::abs( soln.get_interpolated_vars( -5.0 )[0] - 1. / 3. );
147 double E2 = std::abs( soln.get_interpolated_vars( 4.0 )[0] - 8. / 9. );
148 if ( ( E1 > 1.e-4 ) || ( E2 > 1.e-4 ) || ( std::abs( I1 - I2 ) > 1.e-8 ) || ( loop_counter >= 1500 ) )
149 {
150 cout << "\033[1;31;48m * FAILED \033[0m\n";
151 return 1;
152 }
153 else
154 {
155 cout << "\033[1;32;48m * PASSED \033[0m\n";
156 return 0;
157 }
158
159} // end of main()
@ f
Definition: BVPBerman.cpp:15
int main()
A shorter bundled include file for hyperbolic problems.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
void flux_fn(const double &x, const DenseVector< double > &q, DenseVector< double > &f) const
Define the vector flux.
Acoustic_1d()
One dimemsional constant coefft acoustic problem.
double max_charac_speed(const DenseVector< double > &q) const
Bound the shock speed.
A class to represent a one dimensional hyperbolic system of equations.
A one dimensional mesh utility object.
DenseVector< _Type > get_interpolated_vars(const _Xtype &pos) const
Get the variable data at an interpolated position using a first order scheme.
OneD_Node_Mesh< double > get_soln(std::string location="centre", std::string colour="black")
Get a OneD_Mesh<double> object containing the one dimensional data in the usual format.
double update(const double &CFL, const double &max_dt=std::numeric_limits< long double >::max())
Update all the elements in this mesh to a new time step.
void set_limiter(unsigned id)
Set the limiter type to be applied in the slope values.
DenseVector< double > integrate() const
Integrate the concentration values across the entire mesh.
void push_ptr(double *scalar, std::string desc="")
Definition: TrackerFile.cpp:27
void set_filename(std::string filename)
Definition: TrackerFile.cpp:70
void Q_init(const double &x, DenseVector< double > &q)
Set the initial state of the system.
double K(4.0)
bulk modulus
double rho_small(1.0)
lower density
double rho_big(4.0)
higher density
std::string stringify(const int &val)
Return an integer value as a string - useful for file naming.
Definition: Utility.cpp:193
DenseVector< double > uniform_node_vector(const double &lower, const double &upper, const std::size_t &N)
Return a DENSE vector with the nodal points of a uniform mesh distributed between the upper/lower bou...
Definition: Utility.cpp:113
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt