Evaluation

using TinyGismousing TinyGismo: knots, degree, basis, jacobian, hessian

Everything on this page follows the same pattern: pick a parametric point, ask a basis or a geometry for something at that point, and read the answer out of a gsMatrix.

Two objects are used throughout — a quadratic univariate basis and a plane curve built on it:

kv = KnotVector([0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0])b = BSplineBasis(kv)controlpoints = [0.0  0.0                 1.0  1.5                 2.0 -0.5                 3.0  1.0]curve = BSpline(b, controlpoints)ξ = [0.4]

Control points

coefs returns the control net as a gsMatrix — one row per control point, one column per physical coordinate — and coefsSize its row count:

Int(coefsSize(curve)), toMatrix(coefs(curve))
(4, [0.0 0.0; 1.0 1.5; 2.0 -0.5; 3.0 1.0])

For a univariate BSpline there is also numCoefs, which is the same number:

Int(numCoefs(curve))
4

coefAtCorner picks out a corner of the control net, which for a curve is simply the first or last control point:

toVector(coefAtCorner(curve, 1)), toVector(coefAtCorner(curve, 2))
([0.0, 0.0], [3.0, 1.0])

The dimension queries describe the mapping itself rather than its control net:

(    parDim    = parDim(curve),      # number of parameters    targetDim = targetDim(curve),   # dimension of the physical space    geoDim    = geoDim(curve),    coefDim   = coefDim(curve),)
(parDim = 1, targetDim = 2, geoDim = 2, coefDim = 2)

Basis function values

eval! on a basis returns only the functions that are nonzero at the point — p + 1 of them for a univariate degree-p basis, not all size(basis):

N = gsMatrix()eval!(b, ξ, N)toMatrix(N)
3×1 Matrix{Float64}:
 0.03999999999999998
 0.6399999999999999
 0.32000000000000006

Which functions those are is answered by active!, which fills a gsMatrix{Int32} with 1-based indices in the same order as the values above:

A = gsMatrix{Int32}()active!(b, ξ, A)toVector(A)
3-element Vector{Int32}:
 1
 2
 3

Together the two give the standard local assembly loop. Note the partition of unity:

values = toVector(N)indices = toVector(A)collect(zip(indices, values)), sum(values)
(Tuple{Int32, Float64}[(1, 0.03999999999999998), (2, 0.6399999999999999), (3, 0.32000000000000006)], 1.0)

isActive answers the same question for one function at a time, and numActive gives the count:

(    first = isActive(b, 1, ξ),    last  = isActive(b, 4, ξ),    count = Int(numActive(b)),)
(first = true, last = false, count = 3)

numActive works the same way on a tensor product basis, where it is the product over the directions — it never needs an evaluation point, because the count is the same on every element:

tb = basis(createBSplineRectangle())Int(numActive(tb)), (degree(tb, 1) + 1) * (degree(tb, 2) + 1)
(9, 9)

A single basis function can be evaluated on its own with evalSingle, using a global 1-based index rather than a position in the active list:

toMatrix(evalSingle(b, 2, ξ))
1×1 Matrix{Float64}:
 0.6399999999999999

Derivatives of basis functions

deriv! and deriv2! give first and second derivatives of the active functions, in the same layout as eval!:

dN = gsMatrix()deriv!(b, ξ, dN)toMatrix(dN)
3×1 Matrix{Float64}:
 -0.7999999999999998
 -0.8000000000000003
  1.6

The derivatives sum to zero, which is the differentiated partition of unity:

sum(toMatrix(dN))
0.0
ddN = gsMatrix()deriv2!(b, ξ, ddN)toMatrix(ddN)
3×1 Matrix{Float64}:
   8.0
 -12.0
   4.0

The per-function variants derivSingle and deriv2Single mirror evalSingle. Note that deriv2Single returns a bare Float64 rather than a gsMatrix:

toMatrix(derivSingle(b, 2, ξ)), deriv2Single(b, 2, ξ)
([-0.8000000000000003;;], -12.0)

Functions expressed in the basis

evalFunc evaluates $\sum_i c_i N_i(u)$ for a coefficient vector, without building a geometry object. derivFunc and deriv2Func do the same for derivatives:

c = [1.0, 2.0, 0.0, -1.0]toMatrix(evalFunc(b, ξ, c)), toMatrix(derivFunc(b, ξ, c))
([1.3199999999999998;;], [-2.4000000000000004;;])

This agrees with assembling the sum by hand from the active functions:

sum(values[k] * c[indices[k]] for k in eachindex(indices))
1.3199999999999998

Geometry positions

On a geometry, eval! maps parameters to physical points. The result is targetDim × npoints:

x = gsMatrix()eval!(curve, ξ, x)toMatrix(x)
2×1 Matrix{Float64}:
 1.2799999999999998
 0.7999999999999999

Sampling the whole curve is a matter of passing a 1 × n matrix — one column per point:

u = Matrix(collect(range(0.0, 1.0; length = 6))')X = gsMatrix()eval!(curve, u, X)toMatrix(X)
2×6 Matrix{Float64}:
 0.0  0.72  1.28  1.72  2.28  3.0
 0.0  0.8   0.8   0.2   0.2   1.0

The same works in higher parametric dimensions, with one row per parametric coordinate:

rect = createBSplineRectangle(0.0, 0.0, 2.0, 1.0)uv = [0.0 0.5 1.0      0.0 0.5 1.0]P = gsMatrix()eval!(rect, uv, P)toMatrix(P)
2×3 Matrix{Float64}:
 0.0  1.0  2.0
 0.0  0.5  1.0

Derivatives and the Jacobian

deriv on a geometry gives the tangent, and TinyGismo.jacobian the full derivative matrix of the mapping:

toMatrix(deriv(curve, ξ)), toMatrix(jacobian(curve, ξ))
([2.4; -2.0000000000000004;;], [2.4; -2.0000000000000004;;])

For a surface the Jacobian is targetDim × parDim — the two columns are the tangent vectors along $u$ and $v$:

toMatrix(jacobian(rect, [0.5, 0.5]))
2×2 Matrix{Float64}:
 2.0  0.0
 0.0  1.0

TinyGismo.hessian takes the index of the physical coordinate whose second derivatives you want. The rectangle is an affine map, so its Hessians vanish:

toMatrix(hessian(rect, [0.5, 0.5], 1))
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0

Inverting the mapping

closestPointTo goes the other way: given a physical point, it finds the parameter that comes closest to it, writing the answer into a gsVector.

The query point need not lie on the curve. Here it sits well above it:

par = gsVector()closestPointTo(curve, [1.5, 1.5], par)toVector(par)
1-element Vector{Float64}:
 0.3624476723268283

Evaluating the curve at that parameter gives the actual nearest point, which is what you compare against:

foot = gsMatrix()eval!(curve, toVector(par), foot)toMatrix(foot)
2×1 Matrix{Float64}:
 1.1870540589570415
 0.8610028822096107

For a point that is on the curve, the round trip returns it exactly:

onpar = gsVector()closestPointTo(curve, [1.5, 0.5], onpar)back = gsMatrix()eval!(curve, toVector(onpar), back)toVector(onpar), toMatrix(back)
([0.5], [1.5; 0.5;;])

Walking the mesh

knotSpans returns the elements of the parametric mesh as a vector, one entry per element. Each is described by lowerCorner, upperCorner and centerPoint, all of which return a gsVector with one entry per parametric direction.

b2 = BSplineBasis(kv)uniformRefine!(b2)map(knotSpans(b2)) do span    (lower = toVector(lowerCorner(span)), center = toVector(centerPoint(span)),     upper = toVector(upperCorner(span)))end
4-element Vector{@NamedTuple{lower::Vector{Float64}, center::Vector{Float64}, upper::Vector{Float64}}}:
 (lower = [0.0], center = [0.125], upper = [0.25])
 (lower = [0.25], center = [0.375], upper = [0.5])
 (lower = [0.5], center = [0.625], upper = [0.75])
 (lower = [0.75], center = [0.875], upper = [1.0])

In two dimensions the spans tile the parameter domain, ordered with the first direction running fastest:

tb = basis(createBSplineRectangle(0.0, 0.0, 2.0, 1.0))uniformRefine!(tb)map(knotSpans(tb)) do span    toVector(lowerCorner(span)) => toVector(upperCorner(span))end
4-element Vector{Pair{Vector{Float64}, Vector{Float64}}}:
 [0.0, 0.0] => [0.5, 0.5]
 [0.5, 0.0] => [1.0, 0.5]
 [0.0, 0.5] => [0.5, 1.0]
 [0.5, 0.5] => [1.0, 1.0]

This is the natural loop for numerical quadrature: for each span, map quadrature points from the reference element into the span, then evaluate the basis and the geometry there. As a sanity check, a one-point midpoint rule over the spans recovers the area of the rectangle exactly, because the mapping is affine:

refined = createBSplineRectangle(0.0, 0.0, 2.0, 1.0)uniformRefine!(refined, 3)area = sum(knotSpans(basis(refined))) do span    lo, up = toVector(lowerCorner(span)), toVector(upperCorner(span))    J = toMatrix(jacobian(refined, toVector(centerPoint(span))))    detJ = J[1, 1] * J[2, 2] - J[1, 2] * J[2, 1]    abs(detJ) * prod(up .- lo)end
2.0

elementIndex is the inverse lookup — which span contains a given parameter:

[Int(elementIndex(b2, [u])) for u in (0.1, 0.3, 0.6, 0.9)]
4-element Vector{Int64}:
 1
 2
 3
 4