## Basic example

This is a basic example that showcases the potential use of rCTOOL. This is illustrated with samples from the Askov long-term trials in Denmark.

```{r, message = FALSE, warning = FALSE}
library(rCTOOL)
library(tidyverse)
library(ggplot2)
library(dplyr)
library(lubridate)

# load data ----
data('basic_example')
data('scenario_temperature')

scenario_temperature <- set_monthly_temperature_data_historical_amplitude(
  file = scenario_temperature
)
```
The `basic_example` dataset contains annual carbon inputs and monthly allocation settings, while `scenario_temperature` contains the monthly temperature input used in the simulation.
```{r }
head(basic_example, 2)
```
```{r }
head(scenario_temperature, 2)
```
The simulation requires time, carbon input, management, soil and temperature configuration.
```{r }
# define timeperiod 
period  <-  define_timeperiod(yr_start = 1951, yr_end = 2019)

# get annual Carbon inputs 
cin  <-  define_Cinputs(management_filepath = basic_example)

# get management 
management <-  management_config(management_filepath = basic_example, f_man_humification = 0.192)

# get soil configuration 
soil = soil_config(Csoil_init = 105,
                   f_hum_top =  0.533,
                   f_rom_top =  0.405,
                   f_hum_sub =  0.387,
                   f_rom_sub =  0.610,
                   Cproptop = 0.55,
                   clay_top = 0.11,
                   clay_sub = 0.20,
                   f_co2 = 0.628,
                   f_romi = 0.012,
                   k_fom  = 0.12,
                   k_hum = 0.0028,
                   k_rom = 3.85e-5,
                   ftr = 0.0025,
                   temp_th_diff = 0.35e-6)
```
Before the simulation starts, the initial soil carbon stock must be distributed among the FOM, HUM and ROM pools in topsoil and subsoil. This initialization depends on the soil C:N ratio and on the pool distribution parameters defined in `soil_config`.
```{r }
# initialize soil pools
soil_pools  <-  initialize_soil_pools(
  cn = 12,
  soil_config = soil)

```

The monthly simulation can now be run using the previously defined time, input, management, soil and temperature configurations. The optional `verbose` argument can be used for additional diagnostic output.
```{r }
# run rCTOOL
output  <-  run_ctool(time_config = period,
                   cin_config = cin,
                   m_config = management,
                   t_config = scenario_temperature,
                   s_config = soil,
                   soil_pools = soil_pools,
                   verbose = FALSE)
```

We can now visualize the simulated topsoil organic carbon stock over time.

```{r basic-example-plot, echo=FALSE, message=FALSE, warning=FALSE, fig.width=12, fig.height=7}
plot_df <- output |>
  dplyr::mutate(
    time = lubridate::make_date(year = yrs, month = mon)
  )

ggplot(plot_df, aes(x = time, y = C_topsoil)) +
  geom_line(
    colour = "black",
    linewidth = 0.35
  ) +
  geom_smooth(
    method = "loess",
    se = FALSE,
    span = 0.20,
    colour = "#2C7FB8",
    linewidth = 1.3
  ) +
  labs(
    x = "Time (years)",
    y = expression("Topsoil organic carbon (Mg C " * ha^{-1} * ")")
  ) +
  scale_x_date(
    date_breaks = "20 years",
    date_labels = "%Y",
    expand = expansion(mult = c(0.01, 0.01))
  ) +
  scale_y_continuous(
    expand = expansion(mult = c(0.02, 0.02))
  ) +
  theme_classic(base_size = 14) +
  theme(
    axis.title = element_text(face = "bold", size = 14),
    axis.text = element_text(colour = "black", size = 12),
    axis.line = element_line(colour = "black", linewidth = 0.4),
    axis.ticks = element_line(colour = "black", linewidth = 0.4)
  )
```

