Coverage for pySDC/implementations/problem_classes/generic_MPIFFT_Laplacian.py: 100%

89 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 14:51 +0000

1import numpy as np 

2from mpi4py import MPI 

3from mpi4py_fft import PFFT, newDistArray 

4 

5from pySDC.core.errors import ProblemError 

6from pySDC.core.problem import Problem, WorkCounter 

7from pySDC.implementations.datatype_classes.mesh import mesh, imex_mesh 

8 

9 

10class IMEX_Laplacian_MPIFFT(Problem): 

11 r""" 

12 Generic base class for IMEX problems using a spectral method to solve the Laplacian implicitly and a possible rest 

13 explicitly. The FFTs are done with``mpi4py-fft`` [1]_. 

14 Works in two and three dimensions. 

15 

16 Parameters 

17 ---------- 

18 nvars : tuple, optional 

19 Spatial resolution 

20 spectral : bool, optional 

21 If True, the solution is computed in spectral space. 

22 L : float, optional 

23 Denotes the period of the function to be approximated for the Fourier transform. 

24 alpha : float, optional 

25 Multiplicative factor before the Laplacian 

26 comm : MPI.COMM_World 

27 Communicator for parallelisation. 

28 

29 Attributes 

30 ---------- 

31 fft : PFFT 

32 Object for parallel FFT transforms. 

33 X : mesh-grid 

34 Grid coordinates in real space. 

35 K2 : matrix 

36 Laplace operator in spectral space. 

37 

38 References 

39 ---------- 

40 .. [1] Lisandro Dalcin, Mikael Mortensen, David E. Keyes. Fast parallel multidimensional FFT using advanced MPI. 

41 Journal of Parallel and Distributed Computing (2019). 

42 """ 

43 

44 dtype_u = mesh 

45 dtype_f = imex_mesh 

46 

47 xp = np 

48 fft_backend = 'fftw' 

49 fft_comm_backend = 'MPI' 

50 

51 @classmethod 

52 def setup_GPU(cls): 

53 """switch to GPU modules""" 

54 import cupy as cp 

55 from pySDC.implementations.datatype_classes.cupy_mesh import cupy_mesh, imex_cupy_mesh 

56 

57 cls.xp = cp 

58 

59 cls.dtype_u = cupy_mesh 

60 cls.dtype_f = imex_cupy_mesh 

61 

62 cls.fft_backend = 'cupy' 

63 cls.fft_comm_backend = 'NCCL' 

64 

65 def __init__( 

66 self, nvars=None, spectral=False, L=2 * np.pi, alpha=1.0, comm=MPI.COMM_WORLD, dtype='d', useGPU=False, x0=0.0 

67 ): 

68 if useGPU: 

69 self.setup_GPU() 

70 

71 if nvars is None: 

72 nvars = (128, 128) 

73 

74 if not (isinstance(nvars, tuple) and len(nvars) > 1): 

75 raise ProblemError('Need at least two dimensions for distributed FFTs') 

76 

77 # Creating FFT structure 

78 self.ndim = len(nvars) 

79 axes = tuple(range(self.ndim)) 

80 self.fft = PFFT( 

81 comm, 

82 list(nvars), 

83 axes=axes, 

84 dtype=dtype, 

85 collapse=True, 

86 backend=self.fft_backend, 

87 comm_backend=self.fft_comm_backend, 

88 ) 

89 

90 # get test data to figure out type and dimensions 

91 tmp_u = newDistArray(self.fft, spectral) 

92 

93 L = np.array([L] * self.ndim, dtype=float) 

94 

95 # invoke super init, passing the communicator and the local dimensions as init 

96 super().__init__(init=(tmp_u.shape, comm, tmp_u.dtype)) 

97 self._makeAttributeAndRegister( 

98 'nvars', 'spectral', 'L', 'alpha', 'comm', 'x0', 'useGPU', localVars=locals(), readOnly=True 

99 ) 

100 

101 self.getLocalGrid() 

102 self.getLaplacian() 

103 

104 # Need this for diagnostics 

105 self.dx = self.L[0] / nvars[0] 

