Skip to content

Instantly share code, notes, and snippets.

mseri/chladni.py

Forked from profConradi/chladni.py
Created April 9, 2024 19:51
Show Gist options
• Save mseri/33c0949ad801ffbb206de60436cb1572 to your computer and use it in GitHub Desktop.
Chladni Square Plate Normal Modes Simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def energy(z, n, m, L): return np.cos(n* np.pi *np.real(z) / L) *np.cos(m *np.pi*np.imag(z) / L) - np.cos(m*np.pi*np.real(z)/ L) *np.cos(n* np.pi *np.imag(z) / L) class ChladniPlate: def __init__(self, n, m, L=1, n_particles=10000): self.L = L self.n_particles = n_particles self.n = n self.m = m self.positions = np.random.uniform(0, self.L, (self.n_particles,))+1.j*np.random.uniform(0, self.L, (self.n_particles,)) self.history = [self.positions] def metropolis_step(self, z, eps, beta): energies = energy(z, self.n, self.m, self.L)**2 delta = np.random.uniform(-eps,eps,(self.n_particles,)) + 1.j*np.random.uniform(-eps,eps,(self.n_particles,)) new_energies = energy(z+delta, self.n, self.m, self.L)**2 r = np.exp(-beta*(new_energies-energies)) r[r>1]=1 x = np.random.uniform(0., 1., self.n_particles) new_positions = z+delta update = (x0) & (np.real(new_positions)0) & (np.imag(new_positions)
to join this conversation on GitHub. Already have an account? Sign in to comment