Skip to content

Commit

Permalink
Merge commit '4d7cc81a255ad20c8c10ef8fd7146f744af610cf'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Feb 25, 2025
2 parents 646500b + 4d7cc81 commit e760210
Show file tree
Hide file tree
Showing 25 changed files with 1,097 additions and 235 deletions.
3 changes: 2 additions & 1 deletion agrolib/climate/climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,8 @@ bool aggregatedHourlyToDaily(meteoVariable myVar, Crit3DMeteoPoint* meteoPoint,
break;
}

if (hourlyVar == noMeteoVar || elab == noMeteoComp) return false;
if (hourlyVar == noMeteoVar || elab == noMeteoComp)
return false;

for (date = dateIni; date <= dateFin; date = date.addDays(1))
{
Expand Down
20 changes: 17 additions & 3 deletions agrolib/commonChartElements/callout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,24 @@ void Callout::updateGeometry()
prepareGeometryChange();
if (m_series != nullptr)
{
setPos(m_chart->mapToPosition(m_anchor, m_series) + QPoint(10, -50));
if (m_chart->mapToPosition(m_anchor).x() > m_chart->plotArea().center().x())
{
setPos(m_chart->mapToPosition(m_anchor, m_series) + QPoint(-130, -50));
}
else
{
setPos(m_chart->mapToPosition(m_anchor, m_series) + QPoint(10, -50));
}
}
else
{
setPos(m_chart->mapToPosition(m_anchor) + QPoint(10, -50));
{
if (m_chart->mapToPosition(m_anchor).x() > m_chart->plotArea().center().x())
{
setPos(m_chart->mapToPosition(m_anchor) + QPoint(-130, -50));
}
else
{
setPos(m_chart->mapToPosition(m_anchor) + QPoint(10, -50));
}
}
}
30 changes: 24 additions & 6 deletions agrolib/crit3dDate/crit3dDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,30 @@ Crit3DDate min(const Crit3DDate& myDate1, const Crit3DDate& myDate2)
return myDate2;
}


/*
Crit3DDate getDateFromDoy(int year, int doy)
{
return Crit3DDate(1, 1, year).addDays(doy-1);
}
*/
Crit3DDate getDateFromDoy(int year, int doy)
{
static const int daysBeforeMonth[2][13] = {
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, // Non leap year
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } // Leap year
};
int leap = int(isLeapYear(year)); // leap: 1 - not leap: 0
int month = 1;

// Compute the month
while (doy > daysBeforeMonth[leap][month]) {
month++;
}

// Compute the day
int day = doy - daysBeforeMonth[leap][month - 1];
return {day, month, year};
}


void Crit3DDate::setNullDate()
Expand All @@ -230,19 +249,16 @@ int difference(const Crit3DDate &firstDate, const Crit3DDate &lastDate)
return firstDate.daysTo(lastDate);
}


bool isLeapYear(int year)
{
// No year 0 in Gregorian calendar, so -1, -5, -9 etc are leap years
if (year < 1)
++year;
year += (year < 1);

if (year % 4 != 0) return false;
if (year % 100 != 0) return true;
return (year % 400 == 0);
}


