-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
262 lines (221 loc) · 9.96 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import sys
import os
import numpy as np
import pandas as pd
from scipy.optimize import minimize, Bounds, NonlinearConstraint, fsolve
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QGridLayout, QLabel, QFileDialog
from wells_history_preprocessing import history_preprocessing
from reserves_calculation import calculate_oiz_for_all_wells
from displacement_characteristic_model import DisplacementCharacteristic
def choose_file_with_monthly_operating_report():
file_path: str; check: str; file_path, check = QFileDialog.getOpenFileName(
parent=None,
caption='Выберите файл с МЭР',
directory=os.path.dirname(__file__),
filter='Excel files (*.xlsx)'
)
if check:
df_initial: pd.DataFrame = pd.read_excel(file_path, sheet_name='МЭР')
df_boundary: pd.DataFrame = pd.read_excel(file_path, sheet_name='ОИЗ')
main_calculations(df_initial, df_boundary)
def main_calculations(
df_initial: pd.DataFrame,
df_boundary: pd.DataFrame
):
df_initial = history_preprocessing(df_initial, max_delta=365)
oiz_dict, boundaries_dict, wells_data_dict = create_dicts_with_essential_data(
df_initial=df_initial,
df_boundary=df_boundary
)
q_cutoff = df_boundary['Дебит отсечки'].values[0]
output = {}
for well in wells_data_dict.keys():
output[well] = []
for well in wells_data_dict.keys():
oil_rates = np.array(wells_data_dict[well][0], dtype='float')
fluid_rates = np.array(wells_data_dict[well][1], dtype='float')
time_in_prod = np.array(wells_data_dict[well][2], dtype='float')
dates = []
for g in range(oil_rates.size):
y = int(str(wells_data_dict[well][3][g])[0:4])
m = int(str(wells_data_dict[well][3][g])[5:7])
dates.append([y, m, 1])
try:
oiz_for_well = oiz_dict[well][0]
except:
output[well] = [None] * 9
continue
corey_oil_left = boundaries_dict[well][2]
if np.isnan(corey_oil_left):
corey_oil_left = -np.inf
corey_oil_right = np.inf
corey_water_left = boundaries_dict[well][3]
if np.isnan(corey_water_left):
corey_water_left = -np.inf
corey_water_right = np.inf
mef_left = boundaries_dict[well][4]
if np.isnan(mef_left):
mef_left = -np.inf
mef_right = boundaries_dict[well][5]
if np.isnan(mef_right):
mef_right = np.inf
q_oil_sum_for_well = np.sum(oil_rates) / 1000
niz_for_well = oiz_for_well + q_oil_sum_for_well
watercut_fact = (fluid_rates - oil_rates) / fluid_rates
watercut_fact[watercut_fact == -np.inf] = 0
watercut_fact[watercut_fact == np.inf] = 0
current_recovery_factor = q_oil_sum_for_well / niz_for_well
check_watercuts = len(watercut_fact[np.where(watercut_fact < watercut_fact[-1])]) > \
len(watercut_fact[np.where(watercut_fact > watercut_fact[-1])])
if check_watercuts:
new_watercut_fact = watercut_fact
new_oil_rates = oil_rates
new_fluid_rates = fluid_rates
else:
new_watercut_fact = watercut_fact[np.where(watercut_fact <= watercut_fact[-1])]
new_oil_rates = oil_rates[np.where(watercut_fact <= watercut_fact[-1])]
new_fluid_rates = fluid_rates[np.where(watercut_fact <= watercut_fact[-1])]
corey_oil = 3
corey_water = 2
mef = 3
params_list = [corey_oil, corey_water, mef]
displacement_characteristic_model = DisplacementCharacteristic(
oil_production=new_oil_rates,
liq_production=new_fluid_rates,
niz=niz_for_well,
considerations=boundaries_dict,
well_name=well,
mark=check_watercuts,
wc_fact=new_watercut_fact,
rf_now=current_recovery_factor
)
bnds = Bounds(
[corey_oil_left, corey_water_left, mef_left],
[corey_oil_right, corey_water_right, mef_right]
)
try:
nonlinear_con = NonlinearConstraint(
displacement_characteristic_model.to_conditions,
[-0.00001],
[0.00001]
)
res = minimize(
displacement_characteristic_model.solver,
params_list,
method='trust-constr',
bounds=bnds,
constraints=nonlinear_con
)
params_list = res.x
output[well] = res.x
except:
output[well] = ['Невозможно'] * 3
if check_watercuts:
marker_text = '-'
else:
marker_text = 'низкое качество данных'
# проверка характеристик вытеснения
if params_list[0] < 0.01 and params_list[1] < 0.01 and marker_text == '-':
marker_text = 'отброшены точки выше последней по обводнённости'
new_watercut_fact = watercut_fact[np.where(watercut_fact <= watercut_fact[-1])]
new_oil_rates = oil_rates[np.where(watercut_fact <= watercut_fact[-1])]
new_fluid_rates = fluid_rates[np.where(watercut_fact <= watercut_fact[-1])]
corey_oil = 3
corey_water = 2
mef = 3
params_list = [corey_oil, corey_water, mef]
displacement_characteristic_model = DisplacementCharacteristic(
oil_production=new_oil_rates,
liq_production=new_fluid_rates,
niz=niz_for_well,
considerations=boundaries_dict,
well_name=well,
mark=check_watercuts,
wc_fact=new_watercut_fact,
rf_now=current_recovery_factor
)
try:
nonlinear_con = NonlinearConstraint(
displacement_characteristic_model.to_conditions,
[-0.00001],
[0.0001]
)
res = minimize(
displacement_characteristic_model.solver,
params_list,
method='trust-constr',
bounds=bnds,
constraints=nonlinear_con
)
params_list = res.x
output[well] = res.x
except:
output[well] = ['Невозможно'] * 3
if (params_list[0] < 0.01 and params_list[1] < 0.01) or output[well][0] == 'Невозможно':
marker_text = 'отброшены все точки кроме последней'
output[well] = np.array([3, 2, 3])
last_watercut = watercut_fact[-1]
func = lambda x: last_watercut * x ** 3 + 3 * (2 * last_watercut - 1) * x ** 2 - \
3 * last_watercut * x + last_watercut
recovery_factor_new = fsolve(func, (0))
oiz_for_well = q_oil_sum_for_well / recovery_factor_new[0] - q_oil_sum_for_well
oiz_dict[well] = [oiz_for_well]
niz_for_well = oiz_for_well + q_oil_sum_for_well
current_recovery_factor = q_oil_sum_for_well / niz_for_well
output[well] = [output[well][0], output[well][1], output[well][2],
marker_text, [fluid_rates], [oil_rates],
[time_in_prod], [dates], current_recovery_factor]
#print(output)
def create_dicts_with_essential_data(
df_initial: pd.DataFrame,
df_boundary: pd.DataFrame
) -> tuple[dict, dict, dict]:
oiz_dict: dict = calculate_oiz_for_all_wells(
df_history=df_initial,
min_oiz=0.0,
r_max=1000.0,
year_min=0.0,
year_max=np.inf
).set_index('Скважина').T.to_dict('list')
df_boundary = df_boundary.drop(df_boundary.columns[1], axis='columns')
boundaries_dict: dict = df_boundary.set_index('№ скв.').T.to_dict('list')
time_in_production: np.ndarray = df_initial['Время работы в добыче, часы'].ravel()
wells: np.ndarray = df_initial['№ скважины'].ravel()
last_oil_rate: np.ndarray = df_initial['Добыча нефти за посл.месяц, т'].ravel()
last_fluid_rate: np.ndarray = df_initial['Добыча жидкости за посл.месяц, т'].ravel()
dates: np.ndarray = df_initial['Дата'].ravel()
unique_wells: list = list(set(wells))
wells_data_dict: dict = {}
for well in unique_wells:
wells_data_dict[well] = [[], [], [], []]
for i, well in enumerate(wells):
wells_data_dict[well][0] += [last_oil_rate[i]]
wells_data_dict[well][1] += [last_fluid_rate[i]]
wells_data_dict[well][2] += [time_in_production[i]]
wells_data_dict[well][3] += [dates[i]]
return oiz_dict, boundaries_dict, wells_data_dict
class MainWindow(QWidget):
def __init__(
self
):
super().__init__()
self.setWindowTitle('Статистика')
self.setFixedSize(QSize(600, 200))
calculate_button = QPushButton('Рассчитать')
calculate_button.clicked.connect(choose_file_with_monthly_operating_report)
download_button = QPushButton('Загрузить')
tool_description_label = QLabel(
'Инструмент для прогнозирования показателей \nбазовой добычи нефти'
' и обводнённости на основе \nмесячного эксплуатационного рапорта (МЭР)'
)
grid_box = QGridLayout()
grid_box.addWidget(calculate_button, 0, 0)
grid_box.addWidget(download_button, 0, 1)
grid_box.addWidget(tool_description_label, 1, 0, 1, 2)
self.setLayout(grid_box)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()