Source code for mercurial.tests.enhancements.test_coupled_fields

"""Test Kuramoto‑coupled 2D neural fields with visual and auditory stimuli."""

import matplotlib.pyplot as plt
import numpy as np

from mercurial.simulation.engine import SimulationEngine

# Create engine
engine = SimulationEngine(dim=10)


# Define stimuli: Gaussian blob for visual (centred), and a shifted blob for auditory
[docs] def visual_stimulus(t, nx=64, ny=64, sigma=5.0): x = np.linspace(-nx / 2, nx / 2, nx) y = np.linspace(-ny / 2, ny / 2, ny) X, Y = np.meshgrid(x, y) # Modulated by time (pulse at t=0.5) amp = 0.2 * np.exp(-((t - 0.5) ** 2) / 0.1) return amp * np.exp(-(X**2 + Y**2) / (2 * sigma**2))
[docs] def auditory_stimulus(t, nx=64, ny=64, sigma=5.0): x = np.linspace(-nx / 2, nx / 2, nx) y = np.linspace(-ny / 2, ny / 2, ny) X, Y = np.meshgrid(x, y) # Shifted to the right amp = 0.2 * np.exp(-((t - 0.5) ** 2) / 0.1) return amp * np.exp(-((X - 15) ** 2 + Y**2) / (2 * sigma**2))
# Run coupled simulation t_span = (0.0, 2.0) results = engine.run_coupled_fields( t_span, dt=0.005, nx=64, ny=64, dx=0.1, coupling_strength=0.15, stimulus_A=visual_stimulus, stimulus_B=auditory_stimulus, save_history_every=20, ) # Plot final fields plt.figure(figsize=(12, 5)) plt.subplot(1, 2, 1) plt.imshow(results["E_A"][-1], cmap="hot", origin="lower") plt.colorbar(label="Visual field activity") plt.title("Final Visual Field") plt.subplot(1, 2, 2) plt.imshow(results["E_B"][-1], cmap="hot", origin="lower") plt.colorbar(label="Auditory field activity") plt.title("Final Auditory Field") plt.tight_layout() plt.show() # Plot order parameters over time plt.figure() plt.plot(results["t"], results["R_A"], label="Visual R_A") plt.plot(results["t"], results["R_B"], label="Auditory R_B") plt.xlabel("Time (s)") plt.ylabel("Order Parameter R") plt.title("Cross‑Field Synchrony") plt.legend() plt.grid(True, alpha=0.3) plt.show() print(f"Final R_A = {results['final_R_A']:.4f}, R_B = {results['final_R_B']:.4f}")