Skip to content

Commit

Permalink
changing package id checks package type limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearkatie committed Feb 28, 2024
1 parent ab2bb12 commit 82cde77
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 14 deletions.
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
28 changes: 24 additions & 4 deletions src/material.cc
Original file line number Diff line number Diff line change
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 @@ -241,8 +265,4 @@ int Material::package_id() {
return package_id_;
}

void Material::ChangePackageId(int new_package_id) {
package_id_ = new_package_id;
}

} // namespace cyclus
5 changes: 3 additions & 2 deletions src/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ class Material: public Resource {
/// Returns the package id.
virtual int package_id();

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

protected:
Material(Context* ctx, double quantity, Composition::Ptr c, int package_id = default_package_id_);
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
24 changes: 24 additions & 0 deletions src/product.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ Resource::Ptr Product::ExtractRes(double qty) {
return boost::static_pointer_cast<Resource>(Extract(qty));
}

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, int package_id)
: quality_(quality),
Expand Down
3 changes: 3 additions & 0 deletions src/product.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class Product : public Resource {
/// @throws ValueError 'other' resource is of different quality
void Absorb(Product::Ptr other);

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

private:
/// @param ctx the simulation context
/// @param quantity is a double indicating the quantity
Expand Down

0 comments on commit 82cde77

Please sign in to comment.