-
-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathimperative_affect.jl
More file actions
381 lines (345 loc) · 13.8 KB
/
imperative_affect.jl
File metadata and controls
381 lines (345 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
ImperativeAffect(f::Function; modified::NamedTuple, observed::NamedTuple, ctx)
`ImperativeAffect` is a helper for writing affect functions that will compute observed values and
ensure that modified values are correctly written back into the system. The affect function `f` needs to have
the signature
```
f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple
```
The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions.
Each declaration`NamedTuple` should map an expression to a symbol; for example if we pass `observed=(; x = a + b)` this will alias the result of executing `a+b` in the system as `x`
so the value of `a + b` will be accessible as `observed.x` in `f`. `modified` currently restricts symbolic expressions to only bare variables, so only tuples of the form
`(; x = y)` or `(; x)` (which aliases `x` as itself) are allowed.
The argument NamedTuples (for instance `(;x=y)`) will be populated with the declared values on function entry; if we require `(;x=y)` in `observed` and `y=2`, for example,
then the NamedTuple `(;x=2)` will be passed as `observed` to the affect function `f`.
The NamedTuple returned from `f` includes the values to be written back to the system after `f` returns. For example, if we want to update the value of `x` to be the result of `x + y` we could write
ImperativeAffect(observed=(; x_plus_y = x + y), modified=(; x)) do m, o
@set! m.x = o.x_plus_y
end
Where we use Setfield to copy the tuple `m` with a new value for `x`, then return the modified value of `m`. All values updated by the tuple must have names originally declared in
`modified`; a runtime error will be produced if a value is written that does not appear in `modified`. The user can dynamically decide not to write a value back by not including it
in the returned tuple, in which case the associated field will not be updated.
"""
struct ImperativeAffect
f::Any
obs::Vector
obs_syms::Vector{Symbol}
modified::Vector
mod_syms::Vector{Symbol}
ctx::Any
skip_checks::Bool
end
function ImperativeAffect(
f;
observed::NamedTuple = NamedTuple{()}(()),
modified::NamedTuple = NamedTuple{()}(()),
ctx = nothing,
skip_checks = false
)
return ImperativeAffect(
f,
collect(values(observed)), collect(keys(observed)),
collect(values(modified)), collect(keys(modified)),
ctx, skip_checks
)
end
function ImperativeAffect(
f, modified::NamedTuple;
observed::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks = false
)
return ImperativeAffect(
f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks
)
end
function ImperativeAffect(
f, modified::NamedTuple, observed::NamedTuple; ctx = nothing, skip_checks = false
)
return ImperativeAffect(
f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks
)
end
function ImperativeAffect(
f, modified::NamedTuple, observed::NamedTuple, ctx; skip_checks = false
)
return ImperativeAffect(
f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks
)
end
function ImperativeAffect(; f, kwargs...)
return ImperativeAffect(f; kwargs...)
end
function (s::SymbolicUtils.Substituter)(aff::ImperativeAffect)
return ImperativeAffect(
aff.f, s(aff.obs), aff.obs_syms,
s(aff.modified), aff.mod_syms, aff.ctx, aff.skip_checks
)
end
function Base.show(io::IO, mfa::ImperativeAffect)
obs_vals = join(map((ob, nm) -> "$ob => $nm", mfa.obs, mfa.obs_syms), ", ")
mod_vals = join(map((md, nm) -> "$md => $nm", mfa.modified, mfa.mod_syms), ", ")
affect = mfa.f
return print(
io,
"ImperativeAffect(observed: [$obs_vals], modified: [$mod_vals], affect:$affect)"
)
end
func(f::ImperativeAffect) = f.f
context(a::ImperativeAffect) = a.ctx
observed(a::ImperativeAffect) = a.obs
observed_syms(a::ImperativeAffect) = a.obs_syms
function discretes(a::ImperativeAffect)
discs = SymbolicT[]
for val in a.modified
val = unwrap(val)
if val isa SymbolicT
isparameter(val) && push!(discs, val)
elseif val isa AbstractArray
append!(discs, filter(isparameter, map(unwrap, val)))
end
end
return discs
end
modified(a::ImperativeAffect) = a.modified
modified_syms(a::ImperativeAffect) = a.mod_syms
function Base.:(==)(a1::ImperativeAffect, a2::ImperativeAffect)
return isequal(a1.f, a2.f) && isequal(a1.obs, a2.obs) && isequal(a1.modified, a2.modified) &&
isequal(a1.obs_syms, a2.obs_syms) && isequal(a1.mod_syms, a2.mod_syms) &&
isequal(a1.ctx, a2.ctx)
end
function Base.hash(a::ImperativeAffect, s::UInt)
s = hash(a.f, s)::UInt
s = hash(a.obs, s)::UInt
s = hash(a.obs_syms, s)::UInt
s = hash(a.modified, s)::UInt
s = hash(a.mod_syms, s)::UInt
return hash(a.ctx, s)::UInt
end
namespace_affects(af::ImperativeAffect, s) = namespace_affect(af, s)
function namespace_affect(affect::ImperativeAffect, s)
rmn = []
for modded in modified(affect)
if symbolic_type(modded) == NotSymbolic() && modded isa AbstractArray
res = []
for m in modded
push!(res, renamespace(s, m))
end
push!(rmn, res)
else
push!(rmn, renamespace(s, modded))
end
end
return ImperativeAffect(
func(affect),
namespace_expr.(observed(affect), (s,)),
observed_syms(affect),
rmn,
modified_syms(affect),
context(affect),
affect.skip_checks
)
end
function invalid_variables(sys, expr)
return setdiff!(SU.search_variables(expr), all_symbols(sys))
end
function unassignable_variables(sys, expr)
assignable_syms = Set{SymbolicT}()
union!(assignable_syms, unknowns(sys))
for p in parameters(sys; initial_parameters = true)
if Symbolics.isarraysymbolic(p)
for idx in SU.stable_eachindex(p)
push!(assignable_syms, p[idx])
end
else
push!(assignable_syms, p)
end
end
vars = Set{SymbolicT}()
SU.search_variables!(vars, expr)
unassignable = SymbolicT[]
for var in vars
if Symbolics.isarraysymbolic(var)
for idx in SU.stable_eachindex(var)
sym = var[idx]
sym in assignable_syms && continue
push!(unassignable, sym)
end
else
var in assignable_syms && continue
push!(unassignable, var)
end
end
return unassignable
end
@generated function _generated_writeback(
integ, setters::NamedTuple{NS1, <:Tuple},
values::NamedTuple{NS2, <:Tuple}
) where {NS1, NS2}
setter_exprs = []
for name in NS2
if !(name in NS1)
missing_name = "Tried to write back to $name from affect; only declared states ($NS1) may be written to."
error(missing_name)
end
push!(setter_exprs, :(setters.$name(integ, values.$name)))
end
return :(
begin
$(setter_exprs...)
end
)
end
function check_assignable(sys, sym)
return if symbolic_type(sym) == ScalarSymbolic()
is_variable(sys, sym) || is_parameter(sys, sym)
elseif symbolic_type(sym) == ArraySymbolic()
is_variable(sys, sym) || is_parameter(sys, sym) ||
all(x -> check_assignable(sys, x), collect(sym))
elseif sym isa Union{AbstractArray, Tuple}
all(x -> check_assignable(sys, x), sym)
else
false
end
end
"""
FunctionalAffect{UF, CTX, MOF, OF, MN, OST, UPS}
Callable struct representing a compiled [`ImperativeAffect`](@ref). On each invocation,
reads current modified and observed values from the integrator, calls the user function,
and writes back the returned values. Created by [`compile_functional_affect`](@ref).
# Fields
- `user_affect`: the user-supplied function with signature `f(modified, observed, ctx, integ)`
- `ctx`: user-supplied context object passed through to `user_affect`
- `reset_jumps`: if `true`, call `reset_aggregated_jumps!` after the affect
- `mod_og_val_fun`: OOP function that reads the current values of all modified variables
- `obs_fun`: OOP function that reads the observed expressions
- `mod_names`: `NTuple{N, Symbol}` naming the modified variables (for `NamedTuple` construction)
- `obs_sym_tuple`: `NTuple{M, Symbol}` naming the observed values
- `upd_funs`: `NamedTuple` of setter callables mapping each modified variable name to its setter
"""
struct FunctionalAffect{UF, CTX, MOF, OF, MN, OST, UPS}
user_affect::UF
ctx::CTX
reset_jumps::Bool
mod_og_val_fun::MOF
obs_fun::OF
mod_names::MN
obs_sym_tuple::OST
upd_funs::UPS
end
@inline function (fa::FunctionalAffect)(integ)
# Read the current values of the modified variables (pre-affect baseline)
modvals = fa.mod_og_val_fun(integ.u, integ.p, integ.t)
upd_component_array = NamedTuple{fa.mod_names}(modvals)
# Read the observed expressions
obs_component_array = NamedTuple{fa.obs_sym_tuple}(
fa.obs_fun(integ.u, integ.p, integ.t)
)
# Call the user-supplied affect function
upd_vals = fa.user_affect(upd_component_array, obs_component_array, fa.ctx, integ)
# Write back any returned values
if !isnothing(upd_vals)
_generated_writeback(integ, fa.upd_funs, upd_vals)
end
return fa.reset_jumps && reset_aggregated_jumps!(integ)
end
"""
compile_functional_affect(affect::ImperativeAffect, sys; reset_jumps, kwargs...)
Compile an `ImperativeAffect` into a [`FunctionalAffect`](@ref) callable struct. Generates
OOP observed and modified-value reader functions via `build_explicit_observed_function`, and
setter callables via `setu`. All captures are encoded as typed struct fields for type stability.
# Arguments
- `affect`: the `ImperativeAffect` specification
- `sys`: the parent system providing compilation context
- `reset_jumps`: if `true`, call `reset_aggregated_jumps!` after each invocation
# Returns
A `FunctionalAffect` struct callable as `f(integrator)`.
"""
function compile_functional_affect(
affect::ImperativeAffect, sys; reset_jumps = false, kwargs...
)
#=
Implementation sketch:
generate observed function (oop), should save to a component array under obs_syms
do the same stuff as the normal FA for pars_syms
call the affect method
unpack and apply the resulting values
=#
function check_dups(syms, exprs) # = (syms_dedup, exprs_dedup)
seen = Set{Symbol}()
syms_dedup = []
exprs_dedup = []
for (sym, exp) in Iterators.zip(syms, exprs)
if !in(sym, seen)
push!(syms_dedup, sym)
push!(exprs_dedup, exp)
push!(seen, sym)
elseif !affect.skip_checks
@warn "Expression $(expr) is aliased as $sym, which has already been used. The first definition will be used."
end
end
return (syms_dedup, exprs_dedup)
end
dvs = unknowns(sys)
ps = parameters(sys)
obs_exprs = observed(affect)
if !affect.skip_checks
for oexpr in obs_exprs
invalid_vars = invalid_variables(sys, oexpr)
if length(invalid_vars) > 0
error(lazy"Observed equation $(oexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing).")
end
end
end
obs_syms = observed_syms(affect)
obs_syms, obs_exprs = check_dups(obs_syms, obs_exprs)
mod_exprs = modified(affect)
if !affect.skip_checks
for mexpr in mod_exprs
if !check_assignable(sys, mexpr)
@warn ("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.")
end
invalid_vars = unassignable_variables(sys, mexpr)
if length(invalid_vars) > 0
error(lazy"Modified equation $(mexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing) or they may have been reduced away.")
end
end
end
mod_syms = modified_syms(affect)
mod_syms, mod_exprs = check_dups(mod_syms, mod_exprs)
overlapping_syms = intersect(mod_syms, obs_syms)
if length(overlapping_syms) > 0 && !affect.skip_checks
@warn "The symbols $overlapping_syms are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value."
end
# sanity checks done! now build the data and update function for observed values
mkzero(sz) =
if sz === ()
0.0
else
zeros(sz)
end
obs_fun = build_explicit_observed_function(
sys, Symbolics.scalarize.(obs_exprs);
mkarray = (es, _) -> MakeTuple(es)
)
obs_sym_tuple = (obs_syms...,)
# okay so now to generate the stuff to assign it back into the system
mod_pairs = mod_exprs .=> mod_syms
mod_names = (mod_syms...,)
mod_og_val_fun = build_explicit_observed_function(
sys, Symbolics.scalarize.(first.(mod_pairs));
mkarray = (es, _) -> MakeTuple(es)
)
upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,))
return FunctionalAffect(
func(affect), context(affect), reset_jumps,
mod_og_val_fun, obs_fun, mod_names, obs_sym_tuple, upd_funs
)
end
scalarize_affects(affects::ImperativeAffect) = affects
function SU.search_variables!(vars, aff::ImperativeAffect; kwargs...)
for var in Iterators.flatten((observed(aff), modified(aff)))
if symbolic_type(var) == NotSymbolic()
SU.search_variables!(vars, v; kwargs...)
end
end
return
end