<commit_before>package etomica.models.nitrogen;

import java.io.File;

import etomica.action.WriteConfiguration;
import etomica.action.activity.ActivityIntegrate;
import etomica.api.IBox;
import etomica.api.ISpecies;
import etomica.api.IVector;
import etomica.box.Box;
import etomica.box.BoxAgentManager;
import etomica.config.ConfigurationFile;
import etomica.data.AccumulatorAverage;
import etomica.data.AccumulatorAverageFixed;
import etomica.data.DataPump;
import etomica.data.meter.MeterPotentialEnergy;
import etomica.data.types.DataGroup;
import etomica.integrator.IntegratorMC;
import etomica.lattice.crystal.Basis;
import etomica.lattice.crystal.BasisHcp;
import etomica.lattice.crystal.Primitive;
import etomica.lattice.crystal.PrimitiveHexagonal;
import etomica.listener.IntegratorListenerAction;
import etomica.nbr.list.molecule.BoxAgentSourceCellManagerListMolecular;
import etomica.nbr.list.molecule.NeighborListManagerSlantyMolecular;
import etomica.nbr.list.molecule.PotentialMasterListMolecular;
import etomica.normalmode.BasisBigCell;
import etomica.normalmode.MCMoveMoleculeCoupled;
import etomica.simulation.Simulation;
import etomica.space.Boundary;
import etomica.space.BoundaryDeformablePeriodic;
import etomica.space.Space;
import etomica.units.Degree;
import etomica.units.Kelvin;

/**
 * Direct Sampling for Rotational Perturbation
 *  from constraint angle to no rotational d.o.f.
 *   
 * @author <NAME>
 */
public class SimDirectBetaN2RPAngleToNoAngle extends Simulation {

