Exemplo n.º 1
0
def write_config(sim, fields=None, precision=None):
    """
    Write configurations to a trajectory file.

    The trajectory format is taken from the passed Simulation
    instance.
    """
    # Initialize
    # This will clear the variable in a new run
    if sim.current_step == 0 or not hasattr(sim, '__init_write_config'):
        sim.__init_write_config = False
    if sim.restart:
        sim.__init_write_config = True

    # Header
    if not sim.__init_write_config:
        sim.__init_write_config = True
        # TODO: folder-based trajectories should ensure that mode='w' clears up the folder
        rmd(sim.output_path)
        rmf(sim.output_path)

    with sim.trajectory_class(sim.output_path, 'a') as t:
        if precision is not None:
            t.precision = precision
        if fields is not None:
            t.fields = fields
        t.write(sim.system, sim.current_step)
Exemplo n.º 2
0
    def run(self, steps=None):
        if steps is not None:
            self.steps = steps

        dirout = tempfile.mkdtemp()
        file_tmp = os.path.join(dirout, 'lammps.atom')
        file_inp = os.path.join(dirout, 'lammps.atom.inp')

        # Update lammps startup file using self.system
        # This will write the .inp startup file
        with TrajectoryLAMMPS(file_tmp, 'w') as th:
            th.write(self.system, 0)

        # Do things in lammps order: units, read, commands, run. A
        # better approach would be to parse commands and place
        # read_data after units then pack commands again. Even better
        # using PyLammps...
        cmd = """\
units		lj
atom_style	atomic
read_data {file_inp}
{commands}
min_style {method}
minimize 0.0 {tolerance} {steps} {max_evaluations}
write_dump all custom {file_tmp} id type x y z modify sort id format line "%d %d %.15g %.15g %.15g"
""".format(file_tmp=file_tmp,
           file_inp=file_inp,
           commands=self.commands,
           method=self.method,
           tolerance=self.ftol,
           steps=min(100000000, self.steps),
           max_evaluations=min(100000000, self.max_evaluations))

        stdout = _run_lammps_command(cmd)

        if self.verbose:
            print(stdout)

        # Check if we have reached the number of maximum number of steps
        if 'Stopping criterion = max iterations' in stdout:
            self.reached_steps = True
        else:
            self.reached_steps = False

        # Update internal reference to self.system
        with TrajectoryLAMMPS(file_tmp) as th:
            new_system = th[-1]
        for i in range(len(self.system.particle)):
            self.system.particle[i] = new_system.particle[i]

        # Clean up
        rmd(dirout)
Exemplo n.º 3
0
    def setUp(self):
        self.dirname = '/tmp/test_folder'
        rmd(self.dirname)
        mkdir(self.dirname)
        for i in range(10, 13):
            fname = os.path.join(self.dirname, 'step_%d' % i)
            with open(fname, 'w') as fh:
                fh.write("""\
2
step:%d
A 1.0 -1.0 0.0
A 2.9 -2.9 0.0
""" % i)
Exemplo n.º 4
0
    def compute(self, observable, particle, cell):
        # We use self.potential as lammps commands
        dirout = tempfile.mkdtemp()
        file_tmp = os.path.join(dirout, 'lammps.atom')
        file_inp = os.path.join(dirout, 'lammps.atom.inp')
        # Update lammps startup file using self.system
        # This will write the .inp startup file
        with TrajectoryLAMMPS(file_tmp, 'w') as th:
            th.write(system.System(particle, cell), 0)

        # Do things in lammps order: units, read, commands, run. A
        # better approach would be to parse commands and place
        # read_data after units then pack commands again. Even better
        # using PyLammps...
        cmd = """\
units		lj
atom_style	atomic
read_data {}
{}
run 0
write_dump all custom {} fx fy fz modify format line "%.15g %.15g %.15g"
""".format(file_inp, self.potential, file_tmp)

        stdout = _run_lammps_command(cmd)

        found = False
        for line in stdout.split('\n'):
            if 'Step' in line:
                found = True
            elif found:
                if 'MPI' in line:
                    continue
                _, T, U, _, _, P = [float(x) for x in line.split()]
                rho = len(particle) / cell.volume
                ndim = len(cell.side)
                self.energy = U * len(particle)
                self.virial = (P - rho * T) * ndim * cell.volume
                break

        with TrajectoryLAMMPS(file_tmp) as th:
            new_system = th[-1]
            self.forces = new_system.interaction.forces

        # Clean up
        rmd(dirout)
