Repository navigation
Expand file tree
/
Copy pathcomposition.rs
More file actions
215 lines (203 loc) · 6.38 KB
/
Copy pathcomposition.rs
File metadata and controls
215 lines (203 loc) · 6.38 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
use crate::equation_of_state::Residual;
use crate::{FeosError, FeosResult};
use nalgebra::allocator::Allocator;
use nalgebra::{DefaultAllocator, Dim, Dyn, OVector, U1, U2, dvector, vector};
use num_dual::{DualNum, DualStruct};
use quantity::Moles;
/// Trait to generalize over different input types for the composition of
/// a state.
///
/// The trait is implemented for the following data types:
///
/// |components|input|total_moles?|comment|
/// |:-:|-|-|-|
/// |1|`()`|-||
/// |1|`Moles`|✅|
/// |2|`f64`|-|
/// |N|`OVector<f64,N>`|-|
/// |N|`&OVector<f64,N>`|-|
/// |N|`OVector<f64,N-1>`|-|`Dyn` only|
/// |N|`&OVector<f64,N-1>`|-|`Dyn` only|
/// |N|`Moles<OVector<f64,N>>`|✅|
/// |N|`&Moles<OVector<f64,N>>`|✅|
pub trait Composition<D: DualNum<f64> + Copy, N: Dim>
where
DefaultAllocator: Allocator<N>,
{
/// Convert the composition into molefracs and total moles if possible.
#[expect(clippy::type_complexity)]
fn into_molefracs<E: Residual<N, D>>(
self,
eos: &E,
) -> FeosResult<(OVector<D, N>, Option<Moles<D>>)>;
}
// trivial implementations
impl<D: DualNum<f64> + Copy, N: Dim> Composition<D, N> for (OVector<D, N>, Moles<D>)
where
DefaultAllocator: Allocator<N>,
{
fn into_molefracs<E: Residual<N, D>>(
self,
_: &E,
) -> FeosResult<(OVector<D, N>, Option<Moles<D>>)> {
Ok((self.0, Some(self.1)))
}
}
impl<D: DualNum<f64> + Copy, N: Dim> Composition<D, N> for (OVector<D, N>, Option<Moles<D>>)
where
DefaultAllocator: Allocator<N>,
{
fn into_molefracs<E: Residual<N, D>>(
self,
_: &E,
) -> FeosResult<(OVector<D, N>, Option<Moles<D>>)> {
Ok((self.0, self.1))
}
}
// a pure component needs no specification
impl<D: DualNum<f64> + Copy> Composition<D, U1> for () {
fn into_molefracs<E: Residual<U1, D>>(
self,
_: &E,
) -> FeosResult<(OVector<D, U1>, Option<Moles<D>>)> {
Ok(((vector![D::one()]), None))
}
}
impl<D: DualNum<f64> + Copy> Composition<D, Dyn> for () {
fn into_molefracs<E: Residual<Dyn, D>>(
self,
eos: &E,
) -> FeosResult<(OVector<D, Dyn>, Option<Moles<D>>)> {
if eos.components() == 1 {
Ok(((dvector![D::one()]), None))
} else {
Err(FeosError::UndeterminedState(
"The composition needs to be specified for a system with more than one component."
.into(),
))
}
}
}
// a binary mixture can be specified by a scalar (x1)
impl<D: DualNum<f64> + Copy> Composition<D, U2> for D {
fn into_molefracs<E: Residual<U2, D>>(
self,
_: &E,
) -> FeosResult<(OVector<D, U2>, Option<Moles<D>>)> {
Ok(((vector![self, -self + 1.0]), None))
}
}
// this cannot be implemented generically for D due to mising specialization
impl Composition<f64, Dyn> for f64 {
fn into_molefracs<E: Residual>(
self,
eos: &E,
) -> FeosResult<(OVector<f64, Dyn>, Option<Moles>)> {
if eos.components() == 2 {
Ok(((dvector![self, 1.0 - self]), None))
} else {
Err(FeosError::UndeterminedState(format!(
"A scalar ({}) can only be used to specify a binary mixture!",
self
)))
}
}
}
// a pure component can be specified by the total mole number
impl<D: DualNum<f64> + Copy> Composition<D, U1> for Moles<D> {
fn into_molefracs<E: Residual<U1, D>>(
self,
_: &E,
) -> FeosResult<(OVector<D, U1>, Option<Moles<D>>)> {
Ok(((vector![D::one()]), Some(self)))
}
}
impl<D: DualNum<f64> + Copy> Composition<D, Dyn> for Moles<D> {
fn into_molefracs<E: Residual<Dyn, D>>(
self,
eos: &E,
) -> FeosResult<(OVector<D, Dyn>, Option<Moles<D>>)> {
if eos.components() == 1 {
Ok(((dvector![D::one()]), Some(self)))
} else {
Err(FeosError::UndeterminedState(format!(
"A single mole number ({}) can only be used to specify a pure component!",
self.re()
)))
}
}
}
// the mixture can be specified by its molefractions
//
// for a dynamic number of components, it is also possible to specify only the
// N-1 first components
impl<D: DualNum<f64> + Copy, N: Dim> Composition<D, N> for OVector<D, N>
where
DefaultAllocator: Allocator<N>,
{
fn into_molefracs<E: Residual<N, D>>(
self,
eos: &E,
) -> FeosResult<(OVector<D, N>, Option<Moles<D>>)> {
(&self).into_molefracs(eos)
}
}
impl<D: DualNum<f64> + Copy, N: Dim> Composition<D, N> for &OVector<D, N>
where
DefaultAllocator: Allocator<N>,
{
fn into_molefracs<E: Residual<N, D>>(
self,
eos: &E,
) -> FeosResult<(OVector<D, N>, Option<Moles<D>>)> {
let sum = self.sum();
if eos.components() == self.len() {
Ok(((self.clone() / sum), None))
} else if eos.components() == self.len() + 1 {
let mut x = OVector::zeros_generic(N::from_usize(eos.components()), U1);
for i in 0..self.len() {
x[i] = self[i];
}
x[self.len()] = -sum + 1.0;
Ok(((x), None))
} else {
Err(FeosError::UndeterminedState(format!(
"The length of the composition vector ({}) does not match the number of components ({})!",
self.len(),
eos.components()
)))
}
}
}
// the mixture can be specified by its moles
impl<D: DualNum<f64> + Copy, N: Dim> Composition<D, N> for Moles<OVector<D, N>>
where
DefaultAllocator: Allocator<N>,
{
fn into_molefracs<E: Residual<N, D>>(
self,
eos: &E,
) -> FeosResult<(OVector<D, N>, Option<Moles<D>>)> {
(&self).into_molefracs(eos)
}
}
impl<D: DualNum<f64> + Copy, N: Dim> Composition<D, N> for &Moles<OVector<D, N>>
where
DefaultAllocator: Allocator<N>,
{
fn into_molefracs<E: Residual<N, D>>(
self,
eos: &E,
) -> FeosResult<(OVector<D, N>, Option<Moles<D>>)> {
if eos.components() == self.len() {
let total_moles = self.sum();
Ok(((self.convert_to(total_moles)), Some(total_moles)))
} else {
Err(FeosError::UndeterminedState(format!(
"The length of the composition vector ({}) does not match the number of components ({})!",
self.len(),
eos.components()
)))
}
}
}