|
| 1 | +from unittest import TestCase |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from mpi4py import MPI |
| 6 | + |
| 7 | +from micro_manager.adaptivity.model_adaptivity import ModelAdaptivity |
| 8 | +from micro_manager.micro_manager import MicroManagerCoupling |
| 9 | + |
| 10 | + |
| 11 | +class DummyModelClass: |
| 12 | + def __init__(self, name): |
| 13 | + self.name = name |
| 14 | + |
| 15 | + |
| 16 | +class DummySimulation: |
| 17 | + def __init__(self, name, global_id=0, late_init=False): |
| 18 | + self.name = name |
| 19 | + self._global_id = global_id |
| 20 | + self._state = {"state": name} |
| 21 | + self.attachments = {} |
| 22 | + self.destroyed = False |
| 23 | + self.late_init = late_init |
| 24 | + |
| 25 | + def get_global_id(self): |
| 26 | + return self._global_id |
| 27 | + |
| 28 | + def get_state(self): |
| 29 | + return self._state.copy() |
| 30 | + |
| 31 | + def set_state(self, state): |
| 32 | + self._state = state.copy() |
| 33 | + |
| 34 | + def destroy(self): |
| 35 | + self.destroyed = True |
| 36 | + |
| 37 | + |
| 38 | +class DummyModelManager: |
| 39 | + def __init__(self): |
| 40 | + self.created_instances = [] |
| 41 | + |
| 42 | + def get_instance(self, gid, target_class, *, late_init=False): |
| 43 | + self.created_instances.append( |
| 44 | + { |
| 45 | + "gid": gid, |
| 46 | + "target_class": target_class.name, |
| 47 | + "late_init": late_init, |
| 48 | + } |
| 49 | + ) |
| 50 | + return DummySimulation(target_class.name, gid, late_init=late_init) |
| 51 | + |
| 52 | + |
| 53 | +class TestModelAdaptivity(TestCase): |
| 54 | + def _make_controller(self, switching_func): |
| 55 | + controller = ModelAdaptivity.__new__(ModelAdaptivity) |
| 56 | + controller._switching_func = switching_func |
| 57 | + controller._model_classes = [ |
| 58 | + DummyModelClass("fine"), |
| 59 | + DummyModelClass("coarse"), |
| 60 | + ] |
| 61 | + controller._model_manager = DummyModelManager() |
| 62 | + controller._comm = MPI.COMM_SELF |
| 63 | + controller._logger = MagicMock() |
| 64 | + controller._converged = False |
| 65 | + return controller |
| 66 | + |
| 67 | + def test_check_convergence_ignores_invalid_switch_at_finest_resolution(self): |
| 68 | + """ |
| 69 | + Check that convergence is reached when the switching function requests a |
| 70 | + finer model while the simulation is already using the finest available |
| 71 | + resolution. Such an out-of-range request should be clamped to the |
| 72 | + current resolution and treated as no model change. |
| 73 | + """ |
| 74 | + controller = self._make_controller(lambda resolution, *_: -1) |
| 75 | + |
| 76 | + controller.check_convergence( |
| 77 | + np.array([[0.0, 0.0, 0.0]]), |
| 78 | + 1.0, |
| 79 | + [{}], |
| 80 | + None, |
| 81 | + [DummySimulation("fine")], |
| 82 | + ) |
| 83 | + |
| 84 | + self.assertTrue(controller._converged) |
| 85 | + |
| 86 | + def test_check_convergence_ignores_invalid_switch_at_coarsest_resolution(self): |
| 87 | + """ |
| 88 | + Check that convergence is reached when the switching function requests a |
| 89 | + coarser model while the simulation is already using the coarsest |
| 90 | + available resolution. This guards against endless iterations caused by |
| 91 | + repeated out-of-range coarsening requests. |
| 92 | + """ |
| 93 | + controller = self._make_controller(lambda resolution, *_: 1) |
| 94 | + |
| 95 | + controller.check_convergence( |
| 96 | + np.array([[0.0, 0.0, 0.0]]), |
| 97 | + 1.0, |
| 98 | + [{}], |
| 99 | + None, |
| 100 | + [DummySimulation("coarse")], |
| 101 | + ) |
| 102 | + |
| 103 | + self.assertTrue(controller._converged) |
| 104 | + |
| 105 | + def test_check_convergence_detects_valid_switch(self): |
| 106 | + """ |
| 107 | + Check that convergence is not reported when the switching function |
| 108 | + requests a valid change to another available model resolution. The |
| 109 | + adaptivity loop must continue in this case so the requested switch can |
| 110 | + be applied. |
| 111 | + """ |
| 112 | + controller = self._make_controller(lambda resolution, *_: 1) |
| 113 | + |
| 114 | + controller.check_convergence( |
| 115 | + np.array([[0.0, 0.0, 0.0]]), |
| 116 | + 1.0, |
| 117 | + [{}], |
| 118 | + None, |
| 119 | + [DummySimulation("fine")], |
| 120 | + ) |
| 121 | + |
| 122 | + self.assertFalse(controller._converged) |
| 123 | + |
| 124 | + def test_manager_loop_switches_once_then_exits_on_invalid_boundary_request(self): |
| 125 | + """ |
| 126 | + Reproduce the regression scenario where a model is switched once and |
| 127 | + the switching function then keeps requesting another change beyond the |
| 128 | + available resolution range. The manager should solve with the new model, |
| 129 | + avoid reusing output from the previous model, and stop once the repeated |
| 130 | + boundary request is clamped to a no-op. |
| 131 | + """ |
| 132 | + |
| 133 | + def switching_function(resolution, location, t, input, prev_output): |
| 134 | + if prev_output is None: |
| 135 | + return 0 |
| 136 | + return 1 |
| 137 | + |
| 138 | + controller = self._make_controller(switching_function) |
| 139 | + manager = MicroManagerCoupling.__new__(MicroManagerCoupling) |
| 140 | + manager._model_adaptivity_controller = controller |
| 141 | + manager._is_adaptivity_on = False |
| 142 | + manager._mesh_vertex_coords = np.array([[0.0, 0.0, 0.0]]) |
| 143 | + manager._global_ids_of_local_sims = [0] |
| 144 | + manager._t = 1.0 |
| 145 | + manager._micro_sims = [DummySimulation("fine", global_id=0)] |
| 146 | + |
| 147 | + solve_calls = [] |
| 148 | + |
| 149 | + def solve_variant(micro_sims_input, dt, computed_outputs): |
| 150 | + solve_calls.append( |
| 151 | + { |
| 152 | + "sim_name": manager._micro_sims[0].name, |
| 153 | + "computed_outputs": computed_outputs.copy(), |
| 154 | + } |
| 155 | + ) |
| 156 | + return [{"result": len(solve_calls)}] |
| 157 | + |
| 158 | + result = MicroManagerCoupling._solve_micro_simulations_with_model_adaptivity( |
| 159 | + manager, |
| 160 | + [{"input": 1.0}], |
| 161 | + 0.1, |
| 162 | + solve_variant, |
| 163 | + ) |
| 164 | + |
| 165 | + self.assertEqual(len(solve_calls), 2) |
| 166 | + self.assertEqual(solve_calls[0]["sim_name"], "fine") |
| 167 | + self.assertEqual(solve_calls[0]["computed_outputs"], {}) |
| 168 | + |
| 169 | + self.assertEqual(solve_calls[1]["sim_name"], "coarse") |
| 170 | + self.assertEqual( |
| 171 | + solve_calls[1]["computed_outputs"], |
| 172 | + {}, |
| 173 | + "Output from the previous resolution must not be reused after a model switch.", |
| 174 | + ) |
| 175 | + |
| 176 | + self.assertEqual(manager._micro_sims[0].name, "coarse") |
| 177 | + self.assertEqual(result, [{"result": 2}]) |
| 178 | + self.assertTrue(controller._converged) |
| 179 | + |
| 180 | + def test_manager_loop_exits_on_invalid_switch_request(self): |
| 181 | + """ |
| 182 | + Check the manager loop for the simpler boundary case where the |
| 183 | + simulation starts at the coarsest model and the switching function keeps |
| 184 | + requesting an even coarser model. The loop should perform one solve, |
| 185 | + recognize that no valid model change remains, and return normally. |
| 186 | + """ |
| 187 | + controller = self._make_controller(lambda resolution, *_: 1) |
| 188 | + manager = MicroManagerCoupling.__new__(MicroManagerCoupling) |
| 189 | + manager._model_adaptivity_controller = controller |
| 190 | + manager._is_adaptivity_on = False |
| 191 | + manager._mesh_vertex_coords = np.array([[0.0, 0.0, 0.0]]) |
| 192 | + manager._global_ids_of_local_sims = [0] |
| 193 | + manager._t = 1.0 |
| 194 | + manager._micro_sims = [DummySimulation("coarse")] |
| 195 | + |
| 196 | + solve_calls = [] |
| 197 | + |
| 198 | + def solve_variant(micro_sims_input, dt, computed_outputs): |
| 199 | + solve_calls.append( |
| 200 | + { |
| 201 | + "micro_sims_input": micro_sims_input, |
| 202 | + "dt": dt, |
| 203 | + "computed_outputs": computed_outputs, |
| 204 | + } |
| 205 | + ) |
| 206 | + return [{"result": 1.0}] |
| 207 | + |
| 208 | + result = MicroManagerCoupling._solve_micro_simulations_with_model_adaptivity( |
| 209 | + manager, |
| 210 | + [{"input": 1.0}], |
| 211 | + 0.1, |
| 212 | + solve_variant, |
| 213 | + ) |
| 214 | + |
| 215 | + self.assertEqual(len(solve_calls), 1) |
| 216 | + self.assertEqual(solve_calls[0]["computed_outputs"], {}) |
| 217 | + self.assertEqual(result, [{"result": 1.0}]) |
0 commit comments