Ejemplo n.º 1
0
class TestNS(unittest.TestCase):
    def setUp(self):
        self.setUp1()

    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

    def test1(self):
        self.assert_(len(self.ns.replicas) == self.nreplicas)
        self.assert_(self.ns.stepsize != self.stepsize)
        self.assertGreater(len(self.ns.max_energies), 0)
        self.assertLess(self.Emin, self.Emax)
        self.assertLess(self.Emax - self.Emax, self.etol)
        self.assertLess(self.Emax, 2. * self.etol)
Ejemplo n.º 2
0
class TestNS(unittest.TestCase):
    def setUp(self):
        self.setUp1()

    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

    
    def test1(self):
        self.assert_(len(self.ns.replicas) == self.nreplicas)
        self.assert_(self.ns.stepsize != self.stepsize)
        self.assertGreater(len(self.ns.max_energies), 0)
        self.assertLess(self.Emin, self.Emax)
        self.assertLess(self.Emax - self.Emax, self.etol)
        self.assertLess(self.Emax, 2. * self.etol)
Ejemplo n.º 3
0
 def setUp1(self, nproc=1, multiproc=True):
     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)
     
     if multiproc == False:
         hostname=socket.gethostname()
         host = Pyro4.socketutil.getIpAddress(hostname, workaround127=True)
         self.dispatcher_URI = "PYRO:"+"test@"+host+":9090"
     else:
         self.dispatcher_URI = None
     
     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, dispatcher_URI=self.dispatcher_URI)
     
     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
Ejemplo n.º 4
0
    def prepare(self):
        self.ndim = 6
        self.harmonic = Harmonic(self.ndim)
        self.x0 = np.zeros(self.ndim)
        self.energy = self.harmonic.get_energy(self.x0)

        self.mciter = 100
        self.mcwalker = MonteCarloWalker(self.harmonic, mciter=self.mciter)
        self.stepsize = 0.1
        self.Emax = 10.
        self.seed = None
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)
Ejemplo n.º 6
0
class TestNS(unittest.TestCase):
    """to test distributed computing must start a dispatcher with --server-name test and --port 9090
    """
    def setUp(self):
        self.setUp1()

    def setUp1(self, nproc=1, multiproc=True):
        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)

        if multiproc == False:
            hostname = socket.gethostname()
            host = Pyro4.socketutil.getIpAddress(hostname, workaround127=True)
            self.dispatcher_URI = "PYRO:" + "test@" + host + ":9090"
        else:
            self.dispatcher_URI = None

        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,
                                 dispatcher_URI=self.dispatcher_URI)

        self.Emax0 = self.ns.replicas[-1].energy

        self.niter = 100
        for i in range(self.niter):
            self.ns.one_iteration()
        self.Emax = self.ns.replicas[-1].energy
        self.Emin = self.ns.replicas[0].energy

    def test1(self):
        print("running TestNS")
        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.ns.stepsize != self.stepsize)
        self.assertEqual(len(self.ns.max_energies), self.niter * self.nproc)
Ejemplo n.º 7
0
class TestNS(unittest.TestCase):
    """to test distributed computing must start a dispatcher with --server-name test and --port 9090
    """
    def setUp(self):
        self.setUp1()

    def setUp1(self, nproc=1, multiproc=True):
        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)
        
        if multiproc == False:
            hostname=socket.gethostname()
            host = Pyro4.socketutil.getIpAddress(hostname, workaround127=True)
            self.dispatcher_URI = "PYRO:"+"test@"+host+":9090"
        else:
            self.dispatcher_URI = None
        
        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, dispatcher_URI=self.dispatcher_URI)
        
        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):
        print "running TestNS"
        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.ns.stepsize != self.stepsize)
        self.assertEqual(len(self.ns.max_energies), self.niter * self.nproc)
Ejemplo n.º 8
0
    def prepare(self):
        self.ndim = 6
        self.harmonic = Harmonic(self.ndim)
        self.x0 = np.zeros(self.ndim)
        self.energy = self.harmonic.get_energy(self.x0)

        self.mciter = 100
        self.mcwalker = MonteCarloWalker(self.harmonic, mciter=self.mciter)
        self.stepsize = 0.1
        self.Emax = 10.0
        self.seed = None
