Skip to content

Commit

Permalink
Merge pull request #15 from abachma2/resources
Browse files Browse the repository at this point in the history
Fix recording resources issue.
  • Loading branch information
ZoeRichter authored Jun 27, 2023
2 parents 730eea8 + bacfb79 commit 4efd173
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 27 deletions.
119 changes: 95 additions & 24 deletions openmcyclus/DepleteReactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,31 +161,50 @@ def tick(self):
fuel is loaded
'''
if self.retired():
# self.record("RETIRED", "")
print("time:", self.context.time, "retired")
#self.record("RETIRED", "")
if self.context.time == self.exit_time + 1:
print("transmuting fuel for retirement")
if self.decom_transmute_all == 1:
self.transmute(math.ceil(self.n_assem_core))
else:
self.transmute(math.ceil(self.n_assem_core / 2))

while self.core.count > 0:
print("discharging remaining fuel")
if self.discharge() == False:
break
while (
self.fresh_fuel.count > 0) and (
self.spent_fuel.space >= self.assem_size):
print("pushing to spent fuel")
self.spent_fuel.push(self.fresh_fuel.pop())

if self.check_decommission_condition():
print("decommissioning facility")
self.decommission()

if self.cycle_step == self.cycle_time:
print("core:", self.core.count, "spent:", self.spent_fuel.count)
print(
"time:",
self.context.time,
"transmute",
math.ceil(
self.n_assem_batch))
self.transmute(math.ceil(self.n_assem_batch))
#self.record("CYCLE_END", "")
print("core:", self.core.count, "spent:", self.spent_fuel.count)

if (self.cycle_step >= self.cycle_time) and (self.discharged == False):
print("core:", self.core.count, "spent:", self.spent_fuel.count)
self.discharged = self.discharge()
print("core:", self.core.count, "spent:", self.spent_fuel.count)

if self.cycle_step >= self.cycle_time:
#print("time:", self.context.time, "load")
#print("time:", self.context.time, self.core.count)
#print("time:", self.context.time, self.fresh_fuel.count)
self.load()

# lib.Logger('5', str("DepleteReactor" + str(self.power_cap) + "is ticking"))
Expand Down Expand Up @@ -214,18 +233,20 @@ def tock(self):
the cycle duration counter increases by one.
'''
if self.retired():
print("tock retired")
return

if (
self.cycle_step >= self.cycle_time +
self.refuel_time) and (
self.core.count == self.n_assem_core) and (
self.discharged):
print("restarting cycle counter")
self.discharged = False
self.cycle_step = 0

if (self.cycle_step == 0) and (self.core.count == self.n_assem_core):
# self.record("CYCLE_START", "")
#self.record("CYCLE_START", "")
print("Cycle start")

