Skip to content

Commit

Permalink
Merge pull request #109 from dynamicslab/lp/ruff-patch
Browse files Browse the repository at this point in the history
Addition of GitHub Actions + Switch of Linter
  • Loading branch information
ludgerpaehler authored Nov 26, 2023
2 parents ae22586 + 2b5efe3 commit 989194b
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 28 deletions.
4 changes: 0 additions & 4 deletions .flake8

This file was deleted.

13 changes: 13 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Black Formatting

on: [push, pull_request]

jobs:
linter_name:
name: runner / black formatter
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: rickstaa/action-black@v1
with:
black_args: ". --check"
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test Build of Package

on: [push, pull_request]

permissions:
contents: read

jobs:
package-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: 3.9
- name: Install Poetry
run: |
python -m pip install --upgrade pip
pip install poetry
poetry config virtualenvs.in-project false
poetry config virtualenvs.path ~/.virtualenvs
poetry config pypi-token.pypi ${{ secrets.PYPI_API_TOKEN }}
- name: Build package
run: |
poetry build
10 changes: 10 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Linting with Ruff

on: [push, pull_request]

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
8 changes: 5 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ repos:
hooks:
- id: isort
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.6
hooks:
- id: flake8
# Run the linter.
- id: ruff
5 changes: 2 additions & 3 deletions examples/cavity/rllib/ppo_train.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Modified from https://github.com/ray-project/ray/blob/master/rllib/examples/custom_env.py
import os

import ray

# from common import *
Expand All @@ -23,9 +24,7 @@

# Can also register the env creator function explicitly with:
# register_env("corridor", lambda config: SimpleCorridor(config))
ModelCatalog.register_custom_model(
"cav_actor", TorchCustomModel
)
ModelCatalog.register_custom_model("cav_actor", TorchCustomModel)

