commodconverter
 All Classes Namespaces Files Functions Variables Macros
src/commodconverter.h
Go to the documentation of this file.
1 #ifndef CYCLUS_COMMODCONVERTERS_COMMODCONVERTER_H_
2 #define CYCLUS_COMMODCONVERTERS_COMMODCONVERTER_H_
3 
4 #include <string>
5 
6 #include "cyclus.h"
7 
8 // forward declaration
9 namespace commodconverter {
10 class CommodConverter;
11 } // namespace commodconverter
12 
13 
14 namespace commodconverter {
15 /// @class CommodConverter
16 ///
17 /// This Facility is intended to convert a resource from one commodity to
18 /// another. It also has an optional delay parameter. It can therefore be used
19 /// quite easily as a storage facility.
20 /// The CommodConverter class inherits from the Facility class and is
21 /// dynamically loaded by the Agent class when requested.
22 ///
23 /// @section intro Introduction
24 /// This Agent was initially developed to support the fco code-to-code
25 /// comparsion.
26 /// It's very similar to the "NullFacility" of years
27 /// past. Its purpose is to convert a commodity from one commodity to another
28 /// after some period of delay time. This facility is very good for use as a
29 /// storage facility or fuel fabrication.
30 ///
31 /// @section agentparams Agent Parameters
32 /// in_commod is a string naming the commodity that this facility recieves
33 /// out_commod is a string naming the commodity that in_commod is stocks into
34 /// process_time is the number of timesteps between receiving and offering
35 /// in_recipe (optional) describes the incoming resource by recipe
36 /// out_recipe (optional) describes the outgoing resource by recipe
37 ///
38 /// @section optionalparams Optional Parameters
39 /// max_inv_size is the maximum capacity of the inventory storage
40 /// capacity is the maximum processing capacity per timestep
41 ///
42 /// @section detailed Detailed Behavior
43 ///
44 /// Tick:
45 /// Nothing really happens on the tick.
46 ///
47 /// Tock:
48 /// On the tock, any material that has been waiting for long enough (delay
49 /// time) is stocks and placed in the stocks buffer.
50 ///
51 /// Any brand new inventory that was received in this timestep is placed into
52 /// the processing queue to begin waiting.
53 ///
54 /// Making Requests:
55 /// This facility requests all of the in_commod that it can.
56 ///
57 /// Receiving Resources:
58 /// Anything of the in_commod that is received by this facility goes into the
59 /// inventory.
60 ///
61 /// Making Offers:
62 /// Any stocks material in the stocks buffer is offered to the market.
63 ///
64 /// Sending Resources:
65 /// Matched resources are sent immediately.
66 class CommodConverter
67  : public cyclus::Facility,
68  public cyclus::toolkit::CommodityProducer {
69  public:
70  /// Constructor for CommodConverter Class
71  /// @param ctx the cyclus context for access to simulation-wide parameters
72  CommodConverter(cyclus::Context* ctx);
73 
74  /// The Prime Directive
75  /// Generates code that handles all input file reading and restart operations
76  /// (e.g., reading from the database, instantiating a new object, etc.).
77  /// @warning The Prime Directive must have a space before it! (A fix will be
78  /// in 2.0 ^TM)
79 
80  #pragma cyclus decl
81 
82  #pragma cyclus note {"doc": "A commodconverter facility converts from one " \
83  "commodity to another, with an optional delay."}
84 
85  /// A verbose printer for the CommodConverter
86  virtual std::string str();
87 
88  // --- Facility Members ---
89 
90  // --- Agent Members ---
91  virtual void EnterNotify();
92 
93  /// The handleTick function specific to the CommodConverter.
94  /// @param time the time of the tick
95  virtual void Tick();
96 
97  /// The handleTick function specific to the CommodConverter.
98  /// @param time the time of the tock
99  virtual void Tock();
100 
101  /// @brief The CommodConverter request Materials of its given
102  /// commodity.
103  virtual std::set<cyclus::RequestPortfolio<cyclus::Material>::Ptr> GetMatlRequests();
104 
105  /// @brief The CommodConverter place accepted trade Materials in their
106  /// Inventory
107  virtual void AcceptMatlTrades(
108  const std::vector< std::pair<cyclus::Trade<cyclus::Material>,
109  cyclus::Material::Ptr> >& responses);
110 
111  /// @brief Responds to each request for this facility's commodity. If a given
112  /// request is more than this facility's inventory capacity, it will
113  /// offer its minimum of its capacities.
114  virtual std::set<cyclus::BidPortfolio<cyclus::Material>::Ptr>
115  GetMatlBids(cyclus::CommodMap<cyclus::Material>::type&
116  commod_requests);
117 
118  /// @brief respond to each trade with a material of out_commod and out_recipe
119  ///
120  /// @param trades all trades in which this trader is the supplier
121  /// @param responses a container to populate with responses to each trade
122  virtual void GetMatlTrades(
123  const std::vector< cyclus::Trade<cyclus::Material> >& trades,
124  std::vector<std::pair<cyclus::Trade<cyclus::Material>,
125  cyclus::Material::Ptr> >& responses);
126 
127  /* --- */
128 
129  /* --- CommodConverter Members --- */
130 
131  /* --- */
132 
133  protected:
134  /// @brief adds a material into the incoming commodity inventory
135  /// @param mat the material to add to the incoming inventory.
136  /// @throws if there is trouble with pushing to the inventory buffer.
137  void AddMat_(cyclus::Material::Ptr mat);
138 
139  /// @brief generates a request for this facility given its current state. The
140  /// quantity of the material will be equal to the remaining inventory size.
141  /// @return a material that this facility will request
142  cyclus::Material::Ptr Request_();
143 
144  /// @brief gathers information about bids
145  /// @param commod_requests the materials that have been requested
146  cyclus::BidPortfolio<cyclus::Material>::Ptr GetBids_(
147  cyclus::CommodMap<cyclus::Material>::type& commod_requests,
148  std::string commod,
149  cyclus::toolkit::ResourceBuff* buffer);
150 
151  /// @brief suggests, based on the buffer, a material response to an offer
152  cyclus::Material::Ptr TradeResponse_(
153  double qty,
154  cyclus::toolkit::ResourceBuff* buffer);
155 
156  /// @brief Move all unprocessed inventory to processing
157  void BeginProcessing_();
158 
159  /// @brief Convert one ready resource in processing
160  void Convert_();
161 
162  /// @brief this facility's commodity-recipe context
163  inline void crctx(const cyclus::toolkit::CommodityRecipeContext& crctx) {
164  crctx_ = crctx;
165  }
166 
167  inline cyclus::toolkit::CommodityRecipeContext crctx() const {
168  return crctx_;
169  }
170 
171  /// @brief returns the time key for ready materials
172  int ready(){ return context()->time() - process_time ; }
173 
174  /* --- Module Members --- */
175 
176  #pragma cyclus var {"tooltip":"input commodity",\
177  "doc":"commodity accepted by this facility"}
178  std::string in_commod;
179 
180  #pragma cyclus var {"tooltip":"output commodity",\
181  "doc":"commodity produced by this facility"}
182  std::string out_commod;
183 
184  #pragma cyclus var {"default":"",\
185  "tooltip":"input recipe",\
186  "doc":"recipe accepted by this facility"}
187  std::string in_recipe;
188 
189  #pragma cyclus var {"default":"",\
190  "tooltip":"output recipe",\
191  "doc":"recipe to be generated by this facility"}
192  std::string out_recipe;
193 
194  #pragma cyclus var {"default": 0,\
195  "tooltip":"process time (timesteps)",\
196  "doc":"the time it takes to convert a received commodity (timesteps)."}
197  int process_time;
198 
199  #pragma cyclus var {"default": 1e299,\
200  "tooltip":"maximum inventory size (kg)",\
201  "doc":"the amount of material that can be in storage at "\
202  "one time (kg)."}
203  double max_inv_size;
204 
205  #pragma cyclus var{"default": 1e299,\
206  "tooltip":"capacity per timestep (kg)",\
207  "doc":"the maximumamount that can be processed per timestep (kg)"}
208  double capacity;
209 
210  #pragma cyclus var{"default": 0,\
211  "tooltip":"cost per kg of production",\
212  "doc":"cost per kg of produced out_commod"}
213  double cost;
214 
215  cyclus::toolkit::ResourceBuff inventory;
216  cyclus::toolkit::ResourceBuff stocks;
217 
218  /// @brief map from ready time to resource buffers
219  std::map<int, cyclus::toolkit::ResourceBuff> processing;
220 
221  cyclus::toolkit::CommodityRecipeContext crctx_;
222 
223  /// @brief the processing time required for a full process
224  inline void process_time_(int t) { process_time = t; }
225  inline int process_time_() const { return process_time; }
226 
227  /// @brief the maximum amount allowed in inventory
228  inline void max_inv_size_(double c) { max_inv_size = c; }
229  inline double max_inv_size_() const { return max_inv_size; }
230 
231  /// @brief the maximum amount processed per timestep
232  inline void capacity_(double c) { capacity = c; }
233  inline double capacity_() const { return capacity; }
234 
235  /// @brief the cost per unit out_commod
236  inline void cost_(double c) { cost = c; }
237  inline double cost_() const { return cost; }
238 
239  /// @brief the in commodity
240  inline void in_commod_(std::string c) { in_commod = c; }
241  inline std::string in_commod_() const { return in_commod; }
242 
243  /// @brief the out commodity
244  inline void out_commod_(std::string c) { out_commod = c; }
245  inline std::string out_commod_() const { return out_commod; }
246 
247  /// @brief the in recipe
248  inline void in_recipe_(std::string c) { in_recipe = c; }
249  inline std::string in_recipe_() const { return in_recipe; }
250 
251  /// @brief the out recipe
252  inline void out_recipe_(std::string c) { out_recipe = c; }
253  inline std::string out_recipe_() const { return out_recipe; }
254 
255  /// @brief current maximum amount that can be added to processing
256  inline double current_capacity() const {
257  return std::min(capacity, max_inv_size - inventory.quantity()); }
258 
259  friend class CommodConverterTest;
260 };
261 
262 } // namespace commodconverter
263 
264 #endif // CYCLUS_COMMODCONVERTERS_COMMODCONVERTER_H_
double current_capacity() const
current maximum amount that can be added to processing
void capacity_(double c)
the maximum amount processed per timestep
cyclus::BidPortfolio< cyclus::Material >::Ptr GetBids_(cyclus::CommodMap< cyclus::Material >::type &commod_requests, std::string commod, cyclus::toolkit::ResourceBuff *buffer)
gathers information about bids
virtual void GetMatlTrades(const std::vector< cyclus::Trade< cyclus::Material > > &trades, std::vector< std::pair< cyclus::Trade< cyclus::Material >, cyclus::Material::Ptr > > &responses)
respond to each trade with a material of out_commod and out_recipe
cyclus::toolkit::CommodityRecipeContext crctx_
cyclus::Material::Ptr TradeResponse_(double qty, cyclus::toolkit::ResourceBuff *buffer)
suggests, based on the buffer, a material response to an offer
virtual std::set< cyclus::RequestPortfolio< cyclus::Material >::Ptr > GetMatlRequests()
The CommodConverter request Materials of its given commodity.
void out_recipe_(std::string c)
the out recipe
void Convert_()
Convert one ready resource in processing.
void in_recipe_(std::string c)
the in recipe
void in_commod_(std::string c)
the in commodity
virtual void AcceptMatlTrades(const std::vector< std::pair< cyclus::Trade< cyclus::Material >, cyclus::Material::Ptr > > &responses)
The CommodConverter place accepted trade Materials in their Inventory.
cyclus::toolkit::CommodityRecipeContext crctx() const
virtual std::set< cyclus::BidPortfolio< cyclus::Material >::Ptr > GetMatlBids(cyclus::CommodMap< cyclus::Material >::type &commod_requests)
Responds to each request for this facility's commodity.
void BeginProcessing_()
Move all unprocessed inventory to processing.
virtual std::string str()
A verbose printer for the CommodConverter.
virtual void Tock()
The handleTick function specific to the CommodConverter.
void cost_(double c)
the cost per unit out_commod
void process_time_(int t)
the processing time required for a full process
void AddMat_(cyclus::Material::Ptr mat)
adds a material into the incoming commodity inventory
int ready()
returns the time key for ready materials
cyclus::Material::Ptr Request_()
generates a request for this facility given its current state.
CommodConverter(cyclus::Context *ctx)
Constructor for CommodConverter Class.
virtual void Tick()
The handleTick function specific to the CommodConverter.
void out_commod_(std::string c)
the out commodity
void max_inv_size_(double c)
the maximum amount allowed in inventory
void crctx(const cyclus::toolkit::CommodityRecipeContext &crctx)
this facility's commodity-recipe context
std::map< int, cyclus::toolkit::ResourceBuff > processing
map from ready time to resource buffers