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
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 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 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 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, )
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)