def test_melt(self): """ 3d Lennard-Jones melt """ L = PyLammps() L.units('lj') L.atom_style('atomic') L.lattice('fcc', 0.8442) # NOTE: lattice command is different in LJ units # 0.8442 is density fraction L.region('r1 block', 0, 10, 0, 10, 0, 10) L.create_box(1, 'r1') # fill box with atoms according to lattice positions L.create_atoms(1, 'box') L.mass(1, 1.0) L.velocity('all create', 3.0, 87287, 'mom no') L.timestep(0.002) L.pair_style('lj/cut', 2.5) L.pair_coeff(1, 1, 1.0, 1.0, 2.5) L.fix('f1 all nve') L.dump('d1 all image 500 snap-01.*.jpg type type') L.thermo(50) L.run(500) self.assertTrue(os.path.exists('snap-01.0.jpg')) self.assertTrue(os.path.exists('snap-01.500.jpg')) os.remove('snap-01.0.jpg') os.remove('snap-01.500.jpg')
1) # remove linear momentum for better displacement detection #L.fix("thermofix3","clustergr","langevin",Temp,Temp,0.05, 2614,"zero","yes") #L.fix("thermofix3","clustergr","temp/csvr",Temp,Temp,0.1,12345) nodispl = 0 nosputt = 0 displacement = False # save starting coordinates: (startnPart1, startnPart2, nPartsub, startcoords1, startcoords2, coordssub, startindices1, startindices2, indicessub) = getcoords() startcoords = np.append(startcoords1, startcoords2, axis=0) # Output starting geometry: L.dump("Startgeom", "all", "custom", 1, "Startgeom_Full" + ".xyz", "id", "type", "x", "y", "z", "vx", "vy", "vz", "c_K", "c_P", "c_coordno") L.dump_modify("Startgeom", "first", "yes", "pad", 10) # Schreib' beim nullten Schritt ein File raus L.dump_modify("Startgeom", "element", "Au", "Ni", "C") L.dump_modify("Startgeom", "pbc", "yes") # remap atoms via periodic boundary conditions L.run(0) L.undump("Startgeom") L.dump("Startgeom", "all", "xyz", 1, "Startgeom" + ".xyz") L.dump_modify("Startgeom", "first", "yes", "pad", 10) # Schreib' beim nullten Schritt ein File raus L.dump_modify("Startgeom", "element", "Au", "Ni", "C") L.dump_modify("Startgeom", "pbc", "yes") # remap atoms via periodic boundary conditions L.run(0)
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) lmp.run(10000)
py_lmp.variable(micelle_group, 'atom', '"type == {:d}"'.format(sol_tip_bead_type)) py_lmp.group(micelle_group, 'dynamic', simulation.rods_group, 'var', micelle_group, 'every', out_freq) micelle_compute = "micelle_ID" if hasattr(run_args, 'micelle_cutoff'): micelle_cutoff = run_args.micelle_cutoff else: SS_tip_int_key = model.eps[(sol_tip_bead_type, sol_tip_bead_type)][1] SS_tip_int_range = model.int_types[SS_tip_int_key][1] micelle_cutoff = 2 * model.rod_radius + SS_tip_int_range py_lmp.compute(micelle_compute, micelle_group, 'aggregate/atom', micelle_cutoff) # OUTPUT dump_elems = "id x y z type mol" try: dump_elems += " c_" + micelle_compute except: pass py_lmp.dump("dump_cmd", "all", "custom", out_freq, '"' + dump_path + '"', dump_elems) py_lmp.dump_modify("dump_cmd", "sort id") py_lmp.thermo_style("custom", "step atoms", "pe temp") py_lmp.thermo(out_freq) # RUN ... py_lmp.run(args.simlen) MPI.Finalize()
0, concentration, run_args.mc_every, mc_tries, opt=['region', 'gcmc_box', 'tfac_insert', 1.65]) # OUTPUT dump_elems = "id x y z type mol" try: dump_elems += " c_" + micelle_compute except: pass try: dump_elems += " c_" + adsorbed_compute except: pass py_lmp.dump("dump_cmd", "all", "custom", out_freq, dump_path, dump_elems) py_lmp.dump_modify("dump_cmd", "sort id") py_lmp.variable("area", "equal", "lx*ly") py_lmp.thermo_style("custom", "step atoms", "pe temp", "press lx ly v_area") py_lmp.thermo(out_freq) # MSD data (only in x & y directions) py_lmp.variable("top_msd", "equal", '"c_{0}[1] + c_{0}[2]"'.format(top_msd_compute)) py_lmp.fix("mem_top_msd", "all", "ave/time", 1, 1, out_freq, "v_top_msd", "file", os.path.join(output_folder, sim_ID + '_mem_top.msd')) py_lmp.variable("bottom_msd", "equal", '"c_{0}[1] + c_{0}[2]"'.format(bottom_msd_compute)) py_lmp.fix("mem_bottom_msd", "all", "ave/time", 1, 1, out_freq, "v_bottom_msd", "file", os.path.join(output_folder, sim_ID + '_mem_bottom.msd')) py_lmp.variable("full_msd", "equal", '"c_{0}[1] + c_{0}[2]"'.format(full_msd_compute))
class LammpsCalc(object): def __init__(self, initfilepath, potfilepath, poscar): # self.lmp = lammps() self.initpath = initfilepath self.potentialpath = potfilepath self.poscar = poscar # self.scel = tokens[0] # self.id = tokens[1] self.pylmp = PyLammps() self.configurename = None # self.clex = clex self.labels_dict = {} self.num_types_cell = None self.atoms_num = {} self.total_atoms = 0 self.atoms = None self.atom_types = None self.cart_coors = None self.lattice = None self.basis_vec = None self.basis_sites = None self.cos_alpha = None self.cos_beta = None self.cos_gamma = None self.xlo = None self.xhi = None self.ylo = None self.yhi = None self.zlo = None self.zhi = None self.xy = None self.yz = None self.xz = None self.properties = {} self.json_props = None self.relaxed_energy = None # def load_files(self): # self.lmp.file("init.mod") # self.lmp.file("potential.mod") def read_prim(self): prim_path = "./prim.json" prim_file = open(prim_path).read() prim = json.JSONDecoder().decode(prim_file) print("prim:\n\n\n\n", prim) if prim['coordinate_mode'] == "Fractional": self.basis_vec = np.array(prim['lattice_vectors']) self.basis_sites = [] # for basis in prim[''] def read_structure(self): # Dir is defined at CASMcode/python/casm/casm/project/project.py # dir.__setattr__("POS", open(self.configurename + "/POS")) # pos_file = dir.POS(self.configname) pos_file = open(self.poscar) # assert \ self.configurename == pos_file.readline().strip() a0 = float(pos_file.readline().strip()) print(a0) a1str = pos_file.readline().strip() a1 = a1str.split() a2str = pos_file.readline().strip() a2 = a2str.split() a3str = pos_file.readline().strip() a3 = a3str.split() self.lattice = np.array([a1, a2, a3], float) * a0 print("lattice") print(self.lattice) labels = pos_file.readline().strip().split() nums = pos_file.readline().strip().split() for i in range(len(labels)): self.atoms_num[labels[i]] = nums[i] self.total_atoms = np.sum(np.array(nums, int)) self.num_types_cell = len(labels) self.atoms = np.zeros((self.total_atoms, 3)) self.atom_types = np.zeros(self.total_atoms) assert 'Direct' == pos_file.readline().strip() i = 0 line = pos_file.readline() while line: line = line.strip() tokens = line.split() line = pos_file.readline() if len(tokens) <= 0: continue frac_coor = np.array(tokens[:3], float) # cart_coor = frac_coor @ lattice.T # convert fractional coordinates to cartesian # print(self.lmp.atoms) # self.lmp.atoms[i].position = tuple(cart_coor.reshape(1, -1)[0]) self.atoms[i] = frac_coor # self.lmp.atoms[i].type = int(labels_dict[tokens[3].strip()]) self.atom_types[i] = int(self.labels_dict[tokens[3].strip()]) # print(atoms[i], atom_types[i]) i += 1 def create_structure(self): # Might not need this function anymore self.read_structure() # self.xlo = round(np.amin(cart_coors[:, 0]), 8) # # self.xhi = round(np.amax(cart_coors[:, 0]), 8) # self.ylo = round(np.amin(cart_coors[:, 1]), 8) # # self.yhi = round(np.amax(cart_coors[:, 1]), 8) # self.zlo = round(np.amin(cart_coors[:, 2]), 8) # # self.zhi = round(np.amax(cart_coors[:, 2]), 8) self.create_region() self.read_prim() self.create_atoms() def create_atoms(self): self.cart_coors = self.atoms @ self.lattice print('\nAtoms:\n', self.cart_coors[:10], "\n", self.atom_types[:10]) print("test", self.cart_coors[0][0]) print("atom types", self.atom_types) print("coors: ") print(self.cart_coors) print(self.pylmp.atoms.natoms) # Could optionally use lammps_create_atoms(void *, int, tagint *, int *, double *, double *, # imageint *, int) # But would still need to iterate through list and this method aides debugging. for i in range(self.cart_coors.shape[0]): # maybe should iterate through possible rounding values ### rounding must be consistent across all structure values print( 'create_atoms ' + str(int(self.atom_types[i])) + ' single ' + str(round(self.cart_coors[i][0], 16)) + ' ' + str(round(self.cart_coors[i][1], 16)) + ' ' + str(round(self.cart_coors[i][2], 16))) # self.pylmp.command( # 'create_atoms ' + str(int(self.atom_types[i])) + ' single ' + str(round(self.cart_coors[i][0], 16)) + ' ' # + str(round(self.cart_coors[i][1], 16)) + ' ' + str(round(self.cart_coors[i][2], 16))) self.pylmp.command( 'create_atoms ' + str(int(self.atom_types[i])) + ' single ' + str( self.cart_coors[i][0]) + ' ' + str(self.cart_coors[i][1]) + ' ' + str(self.cart_coors[i][2])) print(self.cart_coors.shape[0]) print(self.pylmp.atoms.natoms) def get_labels(self): raise NotImplementedError() def run_calc(self): raise NotImplementedError() def create_properties(self): self.properties['atom_type'] = list(self.labels_dict.keys()) self.properties['atoms_per_type'] = [self.atoms_num[atom] for atom in self.atoms_num.keys()] # enforces same order as atom_type self.properties['coord_mode'] = 'cartesian' self.properties['is_complete'] = True if self.relaxed_energy is not None else False relaxed_basis = [] relaxed_forces = [] for i in range(self.total_atoms): relaxed_basis.append(list(self.pylmp.atoms[i].position)) relaxed_forces.append(list(self.pylmp.atoms[i].force)) self.properties['relaxed_basis'] = relaxed_basis self.properties['relaxed_energy'] = self.relaxed_energy self.properties['relaxed_forces'] = relaxed_forces self.properties['relaxed_lattice'] = [[self.pylmp.system.xhi-self.pylmp.system.xlo, 0, 0], [self.pylmp.eval('xy'), self.pylmp.system.yhi-self.pylmp.system.ylo, 0], [self.pylmp.eval('xz'), self.pylmp.eval('yz'), self.pylmp.system.zhi-self.pylmp.system.zlo]] j_encoder = json.JSONEncoder() self.json_props = j_encoder.encode(self.properties) return self.properties # self.properties['relaxed_lattice'] = self.lattice # def report_results(self): # # try: # os.mkdir(self.configurename + '/calctype.lammps') # except: # pass # # outputfile = self.configurename + '/calctype.lammps/properties.calc.json' # # with open(outputfile, 'wb') as file: # # cls=noindent.NoIndentEncoder, # file.write(six.u(json.dumps(self.properties, indent=4, sort_keys=True)).encode('utf-8')) # print("Wrote " + outputfile) # sys.stdout.flush() # self.report_status() # def report_status(self): # output = {'status': 'complete'} # outputfile = self.configurename + '/calctype.lammps/status.json' # # with open(outputfile, 'wb') as file: # # cls=noindent.NoIndentEncoder, # file.write(six.u(json.dumps(output, indent=4, sort_keys=True)).encode('utf-8')) # print("Wrote " + outputfile) # sys.stdout.flush() def create_region(self): # In the future will have to read prim and take max or min vector for each dimension # Need to add space for next position b/c otherwise periodicity will make atoms at border overlap # basis_vec = np.array([0.666666666667, 0.333333333334, 0.500000000000]) # lat_vec = np.array([[3.184000000000, 0.000000000000, 0.000000000000], # [-1.592000000000, 2.757424885650, 0.000000000000], # [0.000000000000, 0.000000000000, 5.249000000000]]) # dist = basis_vec @ lat_vec # self.xhi += dist[0] # self.yhi += dist[1] # self.zhi += dist[2] # a = 3.2094 # self.lmp.lattice("hcp ", a) # self.xy = self.lattice[1, 0] # self.yz = self.lattice[2, 1] # self.xz = self.lattice[2, 0] self.xlo = 0 self.ylo = 0 self.zlo = 0 self.cos_alpha = self.cos_angle(self.lattice[1], self.lattice[2]) self.cos_beta = self.cos_angle(self.lattice[0], self.lattice[2]) self.cos_gamma = self.cos_angle(self.lattice[0], self.lattice[1]) self.xy = self.norm(self.lattice[1]) * self.cos_gamma self.xz = self.norm(self.lattice[2]) * self.cos_beta self.xhi = self.norm(self.lattice[0]) self.yhi = np.sqrt(self.norm(self.lattice[1])**2 - self.xy**2) self.yz = ((self.norm(self.lattice[1]) * self.norm(self.lattice[2]) * self.cos_alpha) - (self.xy * self.xz)) / self.yhi self.zhi = np.sqrt((self.norm(self.lattice[2])**2) - (self.xz**2) - (self.yz**2)) # self.xhi = self.lattice[0, 0] - self.lattice[1, 0] - self.lattice[2, 0] # self.yhi = self.lattice[1, 1] - self.lattice[0, 1] - self.lattice[2, 1] # self.zhi = self.lattice[2, 2] - self.lattice[0, 2] - self.lattice[1, 2] self.pylmp.atom_style('atomic') print('prism ', self.xlo, ' ', self.xhi, ' ', self.ylo, ' ', self.yhi, ' ', self.zlo, ' ', self.zhi, ' ', self.xy, ' ', self.xz, ' ', self.yz) self.pylmp.region('box prism ', self.xlo, ' ', self.xhi, ' ', self.ylo, ' ', self.yhi, ' ', self.zlo, ' ', self.zhi, ' ', self.xy, ' ', self.xz, ' ', self.yz) self.pylmp.command("boundary p p p") # self.lmp.region("box block", self.xlo, self.xhi, self.ylo, self.yhi, self.zlo, self.zhi) # self.lmp.command("create_box {0:d} box".format(num_types)) self.pylmp.command("create_box {0:d} box".format(len(self.labels_dict.keys()))) print("bounding box", self.pylmp.system.xlo, self.pylmp.system.xhi, self.pylmp.system.ylo, self.pylmp.system.yhi, self.pylmp.system.zlo, self.pylmp.system.zhi) # self.lmp.create_atoms("1 box") for i in self.labels_dict.values(): self.pylmp.command("mass {0:d} 1.0e-20".format(i)) # self.pylmp.command("mass 2 1.0e-20") def norm(self, vec): return np.sqrt(np.sum(vec**2)) def dot_prod(self, vec1, vec2): return np.sum(vec1 * vec2) def cos_angle(self, vec1, vec2): cos_theta = self.dot_prod(vec1, vec2) / (self.norm(vec1) * self.norm(vec2)) assert cos_theta >= -1 and cos_theta <= 1 if cos_theta < -1: cos_theta = -1 elif cos_theta > 1: cos_theta = 1 return cos_theta def visualize(self): try: print("creating", "./dumpfiles/" + self.scel) os.mkdir("./dumpfiles/" + self.scel) except: pass try: print("creating", "./dumpfiles/" + self.configurename) os.mkdir("./dumpfiles/" + self.configurename) except: pass self.pylmp.dump("dump1", "all", "atom", 1, "./dumpfiles/" + self.configurename + "/dump.atom") # , "zoom 2.0"