Skip to content

Commit 5c64c09

Browse files
committed
gridappsd-python updates
1 parent eef6c4a commit 5c64c09

File tree

2 files changed

+82
-97
lines changed

2 files changed

+82
-97
lines changed

simulation_config_files/9500-timeseries-config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
"id": "gridappsd-sensor-simulator",
3838
"user_options": {
3939
"default-perunit-confidence-band": 0.02,
40-
"simulate-all": false,
40+
"simulate-all": true,
4141
"sensors-config": {},
4242
"default-normal-value": 100,
4343
"random-seed": 0,
4444
"default-aggregation-interval": 30,
45-
"passthrough-if-not-specified": false,
45+
"passthrough-if-not-specified": true,
4646
"default-perunit-drop-rate": 0.05
4747
}
4848
}]

test_timeseries.py

Lines changed: 80 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -6,115 +6,100 @@
66
from time import sleep, time
77
import sys
88
import pytest
9-
from gridappsd import GridAPPSD, topics as t
10-
# tm: added for run_simulation workaround
9+
10+
from gridappsd import GridAPPSD
1111
from gridappsd.simulation import Simulation
1212
from gridappsd_docker import docker_up, docker_down
13-
14-
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
13+
from gridappsd import GridAPPSD, topics as t
1514

1615
LOGGER = logging.getLogger(__name__)
1716

18-
19-
@contextmanager
20-
def startup_containers(spec=None):
21-
LOGGER.info('Starting gridappsd containers')
22-
docker_up(spec)
23-
LOGGER.info('Containers started')
24-
25-
yield
26-
27-
LOGGER.info('Stopping gridappsd containers')
28-
# docker_down()
29-
# LOGGER.info('Containers stopped')
30-
31-
32-
@contextmanager
33-
def gappsd() -> GridAPPSD:
34-
gridappsd = GridAPPSD()
35-
LOGGER.info('Gridappsd connected')
36-
37-
yield gridappsd
38-
39-
gridappsd.disconnect()
40-
LOGGER.info('Gridappsd disconnected')
41-
17+
result_weather_data = []
18+
result_timeseries_query = []
19+
result_sensor_query = []
4220

4321
@pytest.mark.parametrize("sim_config_file, sim_result_file", [
4422
("9500-timeseries-config.json", "9500-simulation.json")
4523
# ("123-config.json", "123-simulation.json"),
4624
# ("13-node-config.json", "13-node-sim.json"),
4725
# , ("t3-p1-config.json", "t3-p1.json"),
4826
])
49-
def test_timeseries_output(sim_config_file, sim_result_file):
27+
def test_timeseries_output(gridappsd_client, sim_config_file, sim_result_file):
28+
global result_weather_data
29+
global result_timeseries_query
30+
global result_sensor_query
5031
simulation_id = None
5132
sim_config_file = os.path.join(os.path.dirname(__file__), f"simulation_config_files/{sim_config_file}")
5233
sim_result_file = os.path.join(os.path.dirname(__file__), f"simulation_baseline_files/{sim_result_file}")
53-
# sim_test_config = os.path.join(os.path.dirname(__file__), f"simulation_baseline_files/{sim_test_file}")
5434

