Skip to content

Commit 1f92147

Browse files
authored
Merge pull request #1311 from su2code/fix_cp_inv_design
Fix inverse design Cp function
2 parents a2c4a2c + 8d83463 commit 1f92147

File tree

7 files changed

+170
-1098
lines changed

7 files changed

+170
-1098
lines changed

SU2_CFD/include/output/CFlowOutput.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,16 @@ class CFlowOutput : public CFVMOutput{
7777

7878
/*!
7979
* \brief Add CP inverse design output as history fields
80-
* \param[in] config - Definition of the particular problem.
8180
*/
82-
void Add_CpInverseDesignOutput(CConfig *config);
81+
void Add_CpInverseDesignOutput();
8382

8483
/*!
85-
* \brief Set CP inverse design output field values
86-
* \param[in] solver - The container holding all solution data.
84+
* \brief Set CP inverse design output field values (and also into the solver).
85+
* \param[in,out] solver - The container holding all solution data.
8786
* \param[in] geometry - Geometrical definition of the problem.
8887
* \param[in] config - Definition of the particular problem.
8988
*/
90-
void Set_CpInverseDesign(CSolver *solver, CGeometry *geometry, CConfig *config);
89+
void Set_CpInverseDesign(CSolver *solver, const CGeometry *geometry, const CConfig *config);
9190

9291
/*!
9392
* \brief Compute value of the Q criteration for vortex idenfitication

SU2_CFD/src/output/CFlowCompOutput.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void CFlowCompOutput::SetHistoryOutputFields(CConfig *config){
279279

280280
/*--- Add Cp diff fields ---*/
281281

282-
Add_CpInverseDesignOutput(config);
282+
Add_CpInverseDesignOutput();
283283

284284
}
285285

SU2_CFD/src/output/CFlowOutput.cpp

Lines changed: 58 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -695,123 +695,96 @@ void CFlowOutput::SetRotatingFrameCoefficients(CConfig *config, CSolver *flow_so
695695
}
696696

697697

