Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="AutoMapper" Version="14.0.0" />
<PackageReference Include="APSIM.Graphs" Version="8.1.0" />
<PackageReference Include="APSIM.Soils" Version="8.0.0" />
<PackageReference Include="APSIM.Numerics" Version="8.2.0" />
<PackageReference Include="APSIM.Numerics" Version="8.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions API/Services/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static Graph ToGraph(this Models.Soil soil, double[] thickness = null,
sw = sw.ConvertGravimetricToVolumetric(bdMapped).ToArray();
}
// Map water to bottom of profile.
sw = Soil.SWMappedTo(sw, thickness, soil.Water.Thickness, ll);
sw = Soil.SWMappedTo(sw, thickness, soil.Water.Thickness, soil.Water.LL15, ll, soil.Water.DUL);

// Calculate plant available water.
paw = SoilUtilities.CalcPAWC(soil.Water.Thickness, ll, sw, xf)
Expand All @@ -213,8 +213,10 @@ public static Graph ToGraph(this Models.Soil soil, double[] thickness = null,
double pawc = SoilUtilities.CalcPAWC(soil.Water.Thickness, ll, soil.Water.DUL, Enumerable.Repeat(1.0, ll.Count).ToArray())
.Multiply(soil.Water.Thickness).Sum();

// To stop the sw line crossing over ll, constrain it to ll.
var swConstainedToLL = sw.LowerConstraint(ll);
return SoilGraph.Create(soil.Name, soil.Water.Thickness.ToMidPoints(), soil.Water.AirDry, soil.Water.LL15,
soil.Water.DUL, soil.Water.SAT, ll, cropName, pawc, paw, thickness?.ToMidPoints(), sw);
soil.Water.DUL, soil.Water.SAT, ll, cropName, pawc, paw, thickness?.ToMidPoints(), swConstainedToLL);
}

/// <summary>Get the crop lower limit (volumetric) for a soil.</summary>
Expand Down
22 changes: 9 additions & 13 deletions API/Services/SoilServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static double PAW(SoilDbContext context, string fullName, string cropName
sw = sw.ConvertGravimetricToVolumetric(bdMapped);
}

IReadOnlyList<double> swMapped = sw.SWMappedTo(thickness, soil.Water.Thickness, ll);
IReadOnlyList<double> swMapped = sw.SWMappedTo(thickness, soil.Water.Thickness, soil.Water.LL15, ll, soil.Water.DUL);
var pawByLayer = SoilUtilities.CalcPAWC(soil.Water.Thickness, ll, swMapped, xf);
return MathUtilities.Multiply(pawByLayer, soil.Water.Thickness).Sum();
}
Expand All @@ -198,22 +198,18 @@ public static double PAW(SoilDbContext context, string fullName, string cropName
/// <param name="values">The values to map.</param>
/// <param name="thickness">The thickness to map to.</param>
/// <returns>The mapped values.</returns>
public static double[] SWMappedTo(this IReadOnlyList<double> values, IReadOnlyList<double> fromThickness, IReadOnlyList<double> toThickness, IReadOnlyList<double> ll)
public static double[] SWMappedTo(this IReadOnlyList<double> values, IReadOnlyList<double> fromThickness, IReadOnlyList<double> toThickness, IReadOnlyList<double> ll15, IReadOnlyList<double> ll, IReadOnlyList<double> dul)
{
List<double> sw = values.ToList();
List<double> thickness = fromThickness.ToList();
sw.Add(0.8 * values.Last()); // 1st pseudo layer below profile.
sw.Add(0.4 * values.Last()); // 2nd pseudo layer below profile.
sw.Add(0.0); // 3rd pseudo layer below profile.
thickness.Add(thickness.Last()); // 1st pseudo layer below profile.
thickness.Add(thickness.Last()); // 2nd pseudo layer below profile.
thickness.Add(3000); // 3rd pseudo layer below profile.

var llMapped = ll.MappedTo(toThickness, thickness);
var swMM = sw.LowerConstraint(llMapped, startIndex: values.Count)
.Multiply(thickness);
return SoilUtilities.MapMass(swMM, thickness.ToArray(), toThickness.ToArray())
sw.Add(ll15.Last()); // add a pseudo layer below profile.
thickness.Add(3000);
var swMM = sw.Multiply(thickness);
var swVolumetric = SoilUtilities.MapMass(swMM, thickness.ToArray(), toThickness.ToArray())
.Divide(toThickness); // convert back to volumetric
return swVolumetric.LowerConstraint(ll15, startIndex: 0)
.UpperConstraint(dul, startIndex: 0)
.ToArray();
}

}
4 changes: 2 additions & 2 deletions Tests/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void PAW_ShouldReturnCorrectValue()
var paw = API.Services.Soil.PAW(context, "Clay (Kerikeri No1353)", cropName: "wheat",
thickness: [ 150, 500 ],
sw: [ 0.4, 0.3 ], swIsGrav: false);
Assert.That(paw, Is.EqualTo(19.5996).Within(0.000001));
Assert.That(paw, Is.EqualTo(21.72).Within(0.000001));
}


Expand All @@ -233,7 +233,7 @@ public void PAWFromGravimetric_ShouldReturnCorrectValue()
var paw = API.Services.Soil.PAW(context, "Clay (Kerikeri No1353)", cropName: "wheat",
thickness: [ 150, 500 ],
sw: [ 0.4, 0.3 ], swIsGrav: true);
Assert.That(paw, Is.EqualTo(22.165).Within(0.000001));
Assert.That(paw, Is.EqualTo(24.676).Within(0.000001));
}

[Test]
Expand Down