[CHNOSZ-commits] r713 - in pkg/CHNOSZ: . R demo inst inst/extdata/thermo inst/tinytest man vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Mar 25 05:26:51 CET 2022


Author: jedick
Date: 2022-03-25 05:26:50 +0100 (Fri, 25 Mar 2022)
New Revision: 713

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/logB_to_OBIGT.R
   pkg/CHNOSZ/R/nonideal.R
   pkg/CHNOSZ/R/protein.info.R
   pkg/CHNOSZ/R/subcrt.R
   pkg/CHNOSZ/R/swap.basis.R
   pkg/CHNOSZ/R/util.units.R
   pkg/CHNOSZ/demo/AD.R
   pkg/CHNOSZ/demo/E_coli.R
   pkg/CHNOSZ/demo/TCA.R
   pkg/CHNOSZ/demo/adenine.R
   pkg/CHNOSZ/demo/affinity.R
   pkg/CHNOSZ/demo/comproportionation.R
   pkg/CHNOSZ/demo/lambda.R
   pkg/CHNOSZ/demo/protein.equil.R
   pkg/CHNOSZ/inst/CHECKLIST
   pkg/CHNOSZ/inst/NEWS.Rd
   pkg/CHNOSZ/inst/extdata/thermo/opt.csv
   pkg/CHNOSZ/inst/tinytest/test-AD.R
   pkg/CHNOSZ/inst/tinytest/test-Berman.R
   pkg/CHNOSZ/inst/tinytest/test-DEW.R
   pkg/CHNOSZ/inst/tinytest/test-info.R
   pkg/CHNOSZ/inst/tinytest/test-ionize.aa.R
   pkg/CHNOSZ/inst/tinytest/test-logmolality.R
   pkg/CHNOSZ/inst/tinytest/test-nonideal.R
   pkg/CHNOSZ/inst/tinytest/test-protein.info.R
   pkg/CHNOSZ/inst/tinytest/test-subcrt.R
   pkg/CHNOSZ/inst/tinytest/test-util.data.R
   pkg/CHNOSZ/man/Berman.Rd
   pkg/CHNOSZ/man/add.OBIGT.Rd
   pkg/CHNOSZ/man/subcrt.Rd
   pkg/CHNOSZ/man/thermo.Rd
   pkg/CHNOSZ/man/util.units.Rd
   pkg/CHNOSZ/vignettes/anintro.Rmd
   pkg/CHNOSZ/vignettes/multi-metal.Rmd
Log:
Change default E.units() to Joules


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/DESCRIPTION	2022-03-25 04:26:50 UTC (rev 713)
@@ -1,6 +1,6 @@
-Date: 2022-03-24
+Date: 2022-03-25
 Package: CHNOSZ
-Version: 1.4.3-5
+Version: 1.4.3-6
 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/logB_to_OBIGT.R
===================================================================
--- pkg/CHNOSZ/R/logB_to_OBIGT.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/R/logB_to_OBIGT.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -23,6 +23,11 @@
   Gr <- convert(logB, "G", TK)
   # logK0 gives values for ΔG°r of the reaction with ΔG°f = 0 for the formed species
   Gr0 <- convert(logK0, "G", TK)
+  # TODO: fix convert() so that it uses Joules
+  if(E.units() == "J") {
+    Gr <- convert(Gr, "J")
+    Gr0 <- convert(Gr0, "J")
+  }
   # Calculate ΔG°f of the formed species
   Gf <- Gr - Gr0
 

Modified: pkg/CHNOSZ/R/nonideal.R
===================================================================
--- pkg/CHNOSZ/R/nonideal.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/R/nonideal.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -4,7 +4,7 @@
 # added Helgeson method 20171012
 
 nonideal <- function(species, speciesprops, IS, T, P, A_DH, B_DH, m_star=NULL, method=thermo()$opt$nonideal) {
-  # generate nonideal contributions to thermodynamic properties
+  # Generate nonideal contributions to thermodynamic properties
   # number of species, same length as speciesprops list
   # T in Kelvin, same length as nrows of speciespropss
   # arguments A_DH and B_DH are needed for all methods other than "Alberty", and P is needed for "bgamma"
@@ -16,7 +16,7 @@
     mettext
   }
 
-  # we can use this function to change the nonideal method option
+  # We can use this function to change the nonideal method option
   if(missing(speciesprops)) {
     if(species[1] %in% c("Bdot", "Bdot0", "bgamma", "bgamma0", "Alberty")) {
       thermo <- get("thermo", CHNOSZ)
@@ -28,23 +28,26 @@
     } else stop(species[1], " is not a valid nonideality setting (Bdot, Bdot0, bgamma, bgamma0, or Alberty)")
   }
 
-  # check if we have a valid method setting
+  # Check if we have a valid method setting
   if(!method %in% c("Alberty", "Bdot", "Bdot0", "bgamma", "bgamma0")) {
     if(missing(method)) stop("invalid setting (", thermo$opt$nonideal, ") in thermo()$opt$nonideal")
     else stop("invalid method (", thermo$opt$nonideal, ")")
   }
 
