pysdic.fem.assemble_property_projection#
- assemble_property_projection(point_values, shape_function_matrix, point_weights=None, *, default=nan, return_unaffected=False)[source]#
Project a property defined at integration points within elements back to the nodes of a mesh using a precomputed shape function matrix.
projected_values[i]contains the projected property at the node \(i\).Note
Inputs
point_values,shape_function_matrixandpoint_weightswill be converted tonumpy.float64.Any
scipy.sparsematrix is accepted and converted to the CSR format.
Warning
No tests are performed to check if the inputs array are consistent. Only shapes are validated.
Important
The points with a zero weight, non-finite values (e.g. NaN for points outside the mesh) or a non-finite row in
shape_function_matrixare ignored. The nodes that are not affected by any remaining point are set todefaultand can be retrieved withreturn_unaffected=True.- Parameters:
point_values (ArrayLike) – An array of shape \((N_{p}, P)\) containing the property values defined at the \(N_{p}\) integration points. If 1D-array is provided, it will be treated as a single-component property of shape \((N_{p}, 1)\).
shape_function_matrix (Union[ArrayLike, scipy.sparse.csr_matrix, scipy.sparse.coo_matrix]) – A precomputed shape functions matrix of shape \((N_{p}, N_{v})\) where \(N_{v}\) is the number of nodes in the mesh.
point_weights (Optional[ArrayLike], optional (default:
None)) – An array of shape \((N_{p},)\) containing the non-negative weights associated with each integration point. If not provided, all weights will be assumed to be equal to one.default (Real, optional (default:
numpy.nan)) – The value assigned to the nodes that are not affected by any point (no information to project).return_unaffected (bool, optional (default:
False)) – If set to True, the function will return a tuple containing the projected values and a boolean mask indicating which nodes were unaffected by any integration point.
- Returns:
projected_values (numpy.ndarray) – An array of shape \((N_{v}, P)\) containing the projected property values at the nodes of the mesh.
unaffected_mask (numpy.ndarray) – Only returned if
return_unaffectedis True. A boolean array of shape \((N_{v},)\) indicating which nodes were unaffected by any integration point (True for unaffected nodes). These nodes are set todefault.
- Return type:
Notes
In a space of dimension \(E\) with a mesh constituted of \(N_{e}\) elements and \(N_{v}\) nodes, the mesh is composed of \(K\)-dimensional elements (with \(K \leq E\)) defined by \(N_{vpe}\) nodes for each element.
The evaluation of a property at the given integration points is represented by an array of shape \((N_{p}, P)\) and is performed as:
\[P_{points} = N_f \cdot P_{nodes} \Leftrightarrow P(\xi, \eta, \zeta, ...) = \sum_{i=1}^{N_{vpe}} N_i(\xi, \eta, \zeta, ...) P_i\]where \(N_f\) is the shape functions matrix assembled for all \(N_{p}\) integration points, \(P_{points}\) is the property array at the integration points, and \(P_{nodes}\) is the property array at the mesh nodes.
To project back to the nodes, we solve the following weighted least squares problem:
\[\hat{P}_{nodes} = \underset{P_{nodes}}{\text{argmin}} \; \| W^{1/2} (P_{points} - N_f \cdot P_{nodes}) \|^2\]where \(W\) is a diagonal matrix of weights associated with each integration point. When \(N_f^T W N_f\) is invertible, the solution is \(\hat{P}_{nodes} = (N_f^T W N_f)^{-1} N_f^T W P_{points}\).
The problem is solved directly on \(W^{1/2} N_f\) (without forming the normal equations, whose condition number is squared) with
numpy.linalg.lstsqfor dense matrices andscipy.sparse.linalg.lsqrfor sparse matrices. If some nodes are not sufficiently constrained (e.g. fewer points than nodes in an element), the minimum-norm solution is returned instead of raising an error.Demonstration#
Lets consider the following notations:
\(N_v\) the number of nodes in the mesh.
\(N_{p}\) the number of integration points.
\(P\) the number of property components (e.g., 1 for scalar properties, 3 for vector properties).
\(P_{points}\) the property array at the integration points with shape \((N_{p}, P)\).
\(P_{nodes}\) the property array at the mesh nodes with shape \((N_v, P)\).
\(N_f\) the shape functions matrix with shape \((N_{p}, N_v)\).
\(W\) the diagonal matrix of weights with shape \((N_{p}, N_{p})\).
We know that:
\[P_{points} = N_f \cdot P_{nodes}\]To project back to the vertices of the mesh, we want to minimize the following weighted least squares problem:
\[\hat{P}_{nodes} = \underset{P_{nodes}}{\text{argmin}} \; \frac{1}{2}\sum_{i=1}^{N_p} w_i \| P_{points,i} - N_{f,i} \cdot P_{nodes} \|^2\]\[\hat{P}_{nodes} = \underset{P_{nodes}}{\text{argmin}} \; \frac{1}{2} (P_{points} - N_f \cdot P_{nodes})^T W (P_{points} - N_f \cdot P_{nodes})\]The gradient of the objective function with respect to \(P_{nodes}\) is:
\[- N_f^T W (P_{points} - N_f \cdot P_{nodes})\]To find the optimal solution, we set the gradient to zero and solve for \(P_{nodes}\):
\[N_f^T W P_{points} = N_f^T W N_f \cdot P_{nodes}\]So if we denote \(A = N_f^T W N_f\) invertible, we have:
\[\hat{P}_{nodes} = A^{-1} N_f^T W P_{points}\]See also
pysdic.fem.compute_shape_function_matrixTo compute the shape function matrix from mesh and integration point information.
pysdic.fem.compute_property_projectionTo project properties defined at integration points back to the nodes of the mesh using mesh and integration point information.
pysdic.fem.assemble_property_interpolationTo interpolate properties defined at the nodes of the mesh to integration points within elements.
Examples
Lets construct a simple 2D mesh, interpolate a scalar property at 4 points per element and project it back to the nodes.
1import numpy 2from pysdic.fem import ( 3 compute_shape_function_matrix, 4 assemble_property_projection, 5) 6 7connectivity_array = numpy.array( 8 [[0, 1, 2], 9 [0, 2, 3]] 10) 11N_e = connectivity_array.shape[0] 12 13nodal_values = numpy.array([10.0, 20.0, 30.0, 40.0]) # Scalar property 14 15# Define natural coordinates for interpolation (4 points per element) 16natural_coordinates = numpy.array( 17 [[0.3, 0.3], 18 [0.2, 0.5], 19 [0.5, 0.2], 20 [0.1, 0.1]] 21) 22natural_coordinates = numpy.vstack([natural_coordinates] * N_e) 23element_indices = numpy.repeat(numpy.arange(N_e), 4) 24 25shape_function_matrix = compute_shape_function_matrix( 26 connectivity_array=connectivity_array, 27 element_type='triangle_3', 28 natural_coordinates=natural_coordinates, 29 element_indices=element_indices, 30) 31 32# Values at the points, then projection back to the nodes 33point_values = shape_function_matrix @ nodal_values 34 35projected_values = assemble_property_projection( 36 point_values=point_values, 37 shape_function_matrix=shape_function_matrix 38) 39 40print(f"projected values (shape={projected_values.shape}):") 41print(projected_values)
projected values (shape=(4, 1)): [[10.] [20.] [30.] [40.]]