Skip to content

Commit

Permalink
Merge pull request cyclus#1795 from nuclearkatie/transportation
Browse files Browse the repository at this point in the history
Recording for 1:1 extracted resources
  • Loading branch information
gonuke authored Sep 13, 2024
2 parents 956747c + ac72e6a commit 55b466b
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Since last release
* Rely on ``python3`` in environment instead of ``python`` (#1747)
* Remove ``pandas`` as build dependency (#1748)
* Consistently use hyphens in ``install.py`` flags (#1748)
* Material sell policy can package materials (#1749, #1774, #1775)
* Material sell policy can package materials (#1749, #1774, #1775, #1795)
* Use miniforge for conda CI builds instead of miniconda (#1763)
* Define constants ``CY_LARGE_DOUBLE``, ``CY_LARGE_INT``, and ``CY_NEAR_ZERO`` (#1757)
* Warning and limits on number of packages that can be created from a resource at once (#1771)
Expand Down
31 changes: 24 additions & 7 deletions src/material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ Material::Ptr Material::ExtractComp(double qty, Composition::Ptr c,
// this material regardless of composition.
other->prev_decay_time_ = prev_decay_time_;

if (qty_ > cyclus::eps()) {
tracker_.Extract(&other->tracker_);
} // else, this material is being fully extracted and nothing has effectively
// changed. Don't need to bump state, parent, etc.
tracker_.Extract(&other->tracker_);

return other;
}
Expand Down Expand Up @@ -143,15 +140,35 @@ void Material::Transmute(Composition::Ptr c) {
}
}

Resource::Ptr Material::PackageExtract(double qty, std::string new_package_name) {
if ((qty - qty_) > eps_rsrc()) {
throw ValueError("Attempted to extract more quantity than exists.");
}

qty_ -= qty;
Material::Ptr other(new Material(ctx_, qty, comp_, new_package_name));

// Decay called on the extracted material should have the same dt as for
// this material regardless of composition.
other->prev_decay_time_ = prev_decay_time_;

// this call to res_tracker must come first before the parent resource
// state id gets modified
other->tracker_.Package(&tracker_);
if (qty_ > eps_rsrc()) {
tracker_.Modify();
}
return boost::static_pointer_cast<Resource>(other);
}

void Material::ChangePackage(std::string new_package_name) {
if (new_package_name == package_name_ || ctx_ == NULL) {
if (ctx_ == NULL) {
// no change needed
return;
}
else if (new_package_name == Package::unpackaged_name()) {
// unpackaged has functionally no restrictions
package_name_ = new_package_name;
tracker_.Package();
return;
}

Expand All @@ -160,10 +177,10 @@ void Material::ChangePackage(std::string new_package_name) {
double max = p->fill_max();
if (qty_ >= min && qty_ <= max) {
package_name_ = new_package_name;
tracker_.Package();
} else {
throw ValueError("Material quantity is outside of package fill limits.");
}
tracker_.Package();
}

void Material::Decay(int curr_time) {
Expand Down
3 changes: 3 additions & 0 deletions src/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class Material: public Resource {

virtual std::string package_name();

virtual Resource::Ptr PackageExtract(double qty,
std::string new_package_name = Package::unpackaged_name());

/// Changes the package id. Checks that the resource fits the package
/// type minimum and maximum mass criteria.
virtual void ChangePackage(std::string new_package_name = Package::unpackaged_name());
Expand Down
3 changes: 2 additions & 1 deletion src/package.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "package.h"
#include "error.h"
#include "cyc_limits.h"

namespace cyclus {

Expand Down Expand Up @@ -30,7 +31,7 @@ Package::Ptr& Package::unpackaged() {
}

std::pair<double, int> Package::GetFillMass(double qty) {
if (qty < fill_min_) {
if ((qty - fill_min_) < -eps_rsrc()) {
// less than one pkg of material available
return std::pair<double, int> (0, 0);
}
Expand Down
18 changes: 18 additions & 0 deletions src/product.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "error.h"
#include "logger.h"
#include "cyc_limits.h"

namespace cyclus {

Expand Down Expand Up @@ -76,6 +77,23 @@ std::string Product::package_name() {
return package_name_;
}

Resource::Ptr Product::PackageExtract(double qty, std::string new_package_name) {
if (qty > quantity_) {
throw ValueError("Attempted to extract more quantity than exists.");
}

quantity_ -= qty;
Product::Ptr other(new Product(ctx_, qty, quality_, new_package_name));

// this call to res_tracker must come first before the parent resource
// state id gets modified
other->tracker_.Package(&tracker_);
if (quantity_ > cyclus::eps_rsrc()) {
tracker_.Modify();
}
return boost::static_pointer_cast<Resource>(other);
}

void Product::ChangePackage(std::string new_package_name) {
if (new_package_name == package_name_ || ctx_ == NULL) {
// no change needed
Expand Down
2 changes: 2 additions & 0 deletions src/product.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Product : public Resource {
/// Returns the package id.
virtual std::string package_name();

virtual Resource::Ptr PackageExtract(double qty, std::string new_package_name = Package::unpackaged_name());

/// Changes the product's package id
virtual void ChangePackage(std::string new_package_name = Package::unpackaged_name());

Expand Down
35 changes: 27 additions & 8 deletions src/res_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ void ResTracker::Create(Agent* creator) {

parent1_ = 0;
parent2_ = 0;
Record();
bool bumpId = false;
Record(bumpId);
ctx_->NewDatum("ResCreators")
->AddVal("ResourceId", res_->state_id())
->AddVal("AgentId", creator->id())
Expand Down Expand Up @@ -51,7 +52,7 @@ void ResTracker::Extract(ResTracker* removed) {

Record();
}

removed->parent1_ = res_->state_id();
removed->parent2_ = 0;
removed->tracked_ = tracked_;
Expand All @@ -69,17 +70,36 @@ void ResTracker::Absorb(ResTracker* absorbed) {
Record();
}

void ResTracker::Package() {
void ResTracker::Package(ResTracker* parent) {
if (!tracked_) {
return;
}

parent2_ = 0;
tracked_ = tracked_;
package_name_ = res_->package_name();
Record();

if (parent != NULL) {
parent1_ = parent->res_->state_id();

// Resource was just created, with packaging info, and assigned a state id.
// Do not need to bump again
bool bumpId = false;
Record(bumpId);
} else {
// Resource was not just created. It is being re-packaged. It needs to be
// bumped to get a new state id.
parent1_ = res_->state_id();
Record();
}



}

void ResTracker::Record() {
res_->BumpStateId();
void ResTracker::Record(bool bumpId) {
if (bumpId) {
res_->BumpStateId();
}
ctx_->NewDatum("Resources")
->AddVal("ResourceId", res_->state_id())
->AddVal("ObjId", res_->obj_id())
Expand All @@ -92,7 +112,6 @@ void ResTracker::Record() {
->AddVal("Parent1", parent1_)
->AddVal("Parent2", parent2_)
->Record();

res_->Record(ctx_);
}

Expand Down
9 changes: 6 additions & 3 deletions src/res_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ class ResTracker {
/// decay).
void Modify();

/// Should be called when a resource's package gets modified
void Package();
/// Should be called when a resource's package gets modified. If the resource
/// was just created from a parent resource, the parent should be passed in.
/// If the resource is just being repackaged (e.g. to unpackaged), the parent
/// should be NULL.
void Package(ResTracker* parent = NULL);

private:
void Record();
void Record(bool bumpId = true);

int parent1_;
int parent2_;
Expand Down
14 changes: 11 additions & 3 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <boost/shared_ptr.hpp>

#include "package.h"
#include "cyc_limits.h"

class SimInitTest;

Expand Down Expand Up @@ -94,6 +95,8 @@ class Resource {
/// Returns the package id.
virtual std::string package_name() { return Package::unpackaged_name(); };

virtual Ptr PackageExtract(double qty, std::string new_package_name) = 0;

/// Changes the product's package id
virtual void ChangePackage(std::string new_package_name = Package::unpackaged_name()) {};

Expand All @@ -107,6 +110,12 @@ class Resource {
static int nextstate_id_;
static int nextobj_id_;
int state_id_;
// Setting the state id should only be done when extracting one resource
void state_id(int st_id) {
state_id_ = st_id;
}


int obj_id_;
};

Expand Down Expand Up @@ -143,10 +152,9 @@ std::vector<typename T::Ptr> Resource::Package(Package::Ptr pkg) {
int approx_num_pkgs = fill.second;
Package::ExceedsSplitLimits(approx_num_pkgs);

while (quantity() > 0 && quantity() >= pkg->fill_min()) {
while (quantity() > 0 && (quantity() - pkg->fill_min()) >= -eps_rsrc()) {
double pkg_fill = std::min(quantity(), fill_mass);
t_pkgd = boost::dynamic_pointer_cast<T>(ExtractRes(pkg_fill));
t_pkgd->ChangePackage(pkg->name());
t_pkgd = boost::dynamic_pointer_cast<T>(PackageExtract(pkg_fill, pkg->name()));
ts_pkgd.push_back(t_pkgd);
}
return ts_pkgd;
Expand Down
2 changes: 1 addition & 1 deletion src/toolkit/matl_buy_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ void MatlBuyPolicy::AcceptMatlTrades(
// check if cumulative cap has been reached. If yes, then sample for dormant
// length and reset cycle_total_inv
if (use_cumulative_capacity() && (
(cumulative_cap_ - cycle_total_inv_) < eps())) {
(cumulative_cap_ - cycle_total_inv_) < eps_rsrc())) {
SetNextDormantTime();
LGH(INFO3) << "cycle cumulative inventory has been reached. Dormant period will end at " << next_dormant_end_ << std::endl;
cycle_total_inv_ = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/toolkit/matl_sell_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void MatlSellPolicy::GetMatlTrades(
if (shippable_pkgs > 0){
qty = it->amt;
LGH(INFO3) << " sending " << qty << " kg of " << it->request->commodity();
Material::Ptr mat = buf_->Pop(qty, cyclus::eps_rsrc());
Material::Ptr mat = buf_->Pop(qty, eps_rsrc());
Material::Ptr trade_mat;

// don't go through packaging if you don't need to. packaging always bumps
Expand All @@ -274,7 +274,7 @@ void MatlSellPolicy::GetMatlTrades(
if (package_->name() != mat->package_name()) { // packaging needed
std::vector<Material::Ptr> mat_pkgd = mat->Package<Material>(package_);

if (mat->quantity() > eps()) {
if (mat->quantity() > eps_rsrc()) {
// push any extra material that couldn't be packaged back onto buffer
// don't push unless there's leftover material
buf_->Push(mat);
Expand Down

0 comments on commit 55b466b

Please sign in to comment.