-  # function to calculate extended Debye-Huckel equation and derivatives using Alberty's parameters
+  R <- 1.9872  # gas constant, cal K^-1 mol^-1
+  #R <- 8.31446261815324  # gas constant, J K^-1 mol^-1  20220325
+
+  # Function to calculate extended Debye-Huckel equation and derivatives using Alberty's parameters
   Alberty <- function(prop = "loggamma", Z, I, T) {
-    # extended Debye-Huckel equation ("log")
+    # Extended Debye-Huckel equation ("log")
     # and its partial derivatives ("G","H","S","Cp")
     # T in Kelvin
     B <- 1.6 # L^0.5 mol^-0.5 (Alberty, 2003 p. 47)
-    # equation for A from Clarke and Glew, 1980
+    # Equation for A from Clarke and Glew, 1980
     #A <- expression(-16.39023 + 261.3371/T + 3.3689633*log(T)- 1.437167*(T/100) + 0.111995*(T/100)^2)
     # A = alpha / 3 (Alberty, 2001)
     alpha <- expression(3 * (-16.39023 + 261.3371/T + 3.3689633*log(T)- 1.437167*(T/100) + 0.111995*(T/100)^2))
-    ## equation for alpha from Alberty, 2003 p. 48
+    ## Equation for alpha from Alberty, 2003 p. 48
     #alpha <- expression(1.10708 - 1.54508E-3 * T + 5.95584E-6 * T^2)
     # from examples for deriv() to take first and higher-order derivatives
     DD <- function(expr, name, order = 1) {
@@ -54,7 +57,6 @@
     }
     # Alberty, 2003 Eq. 3.6-1
     lngamma <- function(alpha, Z, I, B) - alpha * Z^2 * I^(1/2) / (1 + B * I^(1/2))
-    R <- 1.9872  # gas constant, cal K^-1 mol^-1
     # 20171013 convert lngamma to common logarithm
     # 20190603 use equations for H, S, and Cp from Alberty, 2001 (doi:10.1021/jp011308v)
     if(prop=="loggamma") return(lngamma(eval(alpha), Z, I, B) / log(10))
@@ -64,24 +66,22 @@
     else if(prop=="Cp") return(- 2 * R * T * lngamma(eval(DD(alpha, "T", 1)), Z, I, B) - R * T^2 * lngamma(eval(DD(alpha, "T", 2)), Z, I, B))
   }
   
-  # function for Debye-Huckel equation with b_gamma or B-dot extended term parameter (Helgeson, 1969)
+  # Function for Debye-Huckel equation with b_gamma or B-dot extended term parameter (Helgeson, 1969)
   Helgeson <- function(prop = "loggamma", Z, I, T, A_DH, B_DH, acirc, m_star, bgamma) {
     loggamma <- - A_DH * Z^2 * I^0.5 / (1 + acirc * B_DH * I^0.5) - log10(1 + 0.0180153 * m_star) + bgamma * I
-    R <- 1.9872  # gas constant, cal K^-1 mol^-1
     if(prop=="loggamma") return(loggamma)
     else if(prop=="G") return(R * T * log(10) * loggamma)
     # note the log(10) (=2.303) ... use natural logarithm to calculate G
   }
 
-  # function for Setchenow equation with b_gamma or B-dot extended term parameter (Shvarov and Bastrakov, 1999)  20181106
+  # Function for Setchenow equation with b_gamma or B-dot extended term parameter (Shvarov and Bastrakov, 1999)  20181106
   Setchenow <- function(prop = "loggamma", I, T, m_star, bgamma) {
     loggamma <- - log10(1 + 0.0180153 * m_star) + bgamma * I
-    R <- 1.9872  # gas constant, cal K^-1 mol^-1
     if(prop=="loggamma") return(loggamma)
     else if(prop=="G") return(R * T * log(10) * loggamma)
   }
 
-  # get species indices
+  # Get species indices
   if(!is.numeric(species[[1]])) species <- info(species, "aq")
   # loop over species #1: get the charge
   Z <- numeric(length(species))
@@ -111,7 +111,7 @@
       "Th+4"=11, "Zr+4"=11, "Ce+4"=11, "Sn+4"=11)
     acirc <- as.numeric(acircdat[formula])
     acirc[is.na(acirc)] <- 4.5
-    ## make a message
+    ## Make a message
     #nZ <- sum(Z!=0)
     #if(nZ > 1) message("nonideal: using ", paste(acirc[Z!=0], collapse=" "), " for ion size parameters of ", paste(formula[Z!=0], collapse=" "))
     #else if(nZ==1) message("nonideal: using ", acirc[Z!=0], " for ion size parameter of ", formula[Z!=0])
@@ -121,11 +121,11 @@
     # "distance of closest approach" of ions in NaCl solutions (HKF81 Table 2)
     acirc <- rep(3.72e-8, length(species))
   }
-  # get b_gamma or B-dot
+  # Get b_gamma or B-dot
   if(method=="bgamma") bgamma <- bgamma(convert(T, "C"), P)
   else if(method=="Bdot") bgamma <- Bdot(convert(T, "C"))
   else if(method %in% c("Bdot0", "bgamma0")) bgamma <- 0
-  # loop over species #2: activity coefficient calculations
+  # Loop over species #2: activity coefficient calculations
   if(is.null(m_star)) m_star <- IS
   iH <- info("H+")
   ie <- info("e-")
@@ -133,11 +133,11 @@
   icharged <- ineutral <- logical(length(species))
   for(i in 1:length(species)) {
     myprops <- speciesprops[[i]]
-    # to keep unit activity coefficients of the proton and electron
+    # To keep unit activity coefficients of the proton and electron
     if(species[i] == iH & get("thermo", CHNOSZ)$opt$ideal.H) next
     if(species[i] == ie & get("thermo", CHNOSZ)$opt$ideal.e) next
     didcharged <- didneutral <- FALSE
-    # logic for neutral and charged species 20181106
+    # Logic for neutral and charged species 20181106
     if(Z[i]==0) {
       for(j in 1:ncol(myprops)) {
         pname <- colnames(myprops)[j]
@@ -163,7 +163,7 @@
         }
       }
     }
