Skip to content

Commit 1170f14

Browse files
committed
Implement 'gather_pairs', 'random_init'; add tests
1 parent c09415e commit 1170f14

File tree

4 files changed

+77
-10
lines changed

4 files changed

+77
-10
lines changed

examples/L96m/L96m.jl

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ variables are fast.
3131
3232
Input:
3333
- `z` : vector of size (K + K*J)
34-
- `p` : parameters
34+
- `p` : parameters (L96m struct)
3535
- `t` : time (not used here since L96m is autonomous)
3636
3737
Output:
@@ -89,7 +89,7 @@ Both `rhs` and `x` are vectors of size p.K.
8989
9090
Input:
9191
- `x` : vector of size K
92-
- `p` : parameters
92+
- `p` : parameters (L96m struct)
9393
- `t` : time (not used here since L96m is autonomous)
9494
9595
Output:
@@ -121,7 +121,7 @@ Both `rhs` and `x` are vectors of size p.K.
121121
122122
Input:
123123
- `x` : vector of size K
124-
- `p` : parameters
124+
- `p` : parameters (L96m struct)
125125
- `t` : time (not used here since L96m is autonomous)
126126
127127
Output:
@@ -177,4 +177,55 @@ function set_G0(p::L96m, slope::Real)
177177
set_G0(p, slope = slope)
178178
end
179179

180+
"""
181+
Gather (xk, Yk) pairs that are further used to train a GP regressor
182+
183+
Input:
184+
- `p` : parameters (L96m struct)
185+
- `sol` : time series of a solution; time steps are in the 2nd dimension
186+
(usually, just `sol` output from a time-stepper)
187+
188+
Output:
189+
- `pairs` : a 2-d array of size (N, 2) containing (xk, Yk) pairs, where N is
190+
the 2nd dimension in `sol` (number of time steps)
191+
192+
"""
193+
function gather_pairs(p::L96m, sol)
194+
N = size(sol, 2)
195+
pairs = Array{Float64, 2}(undef, p.K * N, 2)
196+
for n in 1:N
197+
pairs[p.K * (n-1) + 1 : p.K * n, 1] = sol[1:p.K, n]
198+
pairs[p.K * (n-1) + 1 : p.K * n, 2] = compute_Yk(p, sol[:,n])
199+
end
200+
return pairs
201+
end
202+
203+
"""
204+
Returns a randomly initialized array that can be used as an IC to ODE solver
205+
206+
The returned array is of size `p.K + p.K * p.J`.
207+
The first `p.K` variables are slow and are drawn randomly ~ U[-5; 10]; each of
208+
the fast variables corresponding to the same slow variable is set to the value
209+
of that slow variable.
210+
211+
For example, if K == 2 and J = 3, the returned array will be
212+
[ rand1, rand2, rand1, rand1, rand1, rand2, rand2, rand2 ]
213+
214+
Input:
215+
- `p` : parameters (L96m struct)
216+
217+
Output:
218+
- `z00` : array of size `p.K + p.K * p.J` with random values
219+
"""
220+
function random_init(p::L96m)
221+
z00 = Array{Float64}(undef, p.K + p.K * p.J)
222+
223+
z00[1:p.K] .= rand(p.K) * 15 .- 5
224+
for k_ in 1:p.K
225+
z00[p.K+1 + (k_-1)*p.J : p.K + k_*p.J] .= z00[k_]
226+
end
227+
228+
return z00
229+
end
230+
180231

examples/L96m/main.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,7 @@ print_var(parameters)
143143
l96 = L96m(hx = hx, J = 8)
144144
set_G0(l96) # set the linear closure (essentially, as in balanced)
145145

146-
z00 = Array{Float64}(undef, l96.K + l96.K * l96.J)
147-
148-
# method 1
149-
z00[1:l96.K] .= rand(l96.K) * 15 .- 5
150-
for k_ in 1:l96.K
151-
z00[l96.K+1 + (k_-1)*l96.J : l96.K + k_*l96.J] .= z00[k_]
152-
end
146+
z00 = random_init(l96)
153147

154148
################################################################################
155149
# main section #################################################################

test/L96m/data/dp5_pairs.npy

1.63 KB
Binary file not shown.

test/L96m/runtests.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const Yk_test = NPZ.npzread(joinpath(data_dir, "Yk.npy"))
1717
const full_test = NPZ.npzread(joinpath(data_dir, "full.npy"))
1818
const balanced_test = NPZ.npzread(joinpath(data_dir, "balanced.npy"))
1919
const regressed_test = NPZ.npzread(joinpath(data_dir, "regressed.npy"))
20+
const dp5_pairs = NPZ.npzread(joinpath(data_dir, "dp5_pairs.npy"))
2021
const dp5_test = NPZ.npzread(joinpath(data_dir, "dp5_full.npy"))
2122
const tp8_test = NPZ.npzread(joinpath(data_dir, "tsitpap8_full.npy"))
2223
const bal_test = NPZ.npzread(joinpath(data_dir, "dp5_bal.npy"))
@@ -59,6 +60,27 @@ const bal_test = NPZ.npzread(joinpath(data_dir, "dp5_bal.npy"))
5960
@test -0.5 * v1 == l96.G(v1)
6061
@test -0.5 * v2 == l96.G(v2)
6162
end
63+
64+
m1 = [1:9.0 1:9.0]
65+
z00 = zeros(l96.K + l96.K * l96.J)
66+
z00[1:l96.K] .= 1:l96.K
67+
for k_ in 1:l96.K
68+
z00[l96.K + 1 + (k_-1)*l96.J : l96.K + k_*l96.J] .= z00[k_]
69+
end
70+
@test isapprox(gather_pairs(l96, z00), m1)
71+
72+
z00 = random_init(l96)
73+
x00 = @view(z00[1:l96.K])
74+
y00 = @view(z00[l96.K+1:end])
75+
@test size(z00,1) == l96.K + l96.K * l96.J
76+
@test size(z00,1) == length(z00)
77+
@test all(-5.0 .< x00 .< 10.0)
78+
@test all(x00 .== reshape(y00, l96.J, l96.K)')
79+
80+
m2 = [x00 x00]
81+
@test isapprox(gather_pairs(l96, z00), m2)
82+
83+
@test isapprox(gather_pairs(l96, dp5_test), dp5_pairs)
6284
end
6385
println("")
6486

0 commit comments

Comments
 (0)