// Copyright (C) Stichting Deltares 2023. All rights reserved.
//
// This file is part of the application DAM - Live.
//
// DAM - Live 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.IO;
using System.Linq;
using Deltares.Dam.Application.Live;
using Deltares.Dam.Data;
using Deltares.DamLive.TestHelper;
using Deltares.Standard.IO;
using NUnit.Framework;
namespace Deltares.DamLive.Tests;
[TestFixture]
[Category("Integration")]
public class DamLiveHHNKTests
{
[Test]
[Category("Integration")]
[Category("Work_In_Progress")]
public void CalculateStabilityUsingTestFilesHasExpectedResultsInOutputFile()
{
const double cTolerance = 0.0005;
SetupStabilityProject();
runner.Initialize();
runner.DamProjectData.MaxCalculationCores = 1;
Assert.That(runner, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(runner.DamProjectData, Is.Not.Null);
Assert.That(runner.CalculationParameters, Is.Not.Null);
});
Assert.Multiple(() =>
{
Assert.That(runner.CalculationParameters.CalculationModules, Is.Not.Null);
Assert.That(runner.CalculationParameters.MStabParameters, Is.Not.Null);
});
runner.Run();
//runner.WriteResultsToFile(outputFile);
AssertNoErrors();
// Assertions
List series = runner.OutputTimeSeriesCollection.Series;
var seriesCount = 0;
string[] validParameterIDs = Enum.GetNames(typeof(TimeSerieParameters));
foreach (TimeSerie timeSeries in series)
{
Assert.Multiple(() =>
{
Assert.That(validParameterIDs.Any(n => n == timeSeries.ParameterId), Is.True);
Assert.That(locations.Any(l => l.Name == timeSeries.LocationId), Is.True);
});
// StabilityInside check
if (timeSeries.ParameterId == TimeSerieParameters.StabilityInsideFactor.ToString())
{
if (timeSeries.LocationId == "Purmer_PU0110+20_R_V")
{
TimeSerieEntry firstEntry = timeSeries.Entries.First();
Assert.That(firstEntry.Value, Is.EqualTo(0.742).Within(cTolerance), "The computed safety factory is not correct");
}
if (timeSeries.LocationId == "Purmer_PU0042+00_K")
{
TimeSerieEntry firstEntry = timeSeries.Entries.First();
Assert.That(firstEntry.Value, Is.EqualTo(0.8555).Within(cTolerance), "The computed safety factory is not correct");
}
}
seriesCount++;
}
Assert.That(seriesCount, Is.GreaterThan(0), "No output time series");
}
#region Helper methods
///
/// Asserts that there are no errors in the runner
///
private void AssertNoErrors()
{
if (runner.HasErrors)
{
Assert.Fail("The test failed. See the error log in the test output console for more info");
}
}
#endregion
#region Test Setup
private const string projectStabilityFilename = @"DAMLive.damx";
private const string input1aFilename = @"live.InputTimeSeries.xml";
private const string output1aFilename = @"live.OutputTimeSeries.xml";
private const string calculationParametersStabilityFilename = @"live.ParametersFile.xml";
private const string testWorkingFolder = @".\damLiveHHNKWork";
private const string testStablityDataFolder = @"..\..\..\..\data\HHNK Purmer";
private List locations;
private string inputFile;
private string outputFile;
private string projectFile;
private string parameterFile;
private DamEngineRunner runner;
[SetUp]
public void SetupFixture()
{
IoHelper.RemoveTestWorkingDirectory(testWorkingFolder); // to be sure no test directory exist from previous tests
Directory.CreateDirectory(testWorkingFolder);
}
[TearDown]
public void TearDownFixture()
{
IoHelper.RemoveTestWorkingDirectory(testWorkingFolder);
}
[TearDown]
public void TearDownTest()
{
AssertNoErrors();
}
[SetUp]
public void SetupTest() {}
///
/// Sets up the stability project.
///
private void SetupStabilityProject()
{
string actualTestPath = Path.GetFullPath(testWorkingFolder);
Directory.CreateDirectory(actualTestPath);
DirectoryHelper.CopyRecursive(actualTestPath, testStablityDataFolder);
inputFile = Path.Combine(actualTestPath, input1aFilename);
outputFile = Path.Combine(actualTestPath, output1aFilename);
parameterFile = Path.Combine(actualTestPath, calculationParametersStabilityFilename);
projectFile = Path.Combine(actualTestPath, projectStabilityFilename);
DamProject.SetTestProgramVersion("24.1");
DamProjectData damData = DamProject.LoadData(projectFile);
locations = damData.Locations;
// Load the sensor time serie data (see input file for details)
TimeSerieCollection inputTimeSeries = TimeSerieCollection.LoadFromFile(inputFile);
ShortenTimeSeries(inputTimeSeries);
inputTimeSeries.Save(Path.ChangeExtension(inputFile, "short.xml"));
runner = new DamEngineRunner
{
DamXFile = new FileInfo(projectFile),
ParametersFile = new FileInfo(parameterFile),
FewsInputFile = new FileInfo(inputFile),
FewsOutputFile = new FileInfo(outputFile),
Filter = "",
InputTimeSeriesCollection = inputTimeSeries
};
}
///
/// Make timeserie shorter for testing: the first, the last and the middle entry are only used
///
///
private static void ShortenTimeSeries(TimeSerieCollection inputTimeSeries)
{
foreach (TimeSerie serie in inputTimeSeries.Series)
{
for (int i = serie.Entries.Count - 2; i > 0; i--)
{
if (i != 120) // keep the time entry in the middle of the serie
{
serie.Entries.RemoveAt(i);
}
}
}
}
#endregion
}