-    # append a loggam column if we did any calculations of adjusted thermodynamic properties
+    # Append a loggam column if we did any calculations of adjusted thermodynamic properties
     if(didcharged) {
       if(method=="Alberty") myprops <- cbind(myprops, loggam = Alberty("loggamma", Z[i], IS, T))
       else myprops <- cbind(myprops, loggam = Helgeson("loggamma", Z[i], IS, T, A_DH, B_DH, acirc[i], m_star, bgamma))
@@ -172,7 +172,7 @@
       if(get("thermo", CHNOSZ)$opt$Setchenow == "bgamma") myprops <- cbind(myprops, loggam = Setchenow("loggamma", IS, T, m_star, bgamma))
       else if(get("thermo", CHNOSZ)$opt$Setchenow == "bgamma0") myprops <- cbind(myprops, loggam = Setchenow("loggamma", IS, T, m_star, bgamma = 0))
     }
-    # save the calculated properties and increment progress counters
+    # Save the calculated properties and increment progress counters
     speciesprops[[i]] <- myprops
     if(didcharged) icharged[i] <- TRUE
     if(didneutral) ineutral[i] <- TRUE

Modified: pkg/CHNOSZ/R/protein.info.R
===================================================================
--- pkg/CHNOSZ/R/protein.info.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/R/protein.info.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -149,6 +149,8 @@
 }
 
 protein.equil <- function(protein, T=25, loga.protein=0, digits=4) {
+  # For now we have to use calories 20220325
+  if(thermo()$opt$E.units != "cal") stop('please run E.units("cal") first')
   out <- character()
   mymessage <- function(...) {
     message(...)

Modified: pkg/CHNOSZ/R/subcrt.R
===================================================================
--- pkg/CHNOSZ/R/subcrt.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/R/subcrt.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -582,7 +582,7 @@
     }
     if(convert) {
       for(j in 1:ncol(OUT[[i]])) {
-        if(colnames(OUT[[i]])[j] %in% c('G','H','S','Cp')) OUT[[i]][,j] <- outvert(OUT[[i]][,j],'cal')
+        if(colnames(OUT[[i]])[j] %in% c("G", "H", "S", "Cp")) OUT[[i]][,j] <- outvert(OUT[[i]][,j], "cal")
       }
     }
   }

Modified: pkg/CHNOSZ/R/swap.basis.R
===================================================================
--- pkg/CHNOSZ/R/swap.basis.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/R/swap.basis.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -18,9 +18,10 @@
   # the standard Gibbs energies of the basis species
   # don't take it from thermo()$OBIGT, even at 25 degC, because G for H2O is NA there
   # the sapply(..., "[", 1) is needed to get the first value, in case subcrt appends a polymorph column (i.e. for S(cr))  20171105
-  G <- unlist(sapply(subcrt(basis$ispecies, T=T, property="G")$out, "[", 1))
+  TK <- convert(T, "K")
+  G <- unlist(sapply(subcrt(basis$ispecies, T = TK, property="G", convert = FALSE)$out, "[", 1))
   # chemical potentials of the basis species
-  species.mu <- G - convert(basis$logact, "G", T=convert(T, "K"))
+  species.mu <- G - convert(basis$logact, "G", T = TK)
   # chemical potentials of the elements
   element.mu <- solve(basis.mat, species.mu)
   # give them useful names
@@ -41,12 +42,13 @@
   # the standard Gibbs energies of the basis species
   # don't take it from thermo()$OBIGT, even at 25 degC, because G for H2O is NA there
   # the sapply(..., "[", 1) is needed to get the first value, in case subcrt appends a polymorph column (i.e. for S(cr))  20171105
-  G <- unlist(sapply(subcrt(basis$ispecies, T=T, property="G")$out, "[", 1))
+  TK <- convert(T, "K")
+  G <- unlist(sapply(subcrt(basis$ispecies, T = TK, property = "G", convert = FALSE)$out, "[", 1))
   # the chemical potentials of the basis species in equilibrium
   # with the chemical potentials of the elements
   basis.mu <- colSums((t(basis.mat)*emu)) - G
   # convert chemical potentials to logarithms of activity
-  basis.logact <- -convert(basis.mu, "logK", T=convert(T, "K"))
+  basis.logact <- -convert(basis.mu, "logK", T = TK)
   # give them useful names
   names(basis.logact) <- rownames(basis.mat)
   return(basis.logact)

Modified: pkg/CHNOSZ/R/util.units.R
===================================================================
--- pkg/CHNOSZ/R/util.units.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/R/util.units.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -101,6 +101,7 @@
   }
   else if(units %in% c('g','logk')) {
     R <- 1.9872  # gas constant, cal K^-1 mol^-1
+    #R <- 8.31446261815324  # gas constant, J K^-1 mol^-1  20220325
     if(units=='logk') value <- value / (-log(10) * R * T)
     if(units=='g') value <- value * (-log(10) * R * T)
   }
