Skip to content

Commit 09e7a83

Browse files
Merge pull request #323 from ChrisRackauckas-Claude/runic-formatting
Switch from JuliaFormatter to Runic.jl for code formatting
2 parents 970a927 + 68b8f20 commit 09e7a83

30 files changed

Lines changed: 772 additions & 460 deletions

.JuliaFormatter.toml

Lines changed: 0 additions & 3 deletions
This file was deleted.

.github/workflows/FormatCheck.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: format-check
2+
3+
on:
4+
push:
5+
branches:
6+
- 'master'
7+
- 'main'
8+
- 'release-'
9+
tags: '*'
10+
pull_request:
11+
12+
jobs:
13+
runic:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: fredrikekre/runic-action@v1
18+
with:
19+
version: '1'

docs/make.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ makedocs(;
1717
"Examples" => [
1818
"examples/DiffEqFlux.md",
1919
"examples/adaptive_control.md",
20+
"examples/ODE_jac.md",
2021
"examples/coulomb_control.md",
21-
"examples/ODE_jac.md"
2222
],
23-
"API" => "api.md"
23+
"API" => "api.md",
2424
],
2525
repo = GitHub("SciML/ComponentArrays.jl"),
2626
sitename = "ComponentArrays.jl",

examples/DiffEqFlux_example.jl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ tspan2 = (0.0f0, 25.0f0)
2323
# Make truth data
2424
function trueODEfunc(du, u, p, t)
2525
true_A = [-0.1 2.0; -2.0 -0.1]
26-
du .= ((u .^ 3)'true_A)'
26+
return du .= ((u .^ 3)'true_A)'
2727
end
2828

2929
t = range(tspan[1], tspan[2], length = datasize)
3030
# t = Float32.(vcat(range(0.0, 0.9, length=10), 10 .^ range(log10(tspan[1]+1), log10(tspan[2]), length=datasize-10)))
31-
t = Float32.([0; 10 .^ range(log10(tspan[1] + 0.01), log10(tspan[2]), length = datasize-1)])
31+
t = Float32.([0; 10 .^ range(log10(tspan[1] + 0.01), log10(tspan[2]), length = datasize - 1)])
3232
prob = ODEProblem(trueODEfunc, u0, tspan2)
3333
ode_sol = solve(prob, Tsit5())
3434
ode_data = Array(ode_sol(t)) + MeasurementNoise(0.1)
@@ -39,7 +39,7 @@ neural_layer(in, out) = ComponentArray{Float32}(W = glorot_uniform(out, in), b =
3939
# Dense neural layer function
4040
dense(layer, activation = identity) = u -> activation.(layer.W * u + layer.b)
4141

42-
# Neural ODE function
42+
# Neural ODE function
4343
dudt(u, p, t) = u .^ 3 |> dense(p.L1, σ) |> dense(p.L2)
4444

4545
prob = ODEProblem(dudt, u0, tspan2)
@@ -56,7 +56,7 @@ full_sol(θ) = solve(prob, Tsit5(), u0 = θ.u, p = θ.p)
5656
function loss_n_ode(θ)
5757
pred = predict_n_ode(θ)
5858

59-
loss = sum(abs2, ode_data .- pred)/datasize + 0.1*(sum(abs, θ.p)/length.p))
59+
loss = sum(abs2, ode_data .- pred) / datasize + 0.1 * (sum(abs, θ.p) / length.p))
6060
return loss, pred
6161
end
6262
loss_n_ode(θ)
@@ -83,12 +83,18 @@ cb = function (θ, loss, pred; doplot = false)
8383
plot!(pl_3, ode_sol, vars = (1, 2), label = "truth")
8484
scatter!(pl_3, pred[1, :], pred[2, :], label = "predicted data")
8585
scatter!(pl_3, ode_data[1, :], ode_data[2, :], label = "measured data")
86-
plot!(pl_3, hcat(pred[1, :], ode_data[1, :])', hcat(pred[2, :], ode_data[2, :])',
87-
label = false, color = :lightgray, legend = :bottomright)
88-
89-
display(plot(
90-
plot(pl_1, pl_2, layout = (2, 1), size = (400, 500)), pl_3, layout = (1, 2), size = (
91-
950, 500)))
86+
plot!(
87+
pl_3, hcat(pred[1, :], ode_data[1, :])', hcat(pred[2, :], ode_data[2, :])',
88+
label = false, color = :lightgray, legend = :bottomright
89+
)
90+
91+
display(
92+
plot(
93+
plot(pl_1, pl_2, layout = (2, 1), size = (400, 500)), pl_3, layout = (1, 2), size = (
94+
950, 500,
95+
)
96+
)
97+
)
9298
# frame(anim)
9399
return false
94100
end

examples/ODE_example.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ function lorenz!(D, u, p, t; f = 0.0)
99
@unpack σ, ρ, β = p
1010
@unpack x, y, z = u
1111

12-
D.x = σ*(y - x)
13-
D.y = x*- z) - y - f
14-
D.z = x*y - β*z
12+
D.x = σ * (y - x)
13+
D.y = x * - z) - y - f
14+
D.z = x * y - β * z
1515
return nothing
1616
end
1717

