core.bsplines#

Basic module that provides the means for evaluating the B-Splines basis functions and their derivatives. In order to simplify automatic Fortran code generation with Pyccel, no object-oriented features are employed.

References:

  • [1] L. Piegl and W. Tiller. The NURBS Book, 2nd ed., Springer-Verlag Berlin Heidelberg GmbH, 1997.

  • [2] SELALIB, Semi-Lagrangian Library. http://selalib.gforge.inria.fr

Functions#

basis_ders_on_irregular_grid(knots, degree, ...)

Evaluate B-Splines and their derivatives on an irregular_grid.

basis_ders_on_quad_grid(knots, degree, ...)

Evaluate B-Splines and their derivatives on the quadrature grid.

basis_funs(knots, degree, x, span[, out])

Compute the non-vanishing B-splines at a unique location.

basis_funs_1st_der(knots, degree, x, span[, out])

Compute the first derivative of the non-vanishing B-splines at a location.

basis_funs_all_ders(knots, degree, x, span, n)

Evaluate value and n derivatives at x of all basis functions with support in interval \([x_{span-1}, x_{span}]\).

basis_funs_array(knots, degree, span, x[, out])

Compute the non-vanishing B-splines at several locations.

basis_integrals(knots, degree[, out])

Return the integral of each B-spline basis function over the real line:

breakpoints(knots, degree[, tol, out])

Determine breakpoints' coordinates.

cell_index(breaks, i_grid[, tol, out])

Computes in which cells a given array of locations belong.

collocation_matrix(knots, degree, periodic, ...)

Computes the collocation matrix

elements_spans(knots, degree[, out])

Compute the index of the last non-vanishing spline on each grid element (cell).

elevate_knots(knots, degree, periodic[, ...])

Given the knot sequence of a spline space S of degree p, compute the knot sequence of a spline space S_0 of degree p+1 such that u' is in S for all u in S_0.

find_span(knots, degree, x)

Determine the knot span index at location x, given the B-Splines' knot sequence and polynomial degree.

find_spans(knots, degree, x[, out])

Determine the knot span index at a set of locations x, given the B-Splines' knot sequence and polynomial degree.

greville(knots, degree, periodic[, out, ...])

Compute coordinates of all Greville points.

histopolation_matrix(knots, degree, ...[, ...])

Computes the histopolation matrix.

hrefinement_matrix(ts, p, knots)

Computes the refinement matrix corresponding to the insertion of a given list of knots.

make_knots(breaks, degree, periodic[, ...])

Create spline knots from breakpoints, with appropriate boundary conditions.

quadrature_grid(breaks, quad_rule_x, quad_rule_w)

Compute the quadrature points and weights for performing integrals over each element (interval) of the 1D domain, given a certain Gaussian quadrature rule.

Details#

Basic module that provides the means for evaluating the B-Splines basis functions and their derivatives. In order to simplify automatic Fortran code generation with Pyccel, no object-oriented features are employed.

References:

  • [1] L. Piegl and W. Tiller. The NURBS Book, 2nd ed., Springer-Verlag Berlin Heidelberg GmbH, 1997.

  • [2] SELALIB, Semi-Lagrangian Library. http://selalib.gforge.inria.fr

find_span(knots, degree, x)[source]#

Determine the knot span index at location x, given the B-Splines’ knot sequence and polynomial degree. See Algorithm A2.1 in [1].

For a degree p, the knot span index i identifies the indices [i-p:i] of all p+1 non-zero basis functions at a given location x.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

xfloat

Location of interest.

Returns:
spanint

Knot span index.

find_spans(knots, degree, x, out=None)[source]#

Determine the knot span index at a set of locations x, given the B-Splines’ knot sequence and polynomial degree. See Algorithm A2.1 in [1].

For a degree p, the knot span index i identifies the indices [i-p:i] of all p+1 non-zero basis functions at a given location x.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

xarray_like of floats

Locations of interest.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
spansarray of ints

Knots span indexes.

basis_funs(knots, degree, x, span, out=None)[source]#

Compute the non-vanishing B-splines at a unique location.

Parameters:
knotsarray_like of floats