698-
void CFlowOutput::Add_CpInverseDesignOutput(CConfig *config){
698+
void CFlowOutput::Add_CpInverseDesignOutput(){
699699

700700
AddHistoryOutput("INVERSE_DESIGN_PRESSURE", "Cp_Diff", ScreenOutputFormat::FIXED, "CP_DIFF", "Cp difference for inverse design");
701701

702702
}
703703

704-
void CFlowOutput::Set_CpInverseDesign(CSolver *solver, CGeometry *geometry, CConfig *config){
705-
706-
unsigned short iMarker, icommas, Boundary;
707-
unsigned long iVertex, iPoint, (*Point2Vertex)[2], nPointLocal = 0, nPointGlobal = 0;
708-
su2double XCoord, YCoord, ZCoord, Pressure, PressureCoeff = 0, Cp, CpTarget, *Normal = nullptr, Area, PressDiff = 0.0;
709-
bool *PointInDomain;
710-
string text_line, surfCp_filename;
711-
ifstream Surface_file;
704+
void CFlowOutput::Set_CpInverseDesign(CSolver *solver, const CGeometry *geometry, const CConfig *config){
712705

713706
/*--- Prepare to read the surface pressure files (CSV) ---*/
714707

715-
surfCp_filename = config->GetUnsteady_FileName("TargetCp", (int)curTimeIter, ".dat");
716-
717-
/*--- Read the surface pressure file ---*/
708+
const auto surfCp_filename = config->GetUnsteady_FileName("TargetCp", curTimeIter, ".dat");
718709

719-
string::size_type position;
710+
/*--- Read the surface pressure file, on the first inner iteration. ---*/
720711

712+
ifstream Surface_file;
721713
Surface_file.open(surfCp_filename);
722714

723-
if (!(Surface_file.fail())) {
724-
725-
nPointLocal = geometry->GetnPoint();
726-
SU2_MPI::Allreduce(&nPointLocal, &nPointGlobal, 1, MPI_UNSIGNED_LONG, MPI_SUM, SU2_MPI::GetComm());
727-
728-
Point2Vertex = new unsigned long[nPointGlobal][2];
729-
PointInDomain = new bool[nPointGlobal];
730-
731-
for (iPoint = 0; iPoint < nPointGlobal; iPoint ++)
732-
PointInDomain[iPoint] = false;
733-
734-
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
735-
Boundary = config->GetMarker_All_KindBC(iMarker);
736-
737-
if (config->GetSolid_Wall(iMarker) || (Boundary == NEARFIELD_BOUNDARY)) {
738-
for (iVertex = 0; iVertex < geometry->GetnVertex(iMarker); iVertex++) {
739-
740-
/*--- The Pressure file uses the global numbering ---*/
741-
742-
iPoint = geometry->nodes->GetGlobalIndex(geometry->vertex[iMarker][iVertex]->GetNode());
743-
744-
if (geometry->vertex[iMarker][iVertex]->GetNode() < geometry->GetnPointDomain()) {
745-
Point2Vertex[iPoint][0] = iMarker;
746-
Point2Vertex[iPoint][1] = iVertex;
747-
PointInDomain[iPoint] = true;
748-
solver->SetCPressureTarget(iMarker, iVertex, 0.0);
749-
}
750-
751-
}
752-
}
753-
}
715+
if (!Surface_file.good()) {
716+
solver->SetTotal_CpDiff(0.0);
717+
SetHistoryOutputValue("INVERSE_DESIGN_PRESSURE", 0.0);
718+
return;
719+
}
754720

721+
if ((config->GetInnerIter() == 0) || config->GetDiscrete_Adjoint()) {
722+
string text_line;
755723
getline(Surface_file, text_line);
756724

757725
while (getline(Surface_file, text_line)) {
758-
for (icommas = 0; icommas < 50; icommas++) {
759-
position = text_line.find( ",", 0 );
760-
if (position!=string::npos) text_line.erase (position,1);
761-
}
762-
stringstream point_line(text_line);
763-
764-
if (geometry->GetnDim() == 2) point_line >> iPoint >> XCoord >> YCoord >> Pressure >> PressureCoeff;
765-
if (geometry->GetnDim() == 3) point_line >> iPoint >> XCoord >> YCoord >> ZCoord >> Pressure >> PressureCoeff;
766-
767-
if (PointInDomain[iPoint]) {
768-
769-
/*--- Find the vertex for the Point and Marker ---*/
770-
771-
iMarker = Point2Vertex[iPoint][0];
772-
iVertex = Point2Vertex[iPoint][1];
773-
774-
solver->SetCPressureTarget(iMarker, iVertex, PressureCoeff);
775-
726+
/*--- remove commas ---*/
727+
for (auto& c : text_line) if (c == ',') c = ' ';
728+
stringstream point_line(text_line);
729+
730+
/*--- parse line ---*/
731+
unsigned long iPointGlobal;
732+
su2double XCoord, YCoord, ZCoord=0, Pressure, PressureCoeff;
733+
734+
point_line >> iPointGlobal >> XCoord >> YCoord;
735+
if (nDim == 3) point_line >> ZCoord;
736+
point_line >> Pressure >> PressureCoeff;
737+
738+
const auto iPoint = geometry->GetGlobal_to_Local_Point(iPointGlobal);
739+
740+
/*--- If the point is on this rank set the Cp to associated vertices
741+
* (one point may be shared by multiple vertices). ---*/
742+
if (iPoint >= 0) {
743+
bool set = false;
744+
for (auto iMarker = 0u; iMarker < geometry->GetnMarker(); ++iMarker) {
745+
const auto iVertex = geometry->nodes->GetVertex(iPoint, iMarker);
746+
747+
if (iVertex >= 0) {
748+
solver->SetCPressureTarget(iMarker, iVertex, PressureCoeff);
749+
set = true;
750+
}
751+
}
752+
if (!set)
753+
cout << "WARNING: In file " << surfCp_filename << ", point " << iPointGlobal << " is not a vertex." << endl;
776754
}
777-
778755
}
756+
}
779757

780-
Surface_file.close();
781-
782-
delete [] Point2Vertex;
783-
delete [] PointInDomain;
758+
/*--- Compute the pressure difference. ---*/
784759

785-
/*--- Compute the pressure difference ---*/
760+
su2double PressDiff = 0.0;
786761

787-
PressDiff = 0.0;
788-
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
789-
Boundary = config->GetMarker_All_KindBC(iMarker);
762+
for (auto iMarker = 0u; iMarker < geometry->GetnMarker(); ++iMarker) {
790763

791-
if (config->GetSolid_Wall(iMarker) || (Boundary == NEARFIELD_BOUNDARY)) {
792-
for (iVertex = 0; iVertex < geometry->GetnVertex(iMarker); iVertex++) {
764+
const auto Boundary = config->GetMarker_All_KindBC(iMarker);
793765

794-
Normal = geometry->vertex[iMarker][iVertex]->GetNormal();
766+
if (config->GetSolid_Wall(iMarker) || (Boundary == NEARFIELD_BOUNDARY)) {
767+
for (auto iVertex = 0ul; iVertex < geometry->GetnVertex(iMarker); iVertex++) {
795768

796-
Cp = solver->GetCPressure(iMarker, iVertex);
797-
CpTarget = solver->GetCPressureTarget(iMarker, iVertex);
769+
const auto iPoint = geometry->vertex[iMarker][iVertex]->GetNode();
770+
if (!geometry->nodes->GetDomain(iPoint)) continue;
798771

799-
Area = GeometryToolbox::Norm(nDim, Normal);
772+
const auto Cp = solver->GetCPressure(iMarker, iVertex);
773+
const auto CpTarget = solver->GetCPressureTarget(iMarker, iVertex);
800774

801-
PressDiff += Area * (CpTarget - Cp) * (CpTarget - Cp);
802-
}
775+
const auto Normal = geometry->vertex[iMarker][iVertex]->GetNormal();
776+
const auto Area = GeometryToolbox::Norm(nDim, Normal);
803777

778+
PressDiff += Area * pow(CpTarget-Cp, 2);
804779
}
805780
}
806-
807-
su2double MyPressDiff = PressDiff;
808-
SU2_MPI::Allreduce(&MyPressDiff, &PressDiff, 1, MPI_DOUBLE, MPI_SUM, SU2_MPI::GetComm());
809781
}
782+
su2double tmp = PressDiff;
783+
SU2_MPI::Allreduce(&tmp, &PressDiff, 1, MPI_DOUBLE, MPI_SUM, SU2_MPI::GetComm());
810784

811-
/*--- Update the total Cp difference coeffient ---*/
785+
/*--- Update the total Cp difference coeffient. ---*/
812786

813787
solver->SetTotal_CpDiff(PressDiff);
814-
815788
SetHistoryOutputValue("INVERSE_DESIGN_PRESSURE", PressDiff);
816789

817790
}
@@ -820,7 +793,7 @@ su2double CFlowOutput::GetQ_Criterion(su2double** VelocityGradient) const {
820793

821794
/*--- Make a 3D copy of the gradient so we do not have worry about nDim ---*/
822795

823-
su2double Grad_Vel[3][3] = {{0.0, 0.0, 0.0},{0.0, 0.0, 0.0},{0.0, 0.0, 0.0}};
796+
su2double Grad_Vel[3][3] = {{0.0}};
824797

825798
for (unsigned short iDim = 0; iDim < nDim; iDim++)
826799
for (unsigned short jDim = 0 ; jDim < nDim; jDim++)

SU2_CFD/src/output/CNEMOCompOutput.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ void CNEMOCompOutput::SetHistoryOutputFields(CConfig *config){
284284

285285
/*--- Add Cp diff fields ---*/
286286

287-
Add_CpInverseDesignOutput(config);
287+
Add_CpInverseDesignOutput();
288288

289289
}
290290

0 commit comments

Comments
 (0)