Hi MikeyBoy and jonnin,
thanks for your explanation.
Unfortunately I am doomed to learning by doing, I am writing simulations for computational physics and aside from a few basics, I don't know anything about C/C++.
I try to read through a C++ book, but I am not that far there yet^^"
I finished my programm and during runtime, there seems to be an "out of range" error for a vector. After commenting-out other parts, it seems the error comes from the part which I wanted to do in this thread.
I created a 2d-Array whose elements are vectors.
Then there is lots of code.
pos is a vector, image_pos is a copy of pos. Anything else like L or rx are just double variables.
Can someone see easily, whether there is an 'out of range' error?
The error message is like:
"throwing instance 'out of range', what() vector::M_range_check__n (which is 0)>=this->size() (which is 0)
So some vector has 0 size?
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
|
//matrix storing the 2d forces between all particles
vector <double> **F_ij=new vector <double> *[pos.size()];
for(int i=0;i<pos.size();i++){
F_ij[i]=new vector <double>[pos.size()];
}
//filling the matrix
for(int i=0;i<pos.size();i++){
F_ij[i][i]={0,0}; //no force of particle on itself
for(int j=0; j<i;j++){
F_ij[i][j]={-F_ij[j][i].at(0),-F_ij[j][i].at(1)}; //Newton's 3rd law
}
for(int j=i+1; j<pos.size();j++){
rx=image_pos.at(i).at(0)-image_pos.at(j).at(1); //distances between particles
ry=image_pos.at(i).at(1)-image_pos.at(j).at(1);
if(rx>=L/2){
rx-=L;
image_pos.at(j).at(0)+=L; //imaging particle to neighbouring box
}
else if(rx<=-L/2){
rx+=L;
image_pos.at(j).at(0)-=L;
}
if(ry>=L/2){
ry-=L;
image_pos.at(j).at(1)+=L;
}
else if(ry<=-L/2){
ry+=L;
image_pos.at(j).at(1)-=L;
}
r=sqrt(pow(rx,2)+pow(ry,2));
if(r<=R){ //calculate forces
f_abs=48*pow(r,-14)-24*pow(r,-8);
fx=f_abs*(image_pos.at(i).at(0)-image_pos.at(j).at(0));
fy=f_abs*(image_pos.at(i).at(1)-image_pos.at(j).at(1));
F_ij[i][j]={fx, fy};
Epot+=4*(pow(r,-12)-pow(r,-6))+1;
}
}
|
Btw. it deals with N interacting particles in a box under periodic boundary conditions, just in case one is interested :)