Skip to content

Commit

Permalink
Replace Constants with Functions from R-space
Browse files Browse the repository at this point in the history
  • Loading branch information
jwallwork23 committed Oct 13, 2023
1 parent 6f27698 commit 23b5e63
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 66 deletions.
7 changes: 4 additions & 3 deletions demos/burgers-hessian.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ def get_form(mesh_seq):
def form(index, solutions):
u, u_ = solutions["u"]
P = mesh_seq.time_partition
dt = Constant(P.timesteps[index])

# Specify viscosity coefficient
nu = Constant(0.0001)
# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(P.timesteps[index])
nu = Function(R).assign(0.0001)

# Setup variational problem
v = TestFunction(u.function_space())
Expand Down
10 changes: 6 additions & 4 deletions demos/burgers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ def get_function_spaces(mesh):
#
# Timestepping information associated with a given subinterval
# can be accessed via the :attr:`TimePartition` attribute of
# the :class:`MeshSeq`. ::
# the :class:`MeshSeq`. For technical reasons, we need to create a :class:`Function`
# in the `'R'` space (of real numbers) to hold constants. ::


def get_form(mesh_seq):
def form(index, solutions):
u, u_ = solutions["u"]
P = mesh_seq.time_partition
dt = Constant(P.timesteps[index])

# Specify viscosity coefficient
nu = Constant(0.0001)
# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(P.timesteps[index])
nu = Function(R).assign(0.0001)

# Setup variational problem
v = TestFunction(u.function_space())
Expand Down
7 changes: 4 additions & 3 deletions demos/burgers1.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ def get_form(mesh_seq):
def form(index, solutions):
u, u_ = solutions["u"]
P = mesh_seq.time_partition
dt = Constant(P.timesteps[index])

# Specify viscosity coefficient
nu = Constant(0.0001)
# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(P.timesteps[index])
nu = Function(R).assign(0.0001)

# Setup variational problem
v = TestFunction(u.function_space())
Expand Down
7 changes: 4 additions & 3 deletions demos/burgers2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ def get_form(mesh_seq):
def form(index, solutions):
u, u_ = solutions["u"]
P = mesh_seq.time_partition
dt = Constant(P.timesteps[index])

# Specify viscosity coefficient
nu = Constant(0.0001)
# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(P.timesteps[index])
nu = Function(R).assign(0.0001)

# Setup variational problem
v = TestFunction(u.function_space())
Expand Down
7 changes: 4 additions & 3 deletions demos/burgers_ee.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ def get_form(mesh_seq):
def form(index, solutions):
u, u_ = solutions["u"]
P = mesh_seq.time_partition
dt = Constant(P.timesteps[index])

# Specify viscosity coefficient
nu = Constant(0.0001)
# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(P.timesteps[index])
nu = Function(R).assign(0.0001)

# Setup variational problem
v = TestFunction(u.function_space())
Expand Down
10 changes: 6 additions & 4 deletions demos/burgers_oo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ def get_form(self):
def form(index, solutions):
u, u_ = solutions["u"]
P = self.time_partition
dt = Constant(P.timesteps[index])

# Specify viscosity coefficient
nu = Constant(0.0001)
# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(P.timesteps[index])
nu = Function(R).assign(0.0001)

# Setup variational problem
v = TestFunction(u.function_space())
Expand Down Expand Up @@ -84,7 +85,8 @@ def get_initial_condition(self):

@annotate_qoi
def get_qoi(self, solutions, i):
dt = Constant(self.time_partition[i].timestep)
R = FunctionSpace(self[i], "R", 0)
dt = Function(R).assign(self.time_partition[i].timestep)

def end_time_qoi():
u = solutions["u"]
Expand Down
12 changes: 7 additions & 5 deletions demos/burgers_time_integrated.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ def get_form(mesh_seq):
def form(index, solutions):
u, u_ = solutions["u"]
P = mesh_seq.time_partition
dt = Constant(P.timesteps[index])

# Specify viscosity coefficient
nu = Constant(0.0001)
# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(P.timesteps[index])
nu = Function(R).assign(0.0001)

# Setup variational problem
v = TestFunction(u.function_space())
Expand Down Expand Up @@ -93,12 +94,13 @@ def solver(index, ic):
# \;\mathrm dy\;\mathrm dt.
#
# Note that in this case we multiply by the timestep.
# It is wrapped in a :class:`Constant` to avoid
# It is wrapped in a :class:`Function` from `'R'` space to avoid
# recompilation if the value is changed. ::


