WaveSimPP 1.0
A C library for solving the wave equation and reconstructing the wave field
All Classes Namespaces Functions Modules Pages
CoreTests.cpp
1//
2// Created by Yan Cheng on 12/2/22.
3//
4
5#include <boost/multi_array.hpp>
6#include <boost/array.hpp>
7#include "CustomLibraries/np.hpp"
8#include "CustomLibraries/np_to_matplot.hpp"
9#include "CustomLibraries/wavePlotter.hpp"
10#include <matplot/matplot.h>
11#include <cassert>
12#include <iostream>
13#include <sstream>
14
15#include "CoreAlgorithm/helper_func.hpp"
16#include "CoreAlgorithm/coeff.hpp"
17#include "CoreAlgorithm/source.hpp"
18#include "CoreAlgorithm/computational.hpp"
19#include "CoreAlgorithm/solver.hpp"
20
21std::string format_num(int num, int length = 8)
22{
23 std::string str_num = std::to_string(num);
24
25 int str_length = str_num.length();
26 for (int i = 0; i < length - str_length; i++)
27 str_num = "0" + str_num;
28 return str_num;
29}
30
31void test_()
32{
33 int num_levels = 100;
34 int nx = 100;
35 int nz = 100;
36 int nt = 1000;
37
38 double dx = 0.01;
39 double dz = 0.01;
40 double dt = 0.001;
41
42 double xmin = 0.0;
43 double xmax = nx * dx;
44 double zmin = 0.0;
45 double zmax = nz * dz;
46 double tmin = 0.0;
47 double tmax = nt * dt;
48
49 double f_M = 10.0;
50 double amp = 1e0;
51 double shift = 0.1;
52
53 boost::multi_array<double, 3> f = waveSimCore::ricker(50, 50, f_M, amp, shift, tmin, tmax, nt, nx, nz);
54
55 double r = 150.0;
56 boost::multi_array<double, 2> vel = waveSimCore::get_profile(xmin, xmax, zmin, zmax, nx, nz, r);
57
58 boost::multi_array<double, 3> u = waveSimCore::wave_solver(vel, dt, dx, dz, nt, nx, nz, f);
59 double min_u = np::min(u);
60 double max_u = np::max(u);
61 std::cout << "min_u = " << min_u << " max_u = " << max_u << "\n";
62 std::vector<double> levels = matplot::linspace(min_u, max_u, num_levels);
63 std::cout << u[20][10][10] << "\n";
64
65 // boost::multi_array<double, 2> u20(boost::extents[nx][nz]);
66 // for (int i = 0; i < nx; i++)
67 // {
68 // for (int j = 0; j < nz; j++)
69 // {
70 // u20[i][j] = u[10][i][j];
71 // std::cout << u20[i][j] << " ";
72 // }
73 // std::cout << "\n";
74 // }
75 // std::cout << "\n";
76
77 boost::multi_array<double, 1> x = np::linspace(xmin, xmax, nx);
78 boost::multi_array<double, 1> z = np::linspace(zmin, zmax, nz);
79 const boost::multi_array<double, 1> axis[2] = {x, z};
80 std::vector<boost::multi_array<double, 2>> XcZ = np::meshgrid(axis, false, np::xy);
81
82 matplot::vector_2d Xp = np::convert_to_matplot(XcZ[0]);
83 matplot::vector_2d Zp = np::convert_to_matplot(XcZ[1]);
84 // matplot::contourf(Xp, Zp, vel, levels, "", num_levels);
85 // matplot::show();
86
87 wavePlotter::Plotter my_plotter(u, Xp, Zp, num_levels, nt);
88 // my_plotter.exportAllFrames(0, nt - 1);
89 my_plotter.animate("output-test.mp4", 20, nt - 1, 30);
90
91 // for (int i = 10; i < nt - 1; i++)
92 // {
93 // matplot::vector_2d Up = np::convert_to_matplot(u[i]);
94 // matplot::contourf(Xp, Zp, Up, levels);
95 // matplot::save("output/contourf_" + format_num(i) + ".png");
96 // }
97}
98
99int main()
100{
101 test_();
102}
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