

#Check saturation indices of oxidized Mg-bearing phases 
```{r}

library ("readxl")
experimentaldata <- read_excel("data_phreeqc.xlsx",sheet= "Experimental_data")

model_Mg <- lm(Mg ~ poly(Day,5, raw= TRUE), data =experimentaldata)
model_Si <- lm(Si ~ poly(Day,5, raw= TRUE), data =experimentaldata)
model_pH <- lm(pH ~ poly(Day,5, raw= TRUE), data =experimentaldata)
model_Al <- lm(Al ~ poly(Day,5, raw= TRUE), data =experimentaldata)


new.x <- seq(0, 113, 1.0)
data2 <- data.frame(x=new.x)
colnames (data2) <- "Day"

data2$Mg <- predict(model_Mg, newdata=data2)
data2$Al <- predict(model_Al, newdata=data2)
data2$Si <- predict(model_Si, newdata=data2)
data2$pH <- predict(model_pH, newdata=data2)


Mgvector <- data2$Mg
Alvector <- data2$Al
Sivector <- data2$Si
pHvector <- data2$pH


#Add temperature data

temperature_data <- read_excel("data_phreeqc.xlsx",sheet= "Temperature")
#add polynoom to fit values at every time 
modelT <- lm(Temperature~poly(Day,5, raw= TRUE), data =temperature_data)
data2$Temperature <- predict(modelT, newdata=data2) 
temperaturevector <- data2$Temperature

#run the model for each day in a for loop, store output dataframe plots in a list
#add the lists then together in one  dataframe and make the ggplot

library(phreeqc)
phrLoadDatabaseString(phreeqc.dat)
phases <-c("PHASES",
           "Brucite","Mg(OH)2 + 2H+ = Mg+2 + 2H2O","log_k   16.792","delta_h -25.84  kcal",  "Sepiolite(c)",
        "Mg2Si3O7.5OH:3H2O + 0.5H2O + 4H+ = 2Mg+2 + 3H4SiO4",
        "log_k   15.913",
        "delta_h -27.268 kcal",
        "Talc",
 "Mg3Si4O10(OH)2 + 4H2O + 6H+ = 3Mg+2 + 4H4SiO4",
  "   log_k   23.055",
   "   delta_h -35.005 kcal",
 "Chrysotile",
        "Mg3Si2O5(OH)4 + 6H+ = 3Mg+2 + 2H4SiO4 + H2O",
        "log_k   32.188",
        "delta_h -52.485 kcal", 
 "Chlorite",
 "Mg5Al2Si3O10(OH)8 + 16H+ = 5Mg+2 + 2Al+3 + 3H4SiO4 + 6H2O",
 "log_k	68.38",
	"delta_h -151.494 kcal")

empty_list <- list()

for (i in (1: length(data2$Day))) {
temperature = temperaturevector[i]
Mg = Mgvector[i]
Al = Alvector[i]
Si = Sivector[i]
pH = pHvector[i]

 str_init1 <-  c(paste ("SOLUTION",i),
                           "-units mg/L", "density 1",
                           paste("temperature", temperature) ,
                           paste("pH", pH) , paste("Mg", Mg),   
                           paste("Si", Si), paste ("Al",Al),
                           paste ("S(6) 1 charge"))
 

  output <-  c("SELECTED_OUTPUT",
                     "-reset false","-solution ",
                     "-saturation_indices  Brucite Sepiolite(c) Talc Chrysotile Chlorite")
               
               
 phrRunString(c(phases, str_init1, output))
 phrSOC_EW_out_coupled <- phrGetSelectedOutput()$n1
phrSOC_EW_out_coupled$ pH <- pH 
phrSOC_EW_out_coupled$ Mg <- Mg 
phrSOC_EW_out_coupled$ Si <- Si 
phrSOC_EW_out_coupled$ Day <- i 



 empty_list[[i]] <- phrSOC_EW_out_coupled

}

library ("dplyr")
df <- bind_rows(empty_list)
```

```{r}
#visualization
library("rcartocolor")
spectral <- carto_pal (n=5, name = "Geyser")

library (ggplot2)
SI_figure <- ggplot(df, aes(x = Day)) + 
  geom_line(aes(y = si_Brucite, col = "a"), size =2) + 
  geom_line(aes(y = si_Chrysotile, col = "b"), size =2) +
  geom_line(aes(y = si_Sepiolite.c. , col = "c"), size =2) +
  geom_line(aes(y = si_Talc , col = "d"), size =2) +
    geom_line(aes(y = si_Chlorite , col = "e"), size =2) +
   theme(axis.title.y = element_text(vjust= 3),
        axis.title.x = element_text(vjust= -0.5),
        axis.title = element_text(face = "bold"),
        axis.text.x=element_text(size=25),
        axis.text.y=element_text(size=25),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black")) +
  scale_colour_manual("", 
                      breaks = c("a", "b", "c","d","e"),
                      values = spectral, labels = c("Brucite","Chrysotile","Sepiolite","Talc","Chlorite")) +
 xlab ("Time (days post amendment)") + 
 ylab ("Saturation index (SI)")+
  geom_hline(yintercept=0, linetype="dashed", color = "red")

ggsave("SI_phyllosilicates.jpeg", plot = SI_figure,  dpi = 600)

```
