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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Oct 21 17:34:46 CEST 2017


Author: jedick
Date: 2017-10-21 17:34:45 +0200 (Sat, 21 Oct 2017)
New Revision: 264

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/basis.R
   pkg/CHNOSZ/R/subcrt.R
   pkg/CHNOSZ/tests/testthat/test-basis.R
   pkg/CHNOSZ/vignettes/anintro.Rmd
   pkg/CHNOSZ/vignettes/obigt.Rmd
Log:
mod.basis(): improve changing states of basis species


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2017-10-21 03:56:25 UTC (rev 263)
+++ pkg/CHNOSZ/DESCRIPTION	2017-10-21 15:34:45 UTC (rev 264)
@@ -1,6 +1,6 @@
 Date: 2017-10-21
 Package: CHNOSZ
-Version: 1.1.0-62
+Version: 1.1.0-63
 Title: Thermodynamic Calculations for Geobiochemistry
 Author: Jeffrey Dick
 Maintainer: Jeffrey Dick <j3ffdick at gmail.com>

Modified: pkg/CHNOSZ/R/basis.R
===================================================================
--- pkg/CHNOSZ/R/basis.R	2017-10-21 03:56:25 UTC (rev 263)
+++ pkg/CHNOSZ/R/basis.R	2017-10-21 15:34:45 UTC (rev 264)
@@ -161,10 +161,19 @@
         }
         thermo$basis$logact[ib] <- state[i]
       } else {
-        # look for a species with this name in the requested state
-        ispecies <- suppressMessages(info(thermo$obigt$name[thermo$basis$ispecies[ib]], state[i], check.it=FALSE))
-        if(is.na(ispecies) | is.list(ispecies)) 
-          stop(paste("state or buffer '", state[i], "' not found for ", thermo$obigt$name[thermo$basis$ispecies[ib]], "\n", sep=""))
+        # first, look for a species with the same _name_ in the requested state
+        myname <- thermo$obigt$name[thermo$basis$ispecies[ib]]
+        ispecies <- suppressMessages(info(myname, state[i], check.it=FALSE))
+        if(is.na(ispecies) | is.list(ispecies)) {
+          # if that failed, look for a species with the same _formula_ in the requested state
+          myformula <- thermo$obigt$formula[thermo$basis$ispecies[ib]]
+          ispecies <- suppressMessages(info(myformula, state[i], check.it=FALSE))
+          if(is.na(ispecies) | is.list(ispecies)) {
+            # if that failed, we're out of luck
+            if(myname==myformula) nametxt <- myname else nametxt <- paste(myname, "or", myformula)
+            stop(paste0("state or buffer '", state[i], "' not found for ", nametxt, "\n"))
+          }
+        }
         thermo$basis$ispecies[ib] <- ispecies
         thermo$basis$state[ib] <- state[i]
       }

Modified: pkg/CHNOSZ/R/subcrt.R
===================================================================
--- pkg/CHNOSZ/R/subcrt.R	2017-10-21 03:56:25 UTC (rev 263)
+++ pkg/CHNOSZ/R/subcrt.R	2017-10-21 15:34:45 UTC (rev 264)
@@ -8,6 +8,7 @@
 #source("info.R")
 #source("util.units.R")
 #source("util.data.R")
+#source("species.R")
 
 subcrt <- function(species, coeff = 1, state = NULL, property = c("logK", "G", "H", "S", "V", "Cp"),
   T = seq(273.15, 623.15, 25), P = "Psat", grid = NULL, convert = TRUE, exceed.Ttr = FALSE,
@@ -533,13 +534,15 @@
     }
     # 20120114 only prepend T, P, rho columns if we have more than one T
     # 20171020 or if the 'property' argument is missing (it's nice to see everything using e.g. subcrt("H2O", T=150))
-    if(length(T) > 1 | missing(property)) {
+    # 20171021 or if the 'property' argument is not missing, but is identical to the default (happens when auto-balancing reactions)
+    if(length(T) > 1 | missing(property) | identical(property, c("logK", "G", "H", "S", "V", "Cp"))) {
       # 20090329 added checks for converting T, P units
       if(convert) T.out <- outvert(T,"K") else T.out <- T
       if(convert) P.out <- outvert(P,"bar") else P.out <- P
       # try to stuff in a column of rho if we have aqueous species
       # watch out! supcrt-ish densities are in g/cc not kg/m3
-      if('rho' %in% calcprop | (missing(property) & any(c(isaq,isH2O))) & (names(out)[i])!='polymorph') 
+      if('rho' %in% calcprop | ( (missing(property) | identical(property, c("logK", "G", "H", "S", "V", "Cp"))) &
+                                any(c(isaq,isH2O))) & (names(out)[i])!='polymorph') 
         out[[i]] <- cbind(data.frame(T=T.out,P=P.out,rho=H2O.PT$rho/1000),out[[i]])
       else
         out[[i]] <- cbind(data.frame(T=T.out,P=P.out,out[[i]]))

Modified: pkg/CHNOSZ/tests/testthat/test-basis.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-basis.R	2017-10-21 03:56:25 UTC (rev 263)
+++ pkg/CHNOSZ/tests/testthat/test-basis.R	2017-10-21 15:34:45 UTC (rev 264)
@@ -31,10 +31,14 @@
   expect_equal(basis(), b)
 })
 
