pub struct Equation { /* private fields */ }Expand description
A Chemical Equation. Containing a left and right side. Also keeps track of the mol ratio.
Eg: 4Fe + 3O2 -> 2Fe2O3
Implementations§
Source§impl Equation
impl Equation
Sourcepub fn new(input: &str) -> Result<Self, EquationError>
pub fn new(input: &str) -> Result<Self, EquationError>
Create an Equation from a str. Fails if the string couldn’t
be parsed.
§Examples
use chem_eq::{Equation, error::EquationError};
let eq = Equation::new("2H2 + O2 -> 2H2O");
assert!(eq.is_ok());
let eq = Equation::new("H2b + bad_name == joe");
assert!(matches!(eq, Err(EquationError::ParsingError(_))));Examples found in repository?
More examples
Sourcepub fn mol_ratio(&self) -> (usize, usize)
pub fn mol_ratio(&self) -> (usize, usize)
Get the mol ratio of the equation (left over right). Will count any compound with no specified state.
§Examples
use chem_eq::Equation;
// returns left over right
// if states aren't given, everything is counted
let eq = Equation::new("2H2 + O2 -> 2H2O").unwrap();
assert_eq!(eq.mol_ratio(), (3, 2));
// doesn't matter how bad an equation this is...
let eq = Equation::new("4FeH3(s) + 3O2(g) -> 2Fe2O3(s) + 6H2(g)").unwrap();
assert_eq!(eq.mol_ratio(), (3, 6));Sourcepub fn uniq_elements(&self) -> Vec<&str>
pub fn uniq_elements(&self) -> Vec<&str>
Get a vec of each unique element name
§Examples
use chem_eq::Equation;
let eq = Equation::new("2O2 + H2 -> 2H2O").unwrap();
assert_eq!(eq.uniq_elements().len(), 2);
let eq =
Equation::new("3(NH4)2SO4(aq) + Fe3(PO4)2(s) <- 2(NH4)3PO4(aq) + 3FeSO4(aq)").unwrap();
assert_eq!(eq.uniq_elements().len(), 6);Sourcepub fn num_compounds(&self) -> usize
pub fn num_compounds(&self) -> usize
Count how many compounds are in the whole equation.
§Examples
use chem_eq::Equation;
let eq = Equation::new("O2 + 2H2 -> 2H2O").unwrap();
assert_eq!(eq.num_compounds(), 3);
let eq = Equation::new("3(NH4)2SO4(aq) + Fe3(PO4)2(s) <- 2(NH4)3PO4(aq) + 3FeSO4(aq)").unwrap();
assert_eq!(eq.num_compounds(), 4);Sourcepub fn reconstruct(&self) -> String
pub fn reconstruct(&self) -> String
Reconstruct original equation without using the saved original string.
§Examples
use chem_eq::Equation;
let eq = Equation::new("O2 + H2 -> H2O").unwrap();
assert_eq!(eq.reconstruct(), "1O2 + 1H2 -> 1H2O1");Sourcepub fn iter_compounds(&self) -> impl Iterator<Item = &Compound>
pub fn iter_compounds(&self) -> impl Iterator<Item = &Compound>
Create an iterator over all compounds of an equation
§Examples
use chem_eq::{Equation, Compound};
let eq = Equation::new("O2 + H2 -> H2O").unwrap();
assert_eq!(eq.iter_compounds().collect::<Vec<&Compound>>().len(), 3);Sourcepub fn iter_compounds_mut(&mut self) -> impl Iterator<Item = &mut Compound>
pub fn iter_compounds_mut(&mut self) -> impl Iterator<Item = &mut Compound>
Create a mutable iterator over all compounds of an equation.
§Examples
use chem_eq::{Equation, Compound};
let mut eq = Equation::new("O2 + H2 -> H2O").unwrap();
assert_eq!(eq.iter_compounds_mut().collect::<Vec<&mut Compound>>().len(), 3);Sourcepub fn is_balanced(&self) -> bool
Available on crate feature balance only.
pub fn is_balanced(&self) -> bool
balance only.Check if the equation is balanced
§Examples
use chem_eq::Equation;
let eq = Equation::new("C + 2H2O -> CO2 + 2H2").unwrap();
assert!(eq.is_balanced());
let eq = Equation::new("Mg(OH)2 + Fe -> Fe(OH)3 + Mg").unwrap();
assert!(!eq.is_balanced());Sourcepub fn to_balancer(&self) -> EquationBalancer<'_>
Available on crate feature balance only.
pub fn to_balancer(&self) -> EquationBalancer<'_>
balance only.Create an EquationBalancer, mostly as a convenience method.
§Examples
use chem_eq::Equation;
let eq = Equation::new("H2 + O2 -> H2O").unwrap().to_balancer().balance().unwrap();
assert_eq!(eq.equation(), "2H2 + O2 -> 2H2O".to_string());Sourcepub fn is_exothermic(&self) -> bool
pub fn is_exothermic(&self) -> bool
Check whether an equation is exothermic
§Examples
use chem_eq::Equation;
let mut eq = Equation::new("2Mg(s) + O2(g) -> 2MgO(s)").unwrap();
eq.set_delta_h(-601.1);
assert!(eq.is_exothermic());Sourcepub fn is_endothermic(&self) -> bool
pub fn is_endothermic(&self) -> bool
Check whether an equation is endothermic
§Examples
use chem_eq::Equation;
let mut eq = Equation::new("6CO2 + 6H2O -> C6H12O6 + 6O2").unwrap();
eq.set_delta_h(2802.7);
assert!(eq.is_endothermic());Sourcepub fn compound_names(&self) -> impl Iterator<Item = &str>
pub fn compound_names(&self) -> impl Iterator<Item = &str>
Get an iterator over each compounds name.
§Examples
use chem_eq::Equation;
let eq = Equation::new("H2 + O2 -> H2O").unwrap();
assert_eq!(vec!["H2", "O2", "H2O"], eq.compound_names().collect::<Vec<&str>>());
let eq = Equation::new("Fe2O3 <- Fe + O2").unwrap();
assert_eq!(vec!["Fe2O3", "Fe", "O2"], eq.compound_names().collect::<Vec<&str>>());Sourcepub fn concentrations(&self) -> impl Iterator<Item = &f32>
pub fn concentrations(&self) -> impl Iterator<Item = &f32>
Get an iterator for each concentration in an equation
Sourcepub fn concentrations_mut(&mut self) -> impl Iterator<Item = &mut f32>
pub fn concentrations_mut(&mut self) -> impl Iterator<Item = &mut f32>
Get a mutable iterator for each concentration in an equation
Sourcepub fn name_and_concentration(&self) -> impl Iterator<Item = (&str, &f32)>
pub fn name_and_concentration(&self) -> impl Iterator<Item = (&str, &f32)>
Get an iterator yielding compound names and concentrations
Sourcepub fn name_and_concentration_mut(
&mut self,
) -> impl Iterator<Item = (String, &mut f32)>
pub fn name_and_concentration_mut( &mut self, ) -> impl Iterator<Item = (String, &mut f32)>
Get a mutable iterator yielding compound names and mutable concentrations
Sourcepub fn get_concentrations(&self) -> Vec<f32>
pub fn get_concentrations(&self) -> Vec<f32>
Get a vec of all concentrations
§Examples
use chem_eq::Equation;
let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
assert_eq!(eq.get_concentrations(), vec![0.0, 0.0, 0.0]);
eq.set_concentrations(&[1.0, 2.0, 3.0]);
assert_eq!(eq.get_concentrations(), vec![1.0, 2.0, 3.0]);Sourcepub fn set_concentrations(
&mut self,
concentrations: &[f32],
) -> Result<(), ConcentrationError>
pub fn set_concentrations( &mut self, concentrations: &[f32], ) -> Result<(), ConcentrationError>
Set concentrations with a slice. A convenience method to quickly set all compounds to have a concentration.
§Examples
use chem_eq::{Equation, error::ConcentrationError};
let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
eq.set_concentrations(&[1.0, 2.0, 3.0]).unwrap();
assert_eq!(eq.get_concentrations(), vec![1.0, 2.0, 3.0]);
assert_eq!(eq.set_concentrations(&[1.0, 34.0]), Err(ConcentrationError::WrongSliceSize));Sourcepub fn get_concentration_by_name(
&self,
name: &str,
) -> Result<f32, ConcentrationNameError>
pub fn get_concentration_by_name( &self, name: &str, ) -> Result<f32, ConcentrationNameError>
Get a singular compounds concentration by its name.
§Examples
use chem_eq::{Equation, error::ConcentrationNameError};
let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
eq.set_concentration_by_name("O2", 0.25).unwrap();
assert_eq!(eq.get_concentration_by_name("O2"), Ok(0.25));
assert_eq!(eq.get_concentration_by_name("joe"), Err(ConcentrationNameError::NotFound));Sourcepub fn get_compound_by_name(&self, name: &str) -> Option<&Compound>
pub fn get_compound_by_name(&self, name: &str) -> Option<&Compound>
Get a compound by name
§Examples
use chem_eq::{Equation, Compound};
let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
let cmp = eq.get_compound_by_name("O2");
assert_eq!(cmp.unwrap(), &Compound::parse("O2").unwrap());
assert!(eq.get_compound_by_name("joe").is_none());Sourcepub fn get_compound_by_name_mut(&mut self, name: &str) -> Option<&mut Compound>
pub fn get_compound_by_name_mut(&mut self, name: &str) -> Option<&mut Compound>
Get a mutable reference to a compound by name
§Examples
use chem_eq::{Equation, Compound};
let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
let cmp = eq.get_compound_by_name_mut("O2");
assert_eq!(cmp.unwrap(), &mut Compound::parse("O2").unwrap());
assert!(eq.get_compound_by_name("joe").is_none());Sourcepub fn set_concentration_by_name(
&mut self,
name: &str,
concentration: f32,
) -> Result<(), ConcentrationNameError>
pub fn set_concentration_by_name( &mut self, name: &str, concentration: f32, ) -> Result<(), ConcentrationNameError>
Set a singular compounds concentration by its name.
§Examples
use chem_eq::{Equation, error::ConcentrationNameError};
let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
eq.set_concentration_by_name("O2", 0.25).unwrap();
assert_eq!(eq.get_concentrations(), vec![0.0, 0.25, 0.0]);
assert_eq!(eq.set_concentration_by_name("joe", 24.0), Err(ConcentrationNameError::NotFound));
assert_eq!(eq.set_concentration_by_name("H2O", f32::NAN), Err(ConcentrationNameError::NAN));Sourcepub fn equilibrium_constant(&self) -> Option<f32>
pub fn equilibrium_constant(&self) -> Option<f32>
Get the k expression or Kc of an equation. Returns None if one side
has a compound with a concentration of 0
§Examples
use chem_eq::Equation;
let eq = Equation::new("H2 + O2 -> H2O").unwrap();
// is nan because all compounds have an initial concentration of 0M
assert!(eq.equilibrium_constant().is_none());
let mut eq = Equation::new("N2 + 2O2 -> 2NO2").unwrap();
eq.set_concentrations(&[0.25, 0.50, 0.75]).unwrap();
assert_eq!(eq.equilibrium_constant().unwrap(), (0.75 * 0.75) / (0.25 * 0.5 * 0.5));Sourcepub fn reaction_quotient(&self) -> ReactionQuotient
pub fn reaction_quotient(&self) -> ReactionQuotient
Get Qc of the equation, called the reaction coefficient. It’s calculated the same way as the k-expression, however it can apply to non equilibrium concentrations.
§Returns
Assuming the hypothetical reaction where a, b, c, and d are the coefficents of A, B, C and D respectively:
aA + bB <-> cC + dDSince Qc = ([C]^c * [D]^d) / ([A]^a * [B]^b),
This function will return:
ReactionQuotient::BothSidesZeroif[C]^c * [D]^dand[A]^a * [B]^bare both 0ReactionQuotient::LeftZeroif[A]^a * [B]^bis 0ReactionQuotient::RightZeroif[C]^c * [D]^dis 0- otherwise the result of the above equation contained in
ReactionQuotient::Val
Sourcepub fn nth_compound(&self, idx: usize) -> Option<&Compound>
pub fn nth_compound(&self, idx: usize) -> Option<&Compound>
Get the nth compound of the equation.
§Examples
use chem_eq::{Equation, Compound};
let eq = Equation::new("H2 + O2 -> H2O").unwrap();
assert_eq!(eq.nth_compound(0).unwrap(), &Compound::parse("H2").unwrap());
assert_eq!(eq.nth_compound(1).unwrap(), &Compound::parse("O2").unwrap());
assert_eq!(eq.nth_compound(2).unwrap(), &Compound::parse("H2O").unwrap());
assert!(eq.nth_compound(3).is_none());Sourcepub fn nth_compound_mut(&mut self, idx: usize) -> Option<&mut Compound>
pub fn nth_compound_mut(&mut self, idx: usize) -> Option<&mut Compound>
Get the nth compound of the equation mutably.
§Examples
use chem_eq::{Equation, Compound};
let mut eq = Equation::new("H2 + O2 -> H2O").unwrap();
assert_eq!(eq.nth_compound_mut(0).unwrap(), &mut Compound::parse("H2").unwrap());
assert_eq!(eq.nth_compound_mut(1).unwrap(), &mut Compound::parse("O2").unwrap());
assert_eq!(eq.nth_compound_mut(2).unwrap(), &mut Compound::parse("H2O").unwrap());
assert!(eq.nth_compound_mut(3).is_none());Sourcepub fn right_mut(&mut self) -> &mut Vec<Compound>
pub fn right_mut(&mut self) -> &mut Vec<Compound>
Mut getter for the right side of the equation
Sourcepub fn set_delta_h(&mut self, delta_h: f32)
pub fn set_delta_h(&mut self, delta_h: f32)
Setter for delta_h in kJ
Sourcepub const fn temperature(&self) -> Option<f32>
pub const fn temperature(&self) -> Option<f32>
Getter for temperature in degrees Celsius
Sourcepub fn set_temperature(&mut self, temperature: f32)
pub fn set_temperature(&mut self, temperature: f32)
Setter for temperature in degrees Celsius
Sourcepub fn set_volume(&mut self, volume: f32)
pub fn set_volume(&mut self, volume: f32)
Setter for volume in Litres
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Equation
impl<'de> Deserialize<'de> for Equation
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<'a> From<&'a Equation> for EquationBalancer<'a>
Available on crate feature balance only.
impl<'a> From<&'a Equation> for EquationBalancer<'a>
balance only.Source§impl PartialOrd for Equation
impl PartialOrd for Equation
Source§impl TryFrom<&str> for Equation
impl TryFrom<&str> for Equation
Source§type Error = EquationError
type Error = EquationError
impl Eq for Equation
Auto Trait Implementations§
impl Freeze for Equation
impl RefUnwindSafe for Equation
impl Send for Equation
impl Sync for Equation
impl Unpin for Equation
impl UnwindSafe for Equation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more