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 11 commits into
base: develop
Choose a base branch
from
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
82 changes: 82 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,51 @@ 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;
}

/*!
* \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;
}

/// \}

protected:
Expand All @@ -750,6 +820,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;

};
13 changes: 13 additions & 0 deletions SU2_CFD/include/solvers/CEulerSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,19 @@ class CEulerSolver : public CFVMFlowSolverBase<CEulerVariable, ENUM_REGIME::COMP
CNumerics **numerics_container,
CConfig *config,
unsigned short iMesh) override;
/*!
* \brief Custom 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) override;

/*!
* \brief Source term integration.
Expand Down
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
1 change: 1 addition & 0 deletions SU2_CFD/include/solvers/CFVMFlowSolverBase.inl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ 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);

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

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
4 changes: 4 additions & 0 deletions SU2_CFD/src/drivers/CDriverBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@

unsigned long CDriverBase::GetNumberElements() const { return main_geometry->GetnElem(); }

//unsigned long CDriverBase::GetNumberSolverVars() const { return main_geometry->GetnElem(); }
Fixed Show fixed Hide fixed
bigfooted marked this conversation as resolved.
Show resolved Hide resolved
unsigned short CDriverBase::GetNumberSolverVars(const unsigned short iSol) const { return solver_container[selected_zone][INST_0][MESH_0][iSol]->GetnVar(); }
unsigned short CDriverBase::GetNumberPrimitiveVars(const unsigned short iSol) const { return solver_container[selected_zone][INST_0][MESH_0][iSol]->GetnPrimVar(); }

unsigned long CDriverBase::GetElementGlobalIndex(unsigned long iElem) const {
if (iElem >= GetNumberElements()) {
SU2_MPI::Error("Element index exceeds size.", CURRENT_FUNCTION);
Expand Down
3 changes: 3 additions & 0 deletions SU2_CFD/src/integration/CIntegration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ void CIntegration::Space_Integration(CGeometry *geometry,
/*--- Compute source term residuals ---*/
solver_container[MainSolver]->Source_Residual(geometry, solver_container, numerics, config, iMesh);

/*--- Compute custom (python wrapper) source term residuals ---*/
solver_container[MainSolver]->Custom_Source_Residual(geometry, solver_container, numerics, config, iMesh);

/*--- Add viscous and convective residuals, and compute the Dual Time Source term ---*/

if (dual_time)
Expand Down
Loading
Loading