|
1 | 1 | from datetime import datetime, timedelta |
2 | | -from unittest import TestCase |
3 | 2 |
|
4 | 3 | import pytest |
5 | 4 | from alfalfa_client.alfalfa_client import AlfalfaClient |
|
19 | 18 | ################################################################################################## |
20 | 19 |
|
21 | 20 |
|
| 21 | +@pytest.fixture |
| 22 | +def simple_thermostat_run_id(alfalfa: AlfalfaClient): |
| 23 | + fmu_path = prepare_model('simple_thermostat.fmu') |
| 24 | + run_id = alfalfa.submit(fmu_path) |
| 25 | + |
| 26 | + alfalfa.wait(run_id, "ready") |
| 27 | + |
| 28 | + yield run_id |
| 29 | + |
| 30 | + alfalfa.stop(run_id) |
| 31 | + alfalfa.wait(run_id, "complete") |
| 32 | + |
| 33 | + |
22 | 34 | @pytest.mark.integration |
23 | | -class TestSimpleThermostat(TestCase): |
24 | | - |
25 | | - def setUp(self): |
26 | | - self.alfalfa = AlfalfaClient(host='http://localhost') |
27 | | - fmu_path = prepare_model('simple_thermostat.fmu') |
28 | | - self.model_id = self.alfalfa.submit(fmu_path) |
29 | | - |
30 | | - self.alfalfa.wait(self.model_id, "ready") |
31 | | - |
32 | | - self.current_datetime = datetime(2019, 1, 1) |
33 | | - |
34 | | - self.alfalfa.start( |
35 | | - self.model_id, |
36 | | - external_clock=True, |
37 | | - start_datetime=self.current_datetime, |
38 | | - end_datetime=datetime(2019, 1, 1, 0, 5), |
39 | | - timescale=5 |
40 | | - ) |
41 | | - self.alfalfa.wait(self.model_id, "running") |
42 | | - |
43 | | - def test_io_with_external_clock(self): |
44 | | - # Simulation is running, but time should still be at 0 |
45 | | - model_time = self.alfalfa.get_sim_time(self.model_id) |
46 | | - assert self.current_datetime == model_time |
47 | | - |
48 | | - # If outputs are requested before the simulation is advanced, |
49 | | - # there will be an error. |
50 | | - # See issue https://github.com/NREL/alfalfa/issues/119 |
51 | | - self.current_datetime += timedelta(minutes=1) |
52 | | - self.alfalfa.advance([self.model_id]) |
53 | | - model_time = self.alfalfa.get_sim_time(self.model_id) |
54 | | - assert self.current_datetime == model_time |
55 | | - |
56 | | - # Having not set any inputs the fmu will be at the initial state. |
57 | | - # The control signal output "rea" is at 0.0 |
58 | | - outputs = self.alfalfa.get_outputs(self.model_id) |
59 | | - rea = outputs.get("rea") |
60 | | - assert rea == pytest.approx(0.0) |
61 | | - |
62 | | - # Attempt to override the measured temp (ie zone temperature), |
63 | | - # and the setpoint, such that zone temperature is over setpoint. |
64 | | - self.alfalfa.set_inputs(self.model_id, {"oveWriMeasuredTemp_u": 303.15, "oveWriSetPoint_u": 294.15}) |
65 | | - |
66 | | - # Advance time, outputs will not be updated until advance happens. |
67 | | - # Should this limitation be considered a bug? |
68 | | - # Note that boptest advance and set input apis are combined, |
69 | | - # so that there is no method to set inputs without advancing |
70 | | - self.current_datetime += timedelta(minutes=1) |
71 | | - self.alfalfa.advance([self.model_id]) |
72 | | - model_time = self.alfalfa.get_sim_time(self.model_id) |
73 | | - assert self.current_datetime == model_time |
74 | | - |
75 | | - # When temperature is over setpoint controller returns 0.0 |
76 | | - outputs = self.alfalfa.get_outputs(self.model_id) |
77 | | - rea = outputs.get("rea") |
78 | | - assert rea == pytest.approx(0.0) |
79 | | - |
80 | | - # Now override the measured (zone) temperature such that it is below setpoint |
81 | | - self.alfalfa.set_inputs(self.model_id, {"oveWriMeasuredTemp_u": 283.15, "oveWriSetPoint_u": 294.15}) |
82 | | - |
83 | | - self.current_datetime += timedelta(minutes=1) |
84 | | - self.alfalfa.advance([self.model_id]) |
85 | | - model_time = self.alfalfa.get_sim_time(self.model_id) |
86 | | - assert self.current_datetime == model_time |
87 | | - |
88 | | - # When temperature is below setpoint controller returns 1.0 |
89 | | - outputs = self.alfalfa.get_outputs(self.model_id) |
90 | | - rea = outputs.get("rea") |
91 | | - assert rea == pytest.approx(1.0) |
92 | | - |
93 | | - # Test the control signal override |
94 | | - self.alfalfa.set_inputs(self.model_id, {"oveWriActuatorSignal_u": 0.0}) |
95 | | - self.current_datetime += timedelta(minutes=1) |
96 | | - self.alfalfa.advance([self.model_id]) |
97 | | - model_time = self.alfalfa.get_sim_time(self.model_id) |
98 | | - assert self.current_datetime == model_time |
99 | | - outputs = self.alfalfa.get_outputs(self.model_id) |
100 | | - rea = outputs.get("rea") |
101 | | - assert rea == pytest.approx(0.0) |
102 | | - |
103 | | - def tearDown(self): |
104 | | - self.alfalfa.stop(self.model_id) |
105 | | - self.alfalfa.wait(self.model_id, "complete") |
| 35 | +def test_io_with_external_clock(alfalfa: AlfalfaClient, simple_thermostat_run_id): |
| 36 | + run_id = simple_thermostat_run_id |
| 37 | + |
| 38 | + current_datetime = datetime(2019, 1, 1) |
| 39 | + |
| 40 | + alfalfa.start( |
| 41 | + run_id, |
| 42 | + external_clock=True, |
| 43 | + start_datetime=current_datetime, |
| 44 | + end_datetime=datetime(2019, 1, 1, 0, 5), |
| 45 | + timescale=5 |
| 46 | + ) |
| 47 | + alfalfa.wait(run_id, "running") |
| 48 | + |
| 49 | + # Simulation is running, but time should still be at 0 |
| 50 | + model_time = alfalfa.get_sim_time(run_id) |
| 51 | + assert current_datetime == model_time |
| 52 | + |
| 53 | + # If outputs are requested before the simulation is advanced, |
| 54 | + # there will be an error. |
| 55 | + # See issue https://github.com/NREL/alfalfa/issues/119 |
| 56 | + current_datetime += timedelta(minutes=1) |
| 57 | + alfalfa.advance([run_id]) |
| 58 | + model_time = alfalfa.get_sim_time(run_id) |
| 59 | + assert current_datetime == model_time |
| 60 | + |
| 61 | + # Having not set any inputs the fmu will be at the initial state. |
| 62 | + # The control signal output "rea" is at 0.0 |
| 63 | + outputs = alfalfa.get_outputs(run_id) |
| 64 | + rea = outputs.get("rea") |
| 65 | + assert rea == pytest.approx(0.0) |
| 66 | + |
| 67 | + # Attempt to override the measured temp (ie zone temperature), |
| 68 | + # and the setpoint, such that zone temperature is over setpoint. |
| 69 | + alfalfa.set_inputs(run_id, {"oveWriMeasuredTemp_u": 303.15, "oveWriSetPoint_u": 294.15}) |
| 70 | + |
| 71 | + # Advance time, outputs will not be updated until advance happens. |
| 72 | + # Should this limitation be considered a bug? |
| 73 | + # Note that boptest advance and set input apis are combined, |
| 74 | + # so that there is no method to set inputs without advancing |
| 75 | + current_datetime += timedelta(minutes=1) |
| 76 | + alfalfa.advance([run_id]) |
| 77 | + model_time = alfalfa.get_sim_time(run_id) |
| 78 | + assert current_datetime == model_time |
| 79 | + |
| 80 | + # When temperature is over setpoint controller returns 0.0 |
| 81 | + outputs = alfalfa.get_outputs(run_id) |
| 82 | + rea = outputs.get("rea") |
| 83 | + assert rea == pytest.approx(0.0) |
| 84 | + |
| 85 | + # Now override the measured (zone) temperature such that it is below setpoint |
| 86 | + alfalfa.set_inputs(run_id, {"oveWriMeasuredTemp_u": 283.15, "oveWriSetPoint_u": 294.15}) |
| 87 | + |
| 88 | + current_datetime += timedelta(minutes=1) |
| 89 | + alfalfa.advance([run_id]) |
| 90 | + model_time = alfalfa.get_sim_time(run_id) |
| 91 | + assert current_datetime == model_time |
| 92 | + |
| 93 | + # When temperature is below setpoint controller returns 1.0 |
| 94 | + outputs = alfalfa.get_outputs(run_id) |
| 95 | + rea = outputs.get("rea") |
| 96 | + assert rea == pytest.approx(1.0) |
| 97 | + |
| 98 | + # Test the control signal override |
| 99 | + alfalfa.set_inputs(run_id, {"oveWriActuatorSignal_u": 0.0}) |
| 100 | + current_datetime += timedelta(minutes=1) |
| 101 | + alfalfa.advance([run_id]) |
| 102 | + model_time = alfalfa.get_sim_time(run_id) |
| 103 | + assert current_datetime == model_time |
| 104 | + outputs = alfalfa.get_outputs(run_id) |
| 105 | + rea = outputs.get("rea") |
| 106 | + assert rea == pytest.approx(0.0) |
0 commit comments