Learn R Programming

CHNOSZ (version 1.0.0)

util.units: Functions to Convert Units

Description

These functions to convert values between units and set the user's preferred units.

Usage

P.units(units = NULL)
  T.units(units = NULL)
  E.units(units = NULL)
  convert(value, units, T = get("thermo")$opt$Tr,
    P = get("thermo")$opt$Pr, pH = 7, logaH2O = 0)
  envert(value, units)
  outvert(value, units)

Arguments

units
character, name of units to set or convert to/from
value
numeric, value(s) to be converted
T
numeric, temperature (Kelvin), used in G-logK, pe-Eh and logfO2-E0 conversions
P
numeric, pressure (bar), used in logfO2-E0 conversions
pH
numeric, pH, used in logfO2-E0 conversions
logaH2O
numeric, logarithm of activity of water, used in logfO2-E0 conversions

Value

  • Character return for T.units, P.units and E.units; numeric returns by the other functions.

encoding

UTF-8

Details

The units settings are used by subcrt, affinity, and diagram to accept input in or convert output to the units desired by the user. The settings, which can be queried or changed with T.units, E.units and P.units, refer to the units of temperature (K or C), energy (cal or J), and pressure (bar, MPa). (The first value in each of those pairs refers to the default units).

The actual units conversions are handled by convert, through which values are transformed into destination units (names not case sensitive). The possible conversions and settings for the units argument are shown in the following table. Note that Eh and E0 both stand for the value of Eh (oxidation-reduction potential in volts); they have different names so that one can choose to convert between Eh and either pe or logfO2.

lll{ property units setting of units argument temperature $^{\circ}$C, K C, K pressure bar, MPa bar, MPa energy cal, J cal, J energy cal, cm$^3$ bar calories, cm3bar energy cal, [none] G, logK oxidation potential volt, [none] Eh, pe oxidation potential volt, [none] E0, logfO2 }

envert and outvert are wrappers for convert that handle the conditional conversion of values from or to the current units settings. envert converts the value to the units in the argument, and outvert converts the value from the units in the argument, only if they are different than the current setting; otherwise the value is returned unchanged.

Examples

Run this code
data(thermo)
## examples using convert
# temperature (Kelvin) to degrees C
convert(273.15, "C")
# temperature (degrees C) to Kelvin
convert(100, "K")
# Gibbs energy (cal mol-1) to/from logK
convert(1000, "logK")		
convert(1000, "logK", T=373.15)
convert(1, "G") 
# Eh (volt) to pe 
convert(-1, "pe")		
convert(-1, "pe", T=373.15)
# logfO2 to E0 (volt)
convert(-80, "E0")             
convert(-80, "E0", pH=5)
convert(-80, "E0", pH=5, logaH2O=-5)
# calorie to/from joule
convert(10, "J")		
convert(10, "cal")
# cm3bar to calories		
convert(10, "calories")

## examples showing unit settings
T.units("C")  
T1in <- envert(25, "C")         # no convertion
T1out <- outvert(313.15, "K")   # K to C
T.units("K") 
T2in <- envert(298.15, "C")     # K to C
T2out <- outvert(-233.15, "C")  # C to K
# these are the same temperature (25 deg C)
stopifnot(all.equal(T1in, T2in))
# these are numerically equivalent (40 deg C / 40 K)
stopifnot(all.equal(T1out, T2out))
T.units("C")  # return to default

Run the code above in your browser using DataLab