/// <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();
            }
        }
Exemple #2
0
        /// <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;
            }
        }
Exemple #3
0
        /// <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);
        }
        /// <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();
            }
        }