8888 )
8989 )
9090
91+ # FEATURES
9192 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 )
93+ RUN <- function (... ) kwb.rabimo :: run_rabimo_with_measures(... , silent = TRUE )
9494 GET_MAX <- function (x ) lapply(kwb.rabimo ::: get_measure_stats(x ), `[[` , " max" )
9595 APPLY_MEASURES <- kwb.rabimo ::: apply_measures_to_blocks
96- DATASETS <- lapply(
97- X = list (
98- d2020 = kwb.rabimo :: rabimo_inputs_2020 $ data ,
99- d2025 = kwb.rabimo :: rabimo_inputs_2025 $ data
100- ),
101- FUN = function (df ) {
102- df [sample(seq_len(nrow(df )), 10L ), ]
103- }
104- )
10596 ADD_DELTA <- function (x , element , delta ) {
10697 x [[element ]] <- x [[element ]] + 0.01
10798 x
115106
116107test_that(" run_rabimo_with_measures(old_version = TRUE) works" , {
117108
118- expect_error(RUN_OLD())
119- expect_error(RUN_NEW())
109+ expect_error(RUN())
110+
111+ sample_size <- 100L
120112
121- for (blocks in DATASETS ) {
113+ for (seed in sample( 1e10 , 5 ) ) {
122114
123- # blocks <- DATASETS$d2020
124- m_max <- as.list( SAFETY_FACTOR * unlist(GET_MAX( blocks ) ))
115+ # seed <- seeds[1L]
116+ writeLines(paste( " seed: " , seed ))
125117
126- # The maximum values were ok in the old version
127- expect_no_error(result <- RUN_OLD(blocks , measures = m_max ))
128- expect_true(all(result $ surface_runoff == 0 ))
129-
130- # Exceeding any maximum value results in an error
131- expect_error(RUN_OLD(blocks , measures = ADD_DELTA(m_max , " green_roof" )))
132- expect_error(RUN_OLD(blocks , measures = ADD_DELTA(m_max , " unpaved" )))
133- expect_error(RUN_OLD(blocks , measures = ADD_DELTA(m_max , " to_swale" )))
118+ DATASETS <- lapply(
119+ X = list (
120+ d2020 = kwb.rabimo :: rabimo_inputs_2020 $ data ,
121+ d2025 = kwb.rabimo :: rabimo_inputs_2025 $ data
122+ ),
123+ FUN = function (df ) {
124+ df [sample(seq_len(nrow(df )), sample_size ), ]
125+ }
126+ )
134127
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 )))
128+ for (blocks in DATASETS ) {
129+
130+ # blocks <- DATASETS$d2025
131+ m_max_old <- as.list(SAFETY_FACTOR * unlist(GET_MAX(blocks )))
132+ m_max_new <- CORRECT_TO_SWALE_MAX(m_max_old , blocks )
138133
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 ))
134+ # The maximum values lead to an error in the new version because after
135+ # maximum unpaving there is nothing left to be connected to swales
136+ expect_error(suppressWarnings(RUN(blocks , measures = m_max_old )))
137+
138+ # However, with the corrected maximum value for "to_swale" it works
139+ # The new version should not produce runoff with the well-calculated values
140+ expect_no_error(suppressWarnings(result <- RUN(blocks , measures = m_max_new )))
144141
145- } # end of for (data in DATASETS)
146-
142+ expect_true(all(result $ runoff == 0 ))
143+ # expect_true(all(result$runoff < 0.1))
144+
145+ # Exceeding any maximum value results in an error
146+ expect_error(suppressWarnings(RUN(blocks , measures = ADD_DELTA(m_max_new , " green_roof" ))))
147+ expect_error(suppressWarnings(RUN(blocks , measures = ADD_DELTA(m_max_new , " unpaved" ))))
148+ expect_error(suppressWarnings(RUN(blocks , measures = ADD_DELTA(m_max_new , " to_swale" ))))
149+
150+ } # end of for (data in DATASETS)
151+ }
152+
153+ # Testing the features that caused problems as reported by Luise
147154 measures <- list (green_roof = 0.009 , to_swale = 0 , unpaved = 0.3 )
148- expect_no_error(RUN_OLD (FEATURES , measures = measures ))
149- expect_error(RUN_OLD( FEATURES , measures = ADD_DELTA(measures , " green_roof" )))
155+ expect_no_error(RUN (FEATURES , measures = measures ))
156+ expect_error(suppressWarnings(RUN( FEATURES , measures = ADD_DELTA(measures , " green_roof" ) )))
150157
151- expect_no_error(RUN_NEW(FEATURES , measures ))
152- expect_error(RUN_NEW(FEATURES , ADD_DELTA(measures , " green_roof" )))
153-
158+ expect_no_error(RUN(FEATURES , measures ))
159+ expect_error(suppressWarnings(RUN(FEATURES , ADD_DELTA(measures , " green_roof" ))))
154160})
155161
156162test_that(" Full connection to swales results in zero runoff" , {
@@ -164,18 +170,18 @@ test_that("Full connection to swales results in zero runoff", {
164170 pvd = c(0.3 , 0.2 , 0.1 )
165171 )
166172
167- check_result <- function (result ) {
173+ check_for_no_runoff <- function (result ) {
168174 expect_true(all(result $ runoff == 0 ))
169175 }
170176
171177 measures <- list (green_roof = NA , unpaved = NA , to_swale = 0.3 )
172- result <- RUN_NEW (blocks , measures , config = CONFIG )
173- check_result (result )
178+ result <- RUN (blocks , measures , config = CONFIG )
179+ check_for_no_runoff (result )
174180
175181 # max. green_roof = mean(roof) = 0.1
176182 # max. unpaved = mean(1 - roof) = 0.9
177183 # correct max. to_swale
178184 m_max <- CORRECT_TO_SWALE_MAX(GET_MAX(blocks ), blocks )
179- result <- RUN_NEW (blocks , m_max , config = CONFIG )
180- check_result (result )
185+ result <- RUN (blocks , m_max , config = CONFIG )
186+ check_for_no_runoff (result )
181187})
0 commit comments