Skip to main content

MFEM + PyMFEM (finite element method library)

Project description

PyMFEM (MFEM Python wrapper)

This package (PyMFEM) is Python wrapper for the MFEM high performance parallel finite element metod library.(

Installer downloads a couple of external libraries and build them. By default, "pip install mfem" downloads and builds the serial version of MFEM and PyMFEM. See more detail below for other configurations


pip install mfem --no-binary mfem   # install serial MFEM + wrapper

The setup script accept various options. TO use it, please donwload the package and run the script manually. For example, this below download and build parallel version of MFEM libray (linked with Metis and Hypre) and install under <prefix>/mfem

$ pip3 download mfem
(expand tar.gz file and move to the downloaded directory)
$ python install --with-parallel # it download and build metis/hypre/mfem

Choosing compiler

$ python install --with-parallel --CC=icc --CXX=icpc --MPICC=mpiicc --MPICXX=mpiicpc

For other configurations, see docs/install.txt or help

$ python install --help


Here is an example to solve div(grad(f)) = 1 in squre and to plot the result with matplotlib (modifed from ex1.cpp)

import mfem.ser as mfem

# create sample mesh for squre shape
mesh = mfem.Mesh(10, 10, "TRIANGLE")

# create finite element function space
fec = mfem.H1_FECollection(1, mesh.Dimension())   # H1 order=1
fespace = mfem.FiniteElementSpace(mesh, fec)      

ess_tdof_list = mfem.intArray()
ess_bdr = mfem.intArray([1]*mesh.bdr_attributes.Size())
fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list)

# constant coefficient 
one = mfem.ConstantCoefficient(1.0)

# define Bilinear and Linear operator
a = mfem.BilinearForm(fespace)
b = mfem.LinearForm(fespace)

# create gridfuction, which is where the solution vector is stored
x = mfem.GridFunction(fespace);

# form linear equation (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 it using PCG solver and store the solution to 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 array
verts = mesh.GetVertexArray()
sol = x.GetDataArray()

# plot solution using Matplotlib

import matplotlib.pyplot as plt
import matplotlib.tri as tri

triang = tri.Triangulation(verts[:,0], verts[:,1])

fig1, ax1 = plt.subplots()
tpc = ax1.tripcolor(triang, sol, shading='gouraud')


PyMFEM is licesed under BSD-3. Please refer the developers' web sites for the external libraries

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for mfem, version
Filename, size File type Python version Upload date Hashes
Filename, size mfem- (6.2 MB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page