|
| 1 | +@info "adaptive_loss_logging_tests" |
| 2 | +using DiffEqFlux |
| 3 | +using ModelingToolkit |
| 4 | +using Test, NeuralPDE |
| 5 | +using GalacticOptim |
| 6 | +import ModelingToolkit: Interval, infimum, supremum |
| 7 | +using Random |
| 8 | +#using Plots |
| 9 | +@info "Starting Soon!" |
| 10 | + |
| 11 | +nonadaptive_loss = NeuralPDE.NonAdaptiveLoss(pde_loss_weights=1, bc_loss_weights=1) |
| 12 | +gradnormadaptive_loss = NeuralPDE.GradientScaleAdaptiveLoss(100, pde_loss_weights=1e3, bc_loss_weights=1) |
| 13 | +adaptive_loss = NeuralPDE.MiniMaxAdaptiveLoss(100; pde_loss_weights=1, bc_loss_weights=1) |
| 14 | +adaptive_losses = [nonadaptive_loss, gradnormadaptive_loss,adaptive_loss] |
| 15 | +maxiters=800 |
| 16 | +seed=60 |
| 17 | + |
| 18 | +## 2D Poisson equation |
| 19 | +function test_2d_poisson_equation_adaptive_loss(adaptive_loss, run, outdir, haslogger; seed=60, maxiters=800) |
| 20 | + logdir = joinpath(outdir, string(run)) |
| 21 | + if haslogger |
| 22 | + logger = TBLogger(logdir) |
| 23 | + else |
| 24 | + logger = nothing |
| 25 | + end |
| 26 | + Random.seed!(seed) |
| 27 | + hid = 40 |
| 28 | + chain_ = FastChain(FastDense(2,hid,Flux.σ),FastDense(hid,hid,Flux.σ),FastDense(hid,1)) |
| 29 | + strategy_ = NeuralPDE.StochasticTraining(256) |
| 30 | + @info "adaptive reweighting test logdir: $(logdir), maxiters: $(maxiters), 2D Poisson equation, adaptive_loss: $(nameof(typeof(adaptive_loss))) " |
| 31 | + @parameters x y |
| 32 | + @variables u(..) |
| 33 | + Dxx = Differential(x)^2 |
| 34 | + Dyy = Differential(y)^2 |
| 35 | + |
| 36 | + # 2D PDE |
| 37 | + eq = Dxx(u(x,y)) + Dyy(u(x,y)) ~ -sin(pi*x)*sin(pi*y) |
| 38 | + |
| 39 | + # Initial and boundary conditions |
| 40 | + bcs = [u(0,y) ~ 0.0, u(1,y) ~ -sin(pi*1)*sin(pi*y), |
| 41 | + u(x,0) ~ 0.0, u(x,1) ~ -sin(pi*x)*sin(pi*1)] |
| 42 | + # Space and time domains |
| 43 | + domains = [x ∈ Interval(0.0,1.0), |
| 44 | + y ∈ Interval(0.0,1.0)] |
| 45 | + |
| 46 | + initθ = Float64.(DiffEqFlux.initial_params(chain_)) |
| 47 | + iteration = [0] |
| 48 | + discretization = NeuralPDE.PhysicsInformedNN(chain_, |
| 49 | + strategy_; |
| 50 | + init_params = initθ, |
| 51 | + adaptive_loss = adaptive_loss, |
| 52 | + logger = logger, |
| 53 | + iteration=iteration) |
| 54 | + |
| 55 | + |
| 56 | + @named pde_system = PDESystem(eq,bcs,domains,[x,y],[u(x, y)]) |
| 57 | + prob = NeuralPDE.discretize(pde_system,discretization) |
| 58 | + phi = discretization.phi |
| 59 | + sym_prob = NeuralPDE.symbolic_discretize(pde_system,discretization) |
| 60 | + |
| 61 | + |
| 62 | + xs,ys = [infimum(d.domain):0.01:supremum(d.domain) for d in domains] |
| 63 | + analytic_sol_func(x,y) = (sin(pi*x)*sin(pi*y))/(2pi^2) |
| 64 | + u_real = reshape([analytic_sol_func(x,y) for x in xs for y in ys], (length(xs),length(ys))) |
| 65 | + |
| 66 | + cb = function (p,l) |
| 67 | + iteration[1] += 1 |
| 68 | + if iteration[1] % 100 == 0 |
| 69 | + @info "Current loss is: $l, iteration is $(iteration[1])" |
| 70 | + end |
| 71 | + if haslogger |
| 72 | + log_value(logger, "outer_error/loss", l, step=iteration[1]) |
| 73 | + if iteration[1] % 30 == 0 |
| 74 | + u_predict = reshape([first(phi([x,y],p)) for x in xs for y in ys],(length(xs),length(ys))) |
| 75 | + diff_u = abs.(u_predict .- u_real) |
| 76 | + total_diff = sum(diff_u) |
| 77 | + log_value(logger, "outer_error/total_diff", total_diff, step=iteration[1]) |
| 78 | + total_u = sum(abs.(u_real)) |
| 79 | + total_diff_rel = total_diff / total_u |
| 80 | + log_value(logger, "outer_error/total_diff_rel", total_diff_rel, step=iteration[1]) |
| 81 | + total_diff_sq = sum(diff_u .^ 2) |
| 82 | + log_value(logger, "outer_error/total_diff_sq", total_diff_sq, step=iteration[1]) |
| 83 | + end |
| 84 | + end |
| 85 | + return false |
| 86 | + end |
| 87 | + res = GalacticOptim.solve(prob, ADAM(0.03); maxiters=maxiters, cb=cb) |
| 88 | + |
| 89 | + u_predict = reshape([first(phi([x,y],res.minimizer)) for x in xs for y in ys],(length(xs),length(ys))) |
| 90 | + diff_u = abs.(u_predict .- u_real) |
| 91 | + total_diff = sum(diff_u) |
| 92 | + total_u = sum(abs.(u_real)) |
| 93 | + total_diff_rel = total_diff / total_u |
| 94 | + |
| 95 | + #p1 = plot(xs, ys, u_real, linetype=:contourf,title = "analytic"); |
| 96 | + #p2 = plot(xs, ys, u_predict, linetype=:contourf,title = "predict"); |
| 97 | + #p3 = plot(xs, ys, diff_u,linetype=:contourf,title = "error"); |
| 98 | + #(plot=plot(p1,p2,p3), error=total_diff, total_diff_rel=total_diff_rel) |
| 99 | + (error=total_diff, total_diff_rel=total_diff_rel) |
| 100 | +end |
| 101 | + |
| 102 | +possible_logger_dir = mktempdir() |
| 103 | +if ENV["LOG_SETTING"] == "NoImport" |
| 104 | + haslogger = false |
| 105 | + expected_log_folders = 0 |
| 106 | +elseif ENV["LOG_SETTING"] == "ImportNoUse" |
| 107 | + using NeuralPDELogging |
| 108 | + haslogger = false |
| 109 | + expected_log_folders = 0 |
| 110 | +elseif ENV["LOG_SETTING"] == "ImportUse" |
| 111 | + using NeuralPDELogging |
| 112 | + using TensorBoardLogger |
| 113 | + haslogger = true |
| 114 | + expected_log_folders = 3 |
| 115 | +end |
| 116 | + |
| 117 | +@info "has logger: $(haslogger), expected log folders: $(expected_log_folders)" |
| 118 | + |
| 119 | +test_2d_poisson_equation_adaptive_loss_run_seediters(adaptive_loss, run) = test_2d_poisson_equation_adaptive_loss(adaptive_loss, run, possible_logger_dir, haslogger; seed=seed, maxiters=maxiters) |
| 120 | +error_results = map(test_2d_poisson_equation_adaptive_loss_run_seediters, adaptive_losses, 1:length(adaptive_losses)) |
| 121 | + |
| 122 | +@test length(readdir(possible_logger_dir)) == expected_log_folders |
| 123 | +if expected_log_folders > 0 |
| 124 | + @info "dirs at $(possible_logger_dir): $(string(readdir(possible_logger_dir)))" |
| 125 | + for logdir in readdir(possible_logger_dir) |
| 126 | + @test length(readdir(joinpath(possible_logger_dir, logdir))) > 0 |
| 127 | + end |
| 128 | +end |
0 commit comments