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)

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

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.0.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.0-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.0-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.0-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.0-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.0.tar.gz.

File metadata

  • Download URL: mfem-4.7.0.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.0.tar.gz
Algorithm Hash digest
SHA256 9021dc43ad9bf63bf1320a52da5a46b0c4a416adea7e3efe5aef36e2d9839b44
MD5 c51548141ab84d04549786d5a3e0d9e1
BLAKE2b-256 ee0c72b1e4d07f5acb2bbd882ebbef818c17d0be7314067b575dc5749c625c0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0f755eaf7afa39a70059930e1362ef969133219048afe0312787aafcc74b16da
MD5 f75330f16d8e4f0a450726e4ce3e393a
BLAKE2b-256 8a3060b7e5db906233e5eff3b30d72f3b5c35d73960534c411eb68f0883f6bcd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 01063ef2e12a9d217d07c2c289c21f0dc2ff20f8a699d36cd619b3fb3b517862
MD5 0645eda00c2121595ffcaa2761c5173b
BLAKE2b-256 94e689bd3ed1255215c00c2abc8f0564ac2bb2ac1914b0d36a274a7e8c0a5a1d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4b31a3c47d4966f021ed12a4c782b83c8e2f98c64e5f33c827ec1c4008c1c2a7
MD5 3ad5160c310eeff8400e7c858ade7d80
BLAKE2b-256 5286f2fcf01b4affba1e7bb56ed908d9d755147b8f4baa3f73709db7a45cbdee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9135c501008e8199b3c081f4123eba87841576a1b3b26353d5ab01a1bff87023
MD5 fff90de1932daa6828f83302701b72c6
BLAKE2b-256 171f50598c093e3f1b7353e157d7ffeb6b943e8dfff4fd8914cec4c19f2003e1

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