WaveSimPP 1.0
A C library for solving the wave equation and reconstructing the wave field
All Classes Namespaces Functions Modules Pages
solver.hpp
1//
2// Created by Yan Cheng on 12/6/22.
3//
4
5#ifndef CORETESTS_CPP_SOLVER2_HPP
6#define CORETESTS_CPP_SOLVER2_HPP
7
8#include "CustomLibraries/np.hpp"
9
23namespace waveSimCore
24{
27 boost::multi_array<double, 3> wave_solver(boost::multi_array<double, 2> c,
28 double dt, double dx, double dz, int nt, int nx, int nz,
29 boost::multi_array<double, 3> f)
30 {
31
32 const boost::multi_array<double, 2> CX = np::pow(c * dt / dx, 2.0);
33 const boost::multi_array<double, 2> CZ = np::pow(c * dt / dz, 2.0);
34 const double Cf = np::pow(dt, 2.0);
35
36 int dimensions_1[] = {nt, nx, nz};
37 boost::multi_array<double, 3> u = np::zeros<double>(dimensions_1);
38 int dimensions_4[] = {nx, nz};
39 boost::multi_array<double, 2> u_xx = np::zeros<double>(dimensions_4);
40 int dimensions_5[] = {nx, nz};
41 boost::multi_array<double, 2> u_zz = np::zeros<double>(dimensions_5);
42
43 for (int n = 1; n < nt - 1; n++)
44 {
45 for (int i = 1; i < nx - 1; i++)
46 {
47 for (int j = 1; j < nz - 1; j++)
48 {
49 u_xx[i][j] = u[n][i + 1][j] + u[n][i - 1][j] - 2.0 * u[n][i][j];
50 u_zz[i][j] = u[n][i][j + 1] + u[n][i][j - 1] - 2.0 * u[n][i][j];
51 }
52 }
53
54 // ! Update u
55 for (int i = 0; i < nx; i++)
56 {
57 for (int j = 0; j < nz; j++)
58 {
59 u[n + 1][i][j] = 2.0 * u[n][i][j] + CX[i][j] * u_xx[i][j] + CZ[i][j] * u_zz[i][j] + Cf * f[n][i][j] - u[n - 1][i][j];
60 }
61 }
62
63 // Dirichlet boundary condition
64 for (int i = 0; i < nx; i++)
65 {
66 u[n + 1][i][0] = 0.0;
67 u[n + 1][i][nz - 1] = 0.0;
68 }
69 for (int j = 0; j < nz; j++)
70 {
71 u[n + 1][0][j] = 0.0;
72 u[n + 1][nx - 1][j] = 0.0;
73 }
74 }
75 return u;
76 }
77}
78#endif // CORETESTS_CPP_SOLVER2_HPP
boost::multi_array< double, 3 > wave_solver(boost::multi_array< double, 2 > c, double dt, double dx, double dz, int nt, int nx, int nz, boost::multi_array< double, 3 > f)
Definition: solver.hpp:27
::value constexpr boost::multi_array< T, ND > pow(const boost::multi_array< T, ND > &input_array, const T exponent)
Implements the numpy pow function on multi arrays.
Definition: np.hpp:319