forked from ludwig-cf/ludwig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfe_ternary_stats.c
338 lines (253 loc) · 8.39 KB
/
fe_ternary_stats.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
/*****************************************************************************
*
* fe_ternary_stats.c
*
* Report statistics for ternary model.
*
*
* Edinburgh Soft Matter and Statistical Physics Group and
* Edinburgh Parallel Computing Centre
*
* (c) 2019-2024 The University of Edinburgh
*
* Contributing authors:
* Kevin Stratford ([email protected])
*
*****************************************************************************/
#include <assert.h>
#include "pe.h"
#include "coords.h"
#include "kernel.h"
#include "fe_ternary_stats.h"
#define FE_PHI 0
#define FE_PSI 1
__host__ int fe_ternary_bulk(fe_ternary_t * fe, map_t * map, double * feb);
__host__ int fe_ternary_surf(fe_ternary_t * fe, map_t * map, double * fes);
__global__ void fe_ternary_bulk_kernel(kernel_3d_t k3d,
fe_ternary_t * fe, map_t * map,
double febulk[1]);
__global__ void fe_ternary_surf_kernel(kernel_3d_t k3d,
fe_ternary_param_t param,
field_t * field, map_t * map,
double fes[3]);
/*****************************************************************************
*
* fe_ternary_stats_info
*
* Top level driver for free energy statistics.
*
*****************************************************************************/
__host__ int fe_ternary_stats_info(fe_ternary_t * fe, wall_t * wall,
map_t * map, int nstep) {
double fe_local[5];
double fe_total[5];
pe_t * pe = NULL;
MPI_Comm comm;
assert(fe);
assert(map);
pe = fe->pe;
pe_mpi_comm(pe, &comm);
fe_local[0] = 0.0; /* Total free energy (fluid all sites) */
fe_local[1] = 0.0; /* Fluid only free energy */
fe_local[2] = 0.0; /* Volume of fluid */
fe_local[3] = 0.0; /* surface free energy */
fe_local[4] = 0.0; /* other wall free energy (walls only) */
fe_ternary_bulk(fe, map, fe_local);
if (wall_present(wall)) {
double fes_tot = 0.0;
fe_ternary_surf(fe, map, fe_local + 2);
MPI_Reduce(fe_local, fe_total, 5, MPI_DOUBLE, MPI_SUM, 0, comm);
fes_tot = fe_total[2] + fe_total[3] + fe_total[4];
/* Report is on two lines:
* time fes_rho fes_phi fes_psi
* time surface fluid total */
pe_info(pe, "\nFree energies\n");
pe_info(pe, "[rho/phi/psi] %9d %17.10e %17.10e %17.10e\n",
nstep, fe_total[2], fe_total[3], fe_total[4]);
pe_info(pe, "[surf/fl/tot] %9d %17.10e %17.10e %17.10e\n",
nstep, fes_tot, fe_total[0], fe_total[0] + fes_tot);
}
else {
/* Fluid only */
MPI_Reduce(fe_local, fe_total, 1, MPI_DOUBLE, MPI_SUM, 0, comm);
pe_info(pe, "\nFree energies\n");
pe_info(pe, "[surf/fl/tot] %9d %17.10e %17.10e %17.10e\n",
nstep, 0.0, fe_total[0], fe_total[0]);
}
return 0;
}
/*****************************************************************************
*
* fe_ternary_bulk
*
* Compute (bulk) fluid free energy.
*
*****************************************************************************/
__host__ int fe_ternary_bulk(fe_ternary_t * fe, map_t * map, double * feb) {
int nlocal[3] = {0};
assert(fe);
assert(map);
cs_nlocal(fe->cs, nlocal);
{
dim3 nblk = {};
dim3 ntpb = {};
cs_limits_t lim = {1, nlocal[X], 1, nlocal[Y], 1, nlocal[Z]};
kernel_3d_t k3d = kernel_3d(fe->cs, lim);
double * febd = NULL;
kernel_3d_launch_param(k3d.kiterations, &nblk, &ntpb);
tdpAssert(tdpMalloc((void **) &febd, sizeof(double)));
tdpAssert(tdpMemcpy(febd, feb, sizeof(double), tdpMemcpyHostToDevice));
tdpLaunchKernel(fe_ternary_bulk_kernel, nblk, ntpb, 0, 0,
k3d, fe->target, map->target, febd);
tdpAssert(tdpPeekAtLastError());
tdpAssert(tdpDeviceSynchronize());
tdpAssert(tdpMemcpy(feb, febd, sizeof(double), tdpMemcpyDeviceToHost));
tdpAssert(tdpFree(febd));
}
return 0;
}
/*****************************************************************************
*
* fe_ternary_surf
*
* Compute terms in the surface free energy.
* Three terms are returned: fes[0] = h rho
* fes[1] = h phi
* fes[2] = h psi
*
* Currently 2d only.
*
*****************************************************************************/
__host__ int fe_ternary_surf(fe_ternary_t * fe, map_t * map, double * fes) {
int nlocal[3];
fe_ternary_param_t param;
cs_nlocal(fe->cs, nlocal);
{
dim3 nblk = {};
dim3 ntpb = {};
cs_limits_t lim = {1, nlocal[X], 1, nlocal[Y], 1, 1};
kernel_3d_t k3d = kernel_3d(fe->cs, lim);
double * fesd = NULL;
kernel_3d_launch_param(k3d.kiterations, &nblk, &ntpb);
param = *fe->param;
tdpAssert(tdpMalloc((void **) &fesd, 3*sizeof(double)));
tdpAssert(tdpMemcpy(fesd, fes, 3*sizeof(double), tdpMemcpyHostToDevice));
tdpLaunchKernel(fe_ternary_surf_kernel, nblk, ntpb, 0, 0,
k3d, param, fe->phi->target, map->target, fesd);
tdpAssert(tdpPeekAtLastError());
tdpAssert(tdpDeviceSynchronize());
tdpAssert(tdpMemcpy(fes, fesd, 3*sizeof(double), tdpMemcpyDeviceToHost));
tdpAssert(tdpFree(fesd));
}
return 0;
}
/*****************************************************************************
*
* fe_ternary_bulk_kernel
*
* Accumulate the free energy density at fluid sites.
*
*****************************************************************************/
__global__ void fe_ternary_bulk_kernel(kernel_3d_t k3d, fe_ternary_t * fe,
map_t * map, double febulk[1]) {
int kindex = 0;
int tid;
double febl;
__shared__ double fepart[TARGET_MAX_THREADS_PER_BLOCK];
assert(fe);
assert(febulk);
tid = threadIdx.x;
fepart[tid] = 0.0;
for_simt_parallel(kindex, k3d.kiterations, 1) {
int status;
double fed;
int ic = kernel_3d_ic(&k3d, kindex);
int jc = kernel_3d_jc(&k3d, kindex);
int kc = kernel_3d_kc(&k3d, kindex);
int index = kernel_3d_cs_index(&k3d, ic, jc, kc);
map_status(map, index, &status);
if (status == MAP_FLUID) {
fe_ternary_fed(fe, index, &fed);
fepart[tid] += fed;
}
}
__syncthreads();
/* Reduction: block, and inter-block */
febl = tdpAtomicBlockAddDouble(fepart);
if (tid == 0) tdpAtomicAddDouble(febulk, febl);
}
/*****************************************************************************
*
* fe_ternary_surf_kernel
*
* Accumulate terms in the surface free energy:
* -h_1 (rho + phi - psi)/2
* -h_2 (rho - phi - psi)/2
* -h_3 psi
*
* or
*
* fes[0] = rho (-h_1 - h_2)/2
* fes[1] = phi (-h_1 + h_2)/2
* fes[2] = psi (+h_1 + h_2 - 2h_3)/2
*
*****************************************************************************/
__global__ void fe_ternary_surf_kernel(kernel_3d_t k3d,
fe_ternary_param_t param,
field_t * f,
map_t * map,
double fes[3]) {
int kindex;
int tid;
int bs_cv4[4][2] = {{-1,0}, {0,-1}, {0,1}, {1,0}};
double fesrhobl;
double fesphibl;
double fespsibl;
__shared__ double fesrho[TARGET_MAX_THREADS_PER_BLOCK];
__shared__ double fesphi[TARGET_MAX_THREADS_PER_BLOCK];
__shared__ double fespsi[TARGET_MAX_THREADS_PER_BLOCK];
assert(f);
tid = threadIdx.x;
fesrho[tid] = 0.0;
fesphi[tid] = 0.0;
fespsi[tid] = 0.0;
for_simt_parallel(kindex, k3d.kiterations, 1) {
int ic1, jc1;
int index, inext, p;
int status0, status1;
int ic = kernel_3d_ic(&k3d, kindex);
int jc = kernel_3d_jc(&k3d, kindex);
int kc = 1;
index = kernel_3d_cs_index(&k3d, ic, jc, kc);
map_status(map, index, &status0);
if (status0 == MAP_FLUID) {
/* Look at each neighbour; count only nearest neighbours */
for (p = 0; p < 4; p++) {
ic1 = ic + bs_cv4[p][X];
jc1 = jc + bs_cv4[p][Y];
inext = kernel_3d_cs_index(&k3d, ic1, jc1, kc);
map_status(map, inext, &status1);
if (status1 == MAP_BOUNDARY) {
double rho = 1.0;
double phi = f->data[addr_rank1(f->nsites, f->nf, index, FE_PHI)];
double psi = f->data[addr_rank1(f->nsites, f->nf, index, FE_PSI)];
fesrho[tid] += rho*0.5*(-param.h1 - param.h2);
fesphi[tid] += phi*0.5*(-param.h1 + param.h2);
fespsi[tid] += psi*0.5*( param.h1 + param.h2 - 2.0*param.h3);
}
}
}
/* Next site */
}
__syncthreads();
/* Reduction: block, then inter-block */
fesrhobl = tdpAtomicBlockAddDouble(fesrho);
fesphibl = tdpAtomicBlockAddDouble(fesphi);
fespsibl = tdpAtomicBlockAddDouble(fespsi);
if (tid == 0) {
tdpAtomicAddDouble(fes + 0, fesrhobl);
tdpAtomicAddDouble(fes + 1, fesphibl);
tdpAtomicAddDouble(fes + 2, fespsibl);
}
return;
}