Commit 80553a41 authored by Pieter van Velde's avatar Pieter van Velde

changed normalized weights in functions.py

parents 35c7edba 1b70cc07
import numpy as np
import sys
<<<<<<< HEAD
def add_bead(pos, n, T_dim):
phi = np.linspace(0, 2*np.pi - (2/n)*np.pi, n)
E = np.zeros(n)
for i in range(n):
pos_new = np.zeros(2)
pos_new[0] = pos[1,0] + np.cos(phi[i])
pos_new[1] = pos[1,1] + np.sin(phi[i])
rx = pos_new[0] - pos[:, 0]
ry = pos_new[1] - pos[:, 1]
r = np.sqrt(rx**2 + ry**2)
r[r==0] = np.nan
E[i] = np.sum(4*((1/r)**12 - (1/r)**6))
=======
def add_bead(pos, n, T_dim, j, w_jl, w_l):
# Standard phi, KAN NAAR MAIN.PY
phi_0 = np.linspace(0, 2 * np.pi - (2 / n) * np.pi, n)
# Calculate energy of different angles.
phi = phi_0 + (1 - 2 * np.random.rand(n)) * np.pi / n
# Calculate new positions
pos_new = np.zeros([n, 2])
pos_new[:, 0] = pos[j - 1, 0] + np.cos(phi)
pos_new[:, 1] = pos[j - 1, 1] + np.sin(phi)
# Calculate distance between new bead and beads
rx = np.zeros([n, j])
ry = np.zeros([n, j])
for i in range(n):
rx[i, :] = pos_new[i, 0] - pos[0:j, 0]
ry[i, :] = pos_new[i, 1] - pos[0:j, 1]
print('rx = ', rx)
r = np.sqrt(rx ** 2 + ry ** 2)
r[r == 0] = np.nan
# Calculate energy of different angles
E = np.sum(4 * ((1 / r) ** 12 - (1 / r) ** 6), axis=1)
print('E = ', E)
# for i in range(n):
# phi = phi_0 + (1 - 2 * np.random.rand(n)) * np.pi / n
# pos_new = np.zeros(2)
# pos_new[0] = pos[j - 1, 0] + np.cos(phi[i])
# pos_new[1] = pos[j - 1, 1] + np.sin(phi[i])
# rx = pos_new[0] - pos[0:j, 0]
# ry = pos_new[1] - pos[0:j, 1]
# r = np.sqrt(rx ** 2 + ry ** 2)
# r[r == 0] = np.nan
# E[i] = np.sum(4 * ((1 / r) ** 12 - (1 / r) ** 6))
# print('E = ', E)
# Calculate (normalized) weights
w = np.exp(-E / T_dim)
#if w == NaN:
#print('w = ', w)
w[np.isnan(w)] = 0
<<<<<<< HEAD
......@@ -70,20 +43,24 @@ def add_bead(pos, n, T_dim, j, w_jl, w_l):
pos = np.r_[pos, new]
=======
w_tot = np.sum(w)
# Stop execution if beads can only be placed (almost) on top of other beads
>>>>>>> 1b70cc076f246da4e36ffb50e1759b96fb1f921b
if w_tot == 0:
print('Help, I have nowhere to go')
print('w_tot = ', w_tot)
sys.exit()
# Calculate new position of next bead
P = w / w_tot
print('p = ', P)
final_phi = np.random.choice(phi, p=P)
pos[j] = np.array([[pos[j - 1, 0] + np.cos(final_phi), pos[j - 1, 1] + np.sin(final_phi)]])
# Calculate weights of one polymer to account for deviation form the theoretical value
w_idx = np.where(phi == final_phi)
print('w_idx = ', w_idx[0])
w_jl = w_jl * w[w_idx[0]]
w_l = w_l * w_tot
return pos
\ No newline at end of file
return pos, w_jl, w_l
\ No newline at end of file
......@@ -5,24 +5,16 @@ import time
from functions import add_bead
# Simulation constants
# Simulation parameters
n_beads = 100 #number of beads
n = 6 #number of angles
# Initialization
n = 6 # number of phis
n_beads = 100
T_dim = 1
# Physical parameters
T_dim = 0.1 #T * (Kb / eps) # Non-dimensional temperature
# Preallocate arrays
pos = np.zeros([n_beads, 2])
pos[1] = np.array([1, 0])
w_jl = 1
w_l = 1
# Initialization
pos[1] = np.array([1, 0]) #bead 1: pos[0, 0]. bead 2: pos[1, 0]
......@@ -34,8 +26,6 @@ for j in range(2,n_beads):
pos, w_jl, w_l = add_bead(pos, n, T_dim, j, w_jl, w_l)
P = w_jl/w_l
plt.plot(pos[:,0], pos[:,1], marker= ".", markersize=20, color="k" )
P = w_jl/w_l #Weight per polymer
......@@ -47,3 +37,4 @@ plt.show()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment