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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Feb 23 23:58:23 CET 2019


Author: jedick
Date: 2019-02-23 23:58:22 +0100 (Sat, 23 Feb 2019)
New Revision: 408

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/info.R
   pkg/CHNOSZ/R/retrieve.R
   pkg/CHNOSZ/inst/NEWS
   pkg/CHNOSZ/man/retrieve.Rd
Log:
retrieve(): permit specification of chemical systems


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2019-02-23 13:07:44 UTC (rev 407)
+++ pkg/CHNOSZ/DESCRIPTION	2019-02-23 22:58:22 UTC (rev 408)
@@ -1,6 +1,6 @@
 Date: 2019-02-23
 Package: CHNOSZ
-Version: 1.2.0-15
+Version: 1.2.0-16
 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/info.R
===================================================================
--- pkg/CHNOSZ/R/info.R	2019-02-23 13:07:44 UTC (rev 407)
+++ pkg/CHNOSZ/R/info.R	2019-02-23 22:58:22 UTC (rev 408)
@@ -27,7 +27,7 @@
   ## run info.numeric or info.character depending on the input type
   if(is.numeric(species)) {
     out <- lapply(species, info.numeric, check.it)
-    # if we different states the column names could be different
+    # if we have different states the column names could be different
     if(length(unique(unlist(lapply(out, names)))) > ncol(thermo$obigt)) {
       # make them the same as thermo$obigt
       out <- lapply(out, function(row) {
@@ -36,6 +36,8 @@
     }
     # turn the list into a data frame
     out <- do.call(rbind, out)
+    # ensure that the rownames are numeric values (not names possibly inherited from retrieve()) 20190224
+    if(!is.null(attr(species, "names"))) row.names(out) <- species
   } else {
     # state and species should be same length
     if(!is.null(state)) {

Modified: pkg/CHNOSZ/R/retrieve.R
===================================================================
--- pkg/CHNOSZ/R/retrieve.R	2019-02-23 13:07:44 UTC (rev 407)
+++ pkg/CHNOSZ/R/retrieve.R	2019-02-23 22:58:22 UTC (rev 408)
@@ -1,8 +1,10 @@
 # CHNOSZ/retrieve.R
 # retrieve species with given elements
 # 20190214 initial version
+# 20190224 use ... for multiple arguments (define a chemical system)
 
-retrieve <- function(elements) {
+retrieve <- function(..., include.electron = FALSE, include.groups = FALSE, state = NULL) {
+  ## stoichiometric matrix
   # what are the formulas of species in the current database?
   formula <- thermo()$obigt$formula
   # get a previously calculated stoichiometric matrix, if it matches the current database
@@ -20,14 +22,44 @@
     # store the stoichiometric matrix for later calculations
     thermo("stoich" = stoich)
   }
-  not.present <- ! elements %in% colnames(stoich)
-  if(any(not.present)) {
-    if(sum(not.present)==1) stop(elements[not.present], " is not an element that is present in any species")
-    else stop(paste(elements[not.present], collapse=", "), " are not elements that are present in any species")
+
+  ## species identification
+  args <- list(...)
+  ispecies <- numeric()
+  for(elements in args) {
+    not.present <- ! elements %in% colnames(stoich)
+    if(any(not.present)) {
+      if(sum(not.present)==1) stop(elements[not.present], " is not an element that is present in any species")
+      else stop(paste(elements[not.present], collapse=", "), " are not elements that are present in any species")
+    }
+    # 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)
+    ispecies <- c(ispecies, which(has.elements))
+    ispecies <- ispecies[!duplicated(ispecies)]
   }
-  # 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)
-  which(has.elements)
+  # for a chemical system, defined by multiple arguments, the species can not contain any _other_ elements
+  if(length(args) > 1) {
+    syselements <- unlist(args)
+    isyselements <- colnames(thermo()$stoich) %in% syselements
+    notsysstoich <- thermo()$stoich[, !isyselements]
+    iother <- rowSums(notsysstoich[ispecies, ] != 0) > 0
+    ispecies <- ispecies[!iother]
+    # include the species for "Z" (charge)
+    if(!include.electron) {
+      ielectron <- names(ispecies) == "(Z-1)"
+      ispecies <- ispecies[!ielectron]
+    }
+  }
+  # exclude groups and filter states
+  if(!include.groups) {
+    igroup <- grepl("^\\[.*\\]$", thermo()$obigt$name[ispecies])
+    ispecies <- ispecies[!igroup]
+  }
+  if(!is.null(state)) {
+    istate <- thermo()$obigt$state[ispecies] %in% state
+    ispecies <- ispecies[istate]
+  }
+  ispecies
 }
 

Modified: pkg/CHNOSZ/inst/NEWS
===================================================================
--- pkg/CHNOSZ/inst/NEWS	2019-02-23 13:07:44 UTC (rev 407)
+++ pkg/CHNOSZ/inst/NEWS	2019-02-23 22:58:22 UTC (rev 408)
@@ -1,4 +1,4 @@
-CHANGES IN CHNOSZ 1.2.0-15 (2019-02-23)
+CHANGES IN CHNOSZ 1.2.0-16 (2019-02-24)
 ---------------------------------------
 
 CRAN COMPLIANCE
@@ -15,8 +15,9 @@
 - Add thermo() as a convenience function to access or modify the
   package's data, especially various computational options.
 
-- Add retrieve() to retrieve all the species with given elements. Thanks
-  to Evgeniy Bastrakov for the suggestion.
+- Add retrieve() to retrieve all the species having given elements, or
+  all species in a given chemical system. Thanks to Evgeniy Bastrakov
+  for the suggestion.
 
 - Add AkDi() to calculate thermodynamic properties of aqueous
   nonelectrolytes using the Akinfiev-Diamond model. Thanks to Evgeniy

Modified: pkg/CHNOSZ/man/retrieve.Rd
===================================================================
--- pkg/CHNOSZ/man/retrieve.Rd	2019-02-23 13:07:44 UTC (rev 407)
+++ pkg/CHNOSZ/man/retrieve.Rd	2019-02-23 22:58:22 UTC (rev 408)
@@ -7,17 +7,29 @@
 }
 
 \usage{
-  retrieve(elements)
+  retrieve(..., include.electron = FALSE, include.groups = FALSE, state = NULL)
 }
 
 \arguments{
-  \item{elements}{character, one or more chemical elements}
+  \item{...}{list, one or more arguments, each of which is a character vector with the names of one or more chemical elements}
+  \item{include.electron}{logical, include the electron in the result for chemical systems?}
+  \item{include.groups}{logical, include groups in the result?}
+  \item{state}{character, filter the result on these state(s).}
 }
 
 \details{
-This function retrieves the species in the thermodynamic database (see \code{\link{thermo}}) that have all of the elements specified in \code{elements}.
+This function retrieves the species in the thermodynamic database (see \code{\link{thermo}}) that have all of the elements specified in the arguments.
+A single argument is interpreted as a combination of one or more elements that must be present in each species.
 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.
+If the argument list is empty, then the function returns an empty (length 0) numeric value.
 
+If more than one argument is present, all of the species identified by each argument are combined, then any 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.
+When searching for charged species in a chemical system (using the element named \samp{Z}), the electron is excluded unless \code{include.electron} is TRUE (note that the electron has a chemical formula of \samp{(Z-1)}).
+
+Groups used in group-additivity calculations, which have names with square brackets (e.g. [-CH2-]), are excluded unless \code{include.groups} is TRUE.
+Results can be filtered on physical state by setting the \code{state} argument.
+
 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.
 }
@@ -29,9 +41,25 @@
 \examples{
 # species index of Ti-bearing minerals
 retrieve("Ti")
+# thermodynamic data for those minerals
+info(retrieve("Ti"))
 
 # thermodynamic data for Au-Cl complexes
 info(retrieve(c("Au", "Cl")))
+
+# all species that have Au
+retrieve("Au")
+# all species that have both Au and Cl
+retrieve(c("Au", "Cl"))
+# all species that have Au and/or Cl, and no other elements
+retrieve("Au", "Cl")
+# all species that have Au and/or Cl and/or Z, and no other elements
+retrieve("Au", "Cl", "Z")
+# include the electron with these species
+retrieve("Au", "Cl", "Z", include.electron = TRUE)
+
+# minerals in the system SiO2-MgO-CaO-CO2
+retrieve("Si", "Mg", "Ca", "C", "O", state="cr")
 }
 
 \concept{Extended workflow}



More information about the CHNOSZ-commits mailing list