qlroms.charts
Local charts: clustering, per-cluster POD, and the Chart building block.
Implements section 1 of docs/streamlined_vision_building_blocks.md: - fit_clusters: K-means++ clustering ("full" | "minibatch" | "warmstart"), optional overlapping assignment with a tolerance. - clustering_features: which representation of the snapshots k-means sees ("physical" snapshots, lossless global POD, reduced global POD, or a custom callable). - compute_pod_basis: local basis construction (snapshot POD; exact or randomized SVD; Mw-weighted method of snapshots when a mass matrix is given). - Chart: one cluster's physical <-> reduced round trip under the inner product = u^T Mw v (Chart.from_fenics + mass_to_torch_sparse adapt dolfinx-side POD output without qlroms ever importing dolfinx/petsc4py).
The Atlas container and the one-call build (fit_charts) live in qlroms.atlas.
Chart
One cluster's local chart: the physical(N-dim) <-> reduced(r-dim) round trip for a SINGLE chart (its own Phi, its own centroid), under the inner product = u^T Mw v -- Mw is None for the plain M = I case (default), a (N,) diagonal (e.g. ks2d's quadrature weights), or a dense/sparse (N, N) matrix (e.g. fenics' assembled FEM mass matrix). Phi is assumed Mw-orthonormal, so projection is a = Phi^T Mw (u - c) and recovery is c + Phi a.
This is both the building block Atlas combines pairwise into the cached
tmap/tshift, and the parent of every case single-cluster ROM class
(ks1d.rom.ROM, ks2d.rom.ROM, pinball.rom.ROM): those add
the cluster's dynamics operators + step_reduced on top of this geometry.
Methods only read self.Phi / self.centroid / self.Mw / self.device / self.rdtype, so dataclass subclasses that define those as fields inherit them without calling init here.
Source code in qlroms/charts.py
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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
project_state(state)
(N,) or (N, m) physical -> (r, m) reduced: a = Phi^T Mw (u - c).
Source code in qlroms/charts.py
390 391 392 393 394 395 396 | |
recover_state(a)
(r,) reduced -> (N,), or (r, m) -> (N, m) physical.
Source code in qlroms/charts.py
398 399 400 401 402 403 404 405 406 | |
from_fenics(modes, mean, mass=None)
classmethod
Build a Chart from dolfinx-side POD output (duck-typed: only Function.x.array and Mat.getValuesCSR/getSize are read, so qlroms never imports dolfinx or petsc4py). An atlas of these is qlroms.atlas.Atlas.from_charts(charts) -- Atlas is Mw-aware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
modes
|
sequence of dolfinx Functions -- the cluster's mass-orthonormal POD modes (columns of Phi). |
required | |
mean
|
dolfinx Function -- the cluster centroid (e.g. velocity mean). |
required | |
mass
|
assembled PETSc mass matrix, an already-converted torch tensor, or None (M = I). |
None
|
Source code in qlroms/charts.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
QuadROM
Bases: Chart
The family-neutral quadratic single-cluster ROM member: chart geometry plus
reduced (b, A, B). Shared by BOTH model families -- once the operators are
reduced, intrusive (Galerkin-projected) and non-intrusive (OpInf-fitted) members
are the same compute, the ODE da/dt = b + A a + B(a, a) stepped by ETDRK4 (or,
discrete=True, the one-step map applied directly; B may be None for a
linear/affine map). Where the operators CAME from is recorded by the thin family
subclasses (qlroms.data_driven_qlroms.opinf.OpInfROM, qlroms.intrusive_qlroms.galerkin.GalerkinROM) and
by the compilation class (qlOpinf vs qlGalerkin); this class carries no
provenance. Operators are None until seeded via set_operators.
Source code in qlroms/charts.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
etdrk4_rom
cached
property
(E, E2, Q, f1, f2, f3) ETDRK4 coefficients for the linear part A.
step_reduced(a)
One step of the pure (r, 1) reduced state: a direct map application if discrete, an ETDRK4 integration of the reduced ODE otherwise.
Source code in qlroms/charts.py
468 469 470 471 472 473 474 475 476 477 | |
fit_clusters(Xtrain, K, random_state=1, kmeans_method='minibatch', kmeans_n_init=KMEANS_N_INIT, kmeans_max_iter=KMEANS_MAX_ITER, assign_overlapping=False, overlap_tolerance=1.1)
Cluster training snapshots with KMeans.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Xtrain
|
Tensor
|
Snapshot matrix (Ndof, Ntrain). Real or complex; only real part is used. |
required |
K
|
int
|
Number of clusters. |
required |
random_state
|
int
|
KMeans random seed. |
1
|
assign_overlapping
|
bool
|
Whether to assign points to multiple clusters if they are equidistant. |
False
|
overlap_tolerance
|
float
|
Tolerance for overlapping assignments. |
1.1
|
kmeans_method selects the fitting strategy, all k-means++ seeded: "full": KMeans from scratch, n_init random restarts. "minibatch": MiniBatchKMeans only -- fast, approximate (the default). "warmstart": MiniBatchKMeans first for cheap centroids, then one full KMeans pass (n_init=1) initialized from them for full-quality convergence.
Returns:
| Name | Type | Description |
|---|---|---|
centroids |
Tensor
|
(K, Ndof) tensor on the same device/dtype as Xtrain. |
labels |
Tensor
|
(Ntrain,) long tensor of cluster assignments. |
cluster_sizes |
list[int]
|
list of int cluster populations. |
Xtrain_augment |
Tensor
|
Augmented training data tensor. (same as Xtrain if assign_overlapping=False, else repeated columns for overlapping assignments) |
aug_idx |
Tensor
|
(Naug,) long tensor mapping each augmented column back to its original Xtrain column (arange(Ntrain) if assign_overlapping=False) -- lets callers re-express the augmentation in another space (fit_charts clusters in feature space but needs physical snapshots for the POD). |
Source code in qlroms/charts.py
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 | |
compute_pod_basis(X, r, method='exact', oversampling=10, n_iter=2, block_size=256, random_state=0, return_singular_values=False, Mw=None)
Leading r left singular vectors (POD spatial modes) of an already-centered column-snapshot matrix X (N, n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Mw
|
inner-product weight (None = identity). When given (a (N,) diagonal or a
dense/sparse (N, N) mass matrix), the basis is computed by the method of
snapshots under = u^T Mw v -- the returned modes are Mw-orthonormal
(Phi^T Mw Phi = I), as a FEM mass-matrix POD requires. |
None
|
|
method
|
POD_METHODS
|
"exact": full economy SVD (torch.linalg.svd) -- exact, O(N * n * min(N, n)). "randomized": torch.svd_lowrank, PyTorch's native randomized range-finder + power iteration -- the same approach ks2d/config.py already uses for its larger spectral grids; O(N * n * (r + oversampling)). "randomized_blocked": manual block-processed randomized SVD (see _randomized_pod_basis_blocked); same asymptotic cost as "randomized" but with an explicit, inspectable oversampling/n_iter/block_size and no dependency on torch's internal implementation. |
'exact'
|
oversampling/n_iter
|
extra sampling dimension and power-iteration count for the two randomized methods (ignored for "exact"). |
required | |
block_size
|
int
|
"randomized_blocked" only -- columns processed per matmul chunk. |
256
|
random_state
|
int
|
seed for the randomized methods' Gaussian sketch (ignored for "exact"). |
0
|
return_singular_values
|
bool
|
if True, also return the corresponding leading r singular values -- e.g. for a POD spectrum plot without a second, separate (and possibly inconsistent) SVD call. |
False
|
Returns: Phi: (N, r) tensor (r may be less than requested if min(N, n) < r), or (Phi, svals) if return_singular_values is True.
Source code in qlroms/charts.py
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 | |
clustering_features(X, r=None, cluster_space='physical', pod_method='exact', random_state=1, **pod_kwargs)
Representation of the snapshots that k-means actually clusters on.
The clustering feature options. Only the distance metric used to assign labels changes; fit_charts always recomputes physical-space centroids afterwards when a non-physical space is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Tensor
|
(Ndof, Ntrain) snapshot matrix, columns = snapshots. |
required |
cluster_space
|
CLUSTER_SPACES
|
"physical": cluster directly on the raw snapshots -- correct but the most expensive k-means call when Ndof >> Ntrain. "pod_lossless": cluster on coefficients in a full-rank (rank = min(Ndof, Ntrain - 1)) global POD basis -- an exact, distance- preserving rotation of physical space, so the partition is identical to "physical" (up to k-means++'s random init) but far cheaper when Ndof >> Ntrain. "pod_r": cluster on coefficients in the same r-mode (lossy) truncation the local ROMs will use -- cheapest, but the partition can genuinely differ since truncated energy is invisible to k-means. callable: any custom feature map X -> (Nfeat, Ntrain). |
'physical'
|
r
|
int | None
|
number of modes for "pod_r" (required for that space). |
None
|
pod_method/pod_kwargs
|
forwarded to compute_pod_basis for "pod_r". |
required |
Returns: (Nfeat, Ntrain) feature matrix whose COLUMNS k-means clusters.
Source code in qlroms/charts.py
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 | |
apply_weight(Mw, x)
M @ x for the chart inner-product weight: identity when Mw is None, elementwise for a (N,) diagonal, matmul for a dense/sparse (N, N) matrix.
Source code in qlroms/charts.py
327 328 329 330 331 332 333 334 | |
mass_orthonormalize(X, Mw=None, tol=1e-12)
Modified Gram-Schmidt of the columns of X (N, m) under = u^T Mw v.
Columns whose residual Mw-norm falls below tol are dropped (near-linear
dependence), so the result may have fewer columns than X. Used e.g. to
re-orthonormalize a POD basis after supremizer enrichment (fenics intrusive
build); the output satisfies Q^T Mw Q = I.
Source code in qlroms/charts.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
mass_to_torch_sparse(mass)
PETSc AIJ mass matrix -> torch sparse COO (the chart inner-product weight Mw). Duck-typed (getValuesCSR / getSize only), so qlroms never imports petsc4py.
Source code in qlroms/charts.py
480 481 482 483 484 485 486 487 488 | |