scriptThe code below can be pasted in an R Notebook file. --- title: "Calculate the EC of a nutrient solution" output: html_notebook: toc: true toc_depth: 2 toc_float: true smooth_scroll: false #number_sections: true --- ```{r Clean memory and plots, echo=FALSE, include=FALSE} # Clean memory and plots rm(list=ls()) #remove all the data from the memory of R while (!is.null(dev.list())) dev.off() # delete all the graphs if(!require(data.table)) {install.packages("data.table"); require(data.table)} if(!require(optimx)) {install.packages("optimx"); require(optimx)} ``` # General information This script is based on techniques used by: H. Kalka, “Aqion: Manual (Electrical Conductivity).” https://www.aqion.de/site/130, accessed on 09 April 2020. Table of diffusion coefficients: http://www.aqion.de/site/194 * Parameters are: + ci=input('Concentration in mmol/L') + zi=input('Charge Number of Ions') + Ai=input('Molar Limiting Conductivity') written as ?0m,i values are obtained from [http://www.aqion.de/site/194]. However, Ai (?0m,i) values can also be calculated if the diffusion coefficient of the ion is known. + E.g. for Si: H_4SiO_4 = Di 1.10e-9 [m2s-1]: Ai=zi^2 Di (F^2 R T) * where: + zi charge number of ion i + T in K absolute temperature + F = 9.6485·104 Coulomb/mol Faraday’s constant + R = 8.31446 J/(K mol) gas constant + F2/(RT) = 3.7554·106 s · S mol^-1 proportionality constant at 25°C + Di in m^2/s diffusion coefficient of ion i + A0m,i in S m^2/mol (= 104 S cm^2/mol) molar limiting conductivity of ion i # Nutrient solution composition: ```{r ,echo=FALSE, include=TRUE} # Create the ci data.table based on the nutrient solution composition # Unit conversion umol to mmol (fixed) un = 1000 # Desired nutrient solution dillution / strength Dl = 1 #Nutrient solution concentration per ion in mmol/L for macro elements (1 - 9) and in umol/L for micro elements #Hoagland and Arnon 1950 nut_sol = data.table( N_NO3 = 14 , N_NH4 = 1 , P_H2PO4 = 1 , K = 6 , Ca = 4 , Mg = 2 , Na = 0 , S_SO4 = 2 , Cl = 0.02, Fe = 40 , Cu = 0.32, Zn = 0.77, Mn = 9.15, B = 46.3, Mo = 0.11 ) # Convert macro elements to mol / L nut_sol.macro <- nut_sol[,1:9]*Dl/1000 # Convert micro elements to umol / L nut_sol.micro <- nut_sol[,10:15]/un/1000 ci <- data.table(nut_sol.macro, nut_sol.micro) #Show the nutrient solution content iin mmol / L is: print(paste("Current evaluated nutrient solution in mmol/L")) print(t(ci*1000)) ``` ```{r ,echo=FALSE, include=FALSE} # Parameters for zi and Ai # Create a data.table with the ion charges zi = data.table( N_NO3 = 1, N_NH4 = 1, P_H2PO4 = 1, K = 1, Ca = 2, Mg = 2, Na = 1, S_SO4 = 2, Cl = 1, Fe = 2, Cu = 2, Zn = 2, Mn = 2, B = 3, Mo = 2 ) # Create a data.table with Molar Limiting Conductivity Ai = data.table( N_NO3 = 71.4, N_NH4 = 74.4, P_H2PO4 = 31.8, K = 73.6, Ca = 119.1, Mg = 105.1, Na = 50.0 , S_SO4 = 160.7, Cl = 76.20, Fe = 108, Cu = 110.1, Zn = 107.4, Mn = 103.4, B = 100, #not correct should be entered Mo = 100 ) ``` ```{r ,echo=FALSE, include=FALSE} # A function to calculate the EC # ec25 calculates electrical conductivity of solutioin based on aqion method. # ci is the molar concentration of ion i (ci) # zi is charge number of ion i (zi) # Ai is molar limiting conductivity EC_25 <- function(ci,zi,Ai){ A = 0.5085 # A is constant for gamma formula T = 25 # T is Temperature of solution cn_ci = length(ci) # Number of Measured Ions I_sub = 0.5*sum(ci*zi^2) I = rep(I_sub, 1, cn_ci) ifelse(I <= 0.36*zi, a <- 0.6/(zi^(0.5)), a <- (I^(0.5))/zi ) g = exp(-(log(10))*A*(zi^2)*(I^(.5))) y = Ai*(g^a)*ci ec=sum(y) EC =round(ec/(1+0.02*(T-25)), 3) return(EC) } # Calculate the final EC paste("The final EC is", EC_25(ci,zi,Ai), "dS/m") ``` # Set a specific EC value in dS/m (for the nutrient ratio's in your nutrient solution) ```{r ,echo=FALSE} EC_set = 0.7 paste("EC_set:",EC_set) ``` # This is how many times you have to dillute the original solution ```{r ,echo=FALSE} #The intial dillution factor is: D = 1 min_val <- data.frame(minV = 0) dil_fac <- function(D, ci, zi, Ai, EC_set, EC_25){ ci_D <- data.table(ci[,1:9]*D,ci[,10:15]) min = abs(EC_set - EC_25(ci_D,zi,Ai)) return(min) } Dil_factor_x <- optimize(dil_fac, c(0,10), ci = ci, EC_set = EC_set ,EC_25 =EC_25, zi = zi, Ai = Ai) print(paste("By multiplying the concentration of each marco element in your start nutrient solution with", Dil_factor_x$minimum, "you will get the requested EC of ", EC_set), quote = FALSE) ```