@@ -141,7 +142,7 @@
     if(units=='k' & opt$T.units=='C') return(convert(value,'c'))
   }
   if(units %in% c('j','cal')) {
-    if(units=='j' & opt$E.units=='Cal') return(convert(value,'cal'))
+    if(units=='j' & opt$E.units=='cal') return(convert(value,'cal'))
     if(units=='cal' & opt$E.units=='J') return(convert(value,'j'))
   }
   if(units %in% c('bar','mpa')) {

Modified: pkg/CHNOSZ/demo/AD.R
===================================================================
--- pkg/CHNOSZ/demo/AD.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/demo/AD.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -5,8 +5,6 @@
 
 # Start with default settings
 reset()
-# Change energy units to Joules (affects plot labels and subcrt() output)
-E.units("J")
 
 ########################
 ### HENRY'S CONSTANT ###

Modified: pkg/CHNOSZ/demo/E_coli.R
===================================================================
--- pkg/CHNOSZ/demo/E_coli.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/demo/E_coli.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -49,7 +49,6 @@
 # Calculate polymerization contribution
 # Standard Gibbs energy (J / mol) for AABB -> PBB + H2O
 # (Figure 4 of Amend et al., 2013)
-E.units("J")
 G0.AABB_to_PBB_plus_H2O <- subcrt(c("[AABB]", "[UPBB]", "H2O"), c(-1, 1, 1), T = T)$out$G
 # Standard Gibbs energy for 278 AA -> P[278] + 277H2O
 G0.P278 <- 277 * G0.AABB_to_PBB_plus_H2O
@@ -136,7 +135,7 @@
 plot_G("CH4", "NO3-", "SO4-2")
 plot_G("CH4", "NH4+", "HS-")
 
-# Reset CHNOSZ settings (units and OBIGT database)
-reset()
+# Restore OBIGT database
+OBIGT()
 # Reset plot settings
 par(opar)

Modified: pkg/CHNOSZ/demo/TCA.R
===================================================================
--- pkg/CHNOSZ/demo/TCA.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/demo/TCA.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -4,6 +4,9 @@
 # the citric acid cycle for temperatures to 500 degrees C and pressures to 5 kbar.
 library(CHNOSZ)
 
+# These plots use calories 20220325
+E.units("cal")
+
 # species in reactions
 NADox <- "NAD(ox)-"; NADred <- "NAD(red)-2"
 ADP <- "ADP-3"; ATP <- "ATP-4"
@@ -96,3 +99,6 @@
 text(-70, 284, "Citric Acid Cycle, after Canovas and Shock, 2016", font=2, cex=1.5)
 par(xpd=FALSE)
 par(opar)
+
+# Reset the units
+reset()

Modified: pkg/CHNOSZ/demo/adenine.R
===================================================================
--- pkg/CHNOSZ/demo/adenine.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/demo/adenine.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -1,8 +1,8 @@
 # CHNOSZ/demos/adenine.R
-# plot thermodynamic data and model fits for aqueous adenine 20170725
+# Plot thermodynamic data and model fits for aqueous adenine 20170725
 library(CHNOSZ)
 
-# notable functions in this demo:
+# Notable functions in this demo:
 # EOSregress() - to regress HKF coefficients from Cp data
 # mod.OBIGT() - to modify the thermodynamic database for comparisons with an older set of parameters for adenine
 
@@ -10,7 +10,7 @@
 # LH06 = LaRowe and Helgeson, 2006 (Geochim. Cosmochim. Acta, doi:10.1016/j.gca.2006.04.010)
 # HKF = Helgeson-Kirkham-Flowers equations (e.g. Am. J. Sci., doi:10.2475/ajs.281.10.1249)
 
-# adenine Cp0 and V0 from LCT17 Table 4
+# Cp0 and V0 of adenine from LCT17 Table 4
 AdH <- data.frame(
   T = seq(288.15, 363.15, 5),
   V = c(89.1, 89.9, 90.6, 91.3, 92, 92.7, 93.1, 93.6, 94.1, 94.9, 95.4, 95.9, 96.3, 96.9, 97.1, 97.8),
@@ -18,7 +18,7 @@
   Cp = c(207, 212, 216, 220, 224, 227, 230, 234, 237, 241, 243, 245, 248, 250, 252, 255),
   Cp_SD = c(5, 7, 8, 7, 8, 7, 6, 7, 6, 5, 6, 6, 5, 4, 7, 5)
 )
-# functions to calculate V and Cp using density model (LCT17 Eq. 28)
+# Functions to calculate V and Cp using density model (LCT17 Eq. 28)
 Vfun <- function(v1, v2, k, T) {
   # gas constant (cm3 bar K-1 mol-1)
   R <- 83.144598
@@ -33,22 +33,21 @@
   daldT <- water("daldT", TK)$daldT
   c1 + c2 / (T - 228) ^ 2 - k * R * T * daldT
 }
-# set up units (used for axis labels and HKF calculations)
-E.units("J")
+# Aet up units (used for axis labels and HKF calculations)
 T.units("K")
-# temperature and pressure points for calculations
+# Temperature and pressure points for calculations
 TK <- seq(275, 425)
 P <- water("Psat", TK)$Psat
-# set up plots
+# Set up plots
 opar <- par(no.readonly = TRUE)
 layout(matrix(1:3), heights=c(1, 8, 8))
-# title at top
+# Title at top
 par(mar=c(0, 0, 0, 0), cex=1)
 plot.new()
 text(0.5, 0.5, "Heat capacity and volume of aqueous adenine\n(Lowe et al., 2017)", font=2)
-# settings for plot areas
+# Settings for plot areas
 par(mar = c(4, 4, 0.5, 1), mgp = c(2.4, 0.5, 0))
-# location of x-axis tick marks
+# Location of x-axis tick marks
 xaxp <- c(275, 425, 6)
 
 ### Cp plot (LCT17 Figures 4 and 12) ###
@@ -56,9 +55,9 @@
      xlab = axis.label("T"), ylab = axis.label("Cp0"), 
      pch = 5, tcl = 0.3, xaxs = "i", yaxs = "i", las = 1, xaxp = xaxp
 )
