Skip to content

Commit

Permalink
Merge pull request #8 from paulf81/feature/recursive_log
Browse files Browse the repository at this point in the history
Feature/recursive log
  • Loading branch information
paulf81 authored Mar 16, 2023
2 parents df122b1 + 6d194e6 commit 9ac0fb5
Show file tree
Hide file tree
Showing 2 changed files with 406 additions and 1 deletion.
79 changes: 78 additions & 1 deletion emu_python/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import datetime as dt
import json
import os
import sys
import random
import time

import numpy as np
import pandas as pd
Expand All @@ -20,6 +22,12 @@ def __init__(self, controller, py_sims, input_dict):
# Save the input dict
self.input_dict = input_dict

# Initialize the flattend input_dict
self.input_dict_flat = {}

# Initialize the output file
self.output_file = 'emu_output.csv'

# Save timt step
self.dt = input_dict['dt']

Expand Down Expand Up @@ -119,6 +127,9 @@ def run(self):
self.send_via_helics("control", str("[-1,-1,-1]"))
print(" #### Entering main loop #### ")

# Initialize the first iteration flag
self.first_iteration = True

# Run simulation till endtime
while self.absolute_helics_time < self.endtime:

Expand All @@ -133,7 +144,7 @@ def run(self):
self.input_dict['py_sims'] = self.py_sims.get_py_sim_dict()

# Print the input dict
print(self.input_dict)
# print(self.input_dict)

# Subscribe to helics messages:
incoming_messages = self.helics_connector.get_all_waiting_messages()
Expand Down Expand Up @@ -191,6 +202,8 @@ def run(self):
self.amr_wind_dict[self.amr_wind_names[0]
]['turbine_powers'] = turbine_power_array
self.turbine_power_array = turbine_power_array
self.amr_wind_dict[self.amr_wind_names[0]
]['sim_time_s_amr_wind'] = sim_time_s_amr_wind

# Send turbine powers through Kafka if enabled:
if self.KAFKA:
Expand All @@ -207,6 +220,70 @@ def run(self):

self.sync_time_helics(self.absolute_helics_time + self.deltat)

# Log the input dict
self.log_input_dict()

# If this is first iteration print the input dict
# And turn off the first iteration flag
if self.first_iteration:
print(self.input_dict)
self.first_iteration = False

# Echo the dictionary to a seperate file in case it is helpful
# to see full dictionary in interpreting log

original_stdout = sys.stdout
with open('input_dict.echo', 'w') as f_i:
sys.stdout = f_i # Change the standard output to the file we created.
print(self.input_dict)
sys.stdout = original_stdout # Reset the standard output to its original value

def recursive_flatten_input_dict(self, nested_dict, prefix = ''):

# Recursively flatten the input dict
for k, v in nested_dict.items():
if isinstance(v, dict):
self.recursive_flatten_input_dict(v, prefix + k + '.')
else:
# If v is a list or np.array, enter each element seperately
if isinstance(v, (list, np.ndarray)):
for i, vi in enumerate(v):
if isinstance(vi, (int, float)):
self.input_dict_flat[prefix + k + '.%03d' % i] = vi

# If v is a string, int, or float, enter it directly
if isinstance(v, (int, float)):
self.input_dict_flat[prefix + k] = v


def log_input_dict(self):

# Update the flattened input dict
self.recursive_flatten_input_dict(self.input_dict)

# Add the current time
self.input_dict_flat['clock_time'] = dt.datetime.now()

# The keys and values as two lists
keys = list(self.input_dict_flat.keys())
values = list(self.input_dict_flat.values())

# If this is first iteration, write the keys as csv header
if self.first_iteration:
with open(self.output_file, 'w') as filex:
filex.write(','.join(keys) + os.linesep)

# Load the csv header and check if it matches the current keys
with open(self.output_file, 'r') as filex:
header = filex.readline().strip().split(',')
if header != keys:
print("WARNING: Input dict keys have changed since first iteration.\
Not writing to csv file.")
return

# Append the values to the csv file
with open(self.output_file, 'a') as filex:
filex.write(','.join([str(v) for v in values]) + os.linesep)



Expand Down
Loading

0 comments on commit 9ac0fb5

Please sign in to comment.