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  SetMaxInventorySize(std::numeric_limits<double>::max());
16 }
17 
18 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
19 Sink::~Sink() {}
20 
21 #pragma cyclus def schema cycamore::Sink
22 
23 #pragma cyclus def annotations cycamore::Sink
24 
25 #pragma cyclus def infiletodb cycamore::Sink
26 
27 #pragma cyclus def snapshot cycamore::Sink
28 
29 #pragma cyclus def snapshotinv cycamore::Sink
30 
31 #pragma cyclus def initinv cycamore::Sink
32 
33 #pragma cyclus def clone cycamore::Sink
34 
35 #pragma cyclus def initfromdb cycamore::Sink
36 
37 #pragma cyclus def initfromcopy cycamore::Sink
38 
39 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40 void Sink::EnterNotify() {
41  cyclus::Facility::EnterNotify();
42 
43  if (in_commod_prefs.size() == 0) {
44  for (int i = 0; i < in_commods.size(); ++i) {
45  in_commod_prefs.push_back(cyclus::kDefaultPref);
46  }
47  } else if (in_commod_prefs.size() != in_commods.size()) {
48  std::stringstream ss;
49  ss << "in_commod_prefs has " << in_commod_prefs.size()
50  << " values, expected " << in_commods.size();
51  throw cyclus::ValueError(ss.str());
52  }
53 
54 }
55 
56 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58  using std::string;
59  using std::vector;
60  std::stringstream ss;
61  ss << cyclus::Facility::str();
62 
63  string msg = "";
64  msg += "accepts commodities ";
65  for (vector<string>::iterator commod = in_commods.begin();
66  commod != in_commods.end();
67  commod++) {
68  msg += (commod == in_commods.begin() ? "{" : ", ");
69  msg += (*commod);
70  }
71  msg += "} until its inventory is full at ";
72  ss << msg << inventory.capacity() << " kg.";
73  return "" + ss.str();
74 }
75 
76 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77 std::set<cyclus::RequestPortfolio<cyclus::Material>::Ptr>
79  using cyclus::Material;
80  using cyclus::RequestPortfolio;
81  using cyclus::Request;
82  using cyclus::Composition;
83 
84  std::set<RequestPortfolio<Material>::Ptr> ports;
85  RequestPortfolio<Material>::Ptr port(new RequestPortfolio<Material>());
86  double amt = RequestAmt();
87  Material::Ptr mat;
88 
89  if (recipe_name.empty()) {
90  mat = cyclus::NewBlankMaterial(amt);
91  } else {
92  Composition::Ptr rec = this->context()->GetRecipe(recipe_name);
93  mat = cyclus::Material::CreateUntracked(amt, rec);
94  }
95 
96  if (amt > cyclus::eps()) {
97  std::vector<Request<Material>*> mutuals;
98  for (int i = 0; i < in_commods.size(); i++) {
99  mutuals.push_back(port->AddRequest(mat, this, in_commods[i], in_commod_prefs[i]));
100  }
101  port->AddMutualReqs(mutuals);
102  ports.insert(port);
103  } // if amt > eps
104 
105  return ports;
106 }
107 
108 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
109 std::set<cyclus::RequestPortfolio<cyclus::Product>::Ptr>
111  using cyclus::CapacityConstraint;
112  using cyclus::Product;
113  using cyclus::RequestPortfolio;
114  using cyclus::Request;
115 
116  std::set<RequestPortfolio<Product>::Ptr> ports;
117  RequestPortfolio<Product>::Ptr
118  port(new RequestPortfolio<Product>());
119  double amt = RequestAmt();
120 
121  if (amt > cyclus::eps()) {
122  CapacityConstraint<Product> cc(amt);
123  port->AddConstraint(cc);
124 
125  std::vector<std::string>::const_iterator it;
126  for (it = in_commods.begin(); it != in_commods.end(); ++it) {
127  std::string quality = ""; // not clear what this should be..
128  Product::Ptr rsrc = Product::CreateUntracked(amt, quality);
129  port->AddRequest(rsrc, this, *it);
130  }
131 
132  ports.insert(port);
133  } // if amt > eps
134 
135  return ports;
136 }
137 
138 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
140  const std::vector< std::pair<cyclus::Trade<cyclus::Material>,
141  cyclus::Material::Ptr> >& responses) {
142  std::vector< std::pair<cyclus::Trade<cyclus::Material>,
143  cyclus::Material::Ptr> >::const_iterator it;
144  for (it = responses.begin(); it != responses.end(); ++it) {
145  inventory.Push(it->second);
146  }
147 }
148 
149 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151  const std::vector< std::pair<cyclus::Trade<cyclus::Product>,
152  cyclus::Product::Ptr> >& responses) {
153  std::vector< std::pair<cyclus::Trade<cyclus::Product>,
154  cyclus::Product::Ptr> >::const_iterator it;
155  for (it = responses.begin(); it != responses.end(); ++it) {
156  inventory.Push(it->second);
157  }
158 }
159 
160 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161 void Sink::Tick() {
162  using std::string;
163  using std::vector;
164  LOG(cyclus::LEV_INFO3, "SnkFac") << prototype() << " is ticking {";
165 
166  double requestAmt = RequestAmt();
167  // inform the simulation about what the sink facility will be requesting
168  if (requestAmt > cyclus::eps()) {
169  for (vector<string>::iterator commod = in_commods.begin();
170  commod != in_commods.end();
171  commod++) {
172  LOG(cyclus::LEV_INFO4, "SnkFac") << " will request " << requestAmt
173  << " kg of " << *commod << ".";
174  }
175  }
176  LOG(cyclus::LEV_INFO3, "SnkFac") << "}";
177 }
178 
179 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180 void Sink::Tock() {
181  LOG(cyclus::LEV_INFO3, "SnkFac") << prototype() << " is tocking {";
182 
183  // On the tock, the sink facility doesn't really do much.
184  // Maybe someday it will record things.
185  // For now, lets just print out what we have at each timestep.
186  LOG(cyclus::LEV_INFO4, "SnkFac") << "Sink " << this->id()
187  << " is holding " << inventory.quantity()
188  << " units of material at the close of month "
189  << context()->time() << ".";
190  LOG(cyclus::LEV_INFO3, "SnkFac") << "}";
191 }
192 
193 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
194 extern "C" cyclus::Agent* ConstructSink(cyclus::Context* ctx) {
195  return new Sink(ctx);
196 }
197 
198 } // 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)
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 RequestAmt() const
determines the amount to request
home mouginot work app cyclus cycamore _tmp_build cycamore reactor cc
cycamore::GrowthRegion string
virtual std::set< cyclus::RequestPortfolio< cyclus::Product >::Ptr > GetGenRsrcRequests()
SinkFacilities request Products of their given commodity.
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()