Coverage for pySDC/implementations/problem_classes/Quench.py: 77%

151 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-18 11:33 +0000

1import numpy as np 

2import scipy.sparse as sp 

3from scipy.sparse.linalg import spsolve, gmres 

4from scipy.linalg import inv 

5 

6from pySDC.core.errors import ProblemError 

7from pySDC.core.problem import Problem, WorkCounter 

8from pySDC.helpers import problem_helper 

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

10 

11 

12# noinspection PyUnusedLocal 

13class Quench(Problem): 

14 """ 

15 1D heat equation with a nonlinear heat source, modelling a magnet quench, fully implicit with Newton. 

16 

17 This is a toy problem [1]_ to emulate a magnet that has been cooled to temperatures where superconductivity is possible. 

18 However, there is a leak! Some point in the domain is constantly heated and when this has heated up its environment 

19 sufficiently, there will be a runaway effect heating up the entire magnet. 

20 This effect has actually lead to huge magnets being destroyed at CERN in the past and hence warrants investigation. 

21 

22 The model we use is a 1d heat equation with Neumann-zero boundary conditions, meaning this magnet is totally 

23 insulated from its environment except for the leak. 

24 We add a non-linear term that heats parts of the domain that exceed a certain temperature threshold as well as the 

25 leak itself. 

26 

27 The problem is discretised with finite difference in space and treated *fully-implicitly*. 

28 

29 Parameters 

30 ---------- 

31 Cv : float, optional 

32 Volumetric heat capacity. 

33 K : float, optional 

34 Thermal conductivity. 

35 u_thresh : float, optional 

36 Threshold for temperature. 

37 u_max : float, optional 

38 Maximum temperature. 

39 Q_max : float, optional 

40 Maximum heat source power density. 

41 leak_range : tuple of float 

42 Range of the leak. 

43 leak_type : str, optional 

44 Type of leak, choose between ``'linear'`` or ``'exponential'``. 

45 leak_transition : str, optional 

46 Indicates how the heat in the leak propagates, choose between ``'step'`` and ``'Gaussian'``. 

47 order : int, optional 

48 Order of the finite difference discretization. 

49 stencil_type : str, optional 

50 Type of stencil for finite differences. 

51 bc : str, optional 

52 Type of boundary conditions. Default is ``'neumann-zero'``. 

53 nvars : int, optional 

54 Spatial resolution. 

55 newton_tol : float, optional 

56 Tolerance for Newton to terminate. 

57 newton_maxiter : int, optional 

58 Maximum number of Newton iterations to be done. 

59 lintol : float, optional 

60 Tolerance for linear solver to be done. 

61 liniter : int, optional 

62 Maximum number of linear iterations inside the Newton solver. 

63 direct_solver : bool, optional 

64 Indicates if a direct solver should be used. 

65 inexact_linear_ratio : float, optional 

66 Ratio of tolerance of linear solver to the Newton residual, overrides `lintol` 

67 min_lintol : float, optional 

68 Minimal tolerance for the linear solver 

69 reference_sol_type : str, optional 

70 Indicates which method should be used to compute a reference solution. 

71 Choose between ``'scipy'``, ``'SDC'``, or ``'DIRK'``. 

72 

73 Attributes 

74 ---------- 

75 A : sparse matrix (CSC) 

76 FD discretization matrix of the ND grad operator. 

77 Id : sparse matrix (CSC) 

78 Identity matrix of the same dimension as A. 

79 dx : float 

80 Distance between two spatial nodes. 

81 xv : np.1darray 

82 Spatial grid values. 

83 leak : np.1darray of bool 

84 Indicates the leak. 

85 

86 References 

87 ---------- 

88 .. [1] Thermal thin shell approximation towards finite element quench simulation. E. Schnaubelt, M. Wozniak, S. Schöps. 

89 Supercond. Sci. Technol. 36 044004. DOI 10.1088/1361-6668/acbeea 

90 """ 

91 

92 dtype_u = mesh 

93 dtype_f = mesh 

94 

