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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jan 27 03:35:07 CET 2019


Author: jedick
Date: 2019-01-27 03:35:05 +0100 (Sun, 27 Jan 2019)
New Revision: 371

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/affinity.R
   pkg/CHNOSZ/R/util.affinity.R
   pkg/CHNOSZ/inst/NEWS
   pkg/CHNOSZ/tests/testthat/test-affinity.R
   pkg/CHNOSZ/tests/testthat/test-util.affinity.R
Log:
affinity(): invalid variable names give error


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2019-01-26 03:35:08 UTC (rev 370)
+++ pkg/CHNOSZ/DESCRIPTION	2019-01-27 02:35:05 UTC (rev 371)
@@ -1,6 +1,6 @@
-Date: 2019-01-26
+Date: 2019-01-27
 Package: CHNOSZ
-Version: 1.1.3-78
+Version: 1.1.3-79
 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/affinity.R
===================================================================
--- pkg/CHNOSZ/R/affinity.R	2019-01-26 03:35:08 UTC (rev 370)
+++ pkg/CHNOSZ/R/affinity.R	2019-01-27 02:35:05 UTC (rev 371)
@@ -56,11 +56,6 @@
   mybasis <- thermo$basis
   myspecies <- thermo$species
 
-  # stop if Eh or pe is requested but e- isn't in the basis
-  if(any(c("Eh", "pe") %in% names(args$lims))) {
-    if(!"e-" %in% rownames(mybasis)) stop("variable Eh or pe requested but e- isn't in the basis")
-  }
-
   if(!is.null(property)) {
     # the user just wants an energy property
     buffer <- FALSE

Modified: pkg/CHNOSZ/R/util.affinity.R
===================================================================
--- pkg/CHNOSZ/R/util.affinity.R	2019-01-26 03:35:08 UTC (rev 370)
+++ pkg/CHNOSZ/R/util.affinity.R	2019-01-27 02:35:05 UTC (rev 371)
@@ -1,6 +1,9 @@
 # CHNOSZ/util-affinity.R
 # helper functions for affinity()
 
+## if this file is interactively sourced, the following are also needed to provide unexported functions:
+#source("util.units.R")
+
 slice.affinity <- function(affinity,d=1,i=1) {
   # take a slice of affinity along one dimension
   a <- affinity
@@ -276,7 +279,7 @@
   what <- "A"
   vars <- character()
   vals <- list(NA)
-  # this needs to have 1 as the third component b/c
+  # this needs to have 1 as the third component because
   # energy() uses it to build an array with the given dimension
   lims <- list(c(NA, NA, 1))
   # clean out non-variables
@@ -298,13 +301,9 @@
         names(args)[i] <- "H+"
         if(transect) args[[i]] <- -args[[i]]
         else args[[i]][1:2] <- -args[[i]][1:2]
-        if(!'H+' %in% rownames(thermo$basis)) 
-          message('affinity: pH requested, but no H+ in the basis')
       } 
       if(names(args)[i]=="pe") {
         names(args)[i] <- "e-"
-        if(!'e-' %in% rownames(thermo$basis)) 
-          message('affinity: pe requested, but no e- in the basis')
         if(transect) args[[i]] <- -args[[i]]
         else args[[i]][1:2] <- -args[[i]][1:2]
       }
@@ -325,8 +324,15 @@
       # physical state
       ibasis <- match(nametxt, rownames(thermo$basis))
       if(isTRUE(as.logical(ibasis))) {
-        if(thermo$basis$state[ibasis]=="gas") nametxt <- paste("log_f(", nametxt, ")", sep="") 
-        else nametxt <- paste("log_a(", nametxt, ")", sep="") 
+        if(thermo$basis$state[ibasis]=="gas") nametxt <- paste("log10(f_", nametxt, ")", sep="") 
+        else nametxt <- paste("log10(a_", nametxt, ")", sep="") 
+      } else {
+        # stop if the argument doesn't correspond to a basis species, T, P, or IS
+        if(!nametxt %in% c("T", "P", "IS")) {
+          if(! (nametxt=="pH" & 'H+' %in% rownames(thermo$basis) | nametxt %in% c("pe", "Eh") & 'e-' %in% rownames(thermo$basis))) {
+            stop(nametxt, " is not one of T, P, or IS, and does not match any basis species")
+          }
+        }
       }
       # temperature and pressure and Eh
       if(nametxt=="T") unittxt <- " K"

Modified: pkg/CHNOSZ/inst/NEWS
===================================================================
--- pkg/CHNOSZ/inst/NEWS	2019-01-26 03:35:08 UTC (rev 370)
+++ pkg/CHNOSZ/inst/NEWS	2019-01-27 02:35:05 UTC (rev 371)
@@ -1,4 +1,4 @@
-CHANGES IN CHNOSZ 1.1.3-76 (2019-01-25)
+CHANGES IN CHNOSZ 1.1.3-79 (2019-01-27)
 ---------------------------------------
 
 BUG FIXES
@@ -208,7 +208,7 @@
   (sinfo -> ispecies, inpho -> iphases, sinph -> phasespecies).
 
 - info(): display mineral names instead of just "cr" in messages (e.g.
-  with info("SiO2")).
+  for info("SiO2")).
 
 - Add C implementation of counting occurrences of all letters in a
   string (src/count_letters.c) to speed up operation of count.aa().
