def test_model(self): sim = Simulation() assert sim.model is None model = mock.MagicMock(MicroBenthosModel) clock = mock.MagicMock(ModelClock) model.clock = clock with pytest.raises(ValueError): sim.model = model # model interface is checked, so this is incomplete model.full_eqn = mock.Mock() sim.model = model assert sim.model is model
def test_evolution(self): sim = Simulation() sim._started = True with pytest.raises(RuntimeError): for ret in sim.evolution(): pass sim._started = False model = mock.MagicMock(MicroBenthosModel) clock = mock.MagicMock(ModelClock) clock.return_value = PhysicalField(0, 'h') model.clock = clock model.full_eqn = feqn = mock.Mock() feqn.sweep.return_value = RES = sim.max_residual / 2 sim.model = model evolution = sim.evolution() step, state = next(evolution) model.update_vars.call_count == 2 # once before while loop starts and once for the first yield clock.copy.assert_called_once() assert step == 1 assert isinstance(state, dict) step, state = next(evolution) clock.increment_time.assert_called_once()
def test_run_timestep(self): sim = Simulation() with pytest.raises(RuntimeError): sim.run_timestep() # sim.started is still False sim._started = True with pytest.raises(RuntimeError): sim.run_timestep() # no model available model = mock.MagicMock(MicroBenthosModel) clock = mock.MagicMock(ModelClock) model.clock = clock model.full_eqn = feqn = mock.Mock() feqn.sweep.return_value = RES = sim.max_residual sim.model = model res, nsweeps = sim.run_timestep() assert res == RES assert nsweeps == 1 feqn.sweep.assert_called_once() # now test numerical failure feqn.sweep.side_effect = RuntimeError feqn._vars = mock.MagicMock() var = mock.Mock() feqn._vars.__iter__ = mock.Mock(return_value=iter([var]))
def test_simtime_days(self): sim = Simulation(simtime_days=3) sTot = sim.simtime_total model = mock.MagicMock(MicroBenthosModel) clock = mock.MagicMock(ModelClock) model.clock = clock model.full_eqn = mock.Mock() model.get_object.return_value = I = mock.Mock() I.hours_total = 5 sim.model = model model.get_object.assert_called_once_with('env.irradiance') assert sim.simtime_total == PhysicalField(5 * 3, 'h')
def test_snapshot_due(self): sim = Simulation() model = mock.MagicMock(MicroBenthosModel) clock = mock.MagicMock(ModelClock) clock.return_value = C = PhysicalField(3, 'h') model.clock = clock model.full_eqn = feqn = mock.Mock() sim.model = model sim._prev_snapshot = Variable(C - sim.snapshot_interval / 2.0) assert not sim.snapshot_due() sim._prev_snapshot = Variable(C - sim.snapshot_interval * 1.01) assert sim.snapshot_due()
def test_get_state(self): sim = Simulation() model = mock.MagicMock(MicroBenthosModel) clock = mock.MagicMock(ModelClock) clock.return_value = PhysicalField(0, 'h') model.clock = clock model.full_eqn = feqn = mock.Mock() sim.model = model state = sim.get_state() assert set(state) == {'time', 'metrics'} statesend = dict(a=1, b=2, c=3) state = sim.get_state(state=statesend) sent = set(statesend) sent.update(('metrics', )) assert set(state) == sent V = 30 for m in ('calc_time', 'residual', 'num_sweeps'): state = sim.get_state(**{m: V}) assert state['metrics'][m]['data'][0] == V