Skip to content

Commit 5f70d88

Browse files
authored
Merge branch 'SciML:main' into heating
2 parents 455ccad + 750abea commit 5f70d88

File tree

13 files changed

+251
-45
lines changed

13 files changed

+251
-45
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkitStandardLibrary"
22
uuid = "16a59e39-deab-5bd0-87e4-056b12336739"
33
authors = ["Chris Rackauckas and Julia Computing"]
4-
version = "2.18.0"
4+
version = "2.19.0"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

README.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,34 @@ using ModelingToolkitStandardLibrary.Electrical
5050
using ModelingToolkitStandardLibrary.Blocks: Constant
5151
using ModelingToolkit: t_nounits as t
5252

53-
R = 1.0
54-
C = 1.0
55-
V = 1.0
56-
systems = @named begin
57-
resistor = Resistor(R = R)
58-
capacitor = Capacitor(C = C, v = 0.0)
59-
source = Voltage()
60-
constant = Constant(k = V)
61-
ground = Ground()
53+
@mtkmodel RC begin
54+
@parameters begin
55+
R = 1.0
56+
C = 1.0
57+
V = 1.0
58+
end
59+
@components begin
60+
resistor = Resistor(R = R)
61+
capacitor = Capacitor(C = C, v = 0.0)
62+
source = Voltage()
63+
constant = Constant(k = V)
64+
ground = Ground()
65+
end
66+
@equations begin
67+
connect(constant.output, source.V)
68+
connect(source.p, resistor.p)
69+
connect(resistor.n, capacitor.p)
70+
connect(capacitor.n, source.n, ground.g)
71+
end
6272
end
6373

