using System.Text;
|
|
namespace Yw.EPAnet
|
{
|
/// <summary>
|
/// 管网计算拓展
|
/// </summary>
|
public static class NetworkCalcuExtensions
|
{
|
/// <summary>
|
/// 计算
|
/// </summary>
|
public static CalcuResult Calcu(this Network network,bool MinorLossPreCalc=true)
|
{
|
var result = new CalcuResult();
|
|
//Null验证
|
if (network == null)
|
{
|
result.Succeed = false;
|
return result;
|
}
|
|
string inpString = null;
|
if (MinorLossPreCalc)
|
{
|
CalcuResult minorLossResult = network.CalcuMinorLoss();
|
if (!minorLossResult.Succeed)
|
{
|
result.Succeed = false;
|
result.FailedList.AddRange(minorLossResult.FailedList);
|
return result;
|
}
|
inpString = network.ToInpString(minorLossResult);
|
}
|
else
|
{
|
inpString = network.ToInpString();
|
}
|
//获取系统临时文件目录,创建inp临时文件
|
var inpFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".inp");
|
|
File.WriteAllText(inpFilePath, inpString);
|
|
//加载管网
|
var epanet = new HydraulicCore(true);
|
var errOpen = epanet.open(inpFilePath, "", "");
|
if (errOpen != 0)
|
{
|
|
string txt = epanet.geterrormsg();
|
|
result.Succeed = false;
|
result.FailedList.Add(new CalcuFailed()
|
{
|
Code = errOpen,
|
Message = $"{txt}"
|
});
|
return result;
|
}
|
|
//水力计算
|
var errCalcu = epanet.solveH();
|
if (errCalcu != 0)
|
{
|
|
string txt = epanet.geterrormsg();
|
result.Succeed = false;
|
result.FailedList.Add(new CalcuFailed()
|
{
|
Code = errCalcu,
|
Message = $"{txt}"
|
});
|
return result;
|
}
|
|
int nodeCount = 0;
|
int linkCount = 0;
|
epanet.getcount((int)eCountType.Node, ref nodeCount);
|
epanet.getcount((int)eCountType.Link, ref linkCount);
|
|
const int MAXID = 31;
|
|
var sb = new StringBuilder(MAXID);
|
|
for (int i = 1; i <= nodeCount; i++)
|
{
|
epanet.getnodeid(i, sb);
|
var arr = new string[] { "Head", "Press", "Demand" }; //System.Enum.GetValues(typeof(HydraulicModel.NodeValueType));
|
var arrnum = new int[] { 10, 11, 9 };
|
var resultNode = new Node()
|
{
|
Id = sb.ToString(),
|
};
|
for (var j = 0; j < arr.Length; j++)
|
{
|
float v = 0;
|
//var t = (EPAcore.Core.NodeValueType)j;
|
epanet.getnodevalue(i, arrnum[j], ref v);
|
switch (arr[j])
|
{
|
case "Head":
|
resultNode.Head = v;
|
break;
|
case "Press":
|
resultNode.Press = v;
|
break;
|
case "Demand":
|
resultNode.Demand = v;
|
break;
|
}
|
}
|
result.NodeList.Add(resultNode);
|
result.NodeDict.Add(resultNode.Id, resultNode);
|
}
|
|
for (int i = 1; i <= linkCount; i++)
|
{
|
epanet.getlinkid(i, sb);
|
//var arr = System.Enum.GetValues(typeof(HydraulicModel.LinkValueType));
|
var arr = new string[] { "Flow", "Velocity", "Headloss" }; //System.Enum.GetValues(typeof(HydraulicModel.NodeValueType));
|
var arrnum = new int[] { 8, 9, 10 };
|
var resultLink = new Link()
|
{
|
Id = sb.ToString(),
|
};
|
for (var j = 0; j < arr.Length; j++)
|
{
|
float v = 0;
|
//var t = (EPAcore.Core.NodeValueType)j;
|
epanet.getlinkvalue(i, arrnum[j], ref v);
|
switch (arr[j])
|
{
|
case "Flow":
|
resultLink.Flow = v;
|
break;
|
case "Velocity":
|
resultLink.Velocity = v;
|
break;
|
case "Headloss":
|
resultLink.Headloss = v;
|
break;
|
}
|
}
|
result.LinkList.Add(resultLink);
|
result.LinkDict.Add(resultLink.Id, resultLink);
|
}
|
|
|
|
|
return result;
|
}
|
|
|
}
|
}
|