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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 17 08:32:58 CET 2019


Author: jedick
Date: 2019-01-17 08:32:56 +0100 (Thu, 17 Jan 2019)
New Revision: 356

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/affinity.R
   pkg/CHNOSZ/inst/NEWS
   pkg/CHNOSZ/man/affinity.Rd
   pkg/CHNOSZ/tests/testthat/test-affinity.R
Log:
affinity(): implement argument recall


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2019-01-16 23:10:35 UTC (rev 355)
+++ pkg/CHNOSZ/DESCRIPTION	2019-01-17 07:32:56 UTC (rev 356)
@@ -1,6 +1,6 @@
-Date: 2019-01-15
+Date: 2019-01-17
 Package: CHNOSZ
-Version: 1.1.3-63
+Version: 1.1.3-64
 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-16 23:10:35 UTC (rev 355)
+++ pkg/CHNOSZ/R/affinity.R	2019-01-17 07:32:56 UTC (rev 356)
@@ -28,8 +28,27 @@
   # this is where energy.args() used to sit
   # this is where energy() used to sit
 
+  # argument recall 20190117
+  # if the first argument is the result from a previous affinity() calculation,
+  # just update the remaining arguments
+  args.orig <- list(...)
+  # we can only do anything with at least one argument
+  if(length(args.orig) > 0) {
+    if(identical(args.orig[[1]][1], list(fun="affinity"))) {
+      aargs <- args.orig[[1]]$args
+      # we can only update arguments given after the first argument
+      if(length(args.orig) > 1) {
+        for(i in 2:length(args.orig)) {
+          if(names(args.orig)[i] %in% names(aargs)) aargs[[names(args.orig)[i]]] <- args.orig[[i]]
+          else aargs <- c(aargs, args.orig[i])
+        }
+      }
+      return(do.call(affinity, aargs))
+    }
+  }
+
   # the argument list
-  args <- energy.args(list(...))
+  args <- energy.args(args.orig)
   args <- c(args, list(sout=sout, exceed.Ttr=exceed.Ttr, exceed.rhomin=exceed.rhomin))
 
   # the species we're given
@@ -219,7 +238,6 @@
     vals[[iP]] <- outvert(vals[[iP]], "bar")
   }
   # get Eh
-  args.orig <- list(...)
   iEh <- match("Eh", names(args.orig))
   if(!is.na(iEh)) {
     vars[[iEh]] <- "Eh"
@@ -247,11 +265,16 @@
 
   # content of return value depends on buffer request
   if(return.buffer) return(c(tb, list(vars=vars, vals=vals)))
+  # for argument recall, include all arguments (except sout) in output 20190117
+  allargs <- c(args.orig, list(property=property, exceed.Ttr=exceed.Ttr, exceed.rhomin=exceed.rhomin,
+    return.buffer=return.buffer, balance=balance, iprotein=iprotein, loga.protein=loga.protein))
   # add IS value only if it given as an argument 20171101
   # (even if its value is 0, the presence of IS will trigger diagram() to use "m" instead of "a" in axis labels)
   iIS <- match("IS", names(args.orig))
-  if(!is.na(iIS)) a <- list(sout=sout, property=property, basis=mybasis, species=myspecies, T=T, P=P, IS=args$IS, vars=vars, vals=vals, values=a)
-  else a <- list(sout=sout, property=property, basis=mybasis, species=myspecies, T=T, P=P, vars=vars, vals=vals, values=a)
+  if(!is.na(iIS)) a <- list(fun="affinity", args=allargs, sout=sout, property=property,
+                            basis=mybasis, species=myspecies, T=T, P=P, IS=args$IS, vars=vars, vals=vals, values=a)
+  else a <- list(fun="affinity", args=allargs, sout=sout, property=property,
+                 basis=mybasis, species=myspecies, T=T, P=P, vars=vars, vals=vals, values=a)
   if(buffer) a <- c(a, list(buffer=tb))
   return(a)
 }

Modified: pkg/CHNOSZ/inst/NEWS
===================================================================
--- pkg/CHNOSZ/inst/NEWS	2019-01-16 23:10:35 UTC (rev 355)
+++ pkg/CHNOSZ/inst/NEWS	2019-01-17 07:32:56 UTC (rev 356)
@@ -1,4 +1,4 @@
-CHANGES IN CHNOSZ 1.1.3-63 (2019-01-17)
+CHANGES IN CHNOSZ 1.1.3-64 (2019-01-17)
 ---------------------------------------
 
 NEW FEATURES
