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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Feb 25 12:28:33 CET 2019


Author: jedick
Date: 2019-02-25 12:28:33 +0100 (Mon, 25 Feb 2019)
New Revision: 413

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/AkDi.R
   pkg/CHNOSZ/R/retrieve.R
   pkg/CHNOSZ/R/species.R
   pkg/CHNOSZ/man/retrieve.Rd
Log:
retrieve(): add 'req1' argument (require first element)


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2019-02-25 01:39:51 UTC (rev 412)
+++ pkg/CHNOSZ/DESCRIPTION	2019-02-25 11:28:33 UTC (rev 413)
@@ -1,6 +1,6 @@
 Date: 2019-02-25
 Package: CHNOSZ
-Version: 1.2.0-20
+Version: 1.2.0-21
 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/AkDi.R
===================================================================
--- pkg/CHNOSZ/R/AkDi.R	2019-02-25 01:39:51 UTC (rev 412)
+++ pkg/CHNOSZ/R/AkDi.R	2019-02-25 11:28:33 UTC (rev 413)
@@ -22,7 +22,7 @@
     for(j in seq_along(property)) {
       if(property[[j]]=="G") {
         # send a message
-        message("AkDi(): Akinfiev-Diamond model for ", PAR$name, " gas to aq")
+        message("AkDi: Akinfiev-Diamond model for ", PAR$name, " gas to aq")
         # get gas properties (J mol-1)
         G_gas <- subcrt(PAR$name, "gas", T=T, P=P, convert=FALSE)$out[[1]]$G
         # TODO: does this work if E.units is cal or J?

Modified: pkg/CHNOSZ/R/retrieve.R
===================================================================
--- pkg/CHNOSZ/R/retrieve.R	2019-02-25 01:39:51 UTC (rev 412)
+++ pkg/CHNOSZ/R/retrieve.R	2019-02-25 11:28:33 UTC (rev 413)
@@ -3,7 +3,7 @@
 # 20190214 initial version
 # 20190224 use ... for multiple arguments (define a chemical system)
 
-retrieve <- function(..., state = NULL, add.charge = TRUE, hide.electron = TRUE, hide.proton = TRUE, hide.groups = TRUE) {
+retrieve <- function(..., state = NULL, add.charge = TRUE, hide.groups = TRUE, req1 = FALSE) {
   ## stoichiometric matrix
   # what are the formulas of species in the current database?
   formula <- thermo()$obigt$formula
@@ -30,6 +30,7 @@
   if(add.charge & length(args) > 1) {
     if(!"Z" %in% unlist(args)) args <- c(args, "Z")
   }
+  # for a numeric first argument, limit the result to only those species 20190225
   for(elements in args) {
     if(identical(elements, "all")) {
       ispecies <- 1:nrow(thermo()$obigt)
@@ -43,6 +44,8 @@
       # identify the species that have the elements
       has.elements <- rowSums(stoich[, elements, drop = FALSE] != 0) == length(elements)
       # which species are these (i.e. the species index)
+      # for req1, remember the species containing the first element 20190225
+      if(length(ispecies)==0) ispecies1 <- which(has.elements)
       ispecies <- c(ispecies, which(has.elements))
       ispecies <- ispecies[!duplicated(ispecies)]
     }
@@ -55,19 +58,24 @@
     iother <- rowSums(notsysstoich[ispecies, ] != 0) > 0
     ispecies <- ispecies[!iother]
   }
-  # exclude groups and electron and proton
+  # keep only species that contain the first element
+  if(req1) {
+    ispecies <- intersect(ispecies1, ispecies)
+    names(ispecies) <- thermo()$obigt$name[ispecies]
+  }
+  # exclude groups
   if(hide.groups) {
     igroup <- grepl("^\\[.*\\]$", thermo()$obigt$name[ispecies])
     ispecies <- ispecies[!igroup]
   }
-  if(hide.electron) {
-    ielectron <- names(ispecies) == "(Z-1)"
-    ispecies <- ispecies[!ielectron]
-  }
-  if(hide.proton) {
-    iproton <- names(ispecies) == "H+"
-    ispecies <- ispecies[!iproton]
-  }
+  #if(hide.electron) {
+  #  ielectron <- names(ispecies) == "(Z-1)"
+  #  ispecies <- ispecies[!ielectron]
+  #}
+  #if(hide.proton) {
+  #  iproton <- names(ispecies) == "H+"
+  #  ispecies <- ispecies[!iproton]
+  #}
   # filter states
   if(!is.null(state)) {
     istate <- thermo()$obigt$state[ispecies] %in% state

Modified: pkg/CHNOSZ/R/species.R
===================================================================
--- pkg/CHNOSZ/R/species.R	2019-02-25 01:39:51 UTC (rev 412)
+++ pkg/CHNOSZ/R/species.R	2019-02-25 11:28:33 UTC (rev 413)
@@ -90,7 +90,8 @@
   if(!is.null(iobigt)) {
     if(is.null(thermo$basis)) stop("basis species are not defined")
     # the coefficients in reactions to form the species from basis species
-    f <- (species.basis(iobigt))
+    # wrap values in unname in case they have names from retrieve(), otherwise makeup() doesn't work as intended 20190225
+    f <- (species.basis(unname(iobigt)))
     # the states and species names
     state <- as.character(thermo$obigt$state[iobigt])
     name <- as.character(thermo$obigt$name[iobigt])

Modified: pkg/CHNOSZ/man/retrieve.Rd
===================================================================
--- pkg/CHNOSZ/man/retrieve.Rd	2019-02-25 01:39:51 UTC (rev 412)
+++ pkg/CHNOSZ/man/retrieve.Rd	2019-02-25 11:28:33 UTC (rev 413)
@@ -7,17 +7,15 @@
 }
 
 \usage{
-  retrieve(..., state = NULL, add.charge = TRUE,
-    hide.electron = TRUE, hide.proton = TRUE, hide.groups = TRUE)
+  retrieve(..., state = NULL, add.charge = TRUE, hide.groups = TRUE, req1 = FALSE)
 }
 
 \arguments{
   \item{...}{list, one or more arguments, each of which is a character vector with the names of one or more chemical elements}
   \item{state}{character, filter the result on these state(s).}
   \item{add.charge}{logical, add charge to the system?}
-  \item{hide.electron}{logical, exclude the electron from the result?}
-  \item{hide.proton}{logical, exclude the proton from the result?}
   \item{hide.groups}{logical, exclude groups from the result?}
+  \item{req1}{logical, include only species that contain the first element in the system?}
 }
 
 \details{
@@ -28,14 +26,14 @@
 If more than one argument is present, all of the species identified by each argument are combined, and all species containing any other elements are excluded.
 This can be used to retrieve all of the species in the database within a given chemical system.
 A chemical system includes charged species if \code{add.charge} is TRUE (the default) or the user supplies the \dQuote{element} of charge (\samp{Z}).
+If \code{req1} is TRUE, the result corresponds to the intersection of all of the species in the system with those identified by the first argument (i.e. those bearing the first element).
 
-The electron and proton are excluded from the result unless \code{hide.electron} and/or \code{hide.proton} is FALSE.
-Groups used in group-additivity calculations, which have names with square brackets (e.g. [-CH2-]), are also excluded unless \code{hide.groups} is FALSE.
+Groups used in group-additivity calculations, which have names with square brackets (e.g. [-CH2-]), are excluded unless \code{hide.groups} is FALSE.
 
 The return value is a named numeric vector giving the species index (i.e. rownumber(s) of \code{thermo()$obigt}) with names corresponding to the chemical formulas of the species.
-However, if electron is included in the result, the result uses its name (\samp{e-}) instead of its chemical formula (\samp{(Z-1)}).
+However, if the electron is in the result, its name (\samp{e-}) is used instead of its chemical formula (\samp{(Z-1)}).
 If the argument list is empty, then the function returns an empty (length 0) numeric value.
-A special argument value \samp{all} can be used to retrieve all species in the thermodynamic database, including filtering on state and hiding of the proton, electron, and/or groups.
+A special argument value \samp{all} can be used to retrieve all species in the thermodynamic database, including filtering on state and hiding of the groups.
 
 The first time the function is run, it uses \code{\link{i2A}} to build the stoichiometric matrix for the current database.
 Following runs use the previously calculated stoichiometric matrix, unless a change to the database is detected, which triggers a recalculation.
@@ -57,13 +55,18 @@
 retrieve(c("Au", "Cl"))
 # all species that have Au and/or Cl, including charged species, but no other elements
 retrieve("Au", "Cl")
-# include the electron with these species
-retrieve("Au", "Cl", hide.electron = FALSE)
 # all uncharged species that have Au and/or Cl
 retrieve("Au", "Cl", add.charge = FALSE)
 
 # minerals in the system SiO2-MgO-CaO-CO2
 retrieve("Si", "Mg", "Ca", "C", "O", state="cr")
+
+# make an Eh-pH diagram for aqueous S-bearing species
+basis("CHNOSe")
+ispecies <- retrieve("S", "O", "H", req1 = TRUE, state = "aq")
+species(ispecies)
+a <- affinity(pH = c(0, 14), Eh = c(-1, 1))
+diagram(a, fill = "terrain")
 }
 
 \concept{Extended workflow}



More information about the CHNOSZ-commits mailing list