Skip to content

Commit

Permalink
Merge pull request cyclus#1675 from nuclearkatie/packaging
Browse files Browse the repository at this point in the history
Package ID for resources (materials/products)
  • Loading branch information
gonuke authored Feb 29, 2024
2 parents 2db5599 + 4937f93 commit ae9f9d4
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Since last release
* Adds support for Cython3 (#1636)
* Adds TotalInvTracker, which allows an inventory cap to be set for multiple resource buffers, and is now required for material buy policy (#1646)
* 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)
* Created Package class and optional declaration of packages in input files (#1673), package id is a member of resources (materials/products) (#1675)

**Changed:**

Expand Down
12 changes: 11 additions & 1 deletion src/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,23 @@ void Context::AddPackage(std::string name, double fill_min, double fill_max,
->Record();
}

Package::Ptr Context::GetPackage(std::string name) {
Package::Ptr Context::GetPackageByName(std::string name) {
if (packages_.count(name) == 0) {
throw KeyError("Invalid package name " + name);
}
return packages_[name];
}

Package::Ptr Context::GetPackageById(int 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;
}
}
}

void Context::InitSim(SimInfo si) {
NewDatum("Info")
->AddVal("Handle", si.handle)
Expand Down
4 changes: 3 additions & 1 deletion src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ class Context {
std::string strategy = "first");

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

Package::Ptr GetPackageById(int id);

int random();

Expand Down
43 changes: 36 additions & 7 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) {
Material::Ptr m(new Material(creator->context(), quantity, c));
Composition::Ptr c, int package_id) {
Material::Ptr m(new Material(creator->context(), quantity, c, package_id));
m->tracker_.Create(creator);
return m;
}

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

Expand All @@ -48,6 +48,7 @@ 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 @@ -87,8 +88,7 @@ Material::Ptr Material::ExtractComp(double qty, Composition::Ptr c,
}

qty_ -= qty;

Material::Ptr other(new Material(ctx_, qty, c));
Material::Ptr other(new Material(ctx_, qty, c, default_package_id_));

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

void Material::ChangePackageId(int new_package_id) {
if (ctx_ != NULL) {
throw ValueError("Package Id cannot be changed with NULL context");
}
if (new_package_id == package_id_) {
// no change needed
return;
}
else if (new_package_id == default_package_id_) {
// default has functionally no restrictions
package_id_ = new_package_id;
return;
}

Package::Ptr p = ctx_->GetPackageById(package_id_);
double min = p->fill_min();
double max = p->fill_max();
if (qty_ >= min && qty_ <= max) {
package_id_ = new_package_id;
} else {
throw ValueError("Material quantity is outside of package fill limits.");
}
}

void Material::Decay(int curr_time) {
if (ctx_ != NULL && ctx_->sim_info().decay == "never") {
return;
Expand Down Expand Up @@ -218,12 +242,13 @@ Composition::Ptr Material::comp() {
return comp_;
}

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

int Material::package_id() {
return package_id_;
}

} // namespace cyclus
11 changes: 9 additions & 2 deletions src/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ 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);
static Ptr Create(Agent* creator, double quantity, Composition::Ptr c, int package_id = default_package_id_);

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

int package_id();

/// Changes the package id. Checks that the resource fits the package
/// type minimum and maximum mass criteria.
void ChangePackageId(int new_package_id = default_package_id_);

protected:
Material(Context* ctx, double quantity, Composition::Ptr c);
Material(Context* ctx, double quantity, Composition::Ptr c, int package_id = default_package_id_);

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

/// Creates and returns a new material with the specified quantity and a
Expand Down
6 changes: 1 addition & 5 deletions src/package.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ 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;
}

int Package::id() {
return id_;
}

Package::Package() : id_(next_id_++), fill_min_(0), fill_max_(std::numeric_limits<double>::max()), strategy_("first") {}
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) {}

