-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample3.cpp
More file actions
153 lines (130 loc) · 3.01 KB
/
Copy pathsample3.cpp
File metadata and controls
153 lines (130 loc) · 3.01 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <vector>
void InitializeVariables();
void SetupGrids();
void SetupDensity();
void GetGravitationalPotential();
void OutputData();
namespace grids{
const double Ggra=1.0e0;
const double rho0=1.0e0;
double pi;
double x1min,x1max;
double x2min,x2max;
const int margin=1;
const int nx1=1024;
const int nx2=1024;
double dx1;
std::vector<double> x1a(nx1+1);
std::vector<double> x1b(nx1),dvl1a(nx1);
double dx2;
std::vector<double> x2a(nx2+1);
std::vector<double> x2b(nx2),dvl2a(nx1);
std::vector<double> rho(nx1*nx2),gp(nx1*nx2);
const int l1=1;
const int l2=1;
const double persymx1=-1.0;
const double persymx2=-1.0;
const int itemax=300;
};
int main(int argc, char**argv){
clock_t start,stop;
printf("CPU:\n");
InitializeVariables();
SetupGrids();
SetupDensity();
start=clock();
GetGravitationalPotential();
stop=clock();
OutputData();
printf("Time: %.2f [ms]\n",(double)(stop-start));
}
void InitializeVariables(){
}
void SetupGrids(){
using namespace grids;
int i,j;
x1min = 0.0e0;
x1max = 1.0e0;
x2min = 0.0e0;
x2max = 1.0e0;
dx1 =(x1max-x1min)/(nx1-margin*2);
x1a[0] = x1min -margin*dx1;
for(i=1;i<=nx1;i++){
x1a[i] =x1a[i-1] + dx1;
}
for(i=0;i<nx1;i++){
x1b[i] = 0.5e0*(x1a[i] + x1a[i]);
dvl1a[i] = dx1;
}
dx2 =(x2max-x2min)/(nx2-margin*2);
x2a[0] =x2min -margin*dx2;
for(j=1;j<=nx2;j++){
x2a[j] =x2a[j-1] + dx2;
}
for(j=0;j<nx2;j++){
x2b[j] = 0.5e0*(x2a[j] + x2a[j]);
dvl2a[j] = dx2;
}
}
void SetupDensity(){
using namespace grids;
int i,j;
pi =atan2(0.0,-1.0);
for(j=0;j<nx2;j++){
for(i=0;i<nx1;i++){
rho[j*nx1+i] = rho0 * sin(l1*pi*x1b[i]/(x1max-x1min))
* sin(l2*pi*x2b[j]/(x2max-x2min));
}
}
}
void GetGravitationalPotential(){
using namespace grids;
std::vector<double> gpnxt(nx1*nx2);
double a1,a2,a3;
int i,j,n;
a1 = 1.0e0/dx1/dx1;
a2 = 1.0e0/dx2/dx2;
a3 = 2.0e0*(a1+a2);
for(n=0;n<itemax;n++){
for(j=1;j<nx2-1;j++){
for(i=1;i<nx1-1;i++){
gpnxt[j*nx1+i] = ( a1*(gp[ j*nx1+i+1]+gp[ j*nx1+i-1])
+a2*(gp[(j+1)*nx1+i ]+gp[(j-1)*nx1+i ])
-4.0e0*pi*Ggra*rho[ j*nx1+i ]
)/a3;
}
}
for(j=1;j<nx2-1;j++){
for(i=1;i<nx1-1;i++){
gp[j*nx1+i] = gpnxt[j*nx1+i];
}
}
for(i=0;i<nx1;i++){
gp[ 0*nx1+i] = persymx2*gp[(nx2-2)*nx1+i];
gp[(nx2-1)*nx1+i] = persymx2*gp[( 1)*nx1+i];
}
for(j=0;j<nx2;j++){
gp[ j*nx1+0 ] = persymx1*gp[ j*nx1+nx1-2];
gp[ j*nx1+nx1-1] = persymx1*gp[ j*nx1+ 1];
}
} /* n-loop*/
}
void OutputData(){
using namespace grids;
int i,j;
static FILE *fp;
const char filename[30]="xy-cpu.dat";
fp=fopen(filename,"w");
for(j=1;j<nx2-1;j++){
for(i=1;i<nx1-1;i++){
fprintf(fp," %e %e %e %e\n",x1b[i], x2b[j],rho[j*nx1+i],gp[j*nx1+i]);
}
fprintf(fp,"\n");
}
fclose(fp);
}