-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenergy.c
417 lines (362 loc) · 12.3 KB
/
energy.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// 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-2006; all rights reserved unless otherwise stated.
/** \file energy.c
Implementation of \ref energy.h "energy.h".
*/
#include "energy.h"
#include "parser.h"
#include "cells.h"
#include "integrate.h"
#include "initialize.h"
#include "domain_decomposition.h"
#include "nsquare.h"
#include "layered.h"
#include "elc.h"
#include "magnetic_non_p3m__methods.h"
#include "mdlc_correction.h"
Observable_stat energy = {0, {NULL,0,0}, 0,0,0};
Observable_stat total_energy = {0, {NULL,0,0}, 0,0,0};
/************************************************************/
/* local prototypes */
/************************************************************/
/** Calculate long range energies (P3M, EWALD, MMM2d...). */
void calc_long_range_energies();
/** allocate energy arrays and initialize with zero */
void init_energies(Observable_stat *stat);
/** on the master node: calc energies only if necessary */
void master_energy_calc();
/************************************************************/
void energy_calc(double *result)
{
if (!check_obs_calc_initialized())
return;
init_energies(&energy);
on_observable_calc();
switch (cell_structure.type) {
case CELL_STRUCTURE_LAYERED:
layered_calculate_energies();
break;
case CELL_STRUCTURE_DOMDEC:
if(dd.use_vList) {
if (rebuild_verletlist)
build_verlet_lists();
calculate_verlet_energies();
}
else
calculate_link_cell_energies();
break;
case CELL_STRUCTURE_NSQUARE:
nsq_calculate_energies();
}
/* rescale kinetic energy */
energy.data.e[0] /= (2.0*time_step*time_step);
calc_long_range_energies();
/* gather data */
MPI_Reduce(energy.data.e, result, energy.data.n, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
}
/************************************************************/
void calc_long_range_energies()
{
#ifdef ELECTROSTATICS
/* calculate k-space part of electrostatic interaction. */
switch (coulomb.method) {
#ifdef ELP3M
case COULOMB_P3M:
P3M_charge_assign();
energy.coulomb[1] = P3M_calc_kspace_forces_for_charges(0,1);
break;
case COULOMB_ELC_P3M:
// assign the original charges first
// they may not have been assigned yet
P3M_charge_assign();
if(!elc_params.dielectric_contrast_on)
energy.coulomb[1] = P3M_calc_kspace_forces_for_charges(0,1);
else {
energy.coulomb[1] = 0.5*P3M_calc_kspace_forces_for_charges(0,1);
energy.coulomb[1]+= 0.5*ELC_P3M_dielectric_layers_energy_self();
// assign both original and image charges now
ELC_P3M_charge_assign_both();
ELC_P3M_modify_p3m_sums_both();
energy.coulomb[1] += 0.5*P3M_calc_kspace_forces_for_charges(0,1);
//assign only the image charges now
ELC_P3M_charge_assign_image();
ELC_P3M_modify_p3m_sums_image();
energy.coulomb[1]-= 0.5*P3M_calc_kspace_forces_for_charges(0,1);
}
energy.coulomb[2] = ELC_energy();
break;
#endif
case COULOMB_EWALD:
energy.coulomb[1] = EWALD_calc_kspace_forces(0,1);
EWALD_TRACE(fprintf(stderr,"%d: EWALD: energy.coulomb[1]=%g\n",this_node,energy.coulomb[1]));
break;
case COULOMB_MMM2D:
*energy.coulomb += MMM2D_far_energy();
*energy.coulomb += MMM2D_dielectric_layers_energy_contribution();
break;
/* calculate electric part of energy (only for MAGGS) */
case COULOMB_MAGGS:
*energy.coulomb += maggs_electric_energy();
break;
}
#endif /* ifdef ELECTROSTATICS */
#ifdef MAGNETOSTATICS
switch (coulomb.Dmethod) {
#ifdef ELP3M
case DIPOLAR_P3M:
P3M_dipole_assign();
energy.dipolar[1] = P3M_calc_kspace_forces_for_dipoles(0,1);
break;
#ifdef MDLC
case DIPOLAR_MDLC_P3M:
P3M_dipole_assign();
energy.dipolar[1] = P3M_calc_kspace_forces_for_dipoles(0,1);
energy.dipolar[2] =add_mdlc_energy_corrections();
break;
#endif
#endif
#ifdef DAWAANR
case DIPOLAR_ALL_WITH_ALL_AND_NO_REPLICA:
energy.dipolar[1] = dawaanr_calculations(0,1);
break;
#endif
#ifdef MAGNETIC_DIPOLAR_DIRECT_SUM
#ifdef MDLC
case DIPOLAR_MDLC_DS:
energy.dipolar[1] = magnetic_dipolar_direct_sum_calculations(0,1);
energy.dipolar[2] =add_mdlc_energy_corrections();
break;
#endif
case DIPOLAR_DS:
energy.dipolar[1] = magnetic_dipolar_direct_sum_calculations(0,1);
break;
#endif
}
#endif /* ifdef MAGNETOSTATICS */
}
/************************************************************/
void init_energies(Observable_stat *stat)
{
int n_pre, n_non_bonded, n_coulomb, n_dipolar;
n_pre = 1;
n_non_bonded = (n_particle_types*(n_particle_types+1))/2;
n_coulomb = 0;
#ifdef ELECTROSTATICS
switch (coulomb.method) {
case COULOMB_NONE: n_coulomb = 0; break;
#ifdef ELP3M
case COULOMB_ELC_P3M: n_coulomb = 3; break;
case COULOMB_P3M: n_coulomb = 2; break;
#endif
case COULOMB_EWALD: n_coulomb = 2; break;
default: n_coulomb = 1;
}
#endif
n_dipolar = 0;
#ifdef MAGNETOSTATICS
switch (coulomb.Dmethod) {
case DIPOLAR_NONE: n_dipolar = 0; break;
#ifdef ELP3M
case DIPOLAR_MDLC_P3M: n_dipolar=3; break;
case DIPOLAR_P3M: n_dipolar = 2; break;
#endif
#ifdef DAWAANR
case DIPOLAR_ALL_WITH_ALL_AND_NO_REPLICA: n_dipolar = 2; break;
#endif
#ifdef MAGNETIC_DIPOLAR_DIRECT_SUM
case DIPOLAR_MDLC_DS: n_dipolar=3; break;
case DIPOLAR_DS: n_dipolar = 2; break;
#endif
}
#endif
obsstat_realloc_and_clear(stat, n_pre, n_bonded_ia, n_non_bonded, n_coulomb, n_dipolar, 1);
stat->init_status = 0;
}
/************************************************************/
void master_energy_calc() {
mpi_gather_stats(1, total_energy.data.e, NULL, NULL, NULL);
total_energy.init_status=1;
}
/****************************************************************************************
* parser
****************************************************************************************/
static void print_detailed_energies(Tcl_Interp *interp)
{
char buffer[TCL_DOUBLE_SPACE + TCL_INTEGER_SPACE + 2];
double value;
int i, j;
value = total_energy.data.e[0];
for (i = 1; i < total_energy.data.n; i++)
value += total_energy.data.e[i];
Tcl_PrintDouble(interp, value, buffer);
Tcl_AppendResult(interp, "{ energy ", buffer, " } ", (char *)NULL);
Tcl_PrintDouble(interp, total_energy.data.e[0], buffer);
Tcl_AppendResult(interp, "{ kinetic ", buffer, " } ", (char *)NULL);
for(i=0;i<n_bonded_ia;i++) {
if (bonded_ia_params[i].type != BONDED_IA_NONE) {
sprintf(buffer, "%d ", i);
Tcl_AppendResult(interp, "{ ", buffer, (char *)NULL);
Tcl_PrintDouble(interp, *obsstat_bonded(&total_energy, i), buffer);
Tcl_AppendResult(interp,
get_name_of_bonded_ia(bonded_ia_params[i].type),
" ", buffer, " } ", (char *) NULL);
}
}
for (i = 0; i < n_particle_types; i++)
for (j = i; j < n_particle_types; j++) {
if (checkIfParticlesInteract(i, j)) {
sprintf(buffer, "%d ", i);
Tcl_AppendResult(interp, "{ ", buffer, (char *)NULL);
sprintf(buffer, "%d ", j);
Tcl_AppendResult(interp, " ", buffer, (char *)NULL);
Tcl_PrintDouble(interp, *obsstat_nonbonded(&total_energy, i, j), buffer);
Tcl_AppendResult(interp, "nonbonded ", buffer, " } ", (char *)NULL);
}
}
#if defined(ELECTROSTATICS) || defined(MAGNETOSTATICS)
if(
#ifdef ELECTROSTATICS
coulomb.method != COULOMB_NONE
#else
0
#endif
||
#ifdef MAGNETOSTATICS
coulomb.Dmethod != DIPOLAR_NONE
#else
0
#endif
) {
/* total Coulomb energy */
value = 0;
for (i = 0; i < total_energy.n_coulomb; i++)
value += total_energy.coulomb[i];
for (i = 0; i < total_energy.n_dipolar; i++)
value += total_energy.dipolar[i];
Tcl_PrintDouble(interp, value, buffer);
#if defined(ELECTROSTATICS) && defined(MAGNETOSTATICS)
Tcl_AppendResult(interp, "{ coulomb+magdipoles ", buffer, (char *)NULL);
#else
#ifndef MAGNETOSTATICS
Tcl_AppendResult(interp, "{ coulomb ", buffer, (char *)NULL);
#endif
#ifndef ELECTROSTATICS
Tcl_AppendResult(interp, "{ magdipoles ", buffer, (char *)NULL);
#endif
#endif
/* if it is split up, then print the split up parts */
if (total_energy.n_coulomb > 1) {
for (i = 0; i < total_energy.n_coulomb; i++) {
Tcl_PrintDouble(interp, total_energy.coulomb[i], buffer);
Tcl_AppendResult(interp, " ", buffer, (char *)NULL);
}
}
if (total_energy.n_dipolar > 1) {
for (i = 0; i < total_energy.n_dipolar; i++) {
Tcl_PrintDouble(interp, total_energy.dipolar[i], buffer);
Tcl_AppendResult(interp, " ", buffer, (char *)NULL);
}
}
Tcl_AppendResult(interp, " }", (char *)NULL);
}
#endif
}
/************************************************************/
int parse_and_print_energy(Tcl_Interp *interp, int argc, char **argv)
{
/* 'analyze energy [{ fene <type_num> | harmonic <type_num> | subt_lj_harm <type_num> | subt_lj_fene <type_num> | subt_lj <type_num> | lj <type1> <type2> | ljcos <type1> <type2> | ljcos2 <type1> <type2> | gb <type1> <type2> | coulomb | kinetic | total }]' */
char buffer[TCL_DOUBLE_SPACE + TCL_INTEGER_SPACE + 2];
int i, j;
double value;
value = 0.0;
if (n_total_particles == 0) {
Tcl_AppendResult(interp, "(no particles)",
(char *)NULL);
return (TCL_OK);
}
if (total_energy.init_status == 0) {
init_energies(&total_energy);
master_energy_calc();
}
if (argc == 0)
print_detailed_energies(interp);
else {
if (ARG0_IS_S("kinetic"))
value = total_energy.data.e[0];
else if (ARG0_IS_S("bonded") ||
ARG0_IS_S("fene") ||
ARG0_IS_S("subt_lj_harm") ||
ARG0_IS_S("subt_lj_fene") ||
ARG0_IS_S("subt_lj") ||
ARG0_IS_S("harmonic") ||
ARG0_IS_S("endangledist")) {
if(argc<2 || ! ARG1_IS_I(i)) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "wrong # or type of arguments for: analyze energy bonded <type_num>",
(char *)NULL);
return (TCL_ERROR);
}
if(i < 0 || i >= n_bonded_ia) {
Tcl_AppendResult(interp, "bond type does not exist", (char *)NULL);
return (TCL_ERROR);
}
value = *obsstat_bonded(&total_energy, i);
}
else if (ARG0_IS_S("nonbonded") ||
ARG0_IS_S("lj") ||
ARG0_IS_S("buckingham") ||
ARG0_IS_S("lj-cos") ||
ARG0_IS_S("lj-cos2") ||
ARG0_IS_S("gb") ||
ARG0_IS_S("tabulated")) {
if(argc<3 || ! ARG_IS_I(1, i) || ! ARG_IS_I(2, j)) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "wrong # or type of arguments for: analyze energy nonbonded <type1> <type2>",
(char *)NULL);
return (TCL_ERROR);
}
if(i < 0 || i >= n_particle_types || j < 0 || j >= n_particle_types) {
Tcl_AppendResult(interp, "particle type does not exist", (char *)NULL);
return (TCL_ERROR);
}
value = *obsstat_nonbonded(&total_energy, i, j);
}
else if( ARG0_IS_S("coulomb")) {
#ifdef ELECTROSTATICS
value = 0;
for (i = 0; i < total_energy.n_coulomb; i++)
value += total_energy.coulomb[i];
#else
Tcl_AppendResult(interp, "ELECTROSTATICS not compiled (see config.h)\n", (char *)NULL);
#endif
}
else if( ARG0_IS_S("magnetic")) {
#ifdef MAGNETOSTATICS
value = 0;
for (i = 0; i < total_energy.n_dipolar; i++)
value += total_energy.dipolar[i];
#else
Tcl_AppendResult(interp, "MAGNETOSTATICS not compiled (see config.h)\n", (char *)NULL);
#endif
}
else if (ARG0_IS_S("total")) {
value = total_energy.data.e[0];
for (i = 1; i < total_energy.data.n; i++)
value += total_energy.data.e[i];
}
else {
Tcl_AppendResult(interp, "unknown feature of: analyze energy",
(char *)NULL);
return (TCL_ERROR);
}
Tcl_PrintDouble(interp, value, buffer);
Tcl_AppendResult(interp, buffer, (char *)NULL);
}
return (TCL_OK);
}