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 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
121 changes: 121 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 @@
*/
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 @@
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,90 @@
}
}

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

//if (values[0]>1.0e-6)
// cout << "iPoint="<<iPoint << ", setting custom point source" << values[0]<< endl;
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);
//cout << "setting solution vector " << nodes->GetSolution(iPoint,0) << " " << solutionVector[0] << ", "<< nVar<< endl;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
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);
//cout << "setting primitive vector " << nodes->GetPrimitive(iPoint,0) << " " << primitiveVector[0] << ", "<< nPrimvar<< endl;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

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

/// \}

protected:
Expand All @@ -750,6 +859,18 @@
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
35 changes: 35 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 @@
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 @@
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,28 @@
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 ---*/
//cout << "flow" << endl;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
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
2 changes: 2 additions & 0 deletions SU2_CFD/include/solvers/CScalarSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class CScalarSolver : public CSolver {
vector<su2matrix<su2double*> > SlidingState; // vector of matrix of pointers... inner dim alloc'd elsewhere (welcome, to the twilight zone)
vector<vector<int> > SlidingStateNodes;


/*--- Shallow copy of grid coloring for OpenMP parallelization. ---*/

#ifdef HAVE_OMP
Expand Down Expand Up @@ -578,4 +579,5 @@ class CScalarSolver : public CSolver {
return SlidingStateNodes[val_marker][val_vertex];
}


};
1 change: 1 addition & 0 deletions SU2_CFD/include/solvers/CScalarSolver.inl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ CScalarSolver<VariableType>::CScalarSolver(CGeometry* geometry, CConfig* config,
config->GetNEMOProblem(), geometry->GetnDim(), config->GetnSpecies()) {
nMarker = config->GetnMarker_All();


/*--- Store the number of vertices on each marker for deallocation later ---*/
nVertex.resize(nMarker);
for (unsigned long iMarker = 0; iMarker < nMarker; iMarker++) nVertex[iMarker] = geometry->nVertex[iMarker];
Expand Down
Loading
Loading