CYCAMORE
src/storage.cc
Go to the documentation of this file.
1 // storage.cc
2 // Implements the Storage class
3 #include "storage.h"
4 
5 namespace storage {
6 
7 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8 Storage::Storage(cyclus::Context* ctx) : cyclus::Facility(ctx) {
9  cyclus::Warn<cyclus::EXPERIMENTAL_WARNING>(
10  "The Storage Facility is experimental.");
11 };
12 
13 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
14 // pragmas
15 
16 #pragma cyclus def schema storage::Storage
17 
18 #pragma cyclus def annotations storage::Storage
19 
20 #pragma cyclus def initinv storage::Storage
21 
22 #pragma cyclus def snapshotinv storage::Storage
23 
24 #pragma cyclus def infiletodb storage::Storage
25 
26 #pragma cyclus def snapshot storage::Storage
27 
28 #pragma cyclus def clone storage::Storage
29 
30 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31 void Storage::InitFrom(Storage* m) {
32 #pragma cyclus impl initfromcopy storage::Storage
33  cyclus::toolkit::CommodityProducer::Copy(m);
34 }
35 
36 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37 void Storage::InitFrom(cyclus::QueryableBackend* b) {
38 #pragma cyclus impl initfromdb storage::Storage
39 
40  using cyclus::toolkit::Commodity;
41  Commodity commod = Commodity(out_commods.front());
42  cyclus::toolkit::CommodityProducer::Add(commod);
43  cyclus::toolkit::CommodityProducer::SetCapacity(commod, throughput);
44 }
45 
46 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
47 void Storage::EnterNotify() {
48  cyclus::Facility::EnterNotify();
49  buy_policy.Init(this, &inventory, std::string("inventory"));
50 
51  // dummy comp, use in_recipe if provided
52  cyclus::CompMap v;
53  cyclus::Composition::Ptr comp = cyclus::Composition::CreateFromAtom(v);
54  if (in_recipe != "") {
55  comp = context()->GetRecipe(in_recipe);
56  }
57 
58  if (in_commod_prefs.size() == 0) {
59  for (int i = 0; i < in_commods.size(); ++i) {
60  in_commod_prefs.push_back(cyclus::kDefaultPref);
61  }
62  } else if (in_commod_prefs.size() != in_commods.size()) {
63  std::stringstream ss;
64  ss << "in_commod_prefs has " << in_commod_prefs.size()
65  << " values, expected " << in_commods.size();
66  throw cyclus::ValueError(ss.str());
67  }
68 
69  for (int i = 0; i != in_commods.size(); ++i) {
70  buy_policy.Set(in_commods[i], comp, in_commod_prefs[i]);
71  }
72  buy_policy.Start();
73 
74  if (out_commods.size() == 1) {
75  sell_policy.Init(this, &stocks, std::string("stocks"))
76  .Set(out_commods.front())
77  .Start();
78  } else {
79  std::stringstream ss;
80  ss << "out_commods has " << out_commods.size() << " values, expected 1.";
81  throw cyclus::ValueError(ss.str());
82  }
83 }
84 
85 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87  std::stringstream ss;
88  std::string ans, out_str;
89  if (out_commods.size() == 1) {
90  out_str = out_commods.front();
91  } else {
92  out_str = "";
93  }
94  if (cyclus::toolkit::CommodityProducer::Produces(
95  cyclus::toolkit::Commodity(out_str))) {
96  ans = "yes";
97  } else {
98  ans = "no";
99  }
100  ss << cyclus::Facility::str();
101  ss << " has facility parameters {"
102  << "\n"
103  << " Output Commodity = " << out_str << ",\n"
104  << " Residence Time = " << residence_time << ",\n"
105  << " Throughput = " << throughput << ",\n"
106  << " commod producer members: "
107  << " produces " << out_str << "?:" << ans << "'}";
108  return ss.str();
109 }
110 
111 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112 void Storage::Tick() {
113  // Set available capacity for Buy Policy
114  inventory.capacity(current_capacity());
115 
116  LOG(cyclus::LEV_INFO3, "ComCnv") << prototype() << " is ticking {";
117 
118  if (current_capacity() > cyclus::eps_rsrc()) {
119  LOG(cyclus::LEV_INFO4, "ComCnv")
120  << " has capacity for " << current_capacity() << " kg of material.";
121  }
122  LOG(cyclus::LEV_INFO3, "ComCnv") << "}";
123 }
124 
125 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
126 void Storage::Tock() {
127  LOG(cyclus::LEV_INFO3, "ComCnv") << prototype() << " is tocking {";
128 
129  BeginProcessing_(); // place unprocessed inventory into processing
130 
131  if (ready_time() >= 0 || residence_time == 0 && !inventory.empty()) {
132  ReadyMatl_(ready_time()); // place processing into ready
133  }
134 
135  ProcessMat_(throughput); // place ready into stocks
136 
137  LOG(cyclus::LEV_INFO3, "ComCnv") << "}";
138 }
139 
140 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141 void Storage::AddMat_(cyclus::Material::Ptr mat) {
142  LOG(cyclus::LEV_INFO5, "ComCnv") << prototype() << " is initially holding "
143  << inventory.quantity() << " total.";
144 
145  try {
146  inventory.Push(mat);
147  } catch (cyclus::Error& e) {
148  e.msg(Agent::InformErrorMsg(e.msg()));
149  throw e;
150  }
151 
152  LOG(cyclus::LEV_INFO5, "ComCnv")
153  << prototype() << " added " << mat->quantity()
154  << " of material to its inventory, which is holding "
155  << inventory.quantity() << " total.";
156 }
157 
158 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
160  while (inventory.count() > 0) {
161  try {
162  processing.Push(inventory.Pop());
163  entry_times.push_back(context()->time());
164 
165  LOG(cyclus::LEV_DEBUG2, "ComCnv")
166  << "Storage " << prototype()
167  << " added resources to processing at t= " << context()->time();
168  } catch (cyclus::Error& e) {
169  e.msg(Agent::InformErrorMsg(e.msg()));
170  throw e;
171  }
172  }
173 }
174 
175 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
176 void Storage::ProcessMat_(double cap) {
177  using cyclus::Material;
178  using cyclus::ResCast;
179  using cyclus::toolkit::ResBuf;
180  using cyclus::toolkit::Manifest;
181 
182  if (!ready.empty()) {
183  try {
184  double max_pop = std::min(cap, ready.quantity());
185 
186  if (discrete_handling) {
187  if (max_pop == ready.quantity()) {
188  stocks.Push(ready.PopN(ready.count()));
189  } else {
190  double cap_pop = ready.Peek()->quantity();
191  while (cap_pop <= max_pop && !ready.empty()) {
192  stocks.Push(ready.Pop());
193  cap_pop += ready.empty() ? 0 : ready.Peek()->quantity();
194  }
195  }
196  } else {
197  stocks.Push(ready.Pop(max_pop, cyclus::eps_rsrc()));
198  }
199 
200  LOG(cyclus::LEV_INFO1, "ComCnv") << "Storage " << prototype()
201  << " moved resources"
202  << " from ready to stocks"
203  << " at t= " << context()->time();
204  } catch (cyclus::Error& e) {
205  e.msg(Agent::InformErrorMsg(e.msg()));
206  throw e;
207  }
208  }
209 }
210 
211 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
212 void Storage::ReadyMatl_(int time) {
213  using cyclus::toolkit::ResBuf;
214 
215  int to_ready = 0;
216 
217  while (!entry_times.empty() && entry_times.front() <= time) {
218  entry_times.pop_front();
219  ++to_ready;
220  }
221 
222  ready.Push(processing.PopN(to_ready));
223 }
224 
225 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
226 extern "C" cyclus::Agent* ConstructStorage(cyclus::Context* ctx) {
227  return new Storage(ctx);
228 }
229 
230 } // namespace storage
void AddMat_(cyclus::Material::Ptr mat)
adds a material into the incoming commodity inventory
virtual std::string str()
A verbose printer for the Storage Facility.
cyclus::toolkit::ResBuf< cyclus::Material > ready
virtual void Tock()
The handleTick function specific to the Storage.
virtual void EnterNotify()
Sets up the Storage Facility&#39;s trade requests.
cyclus::toolkit::MatlBuyPolicy buy_policy
virtual void Tick()
The handleTick function specific to the Storage.
std::vector< double > in_commod_prefs
cycamore::GrowthRegion string
int ready_time()
returns the time key for ready materials
virtual void InitFrom(storage::Storage *m)
cyclus::toolkit::ResBuf< cyclus::Material > stocks
cyclus::toolkit::MatlSellPolicy sell_policy
cyclus::toolkit::ResBuf< cyclus::Material > processing
cyclus::toolkit::ResBuf< cyclus::Material > inventory
cyclus::Agent * ConstructStorage(cyclus::Context *ctx)
std::vector< std::string > in_commods
void ReadyMatl_(int time)
move ready resources from processing to ready at a certain time
double current_capacity() const
current maximum amount that can be added to processing
void BeginProcessing_()
Move all unprocessed inventory to processing.
std::vector< std::string > out_commods
void ProcessMat_(double cap)
Move as many ready resources as allowable into stocks.
Storage(cyclus::Context *ctx)