Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/mcs/mcs_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,16 @@ 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()
self.trials = 0
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}()]
Expand All @@ -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.
"""
Expand All @@ -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}
Expand Down
3 changes: 3 additions & 0 deletions test/mcs/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ using Test

@info("test_reshaping.jl")
include("test_reshaping.jl")

@info("test_payload.jl")
include("test_payload.jl")
end
28 changes: 28 additions & 0 deletions test/mcs/test_payload.jl
Original file line number Diff line number Diff line change
@@ -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