コード例 #1
0
 def test_species(self):
     system = copy.copy(self.ref)
     npart = len(system.particle)
     for p in system.particle[0:10]:
         p.species = 'B'
     for p in system.particle[10:30]:
         p.species = 'C'
     from atooms.system.particle import composition, distinct_species
     self.assertEqual(distinct_species(system.particle), ['A', 'B', 'C'])
     self.assertEqual(system.distinct_species(), ['A', 'B', 'C'])
     self.assertEqual(composition(system.particle)['A'], npart - 30)
     self.assertEqual(composition(system.particle)['B'], 10)
     self.assertEqual(composition(system.particle)['C'], 20)
コード例 #2
0
 def test_decimate(self):
     from atooms.system import Particle, System
     from atooms.system.particle import composition, decimate
     p = [Particle(species='A')] * 20 + [Particle(species='B')] * 10
     pnew = decimate(p, 12)
     x = composition(pnew)
     self.assertEqual(x['A'], 8)
     self.assertEqual(x['B'], 4)
コード例 #3
0
ファイル: utils.py プロジェクト: rouol/MolecularDynamics
def is_semigrandcanonical(trajectory, tests=1):
    """
    Simple test to check if a trajectory is semigrandcanonical.
    i.e. if the chemical concentrations fluctuate.

    We compare the first frame to an integer number `tests` of other
    frames starting from the end of `trajectory`.
    """
    # This is adapted from is_cell_variable()
    is_variable = False
    if tests > 0:
        skip = max(1, int(len(trajectory) / float(tests)))
    else:
        skip = 1
    from atooms.system.particle import composition
    x0 = composition(trajectory[0].particle)
    for sample in range(len(trajectory) - 1, 0, -skip):
        x1 = composition(trajectory[sample].particle)
        is_variable = False
        for sp in x0:
            if x0[sp] != x1[sp]:
                is_variable = True
                break
    return is_variable
コード例 #4
0
ファイル: test_pp.py プロジェクト: FTurci/postprocessing
 def test_sk_field(self):
     """
     Test that S(k) with a field that is 0 if id=A and 1 if id=B gives
     the BB partial structure factor.
     """
     # TODO: this test fails with python 3 because of a weird issue with xyz trajectory in atooms (_fallback)
     f = os.path.join(self.reference_path, 'kalj-small.xyz')
     ff = os.path.join(self.reference_path, 'kalj-small-field.xyz')
     t = trajectory.TrajectoryXYZ(f)
     p = postprocessing.StructureFactor(t, [4, 7.3, 10],
                                        trajectory_field=ff)
     p.compute()
     # We multiply by x because the S(k) is normalized to 1/N
     from atooms.system.particle import composition
     x = composition(t[0].particle)['B'] / float(len(t[0].particle))
     ref_value = x * numpy.array(
         [0.86716496871363735, 0.86986885176760842, 0.98112175463699136])
     self.assertLess(deviation(p.value, ref_value), 1e-2)
コード例 #5
0
 def test_sk_field(self):
     """
     Test that S(k) with a field that is 0 if id=A and 1 if id=B gives
     the BB partial structure factor.
     """
     f = os.path.join(self.reference_path, 'kalj-small.xyz')
     ff = os.path.join(self.reference_path, 'kalj-small-field.xyz')
     th = trajectory.TrajectoryXYZ(f)
     tt = trajectory.TrajectoryXYZ(ff)
     p = postprocessing.StructureFactor(th, [4, 7.3, 10])
     p.add_weight(trajectory=tt, field='field_B')
     p.compute()
     # We multiply by x because the S(k) is normalized to 1/N
     from atooms.system.particle import composition
     x = composition(th[0].particle)['B'] / float(len(th[0].particle))
     ref_value = x * numpy.array(
         [0.86716496871363735, 0.86986885176760842, 0.98112175463699136])
     self.assertLess(deviation(p.value, ref_value), 1e-2)
     th.close()
     tt.close()
