Skip to content

Commit f738172

Browse files
committed
Various suggestions from code review.
- Adding a common CFeaSolverBase class for finite elements solvers. - making functions private and variables constant in CGradientSmoothingSolver. - reworked the storage of boundary vertices in CSobolevSmoothingVariable.
1 parent 36bc84c commit f738172

10 files changed

Lines changed: 125 additions & 127 deletions

File tree

Common/include/linear_algebra/CSysMatrix.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,42 @@ class CSysMatrix {
498498
AddBlock(block_i, block_j, val_block, OtherType(-1));
499499
}
500500

501+
/*!
502+
* \brief Set the value of a scaled block in the sparse matrix.
503+
* \note This is an templated overload for C2Dcontainer specialization su2matrix.
504+
* It assumes that MatrixType supports a member type Scalar and access operator[][].
505+
* If the template param Overwrite is false we add to the block (bij += alpha*b).
506+
* \param[in] block_i - Row index.
507+
* \param[in] block_j - Column index.
508+
* \param[in] val_block - Block to set to A(i, j).
509+
* \param[in] alpha - Scale factor.
510+
*/
511+
template <class MatrixType, bool Overwrite = true>
512+
inline void SetBlock(unsigned long block_i, unsigned long block_j, MatrixType& val_block,
513+
typename MatrixType::Scalar alpha = 1.0) {
514+
auto mat_ij = GetBlock(block_i, block_j);
515+
if (!mat_ij) return;
516+
for (auto iVar = 0ul; iVar < nVar; ++iVar) {
517+
for (auto jVar = 0ul; jVar < nEqn; ++jVar) {
518+
*mat_ij = (Overwrite ? ScalarType(0) : *mat_ij) + PassiveAssign(alpha * val_block[iVar][jVar]);
519+
++mat_ij;
520+
}
521+
}
522+
}
523+
524+
/*!
525+
* \brief Adds a scaled block to the sparse matrix (see SetBlock).
526+
* \param[in] block_i - Row index.
527+
* \param[in] block_j - Column index.
528+
* \param[in] val_block - Block to add to A(i, j).
529+
* \param[in] alpha - Scale factor.
530+
*/
531+
template <class MatrixType>
532+
inline void AddBlock(unsigned long block_i, unsigned long block_j, MatrixType& val_block,
533+
typename MatrixType::Scalar alpha = 1.0) {
534+
SetBlock<MatrixType, false>(block_i, block_j, val_block, alpha);
535+
}
536+
501537
/*!
502538
* \brief Update 4 blocks ii, ij, ji, jj (add to i* sub from j*).
503539
* \note This method assumes an FVM-type sparse pattern.

SU2_CFD/include/solvers/CFEASolver.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ class CFEASolver : public CFEASolverBase {
9999
bool topol_filter_applied; /*!< \brief True if density filtering has been performed. */
100100
bool initial_calc = true; /*!< \brief Becomes false after first call to Preprocessing. */
101101

102+
/*!
103+
* \brief The highest level in the variable hierarchy this solver can safely use,
104+
* CVariable is the common denominator between the FEA and Mesh deformation variables.
105+
*/
106+
CVariable* nodes = nullptr;
107+
108+
/*!
109+
* \brief Return nodes to allow CSolver::base_nodes to be set.
110+
*/
111+
inline CVariable* GetBaseClassPointerToNodes() override { return nodes; }
112+
102113
/*!
103114
* \brief Get the element container index and number of nodes of a given VTK type.
104115
* \param[in] VTK_Type - Type of element.

SU2_CFD/include/solvers/CFEASolverBase.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,6 @@ class CFEASolverBase : public CSolver {
4747

4848
vector<unsigned long> ExtraVerticesToEliminate; /*!< Extra vertices for row/column elimination, see CommunicateExtraEliminationVertices. */
4949

