Solved!
Here is the code I tested it with. It is based on the Poisson tutorial from NeuralPDE.jl (It works very well BTW!! I encourage you to try it.):
using NeuralPDE, Lux, ModelingToolkit, Optimization, OptimizationOptimJL
using Random
using LineSearches
import ModelingToolkit: Interval, infimum, supremum
using Plots
@parameters x y
@variables u(..)
Dxx = Differential(x)^2
Dyy = Differential(y)^2
# 2D PDE
eq = Dxx(u(x, y)) + Dyy(u(x, y)) ~ -sin(pi * x) * sin(pi * y)
# Boundary conditions
bcs = [u(0, y) ~ 0.0, u(1, y) ~ 0,
u(x, 0) ~ 0.0, u(x, 1) ~ 0]
# Space and time domains
domains = [x ∈ Interval(0.0, 1.0),
y ∈ Interval(0.0, 1.0)]
dim = 2 # number of dimensions
# Fourier features
nFourier = 10
B = π*rand(nFourier,dim)
gammaFunc(x) = [cos.(B*x); sin.(B*x)]
# Neural network
nn = 10
chain = Lux.Chain(gammaFunc, Dense(2*nFourier, nn, Lux.σ), Dense(nn, nn, Lux.σ), Dense(nn, 1))
#strategy = QuadratureTraining(; batch = 200, abstol = 1e-6, reltol = 1e-6)
dx = 0.05
strategy = GridTraining(dx)
discretization = PhysicsInformedNN(chain, strategy)
@named pde_system = PDESystem(eq, bcs, domains, [x, y], [u(x, y)])
prob = discretize(pde_system, discretization)
lossHist = []
iter = 0
callback = function (p, l)
global iter += 1
println("Iter: $iter, Loss: $l")
push!(lossHist, l)
return false
end
# Train
opt = OptimizationOptimJL.LBFGS(linesearch = BackTracking())
res = Optimization.solve(prob, opt; callback = callback, maxiters = 1000)
# Extract
phi = discretization.phi
# Plot
dx = 0.05
xs, ys = [infimum(d.domain):(dx / 10):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],
(length(xs), length(ys)))
u_real = reshape([analytic_sol_func(x, y) for x in xs for y in ys],
(length(xs), length(ys)))
diff_u = abs.(u_predict .- u_real)
p1 = plot(xs, ys, u_real, linetype = :contourf, title = "analytic");
p2 = plot(xs, ys, u_predict, linetype = :contourf, title = "predict");
p3 = plot(xs, ys, diff_u, linetype = :contourf, title = "error");
plot(p1, p2, p3)
savefig("poisson_PINN_u.png")
plot(lossHist, yscale=:log10)
savefig("poisson_PINN_loss.png")