-
Notifications
You must be signed in to change notification settings - Fork 8
/
openmmmolecule.cpp
1944 lines (1595 loc) · 62.1 KB
/
openmmmolecule.cpp
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "openmmmolecule.h"
#include "SireMol/core.h"
#include "SireMol/moleditor.h"
#include "SireMol/atomelements.h"
#include "SireMol/atomcharges.h"
#include "SireMol/atomcoords.h"
#include "SireMol/atommasses.h"
#include "SireMol/atomproperty.hpp"
#include "SireMol/connectivity.h"
#include "SireMol/bondid.h"
#include "SireMol/bondorder.h"
#include "SireMol/atomvelocities.h"
#include "SireMM/atomljs.h"
#include "SireMM/selectorbond.h"
#include "SireMM/amberparams.h"
#include "SireMM/twoatomfunctions.h"
#include "SireMaths/vector.h"
#include "SireBase/parallel.h"
#include "SireBase/propertylist.h"
#include "SireUnits/units.h"
#include "SireError/errors.h"
#include "tostring.h"
#include <QSet>
#include <QReadWriteLock>
#include <QDebug>
using namespace SireOpenMM;
using namespace SireMM;
using namespace SireMol;
using namespace SireBase;
////////
//////// Implementation of OpenMMMolecule
////////
OpenMMMolecule::OpenMMMolecule()
{
}
OpenMMMolecule::OpenMMMolecule(const Molecule &mol,
const PropertyMap &map)
{
molinfo = mol.info();
number = mol.number();
if (molinfo.nAtoms() == 0)
{
// nothing to extract
molinfo = MoleculeInfo();
return;
}
bool is_perturbable = false;
bool swap_end_states = false;
if (mol.hasProperty(map["is_perturbable"]))
{
is_perturbable = mol.property(map["is_perturbable"]).asABoolean();
if (map.specified("swap_end_states"))
{
swap_end_states = map["swap_end_states"].value().asABoolean();
}
}
if (is_perturbable)
{
ffinfo = mol.property(map["forcefield0"]).asA<MMDetail>();
}
else
{
ffinfo = mol.property(map["forcefield"]).asA<MMDetail>();
}
if (map.specified("constraint"))
{
const auto c = map["constraint"].source().toLower().simplified();
if (c == "none")
{
constraint_type = CONSTRAIN_NONE;
}
else if (c == "h-bonds" or c == "h_bonds")
{
constraint_type = CONSTRAIN_HBONDS;
}
else if (c == "bonds")
{
constraint_type = CONSTRAIN_BONDS;
}
else if (c == "h-bonds-h-angles" or c == "h_bonds_h_angles")
{
constraint_type = CONSTRAIN_HBONDS | CONSTRAIN_HANGLES;
}
else if (c == "bonds-h-angles" or c == "bonds_h_angles")
{
constraint_type = CONSTRAIN_BONDS | CONSTRAIN_HANGLES;
}
else
{
throw SireError::invalid_key(QObject::tr(
"Unrecognised constraint type '%1'. Valid values are "
"'none', 'h-bonds', 'bonds', 'h-bonds-h-angles' or "
"'bonds-h-angles',")
.arg(c),
CODELOC);
}
}
else
{
constraint_type = CONSTRAIN_NONE;
}
if (map.specified("perturbable_constraint"))
{
const auto c = map["perturbable_constraint"].source().toLower().simplified();
if (c == "none")
{
perturbable_constraint_type = CONSTRAIN_NONE;
}
else if (c == "h-bonds")
{
perturbable_constraint_type = CONSTRAIN_HBONDS;
}
else if (c == "bonds")
{
perturbable_constraint_type = CONSTRAIN_BONDS;
}
else if (c == "h-bonds-h-angles")
{
perturbable_constraint_type = CONSTRAIN_HBONDS | CONSTRAIN_HANGLES;
}
else if (c == "bonds-h-angles")
{
perturbable_constraint_type = CONSTRAIN_BONDS | CONSTRAIN_HANGLES;
}
else
{
throw SireError::invalid_key(QObject::tr(
"Unrecognised perturbable constraint type '%1'. Valid values are "
"'none', 'h-bonds', 'bonds', 'h-bonds-h-angles' or "
"'bonds-h-angles',")
.arg(c),
CODELOC);
}
}
else
{
perturbable_constraint_type = constraint_type;
}
if (ffinfo.isAmberStyle())
{
if (is_perturbable)
{
// update the map to find the lambda=0 properties
// Note that we don't use the coordinates0 or coordinates1
// properties, because we need to build the molecule from
// its current coordinates (which should represent the
// current lambda state)
QStringList props = {"LJ", "ambertype", "angle", "atomtype",
"bond", "charge",
"dihedral", "element", "forcefield",
"gb_radii", "gb_screening", "improper",
"intrascale", "mass", "name",
"parameters", "treechain"};
// we can't specialise these globally in case other molecules
// are not of amber type
auto map0 = map.addSuffix("0", props);
auto map1 = map.addSuffix("1", props);
if (swap_end_states)
{
std::swap(map0, map1);
}
// save this perturbable map - this will help us set
// new properties from the results of dynamics, e.g.
// updating coordinates after minimisation
perturtable_map = map0;
// extract the parameters in amber format - this should work,
// as the 'forcefield' property has promised that this is
// an amber-style molecule
const auto params = SireMM::AmberParams(mol, map0);
const auto params1 = SireMM::AmberParams(mol, map1);
perturbed.reset(new OpenMMMolecule(*this));
perturbed->constructFromAmber(mol, params1, params, map1, true);
this->constructFromAmber(mol, params, params1, map0, true);
this->alignInternals(map);
}
else
{
const auto params = SireMM::AmberParams(mol, map);
this->constructFromAmber(mol, params, params, map, false);
}
}
else
{
throw SireError::unsupported(QObject::tr(
"We currently only support creating OpenMM molecules from "
"molecules that have been parameterised using Amber-style "
"forcefields. The molecule %1 has been parameterised using "
"the forcefield %2.")
.arg(mol.toString())
.arg(ffinfo.toString()),
CODELOC);
}
}
OpenMMMolecule::~OpenMMMolecule()
{
}
bool OpenMMMolecule::operator==(const OpenMMMolecule &other) const
{
return this == &other;
}
bool OpenMMMolecule::operator!=(const OpenMMMolecule &other) const
{
return not this->operator==(other);
}
bool OpenMMMolecule::isPerturbable() const
{
return perturbed.get() != 0;
}
bool OpenMMMolecule::isGhostAtom(int atom) const
{
return from_ghost_idxs.contains(atom) or to_ghost_idxs.contains(atom);
}
OpenMM::Vec3 to_vec3(const SireMaths::Vector &coords)
{
const double internal_to_nm = (1 * SireUnits::angstrom).to(SireUnits::nanometer);
return OpenMM::Vec3(internal_to_nm * coords.x(),
internal_to_nm * coords.y(),
internal_to_nm * coords.z());
}
OpenMM::Vec3 to_vec3(const SireMol::Velocity3D &vel)
{
return OpenMM::Vec3(vel.x().to(SireUnits::nanometers_per_ps),
vel.y().to(SireUnits::nanometers_per_ps),
vel.z().to(SireUnits::nanometers_per_ps));
}
inline qint64 to_pair(qint64 x, qint64 y)
{
if (y < x)
return to_pair(y, x);
else
return x << 32 | y & 0x00000000FFFFFFFF;
}
std::tuple<int, int, double, double, double> OpenMMMolecule::getException(
int atom0, int atom1, int start_index, double coul_14_scl, double lj_14_scl) const
{
double charge = 0.0;
double sigma = 0.0;
double epsilon = 0.0;
if (coul_14_scl != 0 or lj_14_scl != 0)
{
if (atom0 < 0 or atom0 >= cljs.count() or atom1 < 0 or atom1 >= cljs.count())
throw SireError::invalid_index(QObject::tr(
"Cannot get CLJ parameters for atom %1 or atom %2.")
.arg(atom0)
.arg(atom1),
CODELOC);
const auto &clj0 = cljs.constData()[atom0];
const auto &clj1 = cljs.constData()[atom1];
charge = coul_14_scl * std::get<0>(clj0) * std::get<0>(clj1);
sigma = 0.5 * (std::get<1>(clj0) + std::get<1>(clj1));
epsilon = lj_14_scl * std::sqrt(std::get<2>(clj0) * std::get<2>(clj1));
}
if (this->isPerturbable() and charge == 0 and epsilon == 0)
{
// openmm tries to optimise away zero parameters - this is an issue
// as perturbation requires that we don't remove them!
// If we don't do this, then we get a
// "updateParametersInContext: The set of non-excluded exceptions has changed"
/// exception when we update parameters in context
sigma = 1e-9;
epsilon = 1e-9;
}
return std::make_tuple(atom0 + start_index,
atom1 + start_index,
charge, sigma, epsilon);
}
/** Return closest constraint length to 'length' based on what
* we have seen before and the constraint_length_tolerance
*/
double getSharedConstraintLength(double length)
{
// distance below which constraints are considered to be equal (e.g. to
// r0 or to another angle - units in nanometers)
const double constraint_length_tolerance = 0.005;
// is this close to a O-H bond length of water?
if (std::abs(length - 0.09572) < constraint_length_tolerance)
{
return 0.09572;
}
static QVector<double> angle_constraint_lengths;
static QReadWriteLock l;
// most of the time we expect a hit, so a read lock is ok
QReadLocker locker(&l);
// is this close to any of the existing angle constraints?
for (const auto &cl : angle_constraint_lengths)
{
if (std::abs(cl - length) < constraint_length_tolerance)
{
// this is close enough to an existing constraint
// so we will use that instead
return cl;
}
}
locker.unlock();
// ok, we didn't find it - we should append it to the list
// unless another thread has got here first
QWriteLocker locker2(&l);
for (const auto &cl : angle_constraint_lengths)
{
if (std::abs(cl - length) < constraint_length_tolerance)
{
// this is close enough to an existing constraint
// so we will use that instead
return cl;
}
}
// this is a new constraint, so add it to the list
angle_constraint_lengths.append(length);
return length;
}
void OpenMMMolecule::constructFromAmber(const Molecule &mol,
const AmberParams ¶ms,
const AmberParams ¶ms1,
const PropertyMap &map,
bool is_perturbable)
{
const auto &moldata = mol.data();
atoms = mol.atoms();
const int nats = atoms.count();
if (nats <= 0)
{
return;
}
// look up the CGAtomIdx of each atom - this is because we
// will use AtomIdx for the ordering and atom identifiers
auto idx_to_cgatomidx = QVector<SireMol::CGAtomIdx>(nats);
auto idx_to_cgatomidx_data = idx_to_cgatomidx.data();
for (int i = 0; i < nats; ++i)
{
idx_to_cgatomidx_data[i] = molinfo.cgAtomIdx(SireMol::AtomIdx(i));
}
// extract the coordinates and convert to OpenMM units
const auto &c = moldata.property(map["coordinates"]).asA<SireMol::AtomCoords>();
this->coords = QVector<OpenMM::Vec3>(nats, OpenMM::Vec3(0, 0, 0));
auto coords_data = coords.data();
for (int i = 0; i < nats; ++i)
{
coords_data[i] = to_vec3(c.at(idx_to_cgatomidx_data[i]));
}
// extract the velocities and convert to OpenMM units
const auto vels_prop = map["velocities"];
vels = QVector<OpenMM::Vec3>(nats, OpenMM::Vec3(0, 0, 0));
if (moldata.hasProperty(vels_prop))
{
const auto &v = moldata.property(vels_prop).asA<SireMol::AtomVelocities>();
auto vels_data = vels.data();
for (int i = 0; i < nats; ++i)
{
vels_data[i] = to_vec3(v.at(idx_to_cgatomidx_data[i]));
}
}
// extract the masses and convert to OpenMM units
const auto params_masses = params.masses();
this->masses = QVector<double>(nats, 0.0);
auto masses_data = masses.data();
if (is_perturbable)
{
const auto params1_masses = params1.masses();
for (int i = 0; i < nats; ++i)
{
const auto cgatomidx = idx_to_cgatomidx_data[i];
// use the largest mass of the perturbing atoms
double mass = std::max(params_masses.at(cgatomidx).to(SireUnits::g_per_mol),
params1_masses.at(cgatomidx).to(SireUnits::g_per_mol));
if (mass < 0.05)
{
mass = 0.0;
}
if (mass < 0.5)
{
virtual_sites.append(i);
}
else if (mass < 2.5)
{
light_atoms.append(i);
}
masses_data[i] = mass;
}
}
else
{
for (int i = 0; i < nats; ++i)
{
double mass = params_masses.at(idx_to_cgatomidx_data[i]).to(SireUnits::g_per_mol);
if (mass < 0.05)
{
mass = 0.0;
}
if (mass < 0.5)
{
virtual_sites.append(i);
}
else if (mass < 2.5)
{
light_atoms.append(i);
}
masses_data[i] = mass;
}
}
// extract the charges and LJ parameters and convert to OpenMM units
const auto params_charges = params.charges();
const auto params_ljs = params.ljs();
this->cljs = QVector<std::tuple<double, double, double>>(nats, std::make_tuple(0.0, 0.0, 0.0));
auto cljs_data = cljs.data();
for (int i = 0; i < nats; ++i)
{
const auto &cgatomidx = idx_to_cgatomidx_data[i];
const double chg = params_charges.at(idx_to_cgatomidx_data[i]).to(SireUnits::mod_electron);
const auto &lj = params_ljs.at(idx_to_cgatomidx_data[i]);
const double sig = lj.sigma().to(SireUnits::nanometer);
const double eps = lj.epsilon().to(SireUnits::kJ_per_mol);
cljs_data[i] = std::make_tuple(chg, sig, eps);
}
this->bond_params.clear();
this->constraints.clear();
// initialise all atoms as being unbonded
this->unbonded_atoms.reserve(nats);
for (int i = 0; i < nats; ++i)
{
this->unbonded_atoms.insert(i);
}
// now the bonds
const double bond_k_to_openmm = 2.0 * (SireUnits::kcal_per_mol / (SireUnits::angstrom * SireUnits::angstrom)).to(SireUnits::kJ_per_mol / (SireUnits::nanometer * SireUnits::nanometer));
const double bond_r0_to_openmm = SireUnits::angstrom.to(SireUnits::nanometer);
QSet<qint64> constrained_pairs;
bool include_constrained_energies = true;
if (map.specified("include_constrained_energies"))
{
include_constrained_energies = map["include_constrained_energies"].value().asABoolean();
}
for (auto it = params.bonds().constBegin();
it != params.bonds().constEnd();
++it)
{
const auto bondid = it.key().map(molinfo);
const auto &bondparam = it.value().first;
int atom0 = bondid.get<0>().value();
int atom1 = bondid.get<1>().value();
if (atom0 > atom1)
std::swap(atom0, atom1);
const double k = bondparam.k() * bond_k_to_openmm;
const double r0 = bondparam.r0() * bond_r0_to_openmm;
if (k != 0)
{
this->unbonded_atoms.remove(atom0);
this->unbonded_atoms.remove(atom1);
}
const bool has_light_atom = (masses_data[atom0] < 2.5 or masses_data[atom1] < 2.5);
const bool has_massless_atom = masses_data[atom0] < 0.5 or masses_data[atom1] < 0.5;
auto this_constraint_type = constraint_type;
if (is_perturbable)
{
this_constraint_type = perturbable_constraint_type;
}
bool bond_is_not_constrained = true;
if ((not has_massless_atom) and ((this_constraint_type & CONSTRAIN_BONDS) or (has_light_atom and (this_constraint_type & CONSTRAIN_HBONDS))))
{
// add the constraint - this constrains the bond to whatever length it has now
const auto delta = coords[atom1] - coords[atom0];
auto constraint_length = std::sqrt((delta[0] * delta[0]) +
(delta[1] * delta[1]) +
(delta[2] * delta[2]));
// use the r0 for the bond if this is close to the measured length and this
// is not a perturbable molecule
if (not is_perturbable and std::abs(constraint_length - r0) < 0.01)
{
constraint_length = r0;
}
this->constraints.append(std::make_tuple(atom0, atom1, constraint_length));
constrained_pairs.insert(to_pair(atom0, atom1));
bond_is_not_constrained = false;
}
if (include_constrained_energies or bond_is_not_constrained)
this->bond_params.append(std::make_tuple(atom0, atom1, r0, k));
}
// now the angles
const double angle_k_to_openmm = 2.0 * (SireUnits::kcal_per_mol).to(SireUnits::kJ_per_mol);
ang_params.clear();
for (auto it = params.angles().constBegin();
it != params.angles().constEnd();
++it)
{
const auto angid = it.key().map(molinfo);
const auto &angparam = it.value().first;
int atom0 = angid.get<0>().value();
int atom1 = angid.get<1>().value();
int atom2 = angid.get<2>().value();
if (atom0 > atom2)
std::swap(atom0, atom2);
const double k = angparam.k() * angle_k_to_openmm;
const double theta0 = angparam.theta0(); // already in radians
const bool is_h_x_h = masses_data[atom0] < 2.5 and masses_data[atom2] < 2.5;
const auto key = to_pair(atom0, atom2);
bool angle_is_not_constrained = true;
if (not constrained_pairs.contains(key))
{
auto this_constraint_type = constraint_type;
if (is_perturbable)
{
this_constraint_type = perturbable_constraint_type;
}
// only include the angle X-y-Z if X-Z are not already constrained
if ((this_constraint_type & CONSTRAIN_HANGLES) and is_h_x_h)
{
const auto delta = coords[atom2] - coords[atom0];
auto constraint_length = std::sqrt((delta[0] * delta[0]) +
(delta[1] * delta[1]) +
(delta[2] * delta[2]));
// we can speed up OpenMM by making sure that constraints are
// equal if they operate on similar molecules (e.g. all water
// constraints are the same. We will check for this if this is
// a non-perturbable small molecule
if (mol.nAtoms() < 10 and not is_perturbable)
{
constraint_length = getSharedConstraintLength(constraint_length);
}
constraints.append(std::make_tuple(atom0, atom2,
constraint_length));
constrained_pairs.insert(key);
angle_is_not_constrained = false;
}
}
else
angle_is_not_constrained = false;
if (include_constrained_energies or angle_is_not_constrained)
ang_params.append(std::make_tuple(atom0, atom1, atom2,
theta0, k));
}
// now the dihedrals
const double dihedral_k_to_openmm = (SireUnits::kcal_per_mol).to(SireUnits::kJ_per_mol);
dih_params.clear();
for (auto it = params.dihedrals().constBegin();
it != params.dihedrals().constEnd();
++it)
{
const auto dihid = it.key().map(molinfo);
const auto &dihparam = it.value().first;
int atom0 = dihid.get<0>().value();
int atom1 = dihid.get<1>().value();
int atom2 = dihid.get<2>().value();
int atom3 = dihid.get<3>().value();
if (atom0 > atom3)
{
std::swap(atom0, atom3);
std::swap(atom1, atom2);
}
for (const auto &term : dihparam.terms())
{
const double v = term.k() * dihedral_k_to_openmm;
const double phase = term.phase(); // already in radians
const int periodicity = std::round(term.periodicity()); // this is just an integer
if (periodicity > 0)
{
dih_params.append(std::make_tuple(atom0, atom1,
atom2, atom3,
periodicity, phase, v));
}
else if (periodicity == 0 and v == 0)
{
// this is a null dihedral, e.g. for perturbation. Make sure
// that the periodicity is 1, else otherwise openmm will complain
dih_params.append(std::make_tuple(atom0, atom1,
atom2, atom3,
1, phase, v));
}
else
{
qWarning() << "IGNORING DIHEDRAL WITH WEIRD PERIODICITY" << dihparam.toString();
}
}
}
// now the impropers
for (auto it = params.impropers().constBegin();
it != params.impropers().constEnd();
++it)
{
const auto impid = it.key().map(molinfo);
const auto &impparam = it.value().first;
const int atom0 = impid.get<0>().value();
const int atom1 = impid.get<1>().value();
const int atom2 = impid.get<2>().value();
const int atom3 = impid.get<3>().value();
for (const auto &term : impparam.terms())
{
const double v = term.k() * dihedral_k_to_openmm;
const double phase = term.phase(); // already in radians
const int periodicity = std::round(term.periodicity()); // this is just an integer
if (periodicity > 0)
{
dih_params.append(std::make_tuple(atom0, atom1,
atom2, atom3,
periodicity, phase, v));
}
else if (periodicity == 0 and v == 0)
{
// this is a null dihedral, e.g. for perturbation. Make sure
// that the periodicity is 1, else otherwise openmm will complain
dih_params.append(std::make_tuple(atom0, atom1,
atom2, atom3,
1, phase, v));
}
else
{
qWarning() << "IGNORING IMPROPER WITH WEIRD PERIODICITY" << impparam.toString();
}
}
}
this->buildExceptions(mol, constrained_pairs, map);
}
bool is_ghost(const std::tuple<double, double, double> &clj)
{
return std::get<0>(clj) == 0 and (std::get<1>(clj) == 0 or std::get<2>(clj) == 0);
}
bool is_ghost_charge(const std::tuple<double, double, double> &clj)
{
return std::get<0>(clj) == 0;
}
bool is_ghost_lj(const std::tuple<double, double, double> &clj)
{
return std::get<1>(clj) == 0 or std::get<2>(clj) == 0;
}
/** Go through all of the internals and compare them to the perturbed
* state. Ensure that there is a one-to-one mapping, with them all
* in the same order. Any that are missing are added as nulls in
* the correct end state.
*/
void OpenMMMolecule::alignInternals(const PropertyMap &map)
{
// first go through an see which atoms are ghosts
// While we do this, set the alpha values for the
// reference and perturbed molecules
if (cljs.count() != perturbed->cljs.count())
throw SireError::incompatible_error(QObject::tr(
"Different number of CLJ parameters between the reference "
"(%1) and perturbed (%2) states.")
.arg(cljs.count())
.arg(perturbed->cljs.count()),
CODELOC);
this->alphas = QVector<double>(cljs.count(), 0.0);
this->perturbed->alphas = this->alphas;
for (int i = 0; i < cljs.count(); ++i)
{
const auto &clj0 = cljs.at(i);
const auto &clj1 = perturbed->cljs.at(i);
if (clj0 != clj1)
{
if (is_ghost(clj0))
{
from_ghost_idxs.insert(i);
this->alphas[i] = 1.0;
}
else if (is_ghost(clj1))
{
to_ghost_idxs.insert(i);
this->perturbed->alphas[i] = 1.0;
}
}
}
QVector<std::tuple<int, int, double, double>> bond_params_1;
bond_params_1.reserve(bond_params.count());
QVector<bool> found_index_0(bond_params.count(), false);
QVector<bool> found_index_1(perturbed->bond_params.count(), false);
for (int i = 0; i < bond_params.count(); ++i)
{
const auto &bond0 = bond_params.at(i);
int atom0 = std::get<0>(bond0);
int atom1 = std::get<1>(bond0);
bool found = false;
for (int j = 0; j < perturbed->bond_params.count(); ++j)
{
if (not found_index_1[j])
{
const auto &bond1 = perturbed->bond_params.at(j);
if (std::get<0>(bond1) == atom0 and std::get<1>(bond1) == atom1)
{
// we have found the matching bond!
bond_params_1.append(bond1);
found_index_0[i] = true;
found_index_1[j] = true;
found = true;
break;
}
}
}
if (not found)
{
// add a null bond with the same r0, but null k
found_index_0[i] = true;
bond_params_1.append(std::tuple<int, int, double, double>(atom0, atom1, std::get<2>(bond0), 0.0));
}
}
for (int j = 0; j < perturbed->bond_params.count(); ++j)
{
if (not found_index_1[j])
{
// need to add a bond missing in the reference state
const auto &bond1 = perturbed->bond_params.at(j);
int atom0 = std::get<0>(bond1);
int atom1 = std::get<1>(bond1);
// add a null bond with the same r0, but null k
bond_params.append(std::tuple<int, int, double, double>(atom0, atom1, std::get<2>(bond1), 0.0));
bond_params_1.append(bond1);
found_index_1[j] = true;
unbonded_atoms.remove(atom0);
unbonded_atoms.remove(atom1);
}
}
// all of found_index_0 and found_index_1 should be true...
if (found_index_0.indexOf(false) != -1 or found_index_1.indexOf(false) != -1)
{
throw SireError::program_bug(QObject::tr(
"Failed to align the bonds!"),
CODELOC);
}
perturbed->bond_params = bond_params_1;
QVector<std::tuple<int, int, int, double, double>> ang_params_1;
ang_params_1.reserve(ang_params.count());
found_index_0 = QVector<bool>(ang_params.count(), false);
found_index_1 = QVector<bool>(perturbed->ang_params.count(), false);
for (int i = 0; i < ang_params.count(); ++i)
{
const auto &ang0 = ang_params.at(i);
int atom0 = std::get<0>(ang0);
int atom1 = std::get<1>(ang0);
int atom2 = std::get<2>(ang0);
bool found = false;
for (int j = 0; j < perturbed->ang_params.count(); ++j)
{
if (not found_index_1[j])
{
const auto &ang1 = perturbed->ang_params.at(j);
if (std::get<0>(ang1) == atom0 and std::get<1>(ang1) == atom1 and std::get<2>(ang1) == atom2)
{
// we have found the matching angle!
ang_params_1.append(ang1);
found_index_0[i] = true;
found_index_1[j] = true;
found = true;
break;
}
}
}
if (not found)
{
// add a null angle with the same theta0, but null k
found_index_0[i] = true;
ang_params_1.append(std::tuple<int, int, int, double, double>(atom0, atom1, atom2, std::get<3>(ang0), 0.0));
}
}
for (int j = 0; j < perturbed->ang_params.count(); ++j)
{
if (not found_index_1[j])
{
// need to add a bond missing in the reference state
const auto &ang1 = perturbed->ang_params.at(j);
int atom0 = std::get<0>(ang1);
int atom1 = std::get<1>(ang1);
int atom2 = std::get<2>(ang1);
// add a null angle with the same theta0, but null k
ang_params.append(std::tuple<int, int, int, double, double>(atom0, atom1, atom2, std::get<3>(ang1), 0.0));
ang_params_1.append(ang1);
found_index_1[j] = true;
}
}
// all of found_index_0 and found_index_1 should be true...
if (found_index_0.indexOf(false) != -1 or found_index_1.indexOf(false) != -1)
{
throw SireError::program_bug(QObject::tr(
"Failed to align the angles!"),
CODELOC);
}
perturbed->ang_params = ang_params_1;
QVector<std::tuple<int, int, int, int, int, double, double>> dih_params_1;
dih_params_1.reserve(dih_params.count());
found_index_0 = QVector<bool>(dih_params.count(), false);
found_index_1 = QVector<bool>(perturbed->dih_params.count(), false);
for (int i = 0; i < dih_params.count(); ++i)
{
const auto &dih0 = dih_params.at(i);
int atom0 = std::get<0>(dih0);
int atom1 = std::get<1>(dih0);
int atom2 = std::get<2>(dih0);
int atom3 = std::get<3>(dih0);
bool found = false;
for (int j = 0; j < perturbed->dih_params.count(); ++j)
{
if (not found_index_1[j])
{
const auto &dih1 = perturbed->dih_params.at(j);
// we need to match all of the atoms AND the periodicity
if (std::get<0>(dih1) == atom0 and std::get<1>(dih1) == atom1 and
std::get<2>(dih1) == atom2 and std::get<3>(dih1) == atom3 and
std::get<4>(dih0) == std::get<4>(dih1))
{
// we have found the matching torsion!
dih_params_1.append(dih1);
found_index_0[i] = true;
found_index_1[j] = true;
found = true;
break;
}
}
}
if (not found)
{
// add a null dihedral with the same periodicity and phase, but null k
dih_params_1.append(std::tuple<int, int, int, int, int, double, double>(atom0, atom1, atom2, atom3, std::get<4>(dih0), std::get<5>(dih0), 0.0));
found_index_0[i] = true;
}
}
for (int j = 0; j < perturbed->dih_params.count(); ++j)
{
if (not found_index_1[j])
{
// need to add a dihedral missing in the reference state
const auto &dih1 = perturbed->dih_params.at(j);
int atom0 = std::get<0>(dih1);
int atom1 = std::get<1>(dih1);
int atom2 = std::get<2>(dih1);
int atom3 = std::get<3>(dih1);
// add a null dihedral with the same periodicity and phase, but null k