MFEM + PyMFEM (finite element method library)
Project description
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.
Requirement
numpy, swig + mpi4py (for --with-parallel)
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
Using additional features (MPI, GPU, GPU-Hypre, GSLIB, SuiteSparse)
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, this below download and build parallel version of MFEM library (linked with Metis and Hypre) and install under /mfem. See also, docs/install.txt
### Using pip
$ pip install mfem --install-option="--with-parallel" [--verbose]
### Runnig setup.py
$ pip download mfem --no-binary mfem (expand tar.gz file and move to the downloaded directory)
$ python setup.py install --with-parallel # it download and build metis/hypre/mfem
### Verbose output
$ python setup.py install --vv # SWIG output and CMAKE_VERBOSE_MAKEFILE is on
### Cleaning
$ python setup.py clean --all # clean external dependencies + wrapper code
Build with MFEM master in Github
$ pip install mfem --install-option="--with-parallel" --install-option="mfem-branch=master"[--verbose]
(or)
$ python setup.py install --with-parallel --mfem-branch='master'
Choosing compiler
$ python setup.py install --with-parallel --CC=icc --CXX=icpc --MPICC=mpiicc --MPICXX=mpiicpc
### Other options
For other configurations, see docs/install.txt or help
$ python setup.py install --help
Install from github source
# Clone this repo
git clone https://github.com/mfem/PyMFEM.git
# Build & Install
pip install ./PyMFEM --verbosel # build both MFEM and PyMFEM
*or*
cd PyMFEM
python setup.py install # build both MFEM and PyMFEM
# Run test
cd test
python test_examples.py -serial
Usage
Here is an example to solve div(grad(f)) = 1 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
one = mfem.ConstantCoefficient(1.0)
# define Bilinear and Linear operator
a = mfem.BilinearForm(fespace)
a.AddDomainIntegrator(mfem.DiffusionIntegrator(one))
a.Assemble()
b = mfem.LinearForm(fespace)
b.AddDomainIntegrator(mfem.DomainLFIntegrator(one))
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for mfem-4.4.0.2-cp310-cp310-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40be7ef89037b6f0ce6b5ecf6bc31dd8c7bc0d8a214e5f826214b4534cada1cb |
|
MD5 | 38dfceea3e6385816181e6be7f8f6ca2 |
|
BLAKE2b-256 | 58bb2039a0d134bb8781b3e147dd30737754972951a376b5937782cdb18a89d1 |
Hashes for mfem-4.4.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d46e63ba90674eca956ed6426c571d960a176ff92943f5aefbb7c057f9b9ccd3 |
|
MD5 | 9a55fe874f9b2597e261a5716b8f5fdf |
|
BLAKE2b-256 | ae16da8a2a7c2f9fa3558f6a040b3e3e5f3e7e256085cba40a517c6fe2c56aaa |
Hashes for mfem-4.4.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 143b9ecb6635cdad64d75f7e71d5ae14ee30a38a44a34ac817a07f43ede41015 |
|
MD5 | cce6cd711773998733625b54af8f6710 |
|
BLAKE2b-256 | d3185658a8f247a43b81823d265e055774ab8ca575501b1e53ea86be551b1193 |
Hashes for mfem-4.4.0.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fb071b6e90897caa83b4ca63bd912a83fb9a2ea0eb18943c454e842856118ea |
|
MD5 | 3cee227e818caebc59c475328d8686d6 |
|
BLAKE2b-256 | 4b6108fd072e3259ef3ebde41e5b0306e59745e9f208f90ce44a868797552492 |
Hashes for mfem-4.4.0.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6af2aa65bb54c0e2ad0c857cd3cea73bd2197b679c41d63f7097d8294ba73c30 |
|
MD5 | d71c16cf61b93c511ca1d81211ee9573 |
|
BLAKE2b-256 | 2377f43c661d2d094b17f2fd3b065d7592d24ecc2ce690690041ce1bdc22b8dd |