106 self.dy = self.L[1] / nvars[1] 

107 

108 # work counters 

109 self.work_counters['rhs'] = WorkCounter() 

110 

111 def getLocalGrid(self): 

112 X = list(self.xp.ogrid[self.fft.local_slice(False)]) 

113 N = self.fft.global_shape() 

114 for i in range(len(N)): 

115 X[i] = self.x0 + (X[i] * self.L[i] / N[i]) 

116 self.X = [self.xp.broadcast_to(x, self.fft.shape(False)) for x in X] 

117 

118 def getLaplacian(self): 

119 s = self.fft.local_slice() 

120 N = self.fft.global_shape() 

121 k = [self.xp.fft.fftfreq(n, 1.0 / n).astype(int) for n in N] 

122 K = [ki[si] for ki, si in zip(k, s)] 

123 Ks = list(self.xp.meshgrid(*K, indexing='ij', sparse=True)) 

124 Lp = 2 * np.pi / self.L 

125 for i in range(self.ndim): 

126 Ks[i] = (Ks[i] * Lp[i]).astype(float) 

127 K = [self.xp.broadcast_to(k, self.fft.shape(True)) for k in Ks] 

128 K = self.xp.array(K).astype(float) 

129 self.K2 = self.xp.sum(K * K, 0, dtype=float) 

130 

131 def eval_f(self, u, t): 

132 """ 

133 Routine to evaluate the right-hand side of the problem. 

134 

135 Parameters 

136 ---------- 

137 u : dtype_u 

138 Current values of the numerical solution. 

139 t : float 

140 Current time at which the numerical solution is computed. 

141 

142 Returns 

143 ------- 

144 f : dtype_f 

145 The right-hand side of the problem. 

146 """ 

147 

148 f = self.dtype_f(self.init) 

149 

150 f.impl[:] = self._eval_Laplacian(u, f.impl) 

151 

152 if self.spectral: 

153 tmp = self.fft.backward(u) 

154 tmp[:] = self._eval_explicit_part(tmp, t, tmp) 

155 f.expl[:] = self.fft.forward(tmp) 

156 

157 else: 

158 f.expl[:] = self._eval_explicit_part(u, t, f.expl) 

159 

160 self.work_counters['rhs']() 

161 return f 

162 

163 def _eval_Laplacian(self, u, f_impl, alpha=None): 

164 alpha = alpha if alpha else self.alpha 

165 if self.spectral: 

166 f_impl[:] = -alpha * self.K2 * u 

167 else: 

168 u_hat = self.fft.forward(u) 

169 lap_u_hat = -alpha * self.K2 * u_hat 

170 f_impl[:] = self.fft.backward(lap_u_hat, f_impl) 

171 return f_impl 

172 

173 def _eval_explicit_part(self, u, t, f_expl): 

174 return f_expl 

175 

176 def solve_system(self, rhs, factor, u0, t): 

177 """ 

178 Simple FFT solver for the diffusion part. 

179 

180 Parameters 

181 ---------- 

182 rhs : dtype_f 

183 Right-hand side for the linear system. 

184 factor : float 

185 Abbrev. for the node-to-node stepsize (or any other factor required). 

186 u0 : dtype_u 

187 Initial guess for the iterative solver (not used here so far). 

188 t : float 

189 Current time (e.g. for time-dependent BCs). 

190 

191 Returns 

192 ------- 

193 me : dtype_u 

194 The solution as mesh. 

195 """ 

196 me = self.dtype_u(self.init) 

197 me[:] = self._invert_Laplacian(me, factor, rhs) 

198 

199 return me 

200 

201 def _invert_Laplacian(self, me, factor, rhs, alpha=None): 

202 alpha = alpha if alpha else self.alpha 

203 if self.spectral: 

204 me[:] = rhs / (1.0 + factor * alpha * self.K2) 

205 

206 else: 

207 rhs_hat = self.fft.forward(rhs) 

208 rhs_hat /= 1.0 + factor * alpha * self.K2 

209 me[:] = self.fft.backward(rhs_hat) 

210 return me