// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Deltares.DamEngine.Calculators.KernelWrappers.Common;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStabilityCommon;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStabilityCommon.Assemblers;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStabilityInwards;
using Deltares.DamEngine.Calculators.KernelWrappers.Interfaces;
using Deltares.DamEngine.Calculators.Tests.KernelWrappers.DamMacroStabilityCommon;
using Deltares.DamEngine.Data.General;
using Deltares.DamEngine.Data.General.Results;
using Deltares.DamEngine.Data.Standard.Logging;
using Deltares.DamEngine.TestHelpers;
using NUnit.Framework;
namespace Deltares.DamEngine.Calculators.Tests.KernelWrappers.DamMacroStabilityInwards
{
[TestFixture]
public class DamMacroStabilityTests
{
private const string TestFolder = @"..\..\Deltares.DamEngine.Calculators.Tests\KernelWrappers\DamMacroStabilityCommon\TestData";
[Test]
public void TestFullCalculation()
{
// expected results are based on test CanCalculateStabilitySafetyFactorGeometry2D
// in 'https://repos.deltares.nl/repos/dam/dam classic' revision 190
const double diff = 0.01;
var soilDbName = Path.Combine(TestFolder, "soilmaterials.mdb");
var soilGeometry2DName = "1D1.sti";
var exeName = @".\KernelWrappers\DamMacroStabilityCommon\DGeoStability.exe";
var absoluteFolder = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), TestFolder));
var workingDir = Path.Combine(absoluteFolder, "FullOut");
// Relative paths in ini file do not work yet in DGeoStability 16.2. This is fixed in 18.1.
if (Directory.Exists(workingDir))
{
Directory.Delete(workingDir, true);
}
var line = FactoryForSurfaceLines.CreateSurfaceLineTutorial1();
var location = new Location();
var scenario = DamMacroStabilityTestHelper.CreateScenarioForLocation(location, line);
scenario.Location.StabilityOptions = new StabilityOptions();
scenario.Location.StabilityOptions.TrafficLoad = 10.0;
scenario.Location.StabilityOptions.MapForSoilGeometries2D = TestFolder;
scenario.Location.StabilityOptions.SoilDatabase = soilDbName;
scenario.ModelFactors.RequiredSafetyFactorStabilityInnerSlope = 1.1;
var subSoilScenario = new SoilGeometryProbability();
subSoilScenario.StiFileName = soilGeometry2DName;
subSoilScenario.SoilProfileType = SoilProfileType.ProfileTypeStiFile;
subSoilScenario.SegmentFailureMechanismType = FailureMechanismSystemType.StabilityInside;
var failureMechanismeParamatersMStab = new FailureMechanismParametersMStab();
failureMechanismeParamatersMStab.MStabParameters.GridPosition = MStabGridPosition.Right;
failureMechanismeParamatersMStab.MStabParameters.SearchMethod = MStabSearchMethod.GeneticAlgorithm;
failureMechanismeParamatersMStab.MStabParameters.CalculationOptions.MinimalCircleDepth = 1.0;
failureMechanismeParamatersMStab.MStabParameters.Model = MStabModelType.Bishop;
failureMechanismeParamatersMStab.DGeoStabilityExePath = exeName;
var damKernelInput = new DamKernelInput();
damKernelInput.Location = location;
damKernelInput.DesignScenario = scenario;
damKernelInput.SubSoilScenario = subSoilScenario;
damKernelInput.WorkingDir = workingDir;
var kernelWrapper = new DamMacroStabilityInwardsKernelWrapper();
kernelWrapper.FailureMechanismParametersMStab = failureMechanismeParamatersMStab;
// Prepare the wrapper. Result is input for the calculation dll
IKernelDataInput damStabilityInput;
IKernelDataOutput kernelOutput;
kernelWrapper.Prepare(damKernelInput, 0, out damStabilityInput, out kernelOutput);
// Validate the input
List messages;
kernelWrapper.Validate(damStabilityInput, kernelOutput, out messages);
Assert.AreEqual(0, messages.Count);
// Run the dll
kernelWrapper.Execute(damStabilityInput, kernelOutput, out messages);
DamMacroStabilityOutput damMacroStabilityOutput = (DamMacroStabilityOutput) kernelOutput;
Assert.AreEqual(0, messages.Count);
Assert.AreEqual(1.5570, damMacroStabilityOutput.StabilityOutputItems[0].Zone1Results.SafetyFactor, diff);
Assert.IsNull(damMacroStabilityOutput.StabilityOutputItems[0].Zone2Results);
// Fill the design results
List results;
kernelWrapper.PostProcess(damKernelInput, damMacroStabilityOutput, "", out results);
Assert.AreEqual(1.5570, results[0].StabilityDesignResults.SafetyFactor, diff);
}
public XDocument ModifiedXmlDocument(string stiFileName)
{
var xmlFileName = Path.Combine(TestFolder, "test.xml");
var geometryFileName = Path.Combine(TestFolder, "DWP_1.sti");
var soilDbName = Path.Combine(TestFolder, "DAM Tutorial Design0.soilmaterials.mdb");
// modify name of output sti file
XDocument xDocument = XDocument.Load(xmlFileName);
XElement inputElement = (from element in xDocument.Root.Descendants()
where element.Name.LocalName == DamMStabAssembler.XmlElementNameInput
select element).Single();
XAttribute mstabFileName = inputElement.Attribute(DamMStabAssembler.XmlAttributeMStabFileName);
Debug.Assert(mstabFileName != null, "mstabFileName != null");
mstabFileName.Value = stiFileName;
// modify name of Soil DB Name
XAttribute dbName = inputElement.Attribute(DamMStabAssembler.XmlAttributeSoilDBName);
Debug.Assert(dbName != null, "dbName != null");
dbName.Value = soilDbName;
// modify name of geometry input file
XElement geometryOptionsElement = (from element in inputElement.Descendants()
where element.Name.LocalName == DamMStabAssembler.XmlElementGeometryCreationOptions
select element).Single();
XAttribute geomFileName = geometryOptionsElement.Attribute(DamMStabAssembler.XmlAttributeSoilGeometry2DFilename);
Debug.Assert(geomFileName != null, "geomFileName != null");
geomFileName.Value = geometryFileName;
return xDocument;
}
[Test]
public void TestCreateDGeoStabilityInputFile()
{
var stiFileName = Path.Combine(TestFolder, "test.sti");
var expectedStiFileName = Path.Combine(TestFolder, "expectedTest.sti");
if (File.Exists(stiFileName))
{
File.Delete(stiFileName);
}
var xDocument = ModifiedXmlDocument(stiFileName);
var kernelWrapper = new DamMacroStabilityInwardsKernelWrapper();
kernelWrapper.CreateStiFile(xDocument);
Assert.IsTrue(File.Exists(stiFileName));
Assert.AreEqual(ContentOfStiFile(expectedStiFileName), ContentOfStiFile(stiFileName));
}
[Test]
[ExpectedException(typeof(MacroStabilityException))]
public void TestThrowsExceptionXmlFileNotValid()
{
var kernelWrapper = new DamMacroStabilityInwardsKernelWrapper();
XDocument xDocument = new XDocument();
kernelWrapper.CreateStiFile(xDocument);
}
private string ContentOfStiFile(string stiFileName)
{
try
{
StreamReader stream = File.OpenText(stiFileName);
for (int i = 1; i <= 7; i++)
{
stream.ReadLine(); // skip first 7 lines with date, time and filename
}
string text = stream.ReadToEnd();
stream.Close();
return text;
}
catch
{
return null;
}
}
[Test]
public void TestValidate()
{
var kernelWrapper = new DamMacroStabilityInwardsKernelWrapper();
// Validate without setting values. Expected error messages.
var damStabilityInput = new DamMacroStabilityInput();
List messages;
// Validate is not implemented (yet), returns always true
// kernelWrapper.Validate(damStabilityInput, out messages);
// Assert.IsTrue(messages.Count > 0);
// Validate the input when valid input is provided. Expected no messages.
damStabilityInput = new DamMacroStabilityInput
{
// ToDo zant Fill input
};
// messages.Clear();
kernelWrapper.Validate(damStabilityInput, null, out messages);
Assert.AreEqual(0, messages.Count);
}
[Test]
public void TestPostProcess()
{
var kernelWrapper = new DamMacroStabilityInwardsKernelWrapper();
DamMacroStabilityOutputItem outputItem = new DamMacroStabilityOutputItem();
var zone1 = new DamMacroStabilityOutputItem.ResultsSingleZone();
zone1.SafetyFactor = 1.1;
zone1.EntryPointXCoordinate = 1.2;
zone1.ExitPointXCoordinate = 1.3;
outputItem.Zone1Results = zone1;
var zone2 = new DamMacroStabilityOutputItem.ResultsSingleZone();
zone2.SafetyFactor = 0.9;
zone2.EntryPointXCoordinate = 2.2;
zone2.ExitPointXCoordinate = 2.3;
outputItem.Zone2Results = zone2;
DamMacroStabilityOutput output = new DamMacroStabilityOutput();
output.StabilityOutputItems = new List();
output.StabilityOutputItems.Add(outputItem);
List results;
kernelWrapper.PostProcess(null, output, "", out results);
Assert.AreEqual(0.9, results[0].StabilityDesignResults.SafetyFactor);
Assert.AreEqual(1.1, results[0].StabilityDesignResults.Zone1SafetyFactor);
Assert.AreEqual(1.2, results[0].StabilityDesignResults.LocalZone1EntryPointX);
Assert.AreEqual(1.3, results[0].StabilityDesignResults.LocalZone1ExitPointX);
Assert.AreEqual(0.9, results[0].StabilityDesignResults.Zone2SafetyFactor);
Assert.AreEqual(2.2, results[0].StabilityDesignResults.LocalZone2EntryPointX);
Assert.AreEqual(2.3, results[0].StabilityDesignResults.LocalZone2ExitPointX);
}
[Test]
[ExpectedException(typeof(NoNullAllowedException), ExpectedMessage = "Geen invoer object gedefinieerd voor Macrostabiliteit")]
[SetUICulture("nl-NL")]
public void TestLanguageNLThrowsExceptionWhenInputIsNull()
{
DamMacroStabilityInwardsKernelWrapper.StabilityCalculator(null);
}
[Test]
[ExpectedException(typeof(NoNullAllowedException), ExpectedMessage = "No input object defined for Macro Stability")]
[SetUICulture("en-US")]
public void TestLanguageENThrowsExceptionWhenInputIsNull()
{
DamMacroStabilityInwardsKernelWrapper.StabilityCalculator(null);
}
[Test]
[ExpectedException(typeof(NoNullAllowedException), ExpectedMessage = "Geen uitvoer object gedefinieerd voor Macrostabiliteit")]
[SetUICulture("nl-NL")]
public void TestThrowsExceptionWhenOutputIsNull()
{
var kernelWrapper = new DamMacroStabilityInwardsKernelWrapper();
List results;
kernelWrapper.PostProcess(null, null, "", out results);
}
}
}