Skip to content

Commit 11e3fcc

Browse files
committed
allow bias(k,z) for source window functions
1 parent a58c8b7 commit 11e3fcc

File tree

8 files changed

+80
-21
lines changed

8 files changed

+80
-21
lines changed

camb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__author__ = "Antony Lewis"
88
__contact__ = "antony at cosmologist dot info"
99
__url__ = "https://camb.readthedocs.io"
10-
__version__ = "1.3.7"
10+
__version__ = "1.3.8"
1111

1212
from . import baseconfig
1313

camb/baseconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ class F2003:
504504
"sized fields only allowed for ctypes Arrays")
505505
if dic["size"] not in [x[0] for x in _fields]:
506506
raise CAMBFortranError(
507-
"size must be name of field in same structure (%s for %s)" % (
507+
"size must be the name of a field in same structure (%s for %s)" % (
508508
dic["size"], field_name))
509509
new_field = SizedArrayField(field_name, dic["size"])
510510
ctypes_fields.append(("_" + field_name, field_type))

camb/cambdll.dll

2.75 KB
Binary file not shown.

camb/sources.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .baseconfig import F2003Class, fortran_class, numpy_1d, np, numpy_1d_or_null
1+
from .baseconfig import F2003Class, fortran_class, numpy_1d, numpy_2d, np, numpy_1d_or_null
22
from ctypes import POINTER, c_int, c_double, byref
33

44

@@ -35,30 +35,45 @@ class SplinedSourceWindow(SourceWindow):
3535
"""
3636
_fortran_class_name_ = "TSplinedSourceWindow"
3737

38-
_methods_ = [("SetTable", [POINTER(c_int), numpy_1d, numpy_1d, numpy_1d_or_null])]
38+
_methods_ = [("SetTable", [POINTER(c_int), numpy_1d, numpy_1d, numpy_1d_or_null]),
39+
("SetTable2DBias", [POINTER(c_int), POINTER(c_int), numpy_1d, numpy_1d, numpy_1d, numpy_2d])
40+
]
3941

4042
def __init__(self, **kwargs):
4143
z = kwargs.pop('z', None)
4244
if z is not None:
43-
self.set_table(z, kwargs.pop('W'), kwargs.pop('bias_z', None))
45+
self.set_table(z, kwargs.pop('W'), kwargs.pop('bias_z', None),
46+
kwargs.pop('k_bias', None), kwargs.pop('bias_kz', None))
4447
super().__init__(**kwargs)
4548

46-
def set_table(self, z, W, bias_z=None):
49+
def set_table(self, z, W, bias_z=None, k_bias=None, bias_kz=None):
4750
"""
4851
Set arrays of z and W(z) for cublic spline interpolation. Note that W(z) is the total count distribution
4952
observed, not a fractional selection function on an underlying distribution.
5053
5154
:param z: array of redshift values (monotonically increasing)
5255
:param W: array of window function values. It must be well enough sampled to smoothly cubic-spline interpolate
53-
:param bias_z: optional array of bias values at each z
56+
:param bias_z: optional array of bias values at each z for scale-independent bias
57+
:param k_bias: optional array of k values for bias
58+
:param bias_kz: optional 2D contiguous array for space-dependent bias(k, z).
59+
Must ensure range of k is large enough to cover required vaules.
60+
5461
"""
5562
if len(W) != len(z) or z[-1] < z[1] or len(z) < 5:
5663
raise ValueError(
5764
"Redshifts must be well sampled and in ascending order, with window function the same length as z")
65+
z = np.ascontiguousarray(z, dtype=np.float64)
66+
W = np.ascontiguousarray(W, dtype=np.float64)
5867
if bias_z is not None:
68+
if bias_kz is not None:
69+
raise ValueError("set bias_k or bias_zk")
5970
bias_z = np.ascontiguousarray(bias_z, dtype=np.float64)
6071
if len(bias_z) != len(z):
6172
raise ValueError("bias array must be same size as the redshift array")
62-
63-
self.f_SetTable(byref(c_int(len(z))), np.ascontiguousarray(z, dtype=np.float64),
64-
np.ascontiguousarray(W, dtype=np.float64), bias_z)
73+
if bias_kz is not None:
74+
k = np.ascontiguousarray(k_bias, dtype=np.float64)
75+
if bias_kz.shape[0] != len(k) or bias_kz.shape[1] != len(z):
76+
raise ValueError('Bias array does not match shape of k,z arrays')
77+
self.f_SetTable2DBias(byref(c_int(len(z))), byref(c_int(len(k))), z, k, W, bias_kz)
78+
else:
79+
self.f_SetTable(byref(c_int(len(z))), z, W, bias_z)

camb/tests/camb_test.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,18 @@ def testSources(self):
625625
cls = results.get_source_cls_dict()
626626
zs = np.arange(0, 0.5, 0.02)
627627
W = np.exp(-(zs - 0.17) ** 2 / 2 / 0.04 ** 2) / np.sqrt(2 * np.pi) / 0.04
628-
pars.SourceWindows[0] = SplinedSourceWindow(bias=1.2, dlog10Ndm=-0.2, z=zs, W=W)
629-
results = camb.get_results(pars)
630-
cls2 = results.get_source_cls_dict()
631-
self.assertTrue(np.allclose(cls2["W1xW1"][2:1200], cls["W1xW1"][2:1200], rtol=1e-3))
628+
629+
ks = np.logspace(-4, 3, 50)
630+
bias_kz = 1.2 * np.ones((len(ks), len(zs)))
631+
test_windows = [SplinedSourceWindow(bias=1.2, dlog10Ndm=-0.2, z=zs, W=W),
632+
SplinedSourceWindow(bias_z=1.2 * np.ones_like(zs), dlog10Ndm=-0.2, z=zs, W=W),
633+
SplinedSourceWindow(k_bias=ks, bias_kz=bias_kz, dlog10Ndm=-0.2, z=zs, W=W)]
634+
for window in test_windows:
635+
pars.SourceWindows[0] = window
636+
results = camb.get_results(pars)
637+
cls2 = results.get_source_cls_dict()
638+
self.assertTrue(np.allclose(cls2["W1xW1"][2:1200], cls["W1xW1"][2:1200], rtol=1e-3))
639+
632640
pars.SourceWindows = [GaussianSourceWindow(redshift=1089, source_type='lensing', sigma=30)]
633641
results = camb.get_results(pars)
634642
cls = results.get_source_cls_dict()

fortran/SourceWindows.f90

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module SourceWindows
22
use precision
33
use Classes
44
use MpiUtils
5+
use Interpolation, only : TCubicSpline, TInterpGrid2D
56
implicit none
67

78
integer, parameter :: window_21cm = 1, window_counts = 2, window_lensing = 3
@@ -31,12 +32,14 @@ module SourceWindows
3132
Type, extends(TSourceWindow) :: TSplinedSourceWindow
3233
Type(TCubicSpline), allocatable :: Window
3334
Type(TCubicSpline), allocatable :: Bias_z
35+
Type(TInterpGrid2D), allocatable :: Bias_zk
3436
real(dl) :: maxwin
3537
contains
3638
procedure, nopass :: SelfPointer => TSplinedSourceWindow_SelfPointer
3739
procedure :: count_obs_window_z => TSplinedSourceWindow_count_obs_window_z
3840
procedure :: GetScales => TSplinedSourceWindow_GetScales
3941
procedure :: SetTable => TSplinedSourceWindow_SetTable
42+
procedure :: SetTable2DBias => TSplinedSourceWindow_SetTable2DBias
4043
procedure :: GetBias => TSplinedSourceWindow_GetBias
4144
end Type TSplinedSourceWindow
4245

@@ -215,7 +218,18 @@ real(dl) function TSplinedSourceWindow_GetBias(this,k,a)
215218
class(TSplinedSourceWindow) :: this
216219
real(dl), intent(in) :: k,a
217220
real(dl) z
218-
if (allocated(this%bias_z)) then
221+
integer error
222+
223+
if (allocated(this%bias_zk)) then
224+
z = 1/a-1
225+
if (z > this%Window%X(this%Window%n) .or. z < this%Window%X(1)) then
226+
TSplinedSourceWindow_GetBias = 0
227+
else
228+
error = 0
229+
TSplinedSourceWindow_GetBias = this%bias_zk%value(z, k, error)
230+
if (error /= 0) TSplinedSourceWindow_GetBias = 0
231+
end if
232+
elseif (allocated(this%bias_z)) then
219233
z = 1/a-1
220234
if (z > this%Window%X(this%Window%n) .or. z < this%Window%X(1)) then
221235
TSplinedSourceWindow_GetBias = 0
@@ -242,21 +256,42 @@ subroutine TSplinedSourceWindow_SetTable(this, n, z, W, bias_z)
242256
end if
243257
if (present(bias_z)) then
244258
if (allocated(this%Bias_z)) deallocate(this%Bias_z)
259+
if (allocated(this%Bias_zk)) deallocate(this%Bias_zk)
245260
if (n>0) then
246261
allocate(this%Bias_z)
247262
call this%Bias_z%Init(z,bias_z)
248263
end if
249264
end if
250-
251265
end subroutine TSplinedSourceWindow_SetTable
252266

267+
subroutine TSplinedSourceWindow_SetTable2DBias(this, n, nk, z, k, W, bias_zk)
268+
class(TSplinedSourceWindow) :: this
269+
integer, intent(in) :: n, nk
270+
real(dl), intent(in) :: z(n), W(n), k(nk)
271+
real(dl), intent(in) :: bias_zk(n,nk)
272+
273+
if (allocated(this%Window)) deallocate(this%Window)
274+
if (n>0) then
275+
allocate(this%Window)
276+
call this%Window%Init(z,W)
277+
this%maxwin = maxval(this%Window%F)
278+
end if
279+
if (allocated(this%Bias_zk)) deallocate(this%Bias_zk)
280+
if (n>0 .and. nk>0) then
281+
allocate(this%Bias_zk)
282+
call this%Bias_zk%Init(z,k,bias_zk)
283+
end if
284+
285+
end subroutine TSplinedSourceWindow_SetTable2DBias
286+
287+
253288
function TSplinedSourceWindow_count_obs_window_z(this, z, winamp)
254289
!distribution function W(z) for the observed sources, used for lensing and number count spectrum
255290
!Winamp is amplitude normalized to 1 so the code can tell when the window is very small
256291
!note this is the total count distribution observed, not a fractional selection function on an underlying distribution
257292
class(TSplinedSourceWindow) :: this
258293
real(dl), intent(in) :: z
259-
real(dl) TSplinedSourceWindow_count_obs_window_z, dz,winamp
294+
real(dl) TSplinedSourceWindow_count_obs_window_z, winamp
260295

261296
if (z > this%Window%X(this%Window%n) .or. z < this%Window%X(1)) then
262297
TSplinedSourceWindow_count_obs_window_z =0
@@ -270,7 +305,7 @@ end function TSplinedSourceWindow_count_obs_window_z
270305
subroutine TSplinedSourceWindow_GetScales(this, zpeak, sigma_z, zpeakstart, zpeakend)
271306
class(TSplinedSourceWindow) :: this
272307
real(dl), intent(out) :: zpeak, sigma_z, zpeakstart, zpeakend
273-
integer ix, i, j
308+
integer i, j
274309
real(dl) z1, zstart, zend, targ
275310

276311
associate(z => this%Window%X, W => this%Window%F, n=>this%Window%n)

fortran/config.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module config
33
use constants, only: const_twopi
44
implicit none
55

6-
character(LEN=*), parameter :: version = '1.3.7'
6+
character(LEN=*), parameter :: version = '1.3.8'
77

88
integer :: FeedbackLevel = 0 !if >0 print out useful information about the model
99

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,9 @@ def run(self):
305305
'Programming Language :: Python :: 3.6',
306306
'Programming Language :: Python :: 3.7',
307307
'Programming Language :: Python :: 3.8',
308-
'Programming Language :: Python :: 3.9'
309-
'Programming Language :: Python :: 3.10'
308+
'Programming Language :: Python :: 3.9',
309+
'Programming Language :: Python :: 3.10',
310+
'Programming Language :: Python :: 3.11'
310311
],
311312
keywords=['cosmology', 'CAMB', 'CMB'],
312313
install_requires=['scipy>=1.0', 'sympy>=1.0'],

0 commit comments

Comments
 (0)