    public SimDirectBetaN2RPAngleToNoAngle(Space space, int numMolecules, double density, double temperature, double angle, long numSteps) {
        super(space);
        
        BoxAgentSourceCellManagerListMolecular boxAgentSourceTarg = new BoxAgentSourceCellManagerListMolecular(this, null, space);
        BoxAgentManager boxAgentManagerTarg = new BoxAgentManager(boxAgentSourceTarg);
 
        BoxAgentSourceCellManagerListMolecular boxAgentSourceRef = new BoxAgentSourceCellManagerListMolecular(this, null, space);
        BoxAgentManager boxAgentManagerRef = new BoxAgentManager(boxAgentSourceRef);
        
        SpeciesN2 species = new SpeciesN2(space);
		addSpecies(species);

        // TARGET
        boxTarg = new Box(space);
        addBox(boxTarg);
        boxTarg.setNMolecules(species, numMolecules);

    	double ratio = 1.631;
		double aDim = Math.pow(4.0/(Math.sqrt(3.0)*ratio*density), 1.0/3.0);
		double cDim = aDim*ratio;
		System.out.println("\naDim: " + aDim + " ;cDim: " + cDim);
		int nC = (int)Math.pow(numMolecules/1.999999999, 1.0/3.0);
		
		Basis basisHCP = new BasisHcp();
		BasisBigCell basis = new BasisBigCell(space, basisHCP, new int[]{nC,nC,nC});
        
		IVector[] boxDim = new IVector[3];
		boxDim[0] = space.makeVector(new double[]{nC*aDim, 0, 0});
		boxDim[1] = space.makeVector(new double[]{-nC*aDim*Math.cos(Degree.UNIT.toSim(60)), nC*aDim*Math.sin(Degree.UNIT.toSim(60)), 0});
		boxDim[2] = space.makeVector(new double[]{0, 0, nC*cDim});
		
		int[] nCells = new int[]{1,1,1};
		Boundary boundary = new BoundaryDeformablePeriodic(space, boxDim);
		Primitive primitive = new PrimitiveHexagonal(space, nC*aDim, nC*cDim);
		
		CoordinateDefinitionNitrogen coordinateDefTarg = new CoordinateDefinitionNitrogen(this, boxTarg, primitive, basis, space);
		coordinateDefTarg.setIsBetaHCP();
		coordinateDefTarg.setOrientationVectorBeta(space);
		coordinateDefTarg.initializeCoordinates(nCells);
		
	    boxTarg.setBoundary(boundary);
	    
		double rCScale = 0.475;
		double rc = aDim*nC*rCScale;
		System.out.println("Truncation Radius (" + rCScale +" Box Length): " + rc);
		P2Nitrogen potentialTarg = new P2Nitrogen(space, rc);
		potentialTarg.setBox(boxTarg);

		PRotConstraint pRotConstraintTarg = new PRotConstraint(space,coordinateDefTarg, boxTarg);
		pRotConstraintTarg.setConstraintAngle(angle);
		
		PotentialMasterListMolecular potentialMasterTarg = 
			new PotentialMasterListMolecular(this, rc, boxAgentSourceTarg, boxAgentManagerTarg, new NeighborListManagerSlantyMolecular.NeighborListSlantyAgentSourceMolecular(rc, space), space);
		potentialMasterTarg.addPotential(potentialTarg, new ISpecies[]{species, species});
		//potentialMasterTarg.addPotential(pRotConstraintTarg,new ISpecies[]{species} );
	       
		MCMoveMoleculeCoupled moveTarg = new MCMoveMoleculeCoupled(potentialMasterTarg, getRandom(),space);
		moveTarg.setBox(boxTarg);
		moveTarg.setPotential(potentialTarg);
		moveTarg.setDoExcludeNonNeighbors(true);
		
//		MCMoveRotateMolecule3D rotateTarg = new MCMoveRotateMolecule3D(potentialMasterTarg, getRandom(), space);
//		rotateTarg.setBox(boxTarg);
		MCMoveRotateMolecule3DConstraint rotateConst
		= new MCMoveRotateMolecule3DConstraint(potentialMasterTarg, getRandom(), space, angle, coordinateDefTarg, boxTarg);
		rotateConst.setBox(boxTarg);
		
        IntegratorMC integratorTarg = new IntegratorMC(potentialMasterTarg, getRandom(), temperature);
        integratorTarg.getMoveManager().addMCMove(moveTarg);
		//integratorTarg.getMoveManager().addMCMove(rotateTarg);
        integratorTarg.getMoveManager().addMCMove(rotateConst);
	    
		integratorTarg.setBox(boxTarg);

        // Reference System
		PotentialMasterListMolecular potentialMasterRef = 
			new PotentialMasterListMolecular(this, rc, boxAgentSourceRef, boxAgentManagerRef, new NeighborListManagerSlantyMolecular.NeighborListSlantyAgentSourceMolecular(rc, space), space);
		potentialMasterRef.addPotential(potentialTarg, new ISpecies[]{species, species});
		potentialMasterRef.addPotential(pRotConstraintTarg,new ISpecies[]{species} );
		
		int cellRange = 6;
	    potentialMasterTarg.setRange(rc);
	    potentialMasterTarg.setCellRange(cellRange); 
	    potentialMasterTarg.getNeighborManager(boxTarg).reset();
	        
//	    int potentialCells = potentialMasterTarg.getNbrCellManager(boxTarg).getLattice().getSize()[0];
//	    if (potentialCells < cellRange*2+1) {
//	      throw new RuntimeException("oops ("+potentialCells+" < "+(cellRange*2+1)+")");
//	    }
	    potentialMasterRef.setRange(rc);
	    potentialMasterRef.setCellRange(cellRange); 
	    potentialMasterRef.getNeighborManager(boxTarg).reset();
	     
	    MeterPotentialEnergy meterPERef = new MeterPotentialEnergy(potentialMasterRef);
        meterPERef.setBox(boxTarg);
        double latticeEnergy = meterPERef.getDataAsScalar();
        System.out.println("lattice energy per mol(sim unit): " + latticeEnergy/numMolecules);
		
		MeterBoltzmannDirect meterBoltzmann = new MeterBoltzmannDirect(integratorTarg, meterPERef);
		
//        MeterRotPerturbMolecule meterBoltzmannRotPerb = new MeterRotPerturbMolecule(integratorTarg, potentialMasterRef, species, space, this, coordinateDefTarg);
//        meterBoltzmannRotPerb.setLatticeEnergy(latticeEnergy);
		int numBlock = 100;
		int interval = numMolecules;
		long blockSize = numSteps/(numBlock*interval);
        if (blockSize == 0) blockSize = 1;
        System.out.println("block size "+blockSize+" interval "+interval);
        boltzmannAverage = new AccumulatorAverageFixed(blockSize);
        
        DataPump boltzmannPump = new DataPump(meterBoltzmann, boltzmannAverage);
        IntegratorListenerAction boltzmannPumpListener = new IntegratorListenerAction(boltzmannPump, 100);
        integratorTarg.getEventManager().addListener(boltzmannPumpListener);

        activityIntegrate = new ActivityIntegrate(integratorTarg);
        getController().addAction(activityIntegrate);
    }

