Skip to content

Commit ce21deb

Browse files
authored
Merge pull request cyclus#1894 from dean-krueger/matl-value
Add unit_value to Resource Objects and Children
2 parents 32f8c75 + 5f3f95d commit ce21deb

8 files changed

Lines changed: 123 additions & 54 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Since last release
77

88
**Added:**
99

10+
* Added a unit value to Resource, Material, and Product objects (#1894)
1011
* Added clang-format protections to .cycpp.h and query_backend.h files, modified .clang-format file (#1880)
1112
* Added a function to facility_cost.cycpp.h to calculate unit price of a DRE bid (#1870 ,#1877, #1884, #1889, #1890)
1213
* Added code injection for matl_buy/sell_policy (#1866)

src/material.cc

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ const ResourceType Material::kType = "Material";
1515
Material::~Material() {}
1616

1717
Material::Ptr Material::Create(Agent* creator, double quantity,
18-
Composition::Ptr c, std::string package_name) {
19-
Material::Ptr m(new Material(creator->context(), quantity, c, package_name));
18+
Composition::Ptr c, std::string package_name,
19+
double unit_value) {
20+
Material::Ptr m(
21+
new Material(creator->context(), quantity, c, package_name, unit_value));
2022
m->tracker_.Create(creator);
2123
return m;
2224
}
2325

24-
Material::Ptr Material::CreateUntracked(double quantity, Composition::Ptr c) {
25-
Material::Ptr m(new Material(NULL, quantity, c, Package::unpackaged_name()));
26+
Material::Ptr Material::CreateUntracked(double quantity, Composition::Ptr c,
27+
double unit_value) {
28+
Material::Ptr m(
29+
new Material(NULL, quantity, c, Package::unpackaged_name(), unit_value));
2630
return m;
2731
}
2832

@@ -86,7 +90,8 @@ Material::Ptr Material::ExtractComp(double qty, Composition::Ptr c,
8690
}
8791

8892
qty_ -= qty;
89-
Material::Ptr other(new Material(ctx_, qty, c, Package::unpackaged_name()));
93+
Material::Ptr other(
94+
new Material(ctx_, qty, c, Package::unpackaged_name(), UnitValue()));
9095

9196
// Decay called on the extracted material should have the same dt as for
9297
// this material regardless of composition.
@@ -117,8 +122,11 @@ void Material::Absorb(Material::Ptr mat) {
117122
if (qty_ < mat->qty_) {
118123
prev_decay_time_ = mat->prev_decay_time_;
119124
}
120-
121-
qty_ += mat->qty_;
125+
double tot_mass = qty_ + mat->quantity();
126+
double avg_unit_value =
127+
(qty_ * UnitValue() + mat->quantity() * mat->UnitValue()) / tot_mass;
128+
SetUnitValue(avg_unit_value);
129+
qty_ = tot_mass;
122130
mat->qty_ = 0;
123131
tracker_.Absorb(&mat->tracker_);
124132
}
@@ -146,7 +154,8 @@ Resource::Ptr Material::PackageExtract(double qty,
146154
}
147155

148156
qty_ -= qty;
149-
Material::Ptr other(new Material(ctx_, qty, comp_, new_package_name));
157+
Material::Ptr other(
158+
new Material(ctx_, qty, comp_, new_package_name, UnitValue()));
150159

151160
// Decay called on the extracted material should have the same dt as for
152161
// this material regardless of composition.
@@ -263,13 +272,14 @@ Composition::Ptr Material::comp() {
263272
}
264273

265274
Material::Material(Context* ctx, double quantity, Composition::Ptr c,
266-
std::string package_name)
275+
std::string package_name, double unit_value)
267276
: qty_(quantity),
268277
comp_(c),
269278
tracker_(ctx, this),
270279
ctx_(ctx),
271280
prev_decay_time_(0),
272281
package_name_(package_name) {
282+
SetUnitValue(unit_value);
273283
if (ctx != NULL) {
274284
prev_decay_time_ = ctx->time();
275285
} else {

src/material.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ class Material : public Resource {
8282
/// "this" pointer). All future output data recorded will be done using the
8383
/// creator's context.
8484
static Ptr Create(Agent* creator, double quantity, Composition::Ptr c,
85-
std::string package_name = Package::unpackaged_name());
85+
std::string package_name = Package::unpackaged_name(),
86+
double unit_value = kUnsetUnitValue);
8687

8788
/// Creates a new material resource that does not actually exist as part of
8889
/// the simulation and is untracked.
89-
static Ptr CreateUntracked(double quantity, Composition::Ptr c);
90+
static Ptr CreateUntracked(double quantity, Composition::Ptr c,
91+
double unit_value = kUnsetUnitValue);
9092

9193
/// Returns the id of the material's internal nuclide composition.
9294
virtual int qual_id() const;
@@ -168,7 +170,8 @@ class Material : public Resource {
168170

169171
protected:
170172
Material(Context* ctx, double quantity, Composition::Ptr c,
171-
std::string package_name = Package::unpackaged_name());
173+
std::string package_name = Package::unpackaged_name(),
174+
double unit_value = kUnsetUnitValue);
172175

173176
private:
174177
Context* ctx_;

src/product.cc

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ int Product::next_qualid_ = 1;
1313

1414
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1515
Product::Ptr Product::Create(Agent* creator, double quantity,
16-
std::string quality, std::string package_name) {
16+
std::string quality, std::string package_name,
17+
double unit_value) {
1718
if (qualids_.count(quality) == 0) {
1819
qualids_[quality] = next_qualid_++;
1920
creator->context()
@@ -24,15 +25,16 @@ Product::Ptr Product::Create(Agent* creator, double quantity,
2425
}
2526

2627
// the next lines must come after qual id setting
27-
Product::Ptr r(
28-
new Product(creator->context(), quantity, quality, package_name));
28+
Product::Ptr r(new Product(creator->context(), quantity, quality,
29+
package_name, unit_value));
2930
r->tracker_.Create(creator);
3031
return r;
3132
}
3233

3334
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3435
Product::Ptr Product::CreateUntracked(double quantity, std::string quality) {
35-
Product::Ptr r(new Product(NULL, quantity, quality));
36+
Product::Ptr r(new Product(NULL, quantity, quality,
37+
Package::unpackaged_name(), kUnsetUnitValue));
3638
r->tracker_.DontTrack();
3739
return r;
3840
}
@@ -50,7 +52,12 @@ void Product::Absorb(Product::Ptr other) {
5052
if (other->quality() != quality()) {
5153
throw ValueError("incompatible resource types.");
5254
}
53-
quantity_ += other->quantity();
55+
double tot_qty = quantity_ + other->quantity();
56+
double avg_unit_value =
57+
(quantity_ * UnitValue() + other->quantity() * other->UnitValue()) /
58+
tot_qty;
59+
SetUnitValue(avg_unit_value);
60+
quantity_ = tot_qty;
5461
other->quantity_ = 0;
5562

5663
tracker_.Absorb(&other->tracker_);
@@ -64,7 +71,8 @@ Product::Ptr Product::Extract(double quantity) {
6471

6572
quantity_ -= quantity;
6673

67-
Product::Ptr other(new Product(ctx_, quantity, quality_, package_name_));
74+
Product::Ptr other(
75+
new Product(ctx_, quantity, quality_, package_name_, UnitValue()));
6876
tracker_.Extract(&other->tracker_);
6977
return other;
7078
}
@@ -78,14 +86,16 @@ std::string Product::package_name() {
7886
return package_name_;
7987
}
8088

89+
// Not sure what to do here with the unit value and packaging...
8190
Resource::Ptr Product::PackageExtract(double qty,
8291
std::string new_package_name) {
8392
if (qty > quantity_) {
8493
throw ValueError("Attempted to extract more quantity than exists.");
8594
}
8695

8796
quantity_ -= qty;
88-
Product::Ptr other(new Product(ctx_, qty, quality_, new_package_name));
97+
Product::Ptr other(
98+
new Product(ctx_, qty, quality_, new_package_name, UnitValue()));
8999

90100
// this call to res_tracker must come first before the parent resource
91101
// state id gets modified
@@ -119,11 +129,13 @@ void Product::ChangePackage(std::string new_package_name) {
119129

120130
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
121131
Product::Product(Context* ctx, double quantity, std::string quality,
122-
std::string package_name)
132+
std::string package_name, double unit_value)
123133
: quality_(quality),
124134
quantity_(quantity),
125135
tracker_(ctx, this),
126136
ctx_(ctx),
127-
package_name_(package_name) {}
137+
package_name_(package_name) {
138+
SetUnitValue(unit_value);
139+
}
128140

129141
} // namespace cyclus

src/product.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class Product : public Resource {
2828
/// "this" pointer). All future output data recorded will be done using the
2929
/// creator's context.
3030
static Ptr Create(Agent* creator, double quantity, std::string quality,
31-
std::string package_name = Package::unpackaged_name());
31+
std::string package_name = Package::unpackaged_name(),
32+
double unit_value = kUnsetUnitValue);
3233

3334
/// Creates a new product that does not actually exist as part of
3435
/// the simulation and is untracked.
@@ -79,7 +80,8 @@ class Product : public Resource {
7980
/// @param quantity is a double indicating the quantity
8081
/// @param quality the resource quality
8182
Product(Context* ctx, double quantity, std::string quality,
82-
std::string package_name = Package::unpackaged_name());
83+
std::string package_name = Package::unpackaged_name(),
84+
double unit_value = kUnsetUnitValue);
8385

8486
// map<quality, quality_id>
8587
static std::map<std::string, int> qualids_;

src/res_tracker.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ void ResTracker::Record(bool bumpId) {
104104
->AddVal("TimeCreated", ctx_->time())
105105
->AddVal("Quantity", res_->quantity())
106106
->AddVal("Units", res_->units())
107+
->AddVal("UnitValue", res_->UnitValue())
107108
->AddVal("QualId", res_->qual_id())
108109
->AddVal("PackageName", res_->package_name())
109110
->AddVal("Parent1", parent1_)

src/resource.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Resource {
2626
public:
2727
typedef boost::shared_ptr<Resource> Ptr;
2828

29-
Resource() : state_id_(nextstate_id_++), obj_id_(nextobj_id_++) {}
29+
Resource()
30+
: state_id_(nextstate_id_++), unit_value_(0.0), obj_id_(nextobj_id_++) {}
3031

3132
virtual ~Resource() {}
3233

@@ -35,6 +36,12 @@ class Resource {
3536
/// You should NOT track resources by pointer.
3637
const int obj_id() const { return obj_id_; }
3738

39+
/// Returns the unit value of this resource.
40+
double UnitValue() const { return unit_value_; }
41+
42+
/// Sets the unit value of this resource.
43+
void SetUnitValue(double unit_value) { unit_value_ = unit_value; }
44+
3845
/// Returns the unique id corresponding to this resource and its current
3946
/// state. All resource id's are unique - even across different resource
4047
/// types/implementations. Runtime tracking of resources should generally
@@ -108,7 +115,12 @@ class Resource {
108115
/// restrictions, the remainder is left in the resource object.
109116
template <class T> std::vector<typename T::Ptr> Package(Package::Ptr pkg);
110117

118+
protected:
119+
constexpr static double kUnsetUnitValue =
120+
std::numeric_limits<double>::quiet_NaN();
121+
111122
private:
123+
double unit_value_;
112124
static int nextstate_id_;
113125
static int nextobj_id_;
114126
int state_id_;

0 commit comments

Comments
 (0)