Skip to content

Commit df681a6

Browse files
Merge pull request #3884 from AayushSabharwal/as/publicify
refactor: mark several functions as public API
2 parents b6facb8 + ba7ee60 commit df681a6

File tree

10 files changed

+226
-3
lines changed

10 files changed

+226
-3
lines changed

docs/src/API/System.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ optimizations).
77

88
```@docs
99
System
10+
ModelingToolkit.AbstractSystem
1011
```
1112

1213
## Utility constructors
@@ -185,3 +186,41 @@ ModelingToolkit.is_bound
185186
```@docs
186187
debug_system
187188
```
189+
190+
## Input validation
191+
192+
The following values can be passed to the `check` keyword of `System` to toggle validation
193+
of input. Flags can be combined with bitwise `|` and `&`.
194+
195+
```@docs
196+
ModelingToolkit.CheckAll
197+
ModelingToolkit.CheckNone
198+
ModelingToolkit.CheckComponents
199+
ModelingToolkit.CheckUnits
200+
```
201+
202+
These can also be used by custom `AbstractSystem` subtypes.
203+
204+
## Utility functions
205+
206+
These utility functions can be useful when manipulating systems, especially when building
207+
custom `AbstractSystem` subtypes.
208+
209+
```@docs
210+
ModelingToolkit.collect_scoped_vars!
211+
ModelingToolkit.collect_var_to_name!
212+
ModelingToolkit.collect_vars!
213+
ModelingToolkit.eqtype_supports_collect_vars
214+
ModelingToolkit.modified_unknowns!
215+
```
216+
217+
## Namepsace manipulation
218+
219+
ModelingToolkit namespaces variables from subsystems when using them in a parent system to
220+
disambiguate from identically named variables in other subsystems or the parent system. The
221+
following functions are useful for manipulating namespacing functionality.
222+
223+
```@docs
224+
ModelingToolkit.renamespace
225+
ModelingToolkit.namespace_equations
226+
```

docs/src/API/model_building.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
This page lists functionality and utilities related to building hierarchical models. It is
44
recommended to read the page on the [`System`](@ref System_type) before this.
55

6+
## Common definitions of `t` and `D`
7+
8+
ModelingToolkit provides common definitions for the independent variable `t` (time) and the
9+
derivative with respect to it `D`.
10+
11+
```@docs
12+
ModelingToolkit.t_nounits
13+
ModelingToolkit.D_nounits
14+
ModelingToolkit.t
15+
ModelingToolkit.D
16+
ModelingToolkit.t_unitful
17+
ModelingToolkit.D_unitful
18+
```
19+
20+
Users are recommended to use the appropriate common definition in their models. The required
21+
definitions can be imported with convenient aliased names. For example:
22+
23+
```julia
24+
using ModelingToolkit: t_nounits as t, D_nounits as D
25+
```
26+
27+
Allows using `t` and `D` to refer to `t_nounits` and `D_nounits` respectively.
28+
629
## Hierarchical model composition
730

831
The `System` data structure can represent a tree-like hierarchy of systems for building models

docs/src/API/variables.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ Symbolic variables can have metadata attached to them. The defaults and guesses
1818
at variable construction time are examples of this metadata. ModelingToolkit also defines
1919
additional types of metadata.
2020

21+
## Variable defaults
22+
23+
Variables can be assigned default values to avoid having to specify defaults to the
24+
[`System`](@ref) constructor.
25+
26+
```@docs
27+
ModelingToolkit.hasdefault
28+
ModelingToolkit.getdefault
29+
ModelingToolkit.setdefault
30+
```
31+
2132
## Variable descriptions
2233

2334
Descriptive strings can be attached to variables using the `[description = "descriptive string"]` syntax:
@@ -56,6 +67,7 @@ help?> u
5667
```@docs
5768
hasdescription
5869
getdescription
70+
ModelingToolkit.VariableDescription
5971
```
6072

6173
## Connect
@@ -83,6 +95,7 @@ getconnect(k)
8395
```@docs
8496
hasconnect
8597
getconnect
98+
ModelingToolkit.VariableConnectType
8699
```
87100

