Skip to content

Commit

Permalink
Merge pull request #3 from sukhreens/master
Browse files Browse the repository at this point in the history
New polished test cases
  • Loading branch information
cgrudz authored Oct 12, 2021
2 parents 7e9ce6c + f7db297 commit a3809a2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 92 deletions.
3 changes: 3 additions & 0 deletions test/Test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Test

end
23 changes: 17 additions & 6 deletions test/TestDeSolvers.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
########################################################################################################################
module RunTest
module TestDeSolvers
########################################################################################################################
########################################################################################################################
# imports and exports
using DataAssimilationBenchmarks.DeSolvers
using DataAssimilationBenchmarks.L96
using Test

export sumfunc
########################################################################################################################
########################################################################################################################
# first testset using Euler Maruyama
@testset "Euler Maruyama" begin
function sumfunc()
# initial conditions and arguments
x = zeros(40)

# step size
h = 0.01
F = 8.0

dx_params = Dict{String, Array{Float64}}("F" => [F])
# forcing parameter
f = 8.0

kwargs = Dict{String, Any}(
"h" => h,
"diffusion" => 0.0,
"dx_params" => [f],
"dx_params" => dx_params,
"dx_dt" => L96.dx_dt,
)

# parameters to test
# em_step! writes over x in place
em_step!(x, 0.0, kwargs)

# evaluate test pass/fail if the vector of x is equal to (f*h) in every instance
@test sum(x .== (f*h)) == 40
# if conditions are true, pass
if sum(x .== (F*h)) == 40
sumresult = true
else
sumresult = false
end

end

########################################################################################################################
Expand Down
126 changes: 65 additions & 61 deletions test/TestTimeSeriesGeneration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,93 +6,97 @@ using DataAssimilationBenchmarks.DeSolvers
using DataAssimilationBenchmarks.L96
using JLD
using Random

#######################################################################################################################
# Test generation of the L96 model time series

function testL96()
# define the model and the solver
dx_dt = L96.dx_dt
step_model! = DeSolvers.em_step!
dx_dt = L96.dx_dt
step_model! = DeSolvers.em_step!

# set model and experimental parameters
F = 8.0
h = 0.001
nanl = 1000
sys_dim = 40
diffusion = 0.1
tanl = 0.01
seed = 0
Random.seed!(seed)
F = 8.0
h = 0.001
nanl = 1000
sys_dim = 40
diffusion = 0.1
tanl = 0.01
seed = 0
Random.seed!(seed)

# define the dx_params dict
dx_params = Dict{String, Array{Float64}}("F" => [F])
fore_steps = convert(Int64, tanl/h)
dx_params = Dict{String, Array{Float64}}("F" => [F])
fore_steps = convert(Int64, tanl/h)

# set the kwargs for the integration scheme
kwargs = Dict{String, Any}(
"h" => h,
"diffusion" => diffusion,
"dx_params" => dx_params,
"dx_dt" => L96.dx_dt,
)
kwargs = Dict{String, Any}(
"h" => h,
"diffusion" => diffusion,
"dx_params" => dx_params,
"dx_dt" => L96.dx_dt,
)

# set arbitrary initial condition
xt = ones(sys_dim)
xt = ones(sys_dim)

# pre-allocate storage for the time series observations
tobs = Array{Float64}(undef,sys_dim, nanl)
tobs = Array{Float64}(undef,sys_dim, nanl)

# loop the experiment, taking observations at time length tanl
for i in 1:nanl
for j in 1:fore_steps
step_model!(xt, 0.0, kwargs)
for i in 1:nanl
for j in 1:fore_steps
step_model!(xt, 0.0, kwargs)
end
tobs[:,i] = xt
end
tobs[:,i] = xt
end

# define the file name for the experiment output
# dynamically based on experiment parameters
fname = "time_series_data_seed_" * lpad(seed, 4, "0") *
"_dim_" * lpad(sys_dim, 2, "0") *
"_diff_" * rpad(diffusion, 5, "0") *
"_F_" * lpad(F, 4, "0") *
"_tanl_" * rpad(tanl, 4, "0") *
"_nanl_" * lpad(nanl, 5, "0") *
"_h_" * rpad(h, 5, "0") *
".jld"
fname = "time_series_data_seed_" * lpad(seed, 4, "0") *
"_dim_" * lpad(sys_dim, 2, "0") *
"_diff_" * rpad(diffusion, 5, "0") *
"_F_" * lpad(F, 4, "0") *
"_tanl_" * rpad(tanl, 4, "0") *
"_nanl_" * lpad(nanl, 5, "0") *
"_h_" * rpad(h, 5, "0") *
".jld"

# define the experimental data in a dictionary to write with JLD
data = Dict{String, Any}(
"h" => h,
"diffusion" => diffusion,
"F" => F,
"tanl" => tanl,
"nanl" => nanl,
"sys_dim" => sys_dim,
"tobs" => tobs
)
path = "../data/time_series/"

data = Dict{String, Any}(
"h" => h,
"diffusion" => diffusion,
"F" => F,
"tanl" => tanl,
"nanl" => nanl,
"sys_dim" => sys_dim,
"tobs" => tobs
)
path = "../data/time_series/"

# test to see if the data can be written to standard output directory
try
save(path * fname, data)
did_write = true
catch
# if not, set test case false
did_write = false
end

function write_file()
try
save(path * fname, data)
did_write = true
catch
# if not, set test case false
did_write = false
end
end

# test to see if the data can be read from standard output directory
try
tmp = load(path * fname)
did_read = true
catch
# if not, set test case false
did_read = false
end
function load_file()
try
tmp = load(path * fname)
did_read = true
catch
# if not, set test case false
did_read = false
end
end

write_file()
load_file()
end

#######################################################################################################################

Expand Down
39 changes: 14 additions & 25 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,22 @@ using JLD
using Random
using Test

########################################################################################################################
########################################################################################################################
# first testset using Euler Maruyama
@testset "Euler Maruyama" begin
# initial conditions and arguments
x = zeros(40)

# step size
h = 0.01

# forcing parameter
f = 8.0
kwargs = Dict{String, Any}(
"h" => h,
"diffusion" => 0.0,
"dx_params" => [f],
"dx_dt" => L96.dx_dt,
)
# include statements and import statements
include("TestDeSolvers.jl")
import .TestDeSolvers
import .L96

# parameters to test
# em_step! writes over x in place
em_step!(x, 0.0, kwargs)
include("TestTimeSeriesGeneration.jl")
import .TestDeSolvers
import .L96

# evaluate test pass/fail if the vector of x is equal to (f*h) in every instance
@test sum(x .== (f*h)) == 40
# test case 1: TestDeSolvers
@testset "Euler Maruyama" begin
@test TestDeSolvers.sumfunc()
end

########################################################################################################################

# test case 2: TestTimeSeriesGeneration
@testset "Time Series" begin
@test TestTimeSeriesGeneration.testL96()
end
end

0 comments on commit a3809a2

Please sign in to comment.