5535
assert os.path.exists(sim_config_file), f"File {sim_config_file} must exist to run simulation test"
56-
# assert os.path.exists(sim_result_file), f"File {sim_result_file} must exist to run simulation test"
57-
58-
with startup_containers():
59-
with gappsd() as gapps:
60-
os.makedirs("/tmp/output", exist_ok=True)
61-
with open("/tmp/output/simulation.json", 'w') as outfile:
62-
sim_complete = False
63-
rcvd_measurement = False
64-
65-
def onmeasurement(sim, timestep, measurements):
66-
nonlocal rcvd_measurement
67-
# print(rcvd_measurement)
68-
if not rcvd_measurement:
69-
# print(f"A measurement happened at {timestep}")
70-
LOGGER.info('Measurement received at %s', timestep)
71-
outfile.write(f"{timestep}|{json.dumps(measurements)}\n")
72-
73-
with open(sim_config_file) as fp:
74-
run_config = json.load(fp)
75-
starttime = run_config["simulation_config"]["start_time"]
76-
# run_config["simulation_config"]["start_time"] = str(starttime)
77-
print(run_config["simulation_config"]["start_time"])
78-
79-
def onfinishsimulation(sim):
80-
nonlocal sim_complete
81-
sim_complete = True
82-
LOGGER.info('Simulation Complete')
83-
84-
sim = Simulation(gapps, run_config)
85-
sim.add_oncomplete_callback(onfinishsimulation)
86-
sim.add_onmesurement_callback(onmeasurement)
87-
sim.start_simulation()
88-
LOGGER.info("Starting simulation")
89-
print(sim.simulation_id)
90-
91-
with open("./simulation_config_files/weather_data.json", 'r') as g:
92-
LOGGER.info('Querying weather data from timeseries')
93-
query1 = json.load(g)
94-
95-
a = gapps.get_response(t.TIMESERIES, query1, timeout=60)
96-
LOGGER.info('Weather data received ')
97-
assert "Diffuse" in a["data"][0], "Weather data query does not have expected output"
98-
LOGGER.info('Weather data query has expected output')
99-
100-
while not sim_complete:
101-
sleep(5)
102-
103-
with open("./simulation_config_files/timeseries_query.json", 'r') as f:
104-
query2 = json.load(f)
105-
query2["queryFilter"]["simulation_id"] = sim.simulation_id
106-
LOGGER.info('Querying simulation data from timeseries')
107-
q = gapps.get_response(t.TIMESERIES, query2, timeout=300)
108-
LOGGER.info('Simulation data received for Timeseries API')
109-
file2 = open("./out-input.txt", 'w')
110-
file2.write(json.dumps(q))
111-
assert "hasSimulationMessageType" in q["data"][0], "Simulation data query does not have expected output"
112-
LOGGER.info('Simulation data query has expected output')
113-
114-
with open("./simulation_config_files/sensor_query.json", 'r') as file:
115-
sensor_query = json.load(file)
116-
sensor_query["queryFilter"]["simulation_id"] = sim.simulation_id
117-
LOGGER.info('Querying GridAPPS-D sensor simulator data from timeseries')
118-
result = gapps.get_response(t.TIMESERIES, sensor_query, timeout=300)
119-
assert "hasSimulationMessageType" in result["data"][0], "Sensor simulator data does not have expected output"
120-
LOGGER.info('Query response received for GridAPPS-D sensor simulator data from timeseries')
36+
37+
gapps = gridappsd_client
38+
sim_complete = False
39+
rcvd_measurement = False
40+
41+
def onmeasurement(sim, timestep, measurements):
42+
LOGGER.info('Measurement received at %s', timestep)
43+
44+
def onfinishsimulation(sim):
45+
nonlocal sim_complete
46+
sim_complete = True
47+
LOGGER.info('Simulation Complete')
48+
49+
with open(sim_config_file) as fp:
50+
LOGGER.info('Loading config')
51+
run_config = json.load(fp)
52+
LOGGER.info(f'Simulation start time {run_config["simulation_config"]["start_time"]}')
53+
54+
LOGGER.info('Starting the simulation')
55+
sim = Simulation(gapps, run_config)
56+
sim.start_simulation()
57+
LOGGER.info(f'Simulation id {sim.simulation_id}')
58+
59+
LOGGER.info('sim.add_oncomplete_callback')
60+
sim.add_oncomplete_callback(onfinishsimulation)
61+
62+
LOGGER.info('sim.add_onmeasurement_callback')
63+
sim.add_onmesurement_callback(onmeasurement)
64+
65+
with open("./simulation_config_files/weather_data.json", 'r') as g:
66+
LOGGER.info('Querying weather data from timeseries')
67+
query1 = json.load(g)
68+
result_weather_data = gapps.get_response(t.TIMESERIES, query1, timeout=60)
69+
LOGGER.info('Weather data received ')
70+
71+
while not sim_complete:
72+
LOGGER.info('Sleeping')
73+
sleep(5)
74+
else:
75+
with open("./simulation_config_files/timeseries_query.json", 'r') as f:
76+
query2 = json.load(f)
77+
query2["queryFilter"]["simulation_id"] = sim.simulation_id
78+
LOGGER.info('Querying simulation data from timeseries')
79+
result_timeseries_query = gapps.get_response(t.TIMESERIES, query2, timeout=300)
80+
LOGGER.info('Simulation data received for Timeseries API')
81+
82+
with open("./simulation_config_files/sensor_query.json", 'r') as file:
83+
sensor_query = json.load(file)
84+
sensor_query["queryFilter"]["simulation_id"] = sim.simulation_id
85+
LOGGER.info('Querying GridAPPS-D sensor simulator data from timeseries')
86+
result_sensor_query = gapps.get_response(t.TIMESERIES, sensor_query, timeout=300)
87+
LOGGER.info('Simulation data received for sensor simulator')
88+
89+
def test_weather_api():
90+
global result_weather_data
91+
assert "Diffuse" in result_weather_data["data"][0], \
92+
"Weather data query does not have expected output"
93+
LOGGER.info('Weather data query has expected output')
94+
95+
def test_timeseries_simulation_api():
96+
global result_timeseries_query
97+
assert "hasSimulationMessageType" in result_timeseries_query["data"][0], \
98+
"Simulation data query does not have expected output"
99+
LOGGER.info('Simulation data query has expected output')
100+
101+
def test_sensor_simulator_api():
102+
global result_sensor_query
103+
assert "hasSimulationMessageType" in result_sensor_query["data"][0], \
104+
"Sensor simulator data does not have expected output"
105+
LOGGER.info('Query response received for GridAPPS-D sensor simulator data from timeseries')

0 commit comments

Comments
 (0)