-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperators_DFT.h
190 lines (156 loc) · 4 KB
/
Operators_DFT.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
#define _USE_MATH_DEFINES
#include <math.h>
#include <typeinfo>
#include "Layer.h"
#include "Matrix2D.h"
class VelocitySolver_Unbounded
{
protected:
int m_nSize[2], m_nBigSize[2];
bool m_bReady;
Matrix2D<Real[2]> * m_matFourierGF;
Matrix2D<Real[2]> * m_matFourierPsi;
Matrix2D<Real> * m_matPsi;
static inline Real _physGF(int x_, int y_, Real h)
{
if (!(x_==0 && y_==0))
{
const Real x = (x_+0.0)*h;
const Real y = (y_+0.0)*h;
const Real r = sqrt(x*x+y*y);
return log(r)/(M_PI*2);
}
else
{
const Real R = h/sqrt(M_PI);
return (2*log(R)-1)/(4*M_PI);
}
}
virtual void _init() =0;
virtual void _solve()=0;
template <int sizeX, int sizeY>
void _copyRHS(const Layer<sizeX, sizeY, 1>& vorticity, Matrix2D<Real>& RHS )
{
RHS = 0.0;
for(int dy=0; dy<sizeY; dy++)
for(int dx=0; dx<sizeX; dx++)
RHS(dx,dy) = -vorticity.read(dx,dy);
}
template <int sizeX, int sizeY>
void _copyResults(Layer<sizeX,sizeY,1>& results )
{
Matrix2D<Real>& PSI = *m_matPsi;
for(int dy=0; dy<sizeY; dy++)
for(int dx=0; dx<sizeX; dx++)
results(dx,dy,0) = PSI(dx,dy);
}
template <int sizeX, int sizeY>
void _computeVelocityFromStreamFunction(Layer<sizeX, sizeY, 2>& velocity)
{
const double h= velocity.getH0();
const double factor = 0.5/h;
Matrix2D<Real>& PSI = *m_matPsi;
for(int dy=1; dy<sizeY-1; dy++)
for(int dx=1; dx<sizeX-1; dx++)
{
const int dxp = (dx + 1);
const int dxm = (dx - 1);
const int dyp = (dy + 1);
const int dym = (dy - 1);
velocity(dx,dy,0) = (PSI(dx,dyp) - PSI(dx,dym))*factor;
velocity(dx,dy,1) = -(PSI(dxp,dy) - PSI(dxm,dy))*factor;
}
}
template <int sizeX, int sizeY>
void _computeVelocityFromVelocityPotential(Layer<sizeX, sizeY, 2>& velocity)
{
const double h= velocity.getH0();
const double factor = 0.5/h;
Matrix2D<Real>& PSI = *m_matPsi;
for(int dy=0; dy<sizeY; dy++)
for(int dx=0; dx<sizeX; dx++)
{
const int dxp = (dx + 1);
const int dxm = (dx - 1);
const int dyp = (dy + 1);
const int dym = (dy - 1);
velocity(dx,dy,0) += (PSI(dxp,dy) - PSI(dxm,dy))*factor;
velocity(dx,dy,1) += (PSI(dx,dyp) - PSI(dx,dym))*factor;
}
}
public:
VelocitySolver_Unbounded(const int nX, const int nY):
m_matFourierGF(NULL), m_matPsi(NULL), m_matFourierPsi(NULL), m_bReady(false)
{
if(!(nX%2))
{
m_nSize[0] = nX;
m_nSize[1] = nY;
m_nBigSize[0] = nX*2;
m_nBigSize[1] = nY*2;
}
else
{
m_nSize[0] = nX;
m_nSize[1] = nY;
m_nBigSize[0] = nX*2-2;
m_nBigSize[1] = nY*2-2;
}
}
virtual ~VelocitySolver_Unbounded(){}
template <int sizeX, int sizeY>
void operator() (const Layer<sizeX, sizeY, 1>& input,
Layer<sizeX, sizeY, 1>& output)
// lapace(output) = -input
{
if (!m_bReady)
{
_init();
m_bReady = true;
}
_copyRHS(input, *m_matPsi);
_solve();
_copyResults(output);
}
template <int sizeX, int sizeY>
void operator() (const Layer<sizeX, sizeY, 1>& vorticity, Layer<sizeX, sizeY, 2>& velocity)
{
if (!m_bReady)
{
_init();
m_bReady = true;
}
_copyRHS(vorticity, *m_matPsi);
_solve();
_computeVelocityFromStreamFunction(velocity);
}
template <int sizeX, int sizeY>
void operator() (const Layer<sizeX,sizeY,1>& vorticity, const Layer<sizeX,sizeY,1>& minusDivergence, Layer<sizeX,sizeY,1>& streamFunction,Layer<sizeX,sizeY,1>& velocityPotential)
{
if (!m_bReady)
{
_init();
m_bReady = true;
}
_copyRHS(vorticity, *m_matPsi);
_solve();
_copyResults(streamFunction);
//_computeVelocityFromStreamFunction(velocity);
_copyRHS(minusDivergence, *m_matPsi);
_solve();
_copyResults(velocityPotential);
//_computeVelocityFromVelocityPotential(velocity);
}
};
class VelocitySolver_Unbounded_FFTW: public VelocitySolver_Unbounded
{
protected:
void _init();
void _solve();
public:
VelocitySolver_Unbounded_FFTW(const int nX, const int nY):
VelocitySolver_Unbounded(nX, nY) {}
~VelocitySolver_Unbounded_FFTW();
};
#include "Operators_DFT_FFTW.h"