Modular Megafauna Model 1.1.5
A physiological, dynamic herbivore simulator in C++.
Loading...
Searching...
No Matches
grass_forage.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2020 W. Traylor <wolfgang.traylor@senckenberg.de>
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
11#ifndef FAUNA_GRASS_FORAGE_H
12#define FAUNA_GRASS_FORAGE_H
13
14#include <cassert>
15#include "Fauna/forage_base.h"
16
17namespace Fauna {
19class GrassForage : public ForageBase {
20 private:
21 double fpc;
22
24 static constexpr double MAX_SWARD_DENSITY = 2e7; // 20 kgDM/m²
25 public:
27 GrassForage() : ForageBase(), fpc(0.0) {}
28
30
36 double get_sward_density() const {
37 if (get_fpc() == 0) return 0.0;
38 const double sd = get_mass() / get_fpc();
39 assert(sd >= 0.0 && sd >= get_mass());
40 // Only throw an error about preposterously high sward density if there is
41 // actually substantial grass in the habitat. Otherwise there might be
42 // spurious erros if both FPC and total grass density are extremely low due
43 // to model artifacts.
44 // The value of 50 gDM/m² (= 5e4 kgDM/km²) is chosen arbitrarily.
45 if (sd > MAX_SWARD_DENSITY && get_mass() > 5e4) {
46 throw std::logic_error(
47 "Fauna::GrassForage::get_sward_density() "
48 "The grass sward density is unreasonably high (" +
49 std::to_string(sd / 1e4) +
50 " gDM/m²). "
51 "This might be because the vegetation model gave an unrealistically "
52 "low FPC value (fraction of habitat covered by grass).\n"
53 "\tFPC = " +
54 std::to_string(get_fpc()) +
55 "\n"
56 "\twhole-habitat grass density = " +
57 std::to_string(get_mass() / 1e4) +
58 " gDM/m²\n"
59 "Please check the vegetation model. Consider hard-setting the FPC "
60 "value to a constant (e.g. 0.8 in grassland) or a minimum (e.g. "
61 "0.1).");
62 }
63 return sd;
64 }
65
67
77 double get_fpc() const {
78 if (get_mass() == 0.0 && fpc != 0.0)
79 throw std::logic_error(
80 "Fauna::GrassForage::get_fpc() "
81 "Mass is zero, but FPC is not zero.");
82 if (get_mass() != 0.0 && fpc == 0.0)
83 throw std::logic_error(
84 "Fauna::GrassForage::get_fpc() "
85 "Mass is not zero, but FPC is zero.");
86 return fpc;
87 }
88
90 GrassForage& merge(const GrassForage& other, const double this_weight,
91 const double other_weight);
92
99 void set_fpc(const double f) {
100 if (!(f >= 0.0 && f <= 1.0))
101 throw std::invalid_argument(
102 "Fauna::GrassForage::set_fpc() "
103 "FPC out of valid range (0.0–1.0).");
104 if (get_mass() == 0.0 && f > 0.0)
105 throw std::logic_error(
106 "Fauna::GrassForage::set_fpc() "
107 "FPC must be zero if mass is zero.");
108 if (get_mass() > 0.0 && f == 0.0)
109 throw std::logic_error(
110 "Fauna::GrassForage::set_fpc() "
111 "FPC cannot be zero if there is grass mass.");
112 fpc = f;
113 }
114};
115} // namespace Fauna
116#endif // FAUNA_GRASS_FORAGE_H
Base class for herbivore forage in a habitat.
Definition: forage_base.h:19
double get_mass() const
Dry matter forage biomass over the whole area [kgDM/km²].
Definition: forage_base.h:32
Grass forage in a habitat.
Definition: grass_forage.h:19
double get_fpc() const
Fraction of habitat covered by grass [fractional].
Definition: grass_forage.h:77
void set_fpc(const double f)
Fraction of habitat covered by grass [fractional].
Definition: grass_forage.h:99
static constexpr double MAX_SWARD_DENSITY
Maximum imaginable real-world sward density [kgDM/km²].
Definition: grass_forage.h:24
double fpc
Definition: grass_forage.h:21
double get_sward_density() const
Dry matter forage in the area covered by grass [kgDM/km²].
Definition: grass_forage.h:36
GrassForage()
Constructor with zero values.
Definition: grass_forage.h:27
GrassForage & merge(const GrassForage &other, const double this_weight, const double other_weight)
Merge this object with another one by building (weighted) means.
Definition: grass_forage.cpp:16
Base class for all forage types.
Definition: average.h:16