diff --git a/src/mcs/mcs_types.jl b/src/mcs/mcs_types.jl index 059cf8c00..27e1b792b 100644 --- a/src/mcs/mcs_types.jl +++ b/src/mcs/mcs_types.jl @@ -161,6 +161,7 @@ mutable struct SimulationInstance{T} sim_def::SimulationDef{T} where T <: AbstractSimulationData models::Vector{Model} results::Vector{Dict{Tuple, DataFrame}} + payload::Any function SimulationInstance{T}(sim_def::SimulationDef{T}) where T <: AbstractSimulationData self = new() @@ -168,7 +169,8 @@ mutable struct SimulationInstance{T} self.current_trial = 0 self.current_data = nothing self.sim_def = deepcopy(sim_def) - + self.payload = deepcopy(self.sim_def.payload) + # These are parallel arrays; each model has a corresponding results dict self.models = Vector{Model}(undef, 0) self.results = [Dict{Tuple, DataFrame}()] @@ -189,7 +191,8 @@ end """ set_payload!(sim_def::SimulationDef, payload) -Attach a user's `payload` to the `SimulationDef` instance so it can be +Attach a user's `payload` to the `SimulationDef`. A copy of the payload object +will be stored in the `SimulationInstance` at run time so it can be accessed in scenario and pre-/post-trial callback functions. The value is not used by Mimi in any way; it can be anything useful to the user. """ @@ -202,6 +205,13 @@ Return the `payload` value set by the user via `set_payload!()`. """ payload(sim_def::SimulationDef) = sim_def.payload +""" + payload(sim_inst::SimulationInstance) + +Return the copy of the `payload` value stored in the `SimulationInstance` set by the user via `set_payload!()`. +""" +payload(sim_inst::SimulationInstance) = sim_inst.payload + struct MCSData <: AbstractSimulationData end const MonteCarloSimulationDef = SimulationDef{MCSData} diff --git a/test/mcs/runtests.jl b/test/mcs/runtests.jl index b428aed11..0eaf2d09a 100644 --- a/test/mcs/runtests.jl +++ b/test/mcs/runtests.jl @@ -14,4 +14,7 @@ using Test @info("test_reshaping.jl") include("test_reshaping.jl") + + @info("test_payload.jl") + include("test_payload.jl") end diff --git a/test/mcs/test_payload.jl b/test/mcs/test_payload.jl new file mode 100644 index 000000000..46b35cb96 --- /dev/null +++ b/test/mcs/test_payload.jl @@ -0,0 +1,28 @@ +using Mimi +using Test + +@defcomp c begin +end + +m = Model() +set_dimension!(m, :time, 1:2) +add_comp!(m, c) + +function post_trial(sim_inst::SimulationInstance, trialnum::Int, ntimesteps::Int, tup::Union{Nothing, Tuple}) + data = Mimi.payload(sim_inst) + data[trialnum] = trialnum +end + +sim_def = @defsim begin +end + +trials = 10 + +original_payload = zeros(trials) +Mimi.set_payload!(sim_def, original_payload) + +sim_inst = run(sim_def, m, trials, post_trial_func = post_trial) + +@test Mimi.payload(sim_def) == original_payload # in the original defintion, it's still zeros +@test Mimi.payload(sim_inst) == collect(1:trials) # in the instance, it's now 1:10 +@test Mimi.payload(sim_inst.sim_def) == original_payload # the definition stored in the instance still holds the unmodified payload object \ No newline at end of file