OpenTTD Source  12.0-beta2
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.Station();
24  PathList &paths = prev_node.Paths();
25  for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
26  Path *path = *i;
27  uint flow = path->GetFlow();
28  if (flow == 0) break;
29  Node node = job[path->GetNode()];
30  StationID via = node.Station();
31  StationID origin = job[path->GetOrigin()].Station();
32  assert(prev != via && via != origin);
33  /* Mark all of the flow for local consumption at "first". */
34  node.Flows().AddFlow(origin, via, flow);
35  if (prev != origin) {
36  /* Pass some of the flow marked for local consumption at "prev" on
37  * to this node. */
38  prev_node.Flows().PassOnFlow(origin, via, flow);
39  } else {
40  /* Prev node is origin. Simply add flow. */
41  prev_node.Flows().AddFlow(origin, via, flow);
42  }
43  }
44  }
45 
46  for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
47  /* Remove local consumption shares marked as invalid. */
48  Node node = job[node_id];
49  FlowStatMap &flows = node.Flows();
50  flows.FinalizeLocalConsumption(node.Station());
51  if (this->scale) {
52  /* Scale by time the graph has been running without being compressed. Add 1 to avoid
53  * division by 0 if spawn date == last compression date. This matches
54  * LinkGraph::Monthly(). */
55  uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression() + 1;
56  for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
57  i->second.ScaleToMonthly(runtime);
58  }
59  }
60  /* Clear paths. */
61  PathList &paths = node.Paths();
62  for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
63  delete *i;
64  }
65  paths.clear();
66  }
67 }
FlowMapper::Run
virtual void Run(LinkGraphJob &job) const
Map the paths generated by the MCF solver into flows associated with nodes.
Definition: flowmapper.cpp:19
Path
A leg of a path in the link graph.
Definition: linkgraphjob.h:365
LinkGraph::Node
Updatable node class.
Definition: linkgraph.h:380
LinkGraphJob::Settings
const LinkGraphSettings & Settings() const
Get the link graph settings for this component.
Definition: linkgraphjob.h:322
LinkGraphJob
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:30
FlowStatMap::FinalizeLocalConsumption
void FinalizeLocalConsumption(StationID self)
Subtract invalid flows from locally consumed flow.
Definition: station_cmd.cpp:4638
LinkGraphJob::Size
NodeID Size() const
Get the size of the underlying link graph.
Definition: linkgraphjob.h:335
FlowMapper::scale
const bool scale
Whether the flow mapper should scale all flows to monthly values.
Definition: flowmapper.h:42
LinkGraph::NodeWrapper::Station
StationID Station() const
Get ID of station belonging to wrapped node.
Definition: linkgraph.h:165
LinkGraphSettings::recalc_time
uint16 recalc_time
time (in days) for recalculating each link graph component.
Definition: settings_type.h:526
FlowStatMap
Flow descriptions by origin stations.
Definition: station_base.h:149
LinkGraphJob::LastCompression
Date LastCompression() const
Get the date when the underlying link graph was last compressed.
Definition: linkgraphjob.h:347
LinkGraphJob::JoinDate
Date JoinDate() const
Get the date when the job should be finished.
Definition: linkgraphjob.h:310
flowmapper.h
Path::GetOrigin
NodeID GetOrigin() const
Get the overall origin of the path.
Definition: linkgraphjob.h:376
Path::GetFlow
uint GetFlow() const
Get the flow on this leg.
Definition: linkgraphjob.h:418
Path::GetNode
NodeID GetNode() const
Get the node this leg passes.
Definition: linkgraphjob.h:373