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 double unit_value) {
18 if (qualids_.count(quality) == 0) {
19 qualids_[quality] = next_qualid_++;
20 creator->context()
21 ->NewDatum("Products")
22 ->AddVal("QualId", qualids_[quality])
23 ->AddVal("Quality", quality)
24 ->Record();
25 }
26
27 // the next lines must come after qual id setting
28 Product::Ptr r(new Product(creator->context(), quantity, quality,
29 package_name, unit_value));
30 r->tracker_.Create(creator);
31 return r;
32}
33
34// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36 Product::Ptr r(new Product(NULL, quantity, quality,
38 r->tracker_.DontTrack();
39 return r;
40}
41
42// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44 Product* g = new Product(*this);
46 g->tracker_.DontTrack();
47 return c;
48}
49
50// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52 if (other->quality() != quality()) {
53 throw ValueError("incompatible resource types.");
54 }
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;
61 other->quantity_ = 0;
62
63 tracker_.Absorb(&other->tracker_);
64}
65
66// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68 if (quantity > quantity_) {
69 throw ValueError("Attempted to extract more quantity than exists.");
70 }
71
72 quantity_ -= quantity;
73
74 Product::Ptr other(
75 new Product(ctx_, quantity, quality_, package_name_, UnitValue()));
76 tracker_.Extract(&other->tracker_);
77 return other;
78}
79
80// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82 return boost::static_pointer_cast<Resource>(Extract(qty));
83}
84
85std::string Product::package_name() {
86 return package_name_;
87}
88
89// Not sure what to do here with the unit value and packaging...
91 std::string new_package_name) {
92 if (qty > quantity_) {
93 throw ValueError("Attempted to extract more quantity than exists.");
94 }
95
96 quantity_ -= qty;
97 Product::Ptr other(
98 new Product(ctx_, qty, quality_, new_package_name, UnitValue()));
99
100 // this call to res_tracker must come first before the parent resource
101 // state id gets modified
102 other->tracker_.Package(&tracker_);
103 if (quantity_ > cyclus::eps_rsrc()) {
104 tracker_.Modify();
105 }
106 return boost::static_pointer_cast<Resource>(other);
107}
108
109void Product::ChangePackage(std::string new_package_name) {
110 if (new_package_name == package_name_ || ctx_ == NULL) {
111 // no change needed
112 return;
113 } else if (new_package_name == Package::unpackaged_name()) {
114 // unpackaged has functionally no restrictions
115 package_name_ = new_package_name;
116 tracker_.Package();
117 return;
118 }
119 Package::Ptr p = ctx_->GetPackage(new_package_name);
120 double min = p->fill_min();
121 double max = p->fill_max();
122 if (quantity_ >= min && quantity_ <= max) {
123 package_name_ = new_package_name;
124 tracker_.Package();
125 } else {
126 throw ValueError("Product quantity is outside of package fill limits.");
127 }
128}
129
130// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131Product::Product(Context* ctx, double quantity, std::string quality,
132 std::string package_name, double unit_value)
133 : quality_(quality),
134 quantity_(quantity),
135 tracker_(ctx, this),
136 ctx_(ctx),
137 package_name_(package_name) {
138 SetUnitValue(unit_value);
139}
140
141} // namespace cyclus
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:50
Context * context() const
Returns this agent's simulation context.
Definition agent.h:364
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:146
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition context.cc:351
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
Definition datum.cc:21
void Record()
Record this datum to its Recorder.
Definition datum.cc:34
boost::shared_ptr< Package > Ptr
Definition package.h:21
static std::string unpackaged_name()
Definition package.h:68
virtual Resource::Ptr Clone() const
Returns an untracked (not part of the simulation) copy of the resource.
Definition product.cc:43
virtual double quantity() const
Returns the quantity of this resource with dimensions as specified by the return value of units().
Definition product.h:50
virtual std::string package_name()
Returns the package id.
Definition product.cc:85
boost::shared_ptr< Product > Ptr
Definition product.h:23
virtual const std::string & quality() const
Returns the quality of this resource (e.g.
Definition product.h:54
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:35
virtual Resource::Ptr PackageExtract(double qty, std::string new_package_name=Package::unpackaged_name())
Definition product.cc:90
void Absorb(Product::Ptr other)
Absorbs the contents of the given 'other' resource into this resource.
Definition product.cc:51
static const ResourceType kType
Definition product.h:24
static Ptr Create(Agent *creator, double quantity, std::string quality, std::string package_name=Package::unpackaged_name(), double unit_value=kUnsetUnitValue)
Creates a new product that is "live" and tracked.
Definition product.cc:15
virtual void ChangePackage(std::string new_package_name=Package::unpackaged_name())
Changes the product's package id.
Definition product.cc:109
virtual Resource::Ptr ExtractRes(double quantity)
Splits the resource and returns the extracted portion as a new resource object.
Definition product.cc:81
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:67
double UnitValue() const
Returns the unit value of this resource.
Definition resource.h:40
static constexpr double kUnsetUnitValue
Definition resource.h:119
boost::shared_ptr< Resource > Ptr
Definition resource.h:27
void SetUnitValue(double unit_value)
Sets the unit value of this resource.
Definition resource.h:43
For values that are too big, too small, etc.
Definition error.h:37
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
std::string ResourceType
Definition resource.h:17