API Reference
This section contains the automatically generated API reference. Each module is
documented with mkdocstrings
and the
examples included in the docstrings are executed as doctests during continuous
integration to ensure they remain correct.
Finite element primitives used by :mod:tofea
.
Element
dataclass
Base dataclass for finite elements.
Source code in tofea/elements.py
10 11 12 13 14 15 16 17 18 |
|
Q4Element
dataclass
Bases: Element
Four-node quadrilateral element.
This class provides utilities shared by all quadrilateral elements
implemented in :mod:tofea
, namely Gauss quadrature points and the
gradients of the bilinear shape functions. Having these helpers in a
common base class avoids code duplication in the concrete element
implementations and gives this abstraction a clear purpose.
Source code in tofea/elements.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
gauss_points()
staticmethod
Return the points for 2x2 Gauss quadrature.
Source code in tofea/elements.py
31 32 33 34 35 36 |
|
grad_shape_funcs(xi, eta)
Return shape function gradients for local coordinates.
Source code in tofea/elements.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
Q4Element_K
dataclass
Bases: Q4Element
Plane stress elasticity element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
e
|
float
|
Young's modulus of the material. |
1.0
|
nu
|
float
|
Poisson's ratio of the material. |
1 / 3
|
Source code in tofea/elements.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
element
cached
property
Return the 8x8 stiffness matrix for the element.
The matrix is assembled using 2x2 Gauss quadrature instead of the symbolic integration previously used. This avoids the costly SymPy computations and speeds up object construction significantly while maintaining numerical precision.
Returns:
Type | Description |
---|---|
ndarray
|
Stiffness matrix of shape |
Examples:
>>> from tofea.elements import Q4Element_K
>>> Q4Element_K().element.shape
(8, 8)
Q4Element_T
dataclass
Bases: Q4Element
Heat conductivity element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
k
|
float
|
Thermal conductivity of the material. |
1.0
|
Source code in tofea/elements.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
element
cached
property
Return the 4x4 conductivity matrix for the element.
Similar to :class:Q4Element_K
, the matrix is assembled numerically
using 2x2 Gauss quadrature. This removes the dependency on SymPy for
runtime calculations and considerably reduces initialization time.
Returns:
Type | Description |
---|---|
ndarray
|
Conductivity matrix of shape |
Examples:
>>> from tofea.elements import Q4Element_T
>>> Q4Element_T().element.shape
(4, 4)
Finite element solvers for 2D problems.
FEA2D
dataclass
Bases: ABC
Abstract base class for 2D finite element models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fixed
|
NDArray[bool_]
|
Boolean mask indicating which degrees of freedom are fixed.
Shape for thermal problems is |
required |
solver
|
str
|
Name of the backend solver to use. |
DEFAULT_SOLVER
|
dx
|
float
|
Element dimensions in |
0.5
|
dy
|
float
|
Element dimensions in |
0.5
|
Notes
Subclasses must provide the element matrix and a mapping from element degrees of freedom to system degrees of freedom.
Source code in tofea/fea2d.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
dofs
cached
property
Indices of all degrees of freedom.
e2sdofmap
cached
property
Map each element to its system DOF indices.
e2sdofmap_reshaped
cached
property
Element mapping reshaped for tensor operations.
fixdofs
cached
property
Indices of fixed degrees of freedom.
freedofs
cached
property
Indices of free degrees of freedom.
index_map
cached
property
Permutation that places free DOFs first.
keep_indices
cached
property
Indices for assembling the global matrix.
shape
property
Number of elements in :math:x
and :math:y
direction.
global_mat(x)
Assemble global matrix from element data.
Source code in tofea/fea2d.py
123 124 125 126 127 128 129 |
|
solve(x, b)
Solve K(x) u = b
for u
.
Source code in tofea/fea2d.py
131 132 133 134 135 136 137 |
|
FEA2D_K
dataclass
Bases: FEA2D
Finite element model for compliance problems.
This model solves small deformation elasticity problems using bilinear quadrilateral elements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
e
|
float
|
Young's modulus of the material. |
1.0
|
nu
|
float
|
Poisson's ratio of the material. |
1 / 3
|
Examples:
>>> import numpy as np
>>> from tofea.fea2d import FEA2D_K
>>> fixed = np.zeros((1, 1, 2), dtype=bool)
>>> fem = FEA2D_K(fixed, e=210e9, nu=0.3)
>>> fem.element.shape
(8, 8)
Source code in tofea/fea2d.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
dofmap
cached
property
Mapping of element DOFs to system DOFs.
element
cached
property
Element stiffness matrix.
compliance(x, b)
Computes the compliance objective using a highly efficient self-adjoint pathway.
This method is optimized for compliance minimization and avoids a redundant
adjoint solve during gradient computation. For constructing arbitrary objective
functions based on the displacement field, use the :meth:displacement
method
instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
Density field of shape |
required |
b
|
ndarray
|
Load array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The scalar compliance value. |
Source code in tofea/fea2d.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
displacement(x, b)
Return displacement field for density x
and load b
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
Density field of shape |
required |
b
|
ndarray
|
Load array of shape |
required |
This
|
|
required | |
suitable
|
|
required | |
objective
|
|
required | |
significantly
|
|
required |
Source code in tofea/fea2d.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
FEA2D_T
dataclass
Bases: FEA2D
Finite element model for heat conduction problems.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
k
|
float
|
Thermal conductivity of the material. |
1.0
|
Examples:
>>> import numpy as np
>>> from tofea.fea2d import FEA2D_T
>>> fixed = np.zeros((1, 1), dtype=bool)
>>> fem = FEA2D_T(fixed, k=200.0)
>>> fem.element.shape
(4, 4)
Source code in tofea/fea2d.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
dofmap
cached
property
Mapping of element DOFs to system DOFs.
element
cached
property
Element conductivity matrix.
temperature(x, b)
Return temperature field for density x
and load b
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
Density field of shape |
required |
b
|
ndarray
|
Heat load array of shape |
required |
This
|
|
required | |
suitable
|
|
required | |
is
|
|
required | |
method
|
|
required |
Source code in tofea/fea2d.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
|
thermal_compliance(x, b)
Computes the thermal compliance objective using a highly efficient self-adjoint pathway.
This method is optimized for thermal compliance minimization and avoids a
redundant adjoint solve during gradient computation. For constructing arbitrary
objective functions based on the temperature field, use the :meth:temperature
method instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
Density field of shape |
required |
b
|
ndarray
|
Heat load array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The scalar thermal compliance value. |
Source code in tofea/fea2d.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
Helper utilities for defining boundary conditions and loads.
BoundaryConditions
dataclass
Container for fixed degrees of freedom and loads.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shape
|
tuple[int, int]
|
Number of elements in |
required |
dof_dim
|
int
|
Degrees of freedom per node. |
1
|
Examples:
>>> bc = BoundaryConditions((1, 1))
>>> bc.fix_edge("left")
>>> bool(bc.fixed[0, 0])
True
>>> bc.apply_point_load(1, 1, 2.0)
>>> float(bc.load[1, 1])
2.0
Source code in tofea/boundary_conditions.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
apply_point_load(node_x, node_y, load_vector)
Apply a load vector at a single node.
Source code in tofea/boundary_conditions.py
73 74 75 76 77 78 79 80 81 82 83 |
|
apply_uniform_load_on_edge(edge, load_vector)
Apply the same load to all nodes on an edge.
Source code in tofea/boundary_conditions.py
85 86 87 88 89 90 91 92 93 94 |
|
fix_edge(edge)
Fix all nodes along an edge.
Source code in tofea/boundary_conditions.py
65 66 67 68 69 70 71 |
|
Autograd primitives used by the finite element routines.
solve_and_compute_self_adjoint_objective(entries, indices, rhs, solver)
Solve K u = b
and return the self-adjoint objective and solution.
Source code in tofea/primitives.py
145 146 147 148 149 150 151 152 153 154 155 156 |
|
solve_and_compute_self_adjoint_objective_vjp(ans, entries, indices, rhs, solver)
Reverse-mode derivative for solve_and_compute_self_adjoint_objective
.
Source code in tofea/primitives.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
solve_coo(entries, indices, rhs, solver)
Solve a sparse linear system in COO format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entries
|
array - like
|
Non-zero matrix entries. |
required |
indices
|
tuple[array - like, array - like]
|
Row and column indices for |
required |
rhs
|
array - like
|
Right-hand side vector. |
required |
solver
|
Solver
|
Factorization backend used to solve the system. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Solution vector. |
Examples:
>>> import numpy as np
>>> from scipy.sparse import coo_matrix
>>> from tofea.primitives import solve_coo
>>> from tofea.solvers import get_solver
>>> m = coo_matrix([[4, 1], [1, 3]])
>>> b = np.array([1, 2])
>>> solve_coo(m.data, (m.row, m.col), b, get_solver("SuperLU"))
array([0.09090909, 0.63636364])
Source code in tofea/primitives.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
solve_coo_b_jvp(g, x, entries, indices, rhs, solver)
Forward-mode derivative of :func:solve_coo
with respect to rhs
.
Source code in tofea/primitives.py
94 95 96 97 98 99 100 101 102 103 104 |
|
solve_coo_b_vjp(ans, entries, indices, rhs, solver)
Reverse-mode derivative of :func:solve_coo
for rhs
.
Source code in tofea/primitives.py
127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
solve_coo_entries_jvp(g, x, entries, indices, rhs, solver)
Forward-mode derivative of :func:solve_coo
with respect to entries
.
Source code in tofea/primitives.py
80 81 82 83 84 85 86 87 88 89 90 91 |
|
solve_coo_entries_vjp(ans, entries, indices, rhs, solver)
Reverse-mode derivative of :func:solve_coo
for entries
.
Source code in tofea/primitives.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
Linear system solver abstractions.
Solver
Bases: ABC
Abstract interface for linear solvers.
Implementations typically perform a two phase factorization of a sparse
matrix: a symbolic analysis that depends only on the sparsity pattern and a
numerical factorization that depends on the actual values. factor_full
is responsible for both steps while refactor_numerical
reuses the
symbolic analysis and only updates the numeric values.
Source code in tofea/solvers.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
factor_full(m)
abstractmethod
Perform a complete (symbolic and numeric) factorization of m
.
Source code in tofea/solvers.py
23 24 25 |
|
refactor_numerical(m)
abstractmethod
Update the numeric factorization of m
using a cached symbolic analysis.
Source code in tofea/solvers.py
27 28 29 |
|
SuperLU
Bases: Solver
scipy.sparse.linalg.splu
wrapper.
Source code in tofea/solvers.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
__init__(**options)
Create a new SuperLU
solver instance.
Source code in tofea/solvers.py
41 42 43 44 |
|
clear()
Remove cached factorization and sparsity pattern.
Source code in tofea/solvers.py
62 63 64 65 |
|
factor_full(m)
Compute the complete factorization of m
.
Source code in tofea/solvers.py
46 47 48 |
|
refactor_numerical(m)
Update numeric values by re-factorizing m
.
scipy
's splu
does not expose symbolic/numeric separation, so we
simply recompute the full factorization.
Source code in tofea/solvers.py
50 51 52 53 54 55 56 |
|
solve(rhs, transpose=False)
Solve the linear system.
Source code in tofea/solvers.py
58 59 60 |
|
get_solver(solver)
Return a solver instance by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
solver
|
str
|
Name of the solver implementation. Currently only |
required |
Examples:
>>> from tofea.solvers import get_solver, Solver
>>> s = get_solver("SuperLU")
>>> isinstance(s, Solver)
True
Source code in tofea/solvers.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|