// 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Deltares.DamEngine.Data.General;
using Deltares.DamEngine.Io;
using Deltares.DamEngine.Io.XmlOutput;
using Deltares.DamEngine.TestHelpers;
using NUnit.Framework;
namespace Deltares.DamEngine.Interface.Tests
{
[TestFixture]
class MultiCorePipingCalculationTests
{
private const double tolerance = 0.0005;
// [Test, Category("MultiCore")]
[Test] // This test intentionally not in category Multicore, so code coverage will reach the multicore methods
[TestCase(PipingModelType.Bligh)]
[TestCase(PipingModelType.Sellmeijer4Forces)]
[TestCase(PipingModelType.SellmeijerVnk)]
public void CanPerformPipingDesignWithAdaptionRechterDiezedijkMultiCore(PipingModelType pipingModelType)
{
// Based on CanPerformBlighDesignWithAdaptionRechterDiezedijk()
const int multiCoreCount = 4;
const string fileName = @"TestFiles\Rechter Diezedijk_BlighDesignInputFile.xml";
const string fileNameOutputPrefix = @"TestFiles\Rechter Diezedijk";
string inputString = File.ReadAllText(fileName);
// Set piping model type
inputString = XmlAdapter.ChangeValueInXml(inputString, "PipingModelType", pipingModelType.ToString());
// Calculate one core
Debug.WriteLine("Perform single core calculation Piping");
EngineInterface engineInterface = new EngineInterface(inputString);
Assert.IsNotNull(engineInterface.DamProjectData);
string outputString = engineInterface.Run();
Assert.IsNotNull(outputString);
var outputOneCore = DamXmlSerialization.LoadOutputFromXmlString(outputString);
string outputFilename = DetermineOutputFilename(fileNameOutputPrefix, pipingModelType.ToString(), 1);
File.WriteAllText(outputFilename, outputString, Encoding.Unicode);
// Calculate multicore
Debug.WriteLine("Perform multicore calculation with {0} cores Piping", multiCoreCount);
inputString = XmlAdapter.ChangeValueInXml(inputString, "MaxCalculationCores", multiCoreCount.ToString());
engineInterface = new EngineInterface(inputString);
Assert.IsNotNull(engineInterface.DamProjectData);
outputString = engineInterface.Run();
Assert.IsNotNull(outputString);
var outputMultiCore = DamXmlSerialization.LoadOutputFromXmlString(outputString);
outputFilename = DetermineOutputFilename(fileNameOutputPrefix, pipingModelType.ToString(), multiCoreCount);
File.WriteAllText(outputFilename, outputString, Encoding.Unicode);
// Compare the results
var differences = new List();
StringBuilder differencesStringBuilder = new StringBuilder();
foreach (DesignResult oneCoreResult in outputOneCore.Results.CalculationResults.DesignResults)
{
DesignResult multiCoreResult = outputMultiCore.Results.CalculationResults.DesignResults.Where(x => x.LocationName.Equals(oneCoreResult.LocationName) && x.ProfileName.Equals(oneCoreResult.ProfileName)).FirstOrDefault();
if (multiCoreResult == null)
{
var diffString = String.Format("No mutlicore result in location '{0}', soilprofile '{1}'", oneCoreResult.LocationName, oneCoreResult.ProfileName);
differencesStringBuilder.AppendLine(diffString);
differences.Add(diffString);
}
else
{
double safetyFactorSingleCore = 0.0;
double safetyFactorMultiCore = 0.0;
switch (pipingModelType)
{
case PipingModelType.Bligh:
safetyFactorSingleCore = oneCoreResult.PipingDesignResults.BlighFactor;
safetyFactorMultiCore = multiCoreResult.PipingDesignResults.BlighFactor;
break;
case PipingModelType.Sellmeijer4Forces:
safetyFactorSingleCore = oneCoreResult.PipingDesignResults.Sellmeijer4ForcesFactor;
safetyFactorMultiCore = multiCoreResult.PipingDesignResults.Sellmeijer4ForcesFactor;
break;
case PipingModelType.SellmeijerVnk:
safetyFactorSingleCore = oneCoreResult.PipingDesignResults.SellmeijerVnkFactor;
safetyFactorMultiCore = multiCoreResult.PipingDesignResults.SellmeijerVnkFactor;
break;
}
if (Math.Abs(safetyFactorSingleCore - safetyFactorMultiCore) > tolerance)
{
var diffString = String.Format("Different result in location '{0}', soilprofile '{1}': 1 core = {2}, multicore = {3}", oneCoreResult.LocationName, oneCoreResult.ProfileName, oneCoreResult.PipingDesignResults.BlighFactor, multiCoreResult.PipingDesignResults.BlighFactor);
differencesStringBuilder.AppendLine(diffString);
differences.Add(diffString);
}
}
}
Assert.IsTrue(differences.Count == 0, "Differences found" + Environment.NewLine + differencesStringBuilder);
}
private string DetermineOutputFilename(string prefix, string modelType, int coreCount, string extension = ".xml")
{
return String.Format("{0}_{1}_{2}core_OutputFile{3}", prefix, modelType, coreCount, extension);
}
}
}