-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathponhy.py
More file actions
1599 lines (1359 loc) · 65.3 KB
/
Copy pathponhy.py
File metadata and controls
1599 lines (1359 loc) · 65.3 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
import csv
import math
import os
import random
import sys
import time
import warnings
from dataclasses import asdict
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from typing import (
Any,
Dict,
List,
Optional,
Sequence,
Tuple,
cast,
)
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import xarray as xr
from tqdm import tqdm
import discretize
import discretize.utils
import simpeg.potential_fields as pf
from discretize.utils import (
active_from_xyz,
)
from scipy.interpolate import griddata
from simpeg import (
data,
data_misfit,
directives,
inversion,
inverse_problem,
maps,
optimization,
regularization,
utils,
)
from utils.geometry import (
build_mesh_from_topography,
compute_rock_volumes_and_masks,
compute_temp_range_depths,
interpolate_nearest_neighbor,
)
from utils.helpers import (
_as_scalar_value,
_clamp_worker_count,
_format_value_for_filename,
_get_progress_bar,
_is_missing,
_moving_average,
_normalize_unit_parameters,
_print_header,
_print_header_once,
_resolve_base_dir_path,
_report_missing_params,
_sample_unit_hypercube,
_scale_samples_to_ranges,
_select_stable_window,
_trim_trailing_dropoff,
)
from utils.no_saturation import run_no_saturation_workflow
from utils.plotting import (
_save_plot_pair,
build_series,
plot_fracture_mc_histogram,
plot_gravity_observed_predicted_residual,
plot_lab_data_panels,
plot_learned_gmm,
plot_misfit_evolution,
plot_magnetic_observed_predicted_residual,
plot_residual_histograms,
prepare_lab_plot_inputs,
set_plot_save_svg,
)
from utils.logging import CustomPrint, log_info
# Keep SVG text as editable text (not paths).
matplotlib.rcParams["svg.fonttype"] = "none"
from utils.reporting import (
print_fracture_monte_carlo_report,
print_inversion_config_summary,
print_inversion_config,
print_inversion_results_summary,
print_no_saturation_report,
print_no_saturation_summary_report,
print_parameters_water_flow_serpentinization,
print_production_rate_volumetric_report,
print_saturation_monte_carlo_report,
print_saturation_summary,
print_summary_serpentinization_section,
save_mc_convergence_sweep_report,
)
from utils.saturation import run_saturation_workflow
from utils.uncertainties import (
analyze_limiting_factors_by_flow_target,
run_fracture_monte_carlo_simulation,
run_mc_convergence_sweep,
run_no_saturation_univariate_sweep,
run_saturation_univariate_sweep,
)
from utils.general import (
build_production_rate_volumetric_dict,
build_thermo_lookup_by_range,
compute_h2_solubility_kk_pr,
compute_mean_lithostatic_pressure_by_range,
compute_serpentinization_degree,
compute_serpentinization_front_velocities,
convert_h2_mol_to_kg,
extract_column,
load_h2_production_database,
plot_serpentinization_heatmap,
parse_temperature_range,
save_saturation_csv,
sort_temperature_ranges,
weighted_average_rates,
)
from utils.config import (
build_flow_target_limiting_factors_params,
build_inversion_config_summary,
build_inversion_mesh_config,
build_inversion_results_summary,
build_mc_common_kwargs,
build_mc_no_sat_kwargs,
build_mean_lithostatic_pressure_params,
build_no_saturation_workflow_params,
build_production_rate_report_params,
build_production_rate_volumetric_params,
build_saturation_workflow_params,
build_serpentinization_front_velocity_params,
build_serpentinization_summary_params,
build_thermo_lookup_params,
Config,
InversionConfigSummary,
InversionMeshConfig,
InversionResultsSummary,
MeanLithostaticPressureParams,
NoSaturationSummaryReportParams,
NoSaturationWorkflowParams,
ProductionRateReportParams,
ProductionRateVolumetricParams,
SaturationMonteCarloReportParams,
SaturationSummaryParams,
SaturationWorkflowParams,
SerpentinizationFrontVelocityParams,
SerpentinizationSummaryParams,
ThermoLookupParams,
WaterFlowSerpentinizationParams,
load_config,
)
def _select_config_yaml(default_name: str = "ponhy_config_pyrenees.yaml") -> str:
"""Select a YAML config from the current working directory.
When multiple YAML files exist, prompt the user to choose one. If stdin is not
interactive or the input is invalid, fall back to the first candidate.
"""
cwd = os.getcwd()
candidates = [
os.path.join(cwd, name)
for name in sorted(os.listdir(cwd))
if name.lower().endswith((".yaml", ".yml"))
and name.lower() != "environment.yml"
]
if not candidates:
raise FileNotFoundError(
"No YAML config files found in the current working directory. "
"Please add a ponhy_config_*.yaml file and try again."
)
if len(candidates) == 1:
return candidates[0]
print("\n[PoNHy] Config YAMLs found:")
for idx, path in enumerate(candidates, start=1):
print(f" {idx}) {os.path.basename(path)}")
if not sys.stdin.isatty():
print("[PoNHy] Non-interactive session detected. Using the first YAML.")
return candidates[0]
while True:
choice = input("Select YAML to use (number -> Enter): ").strip()
if not choice:
return candidates[0]
if choice.isdigit():
idx = int(choice)
if 1 <= idx <= len(candidates):
return candidates[idx - 1]
print("Invalid selection. Try again.")
def _discover_data_dirs(root: str) -> List[str]:
if not root or not os.path.isdir(root):
return []
return [
os.path.join(root, name)
for name in sorted(os.listdir(root))
if name.lower().startswith("data") and os.path.isdir(os.path.join(root, name))
]
def _select_data_dir(root: str) -> Optional[str]:
candidates = _discover_data_dirs(root)
if not candidates:
if not sys.stdin.isatty():
return None
print("\n[PoNHy] No Data folders found.")
while True:
manual = input("Enter Data folder path (or leave blank to cancel): ").strip()
if not manual:
return None
if os.path.isdir(manual):
return manual
print("Invalid path. Try again.")
if len(candidates) == 1:
return candidates[0]
print("\n[PoNHy] Data folders found:")
for idx, path in enumerate(candidates, start=1):
print(f" {idx}) {path}")
if not sys.stdin.isatty():
raise RuntimeError("Multiple Data folders found but no interactive terminal to select one.")
while True:
choice = input("Select Data folder (number -> Enter): ").strip()
if not choice:
return candidates[0]
if choice.isdigit():
idx = int(choice)
if 1 <= idx <= len(candidates):
return candidates[idx - 1]
print("Invalid selection. Try again.")
def _rewrite_data_path(base_dir: str, data_dir: str, path: Optional[str]) -> Optional[str]:
if path is None:
return None
if not isinstance(path, str):
path = str(path)
cleaned = path.strip()
if not cleaned:
return None
if os.path.isabs(cleaned):
return cleaned
has_sep = any(sep in cleaned for sep in (os.sep, "/", "\\"))
if has_sep:
return os.path.join(base_dir, cleaned)
return os.path.join(data_dir, cleaned)
CONFIG_YAML_FILE = _select_config_yaml()
# ======================================================== Dictionaries and data for calculations ========================================================
# Compositions of the rocks estimated in thermodynamic databases. This is just for info and printing (not used in calculations)
lithologies_dict = {
'HZ1': {
'Al2O3': 3.13, 'CaO': 1.25, 'CoO': 0.0, 'Cr2O3': 0.0, 'FeO': 8.69, 'Fe2O3': 0.0,
'H2O': 0.0, 'K2O': 0.0, 'MnO': 0.0, 'MgO': 43.53, 'Na2O': 0.08, 'NiO': 0.0,
'P2O5': 0.0, 'SiO2': 43.33, 'TiO2': 0.0
},
'LH1': {
'Al2O3': 4.46, 'CaO': 2.65, 'CoO': 0.0, 'Cr2O3': 0.0, 'FeO': 7.86, 'Fe2O3': 0.0,
'H2O': 0.0, 'K2O': 0.0, 'MnO': 0.0, 'MgO': 40.13, 'Na2O': 0.16, 'NiO': 0.0,
'P2O5': 0.0, 'SiO2': 44.75, 'TiO2': 0.0
}
}
# Data for volumetric serpentinization degree interpolation (x: density (g/cm3), y: magnetic susceptibility (SI), value: serpentinization degree (%))
# This represents how much of the block is 100% serpentinized (e.g., 50% means half of the block is fully serpentinized, rest is unaltered)
serpentinization_data = {
'x_points': np.array([2.62 , 2.6355, 2.651 , 2.6665, 2.682 , 2.6975, 2.713 , 2.7285,
2.744 , 2.7595, 2.775 , 2.7905, 2.806 , 2.8215, 2.837 , 2.8525,
2.868 , 2.8835, 2.899 , 2.9145, 2.93 , 2.9455, 2.961 , 2.9765,
2.992 , 3.0075, 3.023 , 3.0385, 3.054 , 3.0695, 3.085 , 3.1005,
3.116 , 3.1315, 3.147 , 3.1625, 3.178 , 3.1935, 3.209 , 3.2245,
3.24 ]),
'y_points': np.array([0.07 , 0.0682775, 0.066555 , 0.0648325, 0.06311 , 0.0613875,
0.059665 , 0.0579425, 0.05622 , 0.0544975, 0.052775 , 0.0510525,
0.04933 , 0.0476075, 0.045885 , 0.0441625, 0.04244 , 0.0407175,
0.038995 , 0.0372725, 0.03555 , 0.0338275, 0.032105 , 0.0303825,
0.02866 , 0.0269375, 0.025215 , 0.0234925, 0.02177 , 0.0200475,
0.018325 , 0.0166025, 0.01488 , 0.0131575, 0.011435 , 0.0097125,
0.00799 , 0.0062675, 0.004545 , 0.0028225, 0.0011 ]),
'values': np.array([100. , 97.5, 95. , 92.5, 90. , 87.5, 85. , 82.5, 80. ,
77.5, 75. , 72.5, 70. , 67.5, 65. , 62.5, 60. , 57.5,
55. , 52.5, 50. , 47.5, 45. , 42.5, 40. , 37.5, 35. ,
32.5, 30. , 27.5, 25. , 22.5, 20. , 17.5, 15. , 12.5,
10. , 7.5, 5. , 2.5, 0. ])
}
# Values of corrections for hydrogen production according to the volumetric serpentinization degree (content of remaining olivine in the block).
serp_corr_percentage = {
'percentage': np.array([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]),
'correction': np.array([0.00, 3.29, 6.65, 10.06, 13.55, 17.10, 20.74, 24.46, 28.28, 32.21, 36.26, 40.45, 44.80, 49.33, 54.08, 59.11, 64.48, 70.33, 76.89, 84.70, 98.00])
}
cfg: Config = load_config(CONFIG_YAML_FILE)
# Auto-detect base_dir unless a manual path is provided in YAML.
base_dir_candidate = (cfg.base_dir or "").strip()
if not base_dir_candidate or base_dir_candidate.lower() in {"auto", "none"}:
base_dir_candidate = os.path.abspath(os.getcwd())
cfg.base_dir = base_dir_candidate
data_keys = (
"topo_file",
"grav_file",
"mag_file",
"initial_model",
"db_dir",
"temperature_file",
"density_file",
"magsus_file",
)
data_dir_selected = _select_data_dir(cfg.base_dir)
if data_dir_selected is None:
raise FileNotFoundError(
"No Data* folders found under base_dir. "
"Add a Data_... folder or update the config paths."
)
for key in data_keys:
value = getattr(cfg, key, None)
setattr(cfg, key, _rewrite_data_path(cfg.base_dir, data_dir_selected, value))
# Bind config values into module-level names.
run_inversion = cfg.run_inversion
run_h2_quantification = cfg.run_h2_quantification
base_dir = cfg.base_dir
topo_file = cfg.topo_file
grav_file = cfg.grav_file
mag_file = cfg.mag_file
initial_model = cfg.initial_model
use_initial_model = cfg.use_initial_model
background_dens = cfg.background_dens
background_susc = cfg.background_susc
db_dir = cfg.db_dir
temperature_file = cfg.temperature_file
density_file = cfg.density_file
magsus_file = cfg.magsus_file
dx = cfg.dx
dy = cfg.dy
dz = cfg.dz
depth_core = cfg.depth_core
expansion_percentage = cfg.expansion_percentage
expansion_type = cfg.expansion_type
dominant_side = cfg.dominant_side
original_y = cfg.original_y
original_x = cfg.original_x
uncer_grav = cfg.uncer_grav
uncer_mag = cfg.uncer_mag
inclination = cfg.inclination
declination = cfg.declination
strength = cfg.strength
unit_labels = cfg.unit_labels
unit_dens_adj_list = cfg.unit_dens_adj_list
unit_magsus_list = cfg.unit_magsus_list
unit_dens_disp_list = cfg.unit_dens_disp_list
unit_magsus_disp_list = cfg.unit_magsus_disp_list
vol_unit_list = cfg.vol_unit_list
serpentinite_label = cfg.serpentinite_label
low_dens_adj = cfg.low_dens_adj
up_dens_adj = cfg.up_dens_adj
lower_susceptibility = cfg.lower_susceptibility
upper_susceptibility = cfg.upper_susceptibility
max_iter = cfg.max_iter
max_iter_ls = cfg.max_iter_ls
max_iter_cg = cfg.max_iter_cg
tol_cg = cfg.tol_cg
save_pred_residual_npy = cfg.save_pred_residual_npy
save_svg = cfg.save_svg
alpha_pgi = cfg.alpha_pgi
alpha_x = cfg.alpha_x
alpha_y = cfg.alpha_y
alpha_z = cfg.alpha_z
alpha_xx = cfg.alpha_xx
alpha_yy = cfg.alpha_yy
alpha_zz = cfg.alpha_zz
alpha0_ratio_dens = cfg.alpha0_ratio_dens
alpha0_ratio_susc = cfg.alpha0_ratio_susc
beta0_ratio = cfg.beta0_ratio
cooling_factor = cfg.cooling_factor
tolerance = cfg.tolerance
progress = cfg.progress
use_chi_small = cfg.use_chi_small
chi_small = cfg.chi_small
wait_till_stable = cfg.wait_till_stable
update_gmm = cfg.update_gmm
kappa = cfg.kappa
chi0_ratio = cfg.chi0_ratio
seed = cfg.seed
use_global_seed = cfg.use_global_seed
dist_x = cfg.dist_x
dist_y = cfg.dist_y
dist_z = cfg.dist_z
density_serpentinite = cfg.density_serpentinite
porosity_front = cfg.porosity_front
int_fracture_spacing = cfg.int_fracture_spacing
permeability_fractures = cfg.permeability_fractures
waterrockratio = cfg.waterrockratio
flow_target = cfg.flow_target
density_litho = cfg.density_litho
gravity = cfg.gravity
lithology_code = cfg.lithology_code
years = cfg.years
molar_mass_h2 = cfg.molar_mass_h2
molar_mass_h2o = cfg.molar_mass_h2o
v_ref_synthetic = cfg.v_ref_synthetic
t_ref_range = cfg.t_ref_range
n_cores = cfg.n_cores
run_montecarlo_fault = cfg.run_montecarlo_fault
fault_mc_n_iter = cfg.fault_mc_n_iter
flow_target_fracture_config = asdict(cfg.flow_target_fracture_config)
mc_no_saturation_config = asdict(cfg.mc_no_saturation_config)
mc_saturation_config = asdict(cfg.mc_saturation_config)
run_univariate_analysis_no_sat = cfg.run_univariate_analysis_no_sat
run_univariate_analysis_sat = cfg.run_univariate_analysis_sat
univariate_analysis_config = asdict(cfg.univariate_analysis_config)
run_mc_convergence_sweep_flag = cfg.run_mc_convergence_sweep
mc_convergence_sweep_config = asdict(cfg.mc_convergence_sweep_config)
run_analyze_limiting_factors = cfg.run_analyze_limiting_factors
flow_target_log_min = cfg.flow_target_log_min
flow_target_log_max = cfg.flow_target_log_max
flow_target_n_samples = cfg.flow_target_n_samples
mc_flow_target_config = asdict(cfg.mc_flow_target_config)
# Runtime-only values that should not live in the config object.
runtime_state: Dict[str, Any] = {"surface_area_per_km3": None}
def _compute_surface_area_per_km3(dist_x: float, dist_y: float, dist_z: float) -> float:
original_volume_m3 = 1000 ** 3 # 1 km³
voxel_volume_m3 = dist_x * dist_y * dist_z
total_voxels = original_volume_m3 / voxel_volume_m3
voxel_surface_area = 2 * (dist_x * dist_y + dist_x * dist_z + dist_y * dist_z)
return voxel_surface_area * total_voxels
# Configure global plotting settings (e.g., optional SVG output).
set_plot_save_svg(save_svg)
if __name__ != "__main__":
run_inversion = False
run_h2_quantification = False
######################################################################################################################################################
################################################################# Inversion ##########################################################################
######################################################################################################################################################
INVERSION_PARAM_NAMES = [
"base_dir", "topo_file", "grav_file", "mag_file", "initial_model", "use_initial_model",
"background_dens", "background_susc", "dx", "dy", "dz", "depth_core",
"expansion_percentage", "expansion_type", "dominant_side", "original_y", "original_x",
"uncer_grav", "uncer_mag", "inclination", "declination", "strength", "unit_labels",
"unit_dens_adj_list", "unit_magsus_list", "unit_dens_disp_list", "unit_magsus_disp_list",
"vol_unit_list", "serpentinite_label", "low_dens_adj", "up_dens_adj", "lower_susceptibility",
"upper_susceptibility", "max_iter", "max_iter_ls", "max_iter_cg", "tol_cg",
"save_pred_residual_npy", "save_svg", "alpha_pgi", "alpha_x", "alpha_y", "alpha_z", "alpha_xx",
"alpha_yy", "alpha_zz",
"alpha0_ratio_dens", "alpha0_ratio_susc", "beta0_ratio", "cooling_factor", "tolerance",
"progress", "use_chi_small", "chi_small", "wait_till_stable", "update_gmm", "kappa",
"chi0_ratio",
]
if run_inversion:
# Print the report header once for the full run (stdout is redirected later).
_print_header_once()
print("\nPlease wait, it can take a few minutes.\n")
# Validate required inversion parameters are present and non-empty.
inversion_inputs = {name: getattr(cfg, name, None) for name in INVERSION_PARAM_NAMES}
missing_inversion = [name for name in INVERSION_PARAM_NAMES if _is_missing(inversion_inputs.get(name))]
if missing_inversion:
# Report missing parameters with context-specific messaging.
_report_missing_params(missing_inversion, "inversion")
raise ValueError("Missing required inversion parameters.")
start_time = time.time()
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
dir_path = os.path.join(base_dir, "")
folder_name = f"Results_Inversion_{current_time}"
results_path = os.path.join(dir_path, folder_name)
if not os.path.exists(results_path):
os.makedirs(results_path)
output_file_path = os.path.join(results_path, "output.txt")
# Resolve input data paths relative to base_dir.
topo_filename = _resolve_base_dir_path(base_dir, topo_file)
grav_data_filename = _resolve_base_dir_path(base_dir, grav_file)
mag_data_filename = _resolve_base_dir_path(base_dir, mag_file)
assert topo_filename is not None, "Resolved topo_filename is required"
assert grav_data_filename is not None, "Resolved grav_data_filename is required"
assert mag_data_filename is not None, "Resolved mag_data_filename is required"
# Resolve and load the initial model CSV path for the inversion only when enabled.
if use_initial_model:
initial_model_filename = _resolve_base_dir_path(base_dir, initial_model)
assert initial_model_filename is not None, "Resolved initial_model_filename is required"
if os.path.isdir(initial_model_filename):
raise IsADirectoryError(
f"initial_model points to a directory: {initial_model_filename}. "
"Set initial_model to a CSV file or disable use_initial_model."
)
if not os.path.exists(initial_model_filename):
raise FileNotFoundError(
f"initial_model file not found: {initial_model_filename}. "
"Update initial_model to a valid CSV path or disable use_initial_model."
)
initial_model_data = np.loadtxt(initial_model_filename, delimiter=",", skiprows=1)
else:
initial_model_filename = None
initial_model_data = np.array([])
# Redirect stdout to a file-based logger for the inversion output.
custom_printer = CustomPrint(output_file_path)
sys.stdout = custom_printer
separator = "=" * 150
section_separator = "-" * 150
# Runtime globals
receiver_grav: Any = None
receiver_mag: Any = None
nx = ny = nz = 0
total_distance_x = total_distance_y = total_distance_z = 0.0
uncertainties_grav: np.ndarray = np.array([])
uncertainties_mag: np.ndarray = np.array([])
alpha0_ratio: Any = np.array([])
beta: Any = None
beta_it: Any = None
targets: Any = None
mref_in_smooth: Any = None
update_smallness: Any = None
save_values_directive: Any = None
scaling_init: Any = None
# ====================================================== Density adjustments / constants ===================================================== #
(
unit_labels_resolved,
unit_dens_adj_resolved,
unit_magsus_resolved,
unit_dens_disp_resolved,
unit_magsus_disp_resolved,
vol_unit_resolved,
# Normalize and validate unit parameter lists (labels, means, dispersions, weights).
) = _normalize_unit_parameters(
labels=unit_labels,
dens_adj=unit_dens_adj_list,
magsus=unit_magsus_list,
dens_disp=unit_dens_disp_list,
magsus_disp=unit_magsus_disp_list,
weights=vol_unit_list,
)
unit_dens = 2.67 - unit_dens_adj_resolved
lower_density = 2.67 - low_dens_adj
upper_density = 2.67 - up_dens_adj
# ===================================================== Data loading (topo / grav / mag) ===================================================== #
def _load_txt(path: str) -> np.ndarray:
return np.loadtxt(str(path), dtype=np.float32)
with ThreadPoolExecutor(max_workers=3) as ex:
fut_topo = ex.submit(_load_txt, topo_filename)
fut_grav = ex.submit(_load_txt, grav_data_filename)
fut_mag = ex.submit(_load_txt, mag_data_filename)
topo_xyz = fut_topo.result()
dobs_grav = fut_grav.result()
dobs_mag = fut_mag.result()
receiver_locations = topo_xyz[:, 0:3]
dobs_grav = dobs_grav[:, -1]
dobs_mag = dobs_mag[:, -1]
# ==================================================== Surveys setup (gravity / magnetic) ==================================================== #
maximum_anomaly_grav = np.max(np.abs(dobs_grav))
uncertainties_grav = (uncer_grav / 100.0) * maximum_anomaly_grav
uncertainties_grav = np.full_like(dobs_grav, uncertainties_grav)
maximum_anomaly_mag = np.max(np.abs(dobs_mag))
uncertainties_mag = (uncer_mag / 100.0) * maximum_anomaly_mag
uncertainties_mag = np.full_like(dobs_mag, uncertainties_mag)
receiver_grav = pf.gravity.receivers.Point(receiver_locations, components="gz")
source_field_grav = pf.gravity.sources.SourceField(receiver_list=[receiver_grav])
survey_grav = pf.gravity.survey.Survey(source_field_grav)
receiver_mag = pf.magnetics.receivers.Point(receiver_locations, components="tmi")
source_field_mag = pf.magnetics.sources.UniformBackgroundField(
receiver_list=[receiver_mag],
amplitude=strength,
inclination=inclination,
declination=declination,
)
survey_mag = pf.magnetics.survey.Survey(source_field_mag)
data_object_grav = data.Data(survey_grav, dobs=dobs_grav, standard_deviation=uncertainties_grav)
data_object_mag = data.Data(survey_mag, dobs=dobs_mag, standard_deviation=uncertainties_mag)
# =================================================== Mesh, active cells, and initial model ================================================== #
# Build the 3D inversion mesh from the topography grid.
mesh, nx, ny, nz = build_mesh_from_topography(topo_xyz, dx, dy, dz, depth_core)
actv = active_from_xyz(mesh, topo_xyz, "CC")
assert getattr(mesh, 'cell_centers', None) is not None, "mesh.cell_centers is required"
assert getattr(mesh, 'cell_volumes', None) is not None, "mesh.cell_volumes is required"
cell_centers = cast(np.ndarray, mesh.cell_centers)
cell_volumes = cast(np.ndarray, mesh.cell_volumes)
assert actv is not None, "active cell mask (actv) should not be None"
ndv = np.nan
actvMap = maps.InjectActiveCells(mesh, actv, ndv)
nactv = int(actv.sum())
total_distance_x = nx * dx
total_distance_y = ny * dy
total_distance_z = nz * dz
xyz_active = mesh.gridCC[actv, :]
if use_initial_model:
x_csv, y_csv, z_csv = initial_model_data[:, 0], initial_model_data[:, 1], initial_model_data[:, 2]
initial_density, initial_susceptibility = initial_model_data[:, 3], initial_model_data[:, 4]
xyz_csv = np.vstack((x_csv, y_csv, z_csv)).T
# Interpolate initial model properties onto active mesh cells.
density_mesh = interpolate_nearest_neighbor(xyz_csv, initial_density, xyz_active)
susceptibility_mesh = interpolate_nearest_neighbor(xyz_csv, initial_susceptibility, xyz_active)
m0 = np.r_[density_mesh, susceptibility_mesh]
else:
m0 = np.r_[background_dens * np.ones(actvMap.nP), background_susc * np.ones(actvMap.nP)]
# ==================================================== Physics, misfits, and reference GMM =================================================== #
nC = nactv
wires: Any = maps.Wires(("den", nC), ("sus", nC))
idenMap = maps.IdentityMap(nP=nactv)
gravmap = actvMap * wires.den
magmap = actvMap * wires.sus
simulation_grav = pf.gravity.simulation.Simulation3DIntegral(
survey=survey_grav,
mesh=mesh,
rhoMap=wires.den,
ind_active=actv,
)
simulation_mag = pf.magnetics.simulation.Simulation3DIntegral(
survey=survey_mag,
mesh=mesh,
chiMap=wires.sus,
ind_active=actv,
)
dmis_grav = data_misfit.L2DataMisfit(data=data_object_grav, simulation=simulation_grav)
dmis_mag = data_misfit.L2DataMisfit(data=data_object_mag, simulation=simulation_mag)
dmis = 0.5 * dmis_grav + 0.5 * dmis_mag
gmmref = utils.WeightedGaussianMixture(
n_components=len(unit_dens),
mesh=mesh,
actv=actv,
covariance_type="diag",
)
gmmref.fit(np.random.randn(nactv, 2))
gmmref.means_ = np.column_stack((unit_dens, unit_magsus_resolved))
gmmref.covariances_ = np.column_stack((
unit_dens_disp_resolved ** 2,
unit_magsus_disp_resolved ** 2,
))
gmmref.compute_clusters_precisions()
gmmref.weights_ = vol_unit_resolved
ax = gmmref.plot_pdf(flag2d=True, plotting_precision=100, padding=2)
ax[0].set_xlabel("Density contrast [g/cc]")
ax[2].set_ylabel("magnetic Susceptibility [SI]")
fig_init = ax[0].figure if hasattr(ax[0], "figure") else plt.gcf()
file_base = os.path.join(results_path, 'initial_GMM')
# Save the initial GMM plot in PNG/SVG formats.
_save_plot_pair(file_base, fig_init)
plt.close(fig_init)
# =============================================== Weights, PGI regularization, and alphas/betas ============================================== #
x_min, x_max = cell_centers[actv][:, 0].min(), cell_centers[actv][:, 0].max()
y_min, y_max = cell_centers[actv][:, 1].min(), cell_centers[actv][:, 1].max()
border_distance = 0 * max(dx, dy)
is_edge_cell = (
(cell_centers[actv][:, 0] < x_min + border_distance) |
(cell_centers[actv][:, 0] > x_max - border_distance) |
(cell_centers[actv][:, 1] < y_min + border_distance) |
(cell_centers[actv][:, 1] > y_max - border_distance)
)
if simulation_grav.G is not None:
wr_grav = np.sum(simulation_grav.G**2., axis=0)**0.5 / (cell_volumes[actv])
wr_grav = wr_grav / np.max(wr_grav)
wr_grav[is_edge_cell] *= 0.5
else:
wr_grav = np.ones(nactv)
if simulation_mag.G is not None:
wr_mag = np.sum(simulation_mag.G**2., axis=0)**0.5 / (cell_volumes[actv])
wr_mag = wr_mag / np.max(wr_mag)
wr_mag[is_edge_cell] *= 0.5
else:
wr_mag = np.ones(nactv)
weights_dict = {
'cell_x': cell_centers[actv][:, 0],
'cell_y': cell_centers[actv][:, 1],
'cell_z': cell_centers[actv][:, 2],
'wr_grav': wr_grav,
'wr_mag': wr_mag
}
df_weights = pd.DataFrame(weights_dict)
weights_file_path = os.path.join(results_path, 'weights_gravity_magnetism.csv')
df_weights.to_csv(weights_file_path, index=False)
reg = regularization.PGI(
gmmref=gmmref,
mesh=mesh,
wiresmap=wires,
maplist=[idenMap, idenMap],
active_cells=actv,
alpha_pgi=alpha_pgi,
alpha_x=alpha_x,
alpha_y=alpha_y,
alpha_z=alpha_z,
alpha_xx=alpha_xx,
alpha_yy=alpha_yy,
alpha_zz=alpha_zz,
reference_model=utils.mkvc(
cast(np.ndarray, gmmref.means_)[
cast(np.ndarray, gmmref.predict(m0.reshape(actvMap.nP, -1)))
]
),
weights_list=[wr_grav, wr_mag],
)
reg_objfcts = getattr(reg, "objfcts", None)
def _count_smooth_terms(seq: Sequence[Any], idx: int) -> int:
if 0 <= idx < len(seq):
child = seq[idx]
sub = getattr(child, "objfcts", None)
if isinstance(sub, Sequence):
return max(1, len(sub[1:]))
return 1
if isinstance(reg_objfcts, Sequence):
reg_obj_seq = cast(Sequence[Any], reg_objfcts)
dens_terms = _count_smooth_terms(reg_obj_seq, 1)
susc_terms = _count_smooth_terms(reg_obj_seq, 2)
else:
dens_terms = 1
susc_terms = 1
alpha0_ratio = np.r_[
alpha0_ratio_dens * np.ones(dens_terms),
alpha0_ratio_susc * np.ones(susc_terms),
]
alphas = directives.AlphasSmoothEstimate_ByEig(alpha0_ratio=alpha0_ratio, verbose=True)
beta = directives.BetaEstimate_ByEig(beta0_ratio=beta0_ratio)
beta_it = directives.PGI_BetaAlphaSchedule(
verbose=True,
coolingFactor=cooling_factor,
tolerance=tolerance,
progress=progress,
)
if use_chi_small:
targets = directives.MultiTargetMisfits(
verbose=True,
chiSmall=chi_small,
)
else:
targets = directives.MultiTargetMisfits(
verbose=True,
)
mref_in_smooth = directives.PGI_AddMrefInSmooth(
wait_till_stable=wait_till_stable,
verbose=True,
)
update_smallness = directives.PGI_UpdateParameters(
update_gmm=update_gmm,
kappa=kappa,
)
update_Jacobi = directives.UpdatePreconditioner()
scaling_init = directives.ScalingMultipleDataMisfits_ByEig(chi0_ratio=chi0_ratio)
scale_schedule = directives.JointScalingSchedule(verbose=True)
# =============================================== Optimization, inverse problem, and directives ============================================== #
# Optimization setup
lowerbound = np.r_[upper_density * np.ones(actvMap.nP), lower_susceptibility * np.ones(actvMap.nP)]
upperbound = np.r_[lower_density * np.ones(actvMap.nP), upper_susceptibility * np.ones(actvMap.nP)]
if lowerbound is not None and upperbound is not None:
opt = optimization.ProjectedGNCG(
maxIter=max_iter,
lower=lowerbound,
upper=upperbound,
maxIterLS=max_iter_ls,
maxIterCG=max_iter_cg,
tolCG=tol_cg,
)
else:
opt = optimization.ProjectedGNCG(
maxIter=max_iter,
maxIterLS=max_iter_ls,
maxIterCG=max_iter_cg,
tolCG=tol_cg,
)
invProb = inverse_problem.BaseInvProblem(dmis, reg, opt)
directiveList = [
alphas,
scaling_init,
beta,
update_smallness,
targets,
scale_schedule,
beta_it,
mref_in_smooth,
update_Jacobi,
]
class SaveValuesDirective(directives.InversionDirective):
def initialize(self):
self.phi_d_list = []
self.phi_m_list = []
self.beta_list = []
self.phi_d_gravity_list = []
self.phi_d_magnetic_list = []
self.phi_m_smallness_list = []
def endIter(self):
self.phi_d_list.append(self.invProb.phi_d)
self.phi_m_list.append(self.invProb.phi_m)
self.beta_list.append(self.invProb.beta)
if hasattr(self.invProb, 'dmisfit') and hasattr(self.invProb.dmisfit, 'objfcts'):
if self.invProb.dmisfit.objfcts is not None and len(self.invProb.dmisfit.objfcts) > 1:
phi_d_gravity = self.invProb.dmisfit.objfcts[0](self.invProb.model)
phi_d_magnetic = self.invProb.dmisfit.objfcts[1](self.invProb.model)
else:
phi_d_gravity = 0.0
phi_d_magnetic = 0.0
else:
phi_d_gravity = 0.0
phi_d_magnetic = 0.0
phi_m_smallness = self.invProb.reg.objfcts[0](self.invProb.model)
self.phi_d_gravity_list.append(phi_d_gravity)
self.phi_d_magnetic_list.append(phi_d_magnetic)
self.phi_m_smallness_list.append(phi_m_smallness)
save_values_directive = SaveValuesDirective()
directiveList.append(save_values_directive)
# =============================================================== Run inversion ============================================================== #
# ====================================================================================================================================================
print("\n" + separator)
print("1. INVERSION CONFIGURATION SUMMARY".center(150))
print(separator)
# ====================================================================================================================================================
# Print a full inversion configuration summary for the report.
inversion_summary = build_inversion_config_summary(locals())
print_inversion_config_summary(inversion_summary)
inv = inversion.BaseInversion(
invProb,
directiveList=directiveList
)
pgi_model_no_info = inv.run(m0)
# Capture optimizer iteration counts for reporting.
opt_iter = getattr(opt, "iter", None)
if opt_iter is None:
opt_iter = getattr(opt, "iterNum", None)
opt_max_iter = getattr(opt, "maxIter", None)
# Plot the evolution of data/regularization misfits during inversion.
plot_misfit_evolution(save_values_directive, results_path=results_path)
density_model_no_info = gravmap * pgi_model_no_info
magsus_model_no_info = magmap * pgi_model_no_info
learned_gmm = None
quasi_geology_model_no_info = np.zeros(mesh.nC)
reg_objfcts_post = getattr(reg, "objfcts", None)
if isinstance(reg_objfcts_post, Sequence) and len(reg_objfcts_post) > 0:
reg_first = reg_objfcts_post[0]
gmm_candidate = getattr(reg_first, "gmm", None)
if gmm_candidate is not None:
learned_gmm = gmm_candidate
compute_quasi = getattr(reg_first, "compute_quasi_geology_model", None)
if callable(compute_quasi):
quasi_geology_model_no_info = actvMap * compute_quasi()
# Plot the learned GMM against the recovered model.
plot_learned_gmm(
learned_gmm=learned_gmm,
density_model_no_info=density_model_no_info,
magsus_model_no_info=magsus_model_no_info,
ind_active=actv,
results_path=results_path,
true_means=gmmref.means_,
model_colors=quasi_geology_model_no_info,
)
density_model_no_info = density_model_no_info.astype(float)
magsus_model_no_info = magsus_model_no_info.astype(float)
quasi_geology_model_no_info = np.asarray(quasi_geology_model_no_info)
density_model_no_info = density_model_no_info[actv]
magsus_model_no_info = magsus_model_no_info[actv]
quasi_geology_model_no_info = quasi_geology_model_no_info[actv]
# ======================================================= Central crop and model export ====================================================== #
x_min, x_max = xyz_active[:, 0].min(), xyz_active[:, 0].max()
y_min, y_max = xyz_active[:, 1].min(), xyz_active[:, 1].max()
expansion_x = 0.0
expansion_y = 0.0
if expansion_type == "rectangular":
original_distance_x = total_distance_x / (1 + expansion_percentage / 100)
original_distance_y = total_distance_y / (1 + expansion_percentage / 100)
expansion_x = (total_distance_x - original_distance_x) / 2
expansion_y = (total_distance_y - original_distance_y) / 2
elif expansion_type == "square":
l_new = total_distance_x
if dominant_side == "x":
original_x = l_new / (1 + expansion_percentage / 100)
expansion_x = (l_new - original_x) / 2
expansion_y = (l_new - original_y) / 2
elif dominant_side == "y":
original_y = l_new / (1 + expansion_percentage / 100)
expansion_y = (l_new - original_y) / 2
expansion_x = (l_new - original_x) / 2
x_min_central = x_min + expansion_x
x_max_central = x_max - expansion_x
y_min_central = y_min + expansion_y
y_max_central = y_max - expansion_y
central_mask = (
(xyz_active[:, 0] >= x_min_central) & (xyz_active[:, 0] <= x_max_central + dx) &
(xyz_active[:, 1] >= y_min_central) & (xyz_active[:, 1] <= y_max_central + dy)
)
xyz_active = xyz_active[central_mask]
density_model_no_info = density_model_no_info[central_mask]
magsus_model_no_info = magsus_model_no_info[central_mask]
quasi_geology_model_no_info = quasi_geology_model_no_info[central_mask]