void MDcalculation::calculateForces(bool monitoring){
	V = 0;
	ParticleList* list;
	//Calculating weights, finding 2BF and 3BF particle lists
	findNeighbours();
	// return;
	//Calculating 2BF and 3BF
	for(int i = 0; i < Nx + 2; i++){
	for(int j = 0; j < Ny + 2; j++){
	for(int k = 0; k < Nz + 2; k++){
		list = cells[i][j][k];
		if(i != 0 && i != Nx + 1 && j != 0 && j != Ny + 1 && k != 0 && k != Nz + 1){
			//Internal 3BF calculation
			for(Particle* p = list->getFirst(); list->hasNext(); p = list->getNext()){
				V += potential->force2(p);
				V += potential->force3(p);
			}
		}else{
			//3BF for boundary with internal
			for(Particle* p = list->getFirst(); list->hasNext(); p = list->getNext()){
				V += potential->boundaryForce3(p);
			}
		}
	}
	}
	}
}
void MDcalculation::calculateMeanDisplacement(){
	//Resetting
	for(int t = 0; t < 3; t++){
		rMeanSquared[t] = 0;
	}
	ParticleList* list;
	for(int i = 1; i < Nx + 1; i++){
	for(int j = 1; j < Ny + 1; j++){
	for(int k = 1; k < Nz + 1; k++){
		list = cells[i][j][k];
		for(Particle* p = list->getFirst(); list->hasNext(); p = list->getNext()){
			for(int d = 0; d < 3; d++){
				rMeanSquared[p->type] += p->rReal[d]*p->rReal[d];
			}
		}
	}
	}
	}
	MPI_Reduce(rMeanSquared, rMeanSquaredSum, 3, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
	if(rank == 0){
		for(int t = 0; t < 3; t++){
			if(NparticlesType[t] == 0){
				rMeanSquaredSum[t] = 0;
			}else{
				rMeanSquaredSum[t] = rMeanSquaredSum[t]/NparticlesType[t];
			}
		}
	}
}
void MDcalculation::thermostat(double dt, double T0, double tau, double T){
	double gamma = sqrt(1 + dt/tau*(T0/T - 1));
	//Scaling velocities
	ParticleList* list;
	for(int i = 1; i <= Nx; i++){
	for(int j = 1; j <= Ny; j++){
	for(int k = 1; k <= Nz; k++){
		list = cells[i][j][k];
		for(Particle* p = list->getFirst(); list->hasNext(); p = list->getNext()){
			for(int d = 0; d < 3; d++){
				p->v[d] = p->v[d]*gamma;
			}
		}
	}
	}
	}
} 
void MDcalculation::calculateKineticEnergy(){
	K = 0;
	double Ksum;
	double v2;
	ParticleList* list;
	for(int i = 1; i < Nx + 1; i++){
	for(int j = 1; j < Ny + 1; j++){
	for(int k = 1; k < Nz + 1; k++){
		list = cells[i][j][k];
		for(Particle* p = list->getFirst(); list->hasNext(); p = list->getNext()){
			if(container->mark[p->id]){
				continue;
			}
			v2 = p->v[0]*p->v[0] + p->v[1]*p->v[1] + p->v[2]*p->v[2];
			K += potential->m[p->type]*v2;
		}
	}
	}
	}
	K = 0.5*K;
	MPI_Allreduce(&K, &Ksum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
	K = Ksum;
}