18-
lorenz_p == 10.0, ρ = 28.0, β = 8/3)
18+
lorenz_p == 10.0, ρ = 28.0, β = 8 / 3)
1919
lorenz_ic = ComponentArray(x = 0.0, y = 0.0, z = 0.0)
2020
lorenz_prob = ODEProblem(lorenz!, lorenz_ic, tspan, lorenz_p)
2121

@@ -24,12 +24,12 @@ function lotka!(D, u, p, t; f = 0.0)
2424
@unpack α, β, γ, δ = p
2525
@unpack x, y = u
2626

27-
D.x = α*x - β*x*y + f
28-
D.y = -γ*y + δ*x*y
27+
D.x = α * x - β * x * y + f
28+
D.y = -γ * y + δ * x * y
2929
return nothing
3030
end
3131

32-
lotka_p == 2/3, β = 4/3, γ = 1.0, δ = 1.0)
32+
lotka_p == 2 / 3, β = 4 / 3, γ = 1.0, δ = 1.0)
3333
lotka_ic = ComponentArray(x = 1.0, y = 1.0)
3434
lotka_prob = ODEProblem(lotka!, lotka_ic, tspan, lotka_p)
3535

@@ -38,8 +38,8 @@ function composed!(D, u, p, t)
3838
c = p.c #coupling parameter
3939
@unpack lorenz, lotka = u
4040

41-
lorenz!(D.lorenz, lorenz, p.lorenz, t, f = c*lotka.x)
42-
lotka!(D.lotka, lotka, p.lotka, t, f = c*lorenz.x)
41+
lorenz!(D.lorenz, lorenz, p.lorenz, t, f = c * lotka.x)
42+
lotka!(D.lotka, lotka, p.lotka, t, f = c * lorenz.x)
4343
return nothing
4444
end
4545

examples/ODE_jac_example.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ function lorenz!(D, u, p, t; f = 0.0)
99
@unpack σ, ρ, β = p
1010
@unpack x, y, z = u
1111

12-
D.x = σ*(y - x)
13-
D.y = x*- z) - y - f
14-
D.z = x*y - β*z
12+
D.x = σ * (y - x)
13+
D.y = x * - z) - y - f
14+
D.z = x * y - β * z
1515
return nothing
1616
end
1717
function lorenz_jac!(D, u, p, t)
@@ -31,7 +31,7 @@ function lorenz_jac!(D, u, p, t)
3131
return nothing
3232
end
3333

34-
lorenz_p == 10.0, ρ = 28.0, β = 8/3)
34+
lorenz_p == 10.0, ρ = 28.0, β = 8 / 3)
3535
lorenz_ic = ComponentArray(x = 0.0, y = 0.0, z = 0.0)
3636
lorenz_fun = ODEFunction(lorenz!, jac = lorenz_jac!)
3737
lorenz_prob = ODEProblem(lorenz_fun, lorenz_ic, tspan, lorenz_p)
@@ -41,23 +41,23 @@ function lotka!(D, u, p, t; f = 0.0)
4141
@unpack α, β, γ, δ = p
4242
@unpack x, y = u
4343

