[CHNOSZ-commits] r336 - in pkg/CHNOSZ: . R demo tests/testthat vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Oct 31 04:29:21 CET 2018


Author: jedick
Date: 2018-10-31 04:29:20 +0100 (Wed, 31 Oct 2018)
New Revision: 336

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/basis.R
   pkg/CHNOSZ/R/diagram.R
   pkg/CHNOSZ/R/equilibrate.R
   pkg/CHNOSZ/demo/solubility.R
   pkg/CHNOSZ/tests/testthat/test-basis.R
   pkg/CHNOSZ/tests/testthat/test-diagram.R
   pkg/CHNOSZ/tests/testthat/test-swap.basis.R
   pkg/CHNOSZ/vignettes/anintro.Rmd
Log:
equilibrate(): return name of balanced basis species, even if not specified in argument


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2018-10-30 13:57:03 UTC (rev 335)
+++ pkg/CHNOSZ/DESCRIPTION	2018-10-31 03:29:20 UTC (rev 336)
@@ -1,6 +1,6 @@
-Date: 2018-10-30
+Date: 2018-10-31
 Package: CHNOSZ
-Version: 1.1.3-43
+Version: 1.1.3-44
 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/basis.R
===================================================================
--- pkg/CHNOSZ/R/basis.R	2018-10-30 13:57:03 UTC (rev 335)
+++ pkg/CHNOSZ/R/basis.R	2018-10-31 03:29:20 UTC (rev 336)
@@ -109,8 +109,14 @@
   if("(Z-1)" %in% rownames(comp)) rownames(comp)[rownames(comp)=="(Z-1)"] <- "e-"
   # now check it for validity of basis species
   # the first test: matrix is square
-  if( nrow(comp) > ncol(comp) ) stop("overdetermined system; square stoichiometric matrix needed")
-  if( nrow(comp) < ncol(comp) ) stop("underdetermined system; square stoichiometric matrix needed")
+  if( nrow(comp) > ncol(comp) ) {
+    if("Z" %in% colnames(comp)) stop("the number of basis species is greater than the number of elements and charge")
+    else stop("the number of basis species is greater than the number of elements")
+  }
+  if( nrow(comp) < ncol(comp) ) {
+    if("Z" %in% colnames(comp)) stop("the number of basis species is less than the number of elements and charge")
+    else stop("the number of basis species is less than the number of elements")
+  }
   # the second test: matrix is invertible
   if(class(try(solve(comp), silent=TRUE))=='try-error') 
     stop("singular stoichiometric matrix")

Modified: pkg/CHNOSZ/R/diagram.R
===================================================================
--- pkg/CHNOSZ/R/diagram.R	2018-10-30 13:57:03 UTC (rev 335)
+++ pkg/CHNOSZ/R/diagram.R	2018-10-31 03:29:20 UTC (rev 336)
@@ -53,7 +53,7 @@
     if(!"loga.equil" %in% names(eout)) {
       eout.is.aout <- TRUE
       # get the balancing coefficients
-      if(type=="auto") n.balance <- balance(eout, balance)
+      if(type=="auto") n.balance <- balance(eout, balance)$n.balance
       else n.balance <- rep(1, length(eout$values))
     }
   } else if(type %in% rownames(eout$basis)) {

Modified: pkg/CHNOSZ/R/equilibrate.R
===================================================================
--- pkg/CHNOSZ/R/equilibrate.R	2018-10-30 13:57:03 UTC (rev 335)
+++ pkg/CHNOSZ/R/equilibrate.R	2018-10-31 03:29:20 UTC (rev 336)
@@ -15,7 +15,9 @@
   ## number of possible species
   nspecies <- length(aout$values)
   ## get the balancing coefficients
-  n.balance <- balance(aout, balance)
+  bout <- balance(aout, balance)
+  n.balance <- bout$n.balance
+  balance <- bout$balance
   ## take selected species in 'ispecies'
   if(length(ispecies)==0) stop("the length of ispecies is zero")
   # take out species that have NA affinities
@@ -249,7 +251,7 @@
 
 ### unexported functions ###
 
-# return a list containing the balancing coefficients (n) and a textual description (description)
+# return a list containing the balancing coefficients (n.balance) and a textual description (balance)
 balance <- function(aout, balance=NULL) {
   ## generate n.balance from user-given or automatically identified basis species
   ## extracted from equilibrate() 20120929
@@ -302,5 +304,5 @@
       if(any(n.balance==0)) stop("some species have no ", balance, " in the formation reaction")
     }
   }
-  return(n.balance)
+  return(list(n.balance=n.balance, balance=balance))
 }

