WaveSimPP 1.0
A C library for solving the wave equation and reconstructing the wave field
All Classes Namespaces Functions Modules Pages
wavePlotter.hpp
1#ifndef WAVESOLVER_H_
2#define WAVESOLVER_H_
3#include <boost/multi_array.hpp>
4#include <boost/array.hpp>
5#include <fstream>
6#include <iostream>
7#include "CustomLibraries/np.hpp"
8#include "CustomLibraries/np_to_matplot.hpp"
9
16namespace wavePlotter
17{
21 class Plotter
22 {
23 public:
25 Plotter(const boost::multi_array<double, 3> &u, const matplot::vector_2d &Xp, const matplot::vector_2d &Zp, int num_levels, int nt)
26 {
27 this->u.resize(boost::extents[u.shape()[0]][u.shape()[1]][u.shape()[2]]);
28 this->u = u;
29 this->Xp = Xp;
30 this->Zp = Zp;
31 this->num_levels = num_levels;
32 this->nt = nt;
33 double min_u = np::min(u);
34 double max_u = np::max(u);
35 std::cout << "min_u = " << min_u << " max_u = " << max_u << "\n";
36 this->levels = matplot::linspace(min_u, max_u, num_levels);
37 }
39 void renderFrame(int index)
40 {
41 matplot::vector_2d Up = np::convert_to_matplot(this->u[index]);
42 matplot::contourf(this->Xp, this->Zp, Up, this->levels);
43 matplot::save(save_directory + "/contourf_" + format_num(index) + ".png");
44 }
45
47 void renderAllFrames(int begin_frame_index, int end_frame_index)
48 {
49 for (int i = begin_frame_index; i < end_frame_index; i++)
50 {
51 renderFrame(i);
52 }
53 }
54
56 void animate(std::string output_file_name, int begin_frame_index, int end_frame_index, int frame_rate)
57 {
58 renderAllFrames(begin_frame_index, end_frame_index);
59 std::string ffmpeg_render_command = "ffmpeg -framerate " + std::to_string(frame_rate) + " -pattern_type glob -i '" + save_directory + "/*.png' -c:v libx264 -pix_fmt yuv420p " + output_file_name;
60 std::system(ffmpeg_render_command.c_str());
61 }
63 void exportFrame(int index)
64 {
65 matplot::vector_2d Up = np::convert_to_matplot(this->u[index]);
66 std::ofstream outfile;
67 outfile.open(save_directory + "/frame_" + format_num(index) + ".csv");
68 for (std::size_t i = 0; i < Up.size(); i++)
69 {
70 for (std::size_t j = 0; j < Up[i].size(); j++)
71 {
72 outfile << Up[i][j];
73 if (j != Up[i].size() - 1)
74 outfile << ",";
75 }
76 outfile << "\n";
77 }
78 outfile.close();
79 }
80
82 void exportAllFrames(int begin_frame_index, int end_frame_index)
83 {
84 for (int i = begin_frame_index; i < end_frame_index; i++)
85 {
86 exportFrame(i);
87 }
88 }
89
91 void setSaveDirectory(std::string save_directory)
92 {
93 this->save_directory = save_directory;
94 }
95
96 private:
97 std::string format_num(int num, int length = 8)
98 {
99 std::string str_num = std::to_string(num);
100
101 int str_length = str_num.length();
102 for (int i = 0; i < length - str_length; i++)
103 str_num = "0" + str_num;
104 return str_num;
105 }
106
107 boost::multi_array<double, 3> u;
108 matplot::vector_2d Xp;
109 matplot::vector_2d Zp;
110 int num_levels;
111 int nt;
112 std::vector<double> levels;
113 std::string save_directory = "output";
114 };
115
116}
117#endif
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.
void setSaveDirectory(std::string save_directory)
Set the save directory for the rendered frames.
Definition: wavePlotter.hpp:91
void animate(std::string output_file_name, int begin_frame_index, int end_frame_index, int frame_rate)
Renders a complete video animation of the wave field.
Definition: wavePlotter.hpp:56
void renderAllFrames(int begin_frame_index, int end_frame_index)
Renders all frames of the wave field to form an animation to be saved on disk.
Definition: wavePlotter.hpp:47
void exportAllFrames(int begin_frame_index, int end_frame_index)
Export all frames of the wave field to a .csv format for external use.
Definition: wavePlotter.hpp:82
Plotter(const boost::multi_array< double, 3 > &u, const matplot::vector_2d &Xp, const matplot::vector_2d &Zp, int num_levels, int nt)
Constructor.
Definition: wavePlotter.hpp:25
void exportFrame(int index)
Export a frame of the wave field to a .csv format for external use.
Definition: wavePlotter.hpp:63
void renderFrame(int index)
Renders a frame of the wave field to a image on disk.
Definition: wavePlotter.hpp:39
::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
Custom plotter class.
Definition: wavePlotter.hpp:17