Skip to content

Commit

Permalink
Merge pull request #477 from scopatz/coinop
Browse files Browse the repository at this point in the history
Works for me!
  • Loading branch information
FlanFlanagan authored Nov 14, 2017
2 parents 0e21439 + bef6a32 commit 730c41c
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 19 deletions.
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ IF(NOT CYCLUS_DOC_ONLY)
MESSAGE("-- HDF5 High Level Libraries: ${HDF5_C_HL_LIBRARIES}")

# find coin and link to it
FIND_PACKAGE(COIN REQUIRED)
SET(CYCAMORE_INCLUDE_DIRS ${CYCAMORE_INCLUDE_DIRS} ${COIN_INCLUDE_DIRS})
set(LIBS ${LIBS} ${COIN_LIBRARIES})
FIND_PACKAGE(COIN)
if(COIN_FOUND)
SET(CYCAMORE_INCLUDE_DIRS ${CYCAMORE_INCLUDE_DIRS} ${COIN_INCLUDE_DIRS})
SET(LIBS ${LIBS} ${COIN_LIBRARIES})
endif()
MESSAGE("-- COIN Root: ${COIN_ROOT}")
MESSAGE("-- COIN Include directories: ${COIN_INCLUDE_DIRS}")
MESSAGE("-- COIN Libraries: ${COIN_LIBRARIES}")
Expand Down
4 changes: 4 additions & 0 deletions src/cycamore.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#ifndef CYCAMORE_SRC_CYCAMORE_H_
#define CYCAMORE_SRC_CYCAMORE_H_

// These includes must come before others.
#include "cyclus.h"
#include "cycamore_version.h"

#include "batch_reactor.h"
#include "batch_reactor_tests.h"
#include "deploy_inst.h"
#include "enrichment.h"
#include "enrichment_tests.h"
#if CYCLUS_HAS_COIN
#include "growth_region.h"
#include "growth_region_tests.h"
#endif
#include "inpro_reactor.h"
#include "inpro_reactor_tests.h"
#include "manager_inst.h"
Expand Down
30 changes: 24 additions & 6 deletions src/growth_region.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@

