예제 #1
0
        private void textSimulationDuration_Validating(object sender, CancelEventArgs e)
        {
            double val = Utilities.GetHour(textSimulationDuration.Text);

            if (double.IsNaN(val))
            {
                e.Cancel = true;
            }
        }
예제 #2
0
        private void textReportStart_Validating(object sender, CancelEventArgs e)
        {
            double val = Utilities.GetHour(textReportStart.Text);

            if (double.IsNaN(val))
            {
                e.Cancel = true;
            }
        }
예제 #3
0
        private void runButton_Click(object sender, EventArgs e)
        {
            if (Simulated)
            {
                MessageBox.Show("Simulation in progress! Can not start another one.");
                return;
            }

            if (Utilities.GetHour(textSimulationDuration.Text) < 0)
            {
                MessageBox.Show(
                    "Invalid time expression for simulation duration",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return;
            }

            if (Utilities.GetHour(textReportStart.Text) < 0)
            {
                MessageBox.Show(
                    this,
                    "Invalid time expression for report start time",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return;
            }

            var fdialog = new SaveFileDialog {
                Title    = "Save xlsx file",
                Filter   = "Excel 2007 file (xlsx)|*.xlsx",
                FileName = "report_" + Path.GetFileNameWithoutExtension(_fileInp)
            };

            if (fdialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            try {
                RunSimulation(fdialog.FileName);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Visible = false;
        }
예제 #4
0
        private void RunSimulation(string fileName)
        {
            TraceListener simHandler = null;

            Simulated         = true;
            _canselSimulation = false;

            progressBar.Value   = 0;
            progressBar.Maximum = 100;
            progressBar.Minimum = 0;

            try {
                if (showHydraulicSolverEventsCheckBox.Checked)
                {
                    string logFile = Path.Combine(Path.GetDirectoryName(fileName) ?? string.Empty, "hydEvents.log");

                    try {
                        simHandler = new EpanetTraceListener(logFile, false);
                        simHandler.TraceOutputOptions &= ~TraceOptions.DateTime;
                        log.Listeners.Add(simHandler);
                    }
                    catch (IOException ex) {
                        log.Error(ex);
                    }
                }

                int reportPeriod    = ((TimeStep)reportPeriodBox.SelectedItem).Time;
                int reportStartTime = (int)(Utilities.GetHour(textReportStart.Text) * 3600);
                int hydTStep        = ((TimeStep)hydComboBox.SelectedItem).Time;
                int qualTStep       = ((TimeStep)qualComboBox.SelectedItem).Time;
                int durationTime    = (int)(Utilities.GetHour(textSimulationDuration.Text) * 3600);

                _net.RStart   = reportStartTime;
                _net.RStep    = reportPeriod;
                _net.HStep    = hydTStep;
                _net.Duration = durationTime;
                _net.QStep    = qualTStep;

                statusLabel.Text = "Simulating hydraulics";

                try {
                    _hydSim = new HydraulicSim(_net, log);

                    RunThread(
                        () => _hydSim.Simulate("hydFile.bin"),
                        10,
                        30,
                        () => _hydSim.Htime,
                        () => _hydSim.Htime / (double)_net.Duration);
                }
                catch (ENException ex) {
                    if (ex.Code == ErrorCode.Err1000)
                    {
                        throw new ThreadInterruptedException();
                    }

                    MessageBox.Show(
                        ex.Message + "\nCheck epanet.log for detailed error description",
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);

                    return;
                }

                if (_canselSimulation)
                {
                    return;
                }

                if (_fileMsx != null && qualityMSXCheckBox.Checked)
                {
                    statusLabel.Text = "Simulating MSX";

                    try {
                        // reload MSX
                        _netMsx = new EpanetMSX(_epanetTk);
                        _netMsx.Load(_fileMsx);

                        _netMsx.Network.Rstep  = reportPeriod;
                        _netMsx.Network.Qstep  = qualTStep;
                        _netMsx.Network.Rstart = reportStartTime;
                        _netMsx.Network.Dur    = durationTime;

                        _epanetTk.Open("hydFile.bin");

                        RunThread(
                            () => _netMsx.Run("msxFile.bin"),
                            30,
                            50,
                            () => _netMsx.QTime,
                            () => _netMsx.QTime / (double)_net.Duration);
                    }
                    catch (IOException) {}
                    catch (ENException) {
                        throw new ThreadInterruptedException();
                    }
                    finally {
                        _epanetTk.Close();
                    }

                    // netMSX.getReport().MSXrpt_write(new "msxFile.bin");
                }

                if (_canselSimulation)
                {
                    return;
                }

                if (qualityCheckBox.Checked)
                {
                    try {
                        QualitySim qSim = new QualitySim(_net, log);
                        statusLabel.Text = "Simulating Quality";

                        RunThread(
                            () => qSim.Simulate("hydFile.bin", "qualFile.bin"),
                            30,
                            50,
                            () => qSim.Qtime,
                            () => qSim.Qtime / (double)_net.Duration);
                    }
                    catch (ENException ex) {
                        MessageBox.Show(
                            ex.Message + "\nCheck epanet.log for detailed error description",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                    }
                    catch (IOException e1) {
                        Debug.Print(e1.ToString());
                    }
                }

                if (_canselSimulation)
                {
                    return;
                }

                progressBar.Value = 50;
                statusLabel.Text  = "Writting report";

                _gen = new ReportGenerator(fileName);

                //log

                log.Information("Starting report write");
                if (showSummaryCheckBox.Checked)
                {
                    _gen.WriteSummary(_fileInp, _net, _fileMsx, _netMsx);
                }

                if (_canselSimulation)
                {
                    return;
                }

                if (transposeResultsCheckBox.Checked)
                {
                    _gen.TransposedMode = true;
                }

                if (hydraulicsCheckBox.Checked)
                {
                    // Write hydraulic spreadsheets
                    bool[] values = new bool[ReportGenerator.HydVariable.Values.Length];
                    for (int i = 0; i < ReportGenerator.HydVariable.Values.Length; i++)
                    {
                        values[i] = hydVariables.GetItemChecked(i);
                    }

                    statusLabel.Text = "Writing hydraulic report";

                    RunThread(
                        () => _gen.CreateHydReport("hydFile.bin", _net, values),
                        50,
                        60,
                        () => _gen.Rtime,
                        () => (_gen.Rtime - _net.RStart) / (double)_net.Duration);
                }

                if (_canselSimulation)
                {
                    return;
                }

                if (qualityCheckBox.Checked)
                {
                    statusLabel.Text = "Writing quality report";

                    bool nodes = qualityVariables.GetItemChecked(0);
                    bool links = qualityVariables.GetItemChecked(1);

                    RunThread(
                        () => _gen.CreateQualReport("qualFile.bin", _net, nodes, links),
                        60,
                        70,
                        () => _gen.Rtime,
                        () => (_gen.Rtime - _net.RStart) / (double)_net.Duration);
                }

                if (_canselSimulation)
                {
                    return;
                }

                // Write MSX quality spreadsheets
                if (_fileMsx != null && qualityMSXCheckBox.Checked)
                {
                    bool[] valuesMsx = new bool[speciesCheckList.Items.Count];
                    for (int i = 0; i < speciesCheckList.Items.Count; i++)
                    {
                        valuesMsx[i] = speciesCheckList.GetItemChecked(i);
                    }

                    _gen.CreateMsxReport(
                        "msxFile.bin",
                        _net,
                        _netMsx,
                        _epanetTk,
                        valuesMsx);

                    RunThread(
                        () =>
                        _gen.CreateMsxReport(
                            "msxFile.bin",
                            _net,
                            _netMsx,
                            _epanetTk,
                            valuesMsx),
                        70,
                        80,
                        () => _netMsx.QTime,
                        () => (_gen.Rtime - _net.RStart) / (double)_net.Duration);
                }

                if (_canselSimulation)
                {
                    return;
                }

                statusLabel.Text = "Writing workbook";
                _gen.WriteWorksheet();
                log.Information("Ending report write");
            }
            catch (ThreadInterruptedException) {
                log.Warning(0, "Simulation aborted!");
            }
            finally {
                if (simHandler != null)
                {
                    log.Listeners.Remove(simHandler);
                    simHandler.Close();
                }

                Simulated = false;
            }
        }