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)
pip install mfem --no-binary mfem   # install serial MFEM + wrapper from source

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

Using pip

$ pip install mfem --install-option="--with-parallel" 

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

Here is an example to solve div(alpha grad(u)) = f in a square and to plot the result with matplotlib (modified from ex1.cpp). Use the badge above to open this in Binder.

import mfem.ser as mfem

# create sample mesh for square shape
mesh = mfem.Mesh(10, 10, "TRIANGLE")

# create finite element function space
fec = mfem.H1_FECollection(1, mesh.Dimension())   # H1 order=1
fespace = mfem.FiniteElementSpace(mesh, fec)

#
ess_tdof_list = mfem.intArray()
ess_bdr = mfem.intArray([1]*mesh.bdr_attributes.Size())
fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list)

# constant coefficient (diffusion coefficient and RHS)
alpha = mfem.ConstantCoefficient(1.0)
rhs = mfem.ConstantCoefficient(1.0)

# Note:
#    Diffusion coefficient can be variable. To use numba-JIT compiled
#    functio. Use the following, where x is numpy-like array.
# @mfem.jit.scalar
# def alpha(x):
#     return x+1.0
#

# define Bilinear and Linear operator
a = mfem.BilinearForm(fespace)
a.AddDomainIntegrator(mfem.DiffusionIntegrator(alpha))
a.Assemble()
b = mfem.LinearForm(fespace)
b.AddDomainIntegrator(mfem.DomainLFIntegrator(rhs))
b.Assemble()

# create gridfunction, which is where the solution vector is stored
x = mfem.GridFunction(fespace);
x.Assign(0.0)

# form linear equation (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 it using PCG solver and store the solution to 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 array
verts = mesh.GetVertexArray()
sol = x.GetDataArray()

# plot solution using Matplotlib

import matplotlib.pyplot as plt
import matplotlib.tri as tri

triang = tri.Triangulation(verts[:,0], verts[:,1])

fig1, ax1 = plt.subplots()
ax1.set_aspect('equal')
tpc = ax1.tripcolor(triang, sol, shading='gouraud')
fig1.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.6.1.0.tar.gz (406.4 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.6.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (62.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

mfem-4.6.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (62.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

mfem-4.6.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (62.1 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

mfem-4.6.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (61.8 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: mfem-4.6.1.0.tar.gz
  • Upload date:
  • Size: 406.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.6.1.0.tar.gz
Algorithm Hash digest
SHA256 cf10d8162349dca1ac2f49e0f401a8786ab950a81bbff567ea542252763f11d2
MD5 2b89eae32e458e686e0a7b3e1af9812d
BLAKE2b-256 c1b4f74adf690cb16a5df20a66f8e4156aa345dc2926a23ffda3deed94ac9a30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.6.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3262286f978aafbafd18660d32db00f9d310c67d0cf0fbd6a4deb102bfc959c9
MD5 1045fbbc3065cca740b42de1caa78bc3
BLAKE2b-256 a01c3ebdd30aeab181adb69fefd0e0caa04a8796f59ab02e3883f230d7db1551

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.6.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cc539ee5aca0700d85f0cdff0f05e0b798b224ed3375ce889e074c6eb05f1a7b
MD5 b8362913bcb06f31efb82e9425882442
BLAKE2b-256 639c6e0d4bc36ccc4711df86e8e13e68f002781c234f181211d0605902d98df1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mfem-4.6.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 51482172f11c33c717f8759295a249fdfa180b0ca88ea6f1b93b771dcb912d70
MD5 1c33a386937cd1baa18aae6e32ff92ef
BLAKE2b-256 21f1659f0f8836e2bf6503a0952b7c615a6f81660d4b98563dd2196f75dfa1b3

See more details on using hashes here.

File details

Details for the file mfem-4.6.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mfem-4.6.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a9a7e1fb44b8bfce0bff54422ca48e65537c50a7402e0f4ab4efc5606a8be5d2
MD5 038331ca15f478fe5c0af2d6d6b3bafa
BLAKE2b-256 b05723ec66d6274b121b47fab6bab27c6d5d40b040537cc3beba187e86231404

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