|
| 1 | +from datetime import datetime, timedelta |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +from swmm.toolkit import solver, output, shared_enum |
| 5 | + |
| 6 | +DATA_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data") |
| 7 | +INPUT_FILE = os.path.join(DATA_PATH, "test_Example1.inp") |
| 8 | +REPORT_FILE = os.path.join(DATA_PATH, "temp_align.rpt") |
| 9 | +OUTPUT_FILE = os.path.join(DATA_PATH, "temp_align.out") |
| 10 | + |
| 11 | +REPORT_STEP_SECONDS = 3600 # Example1 |
| 12 | + |
| 13 | + |
| 14 | +def _correct_decimal_digits(t, r): |
| 15 | + """ |
| 16 | + Correct Decimal Digits (CDD) is computed as a bounded form of |
| 17 | + ``-log10(abs(test_value - ref_value))``, which approximates how many |
| 18 | + decimal digits of ``test_value`` agree with ``ref_value``. |
| 19 | + """ |
| 20 | + import math |
| 21 | + |
| 22 | + if t == r: |
| 23 | + return 10.0 |
| 24 | + |
| 25 | + tmp = abs(t - r) |
| 26 | + if tmp < 1.0e-7: |
| 27 | + tmp = 1.0e-7 |
| 28 | + elif tmp > 2.0: |
| 29 | + tmp = 1.0 |
| 30 | + |
| 31 | + tmp = -math.log10(tmp) |
| 32 | + if tmp < 0.0: |
| 33 | + tmp = 0.0 |
| 34 | + |
| 35 | + return tmp |
| 36 | + |
| 37 | + |
| 38 | +def check_cdd_float(test: list[float], ref: list[float], cdd_tol: int) -> bool: |
| 39 | + """ |
| 40 | + Check the minimum number of correct decimal digits (CDD) between two |
| 41 | + float sequences. This function finds the minimum CDD over all element |
| 42 | + pairs in ``test`` and ``ref``, then checks whether ``floor(min_cdd)`` |
| 43 | + is greater than or equal to ``cdd_tol``. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + test : list[float] |
| 48 | + Sequence of test values to be compared. |
| 49 | + ref : list[float] |
| 50 | + Sequence of reference values used as the expected results. |
| 51 | + cdd_tol : int |
| 52 | + Required minimum number of correct decimal digits (integer threshold) |
| 53 | + that the minimum CDD over all pairs must meet or exceed. |
| 54 | + |
| 55 | + Returns |
| 56 | + ------- |
| 57 | + bool |
| 58 | + ``True`` if ``test`` and ``ref`` have the same length and |
| 59 | + ``floor(min_cdd) >= cdd_tol``; ``False`` otherwise (including if the |
| 60 | + sequences differ in length). |
| 61 | + """ |
| 62 | + import math |
| 63 | + |
| 64 | + if len(test) != len(ref): |
| 65 | + return False |
| 66 | + |
| 67 | + min_cdd = 10.0 |
| 68 | + |
| 69 | + for t, r in zip(test, ref): |
| 70 | + tmp = _correct_decimal_digits(t, r) |
| 71 | + |
| 72 | + if tmp < min_cdd: |
| 73 | + min_cdd = tmp |
| 74 | + |
| 75 | + return math.floor(min_cdd) >= cdd_tol |
| 76 | + |
| 77 | + |
| 78 | +def _get_current_datetime(): |
| 79 | + y, m, d, hh, mm, ss = solver.simulation_get_current_datetime() |
| 80 | + return datetime(y, m, d, hh, mm, ss) |
| 81 | + |
| 82 | +def build_link_flow_solver_tuples_aligned(): |
| 83 | + tuples = [] |
| 84 | + solver.swmm_open(INPUT_FILE, REPORT_FILE, OUTPUT_FILE) |
| 85 | + try: |
| 86 | + solver.swmm_start(0) |
| 87 | + # After start callback |
| 88 | + # period_end = _get_current_datetime |
| 89 | + # value = solver.link_get_result(0, shared_enum.LinkResult.FLOW) |
| 90 | + # tuples.append((period_end, value)) |
| 91 | + |
| 92 | + while True: |
| 93 | + # Before step callback |
| 94 | + # |
| 95 | + time_left = solver.swmm_stride(REPORT_STEP_SECONDS) |
| 96 | + # After step callback |
| 97 | + # |
| 98 | + if time_left == 0: |
| 99 | + break |
| 100 | + # Value for the interval that just ended; align to its period-end timestamp |
| 101 | + period_end = _get_current_datetime() - timedelta(seconds=REPORT_STEP_SECONDS) |
| 102 | + value = solver.link_get_result(0, shared_enum.LinkResult.FLOW) |
| 103 | + tuples.append((period_end, value)) |
| 104 | + |
| 105 | + # Before end callback |
| 106 | + period_end = _get_current_datetime() - timedelta(seconds=REPORT_STEP_SECONDS) |
| 107 | + value = solver.link_get_result(0, shared_enum.LinkResult.FLOW) |
| 108 | + tuples.append((period_end, value)) |
| 109 | + |
| 110 | + solver.swmm_end() |
| 111 | + # After end callback |
| 112 | + # |
| 113 | + |
| 114 | + finally: |
| 115 | + solver.swmm_close() |
| 116 | + # After close callback |
| 117 | + # |
| 118 | + return tuples |
| 119 | + |
| 120 | +def build_link_flow_output_tuples(): |
| 121 | + EPOCH_SWMM = datetime(1899, 12, 30) |
| 122 | + h = output.init() |
| 123 | + output.open(h, os.path.join(DATA_PATH, "test_Example1.out")) |
| 124 | + try: |
| 125 | + start_days = output.get_start_date(h) |
| 126 | + rpt = output.get_times(h, shared_enum.Time.REPORT_STEP) |
| 127 | + n = output.get_times(h, shared_enum.Time.NUM_PERIODS) |
| 128 | + start_dt = EPOCH_SWMM + timedelta(days=start_days) |
| 129 | + vals = output.get_link_series(h, 0, shared_enum.LinkAttribute.FLOW_RATE, 0, n - 1) |
| 130 | + tuples = [(start_dt + timedelta(seconds=i * rpt), float(vals[i])) for i in range(n)] |
| 131 | + finally: |
| 132 | + output.close(h) |
| 133 | + return tuples |
| 134 | + |
| 135 | +def test_compare_aligned_series(): |
| 136 | + s = build_link_flow_solver_tuples_aligned() |
| 137 | + o = build_link_flow_output_tuples() |
| 138 | + |
| 139 | + # times must match |
| 140 | + solver_times = [t.strftime("%Y-%m-%d %H:%M:%S") for t, _ in s] |
| 141 | + output_times = [t.strftime("%Y-%m-%d %H:%M:%S") for t, _ in o] |
| 142 | + assert solver_times == output_times, ( |
| 143 | + "Time axes differ.\n" |
| 144 | + f"Solver times: {solver_times[:5]} ...\n" |
| 145 | + f"Output times: {output_times[:5]} ..." |
| 146 | + ) |
| 147 | + |
| 148 | + # values should match within tolerance |
| 149 | + solver_vals = [v for _, v in s] |
| 150 | + output_vals = [v for _, v in o] |
| 151 | + |
| 152 | + assert check_cdd_float(solver_vals, output_vals, 1), ( |
| 153 | + "Solver and output values differ. " |
| 154 | + "See zipped output for details:\n" + |
| 155 | + "\n".join( |
| 156 | + f"{t1.strftime('%Y-%m-%d %H:%M:%S')} | {v1:.6f} || {t2.strftime('%Y-%m-%d %H:%M:%S')} | {v2:.6f} | cdd={cdd(v1, v2):.2f}" |
| 157 | + for (t1, v1), (t2, v2) in list(zip(s, o))[:10] |
| 158 | + ) |
| 159 | + ) |
| 160 | + |
0 commit comments