Graphics tips for modifying the animation:¶
All of these example work by using subclassing from AnimateSimul
, while just modifying a single function _anim_step()
Changing color in the animation¶
In [12]:
%matplotlib widget
import matplotlib.htmlplot as plt
import matplotlib.animation as animation
from matplotlib.collections import EllipseCollection, LineCollection
from simul import Simul
from animate import Animate
class ColorAnim(Animate):
def anim_step(self, m): # m is the number of calls that have occurred to this function
fcolor=['none','none', 'none', 'y']
self.circles.set_facecolor(fcolor)
ecolor = ['r', 'g', 'b', 'none']
self.circles.set_edgecolors(ecolor)
self.simul.md_step() # perform simulation step
self.circles.set_offsets(self.simul.position) # update positions on screen
simulation = Simul(simul_time=0.05, sigma=0.5, L=4)
animate = ColorAnim(simulation)
animate.go(nframes=10)
Animating particle sizes¶
In [9]:
%matplotlib inline
import matplotlib.htmlplot as plt
import matplotlib.animation as animation
from matplotlib.collections import EllipseCollection, LineCollection
from simul import Simul
from animate import Animate
class SizeAnim(Animate):
def anim_step(self, m): # m is the number of calls that have occurred to this function
self.simul.sigma *= 0.95
self.simul.md_step() # perform simulation step
self.circles.remove()
self.circles = EllipseCollection(widths=2*self.simul.sigma, heights=2*self.simul.sigma, angles=0, units='x',
offsets=self.simul.position, transOffset=self.ax.transData) # circles at pos
self.ax.add_collection(self.circles)
simulation = Simul(simul_time=0.02, sigma=0.5, L=4)
animate = SizeAnim(simulation)
animate.go(nframes=20)
Changing enclosing box dimensions¶
In [6]:
%matplotlib widget
import matplotlib.htmlplot as plt
import matplotlib.animation as animation
from matplotlib.collections import EllipseCollection, LineCollection
from simul import Simul
from animate import Animate
class SizeAnim(Animate):
def anim_step(self, m): # m is the number of calls that have occurred to this function
self.simul.md_step() # perform simulation step
self.simul.L *= 0.97
self.segment = [[[0, 0], [0, simulation.L], [simulation.L, simulation.L], [simulation.L, 0], [0, 0]]] # simulation cell
self.line.set_segments(self.segment) # redraw box if needed
self.circles.set_offsets(self.simul.position) # update positions on screen
simulation = Simul(simul_time=0.02, sigma=0.5, L=8)
animate = SizeAnim(simulation)
animate.go(nframes=20)
base construct
Using different particle sizes¶
In [7]:
%matplotlib widget
import matplotlib.htmlplot as plt
import matplotlib.animation as animation
from matplotlib.collections import EllipseCollection, LineCollection
from simul import Simul
from animate import Animate
class ManySizesAnim(Animate):
def anim_step(self, m): # m is the number of calls that have occurred to this function
sizes=[.2 , .3 ,.5,.6]
self.simul.md_step() # perform simulation step
self.circles.remove()
self.circles = EllipseCollection(widths=sizes, heights=sizes, angles=0, units='x',
offsets=self.simul.position, transOffset=self.ax.transData) # circles at pos
self.ax.add_collection(self.circles)
simulation = Simul(simul_time=0.02, sigma=0.5, L=4)
animate = ManySizesAnim(simulation)
animate.go(nframes=20)
base construct
Reset Zoom level¶
In [17]:
%matplotlib widget
import matplotlib.htmlplot as plt
import matplotlib.animation as animation
from matplotlib.collections import EllipseCollection, LineCollection
from simul import Simul
from animate import Animate
class ZoomAnim(Animate):
def anim_step(self, m): # m is the number of calls that have occurred to this function
self.ax.set_xlim(left=-.5, right=self.simul.L+5) # plotting limits on screen
self.ax.set_ylim(bottom=-0.5, top=self.simul.L+5)
self.simul.md_step() # perform simulation step
self.circles.set_offsets(self.simul.position) # update positions on screen
simulation = Simul(simul_time=0.02, sigma=0.5, L=8)
animate = ZoomAnim(simulation)
animate.go(nframes=20)
base construct
Draw a Stadium¶
In [16]:
%matplotlib widget
import matplotlib.htmlplot as plt
import matplotlib.animation as animation
from matplotlib.collections import EllipseCollection, LineCollection
from simul import Simul
from animate import Animate
from matplotlib.patches import Arc
class BoroAnim(Animate):
def __init__(self, simulation):
self.simul = simulation
self.fig, self.ax = plt.subplots(figsize=(5, 5)) # initialise graphics
self.circles = EllipseCollection(widths=2*simulation.sigma, heights=2*simulation.sigma, angles=0, units='x',
offsets=simulation.position, transOffset=self.ax.transData) # circles at position
self.ax.add_collection(self.circles)
self.segment = [[[0, 0], [0, simulation.L]]] # simulation cell
self.line = LineCollection(self.segment, colors='#000000') # one line
self.ax.add_collection(self.line)
self.segment = [[[simulation.L, 0], [simulation.L, simulation.L]]] # simulation cell
self.line = LineCollection(self.segment, colors='#000000') # second line
self.ax.add_collection(self.line)
self.arc= Arc ( xy=(self.simul.L/2,self.simul.L) , width=self.simul.L,height=self.simul.L, angle=0, theta1=0, theta2=180 )
self.ax.add_patch(self.arc)
self.arc= Arc ( xy=(self.simul.L/2,0) , width=self.simul.L,height=self.simul.L, angle=0, theta1=180, theta2=360 )
self.ax.add_patch(self.arc)
self.ax.set_xlim(left=-self.simul.L/2, right=self.simul.L+0.5) # plotting limits on screen
self.ax.set_ylim(bottom=-self.simul.L/2, top=3*self.simul.L/2+0.5)
self._ani = 0
simulation = Simul(simul_time=0.02, sigma=0.5, L=6)
animate = BoroAnim(simulation)
animate.go(nframes=20)
Draw a polymer¶
In [15]:
%matplotlib widget
import matplotlib.htmlplot as plt
import matplotlib.animation as animation
from matplotlib.collections import EllipseCollection, LineCollection
from simul import Simul
from animate import Animate
class PolyAnimate(Animate):
def __init__(self, simulation):
print('AnimateSimul')
self.simul = simulation
self.fig, self.ax = plt.subplots(figsize=(5, 5)) # initialise graphics
color = []
ecolor = []
for i in range(2):
color.append('r')
ecolor.append('none')
for i in range(2):
color.append('none')
ecolor.append('b')
self.circles = EllipseCollection(widths=2*simulation.sigma, heights=2*simulation.sigma, angles=0, units='x',
offsets=simulation.position, transOffset=self.ax.transData) # circles at pos
self.circles.set_facecolor(color)
self.ax.add_collection(self.circles)
self.circles.set_edgecolors(ecolor)
self.segment=[[[0, 0], [0, simulation.L], [simulation.L, simulation.L], [simulation.L, 0], [0, 0]]] # simulation cell
self.line = LineCollection(self.segment, colors='#000000') # draw square, black
self.ax.add_collection(self.line)
self.ax.set_xlim(left=-0.5, right=self.simul.L+0.5) # plotting limits on screen
self.ax.set_ylim(bottom=-0.5, top=self.simul.L+0.5)
self._ani = 0
p=self.simul.position # draw polymer lines
self.segment2 = [p[0:2]] # from particle 0, to particle 1
self.line2 = LineCollection(self.segment2, colors='r')
self.ax.add_collection(self.line2)
simulation = Simul(simul_time=0.02, sigma=0.5, L=8)
animate = PolyAnimate(simulation)
animate.go(nframes=100)
AnimateSimul