示例#1
0
 def setUp(self):
     np.random.seed(0)
     self.npar = 1
     self.system = SamplerSystem()
     
     
     self.nreplicas = 500
     replicas = [Replica(x, energy) for energy, x in
                 [self.system.sample_configuration(10.) for i in range(self.nreplicas)]]
     
     mc_walker = self.system 
     ns = NestedSampling(replicas, 
                         mc_walker=mc_walker, nproc=1, verbose=False, 
                         iprint=100)
     run_nested_sampling(ns, label="test")
     
     self.energies = np.array(ns.max_energies)
     
     self.T = np.linspace(0.1, 1.0, 10)
     # compute the heat capacity and internal energy from the results of the NS run 
     self.cvNS = compute_heat_capacity(self.energies, self.nreplicas, npar=self.npar, 
                                    ndof=self.system.ndof, 
                                    Tmin=min(self.T), Tmax=max(self.T), nT=len(self.T), 
                                    live_replicas=False)
     
     # compute the same directly from the HSA
     self.cvHSA = minima_to_cv(self.system.database.minima(), kT=self.T, k=self.system.ndof)
def main():
    parser = argparse.ArgumentParser(description="do nested sampling on a p[article in a n-dimensional Harmonic well")
    parser.add_argument("-K", "--nreplicas", type=int, help="number of replicas", default=300)
    parser.add_argument("-A", "--ndof", type=int, help="number of degrees of freedom", default=4)
    parser.add_argument("-P", "--nproc", type=int, help="number of processors", default=1)
    parser.add_argument("-N", "--nsteps", type=int, help="number of MC steps per NS iteration", default=100)
    parser.add_argument("--stepsize", type=float, help="stepsize, adapted between NS iterations", default=0.1)
    parser.add_argument("--etol", type=float, help="energy tolerance: the calculation terminates when the energy difference \
                                                    between Emax and Emin is less than etol", default=0.01)
    parser.add_argument("-q", action="store_true", help="turn off verbose printing of information at every step")
    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)
    
    #set basic parameters
    args = parser.parse_args()
    ndof = args.ndof
    nproc = args.nproc
    nsteps = args.nsteps
    nreplicas = args.nreplicas
    stepsize = args.stepsize
    etol = args.etol
    
    #try to read dispatecher URI from default file location
    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
    
    #construct potential (cost function)
    potential = Harmonic(ndof)
    
    #construct Monte Carlo walker
    mc_runner = MonteCarloWalker(potential, mciter=nsteps)

    #initialise replicas (initial uniformly samples set of configurations)
    replicas = []
    for _ in xrange(nreplicas):
        x = potential.get_random_configuration()
        replicas.append(Replica(x, potential.get_energy(x)))
    
    #construct Nested Sampling object and pass dispatcher address
    ns = NestedSampling(replicas, mc_runner, stepsize=stepsize, nproc=nproc, dispatcher_URI=dispatcherURI,
                        max_stepsize=10, verbose=not args.q)
    
    #run Nested Sampling (NS), output:
    ## label.energies (one for each iteration) 
    ## label.replicas_final (live replica energies when NS terminates)
    run_nested_sampling(ns, label="run_hparticle", etol=etol)
示例#3
0
 def run(self,
         label="ns_out",
         etol=1e-5,
         maxiter=None,
         iprint_replicas=1000):
     return run_nested_sampling(self.ns,
                                label=label,
                                etol=etol,
                                maxiter=maxiter,
                                iprint_replicas=iprint_replicas)
示例#4
0
    def setUp1(self, nproc=1):
        self.ndim = 3
        self.harmonic = Harmonic(self.ndim)
        self.nreplicas = 10
        self.stepsize = 0.1
        self.nproc = nproc
        
        self.mc_runner = MonteCarloWalker(self.harmonic, mciter=40)

        replicas = []
        for i in range(self.nreplicas):
            x = self.harmonic.get_random_configuration()
            replicas.append(Replica(x, self.harmonic.get_energy(x)))
        self.ns = NestedSampling(replicas, self.mc_runner, 
                                 stepsize=0.1, nproc=nproc, verbose=False)


        self.etol = 0.01
        run_nested_sampling(self.ns, label="test", etol=self.etol)
        self.Emax = self.ns.replicas[-1].energy
        self.Emin = self.ns.replicas[0].energy
    def setUp1(self, nproc=1):
        self.ndim = 3
        self.harmonic = Harmonic(self.ndim)
        self.nreplicas = 10
        self.stepsize = 0.1
        self.nproc = nproc
        
        self.mc_runner = MonteCarloWalker(self.harmonic, mciter=40)

        replicas = []
        for i in xrange(self.nreplicas):
            x = self.harmonic.get_random_configuration()
            replicas.append(Replica(x, self.harmonic.get_energy(x)))
        self.ns = NestedSampling(replicas, self.mc_runner, 
                                 stepsize=0.1, nproc=nproc, verbose=False)


        self.etol = 0.01
        run_nested_sampling(self.ns, label="test", etol=self.etol)
        self.Emax = self.ns.replicas[-1].energy
        self.Emin = self.ns.replicas[0].energy
