WaveSimPP 1.0
A C library for solving the wave equation and reconstructing the wave field
All Classes Namespaces Functions Modules Pages
MatPlotTest.cpp
1#include <matplot/matplot.h>
2#include <thread>
3#include "boost/multi_array.hpp"
4#include "boost/array.hpp"
5#include "CustomLibraries/np.hpp"
6#include "CustomLibraries/np_to_matplot.hpp"
7
8using namespace matplot;
9void test_simple_plot()
10{
11 std::vector<double> x = linspace(-2 * pi, 2 * pi);
12 std::vector<double> y = linspace(0, 4 * pi);
13 auto [X, Y] = meshgrid(x, y);
14 vector_2d Z =
15 transform(X, Y, [](double x, double y)
16 { return sin(x) + cos(y); });
17 contourf(X, Y, Z, 10);
18
19 show();
20}
21
22void test_conversion()
23{
24 boost::multi_array<double, 1> x = np::linspace(0, 1, 100);
25 boost::multi_array<double, 1> y = np::linspace(0, 1, 100);
26 // x = np::pow(x, 2.0);
27 // y = np::pow(y, 3.0);
28
29 const boost::multi_array<double, 1> axis[2] = {x, y};
30 std::vector<boost::multi_array<double, 2>> XcY = np::meshgrid(axis, false, np::xy);
31
32 double dx, dy;
33 dx = 1.0 / 100.0;
34 dy = 1.0 / 100.0;
35
36 boost::multi_array<double, 2> f = np::pow(XcY[0], 2.0) + XcY[0] * np::pow(XcY[1], 1.0);
37
38 // g.push_back(np::gradient(XcY[0], {dx, dy}));
39 // g.push_back(np::gradient(XcY[1], {dx, dy}));
40 std::vector<boost::multi_array<double, 2>> gradf = np::gradient(f, {dx, dy});
41 matplot::vector_2d Xp = np::convert_to_matplot(XcY[0]);
42 matplot::vector_2d Yp = np::convert_to_matplot(XcY[1]);
43 matplot::vector_2d X = matplot::meshgrid(linspace(0, 1, 100), linspace(0, 1, 100)).first;
44 matplot::vector_2d Y = matplot::meshgrid(linspace(0, 1, 100), linspace(0, 1, 100)).second;
45
46 matplot::vector_2d Z = np::convert_to_matplot(gradf[1]);
47 std::cout << "X.size() = " << X.size() << std::endl;
48 std::cout << "Y.size() = " << Y.size() << std::endl;
49 std::cout << "Z.size() = " << Z.size() << std::endl;
50 for (size_t i = 0; i < X.size(); i++)
51 {
52 for (size_t j = 0; j < X[i].size(); j++)
53 {
54 std::cout << "X[" << i << "][" << j << "] = " << X[i][j] << " | XP[" << i << "][" << j << "] = " << Xp[i][j] << " | YP[" << i << "][" << j << "] = " << Yp[i][j] << std::endl;
55 }
56 }
57 contourf(Xp, Yp, Z, 10);
58 show();
59}
60
61int main()
62{
63 // test_simple_plot();
64 test_conversion();
65 return 0;
66}
matplot::vector_2d convert_to_matplot(const boost::multi_array< double, 2 > &arr)
Convert a 2D boost::multi_array to a matplot::vector_2d.
::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
::value constexpr std::vector< boost::multi_array< T, ND > > gradient(boost::multi_array< T, ND > inArray, std::initializer_list< T > args)
Definition: np.hpp:90
::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