# NOT RUN {
## equilibrium in a simple system:
## ionization of carbonic acid
basis("CHNOS+")
species(c("CO2", "HCO3-", "CO3-2"))
# set unit activity of the species (0 = log10(1))
species(1:3, 0)
# calculate Astar (for unit activity)
res <- 100
Astar <- affinity(pH=c(0, 14, res))$values
# the logarithms of activity for a total activity
# of the balancing component (CO2) equal to 0.001
loga.boltz <- equil.boltzmann(Astar, c(1, 1, 1), 0.001)
# calculated another way
loga.react <- equil.reaction(Astar, c(1, 1, 1), rep(0.001, 100))
# probably close enough for most purposes
stopifnot(all.equal(loga.boltz, loga.react))
# the first ionization constant (pKa)
ipKa <- which.min(abs(loga.boltz[[1]] - loga.boltz[[2]]))
pKa.equil <- seq(0, 14, length.out=res)[ipKa]
# calculate logK directly
logK <- subcrt(c("CO2","H2O","HCO3-","H+"), c(-1, -1, 1, 1), T=25)$out$logK
# we could decrease tolerance here by increasing res
stopifnot(all.equal(pKa.equil, -logK, tolerance=1e-2))
# equilibrate with mosaic: balancing on two elements (N and C)
# thanks to Kirt Robinson for the feature request and test system
loga_N <- -4
loga_C <- -3
basis(c("CO2", "NH3", "O2", "H2O", "H+"))
basis("NH3", loga_N)
species(c("acetamide", "acetic acid", "acetate"))
# this calculates equilibrium activities of NH3 and NH4+ for given loga_N
# and calculates the corresponding affinities of the formed species
m <- mosaic(c("NH3", "NH4+"), pH = c(0, 14))
# this calculates equilibrium activities of the formed species for given loga_C
# and combines them with the activities of the changing basis species (NH3 and NH4+)
eqc <- equilibrate(m, loga.balance = loga_C)
diagram(eqc, ylim = c(-10, -2))
title(main = paste("log(total N in basis species) =", loga_N,
"\nlog(total C in formed species) =", loga_C), font.main = 1)
# }
Run the code above in your browser using DataLab