<commit_before>package etomica;
import etomica.units.*;

/**
 * Meter for the pressure (given as the compressibility factor) of a hard potential.
 * Performs sum of collision virial over all collisions, and manipulates value
 * to obtain the compressibility factor, PV/NkT.
 *
 * @author <NAME>
 */
public final class MeterPressureHard extends MeterScalar implements
                                                IntegratorHard.CollisionListener,
                                                MeterCollisional,
                                                EtomicaElement {
    
    public MeterPressureHard() {
        super();
        setLabel("PV/NkT");
        timer = new DataSourceCountTime();
    }
        
    public static EtomicaInfo getEtomicaInfo() {
        EtomicaInfo info = new EtomicaInfo("Compressibility factor measured via impulsive virial averaged over interatomic hard collisions");
        return info;
    }
        
    /**
     * Indicator that this meter returns dimensionless quantity 
     * (note: returns PV/NkT, not P)
     */
    public Dimension getDimension() {return Dimension.NULL;}

    /**
     * Returns P*V/N*kB*T = 1 - (virial sum)/(elapsed time)/T/(space dimension)/(number of atoms)
     * Virial sum and elapsed time apply to period since last call to this method.
     */
    //XXX phase parameter is not used appropriately here
    //TODO consider how to ensure timer is advanced before this method is invoked
    public double getDataAsScalar(Phase phase) {
        double elapsedTime = timer.getData()[0];
        if(elapsedTime == 0.0) return Double.NaN;
        int D = phase.boundary().dimensions().D();
        double value = 1.0 - virialSum/(integratorHard.temperature()*elapsedTime*(double)(D*phase.atomCount()));
 
        virialSum = 0.0;
        timer.reset();
        return value;
    }
    /**
     * Implementation of CollisionListener interface
     * Adds collision virial (from potential) to accumulator
     */
    public void collisionAction(IntegratorHard.Agent agent) {
        virialSum += agent.collisionPotential.lastCollisionVirial();
    }
    
    /**
     * Implementation of Meter.MeterCollisional interface.  Returns -(collision virial).
     * Suitable for tabulation of PV
     */
	public double collisionValue(IntegratorHard.Agent agent) {
	    return -agent.collisionPotential.lastCollisionVirial();
	}

    /**
     * Registers meter as a collisionListener to the integrator, and sets up
     * a DataSourceTimer to keep track of elapsed time of integrator.
     */
	protected void setIntegrator(IntegratorHard newIntegrator) {
		if(newIntegrator == integratorHard) return;
		if(integratorHard != null) integratorHard.removeCollisionListener(this);
		integratorHard = newIntegrator;
	    timer.setIntegrator(new IntegratorMD[] {(IntegratorMD)newIntegrator});
	    if(newIntegrator != null) integratorHard.addCollisionListener(this);
	}
   
    private double virialSum = 0.0;
    private IntegratorHard integratorHard;
    private final DataSourceCountTime timer;

	
    /**
     * Method to demonstrate and test the use of this class.  
     * Pressure is measured in a hard-sphere MD simulation.
     */
 /*   public static void main(String[] args) {
        etomica.simulations.HSMD2D sim = new etomica.simulations.HSMD2D();
        Simulation.instance = sim;
        
        //here's the part unique to this class
        sim.integrator.setIsothermal(true);
        sim.integrator.setTemperature(Kelvin.UNIT.toSim(300.));
        //make the meter and register it with the integrator
        MeterPressureHard pressureMeter = new MeterPressureHard();<commit_msg>actually set the Integrator<commit_after>package etomica;
import etomica.units.*;

/**
 * Meter for the pressure (given as the compressibility factor) of a hard potential.
 * Performs sum of collision virial over all collisions, and manipulates value
 * to obtain the compressibility factor, PV/NkT.
 *
 * @author <NAME>
 */
public final class MeterPressureHard extends MeterScalar implements
                                                IntegratorHard.CollisionListener,
                                                MeterCollisional,
                                                EtomicaElement {
    
    public MeterPressureHard(IntegratorHard integrator) {
        super();
        setLabel("PV/NkT");
        timer = new DataSourceCountTime();
        setIntegrator(integrator);
    }
        
    public static EtomicaInfo getEtomicaInfo() {
        EtomicaInfo info = new EtomicaInfo("Compressibility factor measured via impulsive virial averaged over interatomic hard collisions");
        return info;
    }
        
    /**
     * Indicator that this meter returns dimensionless quantity 
     * (note: returns PV/NkT, not P)
     */
    public Dimension getDimension() {return Dimension.NULL;}

    /**
     * Returns P*V/N*kB*T = 1 - (virial sum)/(elapsed time)/T/(space dimension)/(number of atoms)
     * Virial sum and elapsed time apply to period since last call to this method.
     */
    //XXX phase parameter is not used appropriately here
    //TODO consider how to ensure timer is advanced before this method is invoked
    public double getDataAsScalar(Phase phase) {
        double elapsedTime = timer.getData()[0];
        if(elapsedTime == 0.0) return Double.NaN;
        int D = phase.boundary().dimensions().D();
        double value = 1.0 - virialSum/(integratorHard.temperature()*elapsedTime*(double)(D*phase.atomCount()));
 
        virialSum = 0.0;
        timer.reset();
        return value;
    }
    /**
     * Implementation of CollisionListener interface
     * Adds collision virial (from potential) to accumulator
     */
    public void collisionAction(IntegratorHard.Agent agent) {
        virialSum += agent.collisionPotential.lastCollisionVirial();
    }
    
    /**
     * Implementation of Meter.MeterCollisional interface.  Returns -(collision virial).
     * Suitable for tabulation of PV
     */
	public double collisionValue(IntegratorHard.Agent agent) {
	    return -agent.collisionPotential.lastCollisionVirial();
	}

    /**
     * Registers meter as a collisionListener to the integrator, and sets up
     * a DataSourceTimer to keep track of elapsed time of integrator.
     */
	protected void setIntegrator(IntegratorHard newIntegrator) {
		if(newIntegrator == integratorHard) return;
		if(integratorHard != null) integratorHard.removeCollisionListener(this);
		integratorHard = newIntegrator;
	    timer.setIntegrator(new IntegratorMD[] {newIntegrator});
	    if(newIntegrator != null) integratorHard.addCollisionListener(this);
	}
   
    private double virialSum = 0.0;
    private IntegratorHard integratorHard;
    private final DataSourceCountTime timer;

	
    /**
     * Method to demonstrate and test the use of this class.  
     * Pressure is measured in a hard-sphere MD simulation.
     */
 /*   public static void main(String[] args) {
        etomica.simulations.HSMD2D sim = new etomica.simulations.HSMD2D();
        Simulation.instance = sim;
        
        //here's the part unique to this class
        sim.integrator.setIsothermal(true);
        sim.integrator.setTemperature(Kelvin.UNIT.toSim(300.));
        //make the meter and register it with the integrator
        MeterPressureHard pressureMeter = new MeterPressureHard();
{'fraction_non_alphanumeric': 0.08505154639175258, 'fraction_numerical': 0.004069451980466631, 'mean_word_length': 2.6572420634920637, 'pattern_counts': {'":': 0, '<': 5, '<?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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6249882', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1943, 'n_words': 590}