44-
D.x = α*x - β*x*y + f
45-
D.y = -γ*y + δ*x*y
44+
D.x = α * x - β * x * y + f
45+
D.y = -γ * y + δ * x * y
4646
return nothing
4747
end
4848
function lotka_jac!(D, u, p, t)
4949
@unpack α, β, γ, δ = p
5050
@unpack x, y = u
5151

52-
D[:x, :x] = α - β*y
53-
D[:x, :y] = -β*x
52+
D[:x, :x] = α - β * y
53+
D[:x, :y] = -β * x
5454

55-
D[:y, :x] = δ*y
56-
D[:y, :y] = -γ + δ*x
55+
D[:y, :x] = δ * y
56+
D[:y, :y] = -γ + δ * x
5757
return nothing
5858
end
5959

60-
lotka_p == 2/3, β = 4/3, γ = 1.0, δ = 1.0)
60+
lotka_p == 2 / 3, β = 4 / 3, γ = 1.0, δ = 1.0)
6161
lotka_ic = ComponentArray(x = 1.0, y = 1.0)
6262
lotka_fun = ODEFunction(lotka!, jac = lotka_jac!)
6363
lotka_prob = ODEProblem(lotka_fun, lotka_ic, tspan, lotka_p)
@@ -67,8 +67,8 @@ function composed!(D, u, p, t)
6767
c = p.c #coupling parameter
6868
@unpack lorenz, lotka = u
6969

70-
lorenz!(D.lorenz, lorenz, p.lorenz, t, f = c*lotka.x)
71-
lotka!(D.lotka, lotka, p.lotka, t, f = c*lorenz.x)
70+
lorenz!(D.lorenz, lorenz, p.lorenz, t, f = c * lotka.x)
71+
lotka!(D.lotka, lotka, p.lotka, t, f = c * lorenz.x)
7272
return nothing
7373
end
7474
function composed_jac!(D, u, p, t)

examples/adaptive_control_example.jl

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ maybe_apply(f, x, p, t) = f
1212

1313
function apply_inputs(func; kwargs...)
1414
simfun(dx, x, p, t) = func(
15-
dx, x, p, t; map(f->maybe_apply(f, x, p, t), (; kwargs...))...)
16-
simfun(x, p, t) = func(x, p, t; map(f->maybe_apply(f, x, p, t), (; kwargs...))...)
15+
dx, x, p, t; map(f -> maybe_apply(f, x, p, t), (; kwargs...))...
16+
)
17+
simfun(x, p, t) = func(x, p, t; map(f -> maybe_apply(f, x, p, t), (; kwargs...))...)
1718
return simfun
1819
end
1920

@@ -27,7 +28,7 @@ SISO_simulator(P::TransferFunction) = SISO_simulator(ss(P))
2728
function SISO_simulator(P::AbstractStateSpace)
2829
@unpack A, B, C, D = P
2930

30-
if size(D)!=(1, 1)
31+
if size(D) != (1, 1)
3132
error("This is not a SISO system")
3233
end
3334

@@ -37,8 +38,8 @@ function SISO_simulator(P::AbstractStateSpace)
3738
DD = D[1, 1]
3839

3940
return function sim!(dx, x, p, t; u = 0.0)
40-
dx .= A*x + BB*u
41-
return CC*x + DD*u
41+
dx .= A * x + BB * u
42+
return CC * x + DD * u
4243
end
4344
end
4445

@@ -60,13 +61,13 @@ nominal_sim! = SISO_simulator(nominal_plant)
6061

6162
# To test robustness to uncertainty, we'll also include unmodeled dynamics with an entirely
6263
# different structure than our nominal plant model.
63-
unmodeled_dynamics = 229/(s^2 + 30s + 229)
64+
unmodeled_dynamics = 229 / (s^2 + 30s + 229)
6465
truth_plant = nominal_plant * unmodeled_dynamics
6566
truth_sim! = SISO_simulator(truth_plant)
6667

