Created
December 6, 2022 03:23
-
-
Save angus-g/8d94eea2260cbab50af503e9855d4367 to your computer and use it in GitHub Desktop.
Demonstration of increasing KSP objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from firedrake import * | |
from firedrake_adjoint import * | |
from firedrake.petsc import PETSc | |
from pyadjoint.tape import pause_annotation | |
mesh = UnitSquareMesh(10, 10) | |
x, y = SpatialCoordinate(mesh) | |
Q = FunctionSpace(mesh, "CG", 2) | |
Y = TestFunction(Q) | |
with stop_annotating(): | |
final_state = Function(Q, name="final_state") | |
final_state.interpolate(0.1 * exp(-0.5*(y-1.5)**2/0.1**2)) | |
T_ic = Function(Q, name="T_IC") | |
T_ic.assign(0) | |
Told = Function(Q, name="OldTemperature") | |
Tnew = Function(Q, name="NewTemperature") | |
Ttheta = 0.5 * Tnew + 0.5 * Told | |
F_energy = ( | |
Y * (Tnew - Told) / Constant(1.0e-6) * dx | |
+ dot(grad(Y), Constant(1.0) * grad(Ttheta)) * dx | |
) | |
energy_problem = NonlinearVariationalProblem(F_energy, Tnew) | |
energy_solver = NonlinearVariationalSolver(energy_problem) | |
control = Control(T_ic) | |
Told.assign(T_ic) | |
Tnew.assign(Told) | |
for timestep in range(20): | |
energy_solver.solve() | |
Told.assign(Tnew) | |
objective = assemble(0.5*(Tnew - final_state)**2 * dx) | |
pause_annotation() | |
reduced_functional = ReducedFunctional(objective, control) | |
h = Function(Q) | |
from numpy.random import rand | |
for i in range(10): | |
h.vector()[:] = rand(h.vector().local_size())*1e-1 | |
reduced_functional(T_ic._ad_add(h)) | |
reduced_functional.derivative() | |
PETSc.garbage_view(comm=mesh.comm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment