Skip to content

Commit

Permalink
AD unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
holos-aafc committed Apr 15, 2024
1 parent a46b098 commit 3956bad
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public void TestInitialize()
{
var n2oEmissionFactorCalculator = new N2OEmissionFactorCalculator(_climateProvider);




_sut = n2oEmissionFactorCalculator;
_sut.ManureService = base._mockManureServiceObject;
_sut.ClimateProvider = base._mockClimateProviderObject;
Expand Down Expand Up @@ -572,7 +569,7 @@ public void CalculateAmmoniaEmissionsFromImportedManure()
[TestMethod]
public void GetManureNitrogenRemainingForFieldTest()
{
_mockManureService.Setup(x => x.GetTotalNitrogenRemainingForFarmAndYear(It.IsAny<int>(), It.IsAny<Farm>())).Returns(600);
_mockManureService.Setup(x => x.GetTotalManureNitrogenRemainingForFarmAndYear(It.IsAny<int>(), It.IsAny<Farm>())).Returns(600);

var cropViewItem = new CropViewItem() {Year = 2022, Area = 20};
var farm = new Farm();
Expand Down Expand Up @@ -870,13 +867,19 @@ public void CalculateDirectN2ONFromLeftOverManureForField()

_farm.Components.Add(fieldWithOutManureApplications);

_mockManureService.Setup(x => x.GetTotalNitrogenRemainingForFarmAndYear(It.IsAny<int>(), It.IsAny<Farm>())).Returns(10);
_mockManureService.Setup(x => x.GetTotalManureNitrogenRemainingForFarmAndYear(It.IsAny<int>(), It.IsAny<Farm>())).Returns(10);

var result = _sut.CalculateDirectN2ONFromLeftOverManureForField(_farm, _viewItem);

Assert.AreEqual(0.0021318977402640473, result);
}

[TestMethod]
public void GetTotalDigestateNitrogenRemainingForFarmAndYearTest()
{

}

#endregion
}
}
72 changes: 71 additions & 1 deletion H.Core.Test/Services/Animals/DigestateServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,26 @@ public void TestInitialize()
Date = DateTime.Now,
FlowRateOfAllSubstratesInDigestate = 100,
TotalAmountOfCarbonInRawDigestateAvailableForLandApplication = 10,
TotalAmountOfNitrogenFromRawDigestateAvailableForLandApplication = 20,
};

var dailyOutput2 = new DigestorDailyOutput()
{
Date = DateTime.Now.AddDays(1),
FlowRateOfAllSubstratesInDigestate = 100,
TotalAmountOfCarbonInRawDigestateAvailableForLandApplication = 30,
TotalAmountOfNitrogenFromRawDigestateAvailableForLandApplication = 50,
};

_dailyResults = new List<DigestorDailyOutput>() { dailyOutput1, dailyOutput2 };
var dailyOutput3 = new DigestorDailyOutput()
{
Date = DateTime.Now.AddDays(-1000),
FlowRateOfAllSubstratesInDigestate = 100,
TotalAmountOfCarbonInRawDigestateAvailableForLandApplication = 30,
TotalAmountOfNitrogenFromRawDigestateAvailableForLandApplication = 50,
};

_dailyResults = new List<DigestorDailyOutput>() { dailyOutput1, dailyOutput2, dailyOutput3 };

_mockAdCalculator.Setup(x => x.CalculateResults(It.IsAny<Farm>(), It.IsAny<List<AnimalComponentEmissionsResults>>())).Returns(new List<DigestorDailyOutput>(_dailyResults));

Expand Down Expand Up @@ -154,6 +164,66 @@ public void GetTotalAmountOfDigestateAppliedOnDay()
Assert.AreEqual(50, result);
}

[TestMethod]
public void GetTotalNitrogenRemainingAtEndOfYearReturnsNonZero()
{
_adComponent.IsLiquidSolidSeparated = false;

var year = DateTime.Now.Year;

var totalNitrogen = _sut.GetTotalNitrogenRemainingAtEndOfYear(year, _farm);

Assert.AreEqual(70, totalNitrogen);
}