50-
/*!
51-
* \brief The highest level in the variable hierarchy all derived solvers can safely use,
52-
* CVariable is the common denominator between the FEA, Mesh deformation, and smoothing variables.
53-
*/
54-
CVariable* nodes = nullptr;
55-
56-
/*!
57-
* \brief Return nodes to allow CSolver::base_nodes to be set.
58-
*/
59-
inline CVariable* GetBaseClassPointerToNodes() override { return nodes; }
60-
6150
public:
6251
/*!
6352
* \brief Constructor of the class.

SU2_CFD/include/solvers/CGradientSmoothingSolver.hpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929

3030
#include "../../../Common/include/linear_algebra/CMatrixVectorProduct.hpp"
3131
#include "../../../Common/include/toolboxes/CSquareMatrixCM.hpp"
32-
#include "../../../SU2_CFD/include/solvers/CFEASolverBase.hpp"
32+
#include "../variables/CSobolevSmoothingVariable.hpp"
33+
#include "CFEASolverBase.hpp"
3334

3435
/*! \class CGradientSmoothingSolver
3536
* \brief Main class for defining a gradient smoothing.
@@ -65,6 +66,17 @@ class CGradientSmoothingSolver final : public CFEASolverBase {
6566

6667
CSquareMatrixCM hessian; /*!< \brief The approximated Hessian with respect to the design variables. */
6768

69+
/*!
70+
* \brief The highest level in the variable hierarchy all derived solvers can safely use,
71+
* CVariable is the common denominator between the FEA and Mesh deformationd variables.
72+
*/
73+
CSobolevSmoothingVariable* nodes = nullptr;
74+
75+
/*!
76+
* \brief Return nodes to allow CSolver::base_nodes to be set.
77+
*/
78+
inline CVariable* GetBaseClassPointerToNodes() override { return nodes; }
79+
6880
public:
6981
/*!
7082
* \brief Constructor of the class.
@@ -129,12 +141,12 @@ class CGradientSmoothingSolver final : public CFEASolverBase {
129141
/*!
130142
* \brief Set Dirichlet boundary conditions
131143
*/
132-
void BC_Dirichlet(CGeometry* geometry, const CConfig* config, unsigned int val_marker);
144+
void BC_Dirichlet(const CGeometry* geometry, const CConfig* config, unsigned int val_marker);
133145

134146
/*!
135147
* \brief Set Dirichlet boundary conditions for the surface solver
136148
*/
137-
void BC_Surface_Dirichlet(CGeometry* geometry, const CConfig* config, unsigned int val_marker);
149+
void BC_Surface_Dirichlet(const CGeometry* geometry, const CConfig* config, unsigned int val_marker);
138150

139151
/*!
140152
* \brief Call the linear systems solver
@@ -174,7 +186,7 @@ class CGradientSmoothingSolver final : public CFEASolverBase {
174186
/*!
175187
* \brief write the DV gradient into a file
176188
*/
177-
void OutputDVGradient(CConfig *config, string out_file="delta_p.txt");
189+
void OutputDVGradient(const CConfig* config, string out_file = "delta_p.txt");
178190

179191
/*!
180192
* \brief Record a tape containing the parameter Jacobian.
@@ -211,18 +223,6 @@ class CGradientSmoothingSolver final : public CFEASolverBase {
211223
CSysVector<su2double> &registeredCoord,
212224
CConfig *config);
213225

214-
/*!
215-
* \brief Extract and set the geometrical sensitivity.
216-
* \param[in] geometry - Geometrical definition of the problem.
217-
* \param[in] config - Definition of the particular problem.
218-
*/
219-
void SetSensitivity(CGeometry *geometry, CConfig *config, CSolver*) override;
220-
221-
/*!
222-
* \brief Write the solution of the linear solver into the sensitivities of the nodes
223-
*/
224-
void WriteSensitivity(CGeometry* geometry, const CConfig* config, unsigned long val_marker = 0);
225-
226226
/*!
227227
* \brief Write the content of sensitivity in the nodes to the sensitivity in the geometry
228228
* \param[in] geometry - Geometrical definition of the problem.
@@ -235,6 +235,12 @@ class CGradientSmoothingSolver final : public CFEASolverBase {
235235
*/
236236
void ReadSensFromGeometry(const CGeometry *geometry) override;
237237

238+
private:
239+
/*!
240+
* \brief Write the solution of the linear solver into the sensitivities of the nodes
241+
*/
242+
void WriteSensitivity(CGeometry* geometry, const CConfig* config, unsigned long val_marker = 0);
243+
238244
/*!
239245
* \brief Copy sensitivities from a vector into the geometry
240246
*/
@@ -257,13 +263,6 @@ class CGradientSmoothingSolver final : public CFEASolverBase {
257263
return geometry->nodes->GetCoord(indexNode, iDim);
258264
}
259265

