In this example, the scattering of an incoming plane wave between two bodies is simulated as depicted in the following figure:
|
Scattering of an incoming plane wave |
The mathematical formulation leads to an integral equation based on the Helmholtz kernel. Solving such an equation involves the discrete representation of the analytic operators and, if neccessary, the computation of a preconditioner. Both shall be performed using the 𝓗-matrix technique.
Problem Description
Let be convex sets with smooth boundaries . Furthermore, let with be a sequence, such that for all .
To compute: densities on defined by
with
with , and with
With a standard Galerkin discretisation using constant ansatz functions, one obtains the following matrices
and
This leads to the following algorithm, which iterates between the surfaces :
- build and ;
- build initial right hand side ;
- for
- solve ;
- if converged stop
- ;
- solve ;
- ;
Grid Definition
After the standard initialisation, which also imports the 𝓗𝖫𝖨𝖡𝗉𝗋𝗈 types for real and complex values,
#include <hlib.hh>
using namespace HLIB;
using namespace std;
typedef HLIB::real real_t;
typedef HLIB::complex complex_t;
int main ( int argc, char ** argv )
{
try
{
INIT();
the program begins with the definition of the grid for the surfaces and . While the grid is read from a file, we assume, that it contains a sphere, centered at with radius 1.
unique_ptr< TGrid > grid1(
read_grid(
"sphere.tri" ) );
grid1->scale( T3Point( 4, 4, 1 ) );
The second command is optional. It deforms the sphere into a disc, which extends the scattering process.
Usually, a grid in 𝓗𝖫𝖨𝖡𝗉𝗋𝗈 will only define triangle normals. Since this leads to constant normal directions per triangle, the approximation of the Helmholtz DLP kernel is less accurate and may even lead to false results (think of scattering between parallel planes). Therefore, the grid file should contain vertex normals, which are used to interpolate normal directions for each point on the triangle, leading to a continuous normal direction on the surface.
If vertex normals are missing, the can be trivially added under the above assumptions since each vertex coordinate equals the corresponding normal direction. You may use add_vtx_normal
of TGrid for this.
Since we need two surfaces, the process is repeated, but in addition, the second disc is shifted with respect to the z direction, such that both disc will have a positive distance:
unique_ptr< TGrid > grid2(
read_grid(
"sphere.tri" ) );
grid2->scale( T3Point( 4, 4, 1 ) );
grid2->translate( T3Point( 0, 0, 2.5 ) );
This leads to the following configuration:
The construction of the grid ends with the definition of the corresponding function spaces. Here, we restrict ourselves to piecewise constant ansatz:
TConstFnSpace fnspace1( grid1.get() );
TConstFnSpace fnspace2( grid2.get() );
Matrix Construction
We need to construct the matrices and . Due to definition of the grid, the matrices and are identical.
For the remaining matrices, cluster trees and block cluster trees are needed:
unique_ptr< TCoordinate > coord1( fnspace1.build_coord() );
unique_ptr< TCoordinate > coord2( fnspace2.build_coord() );
TAutoBSPPartStrat part_strat;
TBSPCTBuilder ct_builder( & part_strat );
unique_ptr< TClusterTree > ct1( ct_builder.build( coord1.get() ) );
unique_ptr< TClusterTree > ct2( ct_builder.build( coord2.get() ) );
TStdAdmCond adm_cond( 2.0 );
TBCBuilder bct_builder;
unique_ptr< TBlockClusterTree > bct11( bct_builder.build( ct1.get(), ct1.get(), & adm_cond ) );
unique_ptr< TBlockClusterTree > bct12( bct_builder.build( ct1.get(), ct2.get(), & adm_cond ) );
unique_ptr< TBlockClusterTree > bct21( bct_builder.build( ct2.get(), ct1.get(), & adm_cond ) );
First, the matrix shall be constructed. 𝓗𝖫𝖨𝖡𝗉𝗋𝗈 provides the class TAcousticScatterBF, which implements the kernel for . Not defined so far is the wavenumber , which should have a value of 6. Furthermore, ACA+ is used to compute low-rank approximations with a fixed block-wise accuracy of .
typedef TAcousticScatterBF< TConstFnSpace, TConstFnSpace > bf_t;
complex_t kappa( 4, 0 );
TTruncAcc acc = fixed_prec( 1e-4 );
unique_ptr< TMatrix > K11;
{
bf_t bf( kappa, & fnspace1, & fnspace1 );
TBFCoeffFn< bf_t > coeff_fn( & bf, bct11->row_ct()->perm_i2e(), bct11->col_ct()->perm_i2e() );
TACAPlus< complex_t > lrapx( & coeff_fn );
TDenseMBuilder< complex_t > h_builder( & coeff_fn, & lrapx );
K11 = unique_ptr< TMatrix >( h_builder.build( bct11.get(), acc ) );
}
The actual operator in the above equation is . Here is not the identity, but the mass matrix, as defined by the identity kernel:
typedef TMassBF< TConstFnSpace, TConstFnSpace > massbf_t;
{
massbf_t bf( & fnspace1, & fnspace1 );
TBFCoeffFn< massbf_t > coeff_fn( & bf, bct11->row_ct()->perm_i2e(), bct11->col_ct()->perm_i2e() );
TACAPlus< real_t > lrapx( & coeff_fn );
TDenseMBuilder< real_t > h_builder( & coeff_fn, & lrapx );
M11 = unique_ptr< TMatrix >( h_builder.build( bct11.get(), acc ) );
M11->set_complex( true );
}
Finally, is updated with the mass matrix:
add( 1.0, M11.get(), -1.0, K11.get(), acc );
The construction of and is similar to . Only the function spaces and block cluster trees will change.
Please note, that is more dense than the other two matrices. This is due to the positive distance between both discs. If this is increased (or if coarsening is applied), and may be represented by a single low-rank matrix.
Right-Hand Side
The initial right-hand side is defined by above, where also the the incident field is used:
Under the above setting, this is equal to:
and is implemented in the following RHS function:
class TAcousticRHS : public TBEMFunction< complex_t >
{
private:
const T3Point _inc_dir;
const complex_t _ikappa;
public:
TAcousticRHS ( const T3Point & inc_dir,
const complex_t kappa )
: _inc_dir( inc_dir )
, _ikappa( complex_t( 0, 1 ) * kappa )
{}
virtual complex_t
eval (
const T3Point & pos,
const T3Point & normal ) const
{
return real_t(2) * _ikappa * exp( _ikappa *
dot( _inc_dir, pos ) ) *
dot( _inc_dir, normal );
}
};
We use TQuadBEMRHS to compute the corresponding vector of the RHS using quadrature:
TAcousticRHS rhsfn( T3Point( 1, 1, 0 ), kappa );
TQuadBEMRHS< TConstFnSpace, complex_t > rhs_build( 4 );
unique_ptr< TVector > rhs( rhs_build.build( ct1->root(),
& fnspace1,
& rhsfn,
ct1->perm_i2e() ) );
The plane wave is chosen to be .
Acoustic Scattering Iteration
The scattering iteration involves to solves with ( ). This is accelerated using a preconditioner computed by and 𝓗-LU factorisation:
unique_ptr< TMatrix > K11_copy( K11->copy() );
unique_ptr< TLinearOperator > K11_pre(
factorise_inv( K11_copy.get(), acc, progress.get() ) );
We also need a vector to hold the current and the last approximation of the solution:
unique_ptr< TVector > x( K11->col_vector() );
unique_ptr< TVector > x_old( K11->col_vector() );
real_t old_ratio = real_t(1);
The variable old_ratio
is needed to check convergence.
The iteration begins with solving , where is the vector of the RHS:
for ( int it = 0; it < 100; ++it )
{
solve( K11.get(), x.get(), rhs.get(), K11_pre.get() );
Convergence is tested, if the ratio between the previous solution and the current solution is below some threshold. For this ratio is tested entry-wise, e.g. for each . This is implemented in the function compare:
real_t
compare ( const TVector * x,
const TVector * x_old )
{
real_t vmax = 0.0;
size_t n = x->size();
for ( idx_t j = 0; j < idx_t(n); ++j )
{
const complex_t v = x->centry( j );
const complex_t v_old = x_old->centry( j );
vmax = std::max( vmax, abs( v / v_old ) );
}
return vmax;
}
If this ratio does no longer change, the iteration is stopped. The threshold in this example is :
if ( it > 0 )
{
const real_t ratio = compare( x.get(), x_old.get() );
const real_t diff = ( it > 1 ? std::abs( ratio - old_ratio ) / std::abs( old_ratio ) : 1.0 );
old_ratio = ratio;
if ( diff < 1e-4 )
break;
}
In addition to this, the iteration shall stop if the solution vanishes:
const real_t normx = x->norm2();
if ( x->norm2() < 1e-20 )
break;
Otherwise, the iteration continues with the scaling of the solution vector and the computation of the RHS for :
x->scale( real_t(1) / normx );
x_old->assign( real_t(1), x.get() );
mul_vec( real_t(-1), K12.get(), x.get(), real_t(0), rhs.get(), MATOP_NORM );
Solving with ( ) and computing the RHS for the next iteration finished the loop and the program:
solve( K11.get(), x.get(), rhs.get(), K11_pre.get(), & solve_info );
mul_vec( real_t(-1), K21.get(), x.get(), real_t(0), rhs.get(), MATOP_NORM );
}
DONE();
}
catch ( Error & e )
{
cout << e.to_string() << endl;
}
return 0;
}
Running the simulation will produce the following output for and . Here, the entry-wise absolute values of the complex valued solution vectors are used for the visualisation (reload page to restart animation):
The Plain Program
#include <hlib.hh>
using namespace HLIB;
using namespace std;
typedef HLIB::real real_t;
typedef HLIB::complex complex_t;
class TAcousticRHS : public TBEMFunction< complex_t >
{
private:
const T3Point _inc_dir;
const complex_t _ikappa;
public:
TAcousticRHS ( const T3Point & inc_dir,
const complex_t kappa )
: _inc_dir( inc_dir )
, _ikappa( complex_t( 0, 1 ) * kappa )
{}
virtual complex_t
eval (
const T3Point & pos,
const T3Point & normal ) const
{
return real_t(2) * _ikappa * exp( _ikappa *
dot( _inc_dir, pos ) ) *
dot( _inc_dir, normal );
}
};
real_t
{
real_t vmax = 0.0;
for ( idx_t j = 0; j < idx_t(n); ++j )
{
const complex_t v = x->
centry( j );
const complex_t v_old = x_old->
centry( j );
vmax = std::max( vmax, abs( v / v_old ) );
}
return vmax;
}
int main ( int argc, char ** argv )
{
try
{
INIT();
unique_ptr< TGrid > grid1(
read_grid(
"sphere.tri" ) );
grid1->scale( T3Point( 4, 4, 1 ) );
unique_ptr< TGrid > grid2(
read_grid(
"sphere.tri" ) );
grid2->scale( T3Point( 4, 4, 1 ) );
grid2->translate( T3Point( 0, 0, 2.5 ) );
TConstFnSpace fnspace1( grid1.get() );
TConstFnSpace fnspace2( grid2.get() );
unique_ptr< TCoordinate > coord1( fnspace1.build_coord() );
unique_ptr< TCoordinate > coord2( fnspace2.build_coord() );
unique_ptr< TClusterTree > ct1( ct_builder.build( coord1.get() ) );
unique_ptr< TClusterTree > ct2( ct_builder.build( coord2.get() ) );
unique_ptr< TBlockClusterTree > bct11( bct_builder.
build( ct1.get(), ct1.get(), & adm_cond ) );
unique_ptr< TBlockClusterTree > bct12( bct_builder.
build( ct1.get(), ct2.get(), & adm_cond ) );
unique_ptr< TBlockClusterTree > bct21( bct_builder.
build( ct2.get(), ct1.get(), & adm_cond ) );
complex_t kappa( 6, 0 );
unique_ptr< TMatrix > K11;
{
bf_t bf( kappa, & fnspace1, & fnspace1 );
TBFCoeffFn< bf_t > coeff_fn( & bf, bct11->row_ct()->perm_i2e(), bct11->col_ct()->perm_i2e() );
TDenseMBuilder< complex_t > h_builder( & coeff_fn, & lrapx );
K11 = unique_ptr< TMatrix >( h_builder.build( bct11.get(), acc ) );
}
unique_ptr< TMatrix > M11;
{
massbf_t bf( & fnspace1, & fnspace1 );
TDenseMBuilder< real_t > h_builder( & coeff_fn, & lrapx );
M11 = unique_ptr< TMatrix >( h_builder.build( bct11.get(), acc ) );
}
add( 1.0, M11.get(), -1.0, K11.get(), acc );
unique_ptr< TMatrix > K12;
{
bf_t bf( kappa, & fnspace1, & fnspace2 );
TBFCoeffFn< bf_t > coeff_fn( & bf, bct12->row_ct()->perm_i2e(), bct12->col_ct()->perm_i2e() );
TDenseMBuilder< complex_t > h_builder( & coeff_fn, & lrapx );
K12 = unique_ptr< TMatrix >( h_builder.build( bct12.get(), acc ) );
}
unique_ptr< TMatrix > K21;
{
bf_t bf( kappa, & fnspace2, & fnspace1 );
TBFCoeffFn< bf_t > coeff_fn( & bf, bct21->row_ct()->perm_i2e(), bct21->col_ct()->perm_i2e() );
TDenseMBuilder< complex_t > h_builder( & coeff_fn, & lrapx );
K21 = unique_ptr< TMatrix >( h_builder.build( bct21.get(), acc ) );
}
TAcousticRHS rhsfn( T3Point( 1, 1, 0 ), kappa );
TQuadBEMRHS< TConstFnSpace, complex_t >
rhs_build( 4 );
unique_ptr< TVector > rhs( rhs_build.build( ct1->root(),
& fnspace1,
& rhsfn,
ct1->perm_i2e() ) );
unique_ptr< TMatrix > K11_copy( K11->
copy() );
unique_ptr< TLinearOperator > K11_pre(
factorise_inv( K11_copy.get(), acc ) );
real_t old_ratio = real_t(1);
for ( int it = 0; it < 100; ++it )
{
solve( K11.get(), x.get(), rhs.get(), K11_pre.get() );
if ( it > 0 )
{
const real_t ratio = compare( x.get(), x_old.get() );
const real_t diff = ( it > 1 ? std::abs( ratio - old_ratio ) / std::abs( old_ratio ) : 1.0 );
old_ratio = ratio;
if ( diff < 1e-4 )
break;
}
const real_t normx = x->
norm2();
if ( x->
norm2() < 1e-20 )
break;
x->
scale( real_t(1) / normx );
x_old->
assign( real_t(1), x.get() );
mul_vec( real_t(-1), K12.get(), x.get(), real_t(0), rhs.get(), MATOP_NORM );
solve( K11.get(), x.get(), rhs.get(), K11_pre.get() );
mul_vec( real_t(-1), K21.get(), x.get(), real_t(0), rhs.get(), MATOP_NORM );
}
DONE();
}
catch ( Error & e )
{
cout << e.to_string() << endl;
}
return 0;
}