Skip to content

Commit 33cef2f

Browse files
Merge pull request #106 from SciML/as/setsym-oop
feat: add `setsym_oop`
2 parents 7b37511 + e8934c4 commit 33cef2f

16 files changed

+263
-123
lines changed

docs/src/api.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ is_markovian
4646

4747
If the index provider contains parameters that change during the course of the simulation
4848
at discrete time points, it must implement the following methods to ensure correct
49-
functioning of [`getu`](@ref) and [`getp`](@ref) for value providers that save the parameter
49+
functioning of [`getsym`](@ref) and [`getp`](@ref) for value providers that save the parameter
5050
timeseries. Note that there can be multiple parameter timeseries, in case different parameters
5151
may change at different times.
5252

@@ -69,10 +69,13 @@ is_timeseries
6969
state_values
7070
set_state!
7171
current_time
72-
getu
73-
setu
72+
getsym
73+
setsym
7474
```
7575

76+
!!! note
77+
`getu` and `setu` have been renamed to [`getsym`](@ref) and [`setsym`](@ref) respectively.
78+
7679
#### Historical value providers
7780

7881
```@docs
@@ -95,7 +98,7 @@ ParameterIndexingProxy
9598

9699
If a solution object saves a timeseries of parameter values that are updated during the
97100
simulation (such as by callbacks), it must implement the following methods to ensure
98-
correct functioning of [`getu`](@ref) and [`getp`](@ref).
101+
correct functioning of [`getsym`](@ref) and [`getp`](@ref).
99102

100103
Parameter timeseries support requires that the value provider store the different
101104
timeseries in a [`ParameterTimeseriesCollection`](@ref).

docs/src/complete_sii.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ end
174174
```
175175

176176
If a type contains the value of state variables, it can define [`state_values`](@ref) to
177-
enable the usage of [`getu`](@ref) and [`setu`](@ref). These methods retturn getter/
177+
enable the usage of [`getsym`](@ref) and [`setsym`](@ref). These methods retturn getter/
178178
setter functions to access or update the value of a state variable (or a collection of
179-
them). If the type also supports generating [`observed`](@ref) functions, `getu` also
179+
them). If the type also supports generating [`observed`](@ref) functions, `getsym` also
180180
enables returning functions to access the value of arbitrary expressions involving
181181
the system's symbols. This also requires that the type implement
182182
[`parameter_values`](@ref) and [`current_time`](@ref) (if the system is time-dependent).
@@ -202,21 +202,21 @@ Then the following example would work:
202202
```julia
203203
sys = ExampleSystem(Dict(:x => 1, :y => 2, :z => 3), Dict(:a => 1, :b => 2), :t, Dict())
204204
integrator = ExampleIntegrator([1.0, 2.0, 3.0], [4.0, 5.0], 6.0, sys)
205-
getx = getu(sys, :x)
205+
getx = getsym(sys, :x)
206206
getx(integrator) # 1.0
207207

208-
get_expr = getu(sys, :(x + y + t))
208+
get_expr = getsym(sys, :(x + y + t))
209209
get_expr(integrator) # 13.0
210210

211-
setx! = setu(sys, :y)
211+
setx! = setsym(sys, :y)
212212
setx!(integrator, 0.0)
213213
getx(integrator) # 0.0
214214
```
215215

216216
In case a type stores timeseries data (such as solutions), then it must also implement
217217
the [`Timeseries`](@ref) trait. The type would then return a timeseries from
218218
[`state_values`](@ref) and [`current_time`](@ref) and the function returned from
219-
[`getu`](@ref) would then return a timeseries as well. For example, consider the
219+
[`getsym`](@ref) would then return a timeseries as well. For example, consider the
220220
`ExampleSolution` below:
221221

222222
```julia
@@ -242,20 +242,20 @@ Then the following example would work:
242242
```julia
243243
# using the same system that the ExampleIntegrator used
244244
sol = ExampleSolution([[1.0, 2.0, 3.0], [1.5, 2.5, 3.5]], [4.0, 5.0], [6.0, 7.0], sys)
245-
getx = getu(sys, :x)
245+
getx = getsym(sys, :x)
246246
getx(sol) # [1.0, 1.5]
247247

248-
get_expr = getu(sys, :(x + y + t))
248+
get_expr = getsym(sys, :(x + y + t))
249249
get_expr(sol) # [9.0, 11.0]
250250

251-
get_arr = getu(sys, [:y, :(x + a)])
251+
get_arr = getsym(sys, [:y, :(x + a)])
252252
get_arr(sol) # [[2.0, 5.0], [2.5, 5.5]]
253253

254-
get_tuple = getu(sys, (:z, :(z * t)))
254+
get_tuple = getsym(sys, (:z, :(z * t)))
255255
get_tuple(sol) # [(3.0, 18.0), (3.5, 24.5)]
256256
```
257257

258-
Note that `setu` is not designed to work for `Timeseries` objects.
258+
Note that `setsym` is not designed to work for `Timeseries` objects.
259259

