Skip to content

Commit 36c1abd

Browse files
committed
Improve pairlist code between matters
1 parent 6f3e5d4 commit 36c1abd

File tree

4 files changed

+121
-52
lines changed

4 files changed

+121
-52
lines changed

include/cando/chem/energyNonbond.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ class EnergyNonbond : public EnergyTerm {
9999
size_t a1CoordinateIndexTimes3,
100100
size_t a2CoordinateIndexTimes3,
101101
EnergyNonbond_sp energyNonbond,
102-
core::HashTable_sp atomTypes );
102+
core::HashTable_sp atomTypes,
103+
core::T_sp keepInteraction
104+
);
103105

104106
public:
105107
public:
@@ -282,6 +284,7 @@ class EnergyNonbond_O : public EnergyComponent_O
282284

283285
core::T_mv maybeRebuildPairList(core::T_sp tcoordinates);
284286
core::T_mv rebuildPairList(core::T_sp tcoordinates);
287+
core::T_mv rebuildPairListBetweenMatters(core::T_sp tcoordinates);
285288
void constructNonbondTermsFromAtomTable( AtomTable_sp atomTable, core::T_sp nbforceField, core::HashTable_sp atomTypes, core::T_sp keepInteractionFactory, core::T_sp coordinates);
286289
void constructNonbondTermsBetweenMatters( Matter_sp matter1, Matter_sp matter2, EnergyFunction_sp energyFunction, core::T_sp keepInteractionFactory );
287290
void construct14InteractionTerms(AtomTable_sp atomTable, Matter_sp matter, core::T_sp nbforceField, core::T_sp keepInteractionFactory, core::HashTable_sp atomTypes );

src/chem/energyNonbond.cc

Lines changed: 95 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ core::T_sp debug_nonbond(double Energy, double x1, double y1, double z1, double
250250

251251
SYMBOL_EXPORT_SC_(ChemPkg, EnergyNonbond);
252252
SYMBOL_EXPORT_SC_(ChemPkg, EnergyNonbond14);
253+
SYMBOL_EXPORT_SC_(ChemPkg, trapEnergyNonbondDefineForAtomPair );
254+
SYMBOL_EXPORT_SC_(ChemPkg, trapEnergyNonbondNotDefineForAtomPair );
253255

254256
core::T_sp nonbond_type(bool is14) {
255257
if (is14)
@@ -848,7 +850,16 @@ CL_DEFMETHOD void EnergyNonbond_O::compareAnalyticalAndNumericalForceAndHessianT
848850

849851
bool EnergyNonbond::defineForAtomPair(core::T_sp forceField, bool is14, Atom_sp a1, Atom_sp a2, size_t a1CoordinateIndexTimes3,
850852
size_t a2CoordinateIndexTimes3, EnergyNonbond_sp energyNonbond,
851-
core::HashTable_sp atomTypes) {
853+
core::HashTable_sp atomTypes, core::T_sp keepInteraction ) {
854+
if (_sym_trapEnergyNonbondDefineForAtomPair->fboundp()) {
855+
core::eval::funcall(_sym_trapEnergyNonbondDefineForAtomPair,
856+
a1, a2,
857+
core::make_fixnum(a1CoordinateIndexTimes3),
858+
core::make_fixnum(a2CoordinateIndexTimes3),
859+
energyNonbond,
860+
atomTypes,
861+
keepInteraction);
862+
}
852863
double amber_charge_conversion_18dot2223 =
853864
core::Number_O::as_double_float(gc::As<core::Number_sp>(_sym_STARamber_charge_conversion_18_DOT_2223STAR->symbolValue()));
854865
double dQ1Q2Scale = amber_charge_conversion_18dot2223 * amber_charge_conversion_18dot2223;
@@ -1148,7 +1159,9 @@ void EnergyNonbond_O::construct14InteractionTerms(AtomTable_sp atomTable, Matter
11481159
ea1->atom(), ea4->atom(),
11491160
ea1->coordinateIndexTimes3(),
11501161
ea4->coordinateIndexTimes3(),
1151-
this->asSmartPtr(), atomTypes );
1162+
this->asSmartPtr(),
1163+
atomTypes,
1164+
keepInteraction );
11521165
this->addTerm14(energyNonbond);
11531166
++terms;
11541167
}
@@ -1217,6 +1230,7 @@ core::T_mv EnergyNonbond_O::rebuildPairList(core::T_sp tcoordinates) {
12171230
// If the nonbond pairs are between two matters then don't rebuild the pair-list
12181231
if (this->_Matter1.notnilp()) {
12191232
ASSERT(this->_Matter2.notnilp());
1233+
this->rebuildPairListBetweenMatters(tcoordinates);
12201234
return Values0<core::T_O>();
12211235
}
12221236

@@ -1284,7 +1298,9 @@ core::T_mv EnergyNonbond_O::rebuildPairList(core::T_sp tcoordinates) {
12841298
iea1->atom(), iea2->atom(),
12851299
iea1->coordinateIndexTimes3(),
12861300
iea2->coordinateIndexTimes3(),
1287-
this->asSmartPtr(), this->_AtomTypes );
1301+
this->asSmartPtr(),
1302+
this->_AtomTypes,
1303+
keepInteraction);
12881304
this->addTerm(energyNonbond);
12891305
++interactionsKept;
12901306
} else {
@@ -1303,6 +1319,80 @@ core::T_mv EnergyNonbond_O::rebuildPairList(core::T_sp tcoordinates) {
13031319
core::clasp_make_fixnum(totalInteractions));
13041320
}
13051321