64-
rc_eqs = [connect(constant.output, source.V)
65-
connect(source.p, resistor.p)
66-
connect(resistor.n, capacitor.p)
67-
connect(capacitor.n, source.n, ground.g)]
68-
69-
@named rc_model = ODESystem(rc_eqs, t; systems)
70-
sys = structural_simplify(rc_model)
74+
@mtkbuild sys = RC()
7175
prob = ODEProblem(sys, Pair[], (0, 10.0))
72-
sol = solve(prob, Tsit5())
73-
plot(sol, idxs = [capacitor.v, resistor.i],
76+
sol = solve(prob)
77+
78+
plot(sol, idxs = [sys.capacitor.v, sys.resistor.i],
7479
title = "RC Circuit Demonstration",
7580
labels = ["Capacitor Voltage" "Resistor Current"])
76-
savefig("plot.png")
7781
```
7882

7983
![](https://user-images.githubusercontent.com/1814174/164912983-c3f73628-0e19-4e42-b085-4f62ba6f23d1.png)

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1313
[compat]
1414
ControlSystemsBase = "1.1"
1515
DataFrames = "1.7"
16-
DataInterpolations = "6.4"
16+
DataInterpolations = "6.4, 7"
1717
DifferentialEquations = "7.6"
1818
Documenter = "1"
1919
IfElse = "0.1"

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ makedocs(sitename = "ModelingToolkitStandardLibrary.jl",
3131
ModelingToolkitStandardLibrary.Hydraulic,
3232
ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible],
3333
clean = true, doctest = false, linkcheck = true,
34+
linkcheck_ignore = ["https://www.mathworks.com/help/simscape/ug/basic-principles-of-modeling-physical-networks.html#bq89sba-6"],
3435
warnonly = [:docs_block, :missing_docs, :cross_references],
3536
format = Documenter.HTML(assets = ["assets/favicon.ico"],
3637
canonical = "https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/"),

docs/src/API/blocks.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ Add
3636
Add3
3737
Product
3838
Division
39+
UnaryMinus
40+
Power
41+
Modulo
42+
Floor
43+
Ceil
3944
StaticNonLinearity
4045
Abs
4146
Sign

src/Blocks/Blocks.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D
1010
export RealInput, RealInputArray, RealOutput, RealOutputArray, SISO
1111
include("utils.jl")
1212

13-
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division
13+
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division, Power, Modulo, UnaryMinus, Floor, Ceil
1414
export Abs, Sign, Sqrt, Sin, Cos, Tan, Asin, Acos, Atan, Atan2, Sinh, Cosh, Tanh, Exp
1515
export Log, Log10
1616
include("math.jl")
@@ -29,6 +29,8 @@ include("continuous.jl")
2929

3030
export AnalysisPoint, get_sensitivity, get_comp_sensitivity,
3131
get_looptransfer, open_loop
32-
include("analysis_points.jl")
32+
@static if !isdefined(ModelingToolkit, :AnalysisPoint)
33+
include("analysis_points.jl")
34+
end
3335

3436
end

src/Blocks/math.jl

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,110 @@ Output first input divided by second input.
208208
end
209209
end
210210

211+
"""
212+
Power(; name)
213+
214+
Output the exponential with base as the first input and exponent as second input i.e u1^u2
215+
216+
# Connectors:
217+
218+
- `base`
219+
- `exponent`
220+
- `output`
221+
"""
222+
@mtkmodel Power begin
223+
@components begin
224+
base = RealInput()
225+
exponent = RealInput()
226+
output = RealOutput()
227+
end
228+
@equations begin
229+
output.u ~ base.u ^ exponent.u
230+
end
231+
end
232+
233+
"""
234+
Modulo(; name)
235+
236+
Output the remainder when the first input is divided by second input.
237+
238+
# Connectors:
239+
240+
- `dividend`
241+
- `divisor`
242+
- `remainder`
243+
"""
244+
@mtkmodel Modulo begin
245+
@components begin
246+
dividend = RealInput()
247+
divisor = RealInput(guess = 1.0) # denominator can not be zero
248+
remainder = RealOutput()
249+
end
250+
@equations begin
251+
remainder.u ~ mod(dividend.u, divisor.u)
252+
end
253+
end
254+
255+
"""
256+
UnaryMinus(; name)
257+
258+
Output the product of -1 and the input.
259+
260+
# Connectors:
261+
262+
- `input`
263+
- `output`
264+
"""
265+
@mtkmodel UnaryMinus begin
266+
@components begin
267+
input = RealInput()
268+
output = RealOutput()
269+
end
270+
@equations begin
271+
output.u ~ -(input.u)
272+
end
273+
end
274+
275+
"""
276+
Floor(; name)
277+
278+
Output the floor rounding of the input.
279+
280+
# Connectors:
281+
282+
- `input`
283+
- `output`
284+
"""
285+
@mtkmodel Floor begin
286+
@components begin
287+
input = RealInput()
288+
output = RealOutput()
289+
end
290+
@equations begin
291+
output.u ~ floor(input.u)
292+
end
293+
end
294+
295+
"""
296+
Ceil(; name)
297+
298+
Output the ceiling rounding of the input.
299+
300+
# Connectors:
301+
302+
- `input`
303+
- `output`
304+
"""
305+
@mtkmodel Ceil begin
306+
@components begin
307+
input = RealInput()
308+
output = RealOutput()
309+
end
310+
@equations begin
311+
output.u ~ ceil(input.u)
312+
end
313+
end
314+
211315
"""
212316
StaticNonLinearity(func; name)
213317

src/Blocks/sources.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,7 @@ data input component.
672672
eqs = [
673673
output.u ~ get_sampled_data(t, p)
674674
]
675-
return ODESystem(eqs, t, vars, [pars; p]; name, systems,
676-
defaults = [
677-
output.u => get_sampled_data(0.0, p)
678-
])
675+
return ODESystem(eqs, t, vars, [pars; p]; name, systems)
679676
end
680677

681678
"""
@@ -701,8 +698,7 @@ data input component.
701698
eqs = [
702699
output.u ~ get_sampled_data(t, buffer)
703700
]
704-
return ODESystem(eqs, t, vars, pars; name, systems,
705-
defaults = [output.u => get_sampled_data(0.0, buffer)])
701+
return ODESystem(eqs, t, vars, pars; name, systems)
706702
end
707703

708704
SampledData(x::SampledDataType.Option; kwargs...) = SampledData(Val(x); kwargs...)

src/Electrical/Analog/ideal_components.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,9 @@ R = R_const + pos * R_ref * (1 + alpha * (port.T - T_ref))
357357

358358
@parameters begin
359359
R_ref = 1.0,
360-
[description = "Resistance at temperature T_ref when fully closed (pos=1.0)",
361-
unit = "Ω"]
362-
T_ref = 300.15, [description = "Reference temperature", unit = "K"]
363-
R_const = 1e-3, [description = "Constant resistance between p and n", unit = "Ω"]
360+
[description = "Resistance at temperature T_ref when fully closed (pos=1.0) (Ω)"]
361+
T_ref = 300.15, [description = "Reference temperature (K)"]
362+
R_const = 1e-3, [description = "Constant resistance between p and n (Ω)"]
364363
end
365364

366365
@components begin
@@ -369,13 +368,12 @@ R = R_const + pos * R_ref * (1 + alpha * (port.T - T_ref))
369368

370369
@variables begin
371370
pos(t), [description = "Position of the wiper (normally 0-1)"]
372-
R(t), [description = "Resistance", unit = "Ω"]
371+
R(t), [description = "Resistance (Ω)"]
373372
end
374373

375374
if T_dep
376375
@parameters begin
377-
alpha = 1e-3,
378-
[description = "Temperature coefficient of resistance", unit = "K^-1"]
376+
alpha = 1e-3, [description = "Temperature coefficient of resistance (K^-1)"]
379377
end
380378
@components begin
381379
port = HeatPort()

test/Blocks/math.jl

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,103 @@ end
157157
@test sol[prod.output.u] 2 * sin.(2 * pi * sol.t)
158158
end
159159

160+
@testset "Power" begin
161+
@named c1 = Sine(; frequency = 1)
162+
@named c2 = Constant(; k = 2)
163+
@named pow = Power(;)
164+
@named int = Integrator(; k = 1)
165+
@named model = ODESystem(
166+
[
167+
connect(c1.output, pow.base),
168+
connect(c2.output, pow.exponent),
169+
connect(pow.output, int.input)
170+
],
171+
t,
172+
systems = [int, pow, c1, c2])
173+
sys = structural_simplify(model)
174+
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
175+
sol = solve(prob, Rodas4())
176+
@test isequal(unbound_inputs(sys), [])
177+
@test sol.retcode == Success
178+
@test sol[pow.output.u] sin.(2 * pi * sol.t) .^ 2
179+
end
180+
181+
@testset "Modulo" begin
182+
@named c1 = Ramp(height = 2, duration = 1, offset = 1, start_time = 0, smooth = false)
183+
@named c2 = Constant(; k = 1)
184+
@named modl = Modulo(;)
185+
@named model = ODESystem(
186+
[
187+
connect(c1.output, modl.dividend),
188+
connect(c2.output, modl.divisor)
189+
],
190+
t,
191+
systems = [modl, c1, c2])
192+
sys = structural_simplify(model)
193+
prob = ODEProblem(sys, [], (0.0, 1.0))
194+
sol = solve(prob, Rodas4())
195+
@test isequal(unbound_inputs(sys), [])
196+
@test sol.retcode == Success
197+
@test sol[modl.remainder.u] mod.(2 * sol.t,1)
198+
end
199+
200+
@testset "UnaryMinus" begin
201+
@named c1 = Sine(; frequency = 1)
202+
@named minu = UnaryMinus(;)
203+
@named int = Integrator(; k = 1)
204+
@named model = ODESystem(
205+
[
206+
connect(c1.output, minu.input),
207+
connect(minu.output, int.input)
208+
],
209+
t,
210+
systems = [int, minu, c1])
211+
sys = structural_simplify(model)
212+
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
213+
sol = solve(prob, Rodas4())
214+
@test isequal(unbound_inputs(sys), [])
215+
@test sol.retcode == Success
216+
@test sol[minu.output.u] - sin.(2 * pi * sol.t)
217+
end
218+
219+
@testset "Floor" begin
220+
@named c1 = Sine(; frequency = 1)
221+
@named flr = Floor(;)
222+
@named int = Integrator(; k = 1)
223+
@named model = ODESystem(
224+
[
225+
connect(c1.output, flr.input),
226+
connect(flr.output, int.input)
227+
],
228+
t,
229+
systems = [int, flr, c1])
230+
sys = structural_simplify(model)
231+
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
232+
sol = solve(prob, Rodas4())
233+
@test isequal(unbound_inputs(sys), [])
234+
@test sol.retcode == Success
235+
@test sol[flr.output.u] floor.(sin.(2 * pi * sol.t))
236+
end
237+
238+
@testset "Ceil" begin
239+
@named c1 = Sine(; frequency = 1)
240+
@named cel = Ceil(;)
241+
@named int = Integrator(; k = 1)
242+
@named model = ODESystem(
243+
[
244+
connect(c1.output, cel.input),
245+
connect(cel.output, int.input)
246+
],
247+
t,
248+
systems = [int, cel, c1])
249+
sys = structural_simplify(model)
250+
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
251+
sol = solve(prob, Rodas4())
252+
@test isequal(unbound_inputs(sys), [])
253+
@test sol.retcode == Success
254+
@test sol[cel.output.u] ceil.(sin.(2 * pi * sol.t))
255+
end
256+
160257
@testset "Division" begin
161258
@named c1 = Sine(; frequency = 1)
162259
@named c2 = Constant(; k = 2)

0 commit comments

Comments
 (0)