Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update docs for MTKv9 #2489

Merged
merged 18 commits into from
Feb 26, 2024
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
eb976a8
docs: fix FunctionAffect usage and documentation
AayushSabharwal Feb 23, 2024
0f6931a
docs: remove usages of ModelingToolkitDesigner
AayushSabharwal Feb 23, 2024
82a570f
docs: remove StructuralIndentifiability dependency
AayushSabharwal Feb 23, 2024
6e010f9
docs: use DynamicQuantities instead of Unitful
AayushSabharwal Feb 23, 2024
9f9d8bf
docs: use `@brownian` in the stochastic_diffeq tutorial
AayushSabharwal Feb 23, 2024
940d637
refactor: add warning that `substitute` does not update events
AayushSabharwal Feb 23, 2024
0272392
refactor: warn that initial values for observed variables will not be…
AayushSabharwal Feb 23, 2024
5cbd523
feat: add dump_variable_metadata, dump_parameters, dump_variables
AayushSabharwal Feb 23, 2024
47aed59
fix: fix repack when new value does not alias canonicalized array
AayushSabharwal Feb 23, 2024
16d6311
docs: document new `dump_*` functions
AayushSabharwal Feb 23, 2024
48ec10b
refactor: remove `integer` and `binary` variable metadata
AayushSabharwal Feb 23, 2024
3953073
docs: disable OptimizationSystem doc examples
AayushSabharwal Feb 23, 2024
7aece73
docs: fix various example block errors
AayushSabharwal Feb 26, 2024
6c5d080
feat: add generate_custom_function
AayushSabharwal Feb 26, 2024
9b6f861
test: remove ControlSystemsMTK from test dependencies
AayushSabharwal Feb 26, 2024
f584815
feat: support indexing with `Symbol`s in IndexCache
AayushSabharwal Feb 26, 2024
87df269
docs: add doc for `generate_custom_function`
AayushSabharwal Feb 26, 2024
9535ba7
feat: remove `IfElse` support
AayushSabharwal Feb 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: remove integer and binary variable metadata
  • Loading branch information
AayushSabharwal committed Feb 23, 2024
commit 48ec10b53139bd6761037eec8848012d034b674e
3 changes: 1 addition & 2 deletions src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
@@ -222,8 +222,7 @@ export connect, domain_connect, @connector, Connection, Flow, Stream, instream
export @component, @mtkmodel, @mtkbuild
export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance,
istunable, getdist, hasdist,
tunable_parameters, isirreducible, getdescription, hasdescription, isbinaryvar,
isintegervar
tunable_parameters, isirreducible, getdescription, hasdescription
export ode_order_lowering, dae_order_lowering, liouville_transform
export PDESystem
export Differential, expand_derivatives, @derivatives
4 changes: 1 addition & 3 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
@@ -120,9 +120,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
(:misc, VariableMisc),
(:disturbance, VariableDisturbance),
(:tunable, VariableTunable),
(:dist, VariableDistribution),
(:binary, VariableBinary),
(:integer, VariableInteger)]
(:dist, VariableDistribution)]

arg isa LineNumberNode && return
MLStyle.@match arg begin
14 changes: 8 additions & 6 deletions src/systems/optimization/optimizationsystem.jl
Original file line number Diff line number Diff line change
@@ -258,8 +258,9 @@
if isnothing(lb) && isnothing(ub) # use the symbolically specified bounds
lb = first.(getbounds.(dvs))
ub = last.(getbounds.(dvs))
lb[isbinaryvar.(dvs)] .= 0
ub[isbinaryvar.(dvs)] .= 1
isboolean = symtype.(unwrap.(dvs)) .<: Bool
lb[isboolean] .= 0
ub[isboolean] .= 1

Check warning on line 263 in src/systems/optimization/optimizationsystem.jl

Codecov / codecov/patch

src/systems/optimization/optimizationsystem.jl#L261-L263

