// 创建一个 Stopwatch 实例 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 = 0d; var minDemand = 0; // 最小总需水量(m³/h) var maxDemand = 43; // 最大总需水量(m³/h) var calcCount = 10; // 计算次数 var rand = new Random(); var list = new List(calcCount); 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 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++) { helper.InitH(false); var demands = allDemands[i]; for (int nodeIdx = 1; nodeIdx <= nodeCount; nodeIdx++) { double demand = demands.NodeDemands[nodeIdx - 1]; err = helper.SetNodeValue(nodeIdx, Yw.Epanet.eNodeProperty.BaseDemand, demand); if (demand > 1) { } err = helper.GetNodeValue(nodeIdx, Yw.Epanet.eNodeProperty.BaseDemand, out double de); } helper.RunH(out long t); var result = new SimulationResult(); result.TotalDemand = demands.TotalDemand; result.RealDemand = demands.NodeDemands.Sum(); for (int nodeIdx = 1; nodeIdx <= nodeCount; nodeIdx++) { pressure = 0; helper.GetNodeValue(nodeIdx, Yw.Epanet.eNodeProperty.Pressure, out pressure); helper.GetNodeValue(nodeIdx, Yw.Epanet.eNodeProperty.BaseDemand, out double de); result.UpdateMaxPressure(nodeIdx, pressure); } helper.NextH(out long tstep); list.Add(result); } helper.Close(); } 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 PressuresDict { get; set; } = new(); //public ConcurrentDictionary MaxPressures { get; } = new(); public void UpdateMaxPressure(int nodeId, double pressure) { PressuresDict.Add(nodeId, pressure); //MaxPressures.AddOrUpdate(nodeId, pressure, // (id, old) => Math.Max(old, pressure)); } }