From a2b1019bf67b029d9e6d63cf106f18193b11994d Mon Sep 17 00:00:00 2001 From: Matthew Gidden Date: Mon, 26 May 2014 14:27:37 -0500 Subject: [PATCH 1/2] added integration test for growth region/manager inst --- .../source_sink_linear.xml | 39 ++++-- src/manager_inst.cc | 25 ++-- src/manager_inst.h | 3 + .../{Inputs => input}/dynamic_capacitated.xml | 0 tests/input/growth.xml | 114 ++++++++++++++++++ tests/test_dynamic_capacitated.py | 2 +- tests/test_growth.py | 62 ++++++++++ 7 files changed, 226 insertions(+), 19 deletions(-) rename input/{growthtest => growth}/source_sink_linear.xml (72%) rename tests/{Inputs => input}/dynamic_capacitated.xml (100%) create mode 100644 tests/input/growth.xml create mode 100644 tests/test_growth.py diff --git a/input/growthtest/source_sink_linear.xml b/input/growth/source_sink_linear.xml similarity index 72% rename from input/growthtest/source_sink_linear.xml rename to input/growth/source_sink_linear.xml index 85f7b95ea2..f458129209 100644 --- a/input/growthtest/source_sink_linear.xml +++ b/input/growth/source_sink_linear.xml @@ -3,7 +3,7 @@ - 10 + 4 1 2000 @@ -13,7 +13,7 @@ - Source + Source1 cycamore Source @@ -22,7 +22,22 @@ commodity commod_recipe - 1 + 1.1 + + + + + + Source2 + + cycamore + Source + + + + commodity + commod_recipe + 2 @@ -41,13 +56,15 @@ + SingleRegion cycamore GrowthRegion - Source + Source1 + Source2 Sink @@ -69,20 +86,20 @@ cycamore ManagerInst - Source - Sink - - Source - 1 - Sink 1 - + + + Sink + Source1 + Source2 + + diff --git a/src/manager_inst.cc b/src/manager_inst.cc index 06210cd5b5..6fe63d1fd3 100644 --- a/src/manager_inst.cc +++ b/src/manager_inst.cc @@ -24,19 +24,31 @@ void ManagerInst::DecomNotify(Agent* a) { void ManagerInst::EnterNotify() { cyclus::Institution::EnterNotify(); - std::set::iterator it; - for (it = cyclus::Agent::children().begin(); - it != cyclus::Agent::children().end(); - ++it) { - Agent* a = *it; + std::set::iterator sit; + for (sit = cyclus::Agent::children().begin(); + sit != cyclus::Agent::children().end(); + ++sit) { + Agent* a = *sit; Register_(a); } + + using cyclus::toolkit::CommodityProducer; + std::vector::iterator vit; + for (vit = prototypes.begin(); vit != prototypes.end(); ++vit) { + Agent* a = context()->prototype(*vit); + CommodityProducer* cp_cast = dynamic_cast(a); + if (cp_cast != NULL) { + LOG(cyclus::LEV_INFO3, "mani") << "Registering prototype " + << a->prototype() << a->id() + << " with the Builder interface."; + Builder::Register(cp_cast); + } + } } void ManagerInst::Register_(Agent* a) { using cyclus::toolkit::CommodityProducer; using cyclus::toolkit::CommodityProducerManager; - using cyclus::toolkit::Builder; CommodityProducer* cp_cast = dynamic_cast(a); if (cp_cast != NULL) { @@ -44,7 +56,6 @@ void ManagerInst::Register_(Agent* a) { << a->prototype() << a->id() << " as a commodity producer."; CommodityProducerManager::Register(cp_cast); - Builder::Register(cp_cast); } } diff --git a/src/manager_inst.h b/src/manager_inst.h index 21faeaf3b5..eeee439c1c 100644 --- a/src/manager_inst.h +++ b/src/manager_inst.h @@ -42,6 +42,9 @@ class ManagerInst : public cyclus::Institution, /// unregister a child void Unregister_(cyclus::Agent* agent); + + #pragma cyclus var {} + std::vector prototypes; }; } // namespace cycamore diff --git a/tests/Inputs/dynamic_capacitated.xml b/tests/input/dynamic_capacitated.xml similarity index 100% rename from tests/Inputs/dynamic_capacitated.xml rename to tests/input/dynamic_capacitated.xml diff --git a/tests/input/growth.xml b/tests/input/growth.xml new file mode 100644 index 0000000000..f458129209 --- /dev/null +++ b/tests/input/growth.xml @@ -0,0 +1,114 @@ + + + + + + 4 + 1 + 2000 + + + + commodity + + + + Source1 + + cycamore + Source + + + + commodity + commod_recipe + 1.1 + + + + + + Source2 + + cycamore + Source + + + + commodity + commod_recipe + 2 + + + + + + Sink + + cycamore + Sink + + + + + commodity + + + + + + + SingleRegion + + cycamore + GrowthRegion + + Source1 + Source2 + Sink + + + commodity + + linear + + + 1 2 + + + 0 + + + + + SingleInstitution + + cycamore + ManagerInst + + + + Sink + 1 + + + + + + Sink + Source1 + Source2 + + + + + + + + commod_recipe + mass + 922350000 0.711 + 922380000 99.289 + + + \ No newline at end of file diff --git a/tests/test_dynamic_capacitated.py b/tests/test_dynamic_capacitated.py index ede28369f8..1a5c8873a5 100644 --- a/tests/test_dynamic_capacitated.py +++ b/tests/test_dynamic_capacitated.py @@ -28,7 +28,7 @@ def test_dynamic_capacitated(): the constraint, resulting in the same transaction amount as in time step 1. """ # Cyclus simulation input for dynamic capacitated - sim_inputs = ["./Inputs/dynamic_capacitated.xml"] + sim_inputs = ["./input/dynamic_capacitated.xml"] for sim_input in sim_inputs: holdsrtn = [1] # needed because nose does not send() to test generator diff --git a/tests/test_growth.py b/tests/test_growth.py new file mode 100644 index 0000000000..8b6c4e57b7 --- /dev/null +++ b/tests/test_growth.py @@ -0,0 +1,62 @@ +#! /usr/bin/env python + +from nose.tools import assert_equal, assert_true +import os +import tables +import numpy as np +import uuid +from tools import check_cmd +from helper import table_exist, find_ids + +def test_growth(): + """Tests GrowthRegion, ManagerInst, and Source over a 4-time step + simulation. + + A linear growth demand (y = x + 2) is provided to the growth region. Two + Sources are allowed in the ManagerInst, with capacities of 2 and 1.1, + respectively. At t=1, a 2-capacity Source is expected to be built, and at + t=2 and t=3, 1-capacity Sources are expected to be built. + """ + holdsrtn = [1] # needed because nose does not send() to test generator + sim_input = "./input/growth.xml" + tmp_file = str(uuid.uuid4()) + ".h5" + cmd = ["cyclus", "-o", tmp_file, "--input-file", sim_input] + yield check_cmd, cmd, '.', holdsrtn + rtn = holdsrtn[0] + if rtn != 0: + return # don't execute further commands + + output = tables.open_file(tmp_file, mode = "r") + # Tables of interest + paths = ["/AgentEntry",] + # Check if these tables exist + yield assert_true, table_exist(output, paths) + if not table_exist(output, paths): + output.close() + if os.path.isfile(tmp_file): + print("removing {0}".format(tmp_file)) + os.remove(tmp_file) + return # don't execute further commands + + # Get specific tables and columns + agent_entry = output.get_node("/AgentEntry")[:] + + # Find agent ids of source and sink facilities + agent_ids = agent_entry["AgentId"] + proto = agent_entry["Prototype"] + depl_time = agent_entry["EnterTime"] + + source1_id = find_ids("Source1", proto, agent_ids) + source2_id = find_ids("Source2", proto, agent_ids) + + assert_equal(len(source2_id), 1) + assert_equal(len(source1_id), 2) + + assert_equal(depl_time[np.where(agent_ids == source2_id[0])], 1) + assert_equal(depl_time[np.where(agent_ids == source1_id[0])], 2) + assert_equal(depl_time[np.where(agent_ids == source1_id[1])], 3) + + output.close() + if os.path.isfile(tmp_file): + print("removing {0}".format(tmp_file)) + os.remove(tmp_file) From 401e73371f592f9e9dbe3b2ab1b0fad68236452b Mon Sep 17 00:00:00 2001 From: Matthew Gidden Date: Tue, 27 May 2014 09:08:43 -0500 Subject: [PATCH 2/2] updated with cyclus core --- src/manager_inst.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manager_inst.cc b/src/manager_inst.cc index 6fe63d1fd3..0ff54dfe59 100644 --- a/src/manager_inst.cc +++ b/src/manager_inst.cc @@ -35,7 +35,7 @@ void ManagerInst::EnterNotify() { using cyclus::toolkit::CommodityProducer; std::vector::iterator vit; for (vit = prototypes.begin(); vit != prototypes.end(); ++vit) { - Agent* a = context()->prototype(*vit); + Agent* a = context()->CreateAgent(*vit); CommodityProducer* cp_cast = dynamic_cast(a); if (cp_cast != NULL) { LOG(cyclus::LEV_INFO3, "mani") << "Registering prototype "