-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalculate_spin_GPU.cu
329 lines (272 loc) · 12.3 KB
/
calculate_spin_GPU.cu
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/********************************************************************************
*
* Copyright (C) 2015 Culham Centre for Fusion Energy,
* United Kingdom Atomic Energy Authority, Oxfordshire OX14 3DB, UK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
********************************************************************************
*
* Program: SPILADY - A Spin-Lattice Dynamics Simulation Program
* Version: 1.0
* Date: Aug 2015
* Author: Pui-Wai (Leo) MA
* Contact: [email protected]
* Address: Culham Centre for Fusion Energy, OX14 3DB, United Kingdom
*
********************************************************************************/
#if (defined SDH || defined SDHL || defined SLDH || defined SLDHL || defined SLDNC) && defined GPU
#include "spilady.h"
#include "prototype_GPU.h"
/****************************************************************************
* GPU codes only
****************************************************************************/
__device__ void calculate_spin_d(int j, curandState *rand_state_ptr_d,
struct varGPU *var_ptr_d,
struct atom_struct *atom_ptr,
struct cell_struct *first_cell_ptr_d,
double dt, double *Jij_ptr_d
#if defined SDHL || defined SLDHL
, double *LandauA_ptr_d
, double *LandauB_ptr_d
, double *LandauC_ptr_d
, double *LandauD_ptr_d
#endif
)
{
#ifdef extfield
atom_ptr->Heff_H = atom_ptr->Hext;
#else
atom_ptr->Heff_H = vec_zero_d();
#endif
#if defined SDHL || defined SLDHL
atom_ptr->Heff_L = vec_zero_d();
#endif
inner_spin_d(var_ptr_d, atom_ptr, first_cell_ptr_d, Jij_ptr_d);//calculate the effective field of current atom
#ifndef spinlang
//exact solution; no thermostat; PRL 86, 898 (2001) I. P. Omelyan
atom_ptr->s = spin_rotation_d(atom_ptr->Heff_H, atom_ptr->s, dt);
#endif /*no spin langevin*/
#ifdef spinlang
double dt_half = dt/2e0;
#if defined SDH || defined SLDH
//Pui-Wai Ma and S. L. Dudarev, PHYSICAL REVIEW B 83, 134418 (2011)
//There are 3 parts. Deterministic -> Stochastic -> Deterministic
vector s_temp = atom_ptr->s;
//1st part
vector s_cross_Heff = vec_cross_d(s_temp, atom_ptr->Heff_H);
double Heff_H0 = vec_length_d(atom_ptr->Heff_H);
double cos_a = cos(Heff_H0*(dt_half/hbar));
double sin_a = sin(Heff_H0*(dt_half/hbar));
double exp_b = exp(-Heff_H0*atom_ptr->s0*var_ptr_d->gamma_S_H*(dt_half/hbar));
double exp_2b = exp_b*exp_b;
double normalized_S_dot_H = 0e0;
if (atom_ptr->s0 > 0e0 && Heff_H0 > 0e0)
normalized_S_dot_H = vec_dot_d(s_temp, atom_ptr->Heff_H)/atom_ptr->s0/Heff_H0;
double denominator = (1e0 + exp_2b + normalized_S_dot_H*(1e0 - exp_2b))*Heff_H0;
double factor = (1e0 - exp_2b + normalized_S_dot_H*(1e0 + exp_2b - 2e0*cos_a*exp_b))*atom_ptr->s0;
atom_ptr->s = vec_divide_d( vec_add_d(vec_add_d(vec_times_d(2e0*cos_a*exp_b*Heff_H0, s_temp),
vec_times_d(2e0*sin_a*exp_b, s_cross_Heff)),
vec_times_d(factor, atom_ptr->Heff_H)), denominator);
//2nd part
#ifdef eltemp
double random_h = sqrt(2e0*(first_cell_ptr_d+(atom_ptr->new_cell_index))->Te*var_ptr_d->gamma_S_H*hbar/dt);
#else
double random_h = sqrt(2e0*var_ptr_d->temperature*var_ptr_d->gamma_S_H*hbar/dt);
#endif
vector dh;
dh.x = random_h*normal_rand_d(rand_state_ptr_d + j);
dh.y = random_h*normal_rand_d(rand_state_ptr_d + j);
dh.z = random_h*normal_rand_d(rand_state_ptr_d + j);
atom_ptr->s = spin_rotation_d(dh, atom_ptr->s, dt);
//3rd part
s_temp = atom_ptr->s;
s_cross_Heff = vec_cross_d(s_temp, atom_ptr->Heff_H);
normalized_S_dot_H = 0e0;
if (atom_ptr->s0 > 0e0 && Heff_H0 > 0e0)
normalized_S_dot_H = vec_dot_d(s_temp, atom_ptr->Heff_H)/atom_ptr->s0/Heff_H0;
denominator = (1e0 + exp_2b + normalized_S_dot_H*(1e0 - exp_2b))*Heff_H0;
factor = (1e0 - exp_2b + normalized_S_dot_H*(1e0 + exp_2b - 2e0*cos_a*exp_b))*atom_ptr->s0;
atom_ptr->s = vec_divide_d( vec_add_d(vec_add_d(vec_times_d(2e0*cos_a*exp_b*Heff_H0, s_temp),
vec_times_d(2e0*sin_a*exp_b, s_cross_Heff)),
vec_times_d(factor, atom_ptr->Heff_H)), denominator);
#endif
#if defined SDHL || defined SLDHL
// In 5 parts.
//part 1
atom_ptr->s = spin_rotation_d(atom_ptr->Heff_H, atom_ptr->s, dt_half);
//part 2
double dt_quad = dt/4e0;
#ifdef SLDHL
double A = LandauA_d(atom_ptr->rho, LandauA_ptr_d, var_ptr_d);
double B = LandauB_d(atom_ptr->rho, LandauB_ptr_d, var_ptr_d);
double C = LandauC_d(atom_ptr->rho, LandauC_ptr_d, var_ptr_d);
double D = LandauD_d(atom_ptr->rho, LandauD_ptr_d, var_ptr_d);
#else
double A = LandauA_d(1, LandauA_ptr_d, var_ptr_d);
double B = LandauB_d(1, LandauB_ptr_d, var_ptr_d);
double C = LandauC_d(1, LandauC_ptr_d, var_ptr_d);
double D = LandauD_d(1, LandauD_ptr_d, var_ptr_d);
#endif
double s_sq;
//RK2
s_sq = vec_sq_d(atom_ptr->s);
atom_ptr->Heff_L = vec_times_d(-(2e0*A + 4e0*B*s_sq + 6e0*C*pow(s_sq,2) + 8e0*D*pow(s_sq,3)), atom_ptr->s);
#ifdef SLDHL
atom_ptr->sum_Jij_sj = 0e0;
inner_sum_Jij_sj_d(var_ptr_d, atom_ptr, first_cell_ptr_d, Jij_ptr_d);
atom_ptr->Heff_HC = vec_zero_d();
double s0;
s0 = sqrt(s_sq);
if (s0 > 0e0) atom_ptr->Heff_HC = vec_times_d(-atom_ptr->sum_Jij_sj/s0, atom_ptr->s);
atom_ptr->Heff_L = vec_add_d(atom_ptr->Heff_L, atom_ptr->Heff_HC);
#endif
vector s_temp = vec_add_d(atom_ptr->s, vec_times_d(var_ptr_d->gamma_S_HL*dt_quad, vec_add_d(atom_ptr->Heff_H, atom_ptr->Heff_L)));
s_sq = vec_sq_d(s_temp);
atom_ptr->Heff_L = vec_times_d(-(2e0*A + 4e0*B*s_sq + 6e0*C*pow(s_sq,2) + 8e0*D*pow(s_sq,3)), s_temp);
#ifdef SLDHL
atom_ptr->Heff_HC = vec_zero_d();
s0 = sqrt(s_sq);
if (s0 > 0e0) atom_ptr->Heff_HC = vec_times_d(-atom_ptr->sum_Jij_sj/s0, s_temp);
atom_ptr->Heff_L = vec_add_d(atom_ptr->Heff_L, atom_ptr->Heff_HC);
#endif
atom_ptr->s = vec_add_d(atom_ptr->s, vec_times_d(var_ptr_d->gamma_S_HL*dt_half, vec_add_d(atom_ptr->Heff_H, atom_ptr->Heff_L)));
//part 3
#ifdef eltemp
double random_S = sqrt(2e0*(first_cell_ptr_d+(atom_ptr->new_cell_index))->Te*var_ptr_d->gamma_S_HL/dt);
#else
double random_S = sqrt(2e0*var_ptr_d->temperature*var_ptr_d->gamma_S_HL/dt);
#endif
vector dS;
dS.x = random_S*normal_rand_d(rand_state_ptr_d + j);
dS.y = random_S*normal_rand_d(rand_state_ptr_d + j);
dS.z = random_S*normal_rand_d(rand_state_ptr_d + j);
atom_ptr->s = vec_add_d(atom_ptr->s, vec_times_d(dt, dS));
//part 4; RK2
s_sq = vec_sq_d(atom_ptr->s);
atom_ptr->Heff_L = vec_times_d(-(2e0*A + 4e0*B*s_sq + 6e0*C*pow(s_sq,2) + 8e0*D*pow(s_sq,3)), atom_ptr->s);
#ifdef SLDHL
atom_ptr->Heff_HC = vec_zero_d();
s0 = sqrt(s_sq);
if (s0 > 0e0) atom_ptr->Heff_HC = vec_times_d(-atom_ptr->sum_Jij_sj/s0, atom_ptr->s);
atom_ptr->Heff_L = vec_add_d(atom_ptr->Heff_L, atom_ptr->Heff_HC);
#endif
s_temp = vec_add_d(atom_ptr->s, vec_times_d(var_ptr_d->gamma_S_HL*dt_quad, vec_add_d(atom_ptr->Heff_H, atom_ptr->Heff_L)));
s_sq = vec_sq_d(s_temp);
atom_ptr->Heff_L = vec_times_d(-(2e0*A + 4e0*B*s_sq + 6e0*C*pow(s_sq,2) + 8e0*D*pow(s_sq,3)), s_temp);
#ifdef SLDHL
atom_ptr->Heff_HC = vec_zero_d();
s0 = sqrt(s_sq);
if (s0 > 0e0) atom_ptr->Heff_HC = vec_times_d(-atom_ptr->sum_Jij_sj/s0, s_temp);
atom_ptr->Heff_L = vec_add_d(atom_ptr->Heff_L, atom_ptr->Heff_HC);
#endif
atom_ptr->s = vec_add_d(atom_ptr->s, vec_times_d(var_ptr_d->gamma_S_HL*dt_half, vec_add_d(atom_ptr->Heff_H, atom_ptr->Heff_L)));
//part 5
atom_ptr->s = spin_rotation_d(atom_ptr->Heff_H, atom_ptr->s, dt_half);
#endif
#endif /*spinlang*/
atom_ptr->s0 = vec_length_d(atom_ptr->s);
}
__device__ vector spin_rotation_d(vector Heff, vector s, double dt){
vector omega = vec_divide_d(Heff, -hbar);
double omega0 = vec_length_d(omega);
if (omega0 > 0e0){
omega = vec_divide_d(omega, omega0);
} else {
omega = vec_zero_d();
}
double omega_12 = omega.x*omega.y;
double omega_23 = omega.y*omega.z;
double omega_13 = omega.x*omega.z;
double omega1_sq = omega.x*omega.x;
double omega2_sq = omega.y*omega.y;
double omega3_sq = omega.z*omega.z;
double A = sin(omega0*dt);
double B = 1e0 - cos(omega0*dt);
vector s_temp;
s_temp.x = s.x
+ (s.x*B*(-omega2_sq - omega3_sq)
+ s.y*(B*omega_12 - A*omega.z)
+ s.z*(A*omega.y + B*omega_13));
s_temp.y = s.y
+ (s.y*B*(-omega1_sq - omega3_sq)
+ s.z*(B*omega_23 - A*omega.x)
+ s.x*(A*omega.z + B*omega_12));
s_temp.z = s.z
+ (s.z*B*(-omega1_sq - omega2_sq)
+ s.x*(B*omega_13 - A*omega.y)
+ s.y*(A*omega.x + B*omega_23));
return s_temp;
}
__device__ void inner_spin_d(struct varGPU *var_ptr_d,
struct atom_struct *atom_ptr,
struct cell_struct *first_cell_ptr_d,
double *Jij_ptr_d)
{
struct atom_struct *work_ptr;
struct cell_struct *ccell_ptr;
struct cell_struct *wcell_ptr;
ccell_ptr = first_cell_ptr_d + atom_ptr->new_cell_index;
for (int i = 0; i <= 26; ++i){
if (i == 26)
wcell_ptr = ccell_ptr;
else
wcell_ptr = first_cell_ptr_d + (ccell_ptr->neigh_cell[i]);
work_ptr = wcell_ptr->head_ptr;
while (work_ptr != NULL){
vector rij = vec_sub_d(atom_ptr->r, work_ptr->r);
//find image of j closest to i
find_image_d(rij, var_ptr_d);
double rsq = vec_sq_d(rij);
if (rsq < var_ptr_d->rcut_mag_sq && atom_ptr != work_ptr){
double rij0 = sqrt(rsq);
double Jij_rij = Jij_d(rij0, Jij_ptr_d, var_ptr_d);
atom_ptr->Heff_H = vec_add_d(atom_ptr->Heff_H, vec_times_d(Jij_rij, work_ptr->s));
}
work_ptr = work_ptr->next_atom_ptr;
}
}
}
#ifdef SLDHL
__device__ void inner_sum_Jij_sj_d(struct varGPU *var_ptr_d,
struct atom_struct *atom_ptr,
struct cell_struct *first_cell_ptr_d,
double *Jij_ptr_d)
{
struct atom_struct *work_ptr;
struct cell_struct *ccell_ptr;
struct cell_struct *wcell_ptr;
ccell_ptr = first_cell_ptr_d + atom_ptr->new_cell_index;
for (int i = 0; i <= 26; ++i){
if (i == 26)
wcell_ptr = ccell_ptr;
else
wcell_ptr = first_cell_ptr_d + (ccell_ptr->neigh_cell[i]);
work_ptr = wcell_ptr->head_ptr;
while (work_ptr != NULL){
vector rij = vec_sub_d(atom_ptr->r, work_ptr->r);
//find image of j closest to i
find_image_d(rij, var_ptr_d);
double rsq = vec_sq_d(rij);
if (rsq < var_ptr_d->rcut_mag_sq && atom_ptr != work_ptr){
double rij0 = sqrt(rsq);
double Jij_rij = Jij_d(rij0, Jij_ptr_d, var_ptr_d);
double sj = vec_length_d(work_ptr->s);
atom_ptr->sum_Jij_sj += Jij_rij*sj;
}
work_ptr = work_ptr->next_atom_ptr;
}
}
}
#endif
#endif