[CHNOSZ-commits] r456 - in pkg/CHNOSZ: . R inst man tests/testthat

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat May 4 13:38:52 CEST 2019


Author: jedick
Date: 2019-05-04 13:38:52 +0200 (Sat, 04 May 2019)
New Revision: 456

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/mosaic.R
   pkg/CHNOSZ/inst/NEWS
   pkg/CHNOSZ/man/mosaic.Rd
   pkg/CHNOSZ/tests/testthat/test-mosaic.R
Log:
mosaic(): equilibrate using specified total activity; return e.out


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2019-04-28 08:31:52 UTC (rev 455)
+++ pkg/CHNOSZ/DESCRIPTION	2019-05-04 11:38:52 UTC (rev 456)
@@ -1,6 +1,6 @@
-Date: 2019-04-28
+Date: 2019-05-04
 Package: CHNOSZ
-Version: 1.3.2-3
+Version: 1.3.2-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/mosaic.R
===================================================================
--- pkg/CHNOSZ/R/mosaic.R	2019-04-28 08:31:52 UTC (rev 455)
+++ pkg/CHNOSZ/R/mosaic.R	2019-05-04 11:38:52 UTC (rev 456)
@@ -75,7 +75,12 @@
   for(i in 1:length(bases)) {
     message("mosaic: calculating affinities of basis species group ", i, ": ", paste(bases[[i]], collapse=" "))
     species(delete = TRUE)
-    species(bases[[i]])
+    # 20190504: when equilibrating the changing basis species, use a total activity equal to the activity from the basis definition
+    act.total <- 10^basis0$logact[ibasis0[i]]
+    # for now, assume that the changing basis species react with a 1:1 stoichiometry
+    # TODO: retrieve the actual balancing coefficients
+    logact.each <- log10(act.total / length(bases[[i]]))
+    species(bases[[i]], logact.each)
     A.bases[[i]] <- suppressMessages(affinity(..., sout = sout))
   }
 
