GSoC 2021 NumFOCUS : Using the Numeric Integral and solving simple equations
The last blog covers how the Symbolic Integral was added to Julia Symbolics ecosystem, this blog covers the numeric evaluation of the integral and making it compatible with NeuralPDE.jl to solve Integro-Differential Equations.
We start of by adding the necessary packages. Note : I am adding the package NeuralPDE.jl (previously NeuralNetDiffEq.jl) from my forked repository branch, because the pull request is in progress.
] add NeuralPDE
using Pkg
Pkg.add("Flux")
Pkg.add("DiffEqFlux")
Pkg.add("ModelingToolkit")
Pkg.add("DiffEqBase")
Pkg.add("GalacticOptim")
Pkg.add("Optim")
Pkg.add("Quadrature")
Pkg.add("SciMLBase")
Pkg.add("DomainSets")
using Flux, DiffEqFlux, ModelingToolkit
using DiffEqBase, SciMLBase
using GalacticOptim, Optim
using Quadrature
using DomainSets
using Plots
using NeuralPDE
We would start with a simple equation
formula not implementedwith boundary conditions
inline_formula not implemented and inline_formula not implemented
and let's take the domain from x in (0, 2)
We know that the solution of the above equation is u(x) = sin(x)
x
u(..)
Ix = Integral(x in DomainSets.ClosedInterval(0, pi/2))
eq = Ix(u(x)) ~ 1.0
bcs = [u(0.0) ~ 0.0, u(1.570) ~ 1.0]
domains = [x ∈ Interval(0.0,2.0)]
We define the Neural Network chain for the solution of the equation "u"
chain = Chain(Dense(1,15,Flux.σ),Dense(15,1))
initθ = Float64.(DiffEqFlux.initial_params(chain))
We define our strategy, discretization and the optimizaiton problem
strategy_ = NeuralPDE.GridTraining(0.001)
discretization = NeuralPDE.PhysicsInformedNN(chain,
strategy_;
init_params = nothing,
phi = nothing,
derivative = nothing,
)
pde_system = PDESystem(eq,bcs,domains,[x],[u])
prob = NeuralPDE.discretize(pde_system,discretization)
cb = function (p,l)
println("Current loss is: $l")
return false
end
res = GalacticOptim.solve(prob, BFGS(); cb = cb, maxiters=100)
xs = 0.00:0.01:1.5
phi = discretization.phi
u_predict = [first(phi([x],res.minimizer)) for x in xs]
u_real = [sin(x) for x in xs]
Flux.mse(u_real, u_predict)
plot(xs, u_predict, label = "Predicted Solution")
plot!(xs, u_real, label = "Real solution")
Let us consider another example with
formula not implementedWe know that the solution would be inline_formula not implemented
Note that we would be using a symbolic upper limit here
Let us write it in the form:
x
u(..)
Ix = Integral(x in DomainSets.ClosedInterval(0, x))
eq = Ix(u(x)) ~ (x^3)/3
bcs = [u(0.) ~ 0.0]
domains = [x ∈ Interval(0.0,1.00)]
chain = Chain(Dense(1,15,Flux.σ),Dense(15,1))
initθ = Float64.(DiffEqFlux.initial_params(chain))
strategy_ = NeuralPDE.GridTraining(0.05)
discretization = NeuralPDE.PhysicsInformedNN(chain,
strategy_;
init_params = nothing,
phi = nothing,
derivative = nothing,
)
pde_system = PDESystem(eq,bcs,domains,[x],[u])
prob = NeuralPDE.discretize(pde_system,discretization)
res = GalacticOptim.solve(prob, BFGS(); cb = cb, maxiters=100)
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_real = [x^2 for x in xs]
plot(xs, u_predict, label = "Predicted Solution")
plot!(xs, u_real, label = "Real solution")
What's Next?
Integro-Differential Equations
Support for multidimensional domains