|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +import nutils, treelog |
| 4 | +import numpy as np |
| 5 | +import precice |
| 6 | + |
| 7 | + |
| 8 | +def main(side='Dirichlet'): |
| 9 | + |
| 10 | + print("Running nutils") |
| 11 | + |
| 12 | + # domain size |
| 13 | + y_bottom, y_top = 0, 1 |
| 14 | + x_left, x_right = 0, 2 |
| 15 | + x_coupling = 1 # x coordinate of coupling interface |
| 16 | + |
| 17 | + n = 10 # number of mesh vertices per dimension |
| 18 | + |
| 19 | + if side == 'Dirichlet': |
| 20 | + x_grid = np.linspace(x_left, x_coupling, n) |
| 21 | + elif side == 'Neumann': |
| 22 | + x_grid = np.linspace(x_coupling, x_right, n) |
| 23 | + else: |
| 24 | + raise Exception('invalid side {!r}'.format(side)) |
| 25 | + |
| 26 | + y_grid = np.linspace(y_bottom, y_top, n) |
| 27 | + |
| 28 | + # define the Nutils mesh |
| 29 | + domain, geom = nutils.mesh.rectilinear([x_grid, y_grid]) |
| 30 | + |
| 31 | + # Nutils namespace |
| 32 | + ns = nutils.function.Namespace() |
| 33 | + ns.x = geom |
| 34 | + |
| 35 | + degree = 1 # linear finite elements |
| 36 | + ns.basis = domain.basis('std', degree=degree) |
| 37 | + ns.alpha = 3 # parameter of problem |
| 38 | + ns.beta = 1.3 # parameter of problem |
| 39 | + ns.u = 'basis_n ?lhs_n' # solution |
| 40 | + ns.dudt = 'basis_n (?lhs_n - ?lhs0_n) / ?dt' # time derivative |
| 41 | + ns.flux = 'basis_n ?fluxdofs_n' # heat flux |
| 42 | + ns.f = 'beta - 2 - 2 alpha' # rhs |
| 43 | + ns.uexact = '1 + x_0 x_0 + alpha x_1 x_1 + beta ?t' # analytical solution |
| 44 | + |
| 45 | + # define the weak form |
| 46 | + res0 = domain.integral('(basis_n dudt - basis_n f + basis_n,i u_,i) d:x' @ ns, degree=degree * 2) |
| 47 | + |
| 48 | + # set boundary conditions at non-coupling boundaries |
| 49 | + # top and bottom boundary are non-coupling for both sides |
| 50 | + sqr0 = domain.boundary['top'].integral('(u - 1 - x_0 x_0 - alpha - beta ?t)^2 d:x' @ ns, degree=degree * 2) |
| 51 | + sqr0 += domain.boundary['bottom'].integral('(u - 1 - x_0 x_0 - beta ?t)^2 d:x' @ ns, degree=degree * 2) |
| 52 | + if side == 'Dirichlet': # left boundary is non-coupling |
| 53 | + sqr0 += domain.boundary['left'].integral('(u - 1 - alpha x_1 x_1 - beta ?t)^2 d:x' @ ns, degree=degree * 2) |
| 54 | + elif side == 'Neumann': # right boundary is non-coupling |
| 55 | + sqr0 += domain.boundary['right'].integral('(u - 1 - x_0 x_0 - alpha x_1 x_1 - beta ?t)^2 d:x' @ ns, degree=degree * 2) |
| 56 | + |
| 57 | + |
| 58 | + # preCICE setup |
| 59 | + interface = precice.Interface(side, "../precice-config.xml", 0, 1) |
| 60 | + |
| 61 | + # define coupling mesh |
| 62 | + mesh_name = side + "-Mesh" |
| 63 | + mesh_id = interface.get_mesh_id(mesh_name) |
| 64 | + coupling_boundary = domain.boundary['right' if side == 'Dirichlet' else 'left'] |
| 65 | + coupling_sample = coupling_boundary.sample('gauss', degree=degree * 2) |
| 66 | + vertices = coupling_sample.eval(ns.x) |
| 67 | + vertex_ids = interface.set_mesh_vertices(mesh_id, vertices) |
| 68 | + |
| 69 | + # coupling data |
| 70 | + write_data = "Temperature" if side == "Neumann" else "Flux" |
| 71 | + read_data = "Flux" if side == "Neumann" else "Temperature" |
| 72 | + write_data_id = interface.get_data_id(write_data, mesh_id) |
| 73 | + read_data_id = interface.get_data_id(read_data, mesh_id) |
| 74 | + |
| 75 | + # helper functions to project heat flux to coupling boundary |
| 76 | + projection_matrix = coupling_boundary.integrate(ns.eval_nm('basis_n basis_m d:x'), degree=degree * 2) |
| 77 | + projection_cons = np.zeros(res0.shape) |
| 78 | + projection_cons[projection_matrix.rowsupp(1e-15)] = np.nan |
| 79 | + fluxdofs = lambda v: projection_matrix.solve(v, constrain=projection_cons) |
| 80 | + |
| 81 | + # helper data structures to integrate heat flux correctly |
| 82 | + dx = coupling_sample.eval('d:x' @ ns) |
| 83 | + dx_function = coupling_sample.asfunction(dx) |
| 84 | + |
| 85 | + precice_dt = interface.initialize() |
| 86 | + |
| 87 | + # write initial data |
| 88 | + if interface.is_action_required(precice.action_write_initial_data()): |
| 89 | + write_data = np.zeros(len(vertex_ids)) |
| 90 | + interface.write_block_scalar_data(write_data_id, vertex_ids, write_data) |
| 91 | + interface.mark_action_fulfilled(precice.action_write_initial_data()) |
| 92 | + |
| 93 | + interface.initialize_data() |
| 94 | + |
| 95 | + t = 0 |
| 96 | + |
| 97 | + # initial condition |
| 98 | + sqr = domain.integral('(u - uexact)^2' @ ns, degree=degree*2) |
| 99 | + lhs0 = nutils.solver.optimize('lhs', sqr, droptol=1e-15, arguments=dict(t=t)) |
| 100 | + bezier = domain.sample('bezier', degree * 2) |
| 101 | + x, u, uexact = bezier.eval(['x_i', 'u', 'uexact'] @ ns, lhs=lhs0, t=t) |
| 102 | + with treelog.add(treelog.DataLog()): |
| 103 | + nutils.export.vtk(side + '-0', bezier.tri, x, Temperature=u, reference=uexact) |
| 104 | + |
| 105 | + t += precice_dt |
| 106 | + timestep = 1 |
| 107 | + dt = 0.1 |
| 108 | + |
| 109 | + while interface.is_coupling_ongoing(): |
| 110 | + |
| 111 | + # update (time-dependent) boundary condition |
| 112 | + cons0 = nutils.solver.optimize('lhs', sqr0, droptol=1e-15, arguments=dict(t=t)) |
| 113 | + |
| 114 | + # read data from interface |
| 115 | + if interface.is_read_data_available(): |
| 116 | + read_data = interface.read_block_scalar_data(read_data_id, vertex_ids) |
| 117 | + read_function = coupling_sample.asfunction(read_data) |
| 118 | + |
| 119 | + if side == 'Dirichlet': |
| 120 | + sqr = coupling_sample.integral((ns.u - read_function)**2) |
| 121 | + cons = nutils.solver.optimize('lhs', sqr, droptol=1e-15, constrain=cons0, arguments=dict(t=t)) |
| 122 | + res = res0 |
| 123 | + else: |
| 124 | + cons = cons0 |
| 125 | + res = res0 + coupling_sample.integral(ns.basis * read_function * dx_function) |
| 126 | + |
| 127 | + # save checkpoint |
| 128 | + if interface.is_action_required(precice.action_write_iteration_checkpoint()): |
| 129 | + lhs_checkpoint = lhs0 |
| 130 | + interface.mark_action_fulfilled(precice.action_write_iteration_checkpoint()) |
| 131 | + |
| 132 | + # potentially adjust non-matching timestep sizes |
| 133 | + dt = min(dt, precice_dt) |
| 134 | + |
| 135 | + # solve nutils timestep |
| 136 | + lhs = nutils.solver.solve_linear('lhs', res, constrain=cons, arguments=dict(lhs0=lhs0, dt=dt, t=t)) |
| 137 | + |
| 138 | + # write data to interface |
| 139 | + if interface.is_write_data_required(dt): |
| 140 | + if side == 'Dirichlet': |
| 141 | + flux_function = res.eval(lhs0=lhs0, lhs=lhs, dt=dt, t=t) |
| 142 | + write_data = coupling_sample.eval('flux' @ ns, fluxdofs=fluxdofs(flux_function)) |
| 143 | + else: |
| 144 | + write_data = coupling_sample.eval('u' @ ns, lhs=lhs) |
| 145 | + |
| 146 | + interface.write_block_scalar_data(write_data_id, vertex_ids, write_data) |
| 147 | + |
| 148 | + # do the coupling |
| 149 | + precice_dt = interface.advance(dt) |
| 150 | + |
| 151 | + # read checkpoint if required |
| 152 | + if interface.is_action_required(precice.action_read_iteration_checkpoint()): |
| 153 | + lhs0 = lhs_checkpoint |
| 154 | + interface.mark_action_fulfilled(precice.action_read_iteration_checkpoint()) |
| 155 | + else: # go to next timestep |
| 156 | + bezier = domain.sample('bezier', degree * 2) |
| 157 | + x, u, uexact = bezier.eval(['x_i', 'u', 'uexact'] @ ns, lhs=lhs, t=t) |
| 158 | + |
| 159 | + with treelog.add(treelog.DataLog()): |
| 160 | + nutils.export.vtk(side + "-" + str(timestep), bezier.tri, x, Temperature=u, reference=uexact) |
| 161 | + |
| 162 | + t += dt |
| 163 | + timestep += 1 |
| 164 | + lhs0 = lhs |
| 165 | + |
| 166 | + interface.finalize() |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == '__main__': |
| 170 | + nutils.cli.run(main) |
0 commit comments