File size: 2,406 Bytes
7f9a25d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0f5437
7f9a25d
 
b0f5437
7f9a25d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
EM Embedded - Global Variables and Constants

Contains PyVista plotter, simulation data, caches, and constants
shared across the EM module.
"""
import numpy as np
import pyvista as pv

# Set PyVista to use off-screen rendering for Trame
pv.OFF_SCREEN = True

__all__ = [
    "plotter",
    "simulation_data",
    "current_mesh", 
    "data_frames",
    "X_grids",
    "Y_grids",
    "surface_clims",
    "stop_simulation",
    "snapshot_times",
    "z_scale",
    "GRID_SIZES",
    "DEFAULT_AXIS_TICKS",
    "EXCITATION_SURFACE_COLORSCALE",
    "qpu_ts_cache",
    "sim_ts_cache",
    "qpu_cfg_id_counter",
]

# --- Constants ---
GRID_SIZES = [8, 16, 32, 64, 128, 256, 512]
DEFAULT_AXIS_TICKS = (0.0, 0.25, 0.5, 0.75, 1.0)

EXCITATION_SURFACE_COLORSCALE = "Turbo"

# --- PyVista and Simulation Data ---
plotter = pv.Plotter()
simulation_data = None
current_mesh = None
data_frames = None
z_scale = 1.0
X_grids = {}  # Dictionary: field -> meshgrid X
Y_grids = {}  # Dictionary: field -> meshgrid Y
surface_clims = {}
stop_simulation = False  # Flag to stop running simulation
snapshot_times = None    # Times corresponding to saved frames

# --- Caches ---
# Cache for QPU Plotly export/selection
qpu_ts_cache = {
    "times": None,
    "series_map": None,
    "field": None,
    "fig": None,
    "positions_by_field": {"All": []},
    "key_to_label": {},
    "label_to_keys": {},
    "nx": None,
    "unique_fields": [],
}

# Cache for Simulator Time Series
sim_ts_cache = {
    "fig": None,
    "field": None,
}

# Stable id generator for QPU monitor config rows
qpu_cfg_id_counter = 1


def new_monitor_cfg(field: str = "Ez", points: str = "(8, 8)") -> dict:
    """Create a new QPU monitor configuration."""
    global qpu_cfg_id_counter
    cfg = {"id": qpu_cfg_id_counter, "field": field, "points": points}
    qpu_cfg_id_counter += 1
    return cfg


def reset_globals():
    """Reset global simulation state."""
    global simulation_data, current_mesh, data_frames, X_grids, Y_grids
    global surface_clims, stop_simulation, snapshot_times, z_scale
    
    simulation_data = None
    current_mesh = None
    data_frames = None
    X_grids = {}
    Y_grids = {}
    surface_clims = {}
    stop_simulation = False
    snapshot_times = None
    z_scale = 1.0


def set_stop_simulation(value: bool):
    """Set the stop simulation flag."""
    global stop_simulation
    stop_simulation = value