Knots sequence.

degreeint

Polynomial degree of B-splines.

xfloat

Evaluation point.

spanint

Knot span index.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
array

1D array containing the values of degree + 1 non-zero Bsplines at location x.

basis_funs_array(knots, degree, span, x, out=None)[source]#

Compute the non-vanishing B-splines at several locations.

Parameters:
knotsarray_like of floats

Knots sequence.

degreeint

Polynomial degree of B-splines.

xarray_like of floats

Evaluation points.

spanarray_like of int

Knot span indexes.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
array

2D array of shape (len(x), degree + 1) containing the values of degree + 1 non-zero Bsplines at each location in x.

basis_funs_1st_der(knots, degree, x, span, out=None)[source]#

Compute the first derivative of the non-vanishing B-splines at a location.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

xfloat

Evaluation point.

spanint

Knot span index.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
array

1D array of size degree + 1 containing the derivatives of the degree + 1 non-vanishing B-Splines at location x.

Notes

See function ‘s_bsplines_non_uniform__eval_deriv’ in Selalib’s ([2]) source file ‘src/splines/sll_m_bsplines_non_uniform.F90’.

References

[2]

SELALIB, Semi-Lagrangian Library. http://selalib.gforge.inria.fr

basis_funs_all_ders(knots, degree, x, span, n, normalization='B', out=None)[source]#

Evaluate value and n derivatives at x of all basis functions with support in interval \([x_{span-1}, x_{span}]\).

If called with normalization=’M’, this uses M-splines instead of B-splines.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

xfloat

Evaluation point.

spanint

Knot span index.

nint

Max derivative of interest.

normalization: str

Set to ‘B’ to get B-Splines and ‘M’ to get M-Splines

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
dersarray

2D array of n+1 (from 0-th to n-th) derivatives at x of all (degree+1) non-vanishing basis functions in given span. ders[i,j] = (d/dx)^i B_k(x) with k=(span-degree+j), for 0 <= i <= n and 0 <= j <= degree+1.

collocation_matrix(knots, degree, periodic, normalization, xgrid, out=None, multiplicity=1)[source]#

Computes the collocation matrix

If called with normalization=’M’, this uses M-splines instead of B-splines.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of spline space.

periodicbool

True if domain is periodic, False otherwise.

normalizationstr

Set to ‘B’ for B-splines, and ‘M’ for M-splines.

xgridarray_like

Evaluation points.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

multiplicityint

Multiplicity of the knots in the knot sequence, we assume that the same multiplicity applies to each interior knot.

Returns:
colloc_matrixndarray of floats

Array containing the collocation matrix.

Notes

The collocation matrix \(C_ij = B_j(x_i)\), contains the values of each B-spline basis function \(B_j\) at all locations \(x_i\).

histopolation_matrix(knots, degree, periodic, normalization, xgrid, multiplicity=1, check_boundary=True, out=None)[source]#

Computes the histopolation matrix.

If called with normalization=’M’, this uses M-splines instead of B-splines.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of spline space.

periodicbool

True if domain is periodic, False otherwise.

normalizationstr

Set to ‘B’ for B-splines, and ‘M’ for M-splines.

xgridarray_like

Grid points.

multiplicityint

Multiplicity of the knots in the knot sequence, we assume that the same multiplicity applies to each interior knot.

check_boundarybool, default=True

If true and periodic, will check the boundaries of xgrid.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
array

Histopolation matrix

Notes

The histopolation matrix \(H_{ij} = \int_{x_i}^{x_{i+1}}B_j(x)\,dx\) contains the integrals of each B-spline basis function \(B_j\) between two successive grid points.

breakpoints(knots, degree, tol=1e-15, out=None)[source]#

Determine breakpoints’ coordinates.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

tol: float

If the distance between two knots is less than tol, we assume that they are repeated knots which correspond to the same break point.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
breaksnumpy.ndarray (1D)

Abscissas of all breakpoints.

greville(knots, degree, periodic, out=None, multiplicity=1)[source]#

Compute coordinates of all Greville points.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

periodicbool

True if domain is periodic, False otherwise.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

multiplicityint

