CYCAMORE
src/sink.cc
Go to the documentation of this file.
1 // Implements the Sink class
2 #include <algorithm>
3 #include <sstream>
4 
5 #include <boost/lexical_cast.hpp>
6 
7 #include "sink.h"
8 
9 namespace cycamore {
10 
11 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
12 Sink::Sink(cyclus::Context* ctx)
13  : cyclus::Facility(ctx),
14  capacity(std::numeric_limits<double>::max()),
15  latitude(0.0),
16  longitude(0.0),
18  SetMaxInventorySize(std::numeric_limits<double>::max());}
19 
20 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
21 Sink::~Sink() {}
22 
23 #pragma cyclus def schema cycamore::Sink
24 
25 #pragma cyclus def annotations cycamore::Sink
26 
27 #pragma cyclus def infiletodb cycamore::Sink
28 
29 #pragma cyclus def snapshot cycamore::Sink
30 
31 #pragma cyclus def snapshotinv cycamore::Sink
32 
33 #pragma cyclus def initinv cycamore::Sink
34 
35 #pragma cyclus def clone cycamore::Sink
36 
37 #pragma cyclus def initfromdb cycamore::Sink
38 
39 #pragma cyclus def initfromcopy cycamore::Sink
40 
41 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42 void Sink::EnterNotify() {
43  cyclus::Facility::EnterNotify();
44 
45  if (in_commod_prefs.size() == 0) {
46  for (int i = 0; i < in_commods.size(); ++i) {
47  in_commod_prefs.push_back(cyclus::kDefaultPref);
48  }
49  } else if (in_commod_prefs.size() != in_commods.size()) {
50  std::stringstream ss;
51  ss << "in_commod_prefs has " << in_commod_prefs.size()
52  << " values, expected " << in_commods.size();
53  throw cyclus::ValueError(ss.str());
54  }
56 }
57 
58 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60  using std::string;
61  using std::vector;
62  std::stringstream ss;
63  ss << cyclus::Facility::str();
64 
65  string msg = "";
66  msg += "accepts commodities ";
67  for (vector<string>::iterator commod = in_commods.begin();
68  commod != in_commods.end();
69  commod++) {
70  msg += (commod == in_commods.begin() ? "{" : ", ");
71  msg += (*commod);
72  }
73  msg += "} until its inventory is full at ";
74  ss << msg << inventory.capacity() << " kg.";
75  return "" + ss.str();
76 }
77 
78 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79 std::set<cyclus::RequestPortfolio<cyclus::Material>::Ptr>
81  using cyclus::Material;
82  using cyclus::RequestPortfolio;
83  using cyclus::Request;
84  using cyclus::Composition;
85 
86  std::set<RequestPortfolio<Material>::Ptr> ports;
87  RequestPortfolio<Material>::Ptr port(new RequestPortfolio<Material>());
88  double amt = RequestAmt();
89  Material::Ptr mat;
90 
91  if (recipe_name.empty()) {
92  mat = cyclus::NewBlankMaterial(amt);
93  } else {
94  Composition::Ptr rec = this->context()->GetRecipe(recipe_name);
95  mat = cyclus::Material::CreateUntracked(amt, rec);
96  }
97 
98  if (amt > cyclus::eps()) {
99  std::vector<Request<Material>*> mutuals;
100  for (int i = 0; i < in_commods.size(); i++) {
101  mutuals.push_back(port->AddRequest(mat, this, in_commods[i], in_commod_prefs[i]));
102 
103  }
104  port->AddMutualReqs(mutuals);
105  ports.insert(port);
106  } // if amt > eps
107  return ports;
108 }
109 
110 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111 std::set<cyclus::RequestPortfolio<cyclus::Product>::Ptr>
113  using cyclus::CapacityConstraint;
114  using cyclus::Product;
115  using cyclus::RequestPortfolio;
116  using cyclus::Request;
117 
118  std::set<RequestPortfolio<Product>::Ptr> ports;
119  RequestPortfolio<Product>::Ptr
120  port(new RequestPortfolio<Product>());
121  double amt = RequestAmt();
122 
123  if (amt > cyclus::eps()) {
124  CapacityConstraint<Product> cc(amt);
125  port->AddConstraint(cc);
126 
127  std::vector<std::string>::const_iterator it;
128  for (it = in_commods.begin(); it != in_commods.end(); ++it) {
129  std::string quality = ""; // not clear what this should be..
130  Product::Ptr rsrc = Product::CreateUntracked(amt, quality);
131  port->AddRequest(rsrc, this, *it);
132  }
133 
134  ports.insert(port);
135  } // if amt > eps
136  return ports;
137 }
138 
139 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141  const std::vector< std::pair<cyclus::Trade<cyclus::Material>,
142  cyclus::Material::Ptr> >& responses) {
143  std::vector< std::pair<cyclus::Trade<cyclus::Material>,
144  cyclus::Material::Ptr> >::const_iterator it;
145  for (it = responses.begin(); it != responses.end(); ++it) {
146  inventory.Push(it->second);
147  }
148 }
149 
150 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152  const std::vector< std::pair<cyclus::Trade<cyclus::Product>,
153  cyclus::Product::Ptr> >& responses) {
154  std::vector< std::pair<cyclus::Trade<cyclus::Product>,
155  cyclus::Product::Ptr> >::const_iterator it;
156  for (it = responses.begin(); it != responses.end(); ++it) {
157  inventory.Push(it->second);
158  }
159 }
160 
161 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
162 void Sink::Tick() {
163  using std::string;
164  using std::vector;
165  LOG(cyclus::LEV_INFO3, "SnkFac") << prototype() << " is ticking {";
166 
167  double requestAmt = RequestAmt();
168  // inform the simulation about what the sink facility will be requesting
169  if (requestAmt > cyclus::eps()) {
170  for (vector<string>::iterator commod = in_commods.begin();
171  commod != in_commods.end();
172  commod++) {
173  LOG(cyclus::LEV_INFO4, "SnkFac") << " will request " << requestAmt
174  << " kg of " << *commod << ".";
175  cyclus::toolkit::RecordTimeSeries<double>("demand"+*commod, this,
176  requestAmt);
177  }
178  }
179  LOG(cyclus::LEV_INFO3, "SnkFac") << "}";
180 }
181 
182 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
183 void Sink::Tock() {
184  LOG(cyclus::LEV_INFO3, "SnkFac") << prototype() << " is tocking {";
185 
186  // On the tock, the sink facility doesn't really do much.
187  // Maybe someday it will record things.
188  // For now, lets just print out what we have at each timestep.
189  LOG(cyclus::LEV_INFO4, "SnkFac") << "Sink " << this->id()
190  << " is holding " << inventory.quantity()
191  << " units of material at the close of month "
192  << context()->time() << ".";
193  LOG(cyclus::LEV_INFO3, "SnkFac") << "}";
194 }
195 
196 void Sink::RecordPosition() {
197  std::string specification = this->spec();
198  context()
199  ->NewDatum("AgentPosition")
200  ->AddVal("Spec", specification)
201  ->AddVal("Prototype", this->prototype())
202  ->AddVal("AgentId", id())
203  ->AddVal("Latitude", latitude)
204  ->AddVal("Longitude", longitude)
205  ->Record();
206 }
207 
208 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
209 extern "C" cyclus::Agent* ConstructSink(cyclus::Context* ctx) {
210  return new Sink(ctx);
211 }
212 
213 } // namespace cycamore
virtual std::set< cyclus::RequestPortfolio< cyclus::Material >::Ptr > GetMatlRequests()
SinkFacilities request Materials of their given commodity.
cyclus::toolkit::ResBuf< cyclus::Resource > inventory
this facility holds material in storage.
cyclus::Agent * ConstructSink(cyclus::Context *ctx)
double RequestAmt() const
determines the amount to request
Sink(cyclus::Context *ctx)
virtual void AcceptGenRsrcTrades(const std::vector< std::pair< cyclus::Trade< cyclus::Product >, cyclus::Product::Ptr > > &responses)
SinkFacilities place accepted trade Materials in their Inventory.
double longitude
cycamore::GrowthRegion string
virtual std::set< cyclus::RequestPortfolio< cyclus::Product >::Ptr > GetGenRsrcRequests()
SinkFacilities request Products of their given commodity.
cyclus::toolkit::Position coordinates
std::vector< double > in_commod_prefs
virtual void AcceptMatlTrades(const std::vector< std::pair< cyclus::Trade< cyclus::Material >, cyclus::Material::Ptr > > &responses)
SinkFacilities place accepted trade Materials in their Inventory.
std::vector< std::string > in_commods
all facilities must have at least one input commodity
virtual std::string str()