Skip to content

Commit 4a4eb32

Browse files
committed
document construction from existing ComponentArrays
1 parent 05b9f61 commit 4a4eb32

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ flat vectors is fair game.
3434
The easiest way to construct 1-dimensional ```ComponentArray```s (aliased as `ComponentVector`) is as if they were ```NamedTuple```s. In fact, a good way to think about them is as arbitrarily nested, mutable ```NamedTuple```s that can be passed through a solver.
3535
```julia
3636
julia> c = (a=2, b=[1, 2]);
37-
37+
3838
julia> x = ComponentArray(a=5, b=[(a=20., b=0), (a=33., b=0), (a=44., b=3)], c=c)
3939
ComponentVector{Float64}(a = 5.0, b = [(a = 20.0, b = 0.0), (a = 33.0, b = 0.0), (a = 44.0, b = 3.0)], c = (a = 2.0, b = [1.0, 2.0]))
40-
40+
4141
julia> x.c.a = 400; x
4242
ComponentVector{Float64}(a = 5.0, b = [(a = 20.0, b = 0.0), (a = 33.0, b = 0.0), (a = 44.0, b = 3.0)], c = (a = 400.0, b = [1.0, 2.0]))
43-
43+
4444
julia> x[8]
4545
400.0
46-
46+
4747
julia> collect(x)
4848
10-element Array{Float64,1}:
4949
5.0
@@ -60,6 +60,14 @@ julia> collect(x)
6060
julia> typeof(similar(x, Int32)) === typeof(ComponentVector{Int32}(a=5, b=[(a=20., b=0), (a=33., b=0), (a=44., b=3)], c=c))
6161
true
6262
```
63+
`ComponentArray`s can be constructed from existing
64+
`ComponentArray`s (currently nested fields cannot be changed this way):
65+
```julia
66+
julia> x = ComponentVector(a=1, b=2, c=3);
67+
68+
julia> ComponentVector(x; a=11, new=42)
69+
ComponentVector{Int64}(a = 11, b = 2, c = 3, new = 42)
70+
```
6371

6472
Higher dimensional ```ComponentArray```s can be created too, but it's a little messy at the moment. The nice thing for modeling is that dimension expansion through broadcasted operations can create higher-dimensional ```ComponentArray```s automatically, so Jacobian cache arrays that are created internally with ```false .* x .* x'``` will be two-dimensional ```ComponentArray```s (aliased as `ComponentMatrix`) with proper axes. Check out the [ODE with Jacobian](https://github.com/jonniedie/ComponentArrays.jl/blob/master/examples/ODE_jac_example.jl) example in the examples folder to see how this looks in practice.
6573
```julia
@@ -75,16 +83,16 @@ julia> x2 = x .* x'
7583
2.0 4.0 2.0 8.0 4.0 2.0 4.0
7684
1.0 2.0 1.0 4.0 2.0 1.0 2.0
7785
2.0 4.0 2.0 8.0 4.0 2.0 4.0
78-
86+
7987
julia> x2[:c,:c]
8088
3×3 ComponentMatrix{Float64,SubArray...} with axes Axis(a = 1, b = 2:3) × Axis(a = 1, b = 2:3)
8189
4.0 2.0 4.0
8290
2.0 1.0 2.0
8391
4.0 2.0 4.0
84-
92+
8593
julia> x2[:a,:a]
8694
1.0
87-
95+
8896
julia> x2[:a,:c]
8997
ComponentVector{Float64,SubArray...}(a = 2.0, b = [1.0, 2.0])
9098

@@ -114,7 +122,7 @@ tspan = (0.0, 20.0)
114122
function lorenz!(D, u, p, t; f=0.0)
115123
@unpack σ, ρ, β = p
116124
@unpack x, y, z = u
117-
125+
118126
D.x = σ*(y - x)
119127
D.y = x*- z) - y - f
120128
D.z = x*y - β*z
@@ -130,7 +138,7 @@ lorenz_prob = ODEProblem(lorenz!, lorenz_ic, tspan, lorenz_p)
130138
function lotka!(D, u, p, t; f=0.0)
131139
@unpack α, β, γ, δ = p
132140
@unpack x, y = u
133-
141+
134142
D.x = α*x - β*x*y + f
135143
D.y = -γ*y + δ*x*y
136144
return nothing
@@ -145,7 +153,7 @@ lotka_prob = ODEProblem(lotka!, lotka_ic, tspan, lotka_p)
145153
function composed!(D, u, p, t)
146154
c = p.c #coupling parameter
147155
@unpack lorenz, lotka = u
148-
156+
149157
lorenz!(D.lorenz, lorenz, p.lorenz, t, f=c*lotka.x)
150158
lotka!(D.lotka, lotka, p.lotka, t, f=c*lorenz.x)
151159
return nothing
@@ -238,7 +246,7 @@ sine_input(;mag=1.0, period=10.0) = (x,p,t) -> mag*sin(t*2π/period)
238246

