MFEM + PyMFEM (FEM library)
This repository provides Python binding for MFEM. MFEM is a high performance parallel finite element method (FEM) library (http://mfem.org/).
Installer (setup.py) builds both MFEM and binding together. By default, "pip install mfem" downloads and builds the serial version of MFEM and PyMFEM. Additionally, the installer supports building MFEM with specific options together with other external libraries, including MPI version.
Install
Using pip (Serial MFEM)
pip install mfem # binary install is available only on linux platforms (Py38-312)
Build with additional features (MPI, GPU, GPU-Hypre, GSLIB, SuiteSparse, libCEED, LAPACK)
The setup script accept various options. Download the package manually and run the script. Examples below downloads and build parallel version of MFEM library (linked with Metis and Hypre) and installs under /mfem. See INSTALL for various other options
Download source and build
$ git clone https://github.com/mfem/PyMFEM.git
# Build it from local source with MPI
$ pip install ./ -C"with-parallel=Yes" --verbose
Cleaning
$ python setup.py clean --all # clean external dependencies + wrapper code
Run test
cd test
python test_examples.py -serial
Usage
This example (modified from ex1.cpp) solves the Poisson equation,
$$\nabla \cdot (\alpha \nabla u) = f$$
in a square and plots the result using matplotlib.
Use the badge above to open this in Colab.
import mfem.ser as mfem
# Create a square mesh
mesh = mfem.Mesh(10, 10, "TRIANGLE")
# Define the finite element function space
fec = mfem.H1_FECollection(1, mesh.Dimension()) # H1 order=1
fespace = mfem.FiniteElementSpace(mesh, fec)
# Define the essential dofs
ess_tdof_list = mfem.intArray()
ess_bdr = mfem.intArray([1]*mesh.bdr_attributes.Size())
fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list)
# Define constants for alpha (diffusion coefficient) and f (RHS)
alpha = mfem.ConstantCoefficient(1.0)
rhs = mfem.ConstantCoefficient(1.0)
"""
Note
-----
In order to represent a variable diffusion coefficient, you
must use a numba-JIT compiled function. For example:
>>> @mfem.jit.scalar
>>> def alpha(x):
>>> return x+1.0
"""
# Define the bilinear and linear operators
a = mfem.BilinearForm(fespace)
a.AddDomainIntegrator(mfem.DiffusionIntegrator(alpha))
a.Assemble()
b = mfem.LinearForm(fespace)
b.AddDomainIntegrator(mfem.DomainLFIntegrator(rhs))
b.Assemble()
# Initialize a gridfunction to store the solution vector
x = mfem.GridFunction(fespace)
x.Assign(0.0)
# Form the linear system of equations (AX=B)
A = mfem.OperatorPtr()
B = mfem.Vector()
X = mfem.Vector()
a.FormLinearSystem(ess_tdof_list, x, b, A, X, B)
print("Size of linear system: " + str(A.Height()))
# Solve the linear system using PCG and store the solution in x
AA = mfem.OperatorHandle2SparseMatrix(A)
M = mfem.GSSmoother(AA)
mfem.PCG(AA, M, B, X, 1, 200, 1e-12, 0.0)
a.RecoverFEMSolution(X, b, x)
# Extract vertices and solution as numpy arrays
verts = mesh.GetVertexArray()
sol = x.GetDataArray()
# Plot the solution using matplotlib
import matplotlib.pyplot as plt
import matplotlib.tri as tri
triang = tri.Triangulation(verts[:,0], verts[:,1])
fig, ax = plt.subplots()
ax.set_aspect('equal')
tpc = ax.tripcolor(triang, sol, shading='gouraud')
fig.colorbar(tpc)
plt.show()
License
PyMFEM is licensed under BSD-3 license. All new contributions must be made under this license. See License for details.
Please refer the developers' web sites for the external libraries
Release files for mfem 4.8.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mfem-4.8.0.1.tar.gz | 437.5 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| mfem-4.8.0.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl | CPython 3.13 | CPython 3.13 | Linux glibc 2.17+ x86-64 | Details |
| mfem-4.8.0.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl | CPython 3.12 | CPython 3.12 | Linux glibc 2.17+ x86-64 | Details |
| mfem-4.8.0.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl | CPython 3.11 | CPython 3.11 | Linux glibc 2.17+ x86-64 | Details |
| mfem-4.8.0.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl | CPython 3.10 | CPython 3.10 | Linux glibc 2.17+ x86-64 | Details |
| mfem-4.8.0.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl | CPython 3.9 | CPython 3.9 | Linux glibc 2.17+ x86-64 | Details |
Total release size:388.0 MB
Release files / mfem-4.8.0.1.tar.gz
| Download URL | mfem-4.8.0.1.tar.gz |
|---|---|
| Size | 437.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
138d4784789fec4592bc90a1046cc2edafba078c63fad3e3ff5e3e9ce6267ece
|
|
BLAKE2b-256 checksum How to use checksums |
32b1e009b4e6e7fed643fc6f336e081562fd796bf52a1372f96a6aed3e0eb30f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|
Release files / mfem-4.8.0.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | mfem-4.8.0.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 78.2 MB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
7274d203ed97d7f846d21e0811c70404fadcf9a4b61e1403b0043645b81930f8
|
|
BLAKE2b-256 checksum How to use checksums |
032a5a8a1556664d50b66b5f7c99dfc2beaeca110476667d07c782e9b243ec81
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Release files / mfem-4.8.0.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | mfem-4.8.0.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 77.9 MB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
4fb97b5969d3587261a7043bef6ab9196a00174e7d16b92c95d42a63c6fd55b2
|
|
BLAKE2b-256 checksum How to use checksums |
6f0018343b4aea35ab892dde34b0ad0864ca31156474bdeb9ac72c766a4a7914
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.11
|
Release files / mfem-4.8.0.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | mfem-4.8.0.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 77.5 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
e689751c308e718151c80d7e431aed2666cc6205938ff77ded9b9eba8cd70fd1
|
|
BLAKE2b-256 checksum How to use checksums |
e497ac8b3909483ef3a6ee5036893ab51464434c36b14e8b0ba029feb4c16945
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.13
|
Release files / mfem-4.8.0.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | mfem-4.8.0.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 77.0 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
9eddd63e0224d628e6b7aeeec423b4ea4b3cd8f0af9c8a5dfec64ff0a05a578a
|
|
BLAKE2b-256 checksum How to use checksums |
bbaa5c625d9f2261ec5bf4298961f687651d6c906d1b7500e8ddb9ac393a9841
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.18
|
Release files / mfem-4.8.0.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | mfem-4.8.0.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 77.0 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
7c86f210798348aa4b41a959dce89370fcbad47ff52f3eec013ed974b032e49a
|
|
BLAKE2b-256 checksum How to use checksums |
d60b46fb484a7156356c8481ebd835b21b16cee2e196d866a52590db6086f9e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|