def run_parallel(self,f,npoints,distribution,expected_value,expected_variance,**kwargs): res,sd = mcimport(f,npoints,distribution,nprocs=2,**kwargs) error = np.sqrt(expected_variance/float(npoints)) assert_within_tol(res,expected_value,3.*max(error,1e-10), "Error in <f> in parallel run.") assert_within_tol(sd,error,0.1*max(error,1e-10), "Error in expected error in parallel run.")
def run_parallel(self, f, npoints, distribution, expected_value, expected_variance, **kwargs): res, sd = mcimport(f, npoints, distribution, nprocs=2, **kwargs) error = np.sqrt(expected_variance / float(npoints)) assert_within_tol(res, expected_value, 3. * max(error, 1e-10), "Error in <f> in parallel run.") assert_within_tol(sd, error, 0.1 * max(error, 1e-10), "Error in expected error in parallel run.")
def run_serial(self,f,npoints,expected_value,expected_variance,**kwargs): res, sd = mcquad(f,npoints,nprocs=1,**kwargs) volume = self.calc_volume(kwargs["xl"],kwargs["xu"]) error = volume*np.sqrt(expected_variance/float(npoints)) assert_within_tol(res,expected_value,3.*max(error,1e-10), "Error in <f> in serial run.") assert_within_tol(sd,error,0.1*max(error,1e-10), "Error in expected error in serial run.")
def run_serial(self, f, npoints, expected_value, expected_variance, **kwargs): res, sd = mcquad(f, npoints, nprocs=1, **kwargs) volume = self.calc_volume(kwargs["xl"], kwargs["xu"]) error = volume * np.sqrt(expected_variance / float(npoints)) assert_within_tol(res, expected_value, 3. * max(error, 1e-10), "Error in <f> in serial run.") assert_within_tol(sd, error, 0.1 * max(error, 1e-10), "Error in expected error in serial run.")
def run_all(self,f,points,serial_only=False,**kwargs): expected_res,expected_err = self.calc_res_err(f,points) # serial run res, err = integrate_from_points(f,points,nprocs=1,**kwargs) assert_within_tol(res,expected_res,1e-10) assert_within_tol(err,expected_err,1e-10) # parallel run if not serial_only: res, err = integrate_from_points(f,points, nprocs=2,batch_size=len(points)/10,**kwargs) assert_within_tol(res,expected_res,1e-10) assert_within_tol(err,expected_err,1e-10)
def test_args(self): """ x**2 + a*y**2, passing a as an argument. """ npoints = 1000 points = numpy.random.ranf(size=2*npoints).reshape(npoints,2) aval = 3.0 f = lambda xs,a: xs[0]**2 + a*xs[1]**2 expected_res, expected_err = self.calc_res_err( lambda xs:xs[0]**2 + aval*xs[1]**2,points) res_serial, err_serial = integrate_from_points(f,points, args=(aval,),nprocs=1) assert_within_tol(res_serial,expected_res,1e-10) assert_within_tol(err_serial,expected_err,1e-10) res_parallel, err_parallel = integrate_from_points(f,points, args=(aval,),nprocs=2,batch_size=npoints/2) assert_within_tol(res_parallel,expected_res,1e-10) assert_within_tol(err_parallel,expected_err,1e-10)
def test_weight(self): """ a*x**2, using a weight. """ npoints = 1000 points = numpy.random.ranf(size=npoints).reshape(npoints,1) aval = 3.0 f = lambda x: x**2 expected_res, expected_err = self.calc_res_err( lambda x:aval*x**2,points) res_serial, err_serial = integrate_from_points(f,points, nprocs=1,weight=aval) assert_within_tol(res_serial,expected_res,1e-10) assert_within_tol(err_serial,expected_err,1e-10) res_parallel, err_parallel = integrate_from_points(f,points, nprocs=2,batch_size=npoints/2,weight=aval) assert_within_tol(res_parallel,expected_res,1e-10) assert_within_tol(err_parallel,expected_err,1e-10)