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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 24 02:45:17 CET 2019


Author: jedick
Date: 2019-01-24 02:45:17 +0100 (Thu, 24 Jan 2019)
New Revision: 366

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/solubility.R
   pkg/CHNOSZ/man/solubility.Rd
Log:
solubility(): add 'in.terms.of' argument


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2019-01-23 10:43:19 UTC (rev 365)
+++ pkg/CHNOSZ/DESCRIPTION	2019-01-24 01:45:17 UTC (rev 366)
@@ -1,6 +1,6 @@
 Date: 2019-01-23
 Package: CHNOSZ
-Version: 1.1.3-73
+Version: 1.1.3-74
 Title: Thermodynamic Calculations and Diagrams for Geo(bio)chemistry
 Authors at R: c(
     person("Jeffrey", "Dick", , "j3ffdick at gmail.com", role = c("aut", "cre"),

Modified: pkg/CHNOSZ/R/solubility.R
===================================================================
--- pkg/CHNOSZ/R/solubility.R	2019-01-23 10:43:19 UTC (rev 365)
+++ pkg/CHNOSZ/R/solubility.R	2019-01-24 01:45:17 UTC (rev 366)
@@ -6,8 +6,9 @@
 ## if this file is interactively sourced, the following are also needed to provide unexported functions:
 #source("equilibrate.R")
 #source("util.misc.R")
+#source("species.R")
 
-solubility <- function(aout, dissociation=NULL, find.IS=FALSE) {
+solubility <- function(aout, dissociation=NULL, find.IS=FALSE, in.terms.of=NULL) {
   ## concept: the logarithms of activities of species at equilibrium are equal to
   ## Astar, the affinities calculated for unit activities of species
 
@@ -22,11 +23,17 @@
     dissociation <- FALSE
     # if the reaction to form the first species involves the second basis species, we consider it to be a dissociation reaction
     if(aout$species[1, 2] != 0) {
-      # however, if the second basis species is H2O, H+, e-, O2 (or others?), we don't have enough information, so stop
-      if(colnames(aout$species)[2] %in% c("H2O", "H+", "e-", "O2")) {
-        stop("Unsure whether the first formation reaction is a dissociation reaction.\nSet the 'dissociation' argument to TRUE or FALSE, or redefine the basis to put a product ion second.")
+      # 20190123 (corundum calculation): if there are only H2O, H+, and e-
+      # besides the first basis species, it's not a dissociation reaction
+      nbasis <- nrow(aout$basis)
+      nH2O <- sum(rownames(aout$basis) %in% c("H2O", "H+", "e-", "O2", "H2"))
+      if(nbasis > (nH2O + 1)) {
+        # if we got here, and the second basis species is H2O, H+, e-, O2 (or others?), we don't have enough information, so stop
+        if(colnames(aout$species)[2] %in% c("H2O", "H+", "e-", "O2", "H2")) {
+          stop("Unsure whether the first formation reaction is a dissociation reaction.\nSet the 'dissociation' argument to TRUE or FALSE, or redefine the basis to put a product ion second.")
+        }
+        dissociation <- TRUE
       }
-      dissociation <- TRUE
     }
     message("solubility: test for dissociation reaction returns ", dissociation)
   } else message("solubility: argument for dissociation reaction is ", dissociation)
@@ -126,6 +133,17 @@
     niter <- niter + 1
   }
 
+  # do we want the solubility expressed in terms of
+  # something other than the first basis species? 20190123
+  if(!is.null(in.terms.of)) {
+    # write the reaction between the basis species and the new species
+    sbasis <- species.basis(in.terms.of)
+    # divide the activity of the conserved basis species by the coefficient in the formation reaction
+    ibalance <- which.balance(aout$species)
+    coeff <- sbasis[, ibalance][1]
+    loga.balance <- loga.balance - log10(coeff)
+  }
+
   # make the output
   # (we don't deal with normalized formulas yet, so for now m.balance==n.balance)
   c(aout, list(balance=bout$balance, m.balance=bout$n.balance, n.balance=bout$n.balance,

Modified: pkg/CHNOSZ/man/solubility.Rd
===================================================================
--- pkg/CHNOSZ/man/solubility.Rd	2019-01-23 10:43:19 UTC (rev 365)
+++ pkg/CHNOSZ/man/solubility.Rd	2019-01-24 01:45:17 UTC (rev 366)
@@ -7,13 +7,14 @@
 }
 
 \usage{
-  solubility(aout, dissociation = NULL, find.IS = FALSE)
+  solubility(aout, dissociation = NULL, find.IS = FALSE, in.terms.of = NULL)
 }
 
 \arguments{
   \item{aout}{list, output from \code{\link{affinity}}}
   \item{dissociation}{logical, does the mineral undergo a dissociation reaction?}
   \item{find.IS}{logical, find the equilibrium ionic strength by iteration?}
+  \item{in.terms.of}{character, express the total solubility in terms of moles of this species}
 }
 
 \details{
@@ -46,10 +47,14 @@
 However, for dissolving a substance in pure water, \code{find.IS} can be set to TRUE to determine the final ionic strength.
 This works by calculating the ionic strength from the equilibrium solubility calculation, then re-running \code{affinity} with those values.
 Note that for dissociation reactions, the ionic strength is calculated from both the ions present in the species definition and the counter ion, which should be the second basis species.
-TODO: Speciation of counterions (e.g. ionized forms of carbonate or sulfate) can be accomplished by using the \code{\link{mosaic}} function instead of \code{affinity}.
 The calculation is iterated until the ionic strength deviation at every point is lower than a preset tolerance (1e-4).
+Alternatively, speciation of counterions (e.g. ionized forms of carbonate or sulfate) can also be accomplished by using the \code{\link{mosaic}} function instead of \code{affinity}.
+See the second example for this method.
 
 The output of \code{solubility} has the same format as that of \code{equilibrate}, and can be used by \code{\link{diagram}} with \code{type = "loga.balance"} to plot the solubilities, or with \code{type = NULL} to plot the activities of species. 
+The value of \code{loga.balance} reflects the activity (or molality) of the conserved basis species, i.e. the thing being dissolved.
+Use \code{in.terms.of} to express this value in terms of another species.
+For example, the solubility of corundum (Al\s{2}O\s{3}) can be expressed in terms of the moles of Al\S{+3} in solution (see the vignette \code{anintro.Rmd}).
 }
 
 \section{Warning}{



More information about the CHNOSZ-commits mailing list