Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builder patern #20

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! builder utility

use std::num::NonZeroUsize;

#[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Serialize};

/// Type of generation
#[non_exhaustive]
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub enum GenType<'rng, Rng: rand::Rng + ?Sized> {
/// Cold generation all elements are set to the default
Cold,
/// Random determinist
#[cfg_attr(feature = "serde-serialize", serde(skip_deserializing))]
HotDeterminist(&'rng mut Rng),
/// Random determinist but own the RNG (for instance the result of `clone`)
HotDeterministOwned(Box<Rng>),
/// Random threaded (non determinist)
HotThreaded(NonZeroUsize),
}
5 changes: 5 additions & 0 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ use super::{
CMatrix3, Complex, Real, Vector8, I,
};

mod link_matrix_builder;
pub use link_matrix_builder::*;
mod e_field_builder;
pub use e_field_builder::*;

/// Adjoint representation of SU(3), it is su(3) (i.e. the lie algebra).
/// See [`su3::GENERATORS`] to view the order of generators.
/// Note that the generators are normalize such that `Tr[T^a T^b] = \delta^{ab} / 2`
Expand Down
75 changes: 75 additions & 0 deletions src/field/e_field_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::borrow::Cow;
use std::fmt::Debug;
use std::num::NonZeroUsize;

use rand::distributions::Distribution;
use rand_distr::Normal;
#[cfg(feature = "serde-serialize")]
use serde::Serialize;

use super::EField;
use crate::builder::GenType;
use crate::lattice::LatticeCyclic;
use crate::{CMatrix3, Real};

#[derive(Debug, Clone)]
enum DistributionForBuilder<'dis, Dis: Distribution<Real> + ?Sized> {
Default(Normal<Real>),
Given(&'dis Dis), // TODO box dyn
}

impl<'d, Dis: Distribution<Real> + ?Sized> Default for DistributionForBuilder<'d, Dis> {
fn default() -> Self {
Self::Default(Normal::new(0_f64, 0.5_f64).unwrap())
}
}

#[derive(Clone, Debug)]
pub struct EFieldProceduralBuilder<
'rng,
'lat,
'dis,
Rng: rand::Rng + ?Sized,
Dis: Distribution<Real> + ToOwned + ?Sized,
const D: usize,
> {
gen_type: GenType<'rng, Rng>,
lattice: Cow<'lat, LatticeCyclic<D>>,
distribution: DistributionForBuilder<'dis, Dis>,
}

/*
impl<'r, 'l, 'd, Rng: rand::Rng + ?Sized, Dis, const D: usize> Debug
for EFieldProceduralBuilder<'r, 'l, 'd, Rng, Dis, D>
where
Dis: Distribution<Real> + ?Sized + ToOwned + Debug,
Rng: Debug,
Dis::Owned: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EFieldProceduralBuilder")
.field("gen_type", &self.gen_type)
.field("lattice", &self.lattice)
.field("distribution", &self.distribution)
.finish()
}
}
*/

impl<
'rng,
'lat,
'dis,
Rng: rand::Rng + ?Sized,
Dis: Distribution<Real> + ToOwned + ?Sized,
const D: usize,
> EFieldProceduralBuilder<'rng, 'lat, 'dis, Rng, Dis, D>
{
pub fn new(lattice: impl Into<Cow<'lat, LatticeCyclic<D>>>) -> Self {
Self {
gen_type: GenType::Cold,
lattice: lattice.into(),
distribution: DistributionForBuilder::default(),
}
}
}
Loading