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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
import sys,os,unittest
from lammps import lammps, LMP_VAR_ATOM
class PythonCommand(unittest.TestCase):
def setUp(self):
machine=None
if 'LAMMPS_MACHINE_NAME' in os.environ:
machine=os.environ['LAMMPS_MACHINE_NAME']
self.lmp=lammps(name=machine,
cmdargs=['-nocite',
'-log','none',
'-echo','screen',
'-var','zpos','1.5',
'-var','x','2'])
# create demo input strings and files
# a few commands to set up a box with a single atom
self.demo_input="""
region box block 0 $x 0 2 0 2
create_box 1 box
create_atoms 1 single 1.0 1.0 ${zpos}
"""
# another command to add an atom and use a continuation line
self.cont_input="""
create_atoms 1 single &
0.2 0.1 0.1
"""
self.demo_file='in.test'
with open(self.demo_file,'w') as f:
f.write(self.demo_input)
self.cont_file='in.cont'
with open(self.cont_file,'w') as f:
f.write(self.cont_input)
# clean up temporary files
def tearDown(self):
if os.path.exists(self.demo_file):
os.remove(self.demo_file)
if os.path.exists(self.cont_file):
os.remove(self.cont_file)
##############################
def testFile(self):
"""Test reading commands from a file"""
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,0)
self.lmp.file(self.demo_file)
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,1)
self.lmp.file(self.cont_file)
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,2)
def testNoFile(self):
"""Test (not) reading commands from no file"""
self.lmp.file(None)
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,0)
def testCommand(self):
"""Test executing individual commands"""
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,0)
cmds = self.demo_input.splitlines()
for cmd in cmds:
self.lmp.command(cmd)
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,1)
def testCommandsList(self):
"""Test executing commands from list of strings"""
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,0)
cmds = self.demo_input.splitlines()+self.cont_input.splitlines()
self.lmp.commands_list(cmds)
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,2)
def testCommandsString(self):
"""Test executing block of commands from string"""
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,0)
self.lmp.commands_string(self.demo_input+self.cont_input)
natoms = self.lmp.get_natoms()
self.assertEqual(natoms,2)
def testNeighborList(self):
self.lmp.command("units lj")
self.lmp.command("atom_style atomic")
self.lmp.command("atom_modify map array")
self.lmp.command("boundary f f f")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
x = [
1.0, 1.0, 1.0,
1.0, 1.0, 1.5
]
types = [1, 1]
self.assertEqual(self.lmp.create_atoms(2, id=None, type=types, x=x), 2)
nlocal = self.lmp.extract_global("nlocal")
self.assertEqual(nlocal, 2)
self.lmp.command("mass 1 1.0")
self.lmp.command("velocity all create 3.0 87287")
self.lmp.command("pair_style lj/cut 2.5")
self.lmp.command("pair_coeff 1 1 1.0 1.0 2.5")
self.lmp.command("neighbor 0.1 bin")
self.lmp.command("neigh_modify every 20 delay 0 check no")
self.lmp.command("run 0")
self.assertEqual(self.lmp.find_pair_neighlist("lj/cut"), 0)
nlist = self.lmp.get_neighlist(0)
self.assertEqual(len(nlist), 2)
atom_i, numneigh_i, neighbors_i = nlist[0]
atom_j, numneigh_j, _ = nlist[1]
self.assertEqual(atom_i, 0)
self.assertEqual(atom_j, 1)
self.assertEqual(numneigh_i, 1)
self.assertEqual(numneigh_j, 0)
self.assertEqual(1, neighbors_i[0])
def test_extract_box_non_periodic(self):
self.lmp.command("boundary f f f")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
boxlo, boxhi, xy, yz, xz, periodicity, box_change = self.lmp.extract_box()
self.assertEqual(boxlo, [0.0, 0.0, 0.0])
self.assertEqual(boxhi, [2.0, 2.0, 2.0])
self.assertEqual(xy, 0.0)
self.assertEqual(yz, 0.0)
self.assertEqual(xz, 0.0)
self.assertEqual(periodicity, [0, 0, 0])
self.assertEqual(box_change, 0)
def test_extract_box_periodic(self):
self.lmp.command("boundary p p p")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
boxlo, boxhi, xy, yz, xz, periodicity, box_change = self.lmp.extract_box()
self.assertEqual(boxlo, [0.0, 0.0, 0.0])
self.assertEqual(boxhi, [2.0, 2.0, 2.0])
self.assertEqual(xy, 0.0)
self.assertEqual(yz, 0.0)
self.assertEqual(xz, 0.0)
self.assertEqual(periodicity, [1, 1, 1])
self.assertEqual(box_change, 0)
def test_extract_box_triclinic(self):
self.lmp.command("boundary p p p")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
self.lmp.command("change_box all triclinic")
self.lmp.command("change_box all xy final 0.1 yz final 0.2 xz final 0.3")
boxlo, boxhi, xy, yz, xz, periodicity, box_change = self.lmp.extract_box()
self.assertEqual(boxlo, [0.0, 0.0, 0.0])
self.assertEqual(boxhi, [2.0, 2.0, 2.0])
self.assertEqual(xy, 0.1)
self.assertEqual(yz, 0.2)
self.assertEqual(xz, 0.3)
self.assertEqual(periodicity, [1, 1, 1])
self.assertEqual(box_change, 0)
def test_reset_box(self):
self.lmp.command("boundary p p p")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
self.lmp.command("change_box all triclinic")
self.lmp.command("change_box all xy final 0.1 yz final 0.2 xz final 0.3")
self.lmp.reset_box([0,0,0], [1,1,1], 0, 0, 0)
boxlo, boxhi, xy, yz, xz, periodicity, box_change = self.lmp.extract_box()
self.assertEqual(boxlo, [0.0, 0.0, 0.0])
self.assertEqual(boxhi, [1.0, 1.0, 1.0])
self.assertEqual(xy, 0)
self.assertEqual(yz, 0)
self.assertEqual(xz, 0)
self.assertEqual(periodicity, [1, 1, 1])
self.assertEqual(box_change, 0)
def test_extract_variable_equalstyle(self):
self.lmp.command("variable a equal 100")
a = self.lmp.extract_variable("a")
self.assertEqual(a, 100)
self.lmp.command("variable a equal 3.14")
a = self.lmp.extract_variable("a")
self.assertEqual(a, 3.14)
def test_extract_variable_atomstyle(self):
self.lmp.command("units lj")
self.lmp.command("atom_style atomic")
self.lmp.command("atom_modify map array")
self.lmp.command("boundary f f f")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
x = [
1.0, 1.0, 1.0,
1.0, 1.0, 1.5
]
types = [1, 1]
self.assertEqual(self.lmp.create_atoms(2, id=None, type=types, x=x), 2)
self.lmp.command("variable a atom x*x+y*y+z*z")
a = self.lmp.extract_variable("a", "all", LMP_VAR_ATOM)
self.assertEqual(a[0], x[0]*x[0]+x[1]*x[1]+x[2]*x[2])
self.assertEqual(a[1], x[3]*x[3]+x[4]*x[4]+x[5]*x[5])
def test_get_thermo(self):
self.lmp.command("units lj")
self.lmp.command("atom_style atomic")
self.lmp.command("atom_modify map array")
self.lmp.command("boundary f f f")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
x = [
1.0, 1.0, 1.0,
1.0, 1.0, 1.5
]
types = [1, 1]
self.lmp.create_atoms(2, id=None, type=types, x=x)
state = {
"step": 0,
"elapsed" : 0.0,
"elaplong": 0,
"dt" : 0.005,
"time" : 0.0,
"atoms" : 2.0,
"temp" : 0,
"press" : 0,
"pe" : 0.0,
"ke" : 0.0,
"etotal" : 0.0,
"enthalpy" : 0.0,
"vol" : 8.0,
"lx" : 2.0,
"ly" : 2.0,
"lz" : 2.0,
"xlo" : 0,
"xhi" : 2.0,
"ylo" : 0,
"yhi" : 2.0,
"zlo" : 0,
"zhi" : 2.0
}
for key, value in state.items():
result = self.lmp.get_thermo(key)
self.assertEqual(value, result, key)
def test_extract_global_double(self):
self.lmp.command("region box block -1 1 -2 2 -3 3")
self.lmp.command("create_box 1 box")
self.assertEqual(self.lmp.extract_global("boxxlo"), -1.0)
self.assertEqual(self.lmp.extract_global("boxxhi"), 1.0)
self.assertEqual(self.lmp.extract_global("boxylo"), -2.0)
self.assertEqual(self.lmp.extract_global("boxyhi"), 2.0)
self.assertEqual(self.lmp.extract_global("boxzlo"), -3.0)
self.assertEqual(self.lmp.extract_global("boxzhi"), 3.0)
##############################
if __name__ == "__main__":
unittest.main()
|