[CHNOSZ-commits] r662 - in pkg/CHNOSZ: . R inst/extdata/OBIGT tests/testthat

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Mar 22 13:48:39 CET 2021


Author: jedick
Date: 2021-03-22 13:48:39 +0100 (Mon, 22 Mar 2021)
New Revision: 662

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/mosaic.R
   pkg/CHNOSZ/R/solubility.R
   pkg/CHNOSZ/inst/extdata/OBIGT/inorganic_cr.csv
   pkg/CHNOSZ/inst/extdata/OBIGT/refs.csv
   pkg/CHNOSZ/tests/testthat/test-solubility.R
Log:
Speed up solubility() a little


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2021-03-22 09:35:23 UTC (rev 661)
+++ pkg/CHNOSZ/DESCRIPTION	2021-03-22 12:48:39 UTC (rev 662)
@@ -1,6 +1,6 @@
 Date: 2021-03-21
 Package: CHNOSZ
-Version: 1.4.0-31
+Version: 1.4.0-32
 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	2021-03-22 09:35:23 UTC (rev 661)
+++ pkg/CHNOSZ/R/mosaic.R	2021-03-22 12:48:39 UTC (rev 662)
@@ -68,11 +68,19 @@
     stop("the starting basis does not have ", paste(names0[ina], collapse = " and "))
   }
 
-  # 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
-  lapply(bases, species, add = TRUE)
-  sout <- affinity(..., return.sout = TRUE)
+  ddd <- list(...)
+  if("sout" %in% names(ddd)) {
+    ddd_has_sout <- TRUE
+    # Get sout from ... (from solubility()) 20210322
+    sout <- ddd$sout
+  } else {
+    ddd_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
+    lapply(bases, species, add = TRUE)
+    sout <- affinity(..., return.sout = TRUE)
+  }
 
   # calculate affinities of the basis species themselves
   A.bases <- list()
@@ -83,7 +91,8 @@
     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]]))
-    A.bases[[i]] <- suppressMessages(affinity(..., sout = sout))
+    if(ddd_has_sout) A.bases[[i]] <- suppressMessages(affinity(...))
+    else A.bases[[i]] <- suppressMessages(affinity(..., sout = sout))
   }
 
   # get all combinations of basis species
@@ -104,7 +113,8 @@
     put.basis(allbases[i, ], thislogact)
     # we have to define the species using the current basis
     species(species0$ispecies, species0$logact)
-    aff.species[[i]] <- suppressMessages(affinity(..., sout = sout))
+    if(ddd_has_sout) aff.species[[i]] <- suppressMessages(affinity(...))
+    else aff.species[[i]] <- suppressMessages(affinity(..., sout = sout))
   }
 
   # calculate equilibrium mole fractions for each group of basis species

Modified: pkg/CHNOSZ/R/solubility.R
===================================================================
--- pkg/CHNOSZ/R/solubility.R	2021-03-22 09:35:23 UTC (rev 661)
+++ pkg/CHNOSZ/R/solubility.R	2021-03-22 12:48:39 UTC (rev 662)
@@ -10,6 +10,8 @@
 #source("species.R")
 #source("util.args.R")
 #source("util.character.R")
+#source("mosaic.R")
+#source("basis.R")
 
 # Function to calculate solubilities of multiple minerals 20210303
 # species() should be used first to load the minerals (all bearing the same metal)