95 def __init__( 

96 self, 

97 Cv=1000.0, 

98 K=1000.0, 

99 u_thresh=3e-2, 

100 u_max=6e-2, 

101 Q_max=1.0, 

102 leak_range=(0.45, 0.55), 

103 leak_type='linear', 

104 leak_transition='step', 

105 order=2, 

106 stencil_type='center', 

107 bc='neumann-zero', 

108 nvars=2**7, 

109 newton_tol=1e-8, 

110 newton_maxiter=99, 

111 lintol=1e-8, 

112 liniter=99, 

113 direct_solver=True, 

114 inexact_linear_ratio=None, 

115 min_lintol=1e-12, 

116 reference_sol_type='scipy', 

117 ): 

118 """Initialization routine""" 

119 # invoke super init, passing number of dofs, dtype_u and dtype_f 

120 super().__init__(init=(nvars, None, np.dtype('float64'))) 

121 self._makeAttributeAndRegister( 

122 'Cv', 

123 'K', 

124 'u_thresh', 

125 'u_max', 

126 'Q_max', 

127 'leak_range', 

128 'leak_type', 

129 'leak_transition', 

130 'order', 

131 'stencil_type', 

132 'bc', 

133 'nvars', 

134 'direct_solver', 

135 'reference_sol_type', 

136 localVars=locals(), 

137 readOnly=True, 

138 ) 

139 self._makeAttributeAndRegister( 

140 'newton_tol', 

141 'newton_maxiter', 

142 'lintol', 

143 'liniter', 

144 'inexact_linear_ratio', 

145 'min_lintol', 

146 localVars=locals(), 

147 readOnly=False, 

148 ) 

149 

150 # setup finite difference discretization from problem helper 

151 self.dx, xvalues = problem_helper.get_1d_grid(size=self.nvars, bc=self.bc) 

152 

153 self.A, self.b = problem_helper.get_finite_difference_matrix( 

154 derivative=2, 

155 order=self.order, 

156 stencil_type=self.stencil_type, 

157 dx=self.dx, 

158 size=self.nvars, 

159 dim=1, 

160 bc=self.bc, 

161 ) 

162 self.A *= self.K / self.Cv 

163 

164 self.xv = xvalues 

165 self.Id = sp.eye(np.prod(self.nvars), format='csc') 

166 

167 self.leak = np.logical_and(self.xv > self.leak_range[0], self.xv < self.leak_range[1]) 

168 

169 self.work_counters['newton'] = WorkCounter() 

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

171 if not self.direct_solver: 

172 self.work_counters['linear'] = WorkCounter() 

173 

174 def eval_f_non_linear(self, u, t): 

175 """ 

176 Get the non-linear part of f. 

177 

178 Parameters 

179 ---------- 

180 u : dtype_u 

181 Current values of the numerical solution: 

182 t : float 

183 Current time at which the numerical solution is computed. 

184 

185 Returns 

186 ------- 

187 me : dtype_u 

188 The non-linear part of the right-hand side. 

189 """ 

190 u_thresh = self.u_thresh 

191 u_max = self.u_max 

192 Q_max = self.Q_max 

193 me = self.dtype_u(self.init) 

194 

195 if self.leak_type == 'linear': 

196 me[:] = (u - u_thresh) / (u_max - u_thresh) * Q_max 

197 elif self.leak_type == 'exponential': 

198 me[:] = Q_max * (np.exp(u) - np.exp(u_thresh)) / (np.exp(u_max) - np.exp(u_thresh)) 

199 else: 

200 raise NotImplementedError(f'Leak type \"{self.leak_type}\" not implemented!') 

201 

202 me[u < u_thresh] = 0 

203 if self.leak_transition == 'step': 

204 me[self.leak] = Q_max 

205 elif self.leak_transition == 'Gaussian': 

206 me[:] = np.max([me, Q_max * np.exp(-((self.xv - 0.5) ** 2) / 3e-2)], axis=0) 

207 else: 

208 raise NotImplementedError(f'Leak transition \"{self.leak_transition}\" not implemented!') 

209 

210 me[u >= u_max] = Q_max 

211 

212 me[:] /= self.Cv 

213 

214 return me 

215 

216 def eval_f(self, u, t): 

217 """ 

218 Evaluate the full right-hand side. 

219 

220 Parameters 

221 ---------- 

222 u : dtype_u 

223 Current values of the numerical solution. 

224 t : float 

225 Current time at which the numerical solution is computed. 

226 

227 Returns 

228 ------- 

229 f : dtype_f 

230 The right-hand side of the problem. 

231 """ 

