class TestMinimaSearch(unittest.TestCase): def setUp(self): self.natoms = 6 self.system = LJClusterSENS(self.natoms, 2.5) # create a database self.database = self.system.create_database() # add some minima to the database bh = self.system.get_basinhopping(self.database, outstream=None) while self.database.number_of_minima() < 2: bh.run(1) self.ndof = self.natoms * 3 - 6 self.minima_searcher = _MinimaSearcher(self.database.minima(), energy_accuracy=1e-4, compare_structures=self.system.get_compare_exact()) def test_exact(self): for m in self.database.minima(): mret, tform = self.minima_searcher.get_minima(m.energy, m.coords) self.assertEqual(m, mret) def test_random(self): for m in self.database.minima(): mret, tform = self.minima_searcher.get_minima(m.energy + np.random.uniform(-1e-4, 1e-4), m.coords) self.assertEqual(m, mret) def test_none(self): for m in self.database.minima(): mret, tform = self.minima_searcher.get_minima(m.energy + 10., m.coords) self.assertIsNone(mret)
class TestSENSExact_LJ(_test_ns_lj.TestNS_LJ): def setUp(self): # self.seed = 4 # np.random.seed(self.seed) self.setUp1() def set_up_system(self): self.natoms = 6 self.gmin = -12.7121 self.system = LJClusterSENS(self.natoms, 2.5) self.ndof = 3 * self.natoms - 6 def setUp1(self, nproc=1): self.set_up_system() self.nreplicas = 10 self.stepsize = 0.01 self.nproc = nproc self.database = self.system.create_database() # add some minima to the database bh = self.system.get_basinhopping(self.database, outstream=None) while self.database.number_of_minima() < 2: bh.run(1) # compute the thermodynamic information get_thermodynamic_information(self.system, self.database) get_all_normalmodes(self.system, self.database) self.minima = list(self.database.minima()) assert self.database.number_of_minima( ) > 1, "%d minima" % self.database.number_of_minima() self.mc_runner = self.system.get_mc_walker(mciter=200) self.energy_accuracy = 1e-4 self.ns = NestedSamplingSAExact( self.system, self.nreplicas, self.mc_runner, self.minima, self.energy_accuracy, mindist=self.system.get_mindist(), config_tests=self.system.get_config_tests(), stepsize=0.1, nproc=nproc, verbose=True, iprint=100) self.Emax0 = self.ns.replicas[-1].energy self.run_ns(max_iter=1000, Etol=.001) def test1(self): super(TestSENSExact_LJ, self).test1() self.assertGreater(self.ns.count_sampled_minima, 0)
class TestSENSExact_LJ(_test_ns_lj.TestNS_LJ): def setUp(self): # self.seed = 4 # np.random.seed(self.seed) self.setUp1() def set_up_system(self): self.natoms = 6 self.gmin = -12.7121 self.system = LJClusterSENS(self.natoms, 2.5) self.ndof = 3*self.natoms - 6 def setUp1(self, nproc=1): self.set_up_system() self.nreplicas = 10 self.stepsize = 0.01 self.nproc = nproc self.database = self.system.create_database() # add some minima to the database bh = self.system.get_basinhopping(self.database, outstream=None) while self.database.number_of_minima() < 2: bh.run(1) # compute the thermodynamic information get_thermodynamic_information(self.system, self.database) get_all_normalmodes(self.system, self.database) self.minima = list(self.database.minima()) assert self.database.number_of_minima() > 1, "%d minima" % self.database.number_of_minima() self.mc_runner = self.system.get_mc_walker(mciter=200) self.energy_accuracy = 1e-4 self.ns = NestedSamplingSAExact(self.system, self.nreplicas, self.mc_runner, self.minima, self.energy_accuracy, mindist=self.system.get_mindist(), config_tests = self.system.get_config_tests(), stepsize=0.1, nproc=nproc, verbose=True, iprint=100) self.Emax0 = self.ns.replicas[-1].energy self.run_ns(max_iter=1000, Etol=.001) def test1(self): super(TestSENSExact_LJ, self).test1() self.assertGreater(self.ns.count_sampled_minima, 0)
def setUp1(self, nproc=1): self.natoms = 31 self.system = LJClusterSENS(self.natoms, 2.5) self.ndof = 3*self.natoms - 6 self.nreplicas = 10 self.stepsize = 0.01 self.nproc = nproc try: self.database = self.system.create_database("/scratch/scratch2/js850/sens/testruns/lj31/lj31.sqlite", createdb=False) except IOError: self.database = build_database(self.system, 4) self.minima = list(self.database.minima()) assert self.database.number_of_minima() > 1, "%d minima" % self.database.number_of_minima() self.sampler = HSASamplerCluster(self.minima, self.ndof, copy_minima=True, center_minima=True, compare_structures=self.system.get_compare_exact(), mindist=self.system.get_mindist(), minimizer=self.system.get_minimizer()) self.Emax = min((m.energy for m in self.minima)) + 1. # for performing transformations like rotations, translations, permutations, etc self.transform = TransformAtomicCluster() # this is a list of random transformations that don't change the energy self.transformations = [self.rtrans, self.rrot, self.invert, self.rperm]
class TestNS_LJ(unittest.TestCase): def setUp(self): self.setUp1() def compute_cv(self, Tmin=.01, Tmax=3., nT=100): T = np.arange(Tmin, Tmax, (Tmax - Tmin) / nT) Cv = compute_cv_c(np.array(self.ns.max_energies), float(self.nproc), float(self.ndof), T.min(), T.max(), T.size, float(self.ndof), live=False) return T, Cv def set_up_system(self): self.natoms = 13 self.gmin = -44.3269 self.system = LJClusterSENS(self.natoms, 2.5) self.ndof = 3 * self.natoms - 6 def setUp1(self, nproc=1): self.set_up_system() self.nreplicas = 10 self.stepsize = 0.01 self.nproc = nproc self.mc_runner = self.system.get_mc_walker(mciter=100) self.ns = NestedSampling(self.system, self.nreplicas, self.mc_runner, stepsize=0.1, nproc=nproc, verbose=False) self.Emax0 = self.ns.replicas[-1].energy self.niter = 100 for i in xrange(self.niter): self.ns.one_iteration() self.Emax = self.ns.replicas[-1].energy self.Emin = self.ns.replicas[0].energy def test1(self): self.assert_(len(self.ns.replicas) == self.nreplicas) self.assert_(self.Emax < self.Emax0) self.assert_(self.Emin < self.Emax) self.assert_(self.Emin < 0) self.assert_(self.Emin >= self.gmin) self.assert_(self.ns.stepsize != self.stepsize) self.assertEqual(len(self.ns.max_energies), self.niter * self.nproc) self.assertEqual(self.ns.failed_mc_walks, 0) def run_ns(self, max_iter=100, Etol=1e-4): # max_iter = 10000 # self.Etol = .01 self.Etol = Etol for i in xrange(int(max_iter)): self.ns.one_iteration() deltaE = self.ns.replicas[-1].energy - self.ns.replicas[0].energy if deltaE < self.Etol: break self.niter = i + 1 self.Emax = self.ns.replicas[-1].energy self.Emin = self.ns.replicas[0].energy
def setUp(self): self.natoms = 6 self.system = LJClusterSENS(self.natoms, 2.5) # create a database self.database = self.system.create_database() # add some minima to the database bh = self.system.get_basinhopping(self.database, outstream=None) while self.database.number_of_minima() < 2: bh.run(1) self.ndof = self.natoms * 3 - 6 self.minima_searcher = _MinimaSearcher( self.database.minima(), energy_accuracy=1e-4, compare_structures=self.system.get_compare_exact())
def main(): parser = argparse.ArgumentParser(description="must pass the URI of the dispatcher") parser.add_argument("natoms", type=int, help="number of atoms") parser.add_argument("-n","--mciter", type=int, default=1000, help="number of steps in the monte carlo walk") parser.add_argument("--radius", type=float, default=2.5, help="maintain atoms in a sphere of this radius") parser.add_argument("--worker-name", type=str, help="name for the worker",default=None) parser.add_argument("--host", type=str, help="address of the host (node on which the worker is started)",default=None) parser.add_argument("--port", type=int, help="port number on which the worker is started)",default=0) parser.add_argument("--server-type", type=str, help="multiplex or threaded",default="multiplex") parser.add_argument("--dispatcherURI-addr", type=str, help=" URI for the dispatcher", default=None) parser.add_argument("--dispatcherURI-file", type=str, help="name of the file containing the dispatcher URI,"\ "by default dispatcher_uri.dat, no need to specify this option unless this changed", default=None) args = parser.parse_args() #=========================================================================== # MUST SET THE SYSTEM THEN GET THE RUNNER TO PASS TO THE WORKER #=========================================================================== system = LJClusterSENS(args.natoms, args.radius) mc_runner = system.get_mc_walker(args.mciter) dispatcher_URI_file = args.dispatcherURI_file dispatcher_URI_addr = args.dispatcherURI_addr if dispatcher_URI_file != None: with open (dispatcher_URI_file, "r") as rfile: dispatcher_URI = rfile.read().replace('\n', '') elif dispatcher_URI_addr != None: dispatcher_URI = dispatcher_URI_addr else: with open ("dispatcher_uri.dat", "r") as rfile: dispatcher_URI = rfile.read().replace('\n', '') print dispatcher_URI worker_name = args.worker_name host = args.host port = args.port server_type = args.server_type worker = pyro_worker(dispatcher_URI, mc_runner, worker_name=worker_name, host=host, port=port, server_type=server_type) worker._start_worker()
def setUp(self): self.natoms = 6 self.system = LJClusterSENS(self.natoms, 2.5) # create a database self.database = self.system.create_database() # add some minima to the database bh = self.system.get_basinhopping(self.database, outstream=None) while self.database.number_of_minima() < 2: bh.run(1) self.ndof = self.natoms * 3 - 6 self.minima_searcher = _MinimaSearcher(self.database.minima(), energy_accuracy=1e-4, compare_structures=self.system.get_compare_exact())
class TestMinimaSearch(unittest.TestCase): def setUp(self): self.natoms = 6 self.system = LJClusterSENS(self.natoms, 2.5) # create a database self.database = self.system.create_database() # add some minima to the database bh = self.system.get_basinhopping(self.database, outstream=None) while self.database.number_of_minima() < 2: bh.run(1) self.ndof = self.natoms * 3 - 6 self.minima_searcher = _MinimaSearcher( self.database.minima(), energy_accuracy=1e-4, compare_structures=self.system.get_compare_exact()) def test_exact(self): for m in self.database.minima(): mret, tform = self.minima_searcher.get_minima(m.energy, m.coords) self.assertEqual(m, mret) def test_random(self): for m in self.database.minima(): mret, tform = self.minima_searcher.get_minima( m.energy + np.random.uniform(-1e-4, 1e-4), m.coords) self.assertEqual(m, mret) def test_none(self): for m in self.database.minima(): mret, tform = self.minima_searcher.get_minima( m.energy + 10., m.coords) self.assertIsNone(mret)
def main(): parser = argparse.ArgumentParser(description="do nested sampling on a Lennard Jones cluster") parser.add_argument("natoms", type=int, help="number of atoms") parser.add_argument("-K", "--nreplicas", type=int, help="number of replicas", default=300) parser.add_argument("-n", "--mciter", type=int, default=1000, help="number of steps in the monte carlo walk") parser.add_argument("-P", "--nproc", type=int, help="number of processors", default=1) parser.add_argument("-q", action="store_true", help="turn off verbose printing of information at every step") parser.add_argument("--radius", type=float, default=2.5, help="maintain atoms in a sphere of this radius") parser.add_argument("--iprint", type=int, default=1, help="if verbose, status messages will be printed every iprint steps") parser.add_argument("--sens-exact", action="store_true", help="use the exact version of superposition enhanced nested sampling") parser.add_argument("--db", type=str, help="location of the database", default="") parser.add_argument("--nminima", type=int, default=-1, help="number of minima from the database to use. If negative, use all minima") args = parser.parse_args() print args if args.sens_exact and args.db == "": raise Exception("for sens you must specify a database file") system = LJClusterSENS(args.natoms, args.radius) energy_accuracy = 1e-3 if args.sens_exact: database = system.create_database(args.db, createdb=False) if args.nminima <= 0 or args.nminima > database.number_of_minima(): minima = database.minima() else: minima = database.minima()[:args.nminima] mcrunner = system.get_mc_walker(args.mciter) nskwargs = dict(nproc=args.nproc, verbose=not args.q, iprint=args.iprint) print "mciter", args.mciter if args.sens_exact: ns = NestedSamplingSAExact(system, args.nreplicas, mcrunner, minima, energy_accuracy, mindist=system.get_mindist(niter=1), minimizer=system.get_minimizer(tol=1e-4), **nskwargs) else: ns = NestedSampling(system, args.nreplicas, mcrunner, **nskwargs) run_nested_sampling(ns, label="lj"+str(args.natoms), etol=1e-2)
def set_up_system(self): self.natoms = 6 self.gmin = -12.7121 self.system = LJClusterSENS(self.natoms, 2.5) self.ndof = 3*self.natoms - 6
def set_up_system(self): self.natoms = 13 self.gmin = -44.3269 self.system = LJClusterSENS(self.natoms, 2.5) self.ndof = 3 * self.natoms - 6
class TestSENSExact_LJ(unittest.TestCase): def setUp(self): self.seed = np.random.randint(1000000) # self.seed = 549670 # failed self.assertGreater(self.ns.count_sampled_minima, 0) print "seed", self.seed np.random.seed(self.seed) self.setUp1() def setUp1(self, nproc=1): self.natoms = 31 self.system = LJClusterSENS(self.natoms, 2.5) self.ndof = 3*self.natoms - 6 self.nreplicas = 10 self.stepsize = 0.01 self.nproc = nproc try: self.database = self.system.create_database("/scratch/scratch2/js850/sens/testruns/lj31/lj31.sqlite", createdb=False) except IOError: self.database = build_database(self.system, 4) self.minima = list(self.database.minima()) assert self.database.number_of_minima() > 1, "%d minima" % self.database.number_of_minima() self.sampler = HSASamplerCluster(self.minima, self.ndof, copy_minima=True, center_minima=True, compare_structures=self.system.get_compare_exact(), mindist=self.system.get_mindist(), minimizer=self.system.get_minimizer()) self.Emax = min((m.energy for m in self.minima)) + 1. # for performing transformations like rotations, translations, permutations, etc self.transform = TransformAtomicCluster() # this is a list of random transformations that don't change the energy self.transformations = [self.rtrans, self.rrot, self.invert, self.rperm] def rtrans(self, x): vec3 = np.random.uniform(-1,1,3) self.transform.translate(x, vec3) def rrot(self, x): q = rotations.random_q() mx = rotations.q2mx(q) self.transform.rotate(x, mx) def invert(self, x): self.transform.invert(x) def rperm(self, x): perm = range(self.natoms) np.random.shuffle(perm) self.transform.permute(x, perm) def do_test(self, tform): # sample a configurations fr # sample a configuration from the HSA m, xsampled = self.sampler.sample_coords(self.Emax) # get the energy of that configuration in the HSA E_HSA = self.sampler.compute_energy(xsampled, m) # get the real energy of the sampled configuration pot = self.system.get_potential() Esampled = pot.getEnergy(xsampled) r = Replica(xsampled, Esampled) # transform xsampled in a way that preserves the real energy but not necessarily the HSA energy tform(xsampled) # assert the real energy has not changed Etrans = pot.getEnergy(xsampled) self.assertAlmostEqual(r.energy, Etrans, delta=1e-4) # use the nested sampling routine to compute the HSA energy of xsampled. # This should undo the transformations we applied and find the correct HSA energy m_quench, E_HSA_computed = self.sampler.compute_energy_in_HSA(r.energy, r.x) # assert that self.ns._compute_energy_in_SA(r) was able to recover the correct HSA energy self.assertIsNotNone(E_HSA_computed) self.assertAlmostEqual(E_HSA, E_HSA_computed, delta=1e-4) self.assertAlmostEqual(m.energy, m_quench.energy, delta=1e-4) def fchain(self, flist): """return a function which applies all of the functions in flist to the input""" def function_chain(x): for f in reversed(flist): f(x) return function_chain def test1(self): """run do_test() on all combinations transformations""" for f in self.transformations: self.do_test(f) def test_2(self): """run do_test() on all combinations of length 2 of the transformations """ for flist in itertools.product(self.transformations, repeat=2): tform = self.fchain(flist) self.do_test(tform) def test_10(self): """run do_test() on all combinations of length 10 of the transformations """ maxiter = 500 i = 0 for flist in itertools.product(self.transformations, repeat=10): tform = self.fchain(flist) self.do_test(tform) i += 1 if i >= maxiter: break
def set_up_system(self): self.natoms = 6 self.gmin = -12.7121 self.system = LJClusterSENS(self.natoms, 2.5) self.ndof = 3 * self.natoms - 6
class TestSENSExact_LJ(_test_ns_lj.TestNS_LJ): def setUp(self): self.seed = np.random.randint(1000000) # self.seed = 549670 # failed self.assertGreater(self.ns.count_sampled_minima, 0) print "seed", self.seed np.random.seed(self.seed) self.setUp1() def set_up_system(self): self.natoms = 6 self.gmin = -12.7121 self.system = LJClusterSENS(self.natoms, 2.5) self.ndof = 3*self.natoms - 6 def setUp1(self, nproc=1): self.set_up_system() self.nreplicas = 10# * nproc self.stepsize = 0.01 self.nproc = nproc self.database = self.system.create_database() # add some minima to the database bh = self.system.get_basinhopping(self.database, outstream=None) while self.database.number_of_minima() < 2: bh.run(1) # compute the thermodynamic information get_thermodynamic_information(self.system, self.database) get_all_normalmodes(self.system, self.database) self.minima = list(self.database.minima()) assert self.database.number_of_minima() > 1, "%d minima" % self.database.number_of_minima() self.mc_runner = self.system.get_mc_walker(mciter=200) self.energy_accuracy = 1e-4 self.hsa_sampler = HSASamplerCluster(self.minima, self.system.k, copy_minima=True, center_minima=True, energy_accuracy=self.energy_accuracy, compare_structures=self.system.get_compare_exact(), mindist=self.system.get_mindist(), minimizer=self.system.get_minimizer(), debug=True) replicas = _utils.create_replicas(self.system, self.nreplicas) potential = self.system.get_potential() potential.get_energy = potential.getEnergy self.ns = NestedSamplingSAExact(replicas, self.mc_runner, self.hsa_sampler, potential, config_tests=self.system.get_config_tests(), nproc=nproc, verbose=True, iprint=1, debug=True) self.Emax0 = self.ns.replicas[-1].energy self.run_ns(max_iter=1000, Etol=.001) def test1(self): super(TestSENSExact_LJ, self).test1() self.assertGreater(self.ns.number_swaps_accepted(), 0)
def main(): parser = argparse.ArgumentParser(description="do nested sampling on a Lennard Jones cluster") parser.add_argument("natoms", type=int, help="number of atoms") parser.add_argument("-K", "--nreplicas", type=int, help="number of replicas", default=300) parser.add_argument("-n", "--mciter", type=int, default=1000, help="number of steps in the monte carlo walk") parser.add_argument("-P", "--nproc", type=int, help="number of processors", default=1) parser.add_argument("-q", action="store_true", help="turn off verbose printing of information at every step") parser.add_argument("--radius", type=float, default=2.5, help="maintain atoms in a sphere of this radius") parser.add_argument("--iprint", type=int, default=1, help="if verbose, status messages will be printed every iprint steps") parser.add_argument("--sens-exact", action="store_true", help="use the exact version of superposition enhanced nested sampling") parser.add_argument("--sens-approximate", action="store_true", help="use the approximate version of superposition enhanced nested sampling") parser.add_argument("--minprob", type=float, default=1., help="only for sens-approximate: probability of sampling from the database (by default <minprob>/K)," "default value 1") parser.add_argument("--energy-offset", type=float, default=2.5, help="only for sens-approximate: value of Emax where the probabilty" "starts to become non zero is <energy_max_database> + <energy_offset>") parser.add_argument("--db", type=str, help="location of the database", default="") parser.add_argument("--nminima", type=int, default=-1, help="number of minima from the database to use. If negative, use all minima") parser.add_argument("--energy-onset", type=float, help="energy at which start sampling from database, by default maximum energy in database",default=None) parser.add_argument("--stop-crit", type=float, default=1e-3, help="run will terminate when stop_crit is larger than the difference between the maximum and minimum replica energies") parser.add_argument("--cpfile", type=str, help="checkpoint file name, unless renamed it takes default output name: <label>.replicas.p", default = None) parser.add_argument("--cpfreq", type=int, help="checkpointing frequency in number of steps", default = 10000) parser.add_argument("--cpstart", action="store_true", help="start calculation from checkpoint binary file") parser.add_argument("--dispatcherURI", action="store_true", help="use URI of the dispatcher server in default location",default=False) parser.add_argument("--dispatcherURI-file", type=str, help="use URI of the dispatcher server if different from default",default=None) args = parser.parse_args() print args if (args.sens_exact or args.sens_approximate) and args.db == "": raise Exception("for sens you must specify a database file") system = LJClusterSENS(args.natoms, args.radius) energy_accuracy = 1e-3 if args.sens_exact or args.sens_approximate: database = system.create_database(args.db, createdb=False) if args.nminima <= 0 or args.nminima > database.number_of_minima(): minima = database.minima() else: minima = database.minima()[:args.nminima] print "using", len(minima), "minima" if args.dispatcherURI is True: with open ("dispatcher_uri.dat", "r") as rfile: dispatcherURI = rfile.read().replace('\n', '') elif args.dispatcherURI_file != None: with open (args.dispatcherURI_file, "r") as rfile: dispatcherURI = rfile.read().replace('\n', '') else: dispatcherURI = None mcrunner = system.get_mc_walker(args.mciter) nskwargs = dict(nproc=args.nproc, verbose=not args.q, iprint=args.iprint, dispatcher_URI = dispatcherURI, cpfreq = args.cpfreq, cpfile = args.cpfile, cpstart = args.cpstart) # create the potential object potential = system.get_potential() potential.get_energy = potential.getEnergy # create the replicas replicas = [] for i in xrange(args.nreplicas): x = system.get_random_configuration() e = potential.getEnergy(x) replicas.append(Replica(x, e)) print "mciter", args.mciter print "radius", args.radius if args.sens_exact: hsa_sampler = HSASamplerCluster(minima, system.k, copy_minima=True, center_minima=True, energy_accuracy=energy_accuracy, compare_structures=system.get_compare_exact(), mindist=system.get_mindist(), minimizer=system.get_minimizer(), debug=True) ns = NestedSamplingSAExact(replicas, mcrunner, hsa_sampler, potential, config_tests=system.get_config_tests(), **nskwargs) elif args.sens_approximate: ns = NestedSamplingSA(replicas, mcrunner, minima, system.k, config_tests=system.get_config_tests(), minprob=args.minprob, energy_offset=args.energy_offset, energy_onset = args.energy_onset, copy_minima=True, center_minima=True, **nskwargs) else: ns = NestedSampling(replicas, mcrunner, **nskwargs) run_nested_sampling(ns, label="lj"+str(args.natoms), etol=args.stop_crit, )