Skip to content

Commit

Permalink
working nlsys jacobians
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Mar 18, 2018
1 parent 832d692 commit 9665d46
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/SciCompDSL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Base.promote_rule(::Type{T},::Type{T2}) where {T<:Number,T2<:Expression} = Expre
Base.one(::Type{T}) where T<:Expression = Constant(1)
Base.zero(::Type{T}) where T<:Expression = Constant(0)

function caclulate_jacobian end

include("operations.jl")
include("operators.jl")
include("systems/diffeqs/diffeqsystem.jl")
Expand Down
16 changes: 11 additions & 5 deletions src/systems/nonlinear/nonlinear_system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ function generate_nlsys_function(sys::NonlinearSystem)
:((du,u,p)->$(block))
end

function generate_nlsys_jacobian(sys::NonlinearSystem,simplify=true)
var_exprs = [:($(sys.vs[i].name) = u[$i]) for i in 1:length(sys.vs)]
param_exprs = [:($(sys.ps[i].name) = p[$i]) for i in 1:length(sys.ps)]

function calculate_jacobian(sys::NonlinearSystem,simplify=true)
sys_idxs = map(eq->isequal(eq.args[1],Constant(0)),sys.eqs)
sys_eqs = sys.eqs[sys_idxs]
calc_eqs = sys.eqs[.!(sys_idxs)]
sys_exprs = [:($(Symbol("resid[$i]")) = $(sys_eqs[i].args[2])) for i in eachindex(sys_eqs)]
rhs = [eq.args[2] for eq in sys_eqs]

for i in 1:length(calc_eqs)
Expand All @@ -59,5 +55,15 @@ function generate_nlsys_jacobian(sys::NonlinearSystem,simplify=true)
sys_exprs
end

function generate_nlsys_jacobian(sys::NonlinearSystem,simplify=true)
var_exprs = [:($(sys.vs[i].name) = u[$i]) for i in 1:length(sys.vs)]
param_exprs = [:($(sys.ps[i].name) = p[$i]) for i in 1:length(sys.ps)]
jac = calculate_jacobian(sys,simplify)
jac_exprs = [:(J[$i,$j] = $(Expr(jac[i,j]))) for i in 1:size(jac,1), j in 1:size(jac,2)]
exprs = vcat(var_exprs,param_exprs,vec(jac_exprs))
block = expr_arr_to_block(exprs)
:((J,u,p,t)->$(block))
end

export NonlinearSystem
export generate_nlsys_function
2 changes: 1 addition & 1 deletion test/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ eqs = [0 ~ σ*(y-x),
0 ~ x*-z)-y,
0 ~ x*y - β*z]
sys = NonlinearSystem(eqs,[x,y,z],[σ,ρ,β])
jac = SciCompDSL.generate_nlsys_jacobian(sys)
jac = SciCompDSL.calculate_jacobian(sys)
@test jac[1,1] == σ*-1
@test jac[1,2] == σ
@test jac[1,3] == 0
Expand Down
1 change: 1 addition & 0 deletions test/system_construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ eqs = [a ~ y-x,
0 ~ x*y - β*z]
ns = NonlinearSystem(eqs,[x,y,z],[σ,ρ,β])
nlsys_func = SciCompDSL.generate_nlsys_function(ns)
jac = SciCompDSL.calculate_jacobian(ns)
jac = SciCompDSL.generate_nlsys_jacobian(ns)

0 comments on commit 9665d46

Please sign in to comment.