[CHNOSZ-commits] r711 - in pkg/CHNOSZ: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Mar 24 08:28:22 CET 2022


Author: jedick
Date: 2022-03-24 08:28:21 +0100 (Thu, 24 Mar 2022)
New Revision: 711

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/add.OBIGT.R
   pkg/CHNOSZ/R/logB_to_OBIGT.R
   pkg/CHNOSZ/man/add.OBIGT.Rd
   pkg/CHNOSZ/man/logB_to_OBIGT.Rd
Log:
Add 'zap' argument to mod.OBIGT()


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2022-03-24 04:21:31 UTC (rev 710)
+++ pkg/CHNOSZ/DESCRIPTION	2022-03-24 07:28:21 UTC (rev 711)
@@ -1,6 +1,6 @@
 Date: 2022-03-24
 Package: CHNOSZ
-Version: 1.4.3-3
+Version: 1.4.3-4
 Title: Thermodynamic Calculations and Diagrams for Geochemistry
 Authors at R: c(
     person("Jeffrey", "Dick", , "j3ffdick at gmail.com", role = c("aut", "cre"),

Modified: pkg/CHNOSZ/R/add.OBIGT.R
===================================================================
--- pkg/CHNOSZ/R/add.OBIGT.R	2022-03-24 04:21:31 UTC (rev 710)
+++ pkg/CHNOSZ/R/add.OBIGT.R	2022-03-24 07:28:21 UTC (rev 711)
@@ -1,11 +1,11 @@
 # CHNOSZ/add.OBIGT.R
 # add or change entries in the thermodynamic database
 
-## if this file is interactively sourced, the following are also needed to provide unexported functions:
+## If this file is interactively sourced, the following are also needed to provide unexported functions:
 #source("info.R")
 #source("util.data.R")
 
-mod.OBIGT <- function(...) {
+mod.OBIGT <- function(..., zap = FALSE) {
   # Add or modify species in thermo()$OBIGT
   thermo <- get("thermo", CHNOSZ)
   # The names and values are in the arguments
@@ -25,12 +25,12 @@
     # If the name of the first argument is missing, assume it's the species name
     if(names(args)[1]=="") names(args)[1] <- "name"
     speciesname <- args$name
-    # search for this species, use check.protein=FALSE to avoid infinite loop when adding proteins
+    # Search for this species, use check.protein = FALSE to avoid infinite loop when adding proteins
     # and suppressMessages to not show messages about matches of this name to other states
     if("state" %in% names(args)) ispecies <- suppressMessages(mapply(info.character, 
-      species=args$name, state=args$state, check.protein=FALSE, SIMPLIFY=TRUE, USE.NAMES=FALSE))
+      species = args$name, state = args$state, check.protein = FALSE, SIMPLIFY = TRUE, USE.NAMES = FALSE))
     else ispecies <- suppressMessages(mapply(info.character, 
-      species=args$name, check.protein=FALSE, SIMPLIFY=TRUE, USE.NAMES=FALSE))
+      species = args$name, check.protein = FALSE, SIMPLIFY = TRUE, USE.NAMES = FALSE))
   }
   # The column names of thermo()$OBIGT, split at the "."
   cnames <- c(do.call(rbind, strsplit(colnames(thermo$OBIGT), ".", fixed=TRUE)), colnames(thermo$OBIGT))
@@ -81,8 +81,13 @@
       # The old values and the state
       oldprop <- thermo$OBIGT[ispecies[iold[i]], icol]
       state <- thermo$OBIGT$state[ispecies[iold[i]]]
+      # Zap (clear) all preexisting values except for state 20220324
+      if(zap) {
+        thermo$OBIGT[ispecies[iold[i]], ] <- NA
+        thermo$OBIGT$state[ispecies[iold[i]]] <- state
+      }
       # Tell user if they're the same, otherwise update the data entry
