Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityCommonHelper.cs =================================================================== diff -u -r3801 -r3819 --- DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityCommonHelper.cs (.../MacroStabilityCommonHelper.cs) (revision 3801) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityCommonHelper.cs (.../MacroStabilityCommonHelper.cs) (revision 3819) @@ -19,17 +19,22 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; +using System.Collections.Generic; using System.Data; using System.IO; using System.Text.RegularExpressions; using Deltares.DamEngine.Calculators.KernelWrappers.Common; +using Deltares.DamEngine.Calculators.KernelWrappers.Interfaces; +using Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityCommon.MacroStabilityIo; using Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityInwards; using Deltares.DamEngine.Calculators.Properties; using Deltares.DamEngine.Data.Design; using Deltares.DamEngine.Data.General; using Deltares.DamEngine.Data.General.Results; using Deltares.DamEngine.Data.Geotechnics; using Deltares.DamEngine.Data.Standard.Calculation; +using Deltares.DamEngine.Data.Standard.Logging; +using Deltares.MacroStability.CSharpWrapper; using Deltares.MacroStability.CSharpWrapper.Input; using Deltares.MacroStability.CSharpWrapper.Output; using Deltares.StixFileWriter; @@ -231,7 +236,7 @@ /// /// Name of the file. /// The input. - public static void WriteStixFileBasedOnInputAndResultsSearchGrid(string fileName, MacroStabilityInput input) + private static void WriteStixFileBasedOnInputAndResultsSearchGrid(string fileName, MacroStabilityInput input) { if (!(input.StabilityModel.ModelOption == StabilityModelOptionType.UpliftVan && input.StabilityModel.SearchAlgorithm == SearchAlgorithm.Beeswarm)) @@ -250,7 +255,7 @@ /// Name of the file. /// The input. /// The macro stability output. - public static void WriteStixFileBasedOnInputAndResultsSlipPlane(string fileName, MacroStabilityInput input, + private static void WriteStixFileBasedOnInputAndResultsSlipPlane(string fileName, MacroStabilityInput input, MacroStabilityOutput macroStabilityOutput) { if (macroStabilityOutput.CalculationResult == CalculationResult.Succeeded) @@ -342,5 +347,146 @@ macroStabilityOutputItem.PassiveCenterPointRadius; } } + + /// + /// Prepares the kernel. + /// + /// The calculator. + /// Name of the file. + /// + public static PrepareResult PrepareKernel(Calculator calculator, string fileName) + { + try + { + // For now a simple check to see if any data has been past at all. + var inputAsXml = calculator.KernelInputXml; + + File.WriteAllText(fileName, inputAsXml); + + if (inputAsXml.Length > 10) + { + return PrepareResult.Successful; + } + + return PrepareResult.Failed; + } + catch + { + return PrepareResult.Failed; + } + } + + /// + /// Validates the specified kernel data input. + /// + /// The kernel data input. + /// The kernel data output. + /// The return messages. + /// + /// Zero when there are no errors, one when there are errors that prevent a calculation + /// + public static int Validate(IKernelDataInput kernelDataInput, IKernelDataOutput kernelDataOutput, out List messages) + { + MacroStabilityKernelDataInput macroStabilityKernelDataInput = (MacroStabilityKernelDataInput)kernelDataInput; + messages = new List(); + try + { + var result = new Validator(macroStabilityKernelDataInput.Input).Validate(); + if (result.IsValid) + { + return 0; + } + + if (kernelDataOutput != null) + { + ((MacroStabilityOutput)kernelDataOutput).CalculationResult = CalculationResult.InvalidInputData; + } + + foreach (var resultMessage in result.Messages) + { + var message = new LogMessage + { + Message = resultMessage.Content + }; + switch (resultMessage.MessageType) + { + case MessageType.Error: + { + message.MessageType = LogMessageType.Error; + break; + } + case MessageType.Info: + { + message.MessageType = LogMessageType.Info; + break; + } + case MessageType.Warning: + { + message.MessageType = LogMessageType.Warning; + break; + } + } + + messages.Add(message); + } + + return 1; + } + catch (Exception e) + { + var message = new LogMessage { MessageType = LogMessageType.FatalError, Message = e.Message }; + messages.Add(message); + if (kernelDataOutput != null) + { + ((MacroStabilityOutput)kernelDataOutput).CalculationResult = CalculationResult.InvalidInputData; + } + + return 1; + } + } + + /// + /// Performs the stability calculation. + /// + /// The input. + /// The macro stability output. + /// Name of the file. + /// The calculator. + /// The messages. + public static void PerformStabilityCalculation(MacroStabilityInput input, MacroStabilityOutput macroStabilityOutput, + string fileName, ICalculator calculator, out List messages) + { + macroStabilityOutput.CalculationResult = CalculationResult.NoRun; + macroStabilityOutput.StabilityOutputItems = new List(); + messages = new List(); + try + { + var macroStabilityOutputKernel = calculator.Calculate(); + FillEngineFromMacroStabilityWrapperOutput.FillEngineDataWithResults(macroStabilityOutputKernel, macroStabilityOutput, + out messages); +#if DEBUG + foreach (var stabilityOutputItem in macroStabilityOutput.StabilityOutputItems) + { + switch (stabilityOutputItem.StabilityModelType) + { + case MStabModelType.Bishop: + input.StabilityModel.BishopCalculationCircle = stabilityOutputItem.BishopCalculationCircle; + break; + case MStabModelType.UpliftVan: + input.StabilityModel.UpliftVanCalculationGrid = stabilityOutputItem.UpliftVanCalculationGrid; + break; + } + + WriteStixFileBasedOnInputAndResultsSearchGrid(fileName, input); + } +#endif + WriteStixFileBasedOnInputAndResultsSlipPlane(fileName, input, macroStabilityOutput); + } + catch (Exception e) + { + macroStabilityOutput.CalculationResult = CalculationResult.UnexpectedError; + messages.Add(new LogMessage(LogMessageType.Error, null, e.Message)); + } + } } }