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

Add more documentation #2871

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions src/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ toconstant(s::Num) = wrap(toconstant(value(s)))
$(SIGNATURES)

Define one or more constants.

See also [`@independent_variables`](@ref), [`@parameters`](@ref) and [`@variables`](@ref).
"""
macro constants(xs...)
Symbolics._parse_vars(:constants,
Expand Down
2 changes: 2 additions & 0 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ tovar(s::Num) = Num(tovar(value(s)))
$(SIGNATURES)

Define one or more known parameters.

See also [`@independent_variables`](@ref), [`@variables`](@ref) and [`@constants`](@ref).
"""
macro parameters(xs...)
Symbolics._parse_vars(:parameters,
Expand Down
2 changes: 2 additions & 0 deletions src/structural_transformation/StructuralTransformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ using SparseArrays

using SimpleNonlinearSolve

using DocStringExtensions

export tearing, partial_state_selection, dae_index_lowering, check_consistency
export dummy_derivative
export build_torn_function, build_observed_function, ODAEProblem
Expand Down
8 changes: 8 additions & 0 deletions src/structural_transformation/symbolics_tearing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ function tearing_sub(expr, dict, s)
s ? simplify(expr) : expr
end

"""
$(TYPEDSIGNATURES)

Like `equations(sys)`, but includes substitutions done by the tearing process.
These equations matches generated numerical code.

See also [`equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref).
"""
function full_equations(sys::AbstractSystem; simplify = false)
empty_substitutions(sys) && return equations(sys)
substitutions = get_substitutions(sys)
Expand Down
74 changes: 67 additions & 7 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ function independent_variables(sys::AbstractMultivariateSystem)
return getfield(sys, :ivs)
end

"""
$(TYPEDSIGNATURES)

Get the independent variable(s) of the system `sys`.

See also [`@independent_variables`](@ref) and [`ModelingToolkit.get_iv`](@ref).
"""
function independent_variables(sys::AbstractSystem)
@warn "Please declare ($(typeof(sys))) as a subtype of `AbstractTimeDependentSystem`, `AbstractTimeIndependentSystem` or `AbstractMultivariateSystem`."
if isdefined(sys, :iv)
Expand Down Expand Up @@ -649,11 +656,29 @@ for prop in [:eqs
:split_idxs
:parent
:index_cache]
fname1 = Symbol(:get_, prop)
fname2 = Symbol(:has_, prop)
fname_get = Symbol(:get_, prop)
fname_has = Symbol(:has_, prop)
@eval begin
$fname1(sys::AbstractSystem) = getfield(sys, $(QuoteNode(prop)))
$fname2(sys::AbstractSystem) = isdefined(sys, $(QuoteNode(prop)))
"""
$(TYPEDSIGNATURES)

Get the internal field `$($(QuoteNode(prop)))` of a system `sys`.
It only includes `$($(QuoteNode(prop)))` local to `sys`; not those of its subsystems,
like `unknowns(sys)`, `parameters(sys)` and `equations(sys)` does.
This is equivalent to, but preferred over `sys.$($(QuoteNode(prop)))`.

See also [`has_$($(QuoteNode(prop)))`](@ref).
"""
$fname_get(sys::AbstractSystem) = getfield(sys, $(QuoteNode(prop)))

"""
$(TYPEDSIGNATURES)

Returns whether the system `sys` has the internal field `$($(QuoteNode(prop)))`.

See also [`get_$($(QuoteNode(prop)))`](@ref).
"""
$fname_has(sys::AbstractSystem) = isdefined(sys, $(QuoteNode(prop)))
end
end

Expand Down Expand Up @@ -1003,6 +1028,14 @@ function namespace_expr(
end
end
_nonum(@nospecialize x) = x isa Num ? x.val : x

"""
$(TYPEDSIGNATURES)

Get the unknown variables of the system `sys` and its subsystems.

See also [`ModelingToolkit.get_unknowns`](@ref).
"""
function unknowns(sys::AbstractSystem)
sts = get_unknowns(sys)
systems = get_systems(sys)
Expand All @@ -1023,6 +1056,13 @@ function unknowns(sys::AbstractSystem)
unique(nonunique_unknowns)
end

"""
$(TYPEDSIGNATURES)

Get the parameters of the system `sys` and its subsystems.

See also [`@parameters`](@ref) and [`ModelingToolkit.get_ps`](@ref).
"""
function parameters(sys::AbstractSystem)
ps = get_ps(sys)
if ps == SciMLBase.NullParameters()
Expand Down Expand Up @@ -1131,6 +1171,15 @@ end

flatten(sys::AbstractSystem, args...) = sys

"""
$(TYPEDSIGNATURES)

Get the flattened equations of the system `sys` and its subsystems.
It may include some abbreviations and aliases of observables.
It is often the most useful way to inspect the equations of a system.

See also [`full_equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref).
"""
function equations(sys::AbstractSystem)
eqs = get_eqs(sys)
systems = get_systems(sys)
Expand All @@ -1145,6 +1194,13 @@ function equations(sys::AbstractSystem)
end
end

"""
$(TYPEDSIGNATURES)

Get the initialization equations of the system `sys` and its subsystems.

See also [`ModelingToolkit.get_initialization_eqs`](@ref).
"""
function initialization_equations(sys::AbstractSystem)
eqs = get_initialization_eqs(sys)
systems = get_systems(sys)
Expand Down Expand Up @@ -2500,8 +2556,10 @@ end
"""
$(TYPEDSIGNATURES)

extend the `basesys` with `sys`, the resulting system would inherit `sys`'s name
Extend the `basesys` with `sys`, the resulting system would inherit `sys`'s name
by default.

See also [`compose`](@ref).
"""
function extend(sys::AbstractSystem, basesys::AbstractSystem; name::Symbol = nameof(sys),
gui_metadata = get_gui_metadata(sys))
Expand Down Expand Up @@ -2550,8 +2608,10 @@ end
"""
$(SIGNATURES)

compose multiple systems together. The resulting system would inherit the first
Compose multiple systems together. The resulting system would inherit the first
system's name.

See also [`extend`](@ref).
"""
function compose(sys::AbstractSystem, systems::AbstractArray; name = nameof(sys))
nsys = length(systems)
Expand All @@ -2572,7 +2632,7 @@ end
"""
missing_variable_defaults(sys::AbstractSystem, default = 0.0; subset = unknowns(sys))

returns a `Vector{Pair}` of variables set to `default` which are missing from `get_defaults(sys)`. The `default` argument can be a single value or vector to set the missing defaults respectively.
Returns a `Vector{Pair}` of variables set to `default` which are missing from `get_defaults(sys)`. The `default` argument can be a single value or vector to set the missing defaults respectively.
"""
function missing_variable_defaults(
sys::AbstractSystem, default = 0.0; subset = unknowns(sys))
Expand Down
Loading