88101
```@docs; canonical=false
@@ -112,6 +125,8 @@ isinput
112125
isoutput
113126
ModelingToolkit.setinput
114127
ModelingToolkit.setoutput
128+
ModelingToolkit.VariableInput
129+
ModelingToolkit.VariableOutput
115130
```
116131

117132
## Bounds
@@ -144,6 +159,7 @@ hasbounds(x[2])
144159
```@docs
145160
hasbounds
146161
getbounds
162+
ModelingToolkit.VariableBounds
147163
```
148164

149165
## Guess
@@ -232,6 +248,7 @@ isirreducible(important_value)
232248

233249
```@docs
234250
isirreducible
251+
ModelingToolkit.VariableIrreducible
235252
```
236253

237254
## State Priority
@@ -245,6 +262,7 @@ state_priority(important_dof)
245262

246263
```@docs
247264
state_priority
265+
ModelingToolkit.VariableStatePriority
248266
```
249267

250268
## Units
@@ -261,6 +279,17 @@ getunit(speed)
261279
```@docs
262280
hasunit
263281
getunit
282+
ModelingToolkit.VariableUnit
283+
```
284+
285+
## Variable type
286+
287+
This metadata is used by the [`System`](@ref) constructor for automatically identifying the different types of variables in a system.
288+
289+
```@docs
290+
ModelingToolkit.VariableType
291+
ModelingToolkit.MTKVariableTypeCtx
292+
ModelingToolkit.isparameter
264293
```
265294

266295
## Miscellaneous metadata
@@ -277,6 +306,7 @@ getmisc(y)
277306
```@docs
278307
hasmisc
279308
getmisc
309+
ModelingToolkit.VariableMisc
280310
```
281311

282312
## Dumping metadata

src/ModelingToolkit.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ in a non-breaking release. Usage of these arguments is not advised.
134134
"""
135135
$(TYPEDEF)
136136
137-
TODO
137+
Abstract supertype of all system types. Any custom system types must subtype this.
138138
"""
139139
abstract type AbstractSystem end
140140
# Solely so that `ODESystem` can be deprecated and still act as a valid type.
@@ -336,6 +336,15 @@ export DynamicOptSolution
336336
@public is_diff_equation, Equality, linearize_symbolic, reorder_unknowns
337337
@public similarity_transform, inputs, outputs, bound_inputs, unbound_inputs, bound_outputs
338338
@public unbound_outputs, is_bound
339+
@public AbstractSystem, CheckAll, CheckNone, CheckComponents, CheckUnits
340+
@public t, D, t_nounits, D_nounits, t_unitful, D_unitful
341+
@public SymbolicContinuousCallback, SymbolicDiscreteCallback
342+
@public VariableType, MTKVariableTypeCtx, VariableBounds, VariableConnectType
343+
@public VariableDescription, VariableInput, VariableIrreducible, VariableMisc
344+
@public VariableOutput, VariableStatePriority, VariableUnit, collect_scoped_vars!
345+
@public collect_var_to_name!, collect_vars!, eqtype_supports_collect_vars, hasdefault
346+
@public getdefault, setdefault, iscomplete, isparameter, modified_unknowns!
347+
@public renamespace, namespace_equations
339348

340349
for prop in [SYS_PROPS; [:continuous_events, :discrete_events]]
341350
getter = Symbol(:get_, prop)

src/parameters.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
import SymbolicUtils: symtype, term, hasmetadata, issym
2+
3+
"""
4+
@enum VariableType
5+
6+
The type of the declared variable, used for automatic identification of
7+
variables/parameters/brownians/etc. by the `System` constructor.
8+
"""
29
@enum VariableType VARIABLE PARAMETER BROWNIAN
10+
11+
"""
12+
$TYPEDEF
13+
14+
The symbolic metadata key for storing the `VariableType`.
15+
"""
316
struct MTKVariableTypeCtx end
417

518
getvariabletype(x, def = VARIABLE) = getmetadata(unwrap(x), MTKVariableTypeCtx, def)
619

20+
"""
21+
$TYPEDEF
22+
23+
Check if the variable contains the metadata identifying it as a parameter.
24+
"""
725
function isparameter(x)
826
x = unwrap(x)
927

src/problems/jumpproblem.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ function get_variables!(dep, jump::MassActionJump, variables)
210210
end
211211

