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

Expose all history outputs via the python wrapper #1986

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
154 changes: 56 additions & 98 deletions SU2_CFD/include/drivers/CDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,58 @@ class CDriver : public CDriverBase {
*/
void Print_DirectResidual(RECORDING kind_recording);

/*!
* \brief Set the solution of all solvers (adjoint or primal) in a zone.
* \param[in] iZone - Index of the zone.
* \param[in] adjoint - True to consider adjoint solvers instead of primal.
* \param[in] solution - Solution object with interface (iPoint,iVar).
* \tparam Old - If true set "old solutions" instead.
*/
template <class Container, bool Old = false>
void SetAllSolutions(unsigned short iZone, bool adjoint, const Container& solution) {
const auto nPoint = geometry_container[iZone][INST_0][MESH_0]->GetnPoint();
for (auto iSol = 0u, offset = 0u; iSol < MAX_SOLS; ++iSol) {
auto solver = solver_container[iZone][INST_0][MESH_0][iSol];
if (!(solver && (solver->GetAdjoint() == adjoint))) continue;
for (auto iPoint = 0ul; iPoint < nPoint; ++iPoint)
for (auto iVar = 0ul; iVar < solver->GetnVar(); ++iVar)
if (!Old) {
solver->GetNodes()->SetSolution(iPoint, iVar, solution(iPoint, offset + iVar));
} else {
solver->GetNodes()->SetSolution_Old(iPoint, iVar, solution(iPoint, offset + iVar));
}
offset += solver->GetnVar();
}
}

/*!
* \brief Set the "old solution" of all solvers (adjoint or primal) in a zone.
*/
template <class Container>
void SetAllSolutionsOld(unsigned short iZone, bool adjoint, const Container& solution) {
SetAllSolutions<Container, true>(iZone, adjoint, solution);
}

/*!
* \brief Get the solution of all solvers (adjoint or primal) in a zone.
* \param[in] iZone - Index of the zone.
* \param[in] adjoint - True to consider adjoint solvers instead of primal.
* \param[out] solution - Solution object with interface (iPoint,iVar).
*/
template <class Container>
void GetAllSolutions(unsigned short iZone, bool adjoint, Container& solution) const {
const auto nPoint = geometry_container[iZone][INST_0][MESH_0]->GetnPoint();
for (auto iSol = 0u, offset = 0u; iSol < MAX_SOLS; ++iSol) {
auto solver = solver_container[iZone][INST_0][MESH_0][iSol];
if (!(solver && (solver->GetAdjoint() == adjoint))) continue;
const auto& sol = solver->GetNodes()->GetSolution();
for (auto iPoint = 0ul; iPoint < nPoint; ++iPoint)
for (auto iVar = 0ul; iVar < solver->GetnVar(); ++iVar)
solution(iPoint, offset + iVar) = SU2_TYPE::GetValue(sol(iPoint, iVar));
offset += solver->GetnVar();
}
}

public:
/*!
* \brief Launch the computation for all zones and all physics.
Expand Down Expand Up @@ -413,56 +465,14 @@ class CDriver : public CDriverBase {
void BoundaryConditionsUpdate();

/*!
* \brief Get the total drag.
* \return Total drag.
*/
passivedouble Get_Drag() const;

/*!
* \brief Get the total lift.
* \return Total lift.
*/
passivedouble Get_Lift() const;

/*!
* \brief Get the total x moment.
* \return Total x moment.
*/
passivedouble Get_Mx() const;

/*!
* \brief Get the total y moment.
* \return Total y moment.
*/
passivedouble Get_My() const;

/*!
* \brief Get the total z moment.
* \return Total z moment.
*/
passivedouble Get_Mz() const;

/*!
* \brief Get the total drag coefficient.
* \return Total drag coefficient.
*/
passivedouble Get_DragCoeff() const;

/*!
* \brief Get the total lift coefficient.
* \return Total lift coefficient.
*/
passivedouble Get_LiftCoeff() const;

/*!
* \brief Get the number of external iterations.
* \return Number of external iterations.
* \brief Get the number of time iterations.
* \return Number of time iterations.
*/
unsigned long GetNumberTimeIter() const;

/*!
* \brief Get the current external iteration.
* \return Current external iteration.
* \brief Get the current time iteration.
* \return Current time iteration.
*/
unsigned long GetTimeIter() const;

Expand Down Expand Up @@ -518,58 +528,6 @@ class CDriver : public CDriverBase {
}
return nVar;
}

