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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun May 5 11:41:58 CEST 2019


Author: jedick
Date: 2019-05-05 11:41:57 +0200 (Sun, 05 May 2019)
New Revision: 461

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/NAMESPACE
   pkg/CHNOSZ/R/equilibrate.R
   pkg/CHNOSZ/inst/NEWS
   pkg/CHNOSZ/man/equilibrate.Rd
Log:
add new function moles()


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2019-05-05 07:45:34 UTC (rev 460)
+++ pkg/CHNOSZ/DESCRIPTION	2019-05-05 09:41:57 UTC (rev 461)
@@ -1,6 +1,6 @@
 Date: 2019-05-05
 Package: CHNOSZ
-Version: 1.3.2-8
+Version: 1.3.2-9
 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/NAMESPACE
===================================================================
--- pkg/CHNOSZ/NAMESPACE	2019-05-05 07:45:34 UTC (rev 460)
+++ pkg/CHNOSZ/NAMESPACE	2019-05-05 09:41:57 UTC (rev 461)
@@ -58,7 +58,7 @@
 # added 20171121 or later
   "dumpdata", "thermo.axis", "solubility", "NaCl",
 # added 20190213 or later
-  "CHNOSZ", "thermo", "reset", "obigt", "retrieve", "AkDi"
+  "CHNOSZ", "thermo", "reset", "obigt", "retrieve", "AkDi", "moles"
 )
 
 # Load shared objects

Modified: pkg/CHNOSZ/R/equilibrate.R
===================================================================
--- pkg/CHNOSZ/R/equilibrate.R	2019-05-05 07:45:34 UTC (rev 460)
+++ pkg/CHNOSZ/R/equilibrate.R	2019-05-05 09:41:57 UTC (rev 461)
@@ -266,6 +266,39 @@
   return(logact)
 }
 
+# a function to calculate the total moles of the elements in the output from equilibrate 20190505
+moles <- function(eout) {
+  # exponentiate loga.equil to get activities
+  act <- lapply(eout$loga.equil, function(x) 10^x)
+  # initialize list for moles of basis species
+  nbasis <- rep(list(act[[1]] * 0), nrow(eout$basis))
+  # loop over species
+  for(i in 1:nrow(eout$species)) {
+    # loop over basis species
+    for(j in 1:nrow(eout$basis)) {
+      # the coefficient of this basis species in the formation reaction of this species
+      n <- eout$species[i, j]
+      # accumulate the number of moles of basis species
+      nbasis[[j]] <- nbasis[[j]] + act[[i]] * n
+    }
+  }
+  # initialize list for moles of elements (same as number of basis species)
+  nelem <- rep(list(act[[1]] * 0), nrow(eout$basis))
+  # loop over basis species
+  for(i in 1:nrow(eout$basis)) {
+    # loop over elements
+    for(j in 1:nrow(eout$basis)) {
+      # the coefficient of this element in the formula of this basis species
+      n <- eout$basis[i, j]
+      # accumulate the number of moles of elements
+      nelem[[j]] <- nelem[[j]] + nbasis[[i]] * n
+    }
+  }
+  # add element names
+  names(nelem) <- colnames(eout$basis)[1:nrow(eout$basis)]
+  nelem
+}
+
 ### unexported functions ###
 
 # return a list containing the balancing coefficients (n.balance) and a textual description (balance)

Modified: pkg/CHNOSZ/inst/NEWS
===================================================================
--- pkg/CHNOSZ/inst/NEWS	2019-05-05 07:45:34 UTC (rev 460)
+++ pkg/CHNOSZ/inst/NEWS	2019-05-05 09:41:57 UTC (rev 461)
@@ -1,16 +1,15 @@
-CHANGES IN CHNOSZ 1.3.2-8 (2019-05-05)
+CHANGES IN CHNOSZ 1.3.2-9 (2019-05-05)
 --------------------------------------
 
 BUG FIXES
 
-- In mosaic(), affinities of formation of species should not only be
-  weighted by the mole fraction of the changing basis species, but they
-  should be calculated using activities of basis species that are
-  adjusted (i.e. lowered) according to their mole fractions. Previously,
-  constant activities were used for each changing basis species,
-  leading to an artificial inflation of affinities of species,
-  especially near the transitions where the changing basis species are
-  nearly equal in abundance.
+- In mosaic(), affinities of formation of species were not calculated
+  using activities of basis species that are adjusted (i.e. lowered)
+  according to their mole fractions. Previously, constant activities
+  were used for each changing basis species, leading to an artificial
+  inflation of the affinities of species, especially near the
+  transitions where the changing basis species are nearly equal in
+  abundance.
 
 - The 'mixing' argument of mosaic() has been removed; it is no longer
   needed with the improved handling of activities of basis species.
