-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathvisualization_charged.py
135 lines (113 loc) · 4.75 KB
/
visualization_charged.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright (C) 2010-2019 The ESPResSo project
#
# This file is part of ESPResSo.
#
# ESPResSo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ESPResSo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Visualization sample for charged particles. Simulates a pool of particles
with various charges, LJ parameters and masses.
"""
import espressomd
from espressomd.visualization_opengl import openGLLive
from espressomd import electrostatics
import numpy as np
required_features = ["P3M", "LENNARD_JONES", "MASS"]
espressomd.assert_features(required_features)
box = [40, 40, 40]
system = espressomd.System(box_l=box)
system.cell_system.set_domain_decomposition(use_verlet_lists=True)
visualizer = openGLLive(system, background_color=[1, 1, 1],
drag_enabled=True, drag_force=10)
system.set_random_state_PRNG()
# TIMESTEP
time_step_fs = 1.0
system.time_step = time_step_fs * 1.0e-2
system.cell_system.skin = 1.2
# TEMPERATURE
SI_temperature = 400.0
kb_kjmol = 0.0083145
temperature = SI_temperature * kb_kjmol
# COULOMB PREFACTOR (elementary charge)^2 / (4*pi*epsilon) in Angstrom*kJ/mol
epsilon_r = 4.0 # dimensionless
epsilon_0 = 8.8541878128e-12 # units of [C^2/J/m]
q_e = 1.602176634e-19 # units of [C]
avogadro = 6.022e23 # units of [mol]
prefactor = q_e**2 / (4 * np.pi * epsilon_r * epsilon_0) # units of [J.m]
# convert energies to kJ/mol, with distances in Angstroms
coulomb_prefactor = prefactor * avogadro / 1000 * 1e10
# FORCE FIELDS
# distances in Angstroms, epsilons in kBT, masses in g/mol
species = ["Cl", "Na", "Colloid", "Solvent"]
types = {"Cl": 0, "Na": 1, "Colloid": 2, "Solvent": 3}
charges = {"Cl": -1.0, "Na": 1.0, "Colloid": -3.0, "Solvent": 0.0}
lj_sigmas = {"Cl": 3.85, "Na": 2.52, "Colloid": 10.0, "Solvent": 1.5}
lj_epsilons = {"Cl": 192.45, "Na": 17.44,
"Colloid": 100.0, "Solvent": 50.0}
lj_cuts = {"Cl": 2.0 * lj_sigmas["Cl"], "Na": 2.0 * lj_sigmas["Na"],
"Colloid": 1.5 * lj_sigmas["Colloid"],
"Solvent": 2.0 * lj_sigmas["Solvent"]}
masses = {"Cl": 35.453, "Na": 22.99, "Colloid": 300, "Solvent": 18.0}
n_ionpairs = 50
for i in range(n_ionpairs):
for t in ["Na", "Cl"]:
system.part.add(pos=box * np.random.random(3),
q=charges[t], type=types[t], mass=masses[t])
n_colloids = 30
t = "Colloid"
t_co = "Na"
for i in range(n_colloids):
system.part.add(pos=box * np.random.random(3),
q=charges[t], type=types[t], mass=masses[t])
for i in range(int(abs(charges[t]))):
system.part.add(pos=box * np.random.random(3),
q=charges[t_co], type=types[t_co], mass=masses[t_co])
n_solvents = 800
t = "Solvent"
for i in range(n_solvents):
system.part.add(pos=box * np.random.random(3),
q=charges[t], type=types[t], mass=masses[t])
def combination_rule_epsilon(rule, eps1, eps2):
if rule == "Lorentz":
return (eps1 * eps2)**0.5
else:
return ValueError("No combination rule defined")
def combination_rule_sigma(rule, sig1, sig2):
if rule == "Berthelot":
return (sig1 + sig2) * 0.5
else:
return ValueError("No combination rule defined")
# Lennard-Jones interactions parameters
for i in range(len(species)):
for j in range(i, len(species)):
s = [species[i], species[j]]
lj_sig = combination_rule_sigma(
"Berthelot", lj_sigmas[s[0]], lj_sigmas[s[1]])
lj_cut = combination_rule_sigma(
"Berthelot", lj_cuts[s[0]], lj_cuts[s[1]])
lj_eps = combination_rule_epsilon(
"Lorentz", lj_epsilons[s[0]], lj_epsilons[s[1]])
system.non_bonded_inter[types[s[0]], types[s[1]]].lennard_jones.set_params(
epsilon=lj_eps, sigma=lj_sig, cutoff=lj_cut, shift="auto")
energy = system.analysis.energy()
print("Before Minimization: E_total = {:.2e}".format(energy['total']))
system.minimize_energy.init(f_max=1000, gamma=30.0,
max_steps=1000, max_displacement=0.01)
system.minimize_energy.minimize()
energy = system.analysis.energy()
print("After Minimization: E_total = {:.2e}".format(energy['total']))
print("Tune p3m")
p3m = electrostatics.P3M(prefactor=coulomb_prefactor, accuracy=1e-1)
system.actors.add(p3m)
system.thermostat.set_langevin(kT=temperature, gamma=2.0, seed=42)
visualizer.run(1)