Skip to content

Commit 8fdee10

Browse files
Format for JuliaFormatter v2
1 parent 251caa5 commit 8fdee10

File tree

64 files changed

+492
-364
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+492
-364
lines changed

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ makedocs(;
4848
"https://twitter.com/ChrisRackauckas/status/1544743542094020615",
4949
"https://link.springer.com/article/10.1007/s40096-020-00339-4",
5050
"https://dl.acm.org/doi/10.1145/210089.210111",
51-
"https://www.sciencedirect.com/science/article/abs/pii/S0045782523007156",
51+
"https://www.sciencedirect.com/science/article/abs/pii/S0045782523007156"
5252
],
5353
checkdocs = :exports,
5454
warnonly = [:missing_docs],

docs/src/tutorials/large_systems.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,19 @@ function brusselator_2d_loop(du, u, p)
7474
@inbounds for I in CartesianIndices((N, N))
7575
i, j = Tuple(I)
7676
x, y = xyd_brusselator[I[1]], xyd_brusselator[I[2]]
77-
ip1, im1, jp1, jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N),
77+
ip1, im1, jp1,
78+
jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N),
7879
limit(j - 1, N)
79-
du[i, j, 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] -
80-
4u[i, j, 1]) +
81-
B +
82-
u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + brusselator_f(x, y)
83-
du[i, j, 2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] -
84-
4u[i, j, 2]) + A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2]
80+
du[i,
81+
j,
82+
1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] -
83+
4u[i, j, 1]) +
84+
B +
85+
u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + brusselator_f(x, y)
86+
du[i,
87+
j,
88+
2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] -
89+
4u[i, j, 2]) + A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2]
8590
end
8691
end
8792
p = (3.4, 1.0, 10.0, step(xyd_brusselator))

docs/src/tutorials/nonlinear_solve_gpus.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using the first form.
3131
In this tutorial we will highlight both use cases in separate parts.
3232

