Skip to content

Commit 004ff93

Browse files
committed
bug fix: use correct eksum
1 parent 92d52d4 commit 004ff93

File tree

3 files changed

+74
-28
lines changed

3 files changed

+74
-28
lines changed

include/md/integrator.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class BasicThermostat : virtual public IntegratorStaticData
153153
virtual ~BasicThermostat();
154154
virtual void printDetail(FILE*);
155155
virtual void control1(time_prec timeStep);
156-
virtual void control2(time_prec timeStep, bool save);
156+
virtual void control2(time_prec timeStep, bool calcEkin);
157157

158158
static BasicThermostat* create(ThermostatEnum);
159159
};
@@ -187,15 +187,15 @@ class NhcDevice : public BasicThermostat
187187
int nnose, nhc_nc;
188188
double g0;
189189
double vnh[maxnose], qnh[maxnose];
190+
double* m_kin_ptr;
190191
double (*f_kin)();
191192
void (*scale_vel)(double);
192193
std::string name;
193194

194-
void controlImpl(double timeStep);
195+
void controlImpl(double timeStep, bool calcEkin);
195196

196197
public:
197-
NhcDevice(int nhclen, int nc, double dfree, //
198-
double (*kin)(), void (*scale)(double), std::string str);
198+
NhcDevice(int nhclen, int nc, double dfree, double* kin_ptr, double (*kin)(), void (*scale)(double), std::string str);
199199
void printDetail(FILE*) override;
200200
void control1(time_prec time_prec) override;
201201
void control2(time_prec time_prec, bool) override;
@@ -217,7 +217,7 @@ class Nhc06Thermostat : public BasicThermostat
217217

218218
void printDetail(FILE*) override;
219219
void control1(time_prec dt) override;
220-
void control2(time_prec dt, bool save) override;
220+
void control2(time_prec dt, bool) override;
221221

222222
static double kineticRattleGroup();
223223
static void scaleVelocityRattleGroup(double scale);
@@ -299,6 +299,7 @@ class IsoBaroDevice : public BasicBarostat
299299
protected:
300300
double* m_vir;
301301
double* m_eksum;
302+
double (*f_kin)();
302303

303304
double m_fric;
304305
double m_rnd;
@@ -322,6 +323,7 @@ class AnisoBaroDevice : public BasicBarostat
322323
double* m_vir;
323324
double* m_eksum;
324325
double (*m_ekin)[3];
326+
void (*f_kin)();
325327

326328
double m_fric;
327329
double m_rnd[3][3];
@@ -404,8 +406,7 @@ class BasicIntegrator : virtual public IntegratorStaticData
404406

405407
public:
406408
void printDetail(FILE*);
407-
BasicIntegrator(int nRespaLogV, PropagatorEnum pe, ThermostatEnum te,
408-
BarostatEnum be);
409+
BasicIntegrator(int nRespaLogV, PropagatorEnum pe, ThermostatEnum te, BarostatEnum be);
409410
BasicIntegrator();
410411
virtual ~BasicIntegrator();
411412
virtual void dynamic(int istep, time_prec dt);

src/md/barostat.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ void IsoBaroDevice::control_1_2(time_prec dt, int idx)
173173
double dim = 3.0;
174174
double al = 1.0 + dim / dofP;
175175
double tr_vir = m_vir[0] + m_vir[4] + m_vir[8];
176-
double eksu1 = *m_eksum;
176+
double eksu1;
177+
if (idx == 1)
178+
eksu1 = *m_eksum;
179+
else
180+
eksu1 = f_kin();
177181

