Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
prehner committed Apr 22, 2024
1 parent 5d5aa63 commit aa7b237
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
59 changes: 26 additions & 33 deletions src/association/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct AssociationSite<A> {
parameters: A,
}

impl<A> AssociationSite<A> {
impl<A: Clone + Default> AssociationSite<A> {
fn new(assoc_comp: usize, site_index: usize, n: f64, parameters: A) -> Self {
Self {
assoc_comp,
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct AssociationRecord<A> {
pub nc: f64,
}

impl<A> AssociationRecord<A> {
impl<A: Clone + Default> AssociationRecord<A> {
pub fn new(parameters: A, na: f64, nb: f64, nc: f64) -> Self {
Self {
parameters,
Expand Down Expand Up @@ -81,29 +81,22 @@ impl<A: fmt::Display> fmt::Display for AssociationRecord<A> {
}

#[derive(Clone, Serialize, Deserialize, Default)]
#[serde(from = "AssociationRecordsSerde")]
#[serde(into = "AssociationRecordsSerde")]
pub struct AssociationRecords(Vec<AssociationRecord>);
#[serde(from = "AssociationRecordsSerde<A>")]
#[serde(into = "AssociationRecordsSerde<A>")]
pub struct AssociationRecords<A: Clone + Default>(Vec<AssociationRecord<A>>);

impl AssociationRecords {
impl<A: Clone + Default> AssociationRecords<A> {
pub fn new(
kappa_ab: Option<f64>,
epsilon_k_ab: Option<f64>,
parameters: Option<A>,
na: Option<f64>,
nb: Option<f64>,
nc: Option<f64>,
association_records: Option<Vec<AssociationRecord>>,
association_records: Option<Vec<AssociationRecord<A>>>,
) -> Self {
let mut association_records = association_records.unwrap_or_default();
if kappa_ab.is_some()
|| epsilon_k_ab.is_some()
|| na.is_some()
|| nb.is_some()
|| nc.is_some()
{
if let Some(parameters) = parameters {
association_records.push(AssociationRecord::new(
kappa_ab.unwrap_or_default(),
epsilon_k_ab.unwrap_or_default(),
parameters,
na.unwrap_or_default(),
nb.unwrap_or_default(),
nc.unwrap_or_default(),
Expand All @@ -113,27 +106,27 @@ impl AssociationRecords {
}
}

impl Deref for AssociationRecords {
type Target = Vec<AssociationRecord>;
impl<A: Clone + Default> Deref for AssociationRecords<A> {
type Target = Vec<AssociationRecord<A>>;

fn deref(&self) -> &Vec<AssociationRecord> {
fn deref(&self) -> &Vec<AssociationRecord<A>> {
&self.0
}
}

impl DerefMut for AssociationRecords {
fn deref_mut(&mut self) -> &mut Vec<AssociationRecord> {
impl<A: Clone + Default> DerefMut for AssociationRecords<A> {
fn deref_mut(&mut self) -> &mut Vec<AssociationRecord<A>> {
&mut self.0
}
}

impl FromIterator<AssociationRecord> for AssociationRecords {
fn from_iter<T: IntoIterator<Item = AssociationRecord>>(iter: T) -> Self {
impl<A: Clone + Default> FromIterator<AssociationRecord<A>> for AssociationRecords<A> {
fn from_iter<T: IntoIterator<Item = AssociationRecord<A>>>(iter: T) -> Self {
Self(Vec::from_iter(iter))
}
}

impl fmt::Display for AssociationRecords {
impl<A: Clone + Default + fmt::Display> fmt::Display for AssociationRecords<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[")?;
for (i, record) in self.0.iter().enumerate() {
Expand All @@ -147,16 +140,16 @@ impl fmt::Display for AssociationRecords {
}

#[derive(Serialize, Deserialize)]
struct AssociationRecordsSerde {
struct AssociationRecordsSerde<A> {
#[serde(flatten)]
single: Option<AssociationRecord>,
single: Option<AssociationRecord<A>>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
association_records: Vec<AssociationRecord>,
association_records: Vec<AssociationRecord<A>>,
}

impl From<AssociationRecordsSerde> for AssociationRecords {
fn from(value: AssociationRecordsSerde) -> Self {
impl<A: Clone + Default> From<AssociationRecordsSerde<A>> for AssociationRecords<A> {
fn from(value: AssociationRecordsSerde<A>) -> Self {
let mut association_records = value.association_records;
if let Some(record) = value.single {
association_records.push(record);
Expand All @@ -165,8 +158,8 @@ impl From<AssociationRecordsSerde> for AssociationRecords {
}
}

impl From<AssociationRecords> for AssociationRecordsSerde {
fn from(value: AssociationRecords) -> Self {
impl<A: Clone + Default> From<AssociationRecords<A>> for AssociationRecordsSerde<A> {
fn from(value: AssociationRecords<A>) -> Self {
let mut association_records = value.0;
let single = if association_records.len() == 1 {
association_records.pop()
Expand Down Expand Up @@ -336,7 +329,7 @@ impl<P: AssociationStrength> Association<P> {
}

pub trait AssociationStrength: HardSphereProperties {
type Record: Copy;
type Record: Copy + Default;
type BinaryRecord: Copy;

fn association_strength<D: DualNum<f64> + Copy>(
Expand Down
22 changes: 10 additions & 12 deletions src/pcsaft/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ impl FromSegments<f64> for PcSaftRecord {
.flat_map(|(s, n)| {
s.association_records.iter().map(move |record| {
AssociationRecord::new(
record.parameters.kappa_ab,
record.parameters.epsilon_k_ab,
record.parameters,
record.na * n,
record.nb * n,
record.nc * n,
Expand Down Expand Up @@ -216,18 +215,17 @@ impl PcSaftRecord {
na: Option<f64>,
nb: Option<f64>,
nc: Option<f64>,
association_records: Option<Vec<AssociationRecord>>,
association_records: Option<Vec<AssociationRecord<PcSaftAssociationRecord>>>,
viscosity: Option<[f64; 4]>,
diffusion: Option<[f64; 5]>,
thermal_conductivity: Option<[f64; 4]>,
) -> PcSaftRecord {
let association_records = AssociationRecords::new(
PcSaftAssociationRecord::new(kappa_ab, epsilon_k_ab),
na,
nb,
nc,
association_records,
);
let assoc = if let (Some(kappa_ab), Some(epsilon_k_ab)) = (kappa_ab, epsilon_k_ab) {
Some(PcSaftAssociationRecord::new(kappa_ab, epsilon_k_ab))
} else {
None
};
let association_records = AssociationRecords::new(assoc, na, nb, nc, association_records);
PcSaftRecord {
m,
sigma,
Expand Down Expand Up @@ -614,8 +612,8 @@ impl PcSaftParameters {
o,
"\n|{}|{}|{}|{}|{}|{}|",
component,
association.kappa_ab,
association.epsilon_k_ab,
association.parameters.kappa_ab,
association.parameters.epsilon_k_ab,
association.na,
association.nb,
association.nc
Expand Down

0 comments on commit aa7b237

Please sign in to comment.