|
| 1 | +using Test |
| 2 | +using Adapt |
| 3 | +using Lux |
| 4 | +using JLD2 |
| 5 | +using ConvolutionalNeuralOperators: create_CNOdownsampler, create_CNO |
| 6 | +using ComponentArrays: ComponentArray |
| 7 | +using Optimisers: Adam, ClipGrad, OptimiserChain |
| 8 | +using Random |
| 9 | +using Zygote: Zygote |
| 10 | +using CUDA |
| 11 | +using CoupledNODE |
| 12 | +using IncompressibleNavierStokes |
| 13 | +using NeuralClosure |
| 14 | +using OrdinaryDiffEqTsit5 |
| 15 | + |
| 16 | +@testset "CoupledNode integration" begin |
| 17 | + |
| 18 | + # Define parameters for the model |
| 19 | + nles = 16 |
| 20 | + T = Float32 |
| 21 | + N = nles |
| 22 | + D = 2 |
| 23 | + rng = Xoshiro(123) |
| 24 | + r = [2, 2] |
| 25 | + c = [2, 2] |
| 26 | + σ = [tanh, identity] |
| 27 | + b = [true, false] |
| 28 | + emb_sizes = [4, 4] |
| 29 | + Ns = reverse([N + 2 * sum(r[1:i]) for i = 1:length(r)]) |
| 30 | + patch_sizes = [8, 5] |
| 31 | + n_heads = [2, 2] |
| 32 | + use_attention = [true, true] |
| 33 | + sum_attention = [false, false] |
| 34 | + |
| 35 | + # Create the model |
| 36 | + ch_ = [2, 2] |
| 37 | + act = [tanh_fast, identity] |
| 38 | + df = [2, 2] |
| 39 | + k_rad = [3, 3] |
| 40 | + bd = [2, 2, 2] |
| 41 | + cutoff = 10 |
| 42 | + closure, θ_start, st = cno( |
| 43 | + T = T, |
| 44 | + N = N, |
| 45 | + D = D, |
| 46 | + cutoff = cutoff, |
| 47 | + ch_sizes = ch_, |
| 48 | + activations = act, |
| 49 | + down_factors = df, |
| 50 | + k_radii = k_rad, |
| 51 | + bottleneck_depths = bd, |
| 52 | + rng = rng, |
| 53 | + use_cuda = false, |
| 54 | + ) |
| 55 | + |
| 56 | + # Define input tensor and pass through model |
| 57 | + batch = 4 |
| 58 | + input_tensor = rand(T, N, N, D, batch) |
| 59 | + output = Lux.apply(closure, input_tensor, θ_start, st)[1] |
| 60 | + @test size(output) == size(input_tensor) |
| 61 | + |
| 62 | + |
| 63 | + # Read conf |
| 64 | + NS = Base.get_extension(CoupledNODE, :NavierStokes) |
| 65 | + conf = NS.read_config("./config.yaml") |
| 66 | + conf["params"]["backend"] = CPU() |
| 67 | + |
| 68 | + # get params |
| 69 | + params = NS.load_params(conf) |
| 70 | + device(x) = adapt(params.backend, x) |
| 71 | + |
| 72 | + # Get the setup in the format expected by the CoupledNODE |
| 73 | + function getsetup(; params, nles) |
| 74 | + Setup(; |
| 75 | + x = ntuple(α -> range(params.lims..., nles + 1), params.D), |
| 76 | + params.Re, |
| 77 | + params.backend, |
| 78 | + params.bodyforce, |
| 79 | + params.issteadybodyforce, |
| 80 | + ) |
| 81 | + end |
| 82 | + setup = getsetup(; params, nles) |
| 83 | + psolver = default_psolver(setup) |
| 84 | + setup = [] |
| 85 | + for nl in nles |
| 86 | + x = ntuple(α -> LinRange(T(0.0), T(1.0), nl + 1), params.D) |
| 87 | + push!(setup, Setup(; x = x, Re = params.Re, params.backend)) |
| 88 | + end |
| 89 | + |
| 90 | + # Load data |
| 91 | + function namedtupleload(file) |
| 92 | + dict = load(file) |
| 93 | + k, v = keys(dict), values(dict) |
| 94 | + pairs = @. Symbol(k) => v |
| 95 | + (; pairs...) |
| 96 | + end |
| 97 | + data_train = [] |
| 98 | + data_i = namedtupleload("data_train.jld2") |
| 99 | + push!(data_train, hcat(data_i)) |
| 100 | + |
| 101 | + |
| 102 | + # Create the io array |
| 103 | + NS = Base.get_extension(CoupledNODE, :NavierStokes) |
| 104 | + io_train = NS.create_io_arrays_posteriori(data_train, setup) |
| 105 | + |
| 106 | + # Create the dataloader |
| 107 | + θ = device(copy(θ_start)) |
| 108 | + nunroll = 2 |
| 109 | + nunroll_valid = 2 |
| 110 | + dataloader_post = NS.create_dataloader_posteriori( |
| 111 | + io_train[1]; |
| 112 | + nunroll = nunroll, |
| 113 | + rng = Random.Xoshiro(24), |
| 114 | + device = device, |
| 115 | + ) |
| 116 | + |
| 117 | + # Create the right hand side and the loss |
| 118 | + dudt_nn = NS.create_right_hand_side_with_closure(setup[1], psolver, closure, st) |
| 119 | + loss = CoupledNODE.create_loss_post_lux( |
| 120 | + dudt_nn; |
| 121 | + sciml_solver = Tsit5(), |
| 122 | + dt = T(conf["params"]["Δt"]), |
| 123 | + use_cuda = CUDA.functional(), |
| 124 | + ) |
| 125 | + callbackstate = trainstate = nothing |
| 126 | + |
| 127 | + |
| 128 | + # For testing reason, explicitely set up the probelm |
| 129 | + # Notice that this is automatically done in CoupledNODE |
| 130 | + u, t = dataloader_post() |
| 131 | + griddims = ((:) for _ = 1:(ndims(u)-2)) |
| 132 | + x = u[griddims..., :, 1] |
| 133 | + y = u[griddims..., :, 2:end] # remember to discard sol at the initial time step |
| 134 | + tspan, dt, prob, pred = nothing, nothing, nothing, nothing # initialize variable outside allowscalar do. |
| 135 | + dt = @views t[2:2] .- t[1:1] |
| 136 | + dt = only(Array(dt)) |
| 137 | + function get_tspan(t) |
| 138 | + return (Array(t)[1], Array(t)[end]) |
| 139 | + end |
| 140 | + tspan = get_tspan(t) |
| 141 | + prob = ODEProblem(dudt_nn, x, tspan, θ) |
| 142 | + pred = Array( |
| 143 | + solve(prob, Tsit5(); u0 = x, p = θ, adaptive = false, saveat = Array(t), dt = dt), |
| 144 | + ) |
| 145 | + |
| 146 | + # Test the forward pass |
| 147 | + @test size(pred[:, :, :, 2:end]) == size(y) |
| 148 | + |
| 149 | + |
| 150 | + # Test the backward pass |
| 151 | + p = prob.p |
| 152 | + y = prob.u0 |
| 153 | + f = prob.f |
| 154 | + λ = zero(prob.u0) |
| 155 | + _dy, back = Zygote.pullback(y, p) do u, p |
| 156 | + vec(f(u, p, t)) |
| 157 | + end |
| 158 | + tmp1, tmp2 = back(λ) |
| 159 | + @test size(tmp1) == (18, 18, 2) |
| 160 | + @test size(tmp2) == (94118,) |
| 161 | + |
| 162 | + # Final integration test of the entire train interface |
| 163 | + l, trainstate = CoupledNODE.train( |
| 164 | + closure, |
| 165 | + θ, |
| 166 | + st, |
| 167 | + dataloader_post, |
| 168 | + loss; |
| 169 | + tstate = trainstate, |
| 170 | + nepochs = 2, |
| 171 | + #alg = OptimiserChain(Adam(T(1.0e-3)), ClipGrad(0.1)), |
| 172 | + alg = Adam(T(1.0e-3)), |
| 173 | + cpu = true, |
| 174 | + ) |
| 175 | + @test isnan(l) == false |
| 176 | + @test trainstate.step == 2 |
| 177 | + @test any(isnan, trainstate.parameters) == false |
| 178 | + |
| 179 | +end |
0 commit comments