OpenTTD Source  20241108-master-g80f628063a
flowmapper.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "flowmapper.h"
12 
13 #include "../safeguards.h"
14 
19 void FlowMapper::Run(LinkGraphJob &job) const
20 {
21  for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
22  Node &prev_node = job[node_id];
23  StationID prev = prev_node.base.station;
24  for (const Path *path : prev_node.paths) {
25  uint flow = path->GetFlow();
26  if (flow == 0) break;
27  Node &node = job[path->GetNode()];
28  StationID via = node.base.station;
29  StationID origin = job[path->GetOrigin()].base.station;
30  assert(prev != via && via != origin);
31  /* Mark all of the flow for local consumption at "first". */
32  node.flows.AddFlow(origin, via, flow);
33  if (prev != origin) {
34  /* Pass some of the flow marked for local consumption at "prev" on
35  * to this node. */
36  prev_node.flows.PassOnFlow(origin, via, flow);
37  } else {
38  /* Prev node is origin. Simply add flow. */
39  prev_node.flows.AddFlow(origin, via, flow);
40  }
41  }
42  }
43 
44  for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
45  /* Remove local consumption shares marked as invalid. */
46  Node &node = job[node_id];
47  FlowStatMap &flows = node.flows;
48  flows.FinalizeLocalConsumption(node.base.station);
49  if (this->scale) {
50  /* Scale by time the graph has been running without being compressed. Add 1 to avoid
51  * division by 0 if spawn date == last compression date. This matches
52  * LinkGraph::Monthly(). */
53  auto runtime = job.JoinDate() - job.Settings().recalc_time / CalendarTime::SECONDS_PER_DAY - job.LastCompression() + 1;
54  for (auto &it : flows) {
55  it.second.ScaleToMonthly(runtime.base());
56  }
57  }
58  /* Clear paths. */
59  for (Path *i : node.paths) delete i;
60  node.paths.clear();
61  }
62 }
void Run(LinkGraphJob &job) const override
Map the paths generated by the MCF solver into flows associated with nodes.
Definition: flowmapper.cpp:19
const bool scale
Whether the flow mapper should scale all flows to monthly values.
Definition: flowmapper.h:38
Flow descriptions by origin stations.
Definition: station_base.h:148
void FinalizeLocalConsumption(StationID self)
Subtract invalid flows from locally consumed flow.
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:29
const LinkGraphSettings & Settings() const
Get the link graph settings for this component.
Definition: linkgraphjob.h:232
TimerGameEconomy::Date JoinDate() const
Get the date when the job should be finished.
Definition: linkgraphjob.h:220
TimerGameEconomy::Date LastCompression() const
Get the date when the underlying link graph was last compressed.
Definition: linkgraphjob.h:257
NodeID Size() const
Get the size of the underlying link graph.
Definition: linkgraphjob.h:245
A leg of a path in the link graph.
Definition: linkgraphjob.h:275
NodeID GetOrigin() const
Get the overall origin of the path.
Definition: linkgraphjob.h:286
uint GetFlow() const
Get the flow on this leg.
Definition: linkgraphjob.h:328
NodeID GetNode() const
Get the node this leg passes.
Definition: linkgraphjob.h:283
static constexpr int SECONDS_PER_DAY
approximate seconds per day, not for precise calculations
Declaration of flow mapper; maps paths into flows at nodes.
uint16_t recalc_time
time (in days) for recalculating each link graph component.
Node of the link graph.
Definition: linkgraph.h:90
StationID station
Station ID.
Definition: linkgraph.h:93