Skip to content

Commit 2f8cf36

Browse files
refactor: use Accessors instead of Setfield
1 parent 4792360 commit 2f8cf36

30 files changed

+173
-167
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "9.59.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
8+
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
89
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
910
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
1011
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
@@ -73,11 +74,12 @@ MTKBifurcationKitExt = "BifurcationKit"
7374
MTKChainRulesCoreExt = "ChainRulesCore"
7475
MTKDeepDiffsExt = "DeepDiffs"
7576
MTKHomotopyContinuationExt = "HomotopyContinuation"
76-
MTKLabelledArraysExt = "LabelledArrays"
7777
MTKInfiniteOptExt = "InfiniteOpt"
78+
MTKLabelledArraysExt = "LabelledArrays"
7879

7980
[compat]
8081
AbstractTrees = "0.3, 0.4"
82+
Accessors = "0.1.36"
8183
ArrayInterface = "6, 7"
8284
BifurcationKit = "0.4"
8385
BlockArrays = "1.1"

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
23
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
34
BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665"
45
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
@@ -23,6 +24,7 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
2324
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
2425

2526
[compat]
27+
Accessors = "0.1.36"
2628
BenchmarkTools = "1.3"
2729
BifurcationKit = "0.4"
2830
DataInterpolations = "6.5"

docs/src/basics/Events.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -412,16 +412,16 @@ is below `furnace_on_threshold` and off when above `furnace_off_threshold`, whil
412412
in between. To do this, we create two continuous callbacks:
413413

414414
```@example events
415-
using Setfield
415+
using Accessors
416416
furnace_disable = ModelingToolkit.SymbolicContinuousCallback(
417417
[temp ~ furnace_off_threshold],
418418
ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, c, i
419-
@set! x.furnace_on = false
419+
@reset x.furnace_on = false
420420
end)
421421
furnace_enable = ModelingToolkit.SymbolicContinuousCallback(
422422
[temp ~ furnace_on_threshold],
423423
ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, c, i
424-
@set! x.furnace_on = true
424+
@reset x.furnace_on = true
425425
end)
426426
```
427427

@@ -432,7 +432,7 @@ You can also write
432432
```julia
433433
[temp ~ furnace_off_threshold] => ModelingToolkit.ImperativeAffect(modified = (;
434434
furnace_on)) do x, o, i, c
435-
@set! x.furnace_on = false
435+
@reset x.furnace_on = false
436436
end
437437
```
438438

@@ -462,7 +462,7 @@ f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple
462462
The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions.
463463
In our example, if `furnace_on` is `false`, then the value of the `x` that's passed in as `modified` will be `(furnace_on = false)`.
464464
The modified values should be passed out in the same format: to set `furnace_on` to `true` we need to return a tuple `(furnace_on = true)`.
465-
The examples does this with Setfield, recreating the result tuple before returning it; the returned tuple may optionally be missing values as
465+
The examples does this with Accessors, recreating the result tuple before returning it; the returned tuple may optionally be missing values as
466466
well, in which case those values will not be written back to the problem.
467467

468468
Accordingly, we can now interpret the `ImperativeAffect` definitions to mean that when `temp = furnace_off_threshold` we
@@ -542,18 +542,18 @@ In our encoder, we interpret this as occlusion or nonocclusion of the sensor, up
542542
```@example events
543543
qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0],
544544
ModelingToolkit.ImperativeAffect((; qA, hA, hB, cnt), (; qB)) do x, o, c, i
545-
@set! x.hA = x.qA
546-
@set! x.hB = o.qB
547-
@set! x.qA = 1
548-
@set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB)
545+
@reset x.hA = x.qA
546+
@reset x.hB = o.qB
547+
@reset x.qA = 1
548+
@reset x.cnt += decoder(x.hA, x.hB, x.qA, o.qB)
549549
x
550550
end,
551551
affect_neg = ModelingToolkit.ImperativeAffect(
552552
(; qA, hA, hB, cnt), (; qB)) do x, o, c, i
553-
@set! x.hA = x.qA
554-
@set! x.hB = o.qB
555-
@set! x.qA = 0
556-
@set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB)
553+
@reset x.hA = x.qA
554+
@reset x.hB = o.qB
555+
@reset x.qA = 0
556+
@reset x.cnt += decoder(x.hA, x.hB, x.qA, o.qB)
557557
x
558558
end)
559559
```
@@ -566,10 +566,10 @@ Instead, we can use right root finding:
566566
```@example events
567567
qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π / 2) ~ 0],
568568
ModelingToolkit.ImperativeAffect((; qB, hA, hB, cnt), (; qA, theta)) do x, o, c, i
569-
@set! x.hA = o.qA
570-
@set! x.hB = x.qB
571-
@set! x.qB = clamp(sign(cos(100 * o.theta - π / 2)), 0.0, 1.0)
572-
@set! x.cnt += decoder(x.hA, x.hB, o.qA, x.qB)
569+
@reset x.hA = o.qA
570+
@reset x.hB = x.qB
571+
@reset x.qB = clamp(sign(cos(100 * o.theta - π / 2)), 0.0, 1.0)
572+
@reset x.cnt += decoder(x.hA, x.hB, o.qA, x.qB)
573573
x
574574
end; rootfind = SciMLBase.RightRootFind)
575575
```