    public void initializeConfigFromFile(String fname){
        ConfigurationFile config = new ConfigurationFile(fname);
        config.initializeCoordinates(boxTarg);
    }
    
    public void writeConfiguration(String fname){
        WriteConfiguration writeConfig = new WriteConfiguration(space);
        writeConfig.setBox(boxTarg);
        writeConfig.setDoApplyPBC(false);
        writeConfig.setConfName(fname);
        writeConfig.actionPerformed();
        System.out.println("\n***output configFile: "+ fname);
    }
    
    
    /**
     * @param args filename containing simulation parameters
     * @see SimDirectBetaN2RPAngleToNoAngle.SimOverlapParam
     */
    public static void main(String[] args) {

        double temperature = 45; //in UNIT KELVIN
        double density = 0.025;
        double angle = 0.05;
        long numSteps = 100000;<commit_msg>update using MCMoveRotateMolecule3DFixedAngle<commit_after>package etomica.models.nitrogen;

import java.io.File;

import etomica.action.WriteConfiguration;
import etomica.action.activity.ActivityIntegrate;
import etomica.api.IBox;
import etomica.api.ISpecies;
import etomica.api.IVector;
import etomica.box.Box;
import etomica.box.BoxAgentManager;
import etomica.config.ConfigurationFile;
import etomica.data.AccumulatorAverage;
import etomica.data.AccumulatorAverageFixed;
import etomica.data.DataPump;
import etomica.data.meter.MeterPotentialEnergy;
import etomica.data.types.DataGroup;
import etomica.integrator.IntegratorMC;
import etomica.lattice.crystal.Basis;
import etomica.lattice.crystal.BasisHcp;
import etomica.lattice.crystal.Primitive;
import etomica.lattice.crystal.PrimitiveHexagonal;
import etomica.listener.IntegratorListenerAction;
import etomica.nbr.list.molecule.BoxAgentSourceCellManagerListMolecular;
import etomica.nbr.list.molecule.NeighborListManagerSlantyMolecular;
import etomica.nbr.list.molecule.PotentialMasterListMolecular;
import etomica.normalmode.BasisBigCell;
import etomica.normalmode.MCMoveMoleculeCoupled;
import etomica.simulation.Simulation;
import etomica.space.Boundary;
import etomica.space.BoundaryDeformablePeriodic;
import etomica.space.Space;
import etomica.units.Degree;
import etomica.units.Kelvin;

/**
 * Direct Sampling for Rotational Perturbation
 * from NO rotational energy to constraint-angle rotational energy
 * @author <NAME>
 */
public class SimDirectBetaN2RPAngleToNoAngle extends Simulation {

    public SimDirectBetaN2RPAngleToNoAngle(Space space, int numMolecules, double density, double temperature, double angle, long numSteps) {
        super(space);
        
        BoxAgentSourceCellManagerListMolecular boxAgentSourceTarg = new BoxAgentSourceCellManagerListMolecular(this, null, space);
        BoxAgentManager boxAgentManagerTarg = new BoxAgentManager(boxAgentSourceTarg);
 
        BoxAgentSourceCellManagerListMolecular boxAgentSourceRef = new BoxAgentSourceCellManagerListMolecular(this, null, space);
        BoxAgentManager boxAgentManagerRef = new BoxAgentManager(boxAgentSourceRef);
        
        SpeciesN2 species = new SpeciesN2(space);
		addSpecies(species);

        // TARGET
        boxTarg = new Box(space);
        addBox(boxTarg);
        boxTarg.setNMolecules(species, numMolecules);

    	double ratio = 1.631;
		double aDim = Math.pow(4.0/(Math.sqrt(3.0)*ratio*density), 1.0/3.0);
		double cDim = aDim*ratio;
		System.out.println("\naDim: " + aDim + " ;cDim: " + cDim);
		int nC = (int)Math.pow(numMolecules/1.999999999, 1.0/3.0);
		
		Basis basisHCP = new BasisHcp();
		BasisBigCell basis = new BasisBigCell(space, basisHCP, new int[]{nC,nC,nC});
        
		IVector[] boxDim = new IVector[3];
		boxDim[0] = space.makeVector(new double[]{nC*aDim, 0, 0});
		boxDim[1] = space.makeVector(new double[]{-nC*aDim*Math.cos(Degree.UNIT.toSim(60)), nC*aDim*Math.sin(Degree.UNIT.toSim(60)), 0});
		boxDim[2] = space.makeVector(new double[]{0, 0, nC*cDim});
		
		int[] nCells = new int[]{1,1,1};
		Boundary boundary = new BoundaryDeformablePeriodic(space, boxDim);
		Primitive primitive = new PrimitiveHexagonal(space, nC*aDim, nC*cDim);
		
		CoordinateDefinitionNitrogen coordinateDefTarg = new CoordinateDefinitionNitrogen(this, boxTarg, primitive, basis, space);
		coordinateDefTarg.setIsBetaHCP();
		coordinateDefTarg.setOrientationVectorBeta(space);
		coordinateDefTarg.initializeCoordinates(nCells);
		
	    boxTarg.setBoundary(boundary);
	    
		double rCScale = 0.465;
		double rc = aDim*nC*rCScale;
		System.out.println("Truncation Radius (" + rCScale +" Box Length): " + rc);
		P2Nitrogen potentialTarg = new P2Nitrogen(space, rc);
		potentialTarg.setBox(boxTarg);

		PRotConstraint pRotConstraintTarg = new PRotConstraint(space,coordinateDefTarg, boxTarg);
		pRotConstraintTarg.setConstraintAngle(angle);
		
		PotentialMasterListMolecular potentialMasterTarg = 
			new PotentialMasterListMolecular(this, rc, boxAgentSourceTarg, boxAgentManagerTarg, new NeighborListManagerSlantyMolecular.NeighborListSlantyAgentSourceMolecular(rc, space), space);
		potentialMasterTarg.addPotential(potentialTarg, new ISpecies[]{species, species});
		//potentialMasterTarg.addPotential(pRotConstraintTarg,new ISpecies[]{species} );
	       
		MCMoveMoleculeCoupled moveTarg = new MCMoveMoleculeCoupled(potentialMasterTarg, getRandom(),space);
		moveTarg.setBox(boxTarg);
		moveTarg.setPotential(potentialTarg);
		moveTarg.setDoExcludeNonNeighbors(true);
		
//		MCMoveRotateMolecule3D rotateTarg = new MCMoveRotateMolecule3D(potentialMasterTarg, getRandom(), space);
//		rotateTarg.setBox(boxTarg);
		MCMoveRotateMolecule3DFixedAngle rotateFixedAngle
		= new MCMoveRotateMolecule3DFixedAngle(potentialMasterTarg, getRandom(), space, angle, coordinateDefTarg, boxTarg);
		rotateFixedAngle.setBox(boxTarg);
		
        IntegratorMC integratorTarg = new IntegratorMC(potentialMasterTarg, getRandom(), temperature);
        integratorTarg.getMoveManager().addMCMove(moveTarg);
		//integratorTarg.getMoveManager().addMCMove(rotateTarg);
        integratorTarg.getMoveManager().addMCMove(rotateFixedAngle);
	    
		integratorTarg.setBox(boxTarg);

        // Reference System
		PotentialMasterListMolecular potentialMasterRef = 
			new PotentialMasterListMolecular(this, rc, boxAgentSourceRef, boxAgentManagerRef, new NeighborListManagerSlantyMolecular.NeighborListSlantyAgentSourceMolecular(rc, space), space);
		potentialMasterRef.addPotential(potentialTarg, new ISpecies[]{species, species});
		potentialMasterRef.addPotential(pRotConstraintTarg,new ISpecies[]{species} );
		
		int cellRange = 6;
	    potentialMasterTarg.setRange(rc);
	    potentialMasterTarg.setCellRange(cellRange); 
	    potentialMasterTarg.getNeighborManager(boxTarg).reset();

	        
//	    int potentialCells = potentialMasterTarg.getNbrCellManager(boxTarg).getLattice().getSize()[0];
//	    if (potentialCells < cellRange*2+1) {
//	      throw new RuntimeException("oops ("+potentialCells+" < "+(cellRange*2+1)+")");
//	    }
	    potentialMasterRef.setRange(rc);
	    potentialMasterRef.setCellRange(cellRange); 
	    potentialMasterRef.getNeighborManager(boxTarg).reset();
	    potentialTarg.setRange(Double.POSITIVE_INFINITY); 
	    
	    MeterPotentialEnergy meterPERef = new MeterPotentialEnergy(potentialMasterRef);
        meterPERef.setBox(boxTarg);
        double latticeEnergy = meterPERef.getDataAsScalar();
        System.out.println("lattice energy (sim unit): " + latticeEnergy);
		System.out.println("lattice energy per mol(sim unit): " + latticeEnergy/numMolecules);
		
		MeterBoltzmannDirect meterBoltzmann = new MeterBoltzmannDirect(integratorTarg, meterPERef);
		
//        MeterRotPerturbMolecule meterBoltzmannRotPerb = new MeterRotPerturbMolecule(integratorTarg, potentialMasterRef, species, space, this, coordinateDefTarg);
//        meterBoltzmannRotPerb.setLatticeEnergy(latticeEnergy);
		int numBlock = 100;
		int interval = numMolecules;
		long blockSize = numSteps/(numBlock*interval);
        if (blockSize == 0) blockSize = 1;
        System.out.println("block size "+blockSize+" interval "+interval);
        boltzmannAverage = new AccumulatorAverageFixed(blockSize);
        
        DataPump boltzmannPump = new DataPump(meterBoltzmann, boltzmannAverage);
        IntegratorListenerAction boltzmannPumpListener = new IntegratorListenerAction(boltzmannPump, 100);
        integratorTarg.getEventManager().addListener(boltzmannPumpListener);

        activityIntegrate = new ActivityIntegrate(integratorTarg);
        getController().addAction(activityIntegrate);
    }

    public void initializeConfigFromFile(String fname){
        ConfigurationFile config = new ConfigurationFile(fname);
        config.initializeCoordinates(boxTarg);
    }
    
    public void writeConfiguration(String fname){
        WriteConfiguration writeConfig = new WriteConfiguration(space);
        writeConfig.setBox(boxTarg);
        writeConfig.setDoApplyPBC(false);
        writeConfig.setConfName(fname);
        writeConfig.actionPerformed();
        System.out.println("\n***output configFile: "+ fname);
    }
    
    
    /**
     * @param args filename containing simulation parameters
     * @see SimDirectBetaN2RPAngleToNoAngle.SimOverlapParam
     */
    public static void main(String[] args) {

        double temperature = 45; //in UNIT KELVIN
        double density = 0.025;
        double angle =0.1;
        long numSteps = 1000000;
{'fraction_non_alphanumeric': 0.09819627388157114, 'fraction_numerical': 0.010264625608164234, 'mean_word_length': 5.004631278945493, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6649331', 'n_tokens_mistral': 5730, 'n_tokens_neox': 5185, 'n_words': 882}