Skip to content

Commit

Permalink
Merge pull request #1749 from nuclearkatie/packaging
Browse files Browse the repository at this point in the history
Add packaging to matl sell policy
  • Loading branch information
gonuke committed May 30, 2024
2 parents e3df773 + 6144905 commit eaa0cd1
Show file tree
Hide file tree
Showing 27 changed files with 353 additions and 163 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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)

**Removed:**

Expand Down
3 changes: 3 additions & 0 deletions cli/cycpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,9 @@ class SchemaFilter(CodeGeneratorFilter):
'recipe': None,
'inrecipe': None,
'outrecipe': None,
'package': None,
'inpackage': None,
'outpackage': None,
'none': None,
None: None,
'': None,
Expand Down
2 changes: 1 addition & 1 deletion cyclus/gentypesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,7 @@ def capsule_any_to_py(value):
uitype The type of the input field in reference in a UI,
currently supported types are; incommodity, outcommodity,
commodity, range, combobox, facility, prototype, recipe, nuclide,
and none.
package, and none.
For 'nuclide' when the type is an int, the values will be read in
from the input file in human readable string format ('U-235') and
automatically converted to results of ``pyne::nucname::id()``
Expand Down
40 changes: 26 additions & 14 deletions src/bid.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <limits>

#include "request.h"
#include "package.h"

namespace cyclus {

Expand Down Expand Up @@ -51,23 +52,26 @@ template <class T> class Bid {
offer,
Trader* bidder,
typename BidPortfolio<T>::Ptr portfolio,
bool exclusive = false) {
bool exclusive = false,
Package::Ptr package = Package::unpackaged()) {
return Create(request, offer, bidder, portfolio, exclusive,
std::numeric_limits<double>::quiet_NaN());
std::numeric_limits<double>::quiet_NaN(), package);
}
/// @brief a factory method for a bid for a bid without a portfolio
/// @warning this factory should generally only be used for testing
inline static Bid<T>* Create(Request<T>* request, boost::shared_ptr<T> offer,
Trader* bidder, bool exclusive,
double preference) {
return new Bid<T>(request, offer, bidder, exclusive, preference);
double preference,
Package::Ptr package = Package::unpackaged()) {
return new Bid<T>(request, offer, bidder, exclusive, preference, package);
}
/// @brief a factory method for a bid for a bid without a portfolio
/// @warning this factory should generally only be used for testing
inline static Bid<T>* Create(Request<T>* request, boost::shared_ptr<T> offer,
Trader* bidder, bool exclusive = false) {
Trader* bidder, bool exclusive = false,
Package::Ptr package = Package::unpackaged()) {
return Create(request, offer, bidder, exclusive,
std::numeric_limits<double>::quiet_NaN());
std::numeric_limits<double>::quiet_NaN(), package);
}

/// @return the request being responded to
Expand All @@ -91,45 +95,53 @@ template <class T> class Bid {
private:
/// @brief constructors are private to require use of factory methods
Bid(Request<T>* request, boost::shared_ptr<T> offer, Trader* bidder,
bool exclusive, double preference)
bool exclusive, double preference,
Package::Ptr package = Package::unpackaged())
: request_(request),
offer_(offer),
bidder_(bidder),
exclusive_(exclusive),
preference_(preference) {}
preference_(preference),
package_(package) {}
/// @brief constructors are private to require use of factory methods
Bid(Request<T>* request, boost::shared_ptr<T> offer, Trader* bidder,
bool exclusive = false)
bool exclusive = false, Package::Ptr package = Package::unpackaged())
: request_(request),
offer_(offer),
bidder_(bidder),
exclusive_(exclusive),
preference_(std::numeric_limits<double>::quiet_NaN()) {}
preference_(std::numeric_limits<double>::quiet_NaN()),
package_(package) {}

Bid(Request<T>* request, boost::shared_ptr<T> offer, Trader* bidder,
typename BidPortfolio<T>::Ptr portfolio, bool exclusive, double preference)
typename BidPortfolio<T>::Ptr portfolio, bool exclusive, double preference,
Package::Ptr package = Package::unpackaged())
: request_(request),
offer_(offer),
bidder_(bidder),
portfolio_(portfolio),
exclusive_(exclusive),
preference_(preference) {}
preference_(preference),
package_(package) {}

Bid(Request<T>* request, boost::shared_ptr<T> offer, Trader* bidder,
typename BidPortfolio<T>::Ptr portfolio, bool exclusive = false)
typename BidPortfolio<T>::Ptr portfolio, bool exclusive = false,
Package::Ptr package = Package::unpackaged())
: request_(request),
offer_(offer),
bidder_(bidder),
portfolio_(portfolio),
exclusive_(exclusive),
preference_(std::numeric_limits<double>::quiet_NaN()) {}
preference_(std::numeric_limits<double>::quiet_NaN()),
package_(package) {}

