Skip to content

Commit

Permalink
Merge branch 'grtest' of https://github.com/gidden/cycamore into gidd…
Browse files Browse the repository at this point in the history
…en-grtest
  • Loading branch information
scopatz committed May 27, 2014
2 parents 0150b11 + 401e733 commit 22f8d77
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<simulation>
<control>
<duration>10</duration>
<duration>4</duration>
<startmonth>1</startmonth>
<startyear>2000</startyear>
</control>
Expand All @@ -13,7 +13,7 @@
</commodity>

<facility>
<name>Source</name>
<name>Source1</name>
<module>
<lib>cycamore</lib>
<agent>Source</agent>
Expand All @@ -22,7 +22,22 @@
<Source>
<out_commod>commodity</out_commod>
<recipe_name>commod_recipe</recipe_name>
<capacity>1</capacity>
<capacity>1.1</capacity>
</Source>
</agent>
</facility>

<facility>
<name>Source2</name>
<module>
<lib>cycamore</lib>
<agent>Source</agent>
</module>
<agent>
<Source>
<out_commod>commodity</out_commod>
<recipe_name>commod_recipe</recipe_name>
<capacity>2</capacity>
</Source>
</agent>
</facility>
Expand All @@ -41,13 +56,15 @@
</Sink>
</agent>
</facility>

<region>
<name>SingleRegion</name>
<module>
<lib>cycamore</lib>
<agent>GrowthRegion</agent>
</module>
<allowedfacility>Source</allowedfacility>
<allowedfacility>Source1</allowedfacility>
<allowedfacility>Source2</allowedfacility>
<allowedfacility>Sink</allowedfacility>
<agent>
<GrowthRegion>
Expand All @@ -69,20 +86,20 @@
<lib>cycamore</lib>
<agent>ManagerInst</agent>
</module>
<availableprototype>Source</availableprototype>
<availableprototype>Sink</availableprototype>
<initialfacilitylist>
<entry>
<prototype>Source</prototype>
<number>1</number>
</entry>
<entry>
<prototype>Sink</prototype>
<number>1</number>
</entry>
</initialfacilitylist>
<agent>
<ManagerInst/>
<ManagerInst>
<prototypes>
<val>Sink</val>
<val>Source1</val>
<val>Source2</val>
</prototypes>
</ManagerInst>
</agent>
</institution>
</region>
Expand Down
25 changes: 18 additions & 7 deletions src/manager_inst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,38 @@ void ManagerInst::DecomNotify(Agent* a) {

void ManagerInst::EnterNotify() {
cyclus::Institution::EnterNotify();
std::set<cyclus::Agent*>::iterator it;
for (it = cyclus::Agent::children().begin();
it != cyclus::Agent::children().end();
++it) {
Agent* a = *it;
std::set<cyclus::Agent*>::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<std::string>::iterator vit;
for (vit = prototypes.begin(); vit != prototypes.end(); ++vit) {
Agent* a = context()->CreateAgent<Agent>(*vit);
CommodityProducer* cp_cast = dynamic_cast<CommodityProducer*>(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<CommodityProducer*>(a);
if (cp_cast != NULL) {
LOG(cyclus::LEV_INFO3, "mani") << "Registering agent "
<< a->prototype() << a->id()
<< " as a commodity producer.";
CommodityProducerManager::Register(cp_cast);
Builder::Register(cp_cast);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/manager_inst.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class ManagerInst : public cyclus::Institution,

/// unregister a child
void Unregister_(cyclus::Agent* agent);

#pragma cyclus var {}
std::vector<std::string> prototypes;
};
} // namespace cycamore

Expand Down
File renamed without changes.
114 changes: 114 additions & 0 deletions tests/input/growth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?xml version="1.0"?>
<!-- 1 Source Source, 1 Sink Sink -->

<simulation>
<control>
<duration>4</duration>
<startmonth>1</startmonth>
<startyear>2000</startyear>
</control>

<commodity>
<name>commodity</name>
</commodity>

<facility>
<name>Source1</name>
<module>
<lib>cycamore</lib>
<agent>Source</agent>
</module>
<agent>
<Source>
<out_commod>commodity</out_commod>
<recipe_name>commod_recipe</recipe_name>
<capacity>1.1</capacity>
</Source>
</agent>
</facility>

<facility>
<name>Source2</name>
<module>
<lib>cycamore</lib>
<agent>Source</agent>
</module>
<agent>
<Source>
<out_commod>commodity</out_commod>
<recipe_name>commod_recipe</recipe_name>
<capacity>2</capacity>
</Source>
</agent>
</facility>

<facility>
<name>Sink</name>
<module>
<lib>cycamore</lib>
<agent>Sink</agent>
</module>
<agent>
<Sink>
<in_commods>
<val>commodity</val>
</in_commods>
</Sink>
</agent>
</facility>

<region>
<name>SingleRegion</name>
<module>
<lib>cycamore</lib>
<agent>GrowthRegion</agent>
</module>
<allowedfacility>Source1</allowedfacility>
<allowedfacility>Source2</allowedfacility>
<allowedfacility>Sink</allowedfacility>
<agent>
<GrowthRegion>
<commodity_name>commodity</commodity_name>
<demand_types>
<val>linear</val>
</demand_types>
<demand_params>
<val>1 2</val>
</demand_params>
<demand_times>
<val>0</val>
</demand_times>
</GrowthRegion>
</agent>
<institution>
<name>SingleInstitution</name>
<module>
<lib>cycamore</lib>
<agent>ManagerInst</agent>
</module>
<initialfacilitylist>
<entry>
<prototype>Sink</prototype>
<number>1</number>
</entry>
</initialfacilitylist>
<agent>
<ManagerInst>
<prototypes>
<val>Sink</val>
<val>Source1</val>
<val>Source2</val>
</prototypes>
</ManagerInst>
</agent>
</institution>
</region>

<recipe>
<name>commod_recipe</name>
<basis>mass</basis>
<nuclide> <id>922350000</id> <comp>0.711</comp> </nuclide>
<nuclide> <id>922380000</id> <comp>99.289</comp> </nuclide>
</recipe>

</simulation>
2 changes: 1 addition & 1 deletion tests/test_dynamic_capacitated.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions tests/test_growth.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 22f8d77

Please sign in to comment.