// Copyright (C) Stichting Deltares 2024. 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.XmlOutput; using Deltares.DamEngine.TestHelpers; using NUnit.Framework; namespace Deltares.DamEngine.IntegrationTests.IntegrationTests; [TestFixture] class MultiCorePipingCalculationTests { private const double tolerance = 0.0005; [Test] // This test intentionally not in category Multicore, so code coverage will reach the multicore methods [TestCase(PipingModelType.Bligh)] public void CanPerformPipingBlighDesignWithAdaptionRechterDiezedijkMultiCore(PipingModelType pipingModelType) { RunMultiCore(pipingModelType); } private void RunMultiCore(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"); string outputFilename = DetermineOutputFilename(fileNameOutputPrefix, pipingModelType.ToString(), 1); Output outputOneCore = GeneralHelper.RunAfterInputValidation(inputString, true, outputFilename); // Calculate multicore Debug.WriteLine(""); Debug.WriteLine("Perform multicore calculation with {0} cores Piping", multiCoreCount); inputString = XmlAdapter.ChangeValueInXml(inputString, "MaxCalculationCores", multiCoreCount.ToString()); outputFilename = DetermineOutputFilename(fileNameOutputPrefix, pipingModelType.ToString(), multiCoreCount); Output outputMultiCore = GeneralHelper.RunAfterInputValidation(inputString, true, outputFilename); // Compare the results var differences = new List(); var differencesStringBuilder = new StringBuilder(); foreach (DesignResult oneCoreResult in outputOneCore.Results.CalculationResults) { DesignResult multiCoreResult = outputMultiCore.Results.CalculationResults.Where(x => x.LocationName.Equals(oneCoreResult.LocationName) && x.ProfileName.Equals(oneCoreResult.ProfileName)).FirstOrDefault(); if (multiCoreResult == null) { var diffString = $"No mutlicore result in location '{oneCoreResult.LocationName}', soilprofile '{oneCoreResult.ProfileName}'"; differencesStringBuilder.AppendLine(diffString); differences.Add(diffString); } else { var safetyFactorSingleCore = 0.0; var safetyFactorMultiCore = 0.0; switch (pipingModelType) { case PipingModelType.Bligh: safetyFactorSingleCore = oneCoreResult.PipingDesignResults.BlighFactor; safetyFactorMultiCore = multiCoreResult.PipingDesignResults.BlighFactor; break; } if (Math.Abs(safetyFactorSingleCore - safetyFactorMultiCore) > tolerance) { var diffString = $"Different result in location '{oneCoreResult.LocationName}', soilprofile '{oneCoreResult.ProfileName}': 1 core = {oneCoreResult.PipingDesignResults.BlighFactor}, multicore = {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 $"{prefix}_{modelType}_{coreCount}core_OutputFile{extension}"; } }