1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
#include <iostream>
#include <cmath>
#include <fstream>
#include "matrix.h"
#include <stdlib.h>
#include <time.h>//Header of time to initiate the seed
#define pi 3.14159265359897
using namespace std;
// The main functions are defined at the beginning of the
//program. The header file "matrix.h" includes the class matrix to work with matrices by using different operations
double* v_rand_MB(int npart, double a){ //arguments are # of particles and parameter "a", related to temperature of the gas
int count;//counts the total number of accepted random numbers from the distribution
count=0;
//Pointer to the array of velocities
double* myp;
double interval,bb,vmax,Pd,Ptest,num,den,alp;
vmax=5.0*a;
interval=abs(vmax);
myp=new double[npart];//The pointer points towards an npart dimensional array of type double
//loop for generation of random numbers
while(count<npart){
//Generate a random number on the desired interval.
bb=rand()/(1.0*RAND_MAX);
bb=interval*bb;
//Probability density, i.e., MB distribution
den=sqrt(2.0/pi)/(1.0*pow(a,3));
alp=1.0/(2*pow(a,2));
num=pow(bb,2)*(exp(-alp*pow(bb,2)));
Pd=den*num; //probability associated to the random number bb
Ptest=rand()/(1.0*RAND_MAX);
if(Ptest<Pd){
myp[count]=bb;
count=count+1;
}
}
return myp;
}
//function that fills the initial velocities matrix
void initiate_velocities(matrix& m, double* myp){
//generate random numbers for theta and phi
double theta,phi,px,py,pz;
int dimr; //dimensions of rows and columns of matrix
dimr=m.nrows();
for(int i=0;i<dimr;i++){
//generate random angles
theta=(rand()/(1.0*RAND_MAX))*pi;
phi=(rand()/(1.0*RAND_MAX))*(2*pi);
//variables to fill the matrix
px=myp[i]*(sin(theta)*cos(phi));
py=myp[i]*(sin(theta)*sin(phi));
pz=myp[i]*cos(theta);
//components one by one, filled with the appropiate value
//x component
m.fill(px,i,0);
//y component
m.fill(py,i,1);
//z component
m.fill(pz,i,2);
}
//ERASE THIS LINE WHEN PROGRAM IS COMPLETED
//m.printm();
}
//generate position of particles
void initiate_positions(matrix& m,int Npart){
int count,ylim,zlim;
double density,epsilon,dx,x,y,z;//density and minimum radius associated to each particle, and epsilon is the value in order to avoid one particle lying on one of
//the faces of the cube;
epsilon=0.1;
//The box lives in the interval -1/2 + 1/2 for each coordinate.
density=1.0/(Npart);
//We need to generate NxNxN positions (a cube). We start by creating the points in the z=+epsilon level, and we rise them till z= 1-epsilon.
dx=0.8/(49.0); //element separating two points in the initial positions cube
matrix rowpos(50,1);
x=0.0;
for(int i=0;i<50;i++){
rowpos.fill(x,i,0);
x=x+dx;
}
z=0;//Initialize at z=0 plane
count=0;
y=0;
ylim=49;
zlim=2499;
for(int k=0;k<Npart;k++){
m.fill(rowpos.get(count,0),k,0);
m.fill(y,k,1);//fill y and z values
m.fill(z,k,2);
if(k==ylim){
ylim=ylim+50;
y=y+dx;
}
if(k==zlim){
zlim=zlim+2500;
y=0.0;
z=z+dx;
}
count=count+1;
if(count==50){
count=0;
}
}
}
//store initial data in .txt file
void store1(matrix& m1,matrix& m2,int Npart){
ofstream dataR("pos_data.txt");//store positions in txt file
ofstream dataV("vel_data.txt");//store velocities
for(int i=0;i<Npart;i++){
dataR<<m1.get(i,0)<<" "<<m1.get(i,1)<<" "<<m1.get(i,2);
dataR<<endl;
dataV<<m2.get(i,0)<<" "<<m2.get(i,1)<<" "<<m2.get(i,2);
dataV<<endl;
}
}
//function that gets the matrix of relative distances between particles, therefore giving the forces between each of them
double forces_matrix(matrix& r0,matrix& forces,double gamma,double beta,int Npart){
double modulus,F;//modulus and force
for(int i=0;i<Npart;i++){
for(int j=0;j<Npart;j++){
if(i==j){
forces.fill(1000000,i,j); //a particle doesnt interact with itself, so the force with this choice will be zero
}
modulus=sqrt( pow(r0.get(i,0)-r0.get(j,0),2) + pow(r0.get(i,1)-r0.get(j,1),2) +pow(r0.get(i,2)-r0.get(j,2),2));
}
}
}
int main(){
srand(time(NULL));//Initialize seed for random numbers
//Definition of variables/objects
int Npart,npt,Nmax; //Total number of particles, total number of time frames, maximum number of particles allowed
double tf,dt,a,gamma,beta;//Final time, differential of time, temp.parameter MB distribution. The parameters gamma and r0 belong to the
//Buckinham potential, and must be chosen carefully.
gamma=0.2;
beta=0.02;
double* vpoint;//Pointer to velocities distribution
cout<<"Welcome to the C++ program for Molecular Dynamics simulation. Introduce how many particles you want to work with: ";
cin>>Npart;
Nmax=125000; //The maximum allowed size of the cube is 50x50x50
while(Npart>Nmax){
cout<<"Sorry, number of particles is too big, please reduce it ";
cin>>Npart;
}
cout<<"Now choose the value of the parameter a>0 , associated to the temperature: ";
cin>>a;
while(a<0){
cout<<"Sorry, but the parameter 'a' needs to be positive, try again: ";
cin>>a;
}
cout<<endl<<"Include now the total number of time steps you want to take for the simulation: ";
cin>>npt;
//The initial positions and velocities of the particles. Lets use a Maxwell-Boltzmann distribution for the velocities, whereas the position of particles
//should be occupying vertices of a cube in space.
//Matrices of initial positions and positions at time t, and for velocities., and the matrix of relative distances between particles, relevant for the
//forces acting over each individual particle
matrix r0(Npart,3),rt(Npart,3),v0(Npart,3),vt(Npart,3),forces(Npart,Npart);
//Generate the array of random velocities
vpoint=v_rand_MB(Npart,a);
//Generate matrix of random velocities components
initiate_velocities(v0,vpoint);
cout<<"The initial velocities have been allocated in a matrix. "<<endl<<endl;
initiate_positions(r0,Npart);
store1(r0,v0,Npart);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
r0.clean();
v0.clean();
rt.clean();
vt.clean();
forces.clean();
delete[] vpoint;//Free memory block
cout<<"End of the program "<<endl;
return 0;
}
|