<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;

import etomica.atom.IAtomKinetic;
import etomica.atom.IAtomList;
import etomica.box.Box;
import etomica.space.Boundary;
import etomica.space.Tensor;
import etomica.space.Vector;

/**
 * Potential class for any hard spherical potential.  The steps in the potential
 * are defined at construction.  This implements Potential2Soft so that it can be
 * used in a PotentialCompute to get the energy.
 */
public class P2HardGeneric implements IPotentialHard, Potential2Soft {

    /**
     * Setting fixOverlapDefault to true will cause any P2HardGeneric to be
     * created with fixOverlap on by default.  With fixOverlap enabled, the
     * energy of overlapping pairs will be returned as the energy of pairs
     * that are barely not overlapping and collision times will be returned
     * to help push the particles apart.
     */
    public static boolean fixOverlapDefault;

    /**
     * The pair distances where collisions should happen.
     * Distances are listed from smallest to largest.
     */
    protected final double[] collisionDistances2;

    /**
     * The pair energies where a pair of atoms is between the various
     * distances returned by getCollisionDistances().
     * <p>
     * energy[0] = u when r < collisionDistance[0]
     * energy[1] = u when collisionDistance[0] < r < collisionDistance[1]
     * energy[2] = u when collisionDistance[1] < r < collisionDistance[2]
     * ...
     * energy[n] = u when r > collisionDistance[n-1]
     * <p>
     * The last energy should always be 0.
     */
    protected final double[] energies;

    // only used to support the energy(IAtomList) legacy method
    protected Boundary boundary;

    protected final boolean fixOverlap;

    public P2HardGeneric(double[] collisionDistances, double[] energies) {
        this(collisionDistances, energies, fixOverlapDefault);
    }

    public P2HardGeneric(double[] collisionDistances, double[] energies, boolean fixOverlap) {
        this.collisionDistances2 = new double[collisionDistances.length];
        for (int i = 0; i < collisionDistances.length; i++) {
            collisionDistances2[i] = collisionDistances[i] * collisionDistances[i];
        }
        this.energies = energies;
        this.fixOverlap = fixOverlap;
    }

    /**
     * Returns the state of the pair of atoms (atom1 and atom2) at distance r12
     */
    public int getState(IAtomKinetic atom1, IAtomKinetic atom2, Vector r12) {
        double r2 = r12.squared();
        double[] cd2 = collisionDistances2;
        for (int i = 0; i < cd2.length; i++) {
            if (cd2[i] > r2) return i;
        }
        return -1;<commit_msg>add methods to get/set energies and collision diameters<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;

import etomica.atom.IAtomKinetic;
import etomica.atom.IAtomList;
import etomica.box.Box;
import etomica.space.Boundary;
import etomica.space.Tensor;
import etomica.space.Vector;

/**
 * Potential class for any hard spherical potential.  The steps in the potential
 * are defined at construction.  This implements Potential2Soft so that it can be
 * used in a PotentialCompute to get the energy.
 */
public class P2HardGeneric implements IPotentialHard, Potential2Soft {

    /**
     * Setting fixOverlapDefault to true will cause any P2HardGeneric to be
     * created with fixOverlap on by default.  With fixOverlap enabled, the
     * energy of overlapping pairs will be returned as the energy of pairs
     * that are barely not overlapping and collision times will be returned
     * to help push the particles apart.
     */
    public static boolean fixOverlapDefault;

    /**
     * The pair distances where collisions should happen.
     * Distances are listed from smallest to largest.
     */
    protected final double[] collisionDistances2;

    /**
     * The pair energies where a pair of atoms is between the various
     * distances returned by getCollisionDistances().
     * <p>
     * energy[0] = u when r < collisionDistance[0]
     * energy[1] = u when collisionDistance[0] < r < collisionDistance[1]
     * energy[2] = u when collisionDistance[1] < r < collisionDistance[2]
     * ...
     * energy[n] = u when r > collisionDistance[n-1]
     * <p>
     * The last energy should always be 0.
     */
    protected final double[] energies;

    // only used to support the energy(IAtomList) legacy method
    protected Boundary boundary;

    protected final boolean fixOverlap;

    public P2HardGeneric(double[] collisionDistances, double[] energies) {
        this(collisionDistances, energies, fixOverlapDefault);
    }

    public P2HardGeneric(double[] collisionDistances, double[] energies, boolean fixOverlap) {
        if (collisionDistances.length != energies.length) {
            throw new IllegalArgumentException("collision distances and energies must have the same length");
        }
        this.collisionDistances2 = new double[collisionDistances.length];
        for (int i = 0; i < collisionDistances.length; i++) {
            collisionDistances2[i] = collisionDistances[i] * collisionDistances[i];
        }
        this.energies = energies;
        this.fixOverlap = fixOverlap;
    }

    public double getCollisionDiameter(int i) {
        return Math.sqrt(collisionDistances2[i]);
    }

    public void setCollisionDiameter(int i, double collisionDiameter) {
        collisionDistances2[i] = collisionDiameter * collisionDiameter;
    }

    public void setEnergyForState(int i, double u) {
        energies[i] = u;
    }

    /**
     * Returns the state of the pair of atoms (atom1 and atom2) at distance r12
     */
    public int getState(IAtomKinetic atom1, IAtomKinetic atom2, Vector r12) {
        double r2 = r12.squared();
        double[] cd2 = collisionDistances2;
        for (int i = 0; i < cd2.length; i++) {
            if (cd2[i] > r2) return i;
        }
        return -1;
{'fraction_non_alphanumeric': 0.08432210093339662, 'fraction_numerical': 0.013605442176870748, 'mean_word_length': 2.9761006289308174, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 11, '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': '3314718', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1760, 'n_words': 639}