From dc2d8bfe5f228cfa8177e1ee3af163d6b3b4c81d Mon Sep 17 00:00:00 2001 From: Cora Kingdon Date: Wed, 4 Sep 2019 14:34:25 -0400 Subject: [PATCH 1/6] add payload retrieval function for SimulationInstance --- src/mcs/mcs_types.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mcs/mcs_types.jl b/src/mcs/mcs_types.jl index 059cf8c00..c2ff01d5e 100644 --- a/src/mcs/mcs_types.jl +++ b/src/mcs/mcs_types.jl @@ -201,6 +201,7 @@ set_payload!(sim_def::SimulationDef, payload) = (sim_def.payload = payload) Return the `payload` value set by the user via `set_payload!()`. """ payload(sim_def::SimulationDef) = sim_def.payload +payload(sim_inst::SimulationInstance) = sim_inst.sim_def.payload struct MCSData <: AbstractSimulationData end From aaa02b0c7a1afe63a1b65a74c70775d92f0ba7e7 Mon Sep 17 00:00:00 2001 From: Cora Kingdon Date: Tue, 10 Sep 2019 09:44:48 -0400 Subject: [PATCH 2/6] make payload field in SimulationInstance --- src/mcs/mcs_types.jl | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/mcs/mcs_types.jl b/src/mcs/mcs_types.jl index c2ff01d5e..ded745224 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 = 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. """ @@ -201,7 +204,13 @@ set_payload!(sim_def::SimulationDef, payload) = (sim_def.payload = payload) Return the `payload` value set by the user via `set_payload!()`. """ payload(sim_def::SimulationDef) = sim_def.payload -payload(sim_inst::SimulationInstance) = sim_inst.sim_def.payload + +""" + payload(sim_def::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 From 891246ed247b1f4d361213cc7f0c212b5ccb6500 Mon Sep 17 00:00:00 2001 From: Cora Kingdon Date: Tue, 10 Sep 2019 09:46:28 -0400 Subject: [PATCH 3/6] fix docstring --- src/mcs/mcs_types.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mcs/mcs_types.jl b/src/mcs/mcs_types.jl index ded745224..16804cb26 100644 --- a/src/mcs/mcs_types.jl +++ b/src/mcs/mcs_types.jl @@ -206,7 +206,7 @@ Return the `payload` value set by the user via `set_payload!()`. payload(sim_def::SimulationDef) = sim_def.payload """ - payload(sim_def::SimulationInstance) + payload(sim_inst::SimulationInstance) Return the copy of the `payload` value stored in the `SimulationInstance` set by the user via `set_payload!()`. """ From b645c25b3b521c8c99b0249e547e6d95bb869d0f Mon Sep 17 00:00:00 2001 From: Cora Kingdon Date: Tue, 10 Sep 2019 10:00:24 -0400 Subject: [PATCH 4/6] create additional copy of payload --- src/mcs/mcs_types.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mcs/mcs_types.jl b/src/mcs/mcs_types.jl index 16804cb26..71f765b49 100644 --- a/src/mcs/mcs_types.jl +++ b/src/mcs/mcs_types.jl @@ -169,7 +169,7 @@ mutable struct SimulationInstance{T} self.current_trial = 0 self.current_data = nothing self.sim_def = deepcopy(sim_def) - self.payload = self.sim_def.payload + self.payload = copy(self.sim_def.payload) # These are parallel arrays; each model has a corresponding results dict self.models = Vector{Model}(undef, 0) From 65d3151067716a7b1c0357ce818d2e2ad0a66e70 Mon Sep 17 00:00:00 2001 From: Cora Kingdon Date: Tue, 10 Sep 2019 16:52:52 -0400 Subject: [PATCH 5/6] use deepcopy for payload --- src/mcs/mcs_types.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mcs/mcs_types.jl b/src/mcs/mcs_types.jl index 71f765b49..27e1b792b 100644 --- a/src/mcs/mcs_types.jl +++ b/src/mcs/mcs_types.jl @@ -169,7 +169,7 @@ mutable struct SimulationInstance{T} self.current_trial = 0 self.current_data = nothing self.sim_def = deepcopy(sim_def) - self.payload = copy(self.sim_def.payload) + self.payload = deepcopy(self.sim_def.payload) # These are parallel arrays; each model has a corresponding results dict self.models = Vector{Model}(undef, 0) From 295022449027d1db6dfee13fc9c59749dcc7cec0 Mon Sep 17 00:00:00 2001 From: Cora Kingdon Date: Fri, 13 Sep 2019 15:16:45 -0400 Subject: [PATCH 6/6] Add tests for payload --- test/mcs/runtests.jl | 3 +++ test/mcs/test_payload.jl | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 test/mcs/test_payload.jl 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