def get_qoi(mesh_seq, solutions, i):
dt = Constant(mesh_seq.time_partition[i].timestep)
R = FunctionSpace(mesh_seq[i], "R", 0)
dt = Function(R).assign(mesh_seq.time_partition[i].timestep)

def time_integrated_qoi(t):
u = solutions["u"]
Expand Down
11 changes: 6 additions & 5 deletions demos/gray_scott.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ def form(index, sols):
psi_a, psi_b = TestFunctions(mesh_seq.function_spaces["ab"][index])

# Define constants
dt = Constant(mesh_seq.time_partition[index].timestep)
D_a = Constant(8.0e-05)
D_b = Constant(4.0e-05)
gamma = Constant(0.024)
kappa = Constant(0.06)
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(mesh_seq.time_partition[index].timestep)
D_a = Function(R).assign(8.0e-05)
D_b = Function(R).assign(4.0e-05)
gamma = Function(R).assign(0.024)
kappa = Function(R).assign(0.06)

# Write the two equations in variational form
F = (
Expand Down
11 changes: 6 additions & 5 deletions demos/gray_scott_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ def form(index, sols):
psi_b = TestFunction(mesh_seq.function_spaces["b"][index])

# Define constants
dt = Constant(mesh_seq.time_partition[index].timestep)
D_a = Constant(8.0e-05)
D_b = Constant(4.0e-05)
gamma = Constant(0.024)
kappa = Constant(0.06)
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(mesh_seq.time_partition[index].timestep)
D_a = Function(R).assign(8.0e-05)
D_b = Function(R).assign(4.0e-05)
gamma = Function(R).assign(0.024)
kappa = Function(R).assign(0.06)

# Write the two equations in variational form
F_a = (
Expand Down
9 changes: 7 additions & 2 deletions demos/point_discharge2d-goal_oriented.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ def get_form(mesh_seq):
def form(index, sols):
c, c_ = sols["c"]
function_space = mesh_seq.function_spaces["c"][index]
D = Constant(0.1)
u = Constant(as_vector([1, 0]))
h = CellSize(mesh_seq[index])
S = source(mesh_seq[index])

# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
D = Function(R).assign(0.1)
u_x = Function(R).assign(1.0)
u_y = Function(R).assign(0.0)
u = as_vector([u_x, u_y])

# SUPG stabilisation parameter
unorm = sqrt(dot(u, u))
tau = 0.5 * h / unorm
Expand Down
9 changes: 7 additions & 2 deletions demos/point_discharge2d-hessian.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ def get_form(mesh_seq):
def form(index, sols):
c, c_ = sols["c"]
function_space = mesh_seq.function_spaces["c"][index]
D = Constant(0.1)
u = Constant(as_vector([1, 0]))
h = CellSize(mesh_seq[index])
S = source(mesh_seq[index])

# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
D = Function(R).assign(0.1)
u_x = Function(R).assign(1.0)
u_y = Function(R).assign(0.0)
u = as_vector([u_x, u_y])

# SUPG stabilisation parameter
unorm = sqrt(dot(u, u))
tau = 0.5 * h / unorm
Expand Down
9 changes: 7 additions & 2 deletions demos/point_discharge2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ def get_form(mesh_seq):
def form(index, sols):
c, c_ = sols["c"]
function_space = mesh_seq.function_spaces["c"][index]
D = Constant(0.1)
u = Constant(as_vector([1, 0]))
h = CellSize(mesh_seq[index])
S = source(mesh_seq[index])

# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
D = Function(R).assign(0.1)
u_x = Function(R).assign(1.0)
u_y = Function(R).assign(0.0)
u = as_vector([u_x, u_y])

# SUPG stabilisation parameter
unorm = sqrt(dot(u, u))
tau = 0.5 * h / unorm
Expand Down
8 changes: 6 additions & 2 deletions demos/solid_body_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,14 @@ def form(index, sols, field="c"):
V = mesh_seq.function_spaces[field][index]
mesh = mesh_seq[index]

# Define velocity field
x, y = SpatialCoordinate(mesh)
u = as_vector([-y, x])
dt = Constant(mesh_seq.time_partition[index].timestep)
theta = Constant(0.5)

# Define constants
R = FunctionSpace(mesh_seq[index], "R", 0)
dt = Function(R).assign(mesh_seq.time_partition[index].timestep)
theta = Function(R).assign(0.5)

psi = TrialFunction(V)
phi = TestFunction(V)
Expand Down
8 changes: 5 additions & 3 deletions test_adjoint/examples/burgers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ def form(i, sols):
u, u_ = sols["uv_2d"]
dt = self.time_partition[i].timestep
fs = self.function_spaces["uv_2d"][i]
dtc = Constant(dt)
nu = Constant(0.0001)
R = FunctionSpace(self[i], "R", 0)
dtc = Function(R).assign(dt)
nu = Function(R).assign(0.0001)
v = TestFunction(fs)
F = (
inner((u - u_) / dtc, v) * dx
Expand Down Expand Up @@ -104,7 +105,8 @@ def get_qoi(self, sol, i):
norm over the right hand
boundary.
"""
dtc = Constant(self.time_partition[i].timestep)
R = FunctionSpace(self[i], "R", 0)
dtc = Function(R).assign(self.time_partition[i].timestep)

def time_integrated_qoi(t):
u = sol["uv_2d"]
Expand Down
15 changes: 9 additions & 6 deletions test_adjoint/examples/point_discharge2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ def source(mesh):

def get_form(self):
"""
Advection-diffusion with SUPG
stabilisation.
Advection-diffusion with SUPG stabilisation.
"""

def form(i, sols):
c, c_ = sols["tracer_2d"]
fs = self.function_spaces["tracer_2d"][i]
D = Constant(0.1)
u = Constant(as_vector([1.0, 0.0]))
R = FunctionSpace(self[i], "R", 0)
D = Function(R).assign(0.1)
u_x = Function(R).assign(1.0)
u_y = Function(R).assign(0.0)
u = as_vector([u_x, u_y])
h = CellSize(self[i])
S = source(self[i])

Expand Down Expand Up @@ -151,8 +153,9 @@ def analytical_solution(mesh):
a given mesh. See [Riadh et al. 2014].
"""
x, y = SpatialCoordinate(mesh)
u = Constant(1.0)
D = Constant(0.1)
R = FunctionSpace(mesh, "R", 0)
u = Function(R).assign(1.0)
D = Function(R).assign(0.1)
Pe = 0.5 * u / D
r = max_value(sqrt((x - src_x) ** 2 + (y - src_y) ** 2), src_r)
return 0.5 / (pi * D) * exp(Pe * (x - src_x)) * bessk0(Pe * r)
13 changes: 9 additions & 4 deletions test_adjoint/examples/point_discharge3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ def get_form(self):
def form(i, sols):
c, c_ = sols["tracer_3d"]
fs = self.function_spaces["tracer_3d"][i]
D = Constant(0.1)
u = Constant(as_vector([1.0, 0.0, 0.0]))
R = FunctionSpace(self[i], "R", 0)
D = Function(R).assign(0.1)
u_x = Function(R).assign(1.0)
u_y = Function(R).assign(0.0)
u_z = Function(R).assign(0.0)
u = as_vector([u_x, u_y, u_z])
h = CellSize(self[i])
S = source(self[i])

Expand Down Expand Up @@ -164,8 +168,9 @@ def analytical_solution(mesh):
a given mesh.
"""
x, y, z = SpatialCoordinate(mesh)
u = Constant(1.0)
D = Constant(0.1)
R = FunctionSpace(mesh, "R", 0)
u = Function(R).assign(1.0)
D = Function(R).assign(0.1)
Pe = 0.5 * u / D
r = max_value(sqrt((x - src_x) ** 2 + (y - src_y) ** 2 + (z - src_z) ** 2), src_r)
return 0.5 / (pi * D) * exp(Pe * (x - src_x)) * bessk0(Pe * r)
3 changes: 2 additions & 1 deletion test_adjoint/examples/steady_flow_past_cyl.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def get_form(self):
def form(i, sols):
up, up_ = sols["up"]
W = self.function_spaces["up"][i]
nu = Constant(1.0)
R = FunctionSpace(self[i], "R", 0)
nu = Function(R).assign(1.0)
u, p = split(up)
v, q = TestFunctions(W)
F = (
Expand Down
7 changes: 3 additions & 4 deletions test_adjoint/test_fp_iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ def solver(index, ic):


def get_qoi(mesh_seq, solutions, index):
R = FunctionSpace(mesh_seq[index], "R", 0)

def qoi():
if mesh_seq.fp_iteration % 2 == 0:
return Constant(1, domain=mesh_seq[index]) * dx
else:
return Constant(2, domain=mesh_seq[index]) * dx
return Function(R).assign(1 if mesh_seq.fp_iteration % 2 == 0 else 2) * dx

return qoi

Expand Down
Loading

0 comments on commit 23b5e63

Please sign in to comment.