Skip to content

Commit

Permalink
update 3D
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Nov 26, 2023
1 parent ff7496c commit a31d16a
Show file tree
Hide file tree
Showing 12 changed files with 693 additions and 718 deletions.
60 changes: 23 additions & 37 deletions agrolib/crop/crop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,9 @@ void Crit3DCrop::clear()
idCrop = "";
type = HERBACEOUS_ANNUAL;

/*!
* \brief roots
*/
roots.clear();

/*!
* \brief crop cycle
*/
// crop cycle
sowingDoy = NODATA;
currentSowingDoy = NODATA;
doyStartSenescence = NODATA;
Expand All @@ -74,17 +69,13 @@ void Crit3DCrop::clear()
degreeDaysDecrease = NODATA;
degreeDaysEmergence = NODATA;

/*!
* \brief water need
*/
// water need
kcMax = NODATA;
psiLeaf = NODATA;
stressTolerance = NODATA;
fRAW = NODATA;

/*!
* \brief irrigation
*/
// irrigation
irrigationShift = NODATA;
irrigationVolume = NODATA;
degreeDaysStartIrrigation = NODATA;
Expand All @@ -93,9 +84,7 @@ void Crit3DCrop::clear()
doyEndIrrigation = NODATA;
maxSurfacePuddle = NODATA;

/*!
* \brief variables
*/
// variables
isLiving = false;
isEmerged = false;
LAIstartSenescence = NODATA;
Expand Down Expand Up @@ -330,7 +319,7 @@ bool Crit3DCrop::isRootStatic() const

