openPSTD  2.0
Open source simulation for sound propagation in urban environments
kernel_functions.h
1 // This file is part of openPSTD. //
3 // //
4 // openPSTD is free software: you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation, either version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // openPSTD is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with openPSTD. If not, see <http://www.gnu.org/licenses/>. //
16 // //
18 
20 //
21 // Date:
22 // 3-9-2015
23 //
24 // Authors:
25 // Omar Richardson
26 // Louis van Harten
27 //
28 // Purpose:
29 // Contains procedural functions that don't fit with any one class.
30 //
32 
33 
34 #ifndef OPENPSTD_KERNEL_FUNCTIONS_H
35 #define OPENPSTD_KERNEL_FUNCTIONS_H
36 
37 #include <Eigen/Dense>
38 #include <complex>
39 #include <fftw3.h>
40 #include <iostream>
41 #include <vector>
42 #include <map>
43 #include <math.h>
44 #include <algorithm>
45 #include "../KernelInterface.h"
46 #include "Geometry.h"
47 
48 namespace OpenPSTD {
49  namespace Kernel {
50 
54  template<typename T>
55  std::vector<T> arange(T start, T stop, T step = 1) {
56  std::vector<T> list;
57  for (T i = start; i < stop; i += step)
58  list.push_back(i);
59  return list;
60  }
61 
65  const float EPSILON = 1E-5; // Depends on float/double
66 
67 
71  enum class CalcDirection {
72  X = 0, Y = 1
73  };
74 
78  enum class CalculationType {
79  PRESSURE, VELOCITY
80  };
81 
85  enum class Direction {
86  LEFT, RIGHT, TOP, BOTTOM
87  };
88 
92  const std::vector<Direction> all_directions = {Direction::LEFT, Direction::RIGHT, Direction::TOP,
93  Direction::BOTTOM};
94 
95  const std::vector<CalcDirection> all_calc_directions = {CalcDirection::X, CalcDirection::Y};
96  const std::vector<CalculationType> all_calculation_types = {CalculationType::PRESSURE,
97  CalculationType::VELOCITY};
98 
102  const std::vector<float> rk_coefficients = {8.91421261e-4f, 7555704391e-3f, 4.0919732041e-2f,
103  1.65919771368e-1f, 5e-1, 1.f}; // Temporary until bugfix
104 
105 
111  Direction get_opposite(Direction direction);
112 
119 
126 
131  struct RhoArray {
132  Eigen::ArrayXXf pressure;
133  Eigen::ArrayXXf velocity;
134  };
135 
156  Eigen::ArrayXXf spatderp3(Eigen::ArrayXXf p1, Eigen::ArrayXXf p2,
157  Eigen::ArrayXXf p3, Eigen::ArrayXcf derfact,
158  RhoArray rho_array, Eigen::ArrayXf window, int wlen,
159  CalculationType ct, CalcDirection direct);
160 
165  Eigen::ArrayXXf spatderp3(Eigen::ArrayXXf p1, Eigen::ArrayXXf p2,
166  Eigen::ArrayXXf p3, Eigen::ArrayXcf derfact,
167  RhoArray rho_array, Eigen::ArrayXf window, int wlen,
168  CalculationType ct, CalcDirection direct,
169  fftwf_plan plan, fftwf_plan plan_inv);
170 
179  RhoArray get_rho_array(const float rho1, const float rho_self, const float rho2);
180 
188  float get_grid_spacing(PSTDSettings cnf);
189 
196  Eigen::ArrayXf get_window_coefficients(int window_size, int patch_error);
197 
203  int next_2_power(float n);
204 
213  bool is_approx(float a, float b);
214 
215  void debug(std::string msg);
216 
217  }
218 }
219 #endif //OPENPSTD_KERNEL_FUNCTIONS_H
This is the general namespace of the OpenPSTD application.
Definition: Boundary.cpp:33
A collection of parameters and settings for the simulation.
Definition: KernelInterface.h:44
std::vector< T > arange(T start, T stop, T step=1)
Helper function equivalent to numpy.arange()
Definition: kernel_functions.h:55
CalcDirection
Helper enums - used to distinguish horizontal boundaries from vertical boundaries.
Definition: kernel_functions.h:71
Eigen::ArrayXf get_window_coefficients(int window_size, int patch_error)
Gives a two-sided array of window coefficients for a given window size and patch error.
Definition: kernel_functions.cpp:280
Struct with arrays containing the reflection and transmission coefficients of the pressure and the ve...
Definition: kernel_functions.h:131
const float EPSILON
Small positive number to facilitate division and other numerical computations.
Definition: kernel_functions.h:65
CalcDirection get_orthogonal(CalcDirection direction)
Get the direction orthogonal to the provided calculation direction.
Definition: kernel_functions.cpp:95
RhoArray get_rho_array(const float rho1, const float rho_self, const float rho2)
Computes and return reflection and transmission matrices for pressure and velocity based on density o...
Definition: kernel_functions.cpp:34
int next_2_power(float n)
Computes the smallest power of 2 larger or equal to n if n positive, and 1 otherwise.
Definition: kernel_functions.cpp:58
const std::vector< Direction > all_directions
Convenience vectors.
Definition: kernel_functions.h:92
bool is_approx(float a, float b)
Perform a numerical check whether a approximately equals b.
Definition: kernel_functions.cpp:294
CalcDirection direction_to_calc_direction(Direction direction)
Obtain corresponding Calculation Direction Axis from direction.
Definition: kernel_functions.cpp:104
Direction get_opposite(Direction direction)
Return the opposite direction of the provided direction.
Definition: kernel_functions.cpp:82
Direction
Enum representing directions among domains.
Definition: kernel_functions.h:85
const std::vector< float > rk_coefficients
Coefficients for a six stage RK time integration.
Definition: kernel_functions.h:102
float get_grid_spacing(PSTDSettings cnf)
Computes the largest grid spacing possible based on the speed of the medium and the maximum frequency...
Definition: kernel_functions.cpp:66
CalculationType
Helper enums - used to distinguish pressure computations from velocity computations.
Definition: kernel_functions.h:78
Eigen::ArrayXXf spatderp3(Eigen::ArrayXXf p1, Eigen::ArrayXXf p2, Eigen::ArrayXXf p3, Eigen::ArrayXcf derfact, RhoArray rho_array, Eigen::ArrayXf window, int wlen, CalculationType ct, CalcDirection direct, fftwf_plan plan, fftwf_plan plan_inv)
Version of spatderp3 that takes cached plans as input.
Definition: kernel_functions.cpp:115