<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package etomica.potential.compute;

import etomica.atom.IAtomKinetic;
import etomica.atom.IAtomList;
import etomica.box.Box;
import etomica.integrator.IntegratorEvent;
import etomica.integrator.IntegratorListener;
import etomica.potential.IPotentialAtomic;
import etomica.potential.IPotentialHard;
import etomica.space.Vector;
import etomica.util.collections.Int2IntHash;

/**
 * Simple (all atoms are neighbors of all atoms) neighbor manager that keeps
 * track of state for pairs of atoms.  We use an int-int hashmap, which isn't
 * super-fast, but should be fine and faster than the alternatives.
 */
public class NeighborManagerSimpleHard extends NeighborManagerSimple implements NeighborManagerHard {

    public Int2IntHash[] stateHash;
    protected IPotentialAtomic[][] pairPotentials;
    protected boolean warningPrinted;

    public NeighborManagerSimpleHard(Box box) {
        super(box);
        reset();
    }

    @Override
    public NeighborIterator makeNeighborIterator() {
        return null;
    }

    @Override
    public void setPairPotentials(IPotentialAtomic[][] potentials) {
        pairPotentials = potentials;
    }

    @Override
    public void init() {
        super.init();
        reset();
    }

    protected void reset() {
        IAtomList atoms = box.getLeafList();
        stateHash = new Int2IntHash[atoms.size()];
        for (int i = 0; i < stateHash.length; i++) {
            stateHash[i] = new Int2IntHash(10);
            IAtomKinetic iAtom = (IAtomKinetic) atoms.get(i);
            Vector ri = iAtom.getPosition();
            IPotentialAtomic[] iPotentials = pairPotentials[iAtom.getType().getIndex()];
            for (int j = i + 1; j < stateHash.length; j++) {
                IAtomKinetic jAtom = (IAtomKinetic) atoms.get(j);
                Vector rj = jAtom.getPosition();
                Vector dr = Vector.d(ri.getD());
                dr.Ev1Mv2(rj, ri);
                box.getBoundary().nearestImage(dr);
                int state = ((IPotentialHard) iPotentials[jAtom.getType().getIndex()]).getState(iAtom, jAtom, dr);
                if (state >= 0) stateHash[i].put(j, state);
            }
        }
    }

    @Override
    public IntegratorListener makeIntegratorListener() {
        return new IntegratorListener() {
            @Override
            public void integratorInitialized(IntegratorEvent e) {
                init();
            }

            @Override
            public void integratorStepStarted(IntegratorEvent e) {
            }

            @Override
            public void integratorStepFinished(IntegratorEvent e) {
            }
        };
    }

    @Override
    public void setPairState(int i, int j, int state) {
        Int2IntHash iHash = stateHash[i];
        int oldState = iHash.get(j);
        if (state == oldState || (state < 0 && oldState < 0)) return;
        if (state < 0) {
            iHash.remove(j);
        } else {
            iHash.put(j, state);
        }
    }
}
<commit_msg>actually construct simple hard iterator<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package etomica.potential.compute;

import etomica.atom.IAtom;
import etomica.atom.IAtomKinetic;
import etomica.atom.IAtomList;
import etomica.box.Box;
import etomica.integrator.IntegratorEvent;
import etomica.integrator.IntegratorListener;
import etomica.potential.IPotentialAtomic;
import etomica.potential.IPotentialHard;
import etomica.space.Vector;
import etomica.util.collections.Int2IntHash;

/**
 * Simple (all atoms are neighbors of all atoms) neighbor manager that keeps
 * track of state for pairs of atoms.  We use an int-int hashmap, which isn't
 * super-fast, but should be fine and faster than the alternatives.
 */
public class NeighborManagerSimpleHard extends NeighborManagerSimple implements NeighborManagerHard {

    public Int2IntHash[] stateHash;
    protected IPotentialAtomic[][] pairPotentials;
    protected boolean warningPrinted;

    public NeighborManagerSimpleHard(Box box) {
        super(box);
        reset();
    }

