Skip to content

Commit

Permalink
Merge pull request #271 from AayushSabharwal/as/mtk-v9
Browse files Browse the repository at this point in the history
refactor: update for compatibility with MTKv9
  • Loading branch information
ChrisRackauckas authored Feb 23, 2024
2 parents 2da69d3 + 27c1a1d commit 4ec8af3
Show file tree
Hide file tree
Showing 37 changed files with 71 additions and 124 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ DataInterpolations = "4"
DiffEqBase = "6.103"
IfElse = "0.1"
LinearAlgebra = "1.10"
ModelingToolkit = "8.69"
ModelingToolkit = "9"
OrdinaryDiffEq = "6.33"
SafeTestsets = "0.1"
Symbolics = "5.2"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ ControlSystemsBase = "1.1"
DifferentialEquations = "7.6"
Documenter = "1"
IfElse = "0.1"
ModelingToolkit = "8.67"
ModelingToolkit = "9"
OrdinaryDiffEq = "6.31"
Plots = "1.36"
3 changes: 1 addition & 2 deletions docs/src/connectors/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ As can be seen, this will give a 1 equation model matching our energy dissipatio

```@example connections
using ModelingToolkitStandardLibrary.Electrical, ModelingToolkit, DifferentialEquations
using ModelingToolkit: t_nounits as t
using Plots
@parameters t
@named resistor = Resistor(R = 1)
@named capacitor = Capacitor(C = 1)
@named ground = Ground()
Expand Down
3 changes: 1 addition & 2 deletions docs/src/tutorials/custom_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ First, we need to make some imports.

```@example components
using ModelingToolkit
using ModelingToolkit: t_nounits as t
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Electrical: OnePort
using OrdinaryDiffEq
Expand Down Expand Up @@ -35,8 +36,6 @@ end NonlinearResistor;
this can almost be directly translated to the syntax of `ModelingToolkit`.

```@example components
@parameters t
function NonlinearResistor(; name, Ga, Gb, Ve)
@named oneport = OnePort()
@unpack v, i = oneport
Expand Down
3 changes: 1 addition & 2 deletions docs/src/tutorials/dc_motor_pi.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ First, the needed packages are imported and the parameters of the model defined.

```@example dc_motor_pi
using ModelingToolkit
using ModelingToolkit: t_nounits as t
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Mechanical.Rotational
using ModelingToolkitStandardLibrary.Blocks
using OrdinaryDiffEq
using Plots
@parameters t
R = 0.5 # [Ohm] armature resistance
L = 4.5e-3 # [H] armature inductance
k = 0.5 # [N.m/A] motor constant
Expand Down
4 changes: 1 addition & 3 deletions docs/src/tutorials/input_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ The `ModelingToolkitStandardLibrary.Blocks.TimeVaryingFunction` component is eas

```julia
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using ModelingToolkitStandardLibrary.Blocks
using DataInterpolations
using OrdinaryDiffEq

@parameters t
D = Differential(t)

function System(f; name)
src = TimeVaryingFunction(f)

Expand Down
2 changes: 1 addition & 1 deletion docs/src/tutorials/rc_circuit.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rc_eqs = [connect(constant.output, source.V)
@named rc_model = ODESystem(rc_eqs, t,
systems = [resistor, capacitor, constant, source, ground])
sys = structural_simplify(rc_model)
prob = ODAEProblem(sys, Pair[], (0, 10.0))
prob = ODEProblem(sys, Pair[], (0, 10.0))
sol = solve(prob, Tsit5())
plot(sol, idxs = [capacitor.v, resistor.i],
title = "RC Circuit Demonstration",
Expand Down
3 changes: 1 addition & 2 deletions docs/src/tutorials/thermal_model.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ from dividing the total initial energy in the system by the sum of the heat capa

```@example
using ModelingToolkitStandardLibrary.Thermal, ModelingToolkit, OrdinaryDiffEq, Plots
@parameters t
using ModelingToolkit: t_nounits as t
C1 = 15
C2 = 15
Expand Down
5 changes: 1 addition & 4 deletions src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ module Blocks
using ModelingToolkit, Symbolics
import IfElse: ifelse
import ..@symcheck
using ModelingToolkit: getdefault

@parameters t
D = Differential(t)
using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D

export RealInput, RealOutput, SISO
include("utils.jl")
Expand Down
1 change: 1 addition & 0 deletions src/Blocks/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSys

