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
|
/* -------------------------------------------------------------------------- *
* SimTK Core: SimTK Molmodel *
* -------------------------------------------------------------------------- *
* This is part of the SimTK Core biosimulation toolkit originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2006-9 Stanford University and the Authors. *
* Authors: Christopher Bruns *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
#include "SimTKmolmodel.h"
#include "SimTKsimbody.h"
#include "SimTKcommon/Testing.h"
#include <iostream>
#include <vector>
using namespace SimTK;
using namespace std;
static const Real angstroms = 0.10;
static const Real kilocalories_per_mole = 4.184;
static const Real degrees = Pi / 180.0;
// Base class for molecule systems used for testing individual forces
class TestSystem {
public:
TestSystem() : system(), matter(system), dumm(system)
{
//dumm.setUseOpenMMAcceleration(false);
}
Real calcEnergy()
{
system.realize(system.updDefaultState(), Stage::Dynamics);
return system.calcPotentialEnergy(system.getDefaultState());
}
SpatialVec calcForce(MobilizedBodyIndex body)
{
system.realize(system.updDefaultState(), Stage::Dynamics);
return system.getRigidBodyForces(system.getDefaultState(), Stage::Dynamics)[body];
}
const CompoundSystem& getSystem() const {return system;}
CompoundSystem& updSystem() {return system;}
const DuMMForceFieldSubsystem& getDuMM() const {return dumm;}
DuMMForceFieldSubsystem& updDuMM() {return dumm;}
const SimbodyMatterSubsystem& getMatter() const {return matter;}
SimbodyMatterSubsystem& updMatter() {return matter;}
protected:
CompoundSystem system;
SimbodyMatterSubsystem matter;
DuMMForceFieldSubsystem dumm;
};
/////////////
// Coulomb //
/////////////
// Simple test system for Coulomb force
class SodiumChlorideSystem : public TestSystem {
public:
SodiumChlorideSystem(Real distance)
{
// Turn off all but Coulomb force
dumm.setAllGlobalScaleFactors(0.0);
dumm.setCoulombGlobalScaleFactor(1.0);
SodiumIon::setAmberLikeParameters(dumm);
ChlorideIon::setAmberLikeParameters(dumm);
SodiumIon na;
ChlorideIon cl;
system.adoptCompound(na, Vec3(distance, 0, 0));
system.adoptCompound(cl);
system.modelCompounds();
sodiumBody = na.getAtomMobilizedBodyIndex(Compound::AtomIndex(0));
system.updDefaultState() = system.realizeTopology();
}
Real calcForce() {
Vec3 forceOnSodium = TestSystem::calcForce(sodiumBody)[1];
return forceOnSodium[0]; // X-component is signed force
}
protected:
MobilizedBodyIndex sodiumBody;
};
void testCoulombEnergy(Real distance)
{
Real observedEnergy = SodiumChlorideSystem(distance).calcEnergy();
// Real expectedEnergy0 = -SimTK_COULOMB_CONSTANT_IN_MD / distance;
Real q1 = -1.0; // charge on chloride
Real q2 = 1.0; // charge on sodium
Real expectedEnergy = 138.935456 * q1 * q2 / distance;
SimTK_TEST_EQ(expectedEnergy, observedEnergy);
}
// Finite difference to prove Force = -dEnergy/dLength
void testCoulombEnergyVsForce(Real distance)
{
// Finite difference must be small relative to total distance
Real delta = 1e-4;
// this assert is really a debugging assert, not a unit test condition
assert( (delta/distance) < 1e-2 );
// Estimate force from -dEnergy/dDistance
Real energy1 = SodiumChlorideSystem(distance).calcEnergy();
Real energy2 = SodiumChlorideSystem(distance + delta).calcEnergy();
Real expectedForce = - (energy2 - energy1)/delta;
Real observedForce = SodiumChlorideSystem(distance + delta/2.0).calcForce();
SimTK_TEST(observedForce < 0.0);
SimTK_TEST_EQ_TOL(expectedForce, observedForce, 0.1);
}
void testCoulombForce()
{
testCoulombEnergy(1.0);
testCoulombEnergy(0.12345);
testCoulombEnergy(17.623);
testCoulombEnergyVsForce(1.0);
testCoulombEnergyVsForce(0.12345);
testCoulombEnergyVsForce(17.623);
}
//////////////////////////
// van der Waals forces //
//////////////////////////
class ArgonSystem : public TestSystem
{
public:
ArgonSystem(Real distance) {
// Turn off all but van der Waals force
dumm.setAllGlobalScaleFactors(0.0);
dumm.setVdwGlobalScaleFactor(1.0);
if (! dumm.hasAtomClass("Argon") ) {
dumm.defineAtomClass(
dumm.getNextUnusedAtomClassIndex(),
"Argon",
Element::getBySymbol("O")->getAtomicNumber(),
0, // no bonds
1.88 * angstroms, // radius
0.0003832 * kilocalories_per_mole // well depth
);
}
if (! dumm.hasChargedAtomType("Argon") ) {
dumm.defineChargedAtomType(
dumm.getNextUnusedChargedAtomTypeIndex(),
"Argon",
dumm.getAtomClassIndex("Argon"),
0.00 // no charge on symmetric molecule
);
}
if (! Biotype::exists("Argon", "Ar") )
Biotype::defineBiotype(Element::getBySymbol("Ar"), 0, "Argon", "Ar");
dumm.setBiotypeChargedAtomType( dumm.getChargedAtomTypeIndex("Argon"), Biotype::get("Argon", "Ar").getIndex() );
Argon argon1, argon2;
argon1.setAtomBiotype("Ar", "Argon", "Ar");
argon2.setAtomBiotype("Ar", "Argon", "Ar");
system.adoptCompound(argon1);
system.adoptCompound(argon2, Vec3(distance, 0, 0));
system.modelCompounds();
argon2Body = argon2.getAtomMobilizedBodyIndex(Compound::AtomIndex(0));
system.updDefaultState() = system.realizeTopology();
}
Real calcForce() {
Vec3 forceOnArgon2 = TestSystem::calcForce(argon2Body)[1];
return forceOnArgon2[0]; // X-component is signed force
}
protected:
MobilizedBodyIndex argon2Body;
};
void testVanDerWaalsEnergy(Real distance)
{
Real observedEnergy = ArgonSystem(distance).calcEnergy();
Real r = 0.5 * distance;
Real rMin = 1.88 * angstroms;
Real epsilon = 0.0003832 * kilocalories_per_mole;
Real expectedEnergy = epsilon * ( std::pow((rMin/r), 12.0) - 2.0 * std::pow((rMin/r), 6.0) );
SimTK_TEST_EQ(expectedEnergy, observedEnergy);
}
// Finite difference to prove Force = -dEnergy/dLength
void testVanDerWaalsEnergyVsForce(Real distance)
{
// Finite difference must be small relative to total distance
Real delta = 1e-4;
// this assert is really a debugging assert, not a unit test condition
assert( (delta/distance) < 1e-2 );
// Estimate force from -dEnergy/dDistance
Real energy1 = ArgonSystem(distance).calcEnergy();
Real energy2 = ArgonSystem(distance + delta).calcEnergy();
Real expectedForce = - (energy2 - energy1)/delta;
Real observedForce = ArgonSystem(distance + delta/2.0).calcForce();
SimTK_TEST_EQ_TOL(expectedForce, observedForce, 0.1);
}
void testVanDerWaalsForce() {
testVanDerWaalsEnergy(5.0 * angstroms);
testVanDerWaalsEnergy(3.7 * angstroms);
testVanDerWaalsEnergy(3.0 * angstroms);
testVanDerWaalsEnergyVsForce(5.0 * angstroms);
testVanDerWaalsEnergyVsForce(3.7 * angstroms);
testVanDerWaalsEnergyVsForce(3.0 * angstroms);
}
//////////////////
// Bond Stretch //
//////////////////
class OxygenSystem : public TestSystem {
public:
OxygenSystem(Real bondLength)
{
// Turn off all but bond stretch force
dumm.setAllGlobalScaleFactors(0.0);
dumm.setBondStretchGlobalScaleFactor(1.0);
if (! dumm.hasAtomClass("O2Mol") ) {
dumm.defineAtomClass(
dumm.getNextUnusedAtomClassIndex(),
"O2Mol",
Element::getBySymbol("O")->getAtomicNumber(),
1, // one bond
1.70 * angstroms, // radius
0.20 * kilocalories_per_mole // well depth
);
}
if (! dumm.hasChargedAtomType("O2Mol") ) {
dumm.defineChargedAtomType(
dumm.getNextUnusedChargedAtomTypeIndex(),
"O2Mol",
dumm.getAtomClassIndex("O2Mol"),
0.00 // no charge on symmetric molecule
);
}
dumm.defineBondStretch(
dumm.getAtomClassIndex("O2Mol"),
dumm.getAtomClassIndex("O2Mol"),
500.0 * kilocalories_per_mole / (angstroms * angstroms),
1.21 * angstroms
);
if (! Biotype::exists("Oxygen Molecule", "O") )
Biotype::defineBiotype(Element::getBySymbol("O"), 1, "Oxygen Molecule", "O");
dumm.setBiotypeChargedAtomType( dumm.getChargedAtomTypeIndex("O2Mol"), Biotype::get("Oxygen Molecule", "O").getIndex() );
Compound o2Molecule;
o2Molecule.setBaseAtom( UnivalentAtom("O1", Element::getBySymbol("O")) );
o2Molecule.bondAtom( UnivalentAtom("O2", Element::getBySymbol("O")), "O1/bond", bondLength );
o2Molecule.setBondMobility(BondMobility::Free, "O1", "O2");
o2Molecule.setAtomBiotype("O1", "Oxygen Molecule", "O");
o2Molecule.setAtomBiotype("O2", "Oxygen Molecule", "O");
system.adoptCompound(o2Molecule);
system.modelCompounds();
o2Body = o2Molecule.getAtomMobilizedBodyIndex(Compound::AtomIndex(1));
system.updDefaultState() = system.realizeTopology();
}
Real calcForce() {
Vec3 forceOnO2 = TestSystem::calcForce(o2Body)[1];
return forceOnO2[0]; // X-component is signed force
}
protected:
MobilizedBodyIndex o2Body;
};
void testBondStretchEnergy(Real bondLength)
{
Real observedEnergy = OxygenSystem(bondLength).calcEnergy();
Real r0 = 1.21 * angstroms;
Real kR = 500.0 * kilocalories_per_mole / (angstroms * angstroms);
// Stiffness constant kR in MD codes like molmodel and tinker is 2.0 times what engineers would expect
// Real expectedEnergy = 0.5 * kR * (bondLength - r0) * (bondLength - r0);
Real expectedEnergy = kR * (bondLength - r0) * (bondLength - r0);
SimTK_TEST_EQ(expectedEnergy, observedEnergy);
}
// Finite difference to prove Force = -dEnergy/dLength
void testBondStretchEnergyVsForce(Real bondLength)
{
// Finite difference must be small relative to total distance
Real delta = 1e-4;
// this assert is really a debugging assert, not a unit test condition
assert( (delta/bondLength) < 1e-2 );
// Estimate force from -dEnergy/dDistance
Real energy1 = OxygenSystem(bondLength).calcEnergy();
Real energy2 = OxygenSystem(bondLength + delta).calcEnergy();
Real expectedForce = - (energy2 - energy1)/delta;
Real observedForce = OxygenSystem(bondLength + delta/2.0).calcForce();
SimTK_TEST_EQ_TOL(expectedForce, observedForce, 0.01);
}
void testBondStretchForce()
{
testBondStretchEnergy(1.10*angstroms);
testBondStretchEnergy(1.35*angstroms);
testBondStretchEnergy(1.71*angstroms);
testBondStretchEnergyVsForce(1.10*angstroms);
testBondStretchEnergyVsForce(1.35*angstroms);
testBondStretchEnergyVsForce(1.71*angstroms);
}
/////////////////////////
// Custom Bond Stretch //
/////////////////////////
class HarmonicBondStretch
: public DuMM::CustomBondStretch
{
public:
HarmonicBondStretch(Real stiffness, Real idealLength)
: stiffness(stiffness), idealLength(idealLength)
{}
Real calcEnergy(Real length) const {
Real dR = length - idealLength;
return stiffness * dR * dR;
}
Real calcForce(Real length) const {
Real dR = length - idealLength;
return -2.0 * stiffness * dR;
}
private:
Real idealLength;
Real stiffness;
};
// Oxygen modified to use custom bond stretch force
class CustomOxygenSystem : public OxygenSystem {
public:
CustomOxygenSystem(Real bondLength) : OxygenSystem(bondLength)
{
dumm.setAllGlobalScaleFactors(0.0);
dumm.setCustomBondStretchGlobalScaleFactor(1.0);
dumm.defineCustomBondStretch(
dumm.getAtomClassIndex("O2Mol"),
dumm.getAtomClassIndex("O2Mol"),
new HarmonicBondStretch(
600.0 * kilocalories_per_mole / (angstroms * angstroms),
1.21 * angstroms
)
);
system.realizeTopology();
}
};
void testCustomBondStretchEnergy(Real bondLength)
{
Real observedEnergy = CustomOxygenSystem(bondLength).calcEnergy();
Real r0 = 1.21 * angstroms;
Real kR = 600.0 * kilocalories_per_mole / (angstroms * angstroms);
// Stiffness constant kR in MD codes like molmodel and tinker is 2.0 times what engineers would expect
// Real expectedEnergy = 0.5 * kR * (bondLength - r0) * (bondLength - r0);
Real expectedEnergy = kR * (bondLength - r0) * (bondLength - r0);
SimTK_TEST_EQ(expectedEnergy, observedEnergy);
}
// Finite difference to prove Force = -dEnergy/dLength
void testCustomBondStretchEnergyVsForce(Real bondLength)
{
// Finite difference must be small relative to total distance
Real delta = 1e-4;
// this assert is really a debugging assert, not a unit test condition
assert( (delta/bondLength) < 1e-2 );
// Estimate force from -dEnergy/dDistance
Real energy1 = CustomOxygenSystem(bondLength).calcEnergy();
Real energy2 = CustomOxygenSystem(bondLength + delta).calcEnergy();
Real expectedForce = - (energy2 - energy1)/delta;
Real observedForce = CustomOxygenSystem(bondLength + delta/2.0).calcForce();
SimTK_TEST_EQ_TOL(expectedForce, observedForce, 0.01);
}
void testCustomBondStretchForce()
{
testCustomBondStretchEnergy(1.10 * angstroms);
testCustomBondStretchEnergy(1.35 * angstroms);
testCustomBondStretchEnergy(1.70 * angstroms);
testCustomBondStretchEnergyVsForce(1.10*angstroms);
testCustomBondStretchEnergyVsForce(1.35*angstroms);
testCustomBondStretchEnergyVsForce(1.71*angstroms);
}
///////////////
// Bond Bend //
///////////////
class WaterSystem : public TestSystem {
public:
WaterSystem(Real bondAngle)
{
// Turn off all but bond bend force
dumm.setAllGlobalScaleFactors(0.0);
dumm.setBondBendGlobalScaleFactor(1.0);
if (! dumm.hasAtomClass("TIP3P Water O") ) {
dumm.defineAtomClass(
dumm.getNextUnusedAtomClassIndex(),
"TIP3P Water O",
Element::getBySymbol("O")->getAtomicNumber(),
2, // one bond
1.7683 * angstroms, // radius
0.1520 * kilocalories_per_mole // well depth
);
}
if (! dumm.hasAtomClass("TIP3P Water H") ) {
dumm.defineAtomClass(
dumm.getNextUnusedAtomClassIndex(),
"TIP3P Water H",
Element::getBySymbol("H")->getAtomicNumber(),
1, // one bond
0.0001 * angstroms, // radius
0.0000 * kilocalories_per_mole // well depth
);
}
if (! dumm.hasChargedAtomType("TIP3P Water O") ) {
dumm.defineChargedAtomType(
dumm.getNextUnusedChargedAtomTypeIndex(),
"TIP3P Water O",
dumm.getAtomClassIndex("TIP3P Water O"),
-0.834
);
}
if (! dumm.hasChargedAtomType("TIP3P Water H") ) {
dumm.defineChargedAtomType(
dumm.getNextUnusedChargedAtomTypeIndex(),
"TIP3P Water H",
dumm.getAtomClassIndex("TIP3P Water H"),
0.417
);
}
dumm.defineBondStretch(
dumm.getAtomClassIndex("TIP3P Water O"),
dumm.getAtomClassIndex("TIP3P Water H"),
553.0 * kilocalories_per_mole / (angstroms * angstroms),
0.9572 * angstroms
);
dumm.defineBondBend(
dumm.getAtomClassIndex("TIP3P Water H"),
dumm.getAtomClassIndex("TIP3P Water O"),
dumm.getAtomClassIndex("TIP3P Water H"),
100.00 * kilocalories_per_mole / (degrees * degrees),
104.52); // define bond bend takes angle in degrees!?!
if (! Biotype::exists("TIP3P Water", "O") )
Biotype::defineBiotype(Element::getBySymbol("O"), 2, "TIP3P Water", "O");
if (! Biotype::exists("TIP3P Water", "H") )
Biotype::defineBiotype(Element::getBySymbol("H"), 1, "TIP3P Water", "H");
dumm.setBiotypeChargedAtomType( dumm.getChargedAtomTypeIndex("TIP3P Water O"), Biotype::get("TIP3P Water", "O").getIndex() );
dumm.setBiotypeChargedAtomType( dumm.getChargedAtomTypeIndex("TIP3P Water H"), Biotype::get("TIP3P Water", "H").getIndex() );
Compound water;
water.setBaseAtom( BivalentAtom("O", Element::getBySymbol("O")) );
water.bondAtom( UnivalentAtom("H1", Element::getBySymbol("H")), "O/bond1", 0.9572 * angstroms );
water.bondAtom( UnivalentAtom("H2", Element::getBySymbol("H")), "O/bond2", 0.9572 * angstroms );
water.setDefaultBondAngle(bondAngle, "H1", "O", "H2");
// Unfortunately, there is not yet a BondMobility::Angle concept, so pure Pin is not available here
water.setBondMobility(BondMobility::Rigid, "O", "H1");
water.setBondMobility(BondMobility::Free, "O", "H2");
water.setAtomBiotype("O", "TIP3P Water", "O");
water.setAtomBiotype("H1", "TIP3P Water", "H");
water.setAtomBiotype("H2", "TIP3P Water", "H");
system.adoptCompound(water);
system.modelCompounds();
system.updDefaultState() = system.realizeTopology();
// debugging...
// system.realize(system.getDefaultState(), Stage::Position);
// cout << endl << endl;
// water.writePdb(system.getDefaultState(), cout);
}
};
void testBondBendEnergy(Real bondAngle)
{
Real observedEnergy = WaterSystem(bondAngle).calcEnergy();
Real r0 = 104.52 * degrees;
Real kR = 100.0 * kilocalories_per_mole / (degrees * degrees);
// Stiffness constant kR in MD codes like molmodel and tinker is 2.0 times what engineers would expect
// Real expectedEnergy = 0.5 * kR * (bondLength - r0) * (bondLength - r0);
Real expectedEnergy = kR * (bondAngle - r0) * (bondAngle - r0);
SimTK_TEST_EQ(expectedEnergy, observedEnergy);
}
void testBondBendForce() {
testBondBendEnergy(104.52*degrees);
testBondBendEnergy(90.0*degrees);
testBondBendEnergy(85.3*degrees);
}
//////////////////////
// Custom Bond Bend //
//////////////////////
class HarmonicBondBend
: public DuMM::CustomBondBend
{
public:
HarmonicBondBend(Real stiffnessInKJPerNmSquared, Real idealAngleInRadians)
: stiffness(stiffnessInKJPerNmSquared), idealAngle(idealAngleInRadians)
{}
Real calcEnergy(Real angle) const {
Real dR = angle - idealAngle;
return stiffness * dR * dR;
}
Real calcTorque(Real angle) const {
Real dR = angle - idealAngle;
return -2.0 * stiffness * dR;
}
private:
Real idealAngle;
Real stiffness;
};
class CustomWaterSystem : public WaterSystem
{
public:
CustomWaterSystem(Real bondAngle)
: WaterSystem(bondAngle)
{
dumm.defineCustomBondBend(
dumm.getAtomClassIndex("TIP3P Water H"),
dumm.getAtomClassIndex("TIP3P Water O"),
dumm.getAtomClassIndex("TIP3P Water H"),
new HarmonicBondBend(
100.00 * kilocalories_per_mole / (degrees * degrees),
104.52 * degrees)
);
dumm.setAllGlobalScaleFactors(0.0);
dumm.setCustomBondBendGlobalScaleFactor(1.0);
system.updDefaultState() = system.realizeTopology();
}
};
// Show that HarmonicCustomBondBend forces are the same as those of internal DuMM bond bends
// It doesn't prove the forces are correct, but at least they agree.
void testCustomBondBendToBondBend(Real bondAngle)
{
WaterSystem internalSystem(bondAngle);
CustomWaterSystem customSystem(bondAngle);
for (MobilizedBodyIndex b(0); b < internalSystem.getMatter().getNumBodies(); ++b) {
SimTK_TEST_EQ(internalSystem.calcForce(b), customSystem.calcForce(b));
}
}
void testCustomBondBendEnergy(Real bondAngle)
{
Real observedEnergy = CustomWaterSystem(bondAngle).calcEnergy();
Real r0 = 104.52 * degrees;
Real kR = 100.0 * kilocalories_per_mole / (degrees * degrees);
// Stiffness constant kR in MD codes like molmodel and tinker is 2.0 times what engineers would expect
// Real expectedEnergy = 0.5 * kR * (bondLength - r0) * (bondLength - r0);
Real expectedEnergy = kR * (bondAngle - r0) * (bondAngle - r0);
SimTK_TEST_EQ(expectedEnergy, observedEnergy);
}
void testCustomBondBendForce()
{
testCustomBondBendEnergy(90.0 * degrees);
testCustomBondBendEnergy(104.5 * degrees);
testCustomBondBendEnergy(110.0 * degrees);
testCustomBondBendToBondBend(90.0 * degrees);
testCustomBondBendToBondBend(104.5 * degrees);
testCustomBondBendToBondBend(110.0 * degrees);
}
////////////////////////////
// Dihedral Torsion Angle //
////////////////////////////
class HydrogenPeroxideSystem : public TestSystem
{
public:
HydrogenPeroxideSystem(Real torsion) {
// Turn off all but bond bend force
dumm.setAllGlobalScaleFactors(0.0);
dumm.setBondTorsionGlobalScaleFactor(1.0);
dumm.loadAmber99Parameters();
if (! dumm.hasChargedAtomType("H2O2 O") ) {
dumm.defineChargedAtomType(
dumm.getNextUnusedChargedAtomTypeIndex(),
"H2O2 O",
dumm.getAtomClassIndex("OH"),
-0.834
);
}
if (! dumm.hasChargedAtomType("H2O2 H") ) {
dumm.defineChargedAtomType(
dumm.getNextUnusedChargedAtomTypeIndex(),
"H2O2 H",
dumm.getAtomClassIndex("HO"),
0.417
);
}
if (! Biotype::exists("H2O2", "O") )
Biotype::defineBiotype(Element::getBySymbol("O"), 2, "H2O2", "O");
if (! Biotype::exists("H2O2", "H") )
Biotype::defineBiotype(Element::getBySymbol("H"), 1, "H2O2", "H");
dumm.setBiotypeChargedAtomType( dumm.getChargedAtomTypeIndex("H2O2 O"), Biotype::get("H2O2", "O").getIndex() );
dumm.setBiotypeChargedAtomType( dumm.getChargedAtomTypeIndex("H2O2 H"), Biotype::get("H2O2", "H").getIndex() );
dumm.defineBondStretch(
dumm.getAtomClassIndex("OH"),
dumm.getAtomClassIndex("OH"),
500.0 * kilocalories_per_mole / (angstroms * angstroms),
1.4464 * angstroms
);
dumm.defineBondBend(
dumm.getAtomClassIndex("HO"),
dumm.getAtomClassIndex("OH"),
dumm.getAtomClassIndex("OH"),
120.0 * kilocalories_per_mole / (degrees * degrees),
100.8982 // * degrees // defineBondBend() takes actual degrees
);
dumm.defineBondTorsion(
dumm.getAtomClassIndex("HO"),
dumm.getAtomClassIndex("OH"),
dumm.getAtomClassIndex("OH"),
dumm.getAtomClassIndex("HO"),
2, // periodicity
0.53 * kilocalories_per_mole,
111.9324 // * degrees // defineTorsion() takes actual degrees
// 0.0 // * degrees // defineTorsion() takes actual degrees
);
Compound h2O2;
h2O2.setBaseAtom( BivalentAtom("O1", Element::getBySymbol("O")) );
h2O2.bondAtom( BivalentAtom("O2", Element::getBySymbol("O")), "O1/bond1", 1.4464 * angstroms );
h2O2.bondAtom( UnivalentAtom("H1", Element::getBySymbol("H")), "O1/bond2", 0.9659 * angstroms );
h2O2.bondAtom( UnivalentAtom("H2", Element::getBySymbol("H")), "O2/bond2", 0.9659 * angstroms );
h2O2.setDefaultBondAngle(100.8982 * degrees, "H1", "O1", "O2");
h2O2.setDefaultBondAngle(100.8982 * degrees, "O1", "O2", "H2");
h2O2.setDefaultDihedralAngle(torsion, "H1", "O1", "O2", "H2");
// Unfortunately, there is not yet a BondMobility::Angle concept, so pure Pin is not available here
h2O2.setBondMobility(BondMobility::Rigid, "O1", "H1");
h2O2.setBondMobility(BondMobility::Rigid, "O2", "H2");
h2O2.setBondMobility(BondMobility::Torsion, "O1", "O2");
h2O2.setAtomBiotype("O1", "H2O2", "O");
h2O2.setAtomBiotype("O2", "H2O2", "O");
h2O2.setAtomBiotype("H1", "H2O2", "H");
h2O2.setAtomBiotype("H2", "H2O2", "H");
system.adoptCompound(h2O2);
system.modelCompounds();
o2BodyIndex = h2O2.getAtomMobilizedBodyIndex(h2O2.getAtomIndex("O2"));
system.updDefaultState() = system.realizeTopology();
}
Real calcTorque() const
{
system.realize(system.getDefaultState(), Stage::Dynamics);
// Express rigid body forces in terms of mobilities
Vector mobilityForces;
matter.calcTreeEquivalentMobilityForces(
system.getDefaultState(),
system.getRigidBodyForces(system.getDefaultState(), Stage::Dynamics),
mobilityForces);
Real torque = matter.getMobilizedBody(o2BodyIndex).getOneFromUPartition(
system.getDefaultState(),
0,
mobilityForces);
return torque;
}
protected:
MobilizedBodyIndex o2BodyIndex;
};
void testTorsionEnergy(Real torsion)
{
Real observedEnergy = HydrogenPeroxideSystem(torsion).calcEnergy();
int periodicity = 2;
Real halfAmplitude = 0.53 * kilocalories_per_mole;
Real phase = 111.9324 * degrees;
// Real phase = 0.0 * degrees;
// Torsion is not doubled
Real expectedEnergy = halfAmplitude * ( 1.0 + std::cos(periodicity * torsion - phase) );
SimTK_TEST_EQ(expectedEnergy, observedEnergy);
}
// Finite difference to prove Force = -dEnergy/dLength
void testTorsionEnergyVsTorque(Real torsion)
{
// Finite difference must be small relative to total distance
Real delta = 1e-4;
// Estimate force from -dEnergy/dDistance
Real energy1 = HydrogenPeroxideSystem(torsion).calcEnergy();
Real energy2 = HydrogenPeroxideSystem(torsion + delta).calcEnergy();
Real expectedTorque = - (energy2 - energy1)/delta;
Real observedTorque = HydrogenPeroxideSystem(torsion + delta/2.0).calcTorque();
SimTK_TEST_EQ_TOL(expectedTorque, observedTorque, 0.01);
}
void testTorsionForce() {
testTorsionEnergy(90.0 * degrees);
testTorsionEnergy(111.932 * degrees);
testTorsionEnergy(120.0 * degrees);
testTorsionEnergy(191.932 * degrees);
testTorsionEnergyVsTorque(90.0 * degrees);
testTorsionEnergyVsTorque(111.932 * degrees);
testTorsionEnergyVsTorque(120.0 * degrees);
testTorsionEnergyVsTorque(191.932 * degrees);
}
///////////////////////////
// Custom Torsion forces //
///////////////////////////
class SinusoidalTorsion : public DuMM::CustomBondTorsion {
public:
SinusoidalTorsion(int periodicity, Real halfAmplitudeInKJPerMole, Real phaseInRadians)
: periodicity(periodicity), halfAmplitude(halfAmplitudeInKJPerMole), phase(phaseInRadians)
{}
Real calcEnergy(Real torsionInRadians) const {
return halfAmplitude * ( 1.0 + std::cos(periodicity * torsionInRadians - phase) );
}
Real calcTorque(Real torsionInRadians) const {
return periodicity * halfAmplitude * ( std::sin(periodicity * torsionInRadians - phase) );
}
private:
int periodicity;
Real halfAmplitude;
Real phase;
};
class CustomHydrogenPeroxideSystem : public HydrogenPeroxideSystem
{
public:
CustomHydrogenPeroxideSystem(Real torsion) : HydrogenPeroxideSystem(torsion) {
dumm.setAllGlobalScaleFactors(0.0);
dumm.setCustomBondTorsionGlobalScaleFactor(1.0);
dumm.defineCustomBondTorsion(
dumm.getAtomClassIndex("HO"),
dumm.getAtomClassIndex("OH"),
dumm.getAtomClassIndex("OH"),
dumm.getAtomClassIndex("HO"),
new SinusoidalTorsion(
2, // periodicity
1.42 * kilocalories_per_mole, // half amplitude
73.4 * degrees) // phase
);
system.updDefaultState() = system.realizeTopology();
}
};
void testCustomTorsionEnergy(Real torsion)
{
Real observedEnergy = CustomHydrogenPeroxideSystem(torsion).calcEnergy();
int periodicity = 2;
Real halfAmplitude = 1.42 * kilocalories_per_mole;
Real phase = 73.4 * degrees;
// Real phase = 0.0 * degrees;
// Torsion is not doubled
Real expectedEnergy = halfAmplitude * ( 1.0 + std::cos(periodicity * torsion - phase) );
SimTK_TEST_EQ(expectedEnergy, observedEnergy);
}
// Finite difference to prove Force = -dEnergy/dLength
void testCustomTorsionEnergyVsTorque(Real torsion)
{
// Finite difference must be small relative to total distance
Real delta = 1e-4;
// Estimate force from -dEnergy/dDistance
Real energy1 = CustomHydrogenPeroxideSystem(torsion).calcEnergy();
Real energy2 = CustomHydrogenPeroxideSystem(torsion + delta).calcEnergy();
Real expectedTorque = - (energy2 - energy1)/delta;
Real observedTorque = CustomHydrogenPeroxideSystem(torsion + delta/2.0).calcTorque();
SimTK_TEST_EQ_TOL(expectedTorque, observedTorque, 0.01);
}
void testCustomTorsionForce()
{
testCustomTorsionEnergy(90.0 * degrees);
testCustomTorsionEnergy(111.932 * degrees);
testCustomTorsionEnergy(120.0 * degrees);
testCustomTorsionEnergy(191.932 * degrees);
testCustomTorsionEnergyVsTorque(90.0 * degrees);
testCustomTorsionEnergyVsTorque(111.932 * degrees);
testCustomTorsionEnergyVsTorque(120.0 * degrees);
testCustomTorsionEnergyVsTorque(191.932 * degrees);
}
int main()
{
SimTK_START_TEST("TestDuMMForces");
SimTK_SUBTEST(testCoulombForce);
SimTK_SUBTEST(testVanDerWaalsForce);
SimTK_SUBTEST(testBondStretchForce);
SimTK_SUBTEST(testCustomBondStretchForce);
SimTK_SUBTEST(testBondBendForce);
SimTK_SUBTEST(testCustomBondBendForce);
SimTK_SUBTEST(testTorsionForce);
SimTK_SUBTEST(testCustomTorsionForce);
SimTK_END_TEST();
}
|