Skip to content

Commit 214708b

Browse files
committed
Add CORRECT_TO_SWALE_MAX
1 parent a027f64 commit 214708b

1 file changed

Lines changed: 37 additions & 25 deletions

File tree

tests/testthat/test-function-run_rabimo_with_measures.R

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@
8888
)
8989
)
9090

91-
SAFETY_FACTOR <- 0.999
92-
RUN <- kwb.rabimo::run_rabimo_with_measures
93-
RUN_OLD <- function(...) RUN(..., old_version = TRUE, silent = TRUE)
91+
SAFETY_FACTOR <- 0.9999
92+
RUN_NEW <- function(...) kwb.rabimo::run_rabimo_with_measures(..., silent = TRUE)
93+
RUN_OLD <- function(...) RUN_NEW(..., old_version = TRUE)
9494
GET_MAX <- function(x) lapply(kwb.rabimo:::get_measure_stats(x), `[[`, "max")
9595
APPLY_MEASURES <- kwb.rabimo:::apply_measures_to_blocks
9696
DATASETS <- lapply(
@@ -106,64 +106,76 @@
106106
x[[element]] <- x[[element]] + 0.01
107107
x
108108
}
109+
CORRECT_TO_SWALE_MAX <- function(m, blocks) {
110+
m$to_swale <- NA
111+
m$to_swale <- GET_MAX(APPLY_MEASURES(blocks, m))$to_swale
112+
m
113+
}
109114
}
110115

111116
test_that("run_rabimo_with_measures(old_version = TRUE) works", {
112117

113-
expect_error(RUN())
118+
expect_error(RUN_OLD())
119+
expect_error(RUN_NEW())
114120

115121
for (blocks in DATASETS) {
116122

117123
#blocks <- DATASETS$d2020
118124
m_max <- as.list(SAFETY_FACTOR * unlist(GET_MAX(blocks)))
119125

126+
# The maximum values were ok in the old version
120127
expect_no_error(result <- RUN_OLD(blocks, measures = m_max))
121128
expect_true(all(result$surface_runoff == 0))
122-
129+
130+
# Exceeding any maximum value results in an error
123131
expect_error(RUN_OLD(blocks, measures = ADD_DELTA(m_max, "green_roof")))
124132
expect_error(RUN_OLD(blocks, measures = ADD_DELTA(m_max, "unpaved")))
125133
expect_error(RUN_OLD(blocks, measures = ADD_DELTA(m_max, "to_swale")))
126134

135+
# The maximum values lead to an error in the new version because after
136+
# maximum unpaving there is nothing left to be connected to swales
137+
expect_error(expect_warning(RUN_NEW(blocks, measures = m_max)))
138+
139+
# However, we can recalculate the maximum "to_swale"
140+
expect_no_error(
141+
result <- RUN_NEW(blocks, measures = CORRECT_TO_SWALE_MAX(m_max, blocks))
142+
)
143+
expect_true(all(result$runoff < 0.1))
144+
127145
} # end of for (data in DATASETS)
128146

129147
measures <- list(green_roof = 0.009, to_swale = 0, unpaved = 0.3)
130148
expect_no_error(RUN_OLD(FEATURES, measures = measures))
131149
expect_error(RUN_OLD(FEATURES, measures = ADD_DELTA(measures, "green_roof")))
132150

151+
expect_no_error(RUN_NEW(FEATURES, measures))
152+
expect_error(RUN_NEW(FEATURES, ADD_DELTA(measures, "green_roof")))
153+
133154
})
134155

135156
test_that("Full connection to swales results in zero runoff", {
136157

137-
run <- function(blocks, measures) {
138-
kwb.rabimo::run_rabimo_with_measures(
139-
blocks = blocks,
140-
measures = measures,
141-
config = kwb.rabimo::rabimo_inputs_2025$config,
142-
silent = TRUE
143-
)
144-
}
145-
158+
CONFIG <- kwb.rabimo::rabimo_inputs_2025$config
159+
146160
# different versions of sealed = 0.3
147161
blocks <- kwb.rabimo::generate_rabimo_area(
148162
code = as.character(1:3),
149163
roof = c(0.0, 0.1, 0.2),
150164
pvd = c(0.3, 0.2, 0.1)
151165
)
152166

167+
check_result <- function(result) {
168+
expect_true(all(result$runoff == 0))
169+
}
170+
153171
measures <- list(green_roof = NA, unpaved = NA, to_swale = 0.3)
154-
result <- run(blocks, measures)
155-
expect_true(all(result$runoff == 0))
172+
result <- RUN_NEW(blocks, measures, config = CONFIG)
173+
check_result(result)
156174

157175
# max. green_roof = mean(roof) = 0.1
158176
# max. unpaved = mean(1 - roof) = 0.9
159-
m_max <- GET_MAX(blocks)
160177
# correct max. to_swale
161-
m_max$to_swale <- GET_MAX(
162-
APPLY_MEASURES(blocks, global_share_unpaved = m_max$unpaved)
163-
)$to_swale
164-
165-
measures <- list(green_roof = 0.1, unpaved = 0.9, to_swale = 0.1)
166-
result <- run(blocks, measures)
167-
expect_true(all(result$runoff == 0))
168-
178+
m_max <- CORRECT_TO_SWALE_MAX(GET_MAX(blocks), blocks)
179+
result <- RUN_NEW(blocks, m_max, config = CONFIG)
180+
check_result(result)
169181
})

0 commit comments

Comments
 (0)