Skip to main content

MFEM + PyMFEM (finite element method library)

Project description

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

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

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.8.0.tar.gz (424.1 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.8.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (77.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

mfem-4.8.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (77.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

mfem-4.8.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (77.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

mfem-4.8.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (76.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

mfem-4.8.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (76.6 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

Details for the file mfem-4.8.0.tar.gz.

File metadata

  • Download URL: mfem-4.8.0.tar.gz
  • Upload date:
  • Size: 424.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for mfem-4.8.0.tar.gz
Algorithm Hash digest
SHA256 b12f311d946c5d51b325518ec738c221db42792845c5b5c95d9f868486013415
MD5 43597f19874eb2a3cfe77069d451cea3
BLAKE2b-256 8ee6f7bce55cb1cfe653d84dc5892fc02ab39a751bb9ebfcb233c9cf6322c27a

See more details on using hashes here.

File details

Details for the file mfem-4.8.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.8.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 aa0194711dfc5e7bf79dda104d4e2babb2ae5380678ca85bce87a387622fbd53
MD5 2bf54380001a6915673dd434aec3dee2
BLAKE2b-256 8fc34cb222e71652d12ae541aab7d3993bcc0791237a6ce136d98f5d5faf29e6

See more details on using hashes here.

File details

Details for the file mfem-4.8.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.8.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 8ac90f6e1290c858972858e1504545d2ba812db35f8da19c6f08698f9d71ca59
MD5 e26662a879344e45ac9ecc321a91c2bf
BLAKE2b-256 d7068d4ad13f6ba9a9921949a2ee0b6401bcefb9a280eba9e148a1ed87e2ba31

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.8.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 bac26a9bd7adfb73439cf4a3c51ef68ea489680dc8471eb66c5618cd54671956
MD5 371df0d461b979f213b08e370c24ccb6
BLAKE2b-256 fefe03d23d163af0665651262aecb498f962e16695689a3789d70f21c283f446

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.8.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 225f2f4a0c524534a8650eff1515a005f8498fef86922ba8587aec9cd27671e7
MD5 813c911ace7d9f7c2adbbff46fc87eef
BLAKE2b-256 ac3e5296b0fabba9db29f11776090316b8162e93aca5a44280892463f612e8c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.8.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 8d1d9495b0d4c8e80faeb59c59c35c2faa539f0eb33a328e45124bb5f47e58a8
MD5 179cf020f11ba23af5085fcbdd7c6974
BLAKE2b-256 88ddd779e58b7f6cf63c1c95c927498772a5e4c56d1be0322b5b540dfc417f87

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