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

Use Difference operator for discrete system #1119

Merged
merged 7 commits into from
Jul 15, 2021

Conversation

sharanry
Copy link
Contributor

@sharanry sharanry commented Jul 13, 2021

#1088 (comment)

TODO:

  • Add utilities which enable handling Difference operators.
  • Add difference function generators
  • Add tests

@sharanry sharanry marked this pull request as draft July 13, 2021 21:15
@sharanry
Copy link
Contributor Author

Current state of discrete function generator:

User side:

@parameters t σ ρ β
D = Difference(t; dt=0.01)
@variables x(t) y(t) z(t)

eqs = [D(x) ~ σ*(y-x),
       D(y) ~ x*-z)-y,
       D(z) ~ x*y - β*z]

de = DiscreteSystem(eqs,t,[x,y,z],[σ,ρ,β])

Function produced:

function (ˍ₋out, ˍ₋arg1, ˍ₋arg2, t)
    let var"x(t)" =  @inbounds(ˍ₋arg1[1]), 
        var"y(t)" =  @inbounds(ˍ₋arg1[2]), 
        var"z(t)" =  @inbounds(ˍ₋arg1[3]), 
        σ =  @inbounds(ˍ₋arg2[1]), 
        ρ =  @inbounds(ˍ₋arg2[2]), 
        β =  @inbounds(ˍ₋arg2[3])
        @inbounds begin
            ˍ₋out[1] = (+)(var"x(t)", (*)(σ, (+)(var"y(t)", (*)(-1, var"x(t)"))))
            ˍ₋out[2] = (*)(var"x(t)", (+)(ρ, (*)(-1, var"z(t)")))
            ˍ₋out[3] = (+)(var"z(t)", (*)(var"x(t)", var"y(t)"), (*)(-1, β, var"z(t)"))
            nothing
        end
    end
end

@sharanry sharanry marked this pull request as ready for review July 14, 2021 15:45
@sharanry
Copy link
Contributor Author

sharanry commented Jul 14, 2021

@ChrisRackauckas @YingboMa The simple SIR test now works fine with Difference operator. Is there any other more complex scenarios I can look at and support or is this sufficient for this initial PR?

@sharanry
Copy link
Contributor Author

sharanry commented Jul 14, 2021

Also, I think currently FunctionMap defaults to dt=1 here. I guess we will have to change that?

@sharanry
Copy link
Contributor Author

One other thing we are assuming right now is that we have a common difference operator on all states with equal timesteps. Do we intend to support multi-timestep processes?

@ChrisRackauckas
Copy link
Member

Also, I think currently FunctionMap defaults to dt=1 here. I guess we will have to change that?

Yes.

f_gen = build_function(rhss, dvs, ps, t; expression=Val{eval_expression}, expression_module=eval_module)
f_oop,f_iip = (@RuntimeGeneratedFunction(eval_module, ex) for ex in f_gen)
f_gen = generate_function(sys; expression=Val{eval_expression}, expression_module=eval_module)
f_oop, _ = (@RuntimeGeneratedFunction(eval_module, ex) for ex in f_gen)
f(u,p,t) = f_oop(u,p,t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there also be an in-place version as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It does get generated. But not sure if we have any use for it in discrete systems as even solvers like FunctionMap uses the not-in-place function (not sure what it is called 😅 ).

Copy link
Member

@YingboMa YingboMa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code copy-pasting seems easily avoidable.

@sharanry
Copy link
Contributor Author

Yup. I will try and refactor the code.

@sharanry sharanry requested a review from YingboMa July 15, 2021 08:30
@sharanry
Copy link
Contributor Author

sharanry commented Jul 15, 2021

@YingboMa @ChrisRackauckas Does the ODESystem assume some kind of ordering of the equations in the differential system? It looks like the generate_function for ODESystem does not take into consideration what the LHS of each equation is. I also noticed this TODO comment.

(This problem is also propagating to DiscreteSystems with/without Difference operator.)

Copy link
Member

@YingboMa YingboMa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@YingboMa
Copy link
Member

Oh, remember to also generalize the vars! function.

@sharanry
Copy link
Contributor Author

sharanry commented Jul 15, 2021

Discrete system doesn't require it. Will add it in the difference operator in ODE system PR on which I have started a discussion here?

@sharanry sharanry changed the title [WIP] Use Difference operator for discrete system Use Difference operator for discrete system Jul 15, 2021
@YingboMa
Copy link
Member

Yes, handling vars! will be required for that issue, but let's do that in a future PR.

@YingboMa YingboMa merged commit c0fe236 into SciML:master Jul 15, 2021
@sharanry sharanry deleted the sy/use_difference_for_discreteSys branch July 15, 2021 15:34
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

Successfully merging this pull request may close these issues.

3 participants