def run(self, np=None, nanohub=None, save_input=True, prefix='mpiexec'): """pysimm.lmps.Simulation.run Begin LAMMPS simulation. Args: np: number of threads to use (serial by default) default=None nanohub: dictionary containing nanohub resource information default=None init: True to write initialization part of LAMMPS input script (set to False if using complete custom input) save_input: True to save input as pysimm.sim.in prefix: prefix for running LAMMPS (i.e. - mpiexec) """ if isinstance(save_input, str): with open(save_input, 'w') as f: f.write(self.input) elif save_input is True: with open('pysimm.sim.in', 'w') as f: f.write(self.input) try: call_lammps(self, np, nanohub, prefix=prefix) except OSError: raise PysimmError( 'There was a problem calling LAMMPS with {}'.format(prefix)) except IOError: if check_lmps_exec(): raise PysimmError( 'There was a problem running LAMMPS. The process started but did not finish successfully. Check the log file, or rerun the simulation with debug=True to debug issue from LAMMPS output' ) else: raise PysimmError( 'There was a problem running LAMMPS. LAMMPS is not configured properly. Make sure the LAMMPS_EXEC environment variable is set to the correct LAMMPS executable path. The current path is set to:\n\n{}' .format(LAMMPS_EXEC))
def __init__(self, fname): if not pd: raise PysimmError('pysimm.lmps.LogFile function requires pandas') self.filename = fname self.data = pd.DataFrame() self._read(self.filename)
def write(self, sim=None): """pysimm.lmps.Init.write Prepare LAMMPS input with initialization settings Args: sim: :class:`~pysimm.lmps.Simulation` object reference Returns: string of LAMMPS input """ if sim: s = sim.system else: s = None if self.forcefield is None and s and s.forcefield is not None: if s.forcefield in ['gaff', 'gaff2']: self.forcefield = 'amber' elif s.forcefield in ['cgenff']: self.forcefield = 'charmm' else: self.forcefield = s.forcefield elif self.forcefield is None and sim and sim.forcefield is not None: self.forcefield = sim.forcefield if self.special_bonds is None and self.forcefield is not None: self.special_bonds = FF_SETTINGS[self.forcefield]['special_bonds'] if self.forcefield is not None: pair_modify = FF_SETTINGS[self.forcefield]['pair_modify'] if self.pair_modify: pair_modify.update(self.pair_modify) self.pair_modify = pair_modify if self.charge is None and s is not None: for p in s.particles: if p.charge: self.charge = True break if self.charge is None: self.charge = False lammps_input = '' lammps_input += '\n' + '#' * 80 + '\n' lammps_input += '#' * 34 + ' Init ' + '#' * 34 + '\n' lammps_input += '#' * 80 + '\n' lammps_input += '{:<15} {}\n'.format('units', self.units) lammps_input += '{:<15} {}\n'.format('atom_style', self.atom_style) if self.create_box and self.create_box.region and type( self.create_box.region) is Region: lammps_input += self.create_box.region.write(None) lammps_input += self.create_box.write(None) if self.pair_style: lammps_input += '{:<15} {}'.format('pair_style', self.pair_style) elif self.forcefield: self.pair_style = FF_SETTINGS[self.forcefield]['pair_style'] lammps_input += '{:<15} {}'.format('pair_style', self.pair_style) if self.charge: lammps_input += '/coul/long' self.pair_style += '/coul/long' else: raise PysimmError( 'A pair_style must be defined during initialization') if self.cutoff: if (self.forcefield == 'charmm') and self.cutoff.get('inner_lj'): lammps_input += ' {} '.format(self.cutoff['inner_lj']) lammps_input += ' {} '.format(self.cutoff['lj']) if self.charge and self.cutoff.get('coul'): lammps_input += ' {} '.format(self.cutoff['coul']) lammps_input += '\n' if self.charge: lammps_input += '{:<15} {}\n'.format('kspace_style', self.kspace_style) if self.bond_style is None and s and s.bonds.count > 0: if self.forcefield: self.bond_style = FF_SETTINGS[self.forcefield]['bond_style'] if self.bond_style: lammps_input += '{:<15} {}\n'.format('bond_style', self.bond_style) if self.angle_style is None and s and s.angles.count > 0: if self.forcefield: self.angle_style = FF_SETTINGS[self.forcefield]['angle_style'] if self.angle_style: lammps_input += '{:<15} {}\n'.format('angle_style', self.angle_style) if self.dihedral_style is None and s and s.dihedrals.count > 0: if self.forcefield: self.dihedral_style = FF_SETTINGS[ self.forcefield]['dihedral_style'] if self.dihedral_style: lammps_input += '{:<15} {}\n'.format('dihedral_style', self.dihedral_style) if self.improper_style is None and s and s.impropers.count > 0: if self.forcefield: self.improper_style = FF_SETTINGS[ self.forcefield]['improper_style'] if self.improper_style: lammps_input += '{:<15} {}\n'.format('improper_style', self.improper_style) if self.special_bonds: lammps_input += '{:<15} {}\n'.format('special_bonds', self.special_bonds) if self.pair_modify: lammps_input += '{:<15} '.format('pair_modify') for k, v in self.pair_modify.items(): lammps_input += '{} {} '.format(k, v) lammps_input += '\n' if self.read_data: lammps_input += '{:<15} {}\n'.format('read_data', self.read_data) elif s: s.write_lammps('temp.lmps') lammps_input += '{:<15} temp.lmps\n'.format('read_data') if self.pair_style and self.pair_style.startswith('buck'): for pt1 in s.particle_types: for pt2 in s.particle_types: if pt1.tag <= pt2.tag: a = pow(pt1.a * pt2.a, 0.5) c = pow(pt1.c * pt2.c, 0.5) rho = 0.5 * (pt1.rho + pt2.rho) lammps_input += '{:<15} {} {} {} {} {}\n'.format( 'pair_coeff', pt1.tag, pt2.tag, a, rho, c) lammps_input += '#' * 80 + '\n\n' return lammps_input
def call_lammps(simulation, np, nanohub, prefix='mpiexec'): """pysimm.lmps.call_lammps Wrapper to call LAMMPS using executable name defined in pysimm.lmps module. Args: simulation: :class:`~pysimm.lmps.Simulation` object reference np: number of threads to use nanohub: dictionary containing nanohub resource information default=None prefix: prefix for running LAMMPS (i.e. - mpiexec) Returns: None """ log_name = simulation.log or 'log.lammps' if nanohub: with open('temp.in', 'w') as f: f.write(simulation.input) if simulation.name: print('%s: sending %s simulation to computer cluster at nanoHUB' % (strftime('%H:%M:%S'), simulation.name)) else: print('%s: sending simulation to computer cluster at nanoHUB' % strftime('%H:%M:%S')) sys.stdout.flush() cmd = ('submit -n %s -w %s -i temp.lmps -i temp.in ' 'lammps-09Dec14-parallel -e both -l none -i temp.in' % (nanohub.get('cores'), nanohub.get('walltime'))) cmd = shlex.split(cmd) exit_status, stdo, stde = RapptureExec(cmd) else: if simulation.name: print('%s: starting %s LAMMPS simulation' % (strftime('%H:%M:%S'), simulation.name)) else: print('%s: starting LAMMPS simulation' % strftime('%H:%M:%S')) if np: p = Popen([prefix, '-np', str(np), LAMMPS_EXEC, '-e', 'both'], stdin=PIPE, stdout=PIPE, stderr=PIPE) else: p = Popen([LAMMPS_EXEC, '-e', 'both'], stdin=PIPE, stdout=PIPE, stderr=PIPE) simulation.write_input() if simulation.debug: print(simulation.input) warning_print( 'debug setting involves streaming output from LAMMPS process and can degrade performance' ) warning_print( 'only use debug for debugging purposes, use print_to_screen to collect stdout after process finishes' ) p.stdin.write(simulation.input.encode('utf-8')) q = Queue() t = Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True t.start() while t.is_alive() or not q.empty(): try: line = q.get_nowait() except Empty: pass else: if simulation.debug: sys.stdout.write(line.decode('utf-8')) sys.stdout.flush() else: stdo, stde = p.communicate(simulation.input.encode('utf-8')) if simulation.print_to_screen: print(stdo.decode('utf-8')) print(stde.decode('utf-8')) simulation.system.read_lammps_dump('pysimm.dump.tmp') try: os.remove('temp.lmps') except OSError as e: print(str(e)) if os.path.isfile('pysimm.qeq.tmp'): os.remove('pysimm.qeq.tmp') try: os.remove('pysimm.dump.tmp') if simulation.name: print('%s: %s simulation using LAMMPS successful' % (strftime('%H:%M:%S'), simulation.name)) else: print('%s: simulation using LAMMPS successful' % (strftime('%H:%M:%S'))) except OSError as e: if simulation.name: raise PysimmError('%s simulation using LAMMPS UNsuccessful' % simulation.name) else: raise PysimmError('simulation using LAMMPS UNsuccessful')