-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: further refactor tests and fix bugs
- Loading branch information
1 parent
528d2d1
commit b1a7fa1
Showing
3 changed files
with
60 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,64 @@ | ||
using Test, Flux | ||
using Random, NeuralPDE | ||
using OrdinaryDiffEq, Optimisers, Statistics | ||
using OrdinaryDiffEq, Statistics | ||
import Lux, OptimizationOptimisers, OptimizationOptimJL | ||
|
||
Random.seed!(100) | ||
|
||
#Example 1 | ||
function example1(du, u, p, t) | ||
du[1] = cos(2pi * t) | ||
du[2] = u[2] + cos(2pi * t) | ||
nothing | ||
@testset "Example 1" begin | ||
function example1(du, u, p, t) | ||
du[1] = cos(2pi * t) | ||
du[2] = u[2] + cos(2pi * t) | ||
nothing | ||
end | ||
u₀ = [1.0, -1.0] | ||
du₀ = [0.0, 0.0] | ||
M = [1.0 0 | ||
0 0] | ||
f = ODEFunction(example1, mass_matrix = M) | ||
tspan = (0.0f0, 1.0f0) | ||
|
||
prob_mm = ODEProblem(f, u₀, tspan) | ||
ground_sol = solve(prob_mm, Rodas5(), reltol = 1e-8, abstol = 1e-8) | ||
|
||
example = (du, u, p, t) -> [cos(2pi * t) - du[1], u[2] + cos(2pi * t) - du[2]] | ||
differential_vars = [true, false] | ||
prob = DAEProblem(example, du₀, u₀, tspan; differential_vars = differential_vars) | ||
chain = Lux.Chain(Lux.Dense(1, 15, cos), Lux.Dense(15, 15, sin), Lux.Dense(15, 2)) | ||
opt = OptimizationOptimisers.Adam(0.1) | ||
alg = NeuralPDE.NNDAE(chain, opt; autodiff = false) | ||
|
||
sol = solve(prob, | ||
alg, verbose = false, dt = 1 / 100.0f0, | ||
maxiters = 3000, abstol = 1.0f-10) | ||
@test ground_sol(0:(1 / 100):1)≈sol atol=0.4 | ||
end | ||
u₀ = [1.0, -1.0] | ||
du₀ = [0.0, 0.0] | ||
M = [1.0 0 | ||
0 0] | ||
f = ODEFunction(example1, mass_matrix = M) | ||
tspan = (0.0f0, 1.0f0) | ||
|
||
prob_mm = ODEProblem(f, u₀, tspan) | ||
ground_sol = solve(prob_mm, Rodas5(), reltol = 1e-8, abstol = 1e-8) | ||
|
||
example = (du, u, p, t) -> [cos(2pi * t) - du[1], u[2] + cos(2pi * t) - du[2]] | ||
differential_vars = [true, false] | ||
prob = DAEProblem(example, du₀, u₀, tspan; differential_vars = differential_vars) | ||
chain = Flux.Chain(Dense(1, 15, cos), Dense(15, 15, sin), Dense(15, 2)) | ||
opt = OptimizationOptimisers.Adam(0.1) | ||
alg = NeuralPDE.NNDAE(chain, opt; autodiff = false) | ||
|
||
sol = solve(prob, | ||
alg, verbose = false, dt = 1 / 100.0f0, | ||
maxiters = 3000, abstol = 1.0f-10) | ||
@test ground_sol(0:(1 / 100):1)≈sol atol=0.4 | ||
|
||
#Example 2 | ||
function example2(du, u, p, t) | ||
du[1] = u[1] - t | ||
du[2] = u[2] - t | ||
nothing | ||
|
||
@testset "Example 2" begin | ||
function example2(du, u, p, t) | ||
du[1] = u[1] - t | ||
du[2] = u[2] - t | ||
nothing | ||
end | ||
M = [0.0 0 | ||
0 1] | ||
u₀ = [0.0, 0.0] | ||
du₀ = [0.0, 0.0] | ||
tspan = (0.0f0, pi / 2.0f0) | ||
f = ODEFunction(example2, mass_matrix = M) | ||
prob_mm = ODEProblem(f, u₀, tspan) | ||
ground_sol = solve(prob_mm, Rodas5(), reltol = 1e-8, abstol = 1e-8) | ||
|
||
example = (du, u, p, t) -> [u[1] - t - du[1], u[2] - t - du[2]] | ||
differential_vars = [false, true] | ||
prob = DAEProblem(example, du₀, u₀, tspan; differential_vars = differential_vars) | ||
chain = Lux.Chain(Lux.Dense(1, 15, Lux.σ), Lux.Dense(15, 2)) | ||
opt = OptimizationOptimisers.Adam(0.1) | ||
alg = NNDAE(chain, OptimizationOptimisers.Adam(0.1); autodiff = false) | ||
|
||
sol = solve(prob, | ||
alg, verbose = false, dt = 1 / 100.0f0, | ||
maxiters = 3000, abstol = 1.0f-10) | ||
|
||
@test ground_sol(0:(1 / 100):(pi / 2))≈sol atol=0.4 | ||
end | ||
M = [0.0 0 | ||
0 1] | ||
u₀ = [0.0, 0.0] | ||
du₀ = [0.0, 0.0] | ||
tspan = (0.0f0, pi / 2.0f0) | ||
f = ODEFunction(example2, mass_matrix = M) | ||
prob_mm = ODEProblem(f, u₀, tspan) | ||
ground_sol = solve(prob_mm, Rodas5(), reltol = 1e-8, abstol = 1e-8) | ||
|
||
example = (du, u, p, t) -> [u[1] - t - du[1], u[2] - t - du[2]] | ||
differential_vars = [false, true] | ||
prob = DAEProblem(example, du₀, u₀, tspan; differential_vars = differential_vars) | ||
chain = Flux.Chain(Dense(1, 15, σ), Dense(15, 2)) | ||
opt = OptimizationOptimisers.Adam(0.1) | ||
alg = NNDAE(chain, OptimizationOptimisers.Adam(0.1); autodiff = false) | ||
|
||
sol = solve(prob, | ||
alg, verbose = false, dt = 1 / 100.0f0, | ||
maxiters = 3000, abstol = 1.0f-10) | ||
|
||
@test ground_sol(0:(1 / 100):(pi / 2))≈sol atol=0.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters