[CHNOSZ-commits] r798 - in pkg/CHNOSZ: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 9 02:01:13 CEST 2023


Author: jedick
Date: 2023-08-09 02:01:13 +0200 (Wed, 09 Aug 2023)
New Revision: 798

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/mosaic.R
   pkg/CHNOSZ/man/mosaic.Rd
Log:
Use do.call() with argument list to call affinity() in mosaic()


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2023-07-06 00:15:11 UTC (rev 797)
+++ pkg/CHNOSZ/DESCRIPTION	2023-08-09 00:01:13 UTC (rev 798)
@@ -1,6 +1,6 @@
-Date: 2023-07-05
+Date: 2023-08-09
 Package: CHNOSZ
-Version: 2.0.0-17
+Version: 2.0.0-18
 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/mosaic.R
===================================================================
--- pkg/CHNOSZ/R/mosaic.R	2023-07-06 00:15:11 UTC (rev 797)
+++ pkg/CHNOSZ/R/mosaic.R	2023-08-09 00:01:13 UTC (rev 798)
@@ -13,21 +13,23 @@
 # Function to calculate affinities with mosaic of basis species
 mosaic <- function(bases, blend = TRUE, stable = list(), loga_aq = NULL, ...) {
 
+  # Get the arguments for affinity() before doing anything else 20230809
+  affinityargs <- list(...)
+
   # 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])
+      bargs <- bases$args
+      # We can only update arguments for affinity()
+      if(length(affinityargs) > 0) {
+        for(i in 1:length(affinityargs)) {
+          if(names(affinityargs)[i] %in% names(bargs)) bargs[[names(affinityargs)[i]]] <- affinityargs[[i]]
+          else bargs <- c(bargs, affinityargs[i])
         }
       }
-      return(do.call(mosaic, aargs))
+      return(do.call(mosaic, bargs))
     }
   }
 
@@ -35,8 +37,7 @@
   # bases can be a vector instead of a list
   if(!is.list(bases)) {
     bases <- list(bases)
-    otherargs <- list(...)
-    allargs <- c(list(bases = bases, blend = blend, stable = stable), otherargs)
+    allargs <- c(list(bases = bases, blend = blend, stable = stable), affinityargs)
     out <- do.call(mosaic, allargs)
     # Replace A.bases (affinity calculations for all groups of basis species) with backwards-compatible A.bases
     out$A.bases <- out$A.bases[[1]]
@@ -71,31 +72,31 @@
     stop("the starting basis does not have ", paste(names0[ina], collapse = " and "))
   }
 
-  ddd <- list(...)
-  if("sout" %in% names(ddd)) {
-    ddd_has_sout <- TRUE
-    # Get sout from ... (from solubility()) 20210322
-    sout <- ddd$sout
+  if("sout" %in% names(affinityargs)) {
+    affinityargs_has_sout <- TRUE
+    # Get sout from affinityargs (from solubility()) 20210322
+    sout <- affinityargs$sout
   } else {
-    ddd_has_sout <- FALSE
+    affinityargs_has_sout <- FALSE
     # Run subcrt() calculations for all basis species and formed species 20190131
-    # this avoids repeating the calculations in different calls to affinity()
-    # add all the basis species here - the formed species are already present
+    #   - This avoids repeating the calculations in different calls to affinity()
+    # Add all the basis species here - the formed species are already present
     lapply(bases, species, add = TRUE)
-    sout <- affinity(..., return.sout = TRUE)
+    sout <- do.call(affinity, c(affinityargs, list(return.sout = TRUE)))
   }
 
+
   # Calculate affinities of the basis species themselves
   A.bases <- list()
   for(i in 1:length(bases)) {
     message("mosaic: calculating affinities of basis species group ", i, ": ", paste(bases[[i]], collapse = " "))
     mysp <- species(bases[[i]])
-    # 20191111 include only aq species in total activity
+    # Include only aq species in total activity 20191111
     iaq <- mysp$state == "aq"
     # Use as.numeric in case a buffer is active 20201014
     if(any(iaq)) species(which(iaq), as.numeric(basis0$logact[ibasis0[i]]))
-    if(ddd_has_sout) A.bases[[i]] <- suppressMessages(affinity(...))
-    else A.bases[[i]] <- suppressMessages(affinity(..., sout = sout))
+    if(affinityargs_has_sout) A.bases[[i]] <- suppressMessages(do.call(affinity, affinityargs))
+    else A.bases[[i]] <- suppressMessages(do.call(affinity, c(affinityargs, list(sout = sout))))
   }
 
   # Get all combinations of basis species
@@ -128,8 +129,8 @@
     put.basis(allbases[i, ], thislogact)
     # We have to define the species using the current basis
     species(species0$ispecies, species0$logact)
-    if(ddd_has_sout) aff.species[[i]] <- suppressMessages(affinity(...))
-    else aff.species[[i]] <- suppressMessages(affinity(..., sout = sout))
+    if(affinityargs_has_sout) aff.species[[i]] <- suppressMessages(do.call(affinity, affinityargs))
+    else aff.species[[i]] <- suppressMessages(do.call(affinity, c(affinityargs, list(sout = sout))))
   }
 
   # Calculate equilibrium mole fractions for each group of basis species
@@ -153,8 +154,7 @@
         group.fraction[[i]] <- A.bases[[i]]$values
       }
     } else {
-      # For blend = FALSE, we just look at whether
-      # a basis species predominates within its group
+      # For blend = FALSE, we just look at whether a basis species predominates within its group
       if(is.null(stable[i][[1]])) {
         d <- diagram(A.bases[[i]], plot.it = FALSE, limit.water = FALSE)
         predom <- d$predominant
@@ -212,7 +212,8 @@
   }
 
   # For argument recall, include all arguments in output 20190120
-  allargs <- c(list(bases = bases, blend = blend), list(...))
+  allargs <- c(list(bases = bases, blend = blend), affinityargs)
   # Return the affinities for the species and basis species
   return(list(fun = "mosaic", args = allargs, A.species = A.species, A.bases = A.bases, E.bases = E.bases))
+
 }

Modified: pkg/CHNOSZ/man/mosaic.Rd
===================================================================
--- pkg/CHNOSZ/man/mosaic.Rd	2023-07-06 00:15:11 UTC (rev 797)
+++ pkg/CHNOSZ/man/mosaic.Rd	2023-08-09 00:01:13 UTC (rev 798)
@@ -66,7 +66,7 @@
 
 \examples{
 \dontshow{reset()}# Fe-minerals and aqueous species in Fe-S-O-H system
-# Speciate SO4-2, HSO4-, HS-, H2S as a function of Eh and pH
+# Speciate SO4-2, HSO4-, HS-, and H2S as a function of Eh and pH
 # After Garrels and Christ, 1965 Figure 7.20
 pH <- c(0, 14)
 Eh <- c(-1, 1)



More information about the CHNOSZ-commits mailing list