-
Notifications
You must be signed in to change notification settings - Fork 5
/
GenerateTimeSeries.jl
executable file
·281 lines (237 loc) · 9.89 KB
/
GenerateTimeSeries.jl
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
##############################################################################################
module GenerateTimeSeries
##############################################################################################
# imports and exports
using Random, Distributions
using LinearAlgebra
using JLD2, HDF5
using ..DataAssimilationBenchmarks, ..DeSolvers, ..L96, ..IEEE39bus
export L96_time_series, IEEE39bus_time_series
##############################################################################################
"""
L96_time_series((seed::Int64, h::Float64, state_dim::Int64, tanl::Float64, nanl::Int64,
spin::Int64, diffusion::Float64, F::Float64)::NamedTuple)
Simulate a "free run" time series of the [Lorenz-96 model](@ref) model
for generating an observation process and truth twin for data assimilation twin experiments.
Output from the experiment is saved in a dictionary of the form,
Dict{String, Any}(
"seed" => seed,
"h" => h,
"diffusion" => diffusion,
"dx_params" => dx_params,
"tanl" => tanl,
"nanl" => nanl,
"spin" => spin,
"state_dim" => state_dim,
"obs" => obs,
"model" => "L96"
)
Experiment output is written to a directory defined by
path = pkgdir(DataAssimilationBenchmarks) * "/src/data/time_series/"
where the file name is written dynamically according to the selected parameters as follows:
"L96_time_series_seed_" * lpad(seed, 4, "0") *
"_dim_" * lpad(state_dim, 2, "0") *
"_diff_" * rpad(diffusion, 5, "0") *
"_F_" * lpad(F, 4, "0") *
"_tanl_" * rpad(tanl, 4, "0") *
"_nanl_" * lpad(nanl, 5, "0") *
"_spin_" * lpad(spin, 4, "0") *
"_h_" * rpad(h, 5, "0") *
".jld2"
"""
function L96_time_series((seed, h, state_dim, tanl, nanl, spin, diffusion, F)::NamedTuple{
(:seed,:h,:state_dim,:tanl,:nanl,:spin,:diffusion,:F),
<:Tuple{Int64,Float64,Int64,Float64,Int64,Int64,
Float64,Float64}})
# time the experiment
t1 = time()
# define the model
dx_dt = L96.dx_dt
dx_params = Dict{String, Array{Float64}}("F" => [8.0])
# define the integration scheme
if diffusion == 0.0
# generate the observations with the Runge-Kutta scheme
step_model! = DeSolvers.rk4_step!
else
# generate the observations with the strong Taylor scheme
step_model! = L96.l96s_tay2_step!
# parameters for the order 2.0 strong Taylor scheme
p = 1
α, ρ = comput_α_ρ(p)
end
# set the number of discrete integrations steps between each observation time
f_steps = convert(Int64, tanl/h)
# set storage for the ensemble timeseries
obs = Array{Float64}(undef, state_dim, nanl)
# define the integration parameters in the kwargs dict
kwargs = Dict{String, Any}(
"h" => h,
"diffusion" => diffusion,
"dx_params" => dx_params,
"dx_dt" => dx_dt,
)
if diffusion != 0.0
kwargs["p"] = p
kwargs["α"] = α
kwargs["ρ"] = ρ
end
# seed the random generator
Random.seed!(seed)
x = rand(Normal(), state_dim)
# spin the model onto the attractor
for j in 1:spin
for k in 1:f_steps
step_model!(x, 0.0, kwargs)
end
end
# save the model state at timesteps of tanl
for j in 1:nanl
for k in 1:f_steps
step_model!(x, 0.0, kwargs)
end
obs[:, j] = x
end
data = Dict{String, Any}(
"seed" => seed,
"h" => h,
"diffusion" => diffusion,
"dx_params" => dx_params,
"tanl" => tanl,
"nanl" => nanl,
"spin" => spin,
"state_dim" => state_dim,
"obs" => obs,
"model" => "L96"
)
path = pkgdir(DataAssimilationBenchmarks) * "/src/data/time_series/"
name = "L96_time_series_seed_" * lpad(seed, 4, "0") *
"_dim_" * lpad(state_dim, 2, "0") *
"_diff_" * rpad(diffusion, 5, "0") *
"_F_" * lpad(F, 4, "0") *
"_tanl_" * rpad(tanl, 4, "0") *
"_nanl_" * lpad(nanl, 5, "0") *
"_spin_" * lpad(spin, 4, "0") *
"_h_" * rpad(h, 5, "0") *
".jld2"
save(path * name, data)
print("Runtime " * string(round((time() - t1) / 60.0, digits=4)) * " minutes\n")
end
##############################################################################################
"""
IEEE39bus_time_series((seed::Int64, h:Float64, tanl::Float64, nanl::Int64, spin::Int64,
diffusion::Float64)::NamedTuple)
Simulate a "free run" time series of the [IEEE39bus](@ref) for
generating an observation process and truth twin for data assimilation twin experiments.
Output from the experiment is saved in a dictionary of the form,
Dict{String, Any}(
"seed" => seed,
"h" => h,
"diffusion" => diffusion,
"diff_mat" => diff_mat,
"dx_params" => dx_params,
"tanl" => tanl,
"nanl" => nanl,
"spin" => spin,
"obs" => obs,
"model" => "IEEE39bus"
)
Experiment output is written to a directory defined by
path = pkgdir(DataAssimilationBenchmarks) * "/src/data/time_series/"
where the file name is written dynamically according to the selected parameters as follows:
"IEEE39bus_time_series_seed_" * lpad(seed, 4, "0") *
"_diff_" * rpad(diffusion, 5, "0") *
"_tanl_" * rpad(tanl, 4, "0") *
"_nanl_" * lpad(nanl, 5, "0") *
"_spin_" * lpad(spin, 4, "0") *
"_h_" * rpad(h, 5, "0") *
".jld2"
"""
function IEEE39bus_time_series((seed, h, tanl, nanl, spin, diffusion)::NamedTuple{
(:seed,:h,:tanl,:nanl,:spin,:diffusion),
<:Tuple{Int64,Float64,Float64,Int64,Int64,Float64}})
# time the experiment
t1 = time()
# set random seed
Random.seed!(seed)
# define the model
dx_dt = IEEE39bus.dx_dt
state_dim = 20
# define the model parameters
input_data = pkgdir(DataAssimilationBenchmarks) *
"/src/models/IEEE39bus_inputs/NE_EffectiveNetworkParams.jld2"
tmp = load(input_data)
dx_params = Dict{String, Array{Float64}}(
"A" => tmp["A"],
"D" => tmp["D"],
"H" => tmp["H"],
"K" => tmp["K"],
"γ" => tmp["γ"],
"ω" => tmp["ω"]
)
# define the integration scheme
step_model! = DeSolvers.rk4_step!
h = 0.01
# define the diffusion coefficient structure matrix
# notice that the perturbations are only applied to the frequencies
# based on the change of variables derivation
# likewise, the diffusion parameter is applied separately as an amplitude
# in the Runge-Kutta scheme
diff_mat = zeros(20,20)
diff_mat[LinearAlgebra.diagind(diff_mat)[11:end]] = tmp["ω"][1] ./ (2.0 * tmp["H"])
# set the number of discrete integrations steps between each observation time
f_steps = convert(Int64, tanl/h)
# set storage for the ensemble timeseries
obs = Array{Float64}(undef, state_dim, nanl)
# define the integration parameters in the kwargs dict
kwargs = Dict{String, Any}(
"h" => h,
"diffusion" => diffusion,
"dx_params" => dx_params,
"dx_dt" => dx_dt,
"diff_mat" => diff_mat
)
# load the steady state, generated by long simulation without noise
x = tmp["synchronous_state"]
# spin the model onto the attractor
for j in 1:spin
for k in 1:f_steps
step_model!(x, 0.0, kwargs)
# set phase angles mod 2pi
x[1:10] .= rem2pi.(x[1:10], RoundNearest)
end
end
# save the model state at timesteps of tanl
for j in 1:nanl
for k in 1:f_steps
step_model!(x, 0.0, kwargs)
# set phase angles mod 2pi
x[1:10] .= rem2pi.(x[1:10], RoundNearest)
end
obs[:, j] = x
end
data = Dict{String, Any}(
"seed" => seed,
"h" => h,
"diffusion" => diffusion,
"diff_mat" => diff_mat,
"dx_params" => dx_params,
"tanl" => tanl,
"nanl" => nanl,
"spin" => spin,
"obs" => obs,
"model" => "IEEE39bus"
)
path = pkgdir(DataAssimilationBenchmarks) * "/src/data/time_series/"
name = "IEEE39bus_time_series_seed_" * lpad(seed, 4, "0") *
"_diff_" * rpad(diffusion, 5, "0") *
"_tanl_" * rpad(tanl, 4, "0") *
"_nanl_" * lpad(nanl, 5, "0") *
"_spin_" * lpad(spin, 4, "0") *
"_h_" * rpad(h, 5, "0") *
".jld2"
save(path * name, data)
print("Runtime " * string(round((time() - t1) / 60.0, digits=4)) * " minutes\n")
end
##############################################################################################
# end module
end