260-
/*!
261-
* \brief Extract the Coordinates of the element from geometry
262-
*/
263-
su2activematrix GetElementCoordinates(CGeometry *geometry,
264-
std::vector<unsigned long>& indexNode,
265-
int EL_KIND = 0);
266-
267266
/*!
268267
* \brief Extra entries to eliminate in the linear system
269268
*/
@@ -289,12 +288,17 @@ class CGradientSmoothingSolver final : public CFEASolverBase {
289288
}
290289
}
291290

292-
private:
293-
291+
/*!
292+
* \brief Set the current working dimension, if the seperate dimension option is set.
293+
* \param[in] iDim - the dimension we are currently working in.
294+
*/
294295
inline void SetCurrentDim(unsigned int iDim) {
295296
curDim=iDim;
296297
}
297298

299+
/*!
300+
* \brief Return the current working dimension.
301+
*/
298302
inline const unsigned int& GetCurrentDim() const {
299303
return curDim;
300304
}

SU2_CFD/include/variables/CSobolevSmoothingVariable.hpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727

2828
#pragma once
2929

30-
#include "../../../SU2_CFD/include/variables/CVariable.hpp"
30+
#include "../../../Common/include/containers/CVertexMap.hpp"
31+
#include "CVariable.hpp"
3132

3233
class CSobolevSmoothingVariable final : public CVariable {
3334
public:
3435
MatrixType Sensitivity; /*!< Vector holding the derivative of target functional with respect to the coordinates at this node */
3536

36-
bool* boundary_vertex; /*!< \brief Stores if a point belongs to the boundary of a boundary. */
37+
CVertexMap<unsigned> BoundaryVertexMap; /*!< \brief Stores if a point belongs to the boundary of a boundary. */
3738

3839
/*!
3940
* \brief Constructor of the class.
@@ -46,7 +47,7 @@ class CSobolevSmoothingVariable final : public CVariable {
4647
/*!
4748
* \brief Destructor of the class.
4849
*/
49-
~CSobolevSmoothingVariable();
50+
~CSobolevSmoothingVariable() override = default;
5051

5152
/*!
5253
* \brief Set the sensitivity at the node
@@ -65,10 +66,15 @@ class CSobolevSmoothingVariable final : public CVariable {
6566
/*!
6667
* \brief Mark a point as boundary of a boundary
6768
*/
68-
void MarkAsBoundaryPoint(unsigned long iPoint) override;
69+
void MarkAsBoundaryPoint(unsigned long iPoint);
6970

7071
/*!
7172
* \brief return wether a point is a boundary of a boundary
7273
*/
73-
bool GetIsBoundaryPoint(unsigned long iPoint) const override;
74+
bool GetIsBoundaryPoint(unsigned long iPoint) const;
75+
76+
/*!
77+
* \brief Allocate member variables for points marked as vertex (via "MarkAsBoundaryPoint").
78+
*/
79+
void AllocateBoundaryVariables();
7480
};

SU2_CFD/include/variables/CVariable.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,14 +2242,4 @@ class CVariable {
22422242
virtual su2double GetSourceTerm_DispAdjoint(unsigned long iPoint, unsigned long iDim) const { return 0.0; }
22432243
virtual su2double GetSourceTerm_VelAdjoint(unsigned long iPoint, unsigned long iDim) const { return 0.0; }
22442244

2245-
/*!
2246-
* \brief Mark a point as boundary of a design surface boundary
2247-
*/
2248-
virtual void MarkAsBoundaryPoint(unsigned long iPoint) {}
2249-
2250-
/*!
2251-
* \brief return wether a point is a boundary of a design surface boundary
2252-
*/
2253-
virtual bool GetIsBoundaryPoint(unsigned long iPoint) const { return false; }
2254-
22552245
};

SU2_CFD/src/solvers/CFEASolver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ CFEASolver::~CFEASolver(void) {
229229
for (unsigned long iPoint = 0; iPoint < nPoint; iPoint++)
230230
omp_destroy_lock(&UpdateLocks[iPoint]);
231231
}
232+
233+
delete nodes;
232234
}
233235

234236
void CFEASolver::HybridParallelInitialization(CGeometry* geometry) {

SU2_CFD/src/solvers/CFEASolverBase.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ CFEASolverBase::~CFEASolverBase() {
6969
delete [] element_container;
7070
}
7171

72-
delete nodes;
7372
}
7473

7574
void CFEASolverBase::CommunicateExtraEliminationVertices(const CGeometry* geometry, vector<unsigned long>& myPoints) {

0 commit comments

Comments
 (0)