[TestMethod]
public void CalculateSolidAmountsAvailable()
{
var fieldSystemComponent = new FieldSystemComponent();
var digestateApplication = new DigestateApplicationViewItem();
digestateApplication.DigestateState = DigestateState.SolidPhase;
digestateApplication.AmountAppliedPerHectare = 20;

var cropViewItem = new CropViewItem();
cropViewItem.Area = 50;
cropViewItem.DigestateApplicationViewItems.Add(digestateApplication);
fieldSystemComponent.CropViewItems.Add(cropViewItem);

_farm.Components.Clear();
_farm.Components.Add(fieldSystemComponent);

var digestorOutput = new DigestorDailyOutput();
var outputDate = DateTime.Now;
var outputNumber = 1;
var tanks = new List<DigestateTank>();
tanks.Add(new DigestateTank()
{
TotalSolidDigestateAvailable = 2000,
NitrogenFromSolidDigestate = 900,
CarbonFromSolidDigestate = 777,
});

var tank = new DigestateTank();

var component = new AnaerobicDigestionComponent();

digestorOutput.FlowRateSolidFraction = 10;
digestorOutput.TotalAmountOfNitrogenInRawDigestateAvailableForLandApplicationFromSolidFraction = 2000;
digestorOutput.TotalAmountOfCarbonInRawDigestateAvailableForLandApplicationFromSolidFraction = 8999;

_sut.CalculateSolidAmountsAvailable(
digestorOutput,
outputDate,
_farm,
outputNumber,
tanks,
tank,
component);

Assert.AreEqual(4912.3184079601988, tank.CarbonFromSolidDigestate);
Assert.AreEqual(1457.2139303482586, tank.NitrogenFromSolidDigestate);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public double CalculateDirectN2ONFromLeftOverManureForField(
var weightedEmissionFactor = CalculateWeightedOrganicNitrogenEmissionFactor(itemsByYear, farm);

// The total N after all applications and exports have been subtracted
var totalNitrogenRemaining = this.ManureService.GetTotalNitrogenRemainingForFarmAndYear(viewItem.Year, farm);
var totalNitrogenRemaining = this.ManureService.GetTotalManureNitrogenRemainingForFarmAndYear(viewItem.Year, farm);
var emissionsFromNitrogenRemaining = this.CalculateTotalDirectN2ONFromRemainingManureNitrogen(
weightedEmissionFactor: weightedEmissionFactor,
totalManureNitrogenRemaining: totalNitrogenRemaining);
Expand Down Expand Up @@ -1579,7 +1579,7 @@ public double GetManureNitrogenRemainingForField(CropViewItem viewItem, Farm far

var totalAreaOfFarm = farm.GetTotalAreaOfFarm(includeNativeGrasslands: false, viewItem.Year);
var fractionOfAreaByThisField = viewItem.Area / totalAreaOfFarm;
var manureNitrogenRemaining = this.ManureService.GetTotalNitrogenRemainingForFarmAndYear(viewItem.Year, farm);
var manureNitrogenRemaining = this.ManureService.GetTotalManureNitrogenRemainingForFarmAndYear(viewItem.Year, farm);

var amountOfNitrogenAssignedToThisField = fractionOfAreaByThisField * manureNitrogenRemaining;

Expand Down
162 changes: 125 additions & 37 deletions H.Core/Services/Animals/DigestateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,45 @@ public List<DigestorDailyOutput> GetDailyResults(Farm farm)
return dailyResults;
}

/// <summary>
/// Equation 4.6.1-4
///
/// (kg N)
/// </summary>
public double GetTotalManureNitrogenRemainingForFarmAndYear(int year, Farm farm)
{
//var totalAvailableNitrogen = this.GetTotalNitrogenCreated(year);

//var items = farm.GetCropViewItemsByYear(year, false);
//var localSourcedNitrogenApplied = 0d;
//var importedNitrogenApplied = 0d;
//foreach (var cropViewItem in items)
//{
// foreach (var manureApplicationViewItem in cropViewItem.GetLocalSourcedApplications(year))
// {
// localSourcedNitrogenApplied += manureApplicationViewItem.AmountOfNitrogenAppliedPerHectare * cropViewItem.Area;
// }

// foreach (var manureApplicationViewItem in cropViewItem.GetManureImportsByYear(year))
// {
// importedNitrogenApplied += manureApplicationViewItem.AmountOfNitrogenAppliedPerHectare * cropViewItem.Area;
// }
//}

//var totalAppliedNitrogen = localSourcedNitrogenApplied;//this.GetTotalNitrogenAppliedToAllFields(year);
//var totalExportedNitrogen = this.GetTotalNitrogenFromExportedManure(year, farm);

//// If all manure used was imported and none from local sources were used or created then there is no remaining N since all imports are used
//if (totalAvailableNitrogen == 0 && totalAppliedNitrogen == 0 && importedNitrogenApplied > 0)
//{
// return 0;
//}

//return totalAvailableNitrogen - (totalAppliedNitrogen - importedNitrogenApplied) - totalExportedNitrogen;

return 0;
}

public DateTime GetDateOfMaximumAvailableDigestate(Farm farm, DigestateState state, int year, List<DigestorDailyOutput> digestorDailyOutputs)
{
var tankStates = this.GetDailyTankStates(farm, digestorDailyOutputs);
Expand Down Expand Up @@ -355,6 +394,39 @@ public double GetTotalCarbonRemainingAtEndOfYear(int year, Farm farm, AnaerobicD
return totalCarbon;
}

public double GetTotalAmountOfDigestateAppliedOnDay(DateTime dateTime, Farm farm, DigestateState state)
{
var result = 0d;

foreach (var farmFieldSystemComponent in farm.FieldSystemComponents)
{
foreach (var cropViewItem in farmFieldSystemComponent.CropViewItems)
{
foreach (var digestateApplicationViewItem in cropViewItem.DigestateApplicationViewItems)
{
if (digestateApplicationViewItem.DateCreated.Date == dateTime.Date && digestateApplicationViewItem.DigestateState == state)
{
result += digestateApplicationViewItem.AmountAppliedPerHectare * cropViewItem.Area;
}
}
}
}

return result;
}

public double GetTotalCarbonAppliedToField(CropViewItem cropViewItem, int year)
{
var result = 0d;

foreach (var digestateApplicationViewItem in cropViewItem.DigestateApplicationViewItems.Where(x => x.DateCreated.Year == year))
{
result += digestateApplicationViewItem.AmountOfCarbonAppliedPerHectare * cropViewItem.Area;
}

return result;
}

public double GetTotalNitrogenRemainingAtEndOfYear(int year, Farm farm)
{
var dailyResults = this.GetDailyResults(farm);
Expand Down Expand Up @@ -387,40 +459,6 @@ public double GetTotalNitrogenRemainingAtEndOfYear(int year, Farm farm)
{
return 0;
}

}

public double GetTotalAmountOfDigestateAppliedOnDay(DateTime dateTime, Farm farm, DigestateState state)
{
var result = 0d;

foreach (var farmFieldSystemComponent in farm.FieldSystemComponents)
{
foreach (var cropViewItem in farmFieldSystemComponent.CropViewItems)
{
foreach (var digestateApplicationViewItem in cropViewItem.DigestateApplicationViewItems)
{
if (digestateApplicationViewItem.DateCreated.Date == dateTime.Date && digestateApplicationViewItem.DigestateState == state)
{
result += digestateApplicationViewItem.AmountAppliedPerHectare * cropViewItem.Area;
}
}
}
}

return result;
}

public double GetTotalCarbonAppliedToField(CropViewItem cropViewItem, int year)
{
var result = 0d;

foreach (var digestateApplicationViewItem in cropViewItem.DigestateApplicationViewItems.Where(x => x.DateCreated.Year == year))
{
result += digestateApplicationViewItem.AmountOfCarbonAppliedPerHectare * cropViewItem.Area;
}

return result;
}

public double GetTotalCarbonRemainingForField(CropViewItem cropViewItem, int year, Farm farm, AnaerobicDigestionComponent component)
Expand Down Expand Up @@ -450,7 +488,11 @@ public double GetTotalCarbonRemainingForField(CropViewItem cropViewItem, int yea
return result;
}

public double GetTotalCarbonForField(CropViewItem cropViewItem, int year, Farm farm, AnaerobicDigestionComponent component)
public double GetTotalCarbonForField(
CropViewItem cropViewItem,
int year,
Farm farm,
AnaerobicDigestionComponent component)
{
var totalAppliedToField = this.GetTotalCarbonAppliedToField(cropViewItem, year);
var totalCarbonRemainingForField = this.GetTotalCarbonRemainingForField(cropViewItem, year, farm, component);
Expand All @@ -460,11 +502,57 @@ public double GetTotalCarbonForField(CropViewItem cropViewItem, int year, Farm f
return result;
}

#endregion
public void CalculateSolidAmountsAvailable(
DigestorDailyOutput outputOnCurrentDay,
DateTime outputDate,
Farm farm,
int outputNumber,
List<DigestateTank> result,
DigestateTank tank,
AnaerobicDigestionComponent component)
{
/*
* Calculate solid amounts available
*/

var totalSolidFractionOnThisDay = outputOnCurrentDay.FlowRateSolidFraction;
var totalSolidDigestateUsedForFieldApplications = this.GetTotalAmountOfDigestateAppliedOnDay(outputDate, farm, DigestateState.SolidPhase);
var totalSolidDigestateFromPreviousDay = outputNumber == 0 ? 0 : result.ElementAt(outputNumber - 1).TotalSolidDigestateAvailable;
var totalSolidProduced = totalSolidFractionOnThisDay + totalSolidDigestateFromPreviousDay;
var totalSolidDigestateAvailableAfterFieldApplications = totalSolidProduced - totalSolidDigestateUsedForFieldApplications;

//// Nitrogen from solid digestate
var totalNitrogenFromSolidDigestateOnThisDay = outputOnCurrentDay.TotalAmountOfNitrogenInRawDigestateAvailableForLandApplicationFromSolidFraction;
var totalNitrogenFromSolidDigestateFromPreviousDay = outputNumber == 0 ? 0 : result.ElementAt(outputNumber - 1).NitrogenFromSolidDigestate;
var totalNitrogenFromSolidDigestateAvailable = totalNitrogenFromSolidDigestateOnThisDay + totalNitrogenFromSolidDigestateFromPreviousDay;

//// Carbon from solid digestate
var totalCarbonFromSolidDigestateOnThisDay = outputOnCurrentDay.TotalAmountOfCarbonInRawDigestateAvailableForLandApplicationFromSolidFraction;
var totalCarbonFromSolidDigestateFromPreviousDay = outputNumber == 0 ? 0 : result.ElementAt(outputNumber - 1).CarbonFromSolidDigestate;
var totalCarbonFromSolidDigestateAvailable = totalCarbonFromSolidDigestateOnThisDay + totalCarbonFromSolidDigestateFromPreviousDay;

#region Private Methods
if (component.IsLiquidSolidSeparated)
{
tank.TotalSolidDigestateAvailable = totalSolidDigestateAvailableAfterFieldApplications;
tank.TotalSolidDigestateProduced = totalSolidProduced;
tank.NitrogenFromSolidDigestate = totalNitrogenFromSolidDigestateAvailable;
tank.CarbonFromSolidDigestate = totalCarbonFromSolidDigestateAvailable;

var totalFractionOfSolidDigestateUsedFromLandApplications = totalSolidDigestateUsedForFieldApplications / totalSolidProduced;
var totalCarbonUsed = totalFractionOfSolidDigestateUsedFromLandApplications * totalCarbonFromSolidDigestateAvailable;
var totalNitrogenUsed = totalFractionOfSolidDigestateUsedFromLandApplications * totalNitrogenFromSolidDigestateAvailable;

if (this.SubtractAmountsFromLandApplications)
{
tank.CarbonFromSolidDigestate -= totalCarbonUsed;
tank.NitrogenFromSolidDigestate -= totalNitrogenUsed;
}
}
}

#endregion

#region Private Methods

#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion H.Core/Services/Animals/IManureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public interface IManureService
///
/// (kg N)
/// </summary>
double GetTotalNitrogenRemainingForFarmAndYear(int year, Farm farm);
double GetTotalManureNitrogenRemainingForFarmAndYear(int year, Farm farm);



Expand Down
2 changes: 1 addition & 1 deletion H.Core/Services/Animals/ManureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ public List<Tuple<double, AnimalType>> GetTotalTanAppliedToAllFields(int year, L
///
/// (kg N)
/// </summary>
public double GetTotalNitrogenRemainingForFarmAndYear(int year, Farm farm)
public double GetTotalManureNitrogenRemainingForFarmAndYear(int year, Farm farm)
{
var totalAvailableNitrogen = this.GetTotalNitrogenCreated(year);

Expand Down
1 change: 1 addition & 0 deletions H.Infrastructure/KmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class KmlHelpers
"Alberta_No_Styles.kml",
"BC_No_Styles.kml"
};

public bool IsPointInPolygon(Polygon polygon, Vector point)
{
bool inside = false;
Expand Down

0 comments on commit 3956bad

Please sign in to comment.