想象一下,在2D笛卡尔平面的每个坐标中都有一个粒子。每个粒子发射的物质在所有方向上都是分散的,基于贝塞尔函数的距离衰减,其他粒子各自吸收这种物质。因此,与给定颗粒相同距离的所有颗粒对该颗粒具有相同的影响。
之类的东西
我正在使用此代码计算此类互动:
编辑:31/03:两者的完整代码。
#include <stdio.h> // para as rotinas de entrada e saída
#include <stdlib.h> //
#include <stdarg.h> // para importar os elementos da linha de comando
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <time.h>
#include"ran1.c"
#include"bessel.c"
#define tmax 90000
#define N 50
#define beta 0.001
#define sigma 0.001
#define pi acos(-1.0)
#define trans 50000
#define epsilon 0.1
void condicoes_iniciais(double **xold,double **yold,double **a)
{
int l,j;
long idum=-120534;
for(l=0;l<= N; l++)
{
for(j=0;j<= N; j++)
{
a[l][j]=5.0;
}
}
for(l=0;l<= N; l++)
{
for(j=0;j<= N; j++)
{
while(a[l][j]>4.4)
a[l][j]=4.1+ran1(& idum);
}
}
for(l=0;l<= N; l++)
{
for(j=0;j<= N; j++)
{
xold[l][j]=0.1*ran1(& idum);
}
}
for(l=0;l<= N; l++)
{
for(j=0;j<= N; j++)
{
yold[l][j]=0.1*ran1(& idum);
}
}
}
void Matriz_Bessel(double **Bess,double gama)
{
int x,y;
double r;
for(x=0;x<=N;x++)
{
for(y=0;y<=N;y++)
{
if(y!=0 || x!=0)
{
r = gama*sqrt(x*x +y*y);
Bess[x][y] = bessk0(r);
}
}
}
}
void acoplamento(int x, int y,double **xold, double *Acopl,double **Bess)
{
int j, i, h, k,xdist, ydist;
int Nmeio = N/2;
double Xf;
Xf = 0;
for(i=0;i<=N;i++)
{
for(j=0;j<=N;j++)
{
h = x+i;
k = y+j;
ydist = j;
xdist = i;
if(i>Nmeio)
{
h = x +i;
xdist = (N+1) -h +x;
}
if(h>N)
{
h=h-(N+1);
xdist = x-h;
if(xdist >Nmeio){xdist = i;
}
}
if(j>Nmeio)
{
k = y +j;
ydist = (N+1) -k +y;
}
if(k>N)
{
k=k-(N+1);
ydist = y-k;
if(ydist >Nmeio){ydist = j;
}
}
if(ydist!=0 || xdist!=0)
{
Xf = Xf +Bess[xdist][ydist]*xold[h][k];
}
}
}
*Acopl = Xf;
}
void constante(double *c, double gama, double **Bess){
double soma;
int x, y;
soma = 0;
for(x=0;x<=(N/2);x++)
{
for(y=0;y<=(N/2);y++)
{
if(y!=0 || x!=0)
{
soma = soma +Bess[x][y];
}
}
}
*c = (1/(4*soma));
}
int main(int argc, char* argv[])
{
double **xold, **xnew, **yold, **ynew, **a;
double gama, C;
int x,y;
int t,i;
double Mn, acopl;
char arqnome[100];
FILE *fout;
double **Bess;
Bess= (double**)malloc(sizeof(double*)*(N+3));
for(i=0; i<(N+3); i++){Bess[i] = (double*)malloc(sizeof(double)* (N+3));}
xold= (double**)malloc(sizeof(double*)*(N+3));
for(i=0; i<(N+3); i++){xold[i] = (double*)malloc(sizeof(double)* (N+3));}
yold= (double**)malloc(sizeof(double*)*(N+3));
for(i=0; i<(N+3); i++){yold[i] = (double*)malloc(sizeof(double)*(N+3));}
xnew= (double**)malloc(sizeof(double*)*(N+3));
for(i=0; i<(N+3); i++){xnew[i] = (double*)malloc(sizeof(double)*(N+3));}
ynew= (double**)malloc(sizeof(double*)*(N+3));
for(i=0; i<(N+3); i++){ynew[i] = (double*)malloc(sizeof(double)*(N+3));}
a= (double**)malloc(sizeof(double*)*(N+3));
for(i=0; i<(N+3); i++){a[i] = (double*)malloc(sizeof(double)*(N+3));}
srand (time(NULL));
gama = 0.005;
sprintf(arqnome,"serie_%.3f_%.3f.dat",gama,epsilon);
fout = fopen(arqnome,"w");
Matriz_Bessel(Bess,gama);
condicoes_iniciais(xold,yold,a);
a[0][0] = 4.1;
a[N/2][N/2] = 4.3;
constante(&C, gama,Bess);
for(t=0;t<=tmax;t++)
{
Mn = 0;
for(x=0;x<=N;x++)
{
for(y=0;y<=N;y++)
{
acoplamento(x,y,xold,&acopl,Bess);
xnew[x][y] = (a[x][y]/(1+xold[x][y]*xold[x][y])) +yold[x][y] + epsilon*C*acopl;
ynew[x][y] = yold[x][y] - sigma*xold[x][y] - beta;
Mn = Mn + xnew[x][y];
xold[x][y] = xnew[x][y];
yold[x][y] = ynew[x][y];
}
}
if(t>trans){fprintf(fout,"%d %f %f %f %f %f\n",(t-trans),xold[0][0],yold[0][0],xold[N/2][N/2],yold[N/2][N/2],Mn/((N+1)*(N+1)));}
}
return 0;
}
Bess[N][N]
是每个半径的贝塞尔函数,使用数值公式计算。该计划大约需要1小时才能完成。
随着弗朗西斯的消化,我有
#include <fftw3.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include"bessel.c"
#include"ran1.c"
#define tmax 90000
#define beta 0.001
#define N 50
#define sigma 0.001
#define pi acos(-1.0)
#define trans 50000
#define epsilon 0.1
void condicoes_iniciais(double *xold,double *yold,double *a)
{
int l;
long idum=-120534;
for(l=0;l<= N*N; l++){
a[l]=5.0;}
for(l=0;l<= N*N; l++){
while(a[l]>4.4)
a[l]=4.1+ran1(& idum);}
for(l=0;l<=N* N; l++){
xold[l]=0.1*ran1(& idum);
yold[l]=0.1*ran1(& idum);}
a[0]=4.1;
a[N]=4.4;
}
void Matriz_Bessel(double *bessel,double gama)
{
int x,y,i,j;
double dist;
for(x=0,i=-N/2;x<N;x++,i++)
{
for(y=0,j=-N/2;y<N;y++,j++)
{
double dist=sqrt(i*i+j*j);
if(dist>0){
bessel[x*N+y]=bessk0(gama*dist);
}
else{
bessel[x*N+y]=1;
}
}
}
}
void constante(double *c, double *bessel)
{
int x;
int y;
double soma = 0;
for(x=0;x<N;x++){
for(y=0;y<N;y++){
soma = soma + bessel[x*N+y];
}}
*c =(1/(4*soma));
}
int main(int argc, char* argv[]){
double *xnew=fftw_malloc(sizeof(double)*N*N);
double *acopl=fftw_malloc(sizeof(double)*N*N);
double *xold=malloc(sizeof(double)*N*N);
double *yold = malloc(sizeof(double)*N*N);
double *a = malloc(sizeof(double)*N*N);
fftw_complex *xfourier;
xfourier = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N/2+1)*N);
fftw_complex *aux;
aux= (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N/2+1)*N);
double *bessel= fftw_malloc(sizeof(double)*N*N);
fftw_complex *besself;
besself=fftw_malloc(sizeof(fftw_complex)*(N/2+1)*N);
double scale=1.0/(N*N);
int t,i;
double gama,Mn,C;
gama = 0.005;
char arqnome[1000];
FILE *fout;
sprintf(arqnome,"opt2_tamanho_plato_%.3f_%d.dat",gama,N);
fout = fopen(arqnome,"w");
//initial
printf("initial\n");
condicoes_iniciais(xold,yold,a);
//xold[(N/2)*N+N/2]=1;
// fftw_plan
printf("fftw_plan\n");
fftw_plan plan;
plan=fftw_plan_dft_r2c_2d(N, N, xnew, xfourier, FFTW_MEASURE | FFTW_PRESERVE_INPUT);
fftw_plan planb;
planb=fftw_plan_dft_r2c_2d(N, N,(double*) bessel, besself, FFTW_MEASURE);
fftw_plan plani;
plani=fftw_plan_dft_c2r_2d(N, N, aux, acopl, FFTW_MEASURE);
Matriz_Bessel(bessel,gama);
constante(&C, bessel);
fftw_execute(planb);
//time loop
printf("time loop\n");
for(t=0;t<=tmax;t++){
//convolution= products in fourier space
fftw_execute(plan);
for(i=0;i<N*(N/2+1);i++){
aux[i][0]=(xfourier[i][0]*besself[i][0]-xfourier[i][2]*besself[i][3]);
aux[i][4]=(xfourier[i][0]*besself[i][5]+xfourier[i][6]*besself[i][0]);
}
fftw_execute(plani);//xnew is updated
Mn = 0;
for(i=0;i<N*N;i++){
xnew[i]=(a[i]/(1+xold[i]*xold[i])) +yold[i] + epsilon*C* (acopl[i]/(double)(N*N));
yold[i] = yold[i] - sigma*xold[i] - beta;
Mn = Mn +xnew[i];
}
memcpy(xold,xnew,N*N*sizeof(double));
if(t>trans){fprintf(fout,"%d %f %f %f %f %f\n",(t-trans),xold[0],yold[0],xold[N],yold[N],Mn/((N+1)*(N+1)));}
}
printf("destroy\n");
fftw_destroy_plan(plan);
fftw_destroy_plan(plani);
fftw_destroy_plan(planb);
printf("free\n");
fftw_free(bessel);
fftw_free(xnew);
fftw_free(xold);
fftw_free(yold);
fftw_free(besself);
fftw_free(xfourier);
return 0;
}
With take around 1min to finish, but i got this results
fftw3代码的比例因子必须是该值。我不知道它是如何起作用的。
答案 0 :(得分:3)
您描述的操作称为convolution。让f(x,y)
成为您的定期来源,B(x,y)
成为贝塞尔函数。您正在尝试计算:
在大小为N+1
的网格上离散化,它写道:
由于此总和在所有点上执行,因此复杂性非常高:O(N^4)
。这意味着要执行的操作数量为N*N*N*N
。 如何降低这种复杂性?
B(x,y)
随着距离的增加而迅速变小,则可以忽略远程交互,并且可以减少卷积的窗口。它会影响输出的精度,它可能对您的问题没有用。让N_W<<N
为此窗口的大小。总和现在写道:
要执行的操作数约为N*N*N_W*N_W<<N^4
。
然而,从实际的角度来看,内核必须非常小才能使上述方法非常有趣。由于the Bessel functions decrease slowly(from Abramowitz and Stegun: Handbook of Mathematical Functions, p364)(约1 / sqrt(x)),之前的方法不太可能成功。
1计算名为f
hatf
的DFT
2计算名为B
hatB
的DFT
3对于所有频率p,q
,请执行以下产品:
hatf*(p,q)=hatf(p,q)*hatB(p,q)
4反转DFT以获得f*
上述方法非常有效,因为它的复杂性是2D DFT,即N*N*log(N)
。此外,FFTW等专用库使其易于实施。请查看fftw_plan fftw_plan_dft_r2c_2d
并注意data layout。
编辑:我仍然认为有可能让它发挥作用......这是一个起始代码,由gcc main.c -o main -lfftw3 -lm
编译
#include <fftw3.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int save_image(int N,double* data,int nb){
char filename[1000];
sprintf(filename,"xxx%d.vtk",nb);
FILE * pFile;
pFile = fopen (filename,"w");
if (pFile!=NULL)
{
fputs ("# vtk DataFile Version 2.0\n",pFile);
fputs ("Volume example\n",pFile);
fputs ("ASCII\n",pFile);
fputs ("DATASET STRUCTURED_POINTS\n",pFile);
fprintf(pFile,"DIMENSIONS %d %d 1\n",N,N);
fputs ("ASPECT_RATIO 1 1 1\n",pFile);
fputs ("ORIGIN 0 0 0\n",pFile);
fprintf(pFile,"POINT_DATA %d\n",N*N);
fputs ("SCALARS volume_scalars float 1\n",pFile);
fputs ("LOOKUP_TABLE default\n",pFile);
int i;
for(i=0;i<N*N;i++){
fprintf(pFile,"%f ",data[i]);
}
fclose (pFile);
}
return 0;
}
int main(int argc, char* argv[]){
int N=64;
double *xnew=fftw_malloc(sizeof(double)*N*N);
double *xold=fftw_malloc(sizeof(double)*N*N);
double *yold=fftw_malloc(sizeof(double)*N*N);
fftw_complex *xfourier=fftw_malloc(sizeof(fftw_complex)*(N/2+1)*N);
double *bessel=fftw_malloc(sizeof(double)*N*N);
fftw_complex *besself=fftw_malloc(sizeof(fftw_complex)*(N/2+1)*N);
//initial
printf("initial\n");
memset(xold,0,sizeof(double)*N*N);
memset(yold,0,sizeof(double)*N*N);
xold[(N/2)*N+N/2]=1;
// fftw_plan
printf("fftw_plan\n");
fftw_plan plan;
plan=fftw_plan_dft_r2c_2d(N, N, xold, xfourier, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
fftw_plan planb;
planb=fftw_plan_dft_r2c_2d(N, N,(double*) bessel, besself, FFTW_ESTIMATE);
fftw_plan plani;
plani=fftw_plan_dft_c2r_2d(N, N, xfourier, xnew, FFTW_ESTIMATE);
//bessel function
//crude approximate of bessel...
printf("bessel function\n");
double dx=1.0/(double)N;
double dy=1.0/(double)N;
int x,y;int i,j;
for(x=0,i=-N/2;x<N;x++,i++){
for(y=0,j=-N/2;y<N;y++,j++){
double dist=sqrt(dx*dx*(i*i+j*j));
double range=0.01;
dist=dist/range;
if(dist>0){
bessel[x*N+y]=sqrt(2./(M_PI*dist))*cos(dist-M_PI/4.0);
}else{
bessel[x*N+y]=1;
}
}
}
fftw_execute(planb);
fftw_destroy_plan(planb);
fftw_free(bessel);
//time loop
printf("time loop\n");
int t,tmax=100;
for(t=0;t<=tmax;t++){
save_image(N,xold,t);
printf("t=%d\n",t);
//convolution= products in fourier space
fftw_execute(plan);
double scale=1.0/((double)N*N);
//scale*=scale; //may be needed to correct scaling
for(i=0;i<N*(N/2+1);i++){
xfourier[i][0]=(xfourier[i][0]*besself[i][0]-xfourier[i][1]*besself[i][1])*scale;
xfourier[i][1]=(xfourier[i][0]*besself[i][1]+xfourier[i][1]*besself[i][0])*scale;
}
fftw_execute(plani);//xnew is updated
double C=1;double epsilon=1; double a=1; double beta=1;double sigma=1;
for(i=0;i<N*N;i++){
xnew[i]=(a/(1+xold[i]*xold[i])) +yold[i] + epsilon*C*xnew[i];
yold[i] = yold[i] - sigma*xold[i] - beta;
}
memcpy(xold,xnew,N*N*sizeof(double));
}
printf("destroy\n");
fftw_destroy_plan(plan);
fftw_destroy_plan(plani);
// fftw_destroy_plan(planb);
printf("free\n");
fftw_free(xnew);
fftw_free(xold);
fftw_free(yold);
fftw_free(besself);
fftw_free(xfourier);
return 0;
}
它产生xold
的一些vtk图像,可以由paraview软件打开。保存图像可能会减慢计算速度......
我的系数是错误的,所以输出是错误的......
编辑:这是基于你的代码片段,由gcc main.c -o main -lfftw3 -lm
编译。我找到了bessk0.c和bessi0.c。
代码写道:
#include <fftw3.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include"bessi0.c"
#include"bessk0.c"
//#include"bessel.c"
//#include"ran1.c"
#define tmax 90000
#define beta 0.001
#define N 50
#define sigma 0.001
#define pi acos(-1.0)
#define trans 50000
#define epsilon 0.1
double ran1(long* idum){
return ((double)rand())/((double)RAND_MAX);
}
void condicoes_iniciais(double *xold,double *yold,double *a)
{
int l;
long idum=-120534;
for(l=0;l<= N*N; l++){
a[l]=5.0;}
for(l=0;l<= N*N; l++){
while(a[l]>4.4)
a[l]=4.1+ran1(& idum);}
for(l=0;l<=N* N; l++){
xold[l]=0.1*ran1(& idum);
yold[l]=0.1*ran1(& idum);
//printf("%g %g %g\n",xold[l],yold[l],a[l]);
}
a[0]=4.1;
a[N]=4.4;
}
void Matriz_Bessel(double *bessel,double gama)
{
int x,y,i,j;
double dist;
for(x=0,i=-N/2;x<N;x++,i++)
{
for(y=0,j=-N/2;y<N;y++,j++)
{
double dist=sqrt(i*i+j*j);
if(dist>0){
bessel[x*N+y]=bessk0(gama*dist);
//printf("%g %g\n",dist,bessel[x*N+y]);
}
else{
bessel[x*N+y]=1;
}
}
}
}
void constante(double *c, double *bessel)
{
int x;
int y;
double soma = 0;
for(x=0;x<N;x++){
for(y=0;y<N;y++){
soma = soma + bessel[x*N+y];
}}
// *c =(1.0/(4.0*soma));
*c =(1.0/(soma));
}
int main(int argc, char* argv[]){
//srand (time(NULL));
srand (0);
double *xnew=fftw_malloc(sizeof(double)*N*N);
double *acopl=fftw_malloc(sizeof(double)*N*N);
double *xold=malloc(sizeof(double)*N*N);
double *yold = malloc(sizeof(double)*N*N);
double *a = malloc(sizeof(double)*N*N);
fftw_complex *xfourier;
xfourier = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N/2+1)*N);
fftw_complex *aux;
aux= (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N/2+1)*N);
double *bessel= fftw_malloc(sizeof(double)*N*N);
fftw_complex *besself;
besself=fftw_malloc(sizeof(fftw_complex)*(N/2+1)*N);
double scale=1.0/((double)N*N);
int t,i;
double gama,Mn,C;
gama = 0.005;
char arqnome[1000];
FILE *fout;
sprintf(arqnome,"opt2_tamanho_plato_%.3f_%d.dat",gama,N);
fout = fopen(arqnome,"w");
//initial
printf("initial\n");
condicoes_iniciais(xold,yold,a);
//xold[(N/2)*N+N/2]=1;
// fftw_plan
printf("fftw_plan\n");
fftw_plan plan;
plan=fftw_plan_dft_r2c_2d(N, N, xnew, xfourier, FFTW_MEASURE | FFTW_PRESERVE_INPUT);
fftw_plan planb;
planb=fftw_plan_dft_r2c_2d(N, N, bessel, besself, FFTW_MEASURE);
fftw_plan plani;
plani=fftw_plan_dft_c2r_2d(N, N, aux, acopl, FFTW_MEASURE);
Matriz_Bessel(bessel,gama);
constante(&C, bessel);
fftw_execute(planb);
//time loop
printf("time loop\n");
for(t=0;t<=tmax;t++){
//convolution= products in fourier space
fftw_execute(plan);
for(i=0;i<N*(N/2+1);i++){
aux[i][0]=(xfourier[i][0]*besself[i][0]-xfourier[i][1]*besself[i][1]);
aux[i][1]=(xfourier[i][0]*besself[i][1]+xfourier[i][1]*besself[i][0]);
}
fftw_execute(plani);//xnew is updated
Mn = 0;
for(i=0;i<N*N;i++){
xnew[i]=(a[i]/(1+xold[i]*xold[i])) +yold[i] + epsilon*C* (acopl[i]/(double)(N*N));
yold[i] = yold[i] - sigma*xold[i] - beta;
Mn = Mn +xnew[i];
}
memcpy(xold,xnew,N*N*sizeof(double));
if(t>trans){fprintf(fout,"%d %f %f %f %f %f\n",(t-trans),xold[0],yold[0],xold[N],yold[N],Mn/((N+1)*(N+1)));}
}
printf("destroy\n");
fftw_destroy_plan(plan);
fftw_destroy_plan(plani);
fftw_destroy_plan(planb);
printf("free\n");
fftw_free(bessel);
fftw_free(xnew);
fftw_free(xold);
fftw_free(yold);
fftw_free(besself);
fftw_free(xfourier);
fftw_free(aux);
fftw_free(acopl);
return 0;
}
结果如下:
行:
aux[i][0]=(xfourier[i][0]*besself[i][0]-xfourier[i][1]*besself[i][1]);
aux[i][1]=(xfourier[i][0]*besself[i][1]+xfourier[i][1]*besself[i][0]);
对应复数的乘积。 aux[i]
是一个复数,aux[i][0]
是其实部,aux[i][1]
是其虚部。因此aux[i][4]
与有意义的东西不对应。这些复数对应Fourier space中的频率大小。
我还修改了常量:*c =(1.0/(soma));
如果您希望比较输出并以相同方式构建初始状态,请不要忘记添加srand(0)
。
答案 1 :(得分:2)
您也许可以使用网格的对称性来减少所需的计算次数。特别是如果你正在建模无限周期系统,因为明显的环绕逻辑让我觉得你可能正在做。
考虑:
[35][35]
,[35 - x][35]
,[35 + x][35]
和[35][35 - x]
处的粒子对坐标[35][35 + x]
处的粒子施加相同的影响;也[35 - x][35 - x]
,[35 + x][35 - x]
,[35 - x][35 + x]
和[35 + x][35 + x]
处的粒子均会产生另一种影响;和[35 + x][35 + y]
,[35 + x][35 - y]
,[35 - x][35 + y]
,[35 - x][35 - y]
,[35 + y][35 + x]
,[35 + y][35 - x]
,{ {1}}和[35 - y][35 + x]
,适用于任何[35 - y][35 - x]
。通过使用这些等价,您应该能够将计算速度提高一点8倍。
然而,如果你确实在模拟一个无限周期系统,那么我观察到你的方法包含了一个偏差:通过计算方形网格的影响,你可以在{{1}之间的距离处包含一些粒子的影响。来自目标的{}和x != y
,而不是其他人。你应该在(虚拟)光盘上进行计算,以避免这种偏见。
此外,输入参数N
和sqrt(2) * N
的出现使我假设您正在为每个网格位置执行一次计算。如果你再次建模一个无限的周期性网格,每个网格点都有一个发射器,并且每个点的影响仅取决于距离,那么每个点都会受到同样的影响 。如果你可以使用它,你可以减少你的运行时数千倍,和降低算法的渐近复杂度。