[CHNOSZ-commits] r73 - in pkg/CHNOSZ: . R demo inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Dec 21 08:31:43 CET 2014


Author: jedick
Date: 2014-12-21 08:31:42 +0100 (Sun, 21 Dec 2014)
New Revision: 73

Added:
   pkg/CHNOSZ/demo/mosaic.R
Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/examples.R
   pkg/CHNOSZ/R/mosaic.R
   pkg/CHNOSZ/R/transfer.R
   pkg/CHNOSZ/demo/00Index
   pkg/CHNOSZ/inst/NEWS
   pkg/CHNOSZ/man/examples.Rd
   pkg/CHNOSZ/man/mosaic.Rd
Log:
add 'bases2' argument to mosaic()


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2014-12-20 07:36:08 UTC (rev 72)
+++ pkg/CHNOSZ/DESCRIPTION	2014-12-21 07:31:42 UTC (rev 73)
@@ -1,6 +1,6 @@
-Date: 2014-12-20
+Date: 2014-12-21
 Package: CHNOSZ
-Version: 1.0.3-10
+Version: 1.0.3-11
 Title: Chemical Thermodynamics and Activity Diagrams
 Author: Jeffrey Dick
 Maintainer: Jeffrey Dick <j3ffdick at gmail.com>

Modified: pkg/CHNOSZ/R/examples.R
===================================================================
--- pkg/CHNOSZ/R/examples.R	2014-12-20 07:36:08 UTC (rev 72)
+++ pkg/CHNOSZ/R/examples.R	2014-12-21 07:31:42 UTC (rev 73)
@@ -31,7 +31,7 @@
 
 demos <- function(which=c("sources", "NaCl", "cordierite", 
   "phosphate", "nucleobase", "orp",
-  "findit", "CO2Ac", "nonideal")) {
+  "findit", "CO2Ac", "nonideal", "TPX", "mosaic")) {
   # run one or more demos from CHNOSZ with ask=FALSE, and return the value of the last one
   for(i in 1:length(which)) {
     # say something so the user sees where we are

Modified: pkg/CHNOSZ/R/mosaic.R
===================================================================
--- pkg/CHNOSZ/R/mosaic.R	2014-12-20 07:36:08 UTC (rev 72)
+++ pkg/CHNOSZ/R/mosaic.R	2014-12-21 07:31:42 UTC (rev 73)
@@ -3,9 +3,16 @@
 # 20141220 jmd
 
 # function to calculate affinities with mosaic of basis species
-mosaic <- function(bases, blend=FALSE, ...) {
-  # the arguments for affinity()
-  myargs <- list(...)
+mosaic <- function(bases, bases2=NULL, blend=FALSE, ...) {
+  if(is.null(bases2)) {
+    # the arguments for affinity()
+    myargs <- list(...)
+  } else {
+    # the arguments for affinity (first set of basis species; outer loop)
+    myargs1 <- list(...)
+    # the arguments for mosaic() (second set of basis species; inner loop)
+    myargs <- list(bases=bases2, blend=blend, ...)
+  }
   # are the swapped basis species on the plot?
   # (the first one should be present in the starting basis set)
   iswap <- match(bases[1], names(myargs))
@@ -14,10 +21,17 @@
   # a list where we'll keep the affinity calculations
   affs <- list()
   for(i in seq_along(bases)) {
+    msgout(paste("mosaic: current basis species is", bases[i], "\n", sep=" "))
     # set up argument list: name of swapped-in basis species
     if(!is.na(iswap)) names(myargs)[iswap] <- bases[i]
     # calculate affinities
-    affs[[i]] <- do.call(affinity, myargs)
+    if(is.null(bases2)) {
+      affs[[i]] <- do.call(affinity, myargs)
+    } else {
+      mcall <- do.call(mosaic, myargs)
+      affs[[i]] <- mcall$A.species
+      A.bases2 <- mcall$A.bases
+    }
     # change the basis species; restore the original at the end of the loop
     if(i < length(bases)) {
       swap.basis(bases[i], bases[i+1]) 
@@ -29,11 +43,13 @@
     }
   }
   # calculate affinities of formation of basis species
+  msgout(paste("mosaic: combining diagrams for", paste(bases, collapse=" "), "\n", sep=" "))
   ispecies <- species()$ispecies
   species.logact <- species()$logact
   species(delete=TRUE)
   species(bases)
-  A.basis <- do.call(affinity, myargs)
+  if(is.null(bases2)) A.bases <- do.call(affinity, myargs)
+  else A.bases <- do.call(affinity, myargs1)
   # restore original species with original activities
   species(delete=TRUE)
   species(ispecies, species.logact)
@@ -41,7 +57,7 @@
   A.species <- affs[[1]]
   if(blend) {
     # calculate affinities using relative abundances of basis species
-    e <- equilibrate(A.basis)
+    e <- equilibrate(A.bases)
     for(j in seq_along(affs)) {
       for(i in seq_along(A.species$values)) {
         # start with zero affinity
@@ -52,7 +68,7 @@
     }
   } else {
     # use affinities from the single predominant basis species
-    d <- diagram(A.basis, plot.it=FALSE)
+    d <- diagram(A.bases, plot.it=FALSE)
     # merge affinities using the second, third, ... basis species
     for(j in tail(seq_along(affs), -1)) {
       is.predominant <- d$predominant==j
@@ -62,5 +78,6 @@
     }
   }
   # return the affinities for the species and basis species
-  return(list(A.species=A.species, A.basis=A.basis))
+  if(is.null(bases2)) return(list(A.species=A.species, A.bases=A.bases))
+  else return(list(A.species=A.species, A.bases=A.bases, A.bases2=A.bases2))
 }

Modified: pkg/CHNOSZ/R/transfer.R
===================================================================
--- pkg/CHNOSZ/R/transfer.R	2014-12-20 07:36:08 UTC (rev 72)
+++ pkg/CHNOSZ/R/transfer.R	2014-12-21 07:31:42 UTC (rev 73)
@@ -25,7 +25,7 @@
   istables <- character()
   myaffs <- list()
   sout <- NULL
-  # logarithm of zero (approx.)
+  # logarithm of a very small number (approaching zero)
   log0 <- -999
   # logarithm of molality above which something
   # is considered possible for reaction (is present)

Modified: pkg/CHNOSZ/demo/00Index
===================================================================
--- pkg/CHNOSZ/demo/00Index	2014-12-20 07:36:08 UTC (rev 72)
+++ pkg/CHNOSZ/demo/00Index	2014-12-21 07:31:42 UTC (rev 73)
@@ -7,4 +7,5 @@
 findit          example of findit() using log-normal distribution as an objective
 CO2Ac           activity of CO2 buffered by acetic acid; comparing affinity(return.buffer=TRUE) with diagram(what="CO2")
 nonideal        activity coefficient of charged species, using the IS argument of subcrt
-TPX             Metastablilities of selected ionized bacterial thiol peroxidases as a chemical activity buffer
+TPX             metastablilities of selected ionized bacterial thiol peroxidases as a chemical activity buffer
+mosaic          Eh-pH diagram for iron oxides, sulfides and carbonate with two sets of changing basis species

Added: pkg/CHNOSZ/demo/mosaic.R
===================================================================
--- pkg/CHNOSZ/demo/mosaic.R	                        (rev 0)
+++ pkg/CHNOSZ/demo/mosaic.R	2014-12-21 07:31:42 UTC (rev 73)
@@ -0,0 +1,38 @@
+# Fe-minerals and aqueous species in Fe-S-O-H-C system
+# after Garrels and Christ, 1965 Figure 7.21
+# to reproduce their diagram as closely as posssible, use their thermodynamic data (from Appendix 2)
+mod.obigt(c("Fe+2", "Fe+3"), G=c(-20300, -2520))
+mod.obigt(c("hematite", "magnetite", "pyrrhotite", "pyrite", "siderite"), G=c(-177100, -242400, -23320, -36000, -161060))
+mod.obigt(c("SO4-2", "HS-", "H2S", "HSO4-"), G=c(-177340, 3010, -6540, -179940))
+mod.obigt(c("CO2", "HCO3-", "CO3-2"), G=c(-92310, -140310, -126220))
+# conditions and system definition
+pH <- c(0, 14, 400)
+Eh <- c(-1, 1, 400)
+T <- 25
+basis(c("FeO", "SO4-2", "H2O", "H+", "e-", "CO3-2"))
+basis("SO4-2", -6)
+basis("CO3-2", 0)
+species(c("Fe+2", "Fe+3"), -6)
+species(c("pyrrhotite", "pyrite", "hematite", "magnetite", "siderite"))
+# two sets of changing basis species:
+# speciate SO4-2, HSO4-, HS-, H2S as a function of Eh and pH
+# speciate CO3-2, HCO3-, CO2 as a function of pH
+bases <- c("SO4-2", "HSO4-", "HS-", "H2S")
+bases2 <- c("CO3-2", "HCO3-", "CO2")
+# calculate affinities using the predominant basis species
+# using blend=TRUE we get curvy lines, particularly at the boundaries with siderite
+# compare with the plot in Garrels and Christ, 1965
+m1 <- mosaic(bases, bases2, TRUE, pH=pH, Eh=Eh, T=T)
+# make a diagram and add water stability lines
+diagram(m1$A.species)
+water.lines("pH", "Eh", T=convert(T, "K"), col="seagreen", lwd=1.5)
+# show lines for Fe(aq) = 10^-4 M
+species(c("Fe+2", "Fe+3"), -4)
+m2 <- mosaic(bases, bases2, TRUE, pH=pH, Eh=Eh, T=T)
+diagram(m2$A.species, add=TRUE, names=NULL, dotted=3)
+title(main=paste("Iron oxides, sulfides and carbonate in water, log(total S) = -6,",
+  "log(total C)=0, after Garrels and Christ, 1965", sep="\n"))
+# overlay the carbonate basis species predominance fields
+diagram(m1$A.bases2, add=TRUE, col="blue", col.names="blue", dotted=3)
+# reset the database, as it was changed in this example
+data(thermo)

Modified: pkg/CHNOSZ/inst/NEWS
===================================================================
--- pkg/CHNOSZ/inst/NEWS	2014-12-20 07:36:08 UTC (rev 72)
+++ pkg/CHNOSZ/inst/NEWS	2014-12-21 07:31:42 UTC (rev 73)
@@ -1,4 +1,4 @@
-CHANGES IN CHNOSZ 1.0.3-10 (2014-12-20)
+CHANGES IN CHNOSZ 1.0.3-11 (2014-12-21)
 ---------------------------------------
 
 - Add files with average amino acid compositions of proteins from Bison

Modified: pkg/CHNOSZ/man/examples.Rd
===================================================================
--- pkg/CHNOSZ/man/examples.Rd	2014-12-20 07:36:08 UTC (rev 72)
+++ pkg/CHNOSZ/man/examples.Rd	2014-12-21 07:31:42 UTC (rev 73)
@@ -8,14 +8,14 @@
 
 \arguments{
   \item{do.png}{logical, generate PNG files for the plots?}
-  \item{which}{character, which example to run.}
+  \item{which}{character, which example to run}
 }
 
 \usage{
   examples(do.png = FALSE)
   demos(which = c("sources", "NaCl", "cordierite",
     "phosphate", "nucleobase", "orp", "findit", 
-    "CO2Ac", "nonideal"))
+    "CO2Ac", "nonideal", "TPX", "mosaic"))
 }
 
 \details{
@@ -39,12 +39,16 @@
     \code{findit} \tab detailed example of usage of \code{\link{findit}} using log-normal distribution as an objective \cr
     \code{CO2Ac} \tab activity of CO2 buffered by acetic acid; comparing \code{\link{affinity}(return.buffer=TRUE)} with \code{\link{diagram}(what="CO2")} \cr
     \code{nonideal} \tab activity coefficient of charged species (Alberty, 2003), using the \code{IS} argument of \code{subcrt} \cr
+    \code{TPX} \tab metastablilities of selected ionized bacterial thiol peroxidases as a chemical activity buffer \cr
+    \code{mosaic} \tab Eh-pH diagram for iron oxides, sulfides and carbonate with two sets of changing basis species (Garrels and Christ, 1965) \cr
   }
 
 }
 
 \references{
   Alberty, R. A. (2003) \emph{Thermodynamics of Biochemical Reactions}, John Wiley & Sons, Hoboken, New Jersey, 397 p. \url{http://www.worldcat.org/oclc/51242181}
+
+  Garrels, R. M. and Christ, C. L. (1965) \emph{Solutions, Minerals, and Equilibria}, Harper & Row, New York, 450 p. \url{http://www.worldcat.org/oclc/517586}
   
   Shock, E. L., Oelkers, E. H., Johnson, J. W., Sverjensky, D. A. and Helgeson, H. C. (1992) Calculation of the thermodynamic properties of aqueous species at high pressures and temperatures: Effective electrostatic radii, dissociation constants and standard partial molal properties to 1000 \eqn{^{\circ}}{°}C and 5 kbar. \emph{J. Chem. Soc. Faraday Trans.} \bold{88}, 803--826. \url{http://dx.doi.org/10.1039/FT9928800803}
 }

Modified: pkg/CHNOSZ/man/mosaic.Rd
===================================================================
--- pkg/CHNOSZ/man/mosaic.Rd	2014-12-20 07:36:08 UTC (rev 72)
+++ pkg/CHNOSZ/man/mosaic.Rd	2014-12-21 07:31:42 UTC (rev 73)
@@ -7,11 +7,12 @@
 }
 
 \usage{
-  mosaic(bases, blend=FALSE, ...)
+  mosaic(bases, bases2=NULL, blend=FALSE, ...)
 }
 
 \arguments{
-  \item{bases}{character, basis species to include in the calculation}
+  \item{bases}{character, basis species to be changed in the calculation}
+  \item{bases2}{character, second set of changing basis species}
   \item{blend}{logical, use relative abundances of basis species?}
   \item{...}{additional arguments to be passed to \code{\link{affinity}}}
 }
@@ -19,23 +20,33 @@
 \details{
 
 \code{mosaic} can be used to calculate the reaction affinities when the basis species listed in \code{bases} change in relative abundance over the range of conditions, due to e.g. ionization, complexation or redox reactions.
+Chemical activity or predominance diagrams constructed by assembling sub-diagrams corresponding to the predominant basis species have sometimes been described as \dQuote{mosaic diagrams} in the literature.
 This is a way to \dQuote{speciate the basis species}.
-An example is consideration of the speciation of sulfur (\samp{SO4-2}, \samp{HSO4-}, \samp{HS-} and \samp{H2S}) as a function of oxygen fugacity (or Eh) and pH in calculating the relative stabilities of minerals and aqueous species in the system Fe-S-O-H.
-The first species listed in \code{bases} should correspond to one of the basis species currently defined, and the function only supports calculations using basis species that all derive from that species (i.e. all share the same element).
+For example, the speciation of sulfur (\samp{SO4-2}, \samp{HSO4-}, \samp{HS-} and \samp{H2S}) as a function of Eh and pH affects the formation affinities, and therefore relative stabilities of iron oxide and sulfide minerals.
+
+The function calculates the affinities using each basis species listed in \code{bases} in turn, changing them via \code{\link{swap.basis}}.
+The first species listed in \code{bases} should be in the current basis definition.
 The arguments in \code{...} are passed to \code{affinity} to specify the conditions.
-
-The function calculates the affinities using each basis species in turn, changing them via \code{\link{swap.basis}}.
 If \code{blend} is FALSE (the default), the function returns the affinities calculated using the single predominant basis species in \code{bases} at each condition.
-Chemical activity or predominance diagrams constructed using this method have been described as \dQuote{mosaic diagrams} in the literature.
 If \code{blend} is TRUE, the function combines the affinities of the formation reactions weighted by the relative abundances of the basis species at each condition.
 This tends to produce curved boundaries.
 
+The basis species listed in \code{bases} should all be related to the first basis species there (i.e. all share the same element).
+A second, independent set of basis species can be provided in \code{bases2} (for example \samp{CO3-2}, \samp{HCO3-}, \samp{CO2}, if the first set of basis species are the sulfur-bearing ones listed above).
+The function then works recursively, by calling itself instead of \code{affinity}, so that the inner loop changes the basis species in \code{bases2}.
+In this way, all possible combinations of the two sets of basis species are used in the calculation.
+
 }
 
 \value{
-A list containing \code{A.species} (affinities of formation of the species with changing basis speceis) and \code{A.basis} (affinities of formation of the basis species in terms of the first basis species), each having same structure as the list returned by \code{\link{affinity}}.
+A list containing \code{A.species} (affinities of formation of the species with changing basis species) and \code{A.bases} (affinities of formation of the basis species in terms of the first basis species), each having same structure as the list returned by \code{\link{affinity}}.
+If \code{bases2} is provided, the list also contains \code{A.bases2} (affinities of formation of the second set of basis species, in terms of the first one in that set).
 }
 
+\seealso{
+\code{demo("mosaic")}, extending the example below by addition of carbonate species in \code{bases2}, with \code{blend} set to TRUE, and using thermodynamic data from Garrels and Christ, 1965.
+}
+
 \examples{
 \dontshow{data(thermo)}# Fe-minerals and aqueous species in Fe-S-O-H system
 # speciate SO4-2, HSO4-, HS-, H2S as a function of Eh and pH
@@ -61,7 +72,7 @@
 title(main=paste("Iron oxides and sulfides in water, log(total S) = -6",
   "After Garrels and Christ, 1965", sep="\n"))
 # we could overlay the basis species predominance fields
-#diagram(m1$A.basis, add=TRUE, col="blue", col.names="blue", dotted=3)
+#diagram(m1$A.bases, add=TRUE, col="blue", col.names="blue", dotted=3)
 }
 
 \references{



More information about the CHNOSZ-commits mailing list