<commit_before># -*- coding: iso-8859-1 -*-
# Maintainer: joaander

from hoomd import *
from hoomd import md
import unittest
import os

context.initialize()

# charge.pppm
class charge_pppm_tests (unittest.TestCase):
    def setUp(self):
        self.s = init.create_lattice(lattice.sc(a=2.1878096788957757),n=[5,5,4]); #target a packing fraction of 0.05

        for i in range(0,50):
            self.s.particles[i].charge = -1;

        for i in range(50,100):
            self.s.particles[i].charge = 1;

    # basic test of creation and param setting
    def test(self):
        all = group.all()
        nl = md.nlist.cell()
        c = md.charge.pppm(all, nlist = nl);
        c.set_params(Nx=16, Ny=16, Nz=16, order=4, rcut=2.0);
        md.integrate.mode_standard(dt=0.005);
        md.integrate.nve(all);
        run(100);

        del all
        del c

    # Cannot test pppm multiple times currently because of implementation limitations
    ## test missing coefficients
    #def test_set_missing_coeff(self):
        #all = group.all()
        #c = charge.pppm(all);
        #integrate.mode_standard(dt=0.005);
        #integrate.nve(all);
        #self.assertRaises(RuntimeError, run, 100);

    ## test enable/disable
    #def test_enable_disable(self):
        #all = group.all()
        #c = charge.pppm(all);
        #c.set_params(Nx=16, Ny=16, Nz=16, order=4, rcut=2.0);

        #c.disable(log=True);
        #c.enable();

    def tearDown(self):
        del self.s
        context.initialize()

# charge.pppm
class charge_pppm_twoparticle_tests (unittest.TestCase):
    def setUp(self):
        print
        # initialize a two particle system in a triclinic box
        snap = data.make_snapshot(N=2, particle_types=[u'A1'], box = data.boxdim(xy=0.5,xz=0.5,yz=0.5,L=10))

        if comm.get_rank() == 0:
            snap.particles.position[0] = (0,0,0)
            snap.particles.position[1] = (3,3,3)
            snap.particles.charge[0] = 1
            snap.particles.charge[1] = -1

        self.s = init.read_snapshot(snap);

    # basic test of creation and param setting
    def test(self):
        all = group.all()
        nl = md.nlist.cell()
        c = md.charge.pppm(all, nlist = nl);
        c.set_params(Nx=128, Ny=128, Nz=128, order=3, rcut=2.0);
        log = analyze.log(quantities = ['pppm_energy','pressure_xx','pressure_xy','pressure_xz', 'pressure_yy','pressure_yz', 'pressure_zz'], period = 1, filename=None);
        md.integrate.mode_standard(dt=0.0);
        md.integrate.nve(all);
        # trick to allow larger decompositions
        nl.set_params(r_buff=0.1)<commit_msg>add unit test for bond exclusions in charge.pppm()<commit_after># -*- coding: iso-8859-1 -*-
# Maintainer: joaander

from hoomd import *
from hoomd import md
import unittest
import os

context.initialize()

# charge.pppm
class charge_pppm_tests (unittest.TestCase):
    def setUp(self):
        self.s = init.create_lattice(lattice.sc(a=2.1878096788957757),n=[5,5,4]); #target a packing fraction of 0.05

        for i in range(0,50):
            self.s.particles[i].charge = -1;

        for i in range(50,100):
            self.s.particles[i].charge = 1;

    # basic test of creation and param setting
    def test(self):
        all = group.all()
        nl = md.nlist.cell()
        c = md.charge.pppm(all, nlist = nl);
        c.set_params(Nx=16, Ny=16, Nz=16, order=4, rcut=2.0);
        md.integrate.mode_standard(dt=0.005);
        md.integrate.nve(all);
        run(100);

        del all
        del c

    # Cannot test pppm multiple times currently because of implementation limitations
    ## test missing coefficients
    #def test_set_missing_coeff(self):
        #all = group.all()
        #c = charge.pppm(all);
        #integrate.mode_standard(dt=0.005);
        #integrate.nve(all);
        #self.assertRaises(RuntimeError, run, 100);

    ## test enable/disable
    #def test_enable_disable(self):
        #all = group.all()
        #c = charge.pppm(all);
        #c.set_params(Nx=16, Ny=16, Nz=16, order=4, rcut=2.0);

        #c.disable(log=True);
        #c.enable();

    def tearDown(self):
        del self.s
        context.initialize()

