Quadratic model

The quadratic model, pytransit.QuadraticModel, implements a transit over a stellar disk with the stellar limb darkening described using the quadratic limb darkening model as described by Mandel & Agol (ApJ 580, 2002). The model is parallelised using numba, and the number of threads can be set using the NUMBA_NUM_THREADS environment variable. An OpenCL version for GPU computation is implemented by pytransit.QuadraticModelCL, and is discussed later in this notebook.


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
sys.path.append('..')

In [3]:
from pytransit import QuadraticModel

In [4]:
seed(0)

times_sc = linspace(0.85, 1.15, 1000)  # Short cadence time stamps
times_lc = linspace(0.85, 1.15,  100)  # Long cadence time stamps

k, t0, p, a, i, e, w  = 0.1, 1., 2.1, 3.2, 0.5*pi, 0.3, 0.4*pi
pvp = tile([k, t0, p, a, i, e, w], (50,1))
pvp[1:,0] += normal(0.0, 0.005, size=pvp.shape[0]-1)
pvp[1:,1] += normal(0.0, 0.02, size=pvp.shape[0]-1)

ldc = stack([normal(0.3, 0.05, pvp.shape[0]), normal(0.1, 0.02, pvp.shape[0])], 1)

Model initialization

The quadratic model doesn't take any special initialization arguments, so the initialization is straightforward.


In [5]:
tm = QuadraticModel()

Data setup

Homogeneous time series

The model needs to be set up by calling set_data() before it can be used. At its simplest, set_data takes the mid-exposure times of the time series to be modelled.


In [6]:
tm.set_data(times_sc)

Model use

Evaluation The transit model can be evaluated using either a set of scalar parameters, a parameter vector (1D ndarray), or a parameter vector array (2D ndarray). The model flux is returned as a 1D ndarray in the first two cases, and a 2D ndarray in the last (one model per parameter vector).

  • tm.evaluate_ps(k, ldc, t0, p, a, i, e=0, w=0) evaluates the model for a set of scalar parameters, where k is the radius ratio, ldc is the limb darkening coefficient vector, t0 the zero epoch, p the orbital period, a the semi-major axis divided by the stellar radius, i the inclination in radians, e the eccentricity, and w the argument of periastron. Eccentricity and argument of periastron are optional, and omitting them defaults to a circular orbit.
  • tm.evaluate_pv(pv, ldc) evaluates the model for a 1D parameter vector, or 2D array of parameter vectors. In the first case, the parameter vector should be array-like with elements [k, t0, p, a, i, e, w]. In the second case, the parameter vectors should be stored in a 2d ndarray with shape (npv, 7) as
[[k1, t01, p1, a1, i1, e1, w1],
 [k2, t02, p2, a2, i2, e2, w2],
 ...
 [kn, t0n, pn, an, in, en, wn]]

The reason for the different options is that the model implementations may have optimisations that make the model evaluation for a set of parameter vectors much faster than if computing them separately. This is especially the case for the OpenCL models.

Note: PyTransit uses always a 2D parameter vector array under the hood, and the scalar evaluation method just packs the parameters into an array before model evaluation.

Limb darkening The quadratic limb darkening coefficients are given either as a 1D or 2D array, depending on whether the model is evaluated for a single set of parameters or an array of parameter vectors. In the first case, the coefficients can be given as [u, v], and in the second, as [[u1, v1], [u2, v2], ... [un, vn]].

In the case of a heterogeneous time series with multiple passbands (more details below), the coefficients are given for a single parameter set as a 1D array with a length $2n_{pb}$ ([u1, v1, u2, v2, ... un, vn], where the index now marks the passband), and for a parameter vector array as a 2D array with a shape (npv, 2*npb), as

[[u11, v11, u12, v12, ... u1n, v1n], 
 [u21, v21, u22, v22, ... u2n, v2n], 
 ...
 [un1, vn1, un2, vn2, ... unn, vnn]]

In [7]:
def plot_transits(tm, ldc, fmt='k'):
    fig, axs = subplots(1, 3, figsize = (13,3), constrained_layout=True, sharey=True)

    flux = tm.evaluate_ps(k, ldc[0], t0, p, a, i, e, w)
    axs[0].plot(tm.time, flux, fmt)
    axs[0].set_title('Individual parameters')

    flux = tm.evaluate_pv(pvp[0], ldc[0])
    axs[1].plot(tm.time, flux, fmt)
    axs[1].set_title('Parameter vector')

    flux = tm.evaluate_pv(pvp, ldc)
    axs[2].plot(tm.time, flux.T, 'k', alpha=0.2);
    axs[2].set_title('Parameter vector array')

    setp(axs[0], ylabel='Normalised flux')
    setp(axs, xlabel='Time [days]', xlim=tm.time[[0,-1]])

In [8]:
tm.set_data(times_sc)
plot_transits(tm, ldc)


Supersampling

The transit model can be supersampled by setting the nsamples and exptimes arguments in set_data.


