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
use super::{
Fluid, FluidOutputError, OutputResult, StateResult,
backend::Backend,
common::{cached_output, guard},
request::FluidUpdateRequest,
};
use crate::{
io::{FluidInput, FluidTrivialParam},
ops::mul,
state_variant::StateVariant,
substance::Substance,
};
impl<S: StateVariant> Fluid<S> {
/// Specified substance.
#[must_use]
pub fn substance(&self) -> &Substance {
&self.substance
}
/// Specified `CoolProp` backend.
#[must_use]
pub fn backend(&self) -> Backend {
self.backend_variant
}
/// Acentric factor **\[dimensionless\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn acentric_factor(&mut self) -> OutputResult<f64> {
self.trivial_output(FluidTrivialParam::AcentricFactor)
}
/// Critical point mass density **\[kg/m³\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn critical_density(&mut self) -> OutputResult<f64> {
let key = FluidTrivialParam::DMassCritical;
// Due to CoolProp freeze
if let Substance::PredefinedMix(_) = self.substance {
return Err(FluidOutputError::UnavailableTrivialOutput(key));
}
self.positive_trivial_output(key)
}
/// Critical point molar density **\[mol/m³\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn critical_molar_density(&mut self) -> OutputResult<f64> {
let key = FluidTrivialParam::DMolarCritical;
// Due to CoolProp freeze
if let Substance::PredefinedMix(_) = self.substance {
return Err(FluidOutputError::UnavailableTrivialOutput(key));
}
self.positive_trivial_output(key)
}
/// Critical point pressure **\[Pa\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn critical_pressure(&mut self) -> OutputResult<f64> {
let key = FluidTrivialParam::PCritical;
// Due to CoolProp freeze
if let Substance::PredefinedMix(_) = self.substance {
return Err(FluidOutputError::UnavailableTrivialOutput(key));
}
self.positive_trivial_output(key)
}
/// Critical point temperature **\[K\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn critical_temperature(&mut self) -> OutputResult<f64> {
let key = FluidTrivialParam::TCritical;
// Due to CoolProp freeze
if let Substance::PredefinedMix(_) = self.substance {
return Err(FluidOutputError::UnavailableTrivialOutput(key));
}
self.positive_trivial_output(key)
}
/// Flammability hazard index **\[dimensionless\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn flammability_hazard(&mut self) -> OutputResult<f64> {
self.non_negative_trivial_output(FluidTrivialParam::FH)
}
/// Freezing temperature for incompressible mixtures **\[K\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn freezing_temperature(&mut self) -> OutputResult<f64> {
self.positive_trivial_output(FluidTrivialParam::TFreeze)
}
/// 20-year global warming potential **\[dimensionless\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn gwp20(&mut self) -> OutputResult<f64> {
self.non_negative_trivial_output(FluidTrivialParam::GWP20)
}
/// 100-year global warming potential **\[dimensionless\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn gwp100(&mut self) -> OutputResult<f64> {
self.non_negative_trivial_output(FluidTrivialParam::GWP100)
}
/// 500-year global warming potential **\[dimensionless\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn gwp500(&mut self) -> OutputResult<f64> {
self.non_negative_trivial_output(FluidTrivialParam::GWP500)
}
/// Health hazard index **\[dimensionless\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn health_hazard(&mut self) -> OutputResult<f64> {
self.non_negative_trivial_output(FluidTrivialParam::HH)
}
/// Maximum pressure **\[Pa\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn max_pressure(&mut self) -> OutputResult<f64> {
self.positive_trivial_output(FluidTrivialParam::PMax)
}
/// Maximum temperature **\[K\]**.
pub fn max_temperature(&mut self) -> f64 {
self.positive_trivial_output(FluidTrivialParam::TMax).unwrap()
}
/// Minimum pressure **\[Pa\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn min_pressure(&mut self) -> OutputResult<f64> {
self.positive_trivial_output(FluidTrivialParam::PMin)
}
/// Minimum temperature **\[K\]**.
pub fn min_temperature(&mut self) -> f64 {
self.positive_trivial_output(FluidTrivialParam::TMin).unwrap()
}
/// Molar mass **\[kg/mol\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn molar_mass(&mut self) -> OutputResult<f64> {
self.positive_trivial_output(FluidTrivialParam::MolarMass)
}
/// Ozone depletion potential **\[dimensionless\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn odp(&mut self) -> OutputResult<f64> {
self.non_negative_trivial_output(FluidTrivialParam::ODP)
}
/// Physical hazard index **\[dimensionless\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn physical_hazard(&mut self) -> OutputResult<f64> {
self.non_negative_trivial_output(FluidTrivialParam::PH)
}
/// Reducing point mass density **\[kg/m³\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn reducing_density(&mut self) -> OutputResult<f64> {
mul(self.reducing_molar_density(), self.molar_mass())
}
/// Reducing point molar density **\[mol/m³\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn reducing_molar_density(&mut self) -> OutputResult<f64> {
self.positive_trivial_output(FluidTrivialParam::DMolarReducing)
}
/// Reducing point pressure **\[Pa\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn reducing_pressure(&mut self) -> OutputResult<f64> {
self.positive_trivial_output(FluidTrivialParam::PReducing)
}
/// Reducing point temperature **\[K\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn reducing_temperature(&mut self) -> OutputResult<f64> {
self.positive_trivial_output(FluidTrivialParam::TReducing)
}
/// Triple point pressure **\[Pa\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn triple_pressure(&mut self) -> OutputResult<f64> {
self.positive_trivial_output(FluidTrivialParam::PTriple)
}
/// Triple point temperature **\[K\]**.
///
/// # Errors
///
/// Returns a [`FluidOutputError`] if the property is not available for the specified substance.
pub fn triple_temperature(&mut self) -> OutputResult<f64> {
self.positive_trivial_output(FluidTrivialParam::TTriple)
}
pub(crate) fn inner_update(
&mut self,
input1: FluidInput,
input2: FluidInput,
) -> StateResult<()> {
let request: FluidUpdateRequest = (input1, input2).try_into()?;
self.backend.update(request.input_pair, request.value1, request.value2)?;
self.outputs.clear();
self.outputs.insert(input1.key, Ok(input1.value));
self.outputs.insert(input2.key, Ok(input2.value));
self.update_request = Some(request);
Ok(())
}
fn positive_trivial_output(&mut self, key: FluidTrivialParam) -> OutputResult<f64> {
self.trivial_output(key).and_then(|value| guard(key.into(), value, |x| x > 0.0))
}
fn non_negative_trivial_output(&mut self, key: FluidTrivialParam) -> OutputResult<f64> {
self.trivial_output(key).and_then(|value| guard(key.into(), value, |x| x >= 0.0))
}
fn trivial_output(&mut self, key: FluidTrivialParam) -> OutputResult<f64> {
cached_output(&mut self.trivial_outputs, &mut self.backend, key, |_| {
FluidOutputError::UnavailableTrivialOutput(key)
})
.and_then(|value| guard(key.into(), value, f64::is_finite))
}
}