x = collect(x)
x_scaled = collect(x_scaled)
bb = collect(bb)

sts = [x; x_scaled; y; u]

Expand Down
26 changes: 15 additions & 11 deletions src/Blocks/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ function Base.show(io::IO, m::MIME"text/plain", p::Parameter)
end

get_sample_time(memory::Parameter) = memory.ref
Symbolics.@register_symbolic get_sample_time(memory)
Symbolics.@register_symbolic get_sample_time(memory::Parameter)

Base.convert(::Type{T}, x::Parameter{T}) where {T <: Real} = x.ref
function Base.convert(::Type{<:Parameter{T}}, x::Number) where {T <: Real}
Expand Down Expand Up @@ -603,8 +603,9 @@ end
function get_sampled_data(t, buffer)
get_sampled_data(t, buffer.data, buffer.ref, buffer.circular_buffer)
end
Symbolics.@register_symbolic get_sampled_data(t, buffer)
Symbolics.@register_symbolic get_sampled_data(t, buffer, dt, circular_buffer) false
Symbolics.@register_symbolic Parameter(data::Vector, ref, circular_buffer::Bool)
Symbolics.@register_symbolic get_sampled_data(t, buffer::Parameter)
Symbolics.@register_symbolic get_sampled_data(t, buffer::Vector, dt, circular_buffer) false

