Skip to content

Commit 9abba56

Browse files
committed
Added pretty print to script; checkpointing otherwise
1 parent 1bd4e7b commit 9abba56

File tree

3 files changed

+149
-53
lines changed

3 files changed

+149
-53
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import colorama
2+
import matplotlib.pyplot as plt
3+
from colorama import Fore, Style
4+
from seaborn import heatmap
5+
6+
colorama.init(autoreset=True)
7+
8+
9+
def print_header(text):
10+
"""Print a formatted header with color"""
11+
print(f"\n{Fore.CYAN}{Style.BRIGHT}{'=' * 80}")
12+
print(f"{Fore.CYAN}{Style.BRIGHT}{text.center(80)}")
13+
print(f"{Fore.CYAN}{Style.BRIGHT}{'=' * 80}{Style.RESET_ALL}")
14+
15+
16+
def print_section(text):
17+
"""Print a formatted section header with color"""
18+
print(f"\n{Fore.GREEN}{Style.BRIGHT}{'-' * 40}")
19+
print(f"{Fore.GREEN}{Style.BRIGHT}{text}")
20+
print(f"{Fore.GREEN}{Style.BRIGHT}{'-' * 40}{Style.RESET_ALL}")
21+
22+
23+
def print_param(name, value, unit=""):
24+
"""Print a parameter with color coding"""
25+
unit_str = f" {unit}" if unit else ""
26+
27+
if isinstance(value, (int, float)):
28+
# For integers, use comma as thousand separator
29+
if isinstance(value, int):
30+
formatted_value = f"{value:,}"
31+
# For floats with decimal places
32+
elif value.is_integer():
33+
formatted_value = f"{int(value):,}"
34+
else:
35+
# Extract the format precision if it exists in the value string
36+
if isinstance(value, str) and "." in value:
37+
parts = value.split(".")
38+
if len(parts) == 2 and parts[1].isdigit():
39+
precision = len(parts[1])
40+
formatted_value = f"{float(value):,.{precision}f}"
41+
else:
42+
formatted_value = value
43+
else:
44+
# Default formatting for floats
45+
formatted_value = f"{value:,.2f}" if isinstance(value, float) else value
46+
else:
47+
# For non-numeric values, use as is
48+
formatted_value = value
49+
50+
print(f"{Fore.YELLOW}{name}: {Fore.WHITE}{Style.BRIGHT}{formatted_value}{unit_str}")
51+
52+
53+
def arr_debug(arr, name, plot_heatmap=False):
54+
"""Enhanced array debug function with color coding"""
55+
print(f"\n{Fore.MAGENTA}Debug info for: {Style.BRIGHT}{name}{Style.RESET_ALL}")
56+
print(f" {Fore.BLUE}Shape: {Fore.WHITE}{arr.shape}")
57+
print(f" {Fore.BLUE}Range: {Fore.WHITE}{arr.min():.6f} to {arr.max():.6f}")
58+
print(f" {Fore.BLUE}Mean: {Fore.WHITE}{arr.mean():.6f}, {Fore.BLUE}Std: {Fore.WHITE}{arr.std():.6f}")
59+
60+
if plot_heatmap:
61+
plt.figure(figsize=(10, 8))
62+
plt.title(f"Heatmap of {name}")
63+
heatmap(arr)
64+
plt.show()

examples-unrendered/MM_random_dev/simulator.py

+71-53
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3+
from colorama import Fore
4+
from pretty_print import arr_debug, print_header, print_param, print_section
35
from scipy.fft import fftfreq, ifft2
46

7+
"""
8+
TRACKING:
9+
- [ ] Check if dy is correct; we are integrating in fourier space
10+
- [ ] Implement eq16 again
11+
- [ ] Set up the comparison numerical integration plot
12+
- [ ] Implement 10 realizations and plot average for F11, F22
13+
14+
15+
For N1 = 2**10, N2 = 2**7,
16+
Field min is
17+
F11 values are
18+
"""
19+
20+
################################
21+
# Flags
22+
plot_field = True
23+
plot_spectra = True
24+
use_eq15 = True # If False, use eq16
25+
large_domain = True
26+
527
# Physical params
628
sigma2 = 2.0
729
L_2d = 15000.0
@@ -11,42 +33,37 @@
1133
c = (8 * sigma2) / (9 * L_2d ** (2 / 3))
1234

