Skip to content

Commit

Permalink
Fix warnings related to firedrake changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcallaham committed Feb 20, 2024
1 parent 1bf6be5 commit f05de2b
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 51 deletions.
16 changes: 7 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"sphinx.ext.intersphinx",
"sphinx.ext.viewcode",
"sphinx.ext.napoleon",
"sphinx.ext.mathjax"
"sphinx.ext.mathjax",
]

intersphinx_mapping = {
Expand All @@ -44,14 +44,12 @@
html_static_path = ["_static"]

html_theme_options = {
'repository_url': 'https://github.com/dynamicslab/hydrogym',
'use_repository_button': True, # add a 'link to repository' button
'use_issues_button': True, # add an 'Open an Issue' button
'path_to_docs': (
'docs'
), # used to compute the path to launch notebooks in colab
'prev_next_buttons_location': None,
'show_navbar_depth': 1,
"repository_url": "https://github.com/dynamicslab/hydrogym",
"use_repository_button": True, # add a 'link to repository' button
"use_issues_button": True, # add an 'Open an Issue' button
"path_to_docs": ("docs"), # used to compute the path to launch notebooks in colab
"prev_next_buttons_location": None,
"show_navbar_depth": 1,
}

# -- Options for EPUB output
Expand Down
Binary file modified examples/demo/checkpoint.h5
Binary file not shown.
22 changes: 7 additions & 15 deletions hydrogym/firedrake/envs/cavity/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,11 @@ def init_bcs(self, mixed=False):
self.bcu_freestream = fd.DirichletBC(
V.sub(1), fd.Constant(0.0), self.FREESTREAM
)
self.bcu_noslip = fd.DirichletBC(
# V, fd.interpolate(fd.Constant((0, 0)), V), (self.WALL, self.SENSOR)
V,
fd.interpolate(fd.Constant((0, 0)), V),
self.WALL,
)
self.bcu_slip = fd.DirichletBC(
V.sub(1), fd.Constant(0.0), self.SLIP
) # Free-slip
self.bcu_noslip = fd.DirichletBC(V, fd.Constant((0, 0)), self.WALL)
# Free-slip on top boundary
self.bcu_slip = fd.DirichletBC(V.sub(1), fd.Constant(0.0), self.SLIP)
self.bcp_outflow = fd.DirichletBC(Q, fd.Constant(0), self.OUTLET)
self.bcu_actuation = [
fd.DirichletBC(V, fd.interpolate(fd.Constant((0, 0)), V), self.CONTROL)
]
self.bcu_actuation = [fd.DirichletBC(V, fd.Constant((0, 0)), self.CONTROL)]

self.set_control(self.control_state)

Expand All @@ -82,7 +74,7 @@ def get_observations(self, q=None):
"""Integral of wall-normal shear stress (see Barbagallo et al, 2009)"""
if q is None:
q = self.q
(u, p) = q.split()
u = q.subfunctions[0]
m = fd.assemble(-dot(grad(u[0]), self.n) * ds(self.SENSOR))
return (m,)

Expand All @@ -91,7 +83,7 @@ def evaluate_objective(self, q=None, qB=None):
q = self.q
if qB is None:
qB = self.qB
(u, p) = q.split()
(uB, pB) = qB.split()
u = q.subfunctions[0]
uB = qB.subfunctions[0]
KE = 0.5 * fd.assemble(fd.inner(u - uB, u - uB) * fd.dx)
return KE
8 changes: 3 additions & 5 deletions hydrogym/firedrake/envs/cylinder/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import ufl
from firedrake import ds
from firedrake.pyplot import tricontourf
from ufl import as_vector, atan2, cos, dot, sign, sin, sqrt

from hydrogym.firedrake import DampedActuator, FlowConfig
Expand Down Expand Up @@ -48,13 +49,10 @@ def init_bcs(self, mixed=False):

# Define actual boundary conditions
self.bcu_inflow = fd.DirichletBC(V, self.U_inf, self.INLET)
# self.bcu_freestream = fd.DirichletBC(V, self.U_inf, self.FREESTREAM)
self.bcu_freestream = fd.DirichletBC(
V.sub(1), fd.Constant(0.0), self.FREESTREAM
) # Symmetry BCs
self.bcu_actuation = [
fd.DirichletBC(V, fd.interpolate(fd.Constant((0, 0)), V), self.CYLINDER)
]
self.bcu_actuation = [fd.DirichletBC(V, fd.Constant((0, 0)), self.CYLINDER)]
self.bcp_outflow = fd.DirichletBC(Q, fd.Constant(0), self.OUTLET)

# Reset the control with the current mixed (or not) function spaces
Expand Down Expand Up @@ -169,7 +167,7 @@ def render(self, mode="human", clim=None, levels=None, cmap="RdBu", **kwargs):
if levels is None:
levels = np.linspace(*clim, 10)
vort = fd.project(fd.curl(self.u), self.pressure_space)
im = fd.tricontourf(
im = tricontourf(
vort,
cmap=cmap,
levels=levels,
Expand Down
6 changes: 3 additions & 3 deletions hydrogym/firedrake/envs/pinball/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import ufl
from firedrake import ds
from firedrake.pyplot import tricontourf
from ufl import atan2, cos, dot, sin

from hydrogym.firedrake import DampedActuator, FlowConfig
Expand Down Expand Up @@ -60,8 +61,7 @@ def init_bcs(self, mixed=False):
V.sub(1), fd.Constant(0.0), self.FREESTREAM
) # Symmetry BCs
self.bcu_actuation = [
fd.DirichletBC(V, fd.interpolate(fd.Constant((0, 0)), V), cyl)
for cyl in self.CYLINDER
fd.DirichletBC(V, fd.Constant((0.0, 0.0)), cyl) for cyl in self.CYLINDER
]
self.bcp_outflow = fd.DirichletBC(Q, fd.Constant(0), self.OUTLET)

Expand Down Expand Up @@ -110,7 +110,7 @@ def render(self, mode="human", clim=None, levels=None, cmap="RdBu", **kwargs):
if levels is None:
levels = np.linspace(*clim, 10)
vort = fd.project(fd.curl(self.flow.u), self.flow.pressure_space)
im = fd.tricontourf(
im = tricontourf(
vort,
cmap=cmap,
levels=levels,
Expand Down
12 changes: 5 additions & 7 deletions hydrogym/firedrake/envs/step/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ def init_bcs(self, mixed=False):
# Define actual boundary conditions
self.bcu_inflow = fd.DirichletBC(V, self.U_inf, self.INLET)
self.bcu_noslip = fd.DirichletBC(
V, fd.interpolate(fd.Constant((0, 0)), V), (self.WALL, self.SENSOR)
V, fd.Constant((0, 0)), (self.WALL, self.SENSOR)
)
self.bcp_outflow = fd.DirichletBC(Q, fd.Constant(0), self.OUTLET)
self.bcu_actuation = [
fd.DirichletBC(V, fd.interpolate(fd.Constant((0, 0)), V), self.CONTROL)
]
self.bcu_actuation = [fd.DirichletBC(V, fd.Constant((0, 0)), self.CONTROL)]

self.set_control(self.control_state)

Expand All @@ -82,7 +80,7 @@ def get_observations(self, q=None):
"""Integral of wall-normal shear stress (see Barbagallo et al, 2009)"""
if q is None:
q = self.q
(u, p) = q.split()
u = q.subfunctions[0]
m = fd.assemble(-dot(grad(u[0]), self.n) * ds(self.SENSOR))
return (m,)

Expand All @@ -91,7 +89,7 @@ def evaluate_objective(self, q=None, qB=None):
q = self.q
if qB is None:
qB = self.qB
(u, p) = q.split()
(uB, pB) = qB.split()
u = q.subfunctions[0]
uB = qB.subfunctions[0]
KE = 0.5 * fd.assemble(fd.inner(u - uB, u - uB) * fd.dx)
return KE
21 changes: 13 additions & 8 deletions hydrogym/firedrake/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pyadjoint
import ufl
from firedrake import dx, logging
from firedrake.__future__ import interpolate
from mpi4py import MPI
from ufl import curl, dot, inner, nabla_grad, sqrt, sym

Expand Down Expand Up @@ -126,7 +127,7 @@ def nu(self):
return fd.Constant(1 / ufl.real(self.Re))

def split_solution(self):
self.u, self.p = self.q.split()
self.u, self.p = self.q.subfunctions
self.u.rename("u")
self.p.rename("p")

Expand Down Expand Up @@ -190,12 +191,14 @@ def sigma(self, u, p) -> ufl.Form:
def max_cfl(self, dt) -> float:
"""Estimate of maximum CFL number"""
h = fd.CellSize(self.mesh)
CFL = fd.interpolate(dt * sqrt(dot(self.u, self.u)) / h, self.pressure_space)
CFL = fd.assemble(
interpolate(dt * sqrt(dot(self.u, self.u)) / h, self.pressure_space)
)
return self.mesh.comm.allreduce(CFL.vector().max(), op=MPI.MAX)

@property
def body_force(self):
return fd.interpolate(fd.Constant((0.0, 0.0)), self.velocity_space)
return fd.Function(self.velocity_space).assign(fd.Constant((0.0, 0.0)))

def linearize_bcs(self):
"""Sets the boundary conditions appropriately for linearized flow"""
Expand All @@ -215,9 +218,10 @@ def set_control(self, act: ActType = None):
if hasattr(self, "bcu_actuation"):
for i in range(self.ACT_DIM):
c = fd.Constant(self.actuators[i].get_state())
self.bcu_actuation[i]._function_arg.assign(
fd.interpolate(c * self.u_ctrl[i], self.velocity_space)
bc_function = fd.assemble(
interpolate(c * self.u_ctrl[i], self.velocity_space)
)
self.bcu_actuation[i]._function_arg.assign(bc_function)

def control_vec(self, mixed=False):
"""Return a list of PETSc.Vecs corresponding to the columns of the control matrix"""
Expand All @@ -226,7 +230,8 @@ def control_vec(self, mixed=False):
B = []
for i, bcu in enumerate(self.bcu_actuation):
domain = bcu.sub_domain
bcs = [fd.DirichletBC(V, fd.interpolate(self.u_ctrl[i], V), domain)]
bc_function = fd.assemble(interpolate(self.u_ctrl[i], V))
bcs = [fd.DirichletBC(V, bc_function, domain)]

# Control as Function
B.append(fd.project(fd.Constant((0, 0)), V, bcs=bcs))
Expand All @@ -235,6 +240,6 @@ def control_vec(self, mixed=False):

def dot(self, q1: fd.Function, q2: fd.Function) -> float:
"""Energy inner product between two fields"""
u1, _ = q1.split()
u2, _ = q2.split()
u1 = q1.subfunctions[0]
u2 = q2.subfunctions[0]
return fd.assemble(inner(u1, u2) * dx)
4 changes: 2 additions & 2 deletions hydrogym/firedrake/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def linearize(self, qB=None, return_operators=True):
if qB is None:
uB = flow.u.copy(deepcopy=True)
else:
uB = qB.split()[0].copy(deepcopy=True)
uB = qB.subfunctions[0].copy(deepcopy=True)

flow.linearize_bcs(mixed=False)
(u, p) = self.q_trial
Expand Down Expand Up @@ -283,7 +283,7 @@ def linearize(self, qB=None, return_operators=True):

def matvec(q_vec, mode):
set_from_array(q, q_vec)
u, p = q.split()
u, p = q.subfunctions
self.u_n.assign(q.sub(0))
self.p_n.assign(q.sub(1))

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 @@ -163,7 +163,7 @@ def pod(
if pvd_dest is not None:
pvd = fd.File(f"{output_dir}/{pvd_dest}", "w")
for i, mode in enumerate(mode_handles):
u, p = mode.get().as_function().split()
u, p = mode.get().as_function().subfunctions
pvd.write(u, p, flow.vorticity(u))

# Compute temporal coefficients
Expand Down
2 changes: 1 addition & 1 deletion hydrogym/firedrake/utils/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def snapshots_to_numpy(flow, filename, save_prefix, m):
for idx in range(m):
logging.log(logging.DEBUG, f"Converting snapshot {idx+1}/{m}")
q = file.load_function(mesh, "q", idx=idx) # Load on different mesh
u, p = q.split()
u, p = q.subfunctions

# Project to new mesh
flow.u.assign(fd.project(u, flow.velocity_space))
Expand Down

0 comments on commit f05de2b

Please sign in to comment.