/*!
* \brief Set the solution of all solvers (adjoint or primal) in a zone.
* \param[in] iZone - Index of the zone.
* \param[in] adjoint - True to consider adjoint solvers instead of primal.
* \param[in] solution - Solution object with interface (iPoint,iVar).
* \tparam Old - If true set "old solutions" instead.
*/
template <class Container, bool Old = false>
void SetAllSolutions(unsigned short iZone, bool adjoint, const Container& solution) {
const auto nPoint = geometry_container[iZone][INST_0][MESH_0]->GetnPoint();
for (auto iSol = 0u, offset = 0u; iSol < MAX_SOLS; ++iSol) {
auto solver = solver_container[iZone][INST_0][MESH_0][iSol];
if (!(solver && (solver->GetAdjoint() == adjoint))) continue;
for (auto iPoint = 0ul; iPoint < nPoint; ++iPoint)
for (auto iVar = 0ul; iVar < solver->GetnVar(); ++iVar)
if (!Old) {
solver->GetNodes()->SetSolution(iPoint, iVar, solution(iPoint, offset + iVar));
} else {
solver->GetNodes()->SetSolution_Old(iPoint, iVar, solution(iPoint, offset + iVar));
}
offset += solver->GetnVar();
}
}

/*!
* \brief Set the "old solution" of all solvers (adjoint or primal) in a zone.
*/
template <class Container>
void SetAllSolutionsOld(unsigned short iZone, bool adjoint, const Container& solution) {
SetAllSolutions<Container, true>(iZone, adjoint, solution);
}

/*!
* \brief Get the solution of all solvers (adjoint or primal) in a zone.
* \param[in] iZone - Index of the zone.
* \param[in] adjoint - True to consider adjoint solvers instead of primal.
* \param[out] solution - Solution object with interface (iPoint,iVar).
*/
template <class Container>
void GetAllSolutions(unsigned short iZone, bool adjoint, Container& solution) const {
const auto nPoint = geometry_container[iZone][INST_0][MESH_0]->GetnPoint();
for (auto iSol = 0u, offset = 0u; iSol < MAX_SOLS; ++iSol) {
auto solver = solver_container[iZone][INST_0][MESH_0][iSol];
if (!(solver && (solver->GetAdjoint() == adjoint))) continue;
const auto& sol = solver->GetNodes()->GetSolution();
for (auto iPoint = 0ul; iPoint < nPoint; ++iPoint)
for (auto iVar = 0ul; iVar < solver->GetnVar(); ++iVar)
solution(iPoint, offset + iVar) = SU2_TYPE::GetValue(sol(iPoint, iVar));
offset += solver->GetnVar();
}
}
};

/*!
Expand Down
53 changes: 48 additions & 5 deletions SU2_CFD/include/drivers/CDriverBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ class CDriverBase {
*/
virtual void Update(){}

/*!
* \brief A virtual member.
*/
virtual void Update_Legacy(){}

