|
88 | 88 | ) |
89 | 89 | ) |
90 | 90 |
|
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) |
94 | 94 | GET_MAX <- function(x) lapply(kwb.rabimo:::get_measure_stats(x), `[[`, "max") |
95 | 95 | APPLY_MEASURES <- kwb.rabimo:::apply_measures_to_blocks |
96 | 96 | DATASETS <- lapply( |
|
106 | 106 | x[[element]] <- x[[element]] + 0.01 |
107 | 107 | x |
108 | 108 | } |
| 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 | + } |
109 | 114 | } |
110 | 115 |
|
111 | 116 | test_that("run_rabimo_with_measures(old_version = TRUE) works", { |
112 | 117 |
|
113 | | - expect_error(RUN()) |
| 118 | + expect_error(RUN_OLD()) |
| 119 | + expect_error(RUN_NEW()) |
114 | 120 |
|
115 | 121 | for (blocks in DATASETS) { |
116 | 122 |
|
117 | 123 | #blocks <- DATASETS$d2020 |
118 | 124 | m_max <- as.list(SAFETY_FACTOR * unlist(GET_MAX(blocks))) |
119 | 125 |
|
| 126 | + # The maximum values were ok in the old version |
120 | 127 | expect_no_error(result <- RUN_OLD(blocks, measures = m_max)) |
121 | 128 | expect_true(all(result$surface_runoff == 0)) |
122 | | - |
| 129 | + |
| 130 | + # Exceeding any maximum value results in an error |
123 | 131 | expect_error(RUN_OLD(blocks, measures = ADD_DELTA(m_max, "green_roof"))) |
124 | 132 | expect_error(RUN_OLD(blocks, measures = ADD_DELTA(m_max, "unpaved"))) |
125 | 133 | expect_error(RUN_OLD(blocks, measures = ADD_DELTA(m_max, "to_swale"))) |
126 | 134 |
|
| 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 | + |
127 | 145 | } # end of for (data in DATASETS) |
128 | 146 |
|
129 | 147 | measures <- list(green_roof = 0.009, to_swale = 0, unpaved = 0.3) |
130 | 148 | expect_no_error(RUN_OLD(FEATURES, measures = measures)) |
131 | 149 | expect_error(RUN_OLD(FEATURES, measures = ADD_DELTA(measures, "green_roof"))) |
132 | 150 |
|
| 151 | + expect_no_error(RUN_NEW(FEATURES, measures)) |
| 152 | + expect_error(RUN_NEW(FEATURES, ADD_DELTA(measures, "green_roof"))) |
| 153 | + |
133 | 154 | }) |
134 | 155 |
|
135 | 156 | test_that("Full connection to swales results in zero runoff", { |
136 | 157 |
|
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 | + |
146 | 160 | # different versions of sealed = 0.3 |
147 | 161 | blocks <- kwb.rabimo::generate_rabimo_area( |
148 | 162 | code = as.character(1:3), |
149 | 163 | roof = c(0.0, 0.1, 0.2), |
150 | 164 | pvd = c(0.3, 0.2, 0.1) |
151 | 165 | ) |
152 | 166 |
|
| 167 | + check_result <- function(result) { |
| 168 | + expect_true(all(result$runoff == 0)) |
| 169 | + } |
| 170 | + |
153 | 171 | 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) |
156 | 174 |
|
157 | 175 | # max. green_roof = mean(roof) = 0.1 |
158 | 176 | # max. unpaved = mean(1 - roof) = 0.9 |
159 | | - m_max <- GET_MAX(blocks) |
160 | 177 | # 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) |
169 | 181 | }) |
0 commit comments