Skip to content

Commit

Permalink
Make Particle::name public
Browse files Browse the repository at this point in the history
There is no need to have getter and setter if they are not enforcing an invariant
  • Loading branch information
Guillaume Fraux committed Sep 18, 2017
1 parent bbecbcc commit 8e92ae5
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 48 deletions.
2 changes: 1 addition & 1 deletion examples/mc_npt_spce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn get_system() -> System {
let h = 0.42380;
let o = - 2.0 * h;
for particle in system.particles_mut() {
match particle.name() {
match particle.name.as_ref() {
"H" => particle.charge = h,
"O" => particle.charge = o,
_ => panic!("Unknown particle name in charge setting."),
Expand Down
8 changes: 4 additions & 4 deletions src/core/src/energy/global/ewald.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,9 +838,9 @@ mod tests {
assert!(system.molecules().len() == 1);

for particle in system.particles_mut() {
if particle.name() == "O" {
if particle.name == "O" {
particle.charge = -0.8476;
} else if particle.name() == "H" {
} else if particle.name == "H" {
particle.charge = 0.4238;
}
}
Expand Down Expand Up @@ -1079,9 +1079,9 @@ mod tests {
assert!(system.molecules().len() == 2);

for particle in system.particles_mut() {
if particle.name() == "O" {
if particle.name == "O" {
particle.charge = -0.8476;
} else if particle.name() == "H" {
} else if particle.name == "H" {
particle.charge = 0.4238;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/energy/global/wolf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ mod tests {
assert!(system.molecules().len() == 2);

for particle in system.particles_mut() {
if particle.name() == "O" {
if particle.name == "O" {
particle.charge = -0.8476;
} else if particle.name() == "H" {
} else if particle.name == "H" {
particle.charge = 0.4238;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/sys/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ mod tests {
system.set_coulomb_potential(Box::new(Wolf::new(8.0)));

for atom in system.particles_mut() {
if atom.name() == "O" {
if atom.name == "O" {
atom.charge = -0.5;
} else if atom.name() == "H" {
} else if atom.name == "H" {
atom.charge = 0.5;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/src/sys/chfl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub trait ToChemfiles {
impl ToChemfiles for Particle {
type Output = chemfiles::Atom;
fn to_chemfiles(&self) -> TrajectoryResult<chemfiles::Atom> {
let mut atom = try!(chemfiles::Atom::new(self.name()));
let mut atom = try!(chemfiles::Atom::new(&*self.name));
try!(atom.set_mass(self.mass));
return Ok(atom);
}
Expand Down Expand Up @@ -534,9 +534,9 @@ H 2.172669 -0.348524 0.000051

assert!(molecule.dihedrals().is_empty());

assert_eq!(atoms[0].name(), "O");
assert_eq!(atoms[1].name(), "H");
assert_eq!(atoms[2].name(), "H");
assert_eq!(atoms[0].name, "O");
assert_eq!(atoms[1].name, "H");
assert_eq!(atoms[2].name, "H");

// This is only a simple regression test on the moltype function. Feel
// free to change the value if the molecule type algorithm change.
Expand Down
6 changes: 3 additions & 3 deletions src/core/src/sys/config/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,9 @@ mod tests {
configuration.add_particle(particle("H"));

assert_eq!(configuration.size(), 3);
assert_eq!(configuration.particle(0).name(), "O");
assert_eq!(configuration.particle(1).name(), "H");
assert_eq!(configuration.particle(2).name(), "H");
assert_eq!(configuration.particle(0).name, "O");
assert_eq!(configuration.particle(1).name, "H");
assert_eq!(configuration.particle(2).name, "H");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/sys/config/molecules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub fn molecule_type(molecule: &Molecule, particles: &[Particle]) -> u64 {
let mut hasher = DefaultHasher::new();
molecule.cached_hash.hash(&mut hasher);
for particle in particles {
particle.name().hash(&mut hasher);
particle.name.hash(&mut hasher);
}
hasher.finish()
}
Expand Down
24 changes: 5 additions & 19 deletions src/core/src/sys/config/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ impl fmt::Display for ParticleKind {
/// contained, so that it will be easy to send data between parallels processes.
#[derive(Clone, Debug)]
pub struct Particle {
/// Particle name. This one is not public, as we always want to get &str,
/// and to use either `String` of `&str` to set it.
name: String,
/// Particle name.
pub name: String,
/// Particle kind, an index for potentials lookup
pub(in ::sys) kind: ParticleKind,
/// Particle mass
Expand Down Expand Up @@ -67,16 +66,6 @@ impl Particle {
velocity: Vector3D::zero()
}
}

/// Get the particle name
pub fn name(&self) -> &str {
&self.name
}

/// Set the particle name to `name`
pub fn set_name<'a, S>(&mut self, name: S) where S: Into<&'a str> {
self.name = String::from(name.into());
}
}

#[cfg(test)]
Expand All @@ -92,23 +81,20 @@ mod tests {

#[test]
fn name() {
let mut particle = Particle::new("");
assert_eq!(particle.name(), "");
let particle = Particle::new("");
assert_eq!(particle.name, "");

assert_eq!(particle.mass, 0.0);
assert_eq!(particle.charge, 0.0);
assert_eq!(particle.kind, ParticleKind::invalid());
assert_eq!(particle.position, Vector3D::new(0.0, 0.0, 0.0));
assert_eq!(particle.velocity, Vector3D::new(0.0, 0.0, 0.0));

particle.set_name("H");
assert_eq!(particle.name(), "H");
}

#[test]
fn with_position() {
let particle = Particle::with_position("", Vector3D::new(1.0, 2.0, 3.0));
assert_eq!(particle.name(), "");
assert_eq!(particle.name, "");
assert_eq!(particle.position, Vector3D::new(1.0, 2.0, 3.0));

assert_eq!(particle.mass, 0.0);
Expand Down
14 changes: 7 additions & 7 deletions src/core/src/sys/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl System {
/// Insert a particle at the end of the internal list.
pub fn add_particle(&mut self, mut particle: Particle) {
if particle.kind == ParticleKind::invalid() {
particle.kind = self.get_kind(particle.name());
particle.kind = self.get_kind(&particle.name);
}
self.configuration.add_particle(particle);
}
Expand Down Expand Up @@ -197,7 +197,7 @@ impl System {
if pairs.is_empty() {
warn_once!(
"No potential defined for the pair ({}, {})",
self.particle(i).name(), self.particle(j).name()
self.particle(i).name, self.particle(j).name
);
}
return pairs;
Expand All @@ -217,7 +217,7 @@ impl System {
if bonds.is_empty() {
warn_once!(
"No potential defined for the bond ({}, {})",
self.particle(i).name(), self.particle(j).name()
self.particle(i).name, self.particle(j).name
);
}
return bonds;
Expand All @@ -233,8 +233,8 @@ impl System {
if angles.is_empty() {
warn_once!(
"No potential defined for the angle ({}, {}, {})",
self.particle(i).name(), self.particle(j).name(),
self.particle(k).name()
self.particle(i).name, self.particle(j).name,
self.particle(k).name
);
}
return angles;
Expand All @@ -251,8 +251,8 @@ impl System {
if dihedrals.is_empty() {
warn_once!(
"No potential defined for the dihedral angle ({}, {}, {}, {})",
self.particle(i).name(), self.particle(j).name(),
self.particle(k).name(), self.particle(m).name()
self.particle(i).name, self.particle(j).name,
self.particle(k).name, self.particle(m).name
);
}
return dihedrals;
Expand Down
6 changes: 3 additions & 3 deletions src/core/src/utils/xyz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ mod tests {
O 0 0 1.5");
assert_eq!(system.size(), 3);

assert_eq!(system.particle(0).name(), "O");
assert_eq!(system.particle(1).name(), "C");
assert_eq!(system.particle(2).name(), "O");
assert_eq!(system.particle(0).name, "O");
assert_eq!(system.particle(1).name, "C");
assert_eq!(system.particle(2).name, "O");

assert_eq!(system.particle(0).position, Vector3D::new(0.0, 0.0, -1.5));
assert_eq!(system.particle(1).position, Vector3D::new(0.0, 0.0, 0.0));
Expand Down
2 changes: 1 addition & 1 deletion src/input/src/interactions/coulomb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl InteractionsInput {

let mut nchanged = 0;
for particle in system.particles_mut() {
if particle.name() == name {
if particle.name == name.as_ref() {
particle.charge = charge;
nchanged += 1;
total_charge += charge;
Expand Down
2 changes: 1 addition & 1 deletion tests/nist-spce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn get_system(path: &str, cutoff: f64) -> System {
}

for particle in system.particles_mut() {
particle.charge = match particle.name() {
particle.charge = match particle.name.as_ref() {
"H" => 0.42380,
"O" => -2.0 * 0.42380,
other => panic!("Unknown particle name: {}", other)
Expand Down

0 comments on commit 8e92ae5

Please sign in to comment.