Modia.jl icon indicating copy to clipboard operation
Modia.jl copied to clipboard

Mixing Differential and Algebraic Equations in variable array

Open tpdsantos opened this issue 7 years ago • 4 comments

I was wondering how could I implement algebraic and differential equations in the same array of variables, since der() does not support slices. For instance, if u = Float(start=zeros(5)), how could I write differential equations for u[[2,4]] and algebraic equations for u[[1,3,5]] ?

Thanks in advance

tpdsantos avatar Sep 04 '18 13:09 tpdsantos

I added an example TestSpatialDiscretization2 showing how this can be achieved by introducing special vectors and splicing them (something went wrong so I couldn't push so I included the model here):

splice(uodd, ueven) = [if isodd(i); uodd[div(i,2)+1] else ueven[div(i,2)] end for i in 1:length(ueven)+length(uodd)] 

@model SpatialDiscretization2 begin
    u = Var(size=(5,))
    ueven = Var(start=[1,2])
    uodd = Var(size=(3,))
    @equations begin
      u = splice(uodd, ueven)
      der(ueven) = - u[[2,4]] + u[[1,3]]
      uodd = ones(3)*time
    end
end

result = simulate(SpatialDiscretization2, 1, logTranslation=true, storeEliminated=false)
plot(result, "ueven")

HildingElmqvist avatar Sep 05 '18 21:09 HildingElmqvist

Thank you for the response but I already found an alternative, by multiplying a decision vector to the derivative, I forgot to update the issue :) By the way, the solver already supports banded jacobians or deals with sparsity? I found the simulations quite slow, with an order of magnitude similar to the dense IDA solver from DifferentialEquations...

tpdsantos avatar Sep 05 '18 23:09 tpdsantos

Please provide your solution to the issue for the record.

No, banded or sparse Jacobian is currently not supported.

Would you give more details about the slow model in order that we can investigate.

HildingElmqvist avatar Sep 06 '18 09:09 HildingElmqvist

Regarding my solution, I just applied the mass matrix form for DAE,

using Modia
@model DAE begin
    T = Var(start=zeros(5))
    @equations begin
        broadcast(*,[0,1,0,1,0],der(T)) = var_treatment(T)
    end
end

The function var_treatment determines the equations that define each variable, being them equal to zero or to der(T).

About the slow model, It's too complex to show here because I use some old modules I created, but with an array variable of length 41 it takes around 3 seconds. With an array variable of length 321 it takes around 2minutes. These are my specs:

julia> versioninfo()
Julia Version 0.6.2
Commit d386e40c17 (2017-12-13 18:08 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-4712MQ CPU @ 2.30GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

I previously used the DifferentialEquations package using the IDA solver from the Sundials package. Using a dense linear solver the time spent was practically the same, 3 seconds for 41 variables and 2 minutes for 321 variables.

If you need more information, I can provide a MWE.

tpdsantos avatar Sep 06 '18 11:09 tpdsantos