-      if(isTRUE(all.equal(oldprop, args[iold[i], ], check.attributes=FALSE))) 
+      if(isTRUE(all.equal(oldprop, args[iold[i], ], check.attributes = FALSE))) 
         message("mod.OBIGT: no change for ", speciesname[iold[i]], "(", state, ")")
       else {
         thermo$OBIGT[ispecies[iold[i]], icol] <- args[iold[i], ]

Modified: pkg/CHNOSZ/R/logB_to_OBIGT.R
===================================================================
--- pkg/CHNOSZ/R/logB_to_OBIGT.R	2022-03-24 04:21:31 UTC (rev 710)
+++ pkg/CHNOSZ/R/logB_to_OBIGT.R	2022-03-24 07:28:21 UTC (rev 711)
@@ -7,16 +7,9 @@
   ## Get the formula of the formed species (used as the species name in OBIGT)
   ### NOTE: the formed species has to be *last* in this list
   name <- tail(species, 1)
-  # Check if the species is already present in OBIGT and delete its properties
-  iname <- suppressMessages(info(name))
-  if(!is.na(iname)) {
-    message(paste("logB_to_OBIGT: deleting existing", name, "from OBIGT"))
-    thermo <- get("thermo", CHNOSZ)
-    thermo$OBIGT <- thermo$OBIGT[-iname, ]
-    assign("thermo", thermo, CHNOSZ)
-  }
   # Add the formed species to the thermodynamic database with ΔG°f = 0 at all temperatures
-  suppressMessages(mod.OBIGT(name, G = 0, S = 0, Cp = 0))
+  # Be sure to zap properties of a formed species that is already in the database
+  suppressMessages(mod.OBIGT(name, formula = name, E_units = E.units(), G = 0, S = 0, Cp = 0, zap = TRUE))
   # Calculate logK of the formation reaction with ΔG°f = 0 for the formed species
   logK0 <- suppressMessages(subcrt(species, coeff, T = T, P = P)$out$logK)
 
@@ -63,4 +56,3 @@
   ispecies
 
 }
-

