Interpolation in Modeling Toolkit

Hello everyone,
I’m gradually getting into the Julia ecosystem and I find it fascinating. I’m experimenting with developing models using ModelingToolkit.jl and I’ve come across a challenge that I haven’t been able to solve. I would like to create a system that, based on the simulation time, outputs data according to values stored in a CSV file.

The documentation shows how to do this using an interpolation block via functions, but I’m wondering if the same can be achieved using the @mtkmodel ecosystem.

I’m attaching a diagram of the model I want to implement (which currently isn’t working) to see if anyone could help me figure it out.

#datos.value and datos.time are columns of a dataframe.
begin

	@mtkmodel CSVSource begin
		@parameters begin
		data=datos.value
		time=datos.time
		end
		@components begin
			clock = ContinuousClock(offset = 0, start_time = 0)
			inter = Interpolation(LinearInterpolation, u= data , x = time)
			transfer = TransferFunction(b=[1 2], a=[1 3 2])
		end
		@equations begin
			connect(clock.output,inter.input)
			connect(inter.output,transfer.input)
		end
	end

	
end

Best regards.

@SebastianM-C

The issue here is that the model macro does not currently support positional arguments. I’ll try to define a fallback that’s compatible with the macro, but that makes it harder (or impossible) to pass both an arbitrary number of positional and keyword arguments to the underlying interpolation.

As of Release v2.21.1 · SciML/ModelingToolkitStandardLibrary.jl · GitHub you should be able to use the interpolation block with the @mtkmodel macro.

Note that since in Interpolation the data and time are not symbolic parameters, you have to construct the interpolation object either outside of the model macro or wrapped in a begin/end block, so that it’s not considered as being symbolic.

See the example in the docs for more details: SampledData Component · ModelingToolkitStandardLibrary.jl

Let me know if you have any issues with this.