Learn R Programming

tidywater (version 0.7.0)

chemdose_ph_once: Apply `chemdose_ph` function and output a dataframe

Description

This function allows chemdose_ph to be added to a piped data frame. Its output is a data frame with updated ions and pH.

Usage

chemdose_ph_once(
  df,
  input_water = "defined_water",
  hcl = 0,
  h2so4 = 0,
  h3po4 = 0,
  co2 = 0,
  naoh = 0,
  na2co3 = 0,
  nahco3 = 0,
  caoh2 = 0,
  mgoh2 = 0,
  cl2 = 0,
  naocl = 0,
  nh4oh = 0,
  nh42so4 = 0,
  alum = 0,
  ferricchloride = 0,
  ferricsulfate = 0,
  ach = 0,
  caco3 = 0
)

Value

A data frame with updated pH, alkalinity, and ions post-chemical addition.

Arguments

df

a data frame containing a water class column, which has already been computed using define_water_chain. The df may include columns named for the chemical(s) being dosed.

input_water

name of the column of water class data to be used as the input for this function. Default is "defined_water".

hcl

Hydrochloric acid: HCl -> H + Cl

h2so4

Sulfuric acid: H2SO4 -> 2H + SO4

h3po4

Phosphoric acid: H3PO4 -> 3H + PO4

co2

Carbon Dioxide CO2 (gas) + H2O -> H2CO3*

naoh

Caustic: NaOH -> Na + OH

na2co3

Soda ash: Na2CO3 -> 2Na + CO3

nahco3

Sodium bicarbonate: NaHCO3 -> Na + H + CO3

caoh2

Lime: Ca(OH)2 -> Ca + 2OH

mgoh2

Magneisum hydroxide: Mg(OH)2 -> Mg + 2OH

cl2

Chlorine gas: Cl2(g) + H2O -> HOCl + H + Cl

naocl

Sodium hypochlorite: NaOCl -> Na + OCl

nh4oh

Amount of ammonium hydroxide added in mg/L as N: NH4OH -> NH4 + OH

nh42so4

Amount of ammonium sulfate added in mg/L as N: (NH4)2SO4 -> 2NH4 + SO4

alum

Hydrated aluminum sulfate Al2(SO4)3*14H2O + 6HCO3 -> 2Al(OH)3(am) +3SO4 + 14H2O + 6CO2

ferricchloride

Ferric Chloride FeCl3 + 3HCO3 -> Fe(OH)3(am) + 3Cl + 3CO2

ferricsulfate

Amount of ferric sulfate added in mg/L: Fe2(SO4)3*8.8H2O + 6HCO3 -> 2Fe(OH)3(am) + 3SO4 + 8.8H2O + 6CO2

ach

Amount of aluminum chlorohydrate added in mg/L: Al2(OH)5Cl*2H2O + HCO3 -> 2Al(OH)3(am) + Cl + 2H2O + CO2

caco3

Amount of calcium carbonate added (or removed) in mg/L: CaCO3 -> Ca + CO3

Details

The data input comes from a `water` class column, as initialized in define_water or balance_ions.

If the input data frame has a column(s) name matching a valid chemical(s), the function will dose that chemical(s) in addition to the ones specified in the function's arguments. The column names must match the chemical names as displayed in chemdose_ph. To see which chemicals can be passed into the function, see chemdose_ph.

tidywater functions cannot be added after this function because they require a `water` class input.

For large datasets, using `fn_once` or `fn_chain` may take many minutes to run. These types of functions use the furrr package for the option to use parallel processing and speed things up. To initialize parallel processing, use `plan(multisession)` or `plan(multicore)` (depending on your operating system) prior to your piped code with the `fn_once` or `fn_chain` functions. Note, parallel processing is best used when your code block takes more than a minute to run, shorter run times will not benefit from parallel processing.

See Also

chemdose_ph

Examples

Run this code

library(purrr)
library(furrr)
library(tidyr)
library(dplyr)

example_df <- water_df %>%
  define_water_chain() %>%
  balance_ions_chain() %>%
  chemdose_ph_once(input_water = "balanced_water", naoh = 5)

example_df <- water_df %>%
  define_water_chain() %>%
  balance_ions_chain() %>%
  mutate(
    hcl = seq(1, 12, 1),
    naoh = 20
  ) %>%
  chemdose_ph_once(input_water = "balanced_water", mgoh2 = 55, co2 = 4)

# Initialize parallel processing
plan(multisession, workers = 2) # Remove the workers argument to use all available compute
example_df <- water_df %>%
  define_water_chain() %>%
  balance_ions_chain() %>%
  chemdose_ph_once(input_water = "balanced_water", naoh = 5)

# Optional: explicitly close multisession processing
plan(sequential)

Run the code above in your browser using DataLab