Skip to main content

MFEM + PyMFEM (finite element method library)

Project description

Binder badge badge

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

pip install mfem                    # binary install is available only on linux platforms (Py36-310)
pip install mfem --no-binary mfem   # install serial MFEM + wrapper from source

Build with additional features (MPI, GPU, GPU-Hypre, GSLIB, SuiteSparse, libCEED, LAPACK)

The setup script accept various options. TO use it, one can either use --install-option flag with pip, or download the package manually and run the script. For example, the one below downloads and build parallel version of MFEM library (linked with Metis and Hypre) and installs under /mfem. See also, docs/install.txt

Using pip

pip install mfem --install-option="--with-parallel"

Build from local source file

# Download source and build
$ pip download mfem --no-binary mfem (expand tar.gz file and move to the downloaded directory)
or clone this repository
$ git clone https://github.com/mfem/PyMFEM.git

# Then, build it from local source
$ python -m pip install ./ --install-option="--with-parallel" --install-option="--mfem-branch=master"
or
$ python setup.py install --with-parallel # it download and build metis/hypre/mfem

# Verbose output
$ python setup.py install --verbose # SWIG output and CMAKE_VERBOSE_MAKEFILE is on

# Cleaning
$ python setup.py clean --all # clean external dependencies + wrapper code

# Choosing compiler
$ python setup.py install --with-parallel --CC=icc --CXX=icpc --MPICC=mpiicc --MPICXX=mpiicpc

# Run test
cd test
python test_examples.py -serial

# For other configurations, see docs/install.txt or help
$ python setup.py install --help

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 Binder.

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. 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.

Source Distribution

mfem-4.7.0rc6.tar.gz (411.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mfem-4.7.0rc6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (64.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

mfem-4.7.0rc6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (64.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

mfem-4.7.0rc6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (64.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

mfem-4.7.0rc6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (64.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file mfem-4.7.0rc6.tar.gz.

File metadata

  • Download URL: mfem-4.7.0rc6.tar.gz
  • Upload date:
  • Size: 411.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for mfem-4.7.0rc6.tar.gz
Algorithm Hash digest
SHA256 bf853abc58fa59bab36c1dc61ac9664c9b37ad2b45a6fa7c84261747ea3bc55e
MD5 68559871394d3b576bf8b0eaa6f9459f
BLAKE2b-256 90899470f56e149bb9d45588ecd7050c595057536b5adca3f0e8da8d734506b3

See more details on using hashes here.

File details

Details for the file mfem-4.7.0rc6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.7.0rc6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 be577a82c0d92456c9a993c14517546110bc837a6c6a603fe8a48cc0936cdaa4
MD5 edf1a687d0174c98ea12e4e52f5ea7e5
BLAKE2b-256 7e9d3502d56cc0cd3f205ccd66126b50ddd1914ad3ad77614c85ce4171af5dff

See more details on using hashes here.

File details

Details for the file mfem-4.7.0rc6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.7.0rc6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e064865942df065481ca414b150adf37793f939c82767a71735c9d17fc3cd80b
MD5 5ace6b7d7ba6a1a2a00458cd3fa914eb
BLAKE2b-256 fede71c8c3368ed2c1eeb0e1193489c93442d42b659d71e61cf2575b0027116e

See more details on using hashes here.

File details

Details for the file mfem-4.7.0rc6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.7.0rc6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fc3bfa6452d5e274721c6911749dd5c1bf1fea94ad381368fbba2f997bfed934
MD5 436e42a8185ade2fb45223b076db39d4
BLAKE2b-256 ac18eef739597cb8b87bbf181a1b0606a25f1e20eade45c816da72b84b3de2fa

See more details on using hashes here.

File details

Details for the file mfem-4.7.0rc6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.7.0rc6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 77913ac335b08cdc9f9389c4f53fcdebca73221a0e1cb3e4cf991d8088f4b3f5
MD5 36c721336b5bba949095f3a8cff13105
BLAKE2b-256 79b2278ffe6b366fd0f045b07760bc1abb8e39988fd698e8aafd39c249083552

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page