CYCLUS
Loading...
Searching...
No Matches
product.cc
Go to the documentation of this file.
1#include "product.h"
2
3#include "error.h"
4#include "logger.h"
5#include "cyc_limits.h"
6
7namespace cyclus {
8
9const ResourceType Product::kType = "Product";
10
11std::map<std::string, int> Product::qualids_;
12int Product::next_qualid_ = 1;
13
14// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16 std::string quality, std::string package_name) {
17 if (qualids_.count(quality) == 0) {
18 qualids_[quality] = next_qualid_++;
19 creator->context()->NewDatum("Products")
20 ->AddVal("QualId", qualids_[quality])
21 ->AddVal("Quality", quality)
22 ->Record();
23 }
24
25 // the next lines must come after qual id setting
27 r->tracker_.Create(creator);
28 return r;
29}
30
31// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33 std::string quality) {
35 r->tracker_.DontTrack();
36 return r;
37}
38
39// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41 Product* g = new Product(*this);
43 g->tracker_.DontTrack();
44 return c;
45}
46
47// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49 if (other->quality() != quality()) {
50 throw ValueError("incompatible resource types.");
51 }
52 quantity_ += other->quantity();
53 other->quantity_ = 0;
54
55 tracker_.Absorb(&other->tracker_);
56}
57
58// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60 if (quantity > quantity_) {
61 throw ValueError("Attempted to extract more quantity than exists.");
62 }
63
64 quantity_ -= quantity;
65
66 Product::Ptr other(new Product(ctx_, quantity, quality_, package_name_));
67 tracker_.Extract(&other->tracker_);
68 return other;
69}
70
71// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73 return boost::static_pointer_cast<Resource>(Extract(qty));
74}
75
76std::string Product::package_name() {
77 return package_name_;
78}
79
81 if (qty > quantity_) {
82 throw ValueError("Attempted to extract more quantity than exists.");
83 }
84
85 quantity_ -= qty;
86 Product::Ptr other(new Product(ctx_, qty, quality_, new_package_name));
87
88 // this call to res_tracker must come first before the parent resource
89 // state id gets modified
90 other->tracker_.Package(&tracker_);
91 if (quantity_ > cyclus::eps_rsrc()) {
92 tracker_.Modify();
93 }
94 return boost::static_pointer_cast<Resource>(other);
95}
96
98 if (new_package_name == package_name_ || ctx_ == NULL) {
99 // no change needed
100 return;
101 }
103 // unpackaged has functionally no restrictions
104 package_name_ = new_package_name;
105 tracker_.Package();
106 return;
107 }
109 double min = p->fill_min();
110 double max = p->fill_max();
111 if (quantity_ >= min && quantity_ <= max) {
112 package_name_ = new_package_name;
113 tracker_.Package();
114 } else {
115 throw ValueError("Product quantity is outside of package fill limits.");
116 }
117}
118
119// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120Product::Product(Context* ctx, double quantity, std::string quality, std::string package_name)
121 : quality_(quality),
122 quantity_(quantity),
123 tracker_(ctx, this),
124 ctx_(ctx),
125 package_name_(package_name) {}
126
127} // namespace cyclus
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:49
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:145
Package::Ptr GetPackage(std::string name)
Retrieve a registered package.
Definition context.cc:214
boost::shared_ptr< Package > Ptr
Definition package.h:21
static std::string unpackaged_name()
Definition package.h:68
A Product is a general type of resource in the Cyclus simulation, and is a catch-all for non-standard...
Definition product.h:18
virtual Resource::Ptr Clone() const
Returns an untracked (not part of the simulation) copy of the resource.
Definition product.cc:40
virtual double quantity() const
Returns the quantity of this resource with dimensions as specified by the return value of units().
Definition product.h:53
static Ptr Create(Agent *creator, double quantity, std::string quality, std::string package_name=Package::unpackaged_name())
Creates a new product that is "live" and tracked.
Definition product.cc:15
virtual std::string package_name()
Returns the package id.
Definition product.cc:76
boost::shared_ptr< Product > Ptr
Definition product.h:24
virtual const std::string & quality() const
Returns the quality of this resource (e.g. bananas, human labor, water, etc.).
Definition product.h:58
static Ptr CreateUntracked(double quantity, std::string quality)
Creates a new product that does not actually exist as part of the simulation and is untracked.
Definition product.cc:32
virtual Resource::Ptr PackageExtract(double qty, std::string new_package_name=Package::unpackaged_name())
Definition product.cc:80
void Absorb(Product::Ptr other)
Absorbs the contents of the given 'other' resource into this resource.
Definition product.cc:48
static const ResourceType kType
Definition product.h:25
virtual void ChangePackage(std::string new_package_name=Package::unpackaged_name())
Changes the product's package id.
Definition product.cc:97
virtual Resource::Ptr ExtractRes(double quantity)
Splits the resource and returns the extracted portion as a new resource object.
Definition product.cc:72
Product::Ptr Extract(double quantity)
Extracts the specified mass from this resource and returns it as a new product object with the same q...
Definition product.cc:59
void Modify()
Should be called when the state of a resource changes (e.g.
void Extract(ResTracker *removed)
Should be called when a resource has some quantity removed from it (e.g.
void Package(ResTracker *parent=NULL)
Should be called when a resource's package gets modified.
void Absorb(ResTracker *absorbed)
Should be called when a resource is combined with another.
boost::shared_ptr< Resource > Ptr
Definition resource.h:27
For values that are too big, too small, etc.
Definition error.h:41
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
double eps_rsrc()
an epsilon value to be used by resources
Definition cyc_limits.h:19
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
std::string ResourceType
Definition resource.h:17