Skip to content

Commit

Permalink
Allow plotting the sankey diagram for single timesteps
Browse files Browse the repository at this point in the history
  • Loading branch information
Bachibouzouk committed Jul 4, 2024
1 parent c4d140c commit fd1ebd2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
38 changes: 28 additions & 10 deletions app/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,8 @@ def graph_costs(
return simulations_results


def graph_sankey(simulation, energy_vector):
def graph_sankey(simulation, energy_vector, timestep=None):
ts = timestep
if isinstance(energy_vector, list) is False:
energy_vector = [energy_vector]
if energy_vector is not None:
Expand All @@ -1395,9 +1396,14 @@ def graph_sankey(simulation, energy_vector):
labels.append(bus_label)
colors.append("blue")

asset_to_bus_names = qs.filter(bus=bus.name, direction="in").values_list(
"asset", "total_flow"
)
if ts is None:
asset_to_bus_names = qs.filter(
bus=bus.name, direction="in"
).values_list("asset", "total_flow")
else:
asset_to_bus_names = qs.filter(
bus=bus.name, direction="in"
).values_list("asset", "flow_data")

for component_label, val in asset_to_bus_names:
# draw link from the component to the bus
Expand All @@ -1408,17 +1414,25 @@ def graph_sankey(simulation, energy_vector):
sources.append(labels.index(component_label))
targets.append(labels.index(bus_label))

if ts is not None:
val = json.loads(val)
val = val[ts]

if component_label in chp_in_flow:
chp_in_flow[component_label]["value"] += val

if val == 0:
val = 1e-6

val = 1e-9
values.append(val)
if ts is None:
bus_to_asset_names = qs.filter(
bus=bus.name, direction="out"
).values_list("asset", "total_flow")
else:
bus_to_asset_names = qs.filter(
bus=bus.name, direction="out"
).values_list("asset", "flow_data")

bus_to_asset_names = qs.filter(bus=bus.name, direction="out").values_list(
"asset", "total_flow"
)
# TODO potentially rename feedin period and consumption period
for component_label, val in bus_to_asset_names:
# draw link from the bus to the component
Expand All @@ -1432,8 +1446,12 @@ def graph_sankey(simulation, energy_vector):
if component_label in chp_in_flow:
chp_in_flow[component_label]["bus"] = bus_label

if ts is not None:
val = json.loads(val)
val = val[ts]

if val == 0:
val = 1e-6
val = 1e-9
values.append(val)

# TODO display the installed capacity, max capacity and optimized_add_capacity on the nodes if applicable
Expand Down
5 changes: 5 additions & 0 deletions app/dashboard/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
scenario_visualize_sankey,
name="scenario_visualize_sankey",
),
re_path(
r"^scenario/results/request_sankey/(?P<scen_id>\d+)?(/(?P<ts>\d+))?$",
scenario_visualize_sankey,
name="scenario_visualize_sankey",
),
re_path(
r"^project/(?P<proj_id>\d+)/scenario/results/request-capacities/(?P<scen_id>\d+)?$",
scenario_visualize_capacities,
Expand Down
11 changes: 8 additions & 3 deletions app/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,13 @@ def scenario_visualize_results(request, proj_id=None, scen_id=None):

topology_data_list = load_scenario_topology_from_db(scen_id)

timestamps = scenario.get_timestamps()
answer = render(
request,
"report/single_scenario.html",
{
"scen_id": scen_id,
"timestamps": timestamps,
"proj_id": proj_id,
"project_list": user_projects,
"scenario_list": user_scenarios,
Expand Down Expand Up @@ -1117,18 +1119,21 @@ def scenario_visualize_costs(request, proj_id, scen_id=None):


# TODO: Sector coupling must be refined (including transformer flows)
def scenario_visualize_sankey(request, scen_id):
def scenario_visualize_sankey(request, scen_id, ts=None):
scenario = get_object_or_404(Scenario, pk=scen_id)
if (scenario.project.user != request.user) and (
scenario.project.viewers.filter(user__email=request.user.email).exists()
is False
):
raise PermissionDenied

if ts is not None:
ts = int(ts)
results_json = report_item_render_to_json(
report_item_id="sankey",
data=REPORT_GRAPHS[GRAPH_SANKEY](
simulation=scenario.simulation, energy_vector=scenario.energy_vectors
simulation=scenario.simulation,
energy_vector=scenario.energy_vectors,
timestep=ts,
),
title="Sankey",
report_item_type=GRAPH_SANKEY,
Expand Down
10 changes: 9 additions & 1 deletion app/templates/report/single_scenario.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
<div class="row">
<div class="col" id="report_items" style="display:flex;flex-direction:column-reverse;">
{% include "report/graph_template.html" with id="sankey" title="Sankey diagram" %}
<select onchange="javascript:scenario_visualize_sankey(scen_id={{scen_id }},ts=this.value)">
<option value="">Aggregated</option>
{% for ts in timestamps %}
<option value="{{ forloop.counter0 }}">{{ ts }}</option>
{% endfor %}
</select>
{% include "report/graph_template.html" with id="capacities" title="Installed and optimized capacities" %}
{% include "report/graph_template.html" with id="stacked_timeseries" title="Stacked timeseries by sector" %}
{% include "report/graph_template.html" with id="all_timeseries" title="All timeseries" %}
Expand All @@ -128,6 +134,8 @@

{% block results_end_body_scripts %}
<script>


document.getElementById('single-scenario-link').classList.add('active');

var dropdownElement = new Choices('#results-scenarios', {
Expand Down Expand Up @@ -272,4 +280,4 @@

</script>
{% endif %}
{% endblock results_end_body_scripts %}
{% endblock results_end_body_scripts %}
10 changes: 8 additions & 2 deletions app/templates/scenario/scenario_results_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,15 @@ <h1 class="header__title">{% translate "Simulation results" %}</h1>
};


function scenario_visualize_sankey(scen_id){
function scenario_visualize_sankey(scen_id, ts=null){
var urlParams = scen_id;
if( ts === null || ts === ""){
}
else{
urlParams = scen_id + "/" + ts;
}
$.ajax({
url: "{% url 'scenario_visualize_sankey' %}" + scen_id,
url: "{% url 'scenario_visualize_sankey' %}" + urlParams,
type: "GET",
success: async (parameters) => {
await graph_type_mapping[parameters.type](parameters.id, parameters);
Expand Down

0 comments on commit fd1ebd2

Please sign in to comment.