// Copyright (C) Stichting Deltares 2019. 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStabilityCommon;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStabilityCommon.Assemblers;
using Deltares.DamEngine.Data.General;
using Deltares.DamEngine.Data.Standard.Logging;
using Deltares.DamEngine.TestHelpers.Factories;
using NUnit.Framework;
namespace Deltares.DamEngine.Calculators.Tests.KernelWrappers.DamMacroStabilityCommon
{
[TestFixture]
public class MStabXmlDocTests
{
private const string TestFolder = @"..\..\Deltares.DamEngine.Calculators.Tests\KernelWrappers\DamMacroStabilityCommon\TestData";
[Test]
public void TestCreateMStabXmlDoc()
{
List errorMessages;
const string stabilityProjectFilename = "testproject";
var xmlFileName = Path.Combine(TestFolder, "testproject.xml");
// Expected1D1.xml was generated with Dam Classic revision 745 with test CanCalculateStabilitySafetyFactorGeometry2D
// In the file that is generated here sometimes value -0 is written instead of 0.
// To compare the two files in an easy way, Expected1D1.xml was manually modified
var expectedxmlFileName = Path.Combine(TestFolder, "Expected1D1.xml");
var soilGeometry2DName = Path.Combine(TestFolder, "1D1.sti");
var soilDbName = Path.Combine(TestFolder, "soilmaterials.mdb");
var requiredSafetyFactor = 1.2;
if (File.Exists(xmlFileName))
{
File.Delete(xmlFileName);
}
var line = FactoryForSurfaceLines.CreateSurfaceLineTutorial1();
var location = DamMacroStabilityTestHelper.CreateLocation(line);
var scenario = DamMacroStabilityTestHelper.CreateScenario(location);
scenario.Location.StabilityOptions = new StabilityOptions();
scenario.Location.StabilityOptions.TrafficLoad = 10.0;
scenario.Location.StabilityOptions.MinimalCircleDepth = 1.0;
scenario.Location.StabilityOptions.SoilDatabaseName = soilDbName;
var subSoilScenario = new SoilGeometryProbability();
subSoilScenario.FullStiFileName = soilGeometry2DName;
subSoilScenario.SoilProfileType = SoilProfileType.ProfileTypeStiFile;
subSoilScenario.SegmentFailureMechanismType = FailureMechanismSystemType.StabilityInside;
var failureMechanismParametersMStab = new FailureMechanismParametersMStab();
failureMechanismParametersMStab.MStabParameters.GridPosition = MStabGridPosition.Right;
failureMechanismParametersMStab.MStabParameters.SearchMethod = MStabSearchMethod.GeneticAlgorithm;
double riverLevelLow = 0.0;
XDocument mstabXml = MStabXmlDoc.CreateMStabXmlDoc(
stabilityProjectFilename,
location,
location.RiverLevel,
riverLevelLow,
subSoilScenario,
null, requiredSafetyFactor,
failureMechanismParametersMStab,
new RegionalAssessmentScenarioJobSettings(),
DateTime.Now,
out errorMessages);
mstabXml.Save(xmlFileName);
Assert.IsTrue(File.Exists(xmlFileName));
CompareByLine(ModifiedXmlDocument(expectedxmlFileName), ModifiedXmlDocument(xmlFileName));
}
private void CompareByLine(XDocument expectedXmlDocument, XDocument xmlDocument)
{
string expectedText = expectedXmlDocument.ToString();
string testedText = xmlDocument.ToString();
Assert.AreEqual(expectedText, testedText);
}
private XDocument ModifiedXmlDocument(string xmlFileName)
{
XDocument xDocument = XDocument.Load(xmlFileName);
// Modify FileIdentification
const string fileIdentificationElementName = "FileIdentification";
XElement fileIdentificationElement = (from element in xDocument.Root.Descendants()
where element.Name.LocalName == fileIdentificationElementName
select element).Single();
// clear application that created the xml
XAttribute application = fileIdentificationElement.Attribute("Application");
Debug.Assert(application != null, "application != null");
application.Value = "";
// clear version of application that created the xml
XAttribute version = fileIdentificationElement.Attribute("Version");
Debug.Assert(version != null, "version != null");
version.Value = "";
// clear creation date and time
XAttribute created = fileIdentificationElement.Attribute("Created");
Debug.Assert(created != null, "created != null");
created.Value = "";
// Modify DamMStabInput
XElement inputElement = (from element in xDocument.Root.Descendants()
where element.Name.LocalName == DamMStabAssembler.XmlElementNameInput
select element).Single();
// clear name of output sti file
XAttribute mstabFileName = inputElement.Attribute(DamMStabAssembler.XmlAttributeMStabFileName);
Debug.Assert(mstabFileName != null, "mstabFileName != null");
mstabFileName.Value = "";
// clear name of soil database
XAttribute dbName = inputElement.Attribute(DamMStabAssembler.XmlAttributeSoilDBName);
Debug.Assert(dbName != null, "dbName != null");
dbName.Value = "";
// clear 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 = "";
return xDocument;
}
}
}