// Copyright (C) Stichting Deltares 2023. 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.Text;
using Deltares.DamEngine.Calculators.KernelWrappers.Common;
using Deltares.DamEngine.Calculators.KernelWrappers.Interfaces;
using Deltares.DamEngine.Calculators.Properties;
using Deltares.DamEngine.Data.Design;
using Deltares.DamEngine.Data.General.Results;
using Deltares.DamEngine.Data.Standard.Calculation;
using Deltares.DamEngine.Data.Standard.Logging;
namespace Deltares.DamEngine.Calculators.DikesDesign;
///
/// The Dam Engine design calculator
///
public class DesignCalculatorSingle
{
///
/// Performs the single calculation.
///
/// The kernel wrapper.
/// The kernel data input.
/// The kernel data output.
/// The dam kernel input.
/// The design scenario.
/// The calculation messages.
/// The design calculations.
public static void PerformSingleCalculation(IKernelWrapper kernelWrapper, IKernelDataInput kernelDataInput, IKernelDataOutput kernelDataOutput,
DamKernelInput damKernelInput, DesignScenario designScenario,
List calculationMessages, List designCalculations)
{
// Perform validation
var designResults = new List();
var locationCalculationMessages = new List();
List validationMessages;
designScenario.CalculationResult = CalculationResult.NoRun;
try
{
int errorCount = kernelWrapper.Validate(kernelDataInput, kernelDataOutput, out validationMessages);
if (errorCount > 0)
{
locationCalculationMessages.Add(new LogMessage(LogMessageType.Error, null,
string.Format(Resources.DesignCalculatorValidationFailed,
damKernelInput.Location.Name,
damKernelInput.SubSoilScenario,
designScenario.LocationScenarioID)));
locationCalculationMessages.AddRange(validationMessages);
}
else
{
// Perform calculation
kernelWrapper.Execute(kernelDataInput, kernelDataOutput, out locationCalculationMessages);
}
// Process output
calculationMessages.AddRange(locationCalculationMessages);
var sb = new StringBuilder();
foreach (LogMessage message in locationCalculationMessages)
{
sb.Append(message.Message + Environment.NewLine);
}
var resultMessage = sb.ToString();
designScenario.CalculationResult = CalculationResult.Succeeded;
kernelWrapper.PostProcess(damKernelInput, kernelDataOutput, designScenario, resultMessage, out designResults);
}
catch (Exception exception)
{
string resultMessage = exception.Message;
designScenario.CalculationResult = CalculationResult.RunFailed;
kernelWrapper.PostProcess(damKernelInput, kernelDataOutput, designScenario, resultMessage, out designResults);
throw new DesignCalculatorException(Resources.DesignUnsuccessful + " " + resultMessage);
}
finally
{
foreach (DesignResult designResult in designResults)
{
designCalculations.Add(designResult);
}
}
}
}