Modified: pkg/CHNOSZ/demo/solubility.R
===================================================================
--- pkg/CHNOSZ/demo/solubility.R	2018-10-30 13:57:03 UTC (rev 335)
+++ pkg/CHNOSZ/demo/solubility.R	2018-10-31 03:29:20 UTC (rev 336)
@@ -50,7 +50,7 @@
   A.whatif <- loga.species.track + A.track - loga.equil.track
 
   # predictive: assuming the species distribution doesn't change,
-  # what is the total loga that would give zero affinity?
+  # what is the log(total activity) that gives zero affinity?
   # TODO: modify this according to stoichiometry (species with > 1 of the balanced basis species)
   loga.total <- loga.balance + A.whatif
 

Modified: pkg/CHNOSZ/tests/testthat/test-basis.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-basis.R	2018-10-30 13:57:03 UTC (rev 335)
+++ pkg/CHNOSZ/tests/testthat/test-basis.R	2018-10-31 03:29:20 UTC (rev 336)
@@ -6,8 +6,8 @@
 test_that("invalid basis definitions cause an error", {
   expect_error(basis(character()), "argument is empty")
   expect_error(basis(c("CO2", "CO2")), "names are not unique")
-  expect_error(basis(c("CO2", "H2O")), "underdetermined")
-  expect_error(basis(c("H2O", "O2", "H2")), "overdetermined")
+  expect_error(basis(c("CO2", "H2O")), "the number of basis species is less than the number of elements")
+  expect_error(basis(c("H2O", "O2", "H2")), "the number of basis species is greater than the number of elements")
   expect_error(basis(c("HCN", "H2O", "O2", "H2")), "singular")
   expect_error(basis(c("CN", "H2O", "O2", "H2")), "species not available")
   expect_error(basis(c("CN")), "species not available")

Modified: pkg/CHNOSZ/tests/testthat/test-diagram.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-diagram.R	2018-10-30 13:57:03 UTC (rev 335)
+++ pkg/CHNOSZ/tests/testthat/test-diagram.R	2018-10-31 03:29:20 UTC (rev 336)
@@ -49,7 +49,7 @@
   # group the species together
   d <- diagram(e, groups=list(1:2, 3:4), plot.it=FALSE)
   # we should find that their activities have been multiplied by the balance coefficients and summed
-  n.balance <- balance(a)
+  n.balance <- balance(a)$n.balance
   expect_equal(d$plotvals[[1]], log10(n.balance[1]*10^e$loga.equil[[1]] + n.balance[2]*10^e$loga.equil[[2]]))
   expect_equal(d$plotvals[[2]], log10(n.balance[3]*10^e$loga.equil[[3]] + n.balance[4]*10^e$loga.equil[[4]]))
   # ask for degrees of formation instead of logarithms of activities

Modified: pkg/CHNOSZ/tests/testthat/test-swap.basis.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-swap.basis.R	2018-10-30 13:57:03 UTC (rev 335)
+++ pkg/CHNOSZ/tests/testthat/test-swap.basis.R	2018-10-31 03:29:20 UTC (rev 336)
@@ -10,7 +10,7 @@
   expect_error(swap.basis(c("CO2", "H2O"), c("HCO3-", "H2O")), "can only swap one species for one species")
   expect_error(swap.basis("CH4", "C2H5OH"), "basis species .* is not defined")
   expect_error(swap.basis("CO2", "C60"), "is not available")
-  expect_error(swap.basis("CO2", "H2"), "overdetermined")
+  expect_error(swap.basis("CO2", "H2"), "the number of basis species is greater than the number of elements and charge")
 })
 
 test_that("basis.logact only accepts defined elements", {

Modified: pkg/CHNOSZ/vignettes/anintro.Rmd
===================================================================
--- pkg/CHNOSZ/vignettes/anintro.Rmd	2018-10-30 13:57:03 UTC (rev 335)
+++ pkg/CHNOSZ/vignettes/anintro.Rmd	2018-10-31 03:29:20 UTC (rev 336)
@@ -924,7 +924,7 @@
 The possible reactions between species are all balanced on 1 C.
 Therefore, although pH alters the total activity of C, in a system with ideal mixing the total activity of C doesn't affect the relative activities of these species.
 ```{marginfigure}
-See [<span style="color:blue">`demo(solubility)`</span>](../demo) for calculations of the total activity of C in this ideal system; uncomment a line in the demo to run calculations for CO<sub>2</sub> instead of calcite.
+See [<span style="color:blue">`demo(solubility)`</span>](../demo) for calculations of the total activity of C in equilibrium with either CO<sub>2(<i>gas</i>)</sub> or calcite.
 ```
 
 ## Groups of species



More information about the CHNOSZ-commits mailing list