Modified: pkg/CHNOSZ/man/add.OBIGT.Rd
===================================================================
--- pkg/CHNOSZ/man/add.OBIGT.Rd	2022-03-24 04:21:31 UTC (rev 710)
+++ pkg/CHNOSZ/man/add.OBIGT.Rd	2022-03-24 07:28:21 UTC (rev 711)
@@ -9,7 +9,7 @@
 
 \usage{
   add.OBIGT(file, species = NULL, force = TRUE)
-  mod.OBIGT(...)
+  mod.OBIGT(..., zap = FALSE)
 }
 
 \arguments{
@@ -17,9 +17,12 @@
   \item{species}{character, names of species to load from file}
   \item{force}{logical, force replacement of already existing species?}
   \item{...}{character or numeric, properties of species to modify in the thermodynamic database}
+  \item{zap}{logical, clear preexisting parameters?}
 }
 
 \details{
+Note: change made to OBIGT are lost if you reload the database by calling \code{\link{reset}} or \code{\link{OBIGT}} or if you quit the \R session without saving it.
+
 \code{\link{add.OBIGT}} is used to update the thermodynamic database (\code{\link{thermo}$OBIGT}) in the running session.
 The format (column names) of the specified file must be the same as the \code{extdata/OBIGT/*.csv} files provided with CHNOSZ.
 Howvever, for backwards compatibility, the \code{E_units} column of the input can be missing; then, it is assigned a value of \samp{cal}.
@@ -38,17 +41,19 @@
 As a result, retrieving the properties of an added aqueous species using \code{\link{info}} requires an explicit \code{state="aq"} argument to that function if a species with the same name is present in one of the cr, liq or gas states.
 
 \code{mod.OBIGT} changes one or more of the properties of species or adds species to the thermodynamic database.
-These changes are lost if you reload the database by calling \code{\link{reset}} or \code{\link{OBIGT}} or if you quit the \R session without saving it.
 The name of the species to add or change must be supplied as the first argument of \code{...} or as a named argument (named \samp{name}).
+Additional arguments to \code{mod.OBIGT} refer to the name of the property(s) to be updated and are matched to any part of compound column names in \code{\link{thermo}()$OBIGT}.
+For instance, either \samp{z} or \samp{T} matches the \samp{z.T} column.
+The values provided should also include any order-of-magnitude scaling factors (see \code{\link{thermo}}).
+
 When adding new species, a chemical formula should be included along with the values of any of the thermodynamic properties.
 The formula is taken from the \samp{formula} argument, or if that is missing, is taken to be the same as the \samp{name} of the species.
-An error results if the formula is not valid (i.e. can not be parsed by \code{\link{makeup}}).
+An error occurs if the formula is not valid (i.e. can not be parsed by \code{\link{makeup}}).
+For new species, properties that are not specified become NA, except for \samp{state} and \samp{E_units}, which take default values from \code{thermo()$opt}.
+These defaults can be overridden by giving a value for \samp{state} or \samp{E_units} in the new data.
 
-Additional arguments to \code{mod.OBIGT} refer to the name of the property(s) to be updated and are matched to any part of compound column names in \code{\link{thermo}()$OBIGT}.
-For instance, either \samp{z} or \samp{T} matches the \samp{z.T} column.
-When adding species, properties that are not specified become NA, except for \samp{state}, which takes a default value from \code{thermo()$opt$state}.
-The values provided should have energy units correponding to the current setting (\code{\link{E.units}}), but this can be overridden by giving a value for \samp{E_units} in the new data.
-The values provided should also include any order-of-magnitude scaling factors (see \code{\link{thermo}}).
+When modifying species, the parameters indicated by the named arguments of \code{mod.OBIGT} are updated.
+Use \code{zap = TRUE} to replace all prexisting parameters (except for \code{state}) with NA values.
 }
 
 \value{
@@ -55,7 +60,7 @@
 The values returned (\code{\link{invisible}}-y) are the indices of the added and/or modified species.
 }
 
-\seealso{ \code{\link{thermo}}, \code{\link{util.data}}, \code{\link{mod.buffer}}, \code{\link{logB_to_OBIGT}} }
+\seealso{ \code{\link{thermo}} (description of OBIGT), \code{\link{mod.buffer}} (modify buffer definitions), \code{\link{logB_to_OBIGT}} (fit thermodynamic parameters to formation constants) }
 
 \examples{\dontshow{reset()}
 ## Modify an existing species (not real properties)

Modified: pkg/CHNOSZ/man/logB_to_OBIGT.Rd
===================================================================
--- pkg/CHNOSZ/man/logB_to_OBIGT.Rd	2022-03-24 04:21:31 UTC (rev 710)
+++ pkg/CHNOSZ/man/logB_to_OBIGT.Rd	2022-03-24 07:28:21 UTC (rev 711)
@@ -22,25 +22,26 @@
 \details{
 
 \code{logB_to_OBIGT} starts with the decimal logarithms of equilibrium constants (\logB) for a formation reaction.
-The formation reaction is defined by \code{species} and \code{coeff}; all species in the formation reaction must be available in OBIGT except for the last species, which is the newly formed species.
+The formation reaction is defined by \code{species} and \code{coeff}.
+All species in the formation reaction must be available in OBIGT except for the \emph{last} species, which must be the newly formed species.
 The properties of the known species are combined with \logB to calculate the standard Gibbs energy (G[T]) of the formed species as a function of \T and \P.
 
-The values of G[T] are fit using the thermodynamic parameters \emph{G}, \emph{S}, and \emph{Cp} (i.e., standard-state values at 25 °C and 1 bar).
-The equation is this: G[T] = G[Tr] + -S[Tr] * (T - Tr) + Cp[Tr] (T - Tr - T * log(T / Tr)), where G, S, and Cp are standard-state Gibbs energy, entropy, and isobaric heat capacity, T is the temperature in Kelvin, Tr is the reference temperature (298.15 K), and log stands for the natural logarithm.
+The values of G[T] are fit using the thermodynamic parameters \code{G}, \code{S}, and \code{Cp} (i.e., standard-state values at 25 °C and 1 bar).
+The equation is this: G[T] = G[Tr] + -S[Tr] * (T - Tr) + Cp[Tr] (T - Tr - T * ln(T / Tr)), where G, S, and Cp are standard-state Gibbs energy, entropy, and isobaric heat capacity, T is the temperature in Kelvin, Tr is the reference temperature (298.15 K), and ln stands for the natural logarithm.
 Note that the equation assumes a constant heat capacity and no volume change, and it may be unsuitable for extrapolation beyond the original \T and \P range.
 
-The fitted \emph{G}, \emph{S}, and \emph{Cp} for the formed species are added to OBIGT.
-\code{\link{all.equal}} is used to test the near equivalence of the input and calculated equilibrium constants; if the differences exceed \code{tolerance}, the code generates an error.
+The fitted \code{G}, \code{S}, and \code{Cp} for the formed species are added to OBIGT.
+\code{\link{all.equal}} is used to test the near equivalence of the input and calculated equilibrium constants; if the mean absolute difference exceeds \code{tolerance}, an error occurs.
 
 }
 
 \seealso{
-See \code{\link{add.OBIGT}} for more information about modifying the database.
+\code{logB_to_OBIGT} calls \code{\link{mod.OBIGT}} with \code{zap = TRUE} to clear the parameters of a formed species if it already exists in the OBIGT database.
+If preexisting parameters (e.g. HKF coefficients) weren't cleared, they would interfere with the fitting done here, which uses only three parameters (\code{G}, \code{S}, and \code{Cp}).
 }
 
-
 \examples{
-# Formation constants of CoHS+ from Migdisov et al. (2011)
+## CoHS+ from Migdisov et al. (2011)
 logB <- c(6.24, 6.02, 5.84, 5.97, 6.52)
 species <- c("Co+2", "HS-", "CoHS+")
 coeff <- c(-1, -1, 1)
@@ -52,9 +53,9 @@
 info(inew)
 # Make a plot of experimental logB
 xlim <- c()
-plot(T, logB, "n", c(100, 320), c(5.8, 6.8), xlab = axis.label("T"))
+plot(T, logB, "n", c(100, 320), c(5.8, 6.8), xlab = axis.label("T"), ylab = quote(log~beta))
 points(T, logB, pch = 19, cex = 2)
-# Add fitted values
+# Plot fitted values
 Tfit <- seq(100, 320, 10)
 sres <- subcrt(species, coeff, T = Tfit)
 lines(sres$out$T, sres$out$logK, col = 4)
@@ -61,8 +62,29 @@
 title(describe.reaction(sres$reaction))
 legend <- c("Migdisov et al. (2011)", "logB_to_OBIGT()")
 legend("topleft", legend, pch = c(19, NA), lty = c(0, 1), col = c(1, 4), pt.cex = 2)
+
+## ZnCl+ from Mei et al. (2015)
+# These are values calculated from the modified Ryzhenko-Bryzgalin model
+logB <- c(-1.93, -1.16, -0.38, 0.45, 1.15, 1.76, 2.30, 2.80, 3.26, 3.70, 4.12, 4.53, 4.92)
+species <- c("Zn+2", "Cl-", "ZnCl+")
+coeff <- c(-1, -1, 1)
+T <- c(25, 60, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600)
+P <- 5000
+# Note: ZnCl+ is in the default OBIGT database;
+# logB_to_OBIGT() "zaps" the previous parameters before adding the fitted ones
+inew <- logB_to_OBIGT(logB, species, coeff, T, P)
+# Plot MRB and logB_to_OBIGT fitted values
+plot(T, logB, xlab = axis.label("T"), ylab = quote(log~beta), pch = 19, cex = 2)
+Tfit <- seq(25, 600, 25)
+sres <- subcrt(species, coeff, T = Tfit, P = P)
+lines(sres$out$T, sres$out$logK, col = 4)
+title(describe.reaction(sres$reaction))
+legend <- c("Mei et al. (2015)", "logB_to_OBIGT()")
+legend("topleft", legend, pch = c(19, NA), lty = c(0, 1), col = c(1, 4), pt.cex = 2)
 }
 
 \references{
 Migdisov, Art. A., Zezin, D. and Williams-Jones, A. E. (2011) An experimental study of Cobalt (II) complexation in Cl\S{-} and H\s{2}S-bearing hydrothermal solutions. \emph{Geochim. Cosmochim. Acta} \bold{75}, 4065--4079. \doi{10.1016/j.gca.2011.05.003}
+
+Mei, Y., Sherman, D. M., Liu, W., Etschmann, B., Testemale, D. and Brugger, J. (2015) Zinc complexation in chloride-rich hydrothermal fluids (25--600 \degC): A thermodynamic model derived from \emph{ab initio} molecular dynamics. \emph{Geochim. Cosmochim. Acta} \bold{150}, 264--284. \doi{10.1016/j.gca.2014.09.023}
 }



More information about the CHNOSZ-commits mailing list