Example #1
0
        public static void Main(string[] args)
        {
            //WriteData myData = new WriteData();

            StaticVariables.myEnvironment = new CreateEnvironment();
            StaticVariables.myEnvironment.PreCompute();
            StaticVariables.myEnvironment.InitAtoms();

            if (StaticVariables.currentPotential == StaticVariables.Potential.LennardJones)
            {
                LennardJones.calculateVerletRadius();
            }
            if (StaticVariables.currentPotential == StaticVariables.Potential.Buckingham)
            {
                Buckingham.calculateVerletRadius();
            }

            Console.WriteLine("Number of atoms = " + StaticVariables.myEnvironment.numAtoms);

            float     totalTime = 20000.0f * StaticVariables.MDTimestep;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            while (StaticVariables.currentTime < totalTime)
            {
                PhysicsEngine.VelocityVerlet();
                PhysicsEngine.ReflectFromWalls();
                PhysicsEngine.CalculateEnergy();
                PairDistributionFunction.calculatePairDistribution();

                StaticVariables.currentTime = StaticVariables.currentTime + StaticVariables.MDTimestep;
                StaticVariables.iTime++;
                //myData.WritePosition();
                //Console.WriteLine("iTime = " + StaticVariables.iTime);
            }

            stopwatch.Stop();
            //myData.WritePairDistribution();
            Console.WriteLine("iTime = " + StaticVariables.iTime + "            Current Time = " + StaticVariables.currentTime);
            Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
            Console.ReadLine();
        }
Example #2
0
        public void PreCompute()
        {
            Atom.templateAtoms.Add(new Copper());
            Atom.templateAtoms.Add(new Gold());
            Atom.templateAtoms.Add(new Platinum());

            // delete the template atoms from the list of m_AllAtoms in Atom.cs
            for (int i = 0; i < Atom.templateAtoms.Count; i++)
            {
                Atom.AllAtoms.Clear();
            }

            if (StaticVariables.currentPotential == StaticVariables.Potential.LennardJones)
            {
                LennardJones.preCompute();
            }
            if (StaticVariables.currentPotential == StaticVariables.Potential.Buckingham)
            {
                Buckingham.preCompute();
            }
        }
        public static void CalculateEnergy()
        {
            StaticVariables.potentialEnergy    = 0.0f;
            StaticVariables.kineticEnergy      = 0.0f;
            StaticVariables.currentTemperature = 0.0f;

            for (int i = 0; i < Atom.AllAtoms.Count - 1; i++)
            {
                Atom firstAtom = Atom.AllAtoms[i];

                // calculate kinetic energy of each atom
                float velocitySqr = firstAtom.velocity[0] * firstAtom.velocity[0] + firstAtom.velocity[1] * firstAtom.velocity[1] + firstAtom.velocity[2] * firstAtom.velocity[2];
                StaticVariables.kineticEnergy += 0.5f * firstAtom.massamu * StaticVariables.amuToKg * velocitySqr * StaticVariables.angstromsToMeters * StaticVariables.angstromsToMeters;

                // calculate potential energy between each pair of atoms
                if (StaticVariables.currentPotential == StaticVariables.Potential.LennardJones)
                {
                    for (int j = 0; j < firstAtom.neighborList.Count; j++)
                    {
                        Atom secondAtom = firstAtom.neighborList[j];
                        StaticVariables.potentialEnergy += LennardJones.getPotential(firstAtom, secondAtom);
                    }
                }
                else if (StaticVariables.currentPotential == StaticVariables.Potential.Buckingham)
                {
                    for (int j = 0; j < firstAtom.neighborList.Count; j++)
                    {
                        Atom secondAtom = firstAtom.neighborList[j];
                        StaticVariables.potentialEnergy += Buckingham.getPotential(firstAtom, secondAtom);
                    }
                }
            }

            StaticVariables.currentTemperature = StaticVariables.kineticEnergy / 1.5f / (float)Atom.AllAtoms.Count / StaticVariables.kB;
            calculateSqrtAlpha();
        }
        public static void VelocityVerlet()
        {
            // update the position of all atoms then initialize the acceleration to be updated
            for (int i = 0; i < Atom.AllAtoms.Count; i++)
            {
                Atom currAtom = Atom.AllAtoms[i];
                for (int idx = 0; idx < 3; idx++)
                {
                    currAtom.position[idx] = currAtom.position[idx] + currAtom.velocity [idx] * StaticVariables.MDTimestep + 0.5f * StaticVariables.MDTimestepSqr * currAtom.accelerationNew [idx];
                }
                currAtom.accelerationOld = currAtom.accelerationNew;
                currAtom.accelerationNew = new float[3] {
                    0.0f, 0.0f, 0.0f
                };
            }

            if (StaticVariables.currentPotential == StaticVariables.Potential.LennardJones)
            {
                if (StaticVariables.iTime % StaticVariables.nVerlet == 0)
                {
                    LennardJones.calculateNeighborList();
                }
                // update the acceleration of all atoms
                for (int i = 0; i < Atom.AllAtoms.Count - 1; i++)
                {
                    Atom firstAtom = Atom.AllAtoms[i];
                    for (int j = 0; j < firstAtom.neighborList.Count; j++)
                    {
                        Atom secondAtom = firstAtom.neighborList[j];
                        LennardJones.getForce(firstAtom, secondAtom);
                    }
                }
            }
            else if (StaticVariables.currentPotential == StaticVariables.Potential.Buckingham)
            {
                if (StaticVariables.iTime % StaticVariables.nVerlet == 0)
                {
                    Buckingham.calculateNeighborList();
                }
                // update the acceleration of all atoms
                for (int i = 0; i < Atom.AllAtoms.Count - 1; i++)
                {
                    Atom firstAtom = Atom.AllAtoms[i];
                    for (int j = 0; j < firstAtom.neighborList.Count; j++)
                    {
                        Atom secondAtom = firstAtom.neighborList[j];
                        Buckingham.getForce(firstAtom, secondAtom);
                    }
                }
            }

            // update the velocity of all atoms
            for (int i = 0; i < Atom.AllAtoms.Count; i++)
            {
                Atom currAtom = Atom.AllAtoms[i];
                for (int idx = 0; idx < 3; idx++)
                {
                    currAtom.velocity[idx] = currAtom.velocity[idx] + 0.5f * (currAtom.accelerationOld[idx] + currAtom.accelerationNew[idx]) * StaticVariables.MDTimestep;
                    currAtom.velocity[idx] = currAtom.velocity[idx] * StaticVariables.sqrtAlpha;
                }
            }
        }