1322+
core::T_mv EnergyNonbond_O::rebuildPairListBetweenMatters(core::T_sp tcoordinates) {
1323+
core::print(fmt::format("DEBUG/in rebuildPairListBetweenMatters --------------\n"));
1324+
core::T_sp keepInteractionFactory = this->_KeepInteractionFactory;
1325+
if (keepInteractionFactory.nilp()) return Values0<core::T_O>();
1326+
NVector_sp coords = gc::As<NVector_sp>(tcoordinates);
1327+
core::T_sp keepInteraction = specializeKeepInteractionFactory( keepInteractionFactory, EnergyNonbond_O::staticClass() );
1328+
Matter_sp mat1 = this->_Matter1;
1329+
Matter_sp mat2 = this->_Matter2;
1330+
bool hasKeepInteractionFunction = gc::IsA<core::Function_sp>(keepInteraction);
1331+
double rpairlist2 = this->_Nonbond_r_pairlist*this->_Nonbond_r_pairlist;
1332+
auto atomTable = this->_AtomTable;
1333+
auto nbForceField = atomTable->nonbondForceFieldForAggregate();
1334+
auto atomTypes = this->_AtomTypes; // This was energyFunction->atomTypes() - I hope this->_AtomTypes works the same.
1335+
{
1336+
LOG("Defining NONBONDS");
1337+
Loop lMat1(mat1, ATOMS);
1338+
while (lMat1.advanceLoopAndProcess()) {
1339+
Atom_sp a1 = lMat1.getAtom();
1340+
size_t i3x1 = atomTable->getCoordinateIndexTimes3(a1);
1341+
Vector3 v1(coords,i3x1,Safe());
1342+
Loop lMat2(mat2, ATOMS);
1343+
while (lMat2.advanceLoopAndProcess()) {
1344+
Atom_sp a2 = lMat2.getAtom();
1345+
size_t i3x2 = atomTable->getCoordinateIndexTimes3(a2);
1346+
Vector3 v2(coords,i3x2,Safe());
1347+
Vector3 vdiff;
1348+
vdiff = v1-v2;
1349+
bool added = false;
1350+
double dist2 = vdiff.dotProduct(vdiff);
1351+
// if ((i3x1==609&&i3x1==480) || (i3x1==480&&i3x1==609)) {
1352+
core::print(fmt::format("ai{}/ai{} dist2 = {} rpairlist2 = {}\n", i3x1, i3x2, dist2, rpairlist2));
1353+
// }
1354+
if (dist2 < rpairlist2) {
1355+
EnergyNonbond energyNonbond;
1356+
bool in14 = false; // Between residues there are never 1-4
1357+
if (hasKeepInteractionFunction){
1358+
core::T_sp result = core::eval::funcall(keepInteraction,a1,a2,
1359+
core::make_fixnum(i3x1),
1360+
core::make_fixnum(i3x2));
1361+
if (result.notnilp()) {
1362+
energyNonbond.defineForAtomPair(nbForceField, in14, a1, a2, i3x1, i3x2, this->asSmartPtr(), atomTypes, keepInteraction);
1363+
added = true;
1364+
this->addTerm(energyNonbond);
1365+
}
1366+
} else {
1367+
energyNonbond.defineForAtomPair(nbForceField, in14, a1, a2,
1368+
i3x1, i3x2,
1369+
this->asSmartPtr(), atomTypes, keepInteraction );
1370+
added = true;
1371+
this->addTerm(energyNonbond);
1372+
}
1373+
}
1374+
if (!added) {
1375+
if (_sym_trapEnergyNonbondNotDefineForAtomPair->fboundp()) {
1376+
core::eval::funcall(_sym_trapEnergyNonbondDefineForAtomPair,
1377+
a1, a2,
1378+
core::make_fixnum(i3x1),
1379+
core::make_fixnum(i3x2),
1380+
this->asSmartPtr(),
1381+
atomTypes,
1382+
keepInteraction);
1383+
}
1384+
}
1385+
}
1386+
}
1387+
}
1388+
return Values0<core::T_O>();
1389+
}
1390+
1391+
1392+
1393+
1394+
1395+
13061396
void EnergyNonbond_O::constructExcludedAtomListFromAtomTable(AtomTable_sp atomTable, core::T_sp nbForceField, core::T_sp keepInteractionFactory ) {
13071397
// ------------------------------------------------------------
13081398
//
@@ -1325,48 +1415,15 @@ bonded to each other. Generate a pair-list in _Terms and that pair-list should
13251415
CL_DEFMETHOD void EnergyNonbond_O::constructNonbondTermsBetweenMatters(Matter_sp mat1, Matter_sp mat2,
13261416
EnergyFunction_sp energyFunction,
13271417
core::T_sp keepInteractionFactory) {
1418+
core::print(fmt::format("DEBUG/in constructNonbondTermsBetweenMatters -------------- {} {}\n", _rep_(mat1), _rep_(mat2)));
13281419
this->_UsesExcludedAtoms = false;
13291420
this->_Matter1 = mat1;
13301421
this->_Matter2 = mat2;
13311422
this->_KeepInteractionFactory = keepInteractionFactory;
13321423
this->_Terms.clear();
13331424
this->_Terms14.clear();
1334-
if (keepInteractionFactory.nilp()) return;
13351425

1336-
core::T_sp keepInteraction = specializeKeepInteractionFactory( keepInteractionFactory, EnergyNonbond_O::staticClass() );
1337-
bool hasKeepInteractionFunction = gc::IsA<core::Function_sp>(keepInteraction);
1338-
auto atomTable = energyFunction->atomTable();
1339-
auto nbForceField = atomTable->nonbondForceFieldForAggregate();
1340-
auto atomTypes = energyFunction->atomTypes();
1341-
{
1342-
LOG("Defining NONBONDS");
1343-
Loop lMat1(mat1, ATOMS);
1344-
while (lMat1.advanceLoopAndProcess()) {
1345-
Atom_sp a1 = lMat1.getAtom();
1346-
size_t a1CoordinateIndexTimes3 = atomTable->getCoordinateIndexTimes3(a1);
1347-
Loop lMat2(mat2, ATOMS);
1348-
while (lMat2.advanceLoopAndProcess()) {
1349-
Atom_sp a2 = lMat2.getAtom();
1350-
size_t a2CoordinateIndexTimes3 = atomTable->getCoordinateIndexTimes3(a2);
1351-
EnergyNonbond energyNonbond;
1352-
bool in14 = false; // Between residues there are never 1-4
1353-
if (hasKeepInteractionFunction){
1354-
core::T_sp result = core::eval::funcall(keepInteraction,a1,a2,
1355-
core::make_fixnum(a1CoordinateIndexTimes3),
1356-
core::make_fixnum(a2CoordinateIndexTimes3));
1357-
if (result.notnilp()) {
1358-
energyNonbond.defineForAtomPair(nbForceField, in14, a1, a2, a1CoordinateIndexTimes3, a2CoordinateIndexTimes3, this->asSmartPtr(), atomTypes);
1359-
this->addTerm(energyNonbond);
1360-
}
1361-
} else {
1362-
energyNonbond.defineForAtomPair(nbForceField, in14, a1, a2,
1363-
a1CoordinateIndexTimes3, a2CoordinateIndexTimes3,
1364-
this->asSmartPtr(), atomTypes);
1365-
this->addTerm(energyNonbond);
1366-
}
1367-
}
1368-
}
1369-
}
1426+
13701427
}
13711428

13721429
SYMBOL_EXPORT_SC_(KeywordPkg, da);

src/kinematics/xyzJoint.cc

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ string XyzJoint_O::asString() const
169169
Stub XyzJoint_O::getInputStub(chem::NVector_sp coords) const
170170
{
171171
Stub stub;
172-
stub._Transform.setToIdentity();
172+
//core::print(fmt::format("{} calling orientation-transform\n",__FUNCTION__));
173+
Matrix m = gc::As<geom::OMatrix_sp>(core::eval::funcall(_sym_orientation_transform, nil<core::T_O>()))->ref();
174+
stub._Transform = m;
173175
return stub;
174176
}
175177

@@ -212,11 +214,11 @@ void XyzJoint_O::_updateXyzCoord(chem::NVector_sp internals, chem::NVector_sp co
212214
ERROR(kinematics::_sym_undefined_internal_coordinates,core::lisp_createList(kw::_sym_joint,this->asSmartPtr()));
213215
}
214216
ASSERT(d2.isDefined());
215-
this->setPosition(coords,d2);
217+
Vector3 pos = stub._Transform.multiplyByVector3(d2);
218+
this->setPosition(coords,pos);
216219
}
217220

