Skip to content

Commit 9e13773

Browse files
committed
cleanup code
1 parent 5b56b32 commit 9e13773

File tree

7 files changed

+122
-131
lines changed

7 files changed

+122
-131
lines changed

Common/include/linear_algebra/CSysMatrix.hpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,22 @@ class CSysMatrix {
553553
SetBlock<MatrixType, false>(block_i, block_j, val_block, alpha);
554554
}
555555

556+
/*!
557+
* \brief Returns the 4 blocks ii, ij, ji, jj used by "UpdateBlocks".
558+
* \note This method assumes an FVM-type sparse pattern.
559+
* \param[in] edge - Index of edge that connects iPoint and jPoint.
560+
* \param[in] iPoint - Row to which we add the blocks.
561+
* \param[in] jPoint - Row from which we subtract the blocks.
562+
* \param[out] bii, bij, bji, bjj - Blocks of the matrix.
563+
*/
564+
inline void GetBlocks(unsigned long iEdge, unsigned long iPoint, unsigned long jPoint, ScalarType*& bii,
565+
ScalarType*& bij, ScalarType*& bji, ScalarType*& bjj) {
566+
bii = &matrix[dia_ptr[iPoint] * nVar * nEqn];
567+
bjj = &matrix[dia_ptr[jPoint] * nVar * nEqn];
568+
bij = &matrix[edge_ptr(iEdge, 0) * nVar * nEqn];
569+
bji = &matrix[edge_ptr(iEdge, 1) * nVar * nEqn];
570+
}
571+
556572
/*!
557573
* \brief Update 4 blocks ii, ij, ji, jj (add to i* sub from j*).
558574
* \note This method assumes an FVM-type sparse pattern.
@@ -566,10 +582,8 @@ class CSysMatrix {
566582
template <class MatrixType, class OtherType = ScalarType>
567583
inline void UpdateBlocks(unsigned long iEdge, unsigned long iPoint, unsigned long jPoint, const MatrixType& block_i,
568584
const MatrixType& block_j, OtherType scale = 1) {
569-
ScalarType* bii = &matrix[dia_ptr[iPoint] * nVar * nEqn];
570-
ScalarType* bjj = &matrix[dia_ptr[jPoint] * nVar * nEqn];
571-
ScalarType* bij = &matrix[edge_ptr(iEdge, 0) * nVar * nEqn];
572-
ScalarType* bji = &matrix[edge_ptr(iEdge, 1) * nVar * nEqn];
585+
ScalarType *bii, *bij, *bji, *bjj;
586+
GetBlocks(iEdge, iPoint, jPoint, bii, bij, bji, bjj);
573587

574588
unsigned long iVar, jVar, offset = 0;
575589

SU2_CFD/include/numerics/turbulent/turb_diffusion.hpp

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class CAvgGrad_TurbSA final : public CAvgGrad_Scalar<FlowIndices> {
5454
const su2double sigma = 2.0/3.0;
5555
const su2double cb2 = 0.622;
5656

57+
const bool use_accurate_jacobians;
58+
5759
/*!
5860
* \brief Adds any extra variables to AD
5961
*/
@@ -68,41 +70,39 @@ class CAvgGrad_TurbSA final : public CAvgGrad_Scalar<FlowIndices> {
6870

6971
/*--- Compute mean effective viscosity ---*/
7072

71-
/* First Term */
72-
su2double nu_i = Laminar_Viscosity_i/Density_i;
73-
su2double nu_j = Laminar_Viscosity_j/Density_j;
74-
su2double nu_e = 0.5*(nu_i + nu_j + (1+cb2)*(ScalarVar_i[0] + ScalarVar_j[0]));
75-
su2double term_1 = nu_e;
73+
/*--- First Term. Normal diffusion, and conservative part of the quadratic diffusion.
74+
* ||grad nu_t||^2 = div(nu_t grad nu_t) - nu_t div grad nu_t ---*/
75+
const su2double nu_i = Laminar_Viscosity_i/Density_i;
76+
const su2double nu_j = Laminar_Viscosity_j/Density_j;
77+
const su2double nu_e = 0.5 * (nu_i + nu_j + (1 + cb2) * (ScalarVar_i[0] + ScalarVar_j[0]));
78+
const su2double term_1 = nu_e;
7679

77-
/* Second Term */
78-
su2double nu_tilde_i = ScalarVar_i[0];
79-
su2double term_2 = cb2 * nu_tilde_i;
80+
/* Second Term (quadratic diffusion, non conservative). */
81+
const su2double nu_tilde_i = ScalarVar_i[0];
82+
const su2double term_2 = cb2 * nu_tilde_i;
8083

81-
Flux[0] = (term_1 - term_2)*Proj_Mean_GradScalarVar[0]/sigma;
82-
su2double dTerm1_dnut_i = (1+cb2)*0.5;
83-
su2double dTerm1_dnut_j = (1+cb2)*0.5;
84+
const su2double diffusion_coefficient = term_1 - term_2;
85+
Flux[0] = diffusion_coefficient * Proj_Mean_GradScalarVar[0] / sigma;
8486

85-
su2double dTerm2_dnut_i = cb2;
86-
su2double dTerm2_dnut_j = 0.0;
87+
if (implicit) {
88+
/*--- For Jacobians -> Use of TSL approx. to compute derivatives of the gradients ---*/
89+
Jacobian_i[0][0] = -diffusion_coefficient * proj_vector_ij / sigma;
90+
Jacobian_j[0][0] = diffusion_coefficient * proj_vector_ij / sigma;
8791

88-
su2double dDC_dnut_i = dTerm1_dnut_i - dTerm2_dnut_i;
89-
su2double dDC_dnut_j = dTerm1_dnut_j - dTerm2_dnut_j;
92+
if (use_accurate_jacobians) {
93+
/*--- The diffusion coefficient is also a function of nu_t. ---*/
94+
const su2double dTerm1_dnut_i = (1 + cb2) * 0.5;
95+
const su2double dTerm1_dnut_j = (1 + cb2) * 0.5;
9096

91-
su2double dGrad_dnut_i = -proj_vector_ij;
92-
su2double dGrad_dnut_j = proj_vector_ij;
97+
const su2double dTerm2_dnut_i = cb2;
98+
const su2double dTerm2_dnut_j = 0.0;
9399

94-
/*--- For Jacobians -> Use of TSL approx. to compute derivatives of the gradients ---*/
95-
su2double diffusion_coefficient = term_1 - term_2;
96-
bool UseExactJacobians = config->GetUse_Accurate_Turb_Jacobians();
100+
const su2double dDC_dnut_i = dTerm1_dnut_i - dTerm2_dnut_i;
101+
const su2double dDC_dnut_j = dTerm1_dnut_j - dTerm2_dnut_j;
97102

98-
if (implicit) {
99-
if (UseExactJacobians) {
100-
Jacobian_i[0][0] = (dDC_dnut_i*Proj_Mean_GradScalarVar[0] + dGrad_dnut_i*diffusion_coefficient)/sigma; //exact
101-
Jacobian_j[0][0] = (dDC_dnut_j*Proj_Mean_GradScalarVar[0] + dGrad_dnut_j*diffusion_coefficient)/sigma; //exact
102-
} else {
103-
Jacobian_i[0][0] = diffusion_coefficient*-proj_vector_ij/sigma; //frozen diffusion coefficients
104-
Jacobian_j[0][0] = diffusion_coefficient* proj_vector_ij/sigma; //frozen diffusion coefficients
105-
}
103+
Jacobian_i[0][0] += dDC_dnut_i * Proj_Mean_GradScalarVar[0] / sigma;
104+
Jacobian_j[0][0] += dDC_dnut_j * Proj_Mean_GradScalarVar[0] / sigma;
105+
}
106106
}
107107
}
108108

@@ -116,7 +116,8 @@ class CAvgGrad_TurbSA final : public CAvgGrad_Scalar<FlowIndices> {
116116
*/
117117
CAvgGrad_TurbSA(unsigned short val_nDim, unsigned short val_nVar,
118118
bool correct_grad, const CConfig* config)
119-
: CAvgGrad_Scalar<FlowIndices>(val_nDim, val_nVar, correct_grad, config) {}
119+
: CAvgGrad_Scalar<FlowIndices>(val_nDim, val_nVar, correct_grad, config),
120+
use_accurate_jacobians(config->GetUse_Accurate_Turb_Jacobians()) {}
120121
};
121122

122123
/*!
@@ -162,42 +163,40 @@ class CAvgGrad_TurbSA_Neg final : public CAvgGrad_Scalar<FlowIndices> {
162163
const su2double nu_i = Laminar_Viscosity_i/Density_i;
163164
const su2double nu_j = Laminar_Viscosity_j/Density_j;
164165

165-
const su2double nu_ij = 0.5*(nu_i+nu_j);
166+
const su2double nu_ij = 0.5 * (nu_i + nu_j);
166167
const su2double nu_tilde_i = ScalarVar_i[0];
167168
const su2double nu_tilde_j = ScalarVar_j[0];
168-
const su2double nu_tilde_ij = 0.5*(nu_tilde_i + nu_tilde_j);
169+
const su2double nu_tilde_ij = 0.5 * (nu_tilde_i + nu_tilde_j);
169170

170-
/* Following Diskin's implementation from 10.2514/1.J064629, they propose a new fn function
171-
to be evaluated at the cell to maintain positivity in the diffusion coefficient, which is
172-
used in both terms. The new fn term averaged across the face reverts to the original fn
173-
function. */
171+
/*--- Following Diskin's implementation from 10.2514/1.J064629, they propose a new fn function
172+
* to be evaluated at the cell to maintain positivity in the diffusion coefficient, which is
173+
* used in both terms. The new fn term averaged across the face reverts to the original fn
174+
* function. ---*/
174175

175176
/*--- Second Term (LHS) ---*/
176-
su2double zeta_i = ((1 + cb2)*nu_tilde_ij - cb2*nu_tilde_i)/nu_ij;
177-
su2double fn_i;
178-
if (zeta_i >= 0.0) {
179-
fn_i = 1.0;
180-
} else {
181-
fn_i = (cn1 + pow(zeta_i,3.0))/(cn1 - pow(zeta_i,3.0));
177+
const su2double zeta_i = ((1 + cb2) * nu_tilde_ij - cb2 * nu_tilde_i) / nu_ij;
178+
su2double fn_i = 1.0;
179+
if (zeta_i < 0.0) {
180+
fn_i = (cn1 + pow(zeta_i,3)) / (cn1 - pow(zeta_i,3));
182181
}
183182

184-
su2double term_1 = (nu_ij + (1 + cb2)*nu_tilde_ij*fn_i);
185-
su2double term_2 = cb2*nu_tilde_i*fn_i;
186-
Flux[0] = (term_1 - term_2)*Proj_Mean_GradScalarVar[0]/sigma;
183+
const su2double term_1 = (nu_ij + (1 + cb2) * nu_tilde_ij * fn_i);
184+
const su2double term_2 = cb2 * nu_tilde_i * fn_i;
185+
Flux[0] = (term_1 - term_2) * Proj_Mean_GradScalarVar[0] / sigma;
187186

188-
/*--- For Jacobians -> Use of TSL approx. to compute derivatives of the gradients
187+
/*--- For Jacobians -> Use of TSL approx. to compute derivatives of the gradients
189188
* Exact Jacobians were tested on multiple cases but resulted in divergence of all
190189
* simulations, hence only frozen diffusion coefficient (approximate) Jacobians are used. ---*/
191190

192-
su2double diffusion_coefficient = (term_1 - term_2);
191+
if (implicit) {
192+
const su2double diffusion_coefficient = (term_1 - term_2);
193193

194-
su2double dGrad_dnut_i = -proj_vector_ij;
195-
su2double dGrad_dnut_j = proj_vector_ij;
194+
const su2double dGrad_dnut_i = -proj_vector_ij;
195+
const su2double dGrad_dnut_j = proj_vector_ij;
196196

197-
if (implicit) {
198-
Jacobian_i[0][0] = diffusion_coefficient * dGrad_dnut_i/sigma; //frozen diffusion
199-
Jacobian_j[0][0] = diffusion_coefficient * dGrad_dnut_j/sigma; //frozen diffusion
200-
}
197+
Jacobian_i[0][0] = diffusion_coefficient * dGrad_dnut_i / sigma;
198+
Jacobian_j[0][0] = diffusion_coefficient * dGrad_dnut_j / sigma;
199+
}
201200
}
202201

203202
public:

SU2_CFD/include/numerics/turbulent/turb_sources.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class CSourceBase_TurbSA : public CNumerics {
8686
*/
8787
inline void ResidualAxisymmetricDiffusion(su2double sigma) {
8888
if (Coord_i[1] < EPS) return;
89-
89+
9090
const su2double yinv = 1.0 / Coord_i[1];
9191
const su2double& nue = ScalarVar_i[0];
9292

@@ -250,7 +250,7 @@ class CSourceBase_TurbSA : public CNumerics {
250250
Residual = (Production - Destruction) * Volume;
251251

252252
if (axisymmetric) ResidualAxisymmetricDiffusion(var.sigma);
253-
253+
254254
Jacobian_i[0] *= Volume;
255255
}
256256

@@ -424,6 +424,7 @@ struct Edw {
424424

425425
/*!
426426
* \brief SA source terms classes: production and destruction term and their derivative.
427+
* \note Quadratic diffusion is included in the viscous fluxes.
427428
* \ingroup SourceDiscr
428429
* \param[in] nue: SA variable.
429430
* \param[in] var: Common SA variables struct.
@@ -550,7 +551,7 @@ class CCompressibilityCorrection final : public ParentClass {
550551
const su2double v = V_i[idx.Velocity() + 1];
551552

552553
const su2double d_axiCorrection = 2.0 * c5 * nue * pow(v * yinv / sound_speed, 2) * Volume;
553-
const su2double axiCorrection = 0.5 * nue * d_axiCorrection;
554+
const su2double axiCorrection = 0.5 * nue * d_axiCorrection;
554555

555556
this->Residual -= axiCorrection;
556557
this->Jacobian_i[0] -= d_axiCorrection;

SU2_CFD/include/solvers/CScalarSolver.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class CScalarSolver : public CSolver {
7777

7878
/*--- Edge fluxes for reducer strategy (see the notes in CEulerSolver.hpp). ---*/
7979
CSysVector<su2double> EdgeFluxes; /*!< \brief Flux across each edge. */
80-
CSysVector<su2double> EdgeFluxes_Diff; /*!< \brief Flux difference across ij and ji for non-conservative discretisation. */
80+
CSysVector<su2double> EdgeFluxesDiff; /*!< \brief Flux difference between ij and ji for non-conservative discretisation. */
8181

8282
/*!
8383
* \brief The highest level in the variable hierarchy this solver can safely use.

SU2_CFD/include/solvers/CScalarSolver.inl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ void CScalarSolver<VariableType>::Upwind_Residual(CGeometry* geometry, CSolver**
341341

342342
template <class VariableType>
343343
void CScalarSolver<VariableType>::SumEdgeFluxes(CGeometry* geometry) {
344+
const bool nonConservative = EdgeFluxesDiff.GetLocSize() > 0;
345+
344346
SU2_OMP_FOR_STAT(omp_chunk_size)
345347
for (unsigned long iPoint = 0; iPoint < nPoint; ++iPoint) {
346348
LinSysRes.SetBlock_Zero(iPoint);
@@ -350,8 +352,8 @@ void CScalarSolver<VariableType>::SumEdgeFluxes(CGeometry* geometry) {
350352
LinSysRes.AddBlock(iPoint, EdgeFluxes.GetBlock(iEdge));
351353
else {
352354
LinSysRes.SubtractBlock(iPoint, EdgeFluxes.GetBlock(iEdge));
353-
if (EdgeFluxes_Diff.GetLocSize() > 0) {
354-
LinSysRes.SubtractBlock(iPoint, EdgeFluxes_Diff.GetBlock(iEdge));
355+
if (nonConservative) {
356+
LinSysRes.SubtractBlock(iPoint, EdgeFluxesDiff.GetBlock(iEdge));
355357
}
356358
}
357359
}

SU2_CFD/include/solvers/CSolver.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ class CSolver {
191191
CSysVector<su2double> LinSysSol; /*!< \brief vector to store iterative solution of implicit linear system. */
192192
CSysVector<su2double> LinSysRes; /*!< \brief vector to store iterative residual of implicit linear system. */
193193
#ifndef CODI_FORWARD_TYPE
194-
CSysMatrix<su2mixedfloat> Jacobian; /*!< \brief Complete sparse Jacobian structure for implicit computations. */
195-
CSysSolve<su2mixedfloat> System; /*!< \brief Linear solver/smoother. */
194+
using JacobianScalarType = su2mixedfloat;
196195
#else
197-
CSysMatrix<su2double> Jacobian;
198-
CSysSolve<su2double> System;
196+
using JacobianScalarType = su2double;
199197
#endif
198+
CSysMatrix<JacobianScalarType> Jacobian; /*!< \brief Complete sparse Jacobian structure for implicit computations. */
199+
CSysSolve<JacobianScalarType> System; /*!< \brief Linear solver/smoother. */
200200

201201
CSysVector<su2double> OutputVariables; /*!< \brief vector to store the extra variables to be written. */
202202
string* OutputHeadingNames; /*!< \brief vector of strings to store the headings for the exra variables */

0 commit comments

Comments
 (0)