;;; afforestation.dai -- Sample setup using afforestation.

(input file "dk-soil.dai")
(input file "log-std.dai")

(defvegetation Oak afforestation
  "A growing oak forest."
  ;; Date forest was planted.
  (planting_time 1970 5 1)
  ;; Forest height as a function of years since planting.
  (canopy_height (0 [y] 0 [m]) (1 [y] 1 [m]) (2 [y] 1.5 [m]) (5 [y] 2 [m])
                 (10 [y] 3 [m]) (20 [y] 4 [m]) (30 [y] 5 [m]))
  ;; Forest height as a function of years since planting.
  (root_depth (0 [y] 0.3 [m]) (1 [y] 1.0 [m]) (2[y]  1.5 [m]))
  ;; LAI shape during a year, 1 representing the relative minimum LAI
  ;; and 5 the maximum LAI, to be scaled for yearly growth according
  ;; to LAI_min and LAI_max.
  (LAI_shape (1 [d] 1 []) (135 [d] 1 []) (160 [d] 5.0 []) 
             (305 [d] 5.0 []) (320 [d] 1 []) (365 [d] 1 [])) 
  ;; Minimum and maximum LAI as a function of year after planting.
  (LAI_min (0 [y] 0.1) (1 [y] 0.2) (5 [y] 0.3))
  (LAI_max (0 [y] 0.5) (1 [y] 1) (5 [y] 3) (10 [y] 4) (20 [y] 5))

  ;; The nitrogen demand of the forest has two components, a short
  ;; time dynamic which depends on LAI, and a long time accumulation
  ;; of nitrogen.
  (N_nonleaves (0 [y] 0 [kg N/ha]) (1 [y] 10 [kg N/ha]) 
               (10 [y] 100 [kg N/ha]) (30 [y] 200 [kg N/ha]))
  ;; Multiply with LAI to get short time dynamic component.
  (N_per_LAI 10 [kg N/ha])
  
  ;; Litterfall during a year, 0 representing no litterfall, and 5
  ;; representing the highest litterfall.
  (litterfall_shape (300 [d] 0 []) (305 [d] 5 []) (320 [d] 0 [])) 
  ;; Total litterfall as a function of years after planting.
  (litterfall_total (0 [y] 0.5 [Mg DM/ha]) (1 [y] 1 [Mg DM/ha])
                    (5 [y] 3 [Mg DM/ha]) (10 [y] 4 [Mg DM/ha])
                    (20 [y] 5 [Mg DM/ha]))
  ;; Content of litterfall.
  (litterfall_C_per_DM 0.42 [])
  (litterfall_C_per_N 50 [])

  ;; Rhizodeposition during a year, 0 representing no rhizodeposition, and 5
  ;; representing the highest rhizodeposition.
  (rhizodeposition_shape (300 [d] 0) (305 [d] 5) (320 [d] 0)) 
  ;; Total rhizodeposition as a function of years after planting.
  (rhizodeposition_total ( 0 [y] 0.5 [Mg DM/ha]) ( 1 [y] 1 [Mg DM/ha])
                         ( 5 [y] 3   [Mg DM/ha]) (10 [y] 4 [Mg DM/ha])
                         (20 [y] 5   [Mg DM/ha]))
  ;; Content of rhizodeposition.
  (rhizodeposition_C_per_DM 0.42 [])
  (rhizodeposition_C_per_N 50 [])

  ;; Constant parameters inherited from the 'permanent' model.
  (Canopy (IntcpCap 0.3 [mm])
          (rs_min 70.0)
          (EpFac 1.136 [])))