Ejemplo n.º 9
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
Ejemplo n.º 10
0
class TestHarmonic(unittest.TestCase):
    def setUp(self):
        self.ndim = 3
        self.harmonic = Harmonic(3)
    
    def test_energy(self):
        x = np.zeros(self.ndim)
        e = self.harmonic.get_energy(x)
        self.assertAlmostEqual(e, 0., 7)
    
    def test_e2(self):
        x = np.ones(self.ndim)
        x *= 1. / np.linalg.norm(x)
        e = self.harmonic.get_energy(x)
        self.assertAlmostEqual(e, 0.5, 7)
    
    def test_random_config(self):
        r = 1.
        x = self.harmonic.get_random_configuration(r)
        xnorm = np.linalg.norm(x)
        self.assertLessEqual(xnorm, r)
Ejemplo n.º 11
0
def main():
    parser = argparse.ArgumentParser(
        description="must pass the URI of the dispatcher")
    parser.add_argument("ndim", type=int, help="number of dimensions")
    parser.add_argument("dispatcher_URI", type=str, help="name for the worker")
    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")
    args = parser.parse_args()

    #===========================================================================
    # MUST SET THE SYSTEM THEN GET THE RUNNER TO PASS
    #===========================================================================
    system = Harmonic(args.ndim)
    mc_runner = MonteCarloWalker(system, mciter=args.mciter)

    dispatcher_URI = args.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()
Ejemplo n.º 12
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)
Ejemplo n.º 13
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)
Ejemplo n.º 14
0
class TestMCWalker(unittest.TestCase):
    def setUp(self):
        self.prepare()
        self.runwalker()

    def prepare(self):
        self.ndim = 6
        self.harmonic = Harmonic(self.ndim)
        self.x0 = np.zeros(self.ndim)
        self.energy = self.harmonic.get_energy(self.x0)

        self.mciter = 100
        self.mcwalker = MonteCarloWalker(self.harmonic, mciter=self.mciter)
        self.stepsize = 0.1
        self.Emax = 10.
        self.seed = None

    def runwalker(self):
        self.res = self.mcwalker(self.x0,
                                 self.stepsize,
                                 self.Emax,
                                 self.energy,
                                 seed=self.seed)

    def test1(self):
        self.assert_(np.all(self.x0 != self.res.x))

    def test2(self):
        self.assert_(self.energy != self.res.energy)
        self.assert_(self.res.energy < self.Emax)

    def test3(self):
        self.assert_(self.res.naccept > 0)

    def test4(self):
        self.assert_(self.res.nsteps == self.mciter)

    def test_large_stepsize(self):
        self.stepsize = 1e4
        self.runwalker()
        self.assert_(self.res.naccept == 0)
        self.assert_(self.energy == self.res.energy)
        self.assert_(np.all(self.x0 == self.res.x))
Ejemplo n.º 15
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 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
Ejemplo n.º 16
0
class TestMCWalker(unittest.TestCase):
    def setUp(self):
        self.prepare()
        self.runwalker()

    def prepare(self):
        self.ndim = 6
        self.harmonic = Harmonic(self.ndim)
        self.x0 = np.zeros(self.ndim)
        self.energy = self.harmonic.get_energy(self.x0)

        self.mciter = 100
        self.mcwalker = MonteCarloWalker(self.harmonic, mciter=self.mciter)
        self.stepsize = 0.1
        self.Emax = 10.0
        self.seed = None

    def runwalker(self):
        self.res = self.mcwalker(self.x0, self.stepsize, self.Emax, self.energy, seed=self.seed)

    def test1(self):
        self.assert_(np.all(self.x0 != self.res.x))

    def test2(self):
        self.assert_(self.energy != self.res.energy)
        self.assert_(self.res.energy < self.Emax)

    def test3(self):
        self.assert_(self.res.naccept > 0)

    def test4(self):
        self.assert_(self.res.nsteps == self.mciter)

    def test_large_stepsize(self):
        self.stepsize = 1e4
        self.runwalker()
        self.assert_(self.res.naccept == 0)
        self.assert_(self.energy == self.res.energy)
        self.assert_(np.all(self.x0 == self.res.x))
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)
Ejemplo n.º 18
0
 def setUp(self):
     self.ndim = 3
     self.harmonic = Harmonic(3)