Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] pywrapper - custom source terms for all solvers #2388

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5875625
initial update
bigfooted Nov 25, 2024
cc3dfab
initial working version for buoyant flow
bigfooted Dec 3, 2024
b407a27
small cleanup
bigfooted Dec 3, 2024
ce0b61e
small cleanup of python file
bigfooted Dec 3, 2024
d8d30a4
small cleanup
bigfooted Dec 3, 2024
488efa0
Update SU2_CFD/include/drivers/CDriverBase.hpp
bigfooted Dec 3, 2024
40e1d97
Fix code scanning alert no. 5107: Comparison of narrow type with wide…
bigfooted Dec 3, 2024
b77fd09
Update SU2_CFD/include/solvers/CFVMFlowSolverBase.inl
bigfooted Dec 3, 2024
4904b19
Update SU2_CFD/src/numerics/flow/flow_sources.cpp
bigfooted Dec 3, 2024
d4ac82b
Update SU2_CFD/src/drivers/CDriverBase.cpp
bigfooted Dec 3, 2024
1e64b8d
Merge branch 'develop' into feature_custom_source
bigfooted Dec 9, 2024
3429530
Merge branch 'develop' into feature_custom_source
bigfooted Dec 28, 2024
51b0b44
Merge branch 'develop' into feature_custom_source
bigfooted Jan 2, 2025
ce99e01
Turbulent combustion (TFC)
bigfooted Jan 6, 2025
eb98506
Merge branch 'feature_custom_source' of https://github.com/su2code/su…
bigfooted Jan 6, 2025
11e6c7a
Merge branch 'develop' into feature_custom_source
bigfooted Jan 14, 2025
a845d20
Merge branch 'develop' into feature_custom_source
bigfooted Feb 5, 2025
33603e3
Merge branch 'develop' into feature_custom_source
bigfooted Feb 18, 2025
dd8462d
cleanup, fix AD compile error
bigfooted Feb 22, 2025
a3439f9
cleanup examples
bigfooted Feb 22, 2025
cd581f4
more cleanup of examples
bigfooted Feb 23, 2025
dc65d47
more cleanup of examples
bigfooted Feb 23, 2025
2003b18
add testcases
bigfooted Feb 24, 2025
effe99d
change testcases branch
bigfooted Feb 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Common/include/option_structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ enum ENUM_FLUIDMODEL {
FLUID_MIXTURE = 9, /*!< \brief Species mixture model. */
COOLPROP = 10, /*!< \brief Thermodynamics library. */
FLUID_FLAMELET = 11, /*!< \brief lookup table (LUT) method for premixed flamelets. */
DATADRIVEN_FLUID = 12, /*!< \brief multi-layer perceptron driven fluid model. */
DATADRIVEN_FLUID = 12, /*!< \brief multi-layer perceptron driven fluid model. */
USER_DEFINED = 13, /*!< \brief user defined through python wrapper. */
};
static const MapType<std::string, ENUM_FLUIDMODEL> FluidModel_Map = {
MakePair("STANDARD_AIR", STANDARD_AIR)
Expand All @@ -565,6 +566,7 @@ static const MapType<std::string, ENUM_FLUIDMODEL> FluidModel_Map = {
MakePair("COOLPROP", COOLPROP)
MakePair("DATADRIVEN_FLUID", DATADRIVEN_FLUID)
MakePair("FLUID_FLAMELET", FLUID_FLAMELET)
MakePair("USER_DEFINED", USER_DEFINED)
};

/*!
Expand Down
31 changes: 31 additions & 0 deletions SU2_CFD/include/drivers/CDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,24 @@ class CDriver : public CDriverBase {
*/
unsigned long GetNumberTimeIter() const;

/*!
* \brief Get the number of inner iterations.
* \return Number of inner iterations.
*/
unsigned long GetNumberInnerIter() const;

/*!
* \brief Get the number of outer iterations.
* \return Number of outer iterations.
*/
unsigned long GetNumberOuterIter() const;

/*!
* \brief Get the current solution
* \return Current solution
*/
unsigned long GetSolution(unsigned short iSolver, unsigned long iPoint, unsigned short iVar);

/*!
* \brief Get the current time iteration.
* \return Current time iteration.
Expand Down Expand Up @@ -555,6 +573,19 @@ class CDriver : public CDriverBase {
*/
void SetMarkerTranslationRate(unsigned short iMarker, passivedouble vel_x, passivedouble vel_y, passivedouble vel_z);

/*!
* \brief Get the Freestream Density for nondimensionalization
* \return Freestream Density
*/
unsigned long GetDensity_FreeStreamND() const;

/*!
* \brief Get the reference Body force for nondimensionalization
* \return reference Body Force
*/
unsigned long GetForce_Ref() const;


/// \}
};