-test_that("changing state maintains species name", {
+test_that("modifying states of basis species is possible", {
   b1 <- basis(c("Al2O3", "quartz", "oxygen"))
   b2 <- basis("SiO2", "cr2")
   # we went from quartz cr to cr2, which is the next row in the database
   expect_equal(sum(b2$ispecies - b1$ispecies), 1)
   expect_error(basis("SiO2", "cr3"), "state or buffer 'cr3' not found for quartz")
+  # can we go from CO2(aq) to CO2(gas) back to CO2(aq)?
+  basis("CHNOS+")  # first basis species is CO2(aq)
+  expect_equal(basis("CO2", "gas")$state[1], "gas")
+  expect_equal(basis("CO2", "aq")$state[1], "aq")
 })

Modified: pkg/CHNOSZ/vignettes/anintro.Rmd
===================================================================
--- pkg/CHNOSZ/vignettes/anintro.Rmd	2017-10-21 03:56:25 UTC (rev 263)
+++ pkg/CHNOSZ/vignettes/anintro.Rmd	2017-10-21 15:34:45 UTC (rev 264)
@@ -1946,7 +1946,7 @@
 
 As you get started writing your own code and functions that use CHNOSZ, it is not uncommon to encounter problems.
 For example, mixing data types can cause problems (the important difference between factor and character was mentioned [above](#adding-data)).
-There are many tricks to learn; becoming familiar with functions like R's `lapply()`, `rbind()`, and `do.call()` will make your programming life easier.
+There are many tricks to learn; becoming familiar with functions like R's `lapply()`, `rbind()`, and `do.call()` will make your programming life more productive and enjoyable.
 
 Some functions in CHNOSZ perform "sanity checks" on the arguments and will report errors if an inconsistency is detected.
 For example, unequal lengths of variables in the transect mode of <span style="color:green">`affinity()`</span> (when the variables have more than 3 values) cause an error:

Modified: pkg/CHNOSZ/vignettes/obigt.Rmd
===================================================================
--- pkg/CHNOSZ/vignettes/obigt.Rmd	2017-10-21 03:56:25 UTC (rev 263)
+++ pkg/CHNOSZ/vignettes/obigt.Rmd	2017-10-21 15:34:45 UTC (rev 264)
@@ -215,13 +215,13 @@
 ## Optional Data {.tabset .tabset-pills}
 
 ```{r Optional_Data, results="asis", echo=FALSE}
-cat("These files contain optional data updates that replace and/or may be inconsistent with other entries in the default database. Use e.g. `add.obigt('SUPCRTBL')` to load the data.\n\n")
+cat("These files contain optional data updates that replace and/or may be inconsistent with other entries in the default database. Use e.g. <span style='color:red'>`add.obigt('SUPCRTBL')`</span> to load the data.\n\n")
 ```
 
 ### `r setfile("DEW_aq.csv")`
 ```{r DEW_aq, results="asis", echo=FALSE}
 cat("The [Deep Earth Water](http://www.dewcommunity.org/) (DEW) model extends the applicability of the revised HKF equations of state to 60 kbar. Accuracy of the thermodynamic calculations at these conditions is improved by revised correlations for the <i>a</i><sub>1</sub> HKF parameter, as described by [Sverjensky et al., 2014](https://doi.org/10.1016/j.gca.2013.12.019). The data here were taken from the May 2017 version of the DEW spreadsheet ([Dew Model, 2017](http://www.dewcommunity.org/resources.html)). The following species are present in the spreadsheet, but are not listed in `DEW_aq.csv` because the parameters are unchanged from the default database in CHNOSZ: B(OH)<sub>3</sub>, Br<sup>-</sup>, Ca<sup>+2</sup>, Cl<sup>-</sup>, Cs<sup>+</sup>, F<sup>-</sup>, H<sup>+</sup>, H<sub>2</sub>, He, I<sup>-</sup>, K<sup>+</sup>, Kr, Li<sup>+</sup>, Mg<sup>+2</sup>, Na<sup>+</sup>, Ne, O<sub>2</sub>, Rb<sup>+</sup>, Rn.\n\n")
-cat("Run [<span style='color:blue'>`demo(DEW)`</span>](../demo) for some examples.<hr>")
+cat("Besides using <span style='color:red'>`add.obigt('DEW')`</span> to load these data, you should also run <span style='color:red'>`water('DEW')`</span> to activate the DEW equations in CHNOSZ. See [<span style='color:blue'>`demo(DEW)`</span>](../demo) for some examples.<hr>")
 ```
 
 ```{r optused, include=FALSE}



More information about the CHNOSZ-commits mailing list