Skip to content

Commit

Permalink
AD Liquid tests
Browse files Browse the repository at this point in the history
  • Loading branch information
holos-aafc committed Apr 16, 2024
1 parent 46cee5a commit 0c45eb7
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 37 deletions.
54 changes: 54 additions & 0 deletions H.Core.Test/Services/Animals/DigestateServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,60 @@ public void CalculateSolidAmountsAvailable()
Assert.AreEqual(1457.2139303482586, tank.NitrogenFromSolidDigestate);
}

[TestMethod]
public void CalculateLiquidAmountsAvailable()
{
var fieldSystemComponent = new FieldSystemComponent();
var digestateApplication = new DigestateApplicationViewItem();
digestateApplication.DigestateState = DigestateState.LiquidPhase;
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 = 0,
NitrogenFromSolidDigestate = 0,
CarbonFromSolidDigestate = 0,

TotalLiquidDigestateAvailable = 6000,
NitrogenFromLiquidDigestate = 1800,
CarbonFromLiquidDigestate = 555,
});

var tank = new DigestateTank();

var component = new AnaerobicDigestionComponent();

digestorOutput.FlowRateLiquidFraction = 30;
digestorOutput.TotalAmountOfNitrogenInRawDigestateAvailableForLandApplicationFromSolidFraction = 0;
digestorOutput.TotalAmountOfCarbonInRawDigestateAvailableForLandApplicationFromSolidFraction = 0;
digestorOutput.TotalAmountOfNitrogenInRawDigestateAvailableForLandApplicationFromLiquidFraction = 220;
digestorOutput.TotalAmountOfCarbonInRawDigestateAvailableForLandApplicationFromLiquidFraction = 330;

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

Assert.AreEqual(738.2338, tank.CarbonFromLiquidDigestate, 0.0001);
Assert.AreEqual(1685.0082, tank.NitrogenFromLiquidDigestate, 0.0001);
}

