///<summary>Finds length of next time step & updates tank levels and rule-based contol actions.</summary> protected virtual long NextHyd() { long hydstep = 0; if (_simulationOutput != null) { AwareStep.Write(_simulationOutput, this, Htime); } if (Htime < net.Duration) { hydstep = TimeStep(); } if (net.Duration == 0) { SimulationPump.StepEnergy(net, _epat, _pumps, Htime, 0); } else if (Htime < net.Duration) { SimulationPump.StepEnergy(net, _epat, _pumps, Htime, hydstep); } if (Htime < net.Duration) { Htime += hydstep; if (Htime >= Rtime) { Rtime += net.RStep; } } return(hydstep); }
public long SimulateSingleStep() { if (!_running) { _running = true; } if (!RunHyd()) { _running = false; return(0); } long hydstep = 0; if (Htime < net.Duration) { hydstep = TimeStep(); } if (net.Duration == 0) { SimulationPump.StepEnergy(net, _epat, _pumps, Htime, 0); } else if (Htime < net.Duration) { SimulationPump.StepEnergy(net, _epat, _pumps, Htime, hydstep); } if (Htime < net.Duration) { Htime += hydstep; if (Htime >= Rtime) { Rtime += net.RStep; } } long tstep = hydstep; if (!_running && tstep > 0) { _running = false; return(0); } if (_running && tstep > 0) { return(tstep); } else { _running = false; return(0); } }
///<summary>Init hydraulic simulation, preparing the linear solver and the hydraulic structures wrappers.</summary> /// <param name="net">Hydraulic network reference.</param> /// <param name="log">Logger reference.</param> public HydraulicSim(EpanetNetwork net, TraceSource log) { _running = false; _logger = log; // this.CreateSimulationNetwork(net); _nodes = new SimulationNode[net.Nodes.Count]; _links = new SimulationLink[net.Links.Count]; _pumps = new List <SimulationPump>(); _tanks = new List <SimulationTank>(); _junctions = new List <SimulationNode>(); _valves = new List <SimulationValve>(); var nodesById = new Dictionary <string, SimulationNode>(net.Nodes.Count); for (int i = 0; i < net.Nodes.Count; i++) { SimulationNode node; var networkNode = net.Nodes[i]; if (networkNode.Type == NodeType.JUNC) { node = new SimulationNode(networkNode, i); _junctions.Add(node); } else { node = new SimulationTank(networkNode, i); _tanks.Add((SimulationTank)node); } _nodes[i] = node; nodesById[node.Id] = node; } for (int i = 0; i < net.Links.Count; i++) { SimulationLink link; var networkLink = net.Links[i]; if (networkLink is Valve) { var valve = new SimulationValve(nodesById, networkLink, i); _valves.Add(valve); link = valve; } else if (networkLink is Pump) { var pump = new SimulationPump(nodesById, networkLink, i); _pumps.Add(pump); link = pump; } else { link = new SimulationLink(nodesById, networkLink, i); } _links[i] = link; } _rules = net.Rules.Select(r => new SimulationRule(r, _links, _nodes)).ToArray(); _curves = net.Curves.ToArray(); _controls = net.Controls.Select(x => new SimulationControl(_nodes, _links, x)).ToArray(); this.net = net; _epat = net.GetPattern(this.net.EPatId); _smat = new SparseMatrix(_nodes, _links, _junctions.Count); _lsv = new LsVariables(_nodes.Length, _smat.CoeffsCount); Htime = 0; switch (this.net.FormFlag) { case FormType.HW: _pHlModel = PipeHeadModelCalculators.HwModelCalculator; break; case FormType.DW: _pHlModel = PipeHeadModelCalculators.DwModelCalculator; break; case FormType.CM: _pHlModel = PipeHeadModelCalculators.CmModelCalculator; break; } foreach (SimulationLink link in _links) { link.InitLinkFlow(); } foreach (SimulationNode node in _junctions) { if (node.Ke > 0.0) { node.SimEmitter = 1.0; } } foreach (SimulationLink link in _links) { if ((link.Type == LinkType.PRV || link.Type == LinkType.PSV || link.Type == LinkType.FCV) && !double.IsNaN(link.Roughness)) { link.SimStatus = StatType.ACTIVE; } if (link.SimStatus <= StatType.CLOSED) { link.SimFlow = Constants.QZERO; } else if (Math.Abs(link.SimFlow) <= Constants.QZERO) { link.InitLinkFlow(link.SimStatus, link.SimSetting); } link.SimOldStatus = link.SimStatus; } Htime = 0; Rtime = this.net.RStep; }