Skip to content

Commit cc187a0

Browse files
Merge pull request #997 from pepijndevos/patch-1
Add documentation for #900 and #777
2 parents fecba7e + 91bb531 commit cc187a0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/src/basics/Composition.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,40 @@ done even if the variable `x` is eliminated from the system from
125125
transformations like `alias_elimination` or `tearing`: the variable
126126
will be lazily reconstructed on demand.
127127

128+
### Variable scope and parameter expressions
129+
130+
In some scenarios, it could be useful for model parameters to be expressed
131+
in terms of other parameters, or shared between common subsystems.
132+
To fascilitate this, ModelingToolkit supports sybmolic expressions
133+
in default values, and scoped variables.
134+
135+
With symbolic parameters, it is possible to set the default value of a parameter or initial condition to an expression of other variables.
136+
137+
```julia
138+
# ...
139+
sys = ODESystem(
140+
# ...
141+
# directly in the defauls argument
142+
defaults=Pair{Num, Any}[
143+
x => u,
144+
y => σ,
145+
z => u-0.1,
146+
])
147+
# by assigning to the parameter
148+
sys.y = u*1.1
149+
```
150+
151+
In a hierarchical system, variables of the subsystem get namespaced by the name of the system they are in. This prevents naming clashes, but also enforces that every state and parameter is local to the subsystem it is used in. In some cases it might be desirable to have variables and parameters that are shared between subsystems, or even global. This can be accomplished as follows.
152+
153+
```julia
154+
@variables a b c d
155+
156+
# a is a local variable
157+
b = ParentScope(b) # b is a variable that belongs to one level up in the hierarchy
158+
c = ParentScope(ParentScope(c)) # ParentScope can be nested
159+
d = GlobalScope(d) # global variables will never be namespaced
160+
```
161+
128162
## Structural Simplify
129163

130164
In many cases, the nicest way to build a model may leave a lot of

0 commit comments

Comments
 (0)