示例#6
0
def main():
    parser = argparse.ArgumentParser(description="do nested sampling on a p[article in a n-dimensional Harmonic well")
    parser.add_argument("-K", "--nreplicas", type=float, help="number of replicas", default=1e1)
    parser.add_argument("-A", "--ndof", type=int, help="number of degrees of freedom", default=3)
    parser.add_argument("-P", "--nproc", type=int, help="number of processors", default=1)
    parser.add_argument("-N", "--nsteps", type=int, help="number of MC steps per NS iteration", default=int(1e3))
    parser.add_argument("--stepsize", type=float, help="stepsize, adapted between NS iterations", default=20)
    parser.add_argument("--etol", type=float, help="energy tolerance: the calculation terminates when the energy difference \
                                                    between Emax and Emin is less than etol", default=0.1)
    parser.add_argument("-q", action="store_true", help="turn off verbose printing of information at every step")
    args = parser.parse_args()
    ndof = args.ndof
    nproc = args.nproc
    nsteps = int(args.nsteps)-8
    nreplicas = int(args.nreplicas)
    stepsize = args.stepsize
    etol = args.etol
    
    #construct potential (cost function)
    potential = Harmonic(ndof)
    
    #construct Monte Carlo walker
    mc_runner = MonteCarloWalker(potential, mciter=nsteps)

    #initialise replicas (initial uniformly samples set of configurations)
    replicas = []
    for _ in range(nreplicas):
        x = potential.get_random_configuration()
        print(x)
        print(potential.get_energy(x))
        replicas.append(Replica(x, potential.get_energy(x)))
    
    #construct Nested Sampling object
    ns = NestedSampling(replicas, mc_runner, stepsize=stepsize, nproc=nproc, max_stepsize=10, verbose=not args.q)
    
    #run Nested Sampling (NS), output:
    ## label.energies (one for each iteration) 
    ## label.replicas_final (live replica energies when NS terminates)
    run_nested_sampling(ns, label="run_hparticle", etol=etol)
示例#7
0
def main():
    parser = argparse.ArgumentParser(description="do nested sampling on a p[article in a n-dimensional Harmonic well")
    parser.add_argument("-K", "--nreplicas", type=int, help="number of replicas", default=300)
    parser.add_argument("-A", "--ndof", type=int, help="number of degrees of freedom", default=4)
    parser.add_argument("-P", "--nproc", type=int, help="number of processors", default=1)
    parser.add_argument("-N", "--nsteps", type=int, help="number of MC steps per NS iteration", default=100)
    parser.add_argument("--stepsize", type=float, help="stepsize, adapted between NS iterations", default=0.1)
    parser.add_argument("--etol", type=float, help="energy tolerance: the calculation terminates when the energy difference \
                                                    between Emax and Emin is less than etol", default=0.01)
    parser.add_argument("-q", action="store_true", help="turn off verbose printing of information at every step")
    
    args = parser.parse_args()
    ndof = args.ndof
    nproc = args.nproc
    nsteps = args.nsteps
    nreplicas = args.nreplicas
    stepsize = args.stepsize
    etol = args.etol
    
    #construct potential (cost function)
    potential = Harmonic(ndof)
    
    #construct Monte Carlo walker
    mc_runner = MonteCarloWalker(potential, mciter=nsteps)

    #initialise replicas (initial uniformly samples set of configurations)
    replicas = []
    for _ in xrange(nreplicas):
        x = potential.get_random_configuration()
        replicas.append(Replica(x, potential.get_energy(x)))
    
    #construct Nested Sampling object
    ns = NestedSampling(replicas, mc_runner, stepsize=stepsize, nproc=nproc, max_stepsize=10, verbose=not args.q)
    
    #run Nested Sampling (NS), output:
    ## label.energies (one for each iteration) 
    ## label.replicas_final (live replica energies when NS terminates)
    run_nested_sampling(ns, label="run_hparticle", etol=etol)
def main():
    parser = argparse.ArgumentParser(
        description=
        "do nested sampling on a p[article in a n-dimensional Harmonic well")
    parser.add_argument("-K",
                        "--nreplicas",
                        type=int,
                        help="number of replicas",
                        default=300)
    parser.add_argument("-A",
                        "--ndof",
                        type=int,
                        help="number of degrees of freedom",
                        default=4)
    parser.add_argument("-P",
                        "--nproc",
                        type=int,
                        help="number of processors",
                        default=1)
    parser.add_argument("-N",
                        "--nsteps",
                        type=int,
                        help="number of MC steps per NS iteration",
                        default=100)
    parser.add_argument("--stepsize",
                        type=float,
                        help="stepsize, adapted between NS iterations",
                        default=0.1)
    parser.add_argument(
        "--etol",
        type=float,
        help=
        "energy tolerance: the calculation terminates when the energy difference \
                                                    between Emax and Emin is less than etol",
        default=0.01)
    parser.add_argument(
        "-q",
        action="store_true",
        help="turn off verbose printing of information at every step")
    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)

    #set basic parameters
    args = parser.parse_args()
    ndof = args.ndof
    nproc = args.nproc
    nsteps = args.nsteps
    nreplicas = args.nreplicas
    stepsize = args.stepsize
    etol = args.etol

    #try to read dispatecher URI from default file location
    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

    #construct potential (cost function)
    potential = Harmonic(ndof)

    #construct Monte Carlo walker
    mc_runner = MonteCarloWalker(potential, mciter=nsteps)

    #initialise replicas (initial uniformly samples set of configurations)
    replicas = []
    for _ in xrange(nreplicas):
        x = potential.get_random_configuration()
        replicas.append(Replica(x, potential.get_energy(x)))

    #construct Nested Sampling object and pass dispatcher address
    ns = NestedSampling(replicas,
                        mc_runner,
                        stepsize=stepsize,
                        nproc=nproc,
                        dispatcher_URI=dispatcherURI,
                        max_stepsize=10,
                        verbose=not args.q)

    #run Nested Sampling (NS), output:
    ## label.energies (one for each iteration)
    ## label.replicas_final (live replica energies when NS terminates)
    run_nested_sampling(ns, label="run_hparticle", etol=etol)
示例#9
0
文件: run_lj.py 项目: smcantab/sens
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, )