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.1rc1.tar.gz (437.5 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.1rc1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (78.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

mfem-4.8.0.1rc1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (77.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

mfem-4.8.0.1rc1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (77.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

mfem-4.8.0.1rc1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (77.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

mfem-4.8.0.1rc1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (77.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

Details for the file mfem-4.8.0.1rc1.tar.gz.

File metadata

  • Download URL: mfem-4.8.0.1rc1.tar.gz
  • Upload date:
  • Size: 437.5 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.1rc1.tar.gz
Algorithm Hash digest
SHA256 3f1ac0fcc29e49e3370bfa3c2a42fb59f0af547b86bd68cb071583594bdc9067
MD5 7d024b298a5eeea443add3bfab780480
BLAKE2b-256 4f80aacfef1d16f1779189eca354d7e0483e6f06b2149a3aabbc39013c5329ee

See more details on using hashes here.

File details

Details for the file mfem-4.8.0.1rc1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.8.0.1rc1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 316b4536c982e22fda02117f048ccf812c291337a91e71b4ad59f4472c6592fd
MD5 275728655012ffe0e50d863037109a77
BLAKE2b-256 5098052d064434babe4a78d448572fcb9fcc1f8a162d77d470076634a3ef9a83

See more details on using hashes here.

File details

Details for the file mfem-4.8.0.1rc1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.8.0.1rc1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4a779690b466e8e99bff2f31ca952fe0a3686a8706c3aa14deff08f75bbee143
MD5 442b3a00150d0b698309b00a689755d0
BLAKE2b-256 afb86e960bc6ad645db393fe798f57846f3379e9adf56d3885d7d2c257c61e71

See more details on using hashes here.

File details

Details for the file mfem-4.8.0.1rc1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.8.0.1rc1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 5bb6cb49ddcba5f525d8e2fc135cc060f3d1c226d1c7bffa3320123621ec2ea2
MD5 692b8bcba5fe560da1b0c57a9b2f2f34
BLAKE2b-256 7e3dadf3f17c02e5c770983c1a6702123fc3eb3a098727edbe93d73c1956add7

See more details on using hashes here.

File details

Details for the file mfem-4.8.0.1rc1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.8.0.1rc1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 98a7fe3ad042edec036bc52236d123874e6ab346d51e8da3fce0d4cb3b44835f
MD5 7df67e9da74d4301e5d9f5c934434f7a
BLAKE2b-256 73599f0e61f1f714d43186a8e4a96d2f302ff71c53d9fd1425999ce5674c0045

See more details on using hashes here.

File details

Details for the file mfem-4.8.0.1rc1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.8.0.1rc1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 95151a28844a0f919b20fe66567d794b0bf2c3b2c2de1227e26aa07dbaacb7ef
MD5 ff1d15b293a994fb54330e4e066321fe
BLAKE2b-256 20a936295ac85f006b354b8e4df6ce27aa9b19b119d3e4d51ae6158e277d4d63

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