forked from ALaDyn/piccante
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrent.cpp
341 lines (294 loc) · 10.7 KB
/
current.cpp
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
330
331
332
333
334
335
336
337
338
339
340
/* Copyright 2014 - Andrea Sgattoni, Luca Fedeli, Stefano Sinigardi */
/*******************************************************************************
This file is part of piccante.
piccante is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
piccante is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with piccante. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#include "current.h"
CURRENT::CURRENT()
{
allocated = 0;
ZGrid_factor = YGrid_factor = 1;
}
CURRENT::~CURRENT()
{
free(val);
}
void CURRENT::allocate(GRID *grid) //field allocation
{
mygrid = grid;
mygrid->alloc_number(N_grid, mygrid->Nloc);
Ntot = ((uint64_t)N_grid[0]) * ((uint64_t)N_grid[1]) * ((uint64_t)N_grid[2]);
if (N_grid[2] == 1)
ZGrid_factor = 0;
if (N_grid[1] == 1)
YGrid_factor = 0;
Ncomp = 4;
#ifndef NO_ALLOCATION
val = (double *)malloc(Ntot*Ncomp*sizeof(double));
allocated = 1;
#endif
}
//REALLOCATION only if load balancing is introduced
void CURRENT::reallocate()
{
if (!allocated){
printf("ERROR: reallocate\n");
exit(17);
}
mygrid->alloc_number(N_grid, mygrid->Nloc);
Ntot = ((uint64_t)N_grid[0]) * ((uint64_t)N_grid[1]) * ((uint64_t)N_grid[2]);
if (N_grid[2] == 1)
ZGrid_factor = 0;
if (N_grid[1] == 1)
YGrid_factor = 0;
#ifndef NO_ALLOCATION
val = (double *)realloc((void*)val, Ntot*Ncomp*sizeof(double));
#endif
}
//set all values to zero!
void CURRENT::setAllValuesToZero() //set all the values to zero
{
if (allocated)
memset((void*)val, 0, Ntot*Ncomp*sizeof(double));
else
{
#ifndef NO_ALLOCATION
printf("ERROR: current.setAllValuesToZero impossible\n");
exit(17);
#else
return;
#endif
}
}
CURRENT CURRENT::operator = (CURRENT &destro)
{
if (!destro.allocated){
printf("---ERROR---\noperation not permitted\nCURRENT=CURRENT\nnot allocated\n");
exit(17);
}
Ncomp = destro.Ncomp;
mygrid = destro.mygrid;
if (!allocated){
allocate(destro.mygrid);
}
else reallocate();
memcpy((void*)val, (void*)destro.val, Ntot*Ncomp*sizeof(double));
return *this;
}
double *CURRENT::getDataPointer(){
return val;
}
void CURRENT::writeN_grid(int *N_grid){
N_grid[0]=this->N_grid[0];
N_grid[1]=this->N_grid[1];
N_grid[2]=this->N_grid[2];
}
integer_or_halfinteger CURRENT::getJCoords(int c){
integer_or_halfinteger crd;
switch (c){
case 0: //Jx
crd.x = _HALF_CRD; crd.y = _INTG_CRD; crd.z = _INTG_CRD;
break;
case 1://Jy
crd.x = _INTG_CRD; crd.y = _HALF_CRD; crd.z = _INTG_CRD;
break;
case 2://Jz
crd.x = _INTG_CRD; crd.y = _INTG_CRD; crd.z = _HALF_CRD;
break;
default:
crd.x = _NULL_CRD; crd.y = _NULL_CRD; crd.z = _NULL_CRD;
break;
}
return crd;
}
integer_or_halfinteger CURRENT::getDensityCoords(){
integer_or_halfinteger crd;
crd.x = _INTG_CRD; crd.y = _INTG_CRD; crd.z = _INTG_CRD;
return crd;
}
// double & CURRENT::Jx(int i,int j,int k){return val[acc.indice(0,i,j,k,N_grid[0],N_grid[1],N_grid[2],Ncomp)];}
// double & CURRENT::Jy(int i,int j,int k){return val[acc.indice(1,i,j,k,N_grid[0],N_grid[1],N_grid[2],Ncomp)];}
// double & CURRENT::Jz(int i,int j,int k){return val[acc.indice(2,i,j,k,N_grid[0],N_grid[1],N_grid[2],Ncomp)];}
// double & CURRENT::density(int i,int j,int k){return val[acc.indice(0,i,j,k,N_grid[0],N_grid[1],N_grid[2],Ncomp)];}
// //double * CURRENT::pointerJ(int i,int j,int k){return (val+ acc.indice(i,j,k,N_grid[0],N_grid[1],N_grid[2],Ncomp));}
// double & CURRENT::JJ(int c,int i,int j,int k){return val[acc.indice(c,i,j,k,N_grid[0],N_grid[1],N_grid[2],Ncomp)];}
void CURRENT::pbc()
{
// add all the field of the "current" field of the periodic boundary region so
// that the contribution J(0)=J(0)+J(N-1) J(N-1)=J(0) and J(1)=J(1)+J(Nx)
// same is done also for the charge density components of the field
int i, j, k, c;
int Nx, Ny, Nz, Nc = Ncomp;
int Ngx, Ngy, Ngz, sendcount;
int dimensions = mygrid->getDimensionality();
int edge = mygrid->getEdge();
int Nxchng = 2 * edge + 1;//, istart=(Nxchng-1)/2;
// number of points to exchange
// (i.e. -2,-1,0,1,2 e.g istart, istart+1,istart+2,istart+3,istart+(Nxchng-1)
double *send_buffer, *recv_buffer;
MPI_Status status;
int iright, ileft;
Nx = mygrid->Nloc[0];
Ny = mygrid->Nloc[1];
Nz = mygrid->Nloc[2];
Ngx = N_grid[0];
Ngy = N_grid[1];
Ngz = N_grid[2];
if (dimensions == 3)
{
//send boundaries along z
sendcount = Ngx*Ngy*Nxchng*Nc;
send_buffer = (double *)malloc(sendcount*sizeof(double));
recv_buffer = (double *)malloc(sendcount*sizeof(double));
// ====== send right: send_buff=right_edge
for (k = 0; k < Nxchng; k++)
for (j = 0; j < Ngy; j++)
for (i = 0; i < Ngx; i++)
for (c = 0; c < Nc; c++)
{
send_buffer[c + i*Nc + j*Nc*Ngx + k*Nc*Ngx*Ngy] = JJ(c, i - edge, j - edge, (Nz - 1) - edge + k);
}
// ====== send edge to right receive from left
MPI_Cart_shift(mygrid->cart_comm, 2, 1, &ileft, &iright);
memset((void*)recv_buffer, 0, sendcount*sizeof(double));
MPI_Sendrecv(send_buffer, sendcount, MPI_DOUBLE, iright, 13,
recv_buffer, sendcount, MPI_DOUBLE, ileft, 13,
MPI_COMM_WORLD, &status);
// ====== add recv_buffer to left_edge and send back to left the result
for (k = 0; k < Nxchng; k++)
for (j = 0; j < Ngy; j++)
for (i = 0; i < Ngx; i++)
for (c = 0; c < Nc; c++)
{
JJ(c, i - edge, j - edge, k - edge) += recv_buffer[c + i*Nc + j*Nc*Ngx + k*Nc*Ngx*Ngy];
send_buffer[c + i*Nc + j*Nc*Ngx + k*Nc*Ngx*Ngy] = JJ(c, i - edge, j - edge, k - edge);
}
// ====== send to left receive from right
memset((void*)recv_buffer, 0, sendcount*sizeof(double));
MPI_Sendrecv(send_buffer, sendcount, MPI_DOUBLE, ileft, 13,
recv_buffer, sendcount, MPI_DOUBLE, iright, 13,
MPI_COMM_WORLD, &status);
// ====== copy recv_buffer to the right edge
for (k = 0; k < Nxchng; k++)
for (j = 0; j < Ngy; j++)
for (i = 0; i < Ngx; i++)
for (c = 0; c < Nc; c++)
{
JJ(c, i - edge, j - edge, (Nz - 1) - edge + k) = recv_buffer[c + i*Nc + j*Nc*Ngx + k*Nc*Ngx*Ngy];
}
// ===== finished, now free the send recv buffers
free(send_buffer);
free(recv_buffer);
}
if (dimensions >= 2)
{
// =============== send boundaries along y ============
sendcount = Ngx*Nxchng*Ngz*Nc;
send_buffer = (double *)malloc(sendcount*sizeof(double));
recv_buffer = (double *)malloc(sendcount*sizeof(double));
// ====== send right: send_buff=right_edge
for (k = 0; k < Ngz; k++)
for (j = 0; j < Nxchng; j++)
for (i = 0; i < Ngx; i++)
for (c = 0; c < Nc; c++)
{
send_buffer[c + i*Nc + j*Nc*Ngx + k*Nc*Ngx*Nxchng] = JJ(c, i - edge, (Ny - 1) - edge + j, k - edge);
}
// ====== send edge to right receive from left
MPI_Cart_shift(mygrid->cart_comm, 1, 1, &ileft, &iright);
memset((void*)recv_buffer, 0, sendcount*sizeof(double));
MPI_Sendrecv(send_buffer, sendcount, MPI_DOUBLE, iright, 13,
recv_buffer, sendcount, MPI_DOUBLE, ileft, 13,
MPI_COMM_WORLD, &status);
// ====== add recv_buffer to left_edge and send back to left the result
for (k = 0; k < Ngz; k++)
for (j = 0; j < Nxchng; j++)
for (i = 0; i < Ngx; i++)
for (c = 0; c < Nc; c++)
{
JJ(c, i - edge, j - edge, k - edge) += recv_buffer[c + i*Nc + j*Nc*Ngx + k*Nc*Ngx*Nxchng];
send_buffer[c + i*Nc + j*Nc*Ngx + k*Nc*Ngx*Nxchng] = JJ(c, i - edge, j - edge, k - edge);
}
// ====== send to left receive from right
memset((void*)recv_buffer, 0, sendcount*sizeof(double));
MPI_Sendrecv(send_buffer, sendcount, MPI_DOUBLE, ileft, 13,
recv_buffer, sendcount, MPI_DOUBLE, iright, 13,
MPI_COMM_WORLD, &status);
// ====== copy recv_buffer to the right edge
for (k = 0; k < Ngz; k++)
for (j = 0; j < Nxchng; j++)
for (i = 0; i < Ngx; i++)
for (c = 0; c < Nc; c++)
{
JJ(c, i - edge, (Ny - 1) - edge + j, k - edge) = recv_buffer[c + i*Nc + j*Nc*Ngx + k*Nc*Ngx*Nxchng];
}
// ===== finished, now free the send recv buffers
free(send_buffer);
free(recv_buffer);
}
//send boundaries along x
sendcount = Nxchng*Ngy*Ngz*Nc;
send_buffer = (double *)malloc(sendcount*sizeof(double));
recv_buffer = (double *)malloc(sendcount*sizeof(double));
// ====== send right: send_buff=right_edge
for (k = 0; k < Ngz; k++)
for (j = 0; j < Ngy; j++)
for (i = 0; i < Nxchng; i++)
for (c = 0; c < Nc; c++)
{
send_buffer[c + i*Nc + j*Nc*Nxchng + k*Nc*Nxchng*Ngy] = JJ(c, (Nx - 1) - edge + i, j - edge, k - edge);
}
// ====== send edge to right receive from left
MPI_Cart_shift(mygrid->cart_comm, 0, 1, &ileft, &iright);
memset((void*)recv_buffer, 0, sendcount*sizeof(double));
MPI_Sendrecv(send_buffer, sendcount, MPI_DOUBLE, iright, 13,
recv_buffer, sendcount, MPI_DOUBLE, ileft, 13,
MPI_COMM_WORLD, &status);
// ====== add recv_buffer to left_edge and send back to left the result
for (k = 0; k < Ngz; k++)
for (j = 0; j < Ngy; j++)
for (i = 0; i < Nxchng; i++)
for (c = 0; c < Nc; c++)
{
JJ(c, i - edge, j - edge, k - edge) += recv_buffer[c + i*Nc + j*Nc*Nxchng + k*Nc*Nxchng*Ngy];
send_buffer[c + i*Nc + j*Nc*Nxchng + k*Nc*Nxchng*Ngy] = JJ(c, i - edge, j - edge, k - edge);
}
// ====== send to left receive from right
memset((void*)recv_buffer, 0, sendcount*sizeof(double));
MPI_Sendrecv(send_buffer, sendcount, MPI_DOUBLE, ileft, 13,
recv_buffer, sendcount, MPI_DOUBLE, iright, 13,
MPI_COMM_WORLD, &status);
// ====== copy recv_buffer to the right edge
for (k = 0; k < Ngz; k++)
for (j = 0; j < Ngy; j++)
for (i = 0; i < Nxchng; i++)
for (c = 0; c < Nc; c++)
{
JJ(c, (Nx - 1) - edge + i, j - edge, k - edge) = recv_buffer[c + i*Nc + j*Nc*Nxchng + k*Nc*Nxchng*Ngy];
}
// ===== finished, now free the send recv buffers
free(send_buffer);
free(recv_buffer);
}
void CURRENT::eraseDensity(){
int i, j, k;
int Ngx = N_grid[0];
int Ngy = N_grid[1];
int Ngz = N_grid[2];
int edge = mygrid->getEdge();
for (k = 0; k < Ngz; k++)
for (j = 0; j < Ngy; j++)
for (i = 0; i < Ngx; i++)
{
density(i - edge, j - edge, k - edge) = 0.0;
}
}