1335
# Domain params
14-
L1 = 40 * L_2d
15-
L2 = 5 * L_2d
36+
L1 = 40 * L_2d if large_domain else L_2d
37+
L2 = 5 * L_2d if large_domain else L_2d / 8
1638
N1 = 2**10
1739
N2 = 2**7
1840

1941
dx = L1 / N1
2042
dy = L2 / N2
2143

22-
23-
################################
24-
# Flags
25-
plot_field = True
26-
plot_spectra = True
27-
28-
use_eq15 = True # If False, use eq16
29-
30-
31-
print("Physical parameters:")
32-
print(f"\t sigma2 = {sigma2:.2f}")
33-
print(f"\t L_2d = {L_2d:.2f}")
34-
print(f"\t psi = {psi:.2f}")
35-
print(f"\t z_i = {z_i:.2f}")
36-
37-
print(f"Obtained c: {c:.2f}")
38-
39-
print("Domain parameters:")
40-
print(f"\t L1 = {L1:.2f}")
41-
print(f"\t L2 = {L2:.2f}")
42-
print(f"\t N1 = {N1}")
43-
print(f"\t N2 = {N2}")
44-
45-
print("Problem parameters:")
46-
print(f"\t Use Equation 15 = {use_eq15}")
47-
print(f"\t Plot field = {plot_field}")
48-
print(f"\t Plot spectra = {plot_spectra}")
49-
44+
# Replace your print statements with these prettier versions
45+
print_header("WIND FIELD SIMULATOR")
46+
47+
print_section("Physical Parameters")
48+
print_param("sigma2", f"{sigma2:.2f}", "m²/s²")
49+
print_param("L_2d", f"{L_2d:.2f}", "m")
50+
print_param("psi", f"{np.rad2deg(psi):.2f}", "degrees")
51+
print_param("z_i", f"{z_i:.2f}", "m")
52+
print_param("c", f"{c:.4f}")
53+
54+
print_section("Domain Parameters")
55+
print_param("L1", f"{L1:.2f}", "m")
56+
print_param("L2", f"{L2:.2f}", "m")
57+
print_param("N1", N1)
58+
print_param("N2", N2)
59+
print_param("dx", f"{dx:.2f}", "m")
60+
print_param("dy", f"{dy:.2f}", "m")
61+
62+
print_section("Problem Configuration")
63+
print_param("Use Equation 15", f"{Fore.GREEN if use_eq15 else Fore.RED}{use_eq15}")
64+
print_param("Plot field", f"{Fore.GREEN if plot_field else Fore.RED}{plot_field}")
65+
print_param("Plot spectra", f"{Fore.GREEN if plot_spectra else Fore.RED}{plot_spectra}")
66+
print_param("Large domain", f"{Fore.GREEN if large_domain else Fore.RED}{large_domain}")
5067

5168
###############################################################################################################
5269
###############################################################################################################
@@ -120,8 +137,6 @@
120137
# C_12[i, j] = ((2 * np.pi)**2 / (L1 * L2) * phi_12[i, j])
121138
C_12[i, j] = 1
122139

123-
exit()
124-
125140
# Generate Gaussian white noise
126141
eta_1 = np.random.normal(0, 1, size=(N1, N2)) + 1j * np.random.normal(0, 1, size=(N1, N2))
127142
eta_2 = np.random.normal(0, 1, size=(N1, N2)) + 1j * np.random.normal(0, 1, size=(N1, N2))
@@ -133,35 +148,48 @@
133148
u1 = np.real(ifft2((C_11 * eta_1) + (C_12 * eta_2))) # Longitudinal component
134149
u2 = np.real(ifft2((C_12 * eta_1) + (C_22 * eta_2))) # Transverse component
135150

