Skip to content

Commit

Permalink
Merge pull request cyclus#1778 from nuclearkatie/resbuf_pkg
Browse files Browse the repository at this point in the history
use keep_packaging in resbuf
  • Loading branch information
gonuke authored Jul 29, 2024
2 parents bf43602 + 070202c commit 7ad7a38
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Since last release
* 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)
* Use keep_packaging instead of unpackaged in ResBuf (#1778)

**Removed:**

Expand Down
25 changes: 21 additions & 4 deletions src/toolkit/res_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ typedef std::vector<Product::Ptr> ProdVec;
template <class T>
class ResBuf {
public:
ResBuf(bool is_bulk=false, bool unpackaged=true) : cap_(INFINITY), qty_(0), is_bulk_(is_bulk), unpackaged_(unpackaged) { }
ResBuf(bool is_bulk=false, bool keep_pkg=false) : qty_(0), is_bulk_(is_bulk) {
capacity(INFINITY);
keep_packaging(keep_pkg);
}

virtual ~ResBuf() {}

Expand All @@ -76,6 +79,10 @@ class ResBuf {
/// @throws ValueError the new capacity is lower (by eps_rsrc()) than the
/// quantity of resources that exist in the buffer.
void capacity(double cap) {
if (cap < 0) {
throw ValueError("capacity must not be negative");
}

if (quantity() - cap > eps_rsrc()) {
std::stringstream ss;
ss << std::setprecision(17) << "new capacity " << cap
Expand All @@ -85,6 +92,16 @@ class ResBuf {
cap_ = cap;
}

/// Sets whether the buffer should keep packaged resources
void keep_packaging(bool keep_packaging) {
if (is_bulk_ && keep_packaging) {
throw ValueError("bulk storage resbufs cannot keep packaging. Only one of the two options can be true.");
}
keep_packaging_ = keep_packaging;
}

bool keep_packaging() const { return keep_packaging_; }

/// Returns the total number of constituent resource objects
/// in the buffer. Never throws.
inline int count() const { return rs_.size(); }
Expand Down Expand Up @@ -267,7 +284,7 @@ class ResBuf {

if (!is_bulk_ || rs_.size() == 0) {
// strip package id and set as default
if (unpackaged_) {
if (!keep_packaging_) {
m->ChangePackage();
}
rs_.push_back(m);
Expand Down Expand Up @@ -319,7 +336,7 @@ class ResBuf {

for (int i = 0; i < rss.size(); i++) {
if (!is_bulk_ || rs_.size() == 0) {
if (unpackaged_) {
if (!keep_packaging_) {
rss[i]->ChangePackage();
}
rs_.push_back(rss[i]);
Expand Down Expand Up @@ -359,7 +376,7 @@ class ResBuf {
bool is_bulk_;
/// Whether materials should be stripped of their packaging before being
/// pushed onto the resbuf. If res_buf is bulk, this is assumed true.
bool unpackaged_;
bool keep_packaging_;

/// List of constituent resource objects forming the buffer's inventory
std::list<typename T::Ptr> rs_;
Expand Down
18 changes: 18 additions & 0 deletions tests/toolkit/res_buf_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,24 @@ TEST_F(MaterialBufTest, BulkTest) {

}

TEST_F(MaterialBufTest, KeepPackaging) {
keep_pkg_store_.Push(mat4_pkgd_);
ASSERT_EQ(keep_pkg_store_.Pop()->package_name(), pkg_name_);
}

TEST_F(MaterialBufTest, ChangeKeepPackaging) {
keep_pkg_store_.Push(mat4_pkgd_);
ASSERT_EQ(keep_pkg_store_.Pop()->package_name(), pkg_name_);

keep_pkg_store_.keep_packaging(false);
keep_pkg_store_.Push(mat4_pkgd_);
ASSERT_EQ(keep_pkg_store_.Pop()->package_name(), cyclus::Package::unpackaged_name());
}

TEST_F(MaterialBufTest, BulkKeepPackaging) {
EXPECT_THROW(ResBuf<Material>(true, true), ValueError);
}


} // namespace toolkit
} // namespace cyclus
26 changes: 24 additions & 2 deletions tests/toolkit/res_buf_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
#include "composition.h"
#include "material.h"
#include "toolkit/res_buf.h"
#include "context.h"
#include "recorder.h"
#include "timer.h"
#include "region.h"

namespace cyclus {
namespace toolkit {

class Dummy : public cyclus::Region {
public:
Dummy(cyclus::Context* ctx) : cyclus::Region(ctx) {}
Dummy* Clone() { return NULL; }
};

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class ProductBufTest : public ::testing::Test {
protected:
Expand Down Expand Up @@ -70,13 +80,21 @@ class MaterialBufTest : public ::testing::Test {

ResBuf<Material> mat_store_; // default constructed mat store
ResBuf<Material> bulk_store_ = ResBuf<Material>(true);
ResBuf<Material> keep_pkg_store_ = ResBuf<Material>(false, true);

Nuc sr89_, fe59_;
Material::Ptr mat1a_, mat1b_, mat2a_, mat2b_, mat3_;
Material::Ptr mat1a_, mat1b_, mat2a_, mat2b_, mat3_, mat4_pkgd_;
Composition::Ptr test_comp1_, test_comp2_, test_comp3_;

std::string pkg_name_ = "keep";

double cap_;

cyclus::Timer ti;
cyclus::Recorder rec;
cyclus::Context* ctx = new cyclus::Context(&ti, &rec);
cyclus::Agent* dummy = new Dummy(ctx);

virtual void SetUp() {
try {

Expand All @@ -94,13 +112,17 @@ class MaterialBufTest : public ::testing::Test {
test_comp3_ = Composition::CreateFromMass(w);

double mat_size = 5 * units::g;

mat1a_ = Material::CreateUntracked(mat_size, test_comp1_);
mat1b_ = Material::CreateUntracked(mat_size, test_comp1_);
mat2a_ = Material::CreateUntracked(mat_size, test_comp2_);
mat2b_ = Material::CreateUntracked(mat_size, test_comp2_);
mat3_ = Material::CreateUntracked(mat_size, test_comp3_);

ctx->AddPackage(pkg_name_, mat_size, mat_size);
cyclus::Package::Ptr pkg = ctx->GetPackage(pkg_name_);
mat4_pkgd_ = Material::Create(dummy, mat_size, test_comp1_, pkg_name_);
mat4_pkgd_->ChangePackage(pkg_name_);

cap_ = 10 * mat_size;

mat_store_.capacity(cap_);
Expand Down

0 comments on commit 7ad7a38

Please sign in to comment.