Skip to content

Commit

Permalink
fix deltat error
Browse files Browse the repository at this point in the history
  • Loading branch information
dvaidhyn committed Feb 6, 2024
1 parent 32c48f4 commit ed369a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
25 changes: 16 additions & 9 deletions hercules/amr_wind_standin.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,19 @@ def run(self):
# Initialize the values
turbine_powers = np.zeros(self.num_turbines)
sim_time_s = 0.0 # initialize time to 0
sim_time_s = self.absolute_helics_time # initialize time to 0
amr_wind_speed = 8.0
amr_wind_direction = 240.0

# Before starting the main time loop need to do an initial connection to the
# Control center to get the starting wind speed and wind direction
# Code the time step as -1 and -1 (to ensure it is an array)
logger.info("** First communication with control center")
message_from_client_array = [0, -1, -1]
# message_from_client_array = [0, -1, -1]
# Send initial message via helics
# publish on topic: status
self.send_via_helics("status", str(message_from_client_array))
logger.info("** Initial Message Sent: {}".format(message_from_client_array))
# self.send_via_helics("status", str(message_from_client_array))
# logger.info("** Initial Message Sent: {}".format(message_from_client_array))

# Subscribe to helics messages:
incoming_messages = self.helics_connector.get_all_waiting_messages()
Expand All @@ -170,6 +171,8 @@ def run(self):

# Synchronize time bewteen control center and AMRWind
self.sync_time_helics(self.absolute_helics_time + self.deltat)
sim_time_s = float(self.absolute_helics_time)

logger.info("** Initial Received reply: {}".format(message_from_server))

logger.info("** Intial Wind Speed: {}".format(amr_wind_speed))
Expand All @@ -178,10 +181,11 @@ def run(self):

self.message_from_server = None

# while self.absolute_helics_time < (self.endtime - self.starttime + 1):
while sim_time_s <= (self.endtime - self.starttime):
while self.absolute_helics_time < (self.endtime - self.starttime + 1):
#while sim_time_s <= (self.endtime - self.starttime):
# SIMULATE A CALCULATION STEP IN AMR WIND=========================
logger.info("Calculating simulation time: %.1f" % sim_time_s)
sim_time_s = float(self.absolute_helics_time)
logger.info("Calculating simulation time: %.3f" % sim_time_s)

# Compute the turbine power using a simple formula
(
Expand All @@ -195,7 +199,7 @@ def run(self):
# Communicate with control center
# Send the turbine powers for this time step and get wind speed and wind direction for
# the next time step
logger.info("Time step: %d" % sim_time_s)
logger.info("Time step: %0.3f" % sim_time_s)
logger.info("** Communicating with control center")
message_from_client_array = (
[
Expand All @@ -209,6 +213,7 @@ def run(self):

# Send helics message to Control Center
# publish on topic: status

self.send_via_helics("status", str(message_from_client_array))
logger.info("** Message Sent: {}".format(message_from_client_array))

Expand All @@ -227,8 +232,10 @@ def run(self):
# Note standin doesn't currently use received info for anything

# Advance simulation time and time step counter
sim_time_s += self.dt
#sim_time_s += self.dt
self.sync_time_helics(self.absolute_helics_time + self.deltat)
print("ABSOLUTE TIME : ",self.absolute_helics_time, self.deltat, self.dt)
sim_time_s = self.absolute_helics_time

# TODO cleanup code to move publish and subscribe here.

Expand Down Expand Up @@ -317,7 +324,7 @@ def launch_amr_wind_standin(amr_input_file, amr_standin_data_file=None):
"name": "amr_wind_standin",
"gridpack": {},
"helics": {
"deltat": 1,
"deltat": 0.5,
"subscription_topics": ["control"],
"publication_topics": ["status"],
"endpoints": [],
Expand Down
11 changes: 7 additions & 4 deletions hercules/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,16 @@ def run(self):
# publish on topic: control
self.send_via_helics("control", str("[-1,-1,-1]"))
print(" #### Entering main loop #### ")

self.sync_time_helics(self.absolute_helics_time + self.deltat)
# Initialize the first iteration flag
self.first_iteration = True

# Run simulation till endtime
while self.absolute_helics_time < self.endtime:
# while self.absolute_helics_time < self.endtime:
while self.absolute_helics_time < (self.endtime - self.starttime + 1):
# Loop till we reach simulation startime.
if self.absolute_helics_time < self.starttime:
continue
# if self.absolute_helics_time < self.starttime:
# continue

# Update controller and py sims
# TODO: Should 'time' in the main dict be AMR-wind time or
Expand Down Expand Up @@ -172,6 +173,7 @@ def receive_amrwind_data(self):
incoming_messages = self.helics_connector.get_all_waiting_messages()
if incoming_messages != {}:
subscription_value = self.process_subscription_messages(incoming_messages)
print("What did we receive ", subscription_value)
else:
print("Emulator: Did not receive subscription from AMRWind, setting everyhthing to 0.")
subscription_value = (
Expand Down Expand Up @@ -358,6 +360,7 @@ def process_periodic_endpoint(self):

def read_amr_wind_input(self, amr_wind_input):
# TODO this function is ugly and uncommented
print("How many times does this get called ", amr_wind_input)

# TODO Initialize to empty in case doesn't run
# Probably want a file not found error instead
Expand Down

0 comments on commit ed369a0

Please sign in to comment.