[CHNOSZ-commits] r359 - in pkg/CHNOSZ: . R inst

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jan 20 19:53:03 CET 2019


Author: jedick
Date: 2019-01-20 19:53:02 +0100 (Sun, 20 Jan 2019)
New Revision: 359

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/mosaic.R
   pkg/CHNOSZ/R/solubility.R
   pkg/CHNOSZ/inst/NEWS
Log:
mosaic(): implement argument recall


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2019-01-20 08:24:26 UTC (rev 358)
+++ pkg/CHNOSZ/DESCRIPTION	2019-01-20 18:53:02 UTC (rev 359)
@@ -1,6 +1,6 @@
 Date: 2019-01-20
 Package: CHNOSZ
-Version: 1.1.3-66
+Version: 1.1.3-67
 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/mosaic.R
===================================================================
--- pkg/CHNOSZ/R/mosaic.R	2019-01-20 08:24:26 UTC (rev 358)
+++ pkg/CHNOSZ/R/mosaic.R	2019-01-20 18:53:02 UTC (rev 359)
@@ -4,6 +4,25 @@
 
 # function to calculate affinities with mosaic of basis species
 mosaic <- function(bases, bases2=NULL, blend=FALSE, ...) {
+
+  # argument recall 20190120
+  # if the first argument is the result from a previous mosaic() calculation,
+  # just update the remaining arguments
+  if(is.list(bases)) {
+    if(identical(bases[1], list(fun="mosaic"))) {
+      aargs <- bases$args
+      # we can only update arguments given in ...
+      ddd <- list(...)
+      if(length(ddd) > 0) {
+        for(i in 1:length(ddd)) {
+          if(names(ddd)[i] %in% names(aargs)) aargs[[names(ddd)[i]]] <- ddd[[i]]
+          else aargs <- c(aargs, ddd[i])
+        }
+      }
+      return(do.call(mosaic, aargs))
+    }
+  }
+
   if(is.null(bases2)) {
     # the arguments for affinity()
     myargs <- list(...)
@@ -13,11 +32,13 @@
     # the arguments for mosaic() (second set of basis species; inner loop)
     myargs <- list(bases=bases2, blend=blend, ...)
   }
+
   # are the swapped basis species on the plot?
   # (the first one should be present in the starting basis set)
   iswap <- match(bases[1], names(myargs))
   # the log activity of the starting basis species
   logact.swap <- basis()$logact[ibasis(bases[1])]
+
   # a list where we'll keep the affinity calculations
   affs <- list()
   for(i in seq_along(bases)) {
@@ -46,6 +67,7 @@
       basis(bformula, logact.swap)
     }
   }
+
   # calculate affinities of formation of basis species
   message(paste("mosaic: combining diagrams for", paste(bases, collapse=" "), sep=" "))
   ispecies <- species()$ispecies
@@ -57,6 +79,7 @@
   # restore original species with original activities
   species(delete=TRUE)
   species(ispecies, species.logact)
+
   # affinities calculated using the first basis species
   A.species <- affs[[1]]
   if(blend) {
@@ -88,7 +111,10 @@
       }
     }
   }
+
+  # for argument recall, include all arguments in output 20190120
+  allargs <- c(list(bases=bases, bases2=bases2, blend=blend), list(...))
   # return the affinities for the species and basis species
-  if(is.null(bases2)) return(list(A.species=A.species, A.bases=A.bases))
-  else return(list(A.species=A.species, A.bases=A.bases, A.bases2=A.bases2))
+  if(is.null(bases2)) return(list(fun="mosaic", args=allargs, A.species=A.species, A.bases=A.bases))
+  else return(list(fun="mosaic", args=allargs, A.species=A.species, A.bases=A.bases, A.bases2=A.bases2))
 }

Modified: pkg/CHNOSZ/R/solubility.R
===================================================================
--- pkg/CHNOSZ/R/solubility.R	2019-01-20 08:24:26 UTC (rev 358)
+++ pkg/CHNOSZ/R/solubility.R	2019-01-20 18:53:02 UTC (rev 359)
@@ -11,6 +11,11 @@
   ## concept: the logarithms of activities of species at equilibrium are equal to
   ## Astar, the affinities calculated for unit activities of species
 
+  ## is aout the output from mosaic() instead of affinity()?
+  aout.save <- aout
+  thisfun <- aout$fun
+  if(thisfun=="mosaic") aout <- aout$A.species
+
   ## does the system involve a dissociation reaction?
   if(is.null(dissociation)) {
     # assume FALSE unless determined otherwise
@@ -107,10 +112,21 @@
     if(find.IS) message("solubility: (iteration ", niter, ") ionic strength range is ", paste(round(range(IS), 4), collapse=" "))
     # stop iterating if we reached the tolerance (or find.IS=FALSE)
     if(!find.IS | all(IS - IS.old < 1e-4)) break
-    # expand argument values for affinity()
-    for(i in 1:length(aout$vals)) aout$args[[i]] <- aout$vals[[i]]
+    # on the first iteration, expand argument values for affinity() or mosaic()
+    if(niter==1) {
+      if(thisfun=="affinity") for(i in 1:length(aout$vals)) {
+        aout.save$args[[i]] <- aout$vals[[i]]
+      }
+      else if(thisfun=="mosaic") {
+        for(i in 1:length(aout$vals)) {
+          argname <- names(aout$args)[i]
+          aout.save$args[[argname]] <- aout$vals[[i]]
+        }
+      }
+    }
     # recalculate the affinity using the new IS
-    aout <- suppressMessages(do.call(aout$fun, list(aout, IS = IS)))
+    aout <- suppressMessages(do.call(thisfun, list(aout.save, IS = IS)))
+    if(thisfun=="mosaic") aout <- aout$A.species
     niter <- niter + 1
   }
 

Modified: pkg/CHNOSZ/inst/NEWS
===================================================================
--- pkg/CHNOSZ/inst/NEWS	2019-01-20 08:24:26 UTC (rev 358)
+++ pkg/CHNOSZ/inst/NEWS	2019-01-20 18:53:02 UTC (rev 359)
@@ -1,4 +1,4 @@
-CHANGES IN CHNOSZ 1.1.3-66 (2019-01-20)
+CHANGES IN CHNOSZ 1.1.3-67 (2019-01-20)
 ---------------------------------------
 
 NEW FEATURE: SOLUBILITY CALCULATIONS
@@ -11,9 +11,10 @@
   finding the final ionic strength for dissolution of a mineral into
   pure water (find.IS argument).
 
-- find.IS depends on the new argument recall feature of affinity().
-  This allows a calculation to be re-run with the same settings except
-  for particular additions or modifications.
+- find.IS depends on the new argument recall feature of affinity() (or
+  mosaic() if that is used instead). This allows a calculation to be
+  re-run with the same settings except for particular additions or
+  modifications, in this case modified values of ionic strength.
 
 - Revise demo/solubility.R to show solubility calculations for CO2(gas)
   and calcite as a function of T and pH.



More information about the CHNOSZ-commits mailing list