Skip to content

Commit

Permalink
getfillmass and repackage
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearkatie committed Mar 21, 2024
1 parent 548ea52 commit 69d38ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
27 changes: 4 additions & 23 deletions src/package.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Package::Ptr Package::Create(std::string name, double fill_min, double fill_max,
Ptr p(new Package(name, fill_min, fill_max, strategy));
return p;
}

Package::Package() : id_(next_id_++), fill_min_(0), fill_max_(std::numeric_limits<double>::max()) {}

Package::Package(std::string name, double fill_min, double fill_max, std::string strategy) : name_(name), id_(next_id_++), fill_min_(fill_min), fill_max_(fill_max), strategy_(strategy) {}

double GetFillMass(Resource::Ptr r, Package::Ptr pkg) {
if (r->quantity() < pkg->fill_min()) {
Expand All @@ -46,27 +50,4 @@ double GetFillMass(Resource::Ptr r, Package::Ptr pkg) {
return fill_mass;
}

std::vector<typename T::Ptr> Package::Repackage(typename T::Ptr r,
Package::Ptr pkg) {
std::vector<typename T::Ptr> rs_pkgd;
typename T::Ptr r_pkgd;

double fill_mass = GetFillMass(r, pkg);
if (fill_mass ==0) {
return rs_pkgd;
}

while (r->quantity() > pkg->fill_min()) {
double pkg_fill = std::min(r->quantity(), fill_mass);
r_pkgd = boost::dynamic_pointer_cast<T>(r->ExtractRes(pkg_fill));
r_pkgd->ChangePackageId(pkg->id());
rs_pkgd.push_back(r_pkgd);
}
return rs_pkgd;
}

Package::Package() : id_(next_id_++), fill_min_(0), fill_max_(std::numeric_limits<double>::max()) {}

Package::Package(std::string name, double fill_min, double fill_max, std::string strategy) : name_(name), id_(next_id_++), fill_min_(fill_min), fill_max_(fill_max), strategy_(strategy) {}

} // namespace cyclus
39 changes: 28 additions & 11 deletions src/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <boost/shared_ptr.hpp>
#include "resource.h"

Expand All @@ -21,17 +22,6 @@ class Package {
// create a new package type
static Ptr Create(std::string name, double fill_min, double fill_max, std::string strategy);

/// Given a single resource and a package type, returns optimal fill mass
/// for the resource to be packaged. Can be used to determine how to
/// respond to requests for material, and to actually package and send off
/// trades
double GetFillMass(Resource::Ptr r, Package::Ptr pkg);

/// Repackages a single resource into a package. If some quantity of the
/// resource cannot be packaged, the remainder is left in the resource
/// object.
std::vector<typename T::Ptr> Repackage(typename T::Ptr r, Package::Ptr pkg);

// returns package id
int id() const { return id_; }
// returns package name
Expand All @@ -56,6 +46,33 @@ class Package {
std::string strategy_;
};

/// Returns optimal fill mass for a resource to be packaged. Can be used
/// to determine how to respond to requests for material, and to actually
/// package and send off trades
double GetFillMass(Resource::Ptr r, Package::Ptr pkg);

/// Repackages a single resource into a package. If some quantity of the
/// resource cannot be packaged using the given packaging strategy and
/// restrictions, the remainder is left in the resource object.
template <class T>
std::vector<typename T::Ptr> Repackage(typename T::Ptr r, Package::Ptr pkg) {
std::vector<typename T::Ptr> rs_pkgd;
typename T::Ptr r_pkgd;

double fill_mass = GetFillMass(r, pkg);
if (fill_mass ==0) {
return rs_pkgd;
}

while (r->quantity() > pkg->fill_min()) {
double pkg_fill = std::min(r->quantity(), fill_mass);
r_pkgd = boost::dynamic_pointer_cast<T>(r->ExtractRes(pkg_fill));
r_pkgd->ChangePackageId(pkg->id());
rs_pkgd.push_back(r_pkgd);
}
return rs_pkgd;
};

} // namespace cyclus

#endif // CYCLUS_SRC_PACKAGE_H_

0 comments on commit 69d38ad

Please sign in to comment.