-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid.c
357 lines (307 loc) · 9.4 KB
/
grid.c
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// This file is part of the ESPResSo distribution (http://www.espresso.mpg.de).
// It is therefore subject to the ESPResSo license agreement which you accepted upon receiving the distribution
// and by which you are legally bound while utilizing this file in any form or way.
// There is NO WARRANTY, not even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// You should have received a copy of that license along with this program;
// if not, refer to http://www.espresso.mpg.de/license.html where its current version can be found, or
// write to Max-Planck-Institute for Polymer Research, Theory Group, PO Box 3148, 55021 Mainz, Germany.
// Copyright (c) 2002-2009; all rights reserved unless otherwise stated.
/** \file grid.c Domain decomposition for parallel computing.
*
* For more information on the domain decomposition,
* see \ref grid.h "grid.h".
*/
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "utils.h"
#include "grid.h"
#include "communication.h"
#include "verlet.h"
#include "cells.h"
#include "interaction_data.h"
/************************************************
* defines
************************************************/
#define MAX_INTERACTION_RANGE 1e100
/**********************************************
* variables
**********************************************/
int node_grid[3] = { -1, -1, -1};
int node_pos[3] = {-1,-1,-1};
int node_neighbors[6] = {0, 0, 0, 0, 0, 0};
int boundary[6] = {0, 0, 0, 0, 0, 0};
int periodic = 7;
double box_l[3] = {1, 1, 1};
double box_l_i[3] = {1, 1, 1};
double min_box_l;
double local_box_l[3] = {1, 1, 1};
double min_local_box_l;
double my_left[3] = {0, 0, 0};
double my_right[3] = {1, 1, 1};
/************************************************************/
void setup_node_grid()
{
if (node_grid[0] < 0) {
/* auto setup, grid not set */
calc_3d_grid(n_nodes,node_grid);
mpi_bcast_parameter(FIELD_NODEGRID);
}
}
int node_grid_is_set()
{
return (node_grid[0] > 0);
}
int map_position_node_array(double pos[3])
{
int i, im[3]={0,0,0};
double f_pos[3];
for (i = 0; i < 3; i++)
f_pos[i] = pos[i];
fold_position(f_pos, im);
for (i = 0; i < 3; i++) {
im[i] = (int)floor(node_grid[i]*f_pos[i]*box_l_i[i]);
if (im[i] < 0)
im[i] = 0;
else if (im[i] >= node_grid[i])
im[i] = node_grid[i] - 1;
}
return map_array_node(im);
}
void map_node_array(int node, int pos[3])
{
get_grid_pos(node, pos, pos + 1, pos + 2, node_grid);
}
int map_array_node(int pos[3]) {
return get_linear_index(pos[0], pos[1], pos[2], node_grid);
}
void calc_node_neighbors(int node)
{
int dir,j;
int n_pos[3];
map_node_array(node,node_pos);
for(dir=0;dir<3;dir++) {
for(j=0;j<3;j++) n_pos[j]=node_pos[j];
/* left neighbor in direction dir */
n_pos[dir] = node_pos[dir] - 1;
if(n_pos[dir]<0) n_pos[dir] += node_grid[dir];
node_neighbors[2*dir] = map_array_node(n_pos);
/* right neighbor in direction dir */
n_pos[dir] = node_pos[dir] + 1;
if(n_pos[dir]>=node_grid[dir]) n_pos[dir] -= node_grid[dir];
node_neighbors[(2*dir)+1] = map_array_node(n_pos);
/* left boundary ? */
if (node_pos[dir] == 0) {
boundary[2*dir] = 1;
}
else {
boundary[2*dir] = 0;
}
/* right boundary ? */
if (node_pos[dir] == node_grid[dir]-1) {
boundary[2*dir+1] = -1;
}
else {
boundary[2*dir+1] = 0;
}
}
}
void grid_changed_box_l()
{
int i;
GRID_TRACE(fprintf(stderr,"%d: grid_changed_box_l:\n",this_node));
for(i = 0; i < 3; i++) {
local_box_l[i] = box_l[i]/(double)node_grid[i];
my_left[i] = node_pos[i] *local_box_l[i];
my_right[i] = (node_pos[i]+1)*local_box_l[i];
box_l_i[i] = 1/box_l[i];
}
calc_minimal_box_dimensions();
#ifdef GRID_DEBUG
fprintf(stderr,"%d: local_box_l = (%.3f, %.3f, %.3f)\n",this_node,
local_box_l[0],local_box_l[1],local_box_l[2]);
fprintf(stderr,"%d: coordinates: x in [%.3f, %.3f], y in [%.3f, %.3f], z in [%.3f, %.3f]\n",this_node,
my_left[0],my_right[0],my_left[1],my_right[1],my_left[2],my_right[2]);
#endif
}
void grid_changed_n_nodes()
{
GRID_TRACE(fprintf(stderr,"%d: grid_changed_n_nodes:\n",this_node));
calc_node_neighbors(this_node);
#ifdef GRID_DEBUG
fprintf(stderr,"%d: node_pos=(%d,%d,%d)\n",this_node,node_pos[0],node_pos[1],node_pos[2]);
fprintf(stderr,"%d: node_neighbors=(%d,%d,%d,%d,%d,%d)\n",this_node,
node_neighbors[0],node_neighbors[1],node_neighbors[2],
node_neighbors[3],node_neighbors[4],node_neighbors[5]);
fprintf(stderr,"%d: boundary=(%d,%d,%d,%d,%d,%d)\n",this_node,
boundary[0],boundary[1],boundary[2],boundary[3],boundary[4],boundary[5]);
#endif
}
void calc_minimal_box_dimensions()
{
int i;
min_box_l = 2*MAX_INTERACTION_RANGE;
min_local_box_l = MAX_INTERACTION_RANGE;
for(i=0;i<3;i++) {
/* #ifdef PARTIAL_PERIODIC
if(periodic[i]) {
min_box_l = dmin(min_box_l, box_l[i]);
min_local_box_l = dmin(min_local_box_l, local_box_l[i]);
}
#else
*/
min_box_l = dmin(min_box_l, box_l[i]);
min_local_box_l = dmin(min_local_box_l, local_box_l[i]);
/* #endif */
}
}
void calc_2d_grid(int n, int grid[3])
{
int i;
i = (int)sqrt((double)n);
while(i>=1) {
if(n%i==0) { grid[0] = n/i; grid[1] = i; grid[2] = 1; return; }
i--;
}
}
void calc_3d_grid(int n, int grid[3])
{
int i,j,k,max;
max = 3*n*n + 1;
/* generate grid in ascending order */
for(i=1;i<=n;i++) {
for(j=i;j<=n;j++) {
for(k=j;k<=n;k++) {
if(i*j*k == n && ((i*i)+(j*j)+(k*k)) < max) {
grid[0] = k; grid[1] = j;grid[2] = i;
max = ((i*i)+(j*j)+(k*k));
}
}
}
}
}
int map_3don2d_grid(int g3d[3],int g2d[3], int mult[3])
{
int i,row_dir=-1;
/* trivial case */
if(g3d[2]==1) {
for(i=0;i<3;i++) mult[i]=1;
return 2;
}
if(g2d[0]%g3d[0] == 0) {
if(g2d[1]%g3d[1] == 0) {row_dir=2; }
else if(g2d[1]%g3d[2] == 0) {row_dir=1; g2d[2]=g2d[1]; g2d[1]=1; }
}
else if(g2d[0]%g3d[1] == 0) {
if(g2d[1]%g3d[0]==0) {row_dir=2; i=g2d[0]; g2d[0]=g2d[1]; g2d[1]=i; }
else if(g2d[1]%g3d[2]==0) {row_dir=0; g2d[2]=g2d[1]; g2d[1]=g2d[0]; g2d[0]=1; }
}
else if(g2d[0]%g3d[2] == 0) {
if(g2d[1]%g3d[0]==0) {row_dir=1; g2d[2]=g2d[0]; g2d[0]=g2d[1]; g2d[1]=1; }
else if(g2d[1]%g3d[1]==0) {row_dir=0; g2d[2]=g2d[0]; g2d[0]=1; }
}
for(i=0;i<3;i++) mult[i]=g2d[i]/g3d[i];
return row_dir;
}
int node_grid_callback(Tcl_Interp *interp, void *_data)
{
int *data = (int *)_data;
if ((data[0] < 0) || (data[1] < 0) || (data[2] < 0)) {
Tcl_AppendResult(interp, "illegal value", (char *) NULL);
return (TCL_ERROR);
}
if (data[0]*data[1]*data[2] != n_nodes) {
Tcl_AppendResult(interp, "node grid does not fit n_nodes",
(char *) NULL);
return (TCL_ERROR);
}
/* outsourced to
sort_int_array(data,3); */
node_grid[0] = data[0];
node_grid[1] = data[1];
node_grid[2] = data[2];
mpi_bcast_parameter(FIELD_NODEGRID);
return (TCL_OK);
}
#ifdef PARTIAL_PERIODIC
int per_callback(Tcl_Interp *interp, void *_data)
{
periodic = *(int *)_data;
mpi_bcast_parameter(FIELD_PERIODIC);
return (TCL_OK);
}
#else
int per_callback(Tcl_Interp *interp, void *_data)
{
int tmp_periodic;
tmp_periodic = *(int *)_data;
if ((tmp_periodic & 7) == 7)
return (TCL_OK);
Tcl_AppendResult(interp, "periodic cannot be set since PARTIAL_PERIODIC not configured.", (char *)NULL);
return (TCL_ERROR);
}
#endif
int boxl_callback(Tcl_Interp *interp, void *_data)
{
double *data = _data;
if ((data[0] <= 0) || (data[1] <= 0) || (data[2] <= 0)) {
Tcl_AppendResult(interp, "illegal value", (char *) NULL);
return (TCL_ERROR);
}
box_l[0] = data[0];
box_l[1] = data[1];
box_l[2] = data[2];
mpi_bcast_parameter(FIELD_BOXL);
return (TCL_OK);
}
int change_volume(ClientData data, Tcl_Interp *interp, int argc, char **argv) {
char buffer[50 + TCL_DOUBLE_SPACE + TCL_INTEGER_SPACE];
char *mode;
double d_new = box_l[0];
int dir = -1;
if (argc < 2) {
Tcl_AppendResult(interp, "Wrong # of args! Usage: change_volume { <V_new> | <L_new> { x | y | z | xyz } }", (char *)NULL); return (TCL_ERROR);
}
if (Tcl_GetDouble(interp, argv[1], &d_new) == TCL_ERROR) return (TCL_ERROR);
if (argc == 3) {
mode = argv[2];
if (!strncmp(mode, "x", strlen(mode))) dir = 0;
else if (!strncmp(mode, "y", strlen(mode))) dir = 1;
else if (!strncmp(mode, "z", strlen(mode))) dir = 2;
else if (!strncmp(mode, "xyz", strlen(mode))) dir = 3;
}
else if (argc > 3) {
Tcl_AppendResult(interp, "Wrong # of args! Usage: change_volume { <V_new> | <L_new> { x | y | z | xyz } }", (char *)NULL); return (TCL_ERROR);
}
if (dir < 0) {
d_new = pow(d_new,1./3.);
rescale_boxl(3,d_new);
}
else {
rescale_boxl(dir,d_new);
}
sprintf(buffer, "%f", box_l[0]*box_l[1]*box_l[2]);
Tcl_AppendResult(interp, buffer, (char *)NULL);
return mpi_gather_runtime_errors(interp, TCL_OK);
}
void rescale_boxl(int dir, double d_new) {
double scale = (dir-3) ? d_new/box_l[dir] : d_new/box_l[0];
if (scale < 1.) {
mpi_rescale_particles(dir,scale);
if (dir < 3)
box_l[dir] = d_new;
else
box_l[0] = box_l[1] = box_l[2] = d_new;
mpi_bcast_parameter(FIELD_BOXL);
}
else if (scale > 1.) {
if (dir < 3)
box_l[dir] = d_new;
else
box_l[0] = box_l[1] = box_l[2] = d_new;
mpi_bcast_parameter(FIELD_BOXL);
mpi_rescale_particles(dir,scale);
}
}