-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy patheos.rs
More file actions
210 lines (195 loc) · 6.17 KB
/
eos.rs
File metadata and controls
210 lines (195 loc) · 6.17 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
use super::implement;
use quote::quote;
use syn::DeriveInput;
// possible additional traits to implement
const OPT_IMPLS: [&str; 2] = ["molar_weight", "entropy_scaling"];
pub(crate) fn expand_equation_of_state(
input: DeriveInput,
) -> syn::Result<proc_macro2::TokenStream> {
let variants = match input.data {
syn::Data::Enum(syn::DataEnum { ref variants, .. }) => variants,
_ => panic!("this derive macro only works on enums"),
};
let eos = impl_equation_of_state(variants);
let molar_weight = impl_molar_weight(variants)?;
let entropy_scaling = impl_entropy_scaling(variants)?;
Ok(quote! {
#eos
#molar_weight
#entropy_scaling
})
}
fn impl_equation_of_state(
variants: &syn::punctuated::Punctuated<syn::Variant, syn::token::Comma>,
) -> proc_macro2::TokenStream {
let components = variants.iter().map(|v| {
let name = &v.ident;
quote! {
Self::#name(eos) => eos.components()
}
});
let compute_max_density = variants.iter().map(|v| {
let name = &v.ident;
quote! {
Self::#name(eos) => eos.compute_max_density(moles)
}
});
let subset = variants.iter().map(|v| {
let name = &v.ident;
quote! {
Self::#name(eos) => Self::#name(eos.subset(component_list))
}
});
let residual = variants.iter().map(|v| {
let name = &v.ident;
quote! {
Self::#name(eos) => eos.residual()
}
});
let ideal_gas = variants.iter().map(|v| {
let name = &v.ident;
quote! {
Self::#name(eos) => eos.ideal_gas()
}
});
quote! {
impl EquationOfState for EosVariant {
fn components(&self) -> usize {
match self {
#(#components,)*
}
}
fn compute_max_density(&self, moles: &Array1<f64>) -> f64 {
match self {
#(#compute_max_density,)*
}
}
fn subset(&self, component_list: &[usize]) -> Self {
match self {
#(#subset,)*
}
}
fn residual(&self) -> &[Box<dyn HelmholtzEnergy>] {
match self {
#(#residual,)*
}
}
fn ideal_gas(&self) -> &dyn IdealGasContribution {
match self {
#(#ideal_gas,)*
}
}
}
}
}
fn impl_molar_weight(
variants: &syn::punctuated::Punctuated<syn::Variant, syn::token::Comma>,
) -> syn::Result<proc_macro2::TokenStream> {
let mut molar_weight = Vec::new();
for v in variants.iter() {
if implement("molar_weight", v, &OPT_IMPLS)? {
let name = &v.ident;
molar_weight.push(quote! {
Self::#name(eos) => eos.molar_weight()
});
}
}
Ok(quote! {
impl MolarWeight for EosVariant {
fn molar_weight(&self) -> SIArray1 {
match self {
#(#molar_weight,)*
_ => unimplemented!()
}
}
}
})
}
fn impl_entropy_scaling(
variants: &syn::punctuated::Punctuated<syn::Variant, syn::token::Comma>,
) -> syn::Result<proc_macro2::TokenStream> {
let mut etar = Vec::new();
let mut etac = Vec::new();
let mut dr = Vec::new();
let mut dc = Vec::new();
let mut thcr = Vec::new();
let mut thcc = Vec::new();
for v in variants.iter() {
if implement("entropy_scaling", v, &OPT_IMPLS)? {
let name = &v.ident;
etar.push(quote! {
Self::#name(eos) => eos.viscosity_reference(temperature, volume, moles)
});
etac.push(quote! {
Self::#name(eos) => eos.viscosity_correlation(s_res, x)
});
dr.push(quote! {
Self::#name(eos) => eos.diffusion_reference(temperature, volume, moles)
});
dc.push(quote! {
Self::#name(eos) => eos.diffusion_correlation(s_res, x)
});
thcr.push(quote! {
Self::#name(eos) => eos.thermal_conductivity_reference(temperature, volume, moles)
});
thcc.push(quote! {
Self::#name(eos) => eos.thermal_conductivity_correlation(s_res, x)
});
}
}
Ok(quote! {
impl EntropyScaling for EosVariant {
fn viscosity_reference(
&self,
temperature: SINumber,
volume: SINumber,
moles: &SIArray1,
) -> EosResult<SINumber> {
match self {
#(#etar,)*
_ => unimplemented!(),
}
}
fn viscosity_correlation(&self, s_res: f64, x: &Array1<f64>) -> EosResult<f64> {
match self {
#(#etac,)*
_ => unimplemented!(),
}
}
fn diffusion_reference(
&self,
temperature: SINumber,
volume: SINumber,
moles: &SIArray1,
) -> EosResult<SINumber> {
match self {
#(#dr,)*
_ => unimplemented!(),
}
}
fn diffusion_correlation(&self, s_res: f64, x: &Array1<f64>) -> EosResult<f64> {
match self {
#(#dc,)*
_ => unimplemented!(),
}
}
fn thermal_conductivity_reference(
&self,
temperature: SINumber,
volume: SINumber,
moles: &SIArray1,
) -> EosResult<SINumber> {
match self {
#(#thcr,)*
_ => unimplemented!(),
}
}
fn thermal_conductivity_correlation(&self, s_res: f64, x: &Array1<f64>) -> EosResult<f64> {
match self {
#(#thcc,)*
_ => unimplemented!(),
}
}
}
})
}