|
| 1 | +""" |
| 2 | +The basic example is taken from "Langtangen, Hans Petter, and Anders Logg. Solving PDEs in Python: The FEniCS |
| 3 | +Tutorial I. Springer International Publishing, 2016." |
| 4 | +
|
| 5 | +The example code has been extended with preCICE API calls and mixed boundary conditions to allow for a Dirichlet-Neumann |
| 6 | +coupling of two separate heat equations. It also has been adapted to be compatible with FEniCSx. |
| 7 | +
|
| 8 | +The original source code can be found on https://jsdokken.com/dolfinx-tutorial/chapter2/heat_code.html. |
| 9 | +
|
| 10 | +Heat equation with Dirichlet conditions. (Dirichlet problem) |
| 11 | + u'= Laplace(u) + f in the unit square [0,1] x [0,1] |
| 12 | + u = u_C on the coupling boundary at x = 1 |
| 13 | + u = u_D on the remaining boundary |
| 14 | + u = u_0 at t = 0 |
| 15 | + u = 1 + x^2 + alpha*y^2 + \beta*t |
| 16 | + f = beta - 2 - 2*alpha |
| 17 | +
|
| 18 | +Heat equation with mixed boundary conditions. (Neumann problem) |
| 19 | + u'= Laplace(u) + f in the shifted unit square [1,2] x [0,1] |
| 20 | + du/dn = f_N on the coupling boundary at x = 1 |
| 21 | + u = u_D on the remaining boundary |
| 22 | + u = u_0 at t = 0 |
| 23 | + u = 1 + x^2 + alpha*y^2 + \beta*t |
| 24 | + f = beta - 2 - 2*alpha |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import print_function, division |
| 28 | +from mpi4py import MPI |
| 29 | +from dolfinx.fem import Function, FunctionSpace, Expression, Constant, dirichletbc, locate_dofs_geometrical |
| 30 | +from dolfinx.fem.petsc import LinearProblem |
| 31 | +from dolfinx.io import XDMFFile |
| 32 | +from ufl import TrialFunction, TestFunction, dx, ds, dot, grad, inner, lhs, rhs, FiniteElement, VectorElement |
| 33 | +from fenicsxprecice import Adapter |
| 34 | +from errorcomputation import compute_errors |
| 35 | +from my_enums import ProblemType, DomainPart |
| 36 | +import argparse |
| 37 | +import numpy as np |
| 38 | +from problem_setup import get_geometry |
| 39 | + |
| 40 | +def determine_gradient(V_g, u): |
| 41 | + """ |
| 42 | + compute flux following http://hplgit.github.io/INF5620/doc/pub/fenics_tutorial1.1/tu2.html#tut-poisson-gradu |
| 43 | + :param V_g: Vector function space |
| 44 | + :param u: solution where gradient is to be determined |
| 45 | + """ |
| 46 | + |
| 47 | + w = TrialFunction(V_g) |
| 48 | + v = TestFunction(V_g) |
| 49 | + |
| 50 | + a = inner(w, v) * dx |
| 51 | + L = inner(grad(u), v) * dx |
| 52 | + problem = LinearProblem(a, L) |
| 53 | + return problem.solve() |
| 54 | + |
| 55 | +parser = argparse.ArgumentParser(description="Solving heat equation for simple or complex interface case") |
| 56 | +command_group = parser.add_mutually_exclusive_group(required=True) |
| 57 | +command_group.add_argument("-d", "--dirichlet", help="create a dirichlet problem", dest="dirichlet", |
| 58 | + action="store_true") |
| 59 | +command_group.add_argument("-n", "--neumann", help="create a neumann problem", dest="neumann", action="store_true") |
| 60 | +parser.add_argument("-e", "--error-tol", help="set error tolerance", type=float, default=10**-6,) |
| 61 | + |
| 62 | +args = parser.parse_args() |
| 63 | + |
| 64 | +fenics_dt = .1 # time step size |
| 65 | +# Error is bounded by coupling accuracy. In theory we would obtain the analytical solution. |
| 66 | +error_tol = args.error_tol |
| 67 | + |
| 68 | +alpha = 3 # parameter alpha |
| 69 | +beta = 1.3 # parameter beta |
| 70 | + |
| 71 | +if args.dirichlet and not args.neumann: |
| 72 | + problem = ProblemType.DIRICHLET |
| 73 | + domain_part = DomainPart.LEFT |
| 74 | +elif args.neumann and not args.dirichlet: |
| 75 | + problem = ProblemType.NEUMANN |
| 76 | + domain_part = DomainPart.RIGHT |
| 77 | + |
| 78 | +mesh, coupling_boundary, remaining_boundary = get_geometry(MPI.COMM_WORLD, domain_part) |
| 79 | + |
| 80 | +# Define function space using mesh |
| 81 | +scalar_element = FiniteElement("P", mesh.ufl_cell(), 2) |
| 82 | +vector_element = VectorElement("P", mesh.ufl_cell(), 1) |
| 83 | +V = FunctionSpace(mesh, scalar_element) |
| 84 | +V_g = FunctionSpace(mesh, vector_element) |
| 85 | +W = V_g.sub(0).collapse()[0] |
| 86 | + |
| 87 | +# Define boundary conditions |
| 88 | + |
| 89 | + |
| 90 | +class Expression_u_D: |
| 91 | + def __init__(self): |
| 92 | + self.t = 0.0 |
| 93 | + |
| 94 | + def eval(self, x): |
| 95 | + return np.full(x.shape[1], 1 + x[0] * x[0] + alpha * x[1] * x[1] + beta * self.t) |
| 96 | + |
| 97 | + |
| 98 | +u_D = Expression_u_D() |
| 99 | +u_D_function = Function(V) |
| 100 | +u_D_function.interpolate(u_D.eval) |
| 101 | + |
| 102 | +if problem is ProblemType.DIRICHLET: |
| 103 | + # Define flux in x direction |
| 104 | + class Expression_f_N: |
| 105 | + def __init__(self): |
| 106 | + pass |
| 107 | + |
| 108 | + def eval(self, x): |
| 109 | + return np.full(x.shape[1], 2 * x[0]) |
| 110 | + |
| 111 | + f_N = Expression_f_N() |
| 112 | + f_N_function = Function(V) |
| 113 | + f_N_function.interpolate(f_N.eval) |
| 114 | + |
| 115 | +# Define initial value |
| 116 | +u_n = Function(V, name="Temperature") |
| 117 | +u_n.interpolate(u_D.eval) |
| 118 | + |
| 119 | +precice, precice_dt, initial_data = None, 0.0, None |
| 120 | + |
| 121 | +# Initialize the adapter according to the specific participant |
| 122 | +if problem is ProblemType.DIRICHLET: |
| 123 | + precice = Adapter(MPI.COMM_WORLD, adapter_config_filename="precice-adapter-config-D.json") |
| 124 | + precice_dt = precice.initialize(coupling_boundary, read_function_space=V, write_object=f_N_function) |
| 125 | +elif problem is ProblemType.NEUMANN: |
| 126 | + precice = Adapter(MPI.COMM_WORLD, adapter_config_filename="precice-adapter-config-N.json") |
| 127 | + precice_dt = precice.initialize(coupling_boundary, read_function_space=W, write_object=u_D_function) |
| 128 | + |
| 129 | +dt = Constant(mesh, 0.0) |
| 130 | +dt.value = np.min([fenics_dt, precice_dt]) |
| 131 | + |
| 132 | +# Define variational problem |
| 133 | +u = TrialFunction(V) |
| 134 | +v = TestFunction(V) |
| 135 | + |
| 136 | + |
| 137 | +class Expression_f: |
| 138 | + def __init__(self): |
| 139 | + self.t = 0.0 |
| 140 | + |
| 141 | + def eval(self, x): |
| 142 | + return np.full(x.shape[1], beta - 2 - 2 * alpha) |
| 143 | + |
| 144 | + |
| 145 | +f = Expression_f() |
| 146 | +f_function = Function(V) |
| 147 | +F = u * v / dt * dx + dot(grad(u), grad(v)) * dx - (u_n / dt + f_function) * v * dx |
| 148 | + |
| 149 | +dofs_remaining = locate_dofs_geometrical(V, remaining_boundary) |
| 150 | +bcs = [dirichletbc(u_D_function, dofs_remaining)] |
| 151 | + |
| 152 | +coupling_expression = precice.create_coupling_expression() |
| 153 | +read_data = precice.read_data() |
| 154 | +precice.update_coupling_expression(coupling_expression, read_data) |
| 155 | +if problem is ProblemType.DIRICHLET: |
| 156 | + # modify Dirichlet boundary condition on coupling interface |
| 157 | + dofs_coupling = locate_dofs_geometrical(V, coupling_boundary) |
| 158 | + bcs.append(dirichletbc(coupling_expression, dofs_coupling)) |
| 159 | +if problem is ProblemType.NEUMANN: |
| 160 | + # modify Neumann boundary condition on coupling interface, modify weak |
| 161 | + # form correspondingly |
| 162 | + F += v * coupling_expression * ds |
| 163 | + |
| 164 | +a, L = lhs(F), rhs(F) |
| 165 | + |
| 166 | +# Time-stepping |
| 167 | +u_np1 = Function(V, name="Temperature") |
| 168 | +t = 0 |
| 169 | + |
| 170 | +# reference solution at t=0 |
| 171 | +u_ref = Function(V, name="reference") |
| 172 | +u_ref.interpolate(u_D_function) |
| 173 | + |
| 174 | +# Generating output files |
| 175 | +temperature_out = XDMFFile(MPI.COMM_WORLD, f"./output/{precice.get_participant_name()}.xdmf", "w") |
| 176 | +temperature_out.write_mesh(mesh) |
| 177 | +ref_out = XDMFFile(MPI.COMM_WORLD, f"./output/ref{precice.get_participant_name()}.xdmf", "w") |
| 178 | +ref_out.write_mesh(mesh) |
| 179 | + |
| 180 | +# output solution at t=0, n=0 |
| 181 | +n = 0 |
| 182 | + |
| 183 | +temperature_out.write_function(u_n, t) |
| 184 | +ref_out.write_function(u_ref, t) |
| 185 | + |
| 186 | +u_D.t = t + dt.value |
| 187 | +u_D_function.interpolate(u_D.eval) |
| 188 | +f.t = t + dt.value |
| 189 | +f_function.interpolate(f.eval) |
| 190 | + |
| 191 | +if problem is ProblemType.DIRICHLET: |
| 192 | + flux = Function(V_g, name="Heat-Flux") |
| 193 | + |
| 194 | +while precice.is_coupling_ongoing(): |
| 195 | + |
| 196 | + # write checkpoint |
| 197 | + if precice.is_action_required(precice.action_write_iteration_checkpoint()): |
| 198 | + precice.store_checkpoint(u_n, t, n) |
| 199 | + |
| 200 | + read_data = precice.read_data() |
| 201 | + |
| 202 | + # Update the coupling expression with the new read data |
| 203 | + precice.update_coupling_expression(coupling_expression, read_data) |
| 204 | + |
| 205 | + dt.value = np.min([fenics_dt, precice_dt]) |
| 206 | + |
| 207 | + linear_problem = LinearProblem(a, L, bcs=bcs) |
| 208 | + u_np1 = linear_problem.solve() |
| 209 | + |
| 210 | + # Write data to preCICE according to which problem is being solved |
| 211 | + if problem is ProblemType.DIRICHLET: |
| 212 | + # Dirichlet problem reads temperature and writes flux on boundary to Neumann problem |
| 213 | + flux = determine_gradient(V_g, u_np1) |
| 214 | + flux_x = Function(W) |
| 215 | + flux_x.interpolate(flux.sub(0)) |
| 216 | + precice.write_data(flux_x) |
| 217 | + elif problem is ProblemType.NEUMANN: |
| 218 | + # Neumann problem reads flux and writes temperature on boundary to Dirichlet problem |
| 219 | + precice.write_data(u_np1) |
| 220 | + |
| 221 | + precice_dt = precice.advance(dt.value) |
| 222 | + |
| 223 | + # roll back to checkpoint |
| 224 | + if precice.is_action_required(precice.action_read_iteration_checkpoint()): |
| 225 | + u_cp, t_cp, n_cp = precice.retrieve_checkpoint() |
| 226 | + u_n.interpolate(u_cp) |
| 227 | + t = t_cp |
| 228 | + n = n_cp |
| 229 | + else: # update solution |
| 230 | + u_n.interpolate(u_np1) |
| 231 | + t += dt.value |
| 232 | + n += 1 |
| 233 | + |
| 234 | + if precice.is_time_window_complete(): |
| 235 | + u_ref.interpolate(u_D_function) |
| 236 | + error = compute_errors(u_n, u_ref, total_error_tol=error_tol) |
| 237 | + print('n = %d, t = %.2f: L2 error on domain = %.3g' % (n, t, error)) |
| 238 | + print('output u^%d and u_ref^%d' % (n, n)) |
| 239 | + |
| 240 | + temperature_out.write_function(u_n, t) |
| 241 | + ref_out.write_function(u_ref, t) |
| 242 | + |
| 243 | + |
| 244 | + # Update Dirichlet BC |
| 245 | + u_D.t = t + dt.value |
| 246 | + u_D_function.interpolate(u_D.eval) |
| 247 | + f.t = t + dt.value |
| 248 | + f_function.interpolate(f.eval) |
| 249 | + |
| 250 | + |
| 251 | +# Hold plot |
| 252 | +precice.finalize() |
0 commit comments