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

File metadata

  • Download URL: mfem-4.8.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 138d4784789fec4592bc90a1046cc2edafba078c63fad3e3ff5e3e9ce6267ece
MD5 ee38000daa9bea0f0566498141ea70d9
BLAKE2b-256 32b1e009b4e6e7fed643fc6f336e081562fd796bf52a1372f96a6aed3e0eb30f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.8.0.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 7274d203ed97d7f846d21e0811c70404fadcf9a4b61e1403b0043645b81930f8
MD5 d02814d98a9aee692bbad73c9503f857
BLAKE2b-256 032a5a8a1556664d50b66b5f7c99dfc2beaeca110476667d07c782e9b243ec81

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.8.0.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4fb97b5969d3587261a7043bef6ab9196a00174e7d16b92c95d42a63c6fd55b2
MD5 4457eed70e562d1f22bc4b2e2c4ec1b6
BLAKE2b-256 6f0018343b4aea35ab892dde34b0ad0864ca31156474bdeb9ac72c766a4a7914

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.8.0.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 e689751c308e718151c80d7e431aed2666cc6205938ff77ded9b9eba8cd70fd1
MD5 7de9ed45a1afcfec36701a5f24894533
BLAKE2b-256 e497ac8b3909483ef3a6ee5036893ab51464434c36b14e8b0ba029feb4c16945

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.8.0.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 9eddd63e0224d628e6b7aeeec423b4ea4b3cd8f0af9c8a5dfec64ff0a05a578a
MD5 a24cd01580ce22b23a84e2adbec48510
BLAKE2b-256 bbaa5c625d9f2261ec5bf4298961f687651d6c906d1b7500e8ddb9ac393a9841

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.8.0.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 7c86f210798348aa4b41a959dce89370fcbad47ff52f3eec013ed974b032e49a
MD5 825a0fbdc3526bb1698d15bd381dd7a5
BLAKE2b-256 d60b46fb484a7156356c8481ebd835b21b16cee2e196d866a52590db6086f9e8

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