CYCLUS
resource_buff.cc
Go to the documentation of this file.
1 #include "resource_buff.h"
2 #include "cyc_arithmetic.h"
3 
4 #include <iomanip>
5 
6 namespace cyclus {
7 namespace toolkit {
8 
9 void ResourceBuff::set_capacity(double cap) {
10  if (quantity() - cap > eps_rsrc()) {
11  std::stringstream ss;
12  ss << std::setprecision(17) <<"new capacity " << cap
13  << " lower than existing quantity " << quantity();
14  throw ValueError(ss.str());
15  }
16  capacity_ = cap;
17 }
18 
20  if (qty > quantity()) {
21  std::stringstream ss;
22  ss << std::setprecision(17) <<"removal quantity " << qty
23  << " larger than buff quantity " << quantity();
24  throw ValueError(ss.str());
25  }
26 
27  Manifest manifest;
28  Resource::Ptr r, tmp;
29  double left = qty;
30  double quan;
31  while (left > 0 && count() > 0) {
32  r = mats_.front();
33  mats_.pop_front();
34  quan = r->quantity();
35  if (quan > left) {
36  // too big - split the res before popping
37  tmp = r->ExtractRes(left);
38  mats_.push_front(r);
39  r = tmp;
40  } else {
41  mats_present_.erase(r);
42  }
43 
44  manifest.push_back(r);
45  left -= quan;
46  }
47 
48  UpdateQty();
49 
50  return manifest;
51 }
52 
53 Manifest ResourceBuff::PopQty(double qty, double eps) {
54  if (qty > quantity() + eps) {
55  std::stringstream ss;
56  ss << std::setprecision(17) <<"removal quantity " << qty
57  << " larger than buff quantity " << quantity();
58  throw ValueError(ss.str());
59  }
60 
61  if (qty >= quantity()) {
62  return PopN(count());
63  }
64  return PopQty(qty);
65 }
66 
67 void ResourceBuff::UpdateQty() {
68  std::list<Resource::Ptr>::iterator it;
69  std::vector<double> qtys;
70  for (it = mats_.begin(); it != mats_.end(); ++it) {
71  qtys.push_back((*it)->quantity());
72  }
73  qty_ = CycArithmetic::KahanSum(qtys);
74 }
75 
77  if (count() < num || num < 0) {
78  std::stringstream ss;
79  ss << "remove count " << num << " larger than buff count " << count();
80  throw ValueError(ss.str());
81  }
82 
83  Manifest manifest;
84  for (int i = 0; i < num; i++) {
85  Resource::Ptr r = mats_.front();
86  mats_.pop_front();
87  manifest.push_back(r);
88  mats_present_.erase(r);
89  }
90 
91  UpdateQty();
92  return manifest;
93 }
94 
96  if (mats_.size() < 1) {
97  throw ValueError("cannot pop resource from an empty buff");
98  }
99 
100  Resource::Ptr r;
101  if (dir == FRONT) {
102  r = mats_.front();
103  mats_.pop_front();
104  } else {
105  r = mats_.back();
106  mats_.pop_back();
107  }
108 
109  mats_present_.erase(r);
110  UpdateQty();
111  return r;
112 }
113 
115  if (r->quantity() - space() > eps_rsrc()) {
116  std::stringstream ss;
117  ss << "resource pushing breaks capacity limit: space=" << space()
118  << ", rsrc->quantity()=" << r->quantity();
119  throw ValueError(ss.str());
120  } else if (mats_present_.count(r) == 1) {
121  throw KeyError("duplicate resource push attempted");
122  }
123 
124  mats_.push_back(r);
125  mats_present_.insert(r);
126  UpdateQty();
127 }
128 
129 } // namespace toolkit
130 } // namespace cyclus
std::vector< Resource::Ptr > Manifest
Definition: resource_buff.h:20
double space() const
Space returns the quantity of space remaining in this store.
Definition: resource_buff.h:74
Manifest PopN(int num)
PopN pops the specified number or count of resource objects from the store.
void Push(Resource::Ptr r)
Push pushs a single resource object to the store.
int count() const
Count returns the total number of constituent resource objects in the store.
Definition: resource_buff.h:60
T::Ptr Pop()
A convenience method identical to Pop for auto-casting to specific Resource types.
For values that are too big, too small, etc.
Definition: error.h:41
double quantity() const
Quantity returns the total resource quantity of constituent resource objects in the store...
Definition: resource_buff.h:66
void set_capacity(double cap)
Set_capacity sets the maximum quantity this store can hold (units based on constituent resource objec...
Definition: resource_buff.cc:9
double eps_rsrc()
an epsilon value to be used by resources
Definition: cyc_limits.h:19
static double KahanSum(std::vector< double > input)
sums the materials in the vector in an intelligent way, to avoid floating point issues.
boost::shared_ptr< Resource > Ptr
Definition: resource.h:24
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
Manifest PopQty(double qty)
PopQty pops the specified quantity of resources from the buffer.
Declares the CycArithmetic class, which holds arithmetic algorithms.
For failed retrieval/insertion of key-based data into/from data structures.
Definition: error.h:47
double eps()
a generic epsilon value
Definition: cyc_limits.h:12