260260
If a type needs to perform some additional actions when updating the state/parameters
261261
or if it is not possible to return a mutable reference to the state/parameter vector
@@ -315,7 +315,7 @@ setp(integrator, :b)(integrator, 3.0) # functionally the same as above
315315

316316
If a solution object includes modified parameter values (such as through callbacks) during the
317317
simulation, it must implement several additional functions for correct functioning of
318-
[`getu`](@ref) and [`getp`](@ref). [`ParameterTimeseriesCollection`](@ref) helps in
318+
[`getsym`](@ref) and [`getp`](@ref). [`ParameterTimeseriesCollection`](@ref) helps in
319319
implementing parameter timeseries objects. The following mockup gives an example of
320320
correct implementation of these functions and the indexing syntax they enable.
321321

@@ -457,12 +457,12 @@ sol.ps[:(b + c)] # observed quantities work too
457457
```
458458

459459
```@example param_timeseries
460-
getu(sol, :b)(sol) # works
460+
getsym(sol, :b)(sol) # works
461461
```
462462

463463
```@example param_timeseries
464464
try
465-
getu(sol, [:b, :d])(sol) # errors since :b and :d belong to different timeseries
465+
getsym(sol, [:b, :d])(sol) # errors since :b and :d belong to different timeseries
466466
catch e
467467
showerror(stdout, e)
468468
end

docs/src/usage.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ sol[allvariables] # equivalent to sol[all_variable_symbols(sol)]
123123

124124
### Evaluating expressions
125125

126-
`getu` also generates functions for expressions if the object passed to it supports
126+
`getsym` also generates functions for expressions if the object passed to it supports
127127
[`observed`](@ref). For example:
128128

129129
```@example Usage
130-
getu(prob, x + y + z)(prob)
130+
getsym(prob, x + y + z)(prob)
131131
```
132132

133133
To evaluate this function using values other than the ones contained in `prob`, we need
@@ -137,7 +137,7 @@ which has trivial implementations of the above functions. We can thus do:
137137

138138
```@example Usage
139139
temp_state = ProblemState(; u = [0.1, 0.2, 0.3, 0.4], p = parameter_values(prob))
140-
getu(prob, x + y + z)(temp_state)
140+
getsym(prob, x + y + z)(temp_state)
141141
```
142142

143143
Note that providing all of the state vector, parameter object and time may not be

src/SymbolicIndexingInterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ include("parameter_timeseries_collection.jl")
3535
export getp, setp, setp_oop
3636
include("parameter_indexing.jl")
3737

38-
export getu, setu
38+
export getsym, setsym, getu, setu
3939
include("state_indexing.jl")
4040

4141
export BatchedInterface, setsym_oop, associated_systems