Request<T>* request_;
boost::shared_ptr<T> offer_;
Trader* bidder_;
boost::weak_ptr<BidPortfolio<T>> portfolio_;
bool exclusive_;
double preference_;
Package::Ptr package_;
};

} // namespace cyclus
Expand Down
41 changes: 18 additions & 23 deletions src/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,42 +193,37 @@ Composition::Ptr Context::GetRecipe(std::string name) {

void Context::AddPackage(std::string name, double fill_min, double fill_max,
std::string strategy) {
packages_[name] = Package::Create(name, fill_min, fill_max, strategy);
if (packages_.count(name) == 0) {
Package::Ptr pkg = Package::Create(name, fill_min, fill_max, strategy);
packages_[name] = pkg;
RecordPackage(pkg);
} else {
throw KeyError("Package " + name + " already exists!");
}
}

void Context::RecordPackage(Package::Ptr pkg) {
NewDatum("Packages")
->AddVal("Package", name)
->AddVal("FillMin", fill_min)
->AddVal("FillMax", fill_max)
->AddVal("Strategy", strategy)
->AddVal("PackageName", pkg->name())
->AddVal("FillMin", pkg->fill_min())
->AddVal("FillMax", pkg->fill_max())
->AddVal("Strategy", pkg->strategy())
->Record();
}

Package::Ptr Context::GetPackageByName(std::string name) {
Package::Ptr Context::GetPackage(std::string name) {
if (name == Package::unpackaged_name()) {
return Package::unpackaged();
}
if (packages_.size() == 0 ) {
throw KeyError("No user-created packages exist");
}
if (packages_.count(name) == 0) {
throw KeyError("Invalid package name " + name);
}
return packages_[name];
}

Package::Ptr Context::GetPackageById(int id) {
if (id == Package::unpackaged_id()) {
return Package::unpackaged();
}
if (id < 0) {
throw ValueError("Invalid package id " + std::to_string(id));
}
// iterate through the list of packages to get the one package with the correct id
std::map<std::string, Package::Ptr>::iterator it;
for (it = packages_.begin(); it != packages_.end(); ++it) {
if (it->second->id() == id) {
return it->second;
}
}
throw ValueError("Invalid package id " + std::to_string(id));
}

void Context::InitSim(SimInfo si) {
NewDatum("Info")
->AddVal("Handle", si.handle)
Expand Down
8 changes: 5 additions & 3 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,12 @@ class Context {
double fill_max = std::numeric_limits<double>::max(),
std::string strategy = "first");

// Retrieve a registered package.
Package::Ptr GetPackageByName(std::string name);
/// Records package information. Should be used first on unpackaged, then
/// to record user-declared packages
void RecordPackage(Package::Ptr);

Package::Ptr GetPackageById(int id);
/// Retrieve a registered package.
Package::Ptr GetPackage(std::string name);

int random();

Expand Down
31 changes: 16 additions & 15 deletions src/material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const ResourceType Material::kType = "Material";
Material::~Material() {}

Material::Ptr Material::Create(Agent* creator, double quantity,
Composition::Ptr c, int package_id) {
Material::Ptr m(new Material(creator->context(), quantity, c, package_id));
Composition::Ptr c, std::string package_name) {
Material::Ptr m(new Material(creator->context(), quantity, c, package_name));
m->tracker_.Create(creator);
return m;
}

Material::Ptr Material::CreateUntracked(double quantity,
Composition::Ptr c) {
Material::Ptr m(new Material(NULL, quantity, c, Package::unpackaged_id()));
Material::Ptr m(new Material(NULL, quantity, c, Package::unpackaged_name()));
return m;
}

Expand All @@ -48,7 +48,6 @@ void Material::Record(Context* ctx) const {
ctx_->NewDatum("MaterialInfo")
->AddVal("ResourceId", state_id())
->AddVal("PrevDecayTime", prev_decay_time_)
->AddVal("PackageId", package_id_)
->Record();

comp_->Record(ctx);
Expand Down Expand Up @@ -88,7 +87,7 @@ Material::Ptr Material::ExtractComp(double qty, Composition::Ptr c,
}

qty_ -= qty;
Material::Ptr other(new Material(ctx_, qty, c, Package::unpackaged_id()));
Material::Ptr other(new Material(ctx_, qty, c, Package::unpackaged_name()));

// Decay called on the extracted material should have the same dt as for
// this material regardless of composition.
Expand Down Expand Up @@ -141,22 +140,24 @@ void Material::Transmute(Composition::Ptr c) {
}
}

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

Package::Ptr p = ctx_->GetPackageById(package_id_);
Package::Ptr p = ctx_->GetPackage(package_name_);
double min = p->fill_min();
double max = p->fill_max();
if (qty_ >= min && qty_ <= max) {
package_id_ = new_package_id;
package_name_ = new_package_name;
tracker_.Package();
} else {
throw ValueError("Material quantity is outside of package fill limits.");
}
Expand Down Expand Up @@ -239,13 +240,13 @@ Composition::Ptr Material::comp() {
return comp_;
}

Material::Material(Context* ctx, double quantity, Composition::Ptr c, int package_id)
Material::Material(Context* ctx, double quantity, Composition::Ptr c, std::string package_name)
: qty_(quantity),
comp_(c),
tracker_(ctx, this),
ctx_(ctx),
prev_decay_time_(0),
package_id_(package_id) {
package_name_(package_name) {
if (ctx != NULL) {
prev_decay_time_ = ctx->time();
} else {
Expand All @@ -258,8 +259,8 @@ Material::Ptr NewBlankMaterial(double quantity) {
return Material::CreateUntracked(quantity, comp);
}

int Material::package_id() {
return package_id_;
std::string Material::package_name() {
return package_name_;
}

} // namespace cyclus
12 changes: 7 additions & 5 deletions src/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class Material: public Resource {
/// pointer to the agent creating the resource (usually will be the caller's
/// "this" pointer). All future output data recorded will be done using the
/// creator's context.
static Ptr Create(Agent* creator, double quantity, Composition::Ptr c, int package_id = Package::unpackaged_id());
static Ptr Create(Agent* creator, double quantity, Composition::Ptr c,
std::string package_name = Package::unpackaged_name());

/// Creates a new material resource that does not actually exist as part of
/// the simulation and is untracked.
Expand Down Expand Up @@ -155,22 +156,23 @@ class Material: public Resource {
/// DEPRECATED - use non-const comp() function.
Composition::Ptr comp() const;

virtual int package_id();
virtual std::string package_name();

/// Changes the package id. Checks that the resource fits the package
/// type minimum and maximum mass criteria.
virtual void ChangePackageId(int new_package_id = Package::unpackaged_id());
virtual void ChangePackage(std::string new_package_name = Package::unpackaged_name());

protected:
Material(Context* ctx, double quantity, Composition::Ptr c, int package_id = Package::unpackaged_id());
Material(Context* ctx, double quantity, Composition::Ptr c,
std::string package_name = Package::unpackaged_name());

private:
Context* ctx_;
double qty_;
Composition::Ptr comp_;
int prev_decay_time_;
ResTracker tracker_;
int package_id_;
std::string package_name_;
};

/// Creates and returns a new material with the specified quantity and a
Expand Down
19 changes: 8 additions & 11 deletions src/package.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

namespace cyclus {

// unpackaged id is 1, so start the user-declared packaging id at 2
int Package::next_package_id_ = 2;
Package::Ptr Package::unpackaged_ = NULL;

Package::Ptr Package::Create(std::string name, double fill_min, double fill_max, std::string strategy) {
Package::Ptr Package::Create(std::string name, double fill_min,
double fill_max,std::string strategy) {
if (fill_min < 0 || fill_max < 0) {
throw ValueError("fill_min and fill_max must be non-negative");
}
Expand Down Expand Up @@ -43,26 +42,24 @@ double Package::GetFillMass(double qty) {
int num_min_fill = std::floor(qty / fill_min_);
int num_max_fill = std::ceil(qty / fill_max_);
if (num_min_fill >= num_max_fill) {
// all material can fit in a package
double fill_mass = qty / num_max_fill;
// all material can fit in package(s)
fill_mass = qty / num_max_fill;
} else {
// some material will remain unpackaged, fill up as many max packages as possible
fill_mass = fill_max_;
}
}
return fill_mass;
return std::min(qty, fill_mass);
}

Package::Package(std::string name, double fill_min, double fill_max, std::string strategy) :
Package::Package(std::string name, double fill_min, double fill_max,
std::string strategy) :
name_(name), fill_min_(fill_min), fill_max_(fill_max), strategy_(strategy) {
if (name == unpackaged_name_) {
if (unpackaged_) {
throw ValueError("can't create a new package with name 'unpackaged'");
}
id_ = unpackaged_id_;
} else {
id_ = next_package_id_++;
}
}
}

} // namespace cyclus
Loading

0 comments on commit eaa0cd1

Please sign in to comment.