218-
Vector3 XyzJoint_O::transformedPos() const
219-
{
221+
Vector3 XyzJoint_O::transformedPos() const {
220222
// https://math.stackexchange.com/questions/133177/finding-a-unit-vector-perpendicular-to-another-vector
221223
ASSERT(this->_Atom.boundp());
222224
Vector3 d2 = this->_Atom->getPosition();
@@ -281,22 +283,28 @@ void StubJoint_O::_updateXyzCoord(chem::NVector_sp internals, chem::NVector_sp c
281283
CL_DEFMETHOD
282284
Vector3 StubJoint_O::transformedParentPos() const {
283285
chem::Atom_sp atm = this->_ParentAtom;
286+
Matrix m = gc::As<geom::OMatrix_sp>(core::eval::funcall(_sym_orientation_transform, nil<core::T_O>()))->ref();
284287
Vector3 pos = atm->getPosition();
285-
return pos;
288+
Vector3 tpos = m.multiplyByVector3(pos);
289+
return tpos;
286290
}
287291

288292
CL_DEFMETHOD
289293
Vector3 StubJoint_O::transformedGrandParentPos() const {
290294
chem::Atom_sp atm = this->_GrandParentAtom;
291295
Vector3 pos = atm->getPosition();
292-
return pos;
296+
Matrix m = gc::As<geom::OMatrix_sp>(core::eval::funcall(_sym_orientation_transform, nil<core::T_O>()))->ref();
297+
Vector3 tpos = m.multiplyByVector3(pos);
298+
return tpos;
293299
}
294300

295301
CL_DEFMETHOD
296302
Vector3 StubJoint_O::transformedGreatGrandParentPos() const {
297-
chem::Atom_sp atm = this->_GreatGrandParentAtom;
298-
Vector3 pos = atm->getPosition();
299-
return pos;
303+
chem::Atom_sp atm = this->_GreatGrandParentAtom;
304+
Vector3 pos = atm->getPosition();
305+
Matrix m = gc::As<geom::OMatrix_sp>(core::eval::funcall(_sym_orientation_transform, nil<core::T_O>()))->ref();
306+
Vector3 tpos = m.multiplyByVector3(pos);
307+
return tpos;
300308
}
301309

302310
};

src/lisp/topology/assembler.lisp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,10 +1417,11 @@ Return the COORDS and NIL or the ligand-transform if it was calculated."
14171417
((null oligomer-shape)
14181418
(unless ligand-orientation-p
14191419
(error "You must provide the ligand-orientation when building ligand and receptor"))
1420-
(update-externals-for-ligand-oligomer-shape assembler assembler-internals
1420+
(setf ligand-transform
1421+
(update-externals-for-ligand-oligomer-shape assembler assembler-internals
14211422
coords
14221423
(ligand-oligomer-shape assembler)
1423-
ligand-orientation)
1424+
ligand-orientation))
14241425
(update-externals-for-receptor-oligomer-shape assembler assembler-internals
14251426
coords
14261427
(receptor-oligomer-shape assembler)))
@@ -1452,7 +1453,6 @@ Return the COORDS and NIL or the ligand-transform if it was calculated."
14521453
(error "Could not find monomer-shape ~s in oligomer-shape ~s" monomer-shape oligomer-shape)))
14531454
(monomer-info (aref (monomer-shape-info-vector oligomer-shape) monomer-shape-pos))
14541455
(monomer (monomer monomer-info))
1455-
(monomer-context (gethash monomer (monomer-contexts assembler)))
14561456
(monomer-position (gethash monomer (monomer-positions assembler)))
14571457
(molecule-index (molecule-index monomer-position))
14581458
(residue-index (residue-index monomer-position))
@@ -1464,4 +1464,5 @@ Return the COORDS and NIL or the ligand-transform if it was calculated."
14641464
(with-orientation-transform ligand-transform
14651465
(update-xyz-coords assembler assembler-internals joint0 coords))
14661466
(update-xyz-coords assembler assembler-internals joint0 coords))
1467-
(adjust-atom-tree-external-coordinates assembler assembler-internals coords oligomer-shape)))
1467+
(adjust-atom-tree-external-coordinates assembler assembler-internals coords oligomer-shape)
1468+
joint0))

0 commit comments

Comments
 (0)