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.1.tar.gz (411.4 kB view details)

Uploaded Source

Built Distributions

mfem-4.7.0.1-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.1-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.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: mfem-4.7.0.1.tar.gz
  • Upload date:
  • Size: 411.4 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.1.tar.gz
Algorithm Hash digest
SHA256 77829d8189f0cfb56301dab332ddfb10a206975fa8bb052841d30d2e21988bf5
MD5 8a535a95cc1744e62575e184ba1e1bca
BLAKE2b-256 70d035adab15f15f58b4881c02ce09e5979ad1a7098da3d1288132f4e6a40753

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.7.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b7f7d0664ebf73328efbcb27e8e0308d254e7b02f21c2e35c29444769882094e
MD5 45892b8f388d56ae45dce53ecbee5e3c
BLAKE2b-256 c04aff74e2c4c32b0786818668909661a39701ec8b68bd08383f739e98152ad7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.7.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 48524ed0885ec47c38b42ff4a0694233e3e3c6bef3fa29ad232cc0564504b298
MD5 579a10bc3e3055194f767ac6d5d82f78
BLAKE2b-256 67b7dd128ee9892668e202cd4c48f202358b30197c5050e4f4a551bd586af844

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.7.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b7690978b827b051f0cc41bcae1aeb1dbd4d74e72eff3c6251a9d5cb9f733d65
MD5 c770cb9bcdeac84422b696b8356d25d0
BLAKE2b-256 25749eba8752516f5245295c4d64a52bcf5692590e0f26853340296b6cb2f6e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.7.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 12f7b642b147632f1c86972615cc365899f4c21c9e187bf71d30292dc977d167
MD5 f21872d3ef6d5cd42330798306283ce7
BLAKE2b-256 9d11f80128d313faf16c17a286668729e2aa854d57f99d9524c455f3e7c37df9

See more details on using hashes here.

Supported by

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