int getDoyFromDate(const Crit3DDate& myDate)
{
int doy = doyMonth[myDate.month-1] + myDate.day;
Expand All @@ -255,7 +271,9 @@ int getDoyFromDate(const Crit3DDate& myDate)

int getMonthFromDoy(int doy,int year)
{
if (doy <1 || doy > 366) return NODATA;
if ((doy < 1) || (doy > 366))
return NODATA;

int month = 0;
int doyMonthSpecific[12];
for (int i=0;i<12;i++)
Expand Down
2 changes: 1 addition & 1 deletion agrolib/crop/biomass.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#define RADRT 1.E-3 // root radius (m)
#define STH0 0.8561 // intercept in self-thinning eq. (log(TREES) vs log(WST)) (m-2)
#define STH1 1.9551 // slope in self-thinning eq. (log(TREES) vs log(WST)) (kgDM-1)
#define ALLRUND 0.5 // coeff of allocation to roots in understorey (-)
//#define ALLRUND 0.5 // coeff of allocation to roots in understorey (-)

/*!
* Define soil respiration parameters, partition soil C into young and old components
Expand Down
16 changes: 8 additions & 8 deletions agrolib/crop/crop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ double Crit3DCrop::getDailyDegreeIncrease(double tmin, double tmax, int doy)
return 0;
}

double tmed = (tmin + MINVALUE(tmax, upperThermalThreshold)) * 0.5;
return MAXVALUE(tmed - thermalThreshold, 0);
double tmed = (tmin + std::min(tmax, upperThermalThreshold)) * 0.5;
return std::max(tmed - thermalThreshold, 0.);
}


Expand Down Expand Up @@ -564,7 +564,7 @@ double Crit3DCrop::computeRootLength(double currentDD, double waterTableDepth)
else
{
// in order to avoid numerical divergences when calculating density through cardioid and gamma function
currentDD = MAXVALUE(currentDD, 1.0);
currentDD = std::max(currentDD, 1.0);
newRootLength = root::getRootLengthDD(roots, currentDD, degreeDaysEmergence);
}
}
Expand Down Expand Up @@ -594,13 +594,13 @@ double Crit3DCrop::computeRootLength(double currentDD, double waterTableDepth)
if (currentDD > roots.degreeDaysRootGrowth)
newRootLength = roots.currentRootLength;
else
newRootLength = MINVALUE(newRootLength, roots.currentRootLength + MAX_DAILY_GROWTH);
newRootLength = std::min(newRootLength, roots.currentRootLength + MAX_DAILY_GROWTH);

// maximum root lenght
double maxRootLenght = waterTableDepth - MIN_WATERTABLE_DISTANCE - roots.rootDepthMin;
if (newRootLength > maxRootLenght)
{
newRootLength = MAXVALUE(roots.currentRootLength, maxRootLenght);
newRootLength = std::max(roots.currentRootLength, maxRootLenght);
}
}

Expand Down Expand Up @@ -646,7 +646,7 @@ void Crit3DCrop::computeRootLength3D(double currentDegreeDays, double totalSoilD
else
{
// in order to avoid numerical divergences
currentDegreeDays = MAXVALUE(currentDegreeDays, 1);
currentDegreeDays = std::max(currentDegreeDays, 1.0);
roots.currentRootLength = root::getRootLengthDD(roots, currentDegreeDays, degreeDaysEmergence);
}
}
Expand Down Expand Up @@ -709,7 +709,7 @@ double Crit3DCrop::getCropWaterDeficit(const std::vector<soil::Crit1DLayer> &soi
waterDeficit += soilLayers[unsigned(i)].FC - soilLayers[unsigned(i)].waterContent;
}

return MAXVALUE(waterDeficit, 0);
return std::max(waterDeficit, 0.);
}


Expand Down Expand Up @@ -825,7 +825,7 @@ double Crit3DCrop::computeTranspiration(double maxTranspiration, const std::vect
if (firstWaterStress > EPSILON && totRootDensityWithoutStress > EPSILON)
{
// redistribution acts on not stressed roots
redistribution = MINVALUE(firstWaterStress, totRootDensityWithoutStress) * maxTranspiration;
redistribution = std::min(firstWaterStress, totRootDensityWithoutStress) * maxTranspiration;

for (int i = roots.firstRootLayer; i <= roots.lastRootLayer; i++)
{
Expand Down
10 changes: 5 additions & 5 deletions agrolib/crop/development.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ namespace leafDevelopment
incrementalRatio = (getTheoreticalLAIGrowth(DD+5,a,b,laiMIN,laiMAX)-getTheoreticalLAIGrowth(DD-5,a,b,laiMIN,laiMAX))/(10.);
incrementalRatio *= getLaiStressCoefficient(fractionTranspirableSoilWater);
if (currentDD < growthDD)
newLai = MINVALUE(previousLai + thermalUnits*incrementalRatio,laiMAX);
newLai = std::min(previousLai + thermalUnits*incrementalRatio,laiMAX);
else
{
*isSenescence = true;
newLai = MINVALUE(previousLai + (thermalUnits-currentDD+growthDD)*incrementalRatio,laiMAX);
newLai = std::min(previousLai + (thermalUnits-currentDD+growthDD)*incrementalRatio,laiMAX);
}
*actualLaiMax = newLai;
}
Expand Down Expand Up @@ -121,8 +121,8 @@ namespace leafDevelopment
if (daysFromStartSenescence > LENGTH_SENESCENCE)
return LaiMin;

a = log(MAXVALUE(LAIStartSenescence, 0.1));
b = (log(MAXVALUE(LaiMin, 0.01)) - a) / LENGTH_SENESCENCE;
a = log(std::max(LAIStartSenescence, 0.1));
b = (log(std::max(LaiMin, 0.01)) - a) / LENGTH_SENESCENCE;

return exp(a + b * daysFromStartSenescence);
}
Expand All @@ -148,7 +148,7 @@ namespace leafDevelopment
double n4 = 4.0;
return myCrop->LAImin + (myCrop->LAImax - myCrop->LAImin) /
(1 + pow(10 * ((myDegreeDays - myCrop->degreeDaysIncrease)
/ MAXVALUE(myCrop->degreeDaysDecrease, 1)) / c4, n4));
/ std::max(myCrop->degreeDaysDecrease, 1.)) / c4, n4));
}
}

Expand Down
4 changes: 2 additions & 2 deletions agrolib/crop/root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ namespace root

int numberOfRootedLayers, numberOfTopUnrootedLayers;
numberOfTopUnrootedLayers = int(round(myCrop->roots.rootDepthMin / minimumThickness));
numberOfRootedLayers = int(round(MINVALUE(myCrop->roots.currentRootLength, soilDepth) / minimumThickness));
numberOfRootedLayers = int(round(std::min(myCrop->roots.currentRootLength, soilDepth) / minimumThickness));

// roots are still too short
if (numberOfRootedLayers == 0)
Expand Down Expand Up @@ -529,7 +529,7 @@ namespace root

int numberOfRootedLayers, numberOfTopUnrootedLayers;
numberOfTopUnrootedLayers = int(round(myCrop.roots.rootDepthMin / minimumThickness));
numberOfRootedLayers = int(round(MINVALUE(myCrop.roots.currentRootLength, currentSoil.totalDepth) / minimumThickness));
numberOfRootedLayers = int(round(std::min(myCrop.roots.currentRootLength, currentSoil.totalDepth) / minimumThickness));

// roots are still too short
if (numberOfRootedLayers == 0)
Expand Down
Loading

0 comments on commit e760210

Please sign in to comment.