@@ -98,6 +103,7 @@
   # calculate equilibrium mole fractions for each group of basis species
   group.fraction <- list()
   if(blend) {
+    e.out <- list()
     for(i in 1:length(A.bases)) {
       # this isn't needed (and doesn't work) if all the affinities are NA 20180925
       if(any(!sapply(A.bases[[1]]$values, is.na))) {
@@ -106,6 +112,8 @@
         a.equil <- lapply(e$loga.equil, function(x) 10^x)
         a.tot <- Reduce("+", a.equil)
         group.fraction[[i]] <- lapply(a.equil, function(x) x / a.tot)
+        # include the equilibrium activities in the output of this function 20190504
+        e.out[[1]] <- e
       } else {
         group.fraction[[i]] <- A.bases[[i]]$values
       }
@@ -156,5 +164,6 @@
   # for argument recall, include all arguments in output 20190120
   allargs <- c(list(bases = bases, blend = blend, mixing = mixing), list(...))
   # return the affinities for the species and basis species
-  return(list(fun = "mosaic", args = allargs, A.species = A.species, A.bases = A.bases))
+  if(blend) return(list(fun = "mosaic", args = allargs, A.species = A.species, A.bases = A.bases, e.out = e.out))
+  else return(list(fun = "mosaic", args = allargs, A.species = A.species, A.bases = A.bases))
 }

Modified: pkg/CHNOSZ/inst/NEWS
===================================================================
--- pkg/CHNOSZ/inst/NEWS	2019-04-28 08:31:52 UTC (rev 455)
+++ pkg/CHNOSZ/inst/NEWS	2019-05-04 11:38:52 UTC (rev 456)
@@ -1,9 +1,14 @@
-CHANGES IN CHNOSZ 1.3.2-3 (2019-04-28)
+CHANGES IN CHNOSZ 1.3.2-4 (2019-05-04)
 --------------------------------------
 
 - demo/aluminum.R: add calculations using Si(OH)4 from Akinfiev-Diamond
   model (SiO2 in these reactions is replaced by Si(OH)4 - 2 H2O).
 
+- In mosaic(), equilibrate groups of changing basis species using total
+  activity taken from the corresponding basis species in the incoming
+  basis() definition, and return the result of equilibrate() in the
+  output.
+
 CHANGES IN CHNOSZ 1.3.2 (2019-04-20)
 ------------------------------------
 

Modified: pkg/CHNOSZ/man/mosaic.Rd
===================================================================
--- pkg/CHNOSZ/man/mosaic.Rd	2019-04-28 08:31:52 UTC (rev 455)
+++ pkg/CHNOSZ/man/mosaic.Rd	2019-05-04 11:38:52 UTC (rev 456)
@@ -20,7 +20,7 @@
 
 \details{
 
-\code{mosaic} can be used to calculate the affinities of formation of species when the relative abundances of basis species listed in \code{bases} changes over the range of conditions, due to e.g. ionization, complexation or redox reactions.
+\code{mosaic} can be used to calculate the affinities of formation of species when the relative abundances of the basis species listed in \code{bases} change over the range of conditions, due to e.g. ionization, complexation or redox reactions.
 This is a way to \dQuote{speciate the basis species}.
 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.
 Chemical activity diagrams constructed by assembling sub-diagrams corresponding to the predominant basis species can described as \dQuote{mosaic diagrams}.
@@ -28,10 +28,11 @@
 The function calculates the affinities using all combination of basis species given as vector arguments to \code{bases} and \code{bases2}.
 The first species listed in each group should be in the current basis definition, and all the basis species in each group should 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 arguments in \code{...} are passed to \code{affinity} to specify the conditions.
+The arguments in \code{...} are passed to \code{affinity} to specify the variable conditions, such as temperature, pressure, and activities of other basis species.
 
-If \code{blend} is TRUE (the default), the function combines the affinities of the formation reactions in proportion to the relative abundances of the basis species at each condition.
-Additionally, if \code{mixing} is TRUE (the default), a term is included to account for the Gibbs energy of ideal mixing.
+If \code{blend} is TRUE (the default), the relative abundances of the basis species in each group are calculated using \code{\link{equilibrate}}, with the total activity taken from the corresponding basis species in the incoming \code{\link{basis}} definition.
+Then, the function calculates overall affinities of the formation reactions of each species by combining reactions written using individual basis species in proportion to the relative abundances of the basis species.
+Additionally, if \code{mixing} is TRUE (the default), a term is included to account for the Gibbs energy of ideal mixing of the basis species.
 See the second example in \code{\link{solubility}} for a numerical test of the calculations using \code{blend} and \code{mixing}.
 If \code{blend} is FALSE, the function returns the affinities calculated using the single predominant basis species in \code{bases} at each condition (in this case, the \code{mixing} argument has no effect).
 
@@ -45,7 +46,8 @@
 
 \value{
 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).
+If \code{blend} is TRUE, the output also contains \code{e.out} (the output of \code{\link{equilibrate}} for each group of basis species)
+If \code{bases2} is provided, the list also contains \code{A.bases2} (affinities of formation of the second group of basis species).
 }
 
 \seealso{

Modified: pkg/CHNOSZ/tests/testthat/test-mosaic.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-mosaic.R	2019-04-28 08:31:52 UTC (rev 455)
+++ pkg/CHNOSZ/tests/testthat/test-mosaic.R	2019-05-04 11:38:52 UTC (rev 456)
@@ -23,7 +23,7 @@
 
 test_that("blend=TRUE produces reasonable values", {
   # a more rigorous test than above. this was failing because loga.tot (actually, a.tot)
-  # was computed incorrectly, by sum'ing an unlist'ed list (the affinities of basis species)
+  # was computed incorrectly, by sum()ing an unlist()ed list (the affinities of basis species)
   # to produce a single value; corrected by using Reduce for addition of vectors/arrays in the list.
   # example adapted from ?mosaic
   basis(c("FeO", "SO4-2", "H2O", "H+", "e-"))



More information about the CHNOSZ-commits mailing list