Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use latest API for IntegralProblem #822

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:
- AdaptiveLoss
- Logging
- Forward
- NeuralAdapter
version:
- "1"
steps:
Expand Down
2 changes: 1 addition & 1 deletion lib/NeuralPDELogging/test/adaptive_loss_log_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function test_2d_poisson_equation_adaptive_loss(adaptive_loss, run, outdir, hasl
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.03); maxiters = maxiters,
callback = callback)

u_predict = reshape([first(phi([x, y], res.minimizer)) for x in xs for y in ys],
u_predict = reshape([first(phi([x, y], res.u)) for x in xs for y in ys],
(length(xs), length(ys)))
diff_u = abs.(u_predict .- u_real)
total_diff = sum(diff_u)
Expand Down
2 changes: 1 addition & 1 deletion src/advancedHMC_MCMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function getlogpdf(strategy::QuadratureTraining, Tar::LogTargetDensity, f,
function integrand(t::Number, θ)
innerdiff(Tar, f, autodiff, [t], θ, ode_params)
end
intprob = IntegralProblem(integrand, tspan[1], tspan[2], θ; nout = length(Tar.prob.u0))
intprob = IntegralProblem(integrand, (tspan[1], tspan[2]), θ; nout = length(Tar.prob.u0))
sol = solve(intprob, QuadGKJL(); abstol = strategy.abstol, reltol = strategy.reltol)
sum(sol.u)
end
Expand Down
2 changes: 1 addition & 1 deletion src/discretize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ function get_numeric_integral(pinnrep::PINNRepresentation)
ChainRulesCore.@ignore_derivatives @views(cord_[integrating_var_id]) .= x
return integrand_func(cord_, p, phi, derivative, nothing, u, nothing)
end
prob_ = IntegralProblem(integrand_, lb, ub, θ)
prob_ = IntegralProblem(integrand_, (lb, ub), θ)
sol = solve(prob_, CubatureJLh(), reltol = 1e-3, abstol = 1e-3)[1]

return sol
Expand Down
2 changes: 1 addition & 1 deletion src/ode_solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function generate_loss(strategy::QuadratureTraining, phi, f, autodiff::Bool, tsp
@assert batch == 0 # not implemented

function loss(θ, _)
intprob = IntegralProblem(integrand, tspan[1], tspan[2], θ)
intprob = IntegralProblem(integrand, (tspan[1], tspan[2]), θ)
sol = solve(intprob, QuadGKJL(); abstol = strategy.abstol, reltol = strategy.reltol)
sol.u
end
Expand Down
2 changes: 1 addition & 1 deletion src/training_strategies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ function get_loss_function(loss_function, lb, ub, eltypeθ, strategy::Quadrature
sum(abs2, view(loss_(x, θ), 1, :), dims = 2) #./ size_x
end
integral_function = BatchIntegralFunction(integrand, max_batch = strategy.batch)
prob = IntegralProblem(integral_function, lb, ub, θ)
prob = IntegralProblem(integral_function, (lb, ub), θ)
solve(prob,
strategy.quadrature_alg,
reltol = strategy.reltol,
Expand Down
12 changes: 6 additions & 6 deletions test/IDE_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end
phi = discretization.phi
analytic_sol_func(t) = 1 / 2 * (exp(-t)) * (sin(2 * t))
u_real = [analytic_sol_func(t) for t in ts]
u_predict = [first(phi([t], res.minimizer)) for t in ts]
u_predict = [first(phi([t], res.u)) for t in ts]
@test Flux.mse(u_real, u_predict) < 0.01
end

Expand All @@ -54,7 +54,7 @@ eq = Ix(u(x) * cos(x)) ~ (x^3) / 3
maxiters = 200)
xs = [infimum(d.domain):0.01:supremum(d.domain) for d in domains][1]
phi = discretization.phi
u_predict = [first(phi([x], res.minimizer)) for x in xs]
u_predict = [first(phi([x], res.u)) for x in xs]
u_real = [x^2 / cos(x) for x in xs]
@test Flux.mse(u_real, u_predict) < 0.001
end
Expand All @@ -78,7 +78,7 @@ end
ys = 0.00:0.01:1.00
phi = discretization.phi
u_real = collect(1 - x^2 - y^2 for y in ys, x in xs);
u_predict = collect(Array(phi([x, y], res.minimizer))[1] for y in ys, x in xs);
u_predict = collect(Array(phi([x, y], res.u))[1] for y in ys, x in xs);
@test Flux.mse(u_real, u_predict) < 0.001
end

Expand All @@ -101,7 +101,7 @@ end
ys = 0.00:0.01:1.00
phi = discretization.phi
u_real = collect(x + y^2 for y in ys, x in xs);
u_predict = collect(Array(phi([x, y], res.minimizer))[1] for y in ys, x in xs);
u_predict = collect(Array(phi([x, y], res.u))[1] for y in ys, x in xs);
@test Flux.mse(u_real, u_predict) < 0.01
end

Expand Down Expand Up @@ -144,7 +144,7 @@ end
res = solve(prob, OptimizationOptimJL.BFGS(); callback = callback, maxiters = 200)
xs = [infimum(d.domain):0.01:supremum(d.domain) for d in domains][1]
phi = discretization.phi
u_predict = [first(phi([x], res.minimizer)) for x in xs]
u_predict = [first(phi([x], res.u)) for x in xs]
u_real = [1 / x^2 for x in xs]
@test u_real≈u_predict rtol=10^-2
end
Expand All @@ -163,7 +163,7 @@ end
res = solve(prob, OptimizationOptimJL.BFGS(); callback = callback, maxiters = 300)
xs = [infimum(d.domain):0.01:supremum(d.domain) for d in domains][1]
phi = discretization.phi
u_predict = [first(phi([x], res.minimizer)) for x in xs]
u_predict = [first(phi([x], res.u)) for x in xs]
u_real = [1 / x^2 for x in xs]
@test u_real≈u_predict rtol=10^-2
end
14 changes: 7 additions & 7 deletions test/NNPDE_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ function test_ode(strategy_)
prob = discretize(pde_system, discretization)

res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.1); maxiters = 1000)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.01); maxiters = 500)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.001); maxiters = 500)
phi = discretization.phi
analytic_sol_func(t) = exp(-(t^2) / 2) / (1 + t + t^3) + t^2
ts = [infimum(d.domain):0.01:supremum(d.domain) for d in domains][1]
u_real = [analytic_sol_func(t) for t in ts]
u_predict = [first(phi(t, res.minimizer)) for t in ts]
u_predict = [first(phi(t, res.u)) for t in ts]
@test u_predict≈u_real atol=0.1
end

