From noreply at r-forge.r-project.org Sun Jan 28 08:16:41 2018 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sun, 28 Jan 2018 08:16:41 +0100 (CET) Subject: [CHNOSZ-commits] r302 - in pkg/CHNOSZ: . R inst tests/testthat Message-ID: <20180128071641.9F38018834C@r-forge.r-project.org> Author: jedick Date: 2018-01-28 08:16:39 +0100 (Sun, 28 Jan 2018) New Revision: 302 Modified: pkg/CHNOSZ/DESCRIPTION pkg/CHNOSZ/R/equilibrate.R pkg/CHNOSZ/inst/NEWS pkg/CHNOSZ/tests/testthat/test-equilibrate.R Log: equilibrate(): accept 'normalize' argument with length > 1 Modified: pkg/CHNOSZ/DESCRIPTION =================================================================== --- pkg/CHNOSZ/DESCRIPTION 2017-12-22 01:54:12 UTC (rev 301) +++ pkg/CHNOSZ/DESCRIPTION 2018-01-28 07:16:39 UTC (rev 302) @@ -1,6 +1,6 @@ -Date: 2017-12-21 +Date: 2018-01-28 Package: CHNOSZ -Version: 1.1.3-9 +Version: 1.1.3-10 Title: Thermodynamic Calculations for Geobiochemistry Authors at R: c( person("Jeffrey", "Dick", , "j3ffdick at gmail.com", role = c("aut", "cre"), Modified: pkg/CHNOSZ/R/equilibrate.R =================================================================== --- pkg/CHNOSZ/R/equilibrate.R 2017-12-22 01:54:12 UTC (rev 301) +++ pkg/CHNOSZ/R/equilibrate.R 2018-01-28 07:16:39 UTC (rev 302) @@ -54,12 +54,14 @@ ## normalize -- normalize the molar formula by the balance coefficients m.balance <- n.balance isprotein <- grepl("_", as.character(aout$species$name)) - if(normalize | as.residue) { + if(any(normalize) | as.residue) { if(any(n.balance < 0)) stop("one or more negative balancing coefficients prohibit using normalized molar formulas") - n.balance <- rep(1, nspecies) + n.balance[normalize|as.residue] <- 1 if(as.residue) message(paste("equilibrate: using 'as.residue' for molar formulas")) else message(paste("equilibrate: using 'normalize' for molar formulas")) - } else m.balance <- rep(1, nspecies) + # set the formula divisor (m.balance) to 1 for species whose formulas are *not* normalized + m.balance[!(normalize|as.residue)] <- 1 + } else m.balance[] <- 1 ## Astar: the affinities/2.303RT of formation reactions with ## formed species in their standard-state activities Astar <- lapply(1:nspecies, function(i) { @@ -76,7 +78,7 @@ if(method[1]=="boltzmann") loga.equil <- equil.boltzmann(Astar, n.balance, loga.balance) else if(method[1]=="reaction") loga.equil <- equil.reaction(Astar, n.balance, loga.balance, tol) ## if we normalized the formulas, get back to activities to species - if(normalize & !as.residue) { + if(any(normalize) & !as.residue) { loga.equil <- lapply(1:nspecies, function(i) { loga.equil[[i]] - log10(m.balance[i]) }) Modified: pkg/CHNOSZ/inst/NEWS =================================================================== --- pkg/CHNOSZ/inst/NEWS 2017-12-22 01:54:12 UTC (rev 301) +++ pkg/CHNOSZ/inst/NEWS 2018-01-28 07:16:39 UTC (rev 302) @@ -1,5 +1,5 @@ -CHANGES IN CHNOSZ 1.1.3-8 (2017-12-21) --------------------------------------- +CHANGES IN CHNOSZ 1.1.3-10 (2018-01-28) +--------------------------------------- - Lines in 1-D diagram()s can optionally be drawn as splines using the method for splinefun() given in the 'spline.method' argument (the @@ -18,8 +18,13 @@ - Fix a bug where subcrt()$reaction$coeffs was incorrect for reactions involving minerals with phase transitions. -- Fix bug in nonideal() where 'Zn' in formula was identified as charge. +- Fix bug in nonideal() where "Zn" in formula was identified as charge. +- TODO: fix overly long message for info("SiO2"). + +- In equilibrate(), accept a length > 1 'normalize' argument in order + normalize the chemical formulas of only the selected species. + CHANGES IN CHNOSZ 1.1.3 (2017-11-13) ------------------------------------ Modified: pkg/CHNOSZ/tests/testthat/test-equilibrate.R =================================================================== --- pkg/CHNOSZ/tests/testthat/test-equilibrate.R 2017-12-22 01:54:12 UTC (rev 301) +++ pkg/CHNOSZ/tests/testthat/test-equilibrate.R 2018-01-28 07:16:39 UTC (rev 302) @@ -155,6 +155,40 @@ expect_equal(list2array(eO2$loga.equil)[21, ], e60.8) }) +test_that("normalizing formulas of only selected species works as expected", { + iC6 <- info("n-hexane", "liq") + iC12 <- info("n-dodecane", "liq") + `n-alkane` <- iC6:iC12 + i2C6 <- info("2-methylpentane", "liq") + i2C9 <- info("2-methyloctane", "liq") + `2-isoalkane` <- i2C6:i2C9 + basis("CHNOS") + basis("O2", -49.5) + species(`n-alkane`) + species(`2-isoalkane`) + # approximate conditions of Computer Experiment 27 (Helgeson et al., 2009, GCA) + a <- affinity(T=150, P=830, exceed.Ttr=TRUE) + # using full chemical formulas + efull <- equilibrate(a) + dloga_isoalkane_full <- diff(unlist(efull$loga.equil[c(8, 11)])) + # normalize all the formulas + enorm <- equilibrate(a, normalize=TRUE) + dloga_nalkane_norm <- diff(unlist(enorm$loga.equil[c(1, 7)])) + dloga_isoalkane_norm <- diff(unlist(enorm$loga.equil[c(8, 11)])) + # normalize only the n-alkane formulas + isalk <- grepl("n-", species()$name) + emix <- equilibrate(a, normalize=isalk) + # the activity ratios for the normalized formulas should be the same in both calculations + dloga_nalkane_mix <- diff(unlist(emix$loga.equil[c(1, 7)])) + expect_equal(dloga_nalkane_mix, dloga_nalkane_norm) + # the actvitity ratios for the not-normalized formulas should be similar in both calculations + # (not identical becuase they are affected by total activity, unlike normalized formulas) + dloga_isoalkane_mix <- diff(unlist(emix$loga.equil[c(8, 11)])) + expect_maxdiff(dloga_isoalkane_mix, dloga_isoalkane_full, 0.07) + # however, the difference between normalized and not-normalized formulas is much greater + expect_true(maxdiff(dloga_isoalkane_mix, dloga_isoalkane_norm) > maxdiff(dloga_isoalkane_mix, dloga_isoalkane_full)) +}) + # references # Seewald, J. S. (2001) From noreply at r-forge.r-project.org Sun Jan 28 09:23:31 2018 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sun, 28 Jan 2018 09:23:31 +0100 (CET) Subject: [CHNOSZ-commits] r303 - in pkg/CHNOSZ: . R inst man Message-ID: <20180128082331.35F361883A0@r-forge.r-project.org> Author: jedick Date: 2018-01-28 09:22:34 +0100 (Sun, 28 Jan 2018) New Revision: 303 Modified: pkg/CHNOSZ/DESCRIPTION pkg/CHNOSZ/NAMESPACE pkg/CHNOSZ/R/util.plot.R pkg/CHNOSZ/inst/NEWS pkg/CHNOSZ/man/util.plot.Rd Log: export thermo.axis() Modified: pkg/CHNOSZ/DESCRIPTION =================================================================== --- pkg/CHNOSZ/DESCRIPTION 2018-01-28 07:16:39 UTC (rev 302) +++ pkg/CHNOSZ/DESCRIPTION 2018-01-28 08:22:34 UTC (rev 303) @@ -1,6 +1,6 @@ Date: 2018-01-28 Package: CHNOSZ -Version: 1.1.3-10 +Version: 1.1.3-11 Title: Thermodynamic Calculations for Geobiochemistry Authors at R: c( person("Jeffrey", "Dick", , "j3ffdick at gmail.com", role = c("aut", "cre"), Modified: pkg/CHNOSZ/NAMESPACE =================================================================== --- pkg/CHNOSZ/NAMESPACE 2018-01-28 07:16:39 UTC (rev 302) +++ pkg/CHNOSZ/NAMESPACE 2018-01-28 08:22:34 UTC (rev 303) @@ -60,7 +60,7 @@ "calculateEpsilon", "calculateQ", "water.DEW", "berman", "maxdiff", "expect_maxdiff", "Bdot", # added 20171121 or later - "dumpdata" + "dumpdata", "thermo.axis" ) # Load shared objects Modified: pkg/CHNOSZ/R/util.plot.R =================================================================== --- pkg/CHNOSZ/R/util.plot.R 2018-01-28 07:16:39 UTC (rev 302) +++ pkg/CHNOSZ/R/util.plot.R 2018-01-28 08:22:34 UTC (rev 303) @@ -187,35 +187,16 @@ rev(dcol)[z] } -### unexported functions ### - # Function to add axes and axis labels to plots, # with some default style settings (rotation of numeric labels) -# and conversions between oxidation-reduction scales (called by thermo.plot.new ()). # With the default arguments (no labels specified), it plots only the axis lines and tick marks # (used by diagram() for overplotting the axis on diagrams filled with colors). -thermo.axis <- function(lab=NULL,side=1:4,line=1.5,cex=par('cex'),lwd=par('lwd'),T=NULL,col=par('col')) { - # if T isn't NULL, looks like we want make a second - # oxidation scale corresponding to one already plotted. - # e.g., Eh-pe, Eh-logfO2, or logfO2-Eh - if(!is.null(T)) { - usr <- par('usr') - if(side %in% c(1,3)) lim <- usr[1:2] else lim <- usr[3:4] - if(length(grep('pe',lab)) > 0) { - lim <- convert(lim,'pe',T=T) - } else if(length(grep('O2',lab)) > 0) { - lim <- convert(lim,'logfO2',T=T) - } else if(length(grep('Eh',lab)) > 0) { - lim <- convert(lim,'E0',T=T) - } - if(side %in% c(1,3)) usr[1:2] <- lim else usr[3:4] <- lim - opar <- par(usr=usr) - } +thermo.axis <- function(lab=NULL,side=1:4,line=1.5,cex=par('cex'),lwd=par('lwd'),col=par('col')) { if(!is.null(lwd)) { ## plot major tick marks and numeric labels for(thisside in side) { do.label <- TRUE - if(missing(side) | (missing(cex) & thisside %in% c(3,4) & is.null(T))) do.label <- FALSE + if(missing(side) | (missing(cex) & thisside %in% c(3,4))) do.label <- FALSE at <- axis(thisside,labels=do.label,tick=TRUE,lwd=lwd,col=col,col.axis=col) ## plot minor tick marks # the distance between major tick marks @@ -263,6 +244,4 @@ if(thisside %in% c(2,4)) las <- 0 else las <- 1 if(!is.null(lab)) mtext(lab,side=thisside,line=line,cex=cex,las=las) } - # reset limits if we were plotting a second axis - if(!is.null(T)) par(opar) } Modified: pkg/CHNOSZ/inst/NEWS =================================================================== --- pkg/CHNOSZ/inst/NEWS 2018-01-28 07:16:39 UTC (rev 302) +++ pkg/CHNOSZ/inst/NEWS 2018-01-28 08:22:34 UTC (rev 303) @@ -1,4 +1,4 @@ -CHANGES IN CHNOSZ 1.1.3-10 (2018-01-28) +CHANGES IN CHNOSZ 1.1.3-11 (2018-01-28) --------------------------------------- - Lines in 1-D diagram()s can optionally be drawn as splines using the @@ -25,6 +25,9 @@ - In equilibrate(), accept a length > 1 'normalize' argument in order normalize the chemical formulas of only the selected species. +- Export thermo.axis(), as it is useful for adding major&minor tick + marks after (above) other plot elements such as legends. + CHANGES IN CHNOSZ 1.1.3 (2017-11-13) ------------------------------------ Modified: pkg/CHNOSZ/man/util.plot.Rd =================================================================== --- pkg/CHNOSZ/man/util.plot.Rd 2018-01-28 07:16:39 UTC (rev 302) +++ pkg/CHNOSZ/man/util.plot.Rd 2018-01-28 08:22:34 UTC (rev 303) @@ -2,6 +2,7 @@ \name{util.plot} \alias{util.plot} \alias{thermo.plot.new} +\alias{thermo.axis} \alias{label.plot} \alias{usrfig} \alias{label.figure} @@ -20,6 +21,8 @@ mgp = c(1.7, 0.3, 0), cex.axis = par("cex"), col = par("col"), yline = NULL, axs = "i", do.box = TRUE, las = 1, xline = NULL, ...) + thermo.axis(lab = NULL, side = 1:4, line = 1.5, cex = par("cex"), + lwd = par("lwd"), col = par("col")) label.plot(x, xfrac = 0.05, yfrac = 0.95, paren = FALSE, italic = FALSE, ...) usrfig() @@ -49,6 +52,8 @@ \item{las}{numeric, style for axis labels} \item{xline}{numeric, margin line on which to plot \eqn{x}{x}-axis name} \item{...}{further arguments passed to \code{par} or \code{mtext}} + \item{lab}{character, axis label} + \item{line}{numeric, margin line on which to place axis label or plot title} \item{x}{character, label to place on plot} \item{xfrac}{numeric, fractional location on \eqn{x}{x}-axis for placement of label} \item{yfrac}{numeric, fractional location on \eqn{y}{y}-axis for placement of label} @@ -59,14 +64,16 @@ \item{lty}{numeric, line type} \item{plot.it}{logical, plot the lines?} \item{main}{character, text for plot title} - \item{line}{numeric, margin line to place title} \item{spacing}{numeric, spacing between multiple lines} \item{z}{numeric, set of values} } \details{ - \code{thermo.plot.new} sets parameters for a new plot, creates a new plot using \code{\link{plot.new}}, and adds axes and major and minor tick marks to the plot. Plot parameters (see \code{\link{par}}) including \code{cex}, \code{mar}, \code{lwd}, \code{mgp} and \code{axs} can be given, as well as a numeric vector in \code{side} identifying which sides of the plot receive tick marks. \code{yline}, if present, denotes the margin line (default \code{\link{par}('mgp')[1]}) where the y-axis name is plotted. +\code{thermo.plot.new} sets parameters for a new plot, creates a new plot using \code{\link{plot.new}}, and adds the axes tick marks to the plot. +Plot parameters (see \code{\link{par}}) including \code{cex}, \code{mar}, \code{lwd}, \code{mgp} and \code{axs} can be given, as well as a numeric vector in \code{side} identifying which sides of the plot receive tick marks. +\code{yline}, if present, denotes the margin line (default \code{\link{par}('mgp')[1]}) where the y-axis name is plotted. +\code{thermo.axis} is the function that actually adds the axes, including inward-pointing major and minor tick marks (often used for thermodynamic property diagrams). \code{water.lines} plots lines representing the oxidation and reduction stability limits of water on Eh/pe/\logfO2/\logfH2 vs pH/\T/\P diagrams. The x- and y-variables and their ranges are taken from \code{eout}. @@ -93,4 +100,8 @@ } +\seealso{ +\code{\link{diagram}} uses \code{thermo.plot.new} to set up a new plot, unless the argument \code{tplot} is set to FALSE in \code{diagram}. +} + \concept{Utility functions}