Skip to content

Commit

Permalink
Avoid interpolation deprecation warning (#129)
Browse files Browse the repository at this point in the history
Closes #105.
  • Loading branch information
ddundo committed Mar 16, 2024
1 parent aa719f4 commit 80da600
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 32 deletions.
2 changes: 1 addition & 1 deletion demos/burgers-hessian.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def solver(index, ic):
def get_initial_condition(mesh_seq):
fs = mesh_seq.function_spaces["u"][0]
x, y = SpatialCoordinate(mesh_seq[0])
return {"u": interpolate(as_vector([sin(pi * x), 0]), fs)}
return {"u": assemble(interpolate(as_vector([sin(pi * x), 0]), fs))}


n = 32
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def solver(index, ic):
def get_initial_condition(mesh_seq):
fs = mesh_seq.function_spaces["u"][0]
x, y = SpatialCoordinate(mesh_seq[0])
return {"u": interpolate(as_vector([sin(pi * x), 0]), fs)}
return {"u": assemble(interpolate(as_vector([sin(pi * x), 0]), fs))}


# Now that we have the above functions defined, we move onto the
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers1.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def solver(index, ic):
def get_initial_condition(mesh_seq):
fs = mesh_seq.function_spaces["u"][0]
x, y = SpatialCoordinate(mesh_seq[0])
return {"u": interpolate(as_vector([sin(pi * x), 0]), fs)}
return {"u": assemble(interpolate(as_vector([sin(pi * x), 0]), fs))}


# In line with the
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers2.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def solver(index, ic):
def get_initial_condition(mesh_seq):
fs = mesh_seq.function_spaces["u"][0]
x, y = SpatialCoordinate(mesh_seq[0])
return {"u": interpolate(as_vector([sin(pi * x), 0]), fs)}
return {"u": assemble(interpolate(as_vector([sin(pi * x), 0]), fs))}


def get_qoi(mesh_seq, solutions, i):
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers_ee.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def solver(index, ic):
def get_initial_condition(mesh_seq):
fs = mesh_seq.function_spaces["u"][0]
x, y = SpatialCoordinate(mesh_seq[0])
return {"u": interpolate(as_vector([sin(pi * x), 0]), fs)}
return {"u": assemble(interpolate(as_vector([sin(pi * x), 0]), fs))}


def get_qoi(mesh_seq, solutions, i):
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers_oo.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def solver(index, ic):
def get_initial_condition(self):
fs = self.function_spaces["u"][0]
x, y = SpatialCoordinate(self[0])
return {"u": interpolate(as_vector([sin(pi * x), 0]), fs)}
return {"u": assemble(interpolate(as_vector([sin(pi * x), 0]), fs))}

@annotate_qoi
def get_qoi(self, solutions, i):
Expand Down
2 changes: 1 addition & 1 deletion demos/burgers_time_integrated.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def form(index, solutions):
def get_initial_condition(mesh_seq):
fs = mesh_seq.function_spaces["u"][0]
x, y = SpatialCoordinate(mesh_seq[0])
return {"u": interpolate(as_vector([sin(pi * x), 0]), fs)}
return {"u": assemble(interpolate(as_vector([sin(pi * x), 0]), fs))}


# The solver needs to be modified slightly in order to take
Expand Down
17 changes: 9 additions & 8 deletions demos/gray_scott_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ def get_initial_condition(mesh_seq):
x, y = SpatialCoordinate(mesh_seq[0])
fs_a = mesh_seq.function_spaces["a"][0]
fs_b = mesh_seq.function_spaces["b"][0]
a_init = Function(fs_a, name="a")
b_init = Function(fs_b, name="b")
b_init.interpolate(
conditional(
And(And(1 <= x, x <= 1.5), And(1 <= y, y <= 1.5)),
0.25 * sin(4 * pi * x) ** 2 * sin(4 * pi * y) ** 2,
0,
b_init = assemble(
interpolate(
conditional(
And(And(1 <= x, x <= 1.5), And(1 <= y, y <= 1.5)),
0.25 * sin(4 * pi * x) ** 2 * sin(4 * pi * y) ** 2,
0,
),
fs_b,
)
)
a_init.interpolate(1 - 2 * b_init)
a_init = assemble(interpolate(1 - 2 * b_init, fs_a))
return {"a": a_init, "b": b_init}


Expand Down
2 changes: 1 addition & 1 deletion demos/solid_body_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_initial_condition(mesh_seq, field="c"):
bell = bell_initial_condition(x, y)
cone = cone_initial_condition(x, y)
slot_cyl = slot_cyl_initial_condition(x, y)
return {field: interpolate(bell + cone + slot_cyl, fs)}
return {field: assemble(interpolate(bell + cone + slot_cyl, fs))}


# Now let's set up the time interval of interest. The `"GOALIE_REGRESSION_TEST"` flag
Expand Down
2 changes: 1 addition & 1 deletion demos/solid_body_rotation_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_initial_condition_split(mesh_seq):
"slot_cyl": slot_cyl_initial_condition,
}
return {
f: interpolate(init[f](x, y), fs[0])
f: assemble(interpolate(init[f](x, y), fs[0]))
for f, fs in mesh_seq.function_spaces.items()
}

Expand Down
1 change: 1 addition & 0 deletions goalie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from goalie.point_seq import * # noqa
from goalie.interpolation import * # noqa
from goalie.error_estimation import * # noqa
from firedrake.__future__ import interpolate # noqa

import numpy as np # noqa
import os # noqa
Expand Down
4 changes: 3 additions & 1 deletion goalie/utility.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""
Utility functions and classes for mesh adaptation.
"""

from collections import OrderedDict
import firedrake
import firedrake.mesh as fmesh
from firedrake.petsc import PETSc
from firedrake.__future__ import interpolate
import mpi4py
import numpy as np
import os
Expand Down Expand Up @@ -49,7 +51,7 @@ def Mesh(arg, **kwargs) -> firedrake.mesh.MeshGeometry:

# Cell size
if dim == 2 and mesh.coordinates.ufl_element().cell == ufl.triangle:
mesh.delta_x = firedrake.interpolate(ufl.CellDiameter(mesh), P0)
mesh.delta_x = firedrake.assemble(interpolate(ufl.CellDiameter(mesh), P0))

return mesh

Expand Down
9 changes: 5 additions & 4 deletions test/test_interpolation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Test interpolation schemes.
"""

from firedrake import *
from goalie import *
from goalie.utility import function2cofunction
Expand Down Expand Up @@ -76,15 +77,15 @@ def test_wrong_number_sub_spaces(self):

def test_project_same_space(self):
Vs = FunctionSpace(self.source_mesh, "CG", 1)
source = interpolate(self.sinusoid(), Vs)
source = assemble(interpolate(self.sinusoid(), Vs))
target = Function(Vs)
project(source, target)
expected = source
self.assertAlmostEqual(errornorm(expected, target), 0)

def test_project_same_space_adjoint(self):
Vs = FunctionSpace(self.source_mesh, "CG", 1)
source = interpolate(self.sinusoid(), Vs)
source = assemble(interpolate(self.sinusoid(), Vs))
source = function2cofunction(source)
target = Cofunction(Vs.dual())
project(source, target)
Expand Down Expand Up @@ -119,7 +120,7 @@ def test_project_same_space_mixed_adjoint(self):
def test_project_same_mesh(self):
Vs = FunctionSpace(self.source_mesh, "CG", 1)
Vt = FunctionSpace(self.source_mesh, "DG", 0)
source = interpolate(self.sinusoid(), Vs)
source = assemble(interpolate(self.sinusoid(), Vs))
target = Function(Vt)
project(source, target)
expected = Function(Vt).project(source)
Expand All @@ -128,7 +129,7 @@ def test_project_same_mesh(self):
def test_project_same_mesh_adjoint(self):
Vs = FunctionSpace(self.source_mesh, "CG", 1)
Vt = FunctionSpace(self.source_mesh, "DG", 0)
source = interpolate(self.sinusoid(), Vs)
source = assemble(interpolate(self.sinusoid(), Vs))
target = Cofunction(Vt.dual())
project(function2cofunction(source), target)
expected = function2cofunction(Function(Vt).project(source))
Expand Down
16 changes: 8 additions & 8 deletions test/test_utility.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Test utility functions.
"""

from firedrake import *
from firedrake.norms import norm as fnorm
from firedrake.norms import errornorm as ferrnorm
Expand Down Expand Up @@ -95,7 +96,7 @@ def setUp(self):
self.mesh = uniform_mesh(2, 4)
self.x, self.y = SpatialCoordinate(self.mesh)
V = FunctionSpace(self.mesh, "CG", 1)
self.f = interpolate(self.x**2 + self.y, V)
self.f = assemble(interpolate(self.x**2 + self.y, V))

def test_boundary_error(self):
with self.assertRaises(NotImplementedError) as cm:
Expand Down Expand Up @@ -150,8 +151,7 @@ def test_consistency_firedrake(self, norm_type):
def test_consistency_hdiv(self):
V = VectorFunctionSpace(self.mesh, "CG", 1)
x, y = SpatialCoordinate(self.mesh)
f = Function(V)
f.interpolate(as_vector([y * y, -x * x]))
f = assemble(interpolate(as_vector([y * y, -x * x]), V))
expected = fnorm(f, norm_type="HDiv")
got = norm(f, norm_type="HDiv")
self.assertAlmostEqual(expected, got)
Expand Down Expand Up @@ -179,8 +179,8 @@ def setUp(self):
self.mesh = uniform_mesh(2, 4)
self.x, self.y = SpatialCoordinate(self.mesh)
V = FunctionSpace(self.mesh, "CG", 1)
self.f = interpolate(self.x**2 + self.y, V)
self.g = interpolate(self.x + self.y**2, V)
self.f = assemble(interpolate(self.x**2 + self.y, V))
self.g = assemble(interpolate(self.x + self.y**2, V))

def test_shape_error(self):
with self.assertRaises(RuntimeError) as cm:
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_zero_scalar(self, norm_type):
def test_zero_hdiv(self):
V = VectorFunctionSpace(self.mesh, "CG", 1)
x, y = SpatialCoordinate(self.mesh)
f = interpolate(as_vector([y * y, -x * x]), V)
f = assemble(interpolate(as_vector([y * y, -x * x]), V))
err = errornorm(f, f, norm_type="HDiv")
self.assertAlmostEqual(err, 0.0)

Expand All @@ -228,8 +228,8 @@ def test_consistency_firedrake(self, norm_type):
def test_consistency_hdiv(self):
V = VectorFunctionSpace(self.mesh, "CG", 1)
x, y = SpatialCoordinate(self.mesh)
f = interpolate(as_vector([y * y, -x * x]), V)
g = interpolate(as_vector([x * y, 1.0]), V)
f = assemble(interpolate(as_vector([y * y, -x * x]), V))
g = assemble(interpolate(as_vector([x * y, 1.0]), V))
expected = ferrnorm(f, g, norm_type="HDiv")
got = errornorm(f, g, norm_type="HDiv")
self.assertAlmostEqual(expected, got)
Expand Down
4 changes: 3 additions & 1 deletion test_adjoint/examples/burgers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
Code here is based on that found at
https://firedrakeproject.org/demos/burgers.py.html
"""

from firedrake import *
from firedrake.__future__ import interpolate


# Problem setup
Expand Down Expand Up @@ -95,7 +97,7 @@ def get_initial_condition(self):
"""
init_fs = self.function_spaces["uv_2d"][0]
x, y = SpatialCoordinate(self.meshes[0])
return {"uv_2d": interpolate(as_vector([sin(pi * x), 0]), init_fs)}
return {"uv_2d": assemble(interpolate(as_vector([sin(pi * x), 0]), init_fs))}


def get_qoi(self, sol, i):
Expand Down
4 changes: 3 additions & 1 deletion test_adjoint/examples/steady_flow_past_cyl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
Code here is based on that found at
https://nbviewer.jupyter.org/github/firedrakeproject/firedrake/blob/master/docs/notebooks/06-pde-constrained-optimisation.ipynb
"""

from firedrake import *
from firedrake.__future__ import interpolate
import os


Expand Down Expand Up @@ -63,7 +65,7 @@ def bcs(i):
u_inflow = as_vector([y * (10 - y) / 25.0, 0])
W = self.function_spaces["up"][i]
noslip = DirichletBC(W.sub(0), (0, 0), (3, 5))
inflow = DirichletBC(W.sub(0), interpolate(u_inflow, W.sub(0)), 1)
inflow = DirichletBC(W.sub(0), assemble(interpolate(u_inflow, W.sub(0))), 1)
return [inflow, noslip, DirichletBC(W.sub(0), 0, 4)]

return bcs
Expand Down

0 comments on commit 80da600

Please sign in to comment.