Expand Down Expand Up @@ -183,7 +183,7 @@ function test_2d_poisson_equation(chain_, strategy_)
xs, ys = [infimum(d.domain):0.01:supremum(d.domain) for d in domains]
analytic_sol_func(x, y) = (sin(pi * x) * sin(pi * y)) / (2pi^2)

u_predict = reshape([first(phi([x, y], res.minimizer)) for x in xs for y in ys],
u_predict = reshape([first(phi([x, y], res.u)) for x in xs for y in ys],
(length(xs), length(ys)))
u_real = reshape([analytic_sol_func(x, y) for x in xs for y in ys],
(length(xs), length(ys)))
Expand Down Expand Up @@ -431,14 +431,14 @@ end
@named pde_system = PDESystem(eq, bcs, domains, [θ], [u])
prob = discretize(pde_system, discretization)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.1); maxiters = 1000)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.01); maxiters = 500)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.001); maxiters = 500)
phi = discretization.phi
analytic_sol_func(t) = exp(-(t^2) / 2) / (1 + t + t^3) + t^2
ts = [infimum(d.domain):0.01:supremum(d.domain) for d in domains][1]
u_real = [analytic_sol_func(t) for t in ts]
u_predict = [first(phi(t, res.minimizer)) for t in ts]
u_predict = [first(phi(t, res.u)) for t in ts]
@test u_predict≈u_real atol=0.1
end
14 changes: 7 additions & 7 deletions test/NNPDE_tests_gpu_Lux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const gpud = gpu_device()
analytic_sol_func(t) = exp(-(t^2) / 2) / (1 + t + t^3) + t^2
ts = [infimum(d.domain):(dt / 10):supremum(d.domain) for d in domains][1]
u_real = [analytic_sol_func(t) for t in ts]
u_predict = [first(Array(phi([t], res.minimizer))) for t in ts]
u_predict = [first(Array(phi([t], res.u))) for t in ts]
@test u_predict≈u_real atol=0.2
end

