# Argument "tgrad" in ODEFunction appears to have no influence. Correct?

**URL:** https://discourse.julialang.org/t/argument-tgrad-in-odefunction-appears-to-have-no-influence-correct/69236
**Category:** Numerics
**Tags:** ode, modelingtoolkit, differentialequation
**Created:** [October 5, 2021, 2:10pm UTC](https://discourse.julialang.org/t/argument-tgrad-in-odefunction-appears-to-have-no-influence-correct/69236 "2021-10-05T14:10:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Andrey](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Andrey](https://discourse.julialang.org/u/Andrey)
#### Post date: [October 5, 2021, 2:10pm UTC](https://discourse.julialang.org/t/argument-tgrad-in-odefunction-appears-to-have-no-influence-correct/69236/1 "2021-10-05T14:10:25Z")

</div>

[Question replicated from _DifferentialEquations Lobby_ at [JuliaDiffEq/Lobby - Gitter](https://gitter.im/JuliaDiffEq/Lobby#) ]

Hi All. I have got a question to the “DifferentialEquations” and “ModelingToolbox” Pkg developers, probably the right addressees are: @ChrisRackauckas and @YingboMa .

It concerns argument “tgrad” which according to documentation is “The gradient of the differential equation with respect to t at state u with parameters p”. The argument “tgrad” is used in function ODEFunction in package “DifferentialEquations” and also it is used in System/Problem Constructors DiffEqBase.ODEFunction and DiffEqBase.ODEProblem in “ModelingToolkit”. In the latter the “tgrad” is defined only after “calculate\_tgrad” is called on the system.

**Question #1** : **Does “tgrad” argument in both use cases have any impact on the ODE solver?** Probably not, since these directional row-vectors are already in Jacobian. Last time “tgrad” argument is used in “DifferentialEquation” Pkg is in call of function DiffEqBase.\_\_init (line 4 of _solve.jl_) as part of kwargs structure but it has no influence on the output structure “integrator” which then passed to the solver. Code extract is below.

```julia
function DiffEqBase.__solve(prob::Union{DiffEqBase.AbstractODEProblem,DiffEqBase.AbstractDAEProblem},
                            alg::Union{OrdinaryDiffEqAlgorithm,DAEAlgorithm}, args...;
                            kwargs...)
  integrator = DiffEqBase.__init(prob, alg, args...; kwargs...)
  solve!(integrator)
  integrator.sol
end

```

**Question #2:**  **Do you have any examples of using “tgrad” for “ModelingToolkit” scenario?** The comments in lines 5-7 of _abstractodesystem.jl_ (“_…we need to remove explicit time dependence on the state because when we have `u(t) * t` we want to …_”) indicates a particular purpose, however the context is not really clear. Do you have any links to lectures/info pages which clarify that?  
Link and code extract of _abstractodesystem.jl_ are below:  
[https://github.com/SciML/ModelingToolkit.jl/blob/master/src/systems/diffeqs/abstractodesystem.jl](https://github.com/SciML/ModelingToolkit.jl/blob/master/src/systems/diffeqs/abstractodesystem.jl)

```julia
function calculate_tgrad(sys::AbstractODESystem;
                         simplify=false)
  isempty(get_tgrad(sys)[]) || return get_tgrad(sys)[] # use cached tgrad, if possible

  # We need to remove explicit time dependence on the state because when we
  # have `u(t) * t` we want to have the tgrad to be `u(t)` instead of `u'(t) *
  # t + u(t)`.
  rhs = [detime_dvs(eq.rhs) for eq ∈ equations(sys)]
  iv = get_iv(sys)

```

Thanks!

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [October 5, 2021, 2:19pm UTC](https://discourse.julialang.org/t/argument-tgrad-in-odefunction-appears-to-have-no-influence-correct/69236/2 "2021-10-05T14:19:28Z")

</div>

> [@Andrey](#):
>
> Question #1: Does “tgrad” argument in both use cases have any impact on the ODE solver? Probably not, since these directional row-vectors are already in Jacobian. Last time “tgrad” argument is used in “DifferentialEquation” Pkg is in call of function DiffEqBase.\_\_init (line 4 of _solve.jl_ ) but it has no influence on the output structure “integrator” which then passed to the solver function.

Rosenbrock methods explicitly use this value, so it can effect the performance there. That then describes its purpose: make Rodas4 etc. a little bit faster in some cases.

---

<div class="post-metadata">

### Author: ![Andrey](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Andrey](https://discourse.julialang.org/u/Andrey)
#### Post date: [October 5, 2021, 2:53pm UTC](https://discourse.julialang.org/t/argument-tgrad-in-odefunction-appears-to-have-no-influence-correct/69236/3 "2021-10-05T14:53:44Z")

</div>

Chris, thanks for the answer.

Do you have a pseudocode for Rodas4? Would like to see what changes in solver when tgrad is true or is false.

If i am not mistaken, in case there is an influence of _tgrad_ argument then i assume there should be some property transfer from _kwargs_ structure to _integrator_ structure in function DiffEqBase.\_\_init (line 4 of _solve.jl_ - this is in DifferentialEquation Pkg source files), which i could not really find. Maybe i am looking at the wrong function.
