-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSBT.py
2257 lines (1916 loc) · 162 KB
/
SBT.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
import scipy.special as sp
from scipy.special import erf,erfc, jv, yv,exp1
import pdb
import scipy.io
import math
from scipy.interpolate import RegularGridInterpolator
#%% -------
# 1. Input
# Generally, the user should only make changes to this section
#---------
## SBT bodel settings
clg_configuration = 1 #Must be 1 or 2. "1" mean co-axial, "2" U-loop
sbt_version = 1 #Must be 1 or 2. 1 means SBT v1 (Temperature only); 2 means SBT v2 (Temperature and Pressure including allowing TP dependent fluid properties)
## Scenario settings applicable in both SBT v1 and v2
m = 20 #Total fluid mass flow rate [kg/s]. m must be provided if the user sets variableflowrate to 0.
Tin = 20 #Constant injection temperature [deg.C]
cp_f = 4200 #Fluid specific heat capacity [J/kgK]
rho_f = 1000 #Fluid density [kg/m3]
k_f = 0.667 #Fluid heat conductivity [W/m.K]
mu_f = 600*10**-6 #Fluid dynamic viscosity [Pa.s]
Tsurf = 20 #Surface temperature [deg C]
GeoGradient = 90/1000 #Geothermal gradient [C/m]
k_m = 2.83 #Rock thermal conductivity [W/m.K]
c_m = 825 #Rock specific heat capacity [J/kgK]
rho_m = 2875 #Rock density [kg/m3]
#co-axial geometry (required if clg_configuration is 1)
radius = 0.2286/2 #Wellbore radius [m] (everything is assumed open-hole)
radiuscenterpipe = 0.127/2 #Inner radius of inner pipe [m]
thicknesscenterpipe = 0.0127 #Thickness of inner pipe [m]
k_center_pipe = 0.006 #Thermal conductivity of insulation of center pipe wall [W/m/K]
coaxialflowtype = 1 #1 = CXA (fluid injection in annulus); 2 = CXC (fluid injection in center pipe)
#U-loop geometry (required if clg_configuration is 2)
radiusvertical = 0.2 #Radius of "vertical" injection and production well (open hole assumed for heat transfer) [m] (it is labeled here as vertical but it is allowed to be deviated)
radiuslateral = 0.15 #Radius of laterals (open hole assumed for heat transfer) [m]
numberoflaterals = 3 #Number of laterals (must be integer) [-]
lateralflowallocation = [1/3, 1/3, 1/3] #Distribution of flow accross laterals, must add up to 1 (it will get normalized below if sum does not equal to 1). Length of array must match number of laterals.
lateralflowmultiplier = 1 #Multiplier to allow decreasing the lateral flow rate to account for other laterals not simulated.
autoadjustlateralflowrates = 0 #Only used in SBT v2 U-loop. Must be 0 or 1. "0" means the flow rate in each lateral remains constant with a distribution as specified in lateralflowallocation. "1" means that the flow rate in each lateral is adjusted over time to ensure matching fluid pressures at the exit of each lateral. Sometimes this can cause convergence issues.
## SBT v1 specific settings
variableflowrate = 0 #Must be 0 or 1. "0" means the user provides a constant mass flow rate m. "1" means the user provides an excel file with a mass flow rate profile. [only works in SBT v1]
flowratefilename = 'MassFlowRate.xlsx' #Name of excel file with mass flow rate profile. Must be provided if user sets variableflowrate to 1. First column stores time in seconds, second column stores mass flow rate in kg/s. Time must start with 0 and end with the final simulation time chosen (as specified in the array times"). [only works in SBT v1]
variableinjectiontemperature = 0 #Must be 0 or 1. "0" means the user provides a constant injection temperature Tin. "1" means the user provides an excel file with an injection temperature profile. [only works in SBT v1]
injectiontemperaturefilename = 'InjectionTemperatures.xlsx' #Name of excel file with injection temperature profile. Must be provided if user sets variableinjectiontemperature to 1. First column stores time in seconds, second column stores injection temperature in degrees C. Time must start with 0 and end with the final simulation time chosen (as specified in the array times"). [only works in SBT v1]
## SBT v2 specific settings
fluid = 1 #Heat transfer fluid selection: 1 = H2O; 2 = CO2
Pin = 100 #Fluid input pressure [bar]
piperoughness = 1e-6 #Pipe/borehole roughness to calculate friction pressure drop [m]
variablefluidproperties = 1 #Must be 0 or 1. "0" means the fluid properties remain constant and are specified by cp_f, rho_f, k_f and mu_f. "1" means that the fluid properties (e.g., density) are calculated internally each time step and are a function of temperature and pressure.
## Simulation and SBT algorithm settings
times = np.concatenate((np.linspace(0,9900,100), np.logspace(np.log10(100*100), np.log10(20*365*24*3600), 75))) #simulation times [s] (must start with 0; to obtain smooth results, abrupt changes in time step size should be avoided. logarithmic spacing is recommended)
#times = np.concatenate((np.linspace(0,9900,100), np.linspace(10000, int(20*3.1*10**7), num=(int(20*3.1*10**7) - 10000) // 3600 + 1)))
#Note 1: When providing a variable injection temperature or flow rate, a finer time grid may be required. Below is an example with long term time steps of about 36 days.
#times = [0] + list(range(100, 10000, 100)) + list(np.logspace(np.log10(100*100), np.log10(0.1*365*24*3600), 40)) + list(np.arange(0.2*365*24*3600, 20*365*24*3600, 0.1*365*24*3600))
#Note 2: To capture the start-up effects, several small time steps are taken during the first 10,000 seconds in the time vector considered. To speed up the simulation, this can be avoided with limited impact on the long-term results. For example, an alternative time vector would be:
#times = [0] + list(range(100, 1000, 100)) + list(range(1000, 10000, 1000)) + list(np.logspace(np.log10(100*100), np.log10(20*365*24*3600), 75))
fullyimplicit = 1 #Should be between 0 and 1. Only required when clg_configuration is 2. Most stable is setting it to 1 which results in a fully implicit Euler scheme when calculting the fluid temperature at each time step. With a value of 0, the convective term is modelled using explicit Euler. A value of 0.5 would model the convective term 50% explicit and 50% implicit, which may be slightly more accurate than fully implicit.
accuracy = 5 #Must be 1,2,3,4 or 5 with 1 lowest accuracy and 5 highest accuracy. Lowest accuracy runs fastest. Accuracy level impacts number of discretizations for numerical integration and decision tree thresholds in SBT algorithm.
FMM = 0 #if 1, use fast multi-pole methold-like approach (i.e., combine old heat pulses to speed up simulation)
FMMtriggertime = 3600*24*10 #threshold time beyond which heat pulses can be combined with others [s]
#converge parameters for SBT v2
reltolerance = 1e-5 #Target maximum acceptable relative tolerance each time step [-]. Lower tolerance will result in more accurate results but requires longer computational time
maxnumberofiterations = 15 #Maximum number of iterations each time step [-]. Each time step, solution converges until relative tolerance criteria is met or maximum number of time steps is reached.
#(x,y,z) geometry of CLG heat exchanger
#The vectors storing the x-, y- and z-coordinates should be column vectors
#To obtain smooth results, abrupt changes in segment lengths should be avoided.
if clg_configuration == 1: #co-axial geometry: (x,y,z)-coordinates of centerline of co-axial heat exchanger [m]
#Example 1: 2 km vertical well
z = np.arange(0, -2001, -50).reshape(-1, 1)
x = np.zeros((len(z), 1))
y = np.zeros((len(z), 1))
# #Example 2: 2 km vertical well + 1km horizontal extent
# Depth = 2 #provided here in km
# HorizontalExtent = 1 #provided here in km
# verticaldepthsection = np.arange(0, -Depth*1000-1, -100)
# horizontalextentsection = np.arange(100, HorizontalExtent*1000+1, 100)
# z = np.concatenate((verticaldepthsection, verticaldepthsection[-1]*np.ones(len(horizontalextentsection)))).reshape(-1, 1)
# x = np.concatenate((np.zeros(len(verticaldepthsection)),horizontalextentsection)).reshape(-1, 1)
# y = np.zeros((len(z), 1))
elif clg_configuration == 2: #U-loop geometry: (x,y,z)-coordinates of centerline of injection well, production well and laterals
# Coordinates of injection well (coordinates are provided from top to bottom in the direction of flow)
zinj = np.arange(0, -2000 - 100, -100).reshape(-1, 1)
yinj = np.zeros((len(zinj), 1))
xinj = -1000 * np.ones((len(zinj), 1))
# Coordinates of production well (coordinates are provided from bottom to top in the direction of flow)
zprod = np.arange(-2000, 0 + 100, 100).reshape(-1, 1)
yprod = np.zeros((len(zprod), 1))
xprod = 1000 * np.ones((len(zprod), 1))
# (x, y, z)-coordinates of laterals are stored in three matrices (one each for the x, y, and z coordinates).
# The number of columns in each matrix corresponds to the number of laterals. The number of discretizations
# should be the same for each lateral. Coordinates are provided in the direction of flow; the first coordinate should match
# the last coordinate of the injection well, and the last coordinate should match the first coordinate of the
# production well
xlat = np.concatenate((np.array([-1000, -918, -814]), np.linspace(-706, 706, 14), np.array([814, 918, 1000]))\
).reshape(-1,1)
ylat = np.concatenate((100 * np.cos(np.linspace(-np.pi/2, 0, 3)), 100 * np.ones(14), 100 *\
np.cos(np.linspace(0, np.pi/2, 3)))).reshape(-1,1)
zlat = (-2000 * np.ones((len(xlat)))).reshape(-1,1)
xlat = np.hstack((xlat,xlat,np.linspace(-1000,1000,20).reshape(-1,1)))
ylat = np.hstack((ylat,-ylat, np.zeros(len(ylat)).reshape(-1,1)))
zlat = np.hstack((zlat,zlat,zlat))
# Merge x-, y-, and z-coordinates
x = np.concatenate((xinj, xprod))
y = np.concatenate((yinj, yprod))
z = np.concatenate((zinj, zprod))
for i in range(numberoflaterals):
x = np.concatenate((x, xlat[:, i].reshape(-1,1)))
y = np.concatenate((y, ylat[:, i].reshape(-1,1)))
z = np.concatenate((z, zlat[:, i].reshape(-1,1)))
# Make 3D figure of borehole geometry to make sure it looks correct
plt.close('all') #close all curent figures
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
if clg_configuration == 1: #co-axial geometry
ax.plot(x, y, z, 'k-o', linewidth=2)
ax.set_xlim([np.min(x) - 200, np.max(x) + 200])
ax.set_ylim([np.min(y) - 200, np.max(y) + 200])
ax.set_zlim([np.min(z) - 500, 0])
ax.set_zlabel('Depth (m)')
ax.set_xlabel('x (m)')
ax.set_ylabel('y (m)')
elif clg_configuration == 2: #U-loop geometry
ax.plot(xinj, yinj, zinj, 'b-o', linewidth=2)
ax.plot(xprod, yprod, zprod, 'r-o', linewidth=2)
for i in range(numberoflaterals):
ax.plot(xlat[:, i], ylat[:, i], zlat[:, i], 'k-o', linewidth=2)
#ax.axis('equal') # Uncomment this line to set the plotted geometry to correct scale with equal axis unit spacing
ax.set_xlim([np.min(x) - 200, np.max(x) + 200])
ax.set_ylim([np.min(y) - 200, np.max(y) + 200])
ax.set_zlim([np.min(z) - 500, 0])
ax.set_zlabel('Depth (m)')
ax.set_xlabel('x (m)')
ax.set_ylabel('y (m)')
ax.legend(['Injection Well', 'Production Well', 'Lateral(s)'])
plt.show()
#%% ----------------
# 2. Pre-Processing
# Generally, nothing should be changed by the user in this section
#------------------
g = 9.81 #Gravitational acceleration [m/s^2]
gamma = 0.577215665 # Euler's constant
alpha_f = k_f / rho_f / cp_f # Fluid thermal diffusivity [m2/s]
Pr_f = mu_f / rho_f / alpha_f # Fluid Prandtl number [-]
alpha_m = k_m / rho_m / c_m # Thermal diffusivity medium [m2/s]
if clg_configuration == 1: #co-axial geometry
outerradiuscenterpipe = radiuscenterpipe+thicknesscenterpipe #Outer radius of inner pipe [m]
A_flow_annulus = math.pi*(radius**2-outerradiuscenterpipe**2) #Flow area of annulus pipe [m^2]
A_flow_centerpipe = math.pi*radiuscenterpipe**2 #Flow area of center pipe [m^2]
Dh_annulus = 2*(radius-outerradiuscenterpipe) #Hydraulic diameter of annulus [m]
if sbt_version == 2:
eps_annulus = Dh_annulus*piperoughness #Relative roughness annulus [-]
eps_centerpipe = 2*radiuscenterpipe*piperoughness #Relative roughness inner pipe [-]
elif clg_configuration == 2: #U-loop geometry
interconnections = np.concatenate((np.array([len(xinj)],dtype=int), np.array([len(xprod)],dtype=int), (np.ones(numberoflaterals - 1, dtype=int) * len(xlat))))
interconnections = np.cumsum(interconnections) # lists the indices of interconnections between inj, prod, and laterals (this will used to take care of the duplicate coordinates of the start and end points of the laterals)
radiusvector = np.concatenate([np.ones(len(xinj) + len(xprod) - 2) * radiusvertical, np.ones(numberoflaterals * len(xlat) - numberoflaterals) * radiuslateral]) # Stores radius of each element in a vector [m]
Dvector = radiusvector * 2 # Diameter of each element [m]
lateralflowallocation = lateralflowallocation / np.sum(lateralflowallocation) # Ensure the sum equals 1
if sbt_version == 2:
radiusvectornodes = np.concatenate([np.full(len(xinj) + len(xprod), radiusvertical),np.full(numberoflaterals * xlat.shape[0] - 2 * numberoflaterals, radiuslateral)]) #Stores the element radius at each node [m] (The bottom nodes of the injector and producer are assume to have the radius of the injector and producer)
Dvectornodes = radiusvectornodes*2 #Vector with element diameter at each node [m]
FlowDistributionMidPoints = np.ones(len(xinj) + len(xprod) - 2)
FlowDistributionNodes = np.ones(len(xinj)+len(xprod))
for dd in range(numberoflaterals):
FlowDistributionMidPoints = np.concatenate([FlowDistributionMidPoints,lateralflowmultiplier * lateralflowallocation[dd] * np.ones(xlat.shape[0] - 1)])
FlowDistributionNodes = np.concatenate([FlowDistributionNodes,lateralflowmultiplier * lateralflowallocation[dd] * np.ones(xlat.shape[0] - 2)])
Area = math.pi*Dvector**2/4 #Vector with cross sectional flow area of each element at the element midpoints (all elements are assumed open hole) [m2]
AreaNodes = math.pi*Dvectornodes**2/4 #Vector with cross sectional flow area of each element at the nodes (all elements are assumed open hole) [m2]
eps = Dvector*piperoughness #Vector with relative roughness at midpoint of each element [-]
signofcorrection = 1 #Parameter used in the script for updating the lateral flow rates to equalize the fluid lateral outlet pressures
secondaverageabslateralflowcorrection = 1 #Parameter used in the script for updating the lateral flow rates to equalize the fluid lateral outlet pressures
mvector = m*FlowDistributionMidPoints #Array that stores the flow rate in each element (initially assumes uniform distribution of flow among laterals but this will be adjusted below (if autoadjustlateralflowrates = 1) to ensure identical pressure change accross all laterals) [kg/s]
mnodalvector = m*FlowDistributionNodes #Array that stores the flow rate at each node (initialy assumes uniform distribution of flow among laterals but this will be adjusted below (if autoadjustlateralflowrates = 1) to ensure identical pressure change accross all laterals) [kg/s]
mlateral = lateralflowallocation*m*lateralflowmultiplier #%Array that stores the flow rate through each lateral (initially assumes uniform flow distribution accross the laterals)
mlateralold = mlateral.copy()
if sbt_version == 2:
if variablefluidproperties == 0: # For computational purposes, use constant fluid property tables
# Define vectors for pressure and temperature
Pvector = np.array([[1, 1e9]])
Tvector = np.array([[1, 1e4]])
# Create 2x2 arrays with constant fluid properties
density = np.array([[rho_f] * 2] * 2)
heatcapacity = np.array([[cp_f] * 2] * 2)
thermalconductivity = np.array([[k_f] * 2] * 2)
viscosity = np.array([[mu_f] * 2] * 2)
thermalexpansion = np.array([[0] * 2] * 2) # Incompressible fluid has zero thermal expansion coefficient
else: # If variable fluid properties, import pre-generated tables
print('Loading fluid properties...')
if fluid == 1: # H2O
try:
mat = scipy.io.loadmat('properties_H2O.mat')
Pvector = mat['Pvector']
Tvector = mat['Tvector']
density = mat['density']
enthalpy = mat['enthalpy']
entropy = mat['entropy']
heatcapacity = mat['heatcapacity']
phase = mat['phase']
thermalconductivity = mat['thermalconductivity']
thermalexpansion = mat['thermalexpansion']
viscosity = mat['viscosity']
print('Fluid properties for water loaded successfully')
except Exception as e:
print(f"Error loading properties for water: {e}")
raise
elif fluid == 2: #CO2
try:
mat = scipy.io.loadmat('properties_CO2.mat')
Pvector = mat['Pvector']
Tvector = mat['Tvector']
density = mat['density']
enthalpy = mat['enthalpy']
entropy = mat['entropy']
heatcapacity = mat['heatcapacity']
phase = mat['phase']
thermalconductivity = mat['thermalconductivity']
thermalexpansion = mat['thermalexpansion']
viscosity = mat['viscosity']
print('Fluid properties for CO2 loaded successfully')
except Exception as e:
print(f"Error loading properties for CO2: {e}")
raise
else:
print('No valid fluid selected')
exit()
#Prepare interpolators
Pvector_1d = Pvector.ravel()
Tvector_1d = Tvector.ravel()
interpolator_density = RegularGridInterpolator((Pvector_1d, Tvector_1d), density)
interpolator_heatcapacity = RegularGridInterpolator((Pvector_1d, Tvector_1d), heatcapacity)
interpolator_thermalconductivity = RegularGridInterpolator((Pvector_1d, Tvector_1d), thermalconductivity)
interpolator_thermalexpansion = RegularGridInterpolator((Pvector_1d, Tvector_1d), thermalexpansion)
interpolator_viscosity = RegularGridInterpolator((Pvector_1d, Tvector_1d), viscosity)
if variablefluidproperties == 1:
interpolator_enthalpy = RegularGridInterpolator((Pvector_1d, Tvector_1d), enthalpy)
interpolator_entropy = RegularGridInterpolator((Pvector_1d, Tvector_1d), entropy)
interpolator_phase = RegularGridInterpolator((Pvector_1d, Tvector_1d), phase)
Deltaz = np.sqrt((x[1:] - x[:-1]) ** 2 + (y[1:] - y[:-1]) ** 2 + (z[1:] - z[:-1]) ** 2) # Length of each segment [m]
Deltaz = Deltaz.reshape(-1)
if clg_configuration == 2:
Deltaz = np.delete(Deltaz, interconnections - 1) # Removes the phantom elements due to duplicate coordinates
TotalLength = np.sum(Deltaz) # Total length of all elements (for informational purposes only) [m]
# Quality Control
if clg_configuration == 1: #co-axial geometry
LoverR = Deltaz / radius # Ratio of pipe segment length to radius along the wellbore [-]
elif clg_configuration == 2: #U-loop geometry
LoverR = Deltaz / radiusvector # Ratio of pipe segment length to radius along the wellbore [-]
smallestLoverR = np.min(LoverR) # Smallest ratio of pipe segment length to pipe radius. This ratio should be larger than 10. [-]
if smallestLoverR < 10:
print('Warning: smallest ratio of segment length over radius is less than 10. Good practice is to keep this ratio larger than 10.')
if clg_configuration == 1: #co-axial geometry
RelativeLengthChanges = (Deltaz[1:] - Deltaz[:-1]) / Deltaz[:-1]
elif clg_configuration == 2: #U-loop geometry
if numberoflaterals > 1:
DeltazOrdered = np.concatenate((Deltaz[0:(interconnections[0]-1)], Deltaz[(interconnections[1]-2):(interconnections[2]-3)], Deltaz[(interconnections[0]-1):(interconnections[1]-2)]))
else:
DeltazOrdered = np.concatenate((Deltaz[0:interconnections[0] - 1], Deltaz[interconnections[1] - 1:-1], Deltaz[interconnections[0]:interconnections[1] - 2]))
RelativeLengthChanges = (DeltazOrdered[1:] - DeltazOrdered[:-1]) / DeltazOrdered[:-1]
if max(abs(RelativeLengthChanges)) > 0.5:
print('Warning: abrupt change(s) in segment length detected, which may cause numerical instabilities. Good practice is to avoid abrupt length changes to obtain smooth results.')
if clg_configuration == 2: #additional quality control for U-loop geometry
for dd in range(1, numberoflaterals + 1):
if abs(xinj[-1] - xlat[0][dd - 1]) > 1e-12 or abs(yinj[-1] - ylat[0][dd - 1]) > 1e-12 or abs(zinj[-1] - zlat[0][dd - 1]) > 1e-12:
print(f'Error: Coordinate mismatch between bottom of injection well and start of lateral #{dd}')
if abs(xprod[0] - xlat[-1][dd - 1]) > 1e-12 or abs(yprod[0] - ylat[-1][dd - 1]) > 1e-12 or abs(zprod[0] - zlat[-1][dd - 1]) > 1e-12:
print(f'Error: Coordinate mismatch between bottom of production well and end of lateral #{dd}')
if len(lateralflowallocation) != numberoflaterals:
print('Error: Length of array "lateralflowallocation" does not match the number of laterals')
# Read injection temperature profile if provided
Tinstore = np.zeros(len(times))
if variableinjectiontemperature == 1 and sbt_version == 1:
# User has provided injection temperature in an Excel spreadsheet. (can currently only be used with sbt version 1)
num = pd.read_excel(injectiontemperaturefilename)
Tintimearray = np.array(num.iloc[:, 0])
Tintemperaturearray = np.array(num.iloc[:, 1])
# Quality control
if len(Tintimearray) < 2:
print('Error: Provided injection temperature profile should have at least 2 values')
if Tintimearray[0] != 0:
print('Error: First time value in the user-provided injection temperature profile does not equal 0 s')
if abs(Tintimearray[-1] - times[-1]) > 1e-5:
print('Error: Last time value in the user-provided injection temperature profile does not equal the final value in the "times" array')
else:
Tintimearray[-1] = times[-1] # Ensure final time values "exactly" match to prevent interpolation issues at the final time step
Tinstore[0] = Tintemperaturearray[0]
else:
Tinstore[0] = Tin
# Read mass flow rate profile if provided
mstore = np.zeros(len(times)) # The value for m used at each time step is stored in this array (is either constant or interpolated from a user-provided mass flow rate profile)
if variableflowrate == 1 and sbt_version == 1: # User has provided mass flow rate in an Excel spreadsheet. (can currently only be used with sbt version 1)
data = pd.read_excel(flowratefilename)
mtimearray = data.iloc[:, 0].values # This array has the times provided by the user
mflowratearray = data.iloc[:, 1].values # This array has the injection temperatures provided by the user
# Quality control
if len(mtimearray) < 2:
print('Error: Provided flow rate profile should have at least 2 values')
if mtimearray[0] != 0:
print('Error: First time value in user-provided flow rate profile does not equal to 0 s')
if abs(mtimearray[-1] - times[-1]) > 1e-5:
print('Error: Last time value in user-provided flow rate profile does not equal to final value in "times" array')
else:
mtimearray[-1] = times[-1] # Ensure final time values "exactly" match to prevent interpolation issues at the final time step
mstore[0] = mflowratearray[0]
else:
mstore[0] = m
#load accuracy parameters
print("Load accuracy parameters ...")
if accuracy == 1:
NoArgumentsFinitePipeCorrection = 25
NoDiscrFinitePipeCorrection = 200
NoArgumentsInfCylIntegration = 25
NoDiscrInfCylIntegration = 200
LimitPointSourceModel = 1.5
LimitCylinderModelRequired = 25
LimitInfiniteModel = 0.05
LimitNPSpacingTime = 0.1
LimitSoverL = 1.5
M = 3
elif accuracy == 2:
NoArgumentsFinitePipeCorrection = 50
NoDiscrFinitePipeCorrection = 400
NoArgumentsInfCylIntegration = 50
NoDiscrInfCylIntegration = 400
LimitPointSourceModel = 2.5
LimitCylinderModelRequired = 50
LimitInfiniteModel = 0.01
LimitNPSpacingTime = 0.04
LimitSoverL = 2
M = 4
elif accuracy == 3:
NoArgumentsFinitePipeCorrection = 100
NoDiscrFinitePipeCorrection = 500
NoArgumentsInfCylIntegration = 100
NoDiscrInfCylIntegration = 500
LimitPointSourceModel = 5
LimitCylinderModelRequired = 100
LimitInfiniteModel = 0.004
LimitNPSpacingTime = 0.02
LimitSoverL = 3
M = 5
elif accuracy == 4:
NoArgumentsFinitePipeCorrection = 200
NoDiscrFinitePipeCorrection = 1000
NoArgumentsInfCylIntegration = 200
NoDiscrInfCylIntegration = 1000
LimitPointSourceModel = 10
LimitCylinderModelRequired = 200
LimitInfiniteModel = 0.002
LimitNPSpacingTime = 0.01
LimitSoverL = 5
M = 10
elif accuracy == 5:
NoArgumentsFinitePipeCorrection = 400
NoDiscrFinitePipeCorrection = 2000
NoArgumentsInfCylIntegration = 400
NoDiscrInfCylIntegration = 2000
LimitPointSourceModel = 20
LimitCylinderModelRequired = 400
LimitInfiniteModel = 0.001
LimitNPSpacingTime = 0.005
LimitSoverL = 9
M = 20
print("Accuracy parameters loaded successfully")
#Precalculate SBT distribution
print("Precalculate SBT distributions ...")
timeforpointssource = max(Deltaz)**2 / alpha_m * LimitPointSourceModel # Calculates minimum time step size when point source model becomes applicable [s]
if clg_configuration == 1: #co-axial geometry
timeforlinesource = radius**2 / alpha_m * LimitCylinderModelRequired # Calculates minimum time step size when line source model becomes applicable [s]
elif clg_configuration == 2: #U-loop geometry
timeforlinesource = max(radiusvector)**2 / alpha_m * LimitCylinderModelRequired # Calculates minimum time step size when line source model becomes applicable [s]
timeforfinitelinesource = max(Deltaz)**2 / alpha_m * LimitInfiniteModel # Calculates minimum time step size when finite line source model should be considered [s]
#precalculate the thermal response with a line and cylindrical heat source. Precalculating allows to speed up the SBT algorithm.
#precalculate finite pipe correction
fpcminarg = min(Deltaz)**2 / (4 * alpha_m * times[-1])
fpcmaxarg = max(Deltaz)**2 / (4 * alpha_m * (min(times[1:] - times[:-1])))
Amin1vector = np.logspace(np.log10(fpcminarg) - 0.1, np.log10(fpcmaxarg) + 0.1, NoArgumentsFinitePipeCorrection)
finitecorrectiony = np.zeros(NoArgumentsFinitePipeCorrection)
for i, Amin1 in enumerate(Amin1vector):
Amax1 = (16)**2
if Amin1 > Amax1:
Amax1 = 10 * Amin1
Adomain1 = np.logspace(np.log10(Amin1), np.log10(Amax1), NoDiscrFinitePipeCorrection)
finitecorrectiony[i] = np.trapz(-1 / (Adomain1 * 4 * np.pi * k_m) * erfc(1/2 * np.power(Adomain1, 1/2)), Adomain1)
#precalculate besselintegration for infinite cylinder
if clg_configuration == 1: #co-axial geometry
besselminarg = alpha_m * (min(times[1:] - times[:-1])) / radius**2
besselmaxarg = alpha_m * timeforlinesource / radius**2
elif clg_configuration == 2: #U-loop geometry
besselminarg = alpha_m * (min(times[1:] - times[:-1])) / max(radiusvector)**2
besselmaxarg = alpha_m * timeforlinesource / min(radiusvector)**2
deltazbessel = np.logspace(-10, 8, NoDiscrInfCylIntegration)
argumentbesselvec = np.logspace(np.log10(besselminarg) - 0.5, np.log10(besselmaxarg) + 0.5, NoArgumentsInfCylIntegration)
besselcylinderresult = np.zeros(NoArgumentsInfCylIntegration)
for i, argumentbessel in enumerate(argumentbesselvec):
besselcylinderresult[i] = 2 / (k_m * np.pi**3) * np.trapz((1 - np.exp(-deltazbessel**2 * argumentbessel)) / (deltazbessel**3 * (jv(1, deltazbessel)**2 + yv(1, deltazbessel)**2)), deltazbessel)
print("SBT distributions calculated successfully")
N = len(Deltaz) # Number of elements
elementcenters = 0.5 * np.column_stack((x[1:], y[1:], z[1:])) + 0.5 * np.column_stack((x[:-1], y[:-1], z[:-1])) # Matrix that stores the mid point coordinates of each element
if clg_configuration == 2: #U-loop geometry
interconnections = interconnections - 1
elementcenters = np.delete(elementcenters, interconnections.reshape(-1,1), axis=0) # Remove duplicate coordinates
SMatrix = np.zeros((N, N)) # Initializes the spacing matrix, which holds the distance between center points of each element [m]
SoverL = np.zeros((N, N)) # Initializes the ratio of spacing to element length matrix
for i in range(N):
SMatrix[i, :] = np.sqrt((elementcenters[i, 0] - elementcenters[:, 0])**2 + (elementcenters[i, 1] - elementcenters[:, 1])**2 + (elementcenters[i, 2] - elementcenters[:, 2])**2)
SoverL[i, :] = SMatrix[i, :] / Deltaz[i] #Calculates the ratio of spacing between two elements and element length
#Element ranking based on spacinng is required for SBT algorithm as elements in close proximity to each other use different analytical heat transfer models than elements far apart
SortedIndices = np.argsort(SMatrix, axis=1, kind = 'stable') # Getting the indices of the sorted elements
SMatrixSorted = np.take_along_axis(SMatrix, SortedIndices, axis=1) # Sorting the spacing matrix
SoverLSorted = SMatrixSorted / Deltaz
#filename = 'smatrixpython.mat'
#scipy.io.savemat(filename, dict(SortedIndicesPython=SortedIndices,SMatrixSortedPython=SMatrixSorted))
mindexNPCP = np.where(np.min(SoverLSorted, axis=0) < LimitSoverL)[0][-1] # Finding the index where the ratio is less than the limit
midpointsx = elementcenters[:, 0] # x-coordinate of center of each element [m]
midpointsy = elementcenters[:, 1] # y-coordinate of center of each element [m]
midpointsz = elementcenters[:, 2] # z-coordinate of center of each element [m]
BBinitial = Tsurf - GeoGradient * midpointsz # Initial temperature at center of each element [degC]
if sbt_version == 2:
verticalchange = z[1:]-z[:-1] #Vertical change between nodes to calculate impact of gravity on pressure [m]
if clg_configuration == 2:
verticalchange = np.delete(verticalchange, interconnections.reshape(-1,1), axis=0)
verticalchange = verticalchange.ravel()
if clg_configuration == 2: #U-loop geometry
if sbt_version == 1:
previouswaterelements = np.zeros(N)
previouswaterelements[0:] = np.arange(-1,N-1)
for i in range(numberoflaterals):
previouswaterelements[interconnections[i + 1] - i-1] = len(xinj) - 2
previouswaterelements[len(xinj) - 1] = 0
lateralendpoints = []
for i in range(1,numberoflaterals+1):
lateralendpoints.append(len(xinj) - 2 + len(xprod) - 1 + i * ((xlat[:, 0]).size- 1))
lateralendpoints = np.array(lateralendpoints)
elif sbt_version == 2:
lateralfirstandlastnodes = [] #Initializes array that will store first and last nodal index of each lateral
for i in range(1, numberoflaterals + 1):
start_node = len(xinj) + len(xprod) + (i - 1) * xlat.shape[0]
end_node = len(xinj) + len(xprod) + (i - 1) * xlat.shape[0] + xlat.shape[0] -1
lateralfirstandlastnodes.extend([start_node, end_node])
if sbt_version == 2: #v2 calculates nodal fluid temperatures
if clg_configuration == 1:
Tfluidupnodes = Tsurf-GeoGradient*(z) #initial temperature of upflowing fluid at nodes [degC]
Tfluiddownnodes = Tsurf-GeoGradient*(z) #initial temperature of downflowing fluid at nodes [degC]
Tfluidupnodes = Tfluidupnodes.ravel()
Tfluiddownnodes = Tfluiddownnodes.ravel()
Tfluiddownmidpoints = 0.5*Tfluiddownnodes[1:]+0.5*Tfluiddownnodes[:-1] #initial midpoint temperature of downflowing fluid [deg.C]
Tfluidupmidpoints = 0.5*Tfluidupnodes[1:]+0.5*Tfluidupnodes[:-1] #initial midpoint temperature of upflowing fluid [deg.C]
elif clg_configuration == 2:
Tfluidnodes = Tsurf-GeoGradient*(z) #Initial fluid temperature at nodes [degC]
Tfluidnodes = np.delete(Tfluidnodes, lateralfirstandlastnodes, axis=0) #Remove duplicate nodes
Tfluidnodes = Tfluidnodes.ravel()
MaxSMatrixSorted = np.max(SMatrixSorted, axis=0)
indicesyoucanneglectupfront = alpha_m * (np.ones((N-1, 1)) * times) / (MaxSMatrixSorted[1:].reshape(-1, 1) * np.ones((1, len(times))))**2 / LimitNPSpacingTime
indicesyoucanneglectupfront[indicesyoucanneglectupfront > 1] = 1
lastneighbourtoconsider = np.zeros(len(times))
for i in range(len(times)):
lntc = np.where(indicesyoucanneglectupfront[:, i] == 1)[0]
if len(lntc) == 0:
lastneighbourtoconsider[i] = 0
else:
lastneighbourtoconsider[i] = max(1, lntc[-1])
distributionx = np.zeros((len(x) - 1, M + 1))
distributiony = np.zeros((len(x) - 1, M + 1))
distributionz = np.zeros((len(x) - 1, M + 1))
for i in range(len(x) - 1):
distributionx[i, :] = np.linspace(x[i], x[i + 1], M + 1).reshape(-1)
distributiony[i, :] = np.linspace(y[i], y[i + 1], M + 1).reshape(-1)
distributionz[i, :] = np.linspace(z[i], z[i + 1], M + 1).reshape(-1)
if clg_configuration == 2: #U-loop geometry
# Remove duplicates
distributionx = np.delete(distributionx, interconnections, axis=0)
distributiony = np.delete(distributiony, interconnections, axis=0)
distributionz = np.delete(distributionz, interconnections, axis=0)
if sbt_version == 2: #calculate and converge intial fluid properties in sbt v2
if clg_configuration == 1: #co-axial system
# Calculate initial pressure distribution
if fluid == 1: # H2O
Pfluidupnodes = Pin * 1e5 - 1000 * g * z # Initial guess for pressure distribution at nodes [Pa]
Pfluiddownnodes = np.copy(Pfluidupnodes) # As initial guess, assume downflowing and upflowing water have the same pressure at each depth [Pa]
Pfluidupmidpoints = Pin * 1e5 - 1000 * g * midpointsz # Initial guess for pressure distribution at midpoints [Pa]
Pfluiddownmidpoints = np.copy(Pfluidupmidpoints) #As initial guess, assume downflowing and upflowing water have the same pressure at each depth [Pa]
elif fluid == 2: # CO2
Pfluidupnodes = Pin * 1e5 - 500 * g * z # Initial guess for pressure distribution at nodes [Pa]
Pfluiddownnodes = np.copy(Pfluidupnodes) #As initial guess, assume downflowing and upflowing water have the same pressure at each depth [Pa]
Pfluidupmidpoints = Pin * 1e5 - 500 * g * midpointsz # Initial guess for pressure distribution at midpoints [Pa]
Pfluiddownmidpoints = np.copy(Pfluidupmidpoints) #As initial guess, assume downflowing and upflowing water have the same pressure at each depth [Pa]
kk = 1
maxrelativechange = 1
print("Calculating initial pressure field ... | Iteration = 1")
while kk < maxnumberofiterations and maxrelativechange > reltolerance: #Iterate to converge to initial pressure distribution
# Store old values
Pfluidupmidpoints_old = np.copy(Pfluidupmidpoints) #Store current guess for upflowing pressure distribution at midpoints to previous guess [Pa]
Pfluiddownmidpoints_old = np.copy(Pfluiddownmidpoints) #Store current guess for downflowing pressure distribution at midpoints to previous guess [Pa]
# Calculate fluid density
densityfluidupmidpoints = interpolator_density(np.array([[x, y] for x, y in zip(Pfluidupmidpoints, BBinitial + 273.15)])) #Calculate density distribution of upflowing fluid at midpoints [kg/m3]
densityfluiddownmidpoints = np.copy(densityfluidupmidpoints) #At time 0 there is no flow yet so upflowing and downflowing fluid have same pressure, temperature and density distribution
# Update pressure distributions
Pfluiddownnodes = Pin * 1e5 - np.cumsum(np.append([0], g * verticalchange * densityfluiddownmidpoints)) #Calculate pressure distribution of downflowing fluid at nodes [Pa]
Pfluidupnodes = np.copy(Pfluiddownnodes) #At time 0 there is no flow yet so upflowing and downflowing fluid have same pressure, temperature and density distribution
Pfluiddownmidpoints = 0.5 * (Pfluiddownnodes[1:] + Pfluiddownnodes[:-1]) #Pressure at midpoints is calculated by interpolating between nodes
Pfluidupmidpoints = np.copy(Pfluiddownmidpoints) #Upflowing and downflowing fluid have same initial pressure at time 0
# Calculate maximum relative change
maxrelativechange = np.max(np.abs((Pfluiddownmidpoints_old - Pfluiddownmidpoints) / Pfluiddownmidpoints_old))
kk += 1
# Print iteration status
print(f"Calculating initial pressure field ... | Iteration = {kk} | Max. Rel. change = {maxrelativechange}")
# Calculate initial density distribution
densityfluiddownnodes = interpolator_density(np.array([[x, y] for x, y in zip(Pfluiddownnodes, Tfluiddownnodes + 273.15)])) #After initial pressure distribution converged, calculate initial density distribution [kg/m3]
densityfluidupnodes = np.copy(densityfluiddownnodes) #Upflowing and downflowing fluid have the same initial density distribution at time 0
if maxrelativechange < reltolerance:
print("Initial pressure field calculated successfully")
else:
print("Initial pressure field calculated but maximum relative tolerance not met")
# Calculate velocity field
if coaxialflowtype == 1: # CXA
velocityfluiddownmidpoints = m / A_flow_annulus / densityfluiddownmidpoints #Downgoing fluid velocity at midpoints in annulus [m/s]
velocityfluidupmidpoints = m / A_flow_centerpipe / densityfluidupmidpoints #Upgoing fluid velocity at midpoint in center pipe [m/s]
velocityfluiddownnodes = m / A_flow_annulus / densityfluiddownnodes #Downgoing fluid velocity at nodes in annulus [m/s]
velocityfluidupnodes = m / A_flow_centerpipe / densityfluidupnodes #Upgoing fluid velocity at nodes in center pipe [m/s]
elif coaxialflowtype == 2: # CXC
velocityfluiddownmidpoints = m / A_flow_centerpipe / densityfluiddownmidpoints #Downgoing fluid velocity at midpoints in center pipe [m/s]
velocityfluidupmidpoints = m / A_flow_annulus / densityfluidupmidpoints #Upgoing fluid velocity at midpoint in annulus [m/s]
velocityfluiddownnodes = m / A_flow_centerpipe / densityfluiddownnodes #Downgoing fluid velocity at nodes in center pipe [m/s]
velocityfluidupnodes = m / A_flow_annulus / densityfluidupnodes #Upgoing fluid velocity at nodes in annulus [m/s]
# Obtain initial viscosity distribution [Pa*s]
viscosityfluiddownmidpoints = interpolator_viscosity(np.array([[x, y] for x, y in zip(Pfluiddownmidpoints, BBinitial + 273.15)]))
viscosityfluidupmidpoints = interpolator_viscosity(np.array([[x, y] for x, y in zip(Pfluidupmidpoints, BBinitial + 273.15)]))
# Obtain initial specific heat capacity distribution [J/kg/K]
heatcapacityfluiddownmidpoints = interpolator_heatcapacity(np.array([[x, y] for x, y in zip(Pfluiddownmidpoints, BBinitial + 273.15)]))
heatcapacityfluidupmidpoints = interpolator_heatcapacity(np.array([[x, y] for x, y in zip(Pfluidupmidpoints, BBinitial + 273.15)]))
# Obtain initial thermal conductivity distribution [W/m/K]
thermalconductivityfluiddownmidpoints = interpolator_thermalconductivity(np.array([[x, y] for x, y in zip(Pfluiddownmidpoints, BBinitial + 273.15)]))
thermalconductivityfluidupmidpoints = interpolator_thermalconductivity(np.array([[x, y] for x, y in zip(Pfluidupmidpoints, BBinitial + 273.15)]))
# Obtain initial thermal diffusivity distribution [m2/s]
alphafluiddownmidpoints = thermalconductivityfluiddownmidpoints / densityfluiddownmidpoints / heatcapacityfluiddownmidpoints
alphafluidupmidpoints = thermalconductivityfluidupmidpoints / densityfluidupmidpoints / heatcapacityfluidupmidpoints
# Obtain initial thermal expansion coefficient distribution [1/K]
thermalexpansionfluiddownmidpoints = interpolator_thermalexpansion(np.array([[x, y] for x, y in zip(Pfluiddownmidpoints, BBinitial + 273.15)]))
thermalexpansionfluidupmidpoints = interpolator_thermalexpansion(np.array([[x, y] for x, y in zip(Pfluidupmidpoints, BBinitial + 273.15)]))
# Obtain initial Prandtl number distribution
Prandtlfluiddownmidpoints = viscosityfluiddownmidpoints / densityfluiddownmidpoints / alphafluiddownmidpoints
Prandtlfluidupmidpoints = viscosityfluidupmidpoints / densityfluidupmidpoints / alphafluidupmidpoints
# Obtain initial Reynolds number distribution
if coaxialflowtype == 1: # CXA
Refluiddownmidpoints = densityfluiddownmidpoints * velocityfluiddownmidpoints * Dh_annulus / viscosityfluiddownmidpoints
Refluidupmidpoints = densityfluidupmidpoints * velocityfluidupmidpoints * (2 * radiuscenterpipe) / viscosityfluidupmidpoints
elif coaxialflowtype == 2: # CXC
Refluiddownmidpoints = densityfluiddownmidpoints * velocityfluiddownmidpoints * (2 * radiuscenterpipe) / viscosityfluiddownmidpoints
Refluidupmidpoints = densityfluidupmidpoints * velocityfluidupmidpoints * Dh_annulus / viscosityfluidupmidpoints
elif clg_configuration == 2: #U-loop system
# Calculate initial pressure distribution
if fluid == 1: # H2O
Pfluidnodes = Pin*1e5 - 1000*g*z #Initial guess for pressure distribution at nodes [Pa]
Pfluidnodes = np.delete(Pfluidnodes, lateralfirstandlastnodes, axis=0) #Remove duplicate nodes
Pfluidmidpoints = Pin*1e5 - 1000*g*midpointsz #Initial guess for pressure distribution at midpoints [Pa]
elif fluid == 2: # CO2
Pfluidnodes = Pin*1e5 - 500*g*z #Initial guess for pressure distribution at nodes [Pa]
Pfluidnodes = np.delete(Pfluidnodes, lateralfirstandlastnodes, axis=0) #Remove duplicate nodes
Pfluidmidpoints = Pin*1e5 - 500*g*midpointsz #Initial guess for pressure distribution at midpoints [Pa]
Pfluidnodes = Pfluidnodes.ravel()
Pfluidmidpoints = Pfluidmidpoints.ravel()
kk = 1
maxrelativechange = 1
print('Calculating initial pressure field ... | Iteration = 1')
lateralnodalstartpoints = [] # Stores the nodal start point of each lateral
lateralnodalendpoints = [] # Stores the nodal end point of each lateral
lateralmidstartpoints = [] # Stores the index of the midpoint of the first element of each lateral
lateralmidendpoints = [] # Stores the index of the midpoint of the last element of each lateral
for i in range(1, numberoflaterals + 1):
lateralnodalstartpoints.append(len(xinj) + len(xprod) + (i - 1) * (xlat.shape[0] - 2))
lateralnodalendpoints.append(len(xinj) + len(xprod) + i * (xlat.shape[0] - 2) - 1)
lateralmidstartpoints.append(len(xinj) + len(xprod) - 2 + (i - 1) * (xlat.shape[0] - 1))
lateralmidendpoints.append(len(xinj) + len(xprod) - 3 + i * (xlat.shape[0] - 1))
while kk < maxnumberofiterations and maxrelativechange > reltolerance: #Iterate to converge to initial pressure distribution
# Store old values
Pfluidmidpointsold = np.copy(Pfluidmidpoints) #Store current guess for pressure distribution at midpoints to old guess [Pa]
densityfluidmidpoints = interpolator_density(np.array([[x, y] for x, y in zip(Pfluidmidpoints, BBinitial + 273.15)])) #Calculate fluid density distribution at midpoints [kg/m3]
Pfluidnodes[:interconnections[0]+1] = Pin * 1e5 - np.cumsum(np.append([0], g * verticalchange[:interconnections[0]] * densityfluidmidpoints[:interconnections[0]])) #Calculate initial fluid pressure distribution at nodes in injection well [Pa]
for i in range(numberoflaterals): #Calculate initial fluid pressure distribution at nodes in each lateral [Pa]
Pfluidnodes[lateralnodalstartpoints[i]:lateralnodalendpoints[i]+1] = Pfluidnodes[interconnections[0]] - np.cumsum([g*verticalchange[lateralmidstartpoints[i]:lateralmidendpoints[i]]*densityfluidmidpoints[lateralmidstartpoints[i]:lateralmidendpoints[i]]])
Pfluidnodes[interconnections[0]+1] = Pfluidnodes[-1]-g*verticalchange[-1]*densityfluidmidpoints[-1] #Calculate initial fluid pressure at bottom node of production well [Pa]
Pfluidnodes[interconnections[0]+2:interconnections[1]+1] = Pfluidnodes[interconnections[0]+1]-np.cumsum(g*verticalchange[interconnections[0]:interconnections[1]-1]*densityfluidmidpoints[interconnections[0]:interconnections[1]-1]) #Calculate initial fluid pressure distribution at nodes in production well [Pa]
Pfluidmidpoints[:interconnections[0]] = 0.5*Pfluidnodes[:interconnections[0]]+0.5*Pfluidnodes[1:interconnections[0]+1] #Calculate initial fluid pressure distribution at midpoints in injection well [Pa]
Pfluidmidpoints[interconnections[0]:interconnections[1]-1] = 0.5*Pfluidnodes[interconnections[0]+1:interconnections[1]]+0.5*Pfluidnodes[interconnections[0]+2:interconnections[1]+1] #Calculate initial fluid pressure distribution at midpoints in production well [Pa]
for i in range(numberoflaterals): #Calculate initial fluid pressure distribution at midpoints in each lateral [Pa]
Pfluidmidpoints[lateralmidstartpoints[i]:lateralmidendpoints[i]+1] = (0.5 * Pfluidnodes[np.concatenate(([interconnections[0]], np.arange(lateralnodalstartpoints[i], lateralnodalendpoints[i] + 1)))] + 0.5 * Pfluidnodes[np.concatenate((np.arange(lateralnodalstartpoints[i], lateralnodalendpoints[i] + 1), [interconnections[0] + 1]))])
# Calculate maximum relative change
maxrelativechange = np.max(np.abs((Pfluidmidpointsold - Pfluidmidpoints) / Pfluidmidpointsold))
kk = kk+1
# Print iteration status
print(f"Calculating initial pressure field ... | Iteration = {kk} | Max. Rel. change = {maxrelativechange}")
densityfluidnodes = interpolator_density(np.array([[x, y] for x, y in zip(Pfluidnodes, Tfluidnodes + 273.15)])) #After initial pressure distribution converged, calculate initial density distribution [kg/m3]
if maxrelativechange < reltolerance:
print("Initial pressure field calculated successfully")
else:
print("Initial pressure field calculated but maximum relative tolerance not met")
#Calculate velocity field right at start up using intial density distribution and assuming initially uniform flow distribution accross all laterals
velocityfluidmidpoints = mvector/Area/densityfluidmidpoints #Fluid velocity at midpoints [m/s]
velocityfluidnodes = mnodalvector/AreaNodes/densityfluidnodes #Fluid velocity at nodes [m/s]
#Obtain initial visocity distribution of fluid [Pa*s]
viscosityfluidmidpoints = interpolator_viscosity(np.array([[x, y] for x, y in zip(Pfluidmidpoints, BBinitial + 273.15)]))
#Obtain initial specific heat capacity distribution of fluid [J/kg/K]
heatcapacityfluidmidpoints = interpolator_heatcapacity(np.array([[x, y] for x, y in zip(Pfluidmidpoints, BBinitial + 273.15)]))
#Obtain initial thermal conductivity distribution of fluid [W/m/K]
thermalconductivityfluidmidpoints = interpolator_thermalconductivity(np.array([[x, y] for x, y in zip(Pfluidmidpoints, BBinitial + 273.15)]))
#Obtain initial thermal diffusivity distribution of fluid [m2/s]
alphafluidmidpoints = thermalconductivityfluidmidpoints/densityfluidmidpoints/heatcapacityfluidmidpoints
#Obtain initial thermal expansion coefficient distribution of fluid [1/K]
thermalexpansionfluidmidpoints = interpolator_thermalexpansion(np.array([[x, y] for x, y in zip(Pfluidmidpoints, BBinitial + 273.15)]))
#Obtain initial Prandtl number distribution of fluid [-]
Prandtlfluidmidpoints = viscosityfluidmidpoints/densityfluidmidpoints/alphafluidmidpoints
#Obtain initial Reynold number distribution of fluid [-]
Refluidmidpoints = densityfluidmidpoints*velocityfluidmidpoints*Dvector/viscosityfluidmidpoints
# Initialize SBT algorithm linear system of equation matrices
if clg_configuration == 1: #co-axial geometry
L = np.zeros((4 * N, 4 * N)) # Will store the "left-hand side" of the system of equations
R = np.zeros((4 * N, 1)) # Will store the "right-hand side" of the system of equations
elif clg_configuration == 2: #U-loop geometry
L = np.zeros((3 * N, 3 * N)) # Will store the "left-hand side" of the system of equations
R = np.zeros((3 * N, 1)) # Will store the "right-hand side" of the system of equations
Q = np.zeros((N, len(times))) # Initializes the heat pulse matrix, i.e., the heat pulse emitted by each element at each time step
Toutput = np.zeros(len(times)) # Initializes the production temperatures array
if sbt_version == 2:
Poutput = np.zeros(len(times)) # Initializes the production pressure array
if clg_configuration == 1: #co-axial geometry
Tw_up_previous = BBinitial # At time zero, the initial upflowing fluid temperature corresponds to the initial local rock temperature
Tw_down_previous = BBinitial # At time zero, the initial downflowing fluid temperature corresponds to the initial local rock temperature
elif clg_configuration == 2: #U-loop geometry
Twprevious = BBinitial # At time zero, the initial fluid temperature corresponds to the initial local rock temperature
Toutput[0] = Tsurf # At time zero, the outlet temperature is the initial local fluid temperature at the surface, which corresponds to the surface temperature
if sbt_version == 2:
Poutput[0] = Pin # At time 0, the fluid flow is assumed zero and we assume the outlet pressure equals the inlet pressure
if sbt_version == 1:
if clg_configuration == 1: #co-axial geometry
Tw_up_Matrix = np.zeros((len(times), N)) # Initializes the matrix that holds the upflowing fluid temperature over time
Tw_up_Matrix[0, :] = Tw_up_previous
Tw_down_Matrix = np.zeros((len(times), N)) # Initializes the matrix that holds the downflowing fluid temperature over time
Tw_down_Matrix[0, :] = Tw_down_previous
elif clg_configuration == 2: #U-loop geometry
TwMatrix = np.zeros((len(times), N)) # Initializes the matrix that holds the fluid temperature over time
TwMatrix[0, :] = Twprevious
elif sbt_version == 2:
if clg_configuration == 1: #co-axial geometry
Tfluidupnodesstore = np.zeros((N + 1, len(times))) #Initializes the matrix that stores the upflowing fluid temperature at the nodes
Tfluiddownnodesstore = np.zeros((N + 1, len(times))) #Initializes the matrix that stores the downflowing fluid temperature at the nodes
Tfluidupmidpointsstore = np.zeros((N, len(times))) #Initializes the matrix that stores the upflowing fluid temperature at the midpoints
Tfluiddownmidpointsstore =np.zeros((N, len(times))) #Initializes the matrix that stores the downflowing fluid temperature at the midpoints
Pfluidupnodesstore = np.zeros((N + 1, len(times))) #Initializes the matrix that stores the upflowing fluid pressure at the nodes
Pfluiddownnodesstore = np.zeros((N + 1, len(times))) #Initializes the matrix that stores the downflowing fluid pressure at the nodes
Pfluidupmidpointsstore = np.zeros((N, len(times))) #Initializes the matrix that stores the upflowing fluid pressure at the midpoints
Pfluiddownmidpointsstore = np.zeros((N, len(times))) #Initializes the matrix that stores the downflowing fluid pressure at the midpoints
Tfluidupnodesstore[:,0] = Tfluidupnodes.ravel()
Tfluiddownnodesstore[:,0] = Tfluiddownnodes.ravel()
Tfluidupmidpointsstore[:,0] = BBinitial.ravel()
Tfluiddownmidpointsstore[:,0] = BBinitial.ravel()
Pfluidupnodesstore[:,0] = Pfluidupnodes.ravel()
Pfluiddownnodesstore[:,0] = Pfluiddownnodes.ravel()
Pfluidupmidpointsstore[:,0] = Pfluidupmidpoints.ravel()
Pfluiddownmidpointsstore[:,0] = Pfluiddownmidpoints.ravel()
elif clg_configuration == 2: #u-loop geometry
Tfluidnodesstore = np.zeros((len(xinj) + len(xprod) + numberoflaterals * (xlat.shape[0] - 2), len(times))) #Initializes the matrix that stores the fluid temperature at the nodes
Tfluidmidpointsstore = np.zeros((N, len(times))) #Initializes the matrix that stores the fluid temperature at the midpoints
Pfluidnodesstore = np.zeros((len(xinj) + len(xprod) + numberoflaterals * (xlat.shape[0] - 2), len(times))) #Initializes the matrix that stores the fluid pressure at the nodes
Pfluidmidpointsstore = np.zeros((N, len(times))) #Initializes the matrix that stores the fluid pressure at the midpoints
Tfluidlateralexitstore = np.zeros((numberoflaterals,len(times))) #Initializes the matrix that stores the fluid temperature at the exit of each lateral
Tfluidnodesstore[:,0] = Tfluidnodes
Tfluidmidpointsstore[:,0] = BBinitial
Tfluidmidpoints = BBinitial.copy()
Pfluidnodesstore[:,0] = Pfluidnodes
Pfluidmidpointsstore[:,0] = Pfluidmidpoints
Tfluidlateralexitstore[:,0] = Tfluidnodes[len(xinj)]
Pfluidlateralexit = np.zeros(numberoflaterals)
Deltahstar = np.zeros(N)
#Initialize FMM arrays
combinedtimes = np.array([0])
combinedQ = np.zeros((N, 1))
combinedtimes2ndlevel = np.array([0])
combinedtimes3rdlevel = np.array([0])
timesFMM = 0
QFMM = 0
print('Pre-processing completed successfully. Starting simulation ...')
#%% -------------
# 3. Calculating
# Generally, nothing should be changed by the user in this section
#---------------
tic = time.time() # start clock to measure computation time
for i in range(1, len(times)):
#print current simulation time
if times[i] < 60:
print('Time = ' + str(round(times[i]*100)/100) + ' seconds')
elif times[i] < 3600:
print('Time = ' + str(round(times[i]/60*100)/100) + ' minutes')
elif times[i] < 24*3600:
print('Time = ' + str(round(times[i]/3600*100)/100) + ' hours')
elif times[i] < 365*24*3600:
print('Time = ' + str(round(times[i]/3600/24*100)/100) + ' days')
else:
print('Time = ' + str(round(times[i]/3600/24/365*100)/100) + ' years')
Deltat = times[i] - times[i - 1] # Current time step size [s]
# If the user has provided an injection temperature profile, current value of Tin is calculated (only allowed in sbt version 1)
if variableinjectiontemperature == 1 and sbt_version == 1:
Tin = np.interp(times[i], Tintimearray, Tintemperaturearray)
Tinstore[i] = Tin # Value that is used for Tin at each time step gets stored for postprocessing purposes
# If the user has provided a flow rate profile, current value of m is calculated (only allowed in sbt version 1)
if variableflowrate == 1 and sbt_version == 1:
m = np.interp(times[i], mtimearray, mflowratearray)
mstore[i] = m # Value that is used for m at each time step gets stored for postprocessing purposes
# ------------------------------------
# CPCP (= Current pipe, current pulse)
# ------------------------------------
if clg_configuration == 1: #co-axial geometry
if alpha_m * Deltat / radius**2 > LimitCylinderModelRequired:
CPCP = np.ones(N) * 1 / (4 * np.pi * k_m) * exp1(radius**2 / (4 * alpha_m * Deltat)) # Use line source model if possible
else:
CPCP = np.ones(N) * np.interp(alpha_m * Deltat / radius**2, argumentbesselvec, besselcylinderresult) # Use cylindrical source model if required
if Deltat > timeforfinitelinesource: # For long time steps, the finite length correction should be applied
CPCP = CPCP + np.interp(Deltaz**2 / (4 * alpha_m * Deltat), Amin1vector, finitecorrectiony)
elif clg_configuration == 2: #U-loop geometry
if alpha_m * Deltat / max(radiusvector)**2 > LimitCylinderModelRequired:
CPCP = np.ones(N) * 1 / (4 * np.pi * k_m) * exp1(radiusvector**2 / (4 * alpha_m * Deltat)) # Use line source model if possible
else:
CPCP = np.ones(N) * np.interp(alpha_m * Deltat / radiusvector**2, argumentbesselvec, besselcylinderresult) # Use cylindrical source model if required
if Deltat > timeforfinitelinesource: # For long time steps, the finite length correction should be applied
CPCP = CPCP + np.interp(Deltaz**2 / (4 * alpha_m * Deltat), Amin1vector, finitecorrectiony)
# ---------------------------------
# CPOP (= Current pipe, old pulses)
# ---------------------------------
if i > 1: # After the second time step, we need to keep track of previous heat pulses
CPOP = np.zeros((N, len(timesFMM)-1))
indexpsstart = 0
indexpsend = np.where(timeforpointssource < (times[i] - timesFMM[1:]))[-1]
if indexpsend.size > 0:
indexpsend = indexpsend[-1] + 1
else:
indexpsend = indexpsstart - 1
if indexpsend >= indexpsstart: # Use point source model if allowed
CPOP[:, 0:indexpsend] = Deltaz * np.ones((N, indexpsend)) / (4 * np.pi * np.sqrt(alpha_m * np.pi) * k_m) * (
np.ones(N) * (1 / np.sqrt(times[i] - timesFMM[indexpsstart + 1:indexpsend + 2]) -
1 / np.sqrt(times[i] - timesFMM[indexpsstart:indexpsend+1])))
indexlsstart = indexpsend + 1
indexlsend = np.where(timeforlinesource < (times[i] - timesFMM[1:]))[0]
if indexlsend.size == 0:
indexlsend = indexlsstart - 1
else:
indexlsend = indexlsend[-1]
if indexlsend >= indexlsstart: # Use line source model for more recent heat pulse events
if clg_configuration == 1: #co-axial geometry
CPOP[:, indexlsstart:indexlsend+1] = np.ones((N,1)) * 1 / (4*np.pi*k_m) * (exp1(radius**2\
/ (4*alpha_m*(times[i]-timesFMM[indexlsstart:indexlsend+1])).reshape(1,len(4 * alpha_m * (times[i] - timesFMM[indexlsstart:indexlsend+1]))))-\
exp1((radius**2)\
/ (4 * alpha_m * (times[i]-timesFMM[indexlsstart+1:indexlsend+2])).reshape(1,len(4 * alpha_m * (times[i] - timesFMM[indexlsstart+1:indexlsend+2])))))
elif clg_configuration == 2: #U-loop geometry
CPOP[:, indexlsstart:indexlsend+1] = np.ones((N,1)) * 1 / (4*np.pi*k_m) * (exp1((radiusvector**2).reshape(len(radiusvector ** 2),1)\
/ (4*alpha_m*(times[i]-timesFMM[indexlsstart:indexlsend+1])).reshape(1,len(4 * alpha_m * (times[i] - timesFMM[indexlsstart:indexlsend+1]))))-\
exp1((radiusvector**2).reshape(len(radiusvector ** 2),1)\
/ (4 * alpha_m * (times[i]-timesFMM[indexlsstart+1:indexlsend+2])).reshape(1,len(4 * alpha_m * (times[i] - timesFMM[indexlsstart+1:indexlsend+2])))))
indexcsstart = max(indexpsend, indexlsend) + 1
#indexcsend = i - 2
indexcsend = len(timesFMM)-2
if indexcsstart <= indexcsend: # Use cylindrical source model for the most recent heat pulses
CPOPPH = np.zeros((CPOP[:, indexcsstart:indexcsend+1].shape))
CPOPdim = CPOP[:, indexcsstart:indexcsend+1].shape
CPOPPH = CPOPPH.T.ravel()
if clg_configuration == 1: #co-axial geometry
CPOPPH = (np.ones(N) * ( \
np.interp(alpha_m * (times[i] - timesFMM[indexcsstart:indexcsend+1]).reshape(len(times[i] - timesFMM[indexcsstart:indexcsend+1]),1) / (radius**2), argumentbesselvec, besselcylinderresult) - \
np.interp(alpha_m * (times[i] - timesFMM[indexcsstart+1: indexcsend+2]).reshape(len(times[i] - timesFMM[indexcsstart+1:indexcsend+2]),1) / (radius**2), argumentbesselvec, besselcylinderresult))).reshape(-1,1)
elif clg_configuration == 2: #U-loop geometry
CPOPPH = (np.ones(N) * ( \
np.interp(alpha_m * (times[i] - timesFMM[indexcsstart:indexcsend+1]).reshape(len(times[i] - timesFMM[indexcsstart:indexcsend+1]),1) / (radiusvector ** 2).reshape(len(radiusvector ** 2),1).T, argumentbesselvec, besselcylinderresult) - \
np.interp(alpha_m * (times[i] - timesFMM[indexcsstart+1: indexcsend+2]).reshape(len(times[i] - timesFMM[indexcsstart+1:indexcsend+2]),1) / (radiusvector ** 2).reshape(len(radiusvector ** 2),1).T, argumentbesselvec, besselcylinderresult))).reshape(-1,1)
CPOPPH=CPOPPH.reshape((CPOPdim),order='F')
CPOP[:, indexcsstart:indexcsend+1] = CPOPPH
indexflsstart = indexpsend + 1
indexflsend = np.where(timeforfinitelinesource < (times[i] - timesFMM[1:]))[-1]
if indexflsend.size == 0:
indexflsend = indexflsstart - 1
else:
indexflsend = indexflsend[-1] - 1
if indexflsend >= indexflsstart: # Perform finite length correction if needed
CPOP[:, indexflsstart:indexflsend+2] = CPOP[:, indexflsstart:indexflsend+2] + (np.interp(np.matmul((Deltaz.reshape(len(Deltaz),1) ** 2),np.ones((1,indexflsend-indexflsstart+2))) / np.matmul(np.ones((N,1)),(4 * alpha_m * (times[i] - timesFMM[indexflsstart:indexflsend+2]).reshape(len(times[i] - timesFMM[indexflsstart:indexflsend+2]),1)).T), Amin1vector, finitecorrectiony) - \
np.interp(np.matmul((Deltaz.reshape(len(Deltaz),1) ** 2),np.ones((1,indexflsend-indexflsstart+2))) / np.matmul(np.ones((N,1)),(4 * alpha_m * (times[i] - timesFMM[indexflsstart+1:indexflsend+3]).reshape(len(times[i] - timesFMM[indexflsstart:indexflsend+2]),1)).T), Amin1vector, finitecorrectiony))
# ------------------------------------------
# NPCP (= Neighbouring pipes, current pulse)
# ------------------------------------------
NPCP = np.zeros((N, N))
np.fill_diagonal(NPCP, CPCP)
spacingtest = alpha_m * Deltat / SMatrixSorted[:, 1:]**2 / LimitNPSpacingTime
maxspacingtest = np.max(spacingtest,axis=0)
if maxspacingtest[0] < 1:
maxindextoconsider = 0 #KB 11/21/2024: maybe this should be -1 at no index should be consdiered?????
else:
maxindextoconsider = np.where(maxspacingtest > 1)[0][-1]+1 #KB 11/21/2024: is plus 1 needed here?????
#Calculate and store neighbouring pipes for current pulse as point sources
if mindexNPCP < maxindextoconsider: #KB 11/21/2024: is plus 1 needed here?????
indicestocalculate = SortedIndices[:, mindexNPCP + 1:maxindextoconsider + 1]
indicestocalculatetranspose = indicestocalculate.T
indicestocalculatelinear = indicestocalculate.ravel()
indicestostorematrix = (indicestocalculate - 1) * N + np.arange(1, N) * np.ones((1, maxindextoconsider - mindexNPCP + 1))
indicestostorematrixtranspose = indicestostorematrix.T
indicestostorelinear = indicestostorematrix.ravel()
NPCP[indicestostorelinear] = Deltaz[indicestocalculatelinear] / (4 * np.pi * k_m * SMatrix[indicestostorelinear]) * erfc(SMatrix[indicestostorelinear] / np.sqrt(4 * alpha_m * Deltat))
#Calculate and store neighbouring pipes for current pulse as set of line sources
if mindexNPCP > 1 and maxindextoconsider > 0:
lastindexfls = min(mindexNPCP, maxindextoconsider + 1)
indicestocalculate = SortedIndices[:, 1:lastindexfls]
indicestocalculatetranspose = indicestocalculate.T
indicestocalculatelinear = indicestocalculate.ravel()
indicestostorematrix = (indicestocalculate) * N + np.arange(N).reshape(-1,1) * np.ones((1, lastindexfls - 1), dtype=int)
# #pdb.set_trace()
indicestostorematrixtranspose = indicestostorematrix.T
indicestostorelinear = indicestostorematrix.ravel()
midpointindices = np.matmul(np.ones((lastindexfls - 1, 1)), np.arange( N ).reshape(1,N)).T
midpointsindices = midpointindices.ravel().astype(int)
rultimate = np.sqrt(np.square((midpointsx[midpointsindices].reshape(len(midpointsindices),1)*( np.ones((1, M + 1))) - distributionx[indicestocalculatelinear,:])) +
np.square((midpointsy[midpointsindices].reshape(len(midpointsindices),1)*( np.ones((1, M + 1))) - distributiony[indicestocalculatelinear,:])) +
np.square((midpointsz[midpointsindices].reshape(len(midpointsindices),1)*( np.ones((1, M + 1))) - distributionz[indicestocalculatelinear,:])))
NPCP[np.unravel_index(indicestostorelinear, NPCP.shape, 'F')] = Deltaz[indicestocalculatelinear] / M * np.sum((1 - erf(rultimate / np.sqrt(4 * alpha_m * Deltat))) / (4 * np.pi * k_m * rultimate) * np.matmul(np.ones((N*(lastindexfls-1),1)),np.concatenate((np.array([1/2]), np.ones(M-1), np.array([1/2]))).reshape(-1,1).T), axis=1)
# ------------------------------------------
# NPOP (= Neighbouring pipes, old pulses)
# ------------------------------------------
if i>1:
NPOP = np.zeros((N * (int(lastneighbourtoconsider[i])+1), len(timesFMM) - 1))
BB = np.zeros((N, 1))
if i > 1 and lastneighbourtoconsider[i] > 0:
SMatrixRelevant = SMatrixSorted[:, 1 : int(lastneighbourtoconsider[i] + 2)]
SoverLRelevant = SoverLSorted[:, 1 : int(lastneighbourtoconsider[i]) + 2]
SortedIndicesRelevant = SortedIndices[:, 1 : int(lastneighbourtoconsider[i]) + 2]
#maxtimeindexmatrix = alpha_m * np.ones((N * int(lastneighbourtoconsider[i]), 1)) * (times[i] - times[1:i]) / (SMatrixRelevant.ravel().reshape(-1,1) * np.ones((1,i-1)))**2
maxtimeindexmatrix = alpha_m * np.ones((N * (int(lastneighbourtoconsider[i])+1), 1)) * (times[i] - timesFMM[1:]) / (SMatrixRelevant.T.ravel().reshape(-1,1) * np.ones((1,len(timesFMM)-1)))**2
#allindices = np.arange(N * int(lastneighbourtoconsider[i]) * (i - 1))
allindices = np.arange(N * (int(lastneighbourtoconsider[i])+1) * (len(timesFMM) - 1))
#if (i>=154):
#
pipeheatcomesfrom = np.matmul(SortedIndicesRelevant.T.ravel().reshape(len(SortedIndicesRelevant.ravel()),1), np.ones((1,len(timesFMM) - 1)))
pipeheatgoesto = np.arange(N).reshape(N,1) * np.ones((1, int(lastneighbourtoconsider[i]+1)))
pipeheatgoesto = pipeheatgoesto.transpose().ravel().reshape(len(pipeheatgoesto.ravel()),1) * np.ones((1, len(timesFMM) - 1))
# Delete everything smaller than LimitNPSpacingTime
#
indicestoneglect = np.where((maxtimeindexmatrix.transpose()).ravel() < LimitNPSpacingTime)[0]
maxtimeindexmatrix = np.delete(maxtimeindexmatrix.transpose().ravel(), indicestoneglect)
allindices = np.delete(allindices.transpose().ravel(), indicestoneglect)
indicesFoSlargerthan = np.where(maxtimeindexmatrix.ravel() > 10)[0]
#
indicestotakeforpsFoS = allindices[indicesFoSlargerthan]
allindices2 = allindices.copy()
allindices2[indicesFoSlargerthan] = []
SoverLinearized = SoverLRelevant.transpose().ravel().reshape(len(SoverLRelevant.ravel()),1) * np.ones((1, len(timesFMM) - 1))
indicestotakeforpsSoverL = np.where(SoverLinearized.transpose().ravel()[allindices2] > LimitSoverL)[0]