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

Cannot import ModelingToolkit: Interval, infimum, supremum #319

Closed
finmod opened this issue Jun 9, 2021 · 15 comments
Closed

Cannot import ModelingToolkit: Interval, infimum, supremum #319

finmod opened this issue Jun 9, 2021 · 15 comments

Comments

@finmod
Copy link

finmod commented Jun 9, 2021

Loading the packages to run the updated PINNs example

using NeuralPDE, Flux, ModelingToolkit, GalacticOptim, Optim, DiffEqFlux
import ModelingToolkit: Interval, infimum, supremum

gives:

WARNING: could not import ModelingToolkit.Interval into Main
WARNING: could not import ModelingToolkit.infimum into Main
WARNING: could not import ModelingToolkit.supremum into Main

@ChrisRackauckas
Copy link
Member

Remove import ModelingToolkit: Interval, infimum, supremum

@finmod
Copy link
Author

finmod commented Jun 9, 2021

then running the next Julia cell:

@parameters x
@variables p(..)
Dx = Differential(x)
Dxx = Differential(x)^2

α = 0.3
β = 0.5
_σ = 0.5
# Discretization
dx = 0.05
# here we use normalization condition: dx*p(x) ~ 1, in order to get non-zero solution.
#(α - 3*β*x^2)*p(x) + (α*x - β*x^3)*Dx(p(x)) ~ (_σ^2/2)*Dxx(p(x))
eq  = Dx((α*x - β*x^3)*p(x)) ~ (_σ^2/2)*Dxx(p(x))+dx*p(x) - 1.

# Initial and boundary conditions
bcs = [p(-2.2) ~ 0. ,p(2.2) ~ 0. , p(-2.2) ~ p(2.2)]

# Space and time domains
domains = [x ∈ Interval(-2.2,2.2)]

# Neural network
chain = FastChain(FastDense(1,16,Flux.σ),FastDense(16,16,Flux.σ),FastDense(16,1))

discretization = PhysicsInformedNN(chain,GridTraining(dx))

pde_system = PDESystem(eq,bcs,domains,[x],[p])
prob = discretize(pde_system,discretization)

#Callback function
cb = function (p,l)
    println("Current loss is: $l")
    return false
end

res = GalacticOptim.solve(prob,ADAM(0.1); cb = cb, maxiters=1500)
prob = remake(prob,u0=res.minimizer)
res = GalacticOptim.solve(prob,Optim.BFGS(initial_stepnorm=0.01);allow_f_increases=true, cb = cb, maxiters=400)

dx = dx/5
discretization = remake(discretization; strategy = NeuralPDE.GridTraining(dx), init_params =res.minimizer)
prob = NeuralPDE.discretize(pde_system,discretization)

res = GalacticOptim.solve(prob,ADAM(0.01); cb = cb, maxiters=1000)
prob = remake(prob,u0=res.minimizer)
res = GalacticOptim.solve(prob,Optim.BFGS(initial_stepnorm=0.01);allow_f_increases=true, cb = cb, maxiters=400)


dx= dx/5
discretization = remake(discretization; strategy = NeuralPDE.GridTraining(dx), init_params =res.minimizer)
prob = NeuralPDE.discretize(pde_system,discretization)

res = GalacticOptim.solve(prob,Optim.BFGS(initial_stepnorm=0.01);allow_f_increases=true, cb = cb, maxiters=400)
prob = remake(prob,u0=res.minimizer)
res = GalacticOptim.solve(prob,ADAM(0.001); cb = cb, maxiters=1000)

phi = discretization.phi

you get:

UndefVarError: Interval not defined

Stacktrace:
[1] top-level scope
@ In[3]:19
[2] eval
@ .\boot.jl:360 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1094

@ChrisRackauckas
Copy link
Member

Then you're not on the latest versions. This is a very recent change.

@finmod
Copy link
Author

finmod commented Jun 9, 2021

NeuralPDE v3.8.0 and ModelingToolkit v5.9.1

@ChrisRackauckas
Copy link
Member

And Symbolics.jl?

@ChrisRackauckas
Copy link
Member

NeuralPDE is v3.11.1.

@finmod
Copy link
Author

finmod commented Jun 9, 2021

Installing Symbolics.jl was incompatible with StructuralIdentifiability and SIAN. I removed them and updated. Now NeuralPDE is updating to v3.11.1 and ModelingToolkit to v5.18.1. This should do it.

@ChrisRackauckas
Copy link
Member

Yup that should do it.

@finmod
Copy link
Author

finmod commented Jun 9, 2021

But you have to put import ModelingToolkit: Interval, infimum, supremum back in.

@ChrisRackauckas
Copy link
Member

That's bad. Those should be exported.

@ChrisRackauckas
Copy link
Member

what where would you need those functions?

@finmod
Copy link
Author

finmod commented Jun 9, 2021

In the PINNs FP example as well as in most others, there are several instances of these functions. I agree that the import should disappear.

# Space and time domains
domains = [x ∈ Interval(-2.2,2.2)]

and in here:

analytic_sol_func(x) = 28.022*exp((1/(2*_σ^2))*(2*α*x^2 - β*x^4))

xs = [infimum(d.domain):dx:supremum(d.domain) for d in domains][1]
u_real  = [analytic_sol_func(x) for x in xs]
u_predict  = [first(phi(x,res.minimizer)) for x in xs]

plot(xs ,u_real, label = "analytic")
plot!(xs ,u_predict, label = "predict")

@ChrisRackauckas
Copy link
Member

I think we should just change the tutorial

@ChrisRackauckas
Copy link
Member

xmin = -2.2
xmax = 2.2
domains = [x ∈ Interval(xmin,xmax)]

etc. Much nicer down the line. Requires less (unnecessary) machinery.

@ChrisRackauckas
Copy link
Member

Version issue, fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants