// Copyright (C) Stichting Deltares 2017. 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.ComponentModel; using System.Diagnostics; using System.IO; namespace Deltares.DamEngine.Calculators.Dikes_Operational { /// /// 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 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); ##Bka } /// /// 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); ##Bka } /// /// 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); ##Bka } } }