[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/mfem/PyMFEM/HEAD?labpath=examples%2Fjupyter)
[![badge](examples/jupyter/ex1.svg)](https://mybinder.org/v2/gh/mfem/PyMFEM/HEAD?labpath=examples%2Fjupyter%2Fex1.ipynb)
[![badge](examples/jupyter/ex9.svg)](https://mybinder.org/v2/gh/mfem/PyMFEM/HEAD?labpath=examples%2Fjupyter%2Fex9.ipynb)
# 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
```shell
pip install mfem # binary install is available only on linux platforms (Py36-310)
```
## 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 <prefix>/mfem. See also, docs/install.txt
### Build from local source file
```shell
# 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
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 Binder.
```python
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()
```
![](https://raw.githubusercontent.com/mfem/PyMFEM/master/docs/example_image.png)
## License
PyMFEM is licensed under BSD-3.
Please refer the developers' web sites for the external libraries
* MFEM: https://mfem.org/
* Hypre: https://computing.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods
* METIS: http://glaros.dtc.umn.edu/gkhome/metis/metis/overview
* libceed: https://github.com/CEED/libCEED
* gslib: https://github.com/Nek5000/gslib
Raw data
{
"_id": null,
"home_page": "http://mfem.org",
"name": "mfem",
"maintainer": "S. Shiraiwa",
"docs_url": null,
"requires_python": null,
"maintainer_email": "shiraiwa@princeton.edu",
"keywords": "scientific computing, finite element method",
"author": "MFEM developement team",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/70/d0/35adab15f15f58b4881c02ce09e5979ad1a7098da3d1288132f4e6a40753/mfem-4.7.0.1.tar.gz",
"platform": "Mac OS X",
"description": "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/mfem/PyMFEM/HEAD?labpath=examples%2Fjupyter)\n[![badge](examples/jupyter/ex1.svg)](https://mybinder.org/v2/gh/mfem/PyMFEM/HEAD?labpath=examples%2Fjupyter%2Fex1.ipynb)\n[![badge](examples/jupyter/ex9.svg)](https://mybinder.org/v2/gh/mfem/PyMFEM/HEAD?labpath=examples%2Fjupyter%2Fex9.ipynb)\n\n# MFEM + PyMFEM (FEM library)\n\nThis repository provides Python binding for MFEM. MFEM is a high performance parallel finite element method (FEM) library (http://mfem.org/).\n\nInstaller (setup.py) builds both MFEM and binding together.\nBy default, \"pip install mfem\" downloads and builds the serial version of MFEM and PyMFEM.\nAdditionally, the installer supports building MFEM with specific options together with other external libraries, including MPI version.\n\n## Install\n```shell\npip install mfem # binary install is available only on linux platforms (Py36-310)\n\n```\n\n## Build with additional features (MPI, GPU, GPU-Hypre, GSLIB, SuiteSparse, libCEED, LAPACK)\nThe setup script accept various options. TO use it, one can either use --install-option flag\nwith pip, or download the package manually and run the script. For example, the one below downloads\nand build parallel version of MFEM library (linked with Metis and Hypre)\nand installs under <prefix>/mfem. See also, docs/install.txt\n\n\n### Build from local source file\n```shell\n# Download source and build\n$ pip download mfem --no-binary mfem (expand tar.gz file and move to the downloaded directory)\nor clone this repository\n$ git clone https://github.com/mfem/PyMFEM.git\n\n# Then, build it from local source\n$ python -m pip install ./ --install-option=\"--with-parallel\" --install-option=\"--mfem-branch=master\"\nor\n$ python setup.py install --with-parallel # it download and build metis/hypre/mfem\n\n# Verbose output\n$ python setup.py install --verbose # SWIG output and CMAKE_VERBOSE_MAKEFILE is on\n\n# Cleaning\n$ python setup.py clean --all # clean external dependencies + wrapper code\n\n# Choosing compiler\n$ python setup.py install --with-parallel --CC=icc --CXX=icpc --MPICC=mpiicc --MPICXX=mpiicpc\n\n# Run test\ncd test\npython test_examples.py -serial\n\n# For other configurations, see docs/install.txt or help\n$ python setup.py install --help\n\n```\n\n## Usage\nThis example (modified from `ex1.cpp`) solves the Poisson equation,\n$$\\nabla \\cdot (\\alpha \\nabla u) = f$$\nin a square and plots the result using matplotlib.\nUse the badge above to open this in Binder.\n\n```python\nimport mfem.ser as mfem\n\n# Create a square mesh\nmesh = mfem.Mesh(10, 10, \"TRIANGLE\")\n\n# Define the finite element function space\nfec = mfem.H1_FECollection(1, mesh.Dimension()) # H1 order=1\nfespace = mfem.FiniteElementSpace(mesh, fec)\n\n# Define the essential dofs\ness_tdof_list = mfem.intArray()\ness_bdr = mfem.intArray([1]*mesh.bdr_attributes.Size())\nfespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list)\n\n# Define constants for alpha (diffusion coefficient) and f (RHS)\nalpha = mfem.ConstantCoefficient(1.0)\nrhs = mfem.ConstantCoefficient(1.0)\n\n\"\"\"\nNote\n-----\nIn order to represent a variable diffusion coefficient, you\nmust use a numba-JIT compiled function. For example:\n\n>>> @mfem.jit.scalar\n>>> def alpha(x):\n>>> return x+1.0\n\"\"\"\n\n# Define the bilinear and linear operators\na = mfem.BilinearForm(fespace)\na.AddDomainIntegrator(mfem.DiffusionIntegrator(alpha))\na.Assemble()\nb = mfem.LinearForm(fespace)\nb.AddDomainIntegrator(mfem.DomainLFIntegrator(rhs))\nb.Assemble()\n\n# Initialize a gridfunction to store the solution vector\nx = mfem.GridFunction(fespace)\nx.Assign(0.0)\n\n# Form the linear system of equations (AX=B)\nA = mfem.OperatorPtr()\nB = mfem.Vector()\nX = mfem.Vector()\na.FormLinearSystem(ess_tdof_list, x, b, A, X, B)\nprint(\"Size of linear system: \" + str(A.Height()))\n\n# Solve the linear system using PCG and store the solution in x\nAA = mfem.OperatorHandle2SparseMatrix(A)\nM = mfem.GSSmoother(AA)\nmfem.PCG(AA, M, B, X, 1, 200, 1e-12, 0.0)\na.RecoverFEMSolution(X, b, x)\n\n# Extract vertices and solution as numpy arrays\nverts = mesh.GetVertexArray()\nsol = x.GetDataArray()\n\n# Plot the solution using matplotlib\nimport matplotlib.pyplot as plt\nimport matplotlib.tri as tri\n\ntriang = tri.Triangulation(verts[:,0], verts[:,1])\n\nfig, ax = plt.subplots()\nax.set_aspect('equal')\ntpc = ax.tripcolor(triang, sol, shading='gouraud')\nfig.colorbar(tpc)\nplt.show()\n```\n![](https://raw.githubusercontent.com/mfem/PyMFEM/master/docs/example_image.png)\n\n\n## License\nPyMFEM is licensed under BSD-3.\nPlease refer the developers' web sites for the external libraries\n* MFEM: https://mfem.org/\n* Hypre: https://computing.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods\n* METIS: http://glaros.dtc.umn.edu/gkhome/metis/metis/overview\n* libceed: https://github.com/CEED/libCEED\n* gslib: https://github.com/Nek5000/gslib\n",
"bugtrack_url": null,
"license": "BSD-3",
"summary": "MFEM + PyMFEM (finite element method library)",
"version": "4.7.0.1",
"project_urls": {
"Download": "https://github.com/mfem",
"Homepage": "http://mfem.org"
},
"split_keywords": [
"scientific computing",
" finite element method"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "67b7dd128ee9892668e202cd4c48f202358b30197c5050e4f4a551bd586af844",
"md5": "579a10bc3e3055194f767ac6d5d82f78",
"sha256": "48524ed0885ec47c38b42ff4a0694233e3e3c6bef3fa29ad232cc0564504b298"
},
"downloads": -1,
"filename": "mfem-4.7.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "579a10bc3e3055194f767ac6d5d82f78",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": null,
"size": 64489090,
"upload_time": "2024-08-09T22:44:20",
"upload_time_iso_8601": "2024-08-09T22:44:20.218505Z",
"url": "https://files.pythonhosted.org/packages/67/b7/dd128ee9892668e202cd4c48f202358b30197c5050e4f4a551bd586af844/mfem-4.7.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c04aff74e2c4c32b0786818668909661a39701ec8b68bd08383f739e98152ad7",
"md5": "45892b8f388d56ae45dce53ecbee5e3c",
"sha256": "b7f7d0664ebf73328efbcb27e8e0308d254e7b02f21c2e35c29444769882094e"
},
"downloads": -1,
"filename": "mfem-4.7.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "45892b8f388d56ae45dce53ecbee5e3c",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": null,
"size": 64904182,
"upload_time": "2024-08-09T22:44:46",
"upload_time_iso_8601": "2024-08-09T22:44:46.948070Z",
"url": "https://files.pythonhosted.org/packages/c0/4a/ff74e2c4c32b0786818668909661a39701ec8b68bd08383f739e98152ad7/mfem-4.7.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9d11f80128d313faf16c17a286668729e2aa854d57f99d9524c455f3e7c37df9",
"md5": "f21872d3ef6d5cd42330798306283ce7",
"sha256": "12f7b642b147632f1c86972615cc365899f4c21c9e187bf71d30292dc977d167"
},
"downloads": -1,
"filename": "mfem-4.7.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "f21872d3ef6d5cd42330798306283ce7",
"packagetype": "bdist_wheel",
"python_version": "cp38",
"requires_python": null,
"size": 64287984,
"upload_time": "2024-08-09T22:44:23",
"upload_time_iso_8601": "2024-08-09T22:44:23.403707Z",
"url": "https://files.pythonhosted.org/packages/9d/11/f80128d313faf16c17a286668729e2aa854d57f99d9524c455f3e7c37df9/mfem-4.7.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "25749eba8752516f5245295c4d64a52bcf5692590e0f26853340296b6cb2f6e3",
"md5": "c770cb9bcdeac84422b696b8356d25d0",
"sha256": "b7690978b827b051f0cc41bcae1aeb1dbd4d74e72eff3c6251a9d5cb9f733d65"
},
"downloads": -1,
"filename": "mfem-4.7.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "c770cb9bcdeac84422b696b8356d25d0",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": null,
"size": 64432850,
"upload_time": "2024-08-09T22:44:23",
"upload_time_iso_8601": "2024-08-09T22:44:23.860613Z",
"url": "https://files.pythonhosted.org/packages/25/74/9eba8752516f5245295c4d64a52bcf5692590e0f26853340296b6cb2f6e3/mfem-4.7.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "70d035adab15f15f58b4881c02ce09e5979ad1a7098da3d1288132f4e6a40753",
"md5": "8a535a95cc1744e62575e184ba1e1bca",
"sha256": "77829d8189f0cfb56301dab332ddfb10a206975fa8bb052841d30d2e21988bf5"
},
"downloads": -1,
"filename": "mfem-4.7.0.1.tar.gz",
"has_sig": false,
"md5_digest": "8a535a95cc1744e62575e184ba1e1bca",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 411359,
"upload_time": "2024-08-09T22:31:47",
"upload_time_iso_8601": "2024-08-09T22:31:47.154452Z",
"url": "https://files.pythonhosted.org/packages/70/d0/35adab15f15f58b4881c02ce09e5979ad1a7098da3d1288132f4e6a40753/mfem-4.7.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-09 22:31:47",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "mfem"
}