2Dの魔法の六角形の格子を生成しようとしています(つまり、C言語で点の座標を生成する必要があります)。添付の画像を参照してください。図は、大きなものの中に六角形があるタマネギの構造のように見えます。
誰かアイデアがありますか?
注:他の言語で誰かが答えを持っていれば大丈夫です。自分のコードの作成を開始できるように、確認する必要があります。前もって感謝します。
void generate_particles(void)
{/* Generates the particle - positions and charge
Here it indicated to use the hexagonal referential !!*/
int i,j;
int n=3; /*n represent the nth centered hex number given by the formula 3*n(n- )+1*/
double b;
b=(1.0/sqrt(sqrt(3)))*sqrt(2.0/par.NA);
/* b=1.0;*/
fprintf(stderr,"Distributing %d particles on the hexagonal lattice'...", par.N);
for (i=0;i<n-1;i++)
{
coo[i][0]= (sqrt(3)*i*b)/2.0;
for (j=0;j<(2*n-i-1);j++)
{
coo [i][1]= (-(2*n-i-2)*b)/2.0 + j*b;
fprintf(stderr," %lf %lf\n",coo[i][0],coo[i][1]);
/*plot the points with coordinate (x,y) here*/
if(coo[i][0]!=0)
{
fprintf(stderr," %lf %lf\n",-coo[i][0],coo[i][1]);
/*plot the points with coordinates (-x,y)*/
}
}
}fprintf(stderr," done\n\n");
}
void write_configuration_file(void)
{/* Writes the binary configuration file '<prefix>_config.<postfix>'
i.e velocities and coordinates. */
FILE *fp;
char filename[100];
char postfix_string[100];
int i;
fprintf(stderr,"Writing configuration file");
if (postfix >=0 ) {
if (postfix < 10) sprintf(postfix_string,"000%d",postfix);
else if (postfix < 100) sprintf(postfix_string, "00%d",postfix);
else if (postfix < 1000) sprintf(postfix_string, "0%d",postfix);
else if (postfix <10000) sprintf(postfix_string, "%d",postfix);
else sprintf(postfix_string, "_%d",postfix);
} else {
fprintf(stderr,"\nThe internal postfix is negative.\n\n");
exit(1);
}
sprintf(filename,"%s_config.%s",prefix,postfix_string);
fprintf(stderr," %s...", filename);
if ((fp = fopen(filename,"r")) != NULL) {
fprintf(stderr,"\nFile '%s' already exists. Don't want to overwrite it.\n\n",filename);
fclose(fp);
exit(1);
}
if ((fp = fopen(filename,"w")) == NULL) {
fprintf(stderr,"Could not create file '%s'.\n\n",filename);
exit(1);
}
/* postfix */
if (fwrite(&postfix,sizeof(int),1,fp) != 1)
{ fprintf(stderr,"fwrite error 1.\n\n"); exit(1); }
/* time */
if (fwrite(&ti,sizeof(int),1,fp) != 1)
{ fprintf(stderr,"fwrite error 2.\n\n"); exit(1); }
/* x,y coordinates of all particles/charges */
if (fwrite(coo,sizeof(double) ,2*par.N,fp) != 2*par.N)
{
fprintf(stderr,"fwrite error 4.\n\n"); exit(1); }
fclose(fp);
fprintf(stderr," done\n");
}
and the main program is:
int main()
{
int i;
printf("\n");
printf("\n");
printf("***************************************************************\n");
printf("* OK LETS GENERATE THE CONFIG FILE FOR MONTE CARLO SIMULATION *\n");
printf("***************************************************************\n\n");
read_wishlist();
init_ran1();
for (i=0; i<seed; i++) ran1_fast();
if (par.N > 0) generate_particles();
write_parameter_file();
write_configuration_file();
write_task_file();
/*final_test();*/
fprintf(stderr,"\n\nConfiguration successfully generated.\n\n");
}
さて、私の問題を説明させてください。実際、前に私に与えたコードは完璧で、Cとmatlabで六角形の粒子をプロットすることができましたが、それはプロットでした。シミュレーションに関しては、各粒子には問題がありました。私のコードでは0からpar.Nまでのラベルがありますが、これを書く方法では、レイヤー13にある13個の粒子しか読み取っていませんでした。したがって、各粒子が1つになるようにこれを変更する方法の解決策を見つけるのを手伝ってください。事前に感謝を調整します。
@MohamedKALLELまず最初に関数generate_particlesで
coo[i][0]はx座標とcooiを表しますy座標、generate_particlesの部分を見ると、残りの部分は忘れてしまいます。前に指定したものと似ていますが、このファイルを実行するときに、自分の言語と他の変数を使用して記述しました。画面にプロットされた座標は、まさに私が最初の構成を開始したいのですが、ここで行うのは、構成バイナリファイルにこれらの座標を書き込むことです。問題は、このバイナリファイルを読んでいるときに、出力される座標は0からn-1nだけであるように見えることです。レイヤーの順序、547個のパーティクルがあるため、コードの記述方法では解決できない問題がありますが、このコードでは13の座標しか与えられていないため、すべてのパーティクルにラベルを付ける必要があります。つまり、547個のパーティクルを指定する必要があります。それぞれに独自の座標がありますか?