# How to implement a lookup-table in ModellingToolkit

Hello,

I want to implement a wind turbine model using ModelingToolkit.

The core model consists of only three differential equations,
but it includes a lookup-table for the power coefficient.

What is the best way to include a lookup table in ModelingToolkit?

I currently use the following function:

``````using Dierckx, Plots

Cp=[0.024384377390186646,0.03811829845979822,0.05731175742000688,0.0803120051369546,0.10526323380995135,0.1312349486430945,
0.1588590278697544,0.1882995422205645,0.21937359147848456,0.25269290703920616,0.2882162631147021,0.3240355349501659,
0.3607290350346774,0.39295261085207084,0.4141821251393675,0.4274331262027119,0.43636422894857274,0.4414767530303278,
0.443412806515125,0.44365385445693295,0.44317448744425714,0.44218641629727234,0.4407405317795181,0.43888039054963845,
0.4362875461540461,0.4325702155989231,0.4278606704093836,0.4224263790651815,0.4164272616252002,0.40987027258773945,
0.4027832291503407,0.39518785578723936,0.38719847687832065]
TSR = 2.0:0.25:10.0

const cp = Spline1D(TSR, Cp)

plot(TSR, cp(TSR), label="Cp", xlabel="TSR")
``````

Can I use this in ModellingToolkit, or do I need a version of the function
that works with dual numbers?

This spline interpolation works only with Float64 numbers.

OK, I tried it and and I get:

ERROR: LoadError: MethodError: no method matching Float64(::Num)

from the line:

``````eqs = [    Pr           ~ 0.5 * ρ * A * U^3 * cp(λ)]
``````

Which function can I use instead of Spline1D that accepts the type ::Num ?

I tried BSplineKit which fails with a different error:

``````ERROR: LoadError: TypeError: non-boolean (Num) used in boolean context
Stacktrace:
[1] find_knot_interval(ts::Vector{Float64}, x::Num, ::Nothing)
@ BSplineKit.BSplines C:\Users\uwefechner\.julia\packages\BSplineKit\FOLdw\src\BSplines\evaluate_all.jl:99
[2] find_knot_interval
@ C:\Users\uwefechner\.julia\packages\BSplineKit\FOLdw\src\BSplines\evaluate_all.jl:99 [inlined]
[3] _evaluate
@ C:\Users\uwefechner\.julia\packages\BSplineKit\FOLdw\src\Splines\spline.jl:155 [inlined]
[4] evaluate
@ C:\Users\uwefechner\.julia\packages\BSplineKit\FOLdw\src\Splines\spline.jl:149 [inlined]
[5] Spline
@ C:\Users\uwefechner\.julia\packages\BSplineKit\FOLdw\src\Splines\spline.jl:142 [inlined]
``````

Try DataInterpolations.jl, their types already have the required symbolic overloads to work with MTK.

4 Likes

This seams to work…