-
Notifications
You must be signed in to change notification settings - Fork 2
/
StateVector.cpp
266 lines (206 loc) · 6.28 KB
/
StateVector.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "StateVector.h"
stratifloat StateVector::FullEvolve(stratifloat T, StateVector& result, bool snapshot, bool screenshot, bool calcmixing) const
{
CopyToSolver();
solver.SetBackground(InitialU);
solver.FilterAll();
solver.PopulateNodalVariables();
solver.RemoveDivergence(0.0f);
stratifloat t = 0.0f;
int step = 0;
bool done = false;
static int runnum = 0;
runnum++;
solver.PrepareRun(std::string("images-")+std::to_string(runnum)+"/", screenshot);
MakeCleanDir("snapshots");
const int stepinterval = 100;
stratifloat mixing = 0;
while (t+0.0001 < T)
{
if(step%stepinterval==0)
{
stratifloat cfl = solver.CFL();
std::cout << step << " " << t << " " << (solver.KE() + solver.PE()) << std::endl;
// finish exactly for last step
stratifloat remaining = T-t;
int remainingSteps = (remaining / solver.deltaT)+1;
if (remainingSteps < stepinterval)
{
// make timestep slightly shorter
solver.deltaT = remaining/remainingSteps;
solver.UpdateForTimestep();
}
if (screenshot)
{
solver.PlotAll(std::to_string(step)+"-"+std::to_string(t)+".png", true);
}
if (snapshot)
{
solver.SaveFlow("snapshots/"+std::to_string(step)+"-"+std::to_string(t)+".fields");
}
}
solver.TimeStep();
if (calcmixing)
{
mixing += solver.JoverK() * solver.deltaT;
}
t += solver.deltaT;
step++;
if (t+0.0001>=T)
{
if (screenshot)
{
solver.PlotAll(std::to_string(step)+"-"+std::to_string(t)+".png", true);
}
if (snapshot)
{
solver.SaveFlow("snapshots/"+std::to_string(step)+"-"+std::to_string(t)+".fields");
}
}
}
CopyFromSolver(result);
return mixing;
}
void StateVector::FixedEvolve(stratifloat deltaT, int steps, std::vector<StateVector>& result) const
{
result.resize(steps);
CopyToSolver();
solver.FilterAll();
solver.PopulateNodalVariables();
solver.RemoveDivergence(0.0f);
solver.deltaT = deltaT;
solver.UpdateForTimestep();
solver.PrepareRun(std::string("blah"), false);
for (int step=0; step<steps; step++)
{
CopyFromSolver(result[step]);
solver.TimeStep();
}
}
void StateVector::LinearEvolve(stratifloat T, const StateVector& about, StateVector& result) const
{
CopyToSolver();
solver.SetBackground(InitialU);
solver.SetBackground(about.u1, about.u2, about.u3, about.b);
solver.FilterAll();
solver.PopulateNodalVariables();
solver.RemoveDivergence(0.0f);
stratifloat t = 0.0f;
int step = 0;
bool done = false;
static int runnum = 0;
runnum++;
solver.PrepareRunLinear(std::string("images-linear-")+std::to_string(runnum)+"/", false);
solver.deltaT = 0.01;
solver.UpdateForTimestep();
const int stepinterval = 100;
while (t+0.0001 < T)
{
if(step%stepinterval==0)
{
stratifloat cfl = solver.CFLlinear();
std::cout << step << " " << t << std::endl;
// finish exactly for last step
stratifloat remaining = T-t;
int remainingSteps = (remaining / solver.deltaT)+1;
if (remainingSteps < stepinterval)
{
// make timestep slightly shorter
solver.deltaT = remaining/remainingSteps;
solver.UpdateForTimestep();
}
}
solver.TimeStepLinear();
t += solver.deltaT;
step++;
}
CopyFromSolver(result);
}
void StateVector::AdjointEvolve(stratifloat deltaT, int steps, const std::vector<StateVector>& intermediate, StateVector& result) const
{
CopyToSolver();
solver.SetBackground(InitialU);
solver.FilterAll();
solver.PopulateNodalVariables();
solver.RemoveDivergence(0.0f);
static int runnum = 0;
runnum++;
solver.PrepareRunAdjoint(std::string("images-adjoint-")+std::to_string(runnum)+"/");
solver.deltaT = deltaT;
solver.UpdateForTimestep();
for (int step=0; step<steps; step++)
{
solver.TimeStepAdjoint(intermediate[steps-1-step].u1,
intermediate[steps-1-step].u2,
intermediate[steps-1-step].u3,
intermediate[steps-1-step].b,
intermediate[steps-step].u1,
intermediate[steps-step].u2,
intermediate[steps-step].u3,
intermediate[steps-step].b);
}
CopyFromSolver(result);
}
void StateVector::Rescale(stratifloat energy)
{
CopyToSolver();
solver.FilterAll();
solver.PopulateNodalVariables();
solver.RemoveDivergence(0.0f);
CopyFromSolver();
stratifloat scale;
// energies are entirely quadratic
// which makes this easy
stratifloat energyBefore = Energy();
if (energyBefore!=0.0f)
{
scale = sqrt(energy/energyBefore);
}
else
{
scale = 0.0f;
}
u1 *= scale;
if (gridParams.ThirdDimension())
{
u2 *= scale;
}
u3 *= scale;
b *= scale;
CopyToSolver();
solver.FilterAll();
solver.PopulateNodalVariables();
solver.RemoveDivergence(0.0f);
CopyFromSolver();
}
void StateVector::ExciteLowWavenumbers(stratifloat energy)
{
u1.ExciteLowWavenumbers(flowParams.L3);
u2.ExciteLowWavenumbers(flowParams.L3);
u3.ExciteLowWavenumbers(flowParams.L3);
b.ExciteLowWavenumbers(flowParams.L3);
Rescale(energy);
}
IMEXRK StateVector::solver;
StateVector operator+(const StateVector& lhs, const StateVector& rhs)
{
StateVector ret = lhs;
ret += rhs;
return ret;
}
StateVector operator-(const StateVector& lhs, const StateVector& rhs)
{
StateVector ret = lhs;
ret -= rhs;
return ret;
}
StateVector operator*(stratifloat scalar, const StateVector& vector)
{
StateVector ret = vector;
ret *= scalar;
return ret;
}
StateVector operator*(const StateVector& vector, stratifloat scalar)
{
return scalar*vector;
}