pysdic.fem.assemble_displacement_jacobian_matrix#
- assemble_displacement_jacobian_matrix(property_gradients, shape_functions, connectivity_array, element_indices, *, n_vertices=None, sparse=False, allow_unlocated_points=False)[source]#
Assemble the displacement Jacobian matrix that maps nodal displacements to the variations of a property (e.g. gray levels) at integration points.
In a space of dimension \(E\) with a mesh constituted of \(N_{e}\) elements and \(N_{v}\) vertices, the mesh is composed of \(K\)-dimensional elements (with \(K \leq E\)) defined by \(N_{vpe}\) vertices for each element.
Lets consider \(M\) integration points located within elements and a property with \(P\) components (e.g. \(P = 1\) for gray levels, \(P = 3\) for RGB images). The gradients of the property with respect to the global coordinates at the integration points are given as an array of shape \((M, P, E)\).
The objective is to build the Jacobian operator \(\mathbf{J} \in \mathbb{R}^{(M \cdot P) \times (N_{v} \cdot E)}\) such that:
\[\mathbf{J} \, dU = dR\]where \(dU \in \mathbb{R}^{N_{v} \cdot E}\) is the vector of nodal displacements and \(dR \in \mathbb{R}^{M \cdot P}\) is the resulting variation of the property (residual) at the integration points.
Note
For (\(P \neq 1\)), the lines are grouped by property first (i.e., all integration points for property 1, followed by all integration points for property 2, etc.). Thus the residual \(R\) associated with the output must be constructed accordingly.
\[\begin{split}R = \begin{bmatrix} R_{1}^{1} \\ R_{2}^{1} \\ \vdots \\ R_{M}^{1} \\ R_{1}^{2} \\ R_{2}^{2} \\ \vdots \\ R_{M}^{2} \\ \vdots \\ R_{1}^{P} \\ R_{2}^{P} \\ \vdots \\ R_{M}^{P} \end{bmatrix}\end{split}\]where \(R_{i}^{j}\) represents the residual at integration point \(i\) for property \(j\).
residual = numpy.random.rand(M, P) residual_vector = residual.flatten(order='F') # Column-major flattening
Note
The columns of the operator correspond to the nodal displacements organized by direction first (i.e., all vertices for direction 1, followed by all vertices for direction 2, etc.). Thus the displacement vector \(dU\) must be constructed accordingly.
\[\begin{split}dU = \begin{bmatrix} dU_{1}^{x} \\ dU_{2}^{x} \\ \vdots \\ dU_{N_{v}}^{x} \\ dU_{1}^{y} \\ dU_{2}^{y} \\ \vdots \\ dU_{N_{v}}^{y} \\ \vdots \\ dU_{1}^{E} \\ dU_{2}^{E} \\ \vdots \\ dU_{N_{v}}^{E} \end{bmatrix}\end{split}\]where \(dU_{i}^{j}\) represents the displacement of vertex \(i\) in direction \(j\).
displacement_vector = numpy.random.rand(N_v * E) # resulting vector of size N_v * E displacement = displacement_vector.reshape((N_v, E), order='F') # Column-major reshaping
Note
Inputs
property_gradientsandshape_functionswill be converted tonumpy.float64.Inputs
connectivity_arrayandelement_indiceswill be converted tonumpy.int64.
Note
The connectivity is validated (non-negative vertex indices lower than the number of vertices, distinct vertices in each element) and the element indices are checked against the number of elements.
Important
The \(P\) rows of a point are zeros, i.e. the point has a zero weight:
if its property gradients are not finite (e.g. a point outside the image): in the sparse matrix, its entries are kept as explicit zeros, so that the structure of the matrix only depends on the connectivity and on the element indices (the same for all the gradients);
if its element index is
-1(only accepted withallow_unlocated_points=True): its rows have no entry in the sparse matrix.
A non-finite shape function value of a located point raises a
ValueError.- Parameters:
property_gradients (ArrayLike) – An array of shape \((M, P, E)\) containing the gradients of the property with respect to the global coordinates \((x, y, z, ...)\) at the \(M\) integration points. If a 2D-array of shape \((M, E)\) is provided, it will be treated as a single-component property of shape \((M, 1, E)\).
shape_functions (ArrayLike) – An array of shape \((M, N_{vpe})\) containing the shape function values evaluated at the \(M\) points for the \(N_{vpe}\) vertices of the element.
connectivity_array (ArrayLike) – An array of shape \((N_{e}, N_{vpe})\) defining the connectivity of the elements in the mesh, where each row contains the indices of the vertices that form an element.
element_indices (ArrayLike) – An array of shape \((M,)\) containing the indices of each element corresponding to the \(M\) integration points.
n_vertices (Optional[Integral], optional (default:
None)) – The total number of vertices \(N_{v}\) in the mesh. If not provided, it will be inferred as the maximum vertex index inconnectivity_arrayplus one.sparse (bool, optional (default:
False)) – If set to True, the function will return ascipy.sparse.csr_matrixrepresentation of the operator, built directly in the CSR format (sorted column indices, no duplicates,numpy.int32indices when possible).allow_unlocated_points (bool, optional (default:
False)) – If False (default), any negative element index raises anIndexError. If True, the element index-1marks a point not located in any element of the mesh, and such a point is treated as invalid (see above). Other negative indices always raise anIndexError.
- Returns:
displacement_jacobian_matrix – The displacement Jacobian operator of shape \((M \cdot P, N_{v} \cdot E)\) mapping nodal displacements to property variations at integration points.
- Return type:
Union[numpy.ndarray, scipy.sparse.csr_matrix]
Notes
The displacement of a point \(m\) of the element is interpolated from the nodal displacements with the shape functions: \(u(m) = \sum_{v} N_{v}(m) \, U_{v}\). At first order, the variation of the component \(p\) of the property at this point is:
\[dR_{m}^{p} = \nabla_X I_{p}(m) \cdot u(m) = \sum_{e=1}^{E} \sum_{v=1}^{N_{v}} \frac{\partial I_{p}}{\partial x_{e}}(m) \, N_{v}(m) \, dU_{v}^{e}\]So the coefficient of the operator at row \(p \cdot M + m\) and column \(e \cdot N_{v} + v\) is:
\[\mathbf{J}[p \cdot M + m, \; e \cdot N_{v} + v] = \frac{\partial I_{p}}{\partial x_{e}}(m) \, N_{v}(m)\]In other words, the operator is made of \(P \times E\) blocks, each block being the shape function matrix \(\mathcal{N}\) (see
pysdic.fem.assemble_shape_function_matrix()) scaled row by row by one component of the gradients:\[\begin{split}\mathbf{J} = \begin{bmatrix} \text{diag}(\partial_{x_1} I_{1}) \, \mathcal{N} & \cdots & \text{diag}(\partial_{x_E} I_{1}) \, \mathcal{N} \\ \vdots & \ddots & \vdots \\ \text{diag}(\partial_{x_1} I_{P}) \, \mathcal{N} & \cdots & \text{diag}(\partial_{x_E} I_{P}) \, \mathcal{N} \end{bmatrix}\end{split}\]See also
pysdic.fem.assemble_shape_function_matrixTo assemble the shape function matrix \(\mathcal{N}\) used to build each block of the operator.
pysdic.fem.compute_shape_functionsTo compute the shape functions at given natural coordinates within elements.
Examples
Lets construct a simple 2D mesh with 3 integration points, a 2-components property, and check that the operator maps a nodal displacement to the expected variation of the property.
1import numpy 2from pysdic.fem import ( 3 assemble_displacement_jacobian_matrix, 4 compute_triangle_3_shape_functions, 5) 6 7connectivity_array = numpy.array( 8 [[0, 1, 2], 9 [0, 2, 3]] 10) 11N_v, E = 4, 2 12 13natural_coordinates = numpy.array( 14 [[0.2, 0.3], 15 [0.6, 0.2], 16 [0.0, 0.0]] 17) 18element_indices = numpy.array([0, 1, -1]) # Last point outside the mesh 19shape_functions = compute_triangle_3_shape_functions(natural_coordinates) 20 21# Gradients of a 2-components property, shape (M, P, E) = (3, 2, 2) 22property_gradients = numpy.array( 23 [[[1.0, 0.0], [0.0, 2.0]], 24 [[1.0, 1.0], [3.0, 0.0]], 25 [[5.0, 5.0], [5.0, 5.0]]] 26) 27 28jacobian = assemble_displacement_jacobian_matrix( 29 property_gradients=property_gradients, 30 shape_functions=shape_functions, 31 connectivity_array=connectivity_array, 32 element_indices=element_indices, 33 n_vertices=N_v, 34 allow_unlocated_points=True, # Accept the -1 of the last point 35) 36print(f"jacobian (shape={jacobian.shape}):") 37print(jacobian) 38 39# Uniform translation (1, 0) of all the vertices 40displacement = numpy.zeros((N_v, E)) 41displacement[:, 0] = 1.0 42variation = jacobian @ displacement.flatten(order='F') # (M * P,) 43print("variation (M, P):") 44print(variation.reshape((3, 2), order='F'))
jacobian (shape=(6, 8)): [[0.5 0.2 0.3 0. 0. 0. 0. 0. ] [0.2 0. 0.6 0.2 0.2 0. 0.6 0.2] [0. 0. 0. 0. 0. 0. 0. 0. ] [0. 0. 0. 0. 1. 0.4 0.6 0. ] [0.6 0. 1.8 0.6 0. 0. 0. 0. ] [0. 0. 0. 0. 0. 0. 0. 0. ]] variation (M, P): [[1. 0.] [1. 3.] [0. 0.]]
For a uniform translation, the variation is the \(x\)-component of the gradients at each valid point, and zero at the invalid point.