experiments/tests/test_minimisation.py

changeset 1
a4137aedcb3a
equal deleted inserted replaced
0:7ec1cfe19a24 1:a4137aedcb3a
1 from dolfinx_access import eval_Function_f64, min_Function_f64_p2
2
3
4 # A simple test that the finite-dimensional subproblem is solved correctly
5 def test_minimisation(pde):
6 test = fem.Function(pde.V)
7
8 # Beale function
9 def beale(x):
10 return (
11 (1.5 - x[0] + x[0] * x[1]) ** 2
12 + (2.25 - x[0] + x[0] * (x[1] ** 2)) ** 2
13 + (2.625 - x[0] + x[0] * (x[1] ** 3)) ** 2
14 )
15
16 test.interpolate(beale)
17 m = min_Function_f64_p2(test._cpp_object)
18 print("Beale minimizer:", m.x, m.v)
19
20 def quadratic_testf(x):
21 return x[0] ** 2 - 5 * x[1] ** 2 + x[0] * x[1] - x[0] + x[1]
22
23 testf = quadratic_testf
24 test.interpolate(testf)
25 n = 256
26 dist = 0.0
27 for x in np.linspace(pde.domain_size[0], pde.domain_size[1], n):
28 for y in np.linspace(pde.domain_size[2], pde.domain_size[3], n):
29 p = [x, y, 0.0]
30 v = eval_Function_f64(test._cpp_object, p)
31 v0 = testf(p)
32 d = np.linalg.norm(v - v0)
33 dist = max(d, dist)
34 print("Quadratic function evaluation max discrepancy:", dist)

mercurial