// Copyright (C) Stichting Deltares 2019. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU 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.IO;
using System.Linq;
using Deltares.Dam.Data;
using Deltares.Dam.Data.CsvImporters;
using Deltares.Dam.Data.Importers;
using NUnit.Framework;
using Deltares.Dam.Data.IO;
using Deltares.DamEngine.Data.Standard.Calculation;
using Deltares.Geotechnics.GeotechnicalGeometry;
using Deltares.Geotechnics.SurfaceLines;
using Deltares.Standard.EventPublisher;
using Deltares.Standard.Logging;
using Deltares.Standard.TestUtils;
using CharacteristicPointType = Deltares.Geotechnics.SurfaceLines.CharacteristicPointType;
using DataAttribute = Deltares.Dam.Data.DataPlugins.Configuration.DataAttribute;
namespace Deltares.Dam.Tests.IO
{
public class CombineImportedDataTest
{
[Test]
[Category(Categories.Slow)]
public void TestCombiningData()
{
DataEventPublisher.InvokeWithoutPublishingEvents(() =>
{
LogManager.Messages.Clear();
var start = DateTime.Now;
ProgressDelegate progress;
using (var damProjectData = new DamProjectData())
{
const string testDataFolder = @"TestData\CsvData\ImporterTest";
damProjectData.WaterBoard.Dikes.Add(new Dike());
var configuredAttributes = new List
{
new DataAttribute
{ AttributeId = "location_id", AttributeName = "LOCATIONID", DataSource = "LocationID.shp" },
new DataAttribute
{ AttributeId = "dikering_id", AttributeName = "DIKERINGID", DataSource = "LocationID.shp" }
};
var locationRecords = new List();
var locationImporter = LocationShapeFileImporter.Create(
locationRecords, configuredAttributes, testDataFolder);
if (locationImporter != null)
{
locationImporter.Import();
locationRecords = locationImporter.ImportedItems.ToList();
}
if (locationRecords.Count > 0)
{
progress = null;
var csvImporter = new CsvImporter();
var dikeRingId = damProjectData.WaterBoard.Dikes[0].Name;
foreach (var locationRecord in locationRecords)
{
locationRecord.DikeRingId = dikeRingId;
locationRecord.SegmentId = "1";
locationRecord.SurfaceLineId = "1";
}
csvImporter.LocationRecords = locationRecords;
csvImporter.ImportCsvDataFromDirectory(testDataFolder, false, progress, DamProjectType.Design);
foreach (var errormessage in csvImporter.ErrorMessages)
{
LogManager.Messages.Add(errormessage);
}
var combineImportedData = new CombineImportedData
{
WaterBoard = damProjectData.WaterBoard,
CharacteristicPointsRecords = csvImporter.CharacteristicPointsRecords,
SurfaceLineRecords = csvImporter.SurfaceLinesRecords,
SegmentRecords = csvImporter.SegmentRecords,
SoilProfilerecords = csvImporter.SoilProfilesRecords,
ScenarioRecords = csvImporter.ScenariosRecords,
LocationRecords = csvImporter.LocationRecords,
};
combineImportedData.AddCsvDataToDikes();
combineImportedData.AddScenarioDataToDikes();
foreach (var errormessage in combineImportedData.ErrorMessages)
{
LogManager.Messages.Add(errormessage);
}
}
var end = DateTime.Now;
var elapsed = end - start;
using (var file = new StreamWriter(testDataFolder + "\\Import Error log.txt"))
{
foreach (var logMessage in LogManager.Messages)
{
file.WriteLine(logMessage.Message + " " + logMessage.MessageType);
}
}
Assert.AreEqual(5547, damProjectData.WaterBoard.Dikes[0].Locations.Count);
Assert.AreEqual(306, damProjectData.WaterBoard.Segments.Count);
Assert.AreEqual(3874, damProjectData.WaterBoard.Dikes[0].SoilProfiles.Count); // ca. 6 layers per profile. kan dus kloppen!
Assert.AreEqual(8, damProjectData.WaterBoard.Dikes[0].SoilProfiles[0].Layers.Count);
Assert.AreEqual("HHNK1_kade", damProjectData.WaterBoard.Dikes[0].SoilProfiles[0].Layers[0].Soil.Name);
Assert.AreEqual("Layer0", damProjectData.WaterBoard.Dikes[0].SoilProfiles[0].Layers[0].Name);
Assert.AreEqual(30.0, damProjectData.WaterBoard.Dikes[0].SoilProfiles[0].Layers[0].TopLevel);
Assert.AreEqual(3, damProjectData.WaterBoard.Dikes[0].SurfaceLines2.Count);
// Test import of scenarios
var loc = damProjectData.WaterBoard.Dikes[0].Locations.FirstOrDefault(x => x.Name.Equals("TPL_BALGZANDKANAAL_0110"));
Assert.AreEqual(8, loc.Scenarios.Count);
loc = damProjectData.WaterBoard.Dikes[0].Locations.FirstOrDefault(x => x.Name.Equals("TPL_BALGZANDKANAAL_0180"));
Assert.AreEqual(6, loc.Scenarios.Count);
// Following location has double entries in CSV file; check if this is handled correctly
loc = damProjectData.WaterBoard.Dikes[0].Locations.FirstOrDefault(x => x.Name.Equals("TPL_SCHERMER_BUITEN_WEST_0490"));
Assert.AreEqual(6, loc.Scenarios.Count);
// Total number of scenarios
Assert.AreEqual(19652, damProjectData.WaterBoard.Dikes[0].Scenarios.Count);
// Check if all soilprobabbilites are read
var segment = damProjectData.WaterBoard.Segments.FirstOrDefault(s => s.Name.Equals("2"));
var stabilityProbabilities = new List(segment.SoilProfileProbabilities.Where(
p => ((p.SegmentFailureMechanismType == FailureMechanismSystemType.StabilityInside) ||
(p.SegmentFailureMechanismType == FailureMechanismSystemType.StabilityOutside) ||
(p.SegmentFailureMechanismType == null))));
Assert.AreEqual(3, stabilityProbabilities.Count);
var pipingProbabilities =
new List(segment.SoilProfileProbabilities.Where(
p => ((p.SegmentFailureMechanismType == FailureMechanismSystemType.Piping) ||
(p.SegmentFailureMechanismType == null))));
Assert.AreEqual(3, pipingProbabilities.Count);
Assert.AreEqual(3530, LogManager.Messages.Count);
// Check time needed to read
var maxTime = TimeSpan.FromSeconds(80); // This is the time on the buildserver; local time was 25 seconds
Assert.Less(elapsed, maxTime, "maximum time of {0} exceeded: {1}", maxTime, elapsed);
}
});
}
[Test]
public void TestCombiningDataFullCsv()
{
LogManager.Messages.Clear();
const double tolerance = 0.001;
using (var damProjectData = new DamProjectData())
{
const string testDataFolder = @"TestData\CsvData\Full1DProject\csvfiles";
DamEngine.Data.Standard.Calculation.ProgressDelegate progress = null;
var csvImporter = new CsvImporter();
csvImporter.ImportCsvDataFromDirectory(testDataFolder, false, progress, DamProjectType.Design);
foreach (var errormessage in csvImporter.ErrorMessages)
{
LogManager.Messages.Add(errormessage);
}
var combineImportedData = new CombineImportedData
{
WaterBoard = damProjectData.WaterBoard,
LocationRecords = csvImporter.LocationRecords,
CharacteristicPointsRecords = csvImporter.CharacteristicPointsRecords,
SurfaceLineRecords = csvImporter.SurfaceLinesRecords,
SegmentRecords = csvImporter.SegmentRecords,
SoilProfilerecords = csvImporter.SoilProfilesRecords,
ScenarioRecords = csvImporter.ScenariosRecords
};
combineImportedData.AddCsvDataToDikes();
foreach (var errormessage in combineImportedData.ErrorMessages)
{
LogManager.Messages.Add(errormessage);
}
using (var file = new StreamWriter(testDataFolder + "\\Import full csv error log.txt"))
{
foreach (var logMessage in LogManager.Messages)
{
file.WriteLine(logMessage.Message + " " + logMessage.MessageType);
}
}
Assert.AreEqual(2, damProjectData.WaterBoard.Dikes[0].Locations.Count);
Assert.AreEqual(2, damProjectData.WaterBoard.Segments.Count);
Assert.AreEqual(23, damProjectData.WaterBoard.Dikes[0].SoilProfiles.Count);
Assert.AreEqual(4, damProjectData.WaterBoard.Dikes[0].SoilProfiles[0].Layers.Count);
Assert.AreEqual("kade", damProjectData.WaterBoard.Dikes[0].SoilProfiles[0].Layers[0].Soil.Name);
Assert.AreEqual("Layer0", damProjectData.WaterBoard.Dikes[0].SoilProfiles[0].Layers[0].Name);
Assert.AreEqual(60.0, damProjectData.WaterBoard.Dikes[0].SoilProfiles[0].Layers[0].TopLevel);
Assert.AreEqual(2, damProjectData.WaterBoard.Dikes[0].SurfaceLines2.Count);
// Check location specific data
var location = damProjectData.WaterBoard.Dikes[0].Locations.FirstOrDefault(s => s.Name.Equals("25-2-2-A-1-A"));
Assert.IsTrue(location != null);
Assert.AreEqual("25-2-2-A-1-A", location.Name);
//Assert.AreEqual("25-2-2-A-1-A", location.SurfaceLine.Name);
//Assert.AreEqual("106", location.Segment.Name);
Assert.AreEqual(66586.0, location.XRd, tolerance);
Assert.AreEqual(424173.0, location.YRd, tolerance);
Assert.AreEqual(2.0, location.XSoilGeometry2DOrigin, tolerance);
//Assert.AreEqual("25-2-2-A-1-A", location.PL1Line.Name);
Assert.AreEqual(-0.25, location.PolderLevel, tolerance);
Assert.AreEqual(0.8727, location.HeadPL2.Value, tolerance);
Assert.AreEqual("klei", location.DikeEmbankmentMaterial);
Assert.AreEqual("klei2", location.ShoulderEmbankmentMaterial);
Assert.AreEqual(1.3, location.PenetrationLength, tolerance);
Assert.AreEqual(10, location.TrafficLoad, tolerance);
Assert.AreEqual(22.44, location.TL_DegreeOfConsolidation, tolerance);
Assert.AreEqual(1.5, location.MinimalCircleDepth, tolerance);
Assert.AreEqual(0.3, location.DampingFactorPL3, tolerance);
Assert.AreEqual(0.4, location.DampingFactorPL4, tolerance);
Assert.AreEqual(PLLineCreationMethod.ExpertKnowledgeRRD, location.PLLineCreationMethod);
Assert.AreEqual(2.1, location.DistanceToEntryPoint, tolerance);
Assert.AreEqual(2.0, location.StabilityShoulderGrowSlope, tolerance);
Assert.AreEqual(0.2, location.StabilityShoulderGrowDeltaX, tolerance);
Assert.AreEqual(0.5, location.StabilitySlopeAdaptionDeltaX, tolerance);
}
}
[Test]
public void TestCheckCharacteristicPointsForCoincidingLocationsValid()
{
var surfaceLine = CreateValidSurfaceLine();
var combineImportedData = new CombineImportedData();
CharacteristicPointType point1;
CharacteristicPointType point2;
var doNotCoincide = combineImportedData.CheckCharacteristicPointsForCoincidingLocations(surfaceLine, out point1, out point2);
Assert.IsTrue(doNotCoincide);
}
[Test]
public void TestCheckCharacteristicPointsForCoincidingLocationsInValid()
{
var surfaceLine = CreateInvalidSurfaceLine();
var combineImportedData = new CombineImportedData();
CharacteristicPointType point1;
CharacteristicPointType point2;
var doNotCoincide = combineImportedData.CheckCharacteristicPointsForCoincidingLocations(surfaceLine, out point1, out point2);
Assert.IsFalse(doNotCoincide);
Assert.AreEqual(CharacteristicPointType.DikeTopAtPolder, point1);
Assert.AreEqual(CharacteristicPointType.ShoulderBaseInside, point2);
}
[Test]
public void TestCheckCharacteristicPointsForCoincidingLocationsValidTrafficLoad()
{
var surfaceLine = CreateValidSurfaceLineTrafficLoad();
var combineImportedData = new CombineImportedData();
CharacteristicPointType point1;
CharacteristicPointType point2;
var doNotCoincide = combineImportedData.CheckCharacteristicPointsForCoincidingLocations(surfaceLine, out point1, out point2);
Assert.IsTrue(doNotCoincide);
}
[Test]
public void TestProperMessageForCheckCharacteristicPointsForCoincidingLocationsInValid()
{
var surfaceLine = CreateInvalidSurfaceLine();
var combineImportedData = new CombineImportedData();
combineImportedData.ErrorMessages.Clear();
var doNotCoincide = combineImportedData.CheckOnCoincidingPoints(surfaceLine);
Assert.IsFalse(doNotCoincide);
Assert.AreEqual(1, combineImportedData.ErrorMessages.Count);
Assert.IsTrue(combineImportedData.ErrorMessages[0].Message.Contains(CharacteristicPointType.DikeTopAtPolder.ToString()));
Assert.IsTrue(combineImportedData.ErrorMessages[0].Message.Contains(CharacteristicPointType.ShoulderBaseInside.ToString()));
}
private SurfaceLine2 CreateValidSurfaceLine()
{
var surfaceLine = new SurfaceLine2()
{
Geometry = new LocalizedGeometryPointString(),
CharacteristicPoints = { GeometryMustContainPoint = true }
};
surfaceLine.EnsurePointOfType(0, 0, 0, CharacteristicPointType.SurfaceLevelOutside);
surfaceLine.EnsurePointOfType(10, 0, 0, CharacteristicPointType.DikeToeAtRiver);
surfaceLine.EnsurePointOfType(12, 0, 2, CharacteristicPointType.DikeTopAtRiver);
surfaceLine.EnsurePointOfType(15, 0, 2, CharacteristicPointType.DikeTopAtPolder);
surfaceLine.EnsurePointOfType(20, 0, -1, CharacteristicPointType.DikeToeAtPolder);
surfaceLine.EnsurePointOfType(80, 0, -1, CharacteristicPointType.SurfaceLevelInside);
return surfaceLine;
}
private SurfaceLine2 CreateInvalidSurfaceLine()
{
var surfaceLine = CreateValidSurfaceLine();
surfaceLine.EnsurePointOfType(15, 0, 2, CharacteristicPointType.ShoulderBaseInside);
return surfaceLine;
}
private SurfaceLine2 CreateValidSurfaceLineTrafficLoad()
{
var surfaceLine = CreateValidSurfaceLine();
surfaceLine.EnsurePointOfType(15, 0, 2, CharacteristicPointType.TrafficLoadOutside);
return surfaceLine;
}
}
}