Skip to content

Commit

Permalink
Merge pull request #381 from thetisproject/debug-test_anisotropic
Browse files Browse the repository at this point in the history
Re-enable turbines in `test_anisotropic`
  • Loading branch information
jwallwork23 authored Jan 7, 2025
2 parents 39880f0 + 50467b7 commit 3515ec4
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions test/swe2d/test_anisotropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,7 @@ def run(solve_adjoint=False, mesh=None, **model_options):
options.use_grad_depth_viscosity_term = False
options.no_exports = True
options.update(model_options)
solver_obj.create_equations()

# recover Hessian
if not solve_adjoint:
if 'hessian_2d' in field_metadata:
field_metadata.pop('hessian_2d')
P1t_2d = get_functionspace(mesh2d, 'CG', 1, tensor=True)
hessian_2d = Function(P1t_2d)
u_2d = solver_obj.fields.uv_2d[0]
hessian_calculator = thetis.diagnostics.HessianRecoverer2D(u_2d, hessian_2d)
solver_obj.add_new_field(hessian_2d,
'hessian_2d',
'Hessian of x-velocity',
'Hessian2d',
preproc_func=hessian_calculator)
solver_obj.create_function_spaces()

# apply boundary conditions
solver_obj.bnd_functions['shallow_water'] = {
Expand Down Expand Up @@ -135,12 +121,36 @@ def bump(mesh, locs, scale=1.0):
farm_options.turbine_options.diameter = D
C_T = thrust_coefficient * correction
farm_options.turbine_options.thrust_coefficient = C_T
solver_obj.options.tidal_turbine_farms['everywhere'] = farm_options
solver_obj.options.tidal_turbine_farms['everywhere'] = [farm_options]
solver_obj.create_equations()

# apply initial guess of inflow velocity, solve and return number of nonlinear solver iterations
# recover Hessian
if not solve_adjoint:
if 'hessian_2d' in field_metadata:
field_metadata.pop('hessian_2d')
P1t_2d = get_functionspace(mesh2d, 'CG', 1, tensor=True)
hessian_2d = Function(P1t_2d)
u_2d = solver_obj.fields.uv_2d[0]
hessian_calculator = thetis.diagnostics.HessianRecoverer2D(u_2d, hessian_2d)
solver_obj.add_new_field(hessian_2d,
'hessian_2d',
'Hessian of x-velocity',
'Hessian2d',
preproc_func=hessian_calculator)

# Apply initial guess of inflow velocity and solve
solver_obj.assign_initial_conditions(uv=inflow_velocity)
solver_obj.iterate()

if not solve_adjoint:
# Check that turbines have been picked up
assert not np.allclose(solver_obj.fields.uv_2d.dat.data[0], 0.5, atol=1e-3)

# Check the turbine density has been set up correctly
total_turbine_density = assemble(solver_obj.tidal_farms[0].turbine_density * dx)
assert np.isclose(total_turbine_density, 2, atol=0.01), f"Expected 2, but got {total_turbine_density}"

# Return number of nonlinear solver iterations
return solver_obj.timestepper.solver.snes.getIterationNumber()

# quantity of interest: power output
Expand Down

0 comments on commit 3515ec4

Please sign in to comment.