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
|
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
using namespace std;
struct particle{
double x;
double y;
};
vector <particle> readin_file(string filename){
ifstream inFile;
inFile.open(filename);
if(inFile.fail()){ // Error test
cerr<< "Error opening file"<<endl;
exit(1);
}
vector <particle> p;
double x,y;
while(inFile >> x >> y){
p.push_back({x,y});
}
inFile.close();
return p;
}
void calculate_forces(vector <particle>& F, vector <particle> image_pos, double R, double L, double& Epot){
//also calculate Epot in this function
double r_quadr;
double rx;
double ry;
double f_abs;
Epot=0;
//reset force vector
for(int i=0;i<image_pos.size();i++){
F.at(i)={0,0};
}
//calculate forces
for(int i=0;i<image_pos.size();i++){
for(int j=i+1; j<image_pos.size();j++){
rx=image_pos.at(i).x-image_pos.at(j).x; //distances between particles
ry=image_pos.at(i).y-image_pos.at(j).y;
if(rx>=L/2){
rx-=L;
image_pos.at(j).x+=L; //imaging particle to neighbouring box
}
else if(rx<=-L/2){
rx+=L;
image_pos.at(j).x-=L;
}
if(ry>=L/2){
ry-=L;
image_pos.at(j).y+=L;
}
else if(ry<=-L/2){
ry+=L;
image_pos.at(j).y-=L;
}
r_quadr=rx*rx+ry*ry;
if(r_quadr<=(R*R)){ //calculate forces
f_abs=48*pow(r_quadr,-7)-24*pow(r_quadr,-4);
F.at(i).x+=rx*f_abs;
F.at(i).y+=ry*f_abs;
F.at(j).x-=rx*f_abs; //Newton's 3rd law
F.at(j).y-=ry*f_abs;
Epot+=4*(pow(r_quadr,-6)-pow(r_quadr,-3))+1;
}
}
}
}
void update_pos(vector <particle>& pos, vector <particle>& vel, vector <particle>& F, double dt, double L){
for (int i=0;i<pos.size();i++){
pos.at(i).x+=dt*vel.at(i).x+(1./2)*dt*dt*F.at(i).x;
if(pos.at(i).x<0) pos.at(i).x+=L; //periodic boundaries
else if(pos.at(i).x>=L)pos.at(i).x-=L;
pos.at(i).y+=dt*vel.at(i).y+(1./2)*dt*dt*F.at(i).y;
if(pos.at(i).y<0) pos.at(i).y+=L;
else if(pos.at(i).y>=L)pos.at(i).y-=L;
}
}
void update_vel(vector <particle>& vel, vector <particle> F, double dt){
for (int i=0; i<vel.size();i++){
vel.at(i).x+=(1./2)*dt*F.at(i).x;
vel.at(i).y+=(1./2)*dt*F.at(i).y;
}
//cout <<"update velocities worked for i= "<<i<<endl;
}
void calculate_Ekin(vector <particle>& vel, double& Ekin){
Ekin=0;
for(int i=0;i<vel.size();i++){
Ekin+=(1./2)*(vel.at(i).x*vel.at(i).x + vel.at(i).y*vel.at(i).y);
}
}
int main(){
double dt=0.01;
double R=pow(2,1./6);
double L=14;
double Ekin;
double Epot;
string pos_dat="positions.txt";
string vel_dat="velocities.txt";
vector <particle> pos=readin_file(pos_dat);
vector <particle> vel=readin_file(vel_dat);
vector <particle> F(pos.size());
calculate_forces(F,pos,R,L,Epot);
calculate_Ekin(vel, Ekin);
//prepare output
ofstream E_kin("E_kin_dt="+to_string(dt)+".dat");
E_kin<< 0<<" "<< Ekin<<endl;
ofstream E_pot("E_pot_dt="+to_string(dt)+".dat");
E_pot<< 0<<" "<< Epot<<endl;
ofstream E_tot("E_tot_dt="+to_string(dt)+".dat");
E_tot<< 0<< " "<<Ekin+Epot<<endl;
//simulation step - velocity-Verlet
for(int i=1; i<=10000;i++){
update_pos(pos,vel,F,dt,L);
update_vel(vel,F,dt);
calculate_forces(F,pos,R,L,Epot);
update_vel(vel,F,dt);
calculate_Ekin(vel, Ekin);
E_kin<< i*dt<<" "<<Ekin<<endl;
E_pot<< i*dt<<" "<<Epot<<endl;
E_tot<<i*dt<<" "<<Ekin+Epot<<endl;
}
E_kin.close();
E_pot.close();
E_tot.close();
}
|