3333
!!! note
34-
34+
3535
If you're looking for GPU-accelerated neural networks inside of nonlinear solvers,
3636
check out [DeepEquilibriumNetworks.jl](https://docs.sciml.ai/DeepEquilibriumNetworks/stable/).
3737

@@ -58,7 +58,7 @@ f(u, p) = u .* u .- p
5858
u0 = cu(ones(1000))
5959
p = cu(collect(1:1000))
6060
prob = NonlinearProblem(f, u0, p)
61-
sol = solve(prob, NewtonRaphson(), abstol=1f-4)
61+
sol = solve(prob, NewtonRaphson(), abstol = 1.0f-4)
6262
```
6363

6464
Notice a few things here. One, nothing is different except the input array types. But
@@ -106,7 +106,7 @@ is saying, "for the ith call, get the i'th parameter set and solve with these pa
106106
The ith result is then this solution".
107107

108108
!!! note
109-
109+
110110
Because kernel code needs to be able to be compiled to a GPU kernel, it has very strict
111111
specifications of what's allowed because GPU cores are not as flexible as CPU cores.
112112
In general, this means that you need to avoid any runtime operations in kernel code,
@@ -137,16 +137,16 @@ Now let's build a nonlinear system to test it on.
137137
out2 = sqrt(p[2]) * (x[3] - x[4])
138138
out3 = (x[2] - p[3] * x[3])^2
139139
out4 = sqrt(p[4]) * (x[1] - x[4]) * (x[1] - x[4])
140-
SA[out1,out2,out3,out4]
140+
SA[out1, out2, out3, out4]
141141
end
142142

143143
p = @SVector [@SVector(rand(Float32, 4)) for _ in 1:1024]
144-
u0 = SA[1f0, 2f0, 3f0, 4f0]
144+
u0 = SA[1.0f0, 2.0f0, 3.0f0, 4.0f0]
145145
prob = NonlinearSolveBase.ImmutableNonlinearProblem{false}(p2_f, u0, p)
146146
```
147147

148148
!!! note
149-
149+
150150
Because the custom kernel is going to need to embed the the code for our nonlinear
151151
problem into the kernel, it also must be written to be GPU compatible.
152152
In general, this means that you need to avoid any runtime operations in kernel code,
@@ -173,4 +173,5 @@ vectorized_solve(prob, SimpleNewtonRaphson(); backend = Metal.MetalBackend())
173173
```
174174

175175
!!! warn
176+
176177
The GPU-based calls will only work on your machine if you have a compatible GPU!

ext/NonlinearSolveFastLevenbergMarquardtExt.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ function SciMLBase.__solve(
2121
termination_condition, alg
2222
)
2323

24-
f_wrapped, u, resid = NonlinearSolveBase.construct_extension_function_wrapper(
24+
f_wrapped, u,
25+
resid = NonlinearSolveBase.construct_extension_function_wrapper(
2526
prob; alias_u0, can_handle_oop = Val(prob.u0 isa SArray)
2627
)
2728
f = if prob.u0 isa SArray
@@ -49,7 +50,8 @@ function SciMLBase.__solve(
4950
)
5051

5152
if prob.u0 isa SArray
52-
res, fx, info, iter, nfev, njev = FastLM.lmsolve(
53+
res, fx, info, iter, nfev,
54+
njev = FastLM.lmsolve(
5355
f, jac_fn, prob.u0; solver_kwargs...
5456
)
5557
LM, solver = nothing, nothing
@@ -68,7 +70,8 @@ function SciMLBase.__solve(
6870

6971
LM = FastLM.LMWorkspace(u, resid, J)
7072

71-
res, fx, info, iter, nfev, njev, LM, solver = FastLM.lmsolve!(
73+
res, fx, info, iter, nfev, njev,
74+
LM, solver = FastLM.lmsolve!(
7275
f, jac_fn, LM; solver, solver_kwargs...
7376
)
7477
end

ext/NonlinearSolveFixedPointAccelerationExt.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ function SciMLBase.__solve(
1515
termination_condition, alg
1616
)
1717

18-
f, u0, resid = NonlinearSolveBase.construct_extension_function_wrapper(
18+
f, u0,
19+
resid = NonlinearSolveBase.construct_extension_function_wrapper(
1920
prob; alias_u0, make_fixed_point = Val(true), force_oop = Val(true)
2021
)
2122

ext/NonlinearSolveMINPACKExt.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function SciMLBase.__solve(
1717
termination_condition, alg
1818
)
1919

20-
f_wrapped!, u0, resid = NonlinearSolveBase.construct_extension_function_wrapper(
20+
f_wrapped!, u0,
21+
resid = NonlinearSolveBase.construct_extension_function_wrapper(
2122
prob; alias_u0
2223
)
2324
resid_size = size(resid)

ext/NonlinearSolveNLSolversExt.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ function SciMLBase.__solve(
3333
prob.f, autodiff, prob.u0, Constant(prob.p)
3434
)
3535

36-
fj_scalar = @closure (Jx, x) -> begin
36+
fj_scalar = @closure (Jx,
37+
x) -> begin
3738
return DifferentiationInterface.value_and_derivative(
3839
prob.f, prep, autodiff, x, Constant(prob.p)
3940
)

ext/NonlinearSolvePETScExt.jl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function SciMLBase.__solve(
2222
termination_condition, alg; abs_norm_supported = false
2323
)
2424

25-
f_wrapped!, u0, resid = NonlinearSolveBase.construct_extension_function_wrapper(
25+
f_wrapped!, u0,
26+
resid = NonlinearSolveBase.construct_extension_function_wrapper(
2627
prob; alias_u0
2728
)
2829
T = eltype(u0)
@@ -48,7 +49,9 @@ function SciMLBase.__solve(
4849

4950
nf = Ref{Int}(0)
5051

51-
f! = @closure (cfx, cx, user_ctx) -> begin
52+
f! = @closure (cfx,
53+
cx,
54+
user_ctx) -> begin
5255
nf[] += 1
5356
fx = cfx isa Ptr{Nothing} ? PETSc.unsafe_localarray(T, cfx; read = false) : cfx
5457
x = cx isa Ptr{Nothing} ? PETSc.unsafe_localarray(T, cx; write = false) : cx
@@ -76,7 +79,8 @@ function SciMLBase.__solve(
7679
)
7780
J_init = zeros(T, 1, 1)
7881
else
79-
jac!, J_init = NonlinearSolveBase.construct_extension_jac(
82+
jac!,
83+
J_init = NonlinearSolveBase.construct_extension_jac(
8084
prob, alg, u0, resid; autodiff, initial_jacobian = Val(true)
8185
)
8286
end
@@ -85,7 +89,10 @@ function SciMLBase.__solve(
8589

8690
if J_init isa AbstractSparseMatrix
8791
PJ = PETSc.MatSeqAIJ(J_init)
88-
jac_fn! = @closure (cx, J, _, user_ctx) -> begin
92+
jac_fn! = @closure (cx,
93+
J,
94+
_,
95+
user_ctx) -> begin
8996
njac[] += 1
9097
x = cx isa Ptr{Nothing} ? PETSc.unsafe_localarray(T, cx; write = false) : cx
9198
if J isa PETSc.AbstractMat
@@ -102,7 +109,10 @@ function SciMLBase.__solve(
102109
snes.user_ctx = (; jacobian = J_init)
103110
else
104111
PJ = PETSc.MatSeqDense(J_init)
105-
jac_fn! = @closure (cx, J, _, user_ctx) -> begin
112+
jac_fn! = @closure (cx,
113+
J,
114+
_,
115+
user_ctx) -> begin
106116
njac[] += 1
107117
x = cx isa Ptr{Nothing} ? PETSc.unsafe_localarray(T, cx; write = false) : cx
108118
jac!(J, x)

ext/NonlinearSolveSIAMFANLEquationsExt.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ function SciMLBase.__solve(
6464
elseif method == :secant
6565
sol = secant(f, prob.u0; maxit = maxiters, atol, rtol, printerr)
6666
elseif method == :anderson
67-
f_aa, u, _ = NonlinearSolveBase.construct_extension_function_wrapper(
67+
f_aa, u,
68+
_ = NonlinearSolveBase.construct_extension_function_wrapper(
6869
prob; alias_u0, make_fixed_point = Val(true)
6970
)
7071
sol = aasol(
@@ -73,7 +74,8 @@ function SciMLBase.__solve(
7374
)
7475
end
7576
else
76-
f, u, resid = NonlinearSolveBase.construct_extension_function_wrapper(
77+
f, u,
78+
resid = NonlinearSolveBase.construct_extension_function_wrapper(
7779
prob; alias_u0, make_fixed_point = Val(method == :anderson)
7880
)
7981
N = length(u)

ext/NonlinearSolveSpeedMappingExt.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function SciMLBase.__solve(
1616
termination_condition, alg
1717
)
1818

19-
m!, u, resid = NonlinearSolveBase.construct_extension_function_wrapper(
19+
m!, u,
20+
resid = NonlinearSolveBase.construct_extension_function_wrapper(
2021
prob; alias_u0, make_fixed_point = Val(true)
2122
)
2223
tol = NonlinearSolveBase.get_tolerance(abstol, eltype(u))

lib/BracketingNonlinearSolve/ext/BracketingNonlinearSolveForwardDiffExt.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ using SciMLBase: SciMLBase, IntervalNonlinearProblem
77

88
using BracketingNonlinearSolve: Bisection, Brent, Alefeld, Falsi, ITP, Ridder
99

10-
const DualIntervalNonlinearProblem{T, V, P} = IntervalNonlinearProblem{
10+
const DualIntervalNonlinearProblem{T,
11+
V,
12+
P} = IntervalNonlinearProblem{
1113
uType, iip, <:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}}
1214
} where {uType, iip}
1315

lib/BracketingNonlinearSolve/src/bisection.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ function CommonSolve.solve(
8686
i += 1
8787
end
8888

89-
sol, i, left, right, fl, fr = Impl.bisection(
89+
sol, i, left, right,
90+
fl, fr = Impl.bisection(
9091
left, right, fl, fr, f, abstol, maxiters - i, prob, alg
9192
)
9293

lib/BracketingNonlinearSolve/src/brent.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ function CommonSolve.solve(
118118
i += 1
119119
end
120120

121-
sol, i, left, right, fl, fr = Impl.bisection(
121+
sol, i, left, right,
122+
fl, fr = Impl.bisection(
122123
left, right, fl, fr, f, abstol, maxiters - i, prob, alg
123124
)
124125

lib/BracketingNonlinearSolve/src/falsi.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ function CommonSolve.solve(
7676
i += 1
7777
end
7878

79-
sol, i, left, right, fl, fr = Impl.bisection(
79+
sol, i, left, right,
80+
fl, fr = Impl.bisection(
8081
left, right, fl, fr, f, abstol, maxiters - i, prob, alg
8182
)
8283

lib/BracketingNonlinearSolve/src/ridder.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ function CommonSolve.solve(
9090
i += 1
9191
end
9292

93-
sol, i, left, right, fl, fr = Impl.bisection(
93+
sol, i, left, right,
94+
fl, fr = Impl.bisection(
9495
left, right, fl, fr, f, abstol, maxiters - i, prob, alg
9596
)
9697

lib/BracketingNonlinearSolve/test/rootfind_tests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ end
1818

1919
@testset for p in 1.1:0.1:100.0
2020
@test g(p)sqrt(p) atol=1e-3 rtol=1e-3
21-
@test ForwardDiff.derivative(g, p)1 / (2 * sqrt(p)) atol=1e-3 rtol=1e-3
21+
@test ForwardDiff.derivative(g, p)1/(2*sqrt(p)) atol=1e-3 rtol=1e-3
2222
end
2323

2424
t = (p) -> [sqrt(p[2] / p[1])]
@@ -30,7 +30,7 @@ end
3030
return [sol.u]
3131
end
3232

33-
@test g2(p)[sqrt(p[2] / p[1])] atol=1e-3 rtol=1e-3
33+
@test g2(p)[sqrt(p[2]/p[1])] atol=1e-3 rtol=1e-3
3434
@test ForwardDiff.jacobian(g2, p)ForwardDiff.jacobian(t, p) atol=1e-3 rtol=1e-3
3535

3636
probB = IntervalNonlinearProblem{false}(quadratic_f, (1.0, 2.0), 2.0)
@@ -50,8 +50,8 @@ end
5050
end
5151

5252
@testitem "Tolerance Tests Interval Methods" setup=[RootfindingTestSnippet] tags=[:core] begin
53-
prob = IntervalNonlinearProblem(quadratic_f, (1.0, 20.0), 2.0)
54-
ϵ = eps(Float64) # least possible tol for all methods
53+
prob=IntervalNonlinearProblem(quadratic_f, (1.0, 20.0), 2.0)
54+
ϵ=eps(Float64) # least possible tol for all methods
5555

5656
@testset for alg in (Bisection(), Falsi(), ITP(), Muller(), nothing)
5757
@testset for abstol in [0.1, 0.01, 0.001, 0.0001, 1e-5, 1e-6]

lib/NonlinearSolveBase/ext/NonlinearSolveBaseForwardDiffExt.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ for algType in GENERAL_SOLVER_TYPES
124124
@eval function SciMLBase.__solve(
125125
prob::DualAbstractNonlinearProblem, alg::$(algType), args...; kwargs...
126126
)
127-
sol, partials = NonlinearSolveBase.nonlinearsolve_forwarddiff_solve(
127+
sol,
128+
partials = NonlinearSolveBase.nonlinearsolve_forwarddiff_solve(
128129
prob, alg, args...; kwargs...
129130
)
130131
dual_soln = NonlinearSolveBase.nonlinearsolve_dual_solution(sol.u, partials, prob.p)

lib/NonlinearSolveBase/src/NonlinearSolveBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ include("forward_diff.jl")
7373
@compat(public, (construct_jacobian_cache,))
7474
@compat(public,
7575
(assert_extension_supported_termination_condition,
76-
construct_extension_function_wrapper, construct_extension_jac))
76+
construct_extension_function_wrapper, construct_extension_jac))
7777

7878
export TraceMinimal, TraceWithJacobianConditionNumber, TraceAll
7979

lib/NonlinearSolveBase/src/autodiff.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,25 @@ function nlls_generate_vjp_function(prob::NonlinearLeastSquaresProblem, sol, uu)
118118
# nested autodiff as the last resort
119119
if SciMLBase.has_vjp(prob.f)
120120
if SciMLBase.isinplace(prob)
121-
return @closure (du, u, p) -> begin
121+
return @closure (
122+
du, u, p) -> begin
122123
resid = Utils.safe_similar(du, length(sol.resid))
123124
prob.f(resid, u, p)
124125
prob.f.vjp(du, resid, u, p)
125126
du .*= 2
126127
return nothing
127128
end
128129
else
129-
return @closure (u, p) -> begin
130+
return @closure (
131+
u, p) -> begin
130132
resid = prob.f(u, p)
131133
return reshape(2 .* prob.f.vjp(resid, u, p), size(u))
132134
end
133135
end
134136
elseif SciMLBase.has_jac(prob.f)
135137
if SciMLBase.isinplace(prob)
136-
return @closure (du, u, p) -> begin
138+
return @closure (
139+
du, u, p) -> begin
137140
J = Utils.safe_similar(du, length(sol.resid), length(u))
138141
prob.f.jac(J, u, p)
139142
resid = Utils.safe_similar(du, length(sol.resid))
@@ -142,7 +145,8 @@ function nlls_generate_vjp_function(prob::NonlinearLeastSquaresProblem, sol, uu)
142145
return nothing
143146
end
144147
else
145-
return @closure (u, p) -> begin
148+
return @closure (u,
149+
p) -> begin
146150
return reshape(2 .* vec(prob.f(u, p))' * prob.f.jac(u, p), size(u))
147151
end
148152
end
@@ -152,7 +156,8 @@ function nlls_generate_vjp_function(prob::NonlinearLeastSquaresProblem, sol, uu)
152156
select_reverse_mode_autodiff(prob, nothing) : AutoForwardDiff()
153157

154158
if SciMLBase.isinplace(prob)
155-
return @closure (du, u, p) -> begin
159+
return @closure (
160+
du, u, p) -> begin
156161
resid = Utils.safe_similar(du, length(sol.resid))
157162
prob.f(resid, u, p)
158163
# Using `Constant` lead to dual ordering issues
@@ -163,7 +168,8 @@ function nlls_generate_vjp_function(prob::NonlinearLeastSquaresProblem, sol, uu)
163168
return nothing
164169
end
165170
else
166-
return @closure (u, p) -> begin
171+
return @closure (u,
172+
p) -> begin
167173
v = prob.f(u, p)
168174
# Using `Constant` lead to dual ordering issues
169175
res = only(DI.pullback(Base.Fix2(prob.f, p), autodiff, u, (v,)))

0 commit comments

Comments
 (0)