Skip to content

Commit

Permalink
Merge pull request cyclus#1687 from gonuke/ResBulk
Browse files Browse the repository at this point in the history
Allow ResBuf to be a bulk storage facility by forcing a squash of all incoming resources
  • Loading branch information
nuclearkatie authored Mar 14, 2024
2 parents 26f05e6 + cea8e94 commit 08a8cd3
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Since last release
* AddMutalReqs and AddReciepe functions and exclusive bids in python API of DRE (#1584)
* Created Package class and optional declaration of packages in input files (#1673, #1699), package id is a member of resources (materials/products) (#1675)
* CI support for Rocky Linux (#1691)
* Added support for a ResBuf to behave as a single bulk storage with mixing & extraction of resources (#1687)

**Changed:**

Expand Down
6 changes: 4 additions & 2 deletions src/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Material: public Resource {
double threshold = eps_rsrc());

/// Combines material mat with this one. mat's quantity becomes zero.
void Absorb(Ptr mat);
virtual void Absorb(Ptr mat);

/// Changes the material's composition to c without changing its mass. Use
/// this method for things like converting fresh to spent fuel via burning in
Expand All @@ -136,7 +136,9 @@ class Material: public Resource {
/// not result in an updated material composition. Does nothing if the
/// simulation decay mode is set to "never" or none of the nuclides' decay
/// constants are significant with respect to the time delta.
void Decay(int curr_time);
/// @param curr_time current time to use for the decay calculation
/// (default: -1 forces the decay to the context's current time)
virtual void Decay(int curr_time = -1);

/// Returns the last time step on which a decay calculation was performed
/// for the material. This is not necessarily synonymous with the last time
Expand Down
12 changes: 12 additions & 0 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <vector>
#include <boost/shared_ptr.hpp>

#include "error.h"

class SimInitTest;

namespace cyclus {
Expand Down Expand Up @@ -79,6 +81,16 @@ class Resource {
/// @return a new resource object with same state id and quantity == quantity
virtual Ptr ExtractRes(double quantity) = 0;

/// To enable the Decay method to be called on any child resource, define
/// a null op Decay method here.
/// @param curr_time the current time for the decay oepration
virtual void Decay(int curr_time) { throw Error("cannot decay resource type " + this->type()); };

/// To enable the Absorb method to be called on any child resource, define
/// a null op Absorb method here.
/// @param res pointer to a resource to be absorbed by this resource
virtual void Absorb(Ptr res) { throw Error("cannot absorb resource type " + this->type()); };

protected:
const static int default_package_id_ = 1;
private:
Expand Down
66 changes: 43 additions & 23 deletions src/toolkit/res_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ typedef std::vector<Product::Ptr> ProdVec;
template <class T>
class ResBuf {
public:
ResBuf() : cap_(INFINITY), qty_(0) { }
ResBuf(bool is_bulk=false) : cap_(INFINITY), qty_(0), is_bulk_(is_bulk) { }

virtual ~ResBuf() {}

Expand Down Expand Up @@ -233,16 +233,16 @@ class ResBuf {
return r;
}

/// Pushes a single resource object to the buffer.
/// Resource objects are never combined in the buffer; they are stored as
/// unique objects. The resource object is only pushed to the buffer if it
/// does not cause the buffer to exceed its capacity.
/// Pushes a single resource object to the buffer. If not classified as a bulk
/// storage buffer, resource objects are not combined in the buffer; they
/// are stored as unique objects. The resource object is only pushed to the
/// buffer if it does not cause the buffer to exceed its capacity.
///
/// @throws ValueError the pushing of the given resource object would
/// cause the buffer to exceed its capacity.
/// @throws ValueError the pushing of the given resource object would cause
/// the buffer to exceed its capacity.
///
/// @throws KeyError the resource object to be pushed is already present
/// in the buffer.
/// @throws KeyError the resource object to be pushed is already present in
/// the buffer.
void Push(Resource::Ptr r) {
typename T::Ptr m = boost::dynamic_pointer_cast<T>(r);
if (m == NULL) {
Expand All @@ -255,24 +255,28 @@ class ResBuf {
} else if (rs_present_.count(m) == 1) {
throw KeyError("duplicate resource push attempted");
}

rs_.push_back(m);
rs_present_.insert(m);
if (!is_bulk_ || rs_.size() == 0) {
rs_.push_back(m);
rs_present_.insert(m);
} else {
rs_.front()->Absorb(m);
}
qty_ += r->quantity();
UpdateQty();
}

/// Pushes one or more resource objects (as a std::vector) to the buffer.
/// Resource objects are never squashed in the buffer; they are stored as
/// unique objects. The resource objects are only pushed to the buffer if
/// they do not cause the buffer to exceed its capacity; otherwise none of the
/// given resource objects are added to the buffer.
/// Pushes one or more resource objects (as a std::vector) to the buffer. If
/// not classified as a bulk storage buffer, resource objects are not
/// squashed in the buffer; they are stored as unique objects. The resource
/// objects are only pushed to the buffer if they do not cause the buffer to
/// exceed its capacity; otherwise none of the given resource objects are
/// added to the buffer.
///
/// @throws ValueError adding the given resource objects would
/// cause the buffer to exceed its capacity.
/// @throws ValueError adding the given resource objects would cause the
/// buffer to exceed its capacity.
///
/// @throws KeyError one or more of the resource objects to be added
/// are already present in the buffer.
/// @throws KeyError one or more of the resource objects to be added are
/// already present in the buffer.
template <class B>
void Push(std::vector<B> rs) {
std::vector<typename T::Ptr> rss;
Expand Down Expand Up @@ -300,12 +304,25 @@ class ResBuf {
}

for (int i = 0; i < rss.size(); i++) {
rs_.push_back(rss[i]);
rs_present_.insert(rss[i]);
if (!is_bulk_ || rs_.size() == 0) {
rs_.push_back(rss[i]);
rs_present_.insert(rss[i]);
} else {
rs_.front()->Absorb(rss[i]);
}
}
qty_ += tot_qty;
}

/// Decays all the materials in a resource buffer
/// @param curr_time time to calculate decay inventory
/// (default: -1 uses the current time of the context)
void Decay(int curr_time = -1) {
for (int i = 0; i < rs_.size(); i++) {
rs_.at(i)->Decay(curr_time);
}
}

private:
void UpdateQty() {
int n = rs_.size();
Expand All @@ -321,6 +338,9 @@ class ResBuf {
/// Maximum quantity of resources this buffer can hold
double cap_;

/// Whether materials should be stored as a single squashed item or as individual resource objects
bool is_bulk_;

/// List of constituent resource objects forming the buffer's inventory
std::list<typename T::Ptr> rs_;
std::set<typename T::Ptr> rs_present_;
Expand Down
Loading

0 comments on commit 08a8cd3

Please sign in to comment.