-
-
Notifications
You must be signed in to change notification settings - Fork 241
Gsoc symbolic parser mvp #1040
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
Open
ajatshatru01
wants to merge
2
commits into
SciML:master
Choose a base branch
from
ajatshatru01:gsoc-symbolic-parser-mvp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Gsoc symbolic parser mvp #1040
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Compact symbolic PINN loss template | ||
| # NN_j(...) are registered neural outputs (not expanded layer-by-layer). | ||
|
|
||
| PDE residual terms: | ||
| (Differential(t, 1)(NN_1(t, x)) + Differential(x, 1)(NN_1(t, x)))^2 | ||
|
|
||
| Boundary residual terms: | ||
| (NN_1(t, x) - sin(6.283185307179586x))^2 + (NN_1(t, x) - sin(-6.283185307179586t))^2 + (-sin(6.283185307179586(1.0 - t)) + NN_1(t, x))^2 | ||
|
|
||
| Loss template: | ||
| sum_over_10_point_grid(PDE residual terms) + 15.0 * (Boundary residual terms) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import Pkg | ||
| Pkg.activate(temp=true) | ||
| Pkg.develop(path=".") | ||
| Pkg.add(["ModelingToolkit", "Lux", "DomainSets", "Optimization", "OptimizationOptimisers", "ComponentArrays", "Plots", "ModelingToolkitNeuralNets"]) | ||
|
|
||
| using NeuralPDE | ||
| using ModelingToolkit | ||
| using DomainSets | ||
| using ComponentArrays | ||
| using Optimization | ||
| using OptimizationOptimisers | ||
| using Lux | ||
| using Plots | ||
| using Random | ||
|
|
||
| Random.seed!(42) | ||
|
|
||
| println("==== Symbolic PINN Parser Demo (MVP) ====") | ||
|
|
||
| # Advection Equation MVP problem: | ||
| # ∂u/∂t + 1.0 * ∂u/∂x = 0 | ||
| # u(0, x) = sin(2pi * x) | ||
| # True solution: u(t, x) = sin(2pi * (x - t)) | ||
|
|
||
| @parameters t x | ||
| @variables u(..) | ||
| Dt = Differential(t) | ||
| Dx = Differential(x) | ||
|
|
||
| eq = Dt(u(t, x)) + 1.0 * Dx(u(t, x)) ~ 0 | ||
|
|
||
| bcs = [ | ||
| u(0.0, x) ~ sin(2pi * x), | ||
| u(t, 0.0) ~ sin(-2pi * t), | ||
| u(t, 1.0) ~ sin(2pi * (1.0 - t)) | ||
| ] | ||
|
|
||
| domains = [ | ||
| t ∈ Interval(0.0, 1.0), | ||
| x ∈ Interval(0.0, 1.0) | ||
| ] | ||
|
|
||
| @named pde_system = PDESystem(eq, bcs, domains, [t, x], [u(t, x)]) | ||
|
|
||
| # Use the newly ported MVP parser | ||
| loss_func, p_init, chain, st = build_pinn_loss( | ||
| pde_system; | ||
| width=6, | ||
| depth=1, | ||
| activation=tanh, | ||
| n_points=10, | ||
| bc_weight=15.0, | ||
| symbolic_expression_path="demo_symbolic_expression.txt" | ||
| ) | ||
|
|
||
| # Convert to Optimization problem | ||
| obj = OptimizationFunction((theta, _) -> loss_func(theta), Optimization.AutoForwardDiff()) | ||
| prob = OptimizationProblem(obj, collect(p_init), nothing) | ||
|
|
||
| println("Starting training for Advection Equation via Symbolic Loss Function...") | ||
| res = Optimization.solve(prob, OptimizationOptimisers.Adam(0.01), maxiters=1500) | ||
| println("Training complete. Final objective = ", res.objective) | ||
|
|
||
| # Evaluate and print comparison | ||
| theta_final = ComponentArray(res.u, getaxes(p_init)) | ||
|
|
||
| xs = collect(range(0.0, 1.0, length=10)) | ||
| u_pred = [first(Lux.apply(chain, [0.5, xi], theta_final, st)[1]) for xi in xs] | ||
| u_true = [sin(2pi * (xi - 0.5)) for xi in xs] | ||
|
|
||
| println("\n=== Results ===") | ||
| println("Predictions: ", round.(u_pred, digits=4)) | ||
| println("Exact: ", round.(u_true, digits=4)) | ||
| println("Max error: ", round(maximum(abs.(u_pred .- u_true)), digits=4)) | ||
| println("\nSymbolic PINN parser executed successfully!\n") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the changes from the other branch are being caught here