Skip to content

Commit 36e7f69

Browse files
committed
allow simple custom outputs for all output classes and allow them to be used for primal and adjoint variables
1 parent 2bb4550 commit 36e7f69

15 files changed

Lines changed: 95 additions & 12 deletions

SU2_CFD/include/output/CFlowOutput.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ class CFlowOutput : public CFVMOutput{
215215
* \brief Helper for custom outputs, converts variable names to indices and pointers which are then used
216216
* to evaluate the custom expressions.
217217
*/
218-
void ConvertVariableSymbolsToIndices(const CPrimitiveIndices<unsigned long>& idx, CustomOutput& output) const;
218+
void ConvertVariableSymbolsToIndices(const CPrimitiveIndices<unsigned long>& idx, bool allowSkip,
219+
CustomOutput& output) const;
219220

220221
/*!
221222
* \brief Compute value of the Q criteration for vortex idenfitication
@@ -296,7 +297,7 @@ class CFlowOutput : public CFVMOutput{
296297
* \param[in] force_writing - boolean that forces writing of volume output
297298
* \param[in] iFile - index to the file that we need to consider for volume output
298299
*/
299-
bool WriteVolumeOutput(CConfig *config, unsigned long Iter, bool force_writing, unsigned short iFile) override;
300+
bool WriteVolumeOutput(CConfig *config, unsigned long Iter, bool force_writing, unsigned short iFile) override;
300301
/*!
301302
* \brief Write the forces breakdown file
302303
* \param[in] config - Definition of the particular problem per zone.

SU2_CFD/include/output/COutput.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ class COutput {
223223
We store pointers to the required outputs to speed-up access. ---*/
224224
std::vector<const su2double*> otherOutputs;
225225

226+
/*--- For discrete adjoint we may need to skip some expressions because there is one output class
227+
for the primal solver and one for the discrete adjoint (each with different variables). ---*/
228+
bool skip = false;
229+
226230
/*--- For evaluation, "vars" is a functor (i.e. has operator()) that returns the value of a variable at a given
227231
point. For example, it can be a wrapper to the primitives pointer, in which case varIndices needs to be setup
228232
with primitive indices. ---*/
@@ -807,6 +811,14 @@ class COutput {
807811
*/
808812
void SetCustomOutputs(const CConfig *config);
809813

814+
/*!
815+
* \brief Evaluates function-type custom outputs.
816+
* Derived classes can use this to compute simple expressions of other outputs if they
817+
* do not implement surface averages. This should be called just before evaluating the
818+
* custom objective function.
819+
*/
820+
void ComputeSimpleCustomOutputs(const CConfig *config);
821+
810822
/*!
811823
* \brief Load values of the history fields common for all solvers.
812824
* \param[in] config - Definition of the particular problem.

SU2_CFD/src/output/CAdjElasticityOutput.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ inline void CAdjElasticityOutput::LoadHistoryData(CConfig *config, CGeometry *ge
172172
SetHistoryOutputValue("BGS_ADJ_DISP_Z", log10(solver[ADJFEA_SOL]->GetRes_BGS(2)));
173173
}
174174
}
175+
176+
ComputeSimpleCustomOutputs(config);
175177
}
176178

177179
void CAdjElasticityOutput::LoadVolumeData(CConfig *config, CGeometry *geometry, CSolver **solver, unsigned long iPoint){

SU2_CFD/src/output/CAdjFlowCompOutput.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ void CAdjFlowCompOutput::LoadHistoryData(CConfig *config, CGeometry *geometry, C
225225
}
226226

227227
LoadHistoryDataAdjScalar(config, solver);
228+
229+
ComputeSimpleCustomOutputs(config);
228230
}
229231

230232
void CAdjFlowCompOutput::SetVolumeOutputFields(CConfig *config) {

SU2_CFD/src/output/CAdjFlowIncOutput.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ void CAdjFlowIncOutput::LoadHistoryData(CConfig *config, CGeometry *geometry, CS
275275
}
276276

277277
LoadHistoryDataAdjScalar(config, solver);
278+
279+
ComputeSimpleCustomOutputs(config);
278280
}
279281

280282
void CAdjFlowIncOutput::SetVolumeOutputFields(CConfig *config) {

SU2_CFD/src/output/CAdjFlowOutput.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ void CAdjFlowOutput::LoadHistoryDataAdjScalar(const CConfig* config, const CSolv
172172
SetHistoryOutputValue("LINSOL_ITER_SPECIES", adjspecies_solver->GetIterLinSolver());
173173
SetHistoryOutputValue("LINSOL_RESIDUAL_SPECIES", log10(adjspecies_solver->GetResLinSolver()));
174174
}
175+
176+
ComputeSimpleCustomOutputs(config);
175177
}
176178

177179
void CAdjFlowOutput::SetVolumeOutputFieldsAdjScalarSolution(const CConfig* config) {

SU2_CFD/src/output/CAdjHeatOutput.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ void CAdjHeatOutput::LoadHistoryData(CConfig *config, CGeometry *geometry, CSolv
142142
SetHistoryOutputValue("DEFORM_RESIDUAL", log10(solver[MESH_SOL]->System.GetResidual()));
143143
}
144144

145+
ComputeSimpleCustomOutputs(config);
145146
}
146147

147148
void CAdjHeatOutput::SetVolumeOutputFields(CConfig *config){

SU2_CFD/src/output/CElasticityOutput.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ void CElasticityOutput::LoadHistoryData(CConfig *config, CGeometry *geometry, CS
146146
SetHistoryOutputValue("VOLUME_FRACTION", fea_solver->GetTotal_OFVolFrac());
147147
SetHistoryOutputValue("TOPOL_DISCRETENESS", fea_solver->GetTotal_OFDiscreteness());
148148
}
149+
150+
ComputeSimpleCustomOutputs(config);
151+
149152
/*--- Keep this as last, since it uses the history values that were set. ---*/
150153
SetCustomAndComboObjectives(FEA_SOL, config, solver);
151154

SU2_CFD/src/output/CFlowCompFEMOutput.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ void CFlowCompFEMOutput::LoadHistoryData(CConfig *config, CGeometry *geometry, C
263263

264264
SetAerodynamicCoefficients(config, flow_solver);
265265

266+
ComputeSimpleCustomOutputs(config);
266267
}
267268

268269
bool CFlowCompFEMOutput::SetInitResiduals(const CConfig *config){

SU2_CFD/src/output/CFlowOutput.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ void CFlowOutput::SetAnalyzeSurfaceSpeciesVariance(const CSolver* const*solver,
745745
SetHistoryOutputValue("SURFACE_SPECIES_VARIANCE", Tot_Surface_SpeciesVariance);
746746
}
747747

748-
void CFlowOutput::ConvertVariableSymbolsToIndices(const CPrimitiveIndices<unsigned long>& idx,
748+
void CFlowOutput::ConvertVariableSymbolsToIndices(const CPrimitiveIndices<unsigned long>& idx, const bool allowSkip,
749749
CustomOutput& output) const {
750750
const auto nameToIndex = PrimitiveNameToIndexMap(idx);
751751

@@ -796,23 +796,38 @@ void CFlowOutput::ConvertVariableSymbolsToIndices(const CPrimitiveIndices<unsign
796796
output.varIndices.back() += output.otherOutputs.size();
797797
output.otherOutputs.push_back(GetPtrToHistoryOutput(var));
798798
if (output.otherOutputs.back() == nullptr) {
799-
SU2_MPI::Error("Invalid history output or solver variable (" + var + ") used in function " + output.name +
800-
"\nValid solvers variables:\n" + knownVariables.str(), CURRENT_FUNCTION);
799+
if (!allowSkip) {
800+
SU2_MPI::Error("Invalid history output or solver variable (" + var + ") used in function " + output.name +
801+
"\nValid solvers variables:\n" + knownVariables.str(), CURRENT_FUNCTION);
802+
} else {
803+
if (rank == MASTER_NODE) {
804+
std::cout << "Info: Ignoring function " + output.name + " because it may be used by the primal/adjoint "
805+
"solver.\n If the function is ignored twice it is invalid." << std::endl;
806+
}
807+
output.skip = true;
808+
break;
809+
}
801810
}
802811
}
803812
}
804813

805814
void CFlowOutput::SetCustomOutputs(const CSolver* const* solver, const CGeometry *geometry, const CConfig *config) {
806815

816+
const bool adjoint = config->GetDiscrete_Adjoint();
807817
const bool axisymmetric = config->GetAxisymmetric();
808818
const auto* flowNodes = su2staticcast_p<const CFlowVariable*>(solver[FLOW_SOL]->GetNodes());
809819

810820
for (auto& output : customOutputs) {
821+
if (output.skip) continue;
822+
811823
if (output.varIndices.empty()) {
824+
const bool allowSkip = adjoint && (output.type == OperationType::FUNCTION);
825+
812826
/*--- Setup indices for the symbols in the expression. ---*/
813827
const auto primIdx = CPrimitiveIndices<unsigned long>(config->GetKind_Regime() == ENUM_REGIME::INCOMPRESSIBLE,
814828
config->GetNEMOProblem(), nDim, config->GetnSpecies());
815-
ConvertVariableSymbolsToIndices(primIdx, output);
829+
ConvertVariableSymbolsToIndices(primIdx, allowSkip, output);
830+
if (output.skip) continue;
816831

817832
/*--- Convert marker names to their index (if any) in this rank. Or probe locations to nearest points. ---*/
818833

0 commit comments

Comments
 (0)