/*!
* \brief A virtual member.
*/
Expand All @@ -125,6 +120,54 @@ class CDriverBase {
/// \addtogroup PySU2
/// @{

/*!
* \brief Get the list of available outputs.
* \return List of output names.
*/
inline vector<string> GetOutputNames() const { return output_container[MESH_0]->GetHistoryOutput_List(); }

/*!
* \brief Get the value of one of the available history outputs.
* \return Value of the output.
*/
inline passivedouble GetOutputValue(std::string output_name) const {
pcarruscag marked this conversation as resolved.
Show resolved Hide resolved
return SU2_TYPE::GetValue(output_container[MESH_0]->GetHistoryFieldValue(output_name));
}

/*!
* \brief Get the list of available surface outputs on **both** MARKER_MONITORING and MARKER_ANALYZE.
* \return List of surface output names.
*/
inline vector<string> GetMarkerOutputNames() const {
return output_container[MESH_0]->GetHistoryOutputPerSurface_List();
}

/*!
* \brief Get the value of one of the available surface outputs at a given MARKER_MONITORING.
* \return Value of the output.
*/
inline passivedouble GetMarkerMonitoringOutputValue(std::string output_name, std::string marker_monitoring) const {
pcarruscag marked this conversation as resolved.
Show resolved Hide resolved
for (auto iMarker = 0u; iMarker < main_config->GetnMarker_Monitoring(); ++iMarker) {
if (marker_monitoring == main_config->GetMarker_Monitoring_TagBound(iMarker))
return SU2_TYPE::GetValue(output_container[MESH_0]->GetHistoryFieldValuePerSurface(output_name, iMarker));
}
SU2_MPI::Error(marker_monitoring + " is not in MARKER_MONITORING.", CURRENT_FUNCTION);
return 0;
}

/*!
* \brief Get the value of one of the available surface outputs at a given MARKER_ANALYZE.
* \return Value of the output.
*/
inline passivedouble GetMarkerAnalyzeOutputValue(std::string output_name, std::string marker_analyze) const {
pcarruscag marked this conversation as resolved.
Show resolved Hide resolved
for (auto iMarker = 0u; iMarker < main_config->GetnMarker_Analyze(); ++iMarker) {
if (marker_analyze == main_config->GetMarker_Analyze_TagBound(iMarker))
return SU2_TYPE::GetValue(output_container[MESH_0]->GetHistoryFieldValuePerSurface(output_name, iMarker));
}
SU2_MPI::Error(marker_analyze + " is not in MARKER_ANALYZE.", CURRENT_FUNCTION);
return 0;
}

/*!
* \brief Get the number of design variables.
* \return Number of design variables.
Expand Down
17 changes: 11 additions & 6 deletions SU2_CFD/include/output/COutput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,11 @@ class COutput {
* \param[in] field - Name of the field
* \return Value of the field
*/
su2double GetHistoryFieldValue(const string& field) const {
return historyOutput_Map.at(field).value;
su2double GetHistoryFieldValue(const string& name) const {
auto it = historyOutput_Map.find(name);
if (it != historyOutput_Map.end()) return it->second.value;
SU2_MPI::Error("Cannot find output field with name " + name, CURRENT_FUNCTION);
return 0;
}

/*!
Expand All @@ -456,8 +459,11 @@ class COutput {
* \param[in] iMarker - Index of the surface marker
* \return Value of the field
*/
su2double GetHistoryFieldValuePerSurface(const string& field, unsigned short iMarker) const {
return historyOutputPerSurface_Map.at(field)[iMarker].value;
su2double GetHistoryFieldValuePerSurface(const string& name, unsigned short iMarker) const {
auto it = historyOutputPerSurface_Map.find(name);
if (it != historyOutputPerSurface_Map.end()) return it->second[iMarker].value;
SU2_MPI::Error("Cannot find output field with name " + name, CURRENT_FUNCTION);
return 0;
}

/*!
Expand Down Expand Up @@ -634,7 +640,7 @@ class COutput {
if (it != historyOutput_Map.end()){
it->second.value = value;
} else {
SU2_MPI::Error(string("Cannot find output field with name ") + name, CURRENT_FUNCTION);
SU2_MPI::Error("Cannot find output field with name " + name, CURRENT_FUNCTION);
}
}

Expand Down Expand Up @@ -718,7 +724,6 @@ class COutput {
volumeOutput_List.push_back(name);
}


/*!
* \brief Set the value of a volume output field
* \param[in] name - Name of the field.
Expand Down
3 changes: 3 additions & 0 deletions SU2_CFD/src/output/CFlowOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,8 @@ void CFlowOutput::LoadSurfaceData(CConfig *config, CGeometry *geometry, CSolver
void CFlowOutput::AddAerodynamicCoefficients(const CConfig* config) {

/// BEGIN_GROUP: AERO_COEFF, DESCRIPTION: Sum of the aerodynamic coefficients and forces on all surfaces (markers) set with MARKER_MONITORING.
/// DESCRIPTION: Reference force for aerodynamic coefficients
AddHistoryOutput("REFERENCE_FORCE", "RefForce", ScreenOutputFormat::FIXED, "AERO_COEFF", "Reference force used to compute aerodynamic coefficients", HistoryFieldType::COEFFICIENT);
/// DESCRIPTION: Drag coefficient
AddHistoryOutput("DRAG", "CD", ScreenOutputFormat::FIXED, "AERO_COEFF", "Total drag coefficient on all surfaces set with MARKER_MONITORING", HistoryFieldType::COEFFICIENT);
/// DESCRIPTION: Lift coefficient
Expand Down Expand Up @@ -1409,6 +1411,7 @@ void CFlowOutput::AddAerodynamicCoefficients(const CConfig* config) {

void CFlowOutput::SetAerodynamicCoefficients(const CConfig* config, const CSolver* flow_solver){

SetHistoryOutputValue("REFERENCE_FORCE", flow_solver->GetAeroCoeffsReferenceForce());
SetHistoryOutputValue("DRAG", flow_solver->GetTotal_CD());
SetHistoryOutputValue("LIFT", flow_solver->GetTotal_CL());
if (nDim == 3)
Expand Down
Loading