Skip to content

Commit fa22c88

Browse files
bigfootedgithub-advanced-security[bot]pcarruscag
authored
pywrapper - custom source terms for all solvers (#2388)
* working version for buoyant flow * Turbulent combustion (TFC) * 3D matrix view --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: Pedro Gomes <[email protected]> Co-authored-by: Pedro Gomes <[email protected]>
1 parent 5864796 commit fa22c88

File tree

30 files changed

+902
-55
lines changed

30 files changed

+902
-55
lines changed

.github/workflows/regression.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ jobs:
211211
uses: docker://ghcr.io/su2code/su2/test-su2:240320-1536
212212
with:
213213
# -t <Tutorials-branch> -c <Testcases-branch>
214-
args: -b ${{github.ref}} -t develop -c develop -s ${{matrix.testscript}}
214+
args: -b ${{github.ref}} -t develop -c feature_custom_source -s ${{matrix.testscript}}
215215
- name: Cleanup
216216
uses: docker://ghcr.io/su2code/su2/test-su2:240320-1536
217217
with:

Common/include/CConfig.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ class CConfig {
707707
Wrt_Restart_Overwrite, /*!< \brief Overwrite restart files or append iteration number.*/
708708
Wrt_Surface_Overwrite, /*!< \brief Overwrite surface output files or append iteration number.*/
709709
Wrt_Volume_Overwrite, /*!< \brief Overwrite volume output files or append iteration number.*/
710+
PyCustomSource, /*!< \brief Use a user-defined custom source term .*/
710711
Restart_Flow; /*!< \brief Restart flow solution for adjoint and linearized problems. */
711712
unsigned short nMarker_Monitoring, /*!< \brief Number of markers to monitor. */
712713
nMarker_Designing, /*!< \brief Number of markers for the objective function. */
@@ -3087,6 +3088,12 @@ class CConfig {
30873088
*/
30883089
unsigned short GetnMarker_PyCustom(void) const { return nMarker_PyCustom; }
30893090

3091+
/*!
3092+
* \brief Get the Python custom source term activation.
3093+
* \return Custom source term is active or not.
3094+
*/
3095+
bool GetPyCustomSource(void) const { return PyCustomSource; }
3096+
30903097
/*!
30913098
* \brief Get the total number of moving markers.
30923099
* \return Total number of moving markers.

Common/include/containers/CPyWrapperMatrixView.hpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
/*! \brief Gets the value for a (row, column) pair. */ \
5656
passivedouble operator()(unsigned long row, unsigned long col) const { return Get(row, col); } \
5757
\
58+
/*! \brief Gets the values for a row of the matrix. */ \
59+
std::vector<passivedouble> operator()(unsigned long row) const { return Get(row); } \
60+
\
5861
/*! \brief Gets the value for a (row, column) pair. */ \
5962
passivedouble Get(unsigned long row, unsigned long col) const { return SU2_TYPE::GetValue(Access(row, col)); } \
6063
\
@@ -163,3 +166,77 @@ class CPyWrapperMarkerMatrixView {
163166
/*--- Use the macro to generate the interface. ---*/
164167
PY_WRAPPER_MATRIX_INTERFACE
165168
};
169+
170+
/*!
171+
* \class CPyWrapper3DMatrixView
172+
* \ingroup PySU2
173+
* \brief This class wraps C3DDoubleMatrix for the python wrapper matrix interface.
174+
* It is generaly used to wrap access to solver gradients defined for the entire volume.
175+
*/
176+
class CPyWrapper3DMatrixView {
177+
protected:
178+
static_assert(su2activematrix::IsRowMajor, "");
179+
su2double* data_ = nullptr;
180+
unsigned long rows_ = 0, cols_ = 0, dims_ = 0;
181+
std::string name_;
182+
bool read_only_ = false;
183+
184+
/*--- Define the functions required by the interface macro. ---*/
185+
inline const su2double& Access(unsigned long row, unsigned long col, unsigned long dim) const {
186+
if (row > rows_ || col > cols_ || dim > dims_) SU2_MPI::Error(name_ + " out of bounds", "CPyWrapper3DMatrixView");
187+
return data_[row * (cols_ * dims_) + col * dims_ + dim];
188+
}
189+
inline su2double& Access(unsigned long row, unsigned long col, unsigned long dim) {
190+
if (read_only_) SU2_MPI::Error(name_ + " is read-only", "CPyWrapper3DMatrixView");
191+
const auto& const_me = *this;
192+
return const_cast<su2double&>(const_me.Access(row, col, dim));
193+
}
194+
195+
public:
196+
CPyWrapper3DMatrixView() = default;
197+
198+
/*!
199+
* \brief Construct the view of the matrix.
200+
* \note "name" should be set to the variable name being returned to give better information to users.
201+
* \note "read_only" can be set to true to prevent the data from being modified.
202+
*/
203+
CPyWrapper3DMatrixView(C3DDoubleMatrix& mat, const std::string& name, bool read_only)
204+
: data_(mat.data()),
205+
rows_(mat.length()),
206+
cols_(mat.rows()),
207+
dims_(mat.cols()),
208+
name_(name),
209+
read_only_(read_only) {}
210+
211+
/*! \brief Returns the shape of the matrix. */
212+
std::vector<unsigned long> Shape() const { return {rows_, cols_, dims_}; }
213+
214+
/*! \brief Returns whether the data is read-only [true] or if it can be modified [false]. */
215+
bool IsReadOnly() const { return read_only_; }
216+
217+
/*! \brief Gets the value for a (row, column, dimension) triplet. */
218+
passivedouble operator()(unsigned long row, unsigned long col, unsigned long dim) const { return Get(row, col, dim); }
219+
220+
/*! \brief Gets the values for a row and column of the matrix. */
221+
std::vector<passivedouble> operator()(unsigned long row, unsigned long col) const { return Get(row, col); }
222+
223+
/*! \brief Gets the value for a (row, column, dimension) triplet. */
224+
passivedouble Get(unsigned long row, unsigned long col, unsigned long dim) const {
225+
return SU2_TYPE::GetValue(Access(row, col, dim));
226+
}
227+
228+
/*! \brief Gets the values for a row and column of the matrix. */
229+
std::vector<passivedouble> Get(unsigned long row, unsigned long col) const {
230+
std::vector<passivedouble> vals(dims_);
231+
for (unsigned long j = 0; j < dims_; ++j) vals[j] = Get(row, col, j);
232+
return vals;
233+
}
234+
/*! \brief Sets the value for a (row, column, dimension) triplet. This clears derivative information. */
235+
void Set(unsigned long row, unsigned long col, unsigned long dim, passivedouble val) { Access(row, col, dim) = val; }
236+
237+
/*! \brief Sets the values for a row and column of the matrix. */
238+
void Set(unsigned long row, unsigned long col, std::vector<passivedouble> vals) {
239+
unsigned long j = 0;
240+
for (const auto& val : vals) Set(row, col, j++, val);
241+
}
242+
};

Common/include/containers/container_decorators.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ class C3DContainerDecorator {
165165
FORCEINLINE StaticContainer get(Int i, Index j = 0) const noexcept {
166166
return m_storage.template get<StaticContainer>(i, j * m_innerSz);
167167
}
168+
169+
/*!
170+
* \brief Raw data access, for Python wrapper.
171+
*/
172+
FORCEINLINE Scalar* data() { return m_storage.data(); }
168173
};
169174

170175
/*!

Common/include/geometry/dual_grid/CPoint.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ class CPoint {
8282
su2activematrix Coord; /*!< \brief vector with the coordinates of the node. */
8383
su2activematrix
8484
Coord_Old; /*!< \brief Old coordinates vector for primal solution reloading for Disc.Adj. with dynamic grid. */
85-
su2activematrix Coord_Sum; /*!< \brief Sum of coordinates vector for geometry smoothing. */
86-
su2activematrix Coord_n; /*!< \brief Coordinates at time n for use with dynamic meshes. */
87-
su2activematrix Coord_n1; /*!< \brief Coordinates at time n-1 for use with dynamic meshes. */
88-
su2activematrix Coord_p1; /*!< \brief Coordinates at time n+1 for use with dynamic meshes. */
85+
su2activematrix Coord_n; /*!< \brief Coordinates at time n for use with dynamic meshes. */
86+
su2activematrix Coord_n1; /*!< \brief Coordinates at time n-1 for use with dynamic meshes. */
87+
su2activematrix Coord_p1; /*!< \brief Coordinates at time n+1 for use with dynamic meshes. */
8988

9089
su2activematrix GridVel; /*!< \brief Velocity of the grid for dynamic mesh cases. */
9190
CVectorOfMatrix GridVel_Grad; /*!< \brief Gradient of the grid velocity for dynamic meshes. */

Common/include/option_structure.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ enum ENUM_FLUIDMODEL {
550550
FLUID_MIXTURE = 9, /*!< \brief Species mixture model. */
551551
COOLPROP = 10, /*!< \brief Thermodynamics library. */
552552
FLUID_FLAMELET = 11, /*!< \brief lookup table (LUT) method for premixed flamelets. */
553-
DATADRIVEN_FLUID = 12, /*!< \brief multi-layer perceptron driven fluid model. */
553+
DATADRIVEN_FLUID = 12, /*!< \brief multi-layer perceptron driven fluid model. */
554554
};
555555
static const MapType<std::string, ENUM_FLUIDMODEL> FluidModel_Map = {
556556
MakePair("STANDARD_AIR", STANDARD_AIR)

Common/src/CConfig.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,9 @@ void CConfig::SetConfig_Options() {
15481548
/*!\brief MARKER_PYTHON_CUSTOM\n DESCRIPTION: Python customizable marker(s) \ingroup Config*/
15491549
addStringListOption("MARKER_PYTHON_CUSTOM", nMarker_PyCustom, Marker_PyCustom);
15501550

1551+
/*!\brief PYTHON_CUSTOM_SOURCE\n DESCRIPTION: Python custom source \ingroup Config*/
1552+
addBoolOption("PYTHON_CUSTOM_SOURCE", PyCustomSource, false);
1553+
15511554
/*!\brief MARKER_WALL_FUNCTIONS\n DESCRIPTION: Viscous wall markers for which wall functions must be applied.
15521555
Format: (Wall function marker, wall function type, ...) \ingroup Config*/
15531556
addWallFunctionOption("MARKER_WALL_FUNCTIONS", nMarker_WallFunctions, Marker_WallFunctions,
@@ -5600,7 +5603,7 @@ void CConfig::SetPostprocessing(SU2_COMPONENT val_software, unsigned short val_i
56005603
"to be equal to the number of entries of SPECIES_INIT +1",
56015604
CURRENT_FUNCTION);
56025605

5603-
// Helper function that checks scalar variable bounds,
5606+
/*--- Helper function that checks scalar variable bounds. ---*/
56045607
auto checkScalarBounds = [&](su2double scalar, const string& name, su2double lowerBound, su2double upperBound) {
56055608
if (scalar < lowerBound || scalar > upperBound)
56065609
SU2_MPI::Error(string("Variable: ") + name + string(", is out of bounds."), CURRENT_FUNCTION);

SU2_CFD/include/drivers/CDriver.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,19 @@ class CDriver : public CDriverBase {
555555
*/
556556
void SetMarkerTranslationRate(unsigned short iMarker, passivedouble vel_x, passivedouble vel_y, passivedouble vel_z);
557557

558+
/*!
559+
* \brief Get the Freestream Density for nondimensionalization
560+
* \return Freestream Density
561+
*/
562+
passivedouble GetDensityFreeStreamND() const;
563+
564+
/*!
565+
* \brief Get the reference Body force for nondimensionalization
566+
* \return reference Body Force
567+
*/
568+
passivedouble GetForceRef() const;
569+
570+
558571
/// \}
559572
};
560573

SU2_CFD/include/drivers/CDriverBase.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,23 @@ class CDriverBase {
444444
return CPyWrapperMatrixView(solver->GetNodes()->GetSolution(), "Solution of " + solver->GetSolverName(), false);
445445
}
446446

447+
/*!
448+
* \brief Get read/write view of the gradients of a solver variable in a point.
449+
*/
450+
inline CPyWrapper3DMatrixView Gradient(unsigned short iSolver) {
451+
auto* solver = GetSolverAndCheckMarker(iSolver);
452+
return CPyWrapper3DMatrixView(solver->GetNodes()->GetGradient(), "Gradient of " + solver->GetSolverName(), false);
453+
}
454+
455+
/*!
456+
* \brief Get a read/write view of the user defined source on all mesh nodes of a solver.
457+
*/
458+
inline CPyWrapperMatrixView UserDefinedSource(unsigned short iSolver) {
459+
auto* solver = GetSolverAndCheckMarker(iSolver);
460+
return CPyWrapperMatrixView(
461+
solver->GetNodes()->GetUserDefinedSource(), "User Defined Source of " + solver->GetSolverName(), false);
462+
}
463+
447464
/*!
448465
* \brief Get a read/write view of the current solution on the mesh nodes of a marker.
449466
*/

SU2_CFD/include/solvers/CSolver.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,6 @@ class CSolver {
15571557
* \param[in] geometry - Geometrical definition of the problem.
15581558
* \param[in] solver_container - Container vector with all the solutions.
15591559
* \param[in] numerics_container - Description of the numerical method.
1560-
* \param[in] second_numerics - Description of the second numerical method.
15611560
* \param[in] config - Definition of the particular problem.
15621561
* \param[in] iMesh - Index of the mesh in multigrid computations.
15631562
*/
@@ -4334,6 +4333,25 @@ class CSolver {
43344333
END_SU2_OMP_FOR
43354334
}
43364335

4336+
inline void CustomSourceResidual(CGeometry *geometry, CSolver **solver_container,
4337+
CNumerics **numerics_container, CConfig *config, unsigned short iMesh) {
4338+
4339+
AD::StartNoSharedReading();
4340+
4341+
SU2_OMP_FOR_STAT(roundUpDiv(nPointDomain,2*omp_get_max_threads()))
4342+
for (auto iPoint = 0ul; iPoint < nPointDomain; iPoint++) {
4343+
/*--- Get control volume size. ---*/
4344+
su2double Volume = geometry->nodes->GetVolume(iPoint);
4345+
/*--- Compute the residual for this control volume and subtract. ---*/
4346+
for (auto iVar = 0ul; iVar < nVar; iVar++) {
4347+
LinSysRes(iPoint,iVar) -= base_nodes->GetUserDefinedSource()(iPoint, iVar) * Volume;
4348+
}
4349+
}
4350+
END_SU2_OMP_FOR
4351+
4352+
AD::EndNoSharedReading();
4353+
}
4354+
43374355
protected:
43384356
/*!
43394357
* \brief Allocate the memory for the verification solution, if necessary.

0 commit comments

Comments
 (0)