WaveSimPP 1.0
A C library for solving the wave equation and reconstructing the wave field
All Classes Namespaces Functions Modules Pages
wave_solver_with_animation.cpp
1#include <boost/multi_array.hpp>
2#include <boost/array.hpp>
3#include "CustomLibraries/np.hpp"
4#include "CustomLibraries/np_to_matplot.hpp"
5#include "CustomLibraries/wavePlotter.hpp"
6#include <matplot/matplot.h>
7#include <cassert>
8#include <iostream>
9#include <sstream>
10
11#include "CoreAlgorithm/helper_func.hpp"
12#include "CoreAlgorithm/coeff.hpp"
13#include "CoreAlgorithm/source.hpp"
14#include "CoreAlgorithm/computational.hpp"
15#include "CoreAlgorithm/solver.hpp"
16
17int main()
18{
19 // Define the constants for the simulation
20
21 // Number of x and z grid points
22 int nx = 100;
23 int nz = 100;
24 // Number of time steps
25 int nt = 1000;
26
27 // Differentiation values
28 double dx = 0.01;
29 double dz = 0.01;
30 double dt = 0.001;
31
32 // Define the domain
33 double xmin = 0.0;
34 double xmax = nx * dx;
35 double zmin = 0.0;
36 double zmax = nz * dz;
37 double tmin = 0.0;
38 double tmax = nt * dt;
39
40 // Define the source parameters
41 double f_M = 10.0;
42 double amp = 1e0;
43 double shift = 0.1;
44
45 // Source location
46 int source_is = 50;
47 int source_js = 50;
48
49 // Create the source
50 boost::multi_array<double, 3> f = waveSimCore::ricker(source_is, source_js, f_M, amp, shift, tmin, tmax, nt, nx, nz);
51
52 // Create the velocity profile
53 double r = 150.0;
54 boost::multi_array<double, 2> vel = waveSimCore::get_profile(xmin, xmax, zmin, zmax, nx, nz, r);
55
56 // Solve the wave equation
57 boost::multi_array<double, 3> u = waveSimCore::wave_solver(vel, dt, dx, dz, nt, nx, nz, f);
58
59 // Define the number of different levels for the contour plot
60 int num_levels = 100;
61 // Create the levels for the contour plot based on the min and max values of u
62 double min_u = np::min(u);
63 double max_u = np::max(u);
64 std::vector<double> levels = matplot::linspace(min_u, max_u, num_levels);
65
66 // Create the x and z axis for the contour plot and convert them to matplot format
67 boost::multi_array<double, 1> x = np::linspace(xmin, xmax, nx);
68 boost::multi_array<double, 1> z = np::linspace(zmin, zmax, nz);
69 const boost::multi_array<double, 1> axis[2] = {x, z};
70 std::vector<boost::multi_array<double, 2>> XcZ = np::meshgrid(axis, false, np::xy);
71
72 matplot::vector_2d Xp = np::convert_to_matplot(XcZ[0]);
73 matplot::vector_2d Zp = np::convert_to_matplot(XcZ[1]);
74
75 // Create the plotter object and animate the results
76 wavePlotter::Plotter my_plotter(u, Xp, Zp, num_levels, nt);
77
78 // If you want to render a specific frame, use this:
79 // my_plotter.renderFrame(int frame_index);
80
81 // Renders the entire animation from start_frame to end_frame
82 int start_frame = 20;
83 int end_frame = nt - 1;
84 int fps = 30;
85 my_plotter.animate("example-wave.mp4", start_frame, end_frame, fps);
86
87 // The animation will be saved in .
88 // Frames will be saved to ./output
89}
This class is used to plot the wave field TODO: make it multithreaded.
Definition: wavePlotter.hpp:22
matplot::vector_2d convert_to_matplot(const boost::multi_array< double, 2 > &arr)
Convert a 2D boost::multi_array to a matplot::vector_2d.
boost::multi_array< double, 3 > ricker(int i_s, int j_s, double f, double amp, double shift, double tmin, double tmax, int nt, int nx, int nz)
Get the Ricker wavelet as a 3D Array.
Definition: source.hpp:14
boost::multi_array< double, 2 > get_profile(double xmin, double xmax, double zmin, double zmax, int nx, int nz, double r)
Get the velocity profile of the model as a 2D Array.
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 std::vector< boost::multi_array< T, ND > > meshgrid(const boost::multi_array< T, 1 >(&cinput)[ND], bool sparsing=false, indexing indexing_type=xy)
Definition: np.hpp:184
boost::multi_array< double, 1 > linspace(double start, double stop, long unsigned int num)
Implements the numpy linspace function.
Definition: np.hpp:161
::value constexpr T min(boost::multi_array< T, ND > const &input_array)
Implements the numpy min function for an n-dimensionl multi array.
Definition: np.hpp:419
::value constexpr T max(boost::multi_array< T, ND > const &input_array)
Implements the numpy max function for an n-dimensionl multi array.
Definition: np.hpp:384