def test_harmonic_1d_force_potential(): """specifying the force should be identical to specifying the potential""" sim1 = fokker_planck(temperature=1 / k, drag=1, extent=10, resolution=.1, boundary=boundary.reflecting, potential=U) sim2 = fokker_planck(temperature=1 / k, drag=1, extent=10, resolution=.1, boundary=boundary.reflecting, force=F) steady_1 = sim1.steady_state() steady_2 = sim2.steady_state() assert np.allclose(steady_1, steady_2, atol=1e-15, rtol=0), 'steady state are equal' tf = 4e-1 x0 = 3 pdf_1 = sim1.propagate(delta_function(x0), tf, dense=True) pdf_2 = sim2.propagate(delta_function(x0), tf, dense=True) assert np.allclose(pdf_1, pdf_2, atol=1e-15, rtol=0), 'finite time are equal'
def test_harmonic_1d_finite_time(plot=False): """1D harmonic oscillator at finite time compared to analytic solution""" sim = fokker_planck(temperature=1 / k, drag=1, extent=10, resolution=.1, boundary=boundary.reflecting, potential=U) tf = 4e-1 x0 = 3 pdf = sim.propagate(delta_function(x0), tf) tau = 2 / sim.diffusion[0] S = 1 - np.exp(-4 * tf / tau) exact = np.exp(-U(sim.grid[0] - x0 * np.exp(-2 * tf / tau)) / S) exact /= np.sum(exact) if not plot: assert np.allclose(exact, pdf, atol=2e-3) else: plt.figure() plt.plot(pdf, label='numerical') plt.plot(exact, 'o', label='exact') plt.legend() plt.title('1D harmonic oscillator: finite time')
def test_uniform_1d_steady_state(): """the steady state solution should be uniform in 1D""" sim = fokker_planck(temperature=1 / k, drag=1, extent=1, resolution=.1, boundary=boundary.periodic) steady = sim.steady_state() dp = np.gradient(steady) assert np.allclose(dp, 0, atol=1e-15, rtol=0), 'PDF is uniform' assert np.allclose(np.sum(steady), 1, atol=1e-15, rtol=0), 'PDF adds to 1'
def test_force_from_data(): """the harmonic oscillator obtained from force data vs. from functional""" sim1 = fokker_planck(temperature=1 / k, drag=1, extent=10, resolution=.1, boundary=boundary.reflecting, force=F) x = np.linspace(-5, 5, 100) Fdata = F(x) newF = potential_from_data(x, Fdata) sim2 = fokker_planck(temperature=1 / k, drag=1, extent=10, resolution=.1, boundary=boundary.reflecting, force=newF) assert np.allclose(sim1.master_matrix._deduped_data(), sim2.master_matrix._deduped_data(), atol=3e-3, rtol=0)
def test_harmonic_1d_time_limit(): """propagating by a large amount of time should yield the same solution as the steady-state""" sim = fokker_planck(temperature=1 / k, drag=1, extent=10, resolution=.1, boundary=boundary.reflecting, potential=U) steady = sim.steady_state() tf = 400 x0 = 3 pdf = sim.propagate(delta_function(x0), tf, dense=True) assert np.allclose(pdf, steady, atol=1e-12, rtol=0)
def __init__(self, inputPatternName, Nsteps, stopTime): nm = 1e-9 viscosity = 8e-4 radius = 50 * nm drag = 6 * np.pi * viscosity * radius L = 20 * nm F = lambda x: 5e-21 * (np.sin(x / L) + 4) / L self.sim = fokker_planck(temperature=300, drag=drag, extent=600 * nm, resolution=10 * nm, boundary=boundary.periodic, force=F) self.servoPattern = self.setupServos(inputPatternName) self.planckPdf = self.funcify(self.servoPattern) self.planckPattern = self.planckify(self.planckPdf, Nsteps, stopTime)
def test_harmonic_1d_steady_state(plot=False): """1D harmonic oscillator steady-state compared to analytic solution""" sim = fokker_planck(temperature=1 / k, drag=1, extent=10, resolution=.1, boundary=boundary.reflecting, potential=U) steady = sim.steady_state() exact = np.exp(-U(sim.grid[0])) exact /= np.sum(exact) if not plot: assert np.allclose(exact, steady, atol=1e-15) else: plt.figure() plt.plot(steady, label='numerical') plt.plot(exact, 'o', label='exact') plt.legend() plt.title('1D harmonic oscillator: steady state')
def F(x, y): rad = np.sqrt(x**2 + y**2) phi = np.arctan2(y, x) L = 200 * nm Fphi = 1e-12 * rad / L * np.exp(-rad / L) Frad = 1e-12 * (1 - rad / L) * np.exp(-rad / L) Fx = -np.sin(phi) * Fphi + np.cos(phi) * Frad Fy = np.cos(phi) * Fphi + np.sin(phi) * Frad return np.array([Fx, Fy]) sim = fokker_planck(temperature=300, drag=drag, extent=[800 * nm, 800 * nm], resolution=10 * nm, boundary=boundary.reflecting, force=F) ### time-evolved solution pdf = gaussian_pdf(center=(-150 * nm, -150 * nm), width=30 * nm) p0 = pdf(*sim.grid) Nsteps = 200 time, Pt = sim.propagate_interval(pdf, 20e-3, Nsteps=Nsteps) ### animation fig = plt.figure(figsize=plt.figaspect(1 / 2)) ax1 = fig.add_subplot(1, 2, 1, projection='3d') surf = ax1.plot_surface(*sim.grid / nm, p0, cmap='viridis')
from fplanck import fokker_planck, boundary, gaussian_pdf import matplotlib as mpl mpl.rc('font', size=15) nm = 1e-9 viscosity = 8e-4 radius = 50 * nm drag = 6 * np.pi * viscosity * radius L = 20 * nm F = lambda x: 5e-21 * (np.sin(x / L) + 4) / L sim = fokker_planck(temperature=300, drag=drag, extent=600 * nm, resolution=10 * nm, boundary=boundary.periodic, force=F) ### steady-state solution steady = sim.steady_state() ### time-evolved solution pdf = gaussian_pdf(-150 * nm, 30 * nm) p0 = pdf(sim.grid[0]) Nsteps = 200 time, Pt = sim.propagate_interval(pdf, 2e-3, Nsteps=Nsteps) ### animation fig, ax = plt.subplots(constrained_layout=True)
import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib.animation import FuncAnimation from fplanck import fokker_planck, boundary, gaussian_pdf nm = 1e-9 viscosity = 8e-4 radius = 50 * nm drag = 6 * np.pi * viscosity * radius L = 20 * nm U = lambda x: 5e-21 * np.cos(x / L) sim = fokker_planck(temperature=300, drag=drag, extent=600 * nm, resolution=10 * nm, boundary=boundary.reflecting, potential=U) ### steady-state solution steady = sim.steady_state() ### time-evolved solution pdf = gaussian_pdf(-150 * nm, 30 * nm) p0 = pdf(sim.grid[0]) Nsteps = 200 time, Pt = sim.propagate_interval(pdf, 10e-3, Nsteps=Nsteps) ### animation fig, ax = plt.subplots()
import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib.animation import FuncAnimation from fplanck import fokker_planck, boundary, delta_function xc = -5 def drag(x): A = 1e-16 return A * ((x - xc)**2) sim = fokker_planck(temperature=1, drag=drag, extent=10, resolution=.05, boundary=boundary.reflecting) ### steady-state solution steady = sim.steady_state() ### time-evolved solution pdf = delta_function(xc) p0 = pdf(sim.grid[0]) Nsteps = 200 time, Pt = sim.propagate_interval(pdf, 1e5, Nsteps=Nsteps) ### animation fig, ax = plt.subplots()