Exemplo n.º 5
0
def _run_lammps_command(cmd):
    """Run a lammps script from the command line"""
    dirout = tempfile.mkdtemp()
    file_tmp = os.path.join(dirout, 'cmd.lammps')
    with open(file_tmp, 'w') as fh:
        fh.write(cmd)
    opt = '-n {}'.format(lammps_mpi_tasks) if lammps_mpi else ''
    shell_cmd = '{} {} {} -in {}'.format(lammps_mpi, opt, lammps_command,
                                         file_tmp)
    try:
        stdout = subprocess.check_output(shell_cmd,
                                         executable='/bin/bash',
                                         stderr=subprocess.STDOUT,
                                         shell=True)
    except subprocess.CalledProcessError as exception:
        print(''.join(exception.output))
        raise

    # Clean up
    rmd(dirout)
    return stdout.decode()
Exemplo n.º 6
0
 def tearDown(self):
     rmf(self.inpfile)
     rmd(self.inpdir)
Exemplo n.º 7
0
 def test_rmd(self):
     utils.rmd(self.dirbase)
Exemplo n.º 8
0
 def tearDown(self):
     rmd('/tmp/test_simulation')
Exemplo n.º 9
0
 def tearDown(self):
     from atooms.core.utils import rmd
     rmd('/tmp/test_xyz')
Exemplo n.º 10
0
 def close(self):
     if self.archive:
         rmd(self.dirname)
Exemplo n.º 11
0
 def tearDown(self):
     from atooms.core.utils import rmf, rmd
     rmf('/tmp/test_backends*')
     rmd('/tmp/test_backends')
Exemplo n.º 12
0
    def run(self, steps):
        dirout = tempfile.mkdtemp()
        file_tmp = os.path.join(dirout, 'lammps.atom')
        file_inp = os.path.join(dirout, 'lammps.atom.inp')
        if self.restart:
            file_res = os.path.join(self.tmpdir, 'lammps.restart')
        # Update lammps startup file using self.system
        # This will write the .inp startup file
        with TrajectoryLAMMPS(file_tmp, 'w') as th:
            th.write(self.system, 0)

        # Set fixes from the system if we find thermostat / barostat
        if self.system.thermostat is not None and self.system.barostat is not None:
            # NPT ensemble
            fix = 'fix 1 all npt temp {0.temperature} {0.temperature} {0.relaxation_time} iso {1.pressure} {1.pressure} {1.relaxation_time}'.format(
                self.system.thermostat, self.system.barostat)
        elif self.system.thermostat is not None:
            # NVT ensemble
            fix = 'fix 1 all nvt temp {0.temperature} {0.temperature} {0.relaxation_time}'.format(
                self.system.thermostat)
        elif not 'fix' in self.commands:
            # NVE ensemble
            fix = 'fix 1 all nve'
        else:
            # The integrator is already contained in the commands
            fix = ''

        # Do things in lammps order: units, read, commands, run. A
        # better approach would be to parse commands and place
        # read_data after units then pack commands again. Even better
        # using PyLammps...
        cmd = """\
units		lj
atom_style	atomic
"""
        # Read restart file if it exists
        if self.restart and os.path.exists(file_res):
            cmd += """
read_restart {}
""".format(file_res)
        else:
            cmd += """
read_data {}
""".format(file_inp)

        # Rest of commands
        cmd += """
{commands}
{fix}
run {steps}
write_dump all custom {file_tmp} id type x y z vx vy vz modify sort id format line "%d %d %.15g %.15g %.15g %.15g %.15g %.15g"
""".format(commands=self.commands, fix=fix, steps=steps, file_tmp=file_tmp)

        if self.restart:
            cmd += """
write_restart {}
""".format(file_res)

        # Execute LAMMPS command
        stdout = _run_lammps_command(cmd)

        if self.verbose:
            print(stdout)

        # Update internal reference to self.system
        # Note that the thermostat and barostat are not touched
        with TrajectoryLAMMPS(file_tmp) as th:
            new_system = th[-1]
            for i in range(len(self.system.particle)):
                self.system.particle[i] = new_system.particle[i]

        # Clean up
        rmd(dirout)
Exemplo n.º 13
0
 def tearDown(self):
     rmd('/tmp/test_lammps.d')
     rmf('/tmp/test_lammps*')