-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvers_common.py
383 lines (305 loc) · 12 KB
/
solvers_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Optional
import numpy as np
import petsc4py
import pyamg
from petsc4py import PETSc
from scipy.sparse import csr_matrix
from scipy.sparse.linalg import LinearOperator, gmres, spsolve
from solver_selector.simulation_runner import SimulationModel
from solver_selector.solver_space import KrylovSolverNode, SolverConfigNode
petsc4py.init()
# ------------------- Solver space -------------------
class LinearSolverNames:
richardson = "richardson"
gmres = "gmres"
fgmres = "fgmres"
none = "none"
direct = "direct"
class PreconditionerNames:
ilu = "ilu"
amg = "amg"
none = "none"
class NodePreconditionerILU(SolverConfigNode):
type = PreconditionerNames.ilu
class NodePreconditionerAMG(SolverConfigNode):
type = PreconditionerNames.amg
class DirectSolverNode(SolverConfigNode):
type = LinearSolverNames.direct
class NoKrylovSolverNode(KrylovSolverNode):
"""Placeholder to apply a preconditioner only."""
type = LinearSolverNames.none
# ------------------- Solvers implementations -------------------
@dataclass(kw_only=True, slots=True, frozen=True)
class LinearSolverStatistics:
num_iters: int
is_converged: bool
is_diverged: bool
residual_decrease: Optional[float] = None
class Preconditioner(ABC):
def __init__(self, config: Optional[dict] = None) -> None:
self.config: dict = config or {}
self.mat: csr_matrix = None
def update(self, mat: csr_matrix) -> None:
self.mat = mat
@abstractmethod
def apply(self, b: np.ndarray) -> np.ndarray:
raise NotImplementedError
def aslinearoperator(self) -> LinearOperator:
return LinearOperator(
matvec=self.apply, shape=self.mat.shape, dtype=self.mat.dtype
)
class LinearSolver:
def __init__(
self,
preconditioner: Preconditioner,
config: Optional[dict] = None,
) -> None:
self.config: dict = config or {}
self.mat: csr_matrix = None
self.preconditioner: Preconditioner = preconditioner
def update(self, mat: csr_matrix) -> None:
self.mat = mat
self.preconditioner.update(mat)
@abstractmethod
def solve(
self,
b: np.ndarray,
x0: Optional[np.ndarray] = None,
tol: Optional[float] = None,
) -> tuple[np.ndarray, LinearSolverStatistics]:
"""Returns the solution and statistics."""
raise NotImplementedError
class SplittingSchur(Preconditioner):
def __init__(
self,
linear_solver_primary: LinearSolver,
linear_solver_secondary: LinearSolver,
config: dict,
):
assert "method" in config
self.linear_solver_primary: LinearSolver = linear_solver_primary
self.linear_solver_secondary: LinearSolver = linear_solver_secondary
# They must be initialized in a problem-specific subclass
self.primary_ind: np.ndarray
self.secondary_ind: np.ndarray
self.mat_01: csr_matrix
self.mat_00: csr_matrix
self.mat_10: csr_matrix
super().__init__(config=config)
def apply(self, b):
rhs_0 = b[self.primary_ind]
rhs_1 = b[self.secondary_ind]
method = self.config["method"]
if method == "upper":
sol_1, _ = self.linear_solver_secondary.solve(rhs_1)
sol_0, _ = self.linear_solver_primary.solve(rhs_0 - self.mat_01 @ sol_1)
elif method == "lower":
sol_0, _ = self.linear_solver_primary.solve(rhs_0)
sol_1, _ = self.linear_solver_secondary.solve(rhs_1 - self.mat_10 @ sol_0)
elif method == "full":
tmp_sol, _ = self.linear_solver_primary.solve(rhs_0)
sol_1, _ = self.linear_solver_secondary.solve(rhs_1 - self.mat_10 @ tmp_sol)
sol_0, _ = self.linear_solver_primary.solve(rhs_0 - self.mat_01 @ sol_1)
else:
raise NotImplementedError(method)
result = np.zeros_like(b)
result[self.primary_ind] = sol_0
result[self.secondary_ind] = sol_1
return result
def residual_secondary(self, sol, rhs):
return np.linalg.norm(
self.linear_solver_secondary.mat.dot(sol) - rhs
) / np.linalg.norm(rhs)
def residual_primary(self, sol, rhs):
return np.linalg.norm(
self.linear_solver_primary.mat.dot(sol) - rhs
) / np.linalg.norm(rhs)
class PreconditionerPetscAMG(Preconditioner):
def __init__(self, config: Optional[dict] = None) -> None:
super().__init__(config)
self.pc = PETSc.PC().create()
options = PETSc.Options()
# options["pc_type"] = "gamg"
# options['pc_gamg_agg_nsmooths'] = 1
# options["mg_levels_ksp_type"] = "chebyshev"
# options["mg_levels_pc_type"] = "jacobi"
# options["mg_levels_ksp_chebyshev_esteig_steps"] = 10
options["pc_type"] = "hypre"
options["pc_hypre_type"] = "boomeramg"
options["pc_hypre_boomeramg_max_iter"] = config.get("max_iter", 1)
options["pc_hypre_boomeramg_cycle_type"] = config.get("cycle", "V")
# options.setValue('pc_hypre_boomeramg_relax_type_all', 'Chebyshev')
# options.setValue('pc_hypre_boomeramg_smooth_type', 'Pilut')
self.pc.setFromOptions()
self.petsc_A = PETSc.Mat()
def update(self, mat):
super().update(mat)
self.petsc_A.createAIJ(size=mat.shape, csr=(mat.indptr, mat.indices, mat.data))
self.pc.setOperators(self.petsc_A)
self.pc.setUp()
def __del__(self):
self.pc.destroy()
self.petsc_A.destroy()
def apply(self, b):
petsc_b = PETSc.Vec().createWithArray(b)
x = self.petsc_A.createVecLeft()
x.set(0.0)
self.pc.apply(petsc_b, x)
res = x.getArray()
petsc_b.destroy()
x.destroy()
return res
class PreconditionerPetscILU(Preconditioner):
def __init__(self, config: Optional[dict] = None) -> None:
super().__init__(config)
self.pc = PETSc.PC().create()
options = PETSc.Options()
options.setValue("pc_type", "ilu")
options.setValue("pc_factor_levels", 0)
options.setValue("pc_factor_diagonal_fill", None) # Doesn't affect
options.setValue("pc_factor_nonzeros_along_diagonal", None)
self.pc.setFromOptions()
self.petsc_A = PETSc.Mat()
def update(self, mat):
super().update(mat)
self.petsc_A.createAIJ(size=mat.shape, csr=(mat.indptr, mat.indices, mat.data))
self.pc.setOperators(self.petsc_A)
self.pc.setUp()
def __del__(self):
self.pc.destroy()
self.petsc_A.destroy()
def apply(self, b):
petsc_b = PETSc.Vec().createWithArray(b)
x = self.petsc_A.createVecLeft()
x.set(0.0)
self.pc.apply(petsc_b, x)
res = x.getArray()
petsc_b.destroy()
x.destroy()
return res
class LinearSolverGMRES(LinearSolver):
def __init__(
self,
preconditioner: Preconditioner,
config: dict,
) -> None:
# maxiter is a number of outer GMRES iterations.
for param in ["restart", "tol", "maxiter"]:
assert param in config, "Parameter not provided."
super().__init__(preconditioner=preconditioner, config=config)
self._num_iters: int = 0
def gmres_callback(self, x):
self._num_iters += 1
def solve(self, b, x0=None, tol: Optional[float] = None):
self._num_iters = 0
tol = tol or self.config["tol"]
restart = self.config["restart"]
maxiter = self.config["maxiter"]
if x0 is None:
res_0 = np.linalg.norm(b)
else:
res_0 = np.linalg.norm(self.mat.dot(x0) - b)
x, info = pyamg.krylov.gmres(
A=self.mat,
b=b,
x0=x0,
tol=tol,
maxiter=maxiter,
M=self.preconditioner.aslinearoperator(),
callback=self.gmres_callback,
restrt=restart,
)
res_end = np.linalg.norm(self.mat.dot(x) - b)
return x, LinearSolverStatistics(
num_iters=self._num_iters,
is_converged=(info == 0),
is_diverged=(info < 0),
residual_decrease=float(res_end / res_0),
)
class LinearSolverFGMRES(LinearSolver):
def __init__(
self,
preconditioner: Preconditioner,
config: dict,
) -> None:
# # maxiter is a number of outer GMRES iterations.
# for param in ["restart", "tol", "maxiter"]:
# assert param in config, "Parameter not provided."
super().__init__(preconditioner=preconditioner, config=config)
def solve(self, b, x0=None, tol: Optional[float] = None):
tol = tol or self.config["tol"]
restart = self.config["restart"]
maxiter = self.config["maxiter"]
if x0 is None:
res_0 = np.linalg.norm(b)
else:
res_0 = np.linalg.norm(self.mat.dot(x0) - b)
residuals = []
x, info = pyamg.krylov.fgmres(
A=self.mat,
b=b,
x0=x0,
tol=tol,
restrt=restart,
maxiter=maxiter,
M=self.preconditioner.aslinearoperator(),
residuals=residuals,
)
res_end = np.linalg.norm(self.mat.dot(x) - b)
return x, LinearSolverStatistics(
num_iters=len(residuals),
is_converged=(info == 0),
is_diverged=(info < 0),
residual_decrease=float(res_end / res_0),
)
class NoneLinearSolver(LinearSolver):
def solve(self, b, x0=None, tol: Optional[float] = None):
return self.preconditioner.apply(b), LinearSolverStatistics(
num_iters=1, is_converged=False, is_diverged=False
)
class NonePreconditioner(Preconditioner):
def apply(self, b: np.ndarray) -> np.ndarray:
return b
class LinearSolverDirect(LinearSolver):
def solve(self, b, x0=None, tol: Optional[float] = None):
return spsolve(self.mat, b), LinearSolverStatistics(
num_iters=1, is_converged=True, is_diverged=False
)
# ------------------- Solver assembling routines -------------------
class SolverAssembler:
def __init__(self, simulation: SimulationModel):
self.simulation: SimulationModel = simulation
def assemble_linear_solver(self, linear_solver_config: dict) -> LinearSolver:
linear_solver_type, linear_solver_config = list(linear_solver_config.items())[0]
preconditioner_config = linear_solver_config.get("preconditioner", {})
if len(preconditioner_config) == 0:
preconditioner_config = {PreconditionerNames.none: {}}
preconditioner = self.assemble_preconditioner(preconditioner_config)
match linear_solver_type:
case LinearSolverNames.none:
solver = NoneLinearSolver(preconditioner, config=linear_solver_config)
case LinearSolverNames.gmres:
solver = LinearSolverGMRES(preconditioner, config=linear_solver_config)
case LinearSolverNames.fgmres:
solver = LinearSolverFGMRES(preconditioner, config=linear_solver_config)
case LinearSolverNames.direct:
solver = LinearSolverDirect(preconditioner, config=linear_solver_config)
case _:
raise ValueError(linear_solver_type)
return solver
def assemble_preconditioner(self, preconditioner_config: dict) -> Preconditioner:
preconditioner_type, preconditioner_config = list(
preconditioner_config.items()
)[0]
match preconditioner_type:
case PreconditionerNames.amg:
preconditioner = PreconditionerPetscAMG(preconditioner_config)
case PreconditionerNames.ilu:
preconditioner = PreconditionerPetscILU(preconditioner_config)
case PreconditionerNames.none:
preconditioner = NonePreconditioner(preconditioner_config)
case _:
raise ValueError(preconditioner_type)
return preconditioner