Skip to content

Commit 6bfec62

Browse files
committed
modified mixer
1 parent 906caa3 commit 6bfec62

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/mixer.cc

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ void Mixer::EnterNotify() {
5757
streambufs[name].capacity(cap);
5858
}
5959
in_commods.push_back(streams_[i].second);
60-
61-
InitializePosition();
6260
}
6361

6462
// ratio normalisation
@@ -91,9 +89,14 @@ void Mixer::EnterNotify() {
9189
}
9290

9391
sell_policy.Init(this, &output, "output").Set(out_commod).Start();
92+
93+
InitializeMarginalCost();
94+
InitializePosition();
9495
}
9596

9697
void Mixer::Tick() {
98+
using cyclus::toolkit::MatVec;
99+
97100
if (output.quantity() < output.capacity()) {
98101
double tgt_qty = output.space();
99102

@@ -118,9 +121,24 @@ void Mixer::Tick() {
118121
m->Absorb(m_);
119122
}
120123
}
124+
121125
output.Push(m);
122126
}
123127
}
128+
// Calculate quantity-weighted average unit value of the output buffer
129+
// NOTE: We can't just use m->UnitValue().
130+
double total_cost = 0.0;
131+
double total_qty = 0.0;
132+
MatVec output_mats = output.PopN(output.count());
133+
for (const auto& mat : output_mats) {
134+
total_cost += mat->quantity() * mat->UnitValue();
135+
total_qty += mat->quantity();
136+
}
137+
output.Push(output_mats);
138+
139+
double avg_unit_value = (total_qty > 0) ? (total_cost / total_qty) : 0.0;
140+
sell_policy.set_cost_per_unit(variable_cost_per_unit);
141+
124142
cyclus::toolkit::RecordTimeSeries<double>("supply"+out_commod, this, output.quantity());
125143
}
126144

@@ -155,11 +173,18 @@ Mixer::GetMatlRequests() {
155173

156174
std::vector<cyclus::Request<cyclus::Material>*> reqs;
157175

158-
std::map<std::string, double>::iterator it;
159-
for (it = in_commods[i].begin() ; it != in_commods[i].end(); it++) {
160-
std::string commod = it->first;
161-
double pref = it->second;
162-
reqs.push_back(port->AddRequest(m, this, commod , pref, false));
176+
// in_commods[i] is map<commodity_name, pref>; extract vectors for CalcMarginalUtility
177+
std::vector<std::string> commods;
178+
std::vector<double> prefs;
179+
for (const auto& kv : in_commods[i]) {
180+
commods.push_back(kv.first); // commodity name
181+
prefs.push_back(kv.second); // pref
182+
}
183+
auto mu_results = CalcMarginalUtility(commods, prefs);
184+
std::vector<std::string> mu_commods = mu_results.first;
185+
std::vector<double> mu_values = mu_results.second;
186+
for (int i = 0; i < mu_commods.size(); i++) {
187+
reqs.push_back(port->AddRequest(m, this, mu_commods[i], mu_values[i], false));
163188
req_inventories_[reqs.back()] = name;
164189
}
165190
port->AddMutualReqs(reqs);

src/mixer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ class Mixer
117117
private:
118118
// Code Injection:
119119
#include "toolkit/position.cycpp.h"
120+
#include "toolkit/marginal_cost.cycpp.h"
121+
#include "toolkit/marginal_utility.cycpp.h"
120122

121123
};
122124

0 commit comments

Comments
 (0)