178182
if (idx == 2 and printPressure) {
179183
auto o = stdout;
@@ -203,16 +207,19 @@ IsoBaroDevice::IsoBaroDevice(double fric)
203207
: BasicBarostat()
204208
, m_vir(nullptr)
205209
, m_eksum(nullptr)
210+
, f_kin(nullptr)
206211
, m_fric(fric)
207212
, m_rnd()
208213
, m_langevin(fric != 0.0)
209214
{
210215
if (atomic) {
211216
m_vir = vir;
212217
m_eksum = &eksum;
218+
f_kin = NhcDevice::kineticAtomic;
213219
} else {
214220
m_vir = hc_vir;
215221
m_eksum = &hc_eksum;
222+
f_kin = Nhc06Thermostat::kineticRattleGroup;
216223
}
217224

218225
if (m_langevin)
@@ -273,6 +280,12 @@ void IsoBaroDevice::control3(time_prec dt)
273280
}
274281

275282
namespace tinker {
283+
static void kineticForAnisoBaroDeviceAtomic()
284+
{
285+
double temp;
286+
kinetic(temp);
287+
}
288+
276289
void AnisoBaroDevice::control_1_2(time_prec dt, int idx)
277290
{
278291
time_prec dt2 = dt * 0.5;
@@ -281,6 +294,8 @@ void AnisoBaroDevice::control_1_2(time_prec dt, int idx)
281294
if (m_langevin)
282295
b = std::sqrt(2 * m_fric * units::gasconst * bath::kelvin / qbar);
283296

297+
if (idx == 2)
298+
f_kin();
284299
double eksu1 = *m_eksum;
285300
double gbar[3][3] = {0};
286301
for (int i = 0; i < 3; ++i)
@@ -354,6 +369,7 @@ AnisoBaroDevice::AnisoBaroDevice(double fric)
354369
, m_vir(nullptr)
355370
, m_eksum(nullptr)
356371
, m_ekin(nullptr)
372+
, f_kin(nullptr)
357373
, m_fric(fric)
358374
, m_rnd()
359375
, m_langevin(fric != 0.0)
@@ -362,10 +378,12 @@ AnisoBaroDevice::AnisoBaroDevice(double fric)
362378
m_vir = vir;
363379
m_eksum = &eksum;
364380
m_ekin = ekin;
381+
f_kin = kineticForAnisoBaroDeviceAtomic;
365382
} else {
366383
m_vir = hc_vir;
367384
m_eksum = &hc_eksum;
368385
m_ekin = hc_ekin;
386+
f_kin = hcKinetic;
369387
}
370388

371389
if (m_langevin) {
@@ -431,8 +449,7 @@ void AnisoBaroDevice::control3(time_prec dt)
431449

432450
double scal[3][3];
433451
trimatExp(scal, vbar_matrix, dt);
434-
double h0[3][3] = {
435-
{lvec1.x, lvec1.y, lvec1.z}, {lvec2.x, lvec2.y, lvec2.z}, {lvec3.x, lvec3.y, lvec3.z}};
452+
double h0[3][3] = {{lvec1.x, lvec1.y, lvec1.z}, {lvec2.x, lvec2.y, lvec2.z}, {lvec3.x, lvec3.y, lvec3.z}};
436453
matmul3(h0, scal);
437454
lvec1.x = h0[0][0], lvec1.y = h0[0][1], lvec1.z = h0[0][2];
438455
lvec2.x = h0[1][0], lvec2.y = h0[1][1], lvec2.z = h0[1][2];

src/md/thermostat.cpp

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
#include <cmath>
1010

11+
namespace tinker {
12+
namespace {
13+
class DummyThermostat : public BasicThermostat
14+
{
15+
public:
16+
void control2(time_prec, bool) override {}
17+
};
18+
}
19+
}
20+
1121
namespace tinker {
1222
void BasicThermostat::printBasic(FILE* o)
1323
{
@@ -23,9 +33,9 @@ void BasicThermostat::printDetail(FILE* o) {}
2333

2434
void BasicThermostat::control1(time_prec) {}
2535

26-
void BasicThermostat::control2(time_prec, bool save)
36+
void BasicThermostat::control2(time_prec, bool calcEkin)
2737
{
28-
if (save) {
38+
if (calcEkin) {
2939
T_prec temp;
3040
kinetic(temp);
3141
}
@@ -40,10 +50,14 @@ BasicThermostat* BasicThermostat::create(ThermostatEnum te)
4050
break;
4151
case ThermostatEnum::NHC:
4252
t = new NhcDevice(5, 5, static_cast<double>(mdstuf::nfree), //
43-
NhcDevice::kineticAtomic, //
53+
&eksum, NhcDevice::kineticAtomic, //
4454
NhcDevice::scaleVelocityAtomic, //
4555
std::string("NHC Atomic Temperature"));
4656
break;
57+
case ThermostatEnum::m_LP2022:
58+
case ThermostatEnum::m_NHC2006:
59+
t = new DummyThermostat;
60+
break;
4761
default:
4862
t = new BasicThermostat;
4963
break;
@@ -69,7 +83,7 @@ void BussiThermostat::control2(time_prec dt, bool)
6983
}
7084

7185
namespace tinker {
72-
void NhcDevice::controlImpl(time_prec dt)
86+
void NhcDevice::controlImpl(time_prec dt, bool calcEkin)
7387
{
7488
const int nc = nhc_nc;
7589
constexpr int ns = nhc_nsy;
@@ -82,7 +96,11 @@ void NhcDevice::controlImpl(time_prec dt)
8296
double kbt = units::gasconst * bath::kelvin;
8397

8498
double dtc = dt / nc;
85-
double eksum0 = f_kin();
99+
double eksum0;
100+
if (calcEkin or m_kin_ptr == nullptr)
101+
eksum0 = f_kin();
102+
else
103+
eksum0 = *m_kin_ptr;
86104
double velsc0 = 1.0;
87105
for (int k = 0; k < nc; ++k) {
88106
for (int j = 0; j < ns; ++j) {
@@ -129,12 +147,13 @@ void NhcDevice::controlImpl(time_prec dt)
129147
scale_vel(velsc0);
130148
}
131149

132-
NhcDevice::NhcDevice(
133-
int nhclen, int nc, double dfree, double (*kin)(), void (*scale)(double), std::string str)
150+
NhcDevice::NhcDevice(int nhclen, int nc, double dfree, double* kin_ptr, double (*kin)(), void (*scale)(double),
151+
std::string str)
134152
: BasicThermostat()
135153
, nnose(nhclen)
136154
, nhc_nc(nc)
137155
, g0(dfree)
156+
, m_kin_ptr(kin_ptr)
138157
, f_kin(kin)
139158
, scale_vel(scale)
140159
, name(str)
@@ -152,6 +171,8 @@ NhcDevice::NhcDevice(
152171
vnh[i] = 0;
153172
}
154173
qnh[0] *= g0;
174+
175+
f_kin();
155176
}
156177

157178
void NhcDevice::printDetail(FILE* o)
@@ -172,12 +193,13 @@ void NhcDevice::printDetail(FILE* o)
172193

173194
void NhcDevice::control1(time_prec dt)
174195
{
175-
this->controlImpl(dt);
196+
bool calcEkin = false;
197+
this->controlImpl(dt, calcEkin);
176198
}
177199

178-
void NhcDevice::control2(time_prec dt, bool)
200+
void NhcDevice::control2(time_prec dt, bool calcEkin)
179201
{
180-
this->controlImpl(dt);
202+
this->controlImpl(dt, calcEkin);
181203
}
182204

183205
double NhcDevice::kineticAtomic()
@@ -209,16 +231,16 @@ Nhc06Thermostat::Nhc06Thermostat()
209231
// tpart
210232
if (atomic) {
211233
dofT = mdstuf::nfree;
212-
m_tpart = new NhcDevice(nhclen, nc, dofT, NhcDevice::kineticAtomic,
213-
NhcDevice::scaleVelocityAtomic, "NHC Atomic Temperature");
234+
m_tpart = new NhcDevice(nhclen, nc, dofT, &eksum, NhcDevice::kineticAtomic, NhcDevice::scaleVelocityAtomic,
235+
"NHC Atomic Temperature");
214236
} else {
215237
dofT = 3.0 * (rattle_dmol.nmol - 1);
216-
m_tpart = new NhcDevice(nhclen, nc, dofT, Nhc06Thermostat::kineticRattleGroup,
238+
m_tpart = new NhcDevice(nhclen, nc, dofT, &hc_eksum, Nhc06Thermostat::kineticRattleGroup,
217239
Nhc06Thermostat::scaleVelocityRattleGroup, "NHC Group Temperature");
218240
}
219241

220242
// tbaro
221-
m_tbaro = new NhcDevice(nhclen, nc, dofVbar(), Nhc06Thermostat::kineticVbar,
243+
m_tbaro = new NhcDevice(nhclen, nc, dofVbar(), nullptr, Nhc06Thermostat::kineticVbar,
222244
Nhc06Thermostat::scaleVelocityVbar, "NHC Barostat Temperature");
223245
}
224246

@@ -241,11 +263,17 @@ void Nhc06Thermostat::control1(time_prec dt)
241263
m_tpart->control1(dt);
242264
}
243265

244-
void Nhc06Thermostat::control2(time_prec dt, bool save)
266+
void Nhc06Thermostat::control2(time_prec dt, bool)
245267
{
246-
m_tpart->control2(dt, save);
247-
if (applyBaro)
248-
m_tbaro->control2(dt, false);
268+
bool calcEkin;
269+
if (applyBaro) {
270+
calcEkin = false; // in nhc06 barostat, ek will be calculated in the barostat
271+
m_tpart->control2(dt, calcEkin);
272+
m_tbaro->control2(dt, calcEkin);
273+
} else {
274+
calcEkin = true;
275+
m_tpart->control2(dt, calcEkin);
276+
}
249277
}
250278

251279
double Nhc06Thermostat::kineticRattleGroup()

0 commit comments

Comments
 (0)