//----------------------------------------------------------------------- // // Copyright (c) 2011 Deltares. All rights reserved. // // B.S.T.I.M. The // tom.the@deltares.nl // 20-05-2011 // Contains class to interact with external Dupuit calcualtion program //----------------------------------------------------------------------- using System.ComponentModel; using System.Diagnostics; using System.IO; using Deltares.Dupuit; namespace Deltares.Dam.Data { using System; /// /// Except for Dupuit calculation /// public class DupuitCalculatorException : ApplicationException { public DupuitCalculatorException(string message) : base(message) { } } /// /// How to call the Dupuit Calculation /// public enum DupuitCalculationType { ViaExeAndFiles, ViaDllAndFiles, ViaDllAndMemory, } /// /// The Dupuit Calculator /// public class DupuitCalculator { const string cDefaultDupuitProgramFullFilename = ".\\Dupuit\\DikeFlow.exe"; private string dupuitProgramFullFilename = ""; private DupuitCalculationType dupuitCalculationType = DupuitCalculationType.ViaDllAndFiles; public string DupuitProgramFullFilename { get { return dupuitProgramFullFilename; } set { dupuitProgramFullFilename = value; } } /// /// Gets or sets the type of the dupuit calculation. /// /// /// The type of the dupuit calculation. /// public DupuitCalculationType DupuitCalculationType { get { return dupuitCalculationType; } set { dupuitCalculationType = value; } } /// /// Constructor /// public DupuitCalculator() { DupuitProgramFullFilename = cDefaultDupuitProgramFullFilename; if (!File.Exists(dupuitProgramFullFilename)) { throw new Deltares.Dam.Data.DupuitCalculatorException(String.Format("Executable '{0}' not found", dupuitProgramFullFilename)); } } /// /// Constructor /// /// public DupuitCalculator(string dupuitProgramFullFilename) { DupuitProgramFullFilename = dupuitProgramFullFilename; if (!File.Exists(dupuitProgramFullFilename)) { throw new DupuitCalculatorException(String.Format("Executable '{0}' not found", dupuitProgramFullFilename)); } } /// /// Perform the Dupuit calculation by calling the external program /// /// The full filename. public void Calculate(string fullFilename) { switch (DupuitCalculationType) { case DupuitCalculationType.ViaExeAndFiles: CalculateViaExe(fullFilename); break; case DupuitCalculationType.ViaDllAndFiles: CalculateViaDll(fullFilename); break; case DupuitCalculationType.ViaDllAndMemory: CalculateViaDllAndMemory(fullFilename); break; default: throw new DupuitCalculatorException(String.Format("Not implemented Dupuitcalculation type: '{0}'", DupuitCalculationType)); } } /// /// Perform the Dupuit calculation by calling the DLL /// /// The full filename. private void CalculateViaDll(string fullFilename) { var dWrap = new dupuitWrapper(); int handle = dWrap.CreateDupuitHandle(); dWrap.setDupuit(handle, fullFilename); dWrap.initialize(handle, fullFilename); int result; do { result = dWrap.calculateTransient(handle, fullFilename); } while (result > 0); } /// /// Perform the Dupuit calculation by calling the DLL /// /// The full filename. private void CalculateViaDllAndMemory(string fullFilename) { var dWrap = new dupuitWrapper(); int handle = dWrap.CreateDupuitHandle(); int result = 1; do { result = dWrap.calculateTransient(handle, fullFilename); } while (result > 0); } /// /// Perform the Dupuit calculation by calling the external program /// /// The full filename. /// /// private void CalculateViaExe(string fullFilename) { Validate(fullFilename); const string quote = "\""; string argument = quote + fullFilename + quote; var process = new Process { StartInfo = { FileName = DupuitProgramFullFilename, Arguments = argument, UseShellExecute = false } }; try { process.Start(); } catch (Win32Exception w) { throw new DupuitCalculatorException(String.Format("DikeFlow error {0}", w.Message)); } process.WaitForExit(); if (process.ExitCode != 0) { throw new DupuitCalculatorException(String.Format("DikeFlow error {0}", process.ExitCode)); } } /// /// Write file with the geometry /// public void WriteSoilLayersFile() { throw new NotImplementedException(); } /// /// Write file with the timeseries of waterlevels /// public void WriteWaterLevelFile() { throw new NotImplementedException(); } /// /// Write file with precipitation /// Not used yet so always write no precipitation /// public void WritePrecipitationFile() { throw new NotImplementedException(); } /// /// Write file with material properties /// public void WriteMaterialFile() { throw new NotImplementedException(); } /// /// Write file with monitoring /// Not used yet so always write empty file /// public void WriteMonitorFile() { throw new NotImplementedException(); } /// /// Write file with characteristic points /// public void WriteSectionsFile() { throw new NotImplementedException(); } /// /// Write file with calculation options /// public void WriteCalculateFile() { throw new NotImplementedException(); } /// /// Read all piezolines (pllines) for all timesteps /// public void ReadPiezoLinesFiles() { throw new NotImplementedException(); } /// /// Throw an exception if object undefined /// /// /// private void ThrowIfArgumentNull(Object argument, string objectName) { if (argument == null) { throw new DupuitCalculatorException(String.Format("Missing input: '{0}' not defined", objectName)); } } /// /// Throw an exception if string undefined /// /// /// private void ThrowIfStringIsEmpty(string argument, string stringName) { if (argument.Equals("")) { throw new DupuitCalculatorException(String.Format("Missing input: '{0}' not defined", stringName)); } } private void ThrowIfFileNotExists(string fullFilename) { if (!File.Exists(fullFilename)) { throw new DupuitCalculatorException(String.Format("Missing file: '{0}'", fullFilename)); } } /// /// Validate if calculation can be made /// private void Validate(string fullFilename) { ThrowIfStringIsEmpty(dupuitProgramFullFilename, "dupuitProgramFullFilename"); string baseFilename = Path.GetFullPath(fullFilename); ThrowIfFileNotExists(dupuitProgramFullFilename); ThrowIfFileNotExists(baseFilename + DupuitCalculatorFileIO.soilLayersFilenameExt); ThrowIfFileNotExists(baseFilename + DupuitCalculatorFileIO.waterLevelFilenameExt); ThrowIfFileNotExists(baseFilename + DupuitCalculatorFileIO.precipitationFilenameExt); ThrowIfFileNotExists(baseFilename + DupuitCalculatorFileIO.materialFilenameExt); ThrowIfFileNotExists(baseFilename + DupuitCalculatorFileIO.monitorFilenameExt); ThrowIfFileNotExists(baseFilename + DupuitCalculatorFileIO.sectionsFilenameExt); ThrowIfFileNotExists(baseFilename + DupuitCalculatorFileIO.calculateFilenameExt); } } }