-
Notifications
You must be signed in to change notification settings - Fork 11
/
grotop.cpp
7927 lines (6634 loc) · 262 KB
/
grotop.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
/********************************************\
*
* Sire - Molecular Simulation Framework
*
* Copyright (C) 2017 Christopher Woods
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For full details of the license please see the COPYING file
* that should have come with this distribution.
*
* You can contact the authors at https://sire.openbiosim.org
*
\*********************************************/
#include "SireIO/grotop.h"
#include "SireSystem/system.h"
#include "SireError/errors.h"
#include "SireIO/errors.h"
#include "SireMol/atomcharges.h"
#include "SireMol/atomelements.h"
#include "SireMol/atommasses.h"
#include "SireMol/connectivity.h"
#include "SireMol/core.h"
#include "SireMol/errors.h"
#include "SireMol/molecule.h"
#include "SireMol/moleculegroup.h"
#include "SireMol/moleditor.h"
#include "SireMol/select.h"
#include "SireMol/trajectory.h"
#include "SireMM/atomljs.h"
#include "SireMM/cljnbpairs.h"
#include "SireMM/fouratomfunctions.h"
#include "SireMM/internalff.h"
#include "SireMM/threeatomfunctions.h"
#include "SireMM/twoatomfunctions.h"
#include "SireBase/booleanproperty.h"
#include "SireBase/parallel.h"
#include "SireBase/stringproperty.h"
#include "SireUnits/units.h"
#include "SireStream/datastream.h"
#include "SireStream/shareddatastream.h"
#include <QDateTime>
#include <QDir>
#include <QElapsedTimer>
#include <QFileInfo>
#include <QRegularExpression>
using namespace SireIO;
using namespace SireUnits;
using namespace SireMol;
using namespace SireMM;
using namespace SireFF;
using namespace SireBase;
using namespace SireSystem;
using namespace SireStream;
////////////////
//////////////// Implementation of GroAtom
////////////////
static const RegisterMetaType<GroAtom> r_groatom(NO_ROOT);
QDataStream &operator<<(QDataStream &ds, const GroAtom &atom)
{
writeHeader(ds, r_groatom, 3);
SharedDataStream sds(ds);
sds << atom.atmname << atom.resname << atom.chainname << atom.atmtyp << atom.bndtyp << atom.atmnum << atom.resnum
<< atom.chggrp << atom.chg.to(mod_electron) << atom.mss.to(g_per_mol);
return ds;
}
QDataStream &operator>>(QDataStream &ds, GroAtom &atom)
{
VersionID v = readHeader(ds, r_groatom);
if (v == 3)
{
SharedDataStream sds(ds);
double chg, mass;
sds >> atom.atmname >> atom.resname >> atom.chainname >> atom.atmtyp >> atom.bndtyp >> atom.atmnum >>
atom.resnum >> atom.chggrp >> chg >> mass;
atom.chg = chg * mod_electron;
atom.mss = mass * g_per_mol;
}
else if (v == 2)
{
SharedDataStream sds(ds);
double chg, mass;
sds >> atom.atmname >> atom.resname >> atom.atmtyp >> atom.bndtyp >> atom.atmnum >> atom.resnum >>
atom.chggrp >> chg >> mass;
atom.chg = chg * mod_electron;
atom.mss = mass * g_per_mol;
}
else if (v == 1)
{
SharedDataStream sds(ds);
double chg, mass;
sds >> atom.atmname >> atom.resname >> atom.atmtyp >> atom.atmnum >> atom.resnum >> atom.chggrp >> chg >> mass;
atom.bndtyp = atom.atmtyp;
atom.chg = chg * mod_electron;
atom.mss = mass * g_per_mol;
}
else
throw version_error(v, "1,2", r_groatom, CODELOC);
return ds;
}
/** Constructor */
GroAtom::GroAtom() : atmnum(-1), resnum(-1), chggrp(-1), chg(0), mss(0)
{
}
/** Copy constructor */
GroAtom::GroAtom(const GroAtom &other)
: atmname(other.atmname), resname(other.resname), chainname(other.chainname), atmtyp(other.atmtyp),
bndtyp(other.bndtyp), atmnum(other.atmnum), resnum(other.resnum), chggrp(other.chggrp), chg(other.chg),
mss(other.mss)
{
}
/** Destructor */
GroAtom::~GroAtom()
{
}
/** Copy assignment operator */
GroAtom &GroAtom::operator=(const GroAtom &other)
{
if (this != &other)
{
atmname = other.atmname;
resname = other.resname;
chainname = other.chainname;
atmtyp = other.atmtyp;
bndtyp = other.bndtyp;
atmnum = other.atmnum;
resnum = other.resnum;
chggrp = other.chggrp;
chg = other.chg;
mss = other.mss;
}
return *this;
}
/** Comparison operator */
bool GroAtom::operator==(const GroAtom &other) const
{
return atmname == other.atmname and resname == other.resname and chainname == other.chainname and
atmtyp == other.atmtyp and bndtyp == other.bndtyp and atmnum == other.atmnum and resnum == other.resnum and
chggrp == other.chggrp and chg == other.chg and mss == other.mss;
}
/** Comparison operator */
bool GroAtom::operator!=(const GroAtom &other) const
{
return not operator==(other);
}
const char *GroAtom::typeName()
{
return QMetaType::typeName(qMetaTypeId<GroAtom>());
}
const char *GroAtom::what() const
{
return GroAtom::typeName();
}
QString GroAtom::toString() const
{
if (isNull())
return QObject::tr("GroAtom::null");
else
return QObject::tr("GroAtom( name() = %1, number() = %2 )").arg(atmname).arg(atmnum);
}
/** Return whether or not this atom is null */
bool GroAtom::isNull() const
{
return operator==(GroAtom());
}
/** Return the name of the atom */
AtomName GroAtom::name() const
{
return AtomName(atmname);
}
/** Return the number of the atom */
AtomNum GroAtom::number() const
{
return AtomNum(atmnum);
}
/** Return the name of the residue that contains this atom */
ResName GroAtom::residueName() const
{
return ResName(resname);
}
/** Return the number of the residue that contains this atom */
ResNum GroAtom::residueNumber() const
{
return ResNum(resnum);
}
/** Return the name of the chain that contains this atom. This will
be an empty name if a chain isn't specified */
ChainName GroAtom::chainName() const
{
return ChainName(chainname);
}
/** Return the charge group of this atom */
qint64 GroAtom::chargeGroup() const
{
return chggrp;
}
/** Return the atom type of this atom */
QString GroAtom::atomType() const
{
return atmtyp;
}
/** Return the bond type of this atom. This is normally the same as the atom type */
QString GroAtom::bondType() const
{
return bndtyp;
}
/** Return the charge on this atom */
SireUnits::Dimension::Charge GroAtom::charge() const
{
return chg;
}
/** Return the mass of this atom */
SireUnits::Dimension::MolarMass GroAtom::mass() const
{
return mss;
}
/** Set the name of this atom */
void GroAtom::setName(const QString &name)
{
atmname = name;
}
/** Set the number of this atom */
void GroAtom::setNumber(qint64 number)
{
if (number >= 0)
atmnum = number;
}
/** Set the name of the residue containing this atom */
void GroAtom::setResidueName(const QString &name)
{
resname = name;
}
/** Set the number of the residue containing this atom */
void GroAtom::setResidueNumber(qint64 number)
{
resnum = number;
}
/** Set the name of the chain containing this atom */
void GroAtom::setChainName(const QString &name)
{
chainname = name;
}
/** Set the charge group of this atom */
void GroAtom::setChargeGroup(qint64 grp)
{
if (grp >= 0)
chggrp = grp;
}
/** Set the atom type and bond type of this atom. To set
the bond type separately, you need to set it after calling
this function */
void GroAtom::setAtomType(const QString &atomtype)
{
atmtyp = atomtype;
bndtyp = atomtype;
}
/** Set the bond type of this atom */
void GroAtom::setBondType(const QString &bondtype)
{
bndtyp = bondtype;
}
/** Set the charge on this atom */
void GroAtom::setCharge(SireUnits::Dimension::Charge charge)
{
chg = charge;
}
/** Set the mass of this atom */
void GroAtom::setMass(SireUnits::Dimension::MolarMass mass)
{
if (mass.value() >= 0)
mss = mass;
}
////////////////
//////////////// Implementation of GroMolType
////////////////
static const RegisterMetaType<GroMolType> r_gromoltyp(NO_ROOT);
QDataStream &operator<<(QDataStream &ds, const GroMolType &moltyp)
{
writeHeader(ds, r_gromoltyp, 2);
SharedDataStream sds(ds);
sds << moltyp.nme << moltyp.warns << moltyp.atms0 << moltyp.atms1 << moltyp.bnds0 << moltyp.bnds1 << moltyp.angs0
<< moltyp.angs1 << moltyp.dihs0 << moltyp.dihs1 << moltyp.first_atoms0 << moltyp.first_atoms1 << moltyp.ffield0
<< moltyp.ffield1 << moltyp.nexcl0 << moltyp.nexcl1 << moltyp.is_perturbable;
return ds;
}
QDataStream &operator>>(QDataStream &ds, GroMolType &moltyp)
{
VersionID v = readHeader(ds, r_gromoltyp);
if (v == 1 or v == 2)
{
SharedDataStream sds(ds);
sds >> moltyp.nme >> moltyp.warns >> moltyp.atms0 >> moltyp.atms1 >> moltyp.bnds0 >> moltyp.bnds1 >>
moltyp.angs0 >> moltyp.angs1 >> moltyp.dihs0 >> moltyp.dihs1 >> moltyp.first_atoms0 >> moltyp.first_atoms1;
if (v == 2)
sds >> moltyp.ffield0 >> moltyp.ffield1;
else
{
moltyp.ffield0 = MMDetail();
moltyp.ffield1 = MMDetail();
}
sds >> moltyp.nexcl0 >> moltyp.nexcl1 >> moltyp.is_perturbable;
}
else
throw version_error(v, "1,2", r_gromoltyp, CODELOC);
return ds;
}
/** Constructor */
GroMolType::GroMolType()
: nexcl0(3), nexcl1(3), // default to 3 as this is normal for most molecules
is_perturbable(false) // default to a non-perturbable molecule
{
}
/** Construct from the passed molecule */
GroMolType::GroMolType(const SireMol::Molecule &mol, const PropertyMap &map)
: nexcl0(3), nexcl1(3), // default to '3' as this is normal for most molecules
is_perturbable(false) // default to a non-perturbable molecule
{
if (mol.nAtoms() == 0)
return;
// Try to see if this molecule is perturbable.
try
{
is_perturbable = mol.property("is_perturbable").asABoolean();
}
catch (...)
{
}
// Perturbable molecule.
if (is_perturbable)
{
// For perturbable molecules we don't user the user PropertyMap to extract
// properties since the naming must be consistent for the properties at
// lambda = 0 and lambda = 1, e.g. "charge0" and "charge1".
// get the name either from the molecule name or the name of the first
// residue
nme = mol.name();
if (nme.isEmpty())
{
nme = mol.residue(ResIdx(0)).name();
}
// replace any strings in the name with underscores
nme = nme.simplified().replace(" ", "_");
// get the forcefields for this molecule
try
{
ffield0 = mol.property("forcefield0").asA<MMDetail>();
}
catch (...)
{
warns.append(QObject::tr("Cannot find a valid MM forcefield for this molecule at lambda = 0!"));
}
try
{
ffield1 = mol.property("forcefield1").asA<MMDetail>();
}
catch (...)
{
warns.append(QObject::tr("Cannot find a valid MM forcefield for this molecule at lambda = 1!"));
}
const auto molinfo = mol.info();
bool uses_parallel = true;
if (map["parallel"].hasValue())
{
uses_parallel = map["parallel"].value().asA<BooleanProperty>().value();
}
// get information about all atoms in this molecule
auto extract_atoms = [&](bool is_lambda1)
{
if (is_lambda1)
atms1 = QVector<GroAtom>(molinfo.nAtoms());
else
atms0 = QVector<GroAtom>(molinfo.nAtoms());
AtomMasses masses;
AtomElements elements;
AtomCharges charges;
AtomIntProperty groups;
AtomStringProperty atomtypes;
AtomStringProperty bondtypes;
bool has_mass(false), has_elem(false), has_chg(false), has_type(false), has_bondtype(false);
try
{
if (is_lambda1)
masses = mol.property("mass1").asA<AtomMasses>();
else
masses = mol.property("mass0").asA<AtomMasses>();
has_mass = true;
}
catch (...)
{
}
if (not has_mass)
{
try
{
if (is_lambda1)
elements = mol.property("element1").asA<AtomElements>();
else
elements = mol.property("element0").asA<AtomElements>();
has_elem = true;
}
catch (...)
{
}
}
try
{
if (is_lambda1)
charges = mol.property("charge1").asA<AtomCharges>();
else
charges = mol.property("charge0").asA<AtomCharges>();
has_chg = true;
}
catch (...)
{
}
try
{
if (is_lambda1)
atomtypes = mol.property("atomtype1").asA<AtomStringProperty>();
else
atomtypes = mol.property("atomtype0").asA<AtomStringProperty>();
has_type = true;
}
catch (...)
{
}
try
{
if (is_lambda1)
bondtypes = mol.property("bondtype1").asA<AtomStringProperty>();
else
bondtypes = mol.property("bondtype0").asA<AtomStringProperty>();
has_bondtype = true;
}
catch (...)
{
}
if (not(has_chg and has_type and (has_elem or has_mass)))
{
warns.append(QObject::tr("Cannot find valid charge, atomtype and (element or mass) "
"properties for the molecule. These are needed! "
"has_charge=%1, has_atomtype=%2, has_mass=%3, has_element=%4")
.arg(has_chg)
.arg(has_type)
.arg(has_mass)
.arg(has_elem));
return;
}
// run through the atoms in AtomIdx order
auto extract_atom = [&](int iatm, bool is_lambda1)
{
AtomIdx i(iatm);
const auto cgatomidx = molinfo.cgAtomIdx(i);
const auto residx = molinfo.parentResidue(i);
QString chainname;
if (molinfo.isWithinChain(residx))
{
chainname = molinfo.name(molinfo.parentChain(residx)).value();
}
// atom numbers have to count up sequentially from 1
int atomnum = i + 1;
QString atomnam = molinfo.name(i);
// assuming that residues are in the same order as the atoms
int resnum = residx + 1;
QString resnam = molinfo.name(residx);
// people like to preserve the residue numbers of ligands and
// proteins. This is very challenging for the gromacs topology,
// as it would force a different topology for every solvent molecule,
// so deciding on the difference between protein/ligand and solvent
// is tough. Will preserve the residue number if the number of
// residues is greater than 1 and the number of atoms is greater
// than 32 (so octanol is a solvent)
if (molinfo.nResidues() > 1 or molinfo.nAtoms() > 32)
{
resnum = molinfo.number(residx).value();
}
// Just use the atom number as the charge group for perturbable molecules.
int group = atomnum;
auto charge = charges[cgatomidx];
SireUnits::Dimension::MolarMass mass;
if (has_mass)
{
mass = masses[cgatomidx];
}
else
{
mass = elements[cgatomidx].mass();
}
if (mass < 0)
{
// not allowed to have a negative mass
mass = 1.0 * g_per_mol;
}
QString atomtype = atomtypes[cgatomidx];
if (is_lambda1)
{
auto &atom = atms1[i];
atom.setName(atomnam);
atom.setNumber(atomnum);
atom.setResidueName(resnam);
atom.setResidueNumber(resnum);
atom.setChainName(chainname);
atom.setChargeGroup(group);
atom.setCharge(charge);
atom.setMass(mass);
atom.setAtomType(atomtype);
if (has_bondtype)
{
atom.setBondType(bondtypes[cgatomidx]);
}
else
{
atom.setBondType(atomtype);
}
}
else
{
auto &atom = atms0[i];
atom.setName(atomnam);
atom.setNumber(atomnum);
atom.setResidueName(resnam);
atom.setResidueNumber(resnum);
atom.setChainName(chainname);
atom.setChargeGroup(group);
atom.setCharge(charge);
atom.setMass(mass);
atom.setAtomType(atomtype);
if (has_bondtype)
{
atom.setBondType(bondtypes[cgatomidx]);
}
else
{
atom.setBondType(atomtype);
}
}
};
if (uses_parallel)
{
tbb::parallel_for(tbb::blocked_range<int>(0, molinfo.nAtoms()), [&](const tbb::blocked_range<int> &r)
{
for (int i = r.begin(); i < r.end(); ++i)
{
extract_atom(i, is_lambda1);
} });
}
else
{
for (int i = 0; i < molinfo.nAtoms(); ++i)
{
extract_atom(i, is_lambda1);
}
}
};
// get all of the bonds in this molecule
auto extract_bonds = [&](bool is_lambda1)
{
bool has_conn(false), has_funcs(false);
TwoAtomFunctions funcs;
Connectivity conn;
const auto R = InternalPotential::symbols().bond().r();
try
{
if (is_lambda1)
funcs = mol.property("bond1").asA<TwoAtomFunctions>();
else
funcs = mol.property("bond0").asA<TwoAtomFunctions>();
has_funcs = true;
}
catch (...)
{
}
try
{
conn = mol.property("connectivity").asA<Connectivity>();
has_conn = true;
}
catch (...)
{
}
// get the bond potentials first
if (has_funcs)
{
for (const auto &bond : funcs.potentials())
{
AtomIdx atom0 = molinfo.atomIdx(bond.atom0());
AtomIdx atom1 = molinfo.atomIdx(bond.atom1());
if (atom0 > atom1)
qSwap(atom0, atom1);
if (is_lambda1)
bnds1.insert(BondID(atom0, atom1), GromacsBond(bond.function(), R));
else
bnds0.insert(BondID(atom0, atom1), GromacsBond(bond.function(), R));
}
}
// now fill in any missing bonded atoms with null bonds
if (has_conn)
{
for (const auto &bond : conn.getBonds())
{
AtomIdx atom0 = molinfo.atomIdx(bond.atom0());
AtomIdx atom1 = molinfo.atomIdx(bond.atom1());
if (atom0 > atom1)
qSwap(atom0, atom1);
BondID b(atom0, atom1);
if (is_lambda1)
{
if (not bnds1.contains(b))
bnds1.insert(b, GromacsBond(5)); // function 5 is a simple connection
}
else
{
if (not bnds0.contains(b))
bnds0.insert(b, GromacsBond(5)); // function 5 is a simple connection
}
}
}
};
// get all of the angles in this molecule
auto extract_angles = [&](bool is_lambda1)
{
bool has_funcs(false);
ThreeAtomFunctions funcs;
const auto theta = InternalPotential::symbols().angle().theta();
try
{
if (is_lambda1)
funcs = mol.property("angle1").asA<ThreeAtomFunctions>();
else
funcs = mol.property("angle0").asA<ThreeAtomFunctions>();
has_funcs = true;
}
catch (...)
{
}
if (has_funcs)
{
for (const auto &angle : funcs.potentials())
{
AtomIdx atom0 = molinfo.atomIdx(angle.atom0());
AtomIdx atom1 = molinfo.atomIdx(angle.atom1());
AtomIdx atom2 = molinfo.atomIdx(angle.atom2());
if (atom0 > atom2)
qSwap(atom0, atom2);
if (is_lambda1)
{
angs1.insert(AngleID(atom0, atom1, atom2), GromacsAngle(angle.function(), theta));
}
else
{
angs0.insert(AngleID(atom0, atom1, atom2), GromacsAngle(angle.function(), theta));
}
}
}
};
// get all of the dihedrals in this molecule
auto extract_dihedrals = [&](bool is_lambda1)
{
bool has_funcs(false);
FourAtomFunctions funcs;
const auto phi = InternalPotential::symbols().dihedral().phi();
const auto theta = InternalPotential::symbols().improper().theta();
try
{
if (is_lambda1)
funcs = mol.property("dihedral1").asA<FourAtomFunctions>();
else
funcs = mol.property("dihedral0").asA<FourAtomFunctions>();
has_funcs = true;
}
catch (...)
{
}
if (has_funcs)
{
for (const auto &dihedral : funcs.potentials())
{
AtomIdx atom0 = molinfo.atomIdx(dihedral.atom0());
AtomIdx atom1 = molinfo.atomIdx(dihedral.atom1());
AtomIdx atom2 = molinfo.atomIdx(dihedral.atom2());
AtomIdx atom3 = molinfo.atomIdx(dihedral.atom3());
if (atom0 > atom3)
{
qSwap(atom0, atom3);
qSwap(atom1, atom2);
}
// get all of the dihedral terms (could be a lot)
auto parts = GromacsDihedral::construct(dihedral.function(), phi);
DihedralID dihid(atom0, atom1, atom2, atom3);
for (const auto &part : parts)
{
if (is_lambda1)
dihs1.insert(dihid, part);
else
dihs0.insert(dihid, part);
}
}
}
bool has_imps(false);
FourAtomFunctions imps;
try
{
if (is_lambda1)
imps = mol.property("improper1").asA<FourAtomFunctions>();
else
imps = mol.property("improper0").asA<FourAtomFunctions>();
has_imps = true;
}
catch (...)
{
}
if (has_imps)
{
for (const auto &improper : imps.potentials())
{
AtomIdx atom0 = molinfo.atomIdx(improper.atom0());
AtomIdx atom1 = molinfo.atomIdx(improper.atom1());
AtomIdx atom2 = molinfo.atomIdx(improper.atom2());
AtomIdx atom3 = molinfo.atomIdx(improper.atom3());
// get all of the dihedral terms (could be a lot)
auto parts = GromacsDihedral::constructImproper(improper.function(), phi, theta);
DihedralID impid(atom0, atom1, atom2, atom3);
for (const auto &part : parts)
{
if (is_lambda1)
dihs1.insert(impid, part);
else
dihs0.insert(impid, part);
}
}
}
};
const QVector<std::function<void(bool)>> functions = {extract_atoms, extract_bonds, extract_angles,
extract_dihedrals};
if (uses_parallel)
{
tbb::parallel_for(tbb::blocked_range<int>(0, functions.count(), 1), [&](const tbb::blocked_range<int> &r)
{
for (int i = r.begin(); i < r.end(); ++i)
{
functions[i](false);
functions[i](true);
} });
}
else
{
for (int i = 0; i < functions.count(); ++i)
{
functions[i](false);
functions[i](true);
}
}
// sanitise this object
this->_pvt_sanitise();
this->_pvt_sanitise(true);
}
// Regular molecule.
else
{
// get the name either from the molecule name or the name of the first
// residue
nme = mol.name();
if (nme.isEmpty())
{
nme = mol.residue(ResIdx(0)).name();
}
// replace any strings in the name with underscores
nme = nme.simplified().replace(" ", "_");
// get the forcefield for this molecule
try
{
ffield0 = mol.property(map["forcefield"]).asA<MMDetail>();
}
catch (...)
{
warns.append(QObject::tr("Cannot find a valid MM forcefield for this molecule!"));
}
const auto molinfo = mol.info();
bool uses_parallel = true;
if (map["parallel"].hasValue())
{
uses_parallel = map["parallel"].value().asA<BooleanProperty>().value();
}
// get information about all atoms in this molecule
auto extract_atoms = [&]()
{
atms0 = QVector<GroAtom>(molinfo.nAtoms());
AtomMasses masses;
AtomElements elements;
AtomCharges charges;
AtomIntProperty groups;
AtomStringProperty atomtypes;
AtomStringProperty bondtypes;
bool has_mass(false), has_elem(false), has_chg(false), has_group(false), has_type(false);
bool has_bondtype(false);
try
{
masses = mol.property(map["mass"]).asA<AtomMasses>();
has_mass = true;
}
catch (...)
{
}
if (not has_mass)
{
try
{
elements = mol.property(map["element"]).asA<AtomElements>();
has_elem = true;
}
catch (...)
{
}
}
try
{
charges = mol.property(map["charge"]).asA<AtomCharges>();
has_chg = true;
}
catch (...)
{
}
try
{
groups = mol.property(map["charge_group"]).asA<AtomIntProperty>();
has_group = true;
}
catch (...)