<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.freeenergy.npath;

import etomica.action.BoxInflate;
import etomica.action.activity.ActivityIntegrate;
import etomica.api.*;
import etomica.box.Box;
import etomica.config.ConfigurationLattice;
import etomica.data.AccumulatorAverageFixed;
import etomica.data.DataPumpListener;
import etomica.data.IData;
import etomica.graphics.ColorScheme;
import etomica.graphics.SimulationGraphic;
import etomica.integrator.IntegratorMC;
import etomica.integrator.mcmove.MCMoveStepTracker;
import etomica.lattice.LatticeCubicFcc;
import etomica.nbr.cell.PotentialMasterCell;
import etomica.potential.P2LennardJones;
import etomica.potential.P2SoftSphericalTruncated;
import etomica.simulation.Simulation;
import etomica.space3d.Space3D;
import etomica.species.SpeciesSpheresMono;
import etomica.util.ParameterBase;
import etomica.util.ParseArgs;

import java.awt.*;

/**
 * Simple Lennard-Jones molecular dynamics simulation in 3D
 */
 
public class SimLJ extends Simulation {
    
    public final PotentialMasterCell potentialMasterCell;
    public final ActivityIntegrate ai;
    public IntegratorMC integrator;
    public SpeciesSpheresMono species;
    public IBox box;
    public P2LennardJones potential;
    public MCMoveAtomNPath mcMoveAtom;
    public MCMoveAtomCoupled mcMoveAtomCoupled;
    public MCMoveAtomSwap mcMoveSwap;
    public P1ImageHarmonic p1ImageHarmonic;

    public SimLJ(int numAtoms, double temperature, double density, double rc, double w, int offsetDim) {
        super(Space3D.getInstance());
        species = new SpeciesSpheresMono(this, space);
        addSpecies(species);
        box = new Box(space);
        addBox(box);
        box.setNMolecules(species, numAtoms);
        IVectorMutable l = space.makeVector();
        l.E(10);
        for (int i=0; i<=offsetDim; i++) {
            l.setX(i,20);
        }
        box.getBoundary().setBoxSize(l);

        BoxInflate inflater = new BoxInflate(box, space);
        inflater.setTargetDensity(density);
        inflater.actionPerformed();
        
        potentialMasterCell = new PotentialMasterCell(this, rc, space);
        potentialMasterCell.setCellRange(2);
        double sigma = 1.0;
        integrator = new IntegratorMC(this, potentialMasterCell);
        integrator.setTemperature(temperature);

        ai = new ActivityIntegrate(integrator);
        getController().addAction(ai);

        potential = new P2LennardJones(space, sigma, 1);
        IAtomType leafType = species.getLeafType();
        P2SoftSphericalTruncated potentialTruncated = new P2SoftSphericalTruncated(space, potential, rc);

        potentialMasterCell.addPotential(potentialTruncated,new IAtomType[]{leafType,leafType});

        IVectorMutable offset = space.makeVector();
        offset.setX(offsetDim, box.getBoundary().getBoxSize().getX(offsetDim)*0.5);
        p1ImageHarmonic = new P1ImageHarmonic(space, offset, w);
        potentialMasterCell.addPotential(p1ImageHarmonic, new IAtomType[]{leafType});

        mcMoveAtom = new MCMoveAtomNPath(random, potentialMasterCell, space, p1ImageHarmonic);
        integrator.getMoveManager().addMCMove(mcMoveAtom);
        mcMoveAtomCoupled = new MCMoveAtomCoupled(random, potentialMasterCell, space);
        integrator.getMoveManager().addMCMove(mcMoveAtomCoupled);
//        ((MCMoveStepTracker)mcMoveAtom.getTracker()).setNoisyAdjustment(true);
//        ((MCMoveStepTracker)mcMoveAtomCoupled.getTracker()).setNoisyAdjustment(true);

        IVector boxLength = box.getBoundary().getBoxSize();
        double lMin = boxLength.getX(0);
        if (boxLength.getX(1) < lMin) lMin = boxLength.getX(1);
        if (boxLength.getX(2) < lMin) lMin = boxLength.getX(2);
        double ww = w / lMin;
        double swapDistance = 5*Math.sqrt(1.5*temperature/ww);
        if (swapDistance > lMin/4) swapDistance = lMin/4;
        if (swapDistance < 1) swapDistance = 1;
        mcMoveSwap = new MCMoveAtomSwap(random, potentialMasterCell, space, p1ImageHarmonic);
        mcMoveSwap.setNbrDistance(swapDistance);
        integrator.getMoveManager().addMCMove(mcMoveSwap);
        integrator.getMoveManager().setFrequency(mcMoveSwap,5);

        integrator.setBox(box);

        integrator.getMoveEventManager().addListener(potentialMasterCell.getNbrCellManager(box).makeMCMoveListener());
		
        ConfigurationLattice configuration = new ConfigurationLattice(new LatticeCubicFcc(space), space);
        configuration.initializeCoordinates(box);
        potentialMasterCell.getNbrCellManager(box).assignCellAll();
    }
    