@@ -17,31 +16,37 @@
 
 OTHER CHANGES
 
-- Tests have been added to test-mosaic.R to check that activitives
-  produced by mosaic() - equilibrate() and mosaic() - solubility()
-  really are equilibrium activities, i.e. that the affinities of
-  reactions between species are zero everywhere, in particular near the
-  transitions of basis species.
+- Add tests to test-mosaic.R to check that activitives produced by
+  mosaic() - equilibrate() and mosaic() - solubility() really are
+  equilibrium activities, i.e. that the affinities of reactions between
+  species are zero everywhere, in particular near the transitions of
+  basis species.
 
-- In mosaic(), equilibrate groups of changing basis species using total
-  activity taken from the corresponding basis species in the incoming
-  basis() definition. This is not directly related to the bug above, but
-  provides a means to constrain the amount of an element in the basis
-  species.
+- In mosaic(), groups of changing basis species are equilibrated using
+  a total activity given by the activity of the starting basis species
+  in the incoming basis() definition. This is not directly related to
+  the bug above, but instead provides a means to constrain the amount of
+  an element in the basis species.
 
 - In mosaic(), include the result of equilibrate() for each group of
   basis species in the output ('E.bases').
 
+NEW FEATURES
+
 - equilibrate(): make it possible to process the output of mosaic().
-  This combines the equilibrium activities of the formed species with
-  the changing basis species to make an object that can be plotted with
-  diagram(). This can be useful for conserving two elements in a system
-  (one in the basis species, the other in the formed species). Thanks
-  to Kirt Robinson for the feature request and test system.
+  This feature combines the equilibrium activities of the formed species
+  with those of the changing basis species to make an object that can be
+  plotted with diagram(). This can be useful for conserving two elements
+  in a system: one in the basis species, the other in the formed
+  species. Thanks to Kirt Robinson for the feature request and test
+  system.
 
-- demo/aluminum.R: add calculations using Si(OH)4 from Akinfiev-Diamond
-  model (SiO2 in these reactions is replaced by Si(OH)4 - 2 H2O).
+- Add function moles(), to calculate the total number of moles of
+  elements in the output from equilibrate().
 
+- demo/aluminum.R: add calculations using Si(OH)4 from the Akinfiev-
+  Diamond model (SiO2 in these reactions is replaced by Si(OH)4 - 2 H2O).
+
 CHANGES IN CHNOSZ 1.3.2 (2019-04-20)
 ------------------------------------
 

Modified: pkg/CHNOSZ/man/equilibrate.Rd
===================================================================
--- pkg/CHNOSZ/man/equilibrate.Rd	2019-05-05 07:45:34 UTC (rev 460)
+++ pkg/CHNOSZ/man/equilibrate.Rd	2019-05-05 09:41:57 UTC (rev 461)
@@ -3,6 +3,7 @@
 \alias{equilibrate}
 \alias{equil.boltzmann}
 \alias{equil.reaction}
+\alias{moles}
 \title{Equilibrium Chemical Activities of Species}
 \description{
 Calculate equilibrium chemical activities of species from the affinities of formation of the species at unit activity. 
@@ -14,10 +15,11 @@
     method=c("boltzmann", "reaction"), tol=.Machine$double.eps^0.25)
   equil.boltzmann(Astar, n.balance, loga.balance)
   equil.reaction(Astar, n.balance, loga.balance, tol=.Machine$double.eps^0.25)
+  moles(eout)
 }
 
 \arguments{
-  \item{aout}{list, output from \code{\link{affinity}}}
+  \item{aout}{list, output from \code{\link{affinity}}} or \code{\link{mosaic}}
   \item{balance}{character or numeric, how to balance the transformations}
   \item{ispecies}{numeric, which species to include}
   \item{normalize}{logical, normalize the molar formulas of species by the balancing coefficients?}
@@ -27,6 +29,7 @@
   \item{loga.balance}{numeric (single value or vector), logarithm of total activity of balanced quantity}
   \item{method}{character, equilibration method to use}
   \item{tol}{numeric, convergence tolerance for \code{\link{uniroot}}}
+  \item{eout}{list, output from \code{\link{equilibrate}}}
 }
 
 \details{
@@ -66,6 +69,8 @@
 Using \code{equil.reaction} may be needed for systems with huge (negative or positive) affinities, where \code{equil.boltzmann} produces a NaN result.
 
 \code{ispecies} can be supplied to identify a subset of the species to include in the calculation.
+
+\code{moles} simply calculates the total number of moles of elements corresponding to the activities of formed species in the output from \code{equilibrate}.
 }
 
 \section{Algorithms}{



More information about the CHNOSZ-commits mailing list