@@ -43,6 +43,9 @@
 - Add demo/bison.R (average oxidation state of carbon of metagenome-
   derived proteins in different microbial phyla at Bison Pool)
 
+- affinity(): implement argument recall, to re-run a calculation with
+  the same settings except for particular additions or modifications.
+
 THERMODYNAMIC DATA
 
 - The Berman data (Berman, 1988 and later additions) have replaced the

Modified: pkg/CHNOSZ/man/affinity.Rd
===================================================================
--- pkg/CHNOSZ/man/affinity.Rd	2019-01-16 23:10:35 UTC (rev 355)
+++ pkg/CHNOSZ/man/affinity.Rd	2019-01-17 07:32:56 UTC (rev 356)
@@ -12,7 +12,7 @@
 }
 
 \arguments{
-  \item{...}{numeric, zero or more named arguments, used to identify the variables of interest in the calculations}
+  \item{...}{numeric, zero or more named arguments, used to identify the variables of interest in the calculations. For argument recall, pass the output from a previous calculation of \code{affinity} as an unnamed first argument.}
   \item{property}{character, the property to be calculated. Default is \samp{A}, for chemical affinity of formation reactions of species of interest}
   \item{sout}{list, output from \code{\link{subcrt}}}
   \item{exceed.Ttr}{logical, allow \code{\link{subcrt}} to compute properties for phases beyond their transition temperature?}
@@ -54,10 +54,13 @@
 The properties returned are those of the formation reactions of the species of interest from the basis species.
 It is also possible to calculate the properties of the species of interest themselves (not their formation reactions) by setting the \code{property} to \samp{G.species}, \samp{Cp.species}, etc.
 Except for \samp{A}, the properties of proteins or their reactions calculated in this manner are restricted to nonionized proteins.
+
+Argument recall is invoked by passing a previous result of \code{affinity} as the first argument.
+The function then calls itself using the settings from the previous calculation, with additions or modifications indicated by the remaining arguments in the current function call.
 }
 
 \value{
-A list, elements of which are \code{sout} output from \code{\link{subcrt}}, \code{property} name of the calculated property (\samp{A} for chemical affinity), \code{basis} and \code{species} definition of basis species and species of interest in effect at runtime, \code{T} and \code{P} temperature and pressure, in the system units of Kelvin and bar, set to \code{numeric()} (length=0) if either one is a variable, \code{vars} the names of the variables, \code{vals} the values of the variables (a list, one element for each variable), \code{values} the result of the calculation (a list, one element for each species, with names taken from the species index in \code{\link{thermo}$obigt}).
+A list, elements of which are \code{fun} the name of the function (\samp{affinity}), \code{args} all of the arguments except for \samp{sout} (these are used for argument recall), \code{sout} output from \code{\link{subcrt}}, \code{property} name of the calculated property (\samp{A} for chemical affinity), \code{basis} and \code{species} definition of basis species and species of interest in effect at runtime, \code{T} and \code{P} temperature and pressure, in the system units of Kelvin and bar, set to \code{numeric()} (length=0) if either one is a variable, \code{vars} the names of the variables, \code{vals} the values of the variables (a list, one element for each variable), \code{values} the result of the calculation (a list, one element for each species, with names taken from the species index in \code{\link{thermo}$obigt}).
 The elements of the lists in \code{vals} and \code{values} are arrays of \eqn{n}{n} dimensions, where \eqn{n}{n} is the number of variables.
 The values of chemical affinity of formation reactions of the species are returned in dimensionless units (for use with decimal logarithms, i.e., \emph{A}/\eqn{2.303RT}).
 

Modified: pkg/CHNOSZ/tests/testthat/test-affinity.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-affinity.R	2019-01-16 23:10:35 UTC (rev 355)
+++ pkg/CHNOSZ/tests/testthat/test-affinity.R	2019-01-17 07:32:56 UTC (rev 356)
@@ -165,3 +165,15 @@
   expect_equal(unlist(lapply(a2$values, tail, 1)), unlist(a1$values))
   nonideal(oldnon)
 })
+
+test_that("argument recall is usable", {
+  basis("CHNOS")
+  species(c("CO2", "CH4"))
+  a0 <- affinity(O2=c(-80, -60))
+  a1 <- affinity(O2=c(-80, -60), T=100)
+  a2 <- affinity(a0, T=100)
+  a3 <- affinity(a1, T=25)
+  expect_identical(a1, a2)
+  # we don't test entire output here becuase a0 doesn't have a "T" argument
+  expect_identical(a0$values, a3$values)
+})



More information about the CHNOSZ-commits mailing list