-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumericalMethod.hh
159 lines (150 loc) · 6.08 KB
/
NumericalMethod.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef NUMERICAL_METHOD
#define NUMERICAL_METHOD
#include <Eigen/Dense>
#include <cmath>
#include "Include.hh"
using namespace Eigen;
template <int order>
class FiniteDifference{
public:
template<typename Derived>
ArrayXXf x(const ArrayBase<Derived> &var, float dx) const{
ArrayXXf result;
int nrows = var.rows(), ncols = var.cols();
if (order == 2){
result.resize(nrows, ncols);
result.row(0) = (var.row(1) - var.row(0)) / dx;
for (size_t i = 1; i < nrows - 1; i++)
result.row(i) = (var.row(i + 1) - var.row(i - 1)) / (2. * dx);
result.row(nrows - 1) = (var.row(nrows - 1) - var.row(nrows - 2)) / dx;
} else if (order == 1){
result.resize(nrows - 1, ncols);
for (size_t i = 0; i < nrows - 1; i++)
result.row(i) = (var.row(i + 1) - var.row(i)) / dx;
}
return result;
}
template<typename Derived>
ArrayXXf y(const ArrayBase<Derived> &var, float dy) const{
ArrayXXf result;
int nrows = var.rows(), ncols = var.cols();
if (order == 2){
result.resize(nrows, ncols);
result.col(0) = (var.col(1) - var.col(0)) / dy;
for (size_t i = 1; i < ncols - 1; i++)
result.col(i) = (var.col(i + 1) - var.col(i - 1)) / (2. * dy);
result.col(ncols - 1) = (var.col(ncols - 1) - var.col(ncols - 2)) / dy;
} else if (order == 1){
result.resize(nrows, ncols - 1);
for (size_t i = 0; i < ncols - 1; i++)
result.col(i) = (var.col(i + 1) - var.col(i)) / dy;
}
return result;
}
};
template <int order>
class HyperDifference{
// calculate higher order difference
protected:
static const float coeff[4][7];
public:
template<typename Derived>
ArrayXXf x(const ArrayBase<Derived> &var, float dx) const{
int r, nrows = var.rows(), ncols = var.cols();
ArrayXXf result = ArrayXXf::Zero(nrows, ncols);
for (size_t i = 0; i < nrows; i++){
r = MIN3(i, nrows - i - 1, order / 2);
for (int j = -r; j < r + 1; j++)
result.row(i) += coeff[r][j + 3] * var.row(i + j) * std::pow(dx, -order);
}
return result;
}
template<typename Derived>
ArrayXXf y(const ArrayBase<Derived> &var, float dy) const{
int r, nrows = var.rows(), ncols = var.cols();
ArrayXXf result = ArrayXXf::Zero(nrows, ncols);
for (size_t i = 0; i < ncols; i++){
r = MIN3(i, ncols - i - 1, order / 2);
for (int j = -r; j < r + 1; j++)
result.col(i) += coeff[r][j + 3] * var.col(i + j) * std::pow(dy, -order);
}
return result;
}
};
template <int order>
const float HyperDifference<order>::coeff[4][7] = {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, -2, 1, 0, 0},
{0, -1, 4, -6, 4, -1, 0},
{1, -6, 15, -20, 15, -6, 1}
};
template <int order>
class FiniteInterpolation{
protected:
ArrayXXf wsignx, wsigny;
static const float coeff[6][6];
public:
template<typename Derived>
ArrayXXf x(const ArrayBase<Derived> &var) const{
int r, nrows = var.rows(), ncols = var.cols();
ArrayXXf result = ArrayXXf::Zero(nrows - 1, ncols);
for (size_t i = 0; i < nrows - 1; i++){
r = MIN3(i + 1, nrows - i - 1, order / 2);
for (int j = -r + 1; j < r + 1; j++)
result.row(i) += coeff[2 * r - 1][j + 2] * var.row(i + j);
}
return result;
}
template<typename Derived>
ArrayXXf y(const ArrayBase<Derived> &var) const{
int r, nrows = var.rows(), ncols = var.cols();
ArrayXXf result = ArrayXXf::Zero(nrows, ncols - 1);
for (size_t i = 0; i < ncols - 1; i++){
r = MIN3(i + 1, ncols - i - 1, order / 2);
for (int j = -r + 1; j < r + 1; j++)
result.col(i) += coeff[2 * r - 1][j + 2] * var.col(i + j);
}
return result;
}
template<typename Deriveda, typename Derivedb>
ArrayXXf x1(const ArrayBase<Deriveda> &wind, const ArrayBase<Derivedb> &var) const{
// upwind transport
int r, nrows = nrows, ncols = ncols;
ArrayXXf result = ArrayXXf::Zero(nrows - 1, ncols);
wsignx = (wind.block(1,0,nrows - 1,ncols) > 0).select(
ArrayXXf::Zero(nrows - 1, ncols) + 1.,
ArrayXXf::Zero(nrows - 1, ncols) - 1.);
for (size_t i = 0; i < nrows - 1; i++){
r = MIN3(i + 1, nrows - i - 1, order / 2);
for (int j = -r + 1; j < r + 1; j++)
result.row(i) += coeff[2 * r - 1][j + 2] * var.row(i + j) +
wsignx.row(i) * coeff[2 * r - 2][j + 2] * var.row(i + j);
}
return result;
}
template<typename Deriveda, typename Derivedb>
ArrayXXf y1(const ArrayBase<Deriveda> &wind, const ArrayBase<Derivedb> &var) const{
int r, nrows = nrows, ncols = ncols;
ArrayXXf result = ArrayXXf::Zero(nrows, ncols - 1);
wsigny = (wind.block(0,1,nrows,ncols - 1) > 0).select(
ArrayXXf::Zero(nrows, ncols - 1) + 1.,
ArrayXXf::Zero(nrows, ncols - 1) - 1.);
for (size_t i = 0; i < ncols - 1; i++){
r = MIN3(i + 1, ncols - i - 1, order / 2);
for (int j = -r + 1; j < r + 1; j++)
result.col(i) += coeff[2 * r - 1][j + 2] * var.col(i + j) +
wsigny.col(i) * coeff[2 * r - 2][j + 2] * var.col(i + j);
}
return result;
}
};
template <int order>
const float FiniteInterpolation<order>::coeff[6][6] = {
{ 0 , 0 , 0.5 , -0.5 , 0 , 0 },
{ 0 , 0 , 0.5 , 0.5 , 0 , 0 },
{ 0 , -1./12., 1./4. , -1./4. , 1./12. , 0 },
{ 0 , -1./12., 7./12. , 7./12 , -1./12. , 0 },
{1./60, -1./12., 1./6. , -1./6. , 1./12. , -1./60.},
{1./60, -2./15., 37./60., 37./60., -2./15. , 1./60.}
};
#endif