Added lines #L261 - L263 were not covered by tests
else # use the user supplied variable bounds
xor(isnothing(lb), isnothing(ub)) &&
throw(ArgumentError("Expected both `lb` and `ub` to be supplied"))
@@ -269,7 +270,7 @@
throw(ArgumentError("Expected both `ub` to be of the same length as the vector of optimization variables"))
end

int = isintegervar.(dvs) .| isbinaryvar.(dvs)
int = symtype.(unwrap.(dvs)) .<: Integer

Check warning on line 273 in src/systems/optimization/optimizationsystem.jl

Codecov / codecov/patch

src/systems/optimization/optimizationsystem.jl#L273

Added line #L273 was not covered by tests

defs = defaults(sys)
defs = mergedefaults(defs, parammap, ps)
@@ -476,8 +477,9 @@
if isnothing(lb) && isnothing(ub) # use the symbolically specified bounds
lb = first.(getbounds.(dvs))
ub = last.(getbounds.(dvs))
lb[isbinaryvar.(dvs)] .= 0
ub[isbinaryvar.(dvs)] .= 1
isboolean = symtype.(unwrap.(dvs)) .<: Bool
lb[isboolean] .= 0
ub[isboolean] .= 1

Check warning on line 482 in src/systems/optimization/optimizationsystem.jl

Codecov / codecov/patch

src/systems/optimization/optimizationsystem.jl#L480-L482

Added lines #L480 - L482 were not covered by tests
else # use the user supplied variable bounds
xor(isnothing(lb), isnothing(ub)) &&
throw(ArgumentError("Expected both `lb` and `ub` to be supplied"))
@@ -487,7 +489,7 @@
throw(ArgumentError("Expected `ub` to be of the same length as the vector of optimization variables"))
end

int = isintegervar.(dvs) .| isbinaryvar.(dvs)
int = symtype.(unwrap.(dvs)) .<: Integer

Check warning on line 492 in src/systems/optimization/optimizationsystem.jl

Codecov / codecov/patch

src/systems/optimization/optimizationsystem.jl#L492

Added line #L492 was not covered by tests