namespace cycamore {

GrowthRegion::GrowthRegion(cyclus::Context* ctx) : cyclus::Region(ctx) { }
GrowthRegion::GrowthRegion(cyclus::Context* ctx) : cyclus::Region(ctx) {
#if !CYCLUS_HAS_COIN
throw cyclus::Error("Growth Region requires that Cyclus & Cycamore be compiled "
"with COIN support.");
#endif
}

GrowthRegion::~GrowthRegion() {}

void GrowthRegion::AddCommodityDemand_(std::string commod,
Demand& demand) {


cyclus::toolkit::PiecewiseFunctionFactory pff;
cyclus::toolkit::BasicFunctionFactory bff;
bool continuous = false;
Expand Down Expand Up @@ -55,7 +60,7 @@ void GrowthRegion::DecomNotify(Agent* a) {
void GrowthRegion::Register_(cyclus::Agent* agent) {
using cyclus::toolkit::CommodityProducerManager;
using cyclus::toolkit::Builder;

#if CYCLUS_HAS_COIN
CommodityProducerManager* cpm_cast =
dynamic_cast<CommodityProducerManager*>(agent);
if (cpm_cast != NULL) {
Expand All @@ -72,12 +77,16 @@ void GrowthRegion::Register_(cyclus::Agent* agent) {
<< " as a builder.";
buildmanager_.Register(b_cast);
}
#else
throw cyclus::Error("Growth Region requires that Cyclus & Cycamore be compiled "
"with COIN support.");
#endif
}

void GrowthRegion::Unregister_(cyclus::Agent* agent) {
using cyclus::toolkit::CommodityProducerManager;
using cyclus::toolkit::Builder;

#if CYCLUS_HAS_COIN
CommodityProducerManager* cpm_cast =
dynamic_cast<CommodityProducerManager*>(agent);
if (cpm_cast != NULL)
Expand All @@ -86,6 +95,10 @@ void GrowthRegion::Unregister_(cyclus::Agent* agent) {
Builder* b_cast = dynamic_cast<Builder*>(agent);
if (b_cast != NULL)
buildmanager_.Unregister(b_cast);
#else
throw cyclus::Error("Growth Region requires that Cyclus & Cycamore be compiled "
"with COIN support.");
#endif
}

void GrowthRegion::Tick() {
Expand All @@ -106,7 +119,7 @@ void GrowthRegion::Tick() {
LOG(cyclus::LEV_INFO3, "greg") << " * demand = " << demand;
LOG(cyclus::LEV_INFO3, "greg") << " * supply = " << supply;
LOG(cyclus::LEV_INFO3, "greg") << " * unmet demand = " << unmetdemand;

if (unmetdemand > 0) {
OrderBuilds(commod, unmetdemand);
}
Expand All @@ -116,6 +129,7 @@ void GrowthRegion::Tick() {

void GrowthRegion::OrderBuilds(cyclus::toolkit::Commodity& commodity,
double unmetdemand) {
#if CYCLUS_HAS_COIN
using std::vector;
vector<cyclus::toolkit::BuildOrder> orders =
buildmanager_.MakeBuildDecision(commodity, unmetdemand);
Expand Down Expand Up @@ -149,6 +163,10 @@ void GrowthRegion::OrderBuilds(cyclus::toolkit::Commodity& commodity,
context()->SchedBuild(instcast, agentcast->prototype());
}
}
#else
throw cyclus::Error("Growth Region requires that Cyclus & Cycamore be compiled "
"with COIN support.");
#endif
}

extern "C" cyclus::Agent* ConstructGrowthRegion(cyclus::Context* ctx) {
Expand Down
11 changes: 6 additions & 5 deletions src/growth_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class GrowthRegion : public cyclus::Region {
inline cyclus::toolkit::SupplyDemandManager* sdmanager() {
return &sdmanager_;
}
protected:

protected:
#pragma cyclus var { \
"alias": ["growth", "commod", \
["piecewise_function", \
Expand Down Expand Up @@ -102,13 +102,15 @@ class GrowthRegion : public cyclus::Region {
"respective documentation pages.", \
}
std::map<std::string, std::vector<std::pair<int, std::pair<std::string, std::string> > > > commodity_demand; // must match Demand typedef


#if CYCLUS_HAS_COIN
/// manager for building things
cyclus::toolkit::BuildingManager buildmanager_;
#endif

/// manager for Supply and demand
cyclus::toolkit::SupplyDemandManager sdmanager_;

/// register a child
void Register_(cyclus::Agent* agent);

Expand All @@ -125,7 +127,6 @@ class GrowthRegion : public cyclus::Region {
/// @param unmetdemand the unmet demand
void OrderBuilds(cyclus::toolkit::Commodity& commodity, double unmetdemand);
};

} // namespace cycamore

#endif // CYCAMORE_SRC_GROWTH_REGION_H_
2 changes: 2 additions & 0 deletions src/growth_region_tests.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sstream>

#include "growth_region_tests.h"
#if CYCLUS_HAS_COIN

namespace cycamore {

Expand Down Expand Up @@ -52,3 +53,4 @@ INSTANTIATE_TEST_CASE_P(GrowthRegion, RegionTests,
Values(&GrowthRegionConstructor));
INSTANTIATE_TEST_CASE_P(GrowthRegion, AgentTests,
Values(&GrowthRegionConstructor));
#endif // CYCLUS_HAS_COIN
4 changes: 3 additions & 1 deletion src/growth_region_tests.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#ifndef CYCAMORE_SRC_GROWTH_REGION_TESTS_H_
#define CYCAMORE_SRC_GROWTH_REGION_TESTS_H_
#include "platform.h"
#if CYCLUS_HAS_COIN

#include <gtest/gtest.h>

Expand Down Expand Up @@ -30,5 +32,5 @@ class GrowthRegionTests : public ::testing::Test {
};

} // namespace cycamore

#endif // CYCLUS_HAS_COIN
#endif // CYCAMORE_SRC_GROWTH_REGION_TESTS_H_
19 changes: 19 additions & 0 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import tables
from nose.tools import assert_equal


CYCLUS_HAS_COIN = None


if sys.version_info[0] >= 3:
str_types = (bytes, str)
else:
Expand Down Expand Up @@ -57,6 +61,7 @@ def exit_times(agent_id, exit_table):

return exit_times


def run_cyclus(cyclus, cwd, in_path, out_path):
"""Runs cyclus with various inputs and creates output databases
"""
Expand All @@ -65,6 +70,7 @@ def run_cyclus(cyclus, cwd, in_path, out_path):
cmd = [cyclus, "-o", out_path, "--input-file", in_path]
check_cmd(cmd, cwd, holdsrtn)


def check_cmd(args, cwd, holdsrtn):
"""Runs a command in a subprocess and verifies that it executed properly.
"""
Expand All @@ -80,3 +86,16 @@ def check_cmd(args, cwd, holdsrtn):
print("STDOUT + STDERR:\n\n" + f.read().decode())
holdsrtn[0] = rtn
assert_equal(rtn, 0)


def cyclus_has_coin():
global CYCLUS_HAS_COIN
if CYCLUS_HAS_COIN is not None:
return CYCLUS_HAS_COIN
s = subprocess.check_output(['cyclus', '--version'], universal_newlines=True)
s = s.strip().replace('Dependencies:', '')
m = {k.strip(): v.strip() for k,v in [line.split()[:2] for line in s.splitlines()
if line != '']}
CYCLUS_HAS_COIN = m['Coin-Cbc'] != '-1'
return CYCLUS_HAS_COIN

6 changes: 5 additions & 1 deletion tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


import helper
from helper import check_cmd, run_cyclus, table_exist
from helper import check_cmd, run_cyclus, table_exist, cyclus_has_coin


ALLOW_MILPS = Env().allow_milps
Expand Down Expand Up @@ -282,6 +282,8 @@ class TestDynamicCapacitated(TestRegression):
def __init__(self, *args, **kwargs):
super(TestDynamicCapacitated, self).__init__(*args, **kwargs)
self.inf = "./input/dynamic_capacitated.xml"
if not cyclus_has_coin():
raise SkipTest('Cyclus not compiled with COIN')

def setUp(self):
super(TestDynamicCapacitated, self).setUp()
Expand Down Expand Up @@ -392,6 +394,8 @@ class TestGrowth(TestRegression):
def __init__(self, *args, **kwargs):
super(TestGrowth, self).__init__(*args, **kwargs)
self.inf = "./input/growth.xml"
if not cyclus_has_coin():
raise SkipTest('Cyclus not compiled with COIN')

def setUp(self):
super(TestGrowth, self).setUp()
Expand Down
20 changes: 17 additions & 3 deletions tests/test_run_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@
import subprocess

from nose.tools import assert_true
from nose.plugins.skip import SkipTest

import run_inputs as ri
from helper import cyclus_has_coin


def coin_skipper(filename):
raise SkipTest(filename + " cannot be executed since Cyclus was not installed "
"with COIN support")


def test_inputs():
files, _, _ = ri.get_files(ri.input_path)
for f in files:
testf = ri.TestFile(ri.cyclus_path, f, "-v0")
testf.run()
yield assert_true, testf.passed, "Failed running {}".format(f)
absfile = os.path.join(ri.input_path, f)
with open(absfile) as fh:
src = fh.read()
if cyclus_has_coin() or "GrowthRegion" not in src:
testf = ri.TestFile(ri.cyclus_path, f, "-v0")
testf.run()
yield assert_true, testf.passed, "Failed running {}".format(f)
else:
yield coin_skipper, absfile

0 comments on commit 730c41c

Please sign in to comment.