# charge.pppm
class charge_pppm_bond_exclusions_test(unittest.TestCase):
    def test_exclusion_energy(self):
        # initialize a two particle system in a large box, to minimize effect of PBC
        snap = data.make_snapshot(N=2, particle_types=[u'A1'], bond_types=['bondA'], box = data.boxdim(L=50))

        if comm.get_rank() == 0:
            snap.particles.position[0] = (0,0,0)
            snap.particles.position[1] = (1,1,1)
            snap.particles.charge[0] = 1
            snap.particles.charge[1] = -1

        self.s = init.read_snapshot(snap);

        # measure the Coulomb energy with sufficient number of grid cells so that the exclusions are accurate
        nl = md.nlist.cell()
        c = md.charge.pppm(group.all(), nlist = nl);

        # rcut should be larger than distance of pair, so we have a split into short range and long range energy
        c.set_params(Nx=128, Ny=128, Nz=128, order=7, rcut=3);
        log = analyze.log(quantities = ['potential_energy','pair_ewald_energy','pppm_energy'], period = 1, filename=None);
        md.integrate.mode_standard(dt=0.0);
        md.integrate.nve(group.all());
        # trick to allow larger decompositions
        nl.set_params(r_buff=0.1)
        context.current.sorter.disable()

        run(1)
        pppm_energy_nobond = log.query('pppm_energy')
        ewald_energy_nobond = log.query('pair_ewald_energy')
        potential_energy_nobond = log.query('potential_energy')

        # check that it is **roughly** equal to the Coulomb energy (not accounting for PBC)
        import numpy as np
        self.assertAlmostEqual(potential_energy_nobond,
            -1/np.linalg.norm(np.array(self.s.particles[1].position)-np.array(self.s.particles[0].position)),3)

        # now introduce a bond
        self.s.bonds.add('bondA', 0,1)

        # we need to manually add it to the exclusions currently
        nl.reset_exclusions(exclusions=['bond'])
        run(1)
        pppm_energy_bond = log.query('pppm_energy')
        ewald_energy_bond = log.query('pair_ewald_energy')
        potential_energy_bond = log.query('potential_energy')

        # energy should be practically zero
        # but not exactly -- exclusions are computed analytically, the long range part using fft
        self.assertAlmostEqual(pppm_energy_bond, 0.0, 3)
        self.assertAlmostEqual(potential_energy_bond, 0.0, 3)

        # but strictly zero for the short range part
        self.assertEqual(ewald_energy_bond, 0.0)

    def tearDown(self):
        del self.s
        context.initialize();


# charge.pppm
class charge_pppm_twoparticle_tests (unittest.TestCase):
    def setUp(self):
        print
        # initialize a two particle system in a triclinic box
        snap = data.make_snapshot(N=2, particle_types=[u'A1'], box = data.boxdim(xy=0.5,xz=0.5,yz=0.5,L=10))

        if comm.get_rank() == 0:
            snap.particles.position[0] = (0,0,0)
            snap.particles.position[1] = (3,3,3)
            snap.particles.charge[0] = 1
            snap.particles.charge[1] = -1

        self.s = init.read_snapshot(snap);

    # basic test of creation and param setting
    def test(self):
        all = group.all()
        nl = md.nlist.cell()
        c = md.charge.pppm(all, nlist = nl);
        c.set_params(Nx=128, Ny=128, Nz=128, order=3, rcut=2.0);
        log = analyze.log(quantities = ['pppm_energy','pressure_xx','pressure_xy','pressure_xz', 'pressure_yy','pressure_yz', 'pressure_zz'], period = 1, filename=None);
        md.integrate.mode_standard(dt=0.0);
        md.integrate.nve(all);
        # trick to allow larger decompositions
        nl.set_params(r_buff=0.1)
{'fraction_non_alphanumeric': 0.13019637847487886, 'fraction_numerical': 0.03430247385870951, 'mean_word_length': 2.601010101010101, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4024089', 'n_tokens_mistral': 2813, 'n_tokens_neox': 2625, 'n_words': 574}