# MethodOfLines.jl: Obtain Jacobian of MOL semi-discretization

**URL:** <https://discourse.julialang.org/t/methodoflines-jl-obtain-jacobian-of-mol-semi-discretization/92681>\
**Category:** Modelling & Simulations\
**Created:** [January 8, 2023, 5:21pm UTC](https://discourse.julialang.org/t/methodoflines-jl-obtain-jacobian-of-mol-semi-discretization/92681 "2023-01-08T17:21:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)\
**Post date:** [January 8, 2023, 5:21pm UTC](https://discourse.julialang.org/t/methodoflines-jl-obtain-jacobian-of-mol-semi-discretization/92681/1 "2023-01-08T17:21:54Z")

</div>

Consider an exemplary PDE with temporal & spatial derivatives

\partial\_t u(t, x) + D\_x f\big(u(t, x)\big) = 0

Where D\_x is a general differential operator, like the Laplacian and f is some function.

I can use [MethodOfLines.jl](https://docs.sciml.ai/MethodOfLines/stable/) to obtain a semi-discretization  
U(t) = F\big(U(t)\big)  
through invoking `discretize(sys::PDESystem, disc::D)` which gives a `AbstractSciMLProblem`. Is it possible to call a routine from one of the other SiML packages to obtain the Jacobian  
 J(U) = \frac{\partial F\big(U(t)\big)}{\partial U} ?  
Ideally, this would be possible via algorithmic/automated differentiation, although Finite DIfferences would also be a suitable starting point.

---

<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:** [January 8, 2023, 5:54pm UTC](https://discourse.julialang.org/t/methodoflines-jl-obtain-jacobian-of-mol-semi-discretization/92681/2 "2023-01-08T17:54:22Z")

</div>

During `discretize` if you pass `jac=true` then it will generate it, and `sys.jac[]` or `prob.f.jac` are these pieces. @xtalax this seems to be missing in the tutorials?

---

<div class="post-metadata">

**Author:** ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)\
**Post date:** [January 8, 2023, 6:21pm UTC](https://discourse.julialang.org/t/methodoflines-jl-obtain-jacobian-of-mol-semi-discretization/92681/3 "2023-01-08T18:21:57Z")

</div>

That is working, thanks!

`prob.f.jac` returns

`(::ModelingToolkit.var"#_jac#491"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x35fe4ab8, 0xfab08b81, 0x0b2eb902, 0x8f534a9e, 0xca1ca74d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x00fc76c7, 0xcbe1a643, 0x66f66510, 0x06b3096d, 0xc25da162)}}) (generic function with 2 methods)`

Now I would like to evaluate that thing to obtain an actual matrix. I guess one of the arguments `arg1, arg2` is the point of evaluation U^\* and `t` is probably time t if the Jacobian is time-dependent, i.e., J\big(t, U(t) \big).  
Question is what `arg1, arg2` are - I will see if I can figure this out via the `ModelingToolkit` documentation.

And yeah, in the `MethodOfLines.jl` repo you [do not really find use cases for `jac`](https://github.com/SciML/MethodOfLines.jl/search?q=jac).

---

<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:** [January 9, 2023, 5:07am UTC](https://discourse.julialang.org/t/methodoflines-jl-obtain-jacobian-of-mol-semi-discretization/92681/4 "2023-01-09T05:07:15Z")

</div>

> [@DanDoe](#):
>
> `prob.f.jac` returns
> 
> `(::ModelingToolkit.var"#_jac#491"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x35fe4ab8, 0xfab08b81, 0x0b2eb902, 0x8f534a9e, 0xca1ca74d)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x00fc76c7, 0xcbe1a643, 0x66f66510, 0x06b3096d, 0xc25da162)}}) (generic function with 2 methods)`
> 
> Now I would like to evaluate that thing to obtain an actual matrix. I guess one of the arguments `arg1, arg2` is the point of evaluation U^\* U∗U^\* and `t` is probably time t tt if the Jacobian is time-dependent, i.e., J\big(t, U(t) \big) J(t,U(t))J\big(t, U(t) \big) .

It’s just the function for DiffEq, so it matches what’s described in the docs.

> **[ODE Problems · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/types/ode_types/#SciMLBase.ODEFunction)**
>
> Documentation for DifferentialEquations.jl.

And because it’s an RGF, you can check its definition via `prob.f.jac.body`.

---

<div class="post-metadata">

**Author:** ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)\
**Post date:** [January 10, 2023, 9:46am UTC](https://discourse.julialang.org/t/methodoflines-jl-obtain-jacobian-of-mol-semi-discretization/92681/5 "2023-01-10T09:46:54Z")

</div>

Alright, got it!

One thing I noted is that the evaluation of the jacobian is extremely slow, probably too slow for practical usage.

Although not important for me, `prob.f.jac.body` returns

```julia
ERROR: type #_jac#491 has no field body
Stacktrace:

 [1] getproperty(x::Function, f::Symbol)
   @ Base ./Base.jl:38

```

---

<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:** [January 10, 2023, 1:41pm UTC](https://discourse.julialang.org/t/methodoflines-jl-obtain-jacobian-of-mol-semi-discretization/92681/6 "2023-01-10T13:41:40Z")

</div>

> [@DanDoe](#):
>
> One thing I noted is that the evaluation of the jacobian is extremely slow, probably too slow for practical usage.

Its evaluation isn’t slow. It just had bad compilation scaling, which we are working on.
