Skip to content

Commit 8a1afc8

Browse files
committed
Fix percental water balance calculation
1 parent ba3387f commit 8a1afc8

1 file changed

Lines changed: 67 additions & 30 deletions

File tree

vignettes/workflow_eisenstadt-2005.Rmd

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,45 @@ dbg = debug)}}), nm = simulation_names)
304304
305305
306306
simulation_results_optimisation <- stats::setNames(lapply(names(simulation_results), function(s_name) {
307-
message(s_name)
308-
309-
wb_element <- simulation_results[[s_name]]$element$water_balance %>%
310-
dplyr::mutate(variable = sprintf("element.%s_", variable),
311-
value_percent = round(100*abs(value)/abs(min(value)), 2)) %>%
312-
dplyr::select(- value) %>%
313-
tidyr::pivot_wider(names_from = "variable", values_from = "value_percent")
314-
315-
wb_connectedarea <- simulation_results[[s_name]]$connected_area$water_balance %>%
316-
dplyr::mutate(variable = sprintf("connectedarea.%s_", variable),
317-
value_percent = round(100*abs(value)/abs(max(value)), 2)) %>%
318-
dplyr::select(- value) %>%
319-
tidyr::pivot_wider(names_from = "variable", values_from = "value_percent")
307+
308+
# --- element: Prozent korrekt auf (Regen + Zufluss aus Verschaltungen) ----
309+
wb_el_raw <- simulation_results[[s_name]]$element$water_balance
310+
311+
# denom = Regen + |Verschaltungen| (beide skalar aus der WB ziehen)
312+
denom_element <- wb_el_raw %>%
313+
tidyr::pivot_wider(names_from = variable, values_from = value) %>%
314+
dplyr::transmute(denom = WB_Regen + abs(WB_Oberflaechenablauf_Verschaltungen)) %>%
315+
dplyr::pull(denom)
316+
317+
wb_element <- wb_el_raw %>%
318+
dplyr::mutate(
319+
variable = sprintf("element.%s_", variable),
320+
value_percent = round(100 * value / denom_element, 2) # <-- SIGN behalten!
321+
) %>%
322+
dplyr::select(-value) %>%
323+
tidyr::pivot_wider(names_from = "variable", values_from = "value_percent")
324+
325+
# --- connected_area: Prozent korrekt (Basis = Regen; keine Rückkopplung) ----
326+
wb_ca_raw <- simulation_results[[s_name]]$connected_area$water_balance
327+
328+
denom_connectedarea <- wb_ca_raw %>%
329+
tidyr::pivot_wider(names_from = variable, values_from = value) %>%
330+
dplyr::transmute(
331+
denom = dplyr::if_else(
332+
!is.na(WB_Regen) & WB_Regen != 0,
333+
WB_Regen,
334+
abs(WB_Oberflaechenablauf_Verschaltungen) # Fallback
335+
)
336+
) %>%
337+
dplyr::pull(denom)
338+
339+
wb_connectedarea <- wb_ca_raw %>%
340+
dplyr::mutate(
341+
variable = sprintf("connectedarea.%s_", variable),
342+
value_percent = round(100 * value / denom_connectedarea, 2) # SIGN behalten
343+
) %>%
344+
dplyr::select(-value) %>%
345+
tidyr::pivot_wider(names_from = "variable", values_from = "value_percent")
320346
321347
xx <- simulation_results[[s_name]]$element$rates %>%
322348
dplyr::filter(variable == "Oberflaechenablauf_Ueberlauf",
@@ -357,7 +383,8 @@ htmlwidgets::saveWidget(DT::datatable(simulation_results_optimisation,
357383
filter = "top",
358384
options = list(pageLength = 25,
359385
autoWidth = TRUE)),
360-
"simulation_results_optimisation.html")
386+
"simulation_results_optimisation.html",
387+
title = "RAINDROP - Solution Space")
361388
362389
### Plot results
363390
@@ -387,30 +414,40 @@ htmlwidgets::saveWidget(
387414
selfcontained = TRUE,
388415
title = "RAINDROP - Main Effects"
389416
)
390-
print(gg)
417+
gg
391418
kwb.utils::finishAndShowPdf(pdff)
392419
393420
394421
pdff <- "simulation_results_optimisation_design-space_mulde-area_vs_parameters.pdf"
395422
kwb.utils::preparePdf(pdfFile = pdff)
396-
for(y in c("mulde_height", "filter_hydraulicconductivity", "storage_height")) {
397-
p <- kwb.raindrop::plot_valid_design_space(param_grid = param_grid,
398-
sim_results = simulation_results_optimisation,
399-
x = "mulde_area",
400-
y = y)
401423
402-
plotly_p <- plotly::ggplotly(p, tooltip = "text")
403-
404-
htmlwidgets::saveWidget(
405-
widget = plotly_p,
406-
file = sprintf("simulation_results_optimisation_design-space_mulde-area_vs_%s.html",
407-
y),
408-
selfcontained = TRUE,
409-
title = sprintf("Design Space: mulde_area vs. %s", y)
410-
)
424+
for (y in c("mulde_height", "filter_hydraulicconductivity", "storage_height")) {
425+
426+
p <- kwb.raindrop::plot_valid_design_space(
427+
param_grid = param_grid,
428+
sim_results = simulation_results_optimisation,
429+
x = "mulde_area",
430+
y = y,
431+
valid_max = 1,
432+
jitter = TRUE,
433+
alpha_mode = "duplicates",
434+
alpha_min = 0.25,
435+
alpha_max = 1,
436+
drop_overflow_gt_valid_max = TRUE,
437+
keep_param_grid_limits = TRUE
438+
)
411439
412-
print(p)
440+
# interaktiv als HTML
441+
plotly_p <- suppressWarnings(plotly::ggplotly(p, tooltip = "text"))
442+
htmlwidgets::saveWidget(
443+
widget = plotly_p,
444+
file = sprintf("simulation_results_optimisation_design-space_mulde-area_vs_%s.html", y),
445+
selfcontained = TRUE,
446+
title = sprintf("Design Space: mulde_area vs. %s", y)
447+
)
413448
449+
# statisch ins PDF (WICHTIG!)
450+
suppressWarnings(print(p))
414451
}
415452
416453
kwb.utils::finishAndShowPdf(pdff)

0 commit comments

Comments
 (0)