Skip to content

Commit 05e8574

Browse files
authored
Merge pull request #1159 from su2code/reduce_disc_adj_mem
Reduce discrete adjoint memory usage ~25%
2 parents 575c157 + 4c3dbd4 commit 05e8574

42 files changed

Lines changed: 710 additions & 1017 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Common/include/geometry/CGeometry.hpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -492,17 +492,31 @@ class CGeometry {
492492
* \brief Get the edge index from using the nodes of the edge.
493493
* \param[in] first_point - First point of the edge.
494494
* \param[in] second_point - Second point of the edge.
495+
* \param[in] error - Throw error if edge does not exist.
495496
* \return Index of the edge.
496497
*/
497-
long FindEdge(unsigned long first_point, unsigned long second_point) const;
498+
inline long FindEdge(unsigned long first_point, unsigned long second_point, bool error = true) const {
499+
for (unsigned short iNode = 0; iNode < nodes->GetnPoint(first_point); iNode++) {
500+
auto iPoint = nodes->GetPoint(first_point, iNode);
501+
if (iPoint == second_point) return nodes->GetEdge(first_point, iNode);
502+
}
503+
if (error) {
504+
char buf[100];
505+
SPRINTF(buf, "Can't find the edge that connects %lu and %lu.", first_point, second_point);
506+
SU2_MPI::Error(buf, CURRENT_FUNCTION);
507+
}
508+
return -1;
509+
}
498510

499511
/*!
500512
* \brief Get the edge index from using the nodes of the edge.
501513
* \param[in] first_point - First point of the edge.
502514
* \param[in] second_point - Second point of the edge.
503515
* \return Index of the edge.
504516
*/
505-
bool CheckEdge(unsigned long first_point, unsigned long second_point) const;
517+
inline bool CheckEdge(unsigned long first_point, unsigned long second_point) const {
518+
return FindEdge(first_point, second_point, false) >= 0;
519+
}
506520

507521
/*!
508522
* \brief Get the distance between a plane (defined by three point) and a point.
@@ -684,11 +698,6 @@ class CGeometry {
684698
*/
685699
inline virtual void GatherInOutAverageValues(CConfig *config, bool allocate) {}
686700

687-
/*!
688-
* \brief Sets CG coordinates.
689-
*/
690-
inline virtual void SetCoord_CG(void) {}
691-
692701
/*!
693702
* \brief Set max length.
694703
* \param[in] config - Definition of the particular problem.
@@ -705,9 +714,8 @@ class CGeometry {
705714
/*!
706715
* \brief A virtual member.
707716
* \param[in] config - Definition of the particular problem.
708-
* \param[in] action - Allocate or not the new elements.
709717
*/
710-
inline virtual void VisualizeControlVolume(CConfig *config, unsigned short action) {}
718+
inline virtual void VisualizeControlVolume(const CConfig *config) const {}
711719

712720
/*!
713721
* \brief A virtual member.
@@ -732,7 +740,7 @@ class CGeometry {
732740
* \param[in] config - Definition of the particular problem.
733741
* \param[in] action - Allocate or not the new elements.
734742
*/
735-
inline virtual void SetBoundControlVolume(CConfig *config, unsigned short action) {}
743+
inline virtual void SetBoundControlVolume(const CConfig *config, unsigned short action) {}
736744

737745
/*!
738746
* \brief A virtual member.
@@ -1589,10 +1597,9 @@ class CGeometry {
15891597
const su2double *cg_elem, vector<long> &neighbours, vector<bool> &is_neighbor) const;
15901598

15911599
/*!
1592-
* \brief Compute and store the volume of the elements.
1593-
* \param[in] config - Problem configuration.
1600+
* \brief Compute and store the volume of the primal elements.
15941601
*/
1595-
void SetElemVolume(CConfig *config);
1602+
void SetElemVolume();
15961603

15971604
/*!
15981605
* \brief Set the multigrid index for the current geometry object.
@@ -1610,7 +1617,7 @@ class CGeometry {
16101617
* \brief A virtual member.
16111618
* \param config - Config
16121619
*/
1613-
inline virtual void ComputeMeshQualityStatistics(CConfig *config) {}
1620+
inline virtual void ComputeMeshQualityStatistics(const CConfig *config) {}
16141621

16151622
/*!
16161623
* \brief Get the sparse pattern of "type" with given level of fill.

Common/include/geometry/CPhysicalGeometry.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,6 @@ class CPhysicalGeometry final : public CGeometry {
444444
*/
445445
void GatherInOutAverageValues(CConfig *config, bool allocate) override;
446446

447-
/*!
448-
* \brief Set the center of gravity of the face, elements and edges.
449-
*/
450-
void SetCoord_CG(void) override;
451-
452447
/*!
453448
* \brief Set the edge structure of the control volume.
454449
* \param[in] config - Definition of the particular problem.
@@ -459,9 +454,8 @@ class CPhysicalGeometry final : public CGeometry {
459454
/*!
460455
* \brief Visualize the structure of the control volume(s).
461456
* \param[in] config - Definition of the particular problem.
462-
* \param[in] action - Allocate or not the new elements.
463457
*/
464-
void VisualizeControlVolume(CConfig *config, unsigned short action) override;
458+
void VisualizeControlVolume(const CConfig *config) const override;
465459

466460
/*!
467461
* \brief Mach the near field boundary condition.
@@ -487,7 +481,7 @@ class CPhysicalGeometry final : public CGeometry {
487481
* \param[in] config - Definition of the particular problem.
488482
* \param[in] action - Allocate or not the new elements.
489483
*/
490-
void SetBoundControlVolume(CConfig *config, unsigned short action) override;
484+
void SetBoundControlVolume(const CConfig *config, unsigned short action) override;
491485

492486
/*!
493487
* \brief Set the maximum cell-center to cell-center distance for CVs.
@@ -598,7 +592,7 @@ class CPhysicalGeometry final : public CGeometry {
598592
* \brief Compute 3 grid quality metrics: orthogonality angle, dual cell aspect ratio, and dual cell volume ratio.
599593
* \param[in] config - Definition of the particular problem.
600594
*/
601-
void ComputeMeshQualityStatistics(CConfig *config) override;
595+
void ComputeMeshQualityStatistics(const CConfig *config) override;
602596

603597
/*!
604598
* \brief Find and store the closest neighbor to a vertex.

Common/include/geometry/dual_grid/CDualGrid.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,16 @@ class CDualGrid{
7474
* \param[in] val_coord_Edge_CG - Coordinates of the centre of gravity of the edge.
7575
* \param[in] val_coord_FaceElem_CG - Coordinates of the centre of gravity of the face of an element.
7676
* \param[in] val_coord_Elem_CG - Coordinates of the centre of gravity of the element.
77-
* \param[in] config - Definition of the particular problem.
7877
*/
79-
virtual void SetNodes_Coord(su2double *val_coord_Edge_CG, su2double *val_coord_FaceElem_CG, su2double *val_coord_Elem_CG) = 0;
78+
virtual void SetNodes_Coord(const su2double *val_coord_Edge_CG, const su2double *val_coord_FaceElem_CG,
79+
const su2double *val_coord_Elem_CG) = 0;
8080

8181
/*!
8282
* \overload
8383
* \param[in] val_coord_Edge_CG - Coordinates of the centre of gravity of the edge.
8484
* \param[in] val_coord_Elem_CG - Coordinates of the centre of gravity of the element.
85-
* \param[in] config - Definition of the particular problem.
8685
*/
87-
virtual void SetNodes_Coord(su2double *val_coord_Edge_CG, su2double *val_coord_Elem_CG) = 0;
86+
virtual void SetNodes_Coord(const su2double *val_coord_Edge_CG, const su2double *val_coord_Elem_CG) = 0;
8887

8988
/*!
9089
* \brief A pure virtual member.

Common/include/geometry/dual_grid/CEdge.hpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "../../containers/C2DContainer.hpp"
3131

32+
class CPhysicalGeometry;
33+
3234
/*!
3335
* \class CEdge
3436
* \brief Class for defining the edges of the dual grid.
@@ -41,7 +43,8 @@ class CEdge {
4143
using NodeArray = C2DContainer<Index, Index, StorageType::ColumnMajor, 64, DynamicSize, 2>;
4244
NodeArray Nodes; /*!< \brief Vector to store the node indices of the edge. */
4345
su2activematrix Normal; /*!< \brief Normal (area) of the edge. */
44-
su2activematrix Coord_CG; /*!< \brief Center-of-gravity (mid point) of the edge. */
46+
47+
friend class CPhysicalGeometry;
4548

4649
public:
4750
enum NodePosition : unsigned long {LEFT = 0, RIGHT = 1};
@@ -58,25 +61,6 @@ class CEdge {
5861
*/
5962
CEdge() = delete;
6063

61-
/*!
62-
* \brief Set the center of gravity of the edge.
63-
* \param[in] iEdge - Edge index.
64-
* \param[in] nodeCoord - Coordinates of the two nodes.
65-
*/
66-
template<class T>
67-
void SetCoord_CG(unsigned long iEdge, const T& nodeCoord) {
68-
for (auto iDim = 0u; iDim < Coord_CG.cols(); ++iDim)
69-
Coord_CG(iEdge,iDim) = 0.5 * (nodeCoord[0][iDim] + nodeCoord[1][iDim]);
70-
}
71-
72-
/*!
73-
* \brief Obtain the center of gravity of the edge.
74-
* \param[in] iEdge - Edge index.
75-
* \param[in] iDim - Dimension.
76-
* \return Coordinate of the centre of gravity.
77-
*/
78-
inline su2double GetCG(unsigned long iEdge, unsigned long iDim) const { return Coord_CG(iEdge,iDim); }
79-
8064
/*!
8165
* \brief Get left/right node index defining the edge.
8266
* \param[in] iEdge - Edge index.

Common/include/geometry/dual_grid/CPoint.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
using namespace std;
3737

3838
class CConfig;
39+
class CPhysicalGeometry;
3940

4041
/*!
4142
* \class CPoint
@@ -44,6 +45,8 @@ class CConfig;
4445
*/
4546
class CPoint {
4647
private:
48+
friend class CPhysicalGeometry;
49+
4750
const unsigned long nDim = 0;
4851

4952
su2vector<unsigned long> GlobalIndex; /*!< \brief Global index in the parallel simulation. */

Common/include/geometry/dual_grid/CVertex.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ class CVertex : public CDualGrid {
7575
* \param[in] val_coord_Elem_CG - Coordinates of the centre of gravity of the element.
7676
* \return Compute the normal (dimensional) to the face that makes the vertex.
7777
*/
78-
void SetNodes_Coord(su2double *val_coord_Edge_CG, su2double *val_coord_FaceElem_CG, su2double *val_coord_Elem_CG) override;
78+
void SetNodes_Coord(const su2double *val_coord_Edge_CG, const su2double *val_coord_FaceElem_CG,
79+
const su2double *val_coord_Elem_CG) override;
7980

8081
/*!
8182
* \overload
8283
* \param[in] val_coord_Edge_CG - Coordinates of the centre of gravity of the edge.
8384
* \param[in] val_coord_Elem_CG - Coordinates of the centre of gravity of the element.
8485
* \return Compute the normal (dimensional) to the face that makes the vertex.
8586
*/
86-
void SetNodes_Coord(su2double *val_coord_Edge_CG, su2double *val_coord_Elem_CG) override;
87+
void SetNodes_Coord(const su2double *val_coord_Edge_CG, const su2double *val_coord_Elem_CG) override;
8788

8889
/*!
8990
* \brief Copy the the normal vector of a face.

Common/include/geometry/primal_grid/CPrimalGrid.hpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ class CPrimalGrid {
4949
long *Neighbor_Elements; /*!< \brief Vector to store the elements surronding an element. */
5050
short *PeriodIndexNeighbors; /*!< \brief Vector to store the periodic index of a neighbor.
5151
A -1 indicates no periodic transformation to the neighbor. */
52-
su2double *Coord_CG; /*!< \brief Coordinates of the center-of-gravity of the element. */
53-
su2double **Coord_FaceElems_CG; /*!< \brief Coordinates of the center-of-gravity of the face of the
54-
elements. */
52+
su2double Coord_CG[3] = {0.0}; /*!< \brief Coordinates of the center-of-gravity of the element. */
5553
static unsigned short nDim; /*!< \brief Dimension of the element (2D or 3D) useful for triangles,
5654
quadrilateral and edges. */
5755
unsigned long DomainElement; /*!< \brief Only for boundaries, in this variable the 3D elements which
@@ -170,14 +168,23 @@ class CPrimalGrid {
170168
* \brief Set the center of gravity of an element (including edges).
171169
* \param[in] val_coord - Coordinates of the element.
172170
*/
173-
void SetCoord_CG(const su2double* const* val_coord);
171+
template<class T>
172+
inline su2double* SetCoord_CG(const T& val_coord) {
173+
for (unsigned short iDim = 0; iDim < nDim; iDim++) {
174+
Coord_CG[iDim] = 0.0;
175+
for (unsigned short iNode = 0; iNode < GetnNodes(); iNode++)
176+
Coord_CG[iDim] += val_coord[iNode][iDim]/su2double(GetnNodes());
177+
}
178+
return Coord_CG;
179+
}
174180

175181
/*!
176182
* \brief Get the center of gravity of an element (including edges).
177183
* \param[in] val_dim - Coordinate of the center of gravity.
178184
* \return Coordinates of the center of gravity.
179185
*/
180-
inline su2double GetCG(unsigned short val_dim) { return Coord_CG[val_dim]; }
186+
inline su2double GetCG(unsigned short val_dim) const { return Coord_CG[val_dim]; }
187+
inline const su2double* GetCG() const { return Coord_CG; }
181188

182189
/*!
183190
* \brief Set the center of gravity of an element (including edges).
@@ -192,14 +199,6 @@ class CPrimalGrid {
192199
*/
193200
inline su2double GetVolume(void) const { return Volume; }
194201

195-
/*!
196-
* \brief Get the CG of a face of an element.
197-
* \param[in] val_face - Local index of the face.
198-
* \param[in] val_dim - Coordinate of the center of gravity.
199-
* \return Coordinates of the center of gravity.
200-
*/
201-
inline su2double GetFaceCG(unsigned short val_face, unsigned short val_dim) { return Coord_FaceElems_CG[val_face][val_dim]; }
202-
203202
/*!
204203
* \brief Get all the neighbors of an element.
205204
* \return List of all the neighbor of an element.
@@ -431,7 +430,7 @@ class CPrimalGrid {
431430
* \brief Virtual function to make available the number of donor elements for the wall function treatment.
432431
* \return The number of donor elements.
433432
*/
434-
inline virtual unsigned short GetNDonorsWallFunctions(void) {return 0;}
433+
inline virtual unsigned short GetNDonorsWallFunctions(void) {return 0;}
435434

436435
/*!
437436
* \brief Virtual function to make available the pointer to the vector for the donor elements

Common/include/linear_algebra/vector_expressions.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,16 @@ FORCEINLINE auto FUN(decay_t<S> u, const CVecExpr<V,S>& v) \
157157
RETURNS( EXPR<Bcast<S>,V,S>(Bcast<S>(u), v.derived()) \
158158
) \
159159

160-
/*--- std::max/min have issues (maybe because they return by reference). ---*/
160+
/*--- std::max/min have issues (maybe because they return by reference).
161+
* For AD codi::max/min need to be used to avoid issues in debug builds. ---*/
161162

163+
#if defined(CODI_REVERSE_TYPE) || defined(CODI_FORWARD_TYPE)
164+
#define max_impl math::max
165+
#define min_impl math::min
166+
#else
162167
#define max_impl(a,b) a<b? Scalar(b) : Scalar(a)
163168
#define min_impl(a,b) b<a? Scalar(b) : Scalar(a)
169+
#endif
164170
MAKE_BINARY_FUN(max, max_, max_impl)
165171
MAKE_BINARY_FUN(min, min_, min_impl)
166172
MAKE_BINARY_FUN(pow, pow_, math::pow)

Common/src/geometry/CGeometry.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,26 +1389,6 @@ su2double CGeometry::Point2Plane_Distance(const su2double *Coord, const su2doubl
13891389

13901390
}
13911391

1392-
long CGeometry::FindEdge(unsigned long first_point, unsigned long second_point) const {
1393-
1394-
for (unsigned short iNode = 0; iNode < nodes->GetnPoint(first_point); iNode++) {
1395-
auto iPoint = nodes->GetPoint(first_point, iNode);
1396-
if (iPoint == second_point) return nodes->GetEdge(first_point, iNode);
1397-
}
1398-
1399-
char buf[100];
1400-
SPRINTF(buf, "Can't find the edge that connects %lu and %lu.", first_point, second_point);
1401-
SU2_MPI::Error(buf, CURRENT_FUNCTION);
1402-
return 0;
1403-
}
1404-
1405-
bool CGeometry::CheckEdge(unsigned long first_point, unsigned long second_point) const {
1406-
1407-
for (auto iPoint : nodes->GetPoints(first_point))
1408-
if (iPoint == second_point) return true;
1409-
return false;
1410-
}
1411-
14121392
void CGeometry::SetEdges(void) {
14131393

14141394
nEdge = 0;
@@ -2545,7 +2525,6 @@ void CGeometry::UpdateGeometry(CGeometry **geometry_container, CConfig *config)
25452525
geometry_container[MESH_0]->CompleteComms(geometry_container[MESH_0], config, GRID_VELOCITY);
25462526
}
25472527

2548-
geometry_container[MESH_0]->SetCoord_CG();
25492528
geometry_container[MESH_0]->SetControlVolume(config, UPDATE);
25502529
geometry_container[MESH_0]->SetBoundControlVolume(config, UPDATE);
25512530
geometry_container[MESH_0]->SetMaxLength(config);
@@ -3499,7 +3478,7 @@ bool CGeometry::GetRadialNeighbourhood(const unsigned long iElem_global,
34993478
return finished;
35003479
}
35013480

3502-
void CGeometry::SetElemVolume(CConfig *config)
3481+
void CGeometry::SetElemVolume()
35033482
{
35043483
SU2_OMP_PARALLEL
35053484
{

0 commit comments

Comments
 (0)