Expand Down Expand Up @@ -86,12 +86,12 @@ end
discretization = PhysicsInformedNN(chain, strategy; init_params = ps)
prob = discretize(pdesys, discretization)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.01); maxiters = 1000)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.001); maxiters = 1000)
phi = discretization.phi
u_exact = (t, x) -> exp.(-t) * cos.(x)
ts, xs = [infimum(d.domain):0.01:supremum(d.domain) for d in domains]
u_predict = reshape([first(Array(phi([t, x], res.minimizer))) for t in ts for x in xs],
u_predict = reshape([first(Array(phi([t, x], res.u))) for t in ts for x in xs],
(length(ts), length(xs)))
u_real = reshape([u_exact(t, x) for t in ts for x in xs], (length(ts), length(xs)))
diff_u = abs.(u_predict .- u_real)
Expand Down Expand Up @@ -130,12 +130,12 @@ end
discretization = PhysicsInformedNN(chain, strategy; init_params = ps)
prob = discretize(pdesys, discretization)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.1); maxiters = 2000)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.01); maxiters = 2000)
phi = discretization.phi
u_exact = (t, x) -> exp(-t) * cos(x)
ts, xs = [infimum(d.domain):0.01:supremum(d.domain) for d in domains]
u_predict = reshape([first(Array(phi([t, x], res.minimizer))) for t in ts for x in xs],
u_predict = reshape([first(Array(phi([t, x], res.u))) for t in ts for x in xs],
(length(ts), length(xs)))
u_real = reshape([u_exact(t, x) for t in ts for x in xs], (length(ts), length(xs)))
diff_u = abs.(u_predict .- u_real)
Expand Down Expand Up @@ -184,12 +184,12 @@ end
@named pde_system = PDESystem(eq, bcs, domains, [t, x, y], [u(t, x, y)])
prob = discretize(pde_system, discretization)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.01); maxiters = 2500)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.001); maxiters = 2500)
phi = discretization.phi
ts, xs, ys = [infimum(d.domain):0.1:supremum(d.domain) for d in domains]
u_real = [analytic_sol_func(t, x, y) for t in ts for x in xs for y in ys]
u_predict = [first(Array(phi([t, x, y], res.minimizer))) for t in ts for x in xs
u_predict = [first(Array(phi([t, x, y], res.u))) for t in ts for x in xs
for y in ys]

@test u_predict≈u_real rtol=0.2
Expand Down
2 changes: 1 addition & 1 deletion test/adaptive_loss_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function test_2d_poisson_equation_adaptive_loss(adaptive_loss; seed = 60, maxite
return false
end
res = solve(prob, OptimizationOptimisers.Adam(0.03); maxiters = maxiters, callback = callback)
u_predict = reshape([first(phi([x, y], res.minimizer)) for x in xs for y in ys],
u_predict = reshape([first(phi([x, y], res.u)) for x in xs for y in ys],
(length(xs), length(ys)))
diff_u = abs.(u_predict .- u_real)
total_diff = sum(diff_u)
Expand Down
12 changes: 6 additions & 6 deletions test/additional_loss_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ using ComponentArrays
function inner_f(x, θ)
dx * phi(x, θ) .- 1
end
prob1 = IntegralProblem(inner_f, lb, ub, θ)
prob1 = IntegralProblem(inner_f, (lb, ub), θ)
norm2 = solve(prob1, HCubatureJL(), reltol = 1e-8, abstol = 1e-8, maxiters = 10)
abs(norm2[1])
end
Expand All @@ -63,7 +63,7 @@ using ComponentArrays
return false
end
res = solve(prob, OptimizationOptimJL.LBFGS(), maxiters = 400, callback = cb_)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = solve(prob, OptimizationOptimJL.BFGS(), maxiters = 2000, callback = cb_)
C = 142.88418699042
analytic_sol_func(x) = C * exp((1 / (2 * _σ^2)) * (2 * α * x^2 - β * x^4))
Expand All @@ -88,7 +88,7 @@ using ComponentArrays
return false
end
res = solve(prob, OptimizationOptimJL.LBFGS(), maxiters = 400, callback = cb_)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = solve(prob, OptimizationOptimJL.BFGS(), maxiters = 2000, callback = cb_)
C = 142.88418699042
analytic_sol_func(x) = C * exp((1 / (2 * _σ^2)) * (2 * α * x^2 - β * x^4))
Expand Down Expand Up @@ -170,7 +170,7 @@ end
Float64[])

res = solve(prob, OptimizationOptimJL.BFGS(); maxiters = 6000)
p_ = res.minimizer[(end - 2):end]
p_ = res.u[(end - 2):end]
@test sum(abs2, p_[1] - 10.00) < 0.1
@test sum(abs2, p_[2] - 28.00) < 0.1
@test sum(abs2, p_[3] - (8 / 3)) < 0.1
Expand All @@ -189,7 +189,7 @@ end
sym_prob = NeuralPDE.symbolic_discretize(pde_system, discretization)
sym_prob.loss_functions.full_loss_function(sym_prob.flat_init_params, nothing)
res = solve(prob, OptimizationOptimJL.BFGS(); maxiters = 6000)
p_ = res.minimizer[(end - 2):end]
p_ = res.u[(end - 2):end]
@test sum(abs2, p_[1] - 10.00) < 0.1
@test sum(abs2, p_[2] - 28.00) < 0.1
@test sum(abs2, p_[3] - (8 / 3)) < 0.1
Expand Down Expand Up @@ -225,7 +225,7 @@ end
phi(xs, flat_init_params)
additional_loss_(phi, flat_init_params, nothing)
res = solve(prob, OptimizationOptimisers.Adam(0.01), maxiters = 500)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = solve(prob, OptimizationOptimJL.BFGS(), maxiters = 500)
@test phi(xs, res.u)≈aproxf_(xs) rtol=0.01
end
10 changes: 5 additions & 5 deletions test/direct_function_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Random.seed!(110)
@named pde_system = PDESystem(eq, bc, domain, [x], [u(x)])
prob = discretize(pde_system, discretization)
res = solve(prob, OptimizationOptimisers.Adam(0.05), maxiters = 1000)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = solve(prob, OptimizationOptimJL.BFGS(initial_stepnorm = 0.01), maxiters = 500)
@test discretization.phi(xs', res.u)≈func(xs') rtol=0.01
end
Expand All @@ -62,7 +62,7 @@ end
@named pde_system = PDESystem(eq, bc, domain, [x], [u(x)])
prob = discretize(pde_system, discretization)
res = solve(prob, OptimizationOptimisers.Adam(0.01), maxiters = 500)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = solve(prob, OptimizationOptimJL.BFGS(), maxiters = 1000)
dx = 0.01
xs = collect(x0:dx:x_end)
Expand Down Expand Up @@ -95,14 +95,14 @@ end
symprob = NeuralPDE.symbolic_discretize(pde_system, discretization)
symprob.loss_functions.full_loss_function(symprob.flat_init_params, nothing)
res = solve(prob, OptimizationOptimisers.Adam(0.01), maxiters = 500)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = solve(prob, OptimizationOptimJL.BFGS(), maxiters = 1000)
prob = remake(prob, u0 = res.minimizer)
prob = remake(prob, u0 = res.u)
res = solve(prob, OptimizationOptimJL.BFGS(), maxiters = 500)
phi = discretization.phi
xs = collect(x0:0.1:x_end)
ys = collect(y0:0.1:y_end)
u_predict = reshape([first(phi([x, y], res.minimizer)) for x in xs for y in ys],
u_predict = reshape([first(phi([x, y], res.u)) for x in xs for y in ys],
(length(xs), length(ys)))
u_real = reshape([func(x, y) for x in xs for y in ys], (length(xs), length(ys)))
diff_u = abs.(u_predict .- u_real)
Expand Down
Loading
Loading