src/batched_interface.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
struct BatchedInterface{S <: AbstractVector, I}
33
function BatchedInterface(indp_syms::Tuple...)
44
5-
A struct which stores information for batched calls to [`getu`](@ref) or [`setu`](@ref).
5+
A struct which stores information for batched calls to [`getsym`](@ref) or [`setsym`](@ref).
66
Given `Tuple`s, where the first element of each tuple is an index provider and the second
77
an array of symbolic variables (either states or parameters) in the index provider,
88
`BatchedInterface` will compute the union of all symbols and associate each symbol with
@@ -17,7 +17,7 @@ be retained internally.
1717
`BatchedInterface` implements [`variable_symbols`](@ref), [`is_variable`](@ref),
1818
[`variable_index`](@ref) to query the order of symbols in the union.
1919
20-
See [`getu`](@ref) and [`setu`](@ref) for further details.
20+
See [`getsym`](@ref) and [`setsym`](@ref) for further details.
2121
2222
See also: [`associated_systems`](@ref).
2323
"""
@@ -118,7 +118,7 @@ is the index of the index provider associated with the corresponding symbol in
118118
associated_systems(bi::BatchedInterface) = bi.associated_systems
119119

120120
"""
121-
getu(bi::BatchedInterface)
121+
getsym(bi::BatchedInterface)
122122
123123
Given a [`BatchedInterface`](@ref) composed from `n` index providers (and corresponding
124124
symbols), return a function which takes `n` corresponding value providers and returns an
@@ -127,7 +127,7 @@ an `AbstractArray` of the appropriate `eltype` and size as its first argument, i
127127
case the operation will populate the array in-place with the values of the symbols in the
128128
union.
129129
130-
Note that all of the value providers passed to the function returned by `getu` must satisfy
130+
Note that all of the value providers passed to the function returned by `getsym` must satisfy
131131
`is_timeseries(prob) === NotTimeseries()`.
132132
133133
The value of the `i`th symbol in the union (obtained through `variable_symbols(bi)[i]`) is
@@ -137,7 +137,7 @@ provider at index `associated_systems(bi)[i]`).
137137
See also: [`variable_symbols`](@ref), [`associated_systems`](@ref), [`is_timeseries`](@ref),
138138
[`NotTimeseries`](@ref).
139139
"""
140-
function getu(bi::BatchedInterface)
140+
function getsym(bi::BatchedInterface)
141141
numprobs = length(bi.system_to_symbol_subset)
142142
probnames = [Symbol(:prob, i) for i in 1:numprobs]
143143

@@ -189,13 +189,13 @@ function getu(bi::BatchedInterface)
189189
end
190190

191191
"""
192-
setu(bi::BatchedInterface)
192+
setsym(bi::BatchedInterface)
193193
194194
Given a [`BatchedInterface`](@ref) composed from `n` index providers (and corresponding
195195
symbols), return a function which takes `n` corresponding problems and an array of the
196196
values, and updates each of the problems with the values of the corresponding symbols.
197197
198-
Note that all of the value providers passed to the function returned by `setu` must satisfy
198+
Note that all of the value providers passed to the function returned by `setsym` must satisfy
199199
`is_timeseries(prob) === NotTimeseries()`.
200200
201201
Note that if any subset of the `n` index providers share common symbols (among those passed
@@ -204,7 +204,7 @@ updated with the values of the common symbols.
204204
205205
See also: [`is_timeseries`](@ref), [`NotTimeseries`](@ref).
206206
"""
207-
function setu(bi::BatchedInterface)
207+
function setsym(bi::BatchedInterface)
208208
numprobs = length(bi.system_to_symbol_subset)
209209
probnames = [Symbol(:prob, i) for i in 1:numprobs]
210210

src/parameter_indexing.jl

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -726,52 +726,26 @@ function setp_oop(indp, sym)
726726
return _setp_oop(indp, symtype, elsymtype, sym)
727727
end
728728

729-
struct OOPSetter{I, D}
730-
indp::I
731-
idxs::D
732-
end
733-
734-
function (os::OOPSetter)(valp, val)
735-
return remake_buffer(os.indp, parameter_values(valp), (os.idxs,), (val,))
736-
end
737-
738-
function (os::OOPSetter)(valp, val::Union{Tuple, AbstractArray})
739-
if os.idxs isa Union{Tuple, AbstractArray}
740-
return remake_buffer(os.indp, parameter_values(valp), os.idxs, val)
741-
else
742-
return remake_buffer(os.indp, parameter_values(valp), (os.idxs,), (val,))
743-
end
744-
end
745-
746-
function _root_indp(indp)
747-
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
748-
(sc = symbolic_container(indp)) != indp
749-
return _root_indp(sc)
750-
else
751-
return indp
752-
end
753-
end
754-
755729
function _setp_oop(indp, ::NotSymbolic, ::NotSymbolic, sym)
756-
return OOPSetter(_root_indp(indp), sym)
730+
return OOPSetter(_root_indp(indp), sym, false)
757731
end
758732

759733
function _setp_oop(indp, ::ScalarSymbolic, ::SymbolicTypeTrait, sym)
760-
return OOPSetter(_root_indp(indp), parameter_index(indp, sym))
734+
return OOPSetter(_root_indp(indp), parameter_index(indp, sym), false)
761735
end
762736

763737
for (t1, t2) in [
764738
(ScalarSymbolic, Any),
765739
(NotSymbolic, Union{<:Tuple, <:AbstractArray})
766740
]
767741
@eval function _setp_oop(indp, ::NotSymbolic, ::$t1, sym::$t2)
768-
return OOPSetter(_root_indp(indp), parameter_index.((indp,), sym))
742+
return OOPSetter(_root_indp(indp), parameter_index.((indp,), sym), false)
769743
end
770744
end
771745

772746
function _setp_oop(indp, ::ArraySymbolic, ::SymbolicTypeTrait, sym)
773747
if is_parameter(indp, sym)
774-
return OOPSetter(_root_indp(indp), parameter_index(indp, sym))
748+
return OOPSetter(_root_indp(indp), parameter_index(indp, sym), false)
775749
end
776750
error("$sym is not a valid parameter")
777751
end

src/problem_state.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
function ProblemState(; u = nothing, p = nothing, t = nothing)
44
55
A value provider struct which can be used as an argument to the function returned by
6-
[`getu`](@ref) or [`setu`](@ref). It stores the state vector, parameter object and
6+
[`getsym`](@ref) or [`setsym`](@ref). It stores the state vector, parameter object and
77
current time, and forwards calls to [`state_values`](@ref), [`parameter_values`](@ref),
88
[`current_time`](@ref), [`set_state!`](@ref), [`set_parameter!`](@ref) to the contained
99
objects.

src/remake.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function remake_buffer(sys, oldbuffer::AbstractArray, idxs, vals)
4646
else
4747
v = elT(v)
4848
end
49-
setu(sys, k)(newbuffer, v)
49+
setsym(sys, k)(newbuffer, v)
5050
end
5151
else
5252
mutbuffer = remake_buffer(sys, collect(oldbuffer), idxs, vals)
@@ -80,7 +80,7 @@ end
8080
function remake_buffer(sys, oldbuffer::Tuple, idxs, vals)
8181
wrap = TupleRemakeWrapper(oldbuffer)
8282
for (idx, val) in zip(idxs, vals)
83-
setu(sys, idx)(wrap, val)
83+
setsym(sys, idx)(wrap, val)
8484
end
8585
return wrap.t
8686
end

0 commit comments

Comments
 (0)