@@ -13,7 +13,8 @@ int Product::next_qualid_ = 1;
1313
1414// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1515Product::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// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3435Product::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...
8190Resource::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// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
121131Product::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
0 commit comments