-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_plotter.py
1453 lines (1263 loc) · 50.5 KB
/
main_plotter.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 os
import sys
import importlib
from blessed import Terminal
from typing import Dict, Any, Callable
import pandas as pd
import numpy as np
import pkg_resources
import subprocess
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import pyvista as pv
import holoviews as hv
from holoviews import opts
from rich.console import Console
from rich.panel import Panel
from rich.progress import (
Progress,
SpinnerColumn,
TextColumn,
BarColumn,
TaskProgressColumn,
TimeRemainingColumn,
)
from rich.table import Table
from rich.prompt import Prompt, IntPrompt, Confirm
from rich import box
from rich.box import ROUNDED, SQUARE
from pathlib import Path
import yaml
import re
import matplotlib.pyplot as plt
from modules import (
search_for_export_csv,
extract_parameters_by_file_name,
list_csv_files,
list_folders,
find_common_and_varying_params,
)
from peakfinder import (
plotter,
plot_amplitude_analysis_separate,
plotter_adiabatic_invariance_check,
plot_eta_fluctuations,
read_exported_csv_2Dsimulation,
Configuration,
)
from plotter_2D import plot_csv_files, select_plotting_parameters
from plotter_3D import (
Plotter_2d3d,
display_available_parameters,
get_parameter_from_input,
)
from plotter_comparison_1D_exact import (
list_csv_files_noFolder,
select_file_by_number,
find_first_difference,
create_fancy_annotation,
save_plots_with_timestamp,
)
from plotter_comparison_2D_3D import plot_comparison
from modules.file_utils import list_comparison_files, list_items
from plotter_energy_momentum_conservasion import CONFIG, plotter_conservation
from plotter_electricField import (
matplotlib_3d_surface,
matplotlib_2d_stream_contour,
matplotlib_2d_quiver,
matplotlib_2d_electric_field_contour,
matplotlib_2d_potential_contour,
plotly_3d_surface,
pyvista_streamlines,
holoviews_vectorfield_plot,
)
from plotter_magneticField import (
create_2d_quiver_plot,
create_3d_contour_plot,
create_2d_contour_plot,
create_3d_quiver_plot,
create_2d_streamplot,
)
term = Terminal()
config = Configuration(os.path.join(os.path.dirname(__file__), "config.yaml"))
console = Console()
def read_requirements():
"""Read requirements from requirements.txt file"""
req_file = Path(__file__).parent / "requirements.txt"
if not req_file.exists():
console.print("[red]requirements.txt not found![/red]")
return {}
requirements = {}
with open(req_file, "r") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#"):
# Handle version specifiers if present
package = line.split("==")[0].split(">=")[0].split("<=")[0].strip()
requirements[package.lower()] = package
return requirements
def check_and_install_requirements():
"""Check and install required packages from requirements.txt"""
print(term.clear)
print_header()
required_packages = read_requirements()
if not required_packages:
console.print(
"[red]No requirements found. Please check requirements.txt file.[/red]"
)
input("\nPress Enter to continue...")
return
installed_packages = {pkg.key: pkg.version for pkg in pkg_resources.working_set}
missing_packages = []
installed_packages_info = []
console.print(
"\n[bold]Checking package requirements from requirements.txt...[/bold]"
)
with Progress() as progress:
task = progress.add_task(
"[cyan]Checking packages...", total=len(required_packages)
)
for package_key, package_name in required_packages.items():
if package_key not in installed_packages:
missing_packages.append(package_name)
else:
installed_packages_info.append(
f"{package_name} (version {installed_packages[package_key]})"
)
progress.update(task, advance=1)
# Display currently installed packages
if installed_packages_info:
table = Table(
title="Currently Installed Packages",
box=box.ROUNDED,
show_header=True,
header_style="bold magenta",
)
table.add_column("Package", style="cyan")
table.add_column("Status", style="green")
for pkg_info in installed_packages_info:
table.add_row(pkg_info, "✓ Installed")
console.print(table)
# Install missing packages
if missing_packages:
table = Table(
title="Missing Packages",
box=box.ROUNDED,
show_header=True,
header_style="bold yellow",
)
table.add_column("Package", style="yellow")
table.add_column("Status", style="yellow")
for package in missing_packages:
table.add_row(package, "○ Not installed")
console.print(table)
if (
Prompt.ask(
"\nWould you like to install missing packages?",
choices=["y", "n"],
default="y",
)
== "y"
):
console.print("\n[yellow]Installing missing packages...[/yellow]")
with Progress() as progress:
task = progress.add_task(
"[cyan]Installing packages...", total=len(missing_packages)
)
for package in missing_packages:
try:
console.print(f"\nInstalling {package}...")
subprocess.check_call(
[sys.executable, "-m", "pip", "install", package],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
console.print(
f"[green]Successfully installed {package}[/green]"
)
except subprocess.CalledProcessError as e:
console.print(f"[red]Failed to install {package}[/red]")
console.print(f"[red]Error: {str(e)}[/red]")
progress.update(task, advance=1)
console.print("\n[green]Package installation completed![/green]")
else:
console.print("\n[green]All required packages are already installed![/green]")
# Final status check
missing_after_install = []
for package_key, package_name in required_packages.items():
if importlib.util.find_spec(package_key.split("[")[0]) is None:
missing_after_install.append(package_name)
if missing_after_install:
console.print(
"\n[yellow]Warning: Some packages may need manual installation:[/yellow]"
)
for package in missing_after_install:
console.print(f"[yellow]- {package}[/yellow]")
console.print(
"\n[yellow]Try installing them manually or check system requirements.[/yellow]"
)
console.print("\n[bold green]Dependency check completed![/bold green]")
input("\nPress Enter to continue...")
def print_header():
print(term.clear + term.bold_green("=" * 50))
print(term.bold_yellow(f"Welcome to the Analysis Scripts Menu"))
print(term.bold_white(f"Developed by Shahab Bahreini Jangjoo"))
print(term.bold_green("=" * 50))
def print_footer():
print(term.bold_green("=" * 50))
print(term.bold_white("Thank you for using the Plotter UI. Goodbye!"))
print(term.bold_green("=" * 50))
def run_adiabatic_condition_analysis():
"""Run the adiabatic condition analysis"""
fname = list_items(root=".", select_type="file", file_keywords=["3D"])
if fname:
try:
plotter(os.getcwd(), fname)
plt.show()
except Exception as e:
print(term.red(f"\nError during analysis: {str(e)}"))
def run_amplitude_analysis():
"""Run the amplitude analysis"""
fname = list_items(root=".", select_type="file", file_keywords=["3D"])
if fname:
try:
plot_amplitude_analysis_separate(os.getcwd(), fname, True)
plt.show()
except Exception as e:
print(term.red(f"\nError during analysis: {str(e)}"))
def run_adiabatic_invariance_check():
"""Run the adiabatic invariance check"""
fname = list_items(root=".", select_type="file", file_keywords=["3D"])
if fname:
try:
plotter_adiabatic_invariance_check(os.getcwd(), fname, True)
plt.show()
except Exception as e:
print(term.red(f"\nError during analysis: {str(e)}"))
def run_eta_fluctuations_analysis():
"""Run the η fluctuations analysis"""
fname = list_items(root=".", select_type="file", file_keywords=["3D"])
if fname:
try:
# First read the data
df = read_exported_csv_2Dsimulation(os.getcwd(), fname)
plot_eta_fluctuations(df, fname, True)
plt.show()
except Exception as e:
print(term.red(f"\nError during analysis: {str(e)}"))
def configure_settings():
"""Configure analysis settings"""
print(term.clear)
print(term.bold_blue("\nCurrent Settings:"))
settings = {
"is_multi_files": ("Enable multi-file analysis", bool),
"based_on_guiding_center": ("Use guiding center for calculations", bool),
"show_extremums_peaks": ("Show extremum peaks in plots", bool),
"extremum_of": ("Variable to find extremums", str),
"share_x_axis": ("Share x-axis in plots", bool),
}
for key, (desc, _) in settings.items():
current = getattr(config, key, "Not set")
print(term.cyan(f"{desc}: {current}"))
if input(term.bold("\nWould you like to modify settings? (y/n): ")).lower() == "y":
try:
for key, (desc, type_) in settings.items():
value = input(
term.bold(f"\nEnter new value for {desc} ({type_.__name__}): ")
)
if type_ == bool:
setattr(config, key, value.lower() in ("true", "yes", "1", "y"))
else:
setattr(config, key, type_(value))
config.save()
print(term.bold_green("\nSettings saved successfully!"))
except Exception as e:
print(term.red(f"\nError saving settings: {str(e)}"))
def peakfinder_menu():
console.print(
Panel(
"[bold blue]Peakfinder functionality:[/bold blue]",
style="bold white",
expand=False,
)
)
options = {
"1": ("Run Adiabatic Condition Analysis", run_adiabatic_condition_analysis),
"2": ("Run Amplitude Analysis", run_amplitude_analysis),
"3": ("Run Adiabatic Invariance Check", run_adiabatic_invariance_check),
"4": ("Run η Fluctuations Analysis", run_eta_fluctuations_analysis),
"5": ("Configure Settings", configure_settings),
"b": ("Back to Main Menu", None),
}
while True:
print(term.clear)
print_header()
print(term.bold_blue("\nPeak Finder Analysis Menu:"))
for key, (desc, _) in options.items():
print(term.cyan(f"[{key}] {desc}"))
choice = input(term.bold("\nSelect an option: ")).strip().lower()
if choice == "b":
return
elif choice in options and options[choice][1]:
try:
options[choice][1]()
input(term.bold_green("\nPress Enter to continue..."))
except Exception as e:
print(term.red(f"\nError: {str(e)}"))
input(term.bold_red("\nPress Enter to continue..."))
def plotter_2d_menu():
"""Submenu for 2D plotter functionality"""
options = {
"1": ("Select files from a folder", "multimode"),
"2": ("Select a single file from the current directory", "singlemode"),
"b": ("Back to Main Menu", None),
}
while True:
print(term.clear)
print_header()
console.print(
Panel(
"[bold blue]2D Plotter Menu:[/bold blue]",
style="bold white",
expand=False,
)
)
for key, (desc, _) in options.items():
print(term.cyan(f"[{key}] {desc}"))
choice = input(term.bold("\nSelect an option: ")).strip().lower()
if choice == "b":
return
if choice in options and options[choice][1]:
mode = options[choice][1]
try:
if mode == "multimode":
console.print("[bold]Select a folder containing CSV files:[/bold]")
selected_folder, selected_files = list_folders()
folder_path = os.path.join(".", selected_folder)
else:
folder_path = os.getcwd()
selected_file = list_items(
root=".", select_type="file", file_keywords=["2D"]
)
selected_files = [selected_file]
param_options = [
"timestamp",
"drho",
"dz",
"rho",
"z",
"omega_rho",
"omega_z",
]
x_param, y_param = select_plotting_parameters(param_options)
with Progress() as progress:
task = progress.add_task("[cyan]Generating plot...", total=100)
plot_csv_files(
selected_files,
folder_path,
x_param,
y_param,
mode,
progress,
task,
)
input(term.bold_green("\nPress Enter to continue..."))
except Exception as e:
console.print(f"[red]\nError: {str(e)}[/red]")
input(term.bold_red("\nPress Enter to continue..."))
else:
console.print("[red]Invalid choice. Please try again.[/red]")
input(term.bold_red("\nPress Enter to continue..."))
def plotter_3d_menu():
"""Submenu for 3D plotter functionality"""
options = {
"1": ("Select files from a folder", "multimode"),
"2": ("Select a single file from the current directory", "singlemode"),
"b": ("Back to Main Menu", None),
}
while True:
print(term.clear)
print_header()
console.print(
Panel(
"[bold blue]3D Plotter Menu:[/bold blue]",
style="bold white",
expand=False,
)
)
for key, (desc, _) in options.items():
print(term.cyan(f"[{key}] {desc}"))
choice = input(term.bold("\nSelect an option: ")).strip().lower()
if choice == "b":
return
if choice in options and options[choice][1]:
mode = options[choice][1]
try:
if mode == "multimode":
console.print("[bold]Select a folder containing CSV files:[/bold]")
selected_folder, selected_files = list_folders()
folder_path = os.path.join(".", selected_folder)
else:
folder_path = os.getcwd()
selected_file = list_items(
root=".", select_type="file", file_keywords=["3D"]
)
selected_files = [selected_file]
# Read the first CSV file to get available parameters
df = pd.read_csv(os.path.join(folder_path, selected_files[0]))
# Get plot configuration
plot_type = Prompt.ask(
"Enter the plot type", choices=["2d", "3d"], default="3d"
)
if plot_type == "2d":
display_available_parameters(df)
x_param = get_parameter_from_input(df, "Enter parameter for x-axis")
y_param = get_parameter_from_input(df, "Enter parameter for y-axis")
coord_system = "cartesian"
else:
coord_system = Prompt.ask(
"Enter coordinate system",
choices=["cartesian", "cylindrical"],
default="cartesian",
)
x_param = y_param = None
# Additional plot settings
use_scatter = Confirm.ask("Use scatter plot?", default=True)
use_time_color = Confirm.ask("Use time-based coloring?", default=True)
show_projections = Confirm.ask("Show projections?", default=False)
with Progress() as progress:
task = progress.add_task("[cyan]Generating plot...", total=100)
try:
Plotter_2d3d(
selected_files,
folder_path,
x_param,
y_param,
plot_type,
coord_system,
use_scatter,
use_time_color,
show_projections,
progress=progress,
task=task,
)
console.print("\n")
console.print(
Panel(
"[green]✓ Plot generated successfully![/green]",
title="Success",
border_style="green",
)
)
except Exception as e:
console.print("\n")
console.print(
Panel(
f"[red]Error during plot generation:",
title="Error",
border_style="red",
)
)
raise
input(term.bold_green("\nPress Enter to continue..."))
except Exception as e:
console.print(f"[red]\nError: {str(e)}[/red]")
input(term.bold_red("\nPress Enter to continue..."))
else:
console.print("[red]Invalid choice. Please try again.[/red]")
input(term.bold_red("\nPress Enter to continue..."))
def plotter_comparison_menu():
"""Submenu for Comparison Approximated 1D to Exact 2D/3D Solution"""
options = {
"1": ("Run Comparison 1D to Exact 2D/3D", run_comparison_1d_to_exact),
"2": ("Run Comparison 2D to Exact 3D", run_2d_3d_comparison),
"b": ("Back to Main Menu", None),
}
while True:
print(term.clear)
print_header()
console.print(
Panel(
"[bold blue]Comparison Approximated 1D to Exact 2D/3D Menu:[/bold blue]",
style="bold white",
expand=False,
)
)
for key, (desc, _) in options.items():
print(term.cyan(f"[{key}] {desc}"))
choice = input(term.bold("\nSelect an option: ")).strip().lower()
if choice == "b":
return
elif choice in options and options[choice][1]:
try:
options[choice][1]()
input(term.bold_green("\nPress Enter to continue..."))
except Exception as e:
console.print(f"[red]\nError: {str(e)}[/red]")
input(term.bold_red("\nPress Enter to continue..."))
else:
console.print("[red]Invalid choice. Please try again.[/red]")
input(term.bold_red("\nPress Enter to continue..."))
def run_comparison_1d_to_exact():
"""Run the comparison analysis between 1D and Exact 2D/3D solutions"""
parameter_mapping = {
"eps": r"$\epsilon$",
"epsphi": r"$\epsilon_\phi$",
"kappa": r"$\kappa$",
"deltas": r"$\delta_s$",
"beta": r"$\beta_0$",
"alpha": r"$\alpha_0$",
"theta": r"$\theta_0$",
"time": r"$\tau$",
}
try:
with Progress() as progress:
task = progress.add_task("[cyan]Running comparison analysis...", total=100)
# Get the current folder path
folder_path = os.getcwd()
# File selection for 2D reference data
progress.update(
task,
description="[cyan]Selecting 2D reference solution file...",
advance=10,
)
file_2d, all_files = list_comparison_files(
folder_path, comparison_type="1D_2D", file_role="reference"
)
if not file_2d:
console.print(
Panel(
"[red]Process cancelled: No file selected for 2D reference data.[/red]\n"
"[yellow]Please ensure 2D solution files are present in the directory.[/yellow]",
title="Error",
border_style="red",
)
)
return
# File selection for 1D comparison data
progress.update(
task,
description="[cyan]Selecting 1D approximation file...",
advance=10,
)
file_1d, _ = list_comparison_files(
folder_path, comparison_type="1D_2D", file_role="approximation"
)
if not file_1d:
console.print(
Panel(
"[red]Process cancelled: No file selected for 1D approximation data.[/red]\n"
"[yellow]Please ensure 1D solution files are present in the directory.[/yellow]",
title="Error",
border_style="red",
)
)
return
# Validate file selections
if file_2d == file_1d:
console.print(
Panel(
"[red]Error: Same file selected for both 2D reference and 1D approximation.[/red]\n"
"[yellow]Please select different files for comparison.[/yellow]",
title="Error",
border_style="red",
)
)
return
# Display selected files information
console.print(
Panel(
f"[bold green]Selected files for comparison:[/bold green]\n\n"
f"[blue]2D Reference:[/blue] {os.path.basename(file_2d)}\n"
f"[green]1D Approximation:[/green] {os.path.basename(file_1d)}",
title="Comparison Setup",
border_style="cyan",
)
)
if not file_1d:
console.print("[red]No file selected for 1D comparison data.[/red]")
return
console.print(
Panel(
f"[bold green]Selected files:[/bold green]\n"
f"2D (reference): {os.path.basename(file_2d)}\n"
f"1D (comparison): {os.path.basename(file_1d)}",
style="bold white",
expand=False,
)
)
# Data loading and processing
progress.update(task, advance=20)
df_2d = pd.read_csv(file_2d)
df_1d = pd.read_csv(file_1d)
time_2d = df_2d["timestamp"]
z_2d = df_2d["z"]
time_1d = df_1d["timestamp"]
z_1d = df_1d["z"]
progress.update(task, advance=20)
interpolated_z_1d = np.interp(time_2d, time_1d, z_1d)
# Difference analysis
progress.update(task, advance=10)
result = find_first_difference(time_2d, z_2d, interpolated_z_1d, 10)
if result is None:
result = find_first_difference(time_2d, z_2d, interpolated_z_1d, 1)
# Plot creation
progress.update(task, advance=20)
fig, ax = plt.subplots(figsize=(12, 7), dpi=100)
ax.plot(
time_2d,
z_2d,
label="2D Exact Z Solution (Reference)",
color="#2E86C1",
linewidth=2,
alpha=0.8,
)
ax.plot(
time_2d,
interpolated_z_1d,
label="1D Approximate Z Solution",
color="#E67E22",
linewidth=2,
linestyle="--",
alpha=0.8,
)
if result is not None:
annotation_text = (
f"Error > {result['threshold']}% Difference\n"
f"────────────────────\n"
f"Time: {result['time']:.3f}\n"
r"$Z_{2D}$" + f"(Exact): {result['z_2d']:.3f}\n"
r"$Z_{1D}$" + f"(Approximated): {result['z_1d']:.3f}\n"
f"Δ: {result['difference']:.2f}%"
)
create_fancy_annotation(
fig,
ax,
xy=(result["time"], result["z_2d"]),
text=annotation_text,
xytext=(
result["time"] - (time_2d.max() - time_2d.min()) * 0.1,
result["z_2d"] + (z_2d.max() - z_2d.min()) * 0.1,
),
)
ax.plot(
result["time"],
result["z_2d"],
"o",
color="#E74C3C",
markersize=8,
alpha=0.8,
)
title = "Trajectory Comparison - Reduced 1D Equation vs Exact Solution"
else:
title = "Trajectory Comparison (No significant differences found)"
ax.set_title(title, pad=20, fontsize=12, fontweight="bold")
ax.set_xlabel(r"$\tau$", labelpad=10)
ax.set_ylabel(r"$\tilde z$", labelpad=10)
ax.legend(
loc="upper right",
framealpha=0.95,
edgecolor="#666666",
fancybox=True,
shadow=True,
)
for spine in ax.spines.values():
spine.set_color("#CCCCCC")
spine.set_linewidth(0.8)
parameters = extract_parameters_by_file_name(file_2d)
param_text = "\n".join(
f"{parameter_mapping.get(key, key)}: {value}"
for key, value in parameters.items()
)
ax.text(
0.02,
0.95,
"Simulation Parameters:\n" + param_text,
transform=ax.transAxes,
fontsize=10,
verticalalignment="top",
bbox=dict(
boxstyle="round,pad=0.5",
facecolor="white",
edgecolor="#CCCCCC",
alpha=0.9,
linewidth=0.5,
),
)
# Final plot adjustments and saving
progress.update(task, advance=10)
plt.tight_layout()
save_plots_with_timestamp(fig, "1D_2D_z_solutions_comparison")
plt.show()
except Exception as e:
console.print(f"[red]\nError: {str(e)}[/red]")
raise # Re-raise the exception to be caught by the menu handler
def calculate_error_statistics(df_1d, df_ref):
"""
Calculate error statistics between 1D approximation and reference solution.
Args:
df_1d (pd.DataFrame): 1D approximation data
df_ref (pd.DataFrame): Reference solution data
Returns:
dict: Dictionary containing error statistics
"""
# Note: Implement this function based on your specific needs
# This is a placeholder implementation
return {
"max_error": 0.0, # Maximum relative error
"avg_error": 0.0, # Average relative error
"rms_error": 0.0, # Root mean square error
}
def run_2d_3d_comparison():
"""
Run the comparison analysis between 2D and 3D solutions.
Handles file selection, data validation, and visualization of the comparison.
"""
console = Console()
try:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TaskProgressColumn(),
TimeRemainingColumn(),
) as progress:
main_task = progress.add_task(
"[cyan]Running 2D-3D comparison analysis...", total=100
)
# Get the current folder path
folder_path = os.getcwd()
# File selection for 2D data
progress.update(
main_task, description="[cyan]Selecting 2D solution file...", advance=10
)
file_2d, all_files = list_comparison_files(
folder_path, comparison_type="2D_3D", file_role="2D"
)
if not file_2d:
console.print(
Panel(
"[red]Process cancelled: No file selected for 2D data.[/red]",
title="Error",
border_style="red",
)
)
return
# File selection for 3D data
progress.update(
main_task, description="[cyan]Selecting 3D solution file...", advance=10
)
file_3d, _ = list_comparison_files(
folder_path, comparison_type="2D_3D", file_role="3D"
)
if not file_3d:
console.print(
Panel(
"[red]Process cancelled: No file selected for 3D data.[/red]",
title="Error",
border_style="red",
)
)
return
# Validate file selections
if file_2d == file_3d:
console.print(
Panel(
"[red]Error: Same file selected for both 2D and 3D data.[/red]\n"
"[yellow]Please select different files for comparison.[/yellow]",
title="Error",
border_style="red",
)
)
return
# Display selected files information
console.print(
Panel(
f"[bold green]Selected files for comparison:[/bold green]\n\n"
f"[blue]2D Solution:[/blue] {os.path.basename(file_2d)}\n"
f"[green]3D Solution:[/green] {os.path.basename(file_3d)}",
title="Comparison Setup",
border_style="cyan",
)
)
# Load and validate data
progress.update(
main_task,
description="[cyan]Loading and validating data...",
advance=20,
)
# Perform comparison analysis
progress.update(
main_task,
description="[cyan]Performing comparison analysis...",
advance=30,
)
try:
# Create comparison visualization
progress.update(
main_task,
description="[cyan]Generating visualization...",
advance=20,
)
plot_comparison(file_2d, file_3d)
# Final update
progress.update(
main_task,
description="[green]Comparison completed successfully!",
completed=100,
)
# Display success message
console.print(
Panel(
"[green]✓ Comparison analysis completed successfully![/green]\n\n"
"[blue]Summary:[/blue]\n"
f"• 2D Solution: {os.path.basename(file_2d)}\n"
f"• 3D Solution: {os.path.basename(file_3d)}\n"
"\n[yellow]The comparison plot has been generated.[/yellow]",
title="Analysis Complete",
border_style="green",
)
)
except Exception as e:
console.print(
Panel(
f"[red]Error during comparison analysis:[/red]\n{str(e)}\n\n"
"[yellow]Please check your data format and try again.[/yellow]",
title="Analysis Error",
border_style="red",
)
)
raise
except KeyboardInterrupt:
console.print(
Panel(
"[yellow]Process interrupted by user.[/yellow]",
title="Cancelled",
border_style="yellow",
)
)
return
except Exception as e:
console.print(
Panel(
f"[red]Unexpected error:[/red]\n{str(e)}\n\n"
"[yellow]Please report this issue if it persists.[/yellow]",
title="Error",
border_style="red",
)
)
raise
def conservation_plots_menu():
"""Submenu for conservation plots functionality"""
options = {
"1": ("Energy Conservation Plot", "energy"),
"2": ("Momentum Conservation Plot", "momentum"),
"b": ("Back to Main Menu", None),
}
while True:
print(term.clear)
print_header()
console.print(
Panel(
"[bold blue]Conservation Plots Menu:[/bold blue]",
style="bold white",
expand=False,