def test_jax_to_fenics_function(test_input, expected_expr): mesh = fenics.UnitIntervalMesh(10) V = fenics.FunctionSpace(mesh, "DG", 0) template = fenics.Function(V) fenics_test_input = numpy_to_fenics(test_input, template) expected = fenics.interpolate(fenics.Expression(expected_expr, degree=1), V) assert numpy.allclose( fenics_test_input.vector().get_local(), expected.vector().get_local() )
def test_numpy_to_fenics_function(): test_input = numpy.linspace(0.05, 0.95, num=10) mesh = fenics.UnitIntervalMesh(10) V = fenics.FunctionSpace(mesh, "DG", 0) template = fenics.Function(V) fenics_test_input = numpy_to_fenics(test_input, template) expected = fenics.interpolate(fenics.Expression("x[0]", degree=1), V) assert numpy.allclose( fenics_test_input.vector().get_local(), expected.vector().get_local() )
constraints = [{ "type": "ineq", "fun": volume_inequality_fun, "jac": lambda x: jax.grad(volume_inequality_fun)(x), }] x0 = np.ones(C.dim()) * max_volume / (L * h) res = minimize_ipopt( min_f, x0, jac=True, bounds=((0.0, 1.0), ) * C.dim(), constraints=constraints, options={ "print_level": 5, "max_iter": 100 }, ) rho_opt_final = numpy_to_fenics(res.x, fa.Function(C)) c = fn.plot(rho_opt_final) plt.colorbar(c) plt.show() # Save optimal solution for visualizing with ParaView # with XDMFFile("1_dist_load/control_solution_1.xdmf") as f: # f.write(rho_opt)
def test_jax_to_fenics_constant(test_input, expected): fenics_test_input = numpy_to_fenics(test_input, fenics.Constant(0.0)) assert numpy.allclose(fenics_test_input.values(), expected.values())
x0, method="L-BFGS-B", jac=True, tol=1e-9, bounds=((0, 0.8),) * W.dim(), options={"gtol": 1e-10, "ftol": 0, "disp": True, "maxiter": 50}, ) # Define the expressions of the analytical solution alpha = 1e-6 x = ufl.SpatialCoordinate(mesh) w = ufl.sin(ufl.pi * x[0]) * ufl.sin(ufl.pi * x[1]) f_analytic = 1 / (1 + alpha * 4 * ufl.pi ** 4) * w u_analytic = 1 / (2 * ufl.pi ** 2) * f_analytic f_opt = numpy_to_fenics(res.x, fn.Function(W)) u = fn.Function(V) v = fn.TestFunction(V) F = (ufl.inner(ufl.grad(u), ufl.grad(v)) - f_opt * v) * ufl.dx bc = fn.DirichletBC(V, 0.0, "on_boundary") fn.solve(F == 0, u, bc) print(f"norm of f_opt is {fn.norm(f_opt)}") # interpolatation of UFL forms does not work in FEniCS, hence projection CG3 = fn.FunctionSpace(mesh, "CG", 3) control_error = fn.errornorm(fn.project(f_analytic, CG3), f_opt) state_error = fn.errornorm(fn.project(u_analytic, CG3), u) print("h(min): %e." % mesh.hmin()) print("Error in state: %e." % state_error) print("Error in control: %e." % control_error)
hessp=SR1(), tol=1e-7, constraints=volume_constraint, bounds=((0, 1.0),) * A.dim(), options={"verbose": 3, "gtol": 1e-7, "maxiter": 20}, ) q.assign(0.1) res = minimize( min_f, res.x, method="trust-constr", jac=True, hessp=SR1(), tol=1e-7, constraints=volume_constraint, bounds=((0, 1.0),) * A.dim(), options={"verbose": 3, "gtol": 1e-7, "maxiter": 100}, ) rho_opt_final = numpy_to_fenics(res.x, fenics.Function(A)) c = fenics.plot(rho_opt_final) plt.colorbar(c) plt.show() rho_opt_file = fenics.XDMFFile( fenics.MPI.comm_world, "output/control_solution_final.xdmf" ) rho_opt_file.write(rho_opt_final)