(defcolumn Oak Askov
  (Vegetation Oak)
  ;; We log the top 30 cm.
  (Soil (border -30 [cm]))
  ;; Change in pH with depth, as a function of calendar year.
  (SoilpH year
          (pH_depth_year (1970 ((-100 [cm] 7.0 [pH]) (  0 [cm] 7.0 [pH])))
                         (1980 ((-100 [cm] 7.0 [pH]) (-20 [cm] 7.0 [pH])
                                ( -10 [cm] 6.5 [pH]) (  0 [cm] 6.5 [pH])))
                         (2000 ((-100 [cm] 7.0 [pH]) (-20 [cm] 6.8 [pH])
                                ( -10 [cm] 6.0 [pH]) (  0 [cm] 6.0 [pH])))))
  ;; Choose a deposition model that depends on vegetation.
  (Bioclimate original 
              (deposition Gundersen_et_al))
  (OrganicMatter original
                 (pH_factor (5 [pH] 0 []) (7 [pH] 1 []) (9 [pH] 0 [])))
  (Groundwater deep))

(deflog Afforestation column
  "Information relevant for the Afforestation vegetation model."
  (where "${colfid}afforestation.dlf")
  (when monthly)
  (summary (balance (input N_uptake) 
                    (output N_litter N_rhizo)
                    (content N_actual)))
  (entries (interval (tag "pH-min")
                     (path column "${column}" SoilpH component pH)
                     (spec soilph component  pH)
                     (handle current)
                     (multi min)
                     (space min))
           (interval (tag "pH-max")
                     (path column "${column}" SoilpH component pH)
                     (spec soilph component  pH)
                     (handle current)
                     (multi max)
                     (space max))
           (interval (tag "pH-avg")
                     (path column "${column}" SoilpH component pH)
                     (spec soilph component pH)
                     (handle current)
                     (density true)
                     (multi sum)
                     (space sum))
           (number (path column "${column}" Vegetation "*" LAI)
                   (dimension "m2/m2"))
           (number (path column "${column}" Vegetation "*" N_demand)
                   (dimension "kg N/ha")
                   (factor 10.0))
           (number (path column "${column}" Vegetation "*" N_actual)
                   (dimension "kg N/ha")
                   (factor 10.0))
           (number (path column "${column}" Vegetation "*" N_uptake)
                   (handle sum)
                   (dimension "kg N/ha")
                   (factor 10.0))
           (number (path column "${column}" Vegetation "*" N_litter)
                   (spec vegetation afforestation N_litter) 
                   (dimension "kg N/ha")
                   (handle sum))
           (number (path column "${column}" Vegetation "*" N_rhizo)
                   (spec vegetation afforestation N_rhizo) 
                   (dimension "kg N/ha")
                   (handle sum))
           (interval (path column "${column}" OrganicMatter default total_C)
                     (spec organic default total_C)
                     (tag "Soil")
                     (dimension "kg C/ha"))
           (interval (path column "${column}" OrganicMatter "default"
                           am "vegetation/root" om "*" C)
                     (spec AOM component C)
                     (tag "Root pool")
                     (dimension "kg C/ha"))
           (interval (path column "${column}" OrganicMatter "default"
                           am "bio/incorporation" om "*" C)
                     (spec AOM component C)
                     (tag "Leaf pool")
                     (dimension "kg C/ha"))
           (number (path column "${column}" OrganicMatter "default" 
                         am "*" om "*" top_C)
                   (spec AOM component top_C)
                   (tag "Surface")
                   (dimension "kg C/ha"))        ;g/cm^2 -> kg/ha

           ))

(deflog SoilpH column
  "Soil pH profile."
  (where "${colfid}soil_pH.dlf")
  (when true)
  (entries (array (path column "${column}" SoilpH component pH)
                  (spec soilph component pH)
                  (handle current))))

(defprogram Oak Daisy
  (weather default "dk-taastrup.dwf")
  (column Oak)
  (time 1969 12 1 23)
  (stop 2002 12 31 0)
  (manager activity)
  (output "Field nitrogen" ("Soil nitrogen" (to -30 [cm])) 
          (Afforestation (to -30 [cm])) SoilpH))

(run Oak)

;;; afforestation.dai ends here.