Multiplicity of the knots in the knot sequence, we assume that the same multiplicity applies to each interior knot.

Returns:
grevillenumpy.ndarray (1D)

Abscissas of all Greville points.

elements_spans(knots, degree, out=None)[source]#

Compute the index of the last non-vanishing spline on each grid element (cell). The length of the returned array is the number of cells.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
spansnumpy.ndarray (1D)

Index of last non-vanishing spline on each grid element.

Notes

  1. Numbering of basis functions starts from 0, not 1;

  2. This function could be written in two lines:

    breaks = breakpoints( knots, degree ) spans = np.searchsorted( knots, breaks[:-1], side=’right’ ) - 1

Examples

>>> import numpy as np
>>> from psydac.core.bsplines import make_knots, elements_spans
>>> p = 3 ; n = 8
>>> grid  = np.arange( n-p+1 )
>>> knots = make_knots( breaks=grid, degree=p, periodic=False )
>>> spans = elements_spans( knots=knots, degree=p )
>>> spans
array([3, 4, 5, 6, 7])
make_knots(breaks, degree, periodic, multiplicity=1, out=None)[source]#

Create spline knots from breakpoints, with appropriate boundary conditions.

If domain is periodic, knot sequence is extended by periodicity to have a total of (n_cells-1)*mult+2p+2 knots (all break points are repeated mult time and we add p+1-mult knots by periodicity at each side).

Otherwise, knot sequence is clamped (i.e. endpoints have multiplicity p+1).

Parameters:
breaksarray_like

Coordinates of breakpoints (= cell edges); given in increasing order and with no duplicates.

degreeint

Spline degree (= polynomial degree within each interval).

periodicbool

True if domain is periodic, False otherwise.

multiplicity: int

Multiplicity of the knots in the knot sequence, we assume that the same multiplicity applies to each interior knot.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
Tnumpy.ndarray (1D)

Coordinates of spline knots.

elevate_knots(knots, degree, periodic, multiplicity=1, tol=1e-15, out=None)[source]#

Given the knot sequence of a spline space S of degree p, compute the knot sequence of a spline space S_0 of degree p+1 such that u’ is in S for all u in S_0.

Specifically, on bounded domains the first and last knots are repeated in the sequence, and in the periodic case the knot sequence is extended by periodicity.

Parameters:
knotsarray_like

Knots sequence of spline space of degree p.

degreeint

Spline degree (= polynomial degree within each interval).

periodicbool

True if domain is periodic, False otherwise.

multiplicityint

Multiplicity of the knots in the knot sequence, we assume that the same multiplicity applies to each interior knot.

tol: float

If the distance between two knots is less than tol, we assume that they are repeated knots which correspond to the same break point.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
new_knotsndarray

Knots sequence of spline space of degree p+1.

quadrature_grid(breaks, quad_rule_x, quad_rule_w)[source]#

Compute the quadrature points and weights for performing integrals over each element (interval) of the 1D domain, given a certain Gaussian quadrature rule.

An n-point Gaussian quadrature rule for the canonical interval \([-1,+1]\) and trivial weighting function \(\omega(x)=1\) is defined by the n abscissas \(x_i\) and n weights \(w_i\) that satisfy the following identity for polynomial functions \(f(x)\) of degree \(2n-1\) or less:

\[\int_{-1}^{+1} f(x) dx = \sum_{i=0}^{n-1} w_i f(x_i)\]
Parameters:
breaksarray_like of floats

Coordinates of spline breakpoints.

quad_rule_xarray_like of ints

Coordinates of quadrature points on canonical interval [-1,1].

quad_rule_warray_like of ints

Weights assigned to quadrature points on canonical interval [-1,1].

Returns:
quad_x2D numpy.ndarray

Abscissas of quadrature points on each element (interval) of the 1D domain. See notes below.

quad_w2D numpy.ndarray

Weights assigned to the quadrature points on each element (interval) of the 1D domain. See notes below.

Notes

Contents of 2D output arrays ‘quad_x’ and ‘quad_w’ are accessed with two indices (ie,iq) where:

  • ie is the global element index;

  • iq is the local index of a quadrature point within the element.