In [9]:
tm.set_data(times_lc, nsamples=10, exptimes=0.01)
plot_transits(tm, ldc)


Heterogeneous time series

PyTransit allows for heterogeneous time series, that is, a single time series can contain several individual light curves (with, e.g., different time cadences and required supersampling rates) observed (possibly) in different passbands.

If a time series contains several light curves, it also needs the light curve indices for each exposure. These are given through lcids argument, which should be an array of integers. If the time series contains light curves observed in different passbands, the passband indices need to be given through pbids argument as an integer array, one per light curve. Supersampling can also be defined on per-light curve basis by giving the nsamplesand exptimes as arrays with one value per light curve.

For example, a set of three light curves, two observed in one passband and the third in another passband

times_1 (lc = 0, pb = 0, sc) = [1, 2, 3, 4]
times_2 (lc = 1, pb = 0, lc) = [3, 4]
times_3 (lc = 2, pb = 1, sc) = [1, 5, 6]

Would be set up as

tm.set_data(time  = [1, 2, 3, 4, 3, 4, 1, 5, 6], 
            lcids = [0, 0, 0, 0, 1, 1, 2, 2, 2], 
            pbids = [0, 0, 1],
            nsamples = [  1,  10,   1],
            exptimes = [0.1, 1.0, 0.1])

Further, each passband requires two limb darkening coefficients, so the limb darkening coefficient array for a single parameter set should now be

ldc = [u1, v1, u2, v2]

where u and v are the passband-specific quadratic limb darkening model coefficients.

Example: two light curves with different cadences and passbands


In [10]:
times_1 = linspace(0.85, 1.0, 500)
times_2 = linspace(1.0, 1.15,  10)
times = concatenate([times_1, times_2])
lcids = concatenate([full(times_1.size, 0, 'int'), full(times_2.size, 1, 'int')])
pbids = [0, 1]
nsamples = [1, 10]
exptimes = [0, 0.0167]

ldc2 = tile(ldc, (1,2))
ldc2[:,2:] /= 2

tm.set_data(times, lcids, pbids, nsamples=nsamples, exptimes=exptimes)
plot_transits(tm, ldc2, 'k.-')


OpenCL

Usage

The OpenCL version of the quadratic model, pytransit.QuadraticModelCL works identically to the Python version, except that the OpenCL context and queue can be given as arguments in the initialiser, and the model evaluation method can be told to not to copy the model from the GPU memory. If the context and queue are not given, the model creates a default context using cl.create_some_context().


In [13]:
import pyopencl as cl
from pytransit import QuadraticModelCL

devices = cl.get_platforms()[0].get_devices()[2:]
ctx = cl.Context(devices)
queue = cl.CommandQueue(ctx)

tm_cl = QuadraticModelCL(cl_ctx=ctx, cl_queue=queue)

In [14]:
tm_cl.set_data(times_sc)
plot_transits(tm_cl, ldc)


GPU vs. CPU Performance

The performance difference between the OpenCL and Python versions depends on the CPU, GPU, number of simultaneously evaluated models, amount of supersampling, and whether the model data is copied from the GPU memory. The performance difference grows in the favour of OpenCL model with the number of simultaneous models and amount of supersampling, but copying the data slows the OpenCL implementation down. For best performance, also the log likelihood computations should be done in the GPU.


In [15]:
times_sc2 = tile(times_sc, 20)  # 20000 short cadence datapoints
times_lc2 = tile(times_lc, 50)  #  5000 long cadence datapoints

In [16]:
tm_py = QuadraticModel()
tm_cl = QuadraticModelCL(cl_ctx=ctx, cl_queue=queue)

Short cadence data without heavy supersampling


In [17]:
tm_py.set_data(times_sc2)
tm_cl.set_data(times_sc2)

In [18]:
%%timeit
tm_py.evaluate_pv(pvp, ldc)


25.5 ms ± 1.93 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [24]:
%%timeit
tm_cl.evaluate_pv(pvp, ldc, copy=True)


6.53 ms ± 214 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Long cadence data with supersampling


In [20]:
tm_py.set_data(times_lc2, nsamples=10, exptimes=0.01)
tm_cl.set_data(times_lc2, nsamples=10, exptimes=0.01)

In [21]:
%%timeit
tm_py.evaluate_pv(pvp, ldc)


56.8 ms ± 3.45 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [23]:
%%timeit
tm_cl.evaluate_pv(pvp, ldc, copy=True)


6.62 ms ± 172 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Increasing the number of simultaneously evaluated models


In [25]:
pvp2 = tile(pvp, (3,1))
ldc2 = tile(ldc, (3,1))

In [26]:
%%timeit
tm_py.evaluate_pv(pvp2, ldc2)


179 ms ± 11.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [27]:
%%timeit
tm_cl.evaluate_pv(pvp2, ldc2, copy=False)


14.6 ms ± 94.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

© Hannu Parviainen 2010-2020