Skip to content

Commit 1186966

Browse files
authored
Update to current firedrake (#20)
Make firedrake-ts usable with the current firedrake * make _TSContext as a subclass of _SNESContext to reduce some code duplications * deal with the new notion of cofunction in UFL/firedrake * modify some examples to run * bump firedrake-ts to version 0.2
1 parent 9c0cad5 commit 1186966

File tree

7 files changed

+156
-362
lines changed

7 files changed

+156
-362
lines changed

examples/adjoint.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ def ts_monitor(ts, steps, time, X):
6363
with dJdu.dat.vec as vec:
6464
dJdu_vec = vec
6565

66-
print(f"Norm of dJdu before the adjoint solve: {fd.norm(dJdu)}")
66+
fdJdu=dJdu.riesz_representation()
67+
68+
print(f"Norm of dJdu before the adjoint solve: {dJdu_vec.norm()=} {fd.norm(fdJdu)=}")
6769

6870
# setCostGradients accepts two PETSc Vecs
6971
# J is the objective function
@@ -75,7 +77,9 @@ def ts_monitor(ts, steps, time, X):
7577

7678
solver.adjoint_solve()
7779

78-
print(f"Norm of dJdu after the adjoint solve: {fd.norm(dJdu)}")
80+
fdJdu=dJdu.riesz_representation()
81+
print(f"Norm of dJdu after the adjoint solve: {dJdu_vec.norm()=} {fd.norm(fdJdu)=}")
82+
7983

8084
adj_out = fd.File("result/adj.pvd")
81-
adj_out.write(dJdu, time=0)
85+
adj_out.write(fdJdu, time=0)

examples/cahn-hilliard.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import firedrake_ts
77
from firedrake import *
8-
from pyop2 import op2
8+
import numpy as np
99

1010
# Model parameters
1111
lmbda = 1.0e-02 # surface parameter
@@ -26,18 +26,6 @@
2626
c, mu = split(u)
2727
c_t, mu_t = split(u_t)
2828

29-
# Create intial conditions and interpolate
30-
init_code = "A[0] = 0.63 + 0.02*(0.5 - (double)random()/RAND_MAX);"
31-
user_code = """int __rank;
32-
MPI_Comm_rank(MPI_COMM_WORLD, &__rank);
33-
srandom(2 + __rank);"""
34-
par_loop(
35-
init_code,
36-
direct,
37-
{"A": (u[0], WRITE)},
38-
kernel_kwargs={"headers": ["#include <stdlib.h>"],
39-
"user_code": user_code}
40-
)
4129

4230
# Compute the chemical potential df/dc
4331
c = variable(c)
@@ -49,7 +37,13 @@
4937
F1 = mu * v * dx - dfdc * v * dx - lmbda * dot(grad(c), grad(v)) * dx
5038
F = F0 + F1
5139

52-
pc = "fieldsplit"
40+
rng = np.random.default_rng(11)
41+
c , mu = u.subfunctions
42+
with c.dat.vec as v:
43+
v[:]=0.63 + 0.2*(0.5-rng.random(v.size))
44+
45+
46+
pc = "lu" #"fieldsplit"
5347
ksp = "lgmres"
5448
inner_ksp = "preonly"
5549
maxit = 1
@@ -77,9 +71,9 @@
7771

7872
params["snes_monitor"] = None
7973
params["ts_monitor"] = None
80-
params["ts_view"] = None
74+
#params["ts_view"] = None
8175

82-
problem = firedrake_ts.DAEProblem(F, u, u_t, (0.0, 50 * dt))
76+
problem = firedrake_ts.DAEProblem(F, u, u_t, (0.0, 2 * dt))
8377
solver = firedrake_ts.DAESolver(problem, solver_parameters=params)
8478

8579
if pc in ["fieldsplit", "ilu"]:

examples/heat-explicit.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
from firedrake import *
22
import firedrake_ts
3+
from firedrake.__future__ import interpolate
34

4-
mesh = UnitIntervalMesh(11)
5+
mesh = UnitIntervalMesh(10)
56
V = FunctionSpace(mesh, "P", 1)
67

78
u = Function(V)
89
u_t = Function(V)
910
v = TestFunction(V)
1011

11-
# F(, u, t) = G(u, t)
12+
# F(u_t, u, t) = G(u, t)
1213
F = inner(u_t, v) * dx
1314
G = -(inner(grad(u), grad(v)) * dx - 1.0 * v * dx)
1415

15-
bc = DirichletBC(V, 0.0, "on_boundary")
16+
bc1 = DirichletBC(V, 1.0, 1)
17+
bc2 = DirichletBC(V, 0.0, 2)
18+
bcs=[bc1, bc2]
1619

1720
x = SpatialCoordinate(mesh)
18-
bump = conditional(lt(abs(x[0] - 0.5), 0.1), 1.0, 0.0)
19-
u.interpolate(bump)
21+
bump = conditional(lt(x[0], 0.5), 1.0, 0.0)
22+
assemble(interpolate(bump, u), tensor=u)
23+
print(f'{u.dat.data}=')
2024

21-
problem = firedrake_ts.DAEProblem(F, u, u_t, (0.0, 1.0), bcs=bc, G=G)
22-
solver = firedrake_ts.DAESolver(problem)
25+
26+
def monitor(ts, step, t, x):
27+
print(f'{solver.ts.getTime()=}')
28+
print(f'{u.dat.data}=')
29+
30+
problem = firedrake_ts.DAEProblem(F, u, u_t, (0.0, 0.4), bcs=bcs, G=G)
31+
32+
solver = firedrake_ts.DAESolver(problem, options_prefix='', monitor_callback=monitor)
2333

2434
solver.solve()
2535

26-
print(solver.ts.getTime())
27-
# solver.ts.view()
28-
print(u.dat.data)

examples/heat.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from firedrake import *
22
import firedrake_ts
3+
from firedrake.__future__ import interpolate
34

45
mesh = UnitIntervalMesh(10)
56
V = FunctionSpace(mesh, "P", 1)
@@ -9,16 +10,37 @@
910
v = TestFunction(V)
1011
F = inner(u_t, v) * dx + inner(grad(u), grad(v)) * dx - 1.0 * v * dx
1112

12-
bc = DirichletBC(V, 0.0, "on_boundary")
13+
bc_val=Constant(0.0)
14+
bc = DirichletBC(V, bc_val, "on_boundary")
1315

1416
x = SpatialCoordinate(mesh)
15-
# gaussian = exp(-30*(x[0]-0.5)**2)
16-
bump = conditional(lt(abs(x[0] - 0.5), 0.1), 1.0, 0.0)
17-
u.interpolate(bump)
17+
bump = conditional(lt(x[0], 0.5), 1.0, 0.0)
18+
assemble(interpolate(bump, u), tensor=u)
19+
print(f'{u.dat.data}=')
1820

19-
problem = firedrake_ts.DAEProblem(F, u, u_t, (0.0, 1.0), bcs=bc)
20-
solver = firedrake_ts.DAESolver(problem)
2121

22+
p0=NonlinearVariationalProblem(F, u, bcs=bc)
23+
s0=NonlinearVariationalSolver(p0)
24+
s0.solve()
25+
print(f'{u.dat.data}=')
26+
27+
bc_val.assign(1.0)
28+
s0.solve()
29+
print(f'{u.dat.data}=')
30+
31+
32+
def monitor(ts, step, t, x):
33+
print(f'{solver.ts.getTime()=} {problem.time=} {bc_val=}')
34+
print(f'{u.dat.data}=')
35+
36+
problem = firedrake_ts.DAEProblem(F, u, u_t, (0.0, 1.0), bcs=bc, time=bc_val)
37+
solver = firedrake_ts.DAESolver(problem, options_prefix='', monitor_callback=monitor)
38+
39+
bc_val.assign(2.0)
40+
solver.solve()
41+
print(id(bc_val), id(problem.time))
42+
43+
bc_val.assign(4.0)
2244
solver.solve()
2345

2446
print(solver.ts.getTime())

0 commit comments

Comments
 (0)