if (self.cycle_step >= 0) and (self.cycle_step < self.cycle_time) and (
Expand Down Expand Up @@ -261,8 +282,10 @@ def check_decommission_condition(self):
Bool: True if conditions are met, otherwise False
'''
if (self.core.count == 0) and (self.spent_fuel.count == 0):
print("decommissioned")
return True
else:
print("not decommissioned")
return False

def get_material_requests(self): # phase 1
Expand Down Expand Up @@ -331,6 +354,8 @@ def get_material_requests(self): # phase 1
lib.record_time_series(
"demand" + commod, self, self.assem_size)
ports.append({"commodities": port, "constraints": self.assem_size})
print("time:", self.context.time, "finish get_material_requests")
#print("request portfolio:", ports, len(ports))
return ports

def get_material_bids(self, requests): # phase 2
Expand Down Expand Up @@ -372,6 +397,7 @@ def get_material_bids(self, requests): # phase 2
got_mats = False
bids = []
port = []
all_mats = {}
for commod_index, commod in enumerate(self.fuel_outcommods):
reqs = requests[commod]
if len(reqs) == 0:
Expand All @@ -386,12 +412,18 @@ def get_material_bids(self, requests): # phase 2

else:
mats = []
print(
"time:",
self.context.time,
"mats to trade matching request commod:",
mats)
print(len(mats))
if len(mats) == 0:
continue

recipe_comp = self.context.get_recipe(
self.fuel_outrecipes[commod_index])

print(recipe_comp)
for req in reqs:
tot_bid = 0
for jj in range(len(mats)):
Expand All @@ -409,6 +441,8 @@ def get_material_bids(self, requests): # phase 2
return

port = {'bids': bids}
#print("time:", self.context.time, "portfolio:", port)
print("time:", self.context.time, "respond", len(bids), "assemblies")
return port

def get_material_trades(self, trades): # phase 5.1
Expand Down Expand Up @@ -436,7 +470,16 @@ def get_material_trades(self, trades): # phase 5.1
'''
responses = {}
mats = self.pop_spent()
print(
"time:",
self.context.time,
"trade away",
len(trades),
"assemblies")
#print(trades)
for ii in range(len(trades)):
#print("time:", self.context.time, "number of trades:", len(trades))
#print(trades[ii])
commodity = trades[ii].request.commodity
mat = mats[commodity].pop(-1)
responses[trades[ii]] = mat
Expand Down Expand Up @@ -471,11 +514,14 @@ def accept_material_trades(self, responses): # phase 5.2
'''
#print("time:", self.context.time, "responses:", responses)
# min(len(responses), self.n_assem_core - self.core.count)
n_load = len(responses)
if n_load > 0:
ss = str(n_load) + " assemblies"
# self.record("LOAD", ss)
#self.record("LOAD", ss)
for trade in responses:
#print(trade.request.commodity, trade.request.preference, trade.amt)
commodity = trade.request.commodity
material = trade.request.target
self.index_res(material, commodity)
Expand All @@ -484,6 +530,7 @@ def accept_material_trades(self, responses): # phase 5.2
else:
self.fresh_fuel.push(material)

print("core:", self.core.count, "fresh:", self.fresh_fuel.count, "spent:", self.spent_fuel.count)
return

def retired(self):
Expand All @@ -495,8 +542,10 @@ def retired(self):
Bool: true if the conditions are met, False if they aren't met
'''
if (self.exit_time != -1) and (self.context.time > self.exit_time):
print("retired, yes")
return 1
else:
print("retired, no")
return 0

def discharge(self):
Expand All @@ -518,21 +567,38 @@ def discharge(self):
'''
npop = min(self.n_assem_batch, self.core.count)
if (self.n_assem_spent - self.spent_fuel.count) < npop:
# self.record("DISCHARGE", "failed")
#self.record("DISCHARGE", "failed")
return False

ss = str(npop) + " assemblies"
# self.record("DISCHARGE", ss)

core_pop = self.core.pop_n(npop)
for ii in range(len(core_pop)):
self.spent_fuel.push(core_pop[ii])

#self.record("DISCHARGE", ss)
print("time:", self.context.time, "discharge", ss)
#print(self.spent_fuel.count, self.core.count)
discharge_assemblies = self.core.pop_n(npop)
for ii in range(len(discharge_assemblies)):
parent_1 = discharge_assemblies[ii].state_id
discharge_assemblies[ii].bump_state_id()
resources_table = self.context.new_datum("Resources")
resources_table.add_val("ResourceId", discharge_assemblies[ii].state_id, None, 'int')
resources_table.add_val("ObjId", discharge_assemblies[ii].obj_id, None, 'int')
resources_table.add_val("Type", discharge_assemblies[ii].type, None, "std::string")
resources_table.add_val("TimeCreated", self.context.time, None, 'int')
resources_table.add_val("Quantity", discharge_assemblies[ii].quantity, None, 'double')
resources_table.add_val("Units", discharge_assemblies[ii].units, None, 'std::string')
resources_table.add_val("QualId", discharge_assemblies[ii].qual_id, None, 'int')
resources_table.add_val("Parent1", parent_1, None, 'int')
resources_table.add_val("Parent2", 0, None, 'int')
resources_table.record()
self.spent_fuel.push_many(discharge_assemblies)
#print(self.spent_fuel.count, self.core.count)
#print(self.spent_fuel.peek().comp())

for ii in range(len(self.fuel_outcommods)):
spent_mats = self.peek_spent()
tot_spent = 0
if self.fuel_outcommods[ii] in spent_mats:
mats = spent_mats[self.fuel_outcommods[ii]]
#for mat in mats:
tot_spent += mats.quantity
lib.record_time_series(
"supply" + self.fuel_outcommods[ii], self, tot_spent)
Expand All @@ -554,10 +620,9 @@ def load(self):
if n == 0:
return
ss = str(n) + " assemblies"
# self.record("LOAD", ss)
assemblies = self.fresh_fuel.pop_n(n)
for ii in range(len(assemblies)):
self.core.push(assemblies[ii])
#self.record("LOAD", ss)

self.core.push_many(self.fresh_fuel.pop_n(n))
return

def transmute(self, n_assem):
Expand All @@ -580,8 +645,9 @@ def transmute(self, n_assem):
Number of assemblies to be transmuted
'''
old = self.core.pop_n(min(n_assem, self.core.count))
for ii in range(len(old)):
self.core.push(old[ii])
self.core.push_many(old)
if self.core.count > len(old):
self.core.push_many(self.core.pop_n(self.core.count - len(old)))
ss = str(len(old)) + " assemblies"
# self.record("TRANSMUTE", ss)
for ii in range(len(old)):
Expand Down Expand Up @@ -612,12 +678,14 @@ def record(self, event, val):
val: str
value of event to be recorded
'''
events = self.context.new_datum(
"ReactorEvents") # creates tables with name ReactorEvents
datum = lib.Datum("Event")
lib.Datum.add_val(datum, "Event", event)
events.add_val("Value", val)
events.record()
print("Recording to ReactorEvents")
datum = self.context.new_datum("ReactorEvents")
datum.add_val("AgentId", self.id, None, 'int')
datum.add_val("Time", self.context.time, None, 'int')
#datum.add_val("Event", event, None, 'std::string')
#datum.add_val("Value", val, None, 'std::string')
#datum.record()
print("done recording")
return

def index_res(self, material, incommod):
Expand Down Expand Up @@ -700,11 +768,12 @@ def peek_spent(self):
Values are the Materials with the given commodity names.
'''
#print("peek_spent")
mapped = {}
if self.spent_fuel.count > 0:
mats = self.spent_fuel.pop_n(self.spent_fuel.count)
self.spent_fuel.push_many(mats)
for ii in range(len(mats)):
self.spent_fuel.push(mats[ii])
commod = self.get_commod(mats[ii], 'out')
mapped[commod] = mats[ii]
return mapped
Expand Down Expand Up @@ -752,9 +821,11 @@ def get_recipe(self, material, flow):
name of recipe for the queried material
'''
ii = self.resource_indexes[material.obj_id]
#print("resource index:", ii)
if flow == 'in':
return self.fuel_inrecipes[ii]
elif flow == 'out':
#print(self.fuel_outrecipes[ii])
return self.fuel_outrecipes[ii]

def get_pref(self, material):
Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests/test_depletereactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,6 @@ def test_resources(self):
tbl = self.resources
times = self.to_array(tbl, "TimeCreated")
quantities = self.to_array(tbl, "Quantity")
assert len(tbl) == 11
assert all(times == [3,3,3,5,5,8,8,11,11,13,13])
assert all(quantities == [10]*11)
assert len(tbl) == 12
assert all(times == [3,3,3,5,5,8,8,11,11,13,13,13])
assert all(quantities == [10]*12)

0 comments on commit 4efd173

Please sign in to comment.