How to make ODESystem with DataInterpolations latex output more readable?

I have defined my model in ModelingToolkit.jl as such:

# define model parameters
@parameters Ci Ria Aw

# define sub-model components
@named C_i = Capacitor(C=Ci)    # thermal capacity of interior
@named R_ia = Resistor(R=Ria)   # thermal resistance between interior and ambient
@named T_a = Voltage()          # ambient temperature
@named P_h = Current()          # thermal power in kW 
@named P_s = Current()          # solar power in kW
@named A_w = Gain(k=Aw)         # dimensionless window area gain (SHGC*Area)
@named gnd = Ground()           # ground node

# input functions
@named T_a_f = TimeVaryingFunction(input_funcs[:T_a])
@named P_h_f = TimeVaryingFunction(input_funcs[:P_h])
@named P_s_f = TimeVaryingFunction(input_funcs[:P_s])

# collect all components in a vector
components = [C_i, R_ia, T_a, P_h, P_s, A_w, T_a_f, P_h_f, P_s_f, gnd]

# make component connections
conn = [
    connect(C_i.p, R_ia.p),
    connect(R_ia.n, T_a.p),
    connect(C_i.n, gnd.g),

    # ambient temperature input
    connect(T_a.p, R_ia.n),
    connect(T_a.V, T_a_f.output),
    connect(T_a.n, gnd.g),

    # solar power input: A_w * P_s
    connect(A_w.output, P_s.I),
    connect(A_w.input, P_s_f.output),
    connect(P_s.n, C_i.p),
    connect(P_s.p, gnd.g),

    # thermal power input
    connect(P_h.n, C_i.n),
    connect(P_h.I, P_h_f.output),
    connect(P_h.p, gnd.g)
]

The input functions are given by DataInterpolations.jl:

# define interpolated input functions of sampled data
T_a_interp = AkimaInterpolation(df.T_a, df.t_hour)
P_s_interp = AkimaInterpolation(df.P_s, df.t_hour)
P_h_interp = AkimaInterpolation(df.P_h, df.t_hour)

input_funcs = Dict(:T_a => T_a_interp, :P_s => P_s_interp, :P_h => P_h_interp)

Now when I display the equations of this model (after calling structural_simplify(sys) I get the following LaTeX output, which is not very readable:

I would like this more readable so I can inspect the equations.

Related question: can I make outputs / inputs such as C_{i_+ v}(t) display something else as well which makes more sense to me? For example, change C_{i_+ v}(t) to V_{C}(t).

ps. I am on ModelingToolkit v8.75 due to requirements of a package I am using.

Thanks a lot :slight_smile:

This is changed a bit in ModelingToolkit v9. With the latest versions, you can have a parameter that is a callable function. This is much better because you can see here that every f call constructs the interpolation and then calls it, while the parameter form will cache it and then reuse it. So the v9 form both makes the printout simpler (just a p(t)) and improves the performance.

That said, you can somewhat simulate it on v8 by building a function with the interpolation enclosed and then registering the enclosed function.

1 Like

Ah okay great. By registering do you mean as it’s done in this post from a while back? Interpolation within ModelingToolkit framework - #6 by jairorua

Kind of, that’s not the enclosed form and will allocate the vectors, though that form will still work.

I see. By enclosed do you mean:

@register f_interpolate(t)

instead of:

@register f_interpolate(t, table_t::AbstractVector, table_u::AbstractVector)

Where the data has already been defined in f_interpolate(t)

Yes exactly.

1 Like

I must still not be doing something right then, this leads to the same result as before:

    # time variable 
    @variables t

    # define interpolated input functions of sampled data
    function T_a(t)
        y = df.T_a
        x = df.t_hour
        int = AkimaInterpolation(y, x)
        out = int(t)
    end

    function P_s(t)
        y = df.P_s
        x = df.t_hour
        int = AkimaInterpolation(y, x)
        out = int(t)
    end

    function P_h(t)
        y = df.P_h
        x = df.t_hour
        int = AkimaInterpolation(y, x)
        out = int(t)
    end

    @eval @register_symbolic P_h(t)
    @eval @register_symbolic P_s(t)
    @eval @register_symbolic T_a(t)

    input_funcs = Dict(:T_a => T_a, :P_s => P_s, :P_h => P_h)

You can’t eval it if it’s in a function since that would be in the wrong world age

1 Like