-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_operations.mbt
More file actions
251 lines (236 loc) · 6.32 KB
/
Copy pathbatch_operations.mbt
File metadata and controls
251 lines (236 loc) · 6.32 KB
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
///|
/// Batch conversion at a sequence of times.
pub fn batch_conversion_curve(
reaction : Reaction,
feed : Feed,
times : ArrayView[Double],
) -> Array[Double] {
times.map(fn(time) { batch_conversion_isothermal(reaction, feed, time) })
}
///|
/// Batch concentration at a sequence of times.
pub fn batch_concentration_curve(
reaction : Reaction,
feed : Feed,
times : ArrayView[Double],
) -> Array[Double] {
batch_conversion_curve(reaction, feed, times).map(fn(conversion) {
concentration_from_conversion(feed, conversion)
})
}
///|
/// Batch rate at a sequence of times.
pub fn batch_rate_curve(
reaction : Reaction,
feed : Feed,
times : ArrayView[Double],
) -> Array[Double] {
batch_concentration_curve(reaction, feed, times).map(fn(concentration) {
reaction.rate(concentration, feed.temperature)
})
}
///|
/// Find the first time at which a batch conversion target is reached.
pub fn batch_time_for_target(
reaction : Reaction,
feed : Feed,
target : Double,
maximum_time : Double,
points : Int,
) -> Double? {
let times = linspace(0.0, maximum_time.max(0.0), points.max(1))
for time in times {
if batch_conversion_isothermal(reaction, feed, time) >=
clamp_conversion(target) {
return Some(time)
}
}
None
}
///|
/// Average batch conversion over a time window.
pub fn average_batch_conversion(
reaction : Reaction,
feed : Feed,
duration : Double,
points : Int,
) -> Double {
let values = batch_conversion_curve(
reaction,
feed,
linspace(0.0, duration.max(0.0), points.max(2)),
)
if values.length() == 0 {
0.0
} else {
values.fold(init=0.0, fn(acc, value) { acc + value }) /
Double::from_int(values.length())
}
}
///|
/// Batch productivity at a selected cycle time.
pub fn batch_productivity(
feed : Feed,
conversion : Double,
cycle_time : Double,
cleaning_time : Double,
) -> Double {
feed.concentration.max(0.0) *
clamp_conversion(conversion) /
(cycle_time.max(0.0) + cleaning_time.max(0.0)).max(1.0e-12)
}
///|
/// Optimize batch cycle time on a regular grid.
pub fn optimize_batch_cycle(
reaction : Reaction,
feed : Feed,
maximum_time : Double,
cleaning_time : Double,
points : Int,
) -> ObjectiveScore? {
let mut best : ObjectiveScore? = None
for time in linspace(0.0, maximum_time.max(0.0), points.max(1)) {
let point = design_batch(reaction, feed, time)
let score = batch_productivity(feed, point.conversion, time, cleaning_time)
let candidate : ObjectiveScore = {
volume: time,
conversion: point.conversion,
temperature: point.outlet_temperature,
score,
feasible: true,
}
match best {
None => best = Some(candidate)
Some(current) => if score > current.score { best = Some(candidate) }
}
}
best
}
///|
/// Batch conversion gain between two times.
pub fn batch_conversion_gain(
reaction : Reaction,
feed : Feed,
first_time : Double,
second_time : Double,
) -> Double {
batch_conversion_isothermal(reaction, feed, second_time) -
batch_conversion_isothermal(reaction, feed, first_time)
}
///|
/// Check monotonicity of a batch curve.
pub fn batch_curve_is_monotone(values : ArrayView[Double]) -> Bool {
for i = 1; i < values.length(); i = i + 1 {
if values[i] + 1.0e-10 < values[i - 1] {
return false
}
}
true
}
///|
/// CSTR conversion over a sequence of volumes.
pub fn cstr_conversion_curve(
reaction : Reaction,
feed : Feed,
volumes : ArrayView[Double],
) -> Array[Double] {
volumes.map(fn(volume) { cstr_conversion_isothermal(reaction, feed, volume) })
}
///|
/// PFR conversion over a sequence of volumes.
pub fn pfr_conversion_curve(
reaction : Reaction,
feed : Feed,
volumes : ArrayView[Double],
) -> Array[Double] {
volumes.map(fn(volume) { pfr_conversion_isothermal(reaction, feed, volume) })
}
///|
/// Compare CSTR and PFR curves at equal volumes.
pub fn compare_reactor_curves(
reaction : Reaction,
feed : Feed,
volumes : ArrayView[Double],
) -> Array[(Double, Double, Double)] {
let cstr = cstr_conversion_curve(reaction, feed, volumes)
let pfr = pfr_conversion_curve(reaction, feed, volumes)
let result : Array[(Double, Double, Double)] = []
for i in 0..<volumes.length().min(cstr.length()).min(pfr.length()) {
result.push((volumes[i], cstr[i], pfr[i]))
}
result
}
///|
/// Average advantage of a PFR curve over a CSTR curve.
pub fn average_pfr_advantage(
curve : ArrayView[(Double, Double, Double)],
) -> Double {
if curve.length() == 0 {
0.0
} else {
curve.fold(init=0.0, fn(acc, point) { acc + point.2 - point.1 }) /
Double::from_int(curve.length())
}
}
///|
/// Select the smallest volume meeting a target on a curve.
pub fn curve_volume_for_target(
curve : ArrayView[(Double, Double, Double)],
target : Double,
use_pfr : Bool,
) -> Double? {
for point in curve {
if (if use_pfr { point.2 } else { point.1 }) >= clamp_conversion(target) {
return Some(point.0)
}
}
None
}
///|
/// Integrate a conversion curve with respect to volume.
pub fn conversion_volume_area(
curve : ArrayView[(Double, Double, Double)],
use_pfr : Bool,
) -> Double {
if curve.length() < 2 {
0.0
} else {
for i = 1, area = 0.0; i < curve.length(); i = i + 1 {
let left = if use_pfr { curve[i - 1].2 } else { curve[i - 1].1 }
let right = if use_pfr { curve[i].2 } else { curve[i].1 }
continue i + 1,
area + (curve[i].0 - curve[i - 1].0) * (left + right) / 2.0
} nobreak {
area
}
}
}
///|
/// A conservative batch design window.
pub fn batch_design_window(
reaction : Reaction,
feed : Feed,
minimum_conversion : Double,
maximum_temperature : Double,
maximum_time : Double,
points : Int,
) -> Array[EnvelopePoint] {
let result : Array[EnvelopePoint] = []
for time in linspace(0.0, maximum_time.max(0.0), points.max(1)) {
let point = design_batch(reaction, feed, time)
let conversion_ok = point.conversion >= clamp_conversion(minimum_conversion)
let temperature_ok = point.outlet_temperature <= maximum_temperature
result.push({
volume: time * feed.volumetric_flow,
conversion: point.conversion,
temperature: point.outlet_temperature,
feasible: conversion_ok && temperature_ok,
reason: if conversion_ok && temperature_ok {
"feasible"
} else {
"outside limits"
},
})
}
result
}