Beispiel #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)
Beispiel #2
0
 def tearDown(self):
     rmf(self.inpfile)
     rmd(self.inpdir)
 def tearDown(self):
     rmf('/tmp/test_potential.h5')
Beispiel #4
0
 def test_rm_glob(self):
     utils.rmf(self.dirout + '/*')
     self.assertFalse(os.path.exists(self.dirout + '/1'))
     self.assertFalse(os.path.exists(self.dirout + '/2'))
     self.assertTrue(os.path.exists(self.dirout + '/3'))
Beispiel #5
0
 def test_rm(self):
     utils.rmf([self.dirout + '/1', self.dirout + '/2'])
     self.assertFalse(os.path.exists(self.dirout + '/1'))
     self.assertFalse(os.path.exists(self.dirout + '/2'))
     self.assertTrue(os.path.exists(self.dirout + '/3'))
Beispiel #6
0
def show_ovito(particle,
               cell,
               outfile=None,
               radius=0.35,
               viewport=None,
               callback=None,
               tmpdir=None,
               camera_dir=(0, 1, 0),
               camera_pos=(0, -10, 0),
               size=(640, 480),
               zoom=True,
               perspective=False):
    """
    Render particle in cell using ovito
    """
    import os
    try:
        from ovito.io import import_file
    except ImportError:
        _log.warning('install ovito to display the particles')
        return
    from ovito.vis import Viewport, TachyonRenderer
    from ovito.vis import ParticlesVis
    import tempfile
    from atooms.core.utils import mkdir

    # Make sure dirname exists
    if outfile is not None:
        mkdir(os.path.dirname(outfile))

    # Get a temporary file to write the sample
    fh = tempfile.NamedTemporaryFile('w',
                                     dir=tmpdir,
                                     suffix='.xyz',
                                     delete=False)
    tmp_file = fh.name

    # Self-contained EXYZ dump (it is not clean to use trajectories here)
    fh.write('{}\n'.format(len(particle)))
    fh.write(
        'Properties=species:S:1:pos:R:3 Lattice="{},0.,0.,0.,{},0.,0.,0.,{}"\n'
        .format(*cell.side))
    for p in particle:
        fh.write('{} {} {} {}\n'.format(p.species, *p.position))
    fh.close()

    # Ovito stuff. Can be customized by client code.
    pipeline = import_file(tmp_file)
    # Ovito seems to ignore the lattice info of exyz file
    # so we forcibly set the cell info here
    pipeline.source.data.cell_[0, 0] = cell.side[0]
    pipeline.source.data.cell_[1, 1] = cell.side[1]
    pipeline.source.data.cell_[2, 2] = cell.side[2]
    pipeline.source.data.cell_[:, 3] = -cell.side / 2
    # Scale radius by default
    vis_element = pipeline.source.data.particles.vis
    vis_element.radius = radius
    # Apply client code callback
    if callback:
        callback(pipeline)
    pipeline.add_to_scene()

    # Define viewport
    if viewport:
        vp = vieport
    else:
        if perspective:
            vp = Viewport(type=Viewport.Type.Perspective,
                          camera_dir=camera_dir,
                          camera_pos=camera_pos)
        else:
            vp = Viewport(type=Viewport.Type.Ortho,
                          camera_dir=camera_dir,
                          camera_pos=camera_pos)

    # Render
    if zoom:
        vp.zoom_all()
    if outfile is None:
        outfile = tmp_file + '.png'

    vp.render_image(filename=outfile, size=size, renderer=TachyonRenderer())

    # Scene is a singleton, so we must clear it
    pipeline.remove_from_scene()

    from atooms.core.utils import rmf
    rmf(tmp_file)

    # Try to display the image (e.g. in a jupyter notebook)
    try:
        from IPython.display import Image
        return Image(outfile)
    except ImportError:
        return outfile
Beispiel #7
0
 def tearDown(self):
     from atooms.core.utils import rmf, rmd
     rmf('/tmp/test_backends*')
     rmd('/tmp/test_backends')
Beispiel #8
0
 def tearDown(self):
     rmd('/tmp/test_lammps.d')
     rmf('/tmp/test_lammps*')
Beispiel #9
0
 def tearDown(self):
     from atooms.core.utils import rmf
     rmf('/tmp/test_utils*')