Initialization¶
The basics¶
BubbleBox.mdbox
lowers the threshold for initializing systems by assuming lot's of default settings. Actually, you can initialize a system without any parameters, defaulting to an infinite 3D space with a single particle at rest.
import bubblebox as bb
b = bb.mdbox() #initialize a default system
This is of course not that interesting, so you should at least provide a number of bubbles and the system size:
n_bubbles = 100
size = (10,10)
b = bb.mdbox(n_bubbles, size = size) #initialize closed 10 by 10 box with 100 bubbles distributed in a grid
b.view() #display system to screen
The size parameters¶
You can initialize a system of any dimensionality, and for each dimension you may choose one out of three boundary conditions. The dimensionality is automatically inferred from the number of parameters in size
, for instance:
b = bb.mdbox(n_bubbles, size = (2,2)) # a two dimensional closed system with size 2x2
b = bb.mdbox(n_bubbles, size = (2,3,4)) # a three dimensional closed system with size 2x3x4
b = bb.mdbox(n_bubbles, size = (2,2,2,2)) # a four dimensional closed system with size 2x2x2x2
The boundary conditions are inferred from the sign of the dimension. When negative, the boundary will be periodic (exiting particles will enter on the opposite side), when 0 the dimension will be infinite, and if positive the boundary will yield perfectly inelastic collisions (particles will bounce back). For instance:
b = bb.mdbox(n_bubbles, size = (2,-2)) # a two dimensional system, closed along x, periodic along y with size 2x2
b = bb.mdbox(n_bubbles, size = (-3,-3,-3)) # a three dimensional periodic system with size 3x3x3
b = bb.mdbox(n_bubbles, size = (0,0,0,0)) # a infinite four dimensional system
The showcase module¶
While you can easily build, customize and combine mdbox
-systems in various ways, it is in many cases easier to start with a default system. The showcase
module contains a range of predefined setups for mdbox
. An overview may be found by calling:
from IPython.display import Markdown
Markdown(help(bb.showcase))
These systems are initialized typically as
b = bb.showcase.fcc_custom() # a face-centered cubic lattice
b.view()
Graph-based (and SMILES) input¶
Finally, you may wish to start from a graph-based definition of connectivity. Two functionalities are included in mdbox
for doing this, with one requiring the installation of pysmiles
( ) and the other only requiring lists of nodes and edges.
A typically procedure is as follows:
from bubblebox.mdbox import smiles2graph, graph2bb
atoms, edges = smiles2graph("""CC(=O)NCCC1=CNc2c1cc(OC)cc2""") # read vanillin
b = graph2bb(atoms, edges, relax = False) #convert to mdbox object and relax
#b.evince()
b.view()
MDView(bg_color=[1.0, 1.0, 1.0], box=[50, 50, 50], colors=[[0.8277645921193568, 0.5855073902470505, 0.54152077…
for i in range(10000):
b.advance()
b.vel_ *= .999
b.update_view()
b.evince()
SpotlightView(aperture=0.0001, bg_color=[1.0, 1.0, 1.0], bonds=[[0, 1], [0, 17], [0, 18], [0, 19], [1, 2], [1,…
B = b*2
B.view()
MDView(bg_color=[1.0, 1.0, 1.0], box=[150, 150, 150], colors=[[0.5071324219989345, 0.3372346264213911, 0.92033…
b = bb.mdbox(100, size = (4,4))
b.view()
MDView(bg_color=[1.0, 1.0, 1.0], box=[4, 4], colors=[[0.16231944653007102, 0.09197764308060097, 0.112859250776…
from bubblebox.mdbox import pos2heatmap, no_force
import numpy as np
def particle_count(b, Nz = 11, col = None):
"""
Create a 2D density array for a set of various bubbles
Author: Audun Skau Hansen (a.s.hansen@kjemi.uio.no)
Hanan Gharayba
Keyword arguments:
pos -- array (2,n_particles) containing positions
masses -- array (n_particles) containing masses
Lx, Ly -- box dimensions
Nz -- number of bins in each dimension
Returns an Nz by Nz by 3 array containing colors according to
bubble density
"""
if type(Nz) in [int, float]:
Nz_ = [Nz for i in range(len(b.size))]
Nz = np.array(Nz_)
print(Nz)
Z = np.zeros(Nz, dtype = int)
for i in range(b.pos.shape[1]):
indx = []
for j in range(len(b.size)):
indx.append( int( Nz[j]*(b.pos[j,i] + b.size[j])/(2*b.size[j]) ) )
#ix, iy = int( Nz*(b.pos[0,i] + b.size[0])/(2*b.size[0]) ), int( Nz*(b.pos[1,i] + b.size[1])/(2*b.size[1]) )
Z[tuple(indx)] += 1
return Z
b = bb.mdbox(100, size =(4,4), vel = 2)
b.pos *= 0.0
b.set_forces(no_force)
for i in range(10):
b.advance()
#b.vel_ *= 0.998
Z = particle_count(b, Nz = np.array([6,4])) #pos2heatmap(b.pos, b.masses, b.size[0], b.size[1], Nz = 5)
#Z[:,:, 3]
print(np.sum(Z))
[6 4] 100
import matplotlib.pyplot as plt
for i in range(1000):
b.advance()
Nz = 4
Z = particle_count(b, Nz = np.array([4,4]))
plt.figure(figsize = (8,8))
#for i in range(Nz):
# plt.axhline(b.size[0]/)
for i in range(Nz):
plt.axhline(i-.5, linewidth = 1, color = (0,0,0))
plt.axvline(i-.5, linewidth = 1, color = (0,0,0))
for j in range(Nz):
t = np.linspace(0,2*np.pi, Z[i,j]+1)[:-1]
plt.plot(i+.2*np.cos(t), j+.2*np.sin(t), ".", markersize = 10, color = (.7,0,0))
plt.text(i, j, "%i" %Z[i,j], ha = "center", va = "center")
plt.axhline(Nz-.5, linewidth = 1, color = (0,0,0))
plt.axvline(Nz-.5, linewidth = 1, color = (0,0,0))
plt.axis("off")
plt.xlim(-1, Nz)
plt.ylim(-1, Nz)
plt.show()
print(np.sum(Z))
[4 4]
100
b.view()
MDView(bg_color=[1.0, 1.0, 1.0], box=[4, 4], colors=[[0.029335247596076308, 0.9416819404860864, 0.524573650447…
a = np.random.uniform(-1,1,(5,5,5))
a[tuple([1,2,3])]
-0.7412144383030279