コード例 #6
0
ファイル: utils.py プロジェクト: rouol/MolecularDynamics
def info(trajectory, keys=None):
    """Return a string with information about a `trajectory` instance."""
    from atooms.system.particle import distinct_species, composition
    system = trajectory[0]
    if keys is None:

        # Default: full info
        txt = ''
        txt += 'path                 = %s\n' % trajectory.filename
        txt += 'format               = %s\n' % trajectory.__class__
        txt += 'frames               = %s\n' % len(trajectory)
        txt += 'megabytes            = %s\n' % (
            os.path.getsize(trajectory.filename) / 1e6)
        txt += 'particles            = %s\n' % len(system.particle)
        txt += 'species              = %s\n' % ', '.join(
            distinct_species(system.particle))
        txt += 'composition          = %s\n' % dict(
            composition(system.particle))
        txt += 'size dispersion      = %s\n' % (
            numpy.std([p.radius for p in system.particle]) /
            numpy.mean([p.radius for p in system.particle]))
        txt += 'density              = %s\n' % round(system.density, 10)
        if system.cell is not None:
            txt += 'cell side            = %s\n' % str(list(
                system.cell.side))[1:-1]
            txt += 'cell volume          = %s\n' % system.cell.volume
        if len(trajectory) > 1:
            txt += 'steps                = %s\n' % trajectory.steps[-1]
            txt += 'duration             = %s\n' % trajectory.times[-1]
            txt += 'timestep             = %s\n' % trajectory.timestep
            txt += 'block size           = %s\n' % trajectory.block_size
            if trajectory.block_size == 1:
                txt += 'steps between frames = %s\n' % (trajectory.steps[1] -
                                                        trajectory.steps[0])
                txt += 'time between frames  = %s\n' % (trajectory.times[1] -
                                                        trajectory.times[0])
            else:
                txt += 'block steps          = %s\n' % trajectory.steps[
                    trajectory.block_size - 1]
                txt += 'block                = %s\n' % ([
                    trajectory.steps[i] for i in range(trajectory.block_size)
                ])
            txt += 'grandcanonical       = %s' % trajectory.grandcanonical
        return txt

    else:
        # Selected infos.
        # TODO: of course, it would be cleaner to have a little class for that
        outs = []
        for key in keys.split(','):
            if key == 'path':
                outs.append(trajectory.filename)
            elif key == 'format':
                outs.append(trajectory.__class__)
            elif key == 'frames':
                outs.append(len(trajectory))
            elif key == 'megabytes':
                outs.append(os.path.getsize(trajectory.filename) / 1e6)
            elif key == 'particles':
                outs.append(len(system.particle))
            elif key == 'species':
                outs.append(', '.join(distinct_species(system.particle)))
            elif key == 'composition':
                outs.append(dict(composition(system.particle)))
            elif key == 'cell density':
                outs.append(round(system.density, 10))
            elif key == 'cell side':
                outs.append(str(list(system.cell.side))[1:-1])
            elif key == 'cell volume':
                outs.append(system.cell.volume)
            elif key == 'steps':
                outs.append(trajectory.steps[-1])
            elif key == 'duration':
                outs.append(trajectory.times[-1])
            elif key == 'timestep':
                outs.append(trajectory.timestep)
            elif key == 'block size':
                outs.append(trajectory.block_size)
            elif key == 'steps between frames':
                outs.append(trajectory.steps[1] - trajectory.steps[0])
            elif key == 'time between frames':
                outs.append(trajectory.times[1] - trajectory.times[0])
            elif key == 'block steps':
                outs.append(trajectory.steps[trajectory.block_size - 1])
            elif key == 'block':
                outs.append([
                    trajectory.steps[i] for i in range(trajectory.block_size)
                ])
            elif key == 'grandcanonical':
                outs.append(trajectory.grandcanonical)

        txt = ''
        fmt = '%%-%ds : %%s\n' % (max([len(key) for key in keys.split(',')]))
        for key, out in zip(keys.split(','), outs):
            txt += fmt % (key, out)

        return txt.strip('\n')