【发布时间】:2018-05-15 18:25:56
【问题描述】:
我模拟了一个 2D 粒子系统,它们相互吸引。吸引力的强度取决于粒子之间的距离。边界条件和相互作用是周期性的。由于吸引力,粒子相互靠近并聚集成一个圆圈。
我想添加硬球斥力,这样,每当两个或多个粒子聚集在同一位置时,我希望它们在连接它们中心的线上分开,直到它们不重叠。我该怎么做?
当存在吸引相互作用时添加硬球的情况比通常情况下更难,因为在某些情况下,同一位置可能有 4 个或更多粒子。
这是我的代码:
#include <iostream>
#include <math.h>
#include <vector>
#include <array>
#include <list>
#include <random>
#include <functional>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <chrono>
#include <set>
using namespace std;
std::minstd_rand gen(std::random_device{}());
std::uniform_real_distribution<double> unirnd(0, 1);
double PBCtwo(double pos, double L)
{
if(pos > L / 2.0)
return pos-L;
else if (pos < -L /2.0)
return L + pos;
else
return pos;
}
// main function
int main()
{ long c = 0;
int N=4000;
double rho, v0, tr,xr,l0, eta,dt, x[N],y[N],L=pow(N / rho , 0.5),l0_two = l0 * l0;
rho = 2;
v0 =300;eta = 1;dt = 0.0001;l0 = 1; c_prod = 500;c_display = 100;tr = -0.4;
// write initial configuration to the file
ofstream configFile;
configFile.open ("Initial Configuration.txt");
configFile << to_string(N) << "\n";
configFile << to_string(L) << "\n";
for (int i = 0; i < N; i++)
{ x[i] = unirnd(gen) * L;
y[i] = unirnd(gen) * L;
configFile << to_string(x[i]) << "\t" << to_string(y[i]) << "\n";
}
configFile.close();
while (c < c_prod)
{
double dx[N], dy[N];
c++;
for(int i = 0; i < N; i++)
{
dx[i] = 0;
dy[i] = 0;
double S_try = 0.0, S_trx = 0.0;
for(int j = 0; j < N; j++)
{
if (j==i) continue;
double delta_x = x[i]-x[j],
delta_y = y[i]-y[j];
double r_x_ij = PBCtwo(delta_x,L),
r_y_ij = PBCtwo(delta_y,L),
r_ij_square = r_x_ij * r_x_ij + r_y_ij * r_y_ij;
if (r_ij_square > l0_two)
{
double r_ij = sqrt(r_ij_square);
r_x_ij/= r_ij;
r_y_ij/= r_ij;
double S_tr = 1 /r_ij_square;
S_trx += r_x_ij * S_tr;
S_try += r_y_ij * S_tr;
}
}
dx[i] += tr * S_trx;
dy[i] += tr * S_try;
}
for(int i = 0; i < N; i++)
{
x[i]+= dt * dx[i];
y[i]+= dt * dy[i];
if (x[i] > L){
x[i]-= L;}
else if( x[i] < 0) {
x[i]+= L;}
if (y[i] > L){
y[i]-= L;}
else if( y[i] < 0){
y[i]+= L;}
}
}
ofstream finalConfigFile;
finalConfigFile.open ("Final Configuration.txt");
finalConfigFile << to_string(N) << "\n";
finalConfigFile << to_string(L) << "\n";
for (int i = 0; i < N; i++)
{
finalConfigFile << to_string(x[i]) << "\t" << to_string(y[i]) <<"\n";
}
finalConfigFile.close();
return 0;
}
【问题讨论】:
-
-1是什么原因?
-
可能是idownvotedbecau.se/nomcve 或者是因为您的文字墙问题陈述或缺少显示特定输入、可重现输出和解释的 特定 测试用例它应该是什么,或者因为丑陋的,随机缩进的未注释代码?
-
我没有投反对票,但很难不投反对票,因为代码写得不好。如果您需要代码方面的帮助,人们阅读起来应该不会很痛苦。我的意思是,它的格式甚至都始终非常糟糕;几乎每条线路都有自己独特的随意品牌,看似莫名的丑陋。此外,一方面,使用非
constexpr初始化器声明的数组不是标准 C++。 -
代码已编辑。现在好了吗? @无用
标签: c++ c++11 simulation