Expand Down
11 changes: 10 additions & 1 deletion src/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ class Package {
// create a new package type
static Ptr Create(std::string name, double fill_min, double fill_max, std::string strategy);

int id();
// returns package id
int id() const { return id_; }
// returns package name
std::string name() const { return name_; }
// returns package fill min
double fill_min() const { return fill_min_; }
// returns package fill max
double fill_max() const { return fill_max_; }
// returns package strategy
std::string strategy() const { return strategy_; }

protected:
Package();
Expand Down
40 changes: 35 additions & 5 deletions src/product.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ int Product::next_qualid_ = 1;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Product::Ptr Product::Create(Agent* creator, double quantity,
std::string quality) {
std::string quality, int package_id) {
if (qualids_.count(quality) == 0) {
qualids_[quality] = next_qualid_++;
creator->context()->NewDatum("Products")
->AddVal("QualId", qualids_[quality])
->AddVal("Quality", quality)
->AddVal("PackageId", package_id)
->Record();
}

// the next lines must come after qual id setting
Product::Ptr r(new Product(creator->context(), quantity, quality));
Product::Ptr r(new Product(creator->context(), quantity, quality, package_id));
r->tracker_.Create(creator);
return r;
}
Expand Down Expand Up @@ -62,7 +63,7 @@ Product::Ptr Product::Extract(double quantity) {

quantity_ -= quantity;

Product::Ptr other(new Product(ctx_, quantity, quality_));
Product::Ptr other(new Product(ctx_, quantity, quality_, package_id_));
tracker_.Extract(&other->tracker_);
return other;
}
Expand All @@ -72,11 +73,40 @@ Resource::Ptr Product::ExtractRes(double qty) {
return boost::static_pointer_cast<Resource>(Extract(qty));
}

int Product::package_id() {
return package_id_;
}

void Product::ChangePackageId(int new_package_id) {
if (ctx_ != NULL) {
throw ValueError("Package Id cannot be changed with NULL context");
}
if (new_package_id == package_id_) {
// no change needed
return;
}
else if (new_package_id == default_package_id_) {
// default has functionally no restrictions
package_id_ = new_package_id;
return;
}

Package::Ptr p = ctx_->GetPackageById(package_id_);
double min = p->fill_min();
double max = p->fill_max();
if (quantity_ >= min && quantity_ <= max) {
package_id_ = new_package_id;
} else {
throw ValueError("Material quantity is outside of package fill limits.");
}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Product::Product(Context* ctx, double quantity, std::string quality)
Product::Product(Context* ctx, double quantity, std::string quality, int package_id)
: quality_(quality),
quantity_(quantity),
tracker_(ctx, this),
ctx_(ctx) {}
ctx_(ctx),
package_id_(package_id) {}

} // namespace cyclus
11 changes: 9 additions & 2 deletions src/product.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Product : 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, std::string quality);
static Ptr Create(Agent* creator, double quantity, std::string quality, int package_id = default_package_id_);

/// Creates a new product that does not actually exist as part of
/// the simulation and is untracked.
Expand Down Expand Up @@ -71,11 +71,17 @@ class Product : public Resource {
/// @throws ValueError 'other' resource is of different quality
void Absorb(Product::Ptr other);

// Returns the package id.
int package_id();

/// Changes the product's package id
void ChangePackageId(int new_package_id);

private:
/// @param ctx the simulation context
/// @param quantity is a double indicating the quantity
/// @param quality the resource quality
Product(Context* ctx, double quantity, std::string quality);
Product(Context* ctx, double quantity, std::string quality, int package_id = default_package_id_);

// map<quality, quality_id>
static std::map<std::string, int> qualids_;
Expand All @@ -85,6 +91,7 @@ class Product : public Resource {
std::string quality_;
double quantity_;
ResTracker tracker_;
int package_id_;
};

} // namespace cyclus
Expand Down
2 changes: 2 additions & 0 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class Resource {
/// @return a new resource object with same state id and quantity == quantity
virtual Ptr ExtractRes(double quantity) = 0;

protected:
const static int default_package_id_ = 1;
private:
static int nextstate_id_;
static int nextobj_id_;
Expand Down

0 comments on commit ae9f9d4

Please sign in to comment.