/*!
* \brief getSurfaceWaterPonding
* \return maximum height of water ponding [mm]
* \return maximum height of water pond [mm]
*/
double Crit3DCrop::getSurfaceWaterPonding() const
{
Expand Down Expand Up @@ -406,7 +395,7 @@ bool Crit3DCrop::needReset(Crit3DDate myDate, double latitude, double waterTable


// reset of (already initialized) crop
// TODO: smart start (using sowing doy and cycle)
// TODO: smart start (using meteo settings)
void Crit3DCrop::resetCrop(unsigned int nrLayers)
{
// roots
Expand Down Expand Up @@ -452,7 +441,7 @@ bool Crit3DCrop::dailyUpdate(const Crit3DDate &myDate, double latitude, const st

unsigned int nrLayers = unsigned(soilLayers.size());

// check start/end crop cycle (update isLiving)
// check start/end crop cycle
if (needReset(myDate, latitude, waterTableDepth))
{
resetCrop(nrLayers);
Expand Down Expand Up @@ -520,29 +509,26 @@ bool Crit3DCrop::restore(const Crit3DDate &myDate, double latitude, const std::v
}


// Liangxia Zhang, Zhongmin Hu, Jiangwen Fan, Decheng Zhou & Fengpei Tang, 2014
// A meta-analysis of the canopy light extinction coefficient in terrestrial ecosystems
// "Cropland had the highest value of K (0.62), followed by broadleaf forest (0.59),
// shrubland (0.56), grassland (0.50), and needleleaf forest (0.45)"
double Crit3DCrop::getSurfaceCoverFraction()
/*! \brief getCoveredSurfaceFraction
* \ref Liangxia Zhang, Zhongmin Hu, Jiangwen Fan, Decheng Zhou & Fengpei Tang, 2014
* A meta-analysis of the canopy light extinction coefficient in terrestrial ecosystems
* "Cropland had the highest value of K (0.62), followed by broadleaf forest (0.59)
* shrubland (0.56), grassland (0.50), and needleleaf forest (0.45)"
* \return covered surface fraction [-]
*/
double Crit3DCrop::getCoveredSurfaceFraction()
{
double k = 0.6; // [-] light extinction coefficient
if (idCrop == "" || ! isLiving || LAI < EPSILON) return 0;

if (idCrop == "" || ! isLiving || LAI < EPSILON)
{
return 0;
}
else
{
return 1 - exp(-k * LAI);
}
double k = 0.6; // [-] light extinction coefficient
return 1 - exp(-k * LAI);
}


double Crit3DCrop::getMaxEvaporation(double ET0)
{
double evapMax = ET0 * (1.0 - getSurfaceCoverFraction());
// TODO check
double evapMax = ET0 * (1.0 - getCoveredSurfaceFraction());
// TODO check evaporation on free water
return evapMax * 0.66;
}

Expand All @@ -552,9 +538,9 @@ double Crit3DCrop::getMaxTranspiration(double ET0)
if (idCrop == "" || ! isLiving || LAI < EPSILON)
return 0;

double SCF = this->getSurfaceCoverFraction();
double kcmaxFactor = 1 + (kcMax - 1) * SCF;
return ET0 * (SCF * kcmaxFactor);
double coverSurfFraction = getCoveredSurfaceFraction();
double kcFactor = 1 + (kcMax - 1) * coverSurfFraction;
return ET0 * coverSurfFraction * kcFactor;
}


Expand Down
2 changes: 1 addition & 1 deletion agrolib/crop/crop.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
bool restore(const Crit3DDate &myDate, double latitude, const std::vector<soil::Crit3DLayer> &soilLayers,
double currentWaterTable, std::string &myError);

double getSurfaceCoverFraction();
double getCoveredSurfaceFraction();
double getMaxEvaporation(double ET0);
double getMaxTranspiration(double ET0);
double getSurfaceWaterPonding() const;
Expand Down
6 changes: 5 additions & 1 deletion agrolib/crop/cropDbTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ bool loadCropParameters(const QSqlDatabase &dbCrop, QString idCrop, Crit3DCrop &
myCrop.roots.rootDepthMax = query.value("root_depth_max").toDouble();
myCrop.roots.actualRootDepthMax = myCrop.roots.rootDepthMax;

if (! getValue(query.value("roots_additional_cohesion"), &(myCrop.roots.rootsAdditionalCohesion)))
if (fieldExists(query, "roots_additional_cohesion"))
{
getValue(query.value("roots_additional_cohesion"), &(myCrop.roots.rootsAdditionalCohesion));
}
else
{
// default: no mechanical effect of roots
myCrop.roots.rootsAdditionalCohesion = 0;
Expand Down
9 changes: 5 additions & 4 deletions agrolib/interpolation/interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
[email protected]
*/

#include <ostream>
#include <stdlib.h>
#include <math.h>
#include <vector>
Expand Down Expand Up @@ -1172,7 +1171,7 @@ float retrend(meteoVariable myVar, vector<double> myProxyValues, Crit3DInterpola

if (! getUseDetrendingVar(myVar)) return 0.;

float retrendValue = 0.;
double retrendValue = 0.;
double myProxyValue;
Crit3DProxy* myProxy = nullptr;
Crit3DProxyCombination myCombination = mySettings->getCurrentCombination();
Expand Down Expand Up @@ -1223,7 +1222,7 @@ float retrend(meteoVariable myVar, vector<double> myProxyValues, Crit3DInterpola
}
}

return retrendValue;
return float(retrendValue);
}


Expand Down Expand Up @@ -1395,6 +1394,7 @@ std::vector <double> getfittingParameters(Crit3DProxyCombination myCombination,
return myParam;
}


bool multipleDetrending(std::vector <Crit3DInterpolationDataPoint> &myPoints, Crit3DInterpolationSettings* mySettings, meteoVariable myVar)
{
if (! getUseDetrendingVar(myVar)) return false;
Expand Down Expand Up @@ -1519,7 +1519,7 @@ bool multipleDetrending(std::vector <Crit3DInterpolationDataPoint> &myPoints, Cr
setFittingParameters(myCombination, mySettings, myFunc, parametersMin, parametersMax, parametersDelta, parameters);

// multiple non linear fitting
int nSteps = interpolation::bestFittingMarquardt_nDimension(&functionSum, myFunc, 10000, 5, parametersMin, parametersMax, parameters, parametersDelta,
interpolation::bestFittingMarquardt_nDimension(&functionSum, myFunc, 10000, 5, parametersMin, parametersMax, parameters, parametersDelta,
100, EPSILON, 0.01, predictors, predictands, false, weights);

mySettings->setFittingFunction(myFunc);
Expand Down Expand Up @@ -1547,6 +1547,7 @@ bool multipleDetrending(std::vector <Crit3DInterpolationDataPoint> &myPoints, Cr
return true;
}


void topographicDistanceOptimize(meteoVariable myVar,
Crit3DMeteoPoint* &myMeteoPoints,
int nrMeteoPoints,
Expand Down
3 changes: 2 additions & 1 deletion agrolib/snow/snow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ void Crit3DSnow::computeSnowBrooksModel()
double freeze_melt = 0; // [mm] freeze (positive) or melt (negative)

// pag.53 (3.25)
/*! net amount of liquid water that freezes (heat added to the snow pack) and ice that melts (heat removed from the snow pack) */
/*! net amount of liquid water that freezes (heat added to the snow pack)
* and ice that melts (heat removed from the snow pack) */
double w = (prevInternalEnergy + QTotal) / (LATENT_HEAT_FUSION * WATER_DENSITY); // [m]
if (w < 0)
{
Expand Down
7 changes: 7 additions & 0 deletions agrolib/utilities/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ QList<QString> getFieldsUpperCase(const QSqlQuery& query)
}


bool fieldExists(const QSqlQuery &query, const QString fieldName)
{
QList<QString> fieldList = getFieldsUpperCase(query);
return fieldList.contains(fieldName.toUpper());
}


// return boolean (false if recordset is not valid)
bool getValue(QVariant myRs)
{
Expand Down
1 change: 1 addition & 0 deletions agrolib/utilities/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
QList<QString> getFields(QSqlDatabase* db_, QString tableName);
QList<QString> getFields(const QSqlQuery& query);
QList<QString> getFieldsUpperCase(const QSqlQuery &query);
bool fieldExists(const QSqlQuery &query, const QString fieldName);

bool getValue(QVariant myRs);
bool getValue(QVariant myRs, int* myValue);
Expand Down
Loading

0 comments on commit a31d16a

Please sign in to comment.