ningshuxia
2025-03-27 afbafeecc1325bff849a17fb63b9b2b65b48ddf1
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
// 创建一个 Stopwatch 实例 
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text;
 
 
Console.WriteLine("开始模拟...");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); 
 
var file_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "1897563622226399232.inp");
 
var  pressure = 0d;
var weVlaues = new double[] { 0, 2.5, 5, 7.5, 10 };
var calcCount = 20000;
var list=new List<SimulationResult>(20000); 
using (var helper = new Yw.Epanet.InteropXHelper())
    var err = helper.Open(file_path, "", "");
    err = helper.GetCount(Yw.Epanet.eCountType.Node, out int nodeCount);
    err = helper.OpenH();
 
    var allDemands = GenerateAllDemands(nodeCount, calcCount, weVlaues);
    for (int i = 0; i < calcCount; i++)
    {
        helper.InitH(false);
        for (int nodeIdx = 1; nodeIdx <= nodeCount; nodeIdx++)
        {
            double demand = allDemands[i, nodeIdx - 1];
            err = helper.SetNodeValue(nodeIdx, Yw.Epanet.eNodeProperty.BaseDemand, demand);
        }
 
        helper.RunH(out long t);
 
        var result = new SimulationResult();
        for (int nodeIdx = 1; nodeIdx <= nodeCount; nodeIdx++)
        {
            pressure = 0;
            helper.GetNodeValue(nodeIdx, Yw.Epanet.eNodeProperty.Pressure, out pressure);
            result.UpdateMaxPressure(nodeIdx, pressure);
        }
        helper.NextH(out long tstep);
        list.Add(result);
    }
     
 
    helper.Close();
 
}
 
 
 
stopwatch.Stop();
 
Console.WriteLine("所有模拟已完成。");
Console.WriteLine($"总时间:{stopwatch.Elapsed.TotalSeconds}");
 
var strBuild = new StringBuilder();
foreach (var item in list)
{
    var pressureList= item.MaxPressures.Values.ToList();
    strBuild.AppendLine($"{pressureList.Min():N5},{pressureList.Max():N5}");
}
 
Console.WriteLine(strBuild.ToString());
Console.ReadLine();
Console.ReadKey();
 
 
 
 
  double[,] GenerateAllDemands(int nodeCount, int totalIterations, double[] weValues)
{
 
    double[,] demands = new double[totalIterations, nodeCount];
    Parallel.For(0, totalIterations, iter =>
    {
        var rand = new Random();
        for (int node = 0; node < nodeCount; node++)
        {
            // 遗传算法式选择
            int idx = rand.Next(weValues.Length);
 
            // 20%概率变异
            if (rand.NextDouble() < 0.2)
                idx = (idx + 1) % weValues.Length;
            demands[iter, node] = weValues[idx];
        }
    });
    return demands;
}
 
public class SimulationResult
{
    public ConcurrentDictionary<int, double> MaxPressures { get; } = new();
 
    public void UpdateMaxPressure(int nodeId, double pressure)
    {
        MaxPressures.AddOrUpdate(nodeId, pressure,
            (id, old) => Math.Max(old, pressure));
    }
}