Skip to content

Commit b0ca7f6

Browse files
authored
Merge pull request #17 from milzj/milzj-patch-1
2 parents ad072d3 + 85d0edb commit b0ca7f6

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

tests/algorithms/test_nonconstant_bounds.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def solve_problem(n, n_ref, u_init=None, maxiter=1000, gtol=1e-15, ftol=-np.inf
3030

3131
# x0 should be a grid point
3232

33-
beta = 0.0
33+
beta = 0.001
3434
lb = Expression("-1", degree = 0)
3535
ub = Expression("1+0.1*sin(2*pi*x[0])", degree = 0)
3636

@@ -45,7 +45,12 @@ def solve_problem(n, n_ref, u_init=None, maxiter=1000, gtol=1e-15, ftol=-np.inf
4545

4646
if u_init != None:
4747
u = project(u_init, U)
48-
48+
u_vec = u.vector()[:]
49+
lb_vec = project(lb, U).vector()[:]
50+
ub_vec = project(ub, U).vector()[:]
51+
u_vec = np.clip(u_vec, lb_vec, ub_vec)
52+
u.vector()[:] = u_vec
53+
4954
V = FunctionSpace(mesh, "CG", 1)
5055
y = Function(V)
5156
w = TrialFunction(V)
@@ -57,7 +62,7 @@ def solve_problem(n, n_ref, u_init=None, maxiter=1000, gtol=1e-15, ftol=-np.inf
5762
L = u*v*dx
5863
A, b = assemble_system(a, L, bc)
5964

60-
solver = LUSolver(A, "petsc")
65+
solver = KrylovSolver(A, "cg")
6166
solver.solve(y.vector(), b)
6267

6368
J = assemble(0.5*inner(y-yd,y-yd)*dx)
@@ -84,7 +89,7 @@ def solve_problem(n, n_ref, u_init=None, maxiter=1000, gtol=1e-15, ftol=-np.inf
8489
def test_convergence_rate():
8590
"""Code verification for a one-dimensional boundary value problem.
8691
87-
abs(dual_gap(u_h)) should converge with rate h^2
92+
dual_gap(u_h) should converge with rate h^2
8893
"""
8994

9095
n_ref = 2**16
@@ -112,13 +117,14 @@ def test_convergence_rate():
112117

113118
ndrop = 0
114119
x_vec = ns
115-
y_vec = np.abs(dual_gaps)
120+
y_vec = dual_gaps
116121
X = np.ones((len(x_vec[ndrop::]), 2)); X[:, 1] = np.log(x_vec[ndrop::]) # design matrix
117122
x, residudals, rank, s = np.linalg.lstsq(X, np.log(y_vec[ndrop::]), rcond=None)
118123

119124
rate = x[1]
120125
constant = np.exp(x[0])
121-
126+
assert np.isclose(-rate, 1.0, rtol=0.0, atol=0.1)
127+
122128
fig, ax = plt.subplots()
123129
ax.plot([n for n in ns], dual_gaps)
124130

@@ -132,7 +138,7 @@ def test_convergence_rate():
132138
fig.savefig("convergence_rates_bilinear.png")
133139

134140

135-
assert np.isclose(np.median(rates), 2.0, rtol=0.0, atol=0.1)
141+
assert np.isclose(np.median(rates), 1.0, rtol=0.0, atol=0.1)
136142

137143
if __name__ == "__main__":
138144

0 commit comments

Comments
 (0)