From 1da93b0b791271cf4cf2d5df09bee3cc200c5766 Mon Sep 17 00:00:00 2001 From: Aditi Naiknaware Date: Thu, 19 Nov 2026 03:48:25 +0000 Subject: [PATCH] Implement factorization reuse for lock exchange (#464) --- examples/cpp/lock_exchange.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/cpp/lock_exchange.cpp b/examples/cpp/lock_exchange.cpp index 1047fc96c..f5c677a51 100644 --- a/examples/cpp/lock_exchange.cpp +++ b/examples/cpp/lock_exchange.cpp @@ -101,7 +101,18 @@ int main() { // Impose Dirichlet boundary conditions using a RobinBC RobinBC BC(k, m, dx, n, dy, 0, 1); // Neumann BC L = L + BC; - +#ifndef EIGEN + // Factorize the pressure Poisson matrix once and reuse the factorization. + // The pure-Neumann Laplacian is singular to working precision, so SuperLU + // must be allowed to proceed with an approximate factorization. + spsolve_factoriser pressure_factoriser; + superlu_opts opts; + opts.allow_ugly = true; + + if (!pressure_factoriser.factorise(L, opts)) { + throw std::runtime_error("Failed to factorize pressure Laplacian"); + } +#endif // Pre-multiply the gradient operator for pressure correction. G *= (-dt / rho_middle); @@ -174,10 +185,11 @@ int main() { #ifdef EIGEN p_vec = Utils::spsolve_eigen(L, b); #else - // Default to SuperLU if EIGEN is not defined - p_vec = spsolve(L, b); + // Reuse the SuperLU factorization because L is unchanged between time steps. + if (!pressure_factoriser.solve(p_vec, b)) { + throw std::runtime_error("Failed to solve pressure Poisson system"); + } #endif - // Reshape the solution vector back into a matrix p = reshape(p_vec, m + 2, n + 2).t();