232 f = self.dtype_f(self.init) 

233 f[:] = self.A.dot(u.flatten()).reshape(self.nvars) + self.b + self.eval_f_non_linear(u, t) 

234 

235 self.work_counters['rhs']() 

236 return f 

237 

238 def get_non_linear_Jacobian(self, u): 

239 """ 

240 Evaluate the non-linear part of the Jacobian only. 

241 

242 Parameters 

243 ---------- 

244 u : dtype_u 

245 Current values of the numerical solution. 

246 

247 Returns 

248 ------- 

249 scipy.sparse.csc 

250 The derivative of the non-linear part of the solution w.r.t. to the solution. 

251 """ 

252 u_thresh = self.u_thresh 

253 u_max = self.u_max 

254 Q_max = self.Q_max 

255 me = self.dtype_u(self.init) 

256 

257 if self.leak_type == 'linear': 

258 me[:] = Q_max / (u_max - u_thresh) 

259 elif self.leak_type == 'exponential': 

260 me[:] = Q_max * np.exp(u) / (np.exp(u_max) - np.exp(u_thresh)) 

261 else: 

262 raise NotImplementedError(f'Leak type {self.leak_type} not implemented!') 

263 

264 me[u < u_thresh] = 0 

265 if self.leak_transition == 'step': 

266 me[self.leak] = 0 

267 elif self.leak_transition == 'Gaussian': 

268 me[self.leak] = 0 

269 me[self.leak][u[self.leak] > Q_max * np.exp(-((self.xv[self.leak] - 0.5) ** 2) / 3e-2)] = 1 

270 else: 

271 raise NotImplementedError(f'Leak transition \"{self.leak_transition}\" not implemented!') 

272 me[u > u_max] = 0 

273 

274 me[:] /= self.Cv 

275 

276 return sp.diags(me, format='csc') 

277 

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

279 r""" 

280 Simple Newton solver for :math:`(I - factor \cdot f)(\vec{u}) = \vec{rhs}`. 

281 

282 Parameters 

283 ---------- 

284 rhs : dtype_f 

285 Right-hand side. 

286 factor : float 

287 Abbrev. for the local stepsize (or any other factor required). 

288 u0 : dtype_u 

289 Initial guess for the iterative solver. 

290 t : float 

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

292 

293 Returns 

294 ------- 

295 u : dtype_u 

296 The solution as mesh. 

297 """ 

298 u = self.dtype_u(u0) 

299 res = np.inf 

300 delta = self.dtype_u(self.init, val=0.0) 

301 

302 # construct a preconditioner for the space solver 

303 if not self.direct_solver: 

304 M = inv((self.Id - factor * self.A).toarray()) 

305 zero = self.dtype_u(self.init, val=0.0) 

306 

307 for n in range(0, self.newton_maxiter): 

308 # assemble G such that G(u) = 0 at the solution of the step 

309 G = u - factor * self.eval_f(u, t) - rhs 

310 self.work_counters[ 

311 'rhs' 

312 ].decrement() # Work regarding construction of the Jacobian etc. should count into the Newton iterations only 

313 

314 res = np.linalg.norm(G, np.inf) 

315 if res <= self.newton_tol and n > 0: # we want to make at least one Newton iteration 

316 break 

317 

318 if self.inexact_linear_ratio: 

319 self.lintol = max([res * self.inexact_linear_ratio, self.min_lintol]) 

320 

321 # assemble Jacobian J of G 

322 J = self.Id - factor * (self.A + self.get_non_linear_Jacobian(u)) 

323 

324 # solve the linear system 

325 if self.direct_solver: 

326 delta = spsolve(J, G) 

327 else: 

328 delta, info = gmres( 

329 J, 

330 G, 

331 x0=zero, 

332 M=M, 

333 rtol=self.lintol, 

334 maxiter=self.liniter, 

335 atol=0, 

336 callback=self.work_counters['linear'], 

337 callback_type='legacy', 

338 ) 

339 

340 if not np.isfinite(delta).all(): 

341 break 

342 

343 # update solution 

344 u = u - delta 

345 

346 self.work_counters['newton']() 

