---
title: "iemisc: Comparing Saturated Vapor Pressure Formulas to the Reference"
author: "Irucka Embry, E.I.T. (EcoC²S)"
date: "`r Sys.Date()`"
lang: en-us
urlcolor: blue
output:
  rmarkdown::pdf_document:
    highlight: kate
    toc: true
    latex_engine: xelatex
vignette: >
  %\VignetteIndexEntry{iemisc Comparing Saturated Vapor Pressure Formulas to the Reference}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

\bigskip

# Replicate the R code

Note: If you wish to replicate the R code below, then you will need to copy and paste the following commands in R first (to make sure you have all the packages and their dependencies):  

```{r eval = FALSE, tidy = TRUE}
install.packages(c("install.load", "iemisc", "units"))
# install the packages and their dependencies
```

\bigskip
\bigskip

```{r, warning = FALSE, message = FALSE, tidy = TRUE}
# load the required packages
install.load::load_package("iemisc", "units", "pander")
# load needed packages using the load_package function from the install.load package (it is assumed that you have already installed these packages)

import::from(fpCompare, "%==%")
```

\bigskip
\bigskip

# Fraction (or Mixed number) to a Decimal (Numeric Vector)

```{r, warning = FALSE, message = FALSE, tidy = TRUE}

install.load::load_package("iemisc", "data.table", "units", "pander")


# reference vapor pressures
reference <- sort(c(611.655, 2339.32, 7384.94, 19946.4, 47414.5, 101418))

T <- sort(c(0.01, seq(from = 20, to = 100, by = 20)))

# hydraulics
hydraulics_svp <- hydraulics::svp(T = T, units = "SI")


# iemisc
iemisc_sat_vapor_pressure_huang <- sat_vapor_pressure(T = T, units = "SI", formula = "Huang")

iemisc_sat_vapor_pressure_buck <- sat_vapor_pressure(T = T, units = "SI", formula = "Buck")

iemisc_sat_vapor_pressure_iapws <- sat_vapor_pressure(T = T, units = "SI", formula = "IAPWS")



# aiRthermo

# create a numeric vector with the units of degrees Celsius
T_C <- set_units(T, "degree_C")
T_C

# create a numeric vector to convert from degrees Celsius to Kelvin
T_K <- T_C
T_K

# create a numeric vector with the units of Kelvin
units(T_K) <- make_units(K)

aiRthermo_saturation_pressure_H2O <- aiRthermo::saturation_pressure_H2O(drop_units(T_K))



comparePress <- data.table(Reference_Pressure = reference, Hydraulics_Pressure = hydraulics_svp, Huang_Pressure = iemisc_sat_vapor_pressure_huang, Buck_Pressure = iemisc_sat_vapor_pressure_buck, IAPWS_Pressure = iemisc_sat_vapor_pressure_iapws, aiRthermo_Pressure = aiRthermo_saturation_pressure_H2O)

comparePress[, `:=` (mreHydraulics = mapply(mre, Hydraulics_Pressure, Reference_Pressure) *
100, mreHuang = mapply(mre, Huang_Pressure, Reference_Pressure) * 100,
mreBuck = mapply(mre, Buck_Pressure, Reference_Pressure) * 100, 
mreIAPWS = mapply(mre, IAPWS_Pressure, Reference_Pressure) *
100, mreaiRthermo = mapply(mre, aiRthermo_Pressure, Reference_Pressure) *
100)] # Source 1


# which row(s) has the maximum value
max_row <- pmax(comparePress$mreHydraulics, comparePress$mreHuang, comparePress$mreBuck, comparePress$mreIAPWS, comparePress$mreaiRthermo)

# which row(s) has the minimum value
min_row <- pmin(comparePress$mreHydraulics, comparePress$mreHuang, comparePress$mreBuck, comparePress$mreIAPWS, comparePress$mreaiRthermo)

# which rows are TRUE
max_row2 <- comparePress == max_row


# which rows are TRUE
min_row2 <- comparePress == min_row

comparePress[, max_mre := c(rep("mreaiRthermo", 3), rep( "mreBuck", 3))]

comparePress[, min_mre := c("mreBuck", rep("mreHydraulics / mreHuang", 4), "mreIAPWS")]

setnames(comparePress, c("Reference Pressure (Pa)", "Hydraulics Package Pressure (Pa)", "Huang Pressure (Pa)", "Buck Pressure (Pa)", "IAPWS Pressure (Pa)", "aiRthermo Pressure (Pa)", "MRE % (Hydraulics Package vs. Reference)", "MRE % (Huang vs. Reference)", "MRE % (Buck vs. Reference)", "MRE % (IAPWS vs. Reference)", "MRE % (aiRthermo vs. Reference)", "Maximum MRE % Formula", "Minumum MRE % Formula"))

pander(comparePress)
```

\bigskip
\bigskip

## R Source

r - How do I reset all options() arguments to their default values? - Stack Overflow answered by stevec on Jul 27 2020 and edited by stevec on Feb 27 2022. See https://stackoverflow.com/questions/36848785/how-do-i-reset-all-options-arguments-to-their-default-values  

\bigskip
\bigskip

# EcoC²S Links

EcoC²S Home -- https://www.ecoccs.com/  
About EcoC²S -- https://www.ecoccs.com/about_ecoc2s.html  
Products -- https://www.questionuniverse.com/products.html  
EcoC²S Media -- https://www.ecoccs.com/media.html  
EcoC²S Resources -- https://www.ecoccs.com/resources.html  
R Trainings and Resources provided by EcoC²S (Irucka Embry, E.I.T.) -- https://www.ecoccs.com/rtraining.html  

\bigskip
\bigskip

# Copyright and License

All R code written by Irucka Embry is distributed under the GPL-3 (or later) license, see the [GNU General Public License {GPL} page](https://www.gnu.org/licenses/gpl-3.0.html).  

All written content originally created by Irucka Embry is copyrighted under the Creative Commons Attribution-ShareAlike 4.0 International License. All other written content retains the copyright of the original author(s).  


```{r, echo = FALSE, out.width = '100%'}
linguisticsdown::include_graphics2("https://i.creativecommons.org/l/by-sa/4.0/88x31.png")
```

This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).  
