Skip to content

Commit

Permalink
Merge pull request #307 from nschloe/meshio-bump
Browse files Browse the repository at this point in the history
update for new meshio
  • Loading branch information
nschloe authored Feb 18, 2020
2 parents 61882d9 + 96067df commit ff5b617
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 53 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- uses: actions/setup-python@v1
with:
python-version: "3.x"
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Lint with flake8
run: |
pip install flake8
Expand All @@ -28,7 +28,7 @@ jobs:
- uses: actions/setup-python@v1
with:
python-version: "3.x"
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- run: sudo apt install -y python3-pip
- name: Install gmsh from PPA
run: |
Expand All @@ -50,5 +50,5 @@ jobs:
gmsh --version
pip install pytest pytest-cov
cd test/ && pytest --cov pygmsh
- name: Submit to codecov
run: bash <(curl -s https://codecov.io/bash)
# - name: Submit to codecov
# run: bash <(curl -s https://codecov.io/bash)
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/psf/black)
[![Documentation Status](https://readthedocs.org/projects/pygmsh/badge/?version=latest&style=flat-square)](https://pygmsh.readthedocs.org/en/latest/?badge=latest)
[![PyPi Version](https://img.shields.io/pypi/v/pygmsh.svg?style=flat-square)](https://pypi.org/project/pygmsh)
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/pygmsh.svg?style=flat-square)](https://pypi.org/pypi/pygmsh/)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1173105.svg?style=flat-square)](https://doi.org/10.5281/zenodo.1173105)
[![GitHub stars](https://img.shields.io/github/stars/nschloe/pygmsh.svg?style=flat-square&logo=github&label=Stars&logoColor=white)](https://github.com/nschloe/pygmsh)
[![PyPi downloads](https://img.shields.io/pypi/dm/pygmsh.svg?style=flat-square)](https://pypistats.org/packages/pygmsh)
[![Slack](https://img.shields.io/static/v1?logo=slack&label=slack&message=chat&color=4a154b&style=flat-square)](https://app.slack.com/client/TTL6Q54A3/CTLGZQFML/)
[![Slack](https://img.shields.io/static/v1?logo=slack&label=chat&message=on%20slack&color=4a154b&style=flat-square)](https://app.slack.com/client/TTL6Q54A3/CTLGZQFML/)

[Gmsh](https://gmsh.info/) is a powerful mesh generation tool with a scripting language
that is notoriously hard to write.
Expand Down
2 changes: 1 addition & 1 deletion pygmsh/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "6.0.2"
__version__ = "6.0.3"
__author__ = "Nico Schlömer"
__author_email__ = "[email protected]"
__copyright__ = f"Copyright (c) 2013-2020, {__author__} <{__author_email__}>"
Expand Down
2 changes: 1 addition & 1 deletion pygmsh/built_in/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def set_recombined_surfaces(self, surfaces):
for i, surface in enumerate(surfaces):
assert isinstance(
surface, (PlaneSurface, Surface)
), "item {} is not a surface".format(i)
), f"item {i} is not a surface"
code = "Recombine Surface {{{}}}".format(
", ".join([surface.id for surface in surfaces])
)
Expand Down
22 changes: 13 additions & 9 deletions pygmsh/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ def generate_mesh( # noqa: C901
mesh = meshio.read(msh_filename)

if remove_faces:
# Only keep the cells of highest topological dimension; discard faces
# and such.
# Only keep the cells of highest topological dimension; discard faces and such.
two_d_cells = {"triangle", "quad"}
three_d_cells = {
"tetra",
Expand All @@ -160,21 +159,26 @@ def generate_mesh( # noqa: C901
elif any(k in mesh.cells for k in two_d_cells):
keep_keys = two_d_cells.intersection(mesh.cells.keys())
else:
keep_keys = mesh.cells.keys()
keep_keys = set(cell_type for cell_type, _ in mesh.cells)

mesh.cells = {key: mesh.cells[key] for key in keep_keys}
mesh.cell_data = {key: mesh.cell_data[key] for key in keep_keys}
for key, val in mesh.cell_data.items():
mesh.cell_data[key] = [
d for d, c in zip(val, mesh.cells) if c[0] in keep_keys
]
mesh.cells = [c for c in mesh.cells if c[0] in keep_keys]

if prune_vertices:
# Make sure to include only those vertices which belong to a cell.
ncells = numpy.concatenate([numpy.concatenate(c) for c in mesh.cells.values()])
ncells = numpy.concatenate([numpy.concatenate(c) for _, c in mesh.cells])
uvertices, uidx = numpy.unique(ncells, return_inverse=True)

k = 0
for key in mesh.cells.keys():
n = numpy.prod(mesh.cells[key].shape)
mesh.cells[key] = uidx[k : k + n].reshape(mesh.cells[key].shape)
cells = []
for key, cellblock in mesh.cells:
n = numpy.prod(cellblock.shape)
cells.append((key, uidx[k : k + n].reshape(cellblock.shape)))
k += n
mesh.cells = cells

mesh.points = mesh.points[uvertices]
for key in mesh.point_data:
Expand Down
20 changes: 10 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import codecs
import os

from setuptools import find_packages, setup
Expand All @@ -10,33 +9,34 @@
exec(f.read(), about)


def read(fname):
return codecs.open(os.path.join(base_dir, fname), encoding="utf-8").read()


setup(
name="pygmsh",
version=about["__version__"],
author=about["__author__"],
author_email=about["__author_email__"],
packages=find_packages(),
description="Python frontend for Gmsh",
long_description=read("README.md"),
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url=about["__website__"],
project_urls={"Documentation": "https://pygmsh.readthedocs.org/en/latest"},
project_urls={
"Code": "https://github.com/nschloe/meshio",
"Documentation": "https://pygmsh.readthedocs.org/en/latest",
"Issue tracker": "https://github.com/nschloe/meshio/issues",
},
license=about["__license__"],
platforms="any",
install_requires=["meshio >=3.0, <4.0", "numpy >= 1.9"],
install_requires=["meshio >=4.0, <5.0", "numpy >= 1.9"],
python_requires=">=3.6",
keywords=["mesh", "gmsh", "mesh generation", "mathematics"],
classifiers=[
about["__status__"],
about["__license__"],
"Intended Audience :: Science/Research",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Topic :: Scientific/Engineering :: Mathematics",
],
)
20 changes: 11 additions & 9 deletions test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,31 @@ def get_simplex_volumes(pts, cells):


def compute_volume(mesh):
if "tetra" in mesh.cells:
if "tetra" in mesh.cells_dict:
vol = math.fsum(
get_simplex_volumes(*prune_nodes(mesh.points, mesh.cells["tetra"]))
get_simplex_volumes(*prune_nodes(mesh.points, mesh.cells_dict["tetra"]))
)
elif "triangle" in mesh.cells or "quad" in mesh.cells:
elif "triangle" in mesh.cells_dict or "quad" in mesh.cells_dict:
vol = 0.0
if "triangle" in mesh.cells:
if "triangle" in mesh.cells_dict:
# triangles
vol += math.fsum(
get_triangle_volumes(*prune_nodes(mesh.points, mesh.cells["triangle"]))
get_triangle_volumes(
*prune_nodes(mesh.points, mesh.cells_dict["triangle"])
)
)
if "quad" in mesh.cells:
if "quad" in mesh.cells_dict:
# quad: treat as two triangles
quads = mesh.cells["quad"].T
quads = mesh.cells_dict["quad"].T
split_cells = numpy.column_stack(
[[quads[0], quads[1], quads[2]], [quads[0], quads[2], quads[3]]]
).T
vol += math.fsum(
get_triangle_volumes(*prune_nodes(mesh.points, split_cells))
)
else:
assert "line" in mesh.cells
segs = numpy.diff(mesh.points[mesh.cells["line"]], axis=1).squeeze()
assert "line" in mesh.cells_dict
segs = numpy.diff(mesh.points[mesh.cells_dict["line"]], axis=1).squeeze()
vol = numpy.sum(numpy.sqrt(numpy.einsum("...j, ...j", segs, segs)))

return vol
Expand Down
21 changes: 10 additions & 11 deletions test/test_booleans.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ def test_square_circle_slice():
val = compute_volume(mesh)
assert np.abs(val - ref) < 1e-3 * ref

outer_mask = np.where(mesh.cell_data["triangle"]["gmsh:geometrical"] == 13)[0]
outer_mask = np.where(mesh.cell_data["gmsh:geometrical"][2] == 13)[0]
outer_cells = {}
outer_cells["triangle"] = mesh.cells["triangle"][outer_mask]
outer_cells["triangle"] = mesh.cells_dict["triangle"][outer_mask]

inner_mask = np.where(mesh.cell_data["triangle"]["gmsh:geometrical"] == 12)[0]
inner_mask = np.where(mesh.cell_data["gmsh:geometrical"][2] == 12)[0]
inner_cells = {}
inner_cells["triangle"] = mesh.cells["triangle"][inner_mask]
inner_cells["triangle"] = mesh.cells_dict["triangle"][inner_mask]

ref = 1 - 0.1 ** 2 * np.pi
value = compute_volume(meshio.Mesh(mesh.points, outer_cells))
Expand All @@ -138,17 +138,16 @@ def test_fragments_diff_union():
mesh = pygmsh.generate_mesh(geo_object)
assert np.abs((compute_volume(mesh) - 1) / 1) < 1e-3
surf = 1 - 0.1 ** 2 * np.pi
outer_mask = np.where(mesh.cell_data["triangle"]["gmsh:physical"] == 1)[0]
outer_mask = np.where(mesh.cell_data_dict["gmsh:physical"]["triangle"] == 1)[0]
outer_cells = {}
outer_cells["triangle"] = mesh.cells["triangle"][outer_mask]
outer_cells["triangle"] = mesh.cells_dict["triangle"][outer_mask]

inner_mask = np.where(mesh.cell_data["triangle"]["gmsh:physical"] == 2)[0]
inner_mask = np.where(mesh.cell_data_dict["gmsh:physical"]["triangle"] == 2)[0]
inner_cells = {}
inner_cells["triangle"] = mesh.cells["triangle"][inner_mask]
inner_cells["triangle"] = mesh.cells_dict["triangle"][inner_mask]

value = compute_volume(meshio.Mesh(mesh.points, outer_cells))
assert np.abs(value - surf) < 1e-2 * surf
return


def test_diff_physical_assignment():
Expand All @@ -166,8 +165,8 @@ def test_diff_physical_assignment():
geo_object2.boolean_difference([surf1], [surf2])
mesh = pygmsh.generate_mesh(geo_object2)
assert np.allclose(
mesh.cell_data["triangle"]["gmsh:physical"],
np.ones(mesh.cells["triangle"].shape[0]),
mesh.cell_data_dict["gmsh:physical"]["triangle"],
np.ones(mesh.cells_dict["triangle"].shape[0]),
)
surf = 1 - 0.1 ** 2 * np.pi
assert np.abs((compute_volume(mesh) - surf) / surf) < 1e-3
Expand Down
3 changes: 2 additions & 1 deletion test/test_hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def test(lcar=1.0):
# TODO compute hex volumes
assert (
abs(
compute_volume(meshio.Mesh(mesh.points, {"quad": mesh.cells["quad"]})) - ref
compute_volume(meshio.Mesh(mesh.points, {"quad": mesh.cells_dict["quad"]}))
- ref
)
< 1.0e-2 * ref
)
Expand Down
2 changes: 1 addition & 1 deletion test/test_opencascade_regular_extrusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test():

# Each grid-cell from layered extrusion will result in 6 tetrahedrons.
ref_tetras = 6 * x_layers * y_layers * z_layers
assert len(mesh.cells["tetra"]) == ref_tetras
assert len(mesh.cells_dict["tetra"]) == ref_tetras

return mesh

Expand Down
7 changes: 4 additions & 3 deletions test/test_recombine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pygmsh
import numpy as np

import pygmsh


def test():
geom = pygmsh.built_in.Geometry()
Expand All @@ -21,9 +22,9 @@ def test():

mesh = pygmsh.generate_mesh(geom)

assert "quad" in mesh.cells.keys()
assert "quad" in mesh.cells_dict.keys()
ref = np.array([[0, 4, 8, 7], [7, 8, 6, 2], [4, 1, 5, 8], [8, 5, 3, 6]])
assert np.array_equal(ref, mesh.cells["quad"])
assert np.array_equal(ref, mesh.cells_dict["quad"])

return mesh

Expand Down
2 changes: 1 addition & 1 deletion test/test_regular_extrusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test():

# Each grid-cell from layered extrusion will result in 6 tetrahedrons.
ref_tetras = 6 * x_layers * y_layers * z_layers
assert len(mesh.cells["tetra"]) == ref_tetras
assert len(mesh.cells_dict["tetra"]) == ref_tetras

return mesh

Expand Down
2 changes: 1 addition & 1 deletion test/test_transfinite.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test(lcar=1.0):
geom.set_transfinite_surface(poly.surface, size=[11, 9])

mesh = pygmsh.generate_mesh(geom, geo_filename="transfinite.geo")
assert len(mesh.cells["triangle"]) == 10 * 8 * 2
assert len(mesh.cells_dict["triangle"]) == 10 * 8 * 2
return mesh


Expand Down

0 comments on commit ff5b617

Please sign in to comment.