/// <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; } }
/// <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; } }