# -*- coding: utf-8 -*- """ This python module implements the FORCE flux for the shallow water equations Copyright (C) 2016 SINTEF ICT This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ # Import packages we need import numpy as np from GPUSimulators.common import ArakawaA2D from GPUSimulators.simulator import BaseSimulator, BoundaryCondition from GPUSimulators.gpu import GPUHandler class Force(BaseSimulator): """ Class that solves the SW equations """ def __init__(self, context, h0, hu0, hv0, nx, ny, dx, dy, g, cfl_scale=0.9, boundary_conditions=BoundaryCondition(), block_width=16, block_height=16, dt: float = None, compile_opts: list[str] = []): """ Initialization routine Args: h0: Water depth incl ghost cells, (nx+1)*(ny+1) cells hu0: Initial momentum along x-axis incl ghost cells, (nx+1)*(ny+1) cells hv0: Initial momentum along y-axis incl ghost cells, (nx+1)*(ny+1) cells nx: Number of cells along x-axis ny: Number of cells along y-axis dx: Grid cell spacing along x-axis (20 000 m) dy: Grid cell spacing along y-axis (20 000 m) dt: Size of each timestep (90 s) g: Gravitational accelleration (9.81 m/s^2) compile_opts: Pass a list of nvcc compiler options """ # Call super constructor super().__init__(context, nx, ny, dx, dy, boundary_conditions, cfl_scale, 1, block_width, block_height) self.g = np.float32(g) # Get kernels module = context.get_module("SWE2D_FORCE", "FORCEKernel", defines={ 'BLOCK_WIDTH': self.block_size[0], 'BLOCK_HEIGHT': self.block_size[1] }, compile_args={ 'cuda': { 'no_extern_c': True, 'options': ["--use_fast_math"] + compile_opts, }, 'hip': compile_opts, }, jit_compile_args={}) self.handler = GPUHandler(context, module, "FORCEKernel", "iiffffiPiPiPiPiPiPiPiiii", self.grid_size) # Create data by uploading to the device self.u0 = ArakawaA2D(self.stream, nx, ny, 1, 1, [h0, hu0, hv0]) self.u1 = ArakawaA2D(self.stream, nx, ny, 1, 1, [None, None, None]) if dt is None: dt_x = np.min(self.dx / (np.abs(hu0 / h0) + np.sqrt(g * h0))) dt_y = np.min(self.dy / (np.abs(hv0 / h0) + np.sqrt(g * h0))) self.dt = min(dt_x, dt_y) else: self.dt = dt self.handler.array_fill(self.dt, self.stream) def substep(self, dt, step_number): self.handler.prepared_call(self.grid_size, self.block_size, self.stream, [self.nx, self.ny, self.dx, self.dy, dt, self.g, self.boundary_conditions, self.u0[0].data, self.u0[0].get_pitch(), self.u0[1].data, self.u0[1].get_pitch(), self.u0[2].data, self.u0[2].get_pitch(), self.u1[0].data, self.u1[0].get_pitch(), self.u1[1].data, self.u1[1].get_pitch(), self.u1[2].data, self.u1[2].get_pitch(), self.handler.cfl_data, 0, 0, self.nx, self.ny]) self.u0, self.u1 = self.u1, self.u0 def get_output(self): return self.u0 def check(self): self.u0.check() self.u1.check() def compute_dt(self): max_dt = self.handler.array_min(self.stream) return max_dt