-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunSimulationForm.cs
593 lines (528 loc) · 22.5 KB
/
RunSimulationForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;
namespace RiverSimulationApplication
{
public partial class RunSimulationForm : Form
{
public RunSimulationForm()
{
InitializeComponent();
}
private RiverSimulationProfile p = RiverSimulationProfile.profile;
private SliderPanel sp = new SliderPanel();
public void SetForm(RiverSimulationProfile profile)
{
p = profile;
}
private BackgroundWorker bw = new BackgroundWorker();
private SimDebugForm simDebugForm = new SimDebugForm();
private void RunSimulationForm_Load(object sender, EventArgs e)
{
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
{
simDebugForm.Show();
simDebugForm.Visible = true;
}
else
{
simDebugForm.Visible = false;
}
bw.DoWork += new DoWorkEventHandler(BwDoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(BwProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BwRunWorkerCompleted);
bw.WorkerReportsProgress = true;
//InitialChart();
this.progTxt.BackColor = Color.Transparent;
//this.progTxt.Parent = progressBar;
this.progressBar.Maximum = 10000;
LoadStatus();
UpdateStatus();
}
private void UpdateStatus()
{
}
private void LoadStatus()
{
maxIterationsNumTxt.Text = p.maxIterationsNum.ToString();
}
private void ResetChart()
{
chart1.Series.SuspendUpdates();
if(chart1.ChartAreas.Count != 0)
{
chart1.Series["DepthD"].Dispose();
chart1.Series["FlowU"].Dispose();
chart1.Series["FlowV"].Dispose();
chart1.ChartAreas["Base"].Dispose();
chart1.Series.Clear();
chart1.ChartAreas.Clear();
}
InitialChart();
chart1.Series.ResumeUpdates();
}
private void InitialChart()
{
chart1.ChartAreas.Add("Base");
chart1.Series.Add("DepthD");
chart1.Series.Add("FlowU");
chart1.Series.Add("FlowV");
ChartArea myArea = chart1.ChartAreas["Base"];
Series mySeriesD = chart1.Series["DepthD"];
Series mySeriesU = chart1.Series["FlowU"];
Series mySeriesV = chart1.Series["FlowV"];
mySeriesD.LegendText = "水深d誤差";
mySeriesU.LegendText = "流速u誤差";
mySeriesV.LegendText = "流速v誤差";
myArea.AxisX.MajorGrid.LineColor = Color.Transparent; // X軸的刻度 縱線
myArea.AxisY.MajorGrid.LineColor = Color.LightGray; // Y軸的刻度 橫線
myArea.AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
myArea.AxisY.IsLabelAutoFit = false;
myArea.AxisY.IsLogarithmic = true;
myArea.AxisY.LogarithmBase = 10.0;
myArea.AxisY.Minimum = 1.0E-6;
myArea.AxisY.Maximum = 1000.0;
myArea.AxisY.Title = "error";
myArea.AxisX.Title = "疊代次數";
myArea.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
myArea.AxisX.IntervalOffsetType = DateTimeIntervalType.Auto;
myArea.AxisX.Minimum = 0.0;
myArea.AxisX.Maximum = 500.0;
// 設定平均值的 Line
if (RiverSimulationProfile.profile.waterModelingConvergenceCriteria2d != 0.0)
{
StripLine lineMean = new StripLine();
lineMean.Text = "收斂值 : " + RiverSimulationProfile.profile.waterModelingConvergenceCriteria2d.ToString();
lineMean.BorderColor = Color.Red; // 線條的顏色
lineMean.BorderDashStyle = ChartDashStyle.Dash;
lineMean.BorderWidth = 1;
lineMean.IntervalOffsetType = DateTimeIntervalType.Auto;
//lineMean.Interval = RiverSimulationProfile.profile.convergenceCriteria2d;
lineMean.IntervalOffset = Math.Log10(RiverSimulationProfile.profile.waterModelingConvergenceCriteria2d);
myArea.AxisY.StripLines.Add(lineMean);
}
// mySeriesD.ChartType = SeriesChartType.Spline; // 曲線圖
mySeriesD.ChartType = SeriesChartType.Line; // 曲線圖
mySeriesD.Color = Color.Blue; // 在圖型上的顏色
mySeriesD.BorderWidth = 1; // 線型的寬度
mySeriesD.ShadowColor = Color.Transparent; // 陰影的顏色
mySeriesD.ShadowOffset = 2; // 陰影位置的角度
mySeriesD.MarkerStyle = MarkerStyle.None; // 標記的樣式 (Circle, Diamond ...)
mySeriesD.IsValueShownAsLabel = false; // 將 Y 值顯示在標記符號旁邊
mySeriesU.ChartType = SeriesChartType.Spline; // 曲線圖
mySeriesU.Color = Color.Orange; // 在圖型上的顏色
mySeriesU.BorderWidth = 1; // 線型的寬度
mySeriesU.ShadowColor = Color.Transparent; // 陰影的顏色
mySeriesU.ShadowOffset = 2; // 陰影位置的角度
mySeriesU.MarkerStyle = MarkerStyle.None; // 標記的樣式 (Circle, Diamond ...)
mySeriesU.IsValueShownAsLabel = false; // 將 Y 值顯示在標記符號旁邊
mySeriesV.ChartType = SeriesChartType.Spline; // 曲線圖
mySeriesV.Color = Color.Green; // 在圖型上的顏色
mySeriesV.BorderWidth = 1; // 線型的寬度
mySeriesV.ShadowColor = Color.Transparent; // 陰影的顏色
mySeriesV.ShadowOffset = 2; // 陰影位置的角度
mySeriesV.MarkerStyle = MarkerStyle.None; // 標記的樣式 (Circle, Diamond ...)
mySeriesV.IsValueShownAsLabel = false; // 將 Y 值顯示在標記符號旁邊
}
private double xStart = 0.0;
private double xScale = 500.0;
private void BwProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressItem pi = (ProgressItem)e.UserState;
simDebugForm.AddResultMessage("\r\n" + pi.msg);
if(pi.t == ProgressItem.Type.ShowConsole)
{
return;
}
this.progressBar.Value = (int)(pi.prog * 100);
this.progTxt.Text = pi.prog.ToString() + "%";
if (this.progressBar.Value == this.progressBar.Maximum)
{
MessageBox.Show("模擬完成");
}
if (pi.t == ProgressItem.Type.ShowProgress)
{
return;
}
if(pi.t == ProgressItem.Type.ShowDivergence)
{
MessageBox.Show("模式發散");
return;
}
if(pi.t == ProgressItem.Type.ShowNotDivergence)
{
MessageBox.Show("到達水理最大疊代次數仍未收歛,停止模擬!");
return;
}
if(pi.x <= 1)
{
return;
}
ChartArea myArea = chart1.ChartAreas["Base"];
//if( pi.x > (myArea.AxisX.Maximum * 0.95))
//{
// myArea.AxisX.Maximum += myArea.AxisX.Maximum;
//}
double dd = 0.000001;
if (pi.z < dd)
{
pi.z += dd;
}
if (pi.u < dd)
{
pi.u += dd;
}
if (pi.v < dd)
{
pi.v += dd;
}
chart1.Series.SuspendUpdates();
if (chart1.Series["DepthD"].Points.Count == 500)
{
chart1.Series["DepthD"].Points.Clear();
chart1.Series["FlowU"].Points.Clear();
chart1.Series["FlowV"].Points.Clear();
xStart += xScale;
myArea.AxisX.Minimum = xStart;
myArea.AxisX.Maximum = xStart + xScale;
}
chart1.Series["DepthD"].Points.AddXY(pi.x, String.Format("{0:0.000000}", pi.z));
chart1.Series["FlowU"].Points.AddXY(pi.x, String.Format("{0:0.000000}", pi.u));
chart1.Series["FlowV"].Points.AddXY(pi.x, String.Format("{0:0.000000}", pi.v));
chart1.Series.ResumeUpdates();
//myArea.AxisY.StripLines[0].IntervalOffset = RiverSimulationProfile.profile.convergenceCriteria2d;
//StripLine ls = myArea.AxisY.StripLines[0];
//ls.IntervalOffset = RiverSimulationProfile.profile.convergenceCriteria2d;
//ls.Text = String.Format("Mean, {0:0.000000}", RiverSimulationProfile.profile.convergenceCriteria2d);
//string ds = "$" + pi.x.ToString() + ", " + pi.z.ToString() + ", " + pi.u.ToString() + ", " + pi.v.ToString() + "*";
//simDebugForm.AddResultMessage("\r\n" + ds);
}
void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled) // 如果非同步作業已被取消...
{
//UpdateProgressBar("非同步作業已被取消。");
}
else // 如果非同步作業順利完成....
{
//UpdateProgressBar("非同步作業順利完成。");
}
EnterSimUI(Status.Ready);
}
private class ProgressItem
{
public double prog;
public int x;
public double z;
public double u;
public double v;
public string msg;
public enum Type
{
ShowConsole,
ShowProgress,
ShowChart,
ShowDivergence,
ShowNotDivergence
};
public Type t;
public ProgressItem()
{
}
public ProgressItem(ProgressItem p)
{
prog = p.prog;
x = p.x;
z = p.z;
u = p.u;
v = p.v;
msg = p.msg;
t = p.t;
}
}
private bool inRunning = false;
private void ParsingResult(string s)
{
ProgressItem pi = new ProgressItem();
pi.msg = s;
if (s.Contains("NUMYY="))
{
inRunning = true;
string s2 = s.Replace("NUMYY=", " ").Replace("&ZMAX=", " ").Replace("&U3XX=", " ").Replace("&VMAX=", " ").Replace("&PROG=", " ");
string[] separators = { " ", "\t" };
string[] words = s2.Split(separators, StringSplitOptions.RemoveEmptyEntries);
pi.t = ProgressItem.Type.ShowProgress;
pi.prog = Convert.ToDouble(words[5]);
pi.x = Convert.ToInt32(words[0]);
pi.z = Convert.ToDouble(words[2]);
pi.u = Convert.ToDouble(words[3]);
pi.v = Convert.ToDouble(words[4]);
bw.ReportProgress(0, new ProgressItem(pi));
}
else if (!inRunning && s.Contains("1NUMXX="))
{
string s2 = s.Replace("1NUMXX=", " ").Replace("|ZMAX=", " ").Replace("|U1XX=", " ").Replace("|VMAX=", " ").Replace("!PROG=", " ");
string[] separators = { " ", "\t" };
string[] words = s2.Split(separators, StringSplitOptions.RemoveEmptyEntries);
const double MaxValue = 1000.0;
pi.t = ProgressItem.Type.ShowChart;
pi.prog = Convert.ToDouble(words[5]);
pi.x = Convert.ToInt32(words[0]);
try
{ pi.z = Convert.ToDouble(words[2]); }
catch
{ pi.z = MaxValue; }
try
{ pi.u = Convert.ToDouble(words[3]); }
catch
{ pi.u = MaxValue; }
try
{ pi.v = Convert.ToDouble(words[4]); }
catch
{ pi.v = MaxValue; }
bw.ReportProgress(0, new ProgressItem(pi));
}
else if(s.Contains("DIVERGENCE"))
{
pi.t = ProgressItem.Type.ShowDivergence;
bw.ReportProgress(0, new ProgressItem(pi));
}
else if(s.Contains("NOT CONVERGED"))
{
pi.t = ProgressItem.Type.ShowNotDivergence;
bw.ReportProgress(0, new ProgressItem(pi));
}
else
{
pi.t = ProgressItem.Type.ShowConsole;
bw.ReportProgress(0, new ProgressItem(pi));
}
}
private Process simProcess = new Process();
void BwDoWork(object sender, DoWorkEventArgs e)
{
if (simDebugForm.runMode == SimDebugForm.RunMode.OutputFile)
{
simProcess.StartInfo.FileName = Environment.CurrentDirectory + "\\DumpOutput.exe";
simProcess.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
simProcess.StartInfo.Arguments = simDebugForm.inputFile + " " + simDebugForm.dataFile;
simProcess.StartInfo.UseShellExecute = false;
simProcess.StartInfo.RedirectStandardOutput = true;
simProcess.StartInfo.CreateNoWindow = true;
simProcess.Start();
}
else if (simDebugForm.runMode == SimDebugForm.RunMode.InputData)
{
simProcess.StartInfo.FileName = Environment.CurrentDirectory + "\\resed.exe";
simProcess.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
simProcess.StartInfo.Arguments = simDebugForm.inputFile + " 123 3D " + simDebugForm.dataFile + " out";
simProcess.StartInfo.UseShellExecute = false;
simProcess.StartInfo.RedirectStandardOutput = true;
simProcess.StartInfo.CreateNoWindow = true;
simProcess.Start();
}
else if (simDebugForm.runMode == SimDebugForm.RunMode.ExternalFile)
{
simProcess.StartInfo.FileName = Program.currentPath + "\\" + simDebugForm.inputFile;
simProcess.StartInfo.WorkingDirectory = Program.currentPath;
simProcess.StartInfo.Arguments = simDebugForm.dataFile;
simProcess.StartInfo.UseShellExecute = false;
simProcess.StartInfo.RedirectStandardOutput = true;
simProcess.StartInfo.CreateNoWindow = true;
simProcess.Start();
}
else if (simDebugForm.runMode == SimDebugForm.RunMode.UseProfile)
{
string resedExe = Program.GetProjectFileWorkingPath() + Program.resedName;
if (!File.Exists(resedExe))
{ //如果沒有主程式則複製一個。
File.Copy(Environment.CurrentDirectory + @"\resed.exe", resedExe);
}
simProcess.StartInfo.FileName = resedExe;
simProcess.StartInfo.WorkingDirectory = Program.GetProjectFileWorkingPath();
//二維水理:resed.exe resed.i 123
//三維水理:resed.exe resed.i 123 3D 3Dinput.dat out
//二維動床:resed.exe resed.i 123 SED sed.dat
//三維動床:resed.exe resed.i 123 3DSED 3Dinput.dat out sed.dat
if(p.Is2DMode() && p.IsWaterModelingMode())
{
simProcess.StartInfo.Arguments = simDebugForm.inputFile + " 123";
}
else if (p.Is3DMode() && p.IsWaterModelingMode())
{
simProcess.StartInfo.Arguments = simDebugForm.inputFile + " 123 3D 3Dinput.dat out";
}
else if (p.Is2DMode() && p.IsMovableBedMode())
{
simProcess.StartInfo.Arguments = simDebugForm.inputFile + " 123 SED sed.dat";
}
else if (p.Is3DMode() && p.IsMovableBedMode())
{
simProcess.StartInfo.Arguments = simDebugForm.inputFile + " 123 3DSED 3Dinput.dat out sed.dat";
}
simProcess.StartInfo.UseShellExecute = false;
simProcess.StartInfo.RedirectStandardOutput = true;
simProcess.StartInfo.CreateNoWindow = true;
using (StreamWriter outfile = new StreamWriter(Program.GetProjectFileWorkingPath() + @"\run.bat"))
{
outfile.Write(resedExe + " " + simProcess.StartInfo.Arguments + "\r\n");
outfile.Close();
}
simProcess.Start();
}
//else
//{
// string strInputFile = "IamReadyNow.i";
// RiverSimulationProfile.profile.GenerateInputFile(Environment.CurrentDirectory + "\\" + strInputFile);
// simProcess.StartInfo.FileName = Environment.CurrentDirectory + "\\resed.exe";
// simProcess.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
// simProcess.StartInfo.Arguments = strInputFile + " 123 3D " + simDebugForm.dataFile + " out";
// simProcess.StartInfo.UseShellExecute = false;
// simProcess.StartInfo.RedirectStandardOutput = true;
// simProcess.StartInfo.CreateNoWindow = true;
// simProcess.Start();
//}
inRunning = false;
StreamReader reader = simProcess.StandardOutput;//截取輸出流
string line = reader.ReadLine();//每次讀取一行
while (!reader.EndOfStream)
{
ParsingResult(line);
line = reader.ReadLine();
}
ParsingResult(line);
simProcess.WaitForExit();
}
private bool RunSimulationMain()
{
bw.WorkerSupportsCancellation = true;
bw.RunWorkerAsync(0);
return true;
}
private enum Status
{
Ready,
Running,
Pause,
}
private bool DoConvert()
{
if (!ControllerUtility.CheckConvertInt32(ref p.maxIterationsNum, maxIterationsNumTxt, "請輸入正確的水理最大疊代次數!", ControllerUtility.CheckType.GreaterThanZero))
{
return false;
}
return true;
}
private Status status = Status.Ready;
private void startBtn_Click(object sender, EventArgs e)
{
if (status == Status.Ready)
{
if (!DoConvert())
{
return;
}
if (p != null)
{
//string tempSave = Program.projectFolder + Program.tempSaveName;
//RiverSimulationProfile.SerializeBinary(p, tempSave);
FunctionlUtility.SaveProject(p);
}
p.GenerateInputFile(Program.GetProjectFileWorkingPath() + @"/resed.i");
if (p.IsMovableBedMode())
{
p.GenerateSedFile(Program.GetProjectFileWorkingPath() + @"/sed.dat");
}
if(p.Is3DMode())
{
p.Generate3dFile(Program.GetProjectFileWorkingPath() + @"/3Dinput.dat");
}
if (p.groundsillWorkSet)
{
p.GenerateStructureFile(p.groundsillWorkSets, Program.GetProjectFileWorkingPath() + @"/3.txt");
}
if (p.sedimentationWeirSet)
{
p.GenerateStructureFile(p.sedimentationWeirSets, Program.GetProjectFileWorkingPath() + @"/4.txt");
}
if (p.IsMovableBedMode() && p.quayStableAnalysisFunction)
{
p.GenerateQuayWallFile(Program.GetProjectFileWorkingPath() + @"/BANKINFO.txt");
}
ResetChart();
RunSimulationMain();
EnterSimUI(Status.Running);
}
else if (status == Status.Running)
{
if (!simProcess.HasExited)
{
Utility.Suspend(simProcess);
}
//timer1.Stop();
EnterSimUI(Status.Pause);
}
else if (status == Status.Pause)
{
if (!simProcess.HasExited)
{
Utility.Resume(simProcess);
}
EnterSimUI(Status.Running);
}
}
private void stopBtn_Click(object sender, EventArgs e)
{
if (status == Status.Running || status == Status.Pause)
{
if (!simProcess.HasExited)
{
simProcess.Kill();
}
progressBar.Value = 0;
EnterSimUI(Status.Ready);
}
}
void EnterSimUI(Status s)
{
if(s==Status.Ready)
{
startBtn.Text = "開始模擬";
startBtn.Enabled = true;
stopBtn.Enabled = false;
stopFlagChk.Enabled = true;
}
else if (s == Status.Running)
{
startBtn.Text = "暫停模擬";
startBtn.Enabled = true;
stopBtn.Enabled = true;
stopFlagChk.Enabled = false;
}
else if(s == Status.Pause)
{
startBtn.Text = "繼續模擬";
startBtn.Enabled = true;
stopBtn.Enabled = true;
stopFlagChk.Enabled = false;
}
status = s;
}
private void RunSimulationForm_FormClosed(object sender, FormClosedEventArgs e)
{
simDebugForm.Hide();
}
private void stopFlagChk_CheckedChanged(object sender, EventArgs e)
{
bool chk = (sender as CheckBox).Checked;
p.stopFlah = chk;
}
private void maxIterationsNumTxt_TextChanged(object sender, EventArgs e)
{
}
}
}