-# error bars (arrows trick from https://stackoverflow.com/questions/13032777/scatter-plot-with-error-bars)
+# Error bars (arrows trick from https://stackoverflow.com/questions/13032777/scatter-plot-with-error-bars)
 arrows(AdH$T, AdH$Cp - AdH$Cp_SD, AdH$T, AdH$Cp + AdH$Cp_SD, length = 0.05, angle = 90, code = 3)
-# get LH06 predictions using HKF model;
+# Get LH06 predictions using HKF model;
 # this version of adenine parameters has been superseded by LCT17,
 # so we add them by hand
 mod.OBIGT("adenine-old", formula="C5H5N5", a1=21.5046, a2=8.501, a3=-2.6632, a4=-5.3561, c1=87.88, c2=-15.87, omega=0.065)
@@ -67,22 +66,22 @@
 # density model (parameters from LCT17 Table 11)
 lines(TK, Cpfun(160.4, -653239, -7930.3, TK), lty = 2)
 
-# regress HKF parameters
-# specify the terms in the HKF equations
+# Regress HKF parameters
+# Specify the terms in the HKF equations
 var <- c("invTTheta2", "TXBorn")
-# build a data frame with T, P, and Cp columns
+# Build a data frame with T, P, and Cp columns
 Cpdat <- data.frame(T = AdH[, "T"], P = 1, Cp = AdH[, "Cp"])
-# convert Cp data from J to cal
+# Convert Cp data from J to cal
 Cpdat$Cp <- convert(Cpdat$Cp, "cal")
-# regress HKF parameters from Cp data
+# Regress HKF parameters from Cp data
 HKFcoeffs <- EOSregress(Cpdat, var)$coefficients
-# get predictions from the fitted model
+# Get predictions from the fitted model
 Cpfit <- EOScalc(HKFcoeffs, TK, P)
-# plot the fitted model
+# Plot the fitted model
 lines(TK, convert(Cpfit, "J"), lwd = 2, col = "green3")
-# format coefficients for legend; use scientific notation for c2 and omega
+# Format coefficients for legend; use scientific notation for c2 and omega
 coeffs <- format(signif(HKFcoeffs, 4), scientific = TRUE)
-# keep c1 out of scientific notation
+# Keep c1 out of scientific notation
 coeffs[1] <- signif(HKFcoeffs[[1]], 4)
 ipos <- which(coeffs >= 0)
 coeffs[ipos] <- paste("+", coeffs[ipos], sep = "")
@@ -89,9 +88,9 @@
 fun.lab <- as.expression(lapply(1:length(coeffs), function(x) {
   EOSlab(names(HKFcoeffs)[x], coeffs[x])
 }))
-# add legend: regressed HKF coefficients
+# Add legend: regressed HKF coefficients
 legend("topleft", legend = fun.lab, pt.cex = 0.1, box.col = "green3")
-# add legend: lines
+# Add legend: lines
 legend("bottomright", lty = c(3, 2, 1), lwd = c(1, 1, 2), col = c("black", "black", "green3"), bty = "n",
   legend = c("HKF model (LaRowe and Helgeson, 2006)",
   "density model (Lowe et al., 2017)", "HKF model (fit using CHNOSZ)")
@@ -107,7 +106,7 @@
 arrows(AdH$T, AdH$V - AdH$V_SD, AdH$T, AdH$V + AdH$V_SD, length = 0.05, angle = 90, code = 3)
 # HKF model with coefficients from LH06
 lines(TK, LH06$V, lty = 3)
-# density model with coefficients from LCT17
+# Density model with coefficients from LCT17
 lines(TK, Vfun(73.9, -917.0, -7930.3, TK), lty = 2)
 # HKF heat capacity coefficients from LCT17
 LCT17 <- subcrt("adenine", T = TK)$out$adenine
@@ -116,7 +115,7 @@
   legend=c("HKF model (LaRowe and Helgeson, 2006)",
   "density model (Lowe et al., 2017)", "HKF model (fit by Lowe et al., 2017 using CHNOSZ)")
 )
-# reset database and computational settings
+# Reset database and computational settings
 reset()
 
 layout(matrix(1))

