-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
228 lines (183 loc) · 8.19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import dash
from dash import dcc, html, Input, Output
import plotly.graph_objs as go
from scipy.optimize import curve_fit
import pandas as pd
import numpy as np
import base64
import io
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend
import matplotlib.pyplot as plt
from Nagata_LaTeX import *
nagata_a = None
nagata_b = None
nagata_f = None
nagata_alfa = None
nagata_p = None
def create_nagata_input_row(label, input_id, input_value):
return html.Div([
html.Div(label, style={'width': '50px', 'textAlign': 'center', 'paddingLeft': '20px', 'fontWeight': 'bold'}),
dcc.Input(
id=input_id,
type='number',
value=input_value,
disabled=True,
style={'width': '100px', 'color': 'white', 'textAlign': 'center', 'fontWeight': 'bold'}
)
], style={'display': 'flex', 'justifyContent': 'center', 'alignItems': 'center', 'marginBottom': '10px'})
# Inicializa o aplicativo Dash
app = dash.Dash(__name__)
# Layout do aplicativo
app.layout = html.Div([
html.Div([
html.Img(src='assets/Nagata_Logo.png'),
], style={'display': 'flex', 'width': '100%', 'justifyContent': 'center',
'margin-left': 'auto', 'margin-right': 'auto'}),
html.Div([
dcc.Upload(id='upload-data',
children=html.Button('Upload File', style={'color': 'white', 'fontWeight': 'bold'}),
multiple=False,
accept='.xlsx',
style={'width': '10%', 'color': 'white', 'textAlign': 'Center',
'justifyContent': 'center', 'alignItems': 'center', 'resize': 'none',
'margin-right': '10px'}
),
html.Div([
create_nagata_input_row("a:", 'a-value', ""),
], style={'display': 'flex', 'width': '150px', 'justifyContent': 'center', 'alignItems': 'center'}),
html.Div([
create_nagata_input_row("b:", 'b-value', ""),
], style={'display': 'flex', 'width': '150px', 'justifyContent': 'center', 'alignItems': 'center'}),
html.Div([
create_nagata_input_row("f:", 'f-value', ""),
], style={'display': 'flex', 'width': '150px', 'justifyContent': 'center', 'alignItems': 'center'}),
html.Div([
create_nagata_input_row("α:", 'alfa-value', ""),
], style={'display': 'flex', 'width': '150px', 'justifyContent': 'center', 'alignItems': 'center'}),
html.Div([
create_nagata_input_row("p:", 'p-value', ""),
], style={'display': 'flex', 'width': '150px', 'justifyContent': 'center', 'alignItems': 'center'}),
], id='div-nagata-fit',
style={'display': 'flex', 'width': '80%', 'justifyContent': 'center',
'margin-left': 'auto', 'margin-right': 'auto', 'padding': '20px'}),
html.Div([
html.Div([
html.Img(id='nagata-equation',
src='',
style={'width': '500px', 'display': 'block', 'marginLeft': 'auto', 'marginRight': 'auto',
'textAlign': 'center'}),
dcc.Download(id="download-python-code"),
html.Button('Download Python Code - Nagata Equation!',
id='download-button',
disabled=False,
style={'display': 'flex', 'width': '500px', 'justifyContent': 'center',
'color': 'white', 'fontWeight': 'bold',
'margin-left': 'auto', 'margin-right': 'auto',
'margin-top': '20px', 'margin-bottom': '20px'}),
], id='div-nagata-equation',
style={'display': 'none'}),
dcc.Graph(id='graph-output',
style={'display': 'None'}),
], id='div-graph-nagata',
style={'width': '80%', 'margin-left': 'auto', 'margin-right': 'auto', 'padding': '20px'}),
], style={'width': '80%', 'justifyContent': 'center', 'margin-left': 'auto', 'margin-right': 'auto', 'padding': '20px'})
@app.callback(
Output("download-python-code", "data"),
Input("download-button", "n_clicks"),
prevent_initial_call=True
)
def download(n_clicks):
global nagata_a, nagata_b, nagata_f, nagata_alfa, nagata_p
function_content = f"""
def Nagata(Re):
a= {nagata_a}
b = {nagata_b}
f = {nagata_f}
alfa = {nagata_alfa}
p = {nagata_p}
Np = a / Re + b * ((10 ** 3 + 0.6 * f * (Re ** alfa)) / (10 ** 3 + 1.6 * f * (Re ** alfa))) ** p
return Np
"""
file_path = 'assets/AjusteNagata.py'
# Escrevendo o conteúdo no arquivo ajuste.py
with open(file_path, "w") as file:
file.write(function_content)
return dcc.send_file(file_path)
# Callback para atualizar o gráfico e os valores dos parâmetros
@app.callback(
[Output('graph-output', 'figure'),
Output('graph-output', 'style'),
Output('a-value', 'value'),
Output('b-value', 'value'),
Output('f-value', 'value'),
Output('alfa-value', 'value'),
Output('p-value', 'value'),
Output('nagata-equation', 'src'),
Output('div-nagata-equation', 'style')],
[Input('upload-data', 'contents')]
)
def update_output(contents):
global nagata_a, nagata_b, nagata_f, nagata_alfa, nagata_p
if contents is None:
raise dash.exceptions.PreventUpdate
# Conversão do conteúdo para um DataFrame
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
df = pd.read_excel(io.BytesIO(decoded))
# Realiza o ajuste de curvas
Re_Exp = df.iloc[:, 0]
Np_Exp = df.iloc[:, 1]
max_value = Np_Exp.max()
def nagata_model(x, a, b, f, alfa, p):
return a / x + b * ((10 ** 3 + 0.6 * f * (x ** alfa)) / (10 ** 3 + 1.6 * f * (x ** alfa))) ** p
def nagata_model_Opt(x):
return a_opt / x + b_opt * (
(10 ** 3 + 0.6 * f_opt * (x ** alfa_opt)) / (10 ** 3 + 1.6 * f_opt * (x ** alfa_opt))) ** p_opt
# Curve Fit
bounds = ([0, 0, 0, 0, 0], [np.inf, np.inf, np.inf, np.inf, np.inf])
popt, pcov = curve_fit(f=nagata_model, xdata=Re_Exp, ydata=Np_Exp, bounds=bounds)
a_opt, b_opt, f_opt, alfa_opt, p_opt = popt
nagata_a, nagata_b, nagata_f, nagata_alfa, nagata_p = popt
nagata_a = round(nagata_a, 2)
nagata_b = round(nagata_b, 2)
nagata_f = round(nagata_f, 2)
nagata_alfa = round(nagata_alfa, 2)
nagata_p = round(nagata_p, 2)
def generate_custom_sequence(start, end):
# Gera sequências separadas para cada ordem de magnitude
sequences = [np.arange(10 ** i, 10 ** (i + 1), 10 ** i) for i in range(start, end)]
# Concatena todas as sequências em um único array
result = np.concatenate(sequences)
return result
Re = generate_custom_sequence(0, 8) # De 10^0 a 10^8
Np = nagata_model_Opt(Re)
# Cria a figura com os dados experimentais e a linha de ajuste
fig = go.Figure()
fig.add_trace(go.Scatter(x=Re_Exp, y=Np_Exp, mode='markers', name='Experimental Data'))
fig.add_trace(go.Scatter(x=Re, y=Np, mode='lines', name='Nagata Model Fit'))
# Definindo os valores e os textos dos ticks para eixos logarítmicos
tickvals = [10 ** i for i in range(0, 10)]
ticktext = [f"10<sup>{i}</sup>" for i in range(0, 10)]
# Define os eixos como logarítmicos
fig.update_layout(xaxis_type="log",
yaxis_type="log",
xaxis=dict(tickvals=tickvals,
ticktext=ticktext),
legend=dict(orientation="h",
x=0.5,
y=1.1,
xanchor="center",
yanchor="bottom")
)
# Nome do arquivo para salvar a imagem
filename = 'assets/equation.png'
# Gera a imagem da equação de Nagata
latex_to_png(nagata_a, nagata_b, nagata_f, nagata_alfa, nagata_p, filename)
# Retorna a figura e os valores dos parâmetros
return (fig, {'display': 'Block'},
nagata_a, nagata_b, nagata_f, nagata_alfa, nagata_p,
'assets/equation.png', {'display': 'Block'})
# Executa o aplicativo
if __name__ == '__main__':
app.run_server(debug=False)