scikit-fem
is a lightweight Python 3.6+ library for performing finite element
assembly. Its main purpose
is the transformation of bilinear forms into sparse matrices and linear forms
into vectors. The library supports triangular, quadrilateral, tetrahedral and
hexahedral meshes as well as one-dimensional problems.
The library fills an important gap in the spectrum of finite element codes. The library is lightweight meaning that it has minimal dependencies. It contains no compiled code meaning that it's easy to install and use on all platforms that support NumPy. Despite being fully interpreted, the code has a reasonably good performance.
Forms are defined using an intuitive syntax:
from skfem import *
from skfem.helpers import dot, grad
@BilinearForm
def laplace(u, v, w):
return dot(grad(u), grad(v))
Meshes can be initialized manually, loaded from external files using meshio, or created with the help of special constructors:
import numpy as np
mesh = MeshLine(np.array([0.0, 0.5, 1.0]))
mesh = MeshTri.load("docs/examples/square.msh")
mesh = MeshTet.init_tensor(*((np.linspace(0, 1, 60),) * 3))
We support many common finite elements. Below the stiffness matrix is assembled using second-order tetrahedra:
basis = InteriorBasis(mesh, ElementTetP2())
A = laplace.assemble(basis) # type: scipy.sparse.csr_matrix
The matrix A
has 1.5 million rows/columns and took only a few seconds to
assemble!
More examples can be found in the source code distribution.
The project is documented using Sphinx. A recent version of the documentation can be found from Github pages.
The most recent release can be installed simply by pip install scikit-fem
.
For more cutting edge features, you can clone this repository.
This project was started while working under a grant from the Finnish Cultural Foundation. The approach used in the finite element assembly has been inspired by the work of A. Hannukainen and M. Juntunen.
We are happy to welcome any contributions to the library. Reasonable projects for first timers include:
- Filing out a bug report.
- Writing an example
- Improving the tests.
- Finding typos in the documentation.
By contributing code to scikit-fem, you are agreeing to release it under BSD-3-Clause, see LICENSE.md.
The library has been used in the preparation of the following scientific works:
- Gustafsson, T., Stenberg, R., & Videman, J. (2020). On Nitsche's method for elastic contact problems. SIAM Journal on Scientific Computing, 42(2), B425–B446. arXiv preprint arXiv:1902.09312.
- Gustafsson, T., Stenberg, R., & Videman, J. (2019). Nitsche's Master-Slave Method for Elastic Contact Problems. arXiv:1912.08279.
- McBain, G. D., Mallinson, S. G., Brown, B. R., Gustafsson, T. (2019). Three ways to compute multiport inertance. The ANZIAM Journal, 60, C140–C155. Open access.
- Gustafsson, T., Stenberg, R., & Videman, J. (2019). Error analysis of Nitsche's mortar method. Numerische Mathematik, 142(4), 973–994. Open access.
- Gustafsson, T., Stenberg, R., & Videman, J. (2019). Nitsche's method for unilateral contact problems. Port. Math. 75, 189–204. arXiv preprint arXiv:1805.04283.
- Gustafsson, T., Stenberg, R. & Videman, J. (2018). A posteriori estimates for conforming Kirchhoff plate elements. SIAM Journal on Scientific Computing, 40(3), A1386–A1407. arXiv preprint arXiv:1707.08396.
- Gustafsson, T., Rajagopal, K. R., Stenberg, R., & Videman, J. (2018). An adaptive finite element method for the inequality-constrained Reynolds equation. Computer Methods in Applied Mechanics and Engineering, 336, 156–170. arXiv preprint arXiv:1711.04274.
- Gustafsson, T., Stenberg, R., & Videman, J. (2018). A stabilised finite element method for the plate obstacle problem. BIT Numerical Mathematics, 59(1), 97–124. arXiv preprint arXiv:1711.04166.
- Gustafsson, T., Stenberg, R., & Videman, J. (2017). Nitsche’s Method for the Obstacle Problem of Clamped Kirchhoff Plates. In European Conference on Numerical Mathematics and Advanced Applications, 407–415. Springer.
- Gustafsson, T., Stenberg, R., & Videman, J. (2017). A posteriori analysis of classical plate elements. Rakenteiden Mekaniikka, 50(3), 141–145. Open access.
In case you want to cite the library, you can use the DOI provided by Zenodo.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
Dofs.__or__
andDofs.__add__
, for merging degree-of-freedom sets (i.e.Dofs
objects) using|
and+
operatorsDofs.drop
andDofs.keep
, for further filtering the degree-of-freedom sets
Mesh.__add__
, for merging meshes using+
operator: duplicated nodes are joinedElementHexS2
, a 20-node quadratic hexahedral serendipity elementElementLineMini
, MINI-element for one-dimensional mesh
Mesh3D.boundary_edges
was broken in case of hexahedral meshesskfem.utils.project
did not work forElementGlobal
MeshQuad._splitquads
aliased asMeshQuad.to_meshtri
: should not be private
ElementTetMini
, MINI-element for tetrahedral mesh
Mesh3D.boundary_edges
incorrectly returned all edges where both nodes are on the boundary
- New-style form constructors
BilinearForm
,LinearForm
, andFunctional
skfem.io.json
for serialization of meshes to/from json-filesElementLinePp
, p-th order one-dimensional elementsElementQuadP
, p-th order quadrilateral elementsElementQuadDG
for transforming quadrilateral H1 elements to DG elementsElementQuadBFS
, Bogner-Fox-Schmit element for biharmonic problemsElementTriMini
, MINI-element for Stokes problemsElementComposite
for using multiple elements in one bilinear formElementQuadS2
, quadratic Serendipity elementElementLineHermite
, cubic Hermite element for Euler-Bernoulli beamsMesh.define_boundary
for defining named boundariesBasis.find_dofs
for finding degree-of-freedom indicesMesh.from_basis
for defining high-order meshesBasis.split
for splitting multicomponent solutionsMortarMapping
with basic support for mortar methods in 2DBasis
constructors now acceptquadrature
keyword argument for specifying a custom quadrature rule
- Old-style form constructors
bilinear_form
,linear_form
, andfunctional
.
Basis.interpolate
returnsDiscreteField
objects instead of ndarray tuplesBasis.interpolate
works now properly for vectorial and high-order elements by interpolating all components and higher order derivativesForm.assemble
accepts now any keyword arguments (with typeDiscreteField
) that are passed over to the forms- Renamed
skfem.importers
toskfem.io
- Renamed
skfem.models.helpers
toskfem.helpers
skfem.utils.solve
will now expand also the solutions of eigenvalue problems
- Additional keyword arguments to
skfem.utils.solve
get passed on to linear solvers
- Made
skfem.visuals.matplotlib
Python 3.6 compatible
- Renamed
GlobalBasis
toBasis
- Moved all
Mesh.plot
andMesh.draw
methods toskfem.visuals
module - Made matplotlib an optional dependency