Open Thomvanrij opened 1 year ago
import tkinter as tk
from tkinter import filedialog
import pandas as pd
import matplotlib.pyplot as plt
width, length, depth = 300, 300, 5
volume, surface_area = width * length * depth, width * length
water_level, water_temp = [400000 / (width * length)], [15]
outside_temp, solar_radiation = 10, 100
heat_capacity_water, density_water = 4186, 1000
days = int(input("Enter the number of days to calculate: "))
def get_flow_data(filename_prompt, flow_rate_prompt):
if input(filename_prompt) == 'y':
root = tk.Tk()
root.withdraw()
path = filedialog.askopenfilename()
data = pd.read_csv(path, header=None, delimiter=";")
data.columns = ['Day', f'{flow_rate_prompt}(m^3)']
else:
rate = float(input(flow_rate_prompt))
data = pd.DataFrame({'Day': range(1, days+1), f'{flow_rate_prompt}(m^3)': [rate] * days})
return data
inflow_data = get_flow_data("Do you want to supply an inflow CSV file? (y/n): ", "Inflow")
outflow_data = get_flow_data("Do you want to supply an outflow CSV file? (y/n): ", "Outflow")
water_level_zero = False
for day in range(1, days + 1):
inflow = inflow_data.loc[inflow_data['Day'] == day, 'Inflow(m^3)'].values[0]
outflow = outflow_data.loc[outflow_data['Day'] == day, 'Outflow(m^3)'].values[0]
water_level_change = (inflow - outflow) / volume
water_level_today, water_temp_prev = water_level[day - 1] + water_level_change, water_temp[day - 1]
heat_received = solar_radiation * surface_area * 86400
heat_loss = (water_temp_prev - outside_temp) * surface_area * 86400
temp_change = (heat_received - heat_loss) / (volume * density_water * heat_capacity_water)
water_temp_today = water_temp_prev + temp_change
if water_level_today < 0:
if not water_level_zero:
print(f"Water level has dropped to 0 on day {day}!")
water_level_zero = True
water_level_today = 0
water_level.append(water_level_today)
water_temp.append(water_temp_today)
print(f"Total water amount in the basin after {days} days: {water_level[-1] * volume:.2f} m^3")
def plot_data(data, ylabel, title):
plt.plot(range(days + 1), data)
plt.xlabel("Days")
plt.ylabel(ylabel)
plt.title(title)
plt.show()
plot_data(water_level, "Water level (m)", "Water level in basin over time")
plot_data(water_temp, "Water
import tkinter as tk
from tkinter import filedialog
import pandas as pd
import matplotlib.pyplot as plt
width, length, depth = 300, 300, 5
volume, surface_area = width * length * depth, width * length
water_level, water_temp = [400000 / (width * length)], [15]
outside_temp, solar_radiation = 10, 100
heat_capacity_water, density_water = 4186, 1000
days = int(input("Enter the number of days to calculate: "))
def get_flow_data(filename_prompt, flow_rate_prompt):
if input(filename_prompt) == 'y':
root = tk.Tk()
root.withdraw()
path = filedialog.askopenfilename()
data = pd.read_csv(path, header=None, delimiter=";")
data.columns = ['Day', f'{flow_rate_prompt}(m^3)']
else:
rate = float(input(flow_rate_prompt))
data = pd.DataFrame({'Day': range(1, days+1), f'{flow_rate_prompt}(m^3)': [rate] * days})
return data
inflow_data = get_flow_data("Do you want to supply an inflow CSV file? (y/n): ", "Inflow")
outflow_data = get_flow_data("Do you want to supply an outflow CSV file? (y/n): ", "Outflow")
water_level_zero = False
for day in range(1, days + 1):
inflow = inflow_data.loc[inflow_data['Day'] == day, 'Inflow(m^3)'].values[0]
outflow = outflow_data.loc[outflow_data['Day'] == day, 'Outflow(m^3)'].values[0]
water_level_change = (inflow - outflow) / volume
water_level_today, water_temp_prev = water_level[day - 1] + water_level_change, water_temp[day - 1]
heat_received = solar_radiation * surface_area * 86400
heat_loss = (water_temp_prev - outside_temp) * surface_area * 86400
temp_change = (heat_received - heat_loss) / (volume * density_water * heat_capacity_water)
water_temp_today = water_temp_prev + temp_change
if water_level_today < 0:
if not water_level_zero:
print(f"Water level has dropped to 0 on day {day}!")
water_level_zero = True
water_level_today = 0
water_level.append(water_level_today)
water_temp.append(water_temp_today)
print(f"Total water amount in the basin after {days} days: {water_level[-1] * volume:.2f} m^3")
def plot_data(data, ylabel, title):
plt.plot(range(days + 1), data)
plt.xlabel("Days")
plt.ylabel(ylabel)
plt.title(title)
plt.show()
plot_data(water_level, "Water level (m)", "Water level in basin over time")
plot_data(water_temp, "Water temperature (°C)", "Water temperature in basin over time")
This revised code condenses the original by reusing functions for file input and plotting. It also simplifies variable assignments and removes unnecessary comments.
Here's a simple GUI version of your code using the tkinter
library. I've created a main window with entry fields, buttons to load CSV files, and buttons to start the simulation and plot the results.
import tkinter as tk
from tkinter import filedialog
import pandas as pd
import matplotlib.pyplot as plt
class WaterSimulationGUI:
def __init__(self, master):
self.master = master
master.title("Water Simulation")
self.days_label = tk.Label(master, text="Number of days:")
self.days_entry = tk.Entry(master)
self.inflow_button = tk.Button(master, text="Load Inflow CSV", command=self.load_inflow_csv)
self.outflow_button = tk.Button(master, text="Load Outflow CSV", command=self.load_outflow_csv)
self.solar_radiation_button = tk.Button(master, text="Load Solar Radiation CSV", command=self.load_solar_radiation_csv)
self.temp_button = tk.Button(master, text="Load Outside Temperature CSV", command=self.load_temp_csv)
self.start_button = tk.Button(master, text="Start Simulation", command=self.start_simulation)
self.plot_water_level_button = tk.Button(master, text="Plot Water Level", command=self.plot_water_level, state=tk.DISABLED)
self.plot_water_temp_button = tk.Button(master, text="Plot Water Temperature", command=self.plot_water_temp, state=tk.DISABLED)
self.days_label.grid(row=0, column=0)
self.days_entry.grid(row=0, column=1)
self.inflow_button.grid(row=1, column=0)
self.outflow_button.grid(row=1, column=1)
self.solar_radiation_button.grid(row=2, column=0)
self.temp_button.grid(row=2, column=1)
self.start_button.grid(row=3, column=0)
self.plot_water_level_button.grid(row=3, column=1)
self.plot_water_temp_button.grid(row=4, column=0)
self.inflow_data = None
self.outflow_data = None
self.solar_radiation_data = None
self.temp_data = None
self.days = None
self.water_level = None
self.water_temp = None
def load_inflow_csv(self):
self.inflow_data = self.load_csv_data("Inflow(m^3)")
def load_outflow_csv(self):
self.outflow_data = self.load_csv_data("Outflow(m^3)")
def load_solar_radiation_csv(self):
self.solar_radiation_data = self.load_csv_data("Solar Radiation(W/m^2)")
def load_temp_csv(self):
self.temp_data = self.load_csv_data("Outside Temp(°C)")
def load_csv_data(self, column_name):
path = filedialog.askopenfilename()
data = pd.read_csv(path, header=None, delimiter=";")
data.columns = ['Day', f'{column_name}']
return data
def start_simulation(self):
self.days = int(self.days_entry.get())
self.run_simulation()
self.plot_water_level_button.config(state=tk.NORMAL)
self.plot_water_temp_button.config(state=tk.NORMAL)
def run_simulation(self):
# Add your simulation code here. Make sure to store the results in
# self.water_level and self.water_temp for plotting later.
def plot_water_level(self):
self.plot_data(self.water_level, "Water level (m)", "Water level in basin over time")
def plot_water_temp(self):
self.plot_data(self.water_temp, "Water temperature (°C)", "Water temperature in basin over time")
def plot_data(self, data, ylabel, title):
plt.plot(range(len(data)), data)
plt.xlabel("Days")
plt.ylabel(ylabel)
plt.title(title)
plt.show()
if __name__ == "__main__":
root = tk.Tk()
gui = WaterSimulationGUI(root)
root.mainloop()
In this code, I have structured the original script as a class named WaterSimulationGUI
. You can insert your simulation code into the run_simulation
method, and make sure to store the results in self.water_level
and self.water_temp
for plotting later.
Here is the updated README:
This model calculates the water level and temperature in a basin over a specified number of days based on inputs for inflow, outflow, solar radiation, air temperature, and other parameters. The model allows you to specify different scenarios by providing external CSV files for the input parameters.
To run the model, enter the number of days to calculate when prompted. You will then be prompted to either supply CSV files or enter single values for the inflow, outflow, solar radiation, air temperature, wind speed, and humidity parameters for each day. If you choose to supply CSV files, you will be presented with a file dialog to select the files.
The model will output the final water volume, water level, and water temperature in the basin after the specified number of days. It will also plot the water level and temperature over time.
The model uses the following parameters:
It requires inputs for the following for each day:
The model outputs the following:
The model allows you to specify different scenarios by providing external CSV files for the input parameters.
He