ext/MTKBifurcationKitExt.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module MTKBifurcationKitExt
33
### Preparations ###
44

55
# Imports
6-
using ModelingToolkit, Setfield
6+
using ModelingToolkit, Accessors
77
import BifurcationKit
88

99
### Observable Plotting Handling ###
@@ -94,7 +94,7 @@ function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem,
9494
if !ModelingToolkit.iscomplete(nsys)
9595
error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `BifurcationProblem`")
9696
end
97-
@set! nsys.index_cache = nothing # force usage of a parameter vector instead of `MTKParameters`
97+
@reset nsys.index_cache = nothing # force usage of a parameter vector instead of `MTKParameters`
9898
# Creates F and J functions.
9999
ofun = NonlinearFunction(nsys; jac = jac)
100100
F = ofun.f

src/ModelingToolkit.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ using JumpProcesses
3131
using DataStructures
3232
using Base.Threads
3333
using Latexify, Unitful, ArrayInterface
34-
using Setfield, ConstructionBase
34+
import Setfield
35+
using Accessors, ConstructionBase
3536
import Libdl
3637
using DocStringExtensions
3738
using Base: RefValue

src/bipartite_graph.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using DocStringExtensions
1414
using UnPack
1515
using SparseArrays
1616
using Graphs
17-
using Setfield
17+
using Accessors
1818

1919
### Matching
2020
struct Unassigned

src/inputoutput.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,12 @@ function inputs_to_parameters!(state::TransformationState, io)
315315
@assert new_v > 0
316316
new_var_to_diff[new_i] = new_v
317317
end
318-
@set! structure.var_to_diff = complete(new_var_to_diff)
319-
@set! structure.graph = complete(new_graph)
318+
@reset structure.var_to_diff = complete(new_var_to_diff)
319+
@reset structure.graph = complete(new_graph)
320320

321-
@set! sys.eqs = isempty(input_to_parameters) ? equations(sys) :
321+
@reset sys.eqs = isempty(input_to_parameters) ? equations(sys) :
322322
fast_substitute(equations(sys), input_to_parameters)
323-
@set! sys.unknowns = setdiff(unknowns(sys), keys(input_to_parameters))
323+
@reset sys.unknowns = setdiff(unknowns(sys), keys(input_to_parameters))
324324
ps = parameters(sys)
325325

326326
if io !== nothing
@@ -334,11 +334,11 @@ function inputs_to_parameters!(state::TransformationState, io)
334334
new_parameters = new_parameters[permutation]
335335
end
336336

337-
@set! sys.ps = [ps; new_parameters]
337+
@reset sys.ps = [ps; new_parameters]
338338

339-
@set! state.sys = sys
340-
@set! state.fullvars = new_fullvars
341-
@set! state.structure = structure
339+
@reset state.sys = sys
340+
@reset state.fullvars = new_fullvars
341+
@reset state.structure = structure
342342
base_params = length(ps)
343343
return state, (base_params + 1):(base_params + length(new_parameters)) # (1:length(new_parameters)) .+ base_params
344344
end

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module StructuralTransformations
22

3-
using Setfield: @set!, @set
3+
using Accessors: @set, @reset
44
using UnPack: @unpack
55

66
using Symbolics: unwrap, linear_expansion, fast_substitute

src/structural_transformation/codegen.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ function build_torn_function(sys;
300300
rhss)
301301

302302
unknown_vars = Any[fullvars[i] for i in unknowns_idxs]
303-
@set! sys.solved_unknowns = unknown_vars
303+
@reset sys.solved_unknowns = unknown_vars
304304

305305
pre = get_postprocess_fbody(sys)
306306
cpre = get_preprocess_constants(rhss)

src/structural_transformation/pantelides.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ function pantelides_reassemble(state::TearingState, var_eq_matching)
6565
filter(x -> value(x.lhs) !== nothing,
6666
out_eqs[sort(filter(x -> x !== unassigned, var_eq_matching))]))
6767

68-
@set! sys.eqs = final_eqs
69-
@set! sys.unknowns = final_vars
68+
@reset sys.eqs = final_eqs
69+
@reset sys.unknowns = final_vars
7070
return sys
7171
end
7272

0 commit comments

Comments
 (0)