defs = defaults(sys)
defs = mergedefaults(defs, parammap, ps)
34 changes: 0 additions & 34 deletions src/variables.jl
Original file line number Diff line number Diff line change
@@ -27,33 +27,33 @@
ModelingToolkit.dump_variable_metadata(p)
```
"""
function dump_variable_metadata(var)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dump_variable_metadata(var) seems perfect to me. The named tuple return type allows direct integration with Tables.jl.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the returned NamedTuple may not have the same type or fields for all variables. It depends on the types of the symbolic variables themselves as well as the contained metadata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, true. I don't think this is compatible with Tables.jl, but I definitely prefer this option that does not print unecessary informatioN!

uvar = unwrap(var)
vartype, name = get(uvar.metadata, VariableSource, (:unknown, :unknown))
shape = Symbolics.shape(var)
if shape == ()
shape = nothing

Check warning on line 35 in src/variables.jl

Codecov / codecov/patch

src/variables.jl#L30-L35

Added lines #L30 - L35 were not covered by tests
end
unit = get(uvar.metadata, VariableUnit, nothing)
connect = get(uvar.metadata, VariableConnectType, nothing)
noise = get(uvar.metadata, VariableNoiseType, nothing)
input = isinput(uvar) || nothing
output = isoutput(uvar) || nothing
irreducible = get(uvar.metadata, VariableIrreducible, nothing)
state_priority = get(uvar.metadata, VariableStatePriority, nothing)
misc = get(uvar.metadata, VariableMisc, nothing)
bounds = hasbounds(uvar) ? getbounds(uvar) : nothing
desc = getdescription(var)
if desc == ""
desc = nothing

Check warning on line 48 in src/variables.jl

Codecov / codecov/patch

src/variables.jl#L37-L48

Added lines #L37 - L48 were not covered by tests
end
guess = getguess(uvar)
disturbance = isdisturbance(uvar) || nothing
tunable = istunable(uvar, isparameter(uvar))
dist = getdist(uvar)
type = symtype(uvar)

Check warning on line 54 in src/variables.jl

Codecov / codecov/patch

src/variables.jl#L50-L54

Added lines #L50 - L54 were not covered by tests

meta = (

Check warning on line 56 in src/variables.jl

Codecov / codecov/patch

src/variables.jl#L56

Added line #L56 was not covered by tests
var = var,
vartype,
name,
@@ -75,7 +75,7 @@
type
)

return NamedTuple(k => v for (k, v) in pairs(meta) if v !== nothing)

Check warning on line 78 in src/variables.jl

Codecov / codecov/patch

src/variables.jl#L78

Added line #L78 was not covered by tests
end

abstract type AbstractConnectType end
@@ -320,7 +320,7 @@

See also [`tunable_parameters`](@ref), [`getbounds`](@ref)
"""
function istunable(x, default = true)

Check warning on line 323 in src/variables.jl

Codecov / codecov/patch

src/variables.jl#L323

Added line #L323 was not covered by tests
p = Symbolics.getparent(x, nothing)
p === nothing || (x = p)
Symbolics.getmetadata(x, VariableTunable, default)
@@ -379,7 +379,7 @@

See also [`getbounds`](@ref), [`istunable`](@ref)
"""
function tunable_parameters(sys, p = parameters(sys); default = true)

Check warning on line 382 in src/variables.jl

Codecov / codecov/patch

src/variables.jl#L382

Added line #L382 was not covered by tests
filter(x -> istunable(x, default), p)
end

@@ -439,40 +439,6 @@
getdescription(x) != ""
end

## binary variables =================================================================
struct VariableBinary end
Symbolics.option_to_metadata_type(::Val{:binary}) = VariableBinary

isbinaryvar(x::Num) = isbinaryvar(Symbolics.unwrap(x))

"""
isbinaryvar(x)

Determine if a variable is binary.
"""
function isbinaryvar(x)
p = Symbolics.getparent(x, nothing)
p === nothing || (x = p)
return Symbolics.getmetadata(x, VariableBinary, false)
end

## integer variables =================================================================
struct VariableInteger end
Symbolics.option_to_metadata_type(::Val{:integer}) = VariableInteger

isintegervar(x::Num) = isintegervar(Symbolics.unwrap(x))

"""
isintegervar(x)

Determine if a variable is an integer.
"""
function isintegervar(x)
p = Symbolics.getparent(x, nothing)
p === nothing || (x = p)
return Symbolics.getmetadata(x, VariableInteger, false)
end

## Brownian
"""
tobrownian(s::Sym)
8 changes: 4 additions & 4 deletions test/model_parsing.jl
Original file line number Diff line number Diff line change
@@ -273,14 +273,14 @@ end

@testset "Metadata in variables" begin
metadata = Dict(:description => "Variable to test metadata in the Model.structure",
:input => true, :bounds => (-1, 1), :connection_type => :Flow, :integer => true,
:binary => false, :tunable => false, :disturbance => true, :dist => Normal(1, 1))
:input => true, :bounds => (-1, 1), :connection_type => :Flow,
:tunable => false, :disturbance => true, :dist => Normal(1, 1))

@connector MockMeta begin
m(t),
[description = "Variable to test metadata in the Model.structure",
input = true, bounds = (-1, 1), connect = Flow, integer = true,
binary = false, tunable = false, disturbance = true, dist = Normal(1, 1)]
input = true, bounds = (-1, 1), connect = Flow,
tunable = false, disturbance = true, dist = Normal(1, 1)]
end

for (k, v) in metadata
16 changes: 0 additions & 16 deletions test/test_variable_metadata.jl
Original file line number Diff line number Diff line change
@@ -112,19 +112,3 @@ sp = Set(p)
@named sys = ODESystem([u ~ p], t)

@test_nowarn show(stdout, "text/plain", sys)

@testset "binary" begin
@parameters t
@variables u(t) [binary = true]
@parameters p [binary = true]
@test isbinaryvar(u)
@test isbinaryvar(p)
end

@testset "integer" begin
@parameters t
@variables u(t) [integer = true]
@parameters p [integer = true]
@test isintegervar(u)
@test isintegervar(p)
end
Loading