-
-
Notifications
You must be signed in to change notification settings - Fork 8
B/polynomialize quadratize reduce #149
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
nekris301
wants to merge
18
commits into
SciML:main
Choose a base branch
from
nekris301:b/polynomialize_quadratize_reduce
base: main
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
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
55a6a61
Create Polynomialization.jl
nekris301 64ee20d
Create Quadratize.jl
nekris301 34d9e1f
Rename src/Polynomialization.jl to src/PolynomializeQuadratizeReduce/…
nekris301 8877217
Adding Galerkin Reduction Code
nekris301 7b4cf34
Removing bad import
nekris301 136be92
Add polynomialize quadratize reduce workflow test
0d3e6fb
Update README.md
nekris301 8eaaffd
Adding Credits to Polynomialization.jl
nekris301 2dcd754
Adding credits to Quadratization.jl
nekris301 7cd1c61
Merge branch 'main' into b/polynomialize_quadratize_reduce
bowenszhu 4650d35
Remove accidentally committed .DS_Store files
bowenszhu 93d152b
Replace SymbolicUtils.istree with iscall
bowenszhu 9823452
Use isnothing for nothing comparisons in Polynomialization
bowenszhu 6eb09b4
Hoist iscall/operation imports to the main module
bowenszhu c76232f
Apply Runic formatting to test file
bowenszhu bf530b5
removing OrdinaryDiffEq dependency
214356f
Delete src/PolynomializeQuadratizeReduce/PolynomializeQuadratizeReduc…
nekris301 a6805af
Fixing formatting and documentation
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 |
|---|---|---|
|
|
@@ -23,4 +23,8 @@ docs/site/ | |
| # environment. | ||
| Manifest.toml | ||
|
|
||
| .vscode | ||
| .vscode | ||
|
|
||
| # macOS metadata | ||
| .DS_Store | ||
| **/.DS_Store | ||
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
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,155 @@ | ||
| function differentiated_variable(eq) | ||
| lhs = unwrap(eq.lhs) | ||
| args = arguments(lhs) | ||
| return Num(args[1]) | ||
| end | ||
|
|
||
| function ordered_rhs(sys) | ||
| eqs = equations(sys) | ||
| xs = unknowns(sys) | ||
|
|
||
| rhs_by_var = Dict{Any, Any}() | ||
|
|
||
| for eq in eqs | ||
| x = differentiated_variable(eq) | ||
| rhs_by_var[x] = eq.rhs | ||
| rhs_by_var[unwrap(x)] = eq.rhs | ||
| end | ||
|
|
||
| return [rhs_by_var[x] for x in xs] | ||
| end | ||
|
|
||
| """ | ||
| galerkin_project_system_affine(sys, V, xbar, a_vars; pmap=Dict(), name=:rom) | ||
|
|
||
| Project an explicit ODE system onto the affine trial space | ||
|
|
||
| x(t) ≈ xbar + V*a(t). | ||
|
|
||
| Here `x(t)` is the full state vector of `sys`, `xbar` is a fixed offset vector, | ||
| `V` is an `n × r` basis matrix, and `a(t)` is the reduced state vector with | ||
| entries `a_vars`. | ||
|
|
||
| The reduced system is constructed by direct Galerkin projection: | ||
|
|
||
| a'(t) = V' * f(xbar + V*a(t)), | ||
|
|
||
| where `f` is the right-hand side of the full system. The returned system also | ||
| contains observed equations reconstructing the full state variables as | ||
|
|
||
| x_i(t) ~ xbar[i] + sum(V[i, α] * a_α(t) for α in 1:r). | ||
|
|
||
| Arguments: | ||
| - `sys`: ModelingToolkit ODE system. | ||
| - `V`: projection basis of size `(n, r)`, where `n = length(unknowns(sys))`. | ||
| - `xbar`: affine offset vector of length `n`. | ||
| - `a_vars`: reduced state variables of length `r`. | ||
|
|
||
| Keywords: | ||
| - `pmap`: optional parameter substitutions applied before projection. | ||
| - `name`: name of the returned reduced system. | ||
|
|
||
| Returns: | ||
| - A ModelingToolkit `System` for the affine Galerkin reduced-order model. | ||
| """ | ||
| function galerkin_project_system_affine(sys, V, xbar, a_vars; pmap = Dict(), name = :rom) | ||
| xs = unknowns(sys) | ||
|
|
||
| n = length(xs) | ||
| n == size(V, 1) || error("V must have size (n,r), where n = length(unknowns(sys))") | ||
| length(xbar) == n || error("xbar must have length n") | ||
| r = size(V, 2) | ||
| length(a_vars) == r || error("length(a_vars) must equal size(V,2)") | ||
|
|
||
| iv = ModelingToolkit.get_iv(sys) | ||
| Dred = Differential(iv) | ||
|
|
||
| rhs = unwrap.(ordered_rhs(sys)) | ||
|
|
||
| if !isempty(pmap) | ||
| subdict = Dict(Symbolics.unwrap(k) => v for (k, v) in pmap) | ||
| rhs = Symbolics.substitute.(rhs, Ref(subdict)) | ||
| end | ||
|
|
||
| x_subs = Dict{Any, Any}() | ||
|
|
||
| for i in 1:n | ||
| rec = xbar[i] | ||
|
|
||
| for α in 1:r | ||
| rec += V[i, α] * a_vars[α] | ||
| end | ||
|
|
||
| x_subs[xs[i]] = rec | ||
| x_subs[unwrap(xs[i])] = rec | ||
| end | ||
|
|
||
| f_affine = [ | ||
| Symbolics.substitute(rhs[i], x_subs) | ||
| for i in 1:n | ||
| ] | ||
|
|
||
| rhs_red = Vector{Any}(undef, r) | ||
|
|
||
| for α in 1:r | ||
| expr = zero(Num) | ||
|
|
||
| for i in 1:n | ||
| expr += V[i, α] * f_affine[i] | ||
| end | ||
|
|
||
| rhs_red[α] = Symbolics.simplify(Symbolics.expand(expr)) | ||
| end | ||
|
|
||
| eqs_red = [Dred(a_vars[α]) ~ rhs_red[α] for α in 1:r] | ||
|
|
||
| obs = Vector{Equation}(undef, n) | ||
|
|
||
| for i in 1:n | ||
| rec = xbar[i] | ||
|
|
||
| for α in 1:r | ||
| rec += V[i, α] * a_vars[α] | ||
| end | ||
|
|
||
| obs[i] = xs[i] ~ Symbolics.simplify(rec) | ||
| end | ||
|
|
||
| return System(eqs_red, iv; observed = obs, name = name) | ||
| end | ||
|
|
||
| """ | ||
| galerkin_project_system(sys, V, a_vars; pmap=Dict(), name=:rom) | ||
|
|
||
| Project an explicit ODE system onto the linear trial space | ||
|
|
||
| x(t) ≈ V*a(t). | ||
|
|
||
| This is the zero-offset special case of `galerkin_project_system_affine`, namely | ||
| `xbar = zeros(n)`, where `n = length(unknowns(sys))`. | ||
|
|
||
| Arguments: | ||
| - `sys`: ModelingToolkit ODE system. | ||
| - `V`: projection basis of size `(n, r)`. | ||
| - `a_vars`: reduced state variables of length `r`. | ||
|
|
||
| Keywords: | ||
| - `pmap`: optional parameter substitutions applied before projection. | ||
| - `name`: name of the returned reduced system. | ||
|
|
||
| Returns: | ||
| - A ModelingToolkit `System` for the linear Galerkin reduced-order model. | ||
| """ | ||
| function galerkin_project_system(sys, V, a_vars; pmap = Dict(), name = :rom) | ||
| n = length(unknowns(sys)) | ||
| xbar = zeros(Float64, n) | ||
|
|
||
| return galerkin_project_system_affine( | ||
| sys, | ||
| V, | ||
| xbar, | ||
| a_vars; | ||
| pmap = pmap, | ||
| name = name, | ||
| ) | ||
| end |
Oops, something went wrong.
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.
OrdinaryDiffEqshould not be a runtime dependency ofModelOrderReduction.jl.The package's mandate is model transformation.
polynomialize,quadratize, and the Galerkin projection helpers are all pure symbolic rewrites that don't touch a solver. The newpolynomialize_quadratize_reduce(sys, u0, tspan, nmodes; ...)wrapper, however, bundles five things into one call:polynomializeandquadratize,ODEProblem+solveto collect snapshots of the lifted system,solveof the ROM.Only step 1 and step 4 belong in this package. Steps 2, 3, and 5 are user-owned simulation / data choices (which solver, which
tspan, which snapshot policy, whether to use POD vs. TSVD vs. RSVD, etc.). The package already exposesPOD/TSVD/RSVDfor step 3, and the existingdeimAPI is a precedent for leaving the simulation step to the caller.