Skip to content

Commit

Permalink
Show UA fit; use least-squares.
Browse files Browse the repository at this point in the history
  • Loading branch information
spahrenk committed Nov 24, 2024
1 parent 6490ce5 commit 81f5beb
Showing 1 changed file with 74 additions and 37 deletions.
111 changes: 74 additions & 37 deletions scripts/python/plotting/plot_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from dash import Dash, dcc, html, Input, Output, State, callback, set_props
import plotly.express as px
import plotly.graph_objects as go

import plotly.io as io
from scipy.optimize import least_squares
import numpy as np
import math

def dash_proc(fig):

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = Dash(__name__, external_stylesheets=external_stylesheets)
Expand All @@ -23,19 +25,18 @@ def dash_proc(fig):
}
}

fig.update_layout(clickmode='event+select')

fig.update_traces(marker_size=4)

PARAMETERS = (
"tank volume (L)",
)

dash_proc.t_minV = []
dash_proc.tank_TV = []
dash_proc.ambient_TV = []


fig.update_layout(clickmode='event+select')

app.layout = [

dcc.Graph(id='test-graph', figure=fig, style ={'width': '1200px', 'height': '800px', 'display': 'block'} ),

html.Div(
Expand All @@ -56,7 +57,7 @@ def dash_proc(fig):

html.Button('Get UA', id='get-ua-btn', n_clicks=0, disabled = True),

html.P(id='ua', style = {'fontSize': 18}), html.Br()
html.P(id='ua-p', style = {'fontSize': 18}), html.Br()
],
id = 'ua-div',
className='four columns',
Expand All @@ -67,35 +68,56 @@ def dash_proc(fig):
@callback(
Output('get-ua-btn', 'disabled'),
Output('ua-div', 'hidden'),
Output('ua', 'children', allow_duplicate=True),
Output('ua-p', 'children', allow_duplicate=True),
Output('test-graph', 'figure', allow_duplicate=True),
Input('test-graph', 'selectedData'),
State('test-graph', 'figure'),
State('tank-volume', 'value'),
prevent_initial_call=True
)
def select_temperature_data(selectedData, fig, tank_vol_L):
if float(tank_vol_L) <= 0:
return True, True, ""
return True, True, "", fig

if not selectedData:
return True, True, ""
return True, True, "", fig

if not "range" in selectedData:
return True, True, ""
return True, True, "", fig

range = selectedData["range"]
if not "y3" in range:
return True, True, ""
return True, True, "", fig

t_min_i = range["x3"][0]
t_min_f = range["x3"][1]


found_avgT = False
for trace in fig["data"]:
if trace["name"] == "Storage Tank Average Temperature - Measured":
measured_tank_T = trace
if "name" in trace:
if trace["name"] == "Storage Tank Average Temperature - Measured":
measured_tank_T = trace
found_avgT = True

found_ambientT = False
for trace in fig["data"]:
if trace["name"] == "Ambient Temperature - Measured":
ambient_T = trace

if "name" in trace:
ambient_T = trace
found_ambientT = True

if not(found_avgT and found_ambientT):
return True, True, "", fig

new_fig = go.Figure(fig)
for i, trace in enumerate(fig["data"]):
if "name" in trace:
if trace["name"] == "temperature fit":
new_data = list(new_fig.data)
new_data.pop(i)
new_fig.data = new_data
new_fig.update_layout()

dash_proc.t_minV = []
dash_proc.tank_TV = []
dash_proc.ambient_TV = []
Expand All @@ -108,19 +130,26 @@ def select_temperature_data(selectedData, fig, tank_vol_L):
n += 1

if n < 2:
return True, True, ""
return True, True, "", fig

return False, False, ""
return False, False, "", new_fig

@callback(
Output('test-graph', 'figure'),
Output('ua', 'children'),
Output('ua-p', 'children'),
Input('get-ua-btn', 'n_clicks'),
State('test-graph', 'figure'),
State('tank-volume', 'value'),
prevent_initial_call=True
)
def calc_ua(n_clicks, fig, tankVol_L):

for i, trace in enumerate(fig["data"]):
if "name" in trace:
if trace["name"] == "temperature fit":
new_data = list(new_fig.data)
new_data.pop(i)
new_fig.data = new_data

t_min_i = dash_proc.t_minV[0]
t_min_f = dash_proc.t_minV[-1]
Expand All @@ -138,28 +167,36 @@ def calc_ua(n_clicks, fig, tankVol_L):
ambientT_avg = (ambient_T_i + ambient_T_f) / 2

temp_ratio = (tank_T_i - tank_T_f) / (tank_T_i - ambientT_avg)
t_h = (t_min_f - t_min_i) / 60

dt_min = t_min_f - t_min_i
dt_h = dt_min / 60

UA = cTank_kJ_per_C * temp_ratio / t_h
tau_min = UA / cTank_kJ_per_C * 60

t_minV = np.array(dash_proc.t_minV)
t_minV -= t_min_i
UA = cTank_kJ_per_C * temp_ratio / dt_h
tau_min0 = dt_min / temp_ratio

t_minV = np.array(dash_proc.t_minV)
tank_TV = np.array(dash_proc.tank_TV)

def T_t(params, t_min):
return ambientT_avg + (tank_T_i - ambientT_avg) * np.exp(-t_min / params[0])
return ambientT_avg + (tank_T_i - ambientT_avg) * np.exp(-(t_min - t_min_i) / params[0])

def diffT_t(params):
return T_t(params, t_minV) - tank_TV
def diffT_t(params, t_min):
return T_t(params, t_min) - tank_TV

paramV = [tau_min]
res = least_squares(diffT_t, paramV)

UA = paramV[0] / 60 * cTank_kJ_per_C
tau_min = tau_min0
res = least_squares(diffT_t, t_minV, args=(tau_min, ))
UA = cTank_kJ_per_C / (tau_min / 60)

fit_tank_TV = tank_TV
for i, t_min in enumerate(t_minV):
fit_tank_TV[i] = T_t([tau_min], t_min)
#print(fit_tank_TV)

trace = go.Scatter(name = "temperature fit", x=t_minV, y=fit_tank_TV, xaxis="x3", yaxis="y3", mode="lines", line={'width': 3})
new_fig = go.Figure(fig)
new_fig.add_trace(trace)
new_fig.update_layout()
#new_fig.show()

return fig, "{:.4f} kJ/hC".format(UA)
return new_fig, "{:.4f} kJ/hC".format(UA)

app.run(debug=True, use_reloader=False)

0 comments on commit 81f5beb

Please sign in to comment.