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
103
104
105
106
107
108
109
110
111
112
113
using IStation.Epanet;
using IStation.Epanet.Enums;
using System.Diagnostics;
using System.Text;
using static PBS.Console.Test.WaterDistributionSystemHelper;
 
 
 
Console.WriteLine("开始模拟...");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
 
var file_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "1897563622226399232.inp");
 
var pressure = 0f;
var minDemand = 0;   // 最小总需水量(m³/h)
var maxDemand = 43;  // 最大总需水量(m³/h)
var calcCount = 20000;           // 计算次数
var rand = new Random();
 
var list = new List<SimulationResult>(calcCount);
 
var err = EpanetMethods.ENopen(file_path, "", "");
err = EpanetMethods.ENgetcount(CountType.Node, out int nodeCount);
err = EpanetMethods.ENopenH();
 
 
var config = new Config
{
    NodeCount = nodeCount,
    TotalRuns = calcCount,
    MinDemand = minDemand,
    MaxDemand = maxDemand,
    NodeLimits = new()
};
 
for (int nodeIndex = 1; nodeIndex <= nodeCount; nodeIndex++)
{
    config.NodeLimits.Add(new(0, rand.NextDouble() * 5));
}
 
 
var generator = new DemandAllocator(config);
var allDemands = generator.GenerateAllocationMatrix();
 
 
for (int i = 0; i < calcCount; i++)
{
    EpanetMethods.ENinitH(0);
    var demands = allDemands[i];
    for (int nodeIdx = 1; nodeIdx <= nodeCount; nodeIdx++)
    {
        double demand = demands.NodeDemands[nodeIdx - 1];
        err = EpanetMethods.ENsetnodevalue(nodeIdx, NodeValue.BaseDemand, (float)demand);
        err = EpanetMethods.ENgetnodevalue(nodeIdx, NodeValue.BaseDemand, out float de);  
    }
 
    EpanetMethods.ENrunH(out int t);
 
    var result = new SimulationResult();
    result.TotalDemand = demands.TotalDemand;
    result.RealDemand = demands.NodeDemands.Sum();
    for (int nodeIdx = 1; nodeIdx <= nodeCount; nodeIdx++)
    {
        pressure = 0;
        EpanetMethods.ENgetnodevalue(nodeIdx, NodeValue.Pressure, out pressure);
        EpanetMethods.ENgetnodevalue(nodeIdx, NodeValue.Demand, out float de);
        result.UpdateMaxPressure(nodeIdx, pressure);
    }
    EpanetMethods.ENnextH(out int tstep);
    list.Add(result);
}
 
 
EpanetMethods.ENcloseH();
EpanetMethods.ENclose();
 
 
stopwatch.Stop();
 
Console.WriteLine("所有模拟已完成。");
Console.WriteLine($"总时间:{stopwatch.Elapsed.TotalSeconds}");
 
list = list.OrderBy(x => x.TotalDemand).ToList();
var strBuild = new StringBuilder();
foreach (var item in list)
{
    var pressureList = item.PressuresDict.Values.ToList();
    var a = item.PressuresDict.OrderBy(x => x.Value).ToList();
    strBuild.AppendLine($"{item.TotalDemand},{item.RealDemand}///{a[0].Value},{a[0].Key}");
}
 
 
Console.WriteLine(strBuild.ToString());
Console.ReadLine();
Console.ReadKey();
 
 
 
public class SimulationResult
{
    public double TotalDemand { get; set; }
    public double RealDemand { get; set; }
    public Dictionary<int, double> PressuresDict { get; set; } = new();
    //public ConcurrentDictionary<int, double> MaxPressures { get; } = new();
 
    public void UpdateMaxPressure(int nodeId, double pressure)
    {
        PressuresDict.Add(nodeId, pressure);
        //MaxPressures.AddOrUpdate(nodeId, pressure,
        //    (id, old) => Math.Max(old, pressure));
    }
}