Skip to content

Commit

Permalink
Changed "MasterPlantProperties" to be more elegant
Browse files Browse the repository at this point in the history
  • Loading branch information
skairunner committed Mar 2, 2014
1 parent c6c2d74 commit 6ce08dd
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 156 deletions.
8 changes: 5 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <iostream>
#include "Engine_tcod.h"
#include "testingSuite.h"


using namespace std;
Expand All @@ -10,9 +11,10 @@ using namespace std;
int main(int argc, char **argv)
{
// vector<string> list = { "fescue grass", "fescue grass", "fescue grass", "fescue grass", "oak" };
/*vector<string> list = { "oak", "oak" };
Tests::multiplePlants(360 * 50, list);*/

vector<string> list = { "oak", "oak" };
ALMANAC::Tests::multiplePlants(360 * 50, list);
return 0;

CursesEngine.Init("Prealpha version 0.0.1", "Content/asciisquare.bmp", 100, 48);
CursesEngine.Start();
CursesEngine.End();
Expand Down
2 changes: 1 addition & 1 deletion plant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void BasePlant::calculate(const WeatherData& data, const double& albedo, const d

potentialDeltaBiomass *= REG;
//test
potentialDeltaBiomass -= min(0.005, Biomass * 0.002);
//potentialDeltaBiomass -= min(0.005, Biomass * 0.002);
partitionBiomass(potentialDeltaBiomass);

currentWaterlogValue -= 0.005;
Expand Down
22 changes: 11 additions & 11 deletions plantDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ void PlantDictionary::loadProperties()
pp.isAnnual = plant["annual"].asBool();
if (!pp.isAnnual)
{
assignPairs(pp.leafFallPeriod, plant["leaf fall period"]);
assignPairs(pp.maxAge, plant["max age"]);
assignPairs(pp.leafFallPeriod_r, plant["leaf fall period"]);
assignPairs(pp.maxAge_r, plant["max age"]);
}
pp.isTree = plant["is tree"].asBool();
if (pp.isTree)
{
assignPairs(pp.yearsUntilMaturity, plant["maturity"]);
assignPairs(pp.vegetativeMaturity, plant["vegetative maturity"]);
assignPairs(pp.maxYearlyGrowth, plant["max yearly growth"]);
assignPairs(pp.yearsUntilMaturity_r, plant["maturity"]);
assignPairs(pp.vegetativeMaturity_r, plant["vegetative maturity"]);
assignPairs(pp.maxYearlyGrowth_r, plant["max yearly growth"]);
}

assignPairs(pp.maxLAI, plant["maxLAI"]);
assignPairs(pp.maxLAI_r, plant["maxLAI"]);
if (plant["uses custom LAI graph"].asBool())
{
double leftroot = plant["LAI graph"]["left root"].asDouble();
Expand All @@ -148,9 +148,9 @@ void PlantDictionary::loadProperties()
pp.growthStages[9] = plant["stage 9"].asDouble();
pp.growthStages[10] = plant["stage 10"].asDouble();
pp.baseTemp = plant["base temp"].asDouble();
assignPairs(pp.waterTolerence, plant["water tolerence"]);
assignPairs(pp.maxHeight, plant["max height"]);
assignPairs(pp.maxRootDepth, plant["max root depth"]);
assignPairs(pp.waterTolerence_r, plant["water tolerence"]);
assignPairs(pp.maxHeight_r, plant["max height"]);
assignPairs(pp.maxRootDepth_r, plant["max root depth"]);
pp.startingNitrogenConcentration = plant["starting nitrogen"].asDouble();
pp.finalNitrogenConcentration = plant["final nitrogen"].asDouble();
pp.dayNeutral = plant["day neutral"].asBool();
Expand Down Expand Up @@ -241,8 +241,8 @@ void PlantDictionary::loadProperties()

pp.minGerminationTemp = plant["germination"]["min temp"].asDouble();
pp.optimalGerminationTemp = plant["germination"]["optimal temp"].asDouble();
assignPairs(pp.averageFruitWeight, plant["average fruit weight"]);
assignPairs(pp.seedRatio, plant["seed ratio"]);
assignPairs(pp.averageFruitWeight_r, plant["average fruit weight"]);
assignPairs(pp.seedRatio_r, plant["seed ratio"]);
pp.dormancy = plant["seed dormancy"].asInt();
pp.seedViability = plant["seed viability"].asDouble();

Expand Down
71 changes: 16 additions & 55 deletions plantproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,61 +50,22 @@ PlantProperties MasterPlantProperties::convert(MendelianInheritance& mendel)
{
PlantProperties out;

out.name = name;

out.gene_maxLAI = mendel.spawnInRange(maxLAI.first, maxLAI.second);
out.growthStages = growthStages;
out.baseTemp = baseTemp;
out.gene_waterTolerence = mendel.spawnInRange(waterTolerence);
out.gene_maxHeight = mendel.spawnInRange(maxHeight);
out.gene_maxYearlyGrowth = mendel.spawnInRange(maxYearlyGrowth);
out.gene_maxRootDepth = mendel.spawnInRange(maxRootDepth);
out.HeatUnitFactorNums = HeatUnitFactorNums;
out.CO2CurveFactors = CO2CurveFactors;
out.flowerTempCurve = flowerTempCurve;
out.vernalizationCurve = vernalizationCurve;
out.LAIGraph = LAIGraph;
out.nightLengthCurve = nightLengthCurve;

out.startingNitrogenConcentration = startingNitrogenConcentration;
out.finalNitrogenConcentration = finalNitrogenConcentration;

out.baseRatios = baseRatios;
out.fruitingRatios = fruitingRatios;
out.finalRatios = finalRatios;

out.dormantHeightDecrease = dormantHeightDecrease;
out.dormantRootDecrease = dormantRootDecrease;

out.minimumTemperature = minimumTemperature;
out.optimalTemperature = optimalTemperature;

out.minGerminationTemp = minGerminationTemp;
out.optimalGerminationTemp = optimalGerminationTemp;
out.germinationThermalUnits = germinationThermalUnits;
out.gene_averageFruitWeight = mendel.spawnInRange(averageFruitWeight);
out.gene_seedRatio = mendel.spawnInRange(seedRatio);
out.seedViability = seedViability;
out.dormancy = dormancy;

out.minFloweringTemp = minFloweringTemp;
out.optimalFloweringTemp = optimalFloweringTemp;

out.minVernalizationTemp = minVernalizationTemp;
out.floralInductionUnitsRequired = floralInductionUnitsRequired;
out.dayNeutral = dayNeutral;
out.longDayPlant = longDayPlant;
out.minimumInduction = minimumInduction;
out.criticalNightLength = criticalNightLength;

out.biomassToVPD = biomassToVPD;

out.isAnnual = isAnnual;
out.isTree = isTree;
out.gene_yearsUntilMaturity = mendel.spawnInRange(yearsUntilMaturity);
out.gene_vegetativeMaturity = mendel.spawnInRange(vegetativeMaturity); // same
out.gene_maxAge = mendel.spawnInRange(maxAge);
out.gene_leafFallPeriod = mendel.spawnInRange(leafFallPeriod);
out = *this;

out.gene_maxLAI = mendel.spawnInRange(maxLAI_r);

out.gene_waterTolerence = mendel.spawnInRange(waterTolerence_r);
out.gene_maxHeight = mendel.spawnInRange(maxHeight_r);
out.gene_maxYearlyGrowth = mendel.spawnInRange(maxYearlyGrowth_r);
out.gene_maxRootDepth = mendel.spawnInRange(maxRootDepth_r);

out.gene_averageFruitWeight = mendel.spawnInRange(averageFruitWeight_r);
out.gene_seedRatio = mendel.spawnInRange(seedRatio_r);

out.gene_yearsUntilMaturity = mendel.spawnInRange(yearsUntilMaturity_r);
out.gene_vegetativeMaturity = mendel.spawnInRange(vegetativeMaturity_r); // same
out.gene_maxAge = mendel.spawnInRange(maxAge_r);
out.gene_leafFallPeriod = mendel.spawnInRange(leafFallPeriod_r);

return out;
}
Expand Down
99 changes: 25 additions & 74 deletions plantproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,80 +43,6 @@ namespace ALMANAC
PolyGene lerp;
};

struct PlantProperties;

// Stores the range of variables when applicable, and can be converted to a PlantProperties with genes.
struct MasterPlantProperties
{
PlantProperties convert(MendelianInheritance& mendel);


std::string name;

pair<double, double> maxLAI;
std::map<int, double> growthStages;
double baseTemp; // ¡ÆC, for GDD calcs
pair<double, double> waterTolerence;
pair<double, double> maxHeight; // mm
pair<double, double> maxYearlyGrowth; // mm, trees only
pair<double, double> maxRootDepth;
SCurve HeatUnitFactorNums; // how fast it grows, shared atm
SCurve CO2CurveFactors; /// This one is shared for all plants.
Parabola flowerTempCurve;
Parabola tempCurve; //growing temp
Parabola vernalizationCurve;
LeafDistribution LAIGraph;
SCurve nightLengthCurve;

double startingNitrogenConcentration;
double finalNitrogenConcentration;

BiomassHolder baseRatios;
BiomassHolder fruitingRatios;
BiomassHolder finalRatios;

double dormantHeightDecrease;
double dormantRootDecrease;
BiomassHolder dormantBiomassDecrease;

double minimumTemperature;
double optimalTemperature;

double minGerminationTemp;
double optimalGerminationTemp;
double germinationThermalUnits;
pair<double, double> averageFruitWeight; // kg
pair<double, double> seedRatio;
double seedViability; // 0 <= x <= 1
int dormancy;

double minFloweringTemp;
double optimalFloweringTemp;

double minVernalizationTemp; // default 0 C
double optimalVernalizationTemp; // default 2.5 C
double vernalizationThermalUnits; // default 45
bool needsVernalization; // default FALSE
bool isObligateVernalization; // default FALSE


double floralInductionUnitsRequired; // defaults to 7
bool dayNeutral;
bool longDayPlant;
double minimumInduction;
double criticalNightLength;

double biomassToVPD; // bc(3), =7

bool isAnnual; // limits HU to the maturity HUs.
bool isTree;
pair<int, int> yearsUntilMaturity; // trees only
pair<int, int> vegetativeMaturity; // same
pair<int,int> maxAge;
pair<int, int> leafFallPeriod; // days. over how many days the plant loses its LAI.
};


/// This class has intristic properties that do not change over the lifetime of a plant, eg its max LAI, growth stages, and various constants.
struct PlantProperties
{
Expand Down Expand Up @@ -196,6 +122,31 @@ namespace ALMANAC
PolyGene gene_leafFallPeriod;
PolyGene gene_seedRatio;
};

// Stores the range of variables when applicable, and can be converted to a PlantProperties with genes.
struct MasterPlantProperties : public PlantProperties
{
PlantProperties convert(MendelianInheritance& mendel);




pair<double, double> maxLAI_r;

pair<double, double> waterTolerence_r;
pair<double, double> maxHeight_r; // mm
pair<double, double> maxYearlyGrowth_r; // mm, trees only
pair<double, double> maxRootDepth_r;


pair<double, double> averageFruitWeight_r; // kg
pair<double, double> seedRatio_r;

pair<int, int> yearsUntilMaturity_r; // trees only
pair<int, int> vegetativeMaturity_r; // same
pair<int, int> maxAge_r;
pair<int, int> leafFallPeriod_r; // days. over how many days the plant loses its LAI.
};
}

extern MendelianInheritance MendelModule;
51 changes: 39 additions & 12 deletions testingSuite.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "testingSuite.h"
#include "plantDictionary.h"
#include "utility_visual.h"

using namespace ALMANAC;

Expand Down Expand Up @@ -146,6 +147,8 @@ void Tests::singlePlant(const int daysToRun, const std::string& plantname, Month

void Tests::multiplePlants(const int daysToRun, const std::vector<std::string>& plantnames, Month startDate)
{
bool discardSeeds = true;

Weather WeatherModule(true);
SoilGrid sg(1, 1);

Expand All @@ -154,10 +157,14 @@ void Tests::multiplePlants(const int daysToRun, const std::vector<std::string>&
filename += s + "_";
filename += ".txt";

for (int counter = 0; counter < plantnames.size()-1; counter++)
for (int counter = 0; counter < plantnames.size() - 1; counter++)
{
sg.ref(0, 0).plants.push_back(BasePlant(PD.getPlant(plantnames[0]), PD.getVisual(plantnames[0]), &sg.ref(0, 0)));
//sg.ref(0, 0).seeds.push_back(Seed(PD.getPlant(plantnames[0]), PD.getVisual(plantnames[0]), startDate, 120, 0.5))
}



cout << soilDict.getSoilName(sg.ref(0, 0).getTopsoilType()) << endl;
fstream rad; rad.open("logs/rad", fstream::trunc | fstream::out);
fstream LAIlog; LAIlog.open("logs/LAIlog-" + filename, fstream::trunc | fstream::out);
fstream Nlog; Nlog.open("logs/nitrogen-" + filename, fstream::trunc | fstream::out);
Expand Down Expand Up @@ -210,6 +217,10 @@ void Tests::multiplePlants(const int daysToRun, const std::vector<std::string>&
rad << sg.radPerPlant[c] << "\t";
}

if (discardSeeds)
sg.ref(0, 0).seeds.clear();


if (sg.radPerPlant.size() < plantnames.size())
for (int c = plantnames.size() - sg.radPerPlant.size(); c > 0; c--)
rad << "\t";
Expand Down Expand Up @@ -238,25 +249,41 @@ void Tests::multiplePlants(const int daysToRun, const std::vector<std::string>&
Nlog << "\n";
}

sg.ref(0, 0).plants.back().createSeeds(WeatherModule.getMonth());
auto date = WeatherModule.getDataBundle().date;
if (date.getYear() == 2063)
cout << ".";

auto seeds = sg.ref(0, 0).plants.back().seedlist;
if (sg.ref(0, 0).plants.size() != 0)
{
sg.ref(0, 0).plants.back().createSeeds(WeatherModule.getMonth());

int seedcounter = 0;
for (auto seed : seeds)
auto seeds = sg.ref(0, 0).plants.back().seedlist;

int seedcounter = 0;
for (auto seed : seeds)
{
seedcounter++;
cout << "Seed " << seedcounter << " biomass: " << seed.seedBiomass * 1000 << "g\n";
}
}
else
{
seedcounter++;
cout << "Seed " << seedcounter << " biomass: " << seed.seedBiomass * 1000 << "g\n";
cout << "no plants detected";
}



/////

cout << sg.ref(0, 0).plants.back().getBiomass();
if (sg.ref(0, 0).plants.back().isDead())
cout << "Dead.\n";
cout << "\nDone.";
if (sg.ref(0, 0).plants.size() > 0)
{
cout << sg.ref(0, 0).plants.back().getBiomass();
if (sg.ref(0, 0).plants.back().isDead())
cout << "Dead.";
}
cout << "\nDone";



cin.ignore(1);
return;
Expand Down

0 comments on commit 6ce08dd

Please sign in to comment.