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
Refactor system storage #92
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2d11f50
Clean up constructors
HarrisonGrodin d9bef69
Remove mutability from DiffEqSystem
HarrisonGrodin 91c3b47
Reshape data to mirror implicit invariants
HarrisonGrodin d5ff6cf
Clean up FunctionVersion
HarrisonGrodin ee83816
Remove ability to store multiple independent variables
HarrisonGrodin 3f205d4
Fix test
HarrisonGrodin 7f8896d
Remove unused naming_scheme
HarrisonGrodin 64086d1
Remove mutability from equations
HarrisonGrodin 259a46f
Remove duplicate DiffEq construction
HarrisonGrodin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,80 +1,56 @@ | ||
extract_idv(eq::Equation) = eq.lhs.op.x | ||
extract_idv(eq::DiffEq) = eq.D.x | ||
|
||
function lower_varname(O::Operation, naming_scheme; lower=false) | ||
@assert isa(O.op, Differential) | ||
|
||
D, x = O.op, O.args[1] | ||
function lower_varname(D::Differential, x; lower=false) | ||
order = lower ? D.order-1 : D.order | ||
|
||
lower_varname(x, D.x, order, naming_scheme) | ||
return lower_varname(x, D.x, order) | ||
end | ||
function lower_varname(var::Variable, idv, order::Int, naming_scheme) | ||
function lower_varname(var::Variable, idv, order::Int) | ||
sym = var.name | ||
name = order == 0 ? sym : Symbol(sym, naming_scheme, string(idv.name)^order) | ||
name = order == 0 ? sym : Symbol(sym, :_, string(idv.name)^order) | ||
return Variable(name, var.subtype, var.dependents) | ||
end | ||
|
||
function ode_order_lowering(sys::DiffEqSystem; kwargs...) | ||
eqs = sys.eqs | ||
ivs = sys.ivs | ||
eqs_lowered = ode_order_lowering(eqs; kwargs...) | ||
DiffEqSystem(eqs_lowered, ivs) | ||
function ode_order_lowering(sys::DiffEqSystem) | ||
eqs_lowered = ode_order_lowering(sys.eqs, sys.iv) | ||
DiffEqSystem(eqs_lowered, sys.iv) | ||
end | ||
ode_order_lowering(eqs; naming_scheme = "_") = ode_order_lowering!(deepcopy(eqs), naming_scheme) | ||
function ode_order_lowering!(eqs, naming_scheme) | ||
idv = extract_idv(eqs[1]) | ||
D = Differential(idv, 1) | ||
function ode_order_lowering(eqs, iv) | ||
D = Differential(iv, 1) | ||
var_order = Dict{Variable,Int}() | ||
vars = Variable[] | ||
dv_name = eqs[1].lhs.args[1].subtype | ||
new_eqs = similar(eqs, DiffEq) | ||
|
||
for eq in eqs | ||
for (i, eq) ∈ enumerate(eqs) | ||
var, maxorder = extract_var_order(eq) | ||
maxorder == 1 && continue # fast pass | ||
if maxorder > get(var_order, var, 0) | ||
var_order[var] = maxorder | ||
var ∈ vars || push!(vars, var) | ||
end | ||
lhs_renaming!(eq, D, naming_scheme) | ||
rhs_renaming!(eq, naming_scheme) | ||
var′ = lower_varname(eq.D, eq.var, lower = true) | ||
rhs′ = rename(eq.rhs) | ||
new_eqs[i] = DiffEq(D, var′, rhs′) | ||
end | ||
|
||
for var ∈ vars | ||
order = var_order[var] | ||
for o in (order-1):-1:1 | ||
lhs = D(lower_varname(var, idv, o-1, naming_scheme)) | ||
rhs = lower_varname(var, idv, o, naming_scheme) | ||
eq = Equation(lhs, rhs) | ||
push!(eqs, eq) | ||
lvar = lower_varname(var, iv, o-1) | ||
rhs = lower_varname(var, iv, o) | ||
eq = DiffEq(D, lvar, rhs) | ||
push!(new_eqs, eq) | ||
end | ||
end | ||
|
||
return eqs | ||
return new_eqs | ||
end | ||
|
||
function lhs_renaming!(eq, D, naming_scheme) | ||
eq.lhs = D(lower_varname(eq.lhs, naming_scheme, lower=true)) | ||
return eq | ||
function rename(O::Expression) | ||
isa(O, Operation) || return O | ||
isa(O.op, Differential) && return lower_varname(O.op, O.args[1]) | ||
return Operation(O.op, rename.(O.args)) | ||
end | ||
rhs_renaming!(eq, naming_scheme) = _rec_renaming!(eq.rhs, naming_scheme) | ||
|
||
function _rec_renaming!(rhs, naming_scheme) | ||
isa(rhs, Operation) && isa(rhs.op, Differential) && return lower_varname(rhs, naming_scheme) | ||
if rhs isa Operation | ||
args = rhs.args | ||
for i in eachindex(args) | ||
args[i] = _rec_renaming!(args[i], naming_scheme) | ||
end | ||
end | ||
rhs | ||
end | ||
|
||
function extract_var_order(eq) | ||
# We assume that the differential with the highest order is always going to be in the LHS | ||
dv = eq.lhs | ||
var = dv.args[1] | ||
order = dv.op.order | ||
return (var, order) | ||
end | ||
extract_var_order(eq::DiffEq) = (eq.var, eq.D.order) | ||
|
||
export ode_order_lowering |
This file contains 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
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.
Rename this ODE system, since something like a PDE system would have two independent variables.