basis_integrals(knots, degree, out=None)[source]#

Return the integral of each B-spline basis function over the real line:

\[K[i] = \int_{-\infty}^{+\infty} B_i(x) dx = (T[i+p+1]-T[i]) / (p+1).\]

This array can be used to convert B-splines to M-splines, which have unit integral over the real line but no partition-of-unity property.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
Knumpy.ndarray

Array with the integrals of each B-spline basis function.

Notes

For convenience, this function does not distinguish between periodic and non-periodic spaces, hence the length of the output array is always equal to (len(knots)-degree-1). In the periodic case the last (degree) values in the array are redundant, as they are a copy of the first (degree) values.

basis_ders_on_quad_grid(knots, degree, quad_grid, nders, normalization, offset=0, out=None)[source]#

Evaluate B-Splines and their derivatives on the quadrature grid.

If called with normalization=’M’, this uses M-splines instead of B-splines.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

quad_grid: ndarray

2D array of shape (ne, nq). Coordinates of quadrature points of each element in 1D domain.

ndersint

Maximum derivative of interest.

normalizationstr

Set to ‘B’ for B-splines, and ‘M’ for M-splines.

offsetint, default=0

Assumes that the quadrature grid starts from cell number offset.

outarray, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
basis: ndarray

Values of B-Splines and their derivatives at quadrature points in each element of 1D domain. Indices are . ie: global element (0 <= ie < ne ) . il: local basis function (0 <= il <= degree) . id: derivative (0 <= id <= nders ) . iq: local quadrature point (0 <= iq < nq )

Examples

>>> knots = np.array([0.0, 0.0, 0.25, 0.5, 0.75, 1., 1.])
>>> degree = 2
>>> bk = breakpoints(knots, degree)
>>> grid = np.array([np.linspace(bk[i], bk[i+1], 4, endpoint=False) for i in range(len(bk) - 1)])
>>> basis_ders_on_quad_grid(knots, degree, grid, 0, "B")
array([[[[0.5, 0.28125, 0.125, 0.03125]],
        [[0.5, 0.6875 , 0.75 , 0.6875 ]],
        [[0. , 0.03125, 0.125, 0.28125]]],
       [[[0.5, 0.28125, 0.125, 0.03125]],
        [[0.5, 0.6875 , 0.75 , 0.6875 ]],
        [[0. , 0.03125, 0.125, 0.28125]]]])
cell_index(breaks, i_grid, tol=1e-15, out=None)[source]#

Computes in which cells a given array of locations belong.

Locations close to a interior breakpoint will be assumed to be present twice in the grid, once of for each cell. Boundary breakpoints are snapped to the interior of the domain.

Parameters:
breaksarray_like

Coordinates of breakpoints (= cell edges); given in increasing order and with no duplicates.

i_gridndarray

1D array of all of the points on which to evaluate the basis functions. The points do not need to be sorted.

tolfloat, default=1e-15

If the distance between a given point in i_grid and a breakpoint is less than tol then it is considered to be the breakpoint.

outarray, optional

If given, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
cell_index: ndarray

1D array of the same shape as i_grid. cell_index[i] is the index of the cell in which i_grid[i] belong.

basis_ders_on_irregular_grid(knots, degree, i_grid, cell_index, nders, normalization, out=None)[source]#

Evaluate B-Splines and their derivatives on an irregular_grid.

If called with normalization=’M’, this uses M-splines instead of B-splines.

Parameters:
knotsarray_like

Knots sequence.

degreeint

Polynomial degree of B-splines.

i_gridndarray

1D array of all of the points on which to evaluate the basis functions. The points do not need to be sorted

cell_indexndarray

1D array of the same shape as i_grid. cell_index[i] is the index of the cell in which i_grid[i] belong.

ndersint

Maximum derivative of interest.

normalizationstr

Set to ‘B’ for B-splines, and ‘M’ for M-splines.

outarray, optional

If given, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Returns:
out: ndarray

3D output array containing the values of B-Splines and their derivatives at each point in i_grid. Indices are: . ie: location (0 <= ie < nx ) . il: local basis function (0 <= il <= degree) . id: derivative (0 <= id <= nders )