Modified: pkg/CHNOSZ/demo/affinity.R
===================================================================
--- pkg/CHNOSZ/demo/affinity.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/demo/affinity.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -12,7 +12,7 @@
 doplot <- function(T) {
   res <- 20
   # calculate affinity/2.303RT as a function of loga(H2) and loga(O2)
-  a <- affinity(H2=c(-10, 0, res), O2=c(-10, 0, res), T=T)
+  a <- affinity(H2 = c(-10, 0, res), O2 = c(-10, 0, res), T = T)
   # Temperature in Kelvin
   T.K <- convert(T, "K")
   # Convert dimensionless affinity (A/2.303RT) to Gibbs energy (cal/mol)
@@ -20,45 +20,39 @@
   # Convert cal/mol to kJ/mol
   GkJ <- convert(Gcal, "J")/1000
   # Now contour the values
-  xyvals <- seq(-10, 0, length.out=res)
-  contour(x=xyvals, y=xyvals, z=t(GkJ), levels=seq(-150, -250, -20),
-    labcex=1, xlab=axis.label("H2"), ylab=axis.label("O2"))
+  xyvals <- seq(-10, 0, length.out = res)
+  contour(x = xyvals, y = xyvals, z = t(GkJ), levels = seq(-150, -250, -20),
+    labcex = 1, xlab = axis.label("H2"), ylab = axis.label("O2"))
   # Show the temperature
-  legend("topleft", bg="white", cex=1,
-    legend=describe.property("T", T, digits=0, ret.val=TRUE) )
+  legend("topleft", bg = "white", cex = 1,
+    legend = describe.property("T", T, digits = 0, ret.val = TRUE) )
 }
 # Plot layout with space for title at top
 opar <- par(no.readonly = TRUE)
-layout(matrix(c(1, 1, 2, 3, 4, 5), ncol=2, byrow=TRUE), heights=c(1, 4, 4))
+layout(matrix(c(1, 1, 2, 3, 4, 5), ncol=2, byrow = TRUE), heights = c(1, 4, 4))
 
-par(mar=c(0, 0, 0, 0))
+par(mar = c(0, 0, 0, 0))
 plot.new()
 # We use subcrt() to generate a reaction for titling the plot
-rxnexpr <- describe.reaction(subcrt("H2O", 1)$reaction, states="all")
+rxnexpr <- describe.reaction(subcrt("H2O", 1)$reaction, states = "all")
 # Also in the title is the property with its units
-E.units("J")
 Gexpr <- axis.label("DGr", prefix="k")[[2]]
-text(0.5, 0.6, substitute(paste(G~~"for"~~r), list(G=Gexpr, r=rxnexpr)), cex=2)
-text(0.5, 0.2, "after Amend and Shock, 2001 Figure 7", cex=2)
+text(0.5, 0.6, substitute(paste(G~"(kJ/mol) for"~r), list(G = Gexpr, r = rxnexpr)), cex = 2)
+text(0.5, 0.2, "after Amend and Shock, 2001 Figure 7", cex = 2)
 # Now make the plots
-par(mar=c(3, 3, 0.5, 0.5), cex=1.3, mgp=c(2, 1, 0))
+par(mar = c(3, 3, 0.5, 0.5), cex = 1.3, mgp = c(2, 1, 0))
 sapply(c(25, 55, 100, 150), doplot)
 # affinity() can handle the three dimensions simultaneously
-print(affinity(H2=c(-10, 0, 3), O2=c(-10, 0, 3), T=c(25, 150, 4))$values)
-# This is so the plots in the next examples show up OK
-E.units("cal")
+print(affinity(H2 = c(-10, 0, 3), O2 = c(-10, 0, 3), T = c(25, 150, 4))$values)
 
 # Reset plot settings
 layout(matrix(1))
 par(opar)
 
-
-
-## amino acid synthesis at low and high temperatures
-## after Amend and Shock, 1998
+## Amino acid synthesis at low and high temperatures, based on:
 ##  Amend, J. P. and Shock, E. L. (1998) Energetics of amino acid synthesis in hydrothermal ecosystems.
 ##  Science 281, 1659--1662. https://doi.org/10.1126/science.281.5383.1659
-# select the basis species and species of interest
+# Select the basis species and species of interest
 # and set their activities, first for the 18 degree C case
 basis(c("H2O", "CO2", "NH4+", "H2", "H+", "H2S"),
   log10(c(1, 1e-4, 5e-8, 2e-9, 5e-9, 1e-15)))
@@ -67,12 +61,12 @@
   0.8, 1.0, 2.8, 0.5, 0.5, 4.6, 5.8, 0.6, 0.9, 2.8)/1e9))
 T <- 18
 TK <- convert(T, "K")
-# calculate A/2.303RT (dimensionless), convert to G of reaction (cal/mol)
-a <- affinity(T=T)
-G.18.cal <- convert(unlist(a$values), "G", T=TK)
-# covvert to kJ/mol
+# Calculate A/2.303RT (dimensionless), convert to G of reaction (cal/mol)
+a <- affinity(T = T)
+G.18.cal <- convert(unlist(a$values), "G", T = TK)
+# Convert to kJ/mol
 G.18.kJ <- convert(G.18.cal, "J")/1000
-# the 100 degree C case
+# The 100 degree C case
 basis(c("H2O", "CO2", "NH4+", "H2", "H+", "H2S"),
   log10(c(1, 2.2e-3, 2.9e-6, 3.4e-4, 1.9e-6, 1.6e-3)))
 species(1:20, log10(c(2.8e-9, 5.0e-10, 7.9e-10, 2.4e-9, 3.6e-10,
@@ -80,23 +74,20 @@
   3.6e-10,3.6e-10, 3.3e-9, 4.2e-9, 4.3e-10, 6.5e-10, 2.0e-9)))
 T <- 100
 TK <- convert(T, "K")
-a <- affinity(T=T)
-G.100.cal <- convert(unlist(a$values), "G", T=TK)
+a <- affinity(T = T)
+G.100.cal <- convert(unlist(a$values), "G", T = TK)
 G.100.kJ <- convert(G.100.cal, "J")/1000
