Skip to content

Commit b6ef166

Browse files
committed
remove some string allocations
1 parent f294d48 commit b6ef166

File tree

25 files changed

+93
-97
lines changed

25 files changed

+93
-97
lines changed

crates/feos-core/src/cubic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl ResidualDyn for PengRobinson {
115115
fn reduced_helmholtz_energy_density_contributions<D: DualNum<f64> + Copy>(
116116
&self,
117117
state: &StateHD<D>,
118-
) -> Vec<(String, D)> {
118+
) -> Vec<(&'static str, D)> {
119119
let density = state.partial_density.sum();
120120
let x = &state.molefracs;
121121
let ak = &self
@@ -140,7 +140,7 @@ impl ResidualDyn for PengRobinson {
140140
* ((v / (v - b)).ln()
141141
- ak_mix / (b * SQRT_2 * 2.0 * state.temperature)
142142
* ((v + b * (1.0 + SQRT_2)) / (v + b * (1.0 - SQRT_2))).ln());
143-
vec![("Peng Robinson".to_string(), f)]
143+
vec![("Peng Robinson", f)]
144144
}
145145
}
146146

crates/feos-core/src/density_iteration.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ where
3030
// Implicit differentiation
3131
let mut density = D::from(density);
3232
for _ in 0..D::NDERIV {
33-
let (_, p, dp_drho) = eos._p_dpdrho(t, density, molefracs);
33+
let (_, p, dp_drho) = eos.p_dpdrho(t, density, molefracs);
3434
density -= (p - pressure) / dp_drho;
3535
}
3636
Ok(Density::from_reduced(density))
@@ -125,7 +125,7 @@ where
125125
let mut iterations = 0;
126126
'iteration: for k in 0..maxiter {
127127
iterations += 1;
128-
let (_, mut p, mut dp_drho) = eos._p_dpdrho(temperature, rho, molefracs);
128+
let (_, mut p, mut dp_drho) = eos.p_dpdrho(temperature, rho, molefracs);
129129

130130
// attempt to correct for poor initial density rho_init
131131
if dp_drho.is_sign_negative() && k == 0 {
@@ -134,7 +134,7 @@ where
134134
} else {
135135
(1.1 * initial_density).min(maxdensity)
136136
};
137-
let p_ = eos._p_dpdrho(temperature, rho, molefracs);
137+
let p_ = eos.p_dpdrho(temperature, rho, molefracs);
138138
p = p_.0;
139139
dp_drho = p_.1;
140140
}
@@ -149,7 +149,7 @@ where
149149

