From 35db58265a91187e4ab029315ed0bd583c099dff Mon Sep 17 00:00:00 2001 From: Cal Wing Date: Fri, 14 Feb 2025 20:11:00 +0100 Subject: [PATCH] Push a1.zip --- a1_support.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ test_data.txt | 1 + test_data_2.txt | 1 + 3 files changed, 53 insertions(+) create mode 100644 a1_support.py create mode 100644 test_data.txt create mode 100644 test_data_2.txt diff --git a/a1_support.py b/a1_support.py new file mode 100644 index 0000000..072cd51 --- /dev/null +++ b/a1_support.py @@ -0,0 +1,51 @@ +import matplotlib.pyplot as plt + +GRAVITY_ACC = 9.81 +WATER_DENSITY = 997.13 + +HELP_MESSAGE = """ + 'h' - provide help message + 'r' - read test data from a file + 'p' - {start_relative_elevation, step, no_steps} - print a table showing daily profit and total energy for various values of relative elevation + 'q' - terminate the program +""" + + + +def load_data(directory: str, filename: str): + """ + Loads the data from a txt file + + Parameters: + directory (str): the directory of the file + filename (str): the name of the file + + Returns: + (list): the data from the file + + """ + + with open(directory + '/' + filename, 'r') as f: + data = [float(i) for i in f.readlines()[0].strip().split(',')] + return data + +def plot_water_height(water_heights: tuple[float, ...], time_inc: float): + """ + Plots the water height over time + + Parameters: + water_heights (tuple): the water heights + time_inc (float): the time increment + + Returns: + None + """ + time = [i*time_inc for i in range(len(water_heights))] + plt.plot(time, water_heights, label='Dam Water Height') + plt.plot(time[0], water_heights[0], 'ro', label='Start') + plt.plot(time[-1], water_heights[-1], 'ro', label='End') + plt.xlabel("Time (s)") + plt.ylabel("Water Height (m)") + plt.title("Water Height vs Time") + plt.legend() + plt.show() \ No newline at end of file diff --git a/test_data.txt b/test_data.txt new file mode 100644 index 0000000..47603b5 --- /dev/null +++ b/test_data.txt @@ -0,0 +1 @@ +30, 20, 40000, 1, 30, 0.02, 0.005, 85 \ No newline at end of file diff --git a/test_data_2.txt b/test_data_2.txt new file mode 100644 index 0000000..9323b2b --- /dev/null +++ b/test_data_2.txt @@ -0,0 +1 @@ +1000, 10, 0.01, 0.005, 1, 0.1, 0.05, 90 \ No newline at end of file