11#ifndef FAUNA_FORAGE_VALUES_H
12#define FAUNA_FORAGE_VALUES_H
22class HerbivoreInterface;
56template <ForageValueTag tag>
77 const double na_value)
const {
79 for (
int ft = 0; ft <
array.size(); ft++) {
80 const double d = divisor.
array[ft];
95 throw std::invalid_argument(
96 "Fauna::ForageValues<>::get() "
97 "The forage type `ForageType::Inedible` is not allowed.");
98 assert((
int)ft <
array.size());
100 return array[(int)ft];
113 const double this_weight = 1.0,
114 const double other_weight = 1.0) {
115 for (
int ft = 0; ft <
array.size(); ft++)
125 if (&other ==
this)
return *
this;
126 for (
int ft = 0; ft <
array.size(); ft++)
135 if (&other ==
this)
return *
this;
136 for (
int ft = 0; ft <
array.size(); ft++)
147 throw std::invalid_argument(
148 "Fauna::ForageValues<>::get() "
149 "The forage type `ForageType::Inedible` is not allowed.");
150 assert((
int)ft <
array.size());
151 assert((
int)ft >= 0);
152 return array[(int)ft];
165 throw std::invalid_argument((std::string)
"ForageValues<> "
166 "Forage type `ForageType::Inedible` is not allowed."+
170 assert((
int)forage_type <
array.size());
171 assert((
int)forage_type >= 0);
172 array[(int)forage_type] = value;
189 return std::accumulate(
array.begin(),
array.end(), 0.0);
194 for (
int ft = 0; ft <
array.size(); ft++)
199 for (
int ft = 0; ft <
array.size(); ft++)
204 for (
int ft = 0; ft <
array.size(); ft++)
211 throw std::domain_error(
"Fauna::ForageValues<> Division by zero.");
212 for (
int ft = 0; ft <
array.size(); ft++)
219 return result.operator+=(rhs);
223 return result.operator-=(rhs);
227 return result.operator*=(rhs);
232 return result.operator/=(rhs);
237 return result.operator+=(rhs);
241 return result.operator-=(rhs);
245 return result.operator*=(rhs);
250 return result.operator/=(rhs);
254 for (
int ft = 0; ft <
array.size(); ft++)
259 for (
int ft = 0; ft <
array.size(); ft++)
264 for (
int ft = 0; ft <
array.size(); ft++)
270 for (
int ft = 0; ft <
array.size(); ft++) {
271 if (rhs.
array[ft] == 0)
272 throw std::domain_error(
273 (std::string)
"Fauna::ForageValues<> Division by zero." +
" (" +
281 for (
int ft = 0; ft <
array.size(); ft++)
288 for (
int ft = 0; ft <
array.size(); ft++)
294 for (
int ft = 0; ft <
array.size(); ft++)
299 for (
int ft = 0; ft <
array.size(); ft++)
304 for (
int ft = 0; ft <
array.size(); ft++)
309 for (
int ft = 0; ft <
array.size(); ft++)
348 throw std::invalid_argument(
349 "ForageValues<PositiveAndZero> Value < 0 not allowed. "
351 std::to_string(value) +
")");
355 if (value < 0.0 || value > 1.0) {
362 throw std::invalid_argument(
363 "ForageValues<ZeroToOne> Value is not in interval [0,1]. "
365 std::to_string(value) +
")");
369 throw std::logic_error(
371 "ForageValueTag not implemented.");
373 if (std::isnan(value))
374 throw std::invalid_argument(
375 "ForageValues<> NAN is not allowed as a value.");
376 if (std::isinf(value))
377 throw std::invalid_argument(
378 "ForageValues<> INFINITY is not allowed as a value.");
398typedef std::vector<std::pair<HerbivoreInterface*, ForageMass>>
443 const ForageValues<ForageValueTag::ZeroToOne>& fractions);
457 const ForageValues<ForageValueTag::PositiveAndZero> values,
458 const double tolerance);
473 const ForageValues<ForageValueTag::PositiveAndZero>& mj_per_kg,
474 const ForageValues<ForageValueTag::ZeroToOne>& mj_proportions);
Helper functions/classes to aggregate data.
Definition: forage_values.h:57
bool operator>=(const ForageValues< tag > &rhs) const
Definition: forage_values.h:308
static constexpr const double IMPRECISION_TOLERANCE
Tolerance range for imprecise floating point results.
Definition: forage_values.h:329
bool operator!=(const ForageValues< tag > &rhs) const
Definition: forage_values.h:287
ForageValues< tag > & operator*=(const ForageValues< tag > &rhs)
Definition: forage_values.h:263
ForageValues< tag > operator-(const ForageValues< tag > &rhs) const
Definition: forage_values.h:239
void set(const ForageType forage_type, double value)
Set a value, only finite values are allowed.
Definition: forage_values.h:162
double operator[](const ForageType ft) const
Read-only value access.
Definition: forage_values.h:142
ForageValues< tag > & operator-=(const ForageValues< tag > &rhs)
Definition: forage_values.h:258
ForageValues< tag > operator+(const double rhs) const
Definition: forage_values.h:217
void check_value(double &value) const
Helper function to throw exceptions in the set() functions.
Definition: forage_values.h:340
ForageValues< tag > operator*(const ForageValues< tag > &rhs) const
Definition: forage_values.h:243
ForageValues< tag > operator*(const double rhs) const
Definition: forage_values.h:225
ForageValues< tag > divide_safely(const ForageValues< tag > &divisor, const double na_value) const
Divide safely also by zero values.
Definition: forage_values.h:76
ForageValues< tag > & operator-=(const double rhs)
Definition: forage_values.h:198
bool operator<(const ForageValues< tag > &rhs) const
Definition: forage_values.h:293
bool operator==(const ForageValues< tag > &rhs) const
Definition: forage_values.h:280
double get(const ForageType ft) const
Get a value (read-only).
Definition: forage_values.h:93
ForageValues< tag > & operator/=(const ForageValues< tag > &rhs)
Definition: forage_values.h:269
bool operator>(const ForageValues< tag > &rhs) const
Definition: forage_values.h:303
double sum() const
Sum of all values.
Definition: forage_values.h:188
ForageValues< tag > & min(const ForageValues< tag > &other)
For each forage type, take the minimum value.
Definition: forage_values.h:134
ForageValues< tag > operator+(const ForageValues< tag > &rhs) const
Definition: forage_values.h:235
ForageValues< tag > & operator+=(const ForageValues< tag > &rhs)
Definition: forage_values.h:253
ForageValues< tag > & operator/=(const double rhs)
Definition: forage_values.h:209
ForageValues< tag > & operator*=(const double rhs)
Definition: forage_values.h:203
ForageValues< tag > & max(const ForageValues< tag > &other)
For each forage type, take the maximum value.
Definition: forage_values.h:124
ForageValues< tag > & operator+=(const double rhs)
Definition: forage_values.h:193
ForageValues< tag > operator/(const double rhs) const
Definition: forage_values.h:230
bool operator<=(const ForageValues< tag > &rhs) const
Definition: forage_values.h:298
double & operator[](const ForageType ft)
Write access to values.
Definition: forage_values.h:145
ForageValues< tag > operator-(const double rhs) const
Definition: forage_values.h:221
std::array< double, 1 > array
Forage values for all but ForageType::Inedible.
Definition: forage_values.h:333
ForageValues< tag > operator/(const ForageValues< tag > &rhs) const
Definition: forage_values.h:248
ForageValues< tag > & merge(const ForageValues< tag > &other, const double this_weight=1.0, const double other_weight=1.0)
Merge this object with another one by building (weighted) means.
Definition: forage_values.h:112
ForageValues(const double init_value=0.0)
Constructor with initializing value.
Definition: forage_values.h:66
void set(double value)
Set all forage types to one value.
Definition: forage_values.h:182
Definition of forage types (grass, browse, etc.)
const std::string & get_forage_type_name(const ForageType)
Get a short, lowercase string identifier for a forage type.
Definition: forage_types.cpp:30
std::vector< std::pair< HerbivoreInterface *, ForageMass > > ForageDistribution
Data structure defining which herbivore gets what to eat [kgDM/km²].
Definition: forage_values.h:399
ForageFraction foragevalues_to_foragefractions(const ForageValues< ForageValueTag::PositiveAndZero > values, const double tolerance)
Convert forage values to fractional values.
Definition: forage_values.cpp:22
ForageValues< ForageValueTag::PositiveAndZero > ForageEnergyContent
Net energy content [MJ/kgDM] for different forage types.
Definition: forage_values.h:389
ForageValues< ForageValueTag::PositiveAndZero > foragefractions_to_foragevalues(const ForageValues< ForageValueTag::ZeroToOne > &fractions)
Convert forage fractions (in [0,1]) into values in [0,∞].
Definition: forage_values.cpp:16
ForageValues< ForageValueTag::PositiveAndZero > ForageMass
Dry matter mass values [kgDM or kgDM/km²] for different forage types.
Definition: forage_values.h:395
double average(const double a, const double b, const double weight_a=1.0, const double weight_b=1.0)
Build weighted average of two numbers.
Definition: average.cpp:25
ForageValues< ForageValueTag::ZeroToOne > ForageFraction
A fraction for each forage type.
Definition: forage_values.h:392
ForageValues< ForageValueTag::PositiveAndZero > ForageEnergy
Energy values [MJ] for different forage types.
Definition: forage_values.h:386
ForageType
Different types of forage.
Definition: forage_types.h:21
@ Inedible
Plants that are not edible for herbivores.
ForageFraction convert_mj_to_kg_proportionally(const ForageValues< ForageValueTag::PositiveAndZero > &mj_per_kg, const ForageValues< ForageValueTag::ZeroToOne > &mj_proportions)
Convert forage energy to mass keeping the energy-wise proportions.
Definition: forage_values.cpp:51
ForageValueTag
Describes which values are allowed in Fauna::ForageValues.
Definition: forage_values.h:25
@ ZeroToOne
Only values in interval [0,1] are allowed.
@ PositiveAndZero
Only values in interval [0,∞) are allowed.
const std::set< ForageType > FORAGE_TYPES
Definition: forage_types.cpp:27
ForageValues< ForageValueTag::ZeroToOne > Digestibility
Digestibility [fraction] for different forage types.
Definition: forage_values.h:383
ForageValues< ForageValueTag::PositiveAndZero > operator*(const double lhs, const ForageFraction &rhs)
Multiply forage fractions with coefficient, allowing numbers >0.
Definition: forage_values.h:411