6768
# We'll make a first-order sensor as well so we can add noise to our measurement
6869
τ = 0.005
69-
sensor_plant = 1 /*s + 1)
70+
sensor_plant = 1 / * s + 1)
7071
sensor_sim! = SISO_simulator(sensor_plant)
7172

7273
## Derivative functions
@@ -76,7 +77,7 @@ control(θ, w) = θ'w
7677

7778
# We'll use a simple gradient descent adaptation law
7879
function adapt!(Dθ, θ, γ, t; e, w)
79-
Dθ .= -γ*e*w
80+
Dθ .= -γ * e * w
8081
return nothing
8182
end
8283

@@ -98,7 +99,7 @@ function feedback_sys!(D, vars, p, t; ym, r, n)
9899
return yp
99100
end
100101
# Now the full system takes in an input signal `r`, feeds it through the reference model,
101-
# and feeds the output of the reference model `ym` and the input signal to `feedback_sys`.
102+
# and feeds the output of the reference model `ym` and the input signal to `feedback_sys`.
102103
function system!(D, vars, p, t; r = 0.0, n = 0.0)
103104
@unpack reference_model, feedback_loop = vars
104105

@@ -122,31 +123,33 @@ sensor_ic = zeros(1)
122123
θ_est_ic = ComponentArray(θr = 0.0, θy = 0.0)
123124

124125
## Set up and run Simulation
125-
function simulate(plant_fun, plant_ic;
126+
function simulate(
127+
plant_fun, plant_ic;
126128
tspan = tspan,
127129
input_signal = input_signal,
128130
adapt_gain = 1.5,
129131
noise_param = nothing,
130-
deterministic_noise = 0.0)
132+
deterministic_noise = 0.0
133+
)
131134
noise(D, vars, p, t) = (D.feedback_loop.sensor[1] = noise_param)
132135

133136
# Truth control parameters
134-
θ_truth = (r = bm/bp, y = (ap-am)/bp)
137+
θ_truth = (r = bm / bp, y = (ap - am) / bp)
135138

136139
# Initial conditions
137140
ic = ComponentArray(
138141
reference_model = ref_ic,
139142
feedback_loop = (
140143
parameter_estimates = θ_est_ic,
141144
sensor = sensor_ic,
142-
plant_model = plant_ic
145+
plant_model = plant_ic,
143146
)
144147
)
145148

146149
# Model parameters
147150
p = (
148151
gamma = adapt_gain,
149-
plant_fun = plant_fun
152+
plant_fun = plant_fun,
150153
)
151154

152155
sim_fun = apply_inputs(system!; r = input_signal, n = deterministic_noise)
@@ -172,9 +175,14 @@ function simulate(plant_fun, plant_ic;
172175
)
173176

174177
# Parameter estimate tracking
175-
bottom = plot(sol,
176-
vars = Symbol.([
177-
"feedback_loop.parameter_estimates.θr", "feedback_loop.parameter_estimates.θy"]))
178+
bottom = plot(
179+
sol,
180+
vars = Symbol.(
181+
[
182+
"feedback_loop.parameter_estimates.θr", "feedback_loop.parameter_estimates.θy",
183+
]
184+
)
185+
)
178186
plot!(
179187
bottom,
180188
[tspan...], [θ_truth.r θ_truth.y; θ_truth.r θ_truth.y],
@@ -184,8 +192,10 @@ function simulate(plant_fun, plant_ic;
184192
)
185193

186194
# Combine both plots
187-
plot(top, bottom, layout = (2, 1), size = (800, 800))
195+
return plot(top, bottom, layout = (2, 1), size = (800, 800))
188196
end
189197

190-
simulate(truth_sim!, truth_ic; input_signal = 2.0,
191-
deterministic_noise = (x, p, t)->0.5sin(16.1t), noise_param = nothing)
198+
simulate(
199+
truth_sim!, truth_ic; input_signal = 2.0,
200+
deterministic_noise = (x, p, t) -> 0.5sin(16.1t), noise_param = nothing
201+
)

0 commit comments

Comments
 (0)