@@ -223,6 +223,8 @@
 
 - In subcrt(), improve error message for invalid property names.
 
+- In affinity(), make invalid variable names an error.
+
 CHANGES IN CHNOSZ 1.1.3 (2017-11-13)
 ------------------------------------
 

Modified: pkg/CHNOSZ/tests/testthat/test-affinity.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-affinity.R	2019-01-26 03:35:08 UTC (rev 370)
+++ pkg/CHNOSZ/tests/testthat/test-affinity.R	2019-01-27 02:35:05 UTC (rev 371)
@@ -3,12 +3,19 @@
 # clear out any previous basis definition or database alterations
 suppressMessages(data(thermo))
 
-test_that("errors come as expected, and output gives T and P in user's units", {
+test_that("errors come as expected", {
   expect_error(affinity(iprotein=7), "basis species are not defined")
   expect_error(affinity(iprotein=NA), "has some NA values")
   expect_error(affinity(iprotein=0), "are not rownumbers")
   basis("CHNOS")
   expect_error(affinity(), "species have not been defined")
+  species("CO2")
+  expect_error(affinity(pe=c(-10, 10), pH=c(0, 14)), "pe.*does not match any basis species")
+  expect_error(affinity(O2=c(-80, -60), pH=c(0, 14)), "pH.*does not match any basis species")
+})
+
+test_that("output gives T and P in user's units", {
+  basis("CHNOS")
   species("5a(H),14b(H)-cholestane")
   a.C_bar <- affinity(T=c(0, 100, 10), P=c(10, 1000, 10))
   expect_equal(range(a.C_bar$vals[[1]]), c(0, 100))

Modified: pkg/CHNOSZ/tests/testthat/test-util.affinity.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-util.affinity.R	2019-01-26 03:35:08 UTC (rev 370)
+++ pkg/CHNOSZ/tests/testthat/test-util.affinity.R	2019-01-27 02:35:05 UTC (rev 371)
@@ -17,8 +17,7 @@
   # (because the conversion from Eh is a function of temperature)
   # here, the original length of the Eh variable is 3, so that should be retained by A.ionization
   # we need a basis() for the function to poke around in 
-  # (no e- or H+ needed at this time though)
-  basis("CHNOS")
+  basis("CHNOSe")
   ea <- energy.args(list(CO2=c(-5, 0, 6), pH=c(0, 14, 2), Eh=c(-1, 0, 3), T=c(25, 28, 4)))
   A <- A.ionization(1, ea$vars, ea$vals)
   expect_equal(dim(A[[1]]), c(6, 2, 3, 4))



More information about the CHNOSZ-commits mailing list