Skip to content

Commit 34dc003

Browse files
committed
helper function
1 parent b379bfd commit 34dc003

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

Common/src/linear_algebra/CSysMatrix.cpp

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,18 @@ void CSysMatrix<ScalarType>::SetValDiagonalZero() {
494494
END_SU2_OMP_FOR
495495
}
496496

497+
/*--- Helper function to regularize small pivots ---*/
498+
template <class ScalarType>
499+
inline void RegularizePivot(ScalarType& pivot, unsigned long row, unsigned long col, const char* context) {
500+
const float eps = 1e-12;
501+
if (std::abs(pivot) < eps) {
502+
pivot = std::copysign(eps, SU2_TYPE::GetValue(pivot));
503+
#ifndef NDEBUG
504+
std::cout << context << ": Regularized small pivot A(" << row << "," << col << ") to " << pivot << std::endl;
505+
#endif
506+
}
507+
}
508+
497509
template <class ScalarType>
498510
void CSysMatrix<ScalarType>::Gauss_Elimination(ScalarType* matrix, ScalarType* vec) const {
499511
#ifdef USE_MKL_LAPACK
@@ -504,21 +516,12 @@ void CSysMatrix<ScalarType>::Gauss_Elimination(ScalarType* matrix, ScalarType* v
504516
#else
505517
#define A(I, J) matrix[(I)*nVar + (J)]
506518

507-
/*--- Regularization epsilon to prevent divide-by-zero ---*/
508-
//constexpr passivedouble eps = 1e-12;
509-
const su2double eps = 1e-12;
510-
511519
/*--- Transform system in Upper Matrix ---*/
512520

513521
for (auto iVar = 1ul; iVar < nVar; iVar++) {
514522
for (auto jVar = 0ul; jVar < iVar; jVar++) {
515523
/*--- Regularize pivot if too small to prevent divide-by-zero ---*/
516-
if (std::abs(A(jVar, jVar)) < eps) {
517-
//A(jVar, jVar) = (A(jVar, jVar) >= ScalarType(0)) ? ScalarType(eps) : ScalarType(-eps);
518-
A(jVar, jVar) = (A(jVar, jVar) >= 0.0 ? eps : -eps);
519-
std::cout << "DEBUG Gauss_Elimination: Regularized small pivot A(" << jVar << "," << jVar << ") to "
520-
<< A(jVar, jVar) << std::endl;
521-
}
524+
RegularizePivot(A(jVar, jVar), jVar, jVar, "DEBUG Gauss_Elimination");
522525

523526
ScalarType weight = A(iVar, jVar) / A(jVar, jVar);
524527

@@ -533,12 +536,7 @@ void CSysMatrix<ScalarType>::Gauss_Elimination(ScalarType* matrix, ScalarType* v
533536
for (auto jVar = iVar + 1; jVar < nVar; jVar++) vec[iVar] -= A(iVar, jVar) * vec[jVar];
534537

535538
/*--- Regularize diagonal if too small ---*/
536-
if (std::abs(A(iVar, iVar)) < eps) {
537-
//A(iVar, iVar) = (A(iVar, iVar) >= ScalarType(0)) ? ScalarType(eps) : ScalarType(-eps);
538-
A(iVar, iVar) = (A(iVar, iVar) >= 0.0 ? eps : -eps);
539-
std::cout << "DEBUG Gauss_Elimination backsubst: Regularized small diagonal A(" << iVar << "," << iVar << ") to "
540-
<< A(iVar, iVar) << std::endl;
541-
}
539+
RegularizePivot(A(iVar, iVar), iVar, iVar, "DEBUG Gauss_Elimination backsubst");
542540

543541
vec[iVar] /= A(iVar, iVar);
544542
}
@@ -570,20 +568,11 @@ void CSysMatrix<ScalarType>::MatrixInverse(ScalarType* matrix, ScalarType* inver
570568
#else
571569
#define A(I, J) matrix[(I)*nVar + (J)]
572570

573-
/*--- Regularization epsilon to prevent divide-by-zero ---*/
574-
const float eps = 1e-12;
575-
576571
/*--- Transform system in Upper Matrix ---*/
577572
for (auto iVar = 1ul; iVar < nVar; iVar++) {
578573
for (auto jVar = 0ul; jVar < iVar; jVar++) {
579574
/*--- Regularize pivot if too small to prevent divide-by-zero ---*/
580-
if (std::abs(A(jVar, jVar)) < eps) {
581-
A(jVar, jVar) = std::copysign(eps, SU2_TYPE::GetValue(A(jVar, jVar)));
582-
#ifndef NDEBUG
583-
std::cout << "MatrixInverse: Regularized small pivot A(" << jVar << "," << jVar << ") to "
584-
<< A(jVar, jVar) << " at iVar=" << iVar << std::endl;
585-
#endif
586-
}
575+
RegularizePivot(A(jVar, jVar), jVar, jVar, "MatrixInverse");
587576

588577
ScalarType weight = A(iVar, jVar) / A(jVar, jVar);
589578
for (auto kVar = jVar; kVar < nVar; kVar++) A(iVar, kVar) -= weight * A(jVar, kVar);
@@ -600,11 +589,7 @@ void CSysMatrix<ScalarType>::MatrixInverse(ScalarType* matrix, ScalarType* inver
600589
for (auto kVar = 0ul; kVar < nVar; kVar++) M(iVar, kVar) -= A(iVar, jVar) * M(jVar, kVar);
601590

602591
/*--- Regularize diagonal if too small ---*/
603-
if (std::abs(A(iVar, iVar)) < eps) {
604-
A(iVar, iVar) = (A(iVar, iVar) >= 0.0 ? eps : -eps);
605-
std::cout << "DEBUG MatrixInverse backsubst: Regularized small diagonal A(" << iVar << "," << iVar << ") to "
606-
<< A(iVar, iVar) << std::endl;
607-
}
592+
RegularizePivot(A(iVar, iVar), iVar, iVar, "DEBUG MatrixInverse backsubst");
608593

609594
for (auto kVar = 0ul; kVar < nVar; kVar++) {
610595
M(iVar, kVar) /= A(iVar, iVar);

0 commit comments

Comments
 (0)