class PythonPyLammps(unittest.TestCase): def setUp(self): machine = None if 'LAMMPS_MACHINE_NAME' in os.environ: machine = os.environ['LAMMPS_MACHINE_NAME'] self.pylmp = PyLammps( name=machine, cmdargs=['-nocite', '-log', 'none', '-echo', 'screen']) self.pylmp.units("lj") self.pylmp.atom_style("atomic") self.pylmp.atom_modify("map array") if 'LAMMPS_CMAKE_CACHE' in os.environ: self.cmake_cache = {} with open(os.environ['LAMMPS_CMAKE_CACHE'], 'r') as f: for line in f: line = line.strip() if not line or line.startswith('#') or line.startswith( '//'): continue parts = line.split('=') key, value_type = parts[0].split(':') if len(parts) > 1: value = parts[1] if value_type == "BOOL": value = (value.upper() == "ON") else: value = None self.cmake_cache[key] = value def tearDown(self): self.pylmp.close() del self.pylmp def test_version(self): self.assertGreaterEqual(self.pylmp.version(), 20200824) def test_create_atoms(self): self.pylmp.region("box block", 0, 2, 0, 2, 0, 2) self.pylmp.create_box(1, "box") x = [1.0, 1.0, 1.0, 1.0, 1.0, 1.5] types = [1, 1] self.assertEqual( self.pylmp.lmp.create_atoms(2, id=None, type=types, x=x), 2) self.assertEqual(self.pylmp.system.natoms, 2) self.assertEqual(len(self.pylmp.atoms), 2) numpy.testing.assert_array_equal(self.pylmp.atoms[0].position, tuple(x[0:3])) numpy.testing.assert_array_equal(self.pylmp.atoms[1].position, tuple(x[3:6])) self.assertEqual(self.pylmp.last_run, None) def test_write_script(self): outfile = 'in.test_write_script' self.pylmp.write_script(outfile) self.assertTrue(os.path.exists(outfile)) os.remove(outfile) def test_runs(self): self.pylmp.lattice("fcc", 0.8442), self.pylmp.region("box block", 0, 4, 0, 4, 0, 4) self.pylmp.create_box(1, "box") self.pylmp.create_atoms(1, "box") self.pylmp.mass(1, 1.0) self.pylmp.velocity("all create", 1.44, 87287, "loop geom") self.pylmp.pair_style("lj/cut", 2.5) self.pylmp.pair_coeff(1, 1, 1.0, 1.0, 2.5) self.pylmp.neighbor(0.3, "bin") self.pylmp.neigh_modify("delay 0 every 20 check no") self.pylmp.fix("1 all nve") self.pylmp.variable("fx atom fx") self.pylmp.run(10) self.assertEqual(len(self.pylmp.runs), 1) self.assertEqual(self.pylmp.last_run, self.pylmp.runs[0]) self.assertEqual(len(self.pylmp.last_run.thermo.Step), 2) self.assertEqual(len(self.pylmp.last_run.thermo.Temp), 2) self.assertEqual(len(self.pylmp.last_run.thermo.E_pair), 2) self.assertEqual(len(self.pylmp.last_run.thermo.E_mol), 2) self.assertEqual(len(self.pylmp.last_run.thermo.TotEng), 2) self.assertEqual(len(self.pylmp.last_run.thermo.Press), 2) def test_info_queries(self): self.pylmp.lattice("fcc", 0.8442), self.pylmp.region("box block", 0, 4, 0, 4, 0, 4) self.pylmp.create_box(1, "box") self.pylmp.variable("a equal 10.0") self.pylmp.variable("b string value") self.assertEqual(self.pylmp.variables['a'].value, 10.0) self.assertEqual(self.pylmp.variables['b'].value, 'value') self.assertEqual(len(self.pylmp.variables), 2) self.assertEqual(self.pylmp.system.units, 'lj') self.assertEqual(self.pylmp.system.atom_style, 'atomic') self.assertEqual(self.pylmp.system.ntypes, 1) self.assertEqual(self.pylmp.system.natoms, 0) self.assertEqual(self.pylmp.communication.comm_style, 'brick') self.assertEqual(self.pylmp.communication.comm_layout, 'uniform') self.assertEqual(self.pylmp.communication.nprocs, 1) self.assertEqual(len(self.pylmp.computes), 3) self.assertEqual(self.pylmp.computes[0]['name'], 'thermo_temp') self.assertEqual(self.pylmp.computes[0]['style'], 'temp') self.assertEqual(self.pylmp.computes[0]['group'], 'all') self.assertEqual(self.pylmp.computes[1]['name'], 'thermo_press') self.assertEqual(self.pylmp.computes[1]['style'], 'pressure') self.assertEqual(self.pylmp.computes[1]['group'], 'all') self.assertEqual(self.pylmp.computes[2]['name'], 'thermo_pe') self.assertEqual(self.pylmp.computes[2]['style'], 'pe') self.assertEqual(self.pylmp.computes[2]['group'], 'all') self.assertEqual(len(self.pylmp.dumps), 0) self.pylmp.fix('one', 'all', 'nve') self.assertEqual(len(self.pylmp.fixes), 1) self.assertEqual(self.pylmp.fixes[0]['name'], 'one') self.assertEqual(self.pylmp.fixes[0]['style'], 'nve') self.assertEqual(self.pylmp.fixes[0]['group'], 'all') self.pylmp.group('none', 'empty') self.assertEqual(len(self.pylmp.groups), 2)
lmp.units("lj") lmp.atom_style("atomic") lmp.lattice("fcc", 0.8442) lmp.region("box", "block", 0, 4, 0, 4, 0, 4) lmp.create_box(1, "box") lmp.create_atoms(1, "box") lmp.mass(1, 1.0) lmp.velocity("all", "create", 10, 87287) lmp.pair_style("lj/cut", 2.5) lmp.pair_coeff(1, 1, 1.0, 1.0, 2.5) lmp.neighbor(0.3, "bin") lmp.neigh_modify("delay", 0, "every", 20, "check no") lmp.fix("1 all nve") a = l.extract_fix("2",2,2) print(a.contents) #nlocal = l.extract_global("nlocal",0) #print(nlocal) #lmp.fix("2 all addforce 1.0 0.0 0.0") #f = l.extract_atom("f",3) #a = np.ctypeslib.as_array(f.contents,shape=(natoms, 3)) #print(a) lmp.dump("id all atom 50 dumpT10.0.lammpstrj") lmp.thermo(50)
membrane.ymax - model.rod_length / 2, 0.0 + model.rod_length / 2, run_args.Lz - model.rod_length / 2) concentration = run_args.conc / model.rod_length**3 simulation.set_state_concentration( 0, concentration, 10, 10, opt=['region', 'gcmc_init', 'tfac_insert', 1.65]) # TEST DUMP... # py_lmp.thermo_style('custom', 'step atoms', 'pe temp') # py_lmp.variable('thermo_var', 'equal', '"stagger({:d}, 1)"'.format(out_freq)) # py_lmp.thermo('v_thermo_var') # py_lmp.dump('test_dump', 'all', 'custom', out_freq, dump_path+'_init', # 'id x y z type mol c_'+cluster_compute) # py_lmp.dump_modify('test_dump', 'sort id') # GENERATING INITIAL CONFIGURATION py_lmp.neigh_modify('every', 1, 'delay', 1) py_lmp.timestep(run_args.dt) py_lmp.run(1000) simulation.unset_state_concentration(0) py_lmp.unfix(zwalls_fix) py_lmp.reset_timestep(0) # ===== MEMBRANE ======================================================================== # create membrane (box update, create membrane & groups, ...) membrane.create_membrane(py_lmp, seed, append=True) py_lmp.fix(zwalls_fix, 'all', 'wall/lj126', 'zlo EDGE', 1.0, model.rod_radius, model.rod_radius * pow(2, 1. / 6), 'zhi EDGE', 1.0, model.rod_radius, model.rod_radius * pow(2, 1. / 6))
py_lmp.dimension(3) py_lmp.boundary("p p p") py_lmp.lattice("sc", 1 / (run_args.cell_size**3)) py_lmp.region("box", "block", -run_args.num_cells / 2, run_args.num_cells / 2, -run_args.num_cells / 2, run_args.num_cells / 2, -run_args.num_cells / 2, run_args.num_cells / 2) simulation.setup("box") simulation.create_rods(box=None) # ROD DYNAMICS py_lmp.fix("thermostat", "all", "langevin", run_args.temp, run_args.temp, run_args.damp, seed) #, "zero yes") simulation.set_rod_dynamics("nve", opt=["mol", model.rod_states[0]]) py_lmp.neigh_modify("every 1 delay 1") py_lmp.timestep(run_args.dt) # THERMALIZE INITIAL CONFIGURATION simulation.deactivate_state(0, vx_eps=5.0) py_lmp.run(1000) simulation.activate_state(0) py_lmp.reset_timestep(0) # GROUPS & COMPUTES if hasattr(run_args, 'label_micelles'): micelle_group = 'sol_tips' sol_tip_bead_type = model.state_structures[0][0][-1] py_lmp.variable(micelle_group, 'atom', '"type == {:d}"'.format(sol_tip_bead_type)) py_lmp.group(micelle_group, 'dynamic', simulation.rods_group, 'var',
class LammpsKernel(IGMKernel): def __init__(self, model, cfg, runid): self.cfg = cfg self.model = model self.runid = runid self.tmp_dir = self.cfg.tmpdir('optimization') self.randseed = int(self.cfg["optimization/kernel_opts/lammps/seed"]) self.initLammps() self.setupSimulationBox() self.setupParticles() self.setCoordinates() self.setRadii() self.setupNeighbor() def initLammps(self): """ setup lammps python interface, log file """ if self.cfg["optimization/kernel_opts/lammps/keep_logs"]: self.Lmp = PyLammps(cmdargs=["-log",os.path.join(self.tmp_dir, self.runid+".log")]) else: self.Lmp = PyLammps(cmdargs=["-log","none"]) self.Lmp.atom_style("bond") self.Lmp.boundary('s','s','s') def setupSimulationBox(self): """ setup lammps simulation box """ atom_types = 1 bond_types = 0 bond_per_atom = 0 for Res in self.model.restraints: atom_types += Res.extra_atom_types bond_types += Res.extra_bond_types bond_per_atom += Res.extra_bond_per_atom if res.type == "Envelope": xx , yy, zz = Res.a*1.2, Res.b*1.2, Res.c*1.2 self.Lmp.region("IGMBOX", "block", -xx, xx, -yy, yy, -zz, zz) self.Lmp.create_box(1, "IGMBOX", "bond/types", bond_types, "extra/bond/per/atom", bond_per_atom) def setupParticles(self): """ initialize particles with random position """ #add user define per-atom property: radius(double) self.Lmp.fix("UserProperty","all","property/atom","d_radius") #number of particles self.nbead = len(self.model.particles) self.atom_style_index = 1 self.Lmp.create_atoms(1, "random", self.nbead, self.randseed, "IGMBOX") #set particle mass 1.0 self.Lmp.mass('*', 1.0) #group particle NORMAL self.lmp_group_NORMAL = "NORMAL" self.Lmp.group(self.lmp_group_NORMAL, "type", 1) #get numpy view of per atom array self.particle_id = self.Lmp.lmp.numpy.extract_atom_iarray('id', n, 1) self._coordinates = self.Lmp.lmp.numpy.extract_atom_darray('x', n, 3) self._radii = self.lmp.lmp.numpy.extract_atom_darray('d_radius', n, 1) def indexMapping(self): """ particle mapping from lammps index to original index """ return np.argsort( self.particle_id[:, 0] ) def setCoordinates(self, crd = None): """ assign xyz values to lammps """ if crd: self.coordinates[self.indexMapping(), :] = crd[:] else: self.coordinates[self.indexMapping(), :] = self.model.particles.coordinates[:] def setRadii(self, radii = None): """ assign radius values to lammps """ if radii: self.radii[self.indexMapping(), :] = radii[:] else: self.radii[self.indexMapping(), :] = self.model.particles.radii[:] self.maxrad = max(self.radii) def setupNeighbor(self): """ setup neighbor list rules """ if hasattr(self, "maxrad"): self.Lmp.neighbor(self.maxrad, 'bin') else: raise RuntimeError("Radii not set before setupNeighbor()") max_neighbor = int(self.cfg["optimization/kernel_opts/lammps/max_neigh"]) self.Lmp.neigh_modify('every',1,'check','yes') self.Lmp.neigh_modify("one", max_neighbor, 'page', 20*max_neighbor) def addRestraints(self): """ add restraints to lammps one by one """ #lammps bond style definition bond_styles = set() n = 1 for Res in self.model.restraints: if hasattr(Res, "bond_style"): bond_styles.add(Res.bond_style) #give_bond_id Res.setBondId(n) n += 1 if len(bond_styles) == 1: self.Lmp.bond_style(bond_styles.pop()) elif len(bond_styles) > 1: cmd = ["bond_style", "hybrid"] while bond_styles: cmd.append(bond_styles.pop()) self.Lmp.command(" ".join(cmd)) #define variable for fast communication/avoid input string parsing self.Lmp.variable("batoms","string","EMPTY") bond_variable = "batoms" #loop all restraints and apply lammps code for Res in self.model.restraints: Res.Lammps(self.Lmp, runid = self.runid, tmp_dir = self.tmp_dir, randseed = self.randseed, normal_group = self.lmp_group_NORMAL, bond_variable = bond_variable)