    @Override
    public NeighborIterator makeNeighborIterator() {
        return new NeighborIterator() {
            @Override
            public void iterUpNeighbors(int i, NeighborConsumer consumer) {
                final NeighborConsumerHard consumerHard = consumer instanceof NeighborConsumerHard ? (NeighborConsumerHard) consumer : null;
                IAtomList atoms = box.getLeafList();
                Vector rij = box.getSpace().makeVector();
                Vector ri = atoms.get(i).getPosition();
                for (int j = i + 1; j < atoms.size(); j++) {
                    rij.Ev1Mv2(atoms.get(j).getPosition(), ri);
                    box.getBoundary().nearestImage(rij);
                    if (consumerHard == null) {
                        // we're like just computing energy
                        consumer.accept(atoms.get(j), rij);
                    } else {
                        consumerHard.accept(atoms.get(j), rij, stateHash[i].get(j));
                    }
                }
            }

            @Override
            public void iterDownNeighbors(int i, NeighborConsumer consumer) {
                final NeighborConsumerHard consumerHard = consumer instanceof NeighborConsumerHard ? (NeighborConsumerHard) consumer : null;
                IAtomList atoms = box.getLeafList();
                Vector rij = box.getSpace().makeVector();
                Vector ri = atoms.get(i).getPosition();
                for (int j = 0; j < i; j++) {
                    rij.Ev1Mv2(atoms.get(j).getPosition(), ri);
                    box.getBoundary().nearestImage(rij);
                    if (consumerHard == null) {
                        // we're like just computing energy
                        consumer.accept(atoms.get(j), rij);
                    } else {
                        consumerHard.accept(atoms.get(j), rij, stateHash[j].get(i));
                    }
                }
            }

            @Override
            public void iterAllNeighbors(int i, NeighborConsumer consumer) {
                final NeighborConsumerHard consumerHard = consumer instanceof NeighborConsumerHard ? (NeighborConsumerHard) consumer : null;
                IAtomList atoms = box.getLeafList();
                Vector rij = box.getSpace().makeVector();
                Vector ri = atoms.get(i).getPosition();
                for (int j = 0; j < atoms.size(); j++) {
                    if (j == i) continue;
                    rij.Ev1Mv2(atoms.get(j).getPosition(), ri);
                    box.getBoundary().nearestImage(rij);
                    consumer.accept(atoms.get(j), rij);
                    if (consumerHard == null) {
                        // we're like just computing energy
                        consumer.accept(atoms.get(j), rij);
                    } else {
                        consumerHard.accept(atoms.get(j), rij, stateHash[i].get(j));
                    }
                }
            }

            @Override
            public double iterAndSumAllNeighbors(IAtom atom1, SuperNbrConsumer consumer) {
                IAtomList atoms = box.getLeafList();
                Vector rij = box.getSpace().makeVector();
                Vector ri = atom1.getPosition();
                double sum = 0;
                for (int j = 0; j < atoms.size(); j++) {
                    if (j == atom1.getLeafIndex()) continue;
                    rij.Ev1Mv2(atoms.get(j).getPosition(), ri);
                    box.getBoundary().nearestImage(rij);
                    sum += consumer.accept(atom1, atoms.get(j), rij);
                }
                return sum;
            }
        };
    }
    @Override
    public void setPairPotentials(IPotentialAtomic[][] potentials) {
        pairPotentials = potentials;
    }

    @Override
    public void init() {
        super.init();
        reset();
    }

    protected void reset() {
        IAtomList atoms = box.getLeafList();
        stateHash = new Int2IntHash[atoms.size()];
        for (int i = 0; i < stateHash.length; i++) {
            stateHash[i] = new Int2IntHash(10);
            IAtomKinetic iAtom = (IAtomKinetic) atoms.get(i);
            Vector ri = iAtom.getPosition();
            IPotentialAtomic[] iPotentials = pairPotentials[iAtom.getType().getIndex()];
            for (int j = i + 1; j < stateHash.length; j++) {
                IAtomKinetic jAtom = (IAtomKinetic) atoms.get(j);
                Vector rj = jAtom.getPosition();
                Vector dr = Vector.d(ri.getD());
                dr.Ev1Mv2(rj, ri);
                box.getBoundary().nearestImage(dr);
                int state = ((IPotentialHard) iPotentials[jAtom.getType().getIndex()]).getState(iAtom, jAtom, dr);
                if (state >= 0) stateHash[i].put(j, state);
            }
        }
    }

    @Override
    public IntegratorListener makeIntegratorListener() {
        return new IntegratorListener() {
            @Override
            public void integratorInitialized(IntegratorEvent e) {
                init();
            }

            @Override
            public void integratorStepStarted(IntegratorEvent e) {
            }

            @Override
            public void integratorStepFinished(IntegratorEvent e) {
            }
        };
    }

    @Override
    public void setPairState(int i, int j, int state) {
        Int2IntHash iHash = stateHash[i];
        int oldState = iHash.get(j);
        if (state == oldState || (state < 0 && oldState < 0)) return;
        if (state < 0) {
            iHash.remove(j);
        } else {
            iHash.put(j, state);
        }
    }
}

{'fraction_non_alphanumeric': 0.10321809305569393, 'fraction_numerical': 0.005479725017435488, 'mean_word_length': 1.9154806854487365, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, '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': '5110662', 'n_tokens_mistral': 2924, 'n_tokens_neox': 2760, 'n_words': 690}