Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strictly type everything #35

Merged
merged 8 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ on:
- cron: '17 3 * * 0'

jobs:
typos:
name: Typos
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: crate-ci/typos@master

ruff:
name: Ruff
runs-on: ubuntu-latest
Expand All @@ -19,6 +26,24 @@ jobs:
pip install ruff
ruff check

mypy:
name: Mypy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
-
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: "Main Script"
run: |
EXTRA_INSTALL="mypy pytest"
curl -L -O https://tiker.net/ci-support-v0
. ./ci-support-v0

build_py_project_in_venv
python -m mypy gmsh_interop test contrib

pytest3:
name: Pytest Conda Py3
runs-on: ubuntu-latest
Expand Down
49 changes: 41 additions & 8 deletions contrib/gmsh-node-tuples.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import os
from collections.abc import Sequence

import gmsh


OUTPUT_TEMPLATE = """# GENERATED by gmsh_interop/contrib/gmsh-node-tuples.py
# GMSH_VERSION: %s
# DO NOT EDIT

triangle_data = %s
from collections.abc import Sequence
from typing import TypedDict


class ElementInfo(TypedDict):
element_name: str
'''Name of the element in GMSH.'''
element_type: int
'''An integer denoting the type of the element.'''
node_tuples: Sequence[tuple[int, ...]]
'''Index tuples that describe the ordering of the nodes.'''


tetrahedron_data = %s
triangle_data: dict[int, ElementInfo] = %s

quadrangle_data = %s
tetrahedron_data: dict[int, ElementInfo] = %s

hexahedron_data = %s
quadrangle_data: dict[int, ElementInfo] = %s

hexahedron_data: dict[int, ElementInfo] = %s
"""


Expand Down Expand Up @@ -70,7 +86,11 @@
}


def generate_node_tuples_from_gmsh(eltype, eldim, elvertices, domain="unit"):
def generate_node_tuples_from_gmsh(
eltype: int,
eldim: int,
elvertices: int,
domain: str = "unit") -> Sequence[tuple[int, ...]]:
# {{{ get element

_name, dim, order, nnodes, nodes, nvertices = (
Expand All @@ -89,10 +109,14 @@ def generate_node_tuples_from_gmsh(eltype, eldim, elvertices, domain="unit"):

# }}}

return [tuple(node) for node in nodes.astype(int)]
return [tuple(int(x) for x in node) for node in nodes.astype(int)]


def generate_node_tuples(filename: str | None, *, overwrite: bool = False) -> int:
if not overwrite and filename is not None and os.path.exists(filename):
print(f"ERROR: File already exists (use --force): '{filename}'")
return 1

def generate_node_tuples(filename):
tri_data = {}
tet_data = {}
qua_data = {}
Expand Down Expand Up @@ -136,6 +160,7 @@ def generate_node_tuples(filename):
gmsh.finalize()

from pprint import pformat

txt = (OUTPUT_TEMPLATE % (
gmsh.GMSH_API_VERSION,
pformat(tri_data, width=80),
Expand All @@ -150,12 +175,20 @@ def generate_node_tuples(filename):
with open(filename, "w") as fd:
fd.write(txt)

return 0


if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("filename", nargs="?", default=None)
parser.add_argument(
"-f",
"--force",
action="store_true",
help="Overwrite existing files",
)
args = parser.parse_args()

generate_node_tuples(args.filename)
raise SystemExit(generate_node_tuples(args.filename, overwrite=args.force))
2 changes: 1 addition & 1 deletion doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SPHINXBUILD ?= python $(shell which sphinx-build)
SOURCEDIR = .
BUILDDIR = _build

Expand Down
23 changes: 18 additions & 5 deletions gmsh_interop/node_tuples.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
# GENERATED by gmsh_interop/contrib/gmsh-node-tuples.py
# GMSH_VERSION: 4.7.0
# GMSH_VERSION: 4.13.1
# DO NOT EDIT

triangle_data = {
from collections.abc import Sequence
from typing import TypedDict


class ElementInfo(TypedDict):
element_name: str
"""Name of the element in GMSH."""
element_type: int
"""An integer denoting the type of the element."""
node_tuples: Sequence[tuple[int, ...]]
"""Index tuples that describe the ordering of the nodes."""


triangle_data: dict[int, ElementInfo] = {
1: {
"element_name": "MSH_TRI_3",
"element_type": 2,
Expand Down Expand Up @@ -339,7 +352,7 @@
},
}

tetrahedron_data = {
tetrahedron_data: dict[int, ElementInfo] = {
1: {
"element_name": "MSH_TET_4",
"element_type": 4,
Expand Down Expand Up @@ -1397,7 +1410,7 @@
},
}

quadrangle_data = {
quadrangle_data: dict[int, ElementInfo] = {
1: {
"element_name": "MSH_QUA_4",
"element_type": 3,
Expand Down Expand Up @@ -1960,7 +1973,7 @@
},
}

hexahedron_data = {
hexahedron_data: dict[int, ElementInfo] = {
1: {
"element_name": "MSH_HEX_8",
"element_type": 5,
Expand Down
Empty file added gmsh_interop/py.typed
Empty file.
Loading
Loading