forked from Brewtarget/brewtarget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMashWizard.cpp
415 lines (354 loc) · 17.1 KB
/
MashWizard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
* MashWizard.cpp is part of Brewtarget, and is copyright the following authors 2009-2024:
* • Adam Hawes <[email protected]>
* • Brian Rower <[email protected]>
* • David Grundberg <[email protected]>
* • Greg Meess <[email protected]>
* • Jeff Bailey <[email protected]>
* • Mattias Måhl <[email protected]>
* • Matt Young <[email protected]>
* • Mik Firestone <[email protected]>
* • Philip Greggory Lee <[email protected]>
* • Rob Taylor <[email protected]>
*
* Brewtarget is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Brewtarget is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌*/
#include "MashWizard.h"
#include <QButtonGroup>
#include <QMessageBox>
#include "Algorithms.h"
#include "database/ObjectStoreWrapper.h"
#include "HeatCalculations.h"
#include "measurement/Measurement.h"
#include "model/Equipment.h"
#include "model/Fermentable.h"
#include "model/Mash.h"
#include "model/MashStep.h"
#include "PhysicalConstants.h"
MashWizard::MashWizard(QWidget* parent) :
QDialog(parent),
m_recObs{nullptr},
m_weightUnit{nullptr},
m_volumeUnit{nullptr} {
setupUi(this);
this->m_bGroup = new QButtonGroup();
this->m_bGroup->addButton(radioButton_noSparge);
this->m_bGroup->addButton(radioButton_batchSparge);
this->m_bGroup->addButton(radioButton_flySparge);
// radioButton_batchSparge->setChecked(true);
connect(this->m_bGroup , QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), this, &MashWizard::toggleSpinBox);
connect(this->buttonBox, &QDialogButtonBox::accepted, this, &MashWizard::wizardry);
connect(this->buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close);
return;
}
void MashWizard::toggleSpinBox(QAbstractButton* button) {
if ( button == radioButton_noSparge ) {
widget_batches->setEnabled(false);
widget_mashThickness->setEnabled(false);
} else if ( button == radioButton_flySparge ) {
widget_batches->setEnabled(false);
widget_mashThickness->setEnabled(true);
} else {
widget_batches->setEnabled(true);
widget_mashThickness->setEnabled(true);
}
return;
}
void MashWizard::setRecipe(Recipe* rec) {
m_recObs = rec;
return;
}
void MashWizard::show() {
if ( m_recObs == nullptr || m_recObs->mash() == nullptr ) {
return;
}
// Ensure at least one mash step.
if ( m_recObs->mash()->mashSteps().size() == 0 ) {
QMessageBox::information(this, tr("No steps"), tr("There must be at least one mash step to run the wizard."));
return;
}
Measurement::getThicknessUnits(&m_volumeUnit,&m_weightUnit);
label_mashThickness->setText(tr("Mash thickness (%1/%2)").arg(m_volumeUnit->name, m_weightUnit->name));
auto firstStep = m_recObs->mash()->mashSteps().first();
auto lastStep = m_recObs->mash()->mashSteps().last();
// Recalculate the mash thickness
double thickNum = firstStep->amount_l()/m_recObs->grainsInMash_kg();
double thickness = thickNum * m_weightUnit->toCanonical(1).quantity / m_volumeUnit->toCanonical(1).quantity ;
doubleSpinBox_thickness->setValue(thickness);
// Is this a batch, fly or no sparge?
if ( firstStep == lastStep ) {
radioButton_noSparge->setChecked(true);
widget_batches->setEnabled(false);
widget_mashThickness->setEnabled(false);
} else if ( lastStep->type() == MashStep::Type::FlySparge ) {
radioButton_flySparge->setChecked(true);
widget_batches->setEnabled(false);
widget_mashThickness->setEnabled(true);
} else {
int countSteps = 0;
auto steps = m_recObs->mash()->mashSteps();
for(auto step : steps) {
if (step->isSparge()) {
countSteps++;
}
}
widget_batches->setEnabled(true);
widget_mashThickness->setEnabled(true);
radioButton_batchSparge->setChecked(true);
spinBox_batches->setValue(countSteps);
}
setVisible(true);
return;
}
double MashWizard::calcDecoctionAmount(MashStep * step,
Mash * mash,
double waterMass,
double grainMass,
double lastTemp,
double boiling) {
double const grainDensity = PhysicalConstants::grainDensity_kgL;
double const stepTemp = step->startTemp_c().value_or(0.0);
double const equipMass = (mash->equipAdjust()) ? mash->mashTunWeight_kg().value_or(0.0) : 0.0;
double const c_e = (mash->equipAdjust()) ? mash->mashTunSpecificHeat_calGC().value_or(0.0) : 0.0;
double const grHeat = grainMass * HeatCalculations::Cw_calGC;
double const waHeat = waterMass * HeatCalculations::Cgrain_calGC;
double const eqHeat = equipMass * c_e;
double const totalHeat = grHeat + waHeat;
double const deltaTemp = stepTemp - lastTemp;
// r is the ratio of water and grain to take out for decoction.
double const r = ((totalHeat + eqHeat)*deltaTemp) / (totalHeat*(boiling - stepTemp) + totalHeat*deltaTemp);
if( r < 0 || r > 1 )
{
QMessageBox::critical(this, tr("Decoction error"), tr("Something went wrong in decoction calculation.") );
qCritical() << Q_FUNC_INFO << "r=" << r;
return -1;
}
return r * (waterMass + grainMass/grainDensity);
}
void MashWizard::wizardry() {
if (!this->m_recObs || !this->m_recObs->mash()) {
return;
}
auto mash = m_recObs->mash();
double const grainDensity = PhysicalConstants::grainDensity_kgL;
double absorption_LKg = PhysicalConstants::grainAbsorption_Lkg;
double boilingPoint_c = 100.0;
double lauterDeadspace = 0.0;
// If we have an equipment, utilize the custom absorption and boiling temp.
if (m_recObs->equipment()) {
absorption_LKg = m_recObs->equipment()->mashTunGrainAbsorption_LKg().value_or(Equipment::default_mashTunGrainAbsorption_LKg);
boilingPoint_c = m_recObs->equipment()->boilingPoint_c();
lauterDeadspace = m_recObs->equipment()->getLauteringDeadspaceLoss_l();
}
auto steps = mash->mashSteps();
decltype(steps) tmp;
// We ensured that there was at least one mash step when we displayed the thickness dialog in show().
auto mashStep = steps.at(0);
if (!mashStep) {
qCritical() << Q_FUNC_INFO << "First mash step was null.";
return;
}
// Ensure first mash step is an infusion.
if (!mashStep->isInfusion() && !mashStep->isSparge()) {
QMessageBox::information(this, tr("First step"), tr("Your first mash step must be an infusion."));
return;
}
// Find any batch sparges and remove them
for (auto step : steps) {
if (step->isSparge()) {
mash->remove(step);
} else {
tmp.append(step);
}
}
double thickness_LKg;
double thickNum;
steps = tmp;
double grainMass = m_recObs->grainsInMash_kg();
if ( m_bGroup->checkedButton() != radioButton_noSparge ) {
thickNum = doubleSpinBox_thickness->value();
thickness_LKg = thickNum * m_volumeUnit->toCanonical(1).quantity / m_weightUnit->toCanonical(1).quantity;
} else {
// not sure I like this. Why is this here and not somewhere later?
if (steps.size() == 1 ) {
mashStep->setAmount_l(m_recObs->targetTotalMashVol_l());
}
// For no sparge, get the thickness of the first mash step
thickNum = mashStep->amount_l()/grainMass;
thickness_LKg = thickNum;
}
if( thickness_LKg <= 0.0 ) {
QMessageBox::information(this, tr("Bad thickness"), tr("You must have a positive mash thickness."));
return;
}
// Do first step
double tempFinal = mashStep->startTemp_c().value_or(0.0);
double tempInitial = mash->grainTemp_c();
double massWater = thickness_LKg * grainMass;
// Thermal mass of mash and water.
double const MCw = HeatCalculations::Cw_calGC * massWater;
double MC = HeatCalculations::Cgrain_calGC * grainMass;
// I am specifically ignoring BeerXML's request to only do this if mash->getEquipAdjust() is set.
double tempWater = MC/MCw * (tempFinal-tempInitial) + (mash->mashTunSpecificHeat_calGC().value_or(0.0)*mash->mashTunWeight_kg().value_or(0.0))/MCw * (tempFinal-mash->tunTemp_c().value_or(0.0)) + tempFinal;
// Can't have water above boiling.
if( tempWater > boilingPoint_c ) {
QMessageBox::information(this,
tr("Mash too thick"),
tr("Your mash is too thick for desired temp. at first step."));
return;
}
mashStep->setAmount_l(massWater);
mashStep->setInfuseTemp_c(tempWater);
//================End of first step=====================
// Do rest of steps.
// Add thermal mass of equipment to MC.
// I am specifically ignoring BeerXML's request to only do this if mash->getEquipAdjust() is set.
MC += mash->mashTunSpecificHeat_calGC().value_or(0.0) * mash->mashTunWeight_kg().value_or(0.0);
for (int i = 1; i < steps.size(); ++i) {
mashStep = steps[i];
if (mashStep->isTemperature()) {
continue;
} else if (mashStep->isDecoction()) {
double m_w, m_g, m_e, r;
double c_w, c_g, c_e;
tempFinal = mashStep->startTemp_c().value_or(0.0);
tempInitial = steps[i-1]->startTemp_c().value_or(0.0);
m_w = 0; // Total mass of water.
for (int j = 0; j < i; ++j) {
m_w += steps[j]->amount_l();
}
m_g = grainMass;
m_e = mash->equipAdjust() ? mash->mashTunWeight_kg().value_or(0.0) : 0.0;
c_w = HeatCalculations::Cw_calGC;
c_g = HeatCalculations::Cgrain_calGC;
c_e = mash->equipAdjust() ? mash->mashTunSpecificHeat_calGC().value_or(0.0) : 0.0;
// r is the ratio of water and grain to take out for decoction.
r = ((m_w*c_w + m_g*c_g + m_e*c_e)*(tempFinal-tempInitial)) / ((m_w*c_w + m_g*c_g)*(boilingPoint_c-tempFinal) + (m_w*c_w + m_g*c_g)*(tempFinal-tempInitial));
if( r < 0 || r > 1 ) {
QMessageBox::critical(this, tr("Decoction error"), tr("Something went wrong in decoction calculation.") );
qCritical().nospace() << Q_FUNC_INFO << "Decoction: r=" << r;
return;
}
mashStep->setAmount_l( r*(m_w + m_g/grainDensity) );
}
else {
tempFinal = mashStep->startTemp_c().value_or(0.0);
tempInitial = steps[i-1]->startTemp_c().value_or(0.0);
tempWater = boilingPoint_c; // Assume adding boiling water to minimize final volume.
MC += massWater * HeatCalculations::Cw_calGC; // Add thermal mass of last addition.
massWater = (MC*(tempFinal-tempInitial))/(HeatCalculations::Cw_calGC * (tempWater-tempFinal));
mashStep->setAmount_l(massWater);
mashStep->setInfuseTemp_c(tempWater);
}
}
// if no sparge, adjust volume of last step to meet target runoff volume
if ( m_bGroup->checkedButton() == radioButton_noSparge && steps.size() > 1) {
double otherMashStepTotal = 0.0;
for (int i = 0; i < steps.size()-1; ++i) {
otherMashStepTotal += steps[i]->amount_l();
}
mashStep = steps.back();
if (steps.size() > 1 ) {
tempInitial = steps[steps.size()-2]->startTemp_c().value_or(0.0);
} else {
tempInitial = mash->grainTemp_c();
}
double targetWortFromMash= m_recObs->targetTotalMashVol_l() + lauterDeadspace;
massWater = (targetWortFromMash - otherMashStepTotal)*Algorithms::getWaterDensity_kgL(0);
tempFinal = mashStep->startTemp_c().value_or(0.0);
MC += massWater * HeatCalculations::Cw_calGC; // Add thermal mass of last addition.
tempWater = (MC*(tempFinal-tempInitial))/massWater/HeatCalculations::Cw_calGC + tempFinal;
if(tempWater > boilingPoint_c)
QMessageBox::information(this,
tr("Infusion temp."),
tr("In order to hit your target temp on the final step, the infusion water must be above boiling. Lower your initial infusion volume."));
mashStep->setAmount_l(massWater);
mashStep->setInfuseTemp_c(tempWater);
}
// Now, do a sparge step, using just enough water that the total
// volume sums up to the target pre-boil size. We need to account for the potential
// lauter dead space, I think?
double spargeWater_l = m_recObs->targetTotalMashVol_l() - m_recObs->mash()->totalMashWater_l();
// If I've done my math right, we should never get here on nosparge
// not sure why I am inferring this when I could just check the button group?
if( spargeWater_l >= 0.001 )
{
spargeWater_l += lauterDeadspace;
int lastMashStep = steps.size()-1;
tempFinal = mash->spargeTemp_c().value_or(0.0); // TODO We should have some better logic if spargeTemp_c is unset or 0.
if( lastMashStep >= 0 )
tempInitial = steps[lastMashStep]->startTemp_c().value_or(0.0) - 10.0; // You will lose about 10C from last step.
else
{
qCritical() << Q_FUNC_INFO << "Should have had at least one mash step before getting to sparging.";
return;
}
MC = m_recObs->grainsInMash_kg() * HeatCalculations::Cgrain_calGC
+ absorption_LKg * m_recObs->grainsInMash_kg() * HeatCalculations::Cw_calGC
+ mash->mashTunWeight_kg().value_or(0.0) * mash->mashTunSpecificHeat_calGC().value_or(0.0);
massWater = spargeWater_l;
tempWater = (MC/(massWater*HeatCalculations::Cw_calGC))*(tempFinal-tempInitial) + tempFinal;
if(tempWater > boilingPoint_c)
QMessageBox::information(this,
tr("Sparge temp."),
tr("In order to hit your sparge temp, the sparge water must be above boiling. Lower your sparge temp, or allow for more sparge water."));
if ( m_bGroup->checkedButton() == radioButton_batchSparge ) {
int numSteps = spinBox_batches->value();
double volPerBatch = spargeWater_l/numSteps; // its evil, but deal with it
for(int i=0; i < numSteps; ++i ) {
auto newMashStep = std::make_shared<MashStep>(tr("Batch Sparge %1").arg(i+1));
newMashStep->setType(MashStep::Type::BatchSparge);
newMashStep->setAmount_l(volPerBatch);
newMashStep->setInfuseTemp_c(tempWater);
newMashStep->setEndTemp_c(tempWater);
newMashStep->setStartTemp_c(tempFinal);
newMashStep->setStepTime_mins(15);
newMashStep->setOwnerId(mash->key());
ObjectStoreWrapper::insert(newMashStep);
steps.append(newMashStep);
newMashStep->setStepNumber(steps.size());
emit newMashStep->changed(
newMashStep->metaObject()->property(
newMashStep->metaObject()->indexOfProperty(*PropertyNames::MashStep::type)
)
);
}
emit mash->stepsChanged();
}
// fly sparge, I think
else {
auto newMashStep = std::make_shared<MashStep>(tr("Fly Sparge"));
newMashStep->setType(MashStep::Type::FlySparge);
newMashStep->setAmount_l(spargeWater_l);
newMashStep->setInfuseTemp_c(tempWater);
newMashStep->setEndTemp_c(tempWater);
newMashStep->setStartTemp_c(tempFinal);
newMashStep->setStepTime_mins(15);
newMashStep->setOwnerId(mash->key());
ObjectStoreWrapper::insert(newMashStep);
steps.append(newMashStep);
newMashStep->setStepNumber(steps.size());
emit newMashStep->changed(
newMashStep->metaObject()->property(
newMashStep->metaObject()->indexOfProperty(*PropertyNames::MashStep::type)
)
);
}
}
else if ( m_bGroup->checkedButton() != radioButton_noSparge )
{
QMessageBox::information(this,
tr("Too much wort"),
tr("You have too much wort from the mash for your boil size. I suggest increasing the boil size by increasing the boil time, or reducing your mash thickness."));
}
return;
}