axelrvite commited on
Commit
d5e2d7a
·
verified ·
1 Parent(s): 588ca4f

Update app/api_utils.py

Browse files
Files changed (1) hide show
  1. app/api_utils.py +62 -3
app/api_utils.py CHANGED
@@ -11,8 +11,16 @@ import matplotlib.pyplot as plt
11
  import io
12
  import base64
13
  import numpy as np
 
 
14
 
15
 
 
 
 
 
 
 
16
  def comp_iv_curve(filename):
17
  path_data = tempfile.gettempdir()
18
  path_model = os.path.join(os.getcwd(), "models", "final_model_mlp.h5")
@@ -26,8 +34,10 @@ def comp_iv_curve(filename):
26
  processor = SolarDataProcessor(path_input, path_data)
27
  X_scaled, I_list, V_list = processor.preprocess_input([filename], path_data)
28
  params, I_model = visualizer.plot_comparaison_courbes(processor, model, X_scaled, I_list, V_list, Plot = False)
29
-
30
- return params, I_model
 
 
31
 
32
  def comp_iv_curve_cnn1d(filename):
33
  path_data = tempfile.gettempdir()
@@ -49,7 +59,56 @@ def comp_iv_curve_cnn1d(filename):
49
 
50
  params, I_model = visualizer.plot_comparaison_courbes(processor, model, X_scaled, I_list, V_list, Plot=False)
51
 
52
- return params, I_model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  def read_iv_csv(filepath):
55
  points = []
 
11
  import io
12
  import base64
13
  import numpy as np
14
+ from scipy.optimize import curve_fit
15
+ from pvlib.pvsystem import i_from_v
16
 
17
 
18
+ def iv_curve(voltage, rsh, rs, nNsVth, J0, Jph):
19
+ """
20
+ Compute the current for a given voltage based on the single-diode equation.
21
+ """
22
+ return i_from_v(voltage, Jph,J0, rs, rsh, nNsVth,method="lambertw")
23
+
24
  def comp_iv_curve(filename):
25
  path_data = tempfile.gettempdir()
26
  path_model = os.path.join(os.getcwd(), "models", "final_model_mlp.h5")
 
34
  processor = SolarDataProcessor(path_input, path_data)
35
  X_scaled, I_list, V_list = processor.preprocess_input([filename], path_data)
36
  params, I_model = visualizer.plot_comparaison_courbes(processor, model, X_scaled, I_list, V_list, Plot = False)
37
+
38
+ param_fit, I_after_fit = fit_after_model(I_list, V_list, params)
39
+
40
+ return param_fit, I_after_fit
41
 
42
  def comp_iv_curve_cnn1d(filename):
43
  path_data = tempfile.gettempdir()
 
59
 
60
  params, I_model = visualizer.plot_comparaison_courbes(processor, model, X_scaled, I_list, V_list, Plot=False)
61
 
62
+ param_fit, I_after_fit = fit_after_model(I_list, V_list, params)
63
+
64
+ return param_fit, I_after_fit
65
+
66
+
67
+ def fit_after_model(I_list, V_list, params):
68
+ #permet de rééfectuer une descente de gradient avec comme préfit les paramètres du modèle
69
+ k = 1.3806503e-23 # Boltzmann constant (J/K)
70
+ q = 1.60217646e-19 # Elementary charge (C)
71
+
72
+ #self.Tmes = int(chemin.split("\\")[-1].split("_")[1]) + 273.15
73
+ Tmes = 23 + 273.15
74
+
75
+ Vth = k * Tmes / q
76
+
77
+ params_to_fit = { "Rsh": params['Rsh'],
78
+ "Rs": params['Rs'],
79
+ "nVth": params['n']*Vth,
80
+ "J0": params['J0'],
81
+ "Jph": params['Jph']}
82
+ bounds = {
83
+ "Rsh": [1, 1e9],
84
+ "Rs": [0.1, 25],
85
+ "nVth": [0.8*Vth, 3*Vth],
86
+ "J0": [1e-20, 1e-5],
87
+ }
88
+
89
+ if params_to_fit["Jph"] > 0:
90
+ low_bounds = [bounds["Rsh"][0],bounds["Rs"][0], bounds["nVth"][0], bounds["J0"][0], params_to_fit["Jph"] * 0.8]
91
+ high_bounds = [bounds["Rsh"][1],bounds["Rs"][1], bounds["nVth"][1], bounds["J0"][1],params_to_fit["Jph"] * 1.2]
92
+ else:
93
+ low_bounds = [bounds["Rsh"][0],bounds["Rs"][0], bounds["nVth"][0], bounds["J0"][0], params_to_fit["Jph"] * 1.2]
94
+ high_bounds = [bounds["Rsh"][1],bounds["Rs"][1], bounds["nVth"][1], bounds["J0"][1],params_to_fit["Jph"] * 0.8]
95
+
96
+ # Ajustement automatique des paramètres s'ils dépassent les bornes
97
+ params_to_fit["Rsh"] = min(max(params_to_fit["Rsh"], bounds["Rsh"][0]), bounds["Rsh"][1])
98
+ params_to_fit["Rs"] = min(max(params_to_fit["Rs"], bounds["Rs"][0]), bounds["Rs"][1])
99
+ params_to_fit["nVth"] = min(max(params_to_fit["nVth"], bounds["nVth"][0]), bounds["nVth"][1])
100
+ params_to_fit["J0"] = min(max(params_to_fit["J0"], bounds["J0"][0]), bounds["J0"][1])
101
+
102
+ popt, pcov = curve_fit(iv_curve, V_list[0], -I_list[0],
103
+ p0=list(params_to_fit.values()), bounds=(low_bounds, high_bounds), maxfev=10000)
104
+ param_fit = { "Rsh": popt[0],
105
+ "Rs": popt[1],
106
+ "nVth": popt[2],
107
+ "J0": popt[3],
108
+ "Jph":popt[4]}
109
+ param_fit["n"] = param_fit["nVth"] / Vth
110
+ I_after_fit = -iv_curve(V_list[0], param_fit['Rsh'], param_fit['Rs'], param_fit['n']*Vth, param_fit['J0'], param_fit['Jph'])
111
+ return param_fit, I_after_fit
112
 
113
  def read_iv_csv(filepath):
114
  points = []