-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprobabilistic_hydrology_process.jl
More file actions
1142 lines (876 loc) · 46.4 KB
/
probabilistic_hydrology_process.jl
File metadata and controls
1142 lines (876 loc) · 46.4 KB
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
using CSV
using DataFrames
#using PrettyTables
using Distributions
using Statistics
using Dates
using LinearAlgebra
using Interpolations
include("core/hydrology_calculations.jl")
include("core/utilities.jl")
include("TexNetWebToolLauncherHelperJulia.jl")
include("core/bill_pfront.jl")
include("graphs/julia_fsp_graphs.jl")
using .HydroCalculations
using .Utilities
using .TexNetWebToolLauncherHelperJulia
using .BillPFront
using .JuliaFSPGraphs
const ARGS_FILE_NAME = "args.json"
const RESULTS_FILE_NAME = "results.json"
struct HydrologyParams
aquifer_thickness::Union{Float64, Int64}
porosity::Union{Float64, Int64}
permeability::Union{Float64, Int64}
fluid_density::Union{Float64, Int64}
dynamic_viscosity::Union{Float64, Int64}
fluid_compressibility::Union{Float64, Int64, Nothing}
rock_compressibility::Union{Float64, Int64, Nothing}
plus_minus::Dict{String, Union{Float64, Int64, Nothing}}
n_iterations::Union{Int64, Nothing, Float64}
end
function run_monte_carlo_hydrology(helper::TexNetWebToolLaunchHelperJulia,
params::HydrologyParams,
distribution_type::String="uniform",
extrapolate_injection_rates::Bool=false,
year_of_interest_date::Date=Date(year_of_interest-1, 12, 31),
year_of_interest::Int64=Dates.year(today()))
if distribution_type == "uniform"
# check if we are missing the plus_minus value for a parameter, set it to 0.0
for (key, value) in params.plus_minus
if isnan(value)
params.plus_minus[key] = 0.0
end
end
# check that the plus_minus values are not greater than the base values
for (key, value) in params.plus_minus
if key == "aquifer_thickness" && value > params.aquifer_thickness ||
key == "porosity" && value > params.porosity ||
key == "permeability" && value > params.permeability ||
key == "fluid_density" && value > params.fluid_density ||
key == "dynamic_viscosity" && value > params.dynamic_viscosity ||
key == "fluid_compressibility" && value > params.fluid_compressibility ||
key == "rock_compressibility" && value > params.rock_compressibility
add_message_with_step_index!(helper, 2, "Uncertainty +/- value ($value) for $key is greater than the base value", 2)
throw(ArgumentError("Uncertainty +/- value for $key is greater than the base value ($value)."))
end
end
# create the Uniform distributions for each parameter
distributions = Dict(
"aquifer_thickness" => create_bounded_uniform_distribution(params.aquifer_thickness, params.plus_minus["aquifer_thickness"], "aquifer_thickness"),
"porosity" => create_bounded_uniform_distribution(params.porosity, params.plus_minus["porosity"], "porosity"),
"permeability" => create_bounded_uniform_distribution(params.permeability, params.plus_minus["permeability"], "permeability"),
"fluid_density" => create_bounded_uniform_distribution(params.fluid_density, params.plus_minus["fluid_density"], "fluid_density"),
"dynamic_viscosity" => create_bounded_uniform_distribution(params.dynamic_viscosity, params.plus_minus["dynamic_viscosity"], "dynamic_viscosity"),
"fluid_compressibility" => create_bounded_uniform_distribution(params.fluid_compressibility, params.plus_minus["fluid_compressibility"], "fluid_compressibility"),
"rock_compressibility" => create_bounded_uniform_distribution(params.rock_compressibility, params.plus_minus["rock_compressibility"], "rock_compressibility")
)
elseif distribution_type == "gaussian" # ADITTIONAL FEATURE, NOT USED YET AND WE MIGHT NEED TO ADD A DIFFERENT DISTRIBUTION TYPE
# create the Gaussian distributions for each parameter
distributions = Dict(
"aquifer_thickness" => create_gaussian_distribution(params.aquifer_thickness, params.plus_minus["aquifer_thickness"]),
"porosity" => create_gaussian_distribution(params.porosity, params.plus_minus["porosity"]),
"permeability" => create_gaussian_distribution(params.permeability, params.plus_minus["permeability"]),
"fluid_density" => create_gaussian_distribution(params.fluid_density, params.plus_minus["fluid_density"]),
"dynamic_viscosity" => create_gaussian_distribution(params.dynamic_viscosity, params.plus_minus["dynamic_viscosity"]),
"fluid_compressibility" => create_gaussian_distribution(params.fluid_compressibility, params.plus_minus["fluid_compressibility"]),
"rock_compressibility" => create_gaussian_distribution(params.rock_compressibility, params.plus_minus["rock_compressibility"])
)
else
throw(ArgumentError("Invalid distribution type."))
end
# Get fault data
fault_data_path = get_dataset_file_path(helper, 5, "faults_model_inputs_output")
#fault_data_path = get_dataset_file_path(helper, 2, "det_geomechanics_results")
if fault_data_path === nothing
error("Required fault dataset not found.")
end
fault_df = CSV.read(fault_data_path, DataFrame)
num_faults = nrow(fault_df)
#println("Read faults_model_inputs_output.csv: $fault_df")
# Extract actual fault IDs from the input file
fault_id_col = "FaultID" in names(fault_df) ? "FaultID" : "ID"
if !(fault_id_col in names(fault_df))
error("No FaultID or ID column found in fault data while running Monte Carlo iterations for hydrology.")
end
# Extract the actual fault IDs
fault_ids = string.(fault_df[!, fault_id_col])
#println("Using fault IDs: $(join(fault_ids, ", "))")
# create the matrix to store the results of the Monte Carlo simulations
ppOnFaultMC = zeros(params.n_iterations, num_faults) # rows are iterations, columns are faults
# Create storage for parameter samples across all Monte Carlo iterations
# this will store the randomly sampled parameters for each iteration which we use to get the histogram data
hydro_samples = Dict{String, Vector{Float64}}()
hydro_samples["aquifer_thickness"] = Float64[]
hydro_samples["porosity"] = Float64[]
hydro_samples["permeability"] = Float64[]
hydro_samples["fluid_density"] = Float64[]
hydro_samples["dynamic_viscosity"] = Float64[]
hydro_samples["fluid_compressibility"] = Float64[]
hydro_samples["rock_compressibility"] = Float64[]
# Get injection well data
injection_wells_filepath, injection_data_type = get_injection_dataset_path(helper, 5)
if injection_wells_filepath === nothing
error("No injection well dataset found. Please provide injection well data.")
end
# Explicitly read API Number as String to preserve leading zeros
if injection_data_type == "injection_tool_data"
injection_wells_df = CSV.read(injection_wells_filepath, DataFrame, types = Dict(
"API Number" => String,
#"APINumber" => String,
"UIC Number" => String
), validate = false)
else
injection_wells_df = CSV.read(injection_wells_filepath, DataFrame)
end
# Get unique well IDs based on data format - moved outside the loops for efficiency
# if we are in the injection_tool_data format, we use the API Number column
# otherwise (for monthly and annual injection data) we use the WellID column
well_id_col = injection_data_type == "injection_tool_data" ? "API Number" : "WellID"
well_ids = unique(injection_wells_df[!, well_id_col])
# Pre-process well data outside the Monte Carlo loop
well_info = Dict{String, Dict{String, Any}}()
for well_id in well_ids
#println("Processing well ID: $well_id")
if injection_data_type == "injection_tool_data"
well_data = injection_wells_df[string.(injection_wells_df[!, well_id_col]) .== string(well_id), :]
if isempty(well_data)
# Try UIC Number
well_data = injection_wells_df[string.(injection_wells_df[!, "UIC Number"]) .== string(well_id), :]
if isempty(well_data)
continue
end
end
# Get well coordinates
well_lat = first(well_data[!, "Surface Latitude"])
well_lon = first(well_data[!, "Surface Longitude"])
# find injection period
dates = Date[]
# Check the type of the date values first
if eltype(well_data[!, "Date of Injection"]) <: Date
dates = well_data[!, "Date of Injection"]
else
# Need to parse from strings
try
# Try different date formats
dates = Date.(well_data[!, "Date of Injection"], dateformat"y-m-d")
catch
try
dates = Date.(well_data[!, "Date of Injection"], dateformat"m/d/y")
catch
try
dates = Date.(well_data[!, "Date of Injection"], dateformat"m/d/yyyy")
catch e
continue
end
end
end
end
if isempty(dates)
continue
end
# Convert years to integers properly
inj_start_year = Dates.year(minimum(dates))
inj_end_year = Dates.year(maximum(dates))
inj_start_date = minimum(dates)
inj_end_date = min(maximum(dates), year_of_interest_date)
#println("Well $well_id: start_year=$inj_start_year ($(typeof(inj_start_year))), end_year=$inj_end_year ($(typeof(inj_end_year)))")
else
# Annual or monthly format
well_data = injection_wells_df[string.(injection_wells_df[!, well_id_col]) .== string(well_id), :]
if isempty(well_data)
continue
end
# Get well coordinates directly
well_lat = first(well_data[!, "Latitude(WGS84)"])
well_lon = first(well_data[!, "Longitude(WGS84)"])
# Get injection period
# FSP Annual format has StartYear and EndYear columns
# FSP Monthly format has Year column
if "StartYear" in names(well_data)
inj_start_year = first(well_data[!, "StartYear"])
inj_end_year = first(well_data[!, "EndYear"])
inj_start_date = Date(inj_start_year, 1, 1)
inj_end_date = Date(inj_end_year-1, 12, 31)
elseif "Year" in names(well_data)
inj_start_year = minimum(well_data[!, "Year"])
inj_start_month = minimum(well_data[well_data[!, "Year"] .== inj_start_year, "Month"])
inj_start_date = Date(inj_start_year, inj_start_month, 1)
inj_end_year = maximum(well_data[!, "Year"])
inj_end_month = maximum(well_data[well_data[!, "Year"] .== inj_end_year, "Month"])
inj_end_date = Date(inj_end_year, inj_end_month, 1)
inj_end_date = lastdayofmonth(inj_end_date)
else
continue
end
end
# Check if the well's injection period started after the year of interest
if inj_start_date > year_of_interest_date
continue
end
actual_end_date = min(inj_end_date, year_of_interest_date)
if actual_end_date < inj_start_date
continue
end
# well info that is used in the Monte Carlo loop
well_info[string(well_id)] = Dict{String, Any}(
"lat" => well_lat,
"lon" => well_lon,
"inj_start_year" => inj_start_year,
"inj_start_date" => inj_start_date,
"inj_end_year" => inj_end_year,
"inj_end_date" => inj_end_date,
"actual_end_date" => actual_end_date
)
end
#println("Pre-processing well injection data with extrapolation = $(extrapolate_injection_rates)...")
prepared_well_data = Dict{String, Dict{String, Any}}()
for (well_id, info) in well_info
#println("Calling prepare_well_data_for_pressure_scenario for well $well_id")
#println("Types: well_id=$(typeof(well_id)), inj_start_year=$(typeof(info["inj_start_year"])), inj_start_date=$(typeof(info["inj_start_date"])), inj_end_year=$(typeof(info["inj_end_year"])), actual_end_date=$(typeof(info["actual_end_date"])), injection_data_type=$(typeof(injection_data_type)), year_of_interest=$(typeof(year_of_interest)), extrapolate_injection_rates=$(typeof(extrapolate_injection_rates)), year_of_interest_date=$(typeof(year_of_interest_date))")
# Prepare injection data
days, rates = prepare_well_data_for_pressure_scenario(
injection_wells_df,
well_id,
info["inj_start_year"],
info["inj_start_date"],
info["inj_end_year"],
info["actual_end_date"],
injection_data_type,
year_of_interest,
extrapolate_injection_rates,
year_of_interest_date
)
#println("Returned from prepare_well_data_for_pressure_scenario for well $well_id: got $(length(days)) days and $(length(rates)) rates")
if !isempty(days) && !isempty(rates)
# Store the data for pressure scenario
prepared_well_data[string(well_id)] = Dict{String, Any}(
"days" => days,
"rates" => rates,
"lat" => info["lat"],
"lon" => info["lon"],
"inj_start_date" => info["inj_start_date"] # Store start date for pressure calculation
)
end
end
# run the Monte Carlo simulations
for i in 1:params.n_iterations
# sample the parameters from the distributions
sampled_params = Dict(
"aquifer_thickness" => rand(distributions["aquifer_thickness"]),
"porosity" => rand(distributions["porosity"]),
"permeability" => rand(distributions["permeability"]),
"fluid_density" => rand(distributions["fluid_density"]),
"dynamic_viscosity" => rand(distributions["dynamic_viscosity"]),
"fluid_compressibility" => rand(distributions["fluid_compressibility"]),
"rock_compressibility" => rand(distributions["rock_compressibility"])
)
# Store the sampled parameters for histogram visualization
push!(hydro_samples["aquifer_thickness"], sampled_params["aquifer_thickness"])
push!(hydro_samples["porosity"], sampled_params["porosity"])
push!(hydro_samples["permeability"], sampled_params["permeability"])
push!(hydro_samples["fluid_density"], sampled_params["fluid_density"])
push!(hydro_samples["dynamic_viscosity"], sampled_params["dynamic_viscosity"])
push!(hydro_samples["fluid_compressibility"], sampled_params["fluid_compressibility"])
push!(hydro_samples["rock_compressibility"], sampled_params["rock_compressibility"])
# calculate storativity and transmissivity
S, T, rho = calcST(
sampled_params["aquifer_thickness"],
sampled_params["porosity"],
sampled_params["permeability"],
sampled_params["fluid_density"],
sampled_params["dynamic_viscosity"],
9.81,
sampled_params["fluid_compressibility"],
sampled_params["rock_compressibility"]
)
STRho = (S, T, rho)
for f in 1:num_faults
# Get fault coordinates
x_fault_km = fault_df[f, "Longitude(WGS84)"]
y_fault_km = fault_df[f, "Latitude(WGS84)"]
# Get fault ID for logging
fault_id = fault_ids[f]
# initialize pp
ppOnFault = 0.0
# Loop over wells using PRE-PROCESSED DATA
for (well_id, data) in prepared_well_data
# Use pre-processed injection data
# Calculate days from injection start to analysis date
evaluation_days_from_start = Float64((year_of_interest_date - data["inj_start_date"]).value + 1)
pressure_contribution = pfieldcalc_all_rates(
x_fault_km,
y_fault_km,
STRho,
data["days"],
data["rates"],
data["lon"],
data["lat"],
evaluation_days_from_start
)
# Add to total pressure for this fault
ppOnFault += pressure_contribution
end
# Ensure pressure is not negative
ppOnFault = max(0.0, ppOnFault)
# Store the result for this fault and iteration
ppOnFaultMC[i, f] = ppOnFault
end
end
# Convert results to a DataFrame
result_rows = []
for i in 1:params.n_iterations
for f in 1:num_faults
push!(result_rows, (
IterationID = i,
ID = fault_ids[f], # Use actual fault ID instead of numeric index
Pressure = ppOnFaultMC[i, f]
))
end
end
results_df = DataFrame(result_rows)
return results_df, hydro_samples
end
"""
Get the injection dataset path based on available data types
"""
function get_injection_dataset_path(helper::TexNetWebToolLaunchHelperJulia, step_index::Int)
for param_name in ["injection_wells_annual_prob_hydro", "injection_wells_monthly_prob_hydro", "injection_tool_data_prob_hydro"]
filepath = get_dataset_file_path(helper, step_index, param_name)
if filepath !== nothing
if param_name == "injection_wells_annual_prob_hydro"
injection_data_type = "annual_fsp"
return filepath, injection_data_type
elseif param_name == "injection_wells_monthly_prob_hydro"
injection_data_type = "monthly_fsp"
return filepath, injection_data_type
elseif param_name == "injection_tool_data_prob_hydro"
injection_data_type = "injection_tool_data"
return filepath, injection_data_type
end
end
end
return nothing, nothing
end
"""
Calculate slip potential by combining probabilistic geomechanics CDF with deterministic hydrology
Using Deterministic Hydrology to Calculate Slip Potential:
Once we have calculated the pore pressure added to a given fault (from deterministic hydrology) and the
probability of fault slip as a function of pore pressure increase (from probabilistic geomechanics), we can calculate the
cumulative probability of fault slip. We do this simply by slicing the probability shown
by the CDF curve in probabilistic geomechanics for the appropriate pore pressure increase
"""
function calculate_deterministic_slip_potential(prob_geo_cdf::DataFrame, det_hydro_pressures::DataFrame, year_of_interest::Union{Int, Nothing}=nothing)
# results df
slip_potential_df = DataFrame(
ID = String[],
Year = Int[],
Date = Date[],
slip_pressure = Float64[],
probability = Float64[]
)
# Get unique fault IDs
fault_ids = unique(prob_geo_cdf.ID)
# Filter deterministic hydrology data by year if specified
filtered_hydro_pressures = det_hydro_pressures
if !isnothing(year_of_interest) && "Date" in names(det_hydro_pressures)
filtered_hydro_pressures = det_hydro_pressures[year.(det_hydro_pressures.Date) .== year_of_interest, :]
#println("Filtered deterministic hydrology data to year $year_of_interest")
end
for fault_id in fault_ids
# Get fault's geomechanics CDF
fault_cdf = prob_geo_cdf[prob_geo_cdf.ID .== fault_id, :]
# Get all deterministic pressure entries for this fault
fault_pressures = filtered_hydro_pressures[filtered_hydro_pressures.ID .== fault_id, :]
if isempty(fault_pressures)
continue
end
if "Date" in names(det_hydro_pressures)
if !all(x -> x isa Date, det_hydro_pressures.Date)
error("det_hydro_pressures.Date contains non-Date values")
end
else
error("det_hydro_pressures is missing Date column")
end
# Process each pressure entry (could be multiple years)
for row in eachrow(fault_pressures)
pressure = row.slip_pressure
evaluation_date = row.Date
evaluation_year = year(evaluation_date)
# Sort the CDF by pore pressure
# Make sure we use the correct column names from prob_geo_cdf
slipPressureCol = "slip_pressure"
cumulativeProbCol = "probability"
sort!(fault_cdf, slipPressureCol)
# Find slip potential by interpolating the CDF
slip_potential = 0.0
# Ensure pressure is not negative
if pressure < 0.0
slip_potential = 0.0
# If pressure is less than minimum in CDF, slip potential is 0
elseif pressure < minimum(fault_cdf[!, slipPressureCol])
slip_potential = 0.0
# If pressure is greater than maximum in CDF, slip potential is 100%
elseif pressure > maximum(fault_cdf[!, slipPressureCol])
slip_potential = 100.0
else
# Interpolate to find slip potential
# CDF uses normalized cumulative probability (0-100%)
slip_potential = interpolate_cdf(fault_cdf[!, slipPressureCol], fault_cdf[!, cumulativeProbCol], pressure)
end
# Convert fault_id to String before adding to DataFrame
push!(slip_potential_df, (string(fault_id), evaluation_year, evaluation_date, pressure, slip_potential))
end
end
return slip_potential_df
end
"""
Calculate slip potential by combining probabilistic geomechanics CDF with probabilistic hydrology exceedance curve.
This integration calculates the overall fault slip potential.
"""
function calculate_probabilistic_slip_potential(prob_geo_cdf::DataFrame, prob_hydro_results::DataFrame)
# this dataframe will store the results
slip_potential_df = DataFrame(
ID = String[],
MeanPorePressure = Float64[],
SlipPotential = Float64[]
)
# prob_hydro_results are the raw monte carlo results, so we need to convert them to an exceedance curve
prob_hydro_cdf_data = prob_hydrology_cdf(prob_hydro_results)
# check both prob_hydro_cdf_data and prob_geo_cdf and see if their 'ID' column is String
# if not, convert it to String
if !(eltype(prob_hydro_cdf_data.ID) <: AbstractString)
prob_hydro_cdf_data.ID = string.(prob_hydro_cdf_data.ID)
end
if !(eltype(prob_geo_cdf.ID) <: AbstractString)
prob_geo_cdf.ID = string.(prob_geo_cdf.ID)
end
# Get unique fault IDs
fault_ids = unique(prob_geo_cdf.ID)
#println("Unique IDs in prob_hydro_cdf_data: ", unique(prob_hydro_cdf_data.ID))
#println("Unique IDs in prob_geo_cdf: ", unique(prob_geo_cdf.ID))
# print the first 10 rows of prob_hydro_cdf_data using pretty_table
#println("prob_hydro_cdf_data (first 10 rows):")
#pretty_table(prob_hydro_cdf_data[1:10, :])
# print the first 10 rows of prob_geo_cdf using pretty_table
#println("prob_geo_cdf (first 10 rows):")
#pretty_table(prob_geo_cdf[1:10, :])
#error("stop here")
# verify column names
geo_pressure_col = "slip_pressure" in names(prob_geo_cdf) ? "slip_pressure" : "pressure"
geo_prob_col = "probability" in names(prob_geo_cdf) ? "probability" : "cumulative_probability"
for fault_id in fault_ids
# Get fault's geomechanics CDF
fault_geo_cdf = prob_geo_cdf[prob_geo_cdf.ID .== fault_id, :]
sort!(fault_geo_cdf, geo_pressure_col)
# Get hydrology exceedance data for this fault
fault_hydro_exceedance = prob_hydro_cdf_data[prob_hydro_cdf_data.ID .== fault_id, :]
if isempty(fault_hydro_exceedance)
#println("No hydrology exceedance data for fault $fault_id")
continue
end
# Sort by pressure for intersection finding
sort!(fault_hydro_exceedance, :slip_pressure)
# Get mean pore pressure
fault_pressures = prob_hydro_results[prob_hydro_results.ID .== fault_id, :Pressure]
mean_pressure = mean(fault_pressures)
# round this to 2 decimal places
mean_pressure = round(mean_pressure, digits=2)
#println("fault_pressures max for fault $fault_id: $(maximum(fault_pressures))")
#println("fault_pressures min for fault $fault_id: $(minimum(fault_pressures))")
# Check for curve overlap - only calculate non-zero FSP if curves intersect
hydro_max_pressure = maximum(fault_hydro_exceedance.slip_pressure)
hydro_min_pressure = minimum(fault_hydro_exceedance.slip_pressure)
geo_max_pressure = maximum(fault_geo_cdf[!, geo_pressure_col])
geo_min_pressure = minimum(fault_geo_cdf[!, geo_pressure_col])
# Check if hydrology is entirely to the left of geomechanics
if hydro_max_pressure < geo_min_pressure
#println("Fault $fault_id: Hydrology curve entirely to left of geomechanics curve - FSP = 0.0")
push!(slip_potential_df, (fault_id, mean_pressure, 0.0))
continue
end
# Check if hydrology is entirely to the right of geomechanics
if hydro_min_pressure > geo_max_pressure
#println("Fault $fault_id: Hydrology curve entirely to right of geomechanics curve - FSP = 1.0")
push!(slip_potential_df, (fault_id, mean_pressure, 1.0))
continue
end
# Find the intersection point between the curves
# Combine all pressure points for evaluation
all_pressures = unique(vcat(fault_geo_cdf[!, geo_pressure_col], fault_hydro_exceedance.slip_pressure))
sort!(all_pressures)
# Filter to pressures where both curves are defined
valid_pressures = filter(p ->
p >= max(geo_min_pressure, hydro_min_pressure) &&
p <= min(geo_max_pressure, hydro_max_pressure),
all_pressures)
# Check each pressure point to find where curves cross
intersection_found = false
intersection_pressure = 0.0
intersection_probability = 0.0
for i in 1:(length(valid_pressures)-1)
p1 = valid_pressures[i]
p2 = valid_pressures[i+1]
# Evaluate both curves at p1
hydro_prob1 = interpolate_cdf_new(
fault_hydro_exceedance.slip_pressure,
fault_hydro_exceedance.probability,
p1)
geo_prob1 = interpolate_cdf_new(
fault_geo_cdf[!, geo_pressure_col],
fault_geo_cdf[!, geo_prob_col],
p1)
# Evaluate both curves at p2
hydro_prob2 = interpolate_cdf_new(
fault_hydro_exceedance.slip_pressure,
fault_hydro_exceedance.probability,
p2)
geo_prob2 = interpolate_cdf_new(
fault_geo_cdf[!, geo_pressure_col],
fault_geo_cdf[!, geo_prob_col],
p2)
# Check if curves cross between p1 and p2
if (hydro_prob1 - geo_prob1) * (hydro_prob2 - geo_prob2) <= 0
# Found a crossing point
# Use linear interpolation to find exact intersection
if hydro_prob1 == geo_prob1
# Exact intersection at p1
intersection_pressure = p1
intersection_probability = hydro_prob1
elseif hydro_prob2 == geo_prob2
# Exact intersection at p2
intersection_pressure = p2
intersection_probability = hydro_prob2
else
# Interpolate to find crossing point
t = (geo_prob1 - hydro_prob1) / ((hydro_prob2 - hydro_prob1) - (geo_prob2 - geo_prob1))
intersection_pressure = p1 + t * (p2 - p1)
# Calculate probability at intersection point - using geomechanics curve
intersection_probability = geo_prob1 + t * (geo_prob2 - geo_prob1)
end
intersection_found = true
#println("Fault $fault_id: Intersection found at pressure = $intersection_pressure psi, probability = $intersection_probability%")
break
end
end
# If no intersection found, use the higher of the two curves where they're closest
if !intersection_found
# Find the point where the curves are closest
min_diff = Inf
closest_pressure = 0.0
closest_probability = 0.0
for p in valid_pressures
hydro_prob = interpolate_cdf_new(
fault_hydro_exceedance.slip_pressure,
fault_hydro_exceedance.probability,
p)
geo_prob = interpolate_cdf_new(
fault_geo_cdf[!, geo_pressure_col],
fault_geo_cdf[!, geo_prob_col],
p)
diff = abs(hydro_prob - geo_prob)
if diff < min_diff
min_diff = diff
closest_pressure = p
closest_probability = max(hydro_prob, geo_prob)
end
end
intersection_pressure = closest_pressure
intersection_probability = closest_probability
# round this to 2 decimal places
#intersection_probability = round(intersection_probability, digits=2)
#println("Fault $fault_id: No exact intersection found. Using closest point at pressure = $intersection_pressure psi, probability = $intersection_probability%")
end
# Add to results - use the intersection probability as the FSP
push!(slip_potential_df, (fault_id, mean_pressure, intersection_probability))
end
return slip_potential_df
end
function interpolate_cdf_new(x_values::Vector{Float64}, y_values::Vector{Float64}, x::Float64)
# handle edge cases
if isempty(x_values) || isempty(y_values)
@warn "Empty vectors provided to interpolate_cdf"
return 0.0
end
# sort x-values and reorder y-values accordingly
p = sortperm(x_values)
x_sorted = x_values[p]
y_sorted = y_values[p]
# Explicitly deduplicate knots to avoid warning
x_deduplicated = copy(x_sorted)
y_deduplicated = copy(y_sorted)
indices = Interpolations.deduplicate_knots!(x_deduplicated)
# Only keep corresponding y values
if length(indices) < length(y_deduplicated)
y_deduplicated = y_deduplicated[indices]
end
# create interpolation object
# we use Flat() which returns the end
itp = LinearInterpolation(x_deduplicated, y_deduplicated, extrapolation_bc=Flat())
# return the interpolated value at x
return itp(x)
end
function interpolate_cdf_new(x_values::Any, y_values::Any, x::Float64)
# Convert inputs to Vector{Float64} and use the new function
x_vec = convert(Vector{Float64}, x_values)
y_vec = convert(Vector{Float64}, y_values)
return interpolate_cdf_new(x_vec, y_vec, x)
end
"""
Interpolate a value on a CDF
"""
function interpolate_cdf(x_values::Vector{Float64}, y_values::Vector{Float64}, x::Float64)
# Handle case where vectors are empty
if isempty(x_values) || isempty(y_values)
@warn "Empty vectors provided to interpolate_cdf"
return 0.0
end
# We need to find the indices where x falls between x_values
i = findfirst(v -> v >= x, x_values)
# edge cases
if i === nothing
return last(y_values)
elseif i == 1
return first(y_values)
end
# Linear interpolation
"""
If x falls between two points (x_values[i-1] and x_values[i]), these lines define the coordinates of the interval:
x1 and y1 are the left endpoint.
x2 and y2 are the right endpoint.
"""
x1 = x_values[i-1]
x2 = x_values[i]
y1 = y_values[i-1]
y2 = y_values[i]
# Linear interpolation formula
return y1 + (y2 - y1) * (x - x1) / (x2 - x1)
end
# Add an overload to handle DataFrameColumns
function interpolate_cdf(x_values::Any, y_values::Any, x::Float64)
# Convert to Vector{Float64} if needed
x_vec = convert(Vector{Float64}, x_values)
y_vec = convert(Vector{Float64}, y_values)
return interpolate_cdf(x_vec, y_vec, x)
end
function main()
#println("\n=== Starting Probabilistic Hydrology Process ===")
scratchPath = ARGS[1]
helper = TexNetWebToolLaunchHelperJulia(scratchPath)
hydro_model_type = get_parameter_value(helper, 5, "hydro_model_type")
#println("Hydro model type from the portal: $hydro_model_type")
extrapolate_injection_rates = get_parameter_value(helper, 5, "extrapolate_injection_rates")
# if it's not provided, we use the default value
if extrapolate_injection_rates === nothing
extrapolate_injection_rates = false
else
extrapolate_injection_rates = parse(Bool, extrapolate_injection_rates)
end
# Default to probabilistic if not specified
if hydro_model_type === nothing
hydro_model_type = "probabilistic" # Default to probabilistic
#println("Hydrology model type not specified, defaulting to probabilistic")
end
# Get year of interest
year_of_interest = get_parameter_value(helper, 5, "year_of_interest")
if year_of_interest === nothing
year_of_interest = year(today())
#println("Year of interest not specified, defaulting to $year_of_interest")
else
# Check if year_of_interest is already an Int before parsing
if isa(year_of_interest, String)
year_of_interest = parse(Int, year_of_interest)
elseif !isa(year_of_interest, Int)
# Handle unexpected types if necessary, e.g., throw an error or log a warning
error("Unexpected type for year_of_interest: $(typeof(year_of_interest))")
end
# If it's already an Int, we don't need to do anything
#println("Year of interest: $year_of_interest")
end
year_of_interest_date = Date(year_of_interest-1, 12, 31)
# Read probabilistic geomechanics CDF data
prob_geo_results = get_dataset_file_path(helper, 5, "prob_geomechanics_cdf_graph_data_prob_hydro")
prob_geo_cdf = CSV.read(prob_geo_results, DataFrame)
if hydro_model_type == "deterministic"
# deterministic hydrology
#println("Running deterministic hydrology model...")
# Check if we have deterministic hydrology results from step 4
# this dataframe holds the data for the straight blue lines that we combine with the probabilistic geomechanics CDF
det_hydro_results = get_dataset_file_path(helper, 5, "deterministic_hydrology_results")
det_hydro_df = CSV.read(det_hydro_results, DataFrame)
# rename the 'FaultID' column to 'ID' (the CDF from geomechanics uses the 'ID' column so they should match)
rename!(det_hydro_df, :FaultID => :ID)
#pretty_table(det_hydro_df)
# Calculate slip potential by combining with probabilistic geomechanics CDF
#=
Using Deterministic Hydrology to Calculate Slip Potential
Once we have calculated the pore pressure added to a given fault and the
probability of fault slip as a function of pore pressure increase, we can calculate the
cumulative probability of fault slip. We do this simply by slicing the prob geomechanics
CDF for the appropriate pore pressure increase
=#
# Calculate slip potential for all years in the dataset
deterministic_slip_potential = calculate_deterministic_slip_potential(prob_geo_cdf, det_hydro_df, nothing)
#println("deterministic_slip_potential:")
#pretty_table(deterministic_slip_potential)
# Filter for the specific year of interest
#year_specific_slip_potential = deterministic_slip_potential[deterministic_slip_potential.Year .== year_of_interest, :]
# Save the year-specific slip potential results
save_dataframe_as_parameter!(helper, 5, "prob_hydrology_cdf_graph_data", det_hydro_df)
# get the faults dataframe
fault_dataset_path = get_dataset_file_path(helper, 5, "faults_model_inputs_output")
faults_df = CSV.read(fault_dataset_path, DataFrame)
# add the fsp to the faults dataframe
# always recreate the column to ensure it's mutable (it's initialized in the model inputs process with missing values)
faults_df[!, :prob_hydro_fsp] = zeros(nrow(faults_df))
# update 'prob_hydro_fsp' column with the fsp values
for row in eachrow(deterministic_slip_potential)
fault_id = row.ID
fsp = row.probability
# find the matching row in the faults_df
idx = findfirst(id -> string(id) == string(fault_id), faults_df.FaultID)
if !isnothing(idx)
faults_df[idx, :prob_hydro_fsp] = fsp
end
end
save_dataframe_as_parameter!(helper, 5, "faults_with_prob_hydro_fsp", faults_df)
deterministic_slip_potential.probability = round.(deterministic_slip_potential.probability, digits=2)
# from the 'deterministic_slip_potential' dataframe
# raname the 'slip_pressure' column to 'MeanPorePressure' and round it to 2 decimal places
rename!(deterministic_slip_potential, :slip_pressure => :MeanPorePressure)
deterministic_slip_potential.MeanPorePressure = round.(deterministic_slip_potential.MeanPorePressure, digits=2)
# rename the 'probability' column to 'SlipPotential'
rename!(deterministic_slip_potential, :probability => :SlipPotential)
# remove the 'Year' and 'Date' columns
deterministic_slip_potential = select(deterministic_slip_potential, Not([:Year, :Date]))
save_dataframe_as_parameter!(helper, 5, "slip_potential_results", deterministic_slip_potential)
#println("slip_potential_results:")
#pretty_table(deterministic_slip_potential)
# set the model run parameter (read by the summary step)
set_parameter_value!(helper, 5, "model_run", 0)
elseif hydro_model_type == "probabilistic"
# set the model run parameter (read by the summary step)
set_parameter_value!(helper, 5, "model_run", 1)
# probabilistic hydrology
#println("Running probabilistic hydrology model...")
# Get hydrology parameters from the portal
aquifer_thickness = get_parameter_value(helper, 4, "aquifer_thickness_ft")
porosity = get_parameter_value(helper, 4, "porosity")
permeability = get_parameter_value(helper, 4, "permeability_md")
fluid_density = get_parameter_value(helper, 4, "fluid_density")
dynamic_viscosity = get_parameter_value(helper, 4, "dynamic_viscosity")
fluid_compressibility = get_parameter_value(helper, 4, "fluid_compressibility")
rock_compressibility = get_parameter_value(helper, 4, "rock_compressibility")
# Get uncertainty parameters
aquifer_thickness_uncertainty = get_parameter_value(helper, 5, "aquifer_thickness_uncertainty")
porosity_uncertainty = get_parameter_value(helper, 5, "porosity_uncertainty")
permeability_uncertainty = get_parameter_value(helper, 5, "permeability_uncertainty")
fluid_density_uncertainty = get_parameter_value(helper, 5, "fluid_density_uncertainty")
dynamic_viscosity_uncertainty = get_parameter_value(helper, 5, "dynamic_viscosity_uncertainty")
fluid_compressibility_uncertainty = get_parameter_value(helper, 5, "fluid_compressibility_uncertainty")
rock_compressibility_uncertainty = get_parameter_value(helper, 5, "rock_compressibility_uncertainty")
#=
#println("input parameters:")
#println("aquifer_thickness: $aquifer_thickness")
#println("porosity: $porosity")