347 

348 return u 

349 

350 def u_exact(self, t, u_init=None, t_init=None): 

351 r""" 

352 Routine to compute the exact solution at time :math:`t`. 

353 

354 Parameters 

355 ---------- 

356 t : float 

357 Time of the exact solution. 

358 u_init : dtype_u, optional 

359 Initial conditions for getting the exact solution. 

360 t_init : float, optional 

361 The starting time. 

362 

363 Returns 

364 ------- 

365 me : dtype_u 

366 The exact solution. 

367 """ 

368 

369 me = self.dtype_u(self.init, val=0.0) 

370 

371 if t > 0: 

372 if self.reference_sol_type == 'scipy': 

373 

374 def jac(t, u): 

375 """ 

376 Get the Jacobian for the implicit BDF method to use in `scipy.solve_ivp` 

377 

378 Parameters 

379 ---------- 

380 t : float 

381 The current time. 

382 u : dtype_u 

383 Current solution. 

384 

385 Returns 

386 ------- 

387 scipy.sparse.csc 

388 The derivative of the non-linear part of the solution w.r.t. to the solution. 

389 """ 

390 return self.A + self.get_non_linear_Jacobian(u) 

391 

392 def eval_rhs(t, u): 

393 """ 

394 Function to pass to `scipy.solve_ivp` to evaluate the full right-hand side. 

395 

396 Parameters 

397 ---------- 

398 t : float 

399 Current time. 

400 u : numpy.1darray 

401 Current solution. 

402 

403 Returns 

404 ------- 

405 numpy.1darray 

406 Right-hand side. 

407 """ 

408 return self.eval_f(u.reshape(self.init[0]), t).flatten() 

409 

410 me[:] = self.generate_scipy_reference_solution(eval_rhs, t, u_init, t_init, method='BDF', jac=jac) 

411 

412 elif self.reference_sol_type in ['DIRK', 'SDC']: 

413 from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI 

414 from pySDC.implementations.hooks.log_solution import LogSolution 

415 from pySDC.helpers.stats_helper import get_sorted 

416 

417 description = {} 

418 description['problem_class'] = Quench 

419 description['problem_params'] = { 

420 'newton_tol': 1e-10, 

421 'newton_maxiter': 99, 

422 'nvars': 2**10, 

423 **self.params, 

424 } 

425 

426 if self.reference_sol_type == 'DIRK': 

427 from pySDC.implementations.sweeper_classes.Runge_Kutta import DIRK43 

428 from pySDC.implementations.convergence_controller_classes.adaptivity import AdaptivityRK 

429 

430 description['sweeper_class'] = DIRK43 

431 description['sweeper_params'] = {} 

432 description['step_params'] = {'maxiter': 1} 

433 description['level_params'] = {'dt': 1e-4} 

434 description['convergence_controllers'] = {AdaptivityRK: {'e_tol': 1e-9, 'update_order': 4}} 

435 elif self.reference_sol_type == 'SDC': 

436 from pySDC.implementations.sweeper_classes.generic_implicit import generic_implicit 

437 

438 description['sweeper_class'] = generic_implicit 

439 description['sweeper_params'] = {'num_nodes': 3, 'QI': 'IE', 'quad_type': 'RADAU-RIGHT'} 

440 description['step_params'] = {'maxiter': 99} 

441 description['level_params'] = {'dt': 0.5, 'restol': 1e-10} 

442 

443 controller_params = {'hook_class': LogSolution, 'mssdc_jac': False, 'logger_level': 99} 

444 

445 controller = controller_nonMPI( 

446 description=description, controller_params=controller_params, num_procs=1 

447 ) 

448 

449 uend, stats = controller.run( 

450 u0=u_init if u_init is not None else self.u_exact(t=0.0), 

451 t0=t_init if t_init is not None else 0, 

452 Tend=t, 

453 ) 

454 

455 u_last = get_sorted(stats, type='u', recomputed=False)[-1] 

456 

457 if abs(u_last[0] - t) > 1e-2: 

458 self.logger.warning( 

459 f'Time difference between reference solution and requested time is {abs(u_last[0]-t):.2e}!' 

460 ) 

461 

462 me[:] = u_last[1] 

463 

464 return me 

465 