    public static void main(String[] args) {

        LjMC3DParams params = new LjMC3DParams();
        ParseArgs.doParseArgs(params, args);
        if (args.length==0) {
            params.graphics = false;
            params.numAtoms = 1000;
            params.steps = 1000000;
            params.density = 1.3;
            params.T = 2;
            params.w = 500;
        }

        final int numAtoms = params.numAtoms;
        final double temperature = params.T;
        final double density = params.density;
        long steps = params.steps;
        double rc = params.rc*Math.pow(params.density, -1.0/3.0);
        boolean graphics = params.graphics;
        double w = params.w;
        int offsetDim = params.offsetDim;

        if (!graphics) {
            System.out.println("Running LJ MC with N="+numAtoms+" at rho="+density+" T="+temperature);
            System.out.println(steps+" steps");
            System.out.println("cutoff: "+rc);
            System.out.println("w: "+w);
        }

        double L = Math.pow(numAtoms/density, 1.0/3.0);
        final SimLJ sim = new SimLJ(numAtoms, temperature, density, rc, w, offsetDim);

        DataSourceEnergies dsEnergies = new DataSourceEnergies(sim.potentialMasterCell);
        dsEnergies.setBox(sim.box);
        IData u = dsEnergies.getData();
        if (!graphics) System.out.println("LJ lattice energy: "+u.getValue(1)/numAtoms);

        if (graphics) {
            final String APP_NAME = "SimLJ";
            final SimulationGraphic simGraphic = new SimulationGraphic(sim, SimulationGraphic.TABBED_PANE, APP_NAME, 3, sim.getSpace(), sim.getController());
            ColorScheme colorScheme = new ColorScheme() {
                @Override
                public Color getAtomColor(IAtom a) {
                    return a.getLeafIndex() < numAtoms/2 ? Color.RED : Color.BLUE;
                }
            };
            simGraphic.getDisplayBox(sim.box).setColorScheme(colorScheme);
            simGraphic.getController().getReinitButton().setPostAction(simGraphic.getPaintAction(sim.box));

            simGraphic.makeAndDisplayFrame(APP_NAME);

            return;
        }<commit_msg>p1 needs to find its N offset tweak max swap distance add energy plots<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.freeenergy.npath;

import etomica.action.BoxInflate;
import etomica.action.activity.ActivityIntegrate;
import etomica.api.*;
import etomica.box.Box;
import etomica.config.ConfigurationLattice;
import etomica.data.*;
import etomica.graphics.ColorScheme;
import etomica.graphics.DisplayPlot;
import etomica.graphics.SimulationGraphic;
import etomica.integrator.IntegratorMC;
import etomica.integrator.mcmove.MCMoveStepTracker;
import etomica.lattice.LatticeCubicFcc;
import etomica.nbr.cell.PotentialMasterCell;
import etomica.potential.P2LennardJones;
import etomica.potential.P2SoftSphericalTruncated;
import etomica.simulation.Simulation;
import etomica.space3d.Space3D;
import etomica.species.SpeciesSpheresMono;
import etomica.units.CompoundUnit;
import etomica.units.Null;
import etomica.units.SimpleUnit;
import etomica.units.Unit;
import etomica.util.HistoryCollapsingAverage;
import etomica.util.ParameterBase;
import etomica.util.ParseArgs;

import java.awt.*;

/**
 * Simple Lennard-Jones molecular dynamics simulation in 3D
 */
 
public class SimLJ extends Simulation {
    
    public final PotentialMasterCell potentialMasterCell;
    public final ActivityIntegrate ai;
    public IntegratorMC integrator;
    public SpeciesSpheresMono species;
    public IBox box;
    public P2LennardJones potential;
    public MCMoveAtomNPath mcMoveAtom;
    public MCMoveAtomCoupled mcMoveAtomCoupled;
    public MCMoveAtomSwap mcMoveSwap;
    public P1ImageHarmonic p1ImageHarmonic;

    public SimLJ(int numAtoms, double temperature, double density, double rc, double w, int offsetDim) {
        super(Space3D.getInstance());
        species = new SpeciesSpheresMono(this, space);
        addSpecies(species);
        box = new Box(space);
        addBox(box);
        box.setNMolecules(species, numAtoms);
        IVectorMutable l = space.makeVector();
        l.E(10);
        for (int i=0; i<=offsetDim; i++) {
            l.setX(i,20);
        }
        box.getBoundary().setBoxSize(l);

        BoxInflate inflater = new BoxInflate(box, space);
        inflater.setTargetDensity(density);
        inflater.actionPerformed();
        
        potentialMasterCell = new PotentialMasterCell(this, rc, space);
        potentialMasterCell.setCellRange(2);
        double sigma = 1.0;
        integrator = new IntegratorMC(this, potentialMasterCell);
        integrator.setTemperature(temperature);

        ai = new ActivityIntegrate(integrator);
        getController().addAction(ai);

        potential = new P2LennardJones(space, sigma, 1);
        IAtomType leafType = species.getLeafType();
        P2SoftSphericalTruncated potentialTruncated = new P2SoftSphericalTruncated(space, potential, rc);

        potentialMasterCell.addPotential(potentialTruncated,new IAtomType[]{leafType,leafType});

        IVectorMutable offset = space.makeVector();
        offset.setX(offsetDim, box.getBoundary().getBoxSize().getX(offsetDim)*0.5);
        p1ImageHarmonic = new P1ImageHarmonic(space, offset, w);
        potentialMasterCell.addPotential(p1ImageHarmonic, new IAtomType[]{leafType});

        mcMoveAtom = new MCMoveAtomNPath(random, potentialMasterCell, space, p1ImageHarmonic);
        integrator.getMoveManager().addMCMove(mcMoveAtom);
        mcMoveAtomCoupled = new MCMoveAtomCoupled(random, potentialMasterCell, space);
        integrator.getMoveManager().addMCMove(mcMoveAtomCoupled);
//        ((MCMoveStepTracker)mcMoveAtom.getTracker()).setNoisyAdjustment(true);
//        ((MCMoveStepTracker)mcMoveAtomCoupled.getTracker()).setNoisyAdjustment(true);

        IVector boxLength = box.getBoundary().getBoxSize();
        double lMin = boxLength.getX(0);
        if (boxLength.getX(1) < lMin) lMin = boxLength.getX(1);
        if (boxLength.getX(2) < lMin) lMin = boxLength.getX(2);
        double ww = w / lMin;
        double swapDistance = 1.5*Math.sqrt(1.5*temperature/ww);
        if (swapDistance > lMin/4) swapDistance = lMin/4;
        if (swapDistance < 1) swapDistance = 1;
        mcMoveSwap = new MCMoveAtomSwap(random, potentialMasterCell, space, p1ImageHarmonic);
        mcMoveSwap.setNbrDistance(swapDistance);
        integrator.getMoveManager().addMCMove(mcMoveSwap);
        integrator.getMoveManager().setFrequency(mcMoveSwap,5);

        integrator.setBox(box);

        integrator.getMoveEventManager().addListener(potentialMasterCell.getNbrCellManager(box).makeMCMoveListener());
		
        ConfigurationLattice configuration = new ConfigurationLattice(new LatticeCubicFcc(space), space);
        configuration.initializeCoordinates(box);
        potentialMasterCell.getNbrCellManager(box).assignCellAll();
        p1ImageHarmonic.findNOffset(box);
    }
    
    public static void main(String[] args) {

        LjMC3DParams params = new LjMC3DParams();
        ParseArgs.doParseArgs(params, args);
        if (args.length==0) {
            params.graphics = false;
            params.numAtoms = 1000;
            params.steps = 1000000;
            params.density = 1.3;
            params.T = 2;
            params.w = 500;
        }

        final int numAtoms = params.numAtoms;
        final double temperature = params.T;
        final double density = params.density;
        long steps = params.steps;
        double rc = params.rc*Math.pow(params.density, -1.0/3.0);
        boolean graphics = params.graphics;
        double w = params.w;
        int offsetDim = params.offsetDim;

        if (!graphics) {
            System.out.println("Running LJ MC with N="+numAtoms+" at rho="+density+" T="+temperature);
            System.out.println(steps+" steps");
            System.out.println("cutoff: "+rc);
            System.out.println("w: "+w);
        }

        double L = Math.pow(numAtoms/density, 1.0/3.0);
        final SimLJ sim = new SimLJ(numAtoms, temperature, density, rc, w, offsetDim);

        DataSourceEnergies dsEnergies = new DataSourceEnergies(sim.potentialMasterCell);
        dsEnergies.setBox(sim.box);
        IData u = dsEnergies.getData();
        if (!graphics) System.out.println("LJ lattice energy: "+u.getValue(1)/numAtoms);

        if (graphics) {
            final String APP_NAME = "SimLJ";
            final SimulationGraphic simGraphic = new SimulationGraphic(sim, SimulationGraphic.TABBED_PANE, APP_NAME, 3, sim.getSpace(), sim.getController());
            ColorScheme colorScheme = new ColorScheme() {
                @Override
                public Color getAtomColor(IAtom a) {
                    return a.getLeafIndex() < numAtoms/2 ? Color.RED : Color.BLUE;
                }
            };
            simGraphic.getDisplayBox(sim.box).setColorScheme(colorScheme);
            simGraphic.getController().getReinitButton().setPostAction(simGraphic.getPaintAction(sim.box));
            
            DataSourceCountSteps tSource = new DataSourceCountSteps(sim.integrator);
            AccumulatorHistory energyHist = new AccumulatorHistory(new HistoryCollapsingAverage());
            energyHist.setTimeDataSource(tSource);
            AccumulatorHistory springHist = new AccumulatorHistory(new HistoryCollapsingAverage());
            springHist.setTimeDataSource(tSource);
            DataSplitter splitter = new DataSplitter();
            DataPumpListener energyPump = new DataPumpListener(dsEnergies, splitter, numAtoms);
            sim.integrator.getEventManager().addListener(energyPump);
            splitter.setDataSink(0, springHist);
            splitter.setDataSink(1, energyHist);
            DisplayPlot energyPlot = new DisplayPlot();
            energyPlot.setLabel("LJ");
            energyPlot.setUnit(new CompoundUnit(new Unit[]{new SimpleUnit(Null.DIMENSION,1.0/numAtoms,"why do you want a name.  just use it.","per atom", false)},new double[]{-1}));
//            energyPlot.setUnit(new CompoundUnit(new Unit[]{ElectronVolt.UNIT, new SimpleUnit(Null.DIMENSION,1.0/numAtoms,"why do you want a name.  just use it.","per atom", false)},new double[]{1,-1}));
//            energyPlot.setUnit(ElectronVolt.UNIT);
            energyHist.addDataSink(energyPlot.getDataSet().makeDataSink());
            simGraphic.add(energyPlot);
            DisplayPlot springPlot = new DisplayPlot();
            springPlot.setLabel("spring");
            springPlot.setUnit(new CompoundUnit(new Unit[]{new SimpleUnit(Null.DIMENSION,1.0/numAtoms,"why do you want a name.  just use it.","per atom", false)},new double[]{-1}));
            springHist.addDataSink(springPlot.getDataSet().makeDataSink());
            simGraphic.add(springPlot);
    
            simGraphic.makeAndDisplayFrame(APP_NAME);

            return;
        }
{'fraction_non_alphanumeric': 0.10345913931069246, 'fraction_numerical': 0.010522336336714762, 'mean_word_length': 3.179041600842549, 'pattern_counts': {'":': 0, '<': 13, '<?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': '5711248', 'n_tokens_mistral': 4962, 'n_tokens_neox': 4752, 'n_words': 940}