150150
// correction for instable region
151151
if dp_drho.is_sign_negative() && k < maxiter {
152-
let (_, _, d2pdrho2) = eos._p_dpdrho_d2pdrho2(temperature, rho, molefracs);
152+
let (_, _, d2pdrho2) = eos.p_dpdrho_d2pdrho2(temperature, rho, molefracs);
153153

154154
if rho > 0.85 * maxdensity {
155155
let (sp_p, sp_rho) =
@@ -258,7 +258,7 @@ where
258258
}
259259

260260
for _ in 0..maxiter {
261-
let (p, dpdrho, d2pdrho2) = eos._p_dpdrho_d2pdrho2(temperature, rho, molefracs);
261+
let (p, dpdrho, d2pdrho2) = eos.p_dpdrho_d2pdrho2(temperature, rho, molefracs);
262262

263263
let mut delta_rho = -dpdrho / d2pdrho2;
264264
if delta_rho.abs() > 0.05 * maxdensity {

crates/feos-core/src/equation_of_state/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<I: Clone, R: ResidualDyn> ResidualDyn for EquationOfState<Vec<I>, R> {
5353
fn reduced_helmholtz_energy_density_contributions<D: DualNum<f64> + Copy>(
5454
&self,
5555
state: &StateHD<D>,
56-
) -> Vec<(String, D)> {
56+
) -> Vec<(&'static str, D)> {
5757
self.residual
5858
.reduced_helmholtz_energy_density_contributions(state)
5959
}
@@ -105,8 +105,7 @@ where
105105
}
106106
}
107107

108-
/// Ideal gas Helmholtz energy contribution that allows calculating derivatives
109-
/// with respect to model parameters.
108+
/// Ideal gas Helmholtz energy contribution.
110109
pub trait IdealGas<D = f64> {
111110
/// Implementation of an ideal gas model in terms of the
112111
/// logarithm of the cubic thermal de Broglie wavelength

crates/feos-core/src/equation_of_state/residual.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub trait ResidualDyn {
5656
fn reduced_helmholtz_energy_density_contributions<D: DualNum<f64> + Copy>(
5757
&self,
5858
state: &StateHD<D>,
59-
) -> Vec<(String, D)>;
59+
) -> Vec<(&'static str, D)>;
6060
}
6161

6262
impl<C: Deref<Target = T> + Clone, T: ResidualDyn, D: DualNum<f64> + Copy> Residual<Dyn, D> for C {
@@ -77,7 +77,7 @@ impl<C: Deref<Target = T> + Clone, T: ResidualDyn, D: DualNum<f64> + Copy> Resid
7777
fn reduced_helmholtz_energy_density_contributions(
7878
&self,
7979
state: &StateHD<D, Dyn>,
80-
) -> Vec<(String, D)> {
80+
) -> Vec<(&'static str, D)> {
8181
ResidualDyn::reduced_helmholtz_energy_density_contributions(self.deref(), state)
8282
}
8383
}
@@ -116,9 +116,9 @@ impl<T: ResidualConst<N, D>, const N: usize, D: DualNum<f64> + Copy> Residual<Co
116116
fn reduced_helmholtz_energy_density_contributions(
117117
&self,
118118
state: &StateHD<D, Const<N>>,
119-
) -> Vec<(String, D)> {
119+
) -> Vec<(&'static str, D)> {
120120
vec![(
121-
T::NAME.into(),
121+
T::NAME,
122122
T::reduced_residual_helmholtz_energy_density(self, state),
123123
)]
124124
}
@@ -156,7 +156,7 @@ where
156156
fn reduced_helmholtz_energy_density_contributions(
157157
&self,
158158
state: &StateHD<D, N>,
159-
) -> Vec<(String, D)>;
159+
) -> Vec<(&'static str, D)>;
160160

161161
/// Evaluate the residual reduced Helmholtz energy density $\beta f^\mathrm{res}$.
162162
fn reduced_residual_helmholtz_energy_density(&self, state: &StateHD<D, N>) -> D {
@@ -172,7 +172,7 @@ where
172172
temperature: D,
173173
molar_volume: D,
174174
molefracs: &OVector<D, N>,
175-
) -> Vec<(String, D)> {
175+
) -> Vec<(&'static str, D)> {
176176
let state = StateHD::new(temperature, molar_volume, molefracs);
177177
self.reduced_helmholtz_energy_density_contributions(&state)
178178
.into_iter()
@@ -321,7 +321,7 @@ where
321321
dc_dt
322322
}
323323

324-
fn _p_dpdrho(&self, temperature: D, density: D, molefracs: &OVector<D, N>) -> (D, D, D) {
324+
fn p_dpdrho(&self, temperature: D, density: D, molefracs: &OVector<D, N>) -> (D, D, D) {
325325
let molar_volume = density.recip();
326326
let (a, da, d2a) = second_derivative(
327327
partial2(
@@ -341,7 +341,7 @@ where
341341
)
342342
}
343343

344-
fn _p_dpdrho_d2pdrho2(
344+
fn p_dpdrho_d2pdrho2(
345345
&self,
346346
temperature: D,
347347
density: D,
@@ -375,7 +375,7 @@ where
375375
) -> (D, OVector<D, N>, OVector<D, N>, OMatrix<D, N, N>)
376376
where
377377
N: Gradients,
378-
DefaultAllocator: Allocator<N> + Allocator<N, N>,
378+
DefaultAllocator: Allocator<N, N>,
379379
{
380380
let (f_res, mu_res, dmu_res) = N::hessian(
381381
|rho, &t| {
@@ -402,7 +402,6 @@ where
402402
) -> (D, OVector<D, N>, D, OVector<D, N>)
403403
where
404404
N: Gradients,
405-
DefaultAllocator: Allocator<N> + Allocator<N, N>,
406405
{
407406
let (_, mu_res, a_res_v, mu_res_v) = N::partial_hessian(
408407
|x, v, &t| self.lift().residual_molar_helmholtz_energy(t, v, &x),
@@ -463,7 +462,7 @@ impl ResidualDyn for NoResidual {
463462
fn reduced_helmholtz_energy_density_contributions<D: DualNum<f64> + Copy>(
464463
&self,
465464
_: &StateHD<D>,
466-
) -> Vec<(String, D)> {
465+
) -> Vec<(&'static str, D)> {
467466
vec![]
468467
}
469468
}

crates/feos-core/src/phase_equilibria/vle_pure.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::errors::{FeosError, FeosResult};
55
use crate::state::{Contributions, DensityInitialization, State};
66
use crate::{ReferenceSystem, SolverOptions, TemperatureOrPressure, Verbosity};
77
use nalgebra::allocator::Allocator;
8-
use nalgebra::{DVector, DefaultAllocator, Dim, U1, dvector};
8+
use nalgebra::{DVector, DefaultAllocator, Dim, dvector};
99
use num_dual::{DualNum, DualStruct, Gradients};
1010
use quantity::{Density, Pressure, RGAS, Temperature};
1111

@@ -37,7 +37,7 @@ impl<E: Residual> PhaseEquilibrium<E, 2> {
3737

3838
impl<E: Residual<N, D>, N: Gradients, D: DualNum<f64> + Copy> PhaseEquilibrium<E, 2, N, D>
3939
where
40-
DefaultAllocator: Allocator<N> + Allocator<N, N> + Allocator<U1, N>,
40+
DefaultAllocator: Allocator<N>,
4141
{
4242
/// Calculate a phase equilibrium for a pure component
4343
/// and given temperature.
@@ -89,8 +89,8 @@ where
8989
for _ in 0..D::NDERIV {
9090
let v_l = liquid_density.recip();
9191
let v_v = vapor_density.recip();
92-
let (f_l, p_l, dp_l) = eos._p_dpdrho(t, liquid_density, &x);
93-
let (f_v, p_v, dp_v) = eos._p_dpdrho(t, vapor_density, &x);
92+
let (f_l, p_l, dp_l) = eos.p_dpdrho(t, liquid_density, &x);
93+
let (f_v, p_v, dp_v) = eos.p_dpdrho(t, vapor_density, &x);
9494
pressure = -(f_l * v_l - f_v * v_v + t * (v_v / v_l).ln()) / (v_l - v_v);
9595
liquid_density += (pressure - p_l) / dp_l;
9696
vapor_density += (pressure - p_v) / dp_v;
@@ -133,8 +133,8 @@ where
133133

134134
for i in 1..=max_iter {
135135
// calculate properties
136-
let (f_l_res, p_l, p_rho_l) = eos._p_dpdrho(temperature, liquid_density, &x);
137-
let (f_v_res, p_v, p_rho_v) = eos._p_dpdrho(temperature, vapor_density, &x);
136+
let (f_l_res, p_l, p_rho_l) = eos.p_dpdrho(temperature, liquid_density, &x);
137+
let (f_v_res, p_v, p_rho_v) = eos.p_dpdrho(temperature, vapor_density, &x);
138138

139139
// Estimate the new pressure
140140
let v_v = vapor_density.recip();

crates/feos-core/src/state/properties.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,15 @@ where
238238
&self,
239239
component: usize,
240240
contributions: Contributions,
241-
) -> Vec<(String, MolarEnergy<D>)> {
241+
) -> Vec<(&'static str, MolarEnergy<D>)> {
242242
let t = Dual::from_re(self.temperature.into_reduced());
243243
let v = Dual::from_re(self.density.into_reduced().recip());
244244
let mut x = self.molefracs.map(Dual::from_re);
245245
x[component].eps = D::one();
246246
let mut res = Vec::new();
247247
if let Contributions::IdealGas | Contributions::Total = contributions {
248248
res.push((
249-
self.eos.ideal_gas_model().into(),
249+
self.eos.ideal_gas_model(),
250250
self.eos.ideal_gas_molar_helmholtz_energy(t, v, &x),
251251
));
252252
}

crates/feos-core/src/state/residual_properties.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,9 @@ where
542542
}
543543

544544
/// Molar Helmholtz energy $a^\text{res}$ evaluated for each residual contribution of the equation of state.
545-
pub fn residual_molar_helmholtz_energy_contributions(&self) -> Vec<(String, MolarEnergy<D>)> {
545+
pub fn residual_molar_helmholtz_energy_contributions(
546+
&self,
547+
) -> Vec<(&'static str, MolarEnergy<D>)> {
546548
let residual_contributions = self.eos.molar_helmholtz_energy_contributions(
547549
self.temperature.into_reduced(),
548550
self.density.into_reduced().recip(),
@@ -559,7 +561,7 @@ where
559561
pub fn residual_chemical_potential_contributions(
560562
&self,
561563
component: usize,
562-
) -> Vec<(String, MolarEnergy<D>)> {
564+
) -> Vec<(&'static str, MolarEnergy<D>)> {
563565
let t = Dual::from_re(self.temperature.into_reduced());
564566
let v = Dual::from_re(self.temperature.into_reduced());
565567
let mut x = self.molefracs.map(Dual::from_re);
@@ -576,7 +578,7 @@ where
576578
}
577579

578580
/// Pressure $p$ evaluated for each contribution of the equation of state.
579-
pub fn pressure_contributions(&self) -> Vec<(String, Pressure<D>)> {
581+
pub fn pressure_contributions(&self) -> Vec<(&'static str, Pressure<D>)> {
580582
let t = Dual::from_re(self.temperature.into_reduced());
581583
let v = Dual::from_re(self.density.into_reduced().recip()).derivative();
582584
let x = self.molefracs.map(Dual::from_re);
@@ -585,7 +587,7 @@ where
585587
.lift()
586588
.molar_helmholtz_energy_contributions(t, v, &x);
587589
let mut res = Vec::with_capacity(contributions.len() + 1);
588-
res.push(("Ideal gas".into(), self.density * RGAS * self.temperature));
590+
res.push(("Ideal gas", self.density * RGAS * self.temperature));
589591
for (s, v) in contributions {
590592
res.push((s, Pressure::from_reduced(-v.eps)));
591593
}

crates/feos-derive/src/residual.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn impl_residual(
5959
#(#compute_max_density,)*
6060
}
6161
}
62-
fn reduced_helmholtz_energy_density_contributions<D: DualNum<f64> + Copy>(&self, state: &StateHD<D>) -> Vec<(String, D)> {
62+
fn reduced_helmholtz_energy_density_contributions<D: DualNum<f64> + Copy>(&self, state: &StateHD<D>) -> Vec<(&'static str, D)> {
6363
match self {
6464
#(#reduced_helmholtz_energy_density_contributions,)*
6565
}

crates/feos-dft/src/adsorption/pore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl ResidualDyn for Helium {
309309
fn reduced_helmholtz_energy_density_contributions<D: DualNum<f64> + Copy>(
310310
&self,
311311
state: &StateHD<D>,
312-
) -> Vec<(String, D)> {
312+
) -> Vec<(&'static str, D)> {
313313
self.evaluate_bulk(state)
314314
}
315315
}

crates/feos-dft/src/functional.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ pub trait HelmholtzEnergyFunctional: Residual {
247247
i
248248
}
249249

250-
fn evaluate_bulk<D: DualNum<f64> + Copy>(&self, state: &StateHD<D>) -> Vec<(String, D)> {
251-
let mut res: Vec<(String, D)> = self
250+
fn evaluate_bulk<D: DualNum<f64> + Copy>(&self, state: &StateHD<D>) -> Vec<(&'static str, D)> {
251+
let mut res: Vec<_> = self
252252
.contributions()
253-
.map(|c| (c.name().to_string(), c.bulk_helmholtz_energy_density(state)))
253+
.map(|c| (c.name(), c.bulk_helmholtz_energy_density(state)))
254254
.collect();
255255
res.push((
256-
self.ideal_chain_contribution().name(),
256+
"Ideal chain",
257257
self.ideal_chain_contribution()
258258
.bulk_helmholtz_energy_density(&state.partial_density),
259259
));

0 commit comments

Comments
 (0)