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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 15 05:39:53 CEST 2020


Author: jedick
Date: 2020-07-15 05:39:52 +0200 (Wed, 15 Jul 2020)
New Revision: 561

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/mosaic.R
   pkg/CHNOSZ/inst/NEWS.Rd
   pkg/CHNOSZ/man/mosaic.Rd
Log:
Add 'predominant' argument to mosaic()


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2020-07-15 01:04:37 UTC (rev 560)
+++ pkg/CHNOSZ/DESCRIPTION	2020-07-15 03:39:52 UTC (rev 561)
@@ -1,6 +1,6 @@
 Date: 2020-07-15
 Package: CHNOSZ
-Version: 1.3.6-34
+Version: 1.3.6-35
 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	2020-07-15 01:04:37 UTC (rev 560)
+++ pkg/CHNOSZ/R/mosaic.R	2020-07-15 03:39:52 UTC (rev 561)
@@ -11,7 +11,7 @@
 #source("util.args.R")
 
 # function to calculate affinities with mosaic of basis species
-mosaic <- function(bases, bases2 = NULL, blend = TRUE, ...) {
+mosaic <- function(bases, bases2 = NULL, blend = TRUE, predominant = list(), ...) {
 
   # argument recall 20190120
   # if the first argument is the result from a previous mosaic() calculation,
@@ -63,7 +63,10 @@
   ibasis0 <- match(ispecies0, basis0$ispecies)
   # quit if starting basis species are not present
   ina <- is.na(ibasis0)
-  if(any(ina)) stop("the starting basis does not have ", paste(bases[ina], collapse = ", "))
+  if(any(ina)) {
+    names0 <- unlist(lapply(bases, "[", 1))
+    stop("the starting basis does not have ", paste(names0[ina], collapse = " and "))
+  }
 
   # run subcrt() calculations for all basis species and formed species 20190131
   # this avoids repeating the calculations in different calls to affinity()
@@ -105,9 +108,10 @@
 
   # calculate equilibrium mole fractions for each group of basis species
   group.fraction <- list()
-  if(blend) {
-    E.bases <- list()
-    for(i in 1:length(A.bases)) {
+  blend <- rep(blend, length(A.bases))
+  for(i in 1:length(A.bases)) {
+    if(blend[i]) {
+      E.bases <- list()
       # this isn't needed (and doesn't work) if all the affinities are NA 20180925
       if(any(!sapply(A.bases[[1]]$values, is.na))) {
         # 20190504: when equilibrating the changing basis species, use a total activity equal to the activity from the basis definition
@@ -122,16 +126,20 @@
       } else {
         group.fraction[[i]] <- A.bases[[i]]$values
       }
-    }
-  } else {
-    # for blend = FALSE, we just look at whether
-    # a basis species predominates within its group
-    for(i in 1:length(A.bases)) {
-      d <- diagram(A.bases[[i]], plot.it = FALSE, limit.water = FALSE)
+    } else {
+      # for blend = FALSE, we just look at whether
+      # a basis species predominates within its group
+      if(is.null(predominant[i][[1]])) {
+        d <- diagram(A.bases[[i]], plot.it = FALSE, limit.water = FALSE)
+        predom <- d$predominant
+      } else {
+        # get the predominances from the argument 20200715
+        predom <- predominant[i][[1]]
+      }
       group.fraction[[i]] <- list()
       for(j in 1:length(bases[[i]])) {
         # if a basis species predominates, it has a mole fraction of 1, or 0 otherwise
-        yesno <- d$predominant
+        yesno <- predom
         yesno[yesno != j] <- 0
         yesno[yesno == j] <- 1
         group.fraction[[i]][[j]] <- yesno
@@ -180,6 +188,6 @@
   # for argument recall, include all arguments in output 20190120
   allargs <- c(list(bases = bases, blend = blend), list(...))
   # return the affinities for the species and basis species
-  if(blend) return(list(fun = "mosaic", args = allargs, A.species = A.species, A.bases = A.bases, E.bases = E.bases))
+  if(any(blend)) return(list(fun = "mosaic", args = allargs, A.species = A.species, A.bases = A.bases, E.bases = E.bases))
   else return(list(fun = "mosaic", args = allargs, A.species = A.species, A.bases = A.bases))
 }

Modified: pkg/CHNOSZ/inst/NEWS.Rd
===================================================================
--- pkg/CHNOSZ/inst/NEWS.Rd	2020-07-15 01:04:37 UTC (rev 560)
+++ pkg/CHNOSZ/inst/NEWS.Rd	2020-07-15 03:39:52 UTC (rev 561)
@@ -5,7 +5,7 @@
 % macros
 \newcommand{\H2O}{\ifelse{latex}{\eqn{\mathrm{H_{2}O}}}{\ifelse{html}{\out{H<sub>2</sub>O}}{H2O}}}
 
-\section{Changes in CHNOSZ version 1.3.6-34 (2020-07-15)}{
+\section{Changes in CHNOSZ version 1.3.6-35 (2020-07-15)}{
 
   \subsection{MAJOR CHANGES}{
     \itemize{
@@ -111,6 +111,15 @@
       \item Change \code{thermo$...} to \code{thermo()$...} in messages and
       comments.
 
+      \item \code{mosaic()} now allows a \samp{blend} argument of length > 1 to
+      apply a specific setting to each group of basis species.
+
+      \item Add a \samp{predominant} argument to \code{mosaic()} to use
+      previously calculated predominances of species (e.g. minerals) for the
+      changing basis species. This allows \code{mosaic()} calculations to be
+      linked in series, for instance to sequentially add metals (Fe, then Cu)
+      to a diagram.
+
     }
   }
 

Modified: pkg/CHNOSZ/man/mosaic.Rd
===================================================================
--- pkg/CHNOSZ/man/mosaic.Rd	2020-07-15 01:04:37 UTC (rev 560)
+++ pkg/CHNOSZ/man/mosaic.Rd	2020-07-15 03:39:52 UTC (rev 561)
@@ -7,7 +7,7 @@
 }
 
 \usage{
-  mosaic(bases, bases2 = NULL, blend = TRUE, ...)
+  mosaic(bases, bases2 = NULL, blend = TRUE, predominant = list(), ...)
 }
 
 \arguments{
@@ -14,6 +14,7 @@
   \item{bases}{character, basis species to be changed in the calculation, or list, containing vectors for each group of changing basis species}
   \item{bases2}{character, second set of changing basis species}
   \item{blend}{logical, use relative abundances of basis species?}
+  \item{predominant}{list, predominant species to use with blend = FALSE}
   \item{...}{additional arguments to be passed to \code{\link{affinity}}}
 }
 
@@ -29,9 +30,17 @@
 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 variable conditions, such as temperature, pressure, and activities of other basis species.
 
+\code{blend} can be a single logical value or multiple values, in order to control the calculations for individual groups of basis species.
+
 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.
+
 If \code{blend} is FALSE, the function returns the affinities calculated using the single predominant basis species in \code{bases} at each condition.
+This is appropriate when minerals, rather than aqueous species, are used as the changing basis species.
+Note, however, that \code{mosaic} is not internally recursive: the predominances of one group of basis species (e.g. minerals) do not take account of changes in another group (e.g. aqueous species).
+This limitation can be overcome by supplying the calculated predominances (from the \code{predominant} element of the output of \code{\link{diagram}}) in the appropriate position (for that group of basis species) in the \code{predominant} argument.
+By using the predominances of minerals in one calculation to identify the basis species in a subsequent calculation, a series of linked \code{mosaic} diagrams with increasing complexity can be made.
+This is useful for diagrams for minerals with multiple metallic elements.
 
 A more flexible method of specifying multiple sets of basis species is now available.
 Instead of using \code{bases} and \code{bases2}, supply a list for just the \code{bases} argument.



More information about the CHNOSZ-commits mailing list