Coverage for dragonfly_sim/core/reduced_order_model.py: 92%
386 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-21 00:13 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-21 00:13 +0000
1"""
2POD reduced-order model (ROM) of a steady flow solve: least-squares
3Petrov-Galerkin (LSPG) on a POD basis of converged solutions.
5The ROM works with any model built on CompressibleEulerModel -- the Euler
6model itself and CompressibleRANSModel, which forwards to it -- through this
7interface, and nothing model-specific::
9 u_vec the state (its layout is the basis's row layout)
10 set_up_solver(), problem the Newton problem: problem.F(x, b) and
11 problem.J(x, problem._A) assemble the residual
12 and its exact Jacobian at the current u_vec
13 (for RANS the condensed-gradient problem, which
14 updates the reconstructed gradient on the way)
15 steady_residual() context in which problem assembles the steady
16 residual (pseudo/physical time terms off)
17 positivity_step_length step length keeping density and pressure positive
18 state_scales() per-component magnitudes for the inner product
19 _assemble_physical_residual(), relative_residual_norm()
21Inner product. Snapshots and basis use (u, v)_W = u^T W v with, by default,
22the state-scaled Euclidean weight W = D^-2, D the per-component state scales
23s_k: every component on the same footing (in absolute terms the SA variable
24rho*nu_tilde is ~1e-4 against rho*E ~ 2, and a truncated SVD would ignore
25it). inner_product='l2' adds the (diagonal) DG0 mass matrix, W = M D^-2,
26i.e. int sum_k u_k v_k / s_k^2 dx; on the NACA0012 alpha sweeps it was 10-100x
27less accurate than the Euclidean weight (M emphasizes the large far-field
28cells). W is built once on the undeformed mesh: the incremental SVD keeps
29Q^T W Q = I only for a fixed W. The LSPG residual is measured in the matching
30norm ||S r||, S = D^-1 (D^-1 M^-1/2 with 'l2', on the current mesh).
31state_scaling=None with 'euclidean' is the plain Euclidean POD/LSPG.
33Solve. With V the basis (V^T W V = I), u = V a (+ mean), LSPG is the
34Gauss-Newton iteration on min ||S R(V a)||:
36 (S A V)^T (S A V) da = -(S A V)^T S r, a <- a + theta da,
38theta from positivity_step_length, optionally backtracked (Armijo) on
39||S r||^2 / 2. The reduced system is small and solved redundantly on every
40rank. Convergence follows project-sandbox benchmark_pod.py (its PODSolver and
41outer loop): passes of at most newton_max_it iterations, each stopping on
42||g|| = ||(S A V)^T S r|| < max(atol, rtol ||g_0||), with g_0 from the first
43iteration of the first pass (not reset by a restart), on a negligible step, or
44on a stall of ||g||; up to max_outer_iterations passes, converged when a pass
45ends with ||g|| < max(newton_atol, g_conv_limit ||g_0||), g_0 the same reference.
47Derivatives. The ROM solves the LSPG stationarity condition, the projected
48residual equations
50 Phi^T R(V a; x) = 0, Phi = S^T S A V,
52so when its solution is accepted the adjoint is that of these k equations
53(ReducedOrderModel.adjoint, an LSPGAdjoint). With V, S and Phi frozen at the
54solution (the basis is not differentiated, and the Gauss-Newton terms, which
55are proportional to S R, are dropped), for an output f with g = (df/du)^T:
57 (S A V)^T (S A V) lam = V^T g, psi = Phi lam = S^T S A V lam,
59and df/dx = df/dx|_u - psi^T dR/dx with the full-order dR/dx: psi takes the
60place of the full-order adjoint A^-T g. It is the Petrov-Galerkin solution of
61A^T psi = g in the span of Phi, tested with V: V^T (A^T psi - g) = 0. psi is
62computed as S Q y with S A V = Q R (a distributed thin QR) and R^T y = V^T g,
63without forming the normal matrix, whose condition number is squared.
64"""
65from dataclasses import dataclass
66from time import perf_counter
68import numpy as np
69import scipy.linalg as sp_la
70from mpi4py import MPI
71from petsc4py import PETSc
73from dragonfly_sim.core.pod import PODBasis
74from dragonfly_sim.utils.pod_utils import DiagonalWeight, tsqr
75from dragonfly_sim.utils.derived_fields import diagonal_mass_inverse
78def scaled_jacobian_basis(model, V, S, AV=None):
79 """
80 S A V at the model's current u_vec, A the exact Jacobian assembled by its
81 Newton problem (problem.J). Returns (this rank's rows of S A V, the PETSc
82 product A V); `AV`, if given, is a product of the same shape to reuse.
83 Without S the rows are a view of AV's array.
84 """
85 problem = model.problem
86 problem.J(model.u_vec.x.petsc_vec, problem._A)
87 A = problem._A
88 if AV is None:
89 AV = A.matMult(V)
90 else:
91 A.matMult(V, result=AV)
92 SAV = AV.getDenseArray()
93 if S is not None:
94 SAV = S.getArray(readonly=True)[:, None] * SAV
95 return SAV, AV
98class LSPGSolver:
99 """
100 Reduced Newton (Gauss-Newton) loop on a POD basis, driving a model's
101 Newton problem (see the module docstring).
103 Parameters
104 ----------
105 V : PETSc.Mat
106 The basis, a distributed MATDENSE in the state layout.
107 W : PETSc.Vec or None
108 The diagonal inner-product weight, for the restriction
109 a = V^T W (u - mean); None: Euclidean (V^T V = I).
110 S : PETSc.Vec or None
111 The diagonal residual weight; None: the plain residual norm.
112 mean : PETSc.Vec or None
113 Optional affine offset (u = V a + mean).
114 """
116 def __init__(self, V, comm, W=None, S=None, mean=None, rtol=1e-10, atol=1e-12, max_it=30,
117 stall_window=3, stall_rtol=1e-10, line_search=True, armijo_c=1e-4, max_backtracks=10):
118 self.V = V
119 self.comm = comm
120 self.W = W
121 self.S = S
122 self.mean = mean
123 self.rtol = rtol
124 self.atol = atol
125 self.max_it = max_it
126 self.stall_window = stall_window
127 self.stall_rtol = stall_rtol
128 self.line_search = line_search
129 self.armijo_c = armijo_c
130 self.max_backtracks = max_backtracks
131 self.converged = False
132 self.stalled = False
133 self.n_iterations = 0
134 self.last_g_norm = None
135 # ||g|| of the first iteration of the first pass: the reference of rtol in every
136 # pass of this solver, so a restart does not ask for a further relative reduction
137 self.g0 = None
138 self._AV = None
139 self._r = None
141 @property
142 def rb_size(self):
143 return self.V.getSize()[1]
145 def destroy(self):
146 for name in ("_AV", "_r"):
147 obj = getattr(self, name)
148 if obj is not None:
149 obj.destroy()
150 setattr(self, name, None)
152 # -- maps between the full and reduced spaces --------------------------
153 def restrict(self, u_vec):
154 """a = V^T W (u - mean), replicated on every rank."""
155 x = u_vec.getArray(readonly=True).copy()
156 if self.mean is not None:
157 x -= self.mean.getArray(readonly=True)
158 if self.W is not None:
159 x *= self.W.getArray(readonly=True)
160 return self.comm.allreduce(self.V.getDenseArray().T @ x, op=MPI.SUM)
162 def expand(self, a, u):
163 """u <- V a (+ mean), ghosts updated."""
164 x = u.x.petsc_vec
165 x.array[:] = self.V.getDenseArray() @ a
166 if self.mean is not None:
167 x.axpy(1.0, self.mean)
168 u.x.scatter_forward()
170 # -- the reduced Newton loop ---------------------------------------------
171 def _weighted_residual(self, model):
172 """Assemble r at the current u_vec; returns (S r local entries, ||S r||)."""
173 problem = model.problem
174 if self._r is None:
175 self._r = problem._b.duplicate()
176 problem.F(model.u_vec.x.petsc_vec, self._r)
177 r = self._r.getArray(readonly=True)
178 if self.S is not None:
179 r = self.S.getArray(readonly=True) * r
180 return r, float(np.sqrt(self.comm.allreduce(float(r @ r), op=MPI.SUM)))
182 def _reduced_system(self, model):
183 """(H, g, ||S r||) with H = (S A V)^T (S A V), g = (S A V)^T S r at the
184 current u_vec."""
185 r, r_norm = self._weighted_residual(model)
186 if self._AV is not None and self._AV.getSize()[1] != self.V.getSize()[1]:
187 self._AV.destroy()
188 self._AV = None
189 SAV, self._AV = scaled_jacobian_basis(model, self.V, self.S, self._AV)
190 H = self.comm.allreduce(SAV.T @ SAV, op=MPI.SUM)
191 g = self.comm.allreduce(SAV.T @ r, op=MPI.SUM)
192 return H, g, r_norm
194 def solve(self, model):
195 """
196 One pass of the LSPG Gauss-Newton loop: advance model.u_vec, starting
197 from its restriction onto the basis, towards the LSPG solution in the
198 span of V. Call inside model.steady_residual() for a steady solve.
199 Returns (n_iterations, converged). ReducedOrderModel.solve runs up to
200 max_outer_iterations passes.
202 Stopping rules (those of the project-sandbox PODSolver, which
203 benchmark_pod.py uses), checked at every iteration from the reduced
204 right-hand side g = (S A V)^T S r of the current state::
206 converged ||g|| < max(atol, rtol ||g_0||), g_0 from the first iteration of
207 the solver's first pass (kept over restarts); or, after a step, ||da|| < max(atol,
208 rtol max(||a||, 1))
209 stalled ||g|| > (1 - stall_rtol) times its value stall_window
210 iterations earlier
211 stop non-finite ||g||, a singular reduced Jacobian, or max_it
213 Step: the Gauss-Newton step da limited to the positivity-preserving
214 length, then (line_search) halved until the merit ||S r||^2 / 2
215 decreases sufficiently (Armijo), at most max_backtracks times; if no
216 halving does, the positivity-limited step is taken as is. A full
217 Gauss-Newton step can overshoot when the minimum residual in the
218 basis is large.
219 """
220 u = model.u_vec
221 a = self.restrict(u.x.petsc_vec)
222 dx = u.x.petsc_vec.duplicate()
223 self.converged = self.stalled = False
224 history = []
225 n = 0
226 PETSc.Sys.Print("LSPG ROM solve, basis width {}".format(self.rb_size))
227 for n in range(self.max_it):
228 self.expand(a, u)
229 H, g, r_norm = self._reduced_system(model)
230 g_norm = float(np.linalg.norm(g))
231 self.last_g_norm = g_norm
232 if self.g0 is None:
233 self.g0 = g_norm
234 if not np.isfinite(g_norm):
235 PETSc.Sys.Print("ROM: non-finite reduced residual at iteration {}; stopping".format(n))
236 break
237 if g_norm < max(self.atol, self.rtol * self.g0):
238 self.converged = True
239 PETSc.Sys.Print("ROM converged at iteration {}: |g| {:.6e} (from {:.6e})".format(n, g_norm, self.g0))
240 break
241 history.append(g_norm)
242 if len(history) > self.stall_window and \
243 g_norm > (1.0 - self.stall_rtol) * history[-(self.stall_window + 1)]:
244 self.stalled = True
245 PETSc.Sys.Print("ROM stalled at iteration {}: |g| {:.6e}, vs {:.6e} {} iterations "
246 "earlier".format(n, g_norm, history[-(self.stall_window + 1)],
247 self.stall_window))
248 break
249 try:
250 da = np.linalg.solve(H, -g)
251 except np.linalg.LinAlgError:
252 PETSc.Sys.Print("ROM: singular reduced Jacobian at iteration {}".format(n))
253 break
254 # the full-space update is +V da; positivity_step_length limits x - theta dx
255 dx.array[:] = -(self.V.getDenseArray() @ da)
256 theta, _ = model.positivity_step_length(u.x.petsc_vec, dx)
257 if self.line_search:
258 theta = self._backtrack(model, a, da, theta, 0.5 * r_norm**2, float(g @ da))
259 a = a + theta * da
260 da_norm = float(np.linalg.norm(da))
261 PETSc.Sys.Print("ROM iter {}: |g| = {:.6e}, ||S r|| = {:.6e}, |da| = {:.6e}, theta = {:.4f}".format(
262 n, g_norm, r_norm, da_norm, theta))
263 if da_norm < max(self.atol, self.rtol * max(float(np.linalg.norm(a)), 1.0)):
264 self.converged = True
265 PETSc.Sys.Print("ROM converged at iteration {}: |da| {:.6e}".format(n, da_norm))
266 break
267 self.expand(a, u)
268 dx.destroy()
269 if not self.converged:
270 PETSc.Sys.Print("[warning] ROM pass stopped after {} iterations without converging{}".format(
271 n + 1, " (stalled)" if self.stalled else ""))
272 self.n_iterations = n + 1
273 return self.n_iterations, self.converged
275 def _backtrack(self, model, a, da, theta, phi, slope):
276 """Largest theta * 2^-k (k <= max_backtracks) with phi(a + theta da)
277 <= phi + armijo_c theta slope; the given theta if there is none."""
278 t = theta
279 for _ in range(self.max_backtracks + 1):
280 self.expand(a + t * da, model.u_vec)
281 _, r_trial = self._weighted_residual(model)
282 if np.isfinite(r_trial) and 0.5 * r_trial**2 <= phi + self.armijo_c * t * slope:
283 return t
284 t *= 0.5
285 PETSc.Sys.Print("ROM: no Armijo step along da; taking theta = {:.4f}".format(theta))
286 return theta
289class LSPGAdjoint:
290 """
291 The adjoint of an accepted LSPG solution (Derivatives in the module
292 docstring): psi = S Q y, with S A V = Q R and R^T y = V^T g, for the basis
293 V and residual weight S of that solve. The factorization is formed by the
294 first solve() and reused for every later right-hand side (CSDL passes one
295 per output row). It belongs to one design and state; matches() checks
296 both.
298 Parameters
299 ----------
300 V : PETSc.Mat
301 The basis of the solve (owned by the POD, not by this object).
302 S : PETSc.Vec or None
303 The diagonal residual weight of the solve; destroyed with this object.
304 parameter_vector, solution
305 The design and this rank's owned entries of the accepted solution.
306 """
308 def __init__(self, V, S, comm, parameter_vector, solution):
309 self.V = V
310 self.S = S
311 self.comm = comm
312 self.parameter_vector = np.array(parameter_vector, dtype=float).ravel()
313 self.solution = np.array(solution, dtype=float)
314 self._Q = None
315 self._R = None
317 @property
318 def rb_size(self):
319 return self.V.getSize()[1]
321 def matches(self, parameter_vector, u_local):
322 """Whether this is the adjoint at design `parameter_vector` and state
323 `u_local` (owned entries): both equal to the accepted solve's on every
324 rank. Collective."""
325 pv = np.asarray(parameter_vector, dtype=float).ravel()
326 local = bool(np.array_equal(pv, self.parameter_vector)
327 and np.array_equal(np.asarray(u_local, dtype=float), self.solution))
328 return self.comm.allreduce(local, op=MPI.LAND)
330 def _factor(self, model):
331 """Q, R of S A V, A the steady Jacobian at the model's current u_vec."""
332 model.set_up_solver()
333 with model.steady_residual():
334 SAV, AV = scaled_jacobian_basis(model, self.V, self.S)
335 self._Q, self._R = tsqr(self.comm, SAV)
336 AV.destroy()
337 diag = np.abs(np.diag(self._R))
338 if not np.all(np.isfinite(self._R)) or diag.min() <= np.finfo(float).eps * diag.max():
339 raise ValueError("reduced adjoint: S A V is singular to working precision "
340 "(|R_ii| from {:.3e} to {:.3e})".format(diag.min(), diag.max()))
342 def solve(self, model, rhs_local):
343 """
344 psi (this rank's owned entries) for the right-hand side g, rhs_local
345 being its owned entries. The model's u_vec, mesh and angle of attack
346 must be those of the accepted solution (the first call assembles the
347 Jacobian there). Collective.
348 """
349 first = self._Q is None
350 if first:
351 self._factor(model)
352 rhs = np.asarray(rhs_local, dtype=float)
353 b = self.comm.allreduce(self.V.getDenseArray().T @ rhs, op=MPI.SUM)
354 y = sp_la.solve_triangular(self._R, b, trans='T', lower=False)
355 psi = self._Q @ y
356 if self.S is not None:
357 psi = self.S.getArray(readonly=True) * psi
358 psi_norm = np.sqrt(self.comm.allreduce(float(psi @ psi), op=MPI.SUM))
359 if not np.isfinite(psi_norm):
360 raise ValueError("reduced adjoint: non-finite solution")
361 if first:
362 diag = np.abs(np.diag(self._R))
363 PETSc.Sys.Print("reduced (LSPG) adjoint: basis width {}, |R_ii| from {:.3e} to {:.3e}, "
364 "Petrov-Galerkin residual |V^T (A^T psi - g)| / |V^T g| = {:.3e}".format(
365 self.rb_size, diag.min(), diag.max(),
366 self._galerkin_residual(model, psi, rhs, b)))
367 else:
368 PETSc.Sys.Print("reduced (LSPG) adjoint: reused the factorization, |psi| = {:.6e}".format(psi_norm))
369 return psi
371 def _galerkin_residual(self, model, psi, rhs, b):
372 """|V^T (A^T psi - g)| / |V^T g|, with the Jacobian _factor assembled
373 (zero up to round-off)."""
374 A = model.problem._A
375 x, Atx = A.createVecLeft(), A.createVecRight()
376 x.array[:] = psi
377 A.multTranspose(x, Atx)
378 res = self.comm.allreduce(self.V.getDenseArray().T @ (Atx.array - rhs), op=MPI.SUM)
379 x.destroy()
380 Atx.destroy()
381 return float(np.linalg.norm(res) / max(np.linalg.norm(b), 1e-300))
383 def destroy(self):
384 if self.S is not None:
385 self.S.destroy()
386 self.S = None
387 self._Q = self._R = None
390@dataclass
391class ROMResult:
392 solution: np.ndarray # this rank's owned entries of the ROM state
393 eta: float # ||R|| / ||u|| of the full steady residual
394 residual_norm: float
395 converged: bool # the reduced Newton loop converged
396 accepted: bool # eta < eta_threshold
397 iterations: int
398 basis_width: int
399 walltime: float
402class ReducedOrderModel:
403 """
404 POD snapshots, basis and LSPG solve for DG_windtunnel_model
405 (reduced_order_model=...). One instance per windtunnel model.
407 Basis::
409 rb_size basis width
410 basis_mode 'global' or 'local_weighted' (core/pod.py)
411 max_rank cap on the stored snapshot SVD rank (default 3 rb_size)
412 subtract_mean local_weighted only: centre the snapshots on their weighted mean,
413 u = V a + mean (default False)
414 mean_weighting 'snapshot_weights' (the local distance weights, default) or
415 'uniform' (1/n per snapshot); used with subtract_mean
416 qr_rank_tol relative weighted residual (outside the span of the stored
417 snapshots) below which a snapshot adds no direction
418 weightfunction, n_nonzero_weights, cubic_cutoff
419 local_weighted weighting (PODBasis.construct_basis);
420 cubic_cutoff = c sets the cubic support radius to
421 c min_dist + (1 - c) max_dist, widened if needed so
422 that the rb_size nearest snapshots keep a weight
423 parameter_scales fixed per-parameter factors on the snapshot distance
424 distance_weighting output-sensitivity weights on the snapshot distance, from the
425 total derivatives of the objective and constraints
426 (utils/dv_sensitivity.py): None (off), 'total_derivative'
427 (default; outputs divided by their own magnitude) or
428 'total_derivative_raw'. They are multiplied with
429 parameter_scales. Without a derivative probe feeding
430 set_distance_sensitivities there is no information, and the
431 distance stays unweighted
433 Inner product (default: Euclidean with free-stream state scaling)::
435 inner_product 'euclidean' or 'l2' (DG0 mass matrix)
436 state_scaling 'freestream' (model.state_scales()), None, or one
437 scale per state component
439 Solve and acceptance (the defaults are project-sandbox benchmark_pod.py's)::
441 newton_max_it, newton_rtol, newton_atol, stall_window, stall_rtol
442 per-pass LSPGSolver stopping rules
443 max_outer_iterations
444 LSPG passes, each restarted from the current state
445 g_conv_limit the ROM solve has converged when the last ||g|| of a pass is
446 below max(newton_atol, g_conv_limit ||g_0||), g_0 the first
447 ||g|| of the first pass (the reference of newton_rtol too);
448 0 never accepts. Keep it >= newton_rtol, or the passes stop
449 before this target
450 line_search Armijo damping of the Gauss-Newton step
451 eta_threshold the ROM solution is accepted when ||R||/||u|| is below it
452 min_snapshots snapshots needed before the ROM is tried
453 run_fom_every_evaluation
454 also run the FOM when the ROM is accepted (benchmarking;
455 the accepted output stays the ROM's)
457 After an accepted solve, `adjoint` is its LSPGAdjoint (None otherwise),
458 valid until the next solve.
459 """
461 def __init__(self, rb_size=20, basis_mode='global', max_rank=None, qr_rank_tol=1e-10,
462 subtract_mean=False, mean_weighting='snapshot_weights',
463 weightfunction='Cubic', n_nonzero_weights=None, cubic_cutoff=None,
464 parameter_scales=None, distance_weighting='total_derivative',
465 inner_product='euclidean', state_scaling='freestream',
466 newton_max_it=15, newton_rtol=1e-10, newton_atol=1e-12, stall_window=3,
467 stall_rtol=1e-10, max_outer_iterations=3, g_conv_limit=1e-10, line_search=True,
468 eta_threshold=1e-10, min_snapshots=2, run_fom_every_evaluation=False):
469 if inner_product not in ('l2', 'euclidean'):
470 raise ValueError("inner_product must be 'l2' or 'euclidean', got {!r}".format(inner_product))
471 if not (state_scaling is None or isinstance(state_scaling, str) and state_scaling == 'freestream'
472 or np.ndim(state_scaling) == 1):
473 raise ValueError("state_scaling must be 'freestream', None or one scale per state component")
474 self.rb_size = rb_size
475 self.basis_mode = basis_mode
476 if subtract_mean and basis_mode != 'local_weighted':
477 raise ValueError("subtract_mean=True needs basis_mode='local_weighted'")
478 if mean_weighting not in ('uniform', 'snapshot_weights'):
479 raise ValueError("mean_weighting must be 'uniform' or 'snapshot_weights', got {!r}".format(
480 mean_weighting))
481 self.subtract_mean = subtract_mean
482 self.mean_weighting = mean_weighting
483 self.max_rank = 3 * rb_size if max_rank is None else max_rank
484 self.qr_rank_tol = qr_rank_tol
485 self.weightfunction = weightfunction
486 self.n_nonzero_weights = n_nonzero_weights
487 self.cubic_cutoff = cubic_cutoff
488 self.parameter_scales = parameter_scales
489 if distance_weighting not in (None, 'total_derivative', 'total_derivative_raw'):
490 raise ValueError("distance_weighting must be None, 'total_derivative' or "
491 "'total_derivative_raw', got {!r}".format(distance_weighting))
492 self.distance_weighting = distance_weighting
493 # Latest mean-normalized weights, written by set_distance_sensitivities;
494 # None until a gradient evaluation has been probed. They lag the basis
495 # they weight by one design point.
496 self.distance_sensitivities = None
497 self._warned_weight_size = False
498 self.inner_product = inner_product
499 self.state_scaling = state_scaling
500 self.newton_max_it = newton_max_it
501 self.newton_rtol = newton_rtol
502 self.newton_atol = newton_atol
503 self.stall_window = stall_window
504 self.stall_rtol = stall_rtol
505 self.max_outer_iterations = max_outer_iterations
506 self.g_conv_limit = g_conv_limit
507 self.line_search = line_search
508 self.eta_threshold = eta_threshold
509 self.min_snapshots = min_snapshots
510 self.run_fom_every_evaluation = run_fom_every_evaluation
512 self.pod = None
513 self.W = None
514 self.scales = None
515 self.last_solver = None
516 self.adjoint = None
518 # ------------------------------------------------------------------
519 # Set-up and weights
520 # ------------------------------------------------------------------
521 def set_up(self, model):
522 """Build the inner-product weight and the POD on the model's
523 current (undeformed) mesh. Call once the model's boundary conditions,
524 function spaces and state exist. Collective."""
525 layout = model.u_vec.x.petsc_vec
526 if self.state_scaling is None:
527 self.scales = None
528 elif isinstance(self.state_scaling, str):
529 self.scales = np.asarray(model.state_scales(), dtype=float)
530 else:
531 self.scales = np.asarray(self.state_scaling, dtype=float)
532 if self.scales is not None and self.scales.shape != (model.n_state,):
533 raise ValueError("{} state scales for {} state components".format(
534 self.scales.shape[0], model.n_state))
535 if self.scales is not None and not np.all(self.scales > 0):
536 raise ValueError("state scales must be positive, got {}".format(self.scales))
538 if self.W is not None:
539 self.W.destroy()
540 w = self._diagonal(model, mass_power=1.0, scale_power=-2.0)
541 self.W = None if w is None else DiagonalWeight(w)
542 if self.pod is not None:
543 self.pod.destroy()
544 self.pod = PODBasis(model.mesh_obj.mesh.comm, layout.getSize(), basis_mode=self.basis_mode,
545 subtract_mean=self.subtract_mean, mean_weighting=self.mean_weighting,
546 IP_matrix=self.W, max_rank=self.max_rank, qr_rank_tol=self.qr_rank_tol)
547 PETSc.Sys.Print("Initialized POD ROM: basis_mode={}, rb_size={}, max_rank={}, n_dofs={}, "
548 "inner product {}{}".format(
549 self.basis_mode, self.rb_size, self.max_rank, layout.getSize(),
550 self.inner_product,
551 "" if self.scales is None else ", state scales {}".format(self.scales)))
553 def set_distance_sensitivities(self, norms):
554 """Take the weights of the snapshot distance from the total-derivative
555 norms of dv_sensitivity (the `norms` dict its probe passes to
556 `on_norms`): ||dF/dp_j|| over the objective and constraints, normalized
557 to mean 1. Unusable norms (non-finite, or all zero) keep the previous
558 weights."""
559 if self.distance_weighting is None:
560 return
561 weights = norms['column_norms_relative'] if self.distance_weighting == 'total_derivative' \
562 else norms['column_norms']
563 if weights is None:
564 # no output values in the derivative result
565 PETSc.Sys.Print("ROM distance weighting: output values unavailable, using the raw "
566 "total-derivative norms for this evaluation")
567 weights = norms['column_norms']
568 weights = np.asarray(weights, dtype=float).ravel()
569 if not np.all(np.isfinite(weights)) or not np.any(weights > 0.):
570 PETSc.Sys.Print("ROM distance weighting: the total-derivative norms are all zero or "
571 "not finite; keeping the previous weights")
572 return
573 self.distance_sensitivities = weights / weights.mean()
574 inert = "" if self.basis_mode == 'local_weighted' else \
575 " -- NOT IN USE, basis_mode is '{}'".format(self.basis_mode)
576 PETSc.Sys.Print("ROM distance weights ({}, {} parameters, mean-normalized): min {:.4e}, "
577 "median {:.4e}, max {:.4e}{}".format(
578 self.distance_weighting, weights.shape[0],
579 self.distance_sensitivities.min(), np.median(self.distance_sensitivities),
580 self.distance_sensitivities.max(), inert))
582 def _distance_weights(self, n_parameters):
583 """The per-parameter factors on the snapshot distance: parameter_scales
584 times the output-sensitivity weights. None where there is neither, and
585 the sensitivity part is dropped when it does not fit the parameter
586 vector (e.g. alpha is not a design variable)."""
587 scales = None if self.parameter_scales is None else np.asarray(self.parameter_scales, dtype=float)
588 weights = self.distance_sensitivities
589 if self.distance_weighting is None or weights is None:
590 return scales
591 if weights.shape[0] != n_parameters:
592 if not self._warned_weight_size:
593 PETSc.Sys.Print("ROM distance weighting: {} weights do not fit the {}-entry parameter "
594 "vector; using the distance without them. They come from the design "
595 "variables of the CSDL problem, which must be the snapshot "
596 "parameters.".format(weights.shape[0], n_parameters))
597 self._warned_weight_size = True
598 return scales
599 return weights if scales is None else scales * weights
601 def _diagonal(self, model, mass_power, scale_power):
602 """diag(M^mass_power D^scale_power) as a Vec in the state layout, on
603 the model's current mesh (None when it is the identity)."""
604 if self.inner_product == 'euclidean' and self.scales is None:
605 return None
606 V = model.functionspaces["V"]
607 if self.inner_product == 'l2':
608 d = diagonal_mass_inverse(V, model.forms.dx)
609 arr = d.array
610 arr[:] = arr ** (-mass_power)
611 else:
612 d = model.u_vec.x.petsc_vec.duplicate()
613 d.set(1.0)
614 arr = d.array
615 if self.scales is not None:
616 # owned entries come in blocks of n_state, one per cell
617 arr *= np.tile(self.scales ** scale_power, arr.shape[0] // model.n_state)
618 return d
620 @property
621 def n_snapshots(self):
622 return 0 if self.pod is None else self.pod.snapshot_matrix.n_snapshots
624 def snapshot_singular_values(self):
625 sigma = None if self.pod is None else self.pod.snapshot_matrix.Sigma
626 return None if sigma is None else sigma.getArray().copy()
628 # ------------------------------------------------------------------
629 # Snapshots and solve
630 # ------------------------------------------------------------------
631 def _check_parameter_size(self, parameter_vector):
632 stored = self.pod.snapshot_matrix.snapshot_list_parameters
633 if len(stored) and np.size(stored[0]) != np.size(parameter_vector):
634 raise ValueError(
635 "the snapshots hold {} parameters, but this design has {}: every solve must use the "
636 "same parameter vector (shape parameters, then alpha). A solve_forward() without "
637 "shape parameters mixed into a shape optimization does this; pass snapshot=False to "
638 "it.".format(np.size(stored[0]), np.size(parameter_vector)))
640 def add_snapshot(self, model, parameter_vector):
641 """Add the model's current u_vec as the snapshot at parameter_vector.
642 Returns whether it added a basis direction. Collective."""
643 self._check_parameter_size(parameter_vector)
644 added = self.pod.snapshot_matrix.process_and_append_to_snapshot_lists(
645 model.u_vec.x.petsc_vec, np.asarray(parameter_vector, dtype=float))
646 self.pod.snapshot_matrix.print_info()
647 return added
649 def discard_adjoint(self):
650 """Drop the adjoint of the last accepted solve. Collective."""
651 if self.adjoint is not None:
652 self.adjoint.destroy()
653 self.adjoint = None
655 def solve(self, model, parameter_vector):
656 """
657 The ROM solution at parameter_vector, starting from the model's
658 current u_vec (projected onto the basis); u_vec holds it afterwards.
659 Returns a ROMResult, or None when there are fewer than min_snapshots
660 snapshots. When the solution is accepted, `adjoint` is its adjoint;
661 any earlier one is discarded first. Collective.
662 """
663 self.discard_adjoint()
664 self._check_parameter_size(parameter_vector)
665 if self.n_snapshots < self.min_snapshots:
666 PETSc.Sys.Print("ROM: {} snapshot(s) stored, {} needed; skipping the ROM solve".format(
667 self.n_snapshots, self.min_snapshots))
668 return None
669 t0 = perf_counter()
670 comm = model.mesh_obj.mesh.comm
671 self.pod.construct_basis(rb_size=self.rb_size,
672 local_parametervector=np.asarray(parameter_vector, dtype=float),
673 parameter_weights=self._distance_weights(len(parameter_vector)),
674 weightfunction=self.weightfunction,
675 n_nonzero_weights=self.n_nonzero_weights,
676 cubic_cutoff=self.cubic_cutoff,
677 layout_vec=model.u_vec.x.petsc_vec)
678 if self.pod.basis is None or self.pod.basis.getSize()[1] == 0:
679 PETSc.Sys.Print("ROM: empty POD basis; skipping the ROM solve")
680 return None
682 model.set_up_solver()
683 S = self._diagonal(model, mass_power=-0.5, scale_power=-1.0)
684 solver = LSPGSolver(self.pod.basis, comm, W=None if self.W is None else self.W.w, S=S,
685 mean=self.pod.mean, rtol=self.newton_rtol, atol=self.newton_atol,
686 max_it=self.newton_max_it, stall_window=self.stall_window,
687 stall_rtol=self.stall_rtol, line_search=self.line_search)
688 # Up to max_outer_iterations passes, each restarted from the current
689 # state; converged when the pass's last |g| is below
690 # max(newton_atol, g_conv_limit g0), g0 the solver's first |g| (the
691 # LSPG stationarity residual -- the projected residual V^T r does not
692 # vanish at the LSPG solution, so it is not tested)
693 converged = False
694 iterations = 0
695 residual_norm = np.nan
696 try:
697 for _ in range(self.max_outer_iterations):
698 with model.steady_residual():
699 its, _ = solver.solve(model)
700 iterations += its
701 residual_norm = model._assemble_physical_residual()
702 if not np.isfinite(residual_norm):
703 PETSc.Sys.Print("ROM: non-finite residual; stopping")
704 break
705 g_target = max(self.newton_atol, self.g_conv_limit * solver.g0) \
706 if self.g_conv_limit > 0.0 and solver.g0 is not None else None
707 if g_target is not None and solver.last_g_norm is not None and solver.last_g_norm < g_target:
708 converged = True
709 PETSc.Sys.Print("LSPG converged: |g| = {:.6e} < {:.6e} ({:.1e} |g0|)".format(
710 solver.last_g_norm, g_target, self.g_conv_limit))
711 break
712 else:
713 PETSc.Sys.Print("ROM: {} LSPG passes without |g| < {:.1e} |g0| (last |g| {:.6e}, g0 {})".format(
714 self.max_outer_iterations, self.g_conv_limit, solver.last_g_norm, solver.g0))
715 except BaseException:
716 if S is not None:
717 S.destroy()
718 raise
719 finally:
720 solver.destroy()
721 self.last_solver = solver
722 solver.n_iterations = iterations
724 # one verdict line for every way out of the loop
725 PETSc.Sys.Print("ROM LSPG solve {}: |g| = {} (g0 {}, limit {:.1e} |g0|), {} iterations".format(
726 "CONVERGED" if converged else "NOT CONVERGED", solver.last_g_norm, solver.g0,
727 self.g_conv_limit, iterations))
729 eta = model.relative_residual_norm(residual_norm)
730 model.u_vec.x.scatter_forward()
731 result = ROMResult(solution=model.u_vec.x.petsc_vec.getArray().copy(), eta=eta,
732 residual_norm=residual_norm, converged=converged,
733 accepted=bool(np.isfinite(eta) and eta < self.eta_threshold),
734 iterations=iterations, basis_width=solver.rb_size,
735 walltime=perf_counter() - t0)
736 PETSc.Sys.Print("ROM relative residual norm eta: {} (||r||: {}); {} iterations, wall time {}".format(
737 eta, residual_norm, iterations, result.walltime))
738 if result.accepted:
739 # the basis stays valid until the next solve rebuilds it, which
740 # discards this adjoint first
741 self.adjoint = LSPGAdjoint(self.pod.basis, S, comm, parameter_vector, result.solution)
742 elif S is not None:
743 S.destroy()
744 return result
746 def destroy(self):
747 self.discard_adjoint()
748 if self.pod is not None:
749 self.pod.destroy()
750 self.pod = None
751 if self.W is not None:
752 self.W.destroy()
753 self.W = None