Skip to content

Commit

Permalink
ENH: improve Flight.TimeNodes.merge method
Browse files Browse the repository at this point in the history
  • Loading branch information
Gui-FernandesBR committed Apr 18, 2024
1 parent af31b94 commit 2602e4d
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions rocketpy/simulation/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -3633,19 +3633,22 @@ def sort(self):
self.list.sort(key=(lambda node: node.t))

def merge(self):
# Initialize temporary list
tmp_list = [self.list[0]]
# Iterate through all other time nodes
for node in self.list[1:]:
# If there is already another node with similar time: merge
if abs(node.t - tmp_list[-1].t) < 1e-7:
tmp_list[-1].parachutes += node.parachutes
tmp_list[-1].callbacks += node.callbacks
# Add new node to tmp list if there is none with the same time
else:
tmp_list.append(node)
# Save tmp list to permanent
self.list = tmp_list
"""Merge all the time nodes that have the same time. This is made to
avoid multiple evaluations of the same time node. This method does
not guarantee the order of the nodes in the list, so it is
recommended to sort the list before or after using this method.
"""
tmp_dict = {}
for node in self.list:
time = round(node.t, 7)
try:
# Try to access the node and merge if it exists
tmp_dict[time].parachutes += node.parachutes
tmp_dict[time].callbacks += node.callbacks
except KeyError:
# If the node does not exist, add it to the dictionary
tmp_dict[time] = node
self.list = list(tmp_dict.values())

def flush_after(self, index):
del self.list[index + 1 :]
Expand Down

0 comments on commit 2602e4d

Please sign in to comment.