@@ -17,11 +19,17 @@
 # '...' contains arguments for affinity() or mosaic() (i.e. plotting variables)
 solubility <- function(iaq, ..., in.terms.of = NULL, dissociate = FALSE, find.IS = FALSE) {
 
-  # If iaq is the output of affinity(), use old method 20210318
+  # If iaq is the output of affinity(), use old calling style 20210318
   if(is.list(iaq)) return(solubility_calc(aout = iaq, in.terms.of = in.terms.of, dissociate = dissociate, find.IS = find.IS))
   # Check whether to use affinity() or mosaic()
-  ddd <- list(...)
-  if(identical(names(ddd)[1], "bases")) is.mosaic <- TRUE else is.mosaic <- FALSE
+  affargs <- ddd <- list(...)
+  is.mosaic <- FALSE
+  if(identical(names(ddd)[1], "bases")) {
+    is.mosaic <- TRUE
+    # For getting 'sout' from affinity(), drop arguments specific for mosaic()
+    affargs <- ddd[-1]
+    affargs <- affargs[!names(affargs) %in% c("bases", "bases2", "stable", "blend")]
+  }
 
   # Save current thermodynamic system settings
   thermo <- get("thermo", CHNOSZ)
@@ -32,6 +40,15 @@
   mineral <- species()
   if(is.null(mineral)) stop("please load minerals or gases with species()")
 
+  if(!find.IS) {
+    # Get subcrt() output for all aqueous species and minerals 20210322
+    # Add aqueous species here - the minerals are already present
+    lapply(iaq, species, add = TRUE)
+    # Also add basis species for mosaic()!
+    if(is.mosaic) lapply(unlist(ddd$bases), species, add = TRUE)
+    sout <- suppressMessages(do.call(affinity, c(affargs, return.sout = TRUE)))
+  }
+
   # Make a list to store the calculated solubilities for each mineral
   slist <- list()
   # Loop over minerals
@@ -48,7 +65,11 @@
     if(any(is.na)) basis(rownames(basis())[is.na], logact[is.na])
     # Add aqueous species (no need to define activities here - they will be calculated by solubility_calc)
     species(iaq)
-    if(is.mosaic) aout <- suppressMessages(mosaic(...)) else aout <- suppressMessages(affinity(...))
+    if(find.IS) {
+      if(is.mosaic) aout <- suppressMessages(mosaic(...)) else aout <- suppressMessages(affinity(...))
+    } else {
+      if(is.mosaic) aout <- suppressMessages(mosaic(..., sout = sout)) else aout <- suppressMessages(affinity(..., sout = sout))
+    }
     # Calculate solubility of this mineral
     scalc <- solubility_calc(aout, in.terms.of = in.terms.of, dissociate = dissociate, find.IS = find.IS)
     # Store the solubilities in the list

Modified: pkg/CHNOSZ/inst/extdata/OBIGT/inorganic_cr.csv
===================================================================
--- pkg/CHNOSZ/inst/extdata/OBIGT/inorganic_cr.csv	2021-03-22 09:35:23 UTC (rev 661)
+++ pkg/CHNOSZ/inst/extdata/OBIGT/inorganic_cr.csv	2021-03-22 12:48:39 UTC (rev 662)
@@ -115,7 +115,7 @@
 jarosite,NA,KFe3(SO4)2(OH)6,cr,SAJ00,NA,2009-04-13,cal,-791061.2,-887930.2,92.949,NA,NA,147.44,23.599,-47.706,0,0,0,0,NA
 natrojarosite,NA,NaFe3(SO4)2(OH)6,cr,SAJ00,NA,2009-04-13,cal,-778370,-877892,91.396,NA,NA,147.321,21.8,-48.7,0,0,0,0,NA
 melanterite,NA,FeSO4*7H2O,cr,PK95,NA,2009-04-13,cal,-599366.6,-720028.7,97.801,NA,NA,0,0,0,0,0,0,0,NA
-gypsum,NA,CaSO4*2H2O,cr,RH95.7,Kel60.3,2015-11-22,cal,-429493,-483509,46.32,NA,74.69,21.84,76,0,0,0,0,0,400
+gypsum,NA,CaSO4*2H2O,cr,RH95,Kel60.3,2015-11-22,cal,-429493,-483509,46.32,NA,74.69,21.84,76,0,0,0,0,0,400
 MgSO4,NA,MgSO4,cr,WEP+82.1,NA,2015-11-24,cal,-279780,-307100,21.89,NA,NA,23.06,0,0,0,0,0,0,NA
 "arsenic,alpha",NA,As,cr,NA03,ZZL+16.1,2017-10-16,J,0,0,35.63,24.43,12.96,NA,NA,NA,NA,NA,NA,NA,NA
 arsenolite,NA,As2O3,cr,NA03,NA,2017-10-16,J,-576340,-657270,107.38,96.88,NA,NA,NA,NA,NA,NA,NA,NA,NA

Modified: pkg/CHNOSZ/inst/extdata/OBIGT/refs.csv
===================================================================
--- pkg/CHNOSZ/inst/extdata/OBIGT/refs.csv	2021-03-22 09:35:23 UTC (rev 661)
+++ pkg/CHNOSZ/inst/extdata/OBIGT/refs.csv	2021-03-22 12:48:39 UTC (rev 662)
@@ -59,7 +59,7 @@
 HSS95,"J. R. Haas, E. L. Shock and D. C. Sassani",1995,"Geochim. Cosmochim. Acta 59, 4329-4350","complexes of rare earth elements",https://doi.org/10.1016/0016-7037(95)00314-P
 PH95,"V. A. Pokrovskii and H. C. Helgeson",1995,"Am. J. Sci. 295, 1255-1342","aluminum species",https://doi.org/10.2475/ajs.295.10.1255
 PK95,"V. B. Parker and I. L. Khodakovskii",1995,"J. Phys. Chem. Ref. Data 24, 1699-1745",melanterite,https://doi.org/10.1063/1.555964
-RH95,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","manganese, manganosite, pyrolusite, bixbyite, hausmannite, heubnerite, cattierite, cobalt, and wustite",https://doi.org/10.3133/b2131
+RH95,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","gypsum, manganese, manganosite, pyrolusite, bixbyite, hausmannite, heubnerite, cattierite, cobalt, wustite, and willemite",https://doi.org/10.3133/b2131
 RH95.1,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","hydrogen fluoride and hydrogen chloride",https://doi.org/10.3133/b2131
 RH95.2,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","dawsonite: Cp coefficients corrected in @TKSS14; Cp value at 25 °C from @BPAH07, citing @FSR76",https://doi.org/10.3133/b2131
 RH95.3,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","almandine, dickite, glaucophane, grunerite, halloysite, pyrope: GHS and Cp at 25 °C",https://doi.org/10.3133/b2131
@@ -66,7 +66,6 @@
 RH95.4,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","fluorphlogopite (Al/Si disordered) (G and H not in SUPCRT92)",https://doi.org/10.3133/b2131
 RH95.5,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","larnite (G and H not in SUPCRT92); Cp from @Kel60",https://doi.org/10.3133/b2131
 RH95.6,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","bromellite (G and H not in SUPCRT92)",https://doi.org/10.3133/b2131
-RH95.7,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","gypsum GHS",https://doi.org/10.3133/b2131
 RH95.8,"R. A. Robie and B. S. Hemingway",1995,"U. S. Geological Survey Bull. 2131","linnaeite V",https://doi.org/10.3133/b2131
 SK95,"E. L. Shock and C. M. Koretsky",1995,"Geochim. Cosmochim. Acta 59, 1497-1532","metal-organic acid complexes",https://doi.org/10.1016/0016-7037(95)00058-8
 SK95.1,"E. L. Shock and C. M. Koretsky",1995,"Geochim. Cosmochim. Acta 59, 1497-1532","alanate, glycinate and their complexes with metals. Values are taken from slop98.dat, which notes corrected values for some species.",https://doi.org/10.1016/0016-7037(95)00058-8

Modified: pkg/CHNOSZ/tests/testthat/test-solubility.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-solubility.R	2021-03-22 09:35:23 UTC (rev 661)
+++ pkg/CHNOSZ/tests/testthat/test-solubility.R	2021-03-22 12:48:39 UTC (rev 662)
@@ -43,7 +43,7 @@
   expect_equal(max(abs(unlist(checkfun(99)$values))), 0)
 })
 
-test_that("backward compatible and new calling styles produce identical results", {
+test_that("backward compatible and new calling styles produce the same results", {
   # Test added 20210319
   # Calculate solubility of a single substance:
   # Gaseous S2 with a given fugacity
@@ -68,5 +68,9 @@
   a <- affinity(O2 = c(-55, -40), T = 125)
   s_old <- solubility(a, in.terms.of = "SO4-2")
 
+  # sout$species (in memoized subcrt() output) have different rownames
+  s1 <- s1[names(s1) != "sout"]
+  s_old <- s_old[names(s_old) != "sout"]
+
   expect_identical(s1, s_old)
 })



More information about the CHNOSZ-commits mailing list