212212
### Functions to determine which unknowns are modified by a given jump
213+
214+
"""
215+
$(TYPEDSIGNATURES)
216+
217+
Push to `munknowns` the variables modified by jump `jump`. `sts` is the list of unknowns of
218+
the system. Return the modified `munknowns`.
219+
"""
213220
function modified_unknowns!(munknowns, jump::Union{ConstantRateJump, VariableRateJump}, sts)
214221
for eq in jump.affect!
215222
st = eq.lhs

src/systems/abstractsystem.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,12 @@ end
10951095
renamespace(sys, eq::Equation) = namespace_equation(eq, sys)
10961096

10971097
renamespace(names::AbstractVector, x) = foldr(renamespace, names, init = x)
1098+
1099+
"""
1100+
$(TYPEDSIGNATURES)
1101+
1102+
Namespace `x` with the name of `sys`.
1103+
"""
10981104
function renamespace(sys, x)
10991105
sys === nothing && return x
11001106
x = unwrap(x)
@@ -1141,6 +1147,11 @@ function namespace_guesses(sys)
11411147
Dict(unknowns(sys, k) => namespace_expr(v, sys) for (k, v) in guess)
11421148
end
11431149

1150+
"""
1151+
$(TYPEDSIGNATURES)
1152+
1153+
Return `equations(sys)`, namespaced by the name of `sys`.
1154+
"""
11441155
function namespace_equations(sys::AbstractSystem, ivs = independent_variables(sys))
11451156
eqs = equations(sys)
11461157
isempty(eqs) && return Equation[]
@@ -1162,6 +1173,11 @@ function namespace_tstops(sys::AbstractSystem)
11621173
end
11631174
end
11641175

1176+
"""
1177+
$(TYPEDSIGNATURES)
1178+
1179+
Namespace the given equation with the name of the given system `sys`.
1180+
"""
11651181
function namespace_equation(eq::Equation,
11661182
sys,
11671183
n = nameof(sys);

src/systems/model_parsing.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ function update_readable_metadata!(varclass_dict, meta::Dict, varname)
186186
(:description, VariableDescription),
187187
(:unit, VariableUnit),
188188
(:bounds, VariableBounds),
189-
(:noise, VariableNoiseType),
190189
(:input, VariableInput),
191190
(:output, VariableOutput),
192191
(:irreducible, VariableIrreducible),

src/utils.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,31 @@ function readable_code(expr)
8888
end
8989

9090
# System validation enums
91+
"""
92+
const CheckNone
93+
94+
Value that can be provided to the `check` keyword of `System` to disable checking of input.
95+
"""
9196
const CheckNone = 0
97+
"""
98+
const CheckAll
99+
100+
Value that can be provided to the `check` keyword of `System` to enable all input
101+
validation.
102+
"""
92103
const CheckAll = 1 << 0
104+
"""
105+
const CheckComponents
106+
107+
Value that can be provided to the `check` keyword of `System` to only enable checking of
108+
basic components of the system, such as equations, variables, etc.
109+
"""
93110
const CheckComponents = 1 << 1
111+
"""
112+
const CheckUnits
113+
114+
Value that can be provided to the `check` keyword of `System` to enable checking of units.
115+
"""
94116
const CheckUnits = 1 << 2
95117

96118
function check_independent_variables(ivs)
@@ -228,8 +250,23 @@ function iv_from_nested_derivative(x, op = Differential)
228250
end
229251
end
230252

253+
"""
254+
$(TYPEDSIGNATURES)
255+
256+
Check if the symbolic variable `v` has a default value.
257+
"""
231258
hasdefault(v) = hasmetadata(v, Symbolics.VariableDefaultValue)
259+
"""
260+
$(TYPEDSIGNATURES)
261+
262+
Return the default value of symbolic variable `v`.
263+
"""
232264
getdefault(v) = value(Symbolics.getdefaultval(v))
265+
"""
266+
$(TYPEDSIGNATURES)
267+
268+
Set the default value of symbolic variable `v` to `val`.
269+
"""
233270
function setdefault(v, val)
234271
val === nothing ? v : wrap(setdefaultval(unwrap(v), value(val)))
235272
end

0 commit comments

Comments
 (0)