예제 #1
0
 def test_seed_different(self):
     """
     Test different seed -> different result in MISER
     """
     func = lambda x: x**2
     npoints = 50000
     res, error = mcmiser(func, npoints, [0.], [1.], seed=[1234,5678])
     res2, error2 = mcmiser(func, npoints, [0.], [1.], seed=[1235,5678])
     assert res != res2, "{0} == {1}".format(res, res2)
     assert error != error2
예제 #2
0
 def test_integer_seed(self):
     """
     Check that MISER works with an integer seed.
     """
     func = lambda x: x**2
     npoints = 50000
     res, error = mcmiser(func, npoints, [0.], [1.], seed=12345)
     res2, error2 = mcmiser(func, npoints, [0.], [1.], seed=12345)
     assert res == res2, "{0} != {1}".format(res, res2)
     assert error == error2
예제 #3
0
 def test_seed(self):
     """
     Test same seed -> same result in MISER
     """
     func = lambda x: x**2
     npoints = 50000
     res, error = mcmiser(func, npoints, [0.], [1.], seed=[1234,5678])
     res2, error2 = mcmiser(func, npoints, [0.], [1.], seed=[1234,5678])
     assert res == res2, "{0} != {1}".format(res, res2)
     assert error == error2
예제 #4
0
    def forward_model(self, src, dist, R, h, sigma, src_type):
        """FWD model functions
        Evaluates potential at point (dist) by a basis source located at (src).
        dist can be a 1D point on the morphology loop or a 3D point 
        in 3D space. src is a 1D point of the morphology loop.
        Utlizies sk monaco monte carlo method if available, otherwise defaults
        to scipy integrate

        Parameters
        ----------
        x : float
        R : float
        h : float
        sigma : float
        src_type : basis_3D.key

        Returns
        -------
        pot : float
            value of potential at specified distance from the source
        """
        if isinstance(dist, float):
            if self.skmonaco_available:
                pot, err = mcmiser(self.int_pot_1D_mc,
                                   npoints=1e5,
                                   xl=[-2**1.5 * R],
                                   xu=[2**1.5 * R + self.cell.max_dist],
                                   seed=42,
                                   nprocs=num_cores,
                                   args=(src, dist, R, src_type))
            else:
                pot, err = integrate.quad(self.int_pot_1D,
                                          -2**1.5 * R,
                                          2**1.5 * R + self.cell.max_dist,
                                          args=(src, dist, R, src_type))

            return pot / (4.0 * np.pi * sigma)
        elif len(dist) == 3:
            if self.skmonaco_available:
                pot, err = mcmiser(self.int_pot_3D_mc,
                                   npoints=1e5,
                                   xl=[-2**1.5 * R],
                                   xu=[2**1.5 * R + self.cell.max_dist],
                                   seed=42,
                                   nprocs=num_cores,
                                   args=(src, dist[0], dist[1], dist[2], R,
                                         src_type))
            else:
                pot, err = integrate.quad(self.int_pot_3D,
                                          -2**1.5 * R,
                                          2**1.5 * R + self.cell.max_dist,
                                          args=(src, dist[0], dist[1], dist[2],
                                                R, src_type))

            return pot / (4.0 * np.pi * sigma)
예제 #5
0
 def test_args2(self):
     """
     Check that MISER works with arguments (2)
     """
     func_noargs = lambda x: x**2
     func_args = lambda x, a: a*x**2
     npoints = 50000
     res_noargs, error_noargs = mcmiser(func_noargs, npoints, [0.],[1.], seed=12345)
     res_args, error_args = mcmiser(func_args, npoints, [0.],[1.], seed=12345, args=(2,))
     self.assertEqual(2*res_noargs, res_args)
     self.assertEqual(2*error_noargs, error_args)
예제 #6
0
def b_pot_3d_mc(x, R, h, sigma, basis_func=bf.gauss_rescale_3D):
    """
    Calculate potential in the 3D case using Monte Carlo integration.
    It utilizes the MISER algorithm
    """
    pot, err = mcmiser(int_pot_3D_mc, npoints=1e5,
                       xl=[-R, -R, -R], xu=[R, R, R],
                       nprocs=4, args=(x, R, h, basis_func))
    pot *= 1./(4.0*pi*sigma)
    return pot
