/// <summary> /// This is basically the same example as the SaveNetworkData above, though /// this time the data is stored as a part of the MIKE 1D setup in the /// standard .m1dx file. /// </summary> /// <param name="vidaaFilepath">Path and name of the Vidaa example</param> public static void SaveDoubleNetworkDataWithSetup(string vidaaFilepath) { { // Creates a new Mike 1D controller and load the setup Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(vidaaFilepath), diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Loading errors, aborting"); } // Create a NetworkData that can store a user defined type, and add some data at some locations NetworkDataDouble myDoubleData = new NetworkDataDouble() { CanInterpolate = false }; myDoubleData.AddValue(new Location("SonderAa", 1034), 1.034); myDoubleData.AddValue(new Location("LindSkov", 2860), 2.86); myDoubleData.AddValue("MyNodeId", -42); // Set the NetworkData as additional data, using the string "UserData" as key controller.Mike1DData.SetAdditionalData("UserData", myDoubleData); // Saving the file again (same filename, new extension) controller.Mike1DData.Connection.BridgeName = "m1dx"; controller.Mike1DData.Connection.FilePath.Extension = ".m1dx"; Mike1DBridge.Save(controller.Mike1DData); controller.Finish(); } { // Load the setup again, changing the extension to .m1dx FilePath vidaaFile = new FilePath(vidaaFilepath); vidaaFile.Extension = ".m1dx"; // Load the setup Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(vidaaFile.Path), diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Loading errors, aborting"); } // Get the additional data using the string "UserData" as key, which we know is a NetworkData with MyData NetworkDataDouble myComplexData = (NetworkDataDouble)controller.Mike1DData.GetAdditionalData("UserData"); // Now we can extract our data again double v1034; bool ok1034 = myComplexData.GetValue(new Location("SonderAa", 1034), out v1034); double v2860; bool ok2860 = myComplexData.GetValue(new Location("LindSkov", 2860), out v2860); double vNode; bool okNode = myComplexData.GetValue("MyNodeId", out vNode); controller.Finish(); } }
/// <summary> /// Using the MIKE 1D controller to run a simulation /// <para> /// The controller gives more control of the simulation. /// It also pushes the responsibility of error reporting and /// summary information to the user. /// </para> /// </summary> /// <param name="setupFilepath">Path to setup file (.sim11, .mdb or .m1dx)</param> public static void ControllerRun(string setupFilepath) { // The controller factory Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); IMike1DController controller = null; // Try-catch to catch any unexpected exception or runtime exceptions. try { // Diagnostics object receiving errors, warning and hints during // load and initialize. Diagnostics diagnostics = new Diagnostics("My Diagnostics"); // creates a new Mike 1D controller and load the setup controller = controllerFactory.OpenAndCreate(Connection.Create(setupFilepath), diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Loading errors, aborting"); } // Now the MIKE 1D data is available Mike1DData mike1DData = controller.Mike1DData; // Validate setup, returns a new diagnostics object IDiagnostics validated = controller.Validate(); if (validated.ErrorCountRecursive > 0) { throw new Exception("Validation errors, aborting"); } // Initialize simulation controller.Initialize(diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Initialization errors, aborting"); } // Run the simulation controller.Prepare(); controller.Run(); controller.Finish(); } catch (Exception e) { // Write exception to log file if (controllerFactory.LogFileWriter != null) { controllerFactory.LogFileWriter.ExceptionToLogFile(e); } // Call Finish, which should make sure to close down properly, and release any licences. if (controller != null) { try { controller.Finish(); } catch { } } // Rethrow exception throw; } }
public void SetMaxNumberOfThreads(IMike1DController controller) { Mike1DController hdController = controller as Mike1DController; if (hdController != null) { hdController.MaxNumberOfThreadsUser = 1; } }
/// <summary> /// Example of how to add a cross section after loading a complete /// setup, and running the simulation with the new cross section /// included. It is not necessary to save the modified cross sections /// to file before running. /// </summary> /// <param name="setupFilepath">A .sim11 setup path</param> public static void AddCrossSectionAndRun(string setupFilepath) { Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); IMike1DController controller = null; try { // creates a new Mike 1D controller and load the setup Diagnostics diagnostics = new Diagnostics("My Diagnostics"); controller = controllerFactory.OpenAndCreate(Connection.Create(setupFilepath), diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Loading errors, aborting"); } // Add an additional cross section AddCrossSection(controller.Mike1DData.CrossSections); // Change the output file name, so we can compare results with/without change controller.Mike1DData.ResultSpecifications[0].Connection.FilePath.FileNameWithoutExtension += "-csAdd"; IDiagnostics validated = controller.Validate(); if (validated.ErrorCountRecursive > 0) { throw new Exception("Validation errors, aborting"); } // run the simulation with the new cross section included controller.Initialize(diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Initialization errors, aborting"); } controller.Prepare(); controller.Run(); controller.Finish(); } catch (Exception e) { // Write exception to log file if (controllerFactory.LogFileWriter != null) { controllerFactory.LogFileWriter.ExceptionToLogFile(e); } // Call Finish, which should make sure to close down properly, and release any licences. if (controller != null) { try { controller.Finish(); } catch { } } // Rethrow exception throw; } }
public void UserStructureSaveData() { string setupFilePath = Path.Combine(ExampleBase.ExampleRoot, @"UnitFlow\UnitFlowStruc.sim11"); Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(setupFilePath), diagnostics); // Add custom assembly and type - required for the standalong engine to load it again. controller.Mike1DData.CustomTypes.AssemblyFiles.Add(@"..\..\DHI.Mike1D.Examples\bin\Debug\DHI.Mike1D.Examples.dll"); controller.Mike1DData.CustomTypes.Add(typeof(MyStructure)); // Save to .m1dx file controller.Mike1DData.Connection.FilePath.Extension = ".m1dx"; controller.Mike1DData.Connection.FilePath.FileNameWithoutExtension = "UnitFlowStruc-2"; controller.Mike1DData.Connection.BridgeName = "m1dx"; controller.Mike1DData.ResultSpecifications.ForEach(rs => rs.Connection.FilePath.FileNameWithoutExtension += "-2"); Mike1DBridge.Save(controller.Mike1DData); }
public void CreateSumAllCatchment(IMike1DController controller) { Mike1DData mike1DData = controller.Mike1DData; sumTotalRunoffCatchment = new CatchmentCombined("SumAllCatchments") { ScaleByArea = false, Area = 1, }; double minTimestep = double.MaxValue; double maxTimestep = double.MinValue; foreach (ICatchment catchment in mike1DData.RainfallRunoffData.Catchments) { if (!(catchment is CatchmentCombined)) { sumTotalRunoffCatchment.AddNewCatchment(catchment.ModelId, 1.0); minTimestep = System.Math.Min(minTimestep, catchment.TimeStep.TotalSeconds); maxTimestep = System.Math.Max(maxTimestep, catchment.TimeStep.TotalSeconds); } } sumTotalRunoffCatchment.TimeStep = TimeSpan.FromSeconds(minTimestep); mike1DData.RainfallRunoffData.Catchments.Add(sumTotalRunoffCatchment); // Setup writer to write total runoff to csv file writer = new StreamWriter("SumTotalRunoff.csv"); writer.WriteLine("sep=;"); sumTotalRunoffCatchment.PostTimeStepEvent += delegate(DateTime time) { writer.WriteLine("{0};{1}", time.ToString(Util.DateTimeFormatString), runoffGetter().ToString(CultureInfo.InvariantCulture)); }; controller.ControllerEvent += HandleControllerEvent; }
/// <summary> /// (*1) Method that is called when a new controller has been created. /// </summary> /// <param name="sender">The <see cref="IMike1DController"/> that triggered the event</param> /// <param name="controllerCreatedEventArgs">Event arguments</param> private void ControllerCreated(object sender, ControllerCreatedEventArgs controllerCreatedEventArgs) { // Store the controller, and register for the ControllerEvent. _controller = controllerCreatedEventArgs.Controller; _controller.ControllerEvent += ControllerEvent; }
public void Initialize(IMike1DController controller) { // The special event is called with name "SummaryReady", when the summary has been build // and can be modified. controller.SpecialEvent += AddReachLengthColumnToSummary; }
/// <summary> /// This is basically the same example as the SaveNetworkData above, though /// this time the data is stored as a part of the MIKE 1D setup in the /// standard .m1dx file. /// <para> /// This is ONLY recommended for advanced users and use: /// The disadvantage of this approach is that the resulting .m1dx file can only be /// read again by a similar approach, i.e. it WILL ONLY run using the default engine /// when the custom assembly is available, hence it CAN NOT be read by others unless /// they are also provided with the custom assembly. /// </para> /// </summary> /// <param name="vidaaFilepath">Path and name of the Vidaa example</param> /// <param name="customAssemblyFile"></param> public static void SaveComplexNetworkDataWithSetup(string vidaaFilepath, string customAssemblyFile) { { // Creates a new Mike 1D controller and load the setup Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(vidaaFilepath), diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Loading errors, aborting"); } // Create a NetworkData that can store a user defined type, and add some data at some locations NetworkData <MyData> myComplexData = new NetworkData <MyData>() { CanInterpolate = false }; myComplexData.AddValue(new Location("SonderAa", 1034), new MyData() { SValue = "SA", DValue = 1.034 }); myComplexData.AddValue(new Location("LindSkov", 2860), new MyData() { SValue = "LS", DValue = 2.86 }); myComplexData.AddValue("MyNodeId", new MyData() { SValue = "Node", DValue = -42 }); // Set the NetworkData as additional data, using the string "UserData" as key controller.Mike1DData.SetAdditionalData("UserData", myComplexData); controller.Mike1DData.SetAdditionalData("UserData2", new MyData() { SValue = "Upstream", DValue = 15.03 }); // Saving the file again (same filename, new extension) controller.Mike1DData.Connection.BridgeName = "m1dx"; controller.Mike1DData.Connection.FilePath.Extension = ".m1dx"; // We need to add two custom types, in order to store data to .m1dx // The types are found in the DHI.Mike1D.Examples.dll, which cannot be found by the MIKE 1D engine unless an explicit path is provided // Make the path relative to the setup. string relativeAssemblyFile = FilePath.GetRelativeFilePath(customAssemblyFile, Path.GetDirectoryName(Path.GetFullPath(vidaaFilepath))); controller.Mike1DData.CustomTypes.AssemblyFiles.Add(relativeAssemblyFile); controller.Mike1DData.CustomTypes.Add(myComplexData.GetType()); controller.Mike1DData.CustomTypes.Add(typeof(MyData)); Mike1DBridge.Save(controller.Mike1DData); // Close any log files controller.Finish(); } { // Load the setup again, changing the extension to .m1dx FilePath vidaaFile = new FilePath(vidaaFilepath); vidaaFile.Extension = ".m1dx"; Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(vidaaFile), diagnostics); Mike1DData mike1DData = controller.Mike1DData; // Get the additional data using the string "UserData" as key, which we know is a NetworkData with MyData NetworkData <MyData> myComplexData = (NetworkData <MyData>)mike1DData.GetAdditionalData("UserData"); MyData myComplexData2 = (MyData)mike1DData.GetAdditionalData("UserData2"); // Now we can extract our data again MyData v1034; bool ok1034 = myComplexData.GetValue(new Location("SonderAa", 1034), out v1034); MyData v2860; bool ok2860 = myComplexData.GetValue(new Location("LindSkov", 2860), out v2860); MyData vNode; bool okNode = myComplexData.GetValue("MyNodeId", out vNode); controller.Finish(); } }
public void CatchmenUrbanSnowPlowingSetup(IMike1DController controller) { // Listen for ControllerEvent's controller.ControllerEvent += CatchmentUrbanSnowPlowingControllerEvent; }
public void ControllerCreated(IMike1DController controller) { _controller = controller; _controller.ControllerEvent += ControllerOnControllerEvent; }
private void ControllerCreatedEvent(object sender, ControllerCreatedEventArgs controllerCreatedEventArgs) { // - and then the ControllerEvent controller = controllerCreatedEventArgs.Controller; controller.ControllerEvent += Controller_ControllerEvent; }
public void Initialize(IMike1DController controller) { controller.ControllerEvent += ControllerOnControllerEvent; }