Skip to content

Commit d992c97

Browse files
committed
remove unneccessary let block
1 parent 391049d commit d992c97

File tree

2 files changed

+49
-55
lines changed

2 files changed

+49
-55
lines changed

src/initialization.jl

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -154,64 +154,57 @@ function initialization_problem(cf::T; t=NaN, apply_bound_transformation, verbos
154154
end
155155

156156
N = ForwardDiff.pickchunksize(Nfree)
157-
fz = let fg = compfg(cf),
158-
unlcache=map(d->DiffCache(zeros(d), N), length(freesym)),
159-
outcaches=map(d->DiffCache(zeros(d), N), outdim_normalized(cf)),
160-
ucache=DiffCache(zeros(dim(cf)), N),
161-
incaches=map(d->DiffCache(zeros(d), N), indim_normalized(cf)),
162-
pcache=DiffCache(zeros(pdim(cf))),
163-
t=t,
164-
outfree_ms=outfree_ms, ufree_m=ufree_m, infree_ms=infree_ms, pfree_m=pfree_m,
165-
outfixs=outfixs, ufix=ufix, infixs=infixs, pfix=pfix,
166-
unl_range_outs=unl_range_outs, unl_range_u=unl_range_u,
167-
unl_range_ins=unl_range_ins, unl_range_p=unl_range_p,
168-
boundT! = boundT!
169-
170-
(dunl, unl, _) -> begin
171-
# apply the bound conserving transformation
172-
unlbuf = PreallocationTools.get_tmp(unlcache, unl)
173-
unlbuf .= unl
174-
boundT!(unlbuf)
175-
176-
outbufs = PreallocationTools.get_tmp.(outcaches, Ref(dunl))
177-
ubuf = PreallocationTools.get_tmp(ucache, dunl)
178-
inbufs = PreallocationTools.get_tmp.(incaches, Ref(dunl))
179-
pbuf = PreallocationTools.get_tmp(pcache, dunl)
180-
181-
# prefill buffers with fixed values
182-
for (buf, fix) in zip(outbufs, outfixs)
183-
buf .= fix
184-
end
185-
ubuf .= ufix
186-
for (buf, fix) in zip(inbufs, infixs)
187-
buf .= fix
188-
end
189-
pbuf .= pfix
157+
fg = compfg(cf)
158+
unlcache = map(d->DiffCache(zeros(d), N), length(freesym))
159+
outcaches = map(d->DiffCache(zeros(d), N), outdim_normalized(cf))
160+
ucache = DiffCache(zeros(dim(cf)), N)
161+
incaches = map(d->DiffCache(zeros(d), N), indim_normalized(cf))
162+
pcache = DiffCache(zeros(pdim(cf)))
163+
164+
fz = (dunl, unl, _) -> begin
165+
# apply the bound conserving transformation
166+
unlbuf = PreallocationTools.get_tmp(unlcache, unl)
167+
unlbuf .= unl
168+
boundT!(unlbuf)
169+
170+
outbufs = PreallocationTools.get_tmp.(outcaches, Ref(dunl))
171+
ubuf = PreallocationTools.get_tmp(ucache, dunl)
172+
inbufs = PreallocationTools.get_tmp.(incaches, Ref(dunl))
173+
pbuf = PreallocationTools.get_tmp(pcache, dunl)
174+
175+
# prefill buffers with fixed values
176+
for (buf, fix) in zip(outbufs, outfixs)
177+
buf .= fix
178+
end
179+
ubuf .= ufix
180+
for (buf, fix) in zip(inbufs, infixs)
181+
buf .= fix
182+
end
183+
pbuf .= pfix
190184

191-
# overwrite nonfixed values
192-
for (buf, mask, range) in zip(outbufs, outfree_ms, unl_range_outs)
193-
_overwrite_at_mask!(buf, mask, unlbuf, range)
194-
end
195-
_overwrite_at_mask!(ubuf, ufree_m, unlbuf, unl_range_u)
196-
for (buf, mask, range) in zip(inbufs, infree_ms, unl_range_ins)
197-
_overwrite_at_mask!(buf, mask, unlbuf, range)
198-
end
199-
_overwrite_at_mask!(pbuf, pfree_m, unlbuf, unl_range_p)
185+
# overwrite nonfixed values
186+
for (buf, mask, range) in zip(outbufs, outfree_ms, unl_range_outs)
187+
_overwrite_at_mask!(buf, mask, unlbuf, range)
188+
end
189+
_overwrite_at_mask!(ubuf, ufree_m, unlbuf, unl_range_u)
190+
for (buf, mask, range) in zip(inbufs, infree_ms, unl_range_ins)
191+
_overwrite_at_mask!(buf, mask, unlbuf, range)
192+
end
193+
_overwrite_at_mask!(pbuf, pfree_m, unlbuf, unl_range_p)
200194

201-
# view into du buffer for the fg funtion
202-
@views dunl_fg = dunl[1:dim(cf)]
203-
# view into the output buffer for the outputs
204-
@views dunl_out = dunl[dim(cf)+1:end]
195+
# view into du buffer for the fg funtion
196+
@views dunl_fg = dunl[1:dim(cf)]
197+
# view into the output buffer for the outputs
198+
@views dunl_out = dunl[dim(cf)+1:end]
205199

206-
# this fills the second half of the du buffer with the fixed and current outputs
207-
dunl_out .= RecursiveArrayTools.ArrayPartition(outbufs...)
208-
# execute fg to fill dunl and outputs
209-
fg(outbufs, dunl_fg, ubuf, inbufs, pbuf, t)
200+
# this fills the second half of the du buffer with the fixed and current outputs
201+
dunl_out .= RecursiveArrayTools.ArrayPartition(outbufs...)
202+
# execute fg to fill dunl and outputs
203+
fg(outbufs, dunl_fg, ubuf, inbufs, pbuf, t)
210204

211-
# calculate the residual for the second half ov the dunl buf, the outputs
212-
dunl_out .= dunl_out .- RecursiveArrayTools.ArrayPartition(outbufs...)
213-
nothing
214-
end
205+
# calculate the residual for the second half ov the dunl buf, the outputs
206+
dunl_out .= dunl_out .- RecursiveArrayTools.ArrayPartition(outbufs...)
207+
nothing
215208
end
216209

217210
nlf = NonlinearFunction(fz; resid_prototype=zeros(Neqs), sys=SII.SymbolCache(freesym))

test/initialization_test.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,6 @@ end
194194

195195
prob, _ = NetworkDynamics.initialization_problem(vf; apply_bound_transformation=true);
196196
du = zeros(length(prob.f.resid_prototype));
197-
@b $(prob.f)($du, $(prob.u0), nothing)
197+
b = @b $(prob.f)($du, $(prob.u0), nothing)
198+
@test iszero(b.allocs)
198199
end

0 commit comments

Comments
 (0)