예제 #7
0
 def _get_results_errors(self):
     results, errors = [], []
     for itrial in range(self.ntrials):
         res, err = mcmiser(self.f,self.npoints,self.lbound,self.ubound,
                 seed=itrial+self.seed)
         results.append(res)
         errors.append(err)
     results = np.array(results)
     errors = np.array(errors)
     return results, errors
예제 #8
0
def b_pot_3d_mc(x, R, h, sigma, basis_func=bf.gauss_rescale_3D):
    """
    Calculate potential in the 3D case using Monte Carlo integration.
    It utilizes the MISER algorithm
    """
    pot, err = mcmiser(int_pot_3D_mc,
                       npoints=1e5,
                       xl=[-R, -R, -R],
                       xu=[R, R, R],
                       nprocs=4,
                       args=(x, R, h, basis_func))
    pot *= 1. / (4.0 * pi * sigma)
    return pot
예제 #9
0
    def forward_model(self, x, R, h, sigma, src_type):
        """FWD model functions
        Evaluates potential at point (x,0) by a basis source located at (0,0)
        Utlizies sk monaco monte carlo method if available, otherwise defaults
        to scipy integrate

        Parameters
        ----------
        x : float
        R : float
        h : float
        sigma : float
        src_type : basis_3D.key

        Returns
        -------
        pot : float
            value of potential at specified distance from the source
        """
        if src_type.__name__ == "gauss_3D":
            if x == 0: x = 0.0001
            pot = special.erf(x / (np.sqrt(2) * R / 3.0)) / x
        elif src_type.__name__ == "gauss_lim_3D":
            if x == 0: x = 0.0001
            d = R / 3.
            if x < R:
                e = np.exp(-(x / (np.sqrt(2) * d))**2)
                erf = special.erf(x / (np.sqrt(2) * d))
                pot = 4 * np.pi * ((d**2) * (e - np.exp(-4.5)) + (1 / x) *
                                   ((np.sqrt(np.pi / 2) * (d**3) * erf) - x *
                                    (d**2) * e))
            else:
                pot = 15.28828 * (d)**3 / x
            pot /= (np.sqrt(2 * np.pi) * d)**3
        elif src_type.__name__ == "step_3D":
            Q = 4. * np.pi * (R**3) / 3.
            if x < R:
                pot = (Q * (3 - (x / R)**2)) / (2. * R)
            else:
                pot = Q / x
            pot *= 3 / (4 * np.pi * R**3)
        else:
            if skmonaco_available:
                pot, err = mcmiser(self.int_pot_3D_mc,
                                   npoints=1e5,
                                   xl=[-R, -R, -R],
                                   xu=[R, R, R],
                                   seed=42,
                                   nprocs=num_cores,
                                   args=(x, R, h, src_type))
            else:
                pot, err = integrate.tplquad(self.int_pot_3D,
                                             -R,
                                             R,
                                             lambda x: -R,
                                             lambda x: R,
                                             lambda x, y: -R,
                                             lambda x, y: R,
                                             args=(x, R, h, src_type))
        pot *= 1. / (4.0 * np.pi * sigma)
        return pot
예제 #10
0
 def check_trial_run(self,*args,**kwargs):
     res, err = mcmiser(self.f, self.npoints, self.lbound, self.ubound, *args, **kwargs)
     error_in_mean = abs(res-self.analytical_value)
     assert error_in_mean < 3*self.mean_sigma,\
             "Error in mean  = {}\n\
              Expected error = {}".format(error_in_mean,self.mean_sigma)
예제 #11
0
 def test_wrong_nprocs(self):
     """
     Raise a ValueError if nprocs < 1
     """
     with self.assertRaises(ValueError):
         mcmiser(lambda x: x**2,2000,xl=[0.],xu=[1.],nprocs=-1)
예제 #12
0
 def test_wrong_npoints(self):
     """
     Raise a ValueError if npoints < 2 in MISER.
     """
     with self.assertRaises(ValueError):
         mcmiser(lambda x: x**2,0,xl=[0.],xu=[1.])
예제 #13
0
 def test_wrong_xl(self):
     """
     Raise a ValueError if len(xl) != len(xu) in MISER.
     """
     with self.assertRaises(ValueError):
         mcmiser(lambda x: x**2,2000,xl=[0.,0.],xu=[1.])
예제 #14
0
 def test_zero_volume(self):
     """
     Passing empty integration volume to MISER raises ValueError.
     """
     with self.assertRaises(ValueError):
         mcmiser(lambda x:x**2, 20000, [0.],[0.])