#endregion
}
}
89 changes: 54 additions & 35 deletions H.Core/Services/Animals/DigestateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,39 +210,14 @@ public List<DigestateTank> GetDailyTankStates(List<DigestorDailyOutput> digestor
* Calculate liquid amounts available
*/

var totalLiquidFractionOnThisDay = outputOnCurrentDay.FlowRateLiquidFraction;
var totalLiquidDigestateUsedForFieldApplications = this.GetTotalAmountOfDigestateAppliedOnDay(outputDate, farm, DigestateState.LiquidPhase);
var totalLiquidDigestateFromPreviousDay = i == 0 ? 0 : result.ElementAt(i - 1).TotalLiquidDigestateAvailable;
var totalLiquidProduced = totalLiquidFractionOnThisDay + totalLiquidDigestateFromPreviousDay;
var totalLiquidDigestateAvailableAfterFieldApplications = totalLiquidProduced - totalLiquidDigestateUsedForFieldApplications;

// Nitrogen from liquid digestate
var totalNitrogenFromLiquidDigestateOnThisDay = outputOnCurrentDay.TotalAmountOfNitrogenInRawDigestateAvailableForLandApplicationFromLiquidFraction;
var totalNitrogenFromLiquidDigestateFromPreviousDay = i == 0 ? 0 : result.ElementAt(i - 1).NitrogenFromLiquidDigestate;
var totalNitrogenFromLiquidDigestateAvailable = totalNitrogenFromLiquidDigestateOnThisDay + totalNitrogenFromLiquidDigestateFromPreviousDay;

// Carbon from liquid digestate
var totalCarbonFromLiquidDigestateOnThisDay = outputOnCurrentDay.TotalAmountOfCarbonInRawDigestateAvailableForLandApplicationFromLiquidFraction;
var totalCarbonFromLiquidDigestateFromPreviousDay = i == 0 ? 0 : result.ElementAt(i - 1).CarbonFromLiquidDigestate;
var totalCarbonFromLiquidDigestateAvailable = totalCarbonFromLiquidDigestateOnThisDay + totalCarbonFromLiquidDigestateFromPreviousDay;

if (component.IsLiquidSolidSeparated)
{
tank.TotalLiquidDigestateAvailable = totalLiquidDigestateAvailableAfterFieldApplications;
tank.TotalLiquidDigestateProduced = totalLiquidProduced;
tank.NitrogenFromLiquidDigestate = totalNitrogenFromLiquidDigestateAvailable;
tank.CarbonFromLiquidDigestate = totalCarbonFromLiquidDigestateAvailable;

var totalFractionOfLiquidDigestateUsedFromLandApplications = totalLiquidDigestateUsedForFieldApplications / totalLiquidProduced;
var totalCarbonUsed = totalFractionOfLiquidDigestateUsedFromLandApplications * totalCarbonFromLiquidDigestateAvailable;
var totalNitrogenUsed = totalFractionOfLiquidDigestateUsedFromLandApplications * totalNitrogenFromLiquidDigestateAvailable;

if (this.SubtractAmountsFromLandApplications)
{
tank.CarbonFromLiquidDigestate -= totalCarbonUsed;
tank.NitrogenFromLiquidDigestate -= totalNitrogenUsed;
}
}
this.CalculateLiquidAmountsAvailable(
outputOnCurrentDay: outputOnCurrentDay,
outputDate: outputDate,
farm: farm,
outputNumber: i,
result: result,
tank: tank,
component: component);

/*
* Calculate solid amounts available
Expand Down Expand Up @@ -477,6 +452,50 @@ public double GetTotalCarbonForField(
return result;
}

public void CalculateLiquidAmountsAvailable(
DigestorDailyOutput outputOnCurrentDay,
DateTime outputDate,
Farm farm,
int outputNumber,
List<DigestateTank> result,
DigestateTank tank,
AnaerobicDigestionComponent component)
{
var totalLiquidFractionOnThisDay = outputOnCurrentDay.FlowRateLiquidFraction;
var totalLiquidDigestateUsedForFieldApplications = this.GetTotalAmountOfDigestateAppliedOnDay(outputDate, farm, DigestateState.LiquidPhase);
var totalLiquidDigestateFromPreviousDay = outputNumber == 0 ? 0 : result.ElementAt(outputNumber - 1).TotalLiquidDigestateAvailable;
var totalLiquidProduced = totalLiquidFractionOnThisDay + totalLiquidDigestateFromPreviousDay;
var totalLiquidDigestateAvailableAfterFieldApplications = totalLiquidProduced - totalLiquidDigestateUsedForFieldApplications;

// Nitrogen from liquid digestate
var totalNitrogenFromLiquidDigestateOnThisDay = outputOnCurrentDay.TotalAmountOfNitrogenInRawDigestateAvailableForLandApplicationFromLiquidFraction;
var totalNitrogenFromLiquidDigestateFromPreviousDay = outputNumber == 0 ? 0 : result.ElementAt(outputNumber - 1).NitrogenFromLiquidDigestate;
var totalNitrogenFromLiquidDigestateAvailable = totalNitrogenFromLiquidDigestateOnThisDay + totalNitrogenFromLiquidDigestateFromPreviousDay;

// Carbon from liquid digestate
var totalCarbonFromLiquidDigestateOnThisDay = outputOnCurrentDay.TotalAmountOfCarbonInRawDigestateAvailableForLandApplicationFromLiquidFraction;
var totalCarbonFromLiquidDigestateFromPreviousDay = outputNumber == 0 ? 0 : result.ElementAt(outputNumber - 1).CarbonFromLiquidDigestate;
var totalCarbonFromLiquidDigestateAvailable = totalCarbonFromLiquidDigestateOnThisDay + totalCarbonFromLiquidDigestateFromPreviousDay;

if (component.IsLiquidSolidSeparated)
{
tank.TotalLiquidDigestateAvailable = totalLiquidDigestateAvailableAfterFieldApplications;
tank.TotalLiquidDigestateProduced = totalLiquidProduced;
tank.NitrogenFromLiquidDigestate = totalNitrogenFromLiquidDigestateAvailable;
tank.CarbonFromLiquidDigestate = totalCarbonFromLiquidDigestateAvailable;

var totalFractionOfLiquidDigestateUsedFromLandApplications = totalLiquidDigestateUsedForFieldApplications / totalLiquidProduced;
var totalCarbonUsed = totalFractionOfLiquidDigestateUsedFromLandApplications * totalCarbonFromLiquidDigestateAvailable;
var totalNitrogenUsed = totalFractionOfLiquidDigestateUsedFromLandApplications * totalNitrogenFromLiquidDigestateAvailable;

if (this.SubtractAmountsFromLandApplications)
{
tank.CarbonFromLiquidDigestate -= totalCarbonUsed;
tank.NitrogenFromLiquidDigestate -= totalNitrogenUsed;
}
}
}

public void CalculateSolidAmountsAvailable(
DigestorDailyOutput outputOnCurrentDay,
DateTime outputDate,
Expand All @@ -487,8 +506,8 @@ public void CalculateSolidAmountsAvailable(
AnaerobicDigestionComponent component)
{
/*
* Calculate solid amounts available
*/
* Calculate solid amounts available
*/

var totalSolidFractionOnThisDay = outputOnCurrentDay.FlowRateSolidFraction;
var totalSolidDigestateUsedForFieldApplications = this.GetTotalAmountOfDigestateAppliedOnDay(outputDate, farm, DigestateState.SolidPhase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,6 @@ public void CreateDetailViewItems(

this.ProcessDigestateViewItems(farm, fieldSystemComponent);



// Before creating view items for each year, calculate carbon lost from bale exports
this.CalculateCarbonLostFromHayExports(fieldSystemComponent, farm);

Expand Down

0 comments on commit 0c45eb7

Please sign in to comment.