def test_protein_moves(verbose = False): mols = MoleculeGroup("mols") for residue in protein.residues(): mols.add(residue) oldsys = System() newsys = System() parsys = System() oldsys.add(oldff) oldsys.add(mols) newsys.add(newff) newsys.add(mols) parsys.add(parff) parsys.add(mols) moves = RigidBodyMC(mols) moves.setMaximumTranslation(0.25 * angstrom) moves.setMaximumRotation(1 * degrees) moves.enableOptimisedMoves() oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() parsys.mustNowRecalculateFromScratch() if verbose: print("\nChecking total system energies...") _checkEnergies(oldsys, newsys, parsys, verbose) if verbose: print("\nChecking system simulation...") moves.setGenerator( RanGenerator(42) ) moves.clearStatistics() t.start() moves.move(oldsys, nmoves, False) oldns = t.nsecsElapsed() old_naccept = moves.nAccepted() old_nreject = moves.nRejected() moves.setGenerator( RanGenerator(42) ) moves.clearStatistics() t.start() moves.move(newsys, nmoves, False) newns = t.nsecsElapsed() new_naccept = moves.nAccepted() new_nreject = moves.nRejected() moves.setGenerator( RanGenerator(42) ) moves.clearStatistics() t.start() moves.move(parsys, nmoves, False) parns = t.nsecsElapsed() par_naccept = moves.nAccepted() par_nreject = moves.nRejected() if verbose: print("OLD: accept = %s, reject = %s, time = %s ms" % \ (old_naccept, old_nreject, oldns*0.000001)) print("NEW: accept = %s, reject = %s, time = %s ms" % \ (new_naccept, new_nreject, newns*0.000001)) print("PAR: accept = %s, reject = %s, time = %s ms" % \ (par_naccept, par_nreject, parns*0.000001)) print("\nChecking final total energies...") _checkEnergies(oldsys, newsys, parsys, verbose) if verbose: print("\nRechecking total energies...") oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() parsys.mustNowRecalculateFromScratch() _checkEnergies(oldsys, newsys, parsys, verbose)
def test_system(verbose=False): cljff = InterCLJFF() mincoords = Vector(-18.3854, -18.66855, -18.4445) maxcoords = Vector( 18.3854, 18.66855, 18.4445) vol = PeriodicBox(mincoords, maxcoords) cljff.setSpace(vol) mols = PDB().read("../io/water.pdb") if verbose: print("Read in %d molecules!" % mols.nMolecules()) i = 0 mol = mols.moleculeAt(0).molecule() mol = mol.edit().atom( AtomName("O00") ) \ .setProperty("LJ", LJParameter(3.15363*angstrom, \ 0.1550*kcal_per_mol)).molecule() \ .atom( AtomName("H01") ) \ .setProperty("charge", 0.520 * mod_electron).molecule() \ .atom( AtomName("H02") ) \ .setProperty("charge", 0.520 * mod_electron).molecule() \ .atom( AtomName("M03") ) \ .setProperty("charge", -1.04 * mod_electron).molecule() \ .commit() charges = mol.property("charge") ljs = mol.property("LJ") cljff.add(mol) for i in range(1, mols.nMolecules()): mol = mols.moleculeAt(i).molecule() mol = mol.edit().setProperty("charge", charges) \ .setProperty("LJ", ljs) \ .commit() cljff.add(mol) system = System() system.add(cljff) nrg = system.energy() if verbose: print("System energy = %s" % (nrg)) copy_system = System(system) nrg2 = copy_system.energy() if verbose: print("Copy energy: %s (should be %s)" % (nrg2,nrg)) assert_almost_equal(nrg.value(), nrg2.value(), 5) copy_system.mustNowRecalculateFromScratch() nrg3 = copy_system.energy() if verbose: print("Copy energy: %s (should be %s)" % (nrg2,nrg)) assert_almost_equal(nrg.value(), nrg3.value(), 5)
def test_system(verbose=False): cljff = InterCLJFF() mincoords = Vector(-18.3854, -18.66855, -18.4445) maxcoords = Vector(18.3854, 18.66855, 18.4445) vol = PeriodicBox(mincoords, maxcoords) cljff.setSpace(vol) mols = PDB().read("../io/water.pdb") if verbose: print("Read in %d molecules!" % mols.nMolecules()) i = 0 mol = mols.moleculeAt(0).molecule() mol = mol.edit().atom( AtomName("O00") ) \ .setProperty("LJ", LJParameter(3.15363*angstrom, \ 0.1550*kcal_per_mol)).molecule() \ .atom( AtomName("H01") ) \ .setProperty("charge", 0.520 * mod_electron).molecule() \ .atom( AtomName("H02") ) \ .setProperty("charge", 0.520 * mod_electron).molecule() \ .atom( AtomName("M03") ) \ .setProperty("charge", -1.04 * mod_electron).molecule() \ .commit() charges = mol.property("charge") ljs = mol.property("LJ") cljff.add(mol) for i in range(1, mols.nMolecules()): mol = mols.moleculeAt(i).molecule() mol = mol.edit().setProperty("charge", charges) \ .setProperty("LJ", ljs) \ .commit() cljff.add(mol) system = System() system.add(cljff) nrg = system.energy() if verbose: print("System energy = %s" % (nrg)) copy_system = System(system) nrg2 = copy_system.energy() if verbose: print("Copy energy: %s (should be %s)" % (nrg2, nrg)) assert_almost_equal(nrg.value(), nrg2.value(), 5) copy_system.mustNowRecalculateFromScratch() nrg3 = copy_system.energy() if verbose: print("Copy energy: %s (should be %s)" % (nrg2, nrg)) assert_almost_equal(nrg.value(), nrg3.value(), 5)
def test_ligand_moves(verbose = False): mols = MoleculeGroup("mols") mols.add(ligand) oldsys = System() newsys = System() parsys = System() oldsys.add(oldff) oldsys.add(mols) newsys.add(newff) newsys.add(mols) parsys.add(parff) parsys.add(mols) moves = InternalMove(mols) moves.enableOptimisedMoves() oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() parsys.mustNowRecalculateFromScratch() if verbose: print("\nChecking total system energies...") _checkEnergies(oldsys, newsys, parsys, verbose) if verbose: print("\nChecking system simulation...") moves.setGenerator( RanGenerator(42) ) moves.clearStatistics() t.start() moves.move(oldsys, nmoves, False) oldns = t.nsecsElapsed() old_naccept = moves.nAccepted() old_nreject = moves.nRejected() moves.setGenerator( RanGenerator(42) ) moves.clearStatistics() t.start() moves.move(newsys, nmoves, False) newns = t.nsecsElapsed() new_naccept = moves.nAccepted() new_nreject = moves.nRejected() moves.setGenerator( RanGenerator(42) ) moves.clearStatistics() t.start() moves.move(parsys, nmoves, False) parns = t.nsecsElapsed() par_naccept = moves.nAccepted() par_nreject = moves.nRejected() if verbose: print("OLD: accept = %s, reject = %s, time = %s ms" % \ (old_naccept, old_nreject, oldns*0.000001)) print("NEW: accept = %s, reject = %s, time = %s ms" % \ (new_naccept, new_nreject, newns*0.000001)) print("PAR: accept = %s, reject = %s, time = %s ms" % \ (par_naccept, par_nreject, parns*0.000001)) print("\nChecking final total energies...") _checkEnergies(oldsys, newsys, parsys, verbose) if verbose: print("\nRechecking total energies...") oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() parsys.mustNowRecalculateFromScratch() _checkEnergies(oldsys, newsys, parsys, verbose)
def test_sim(verbose = False): oldsys = System() newsys = System() oldsys.add(mols) newsys.add(mols) oldsys.add(oldff) newsys.add(newff) t = QElapsedTimer() oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() t.start() nrgs = oldsys.energies() oldns = t.nsecsElapsed() t.start() nrgs = newsys.energies() newns = t.nsecsElapsed() oldcnrg = oldsys.energy( oldff.components().coulomb() ).value() oldljnrg = oldsys.energy( oldff.components().lj() ).value() newcnrg = newsys.energy( newff.components().coulomb() ).value() newljnrg = newsys.energy( newff.components().lj() ).value() if verbose: print("\nStarting energy") print("OLD SYS: %s %s %s : %s ms" % (oldcnrg+oldljnrg,oldcnrg,oldljnrg, 0.000001*oldns)) print("NEW SYS: %s %s %s : %s ms" % (newcnrg+newljnrg,newcnrg,newljnrg, 0.000001*newns)) moves = RigidBodyMC(mols) moves.setGenerator( RanGenerator( 42 ) ) moves.enableOptimisedMoves() t.start() moves.move(oldsys, nmoves, False) move_oldns = t.nsecsElapsed() old_naccepted = moves.nAccepted() old_nrejected = moves.nRejected() moves.setGenerator( RanGenerator( 42 ) ) moves.clearStatistics() t.start() moves.move(newsys, nmoves, False) move_newns = t.nsecsElapsed() new_naccepted = moves.nAccepted() new_nrejected = moves.nRejected() t.start() nrgs = oldsys.energies() oldns = t.nsecsElapsed() t.start() nrgs = newsys.energies() newns = t.nsecsElapsed() oldcnrg = oldsys.energy( oldff.components().coulomb() ).value() oldljnrg = oldsys.energy( oldff.components().lj() ).value() newcnrg = newsys.energy( newff.components().coulomb() ).value() newljnrg = newsys.energy( newff.components().lj() ).value() if verbose: print("\nMoves: old %s ms vs. new %s ms" % (0.000001*move_oldns, 0.000001*move_newns)) print("OLD SYS: %s %s %s : %s ms" % (oldcnrg+oldljnrg,oldcnrg,oldljnrg, 0.000001*oldns)) print("nAccepted() = %s, nRejected() = %s" % (old_naccepted, old_nrejected)) print("NEW SYS: %s %s %s : %s ms" % (newcnrg+newljnrg,newcnrg,newljnrg, 0.000001*newns)) print("nAccepted() = %s, nRejected() = %s" % (new_naccepted, new_nrejected)) oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() t.start() nrgs = oldsys.energies() oldns = t.nsecsElapsed() t.start() nrgs = newsys.energies() newns = t.nsecsElapsed() r_oldcnrg = oldsys.energy( oldff.components().coulomb() ).value() r_oldljnrg = oldsys.energy( oldff.components().lj() ).value() r_newcnrg = newsys.energy( newff.components().coulomb() ).value() r_newljnrg = newsys.energy( newff.components().lj() ).value() if verbose: print("\nRecalculated energy") print("OLD SYS: %s %s %s : %s ms" % (r_oldcnrg+r_oldljnrg,r_oldcnrg,r_oldljnrg, 0.000001*oldns)) print("NEW SYS: %s %s %s : %s ms" % (r_newcnrg+r_newljnrg,r_newcnrg,r_newljnrg, 0.000001*newns))
def test_grid_sim(verbose = False): oldsys = System() newsys = System() oldsys.add(cluster) oldsys.add(old_clusterff) oldsys.add(old_fixedff) newsys.add(cluster) newsys.add(new_clusterff) t = QElapsedTimer() t.start() old_total = oldsys.energy().value() oldns = t.nsecsElapsed() t.start() new_total = newsys.energy().value() newns = t.nsecsElapsed() ff = newsys[FFName("new_clusterff")] print(ff.grid()) old_cnrg = oldsys.energy( old_clusterff.components().coulomb() ).value() + \ oldsys.energy( old_fixedff.components().coulomb() ).value() old_ljnrg = oldsys.energy( old_clusterff.components().lj() ).value() + \ oldsys.energy( old_fixedff.components().lj() ).value() new_cnrg = newsys.energy( new_clusterff.components().coulomb() ).value() new_ljnrg = newsys.energy( new_clusterff.components().lj() ).value() if verbose: print("OLD: %s %s %s %s : %s ms" % (old_total,old_cnrg+old_ljnrg,old_cnrg,old_ljnrg, 0.000001*oldns)) print("NEW: %s %s %s %s : %s ms" % (new_total,new_cnrg+new_ljnrg,new_cnrg,new_ljnrg, 0.000001*newns)) moves = RigidBodyMC(cluster) moves.enableOptimisedMoves() moves.setReflectionSphere( reflect_sphere_center, reflect_sphere_radius ) moves.setGenerator( RanGenerator( 42 ) ) t.start() moves.move(oldsys, 1000, False) move_oldns = t.nsecsElapsed() moves.setGenerator( RanGenerator( 42 ) ) t.start() moves.move(newsys, 1000, False) move_newns = t.nsecsElapsed() t.start() old_total = oldsys.energy().value() old_ns = t.nsecsElapsed() t.start() new_total = newsys.energy().value() new_ns = t.nsecsElapsed() old_cnrg = oldsys.energy( old_clusterff.components().coulomb() ).value() + \ oldsys.energy( old_fixedff.components().coulomb() ).value() old_ljnrg = oldsys.energy( old_clusterff.components().lj() ).value() + \ oldsys.energy( old_fixedff.components().lj() ).value() new_cnrg = newsys.energy( new_clusterff.components().coulomb() ).value() new_ljnrg = newsys.energy( new_clusterff.components().lj() ).value() if verbose: print("\nMoves: %s ms vs. %s ms" % (0.000001*move_oldns, 0.000001*move_newns)) print("OLD SYS: %s %s %s %s : %s ms" % (old_total,old_cnrg+old_ljnrg,old_cnrg,old_ljnrg, 0.000001*old_ns)) print("NEW SYS: %s %s %s %s : %s ms" % (new_total,new_cnrg+new_ljnrg,new_cnrg,new_ljnrg, 0.000001*new_ns)) newsys.mustNowRecalculateFromScratch() oldsys.mustNowRecalculateFromScratch() t.start() old_total = oldsys.energy().value() old_ns = t.nsecsElapsed() t.start() new_total = newsys.energy().value() new_ns = t.nsecsElapsed() old_cnrg = oldsys.energy( old_clusterff.components().coulomb() ).value() + \ oldsys.energy( old_fixedff.components().coulomb() ).value() old_ljnrg = oldsys.energy( old_clusterff.components().lj() ).value() + \ oldsys.energy( old_fixedff.components().lj() ).value() new_cnrg = newsys.energy( new_clusterff.components().coulomb() ).value() new_ljnrg = newsys.energy( new_clusterff.components().lj() ).value() if verbose: print("\nRecalculate energy") print("OLD SYS: %s %s %s %s : %s ms" % (old_total,old_cnrg+old_ljnrg,old_cnrg,old_ljnrg, 0.000001*old_ns)) print("NEW SYS: %s %s %s %s : %s ms" % (new_total,new_cnrg+new_ljnrg,new_cnrg,new_ljnrg, 0.000001*new_ns))
system = moves.move(system, 10000, True) ms = t.elapsed() print("Done! (took %d ms)" % ms) system.add( "trajectory", TrajectoryMonitor(solvent), 1000 ) print("Running 10000 moves with saving the trajectory...") t.start() system = moves.move(system, 10000, True) ms = t.elapsed() print("Done! (took %d ms)" % ms) print("Writing the trajectory to disk...") t.start() system[ MonitorName("trajectory") ].writeToDisk("tempXXXXXX.pdb") ms = t.elapsed() print("Took %d ms" % ms) print("Final energy = %s" % system.energy()) system.mustNowRecalculateFromScratch(); print("Are we sure? = %s" % system.energy()) mc = moves.moves()[0] print("nAccepted() == %d, nRejected() == %d (%f %%)" % (mc.nAccepted(), \ mc.nRejected(), 100 * mc.acceptanceRatio()))
ms = t.elapsed() print("Submitted in %d ms" % ms) print(sim.hasFinished()) print(sim.isRunning()) print("Waiting...") sim.wait() ms = t.elapsed() system = sim.system() print("Final energy = %s" % system.energy()) system.mustNowRecalculateFromScratch() print("Are we sure? = %s" % system.energy()) mc = sim.moves().moves()[0] print("nAccepted() == %d, nRejected() == %d (%f %%)" % (mc.nAccepted(), \ mc.nRejected(), 100 * mc.acceptanceRatio())) print("Took %d ms" % ms) #mc.setSynchronisedTranslation(True) #mc.setSynchronisedRotation(True) #moves = SameMoves(mc) for i in range(0, 10):
def test_grid_sim(verbose=False): oldsys = System() newsys = System() oldsys.add(cluster) oldsys.add(old_clusterff) oldsys.add(old_fixedff) newsys.add(cluster) newsys.add(new_clusterff) t = QElapsedTimer() t.start() old_total = oldsys.energy().value() oldns = t.nsecsElapsed() t.start() new_total = newsys.energy().value() newns = t.nsecsElapsed() ff = newsys[FFName("new_clusterff")] print(ff.grid()) old_cnrg = oldsys.energy( old_clusterff.components().coulomb() ).value() + \ oldsys.energy( old_fixedff.components().coulomb() ).value() old_ljnrg = oldsys.energy( old_clusterff.components().lj() ).value() + \ oldsys.energy( old_fixedff.components().lj() ).value() new_cnrg = newsys.energy(new_clusterff.components().coulomb()).value() new_ljnrg = newsys.energy(new_clusterff.components().lj()).value() if verbose: print("OLD: %s %s %s %s : %s ms" % (old_total, old_cnrg + old_ljnrg, old_cnrg, old_ljnrg, 0.000001 * oldns)) print("NEW: %s %s %s %s : %s ms" % (new_total, new_cnrg + new_ljnrg, new_cnrg, new_ljnrg, 0.000001 * newns)) moves = RigidBodyMC(cluster) moves.setReflectionSphere(reflect_sphere_center, reflect_sphere_radius) moves.setGenerator(RanGenerator(42)) t.start() moves.move(oldsys, 1000, False) move_oldns = t.nsecsElapsed() moves.setGenerator(RanGenerator(42)) t.start() moves.move(newsys, 1000, False) move_newns = t.nsecsElapsed() t.start() old_total = oldsys.energy().value() old_ns = t.nsecsElapsed() t.start() new_total = newsys.energy().value() new_ns = t.nsecsElapsed() old_cnrg = oldsys.energy( old_clusterff.components().coulomb() ).value() + \ oldsys.energy( old_fixedff.components().coulomb() ).value() old_ljnrg = oldsys.energy( old_clusterff.components().lj() ).value() + \ oldsys.energy( old_fixedff.components().lj() ).value() new_cnrg = newsys.energy(new_clusterff.components().coulomb()).value() new_ljnrg = newsys.energy(new_clusterff.components().lj()).value() if verbose: print("\nMoves: %s ms vs. %s ms" % (0.000001 * move_oldns, 0.000001 * move_newns)) print("OLD SYS: %s %s %s %s : %s ms" % (old_total, old_cnrg + old_ljnrg, old_cnrg, old_ljnrg, 0.000001 * old_ns)) print("NEW SYS: %s %s %s %s : %s ms" % (new_total, new_cnrg + new_ljnrg, new_cnrg, new_ljnrg, 0.000001 * new_ns)) newsys.mustNowRecalculateFromScratch() oldsys.mustNowRecalculateFromScratch() t.start() old_total = oldsys.energy().value() old_ns = t.nsecsElapsed() t.start() new_total = newsys.energy().value() new_ns = t.nsecsElapsed() old_cnrg = oldsys.energy( old_clusterff.components().coulomb() ).value() + \ oldsys.energy( old_fixedff.components().coulomb() ).value() old_ljnrg = oldsys.energy( old_clusterff.components().lj() ).value() + \ oldsys.energy( old_fixedff.components().lj() ).value() new_cnrg = newsys.energy(new_clusterff.components().coulomb()).value() new_ljnrg = newsys.energy(new_clusterff.components().lj()).value() if verbose: print("\nRecalculate energy") print("OLD SYS: %s %s %s %s : %s ms" % (old_total, old_cnrg + old_ljnrg, old_cnrg, old_ljnrg, 0.000001 * old_ns)) print("NEW SYS: %s %s %s %s : %s ms" % (new_total, new_cnrg + new_ljnrg, new_cnrg, new_ljnrg, 0.000001 * new_ns))
def test_sim(verbose=False): oldsys = System() newsys = System() #oldsys.add(mols) #newsys.add(mols) oldsys.add(oldff) newsys.add(newff) t = QElapsedTimer() oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() t.start() nrgs = oldsys.energies() oldns = t.nsecsElapsed() t.start() nrgs = newsys.energies() newns = t.nsecsElapsed() oldcnrg = oldsys.energy(oldff.components().coulomb()).value() oldljnrg = oldsys.energy(oldff.components().lj()).value() newcnrg = newsys.energy(newff.components().coulomb()).value() newljnrg = newsys.energy(newff.components().lj()).value() if verbose: print("\nStarting energy") print("OLD SYS: %s %s %s : %s ms" % (oldcnrg + oldljnrg, oldcnrg, oldljnrg, 0.000001 * oldns)) print("NEW SYS: %s %s %s : %s ms" % (newcnrg + newljnrg, newcnrg, newljnrg, 0.000001 * newns)) moves = RigidBodyMC(mols) moves.setGenerator(RanGenerator(42)) t.start() moves.move(oldsys, nmoves, False) move_oldns = t.nsecsElapsed() old_naccepted = moves.nAccepted() old_nrejected = moves.nRejected() moves.setGenerator(RanGenerator(42)) moves.clearStatistics() t.start() moves.move(newsys, nmoves, False) move_newns = t.nsecsElapsed() new_naccepted = moves.nAccepted() new_nrejected = moves.nRejected() t.start() nrgs = oldsys.energies() oldns = t.nsecsElapsed() t.start() nrgs = newsys.energies() newns = t.nsecsElapsed() oldcnrg = oldsys.energy(oldff.components().coulomb()).value() oldljnrg = oldsys.energy(oldff.components().lj()).value() newcnrg = newsys.energy(newff.components().coulomb()).value() newljnrg = newsys.energy(newff.components().lj()).value() if verbose: print("\nMoves: %s ms vs. %s ms" % (0.000001 * move_oldns, 0.000001 * move_newns)) print("OLD SYS: %s %s %s : %s ms" % (oldcnrg + oldljnrg, oldcnrg, oldljnrg, 0.000001 * oldns)) print("nAccepted() = %s, nRejected() = %s" % (old_naccepted, old_nrejected)) print("NEW SYS: %s %s %s : %s ms" % (newcnrg + newljnrg, newcnrg, newljnrg, 0.000001 * newns)) print("nAccepted() = %s, nRejected() = %s" % (new_naccepted, new_nrejected)) oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() t.start() nrgs = oldsys.energies() oldns = t.nsecsElapsed() t.start() nrgs = newsys.energies() newns = t.nsecsElapsed() r_oldcnrg = oldsys.energy(oldff.components().coulomb()).value() r_oldljnrg = oldsys.energy(oldff.components().lj()).value() r_newcnrg = newsys.energy(newff.components().coulomb()).value() r_newljnrg = newsys.energy(newff.components().lj()).value() if verbose: print("\nRecalculated energy") print( "OLD SYS: %s %s %s : %s ms" % (r_oldcnrg + r_oldljnrg, r_oldcnrg, r_oldljnrg, 0.000001 * oldns)) print( "NEW SYS: %s %s %s : %s ms" % (r_newcnrg + r_newljnrg, r_newcnrg, r_newljnrg, 0.000001 * newns))
def test_sim(verbose = False): oldsys = System() newsys = System() parsys = System() oldsys.add(mols) newsys.add(mols) parsys.add(mols) oldsys.add(newff) newsys.add(newff) parsys.add(parff) t = QElapsedTimer() oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() parsys.mustNowRecalculateFromScratch() t.start() nrgs = oldsys.energies() oldns = t.nsecsElapsed() t.start() nrgs = newsys.energies() newns = t.nsecsElapsed() t.start() nrgs = parsys.energies() parns = t.nsecsElapsed() oldcnrg = oldsys.energy( newff.components().coulomb() ).value() oldljnrg = oldsys.energy( newff.components().lj() ).value() newcnrg = newsys.energy( newff.components().coulomb() ).value() newljnrg = newsys.energy( newff.components().lj() ).value() parcnrg = parsys.energy( parff.components().coulomb() ).value() parljnrg = parsys.energy( parff.components().lj() ).value() if verbose: print("\nStarting energy") print("OLD SYS: %s %s %s : %s ms" % (oldcnrg+oldljnrg,oldcnrg,oldljnrg, 0.000001*oldns)) print("NEW SYS: %s %s %s : %s ms" % (newcnrg+newljnrg,newcnrg,newljnrg, 0.000001*newns)) print("PAR SYS: %s %s %s : %s ms" % (parcnrg+parljnrg,parcnrg,parljnrg, 0.000001*parns)) assert_almost_equal( oldcnrg, newcnrg ) assert_almost_equal( oldljnrg, newljnrg ) assert_almost_equal( parcnrg, newcnrg ) assert_almost_equal( parljnrg, newljnrg ) moves = RigidBodyMC(mols) moves.disableOptimisedMoves() moves.setGenerator( RanGenerator( 42 ) ) optmoves = RigidBodyMC(mols) optmoves.enableOptimisedMoves() optmoves.setGenerator( RanGenerator( 42 ) ) parmoves = RigidBodyMC(mols) parmoves.enableOptimisedMoves() parmoves.setGenerator( RanGenerator( 42 ) ) t.start() moves.move(oldsys, nmoves, False) move_oldns = t.nsecsElapsed() old_naccepted = moves.nAccepted() old_nrejected = moves.nRejected() t.start() optmoves.move(newsys, nmoves, False) move_newns = t.nsecsElapsed() new_naccepted = optmoves.nAccepted() new_nrejected = optmoves.nRejected() t.start() parmoves.move(parsys, nmoves, False) move_parns = t.nsecsElapsed() par_naccepted = parmoves.nAccepted() par_nrejected = parmoves.nRejected() t.start() nrgs = oldsys.energies() oldns = t.nsecsElapsed() t.start() nrgs = newsys.energies() newns = t.nsecsElapsed() t.start() nrgs = parsys.energies() parns = t.nsecsElapsed() oldcnrg = oldsys.energy( newff.components().coulomb() ).value() oldljnrg = oldsys.energy( newff.components().lj() ).value() newcnrg = newsys.energy( newff.components().coulomb() ).value() newljnrg = newsys.energy( newff.components().lj() ).value() parcnrg = parsys.energy( parff.components().coulomb() ).value() parljnrg = parsys.energy( parff.components().lj() ).value() if verbose: print("\nMoves: %s ms vs. %s ms vs. %s ms" % (0.000001*move_oldns, 0.000001*move_newns, 0.000001*move_parns)) print("OLD SYS: %s %s %s : %s ms" % (oldcnrg+oldljnrg,oldcnrg,oldljnrg, 0.000001*oldns)) print("nAccepted() = %s, nRejected() = %s" % (old_naccepted, old_nrejected)) print("NEW SYS: %s %s %s : %s ms" % (newcnrg+newljnrg,newcnrg,newljnrg, 0.000001*newns)) print("nAccepted() = %s, nRejected() = %s" % (new_naccepted, new_nrejected)) print("PAR SYS: %s %s %s : %s ms" % (parcnrg+parljnrg,parcnrg,parljnrg, 0.000001*parns)) print("nAccepted() = %s, nRejected() = %s" % (par_naccepted, par_nrejected)) assert_almost_equal( old_naccepted, new_naccepted ) assert_almost_equal( old_nrejected, new_nrejected ) assert_almost_equal( oldcnrg, newcnrg ) assert_almost_equal( oldljnrg, newljnrg ) assert_almost_equal( par_naccepted, new_naccepted ) assert_almost_equal( par_nrejected, new_nrejected ) assert_almost_equal( parcnrg, newcnrg ) assert_almost_equal( parljnrg, newljnrg ) oldsys.mustNowRecalculateFromScratch() newsys.mustNowRecalculateFromScratch() parsys.mustNowRecalculateFromScratch() t.start() nrgs = oldsys.energies() oldns = t.nsecsElapsed() t.start() nrgs = newsys.energies() newns = t.nsecsElapsed() t.start() nrgs = parsys.energies() parns = t.nsecsElapsed() r_oldcnrg = oldsys.energy( newff.components().coulomb() ).value() r_oldljnrg = oldsys.energy( newff.components().lj() ).value() r_newcnrg = newsys.energy( newff.components().coulomb() ).value() r_newljnrg = newsys.energy( newff.components().lj() ).value() r_parcnrg = parsys.energy( parff.components().coulomb() ).value() r_parljnrg = parsys.energy( parff.components().lj() ).value() if verbose: print("\nRecalculated energy") print("OLD SYS: %s %s %s : %s ms" % (r_oldcnrg+r_oldljnrg,r_oldcnrg,r_oldljnrg, 0.000001*oldns)) print("NEW SYS: %s %s %s : %s ms" % (r_newcnrg+r_newljnrg,r_newcnrg,r_newljnrg, 0.000001*newns)) print("PAR SYS: %s %s %s : %s ms" % (r_parcnrg+r_parljnrg,r_parcnrg,r_parljnrg, 0.000001*parns)) assert_almost_equal( r_oldcnrg, r_newcnrg ) assert_almost_equal( r_oldljnrg, r_newljnrg ) assert_almost_equal( r_parljnrg, r_newljnrg )
def test_moves(verbose = False): newsys = System() oldsys = System() optsys = System() parsys = System() res = MoleculeGroup("residues") protein = mols[ MolWithResID("ALA") ].molecule() for residue in protein.residues(): res.add(residue) newsys.add(newff) oldsys.add(oldff) optsys.add(newff) parsys.add(parff) newsys.add(res) oldsys.add(res) optsys.add(res) parsys.add(res) moves = RigidBodyMC(res) moves.disableOptimisedMoves() moves.setMaximumTranslation( 0.2 * angstrom ) moves.setMaximumRotation( 0.5 * degrees ) opt_moves = RigidBodyMC(res) opt_moves.enableOptimisedMoves() opt_moves.setMaximumTranslation( 0.2 * angstrom ) opt_moves.setMaximumRotation( 0.5 * degrees ) t = QElapsedTimer() if verbose: print("Calculating initial energy...") t.start() oldnrg = oldsys.energy().value() oldns = t.nsecsElapsed() t.start() newnrg = newsys.energy().value() newns = t.nsecsElapsed() t.start() optnrg = optsys.energy().value() optns = t.nsecsElapsed() t.start() parnrg = parsys.energy().value() parns = t.nsecsElapsed() if verbose: print("\nTIMES: old = %s ms, new = %s ms, opt = %s ms, par = %s ms" % \ (oldns*0.000001,newns*0.000001,optns*0.000001,parns*0.000001)) print("\nENERGIES: old = %s, new = %s, opt = %s, par = %s" % \ (oldnrg,newnrg,optnrg,parnrg)) assert_almost_equal( oldnrg, newnrg, 0.5 ) assert_almost_equal( optnrg, newnrg, 0.1 ) assert_almost_equal( parnrg, newnrg, 0.1 ) if verbose: print("\nPerforming simulation...") moves.clearStatistics() moves.setGenerator( RanGenerator(42) ) t.start() moves.move(oldsys, nmoves, False) oldns = t.nsecsElapsed() old_naccept = moves.nAccepted() old_nreject = moves.nRejected() oldcnrg = oldsys.energy( oldff.components().coulomb() ).value() oldljnrg = oldsys.energy( oldff.components().lj() ).value() oldsys.mustNowRecalculateFromScratch() check_oldcnrg = oldsys.energy( oldff.components().coulomb() ).value() check_oldljnrg = oldsys.energy( oldff.components().lj() ).value() moves.clearStatistics() moves.setGenerator( RanGenerator(42) ) t.start() moves.move(newsys, nmoves, False) newns = t.nsecsElapsed() new_naccept = moves.nAccepted() new_nreject = moves.nRejected() newcnrg = newsys.energy( newff.components().coulomb() ).value() newljnrg = newsys.energy( newff.components().lj() ).value() newsys.mustNowRecalculateFromScratch() check_newcnrg = newsys.energy( newff.components().coulomb() ).value() check_newljnrg = newsys.energy( newff.components().lj() ).value() opt_moves.clearStatistics() opt_moves.setGenerator( RanGenerator(42) ) t.start() opt_moves.move(optsys, nmoves, False) optns = t.nsecsElapsed() opt_naccept = moves.nAccepted() opt_nreject = moves.nRejected() optcnrg = optsys.energy( newff.components().coulomb() ).value() optljnrg = optsys.energy( newff.components().lj() ).value() optsys.mustNowRecalculateFromScratch() check_optcnrg = optsys.energy( newff.components().coulomb() ).value() check_optljnrg = optsys.energy( newff.components().lj() ).value() opt_moves.clearStatistics() opt_moves.setGenerator( RanGenerator(42) ) t.start() opt_moves.move(parsys, nmoves, False) parns = t.nsecsElapsed() par_naccept = moves.nAccepted() par_nreject = moves.nRejected() parcnrg = parsys.energy( parff.components().coulomb() ).value() parljnrg = parsys.energy( parff.components().lj() ).value() parsys.mustNowRecalculateFromScratch() check_parcnrg = parsys.energy( parff.components().coulomb() ).value() check_parljnrg = parsys.energy( parff.components().lj() ).value() if verbose: print("\nTIMES: old = %s ms, new = %s ms, opt = %s ms, par = %s ms" % \ (oldns*0.000001,newns*0.000001,optns*0.000001,parns*0.000001)) print("\nNACCEPT: old = %s, new = %s, opt = %s, par = %s" % \ (old_naccept,new_naccept,opt_naccept,par_naccept)) print("NREJECT: old = %s, new = %s, opt = %s, par = %s" % \ (old_nreject,new_nreject,opt_nreject,par_nreject)) print("\nTotal energy") print("OLD FF : %s %s %s" % (oldcnrg+oldljnrg,oldcnrg,oldljnrg)) print("NEW FF : %s %s %s" % (newcnrg+newljnrg,newcnrg,newljnrg)) print("OPT FF : %s %s %s" % (optcnrg+optljnrg,optcnrg,optljnrg)) print("PAR FF : %s %s %s" % (parcnrg+parljnrg,parcnrg,parljnrg)) print("\nCheck energy") print("OLD FF : %s %s %s" % (check_oldcnrg+check_oldljnrg,check_oldcnrg,check_oldljnrg)) print("NEW FF : %s %s %s" % (check_newcnrg+check_newljnrg,check_newcnrg,check_newljnrg)) print("OPT FF : %s %s %s" % (check_optcnrg+check_optljnrg,check_optcnrg,check_optljnrg)) print("PAR FF : %s %s %s" % (check_parcnrg+check_parljnrg,check_parcnrg,check_parljnrg)) assert_equal( new_naccept, old_naccept ) assert_equal( new_nreject, old_nreject ) assert_equal( opt_naccept, old_naccept ) assert_equal( opt_nreject, old_nreject ) assert_equal( par_naccept, old_naccept ) assert_equal( par_nreject, old_nreject ) assert_almost_equal( oldcnrg, check_oldcnrg, 0.1 ) assert_almost_equal( oldljnrg, check_oldljnrg, 0.1 ) assert_almost_equal( newcnrg, check_newcnrg, 0.1 ) assert_almost_equal( newljnrg, check_newljnrg, 0.1 ) assert_almost_equal( optcnrg, check_optcnrg, 0.1 ) assert_almost_equal( optljnrg, check_optljnrg, 0.1 ) assert_almost_equal( parcnrg, check_parcnrg, 0.1 ) assert_almost_equal( parljnrg, check_parljnrg, 0.1 ) assert_almost_equal( newcnrg, oldcnrg, 0.5 ) assert_almost_equal( newljnrg, oldljnrg, 0.5 ) assert_almost_equal( optcnrg, newcnrg, 0.1 ) assert_almost_equal( optljnrg, newljnrg, 0.1 ) assert_almost_equal( parcnrg, newcnrg, 0.1 ) assert_almost_equal( parljnrg, newljnrg, 0.1 )
ms = t.elapsed() print(("Moves complete! Took %d ms" % ms)) print(("GRID: ",grid_system.energies())) exp_system.update( grid_system.molecules() ) print(("EXPT: ",exp_system.energies())) print(("\nGrid energy equals: %s. Explicit energy equals: %s." % \ (grid_system.energy(), exp_system.energy()))) diff = grid_system.energy() - exp_system.energy() print(("The difference is %s\n" % diff)) PDB().write(grid_system.molecules(), "test%0004d.pdb" % (i+10)) print("\nTriggering recalculation from scratch...") grid_system.mustNowRecalculateFromScratch() exp_system.mustNowRecalculateFromScratch() print((grid_system.energies())) print((exp_system.energies())) print(("\nGrid energy equals: %s. Explicit energy equals: %s." % \ (grid_system.energy(), exp_system.energy()))) diff = grid_system.energy() - exp_system.energy() print(("The difference is %s\n" % diff)) for i in range(1,11): print("Moving the system...") t.start() grid_system = moves.move(grid_system, 1000, False) ms = t.elapsed()