151+
arr_debug(u1, "u1", plot_heatmap=False)
152+
arr_debug(u2, "u2", plot_heatmap=False)
153+
154+
136155
# Verify total variance (should match sigma2)
137156
var_u1 = np.var(u1)
138157
var_u2 = np.var(u2)
139-
print(f"Variance of u1: {var_u1:.8f}, Variance of u2: {var_u2:.8f}")
140-
print(f"Target variance: {sigma2:.4f}")
158+
159+
# Later in your code, replace the variance print statements with:
160+
print_section("Variance Verification")
161+
print_param("Variance of u1", f"{var_u1:.8f}", "m²/s²")
162+
print_param("Variance of u2", f"{var_u2:.8f}", "m²/s²")
163+
print_param("Target variance", f"{sigma2:.4f}", "m²/s²")
164+
print_param("Ratio u1/target", f"{var_u1/sigma2:.4f}")
165+
print_param("Ratio u2/target", f"{var_u2/sigma2:.4f}")
141166

142167
# Now plot
143168
if plot_field:
144-
plt.figure(figsize=(12, 5))
169+
plt.figure(figsize=(10, 6), dpi=100)
170+
171+
x_km = np.linspace(0, L1 / 1000, N1)
172+
y_km = np.linspace(0, L2 / 1000, N2)
173+
X_km, Y_km = np.meshgrid(x_km, y_km, indexing="ij")
145174

146-
# Longitudinal (u) wind field - subplot (a)
147-
plt.subplot(211) # 1 row, 2 columns, 1st plot
148-
plt.imshow(u1.T, cmap="coolwarm")
149-
plt.colorbar(label="m/s")
175+
plt.subplot(211)
176+
im1 = plt.pcolormesh(X_km.T, Y_km.T, u1.T, cmap="RdBu_r", shading="auto")
177+
cbar1 = plt.colorbar(im1, label="[m s$^{-1}$]")
150178
plt.xlabel("x [km]")
151179
plt.ylabel("y [km]")
152180
plt.title("(a) u")
153181

154-
# Transverse (v) wind field - subplot (b)
155-
plt.subplot(212) # 1 row, 2 columns, 2nd plot
156-
plt.imshow(u2.T, cmap="coolwarm")
157-
plt.colorbar(label="m/s")
182+
plt.subplot(212)
183+
im2 = plt.pcolormesh(X_km.T, Y_km.T, u2.T, cmap="RdBu_r", shading="auto")
184+
cbar2 = plt.colorbar(im2, label="[m s$^{-1}$]")
158185
plt.xlabel("x [km]")
159186
plt.ylabel("y [km]")
160187
plt.title("(b) v")
161188

162189
plt.tight_layout()
163190
plt.show()
164191

192+
exit()
165193

166194
#####################################################################################################
167195
#####################################################################################################
@@ -182,16 +210,6 @@
182210
positive_mask = k1_arr >= 0
183211
k1_positive = k1_arr[positive_mask] # Positive wavenumbers
184212

185-
print("k1_arr.shape: ", k1_arr.shape)
186-
print("k1.shape: ", k1.shape)
187-
188-
print("u1_fft.shape: ", u1_fft.shape)
189-
print("psd_u1.shape: ", psd_u1.shape)
190-
print("F11.shape: ", F11.shape)
191-
192-
print("k1_positive.shape: ", k1_positive.shape)
193-
print("positive_mask.shape: ", positive_mask.shape)
194-
195213
F11_positive = F11[positive_mask]
196214
F22_positive = F22[positive_mask]
197215

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## TRACKING:
2+
3+
- [ ] Check if dy is correct; we are integrating in fourier space
4+
- [ ] Implement eq16 again
5+
- [ ] Set up the comparison numerical integration plot
6+
- [ ] Implement 10 realizations and plot average for F11, F22
7+
8+
9+
## Values
10+
11+
### 2/24/2025
12+
For N1 = 2^10, N2 = 2^7,
13+
Field min is -1.000000, Field max is 1.000000
14+
F11 values are

0 commit comments

Comments
 (0)