Tue, 06 Oct 2026 00:47:42 -0500
README updates
| 12 | 1 | import numpy as np |
| 2 | from dolfinx import fem, mesh | |
| 3 | from mpi4py import MPI | |
| 4 | ||
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | from dolfinx_access import eval_Function_f64, min_Function_f64_p2 |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
8 | # A simple test that the finite-dimensional subproblem is solved correctly |
| 12 | 9 | def test_minimisation(domain_dims=[[-5, 5], [-5, 5]], degree=2, nx=256, ny=256, n=512): |
| 10 | domain = mesh.create_rectangle( | |
| 11 | MPI.COMM_SELF, | |
| 12 | [ | |
| 13 | np.array([domain_dims[0][0], domain_dims[1][0]]), | |
| 14 | np.array([domain_dims[0][1], domain_dims[1][1]]), | |
| 15 | ], | |
| 16 | [nx, ny], | |
| 17 | mesh.CellType.triangle, | |
| 18 | ) | |
| 19 | V = fem.functionspace(domain, ("Lagrange", degree)) | |
| 20 | test = fem.Function(V) | |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
21 | |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | # Beale function |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | def beale(x): |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | return ( |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
25 | (1.5 - x[0] + x[0] * x[1]) ** 2 |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | + (2.25 - x[0] + x[0] * (x[1] ** 2)) ** 2 |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | + (2.625 - x[0] + x[0] * (x[1] ** 3)) ** 2 |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
28 | ) |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
29 | |
| 12 | 30 | def quadratic(x): |
|
1
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
31 | return x[0] ** 2 - 5 * x[1] ** 2 + x[0] * x[1] - x[0] + x[1] |
|
a4137aedcb3a
Initial working version for known convectivity and diffusivity
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
32 | |
| 12 | 33 | def sphere(x): |
| 34 | return x[0] ** 2 + x[1] ** 2 | |
| 35 | ||
| 36 | def booth(x): | |
| 37 | return (x[0] + 2 * x[1] - 7) ** 2 + (2 * x[0] + x[1] - 5) ** 2 | |
| 38 | ||
| 39 | def himmelblau(x): | |
| 40 | return (x[0] ** 2 + x[1] - 11) ** 2 + (x[0] + x[1] ** 2 - 7) ** 2 | |
| 41 | ||
| 42 | for testf, name in [ | |
| 43 | (beale, "Beale"), | |
| 44 | (quadratic, "Quadratic"), | |
| 45 | (sphere, "Sphere"), | |
| 46 | (booth, "Booth"), | |
| 47 | (himmelblau, "Himmelblau"), | |
| 48 | ]: | |
| 49 | test.interpolate(testf) | |
| 50 | m = min_Function_f64_p2(test._cpp_object) | |
| 51 | print( | |
| 52 | name, | |
| 53 | "minimizer:", | |
| 54 | m.v, | |
| 55 | "(verification ", | |
| 56 | eval_Function_f64(test._cpp_object, [m.x[0], m.x[1], 0]), | |
| 57 | " original ", | |
| 58 | testf(m.x), | |
| 59 | ") @ ", | |
| 60 | m.x, | |
| 61 | ) | |
| 62 | ||
| 63 | dist = 0.0 | |
| 64 | m = float("inf") | |
| 65 | m0 = float("inf") | |
| 66 | mp = None | |
| 67 | for x in np.linspace(domain_dims[0][0], domain_dims[0][1], n): | |
| 68 | for y in np.linspace(domain_dims[1][0], domain_dims[1][1], n): | |
| 69 | p = [x, y, 0.0] | |
| 70 | v = eval_Function_f64(test._cpp_object, p) | |
| 71 | v0 = testf(p) | |
| 72 | if v0 < m0: | |
| 73 | m = v | |
| 74 | m0 = v0 | |
| 75 | mp = p | |
| 76 | d = np.linalg.norm(v - v0) | |
| 77 | dist = max(d, dist) | |
| 78 | print(name, "evaluation max discrepancy:", dist) | |
| 79 | print( | |
| 80 | name, | |
| 81 | "rough scan minimiser ", | |
| 82 | m, | |
| 83 | "(model", | |
| 84 | m0, | |
| 85 | ") @ [", | |
| 86 | float(mp[0]), | |
| 87 | ", ", | |
| 88 | float(mp[1]), | |
| 89 | "]", | |
| 90 | ) | |
| 91 | print() | |
| 92 | ||
| 93 | ||
| 94 | test_minimisation() | |
| 95 | exit(0) |