Skip to content

optimal_graded_mesh

optimal_graded_mesh

optimal_graded_mesh(*, alpha, t_end, n_intervals, order)

Return a graded mesh of n_intervals + 1 breakpoints suitable for a weakly singular convolution kernel \(K(u) \sim u^{-\alpha}\), \(\alpha \in [0, 1)\).

Grading: \(t_n = T (n/M)^r\) with \(T\) = t_end, \(M\) = n_intervals and \(r = p / (1 - \alpha)\), where \(p\) = order is the order of the collocation method (the number of collocation nodes per interval). This recovers the optimal convergence order for Abel-type kernels (Brunner 2004, ch. 6). At alpha == 0 the kernel is non-singular and the solution smooth, so a uniform mesh (\(r = 1\)) is returned instead.

Pair the mesh with kernel_singularity={0.0: alpha} in the solver. Declaring the exponent, not only the location, lets the singular blocks use Gauss–Jacobi rules instead of adaptive quadrature, which is typically 3–10× faster.

Parameters:

Name Type Description Default
alpha float

Singularity exponent, in \([0, 1)\).

required
t_end float

Right endpoint of the mesh (positive).

required
n_intervals int

Number of intervals (positive). The returned array has length n_intervals + 1.

required
order int

Order of the collocation method (positive). For a matched mesh, pass the number of collocation nodes the solver uses.

required

Returns:

Type Description
ndarray of shape (n_intervals + 1,)

Strictly increasing breakpoints with [0] == 0 and [-1] == t_end.

Raises:

Type Description
ValueError

If alpha is outside \([0, 1)\), t_end is not positive and finite, or n_intervals / order is less than 1.

TypeError

If alpha or t_end is not a real number, n_intervals / order is not an integer, or an argument is missing.

Examples:

>>> import voles
>>> voles.optimal_graded_mesh(alpha=0.5, t_end=1.0, n_intervals=4, order=2)
array([0.        , 0.00390625, 0.0625    , 0.31640625, 1.        ])

Grade for the same order as the solver's collocation method. The solution of \(y(t) = 1 + \int_0^t (t-s)^{-1/2} y(s)\,ds\) is \(\operatorname{erfcx}(-\sqrt{\pi t})\):

>>> import numpy as np
>>> from scipy.special import erfcx
>>> nodes = voles.radau_iia_nodes(3)
>>> mesh = voles.optimal_graded_mesh(alpha=0.5, t_end=1.0, n_intervals=60,
...                                  order=len(nodes))
>>> sol = voles.solve_vie2(kernel=lambda u: u**-0.5, g=lambda t: np.ones_like(t),
...                        mesh=mesh, coll_nodes=nodes, kernel_singularity={0.0: 0.5})
>>> print(f"relative error {abs(sol(1.0) / erfcx(-np.sqrt(np.pi)) - 1):.1e}")
relative error 1.3e-05
Source code in src/voles/_functional.py
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
def optimal_graded_mesh(*, alpha: float, t_end: float, n_intervals: int,
                        order: int) -> np.ndarray:
    r"""Return a graded mesh of ``n_intervals + 1`` breakpoints suitable for a
    weakly singular convolution kernel $K(u) \sim u^{-\alpha}$,
    $\alpha \in [0, 1)$.

    Grading: $t_n = T (n/M)^r$ with $T$ = ``t_end``, $M$ = ``n_intervals``
    and $r = p / (1 - \alpha)$, where $p$ = ``order`` is the order of the
    collocation method (the number of collocation nodes per interval). This
    recovers the optimal convergence order for Abel-type kernels (Brunner
    2004, ch. 6). At ``alpha == 0`` the kernel is non-singular and the
    solution smooth, so a uniform mesh ($r = 1$) is returned instead.

    Pair the mesh with ``kernel_singularity={0.0: alpha}`` in the solver.
    Declaring the exponent, not only the location, lets the singular blocks
    use Gauss–Jacobi rules instead of adaptive quadrature, which is
    typically 3–10× faster.

    Parameters
    ----------
    alpha : float
        Singularity exponent, in $[0, 1)$.
    t_end : float
        Right endpoint of the mesh (positive).
    n_intervals : int
        Number of intervals (positive). The returned array has length
        ``n_intervals + 1``.
    order : int
        Order of the collocation method (positive). For a matched mesh,
        pass the number of collocation nodes the solver uses.
    Returns
    -------
    ndarray of shape (n_intervals + 1,)
        Strictly increasing breakpoints with ``[0] == 0`` and
        ``[-1] == t_end``.

    Raises
    ------
    ValueError
        If ``alpha`` is outside $[0, 1)$, ``t_end`` is not positive and
        finite, or ``n_intervals`` / ``order`` is less than 1.
    TypeError
        If ``alpha`` or ``t_end`` is not a real number, ``n_intervals`` /
        ``order`` is not an integer, or an argument is missing.

    Examples
    --------
    >>> import voles
    >>> voles.optimal_graded_mesh(alpha=0.5, t_end=1.0, n_intervals=4, order=2)
    array([0.        , 0.00390625, 0.0625    , 0.31640625, 1.        ])

    Grade for the same order as the solver's collocation method. The
    solution of $y(t) = 1 + \int_0^t (t-s)^{-1/2} y(s)\,ds$ is
    $\operatorname{erfcx}(-\sqrt{\pi t})$:

    >>> import numpy as np
    >>> from scipy.special import erfcx
    >>> nodes = voles.radau_iia_nodes(3)
    >>> mesh = voles.optimal_graded_mesh(alpha=0.5, t_end=1.0, n_intervals=60,
    ...                                  order=len(nodes))
    >>> sol = voles.solve_vie2(kernel=lambda u: u**-0.5, g=lambda t: np.ones_like(t),
    ...                        mesh=mesh, coll_nodes=nodes, kernel_singularity={0.0: 0.5})
    >>> print(f"relative error {abs(sol(1.0) / erfcx(-np.sqrt(np.pi)) - 1):.1e}")
    relative error 1.3e-05
    """
    alpha = _v.real("alpha", alpha)
    if not 0.0 <= alpha < 1.0:
        raise ValueError(f"alpha must satisfy 0 <= alpha < 1, got {alpha}")
    t_end = _v.real("t_end", t_end, positive=True,
                    value_msg=f"t_end must be positive and finite, got {t_end}")
    n_intervals = _v.integer("n_intervals", n_intervals, minimum=1,
                             value_msg=f"n_intervals must be a positive integer, got {n_intervals}")
    order = _v.integer("order", order, minimum=1,
                       type_msg=f"order must be a positive integer, got {order!r}",
                       value_msg=f"order must be a positive integer, got {order}")
    p = order
    # At alpha=0 the kernel is non-singular and the solution is smooth, so a
    # uniform mesh (r=1) is optimal; grading would only waste resolution near 0.
    r = p / (1.0 - alpha) if alpha > 0.0 else 1.0
    n = np.arange(n_intervals + 1, dtype=np.float64)
    return t_end * (n / n_intervals) ** r