239247
## Interactive GUI for switching out plant models and varying PID gains
240248
@manipulate for kp in 0:0.01:15,
241-
ki in 0:0.01:15,
249+
ki in 0:0.01:15,
242250
kd in 0:0.01:15,
243251
damping in Dict(
244252
"Coulomb" => coulomb_block!,
@@ -251,19 +259,19 @@ sine_input(;mag=1.0, period=10.0) = (x,p,t) -> mag*sin(t*2π/period)
251259
magnitude in 0:0.01:10, # pop-pop!
252260
period in 1:0.01:30,
253261
plot_v in false
254-
262+
255263
# Inputs
256264
tspan = (0.0, 30.0)
257265

258266
ctrl_fun = PID_controller!
259267
# plant_fun = coulomb_block!
260-
268+
261269
ref = if reference==sine_input
262270
reference(period=period, mag=magnitude)
263271
else
264272
reference(mag=magnitude)
265273
end
266-
274+
267275
m = 50.0
268276
μ = 0.1
269277
ω = 2π/period

docs/src/quickstart.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
The easiest way to construct 1-dimensional ```ComponentArray```s is as if they were ```NamedTuple```s. In fact, a good way to think about them is as arbitrarily nested, mutable ```NamedTuple```s that can be passed through a solver.
55
```julia
66
julia> c = (a=2, b=[1, 2]);
7-
7+
88
julia> x = ComponentArray(a=1.0, b=[2, 1, 4], c=c)
99
ComponentVector{Float64}(a = 1.0, b = [2.0, 1.0, 4.0], c = (a = 2.0, b = [1.0, 2.0]))
10-
10+
1111
julia> x.c.a = 400; x
1212
ComponentVector{Float64}(a = 1.0, b = [2.0, 1.0, 4.0], c = (a = 400.0, b = [1.0, 2.0]))
13-
13+
1414
julia> x[5]
1515
400.0
16-
16+
1717
julia> collect(x)
1818
7-element Array{Float64,1}:
1919
1.0
@@ -27,6 +27,14 @@ julia> collect(x)
2727
julia> typeof(similar(x, Int32)) === typeof(ComponentVector{Int32}(a=1, b=[2, 1, 4], c=c))
2828
true
2929
```
30+
`ComponentArray`s can be constructed from existing
31+
`ComponentArray`s (currently nested fields cannot be changed this way):
32+
```julia
33+
julia> x = ComponentVector(a=1, b=2, c=3);
34+
35+
julia> ComponentVector(x; a=11, new=42)
36+
ComponentVector{Int64}(a = 11, b = 2, c = 3, new = 42)
37+
```
3038

3139
Higher dimensional ```ComponentArray```s can be created too, but it's a little messy at the moment. The nice thing for modeling is that dimension expansion through broadcasted operations can create higher-dimensional ```ComponentArray```s automatically, so Jacobian cache arrays that are created internally with ```false .* x .* x'``` will be ```ComponentArray```s with proper axes. Check out the [ODE with Jacobian](https://github.com/jonniedie/ComponentArrays.jl/blob/master/examples/ODE_jac_example.jl) example in the examples folder to see how this looks in practice.
3240
```julia
@@ -39,16 +47,16 @@ julia> x2 = x .* x'
3947
400.0 800.0 400.0 1600.0 160000.0 400.0 800.0
4048
1.0 2.0 1.0 4.0 400.0 1.0 2.0
4149
2.0 4.0 2.0 8.0 800.0 2.0 4.0
42-
50+
4351
julia> x2[:c,:c]
4452
3×3 ComponentMatrix{Float64,SubArray...} with axes Axis(a = 1, b = 2:3) × Axis(a = 1, b = 2:3)
4553
160000.0 400.0 800.0
4654
400.0 1.0 2.0
4755
800.0 2.0 4.0
48-
56+
4957
julia> x2[:a,:a]
5058
1.0
51-
59+
5260
julia> x2[:a,:c]
5361
ComponentVector{Float64,SubArray...}(a = 400.0, b = [1.0, 2.0])
5462

0 commit comments

Comments
 (0)