-# the average oxidation states of carbon
+# Rhe average oxidation states of carbon
 Z.C <- ZC(thermo()$OBIGT$formula[thermo()$species$ispecies])
-# put everything together like Table 3 in the paper
-print(out <- data.frame(G.18=G.18.kJ, G.100=G.100.kJ, Z.C=Z.C))
-# make a plot; set units to get correct label
-E.units("J")
-plot(out$Z.C, out$G.18, pch=20, xlim=c(-1.1, 1.1), ylim=c(-200, 500), 
-  xlab=axis.label("ZC"), ylab=axis.label("DGr", prefix="k"))
-points(out$Z.C, out$G.100, col="red", pch=20)
-legend("topleft", pch=c(20, 20), col=c("black", "red"),
-  legend=describe.property(c("T", "T"), c(18, 100)))
-title(main="Amino acid synthesis, after Amend and Shock, 1998")
+# Put everything together like Table 3 in the paper
+print(out <- data.frame(G.18 = G.18.kJ, G.100 = G.100.kJ, Z.C = Z.C))
+# Make a plot; set units to get correct label
+plot(out$Z.C, out$G.18, pch = 20, xlim = c(-1.1, 1.1), ylim = c(-200, 500), 
+  xlab = axis.label("ZC"), ylab = axis.label("DGr", prefix = "k"))
+points(out$Z.C, out$G.100, col = "red", pch = 20)
+legend("topleft", pch = c(20, 20), col = c("black", "red"),
+  legend = describe.property(c("T", "T"), c(18, 100)))
+title(main = "Amino acid synthesis, after Amend and Shock, 1998")
 # 9 amino acids have negative delta Gr under hydrothermal conditions
 # (cf. AS98 with 11; we are using more recent thermodynamic data)
-stopifnot(sum(out$G.100 < 0)==9)
-# reset units to run next examples
-E.units("cal")
+stopifnot(sum(out$G.100 < 0) == 9)

Modified: pkg/CHNOSZ/demo/comproportionation.R
===================================================================
--- pkg/CHNOSZ/demo/comproportionation.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/demo/comproportionation.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -37,8 +37,7 @@
 rxn <- subcrt("S", 1)$reaction
 rxn$coeff <- rxn$coeff * 4
 rxntext <- describe.reaction(rxn)
-# Set units to get label for Delta G (kJ / mol)
-E.units("J")
+# Get label for Delta G (kJ / mol)
 DGlab <- axis.label("DGr", prefix = "k")
 
 # Calculate pK of H2S and HSO4-

Modified: pkg/CHNOSZ/demo/lambda.R
===================================================================
--- pkg/CHNOSZ/demo/lambda.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/demo/lambda.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -14,8 +14,6 @@
 T <- convert(TC, "K")
 
 labplot <- function(x) label.plot(x, xfrac=0.9, yfrac=0.1, paren=TRUE)
-# this sets the units used for making the axis labels
-E.units("J")
 Cplab <- axis.label("Cp")
 Vlab <- axis.label("V")
 Tlab <- axis.label("T")

Modified: pkg/CHNOSZ/demo/protein.equil.R
===================================================================
--- pkg/CHNOSZ/demo/protein.equil.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/demo/protein.equil.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -12,6 +12,7 @@
 # note this yields logaH2 = -4.657486
 swap.basis("O2", "H2")
 # demonstrate the steps of the equilibrium calculation
+E.units("cal")
 protein.equil(protein, loga.protein=-3)
 ## we can also look at the affinities
 # (Reaction 7, Dick and Shock, 2011)
@@ -26,5 +27,6 @@
 Aref.residue <- Astar.residue - loga.residue  # 0.446, after Eq. 16
 # A-star of the residue in natural log units (A/RT)
 log(10) * Astar.residue  # 0.4359, after Eq. 23
-# forget about the superseded group properties for whatever comes next
+
+# forget about the changed units and superseded group properties for whatever comes next
 reset()

Modified: pkg/CHNOSZ/inst/CHECKLIST
===================================================================
--- pkg/CHNOSZ/inst/CHECKLIST	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/inst/CHECKLIST	2022-03-25 04:26:50 UTC (rev 713)
@@ -85,3 +85,9 @@
 - Run doc/mklinks.sh in installed directory (adds links to HTML renditions of
   Rd files)
 
+*******
+Testing
+*******
+
+- Comment this line in test-AD.R:
+exit_file("Skipping tests so development builds on R-Forge work")

