API
 
Loading...
Searching...
No Matches
test_aoSim.py
Go to the documentation of this file.
1import os
2import sys
3from pathlib import Path
4
5import numpy as np
6import logging
7
8# Ensure tests run from apps/aoSim as requested
9repo_root = Path(__file__).resolve().parents[2] # apps/aoSim/test -> apps/aoSim -> apps
10sys.path.insert(0, str(repo_root / "aoSim"))
11
12from xapp.aoSim import aoSim, aoSimConfig
13from purepyindi2 import constants
14
15
17 def __init__(self, data=None):
18 self._data = data
19
20 def get_data(self, wait=False):
21 if self._data is None:
22 raise RuntimeError("No data configured")
23 return self._data
24
25 def write(self, data):
26 self._data = np.array(data, dtype=np.float32)
27
28
30 sim = object.__new__(aoSim)
31 sim._nmodes = 2
32 sim._noise = 0.0
33 sim._frequency = 0.5
34 sim._t = np.array([0.0], dtype=np.float32)
35 sim._dt = np.array([0.01], dtype=np.float32)
36 sim._current_disturbance = np.zeros((2, 1), dtype=np.float32)
37 sim._current_dm_state = np.zeros((2, 1), dtype=np.float32)
38 sim._lag = 1
39 sim.log = logging.getLogger()
40 sim._dm_command_history = np.zeros((sim._lag + 1, sim._nmodes, 1), dtype=np.float32)
41
42 sim._dm = DummyImage(np.array([[0.5], [1.5]], dtype=np.float32))
43 sim._disturbance = DummyImage()
44 sim._wfs = DummyImage()
45 return sim
46
47
49 cfg = aoSimConfig()
50 assert cfg.num_modes == 2
51
52
54 sim = make_fake_aosim()
55 sim._dm_command_history[0] = np.array([[0.1], [0.2]], dtype=np.float32)
56 sim._dm_command_history[1] = np.array([[0.3], [0.4]], dtype=np.float32)
57
58 sim.update_dm()
59
60 np.testing.assert_array_equal(sim._current_dm_state, np.array([[0.1], [0.2]], dtype=np.float32))
61 np.testing.assert_array_equal(sim._dm_command_history[0], np.array([[0.3], [0.4]], dtype=np.float32))
62 np.testing.assert_array_equal(sim._dm_command_history[-1, :, 0], np.array([0.5, 1.5], dtype=np.float32))
63
64
66 sim = make_fake_aosim()
67 sim._current_dm_state = np.array([[0.2], [0.3]], dtype=np.float32)
68 sim._t = np.array([0.25], dtype=np.float32)
69
70 sim.update_disturbance()
71 assert sim._disturbance.get_data() is not None
72
73 sim.update_wfs()
74 np.testing.assert_array_equal(sim.err, sim._current_disturbance + sim._current_dm_state)
75 np.testing.assert_array_equal(sim._wfs.get_data(), sim.err)
76
77
79 sim = make_fake_aosim()
80 sim._dm_command_history[0] = np.array([[0.1], [0.1]], dtype=np.float32)
81 sim._dm_command_history[1] = np.array([[0.0], [0.0]], dtype=np.float32)
82
83 t_before = sim._t.copy()
84 sim.loop()
85
86 np.testing.assert_allclose(sim._t, t_before + sim._dt)
87 assert sim._wfs.get_data() is not None
88 assert sim._disturbance.get_data() is not None
89
90
92 sim = make_fake_aosim()
93 # Modify state to non-zero values
94 sim._t = np.array([1.5], dtype=np.float32)
95 sim._current_disturbance = np.array([[0.5], [0.3]], dtype=np.float32)
96 sim._current_dm_state = np.array([[0.2], [0.4]], dtype=np.float32)
97 sim._dm_command_history = np.ones((sim._lag + 1, sim._nmodes, 1), dtype=np.float32)
98
99 # Mock the property and message
100 existing_property = {'toggle': constants.SwitchState.OFF}
101 new_message = {'toggle': constants.SwitchState.ON}
102
103 # Mock update_property to do nothing
104 sim.update_property = lambda prop: None
105 print(sim._dm_command_history)
106 sim.handle_reset(existing_property, new_message)
107
108 # Check that state is reset
109 np.testing.assert_array_equal(sim._t, np.array([0.0], dtype=np.float32))
110 np.testing.assert_array_equal(sim._current_disturbance, np.zeros((2, 1), dtype=np.float32))
111 np.testing.assert_array_equal(sim._current_dm_state, np.zeros((2, 1), dtype=np.float32))
112 print(sim._dm_command_history)
113 np.testing.assert_array_equal(sim._dm_command_history, np.zeros((sim._lag + 1, sim._nmodes, 1), dtype=np.float32))
114
115 # Check that shmims are written with zeros
116 np.testing.assert_array_equal(sim._wfs.get_data(), np.zeros((2, 1), dtype=np.float32))
117 np.testing.assert_array_equal(sim._disturbance.get_data(), np.zeros((2, 1), dtype=np.float32))
118 np.testing.assert_array_equal(sim._dm.get_data(), np.zeros((2, 1), dtype=np.float32))
119
120 # Check that property is set back to OFF
121 assert existing_property['toggle'] == constants.SwitchState.OFF
__init__(self, data=None)
Definition test_aoSim.py:17
get_data(self, wait=False)
Definition test_aoSim.py:20
test_update_disturbance_and_wfs_propagate_values()
Definition test_aoSim.py:65
test_update_dm_uses_command_history_and_dm_data()
Definition test_aoSim.py:53
test_handle_reset_resets_simulation_state()
Definition test_aoSim.py:91
test_config_default_num_modes()
Definition test_aoSim.py:48
test_loop_advances_time_and_updates_fields()
Definition test_aoSim.py:78
make_fake_aosim()
Definition test_aoSim.py:29