function Symbolics.derivative(::typeof(get_sampled_data), args::NTuple{2, Any}, ::Val{1})
t = @inbounds args[1]
Expand Down Expand Up @@ -655,21 +656,23 @@ data input component.
buffer,
sample_time,
circular_buffer = true)
T = eltype(buffer)
pars = @parameters begin
buffer = buffer #::Vector{Real}
sample_time = sample_time #::Real
circular_buffer = circular_buffer #::Bool
buffer::Vector{T} = buffer #::Vector{Real}
sample_time::T = sample_time #::Real
circular_buffer::Bool = circular_buffer #::Bool
end
@parameters p::Parameter{T} = Parameter(buffer, sample_time, circular_buffer)
vars = []
systems = @named begin
output = RealOutput()
end
eqs = [
output.u ~ get_sampled_data(t, buffer, sample_time, circular_buffer)
output.u ~ get_sampled_data(t, p)
]
return ODESystem(eqs, t, vars, pars; name, systems,
return ODESystem(eqs, t, vars, [pars; p]; name, systems,
defaults = [
output.u => get_sampled_data(0.0, buffer, sample_time, circular_buffer)
output.u => get_sampled_data(0.0, p)
])
end

Expand All @@ -684,9 +687,10 @@ data input component.
# Connectors:
- `output`
"""
@component function SampledData(::Val{SampledDataType.struct_based}; name, buffer)
@component function SampledData(
::Val{SampledDataType.struct_based}; name, buffer::Parameter)
pars = @parameters begin
buffer = buffer #::Parameter
buffer::typeof(buffer) = buffer #::Parameter
end
vars = []
systems = @named begin
Expand Down
4 changes: 1 addition & 3 deletions src/Electrical/Electrical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ This library contains electrical components to build up analog circuits.
module Electrical

using ModelingToolkit, Symbolics, IfElse
using ModelingToolkit: t_nounits as t, D_nounits as D
using ..Thermal: HeatPort
using ..Mechanical.Rotational: Flange, Support
using ..Blocks: RealInput, RealOutput

@parameters t
D = Differential(t)

export Pin, OnePort
include("utils.jl")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ Library to model iso-thermal compressible liquid fluid flow
module IsothermalCompressible

using ModelingToolkit, Symbolics
using ModelingToolkit: t_nounits as t, D_nounits as D

using ...Blocks: RealInput, RealOutput
using ...Mechanical.Translational: MechanicalPort, Mass

using IfElse: ifelse

@parameters t
D = Differential(t)

export HydraulicPort, HydraulicFluid
include("utils.jl")

Expand Down
4 changes: 1 addition & 3 deletions src/Magnetic/FluxTubes/FluxTubes.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module FluxTubes
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using ...Electrical: Pin

@parameters t
D = Differential(t)

export PositiveMagneticPort, NegativeMagneticPort, TwoPort
include("utils.jl")

Expand Down
4 changes: 1 addition & 3 deletions src/Mechanical/MultiBody2D/MultiBody2D.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module MultiBody2D

using ModelingToolkit, Symbolics, IfElse
using ModelingToolkit: t_nounits as t, D_nounits as D
using ..TranslationalPosition

@parameters t
D = Differential(t)

export Link
include("components.jl")

Expand Down
4 changes: 1 addition & 3 deletions src/Mechanical/Rotational/Rotational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ Library to model 1-dimensional, rotational mechanical systems
module Rotational

using ModelingToolkit, Symbolics, IfElse
using ModelingToolkit: t_nounits as t, D_nounits as D
using ...Blocks: RealInput, RealOutput
import ...@symcheck

@parameters t
D = Differential(t)

export Flange, Support
include("utils.jl")

Expand Down
5 changes: 1 addition & 4 deletions src/Mechanical/Translational/Translational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ Library to model 1-dimensional, translational mechanical systems
module Translational

using ModelingToolkit, Symbolics
using ModelingToolkit: getdefault
using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D

using ModelingToolkitStandardLibrary.Blocks: RealInput, RealOutput
using IfElse: ifelse

@parameters t
D = Differential(t)

export MechanicalPort
include("utils.jl")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ Library to model 1-dimensional, translational mechanical components.
module TranslationalModelica

using ModelingToolkit, Symbolics, IfElse
using ModelingToolkit: t_nounits as t, D_nounits as D
using ...Blocks: RealInput, RealOutput

@parameters t
D = Differential(t)

export Flange
include("utils.jl")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ Library to model 1-dimensional, translational mechanical components.
module TranslationalPosition

using ModelingToolkit, Symbolics, IfElse
using ModelingToolkit: t_nounits as t, D_nounits as D
using ...Blocks: RealInput, RealOutput

@parameters t
D = Differential(t)

export Flange
include("utils.jl")

Expand Down
4 changes: 1 addition & 3 deletions src/Thermal/Thermal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ Library of thermal system components to model heat transfer.
"""
module Thermal
using ModelingToolkit, Symbolics, IfElse
using ModelingToolkit: t_nounits as t, D_nounits as D
using ...Blocks: RealInput, RealOutput

@parameters t
D = Differential(t)

export HeatPort, Element1D
include("utils.jl")

Expand Down
3 changes: 1 addition & 2 deletions test/Blocks/continuous.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using ModelingToolkit, ModelingToolkitStandardLibrary, OrdinaryDiffEq
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkit: t_nounits as t
using OrdinaryDiffEq: ReturnCode.Success
using Test

@parameters t

#=
Testing strategy:
The general strategy is to test systems using simple inputs where the solution
Expand Down
4 changes: 1 addition & 3 deletions test/Blocks/math.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkit, OrdinaryDiffEq, Test
using ModelingToolkitStandardLibrary.Blocks: _clamp, _dead_zone
using ModelingToolkit: inputs, unbound_inputs, bound_inputs
using ModelingToolkit: inputs, unbound_inputs, bound_inputs, t_nounits as t
using OrdinaryDiffEq: ReturnCode.Success

@parameters t

@testset "Gain" begin
@named c = Constant(; k = 1)
@named gain = Gain(; k = 1)
Expand Down
3 changes: 1 addition & 2 deletions test/Blocks/nonlinear.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using ModelingToolkit, OrdinaryDiffEq
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkit: t_nounits as t
using ModelingToolkitStandardLibrary.Blocks: _clamp, _dead_zone
using OrdinaryDiffEq: ReturnCode.Success

@parameters t

@testset "Limiter" begin
@testset "Constant" begin
@named c = Constant(; k = 1)
Expand Down
4 changes: 1 addition & 3 deletions test/Blocks/sources.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using ModelingToolkit, ModelingToolkitStandardLibrary, OrdinaryDiffEq
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkit: t_nounits as t, D_nounits as D
using ModelingToolkitStandardLibrary.Blocks: smooth_sin, smooth_cos, smooth_damped_sin,
smooth_square, smooth_step, smooth_ramp,
smooth_triangular, triangular, square
using OrdinaryDiffEq: ReturnCode.Success

@parameters t
D = Differential(t)

@testset "Constant" begin
@named src = Constant(k = 2)
@named int = Integrator()
Expand Down
Loading

0 comments on commit 4ec8af3

Please sign in to comment.