9 from numpy.testing
import assert_array_almost_equal
10 from numpy.testing
import assert_almost_equal
11 from cyclus.lib
import Env
13 from nose.plugins.skip
import SkipTest
14 from nose.tools
import assert_equal, assert_true
18 from helper
import check_cmd, run_cyclus, table_exist, cyclus_has_coin
21 ALLOW_MILPS = Env().allow_milps
25 """A don't run certain tests if MILPs are disabled.""" 27 raise SkipTest(
"Cyclus was compiled without MILPS support or the " 28 "ALLOW_MILPS env var was not set to true.")
32 """A base class for all regression tests. A derived class is required for 33 each new input file to be tested. Each derived class *must* declare an `inf` 34 member in their `__init__` function that points to the input file to be 35 tested, e.g., `self.inf_ = ./path/to/my/input_file.xml. See below for 45 raise TypeError((
"self.inf must be set in derived classes " 46 "to run regression tests."))
51 with tables.open_file(self.
outf, mode=
"r") as f: 55 if "/AgentExit" in f \
58 if "/Enrichments" in f \
63 self.
info = f.get_node(
"/Info")[:]
65 x[
"ResourceId"]: x[
"Quantity"]
for x
in self.
resources}
68 self.
conn.row_factory = sqlite3.Row
70 exc = self.
cur.execute
71 self.
agent_entry = exc(
'SELECT * FROM AgentEntry').fetchall()
72 self.
agent_exit = exc(
'SELECT * FROM AgentExit').fetchall() \
74 (
"SELECT * FROM sqlite_master WHERE " 75 "type='table' AND name='AgentExit'")).fetchall()) > 0 \
77 self.
enrichments = exc(
'SELECT * FROM Enrichments').fetchall() \
79 (
"SELECT * FROM sqlite_master WHERE " 80 "type='table' AND name='Enrichments'")).fetchall()) > 0 \
82 self.
resources = exc(
'SELECT * FROM Resources').fetchall()
83 self.
transactions = exc(
'SELECT * FROM Transactions').fetchall()
84 self.
compositions = exc(
'SELECT * FROM Compositions').fetchall()
85 self.
info = exc(
'SELECT * FROM Info').fetchall()
87 x[
"ResourceId"]: x[
"Quantity"]
for x
in self.
resources}
89 def find_ids(self, spec, a, spec_col="Spec", id_col="AgentId"):
93 return [x[id_col]
for x
in a
if x[spec_col] == spec]
96 if self.
ext ==
'.sqlite':
97 return np.array([x[k]
for x
in a])
102 if self.
ext ==
'.sqlite':
104 if os.path.isfile(self.
outf):
105 print(
"removing {0}".format(self.
outf))
109 """This class tests the 1_Enrichment_2_Reactor.xml file related to the 110 Cyclus Physor 2014 publication. The number of key facilities, the enrichment 111 values, and the transactions to each reactor are tested. 114 super(_PhysorEnrichment, self).
__init__(*args, **kwargs)
117 super(_PhysorEnrichment, self).
setUp()
123 assert_equal(len(self.
rx_id), 2)
124 assert_equal(len(self.
enr_id), 1)
131 exp = [6.85, 9.94, 4.11, 6.85]
132 obs = [np.sum(self.
to_ary(enr,
"SWU")[
133 self.
to_ary(enr,
"Time") == t])
for t
in range(4)]
134 assert_array_almost_equal(exp, obs, decimal=2)
142 exp = [13.14, 16.69, 7.88, 13.14]
143 obs = [np.sum(self.
to_ary(enr,
"Natural_Uranium")[
144 self.
to_ary(enr,
"Time") == t])
for t
in range(4)]
145 assert_array_almost_equal(exp, obs, decimal=2)
152 if tx[
'ReceiverId'] == self.
rx_id[0]:
153 txs[tx[
'Time']] += self.
rsrc_qtys[tx[
'ResourceId']]
155 msg =
"Testing that first reactor gets less than it wants." 156 assert_array_almost_equal(exp, txs, decimal=2, err_msg=msg)
160 exp = [1, 0.8, 0.2, 1]
163 if tx[
'ReceiverId'] == self.
rx_id[1]:
164 txs[tx[
'Time']] += self.
rsrc_qtys[tx[
'ResourceId']]
166 msg =
"Testing that second reactor gets what it wants." 167 assert_array_almost_equal(exp, txs, decimal=2, err_msg=msg)
171 super(TestCBCPhysorEnrichment, self).
__init__(*args, **kwargs)
172 self.
inf =
"../input/physor/1_Enrichment_2_Reactor.xml" 177 super(TestGreedyPhysorEnrichment, self).
__init__(*args, **kwargs)
178 self.
inf =
"../input/physor/greedy_1_Enrichment_2_Reactor.xml" 181 """This class tests the 2_Sources_3_Reactor.xml file related to the Cyclus 182 Physor 2014 publication. Reactor deployment and transactions between 183 suppliers and reactors are tested. 186 super(_PhysorSources, self).
__init__(*args, **kwargs)
189 super(_PhysorSources, self).
setUp()
193 rx_id = self.
find_ids(
":cycamore:Reactor", tbl)
194 self.r1, self.r2, self.
r3 = tuple(rx_id)
195 s_id = self.
find_ids(
":cycamore:Source", tbl)
197 s_id.remove(self.
smox)
201 depl_time = {x[
"AgentId"]: x[
"EnterTime"]
for x
in self.
agent_entry}
203 assert_equal(depl_time[self.r1], 1)
204 assert_equal(depl_time[self.r2], 2)
205 assert_equal(depl_time[self.
r3], 3)
208 mox_exp = [0, 1, 1, 1, 0]
209 txs = [0, 0, 0, 0, 0]
211 if tx[
'ReceiverId'] == self.r1
and tx[
'SenderId'] == self.
smox:
212 txs[tx[
'Time']] += self.
rsrc_qtys[tx[
'ResourceId']]
213 assert_array_almost_equal(mox_exp, txs)
215 uox_exp = [0, 0, 0, 0, 1]
216 txs = [0, 0, 0, 0, 0]
218 if tx[
'ReceiverId'] == self.r1
and tx[
'SenderId'] == self.
suox:
219 txs[tx[
'Time']] += self.
rsrc_qtys[tx[
'ResourceId']]
220 assert_array_almost_equal(uox_exp, txs)
223 mox_exp = [0, 0, 1, 1, 1]
224 txs = [0, 0, 0, 0, 0]
226 if tx[
'ReceiverId'] == self.r2
and tx[
'SenderId'] == self.
smox:
227 txs[tx[
'Time']] += self.
rsrc_qtys[tx[
'ResourceId']]
228 assert_array_almost_equal(mox_exp, txs)
230 uox_exp = [0, 0, 0, 0, 0]
231 txs = [0, 0, 0, 0, 0]
233 if tx[
'ReceiverId'] == self.r2
and tx[
'SenderId'] == self.
suox:
234 txs[tx[
'Time']] += self.
rsrc_qtys[tx[
'ResourceId']]
235 assert_array_almost_equal(uox_exp, txs)
238 mox_exp = [0, 0, 0, 0.5, 1]
239 txs = [0, 0, 0, 0, 0]
241 if tx[
'ReceiverId'] == self.
r3 and tx[
'SenderId'] == self.
smox:
242 txs[tx[
'Time']] += self.
rsrc_qtys[tx[
'ResourceId']]
243 assert_array_almost_equal(mox_exp, txs)
245 uox_exp = [0, 0, 0, 0.5, 0]
246 txs = [0, 0, 0, 0, 0]
248 if tx[
'ReceiverId'] == self.
r3 and tx[
'SenderId'] == self.
suox:
249 txs[tx[
'Time']] += self.
rsrc_qtys[tx[
'ResourceId']]
250 assert_array_almost_equal(uox_exp, txs)
254 super(TestCBCPhysorSources, self).
__init__(*args, **kwargs)
255 self.
inf =
"../input/physor/2_Sources_3_Reactors.xml" 260 super(TestGreedyPhysorSources, self).
__init__(*args, **kwargs)
261 self.
inf =
"../input/physor/greedy_2_Sources_3_Reactors.xml" 264 """Tests dynamic capacity restraints involving changes in the number of 265 source and sink facilities. 267 A source facility is expected to offer a commodity of amount 1, 268 and a sink facility is expected to request for a commodity of amount 1. 269 Therefore, number of facilities correspond to the amounts of offers 272 At time step 1, 3 source facilities and 2 sink facilities are deployed, and 273 at time step 2, additional 2 sink facilities are deployed. After time 274 step 2, the older 2 sink facilities are decommissioned. 275 According to this deployment schedule, at time step 1, only 2 transactions 276 are expected, the number of sink facilities being the constraint; whereas, 277 at time step 2, only 3 transactions are expected, the number of source 278 facilities being the constraint. At time step 3, after decommissioning 2 279 older sink facilities, the remaining number of sink facilities becomes 280 the constraint, resulting in the same transaction amount as in time step 1. 283 super(TestDynamicCapacitated, self).
__init__(*args, **kwargs)
284 self.
inf =
"./input/dynamic_capacitated.xml" 286 raise SkipTest(
'Cyclus not compiled with COIN')
289 super(TestDynamicCapacitated, self).
setUp()
311 super(TestDynamicCapacitated, self).
tearDown()
322 assert_equal(len(self.
sink_id), 4)
345 assert_equal(len(np.where(self.
source_id == s)[0]), 1)
347 assert_equal(len(np.where(self.
sink_id == r)[0]), 1)
351 assert_equal(len(np.where(self.
trans_time == 1)[0]), 2)
353 assert_equal(len(np.where(self.
trans_time == 2)[0]), 3)
355 assert_equal(len(np.where(self.
trans_time == 3)[0]), 2)
364 assert_equal(quantity, 2)
372 assert_equal(quantity, 3)
380 assert_equal(quantity, 2)
383 """Tests GrowthRegion, ManagerInst, and Source over a 4-time step 386 A linear growth demand (y = x + 2) is provided to the growth region. Two 387 Sources are allowed in the ManagerInst, with capacities of 2 and 1.1, 388 respectively. At t=1, a 2-capacity Source is expected to be built, and at 389 t=2 and t=3, 1-capacity Sources are expected to be built. 391 A linear growth demand (y = 0x + 3) for a second commodity is provided at t=2 392 to test the demand for multiple commodities. 395 super(TestGrowth, self).
__init__(*args, **kwargs)
396 self.
inf =
"./input/growth.xml" 398 raise SkipTest(
'Cyclus not compiled with COIN')
401 super(TestGrowth, self).
setUp()
413 spec_col=
"Prototype")
415 spec_col=
"Prototype")
417 spec_col=
"Prototype")
419 assert_equal(len(source2_id), 1)
420 assert_equal(len(source1_id), 2)
421 assert_equal(len(source3_id), 3)
423 assert_equal(enter_time[np.where(agent_ids == source2_id[0])], 1)
424 assert_equal(enter_time[np.where(agent_ids == source1_id[0])], 2)
425 assert_equal(enter_time[np.where(agent_ids == source1_id[1])], 3)
427 assert_equal(enter_time[np.where(agent_ids == x)], 2)
430 """This class tests the input/recycle.xml file. 433 super(_Recycle, self).
__init__(*args, **kwargs)
437 base, _ = os.path.splitext(self.
outf)
441 SELECT t.time as time,SUM(c.massfrac*r.quantity) as qty FROM transactions as t 442 JOIN resources as r ON t.resourceid=r.resourceid AND r.simid=t.simid 443 JOIN agententry as send ON t.senderid=send.agentid AND send.simid=t.simid 444 JOIN agententry as recv ON t.receiverid=recv.agentid AND recv.simid=t.simid 445 JOIN compositions as c ON c.qualid=r.qualid AND c.simid=r.simid 446 WHERE send.prototype=? AND recv.prototype=? AND c.nucid=? 450 conn = sqlite3.connect(self.
outf)
453 simdur = len(exp_invs)
455 invs = [0.0] * simdur
456 for i, row
in enumerate(c.execute(self.
sql, (fromfac, tofac, nuclide))):
460 expfname =
'exp_recycle_{0}-{1}-{2}.dat'.format(fromfac, tofac, nuclide)
461 with open(expfname,
'w')
as f:
462 for t, val
in enumerate(exp_invs):
463 f.write(
'{0} {1}\n'.format(t, val))
464 obsfname =
'obs_recycle_{0}-{1}-{2}.dat'.format(fromfac, tofac, nuclide)
465 with open(obsfname,
'w')
as f:
466 for t, val
in enumerate(invs):
467 f.write(
'{0} {1}\n'.format(t, val))
470 for exp, obs
in zip(invs, exp_invs):
472 exp, obs, err_msg=
'mismatch at t={}, {} != {}'.format(i, exp, obs))
486 exp[113] = 1.70022267
487 exp[132] = 1.70022267
488 exp[151] = 1.70022267
489 exp[170] = 1.70022267
490 exp[189] = 1.70022267
491 exp[208] = 1.70022267
492 exp[246] = 1.70022267
493 exp[265] = 1.70022267
494 exp[284] = 1.70022267
495 exp[303] = 1.70022267
496 exp[322] = 1.70022267
497 exp[341] = 1.70022267
498 exp[360] = 1.70022267
499 exp[379] = 1.70022267
500 exp[417] = 1.70022267
501 exp[436] = 1.70022267
502 exp[455] = 1.70022267
503 exp[474] = 1.70022267
504 exp[493] = 1.70022267
505 exp[512] = 1.70022267
506 exp[531] = 1.70022267
507 exp[569] = 1.70022267
508 exp[588] = 1.70022267
510 self.
do_compare(
'separations',
'repo', 942390000, exp)
515 exp[226] = 420.42772559790944
516 exp[397] = 420.42772559790944
517 exp[549] = 420.42772559790944
518 self.
do_compare(
'reactor',
'repo', 942390000, exp)
521 """This class tests the input/recycle.xml file. 524 super(TestGreedyRecycle, self).
__init__(*args, **kwargs)
525 self.
inf =
"../input/greedy_recycle.xml" 528 """This class tests the input/recycle.xml file. 531 super(TestCbcRecycle, self).
__init__(*args, **kwargs)
532 self.
inf =
"../input/recycle.xml"
def __init__(self, args, kwargs)
def run_cyclus(cyclus, cwd, in_path, out_path)
def __init__(self, args, kwargs)
def test_pu239_reactor_repo(self)
def __init__(self, args, kwargs)
def test_xaction_general(self)
def test_rxtr1_xactions(self)
def do_compare(self, fromfac, tofac, nuclide, exp_invs)
def test_sink_deployment(self)
def __init__(self, args, kwargs)
def __init__(self, args, kwargs)
def __init__(self, args, kwargs)
def __init__(self, args, kwargs)
def __init__(self, args, kwargs)
def test_rxtr_deployment(self)
def test_rxtr2_xactions(self)
def __init__(self, args, kwargs)
def test_rxtr3_xactions(self)
def test_source_deployment(self)
def __init__(self, args, kwargs)
def test_xaction_specific(self)
def test_deployment(self)
def skip_if_dont_allow_milps()
def __init__(self, args, kwargs)
def find_ids(data, data_table, id_table)
def find_ids(self, spec, a, spec_col="Spec", id_col="AgentId")
def test_pu239_sep_repo(self)
def __init__(self, args, kwargs)