Expand Down
116 changes: 116 additions & 0 deletions SU2_CFD/include/drivers/CDriverBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ class CDriverBase {
*/
unsigned long GetNumberElements() const;

/*!
* \brief Get the number of solution variables
* \return Number of solution variables.
*/

unsigned short GetNumberSolverVars(const unsigned short iSol) const;
unsigned short GetNumberPrimitiveVars(const unsigned short iSol) const;

/*!
* \brief Get the global index of a mesh element.
* \param[in] iElem - Mesh element index.
Expand Down Expand Up @@ -667,6 +675,23 @@ class CDriverBase {
return sens;
}

/*!
* \brief Get the gradients of a solver variable in a point.
* \returns Vector of gradients grad(iVar).
*/
inline vector<passivedouble> GetGradient(unsigned short iSolver, unsigned long iPoint, unsigned short iVar) {
const auto nDim = GetNumberDimensions();
auto* solver = GetSolverAndCheckMarker(iSolver);
auto* nodes = solver->GetNodes();

vector<passivedouble> grad(nDim, 0.0);
for (auto iDim = 0u; iDim < nDim; ++iDim) {
grad[iDim] = SU2_TYPE::GetValue(nodes->GetGradient(iPoint, iVar, iDim));
}
return grad;
}


/*!
* \brief Set the adjoint of the structural displacements.
* \note This can be the input of the FEA solver in an adjoint FSI setting.
Expand Down Expand Up @@ -734,6 +759,85 @@ class CDriverBase {
}
}

/*!
* \brief Set the array of variables for the source in the point
* \param[in] iSolver - Solver index.
* \param[in] iPoint - Point index.
* \param[in] values - Vector values of the source term.
*/
void SetPointCustomSource(unsigned short iSolver, unsigned long iPoint, std::vector<passivedouble> values) {
auto* solver = solver_container[selected_zone][INST_0][MESH_0][iSolver];
solver->SetCustomPointSource(iPoint, values);
}

/*!
* \brief Get the solution vector in a point for a specific solver
* \param[in] iSolver - Solver index.
* \param[in] iPoint - Point index.
* \param[out] solutionvector - Vector values of the solution.
*/
inline vector<passivedouble> GetSolutionVector(unsigned short iSolver, unsigned long iPoint) {
auto* solver = solver_container[selected_zone][INST_0][MESH_0][iSolver];
auto* nodes = solver->GetNodes();
auto nVar = GetNumberSolverVars(iSolver);
vector<passivedouble> solutionvector(nVar, 0.0);
for (auto iVar = 0u; iVar < nVar; ++iVar) {
solutionvector[iVar] = SU2_TYPE::GetValue(nodes->GetSolution(iPoint,iVar));
}
return solutionvector;
}

/* The vector with actual solution values */
inline void SetSolutionVector(unsigned short iSolver, unsigned long iPoint, vector<passivedouble> solutionVector) {
auto* solver = solver_container[selected_zone][INST_0][MESH_0][iSolver];
auto* nodes = solver->GetNodes();

/*--- check the size of the solver variables ---*/
unsigned short nVar = GetNumberSolverVars(iSolver);
if (nVar != solutionVector.size() )
SU2_MPI::Error("Solution Vector size is not equal to Solver size.", CURRENT_FUNCTION);
for (unsigned int iVar = 0u; iVar < nVar; ++iVar) {
nodes->SetSolution(iPoint,iVar, solutionVector[iVar]);
nodes->SetSolution_Old(iPoint,iVar, solutionVector[iVar]);
}
}


/*!
* \brief Get the primitive variables vector in a point for a specific solver
* \param[in] iSolver - Solver index.
* \param[in] iPoint - Point index.
* \param[out] solutionvector - Vector values of the primitive variables.
*/
inline vector<passivedouble> GetPrimitiveVector(unsigned short iSolver, unsigned long iPoint) {
auto* solver = solver_container[selected_zone][INST_0][MESH_0][iSolver];
auto* nodes = solver->GetNodes();
auto nPrimvar = GetNumberPrimitiveVars(iSolver);
vector<passivedouble> solutionvector(nPrimvar, 0.0);
for (auto iVar = 0u; iVar < nPrimvar; ++iVar) {
solutionvector[iVar] = SU2_TYPE::GetValue(nodes->GetPrimitive(iPoint,iVar));
}
return solutionvector;
}


/*!
* \brief Get the primitive variables vector in a point for a specific solver
* \param[in] iSolver - Solver index.
* \param[in] iPoint - Point index.
* \param[out] solutionvector - Vector values of the primitive variables.
*/
inline void SetPrimitiveVector(unsigned short iSolver, unsigned long iPoint, vector<passivedouble> primitiveVector) {
auto* solver = solver_container[selected_zone][INST_0][MESH_0][iSolver];
auto* nodes = solver->GetNodes();
auto nPrimvar = GetNumberPrimitiveVars(iSolver);
vector<passivedouble> solutionvector(nPrimvar, 0.0);

for (auto iVar = 0u; iVar < nPrimvar; ++iVar) {
nodes->SetPrimitive(iPoint,iVar, primitiveVector[iVar]);
}
}

/// \}

protected:
Expand All @@ -750,6 +854,18 @@ class CDriverBase {
return solver;
}

/*!
* \brief Automates some boilerplate of accessing solution fields for the python wrapper.
*/
inline CSolver* GetSolverAndCheckField(unsigned short iSolver,
unsigned long iPoint = std::numeric_limits<unsigned long>::max()) const {
// 1. check for the solver the number of variables
// 2. check for the mesh the number of points
auto* solver = solver_container[selected_zone][INST_0][MESH_0][iSolver];
if (solver == nullptr) SU2_MPI::Error("The selected solver does not exist.", CURRENT_FUNCTION);
return solver;
}

/*!
* \brief Initialize containers.
*/
Expand Down
1 change: 1 addition & 0 deletions SU2_CFD/include/numerics/CNumerics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class CNumerics {
su2double
LocalGridLength_i; /*!< \brief Local grid length at point i. */
const su2double *RadVar_Source; /*!< \brief Source term from the radiative heat transfer equation. */
const su2double *UserDefinedSource; /*!< \brief User Defined Source term. */
const su2double
*Coord_i, /*!< \brief Cartesians coordinates of point i. */
*Coord_j; /*!< \brief Cartesians coordinates of point j. */
Expand Down
24 changes: 24 additions & 0 deletions SU2_CFD/include/numerics/flow/flow_sources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,27 @@ class CSourceRadiation : public CSourceBase_Flow {
ResidualType<> ComputeResidual(const CConfig* config) override;

};


/*!
* \class CSourceUserDefined
* \brief Class for a user defined source term using the python wrapper
* \ingroup SourceDiscr
* \author Nijso Beishuizen
*/
class CSourceUserDefined : public CSourceBase_Flow {
private:
bool implicit;

public:

CSourceUserDefined(unsigned short val_nDim, unsigned short val_nVar, const CConfig *config);

/*!
* \brief Source term integration for a user defined source.
* \param[in] config - Definition of the particular problem.
* \return Lightweight const-view of residual and Jacobian.
*/
ResidualType<> ComputeResidual(const CConfig* config) override;

};
34 changes: 34 additions & 0 deletions SU2_CFD/include/solvers/CFVMFlowSolverBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class CFVMFlowSolverBase : public CSolver {
vector<vector<su2double> > Inlet_Ptotal; /*!< \brief Value of the Total P. */
vector<vector<su2double> > Inlet_Ttotal; /*!< \brief Value of the Total T. */
vector<su2activematrix> Inlet_FlowDir; /*!< \brief Value of the Flow Direction. */
su2activematrix PointSource; /*!< \brief Value of the Flow Direction. */
vector<vector<su2double> > HeatFlux; /*!< \brief Heat transfer coefficient for each boundary and vertex. */
vector<vector<su2double> > HeatFluxTarget; /*!< \brief Heat transfer coefficient for each boundary and vertex. */
vector<su2activematrix> CharacPrimVar; /*!< \brief Value of the characteristic variables at each boundary. */
Expand Down Expand Up @@ -2148,6 +2149,18 @@ class CFVMFlowSolverBase : public CSolver {
return Inlet_FlowDir[val_marker][val_vertex][val_dim];
}

/*!
* \brief A component of the unit vector representing the flow direction at an inlet boundary.
* \param[in] val_marker - Surface marker where the flow direction is evaluated
* \param[in] val_vertex - Vertex of the marker <i>val_marker</i> where the flow direction is evaluated
* \param[in] val_dim - The component of the flow direction unit vector to be evaluated
* \return Component of a unit vector representing the flow direction.
*/
inline su2double GetCustomPointSource(unsigned long val_point,
unsigned short val_var) const final {
return PointSource[val_point][val_var];
}

/*!
* \brief Set the value of the total temperature at an inlet boundary.
* \param[in] val_marker - Surface marker where the total temperature is set.
Expand Down Expand Up @@ -2201,6 +2214,27 @@ class CFVMFlowSolverBase : public CSolver {
Inlet_FlowDir[val_marker][val_vertex][val_dim] = val_flowdir;
}

/*!
* \brief Set a component of the unit vector representing the flow direction at an inlet boundary.
* \param[in] val_marker - Surface marker where the flow direction is set.
* \param[in] val_vertex - Vertex of the marker <i>val_marker</i> where the flow direction is set.
* \param[in] val_dim - The component of the flow direction unit vector to be set
* \param[in] val_flowdir - Component of a unit vector representing the flow direction.
*/
inline void SetCustomPointSource(unsigned long val_point,
vector<passivedouble> val_source) final {
/*--- Since this call can be accessed indirectly using python, do some error
* checking to prevent segmentation faults ---*/
if (val_point > nPointDomain)
SU2_MPI::Error("Out-of-bounds point index used on solver.", CURRENT_FUNCTION);
else if (val_source.size() > nVar)
SU2_MPI::Error("Out-of-bounds source size used on solver.", CURRENT_FUNCTION);
else {
for (size_t iVar=0; iVar < val_source.size(); iVar++)
PointSource[val_point][iVar] = val_source[iVar];
}
}

/*!
* \brief Update the multi-grid structure for the customized boundary conditions.
* \param geometry_container - Geometrical definition.
Expand Down
4 changes: 3 additions & 1 deletion SU2_CFD/include/solvers/CFVMFlowSolverBase.inl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ void CFVMFlowSolverBase<V, R>::Allocate(const CConfig& config) {
/*--- Store the value of the Flow direction at the inlet BC ---*/

AllocVectorOfMatrices(nVertex, nDim, Inlet_FlowDir);

PointSource.resize(nPointDomain,nVar);
PointSource.setConstant(0.0);

/*--- Force definition and coefficient arrays for all of the markers ---*/

AllocVectorOfVectors(nVertex, CPressure);
Expand Down
13 changes: 13 additions & 0 deletions SU2_CFD/include/solvers/CIncEulerSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ class CIncEulerSolver : public CFVMFlowSolverBase<CIncEulerVariable, ENUM_REGIME
CNumerics **numerics_container,
CConfig *config,
unsigned short iMesh) final;
/*!
* \brief Source term integration.
* \param[in] geometry - Geometrical definition of the problem.
* \param[in] solver_container - Container vector with all the solutions.
* \param[in] numerics_container - Description of the numerical method.
* \param[in] config - Definition of the particular problem.
* \param[in] iMesh - Index of the mesh in multigrid computations.
*/
void Custom_Source_Residual(CGeometry *geometry,
CSolver **solver_container,
CNumerics **numerics_container,
CConfig *config,
unsigned short iMesh) final;

/*!
* \brief Source term integration.
Expand Down
34 changes: 32 additions & 2 deletions SU2_CFD/include/solvers/CSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,20 @@ class CSolver {
CNumerics **numerics_container,
CConfig *config,
unsigned short iMesh) { }
/*!
* \brief A virtual member.
* \param[in] geometry - Geometrical definition of the problem.
* \param[in] solver_container - Container vector with all the solutions.
* \param[in] numerics_container - Description of the numerical method.
* \param[in] second_numerics - Description of the second numerical method.
* \param[in] config - Definition of the particular problem.
* \param[in] iMesh - Index of the mesh in multigrid computations.
*/
inline virtual void Custom_Source_Residual(CGeometry *geometry,
CSolver **solver_container,
CNumerics **numerics_container,
CConfig *config,
unsigned short iMesh) { }

/*!
* \brief A virtual member.
Expand Down Expand Up @@ -2840,7 +2854,15 @@ class CSolver {
inline virtual su2double GetInletFlowDir(unsigned short val_marker,
unsigned long val_vertex,
unsigned short val_dim) const { return 0; }

/*!
* \brief A virtual member
* \param[in] val_marker - Surface marker where the flow direction is evaluated
* \param[in] val_vertex - Vertex of the marker <i>val_marker</i> where the flow direction is evaluated
* \param[in] val_dim - The component of the flow direction unit vector to be evaluated
* \return Component of a unit vector representing the flow direction.
*/
inline virtual su2double GetCustomPointSource(unsigned long val_point,
unsigned short val_var) const { return 0; }
/*!
* \brief A virtual member
* \param[in] val_marker - Surface marker where the total temperature is set.
Expand Down Expand Up @@ -2872,7 +2894,15 @@ class CSolver {
unsigned long val_vertex,
unsigned short val_dim,
su2double val_flowdir) { }

/*!
* \brief A virtual member
* \param[in] val_marker - Surface marker where the flow direction is set.
* \param[in] val_vertex - Vertex of the marker <i>val_marker</i> where the flow direction is set.
* \param[in] val_dim - The component of the flow direction unit vector to be set
* \param[in] val_flowdir - Component of a unit vector representing the flow direction.
*/
inline virtual void SetCustomPointSource(unsigned long val_Point,
vector<passivedouble> val_source) { }
/*!
* \brief Updates the components of the farfield velocity vector.
*/
Expand Down
Loading