466 

467class QuenchIMEX(Quench): 

468 """ 

469 1D heat equation with a nonlinear heat source, modelling a magnet quench, IMEX with diffusion implicit. 

470 

471 This is a toy problem [1]_ to emulate a magnet that has been cooled to temperatures where superconductivity is possible. 

472 However, there is a leak! Some point in the domain is constantly heated and when this has heated up its environment 

473 sufficiently, there will be a runaway effect heating up the entire magnet. 

474 This effect has actually lead to huge magnets being destroyed at CERN in the past and hence warrants investigation. 

475 

476 The model we use is a 1d heat equation with Neumann-zero boundary conditions, meaning this magnet is totally 

477 insulated from its environment except for the leak. 

478 We add a non-linear term that heats parts of the domain that exceed a certain temperature threshold as well as the 

479 leak itself. 

480 

481 The problem is discretised with finite difference in space and treated *semi-implicitly*. 

482 """ 

483 

484 dtype_f = imex_mesh 

485 

486 def eval_f(self, u, t): 

487 """ 

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

489 

490 Parameters 

491 ---------- 

492 u : dtype_u 

493 Current values of the numerical solution. 

494 t : float 

495 Current time of the numerical solution is computed. 

496 

497 Returns 

498 ------- 

499 f : dtype_f 

500 The right-hand side of the problem. 

501 """ 

502 

503 f = self.dtype_f(self.init) 

504 f.impl[:] = self.A.dot(u.flatten()).reshape(self.nvars) 

505 f.expl[:] = self.eval_f_non_linear(u, t) + self.b 

506 

507 self.work_counters['rhs']() 

508 return f 

509 

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

511 r""" 

512 Simple linear solver for :math:`(I - factor \cdot f_{expl})(\vec{u}) = \vec{rhs}`. 

513 

514 Parameters 

515 ---------- 

516 rhs : dtype_f 

517 Right-hand side for the linear system. 

518 factor : float 

519 Abbrev. for the local stepsize (or any other factor required). 

520 u0 : dtype_u 

521 Initial guess for the iterative solver. 

522 t : float 

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

524 

525 Returns 

526 ------- 

527 me : dtype_u 

528 The solution as mesh. 

529 """ 

530 

531 me = self.dtype_u(self.init) 

532 me[:] = spsolve(self.Id - factor * self.A, rhs.flatten()).reshape(self.nvars) 

533 return me 

534 

535 def u_exact(self, t, u_init=None, t_init=None): 

536 r""" 

537 Routine to compute the exact solution at time :math:`t`. 

538 

539 Parameters 

540 ---------- 

541 t : float 

542 Time of the exact solution. 

543 u_init : dtype_u, optional 

544 Initial conditions for getting the exact solution. 

545 t_init : float, optional 

546 The starting time. 

547 

548 Returns 

549 ------- 

550 me : dtype_u 

551 The exact solution. 

552 """ 

553 me = self.dtype_u(self.init, val=0.0) 

554 

555 if t == 0: 

556 me[:] = super().u_exact(t, u_init, t_init) 

557 

558 if t > 0: 

559 

560 def jac(t, u): 

561 """ 

562 Get the Jacobian for the implicit BDF method to use in `scipy.solve_ivp`. 

563 

564 Parameters 

565 ---------- 

566 t : float 

567 Current time. 

568 u : dtype_u 

569 Current solution. 

570 

571 Returns 

572 ------- 

573 scipy.sparse.csc 

574 The derivative of the non-linear part of the solution w.r.t. to the solution. 

575 """ 

576 return self.A 

577 

578 def eval_rhs(t, u): 

579 """ 

580 Function to pass to `scipy.solve_ivp` to evaluate the full right-hand side. 

581 

582 Parameters 

583 ---------- 

584 t : float 

585 Current time 

586 u : numpy.1darray 

587 Current solution 

588 

589 Returns 

590 ------- 

591 numpy.1darray 

592 The right-hand side. 

593 """ 

594 f = self.eval_f(u.reshape(self.init[0]), t) 

595 return (f.impl + f.expl).flatten() 

596 

597 me[:] = self.generate_scipy_reference_solution(eval_rhs, t, u_init, t_init, method='BDF', jac=jac) 

598 return me