packages = ["numpy", "matplotlib"] [[fetch]] files = ["numpy_fno_engine.py", "weights_real.npz"]

Real-Time AI Surrogate for Fluid-Thermal Dynamics

Zero-Install Browser Inference via Fourier Neural Operators (FNO)

1.5 m/s
293.15 K
Initializing Python Kernel & Solver...
from pyscript import window, document, display from numpy_fno_engine import NumPyFNOEngine import numpy as np import matplotlib.pyplot as plt # ========================================== # MODULE 3: Global AI Engine Initialization # ========================================== engine = None try: # 1. Instantiate the engine engine = NumPyFNOEngine() # 2. Load the weights globally engine.load_global_weights("weights_real.npz") print("SUCCESS: CFD Surrogate Engine initialized and weights loaded successfully.") # 3. DOM Manipulation (Ready State) loading_indicator = document.getElementById("loading-indicator") if loading_indicator: loading_indicator.style.display = "none" plot_container = document.getElementById("plot-container") if plot_container: plot_container.style.display = "block" compute_btn = document.getElementById("compute-btn") if compute_btn: compute_btn.innerText = "Compute AI Surrogate: Ready" compute_btn.disabled = False except Exception as error: print(f"ERROR: Initialization failed. {error}") loading_indicator = document.getElementById("loading-indicator") if loading_indicator: loading_indicator.innerText = "Fatal Error: Failed to load AI Engine Weights." loading_indicator.style.color = "red" # ========================================== # MODULE 4: Inference & Visualization Logic # ========================================== def run_inference(event): global engine if engine is None: print("Engine not initialized.") return # 1. Read DOM & Cast to float velocity = float(document.getElementById('vel-slider').value) temperature = float(document.getElementById('temp-slider').value) # 2. Tensor Preparation (The Mask) input_tensor = np.zeros((2, 64, 64)) input_tensor[0, 25:38, 0] = velocity input_tensor[1, 25:38, 0] = temperature # 3. Inference output = engine.predict(input_tensor) # 4. Matplotlib Rendering fig, axs = plt.subplots(1, 3, figsize=(15, 4)) im0 = axs[0].imshow(output[0, :, :], origin='lower', cmap='jet') axs[0].set_title('Velocity X') fig.colorbar(im0, ax=axs[0]) im1 = axs[1].imshow(output[1, :, :], origin='lower', cmap='jet') axs[1].set_title('Velocity Y') fig.colorbar(im1, ax=axs[1]) im2 = axs[2].imshow(output[2, :, :], origin='lower', cmap='jet') axs[2].set_title('Temperature') fig.colorbar(im2, ax=axs[2]) fig.tight_layout() # 5. DOM Injection & Memory Management document.getElementById('plot-container').innerHTML = "" display(fig, target="plot-container") plt.close(fig) # Bind the event to the button compute_btn = document.getElementById('compute-btn') if compute_btn: compute_btn.onclick = run_inference