# Set up the printing callback
log = hydrogym.io.LogCallback(
Expand Down
2 changes: 1 addition & 1 deletion examples/cavity/solve-steady.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
dof = flow.mixed_space.dim()
hgym.print(f"Total dof: {dof} --- dof/rank: {int(dof/fd.COMM_WORLD.size)}")

for (i, Re) in enumerate(Re_init):
for i, Re in enumerate(Re_init):
flow.Re.assign(Re)
hgym.print(f"Steady solve at Re={Re_init[i]}")
solver = hgym.NewtonSolver(flow, solver_parameters=solver_parameters)
Expand Down
2 changes: 1 addition & 1 deletion examples/cavity/unsteady.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# ramp to get the steady state
Re_init = [500, 1000, 2000, 4000, Re]

for (i, Re) in enumerate(Re_init):
for i, Re in enumerate(Re_init):
flow.Re.assign(Re)
hgym.print(f"Steady solve at Re={Re_init[i]}")
solver = hgym.NewtonSolver(flow, solver_parameters=solver_parameters)
Expand Down
2 changes: 1 addition & 1 deletion examples/step/solve-steady.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
dof = flow.mixed_space.dim()
hgym.print(f"Total dof: {dof} --- dof/rank: {int(dof/fd.COMM_WORLD.size)}")

for (i, Re) in enumerate(Re_init):
for i, Re in enumerate(Re_init):
flow.Re.assign(Re)
hgym.print(f"Steady solve at Re={Re_init[i]}")
solver = hgym.NewtonSolver(flow, solver_parameters=solver_parameters)
Expand Down
2 changes: 1 addition & 1 deletion examples/step/unsteady.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# ramp to get the steady state
Re_init = np.arange(100, Re + 100, 100, dtype=float)

for (i, Re) in enumerate(Re_init):
for i, Re in enumerate(Re_init):
flow.Re.assign(Re)
hgym.print(f"Steady solve at Re={Re_init[i]}")
solver = hgym.NewtonSolver(flow, solver_parameters=solver_parameters)
Expand Down
5 changes: 4 additions & 1 deletion hydrogym/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ def render(self, **kwargs):
"""Plot the current PDE state (called by `gym.Env`)"""
raise NotImplementedError


'''
@ray.remote
class EvaluationActor:
"""To remotely evaluate Firedrake solutions."""
def __init__(self, firedrake_instance: "Firedrake_instance", index: int, seeds: Union[ActorSeeds, tuple], state:dict):
def __init__(self, firedrake_instance: "Firedrake_instance", index: int, seeds:
Union[ActorSeeds, tuple], state:dict):
"""
Initialize a remote runner for a Firedrake problem instance.
Expand All @@ -217,6 +219,7 @@ def __init__(self, firedrake_instance: "Firedrake_instance", index: int, seeds:
# code here running.
'''


class CallbackBase:
def __init__(self, interval: int = 1):
"""
Expand Down
4 changes: 2 additions & 2 deletions hydrogym/firedrake/envs/pinball/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Pinball(FlowConfig):
y0 = [0.0, 0.5 * rad, -0.5 * rad]

ACT_DIM = len(CYLINDER)
OBS_DIM = 2 * len(CYLINDER) # [CL, CD] for each cyliner
OBS_DIM = 2 * len(CYLINDER) # [CL, CD] for each cyliner
MAX_CONTROL = 0.5 * np.pi
TAU = 1.0 # TODO: Tune this based on vortex shedding period
I_CM = 1.0 # Moment of inertia
Expand Down Expand Up @@ -120,6 +120,6 @@ def render(self, mode="human", clim=None, levels=None, cmap="RdBu", **kwargs):
**kwargs,
)

for (x0, y0) in zip(self.flow.x0, self.flow.y0):
for x0, y0 in zip(self.flow.x0, self.flow.y0):
cyl = plt.Circle((x0, y0), self.flow.rad, edgecolor="k", facecolor="gray")
im.axes.add_artist(cyl)
4 changes: 3 additions & 1 deletion hydrogym/firedrake/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ def control_vec(self, mixed=False):

# Control as Function
B.append(
fd.assemble(inner(fd.Constant((0, 0)), v) * dx, bcs=self.collect_bcs()).riesz_representation(riesz_map = 'l2')
fd.assemble(
inner(fd.Constant((0, 0)), v) * dx, cs=self.collect_bcs()
).riesz_representation(riesz_map="l2")
)

# Have to have mixed function space for computing B functions
Expand Down
9 changes: 4 additions & 5 deletions hydrogym/firedrake/solver.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import firedrake as fd
import numpy as np
from firedrake import logging
from ufl import div, dot, ds, dx, inner, lhs, nabla_grad, rhs, as_ufl

import ufl
from firedrake import logging
from ufl import as_ufl, div, dot, ds, dx, inner, lhs, nabla_grad, rhs

from hydrogym.core import TransientSolver

Expand Down Expand Up @@ -185,7 +184,7 @@ def step(self, iter, control=None):
self.predictor.solve()
if control is not None:
control = self.flow.update_actuators(control, self.dt)
for (B, ctrl) in zip(self.B, control):
for B, ctrl in zip(self.B, control):
Bu, _ = B.split()
self.u += Bu * fd.Constant(ctrl)

Expand Down Expand Up @@ -303,7 +302,7 @@ def rmatvec(q_vec):
N = q.vector().size()
A = LinearOperator(shape=(N, N), matvec=lmatvec, rmatvec=rmatvec)
B = np.zeros((N, len(self.B)))
for (i, Bi) in enumerate(self.B):
for i, Bi in enumerate(self.B):
B[:, i] = get_array(Bi)
return A, B

Expand Down
2 changes: 1 addition & 1 deletion hydrogym/firedrake/utils/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def pod(
# Save for visualization
if pvd_dest is not None:
pvd = fd.File(f"{output_dir}/{pvd_dest}", "w")
for (i, mode) in enumerate(mode_handles):
for i, mode in enumerate(mode_handles):
u, p = mode.get().as_function().split()
pvd.write(u, p, flow.vorticity(u))

Expand Down
11 changes: 9 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hydrogym"
version = "0.1.2.1"
version = "0.1.2.2"
authors = [
"Jared Callaham et al."
]
Expand Down Expand Up @@ -51,7 +51,7 @@ gmsh = "^4.11.1"
gym = "^0.26.2"
modred = "^2.1.0"
python = "^3.10"
torch = "^2.0.0"
torch = "^2.0"

[tool.poetry.group.dev.dependencies]
jupyterlab = "^3.5.2"
Expand All @@ -64,13 +64,20 @@ nbconvert = "^7.2.7"
memory-profiler = "^0.61.0"
seaborn = "^0.12.1"
tensorboard = "^2.11.0"
ruff = "^0.1.6"
black = "^23.11.0"

# In case one seeks to add a further backend, their further dependencies need to be added to the
# poetry dependencies above, as well as be specified here s.t. these additional dependencies
# can then later be installed with ```pip install hydrogym[sim_backend]```
[tool.poetry.extras]
#sim_backend = ["sim_framework"]

[tool.ruff]
line-length = 120
select = ["E", "F"]
ignore = ["F401"]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
6 changes: 4 additions & 2 deletions test/test_cyl.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def test_steady_rotation(tol=1e-3):
assert abs(CL - 0.0594) < tol
assert abs(CD - 1.2852) < tol # Re = 100

'''

"""
def test_steady_grad():
flow = hgym.Cylinder(Re=100, mesh="coarse")
Expand All @@ -57,7 +58,8 @@ def test_steady_grad():
dJ = fda.compute_gradient(J, fda.Control(omega))
assert abs(dJ) > 0
'''
"""


def test_integrate():
flow = hgym.Cylinder(mesh="coarse")
Expand Down

0 comments on commit 989194b

Please sign in to comment.