-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu-wh.cc
181 lines (149 loc) · 4.11 KB
/
cpu-wh.cc
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <math.h>
#include <fstream>
#include <cstdlib>
using namespace std;
typedef struct {
double r;
double theta;
double phi;
double pr;
double ptheta;
double b;
double Bsq;
} State; // Dynamical System State Vector
State state_add(const State &s1, const State &s2)
{
State s;
s.r = s1.r + s2.r;
s.theta = s1.theta + s2.theta;
s.phi = s1.phi + s2.phi;
s.pr = s1.pr + s2.pr;
s.ptheta = s1.ptheta + s2.ptheta;
s.b = s1.b + s2.b;
s.Bsq = s1.Bsq + s2.Bsq;
return s;
}
State state_mul(double g, const State &s)
{
State rs;
rs.r = g*s.r;
rs.theta = g*s.theta;
rs.phi = g*s.phi;
rs.pr = g*s.pr;
rs.ptheta = g*s.ptheta;
rs.b = g*s.b;
rs.Bsq = g*s.Bsq;
return rs;
}
double l(double r)
{
double rhosq = 0.01;
return sqrt(rhosq + pow(r, 2));
}
double dldr(double r)
{
return r/l(r) ;
}
State rhs(const State &s, double gamma)
{
State ds;
double rsq = pow(l(s.r), 2);
ds.r = s.pr;
ds.theta = s.ptheta / rsq;
ds.phi = s.b / (rsq*pow(sin(s.theta), 2));
ds.pr = s.Bsq*(dldr(s.r) / (pow(l(s.r), 3)));
ds.ptheta = (pow(s.b, 2)/rsq) * cos(s.theta)/pow(sin(s.theta), 3);
ds.b = 0.0;
ds.Bsq = 0.0;
return ds;
}
void rk4_step(int N, State *states, double gamma, double h, int indx)
{
State s = states[indx];
State y = s;
if(s.pr < 0) {
int a = 1;
}
State k1 = rhs(s, gamma);
s = state_add(y, state_mul(h/2, k1));
State k2 = rhs(s, gamma);
s = state_add(y, state_mul(h/2, k2));
State k3 = rhs(s, gamma);
s = state_add(y, state_mul(h, k3));
State k4 = rhs(s, gamma);
s = state_add(y, state_mul(h/6, state_add(k4, state_add(state_mul(2.0, state_add(k2, k3)), k1))));
states[indx] = s;
}
int main(void)
{
int Nx = 8;
int Ny = 8; // Pixes
int N = Nx*Ny; // total pixels in image.
// Allocate Host array for initial conditions:
State *states_host = (State *)malloc(N*sizeof(State));
if (states_host == NULL)
{
cout << "Allocation error" << endl;
}
// Setup Initial conditions
int i,j;
double pi = 3.14159;
double deg2rad = pi/180;
double thetaFOV = 20;
double phiFOV = 20;
double mintheta = pi/2 - deg2rad*thetaFOV/2;
double maxtheta = pi/2 + deg2rad*thetaFOV/2;
double minphi = -deg2rad*phiFOV/2;
double maxphi = deg2rad*phiFOV/2;
double thetacs, phics;
double nx, ny, nz;
double cam_l = -10.0;
double cam_r = sqrt(1.0 + pow(cam_l, 2));
double cam_phi = 0.0;
double cam_theta = pi/2;
for(i=0; i<Nx; i++) // phi
{
for(j=0; j<Ny; j++) // theta
{
thetacs = mintheta + (((double) j)/Ny)*(maxtheta - mintheta);
phics = minphi + (((double) i)/Nx)*(maxphi - minphi);
nx = sin(thetacs)*cos(phics);
ny = sin(thetacs)*sin(phics);
nz = -1.0*cos(thetacs);
states_host[i*Nx +j].r = cam_l;
states_host[i*Nx +j].theta = cam_theta;
states_host[i*Nx +j].phi = cam_phi;
states_host[i*Nx +j].pr = nx;
states_host[i*Nx +j].ptheta = cam_r*nz;
states_host[i*Nx +j].b = cam_r*sin(cam_theta)*ny;
states_host[i*Nx +j].Bsq = pow(cam_r, 2)*(pow(nz,2) + pow(ny,2));
}
}
int ind = 4*Nx + 4;
ind =0;
cout << states_host[ind].r << " " << states_host[ind].theta << " " <<
states_host[ind].phi << " " << " " << states_host[ind].pr << " "
<< states_host[ind].b << endl;
// Integrate
double dt = 1e-3;
double t = 0.0;
double tend = 100.0;
int k = 0;
while(t < tend)
{
for (int m = 0; m < N; m++){
rk4_step(N, states_host, 1.0, dt, m);
}
t += dt;
k +=1;
int ind = 4*Nx+4;
ind =0;
for (int j=0; j< 1; j++)
{
cout << "UPDATE " << states_host[ind].r << " "
<< states_host[ind].theta << " " << states_host[ind].phi
<< " " << states_host[ind].pr << " " << states_host[ind].b << endl;
}
}
}