-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcolvarproxy_lammps.cpp
More file actions
290 lines (227 loc) · 8.61 KB
/
colvarproxy_lammps.cpp
File metadata and controls
290 lines (227 loc) · 8.61 KB
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
// clang-format off
// -*- c++ -*-
// This file is part of the Collective Variables module (Colvars).
// The original version of Colvars and its updates are located at:
// https://github.com/Colvars/colvars
// Please update all Colvars source files before making any changes.
// If you wish to distribute your changes, please submit them to the
// Colvars repository at GitHub.
#include "colvarproxy_lammps.h"
#include "colvarproxy_lammps_version.h" // IWYU pragma: export
#include "domain.h"
#include "error.h"
#include "force.h"
#include "random_park.h"
#include "update.h"
#include "colvarmodule.h"
#include "colvarproxy.h"
#include "colvarscript.h"
#include "colvartypes.h"
#define HASH_FAIL -1
/* ---------------------------------------------------------------------- */
colvarproxy_lammps::colvarproxy_lammps(LAMMPS_NS::LAMMPS *lmp) : _lmp(lmp), _random(nullptr)
{
engine_name_ = "LAMMPS";
first_timestep = true;
previous_step = -1;
do_exit = false;
bias_energy = 0.0;
engine_ready_ = false;
}
/* ---------------------------------------------------------------------- */
void colvarproxy_lammps::init()
{
version_int = get_version_from_string(COLVARPROXY_VERSION);
// create the colvarmodule instance
colvars = new colvarmodule();
colvars->init(this);
// Create instance of scripting interface
script = new colvarscript(this, colvars);
cvm::log("Using LAMMPS interface, version " + cvm::to_str(COLVARPROXY_VERSION) + ".\n");
colvars->cite_feature("LAMMPS engine");
colvars->cite_feature("Colvars-LAMMPS interface");
angstrom_value_ = _lmp->force->angstrom;
boltzmann_ = _lmp->force->boltz;
set_integration_timestep(_lmp->update->dt * _lmp->force->femtosecond);
if (_lmp->update->ntimestep != 0) {
colvars->set_initial_step(static_cast<cvm::step_number>(_lmp->update->ntimestep));
}
}
/* ---------------------------------------------------------------------- */
colvarproxy_lammps::~colvarproxy_lammps()
{
if (_random) delete _random;
}
/* ---------------------------------------------------------------------- */
void colvarproxy_lammps::set_random_seed(int seed)
{
if (_random) delete _random;
_random = new LAMMPS_NS::RanPark(_lmp, seed);
}
cvm::real colvarproxy_lammps::rand_gaussian()
{
return _random->gaussian();
}
/* ----------------------------------------------------------------------
re-initialize data where needed
------------------------------------------------------------------------- */
int colvarproxy_lammps::setup()
{
int error_code = colvarproxy::setup();
set_integration_timestep(_lmp->update->dt * _lmp->force->femtosecond);
error_code |= colvars->update_engine_parameters();
error_code |= colvars->setup_input();
error_code |= colvars->setup_output();
return error_code;
}
/* ----------------------------------------------------------------------
trigger colvars computation
------------------------------------------------------------------------- */
double colvarproxy_lammps::compute()
{
if (cvm::debug()) {
cvm::log(std::string(cvm::line_marker) +
"colvarproxy_lammps step no. " +
cvm::to_str(_lmp->update->ntimestep) + " [first - last = " +
cvm::to_str(_lmp->update->beginstep) + " - " +
cvm::to_str(_lmp->update->endstep) + "]\n");
}
if (first_timestep) {
first_timestep = false;
} else {
// Use the time step number from LAMMPS Update object
if (_lmp->update->ntimestep - previous_step == 1) {
colvarmodule::it++;
b_simulation_continuing = false;
} else {
// Cases covered by this condition:
// - run 0
// - beginning of a new run statement
// The internal counter is not incremented, and the objects are made
// aware of this via the following flag
b_simulation_continuing = true;
}
}
previous_step = _lmp->update->ntimestep;
unit_cell_x.set(_lmp->domain->xprd, 0.0, 0.0);
unit_cell_y.set(0.0, _lmp->domain->yprd, 0.0);
unit_cell_z.set(0.0, 0.0, _lmp->domain->zprd);
if (_lmp->domain->xperiodic == 0 && _lmp->domain->yperiodic == 0 &&
_lmp->domain->zperiodic == 0) {
boundaries_type = boundaries_non_periodic;
reset_pbc_lattice();
} else if ((_lmp->domain->nonperiodic == 0) &&
(_lmp->domain->dimension == 3) &&
(_lmp->domain->triclinic == 0)) {
// Orthogonal unit cell
boundaries_type = boundaries_pbc_ortho;
colvarproxy_system::update_pbc_lattice();
// It is safer to let LAMMPS deal with high-tilt triclinic boxes
} else {
boundaries_type = boundaries_unsupported;
}
if (cvm::debug()) {
cvm::log(std::string(cvm::line_marker) +
"colvarproxy_lammps, step no. " + cvm::to_str(colvarmodule::it) + "\n" +
"Updating internal data.\n");
}
// zero the forces on the atoms, so that they can be accumulated by the colvars
for (size_t i = 0; i < atoms_new_colvar_forces.size(); i++)
atoms_new_colvar_forces[i].reset();
bias_energy = 0.0;
if (cvm::debug()) {
cvm::log("atoms_ids = " + cvm::to_str(atoms_ids) + "\n");
cvm::log("atoms_refcount = " + cvm::to_str(atoms_refcount) + "\n");
cvm::log("atoms_positions = " + cvm::to_str(atoms_positions) + "\n");
cvm::log("atoms_new_colvar_forces = " + cvm::to_str(atoms_new_colvar_forces) + "\n");
}
// Call the collective variable module
if (colvars->calc() != COLVARS_OK)
cvm::error("Error in the collective variables module.\n", COLVARS_ERROR);
if (cvm::debug()) {
cvm::log("atoms_ids = " + cvm::to_str(atoms_ids) + "\n");
cvm::log("atoms_refcount = " + cvm::to_str(atoms_refcount) + "\n");
cvm::log("atoms_positions = " + cvm::to_str(atoms_positions) + "\n");
cvm::log("atoms_new_colvar_forces = " + cvm::to_str(atoms_new_colvar_forces) + "\n");
}
return bias_energy;
}
/* ---------------------------------------------------------------------- */
cvm::rvector colvarproxy_lammps::position_distance(cvm::atom_pos const &pos1,
cvm::atom_pos const &pos2)
const
{
double xtmp = pos2.x - pos1.x;
double ytmp = pos2.y - pos1.y;
double ztmp = pos2.z - pos1.z;
_lmp->domain->minimum_image_big(FLERR, xtmp,ytmp,ztmp);
return {xtmp, ytmp, ztmp};
}
/* ---------------------------------------------------------------------- */
void colvarproxy_lammps::log(std::string const &message)
{
LAMMPS_NS::utils::logmesg(_lmp, message);
}
/* ---------------------------------------------------------------------- */
void colvarproxy_lammps::error(std::string const &message)
{
log(message);
_lmp->error->one(FLERR, "Fatal error in the collective variables module");
}
/* ---------------------------------------------------------------------- */
char const *colvarproxy_lammps::script_obj_to_str(unsigned char *obj)
{
// For now we assume that all objects passed by FixColvars are strings
return reinterpret_cast<char *>(obj);
}
/* ---------------------------------------------------------------------- */
std::vector<std::string> colvarproxy_lammps::script_obj_to_str_vector(unsigned char *obj)
{
if (cvm::debug()) {
cvm::log("Called colvarproxy_lammps::script_obj_to_str_vector().\n");
}
std::string const input(reinterpret_cast<char *>(obj));
return LAMMPS_NS::utils::split_words(input); // :-)))
}
/* ---------------------------------------------------------------------- */
int colvarproxy_lammps::set_unit_system(std::string const &units_in, bool /*check_only*/)
{
std::string lmp_units = _lmp->update->unit_style;
if (units_in != lmp_units) {
cvm::error("Error: Specified unit system for Colvars \"" + units_in +
"\" is incompatible with LAMMPS internal units (" + lmp_units + ").\n");
return COLVARS_ERROR;
}
return COLVARS_OK;
}
int colvarproxy_lammps::check_atom_id(int atom_number)
{
int const aid = atom_number;
if (cvm::debug())
log("Adding atom " + cvm::to_str(atom_number) + " for collective variables calculation.\n");
// TODO add upper boundary check?
if ((aid < 0)) {
cvm::error("Error: invalid atom number specified, " +
cvm::to_str(atom_number) + "\n", COLVARS_INPUT_ERROR);
return COLVARS_INPUT_ERROR;
}
return aid;
}
/* ---------------------------------------------------------------------- */
int colvarproxy_lammps::init_atom(int atom_number)
{
int aid = atom_number;
for (size_t i = 0; i < atoms_ids.size(); i++) {
if (atoms_ids[i] == aid) {
// this atom id was already recorded
atoms_refcount[i] += 1;
return i;
}
}
aid = check_atom_id(atom_number);
if (aid < 0) return aid;
int const index = colvarproxy::add_atom_slot(aid);
// add entries for the LAMMPS-specific fields
atoms_types.push_back(0);
return index;
}