|
| 1 | +#!/bin/python3 |
| 2 | + |
| 3 | +import precice |
| 4 | +import numpy as np |
| 5 | +import math |
| 6 | +import sys |
| 7 | +from mpi4py import MPI |
| 8 | + |
| 9 | +import argparse |
| 10 | + |
| 11 | +def split(num): |
| 12 | + for a in range(math.isqrt(num), 0, -1): |
| 13 | + if num % a == 0: |
| 14 | + return a, num // a |
| 15 | + |
| 16 | + |
| 17 | +def main(): |
| 18 | + parser = argparse.ArgumentParser() |
| 19 | + parser.add_argument("participant", choices=["A", "B"]) |
| 20 | + parser.add_argument("--config", "-c", default="../precice-config.xml") |
| 21 | + parser.add_argument("--no-remesh", dest="remesh", action="store_false") |
| 22 | + parser.add_argument("--nx", type=int, default=512) |
| 23 | + parser.add_argument("--ny", type=int, default=512) |
| 24 | + args = parser.parse_args() |
| 25 | + |
| 26 | + participant_name = args.participant |
| 27 | + remote_name = "A" if participant_name == "B" else "B" |
| 28 | + |
| 29 | + # x is partitioned per rank and doesn't change |
| 30 | + nx = args.nx |
| 31 | + ny = args.ny |
| 32 | + x = 0.0, 1.0 |
| 33 | + y = 0.0, 1.0 |
| 34 | + |
| 35 | + # y grows over time |
| 36 | + newNodesPerEvent = 2 |
| 37 | + eventFrequency = 3 # time windows |
| 38 | + dz = 0.1 |
| 39 | + |
| 40 | + # Handle partitioning |
| 41 | + world = MPI.COMM_WORLD |
| 42 | + size: int = world.size |
| 43 | + rank: int = world.rank |
| 44 | + xr, yr = split(size) |
| 45 | + |
| 46 | + assert nx % xr == 0, f"Cannot split {nx=} by {xr=} ranks" |
| 47 | + assert ny % yr == 0, f"Cannot split {ny=} by {yr=} ranks" |
| 48 | + |
| 49 | + pnx = nx // xr |
| 50 | + pny = ny // yr |
| 51 | + |
| 52 | + px = rank % xr |
| 53 | + py = rank // xr |
| 54 | + print(f"Rank {rank}/{size} has partition ({px}, {py})/({xr}, {yr})") |
| 55 | + if rank == 0: |
| 56 | + print(f"Each of {size} partitions has node size {pnx}x{pny} = {pnx*pny} for a total of {nx*ny} nodes on the base") |
| 57 | + |
| 58 | + |
| 59 | + def getMesh(nz): |
| 60 | + basex = np.linspace(0, 1, nx)[px*pnx:(px+1)*pnx] |
| 61 | + basey = np.linspace(0, 1, ny)[py*pny:(py+1)*pny] |
| 62 | + z = np.array(range(nz)) * dz |
| 63 | + return np.stack(np.meshgrid(basex, basey, z, indexing="ij"), axis=-1).reshape(-1, 3) |
| 64 | + |
| 65 | + def requiresEvent(tw): |
| 66 | + return tw % eventFrequency == 0 |
| 67 | + |
| 68 | + assert not requiresEvent(eventFrequency - 1) |
| 69 | + assert requiresEvent(eventFrequency) |
| 70 | + assert not requiresEvent(eventFrequency + 1) |
| 71 | + |
| 72 | + def eventsAt(tw): |
| 73 | + # First event block at tw=0, second at eventFrequency |
| 74 | + return 1 + math.floor(tw / eventFrequency) |
| 75 | + |
| 76 | + assert eventsAt(0) == 1 |
| 77 | + assert eventsAt(eventFrequency - 1) == 1 |
| 78 | + assert eventsAt(eventFrequency) == 2 |
| 79 | + assert eventsAt(eventFrequency + 1) == 2 |
| 80 | + |
| 81 | + def getMeshAtTimeWindow(tw): |
| 82 | + znodes = eventsAt(tw) * newNodesPerEvent |
| 83 | + return getMesh(znodes) |
| 84 | + |
| 85 | + def dataAtTimeWindow(tw): |
| 86 | + znodes = eventsAt(tw) * newNodesPerEvent |
| 87 | + total = pnx * pny * znodes |
| 88 | + return np.full(total, tw) |
| 89 | + |
| 90 | + participant = precice.Participant(participant_name, args.config, rank, size) |
| 91 | + |
| 92 | + mesh_name = participant_name + "-Mesh" |
| 93 | + read_data_name = "Data-" + remote_name |
| 94 | + write_data_name = "Data-" + participant_name |
| 95 | + |
| 96 | + coords = getMeshAtTimeWindow(0) |
| 97 | + vertex_ids = participant.set_mesh_vertices(mesh_name, coords) |
| 98 | + participant.initialize() |
| 99 | + |
| 100 | + tw = 1 |
| 101 | + while participant.is_coupling_ongoing(): |
| 102 | + dt = participant.get_max_time_step_size() |
| 103 | + |
| 104 | + data = participant.read_data(mesh_name, read_data_name, vertex_ids, dt) |
| 105 | + if rank == 0: |
| 106 | + print(f"{participant_name}: data: {data[0]} * {len(data)}") |
| 107 | + |
| 108 | + if args.remesh and requiresEvent(tw): |
| 109 | + oldCount = len(coords) |
| 110 | + coords = getMeshAtTimeWindow(tw) |
| 111 | + if rank == 0: |
| 112 | + print( |
| 113 | + f"{participant_name}: Event grows local mesh from {oldCount} to { |
| 114 | + len(coords)} and global mesh from { |
| 115 | + oldCount * |
| 116 | + size} to { |
| 117 | + len(coords) * |
| 118 | + size}") |
| 119 | + participant.reset_mesh(mesh_name) |
| 120 | + vertex_ids = participant.set_mesh_vertices(mesh_name, coords) |
| 121 | + |
| 122 | + participant.write_data(mesh_name, write_data_name, vertex_ids, dataAtTimeWindow(tw)) |
| 123 | + |
| 124 | + participant.advance(dt) |
| 125 | + tw += 1 |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + try: |
| 129 | + main() |
| 130 | + except Exception as e: |
| 131 | + print(e) |
| 132 | + sys.exit(1) |
0 commit comments