Modified: pkg/CHNOSZ/inst/NEWS.Rd
===================================================================
--- pkg/CHNOSZ/inst/NEWS.Rd	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/inst/NEWS.Rd	2022-03-25 04:26:50 UTC (rev 713)
@@ -10,8 +10,25 @@
 \newcommand{\s}{\ifelse{latex}{\eqn{_{#1}}}{\ifelse{html}{\out{<sub>#1</sub>}}{#1}}}
 \newcommand{\S}{\ifelse{latex}{\eqn{^{#1}}}{\ifelse{html}{\out{<sup>#1</sup>}}{^#1}}}
 
-\section{Changes in CHNOSZ version 1.4.3-4 (2022-03-24)}{
+\section{Changes in CHNOSZ version 1.4.3-5 (2022-03-25)}{
 
+  \subsection{MAJOR CHANGE}{
+    \itemize{
+
+      \item Units of Joules instead of calories are now used by default for the
+      thermodynamic properties output by \code{subcrt()}. That is,
+      \code{E.units("J")} is the default setting. User scripts that implicitly
+      depend on the previous default setting of \code{E.units("cal")} need to
+      be modified to produce expected output.
+
+      \item The switch to Joules is work in progress. \code{convert()} and
+      other functions still use calories, in particular for the value of the
+      gas constant. Functions will continue to be modified to make the units as
+      consistent as possible and avoid unit conversions.
+
+    }
+  }
+
   \subsection{NEW FEATURES}{
     \itemize{
 

Modified: pkg/CHNOSZ/inst/extdata/thermo/opt.csv
===================================================================
--- pkg/CHNOSZ/inst/extdata/thermo/opt.csv	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/inst/extdata/thermo/opt.csv	2022-03-25 04:26:50 UTC (rev 713)
@@ -1,2 +1,2 @@
 cutoff,E.units,T.units,P.units,state,water,G.tol,Cp.tol,V.tol,varP,IAPWS.sat,paramin,ideal.H,ideal.e,nonideal,Setchenow,Berman,maxcores,ionize.aa
-1E-10,cal,C,bar,aq,SUPCRT92,100,1,1,FALSE,liquid,1000,TRUE,TRUE,Bdot,bgamma0,NA,2,TRUE
+1E-10,J,C,bar,aq,SUPCRT92,100,1,1,FALSE,liquid,1000,TRUE,TRUE,Bdot,bgamma0,NA,2,TRUE

Modified: pkg/CHNOSZ/inst/tinytest/test-AD.R
===================================================================
--- pkg/CHNOSZ/inst/tinytest/test-AD.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/inst/tinytest/test-AD.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -1,7 +1,5 @@
 # Load default settings for CHNOSZ
 reset()
-# Set subcrt() to output values in Joules
-E.units("J")
 
 # 20220206
 info <- "Database is set up correctly"
@@ -83,6 +81,9 @@
 # The largest differences are for HCl, ethane, and B(OH)3
 expect_equal(sort(info(iaq[abs(GAD - GOBIGT) > 900])$name), sort(c("HCl", "ethane", "B(OH)3")))
 
+# This line should be commented for a released package
+exit_file("Skipping tests so development builds on R-Forge work")
+
 ## The following tests work on JMD's Linux machine "at home" but not on some CRAN machines 20220210
 if(!at_home()) exit_file("Skipping tests on CRAN")
 # This one fails on Windows (tolerance = 9 works) 20220208

Modified: pkg/CHNOSZ/inst/tinytest/test-Berman.R
===================================================================
--- pkg/CHNOSZ/inst/tinytest/test-Berman.R	2022-03-24 23:57:51 UTC (rev 712)
+++ pkg/CHNOSZ/inst/tinytest/test-Berman.R	2022-03-25 04:26:50 UTC (rev 713)
@@ -13,29 +13,29 @@
 maxdiff <- function(x, y) max(abs(y - x))
 
 info <- "high-T,P calculated properties are similar to precalculated ones"
-# Reference values for G were taken from the spreadsheet Berman_Gibbs_Free_Energies.xlsx
+# Reference values for G (cal/mol) were taken from the spreadsheet Berman_Gibbs_Free_Energies.xlsx
 #   (http://www.dewcommunity.org/uploads/4/1/7/6/41765907/sunday_afternoon_sessions__1_.zip accessed on 2017-10-03)
 T <- c(100, 100, 1000, 1000)
 P <- c(5000, 50000, 5000, 50000)
 
 # anadalusite: an uncomplicated mineral (no transitions)
-And_G <- c(-579368, -524987, -632421, -576834)
+And_G <- convert(c(-579368, -524987, -632421, -576834), "J")
 And <- subcrt("andalusite", T = T, P = P)$out[[1]]
-expect_true(maxdiff(And$G, And_G) < 7.5, info = info)
+expect_true(maxdiff(And$G, And_G) < 32, info = info)
 
 # quartz: a mineral with polymorphic transitions
-aQz_G <- c(-202800, -179757, -223864, -200109)
+aQz_G <- convert(c(-202800, -179757, -223864, -200109), "J")
 aQz <- subcrt("quartz", T = T, P = P)$out[[1]]
-expect_true(maxdiff(aQz$G[-2], aQz_G[-2]) < 1.2, info = info)
-# here, the high-P, low-T point suffers
-expect_true(maxdiff(aQz$G[2], aQz_G[2]) < 1250, info = info)
+expect_true(maxdiff(aQz$G[-2], aQz_G[-2]) < 5, info = info)
+# The high-P, low-T point suffers
+expect_true(maxdiff(aQz$G[2], aQz_G[2]) < 5200, info = info)
 
 # K-feldspar: this one has disordering effects
-Kfs_G <- c(-888115, -776324, -988950, -874777)
+Kfs_G <- convert(c(-888115, -776324, -988950, -874777), "J")
 Kfs <- subcrt("K-feldspar", T = T, P = P)$out[[1]]
-expect_true(maxdiff(Kfs$G[1:2], Kfs_G[1:2]) < 5, info = info)
+expect_true(maxdiff(Kfs$G[1:2], Kfs_G[1:2]) < 20, info = info)
 # we are less consistent with the reference values at high T
-expect_true(maxdiff(Kfs$G[3:4], Kfs_G[3:4]) < 350, info = info)
+expect_true(maxdiff(Kfs$G[3:4], Kfs_G[3:4]) < 1500, info = info)
 
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/chnosz -r 713


More information about the CHNOSZ-commits mailing list