Skip to content

Commit 30b2554

Browse files
committed
simplify a few things
1 parent 813ff06 commit 30b2554

1 file changed

Lines changed: 40 additions & 164 deletions

File tree

SU2_CFD/src/solvers/CFEASolver.cpp

Lines changed: 40 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -337,31 +337,25 @@ void CFEASolver::HybridParallelInitialization(CGeometry* geometry) {
337337

338338
void CFEASolver::Set_ElementProperties(CGeometry *geometry, CConfig *config) {
339339

340-
unsigned long iElem;
341-
unsigned long index;
342-
unsigned long elProperties[4];
340+
const auto iZone = config->GetiZone();
341+
const auto nZone = geometry->GetnZone();
343342

344-
unsigned short iZone = config->GetiZone();
345-
unsigned short nZone = geometry->GetnZone();
346-
347-
bool topology_mode = config->GetTopology_Optimization();
348-
349-
string filename;
350-
ifstream properties_file;
343+
const bool topology_mode = config->GetTopology_Optimization();
351344

352345
element_properties = new CProperty*[nElement];
353346

354347
/*--- Restart the solution from file information ---*/
355348

356-
filename = config->GetFEA_FileName();
349+
auto filename = config->GetFEA_FileName();
357350

358351
/*--- If multizone, append zone name ---*/
359352
if (nZone > 1)
360353
filename = config->GetMultizone_FileName(filename, iZone, ".dat");
361354

362355
if (rank == MASTER_NODE) cout << "Filename: " << filename << "." << endl;
363356

364-
properties_file.open(filename.data(), ios::in);
357+
ifstream properties_file;
358+
properties_file.open(filename);
365359

366360
/*--- In case there is no file, all elements get the same property (0) ---*/
367361

@@ -374,7 +368,7 @@ void CFEASolver::Set_ElementProperties(CGeometry *geometry, CConfig *config) {
374368
SU2_MPI::Error("Topology mode requires an element-based properties file.",CURRENT_FUNCTION);
375369
}
376370

377-
for (iElem = 0; iElem < nElement; iElem++){
371+
for (auto iElem = 0ul; iElem < nElement; iElem++){
378372
element_properties[iElem] = new CElementProperty(FEA_TERM, 0, 0, 0);
379373
}
380374

@@ -385,24 +379,15 @@ void CFEASolver::Set_ElementProperties(CGeometry *geometry, CConfig *config) {
385379

386380
element_based = true;
387381

388-
/*--- In case this is a parallel simulation, we need to perform the
389-
Global2Local index transformation first. ---*/
382+
/*--- In case this is a parallel simulation, we need to perform the Global2Local index transformation first. ---*/
390383

391-
long *Global2Local = new long[geometry->GetGlobal_nElemDomain()];
384+
unordered_map<unsigned long, unsigned long> Global2Local;
392385

393-
/*--- First, set all indices to a negative value by default ---*/
394-
395-
for (iElem = 0; iElem < geometry->GetGlobal_nElemDomain(); iElem++)
396-
Global2Local[iElem] = -1;
397-
398-
/*--- Now fill array with the transform values only for the points in the rank (including halos) ---*/
399-
400-
for (iElem = 0; iElem < nElement; iElem++)
386+
for (auto iElem = 0ul; iElem < nElement; iElem++)
401387
Global2Local[geometry->elem[iElem]->GetGlobalIndex()] = iElem;
402388

403389
/*--- Read all lines in the restart file ---*/
404390

405-
long iElem_Local;
406391
unsigned long iElem_Global_Local = 0, iElem_Global = 0; string text_line;
407392

408393
/*--- The first line is the header ---*/
@@ -420,9 +405,13 @@ void CFEASolver::Set_ElementProperties(CGeometry *geometry, CConfig *config) {
420405
Otherwise, the local index for this node on the current processor
421406
will be returned and used to instantiate the vars. ---*/
422407

423-
iElem_Local = Global2Local[iElem_Global];
408+
auto it = Global2Local.find(iElem_Global);
409+
410+
if (it != Global2Local.end()) {
424411

425-
if (iElem_Local >= 0) {
412+
auto iElem_Local = it->second;
413+
414+
unsigned long elProperties[4], index;
426415

427416
if (config->GetAdvanced_FEAElementBased() || topology_mode){
428417
point_line >> index >> elProperties[0] >> elProperties[1] >> elProperties[2] >> elProperties[3];
@@ -454,63 +443,43 @@ void CFEASolver::Set_ElementProperties(CGeometry *geometry, CConfig *config) {
454443
string("It could be empty lines at the end of the file."), CURRENT_FUNCTION);
455444
}
456445

457-
/*--- Close the restart file ---*/
458-
459-
properties_file.close();
460-
461-
/*--- Free memory needed for the transformation ---*/
462-
463-
delete [] Global2Local;
464-
465446
}
466447

467448
}
468449

469450
void CFEASolver::Set_Prestretch(CGeometry *geometry, CConfig *config) {
470451

471-
unsigned long iPoint;
472-
unsigned long index;
473-
474-
unsigned short iVar;
475-
unsigned short iZone = config->GetiZone();
476-
unsigned short nZone = geometry->GetnZone();
477-
478-
string filename;
479-
ifstream prestretch_file;
480-
452+
const auto iZone = config->GetiZone();
453+
const auto nZone = geometry->GetnZone();
481454

482455
/*--- Restart the solution from file information ---*/
483456

484-
filename = config->GetPrestretch_FEMFileName();
457+
auto filename = config->GetPrestretch_FEMFileName();
485458

486459
/*--- If multizone, append zone name ---*/
487460
if (nZone > 1)
488461
filename = config->GetMultizone_FileName(filename, iZone, ".dat");
489462

490463
if (rank == MASTER_NODE) cout << "Filename: " << filename << "." << endl;
491464

492-
prestretch_file.open(filename.data(), ios::in);
465+
ifstream prestretch_file;
466+
prestretch_file.open(filename);
493467

494468
/*--- In case there is no file ---*/
495469

496470
if (prestretch_file.fail()) {
497471
SU2_MPI::Error(string("There is no FEM prestretch reference file ") + filename, CURRENT_FUNCTION);
498472
}
499473

500-
/*--- In case this is a parallel simulation, we need to perform the
501-
Global2Local index transformation first. ---*/
502-
503-
map<unsigned long,unsigned long> Global2Local;
504-
map<unsigned long,unsigned long>::const_iterator MI;
474+
/*--- Make a global to local map that also covers halo nodes (the one in geometry does not). ---*/
505475

506-
/*--- Now fill array with the transform values only for local points ---*/
476+
unordered_map<unsigned long, unsigned long> Global2Local;
507477

508-
for (iPoint = 0; iPoint < nPointDomain; iPoint++)
478+
for (auto iPoint = 0ul; iPoint < nPoint; ++iPoint)
509479
Global2Local[geometry->nodes->GetGlobalIndex(iPoint)] = iPoint;
510480

511481
/*--- Read all lines in the restart file ---*/
512482

513-
long iPoint_Local;
514483
unsigned long iPoint_Global_Local = 0, iPoint_Global = 0; string text_line;
515484

516485
/*--- The first line is the header ---*/
@@ -523,17 +492,20 @@ void CFEASolver::Set_Prestretch(CGeometry *geometry, CConfig *config) {
523492
/*--- Retrieve local index. If this node from the restart file lives
524493
on the current processor, we will load and instantiate the vars. ---*/
525494

526-
MI = Global2Local.find(iPoint_Global);
527-
if (MI != Global2Local.end()) {
495+
auto it = Global2Local.find(iPoint_Global);
496+
497+
if (it != Global2Local.end()) {
528498

529-
iPoint_Local = Global2Local[iPoint_Global];
499+
auto iPoint_Local = it->second;
530500

531501
su2double Sol[MAXNVAR] = {0.0};
502+
unsigned long index;
532503

533504
if (nDim == 2) point_line >> Sol[0] >> Sol[1] >> index;
534505
if (nDim == 3) point_line >> Sol[0] >> Sol[1] >> Sol[2] >> index;
535506

536-
for (iVar = 0; iVar < nVar; iVar++) nodes->SetPrestretch(iPoint_Local,iVar, Sol[iVar]);
507+
for (unsigned short iVar = 0; iVar < nVar; iVar++)
508+
nodes->SetPrestretch(iPoint_Local, iVar, Sol[iVar]);
537509

538510
iPoint_Global_Local++;
539511
}
@@ -542,99 +514,28 @@ void CFEASolver::Set_Prestretch(CGeometry *geometry, CConfig *config) {
542514

543515
/*--- Detect a wrong solution file ---*/
544516

545-
if (iPoint_Global_Local != nPointDomain) {
517+
if (iPoint_Global_Local != nPoint) {
546518
SU2_MPI::Error(string("The solution file ") + filename + string(" doesn't match with the mesh file!\n") +
547519
string("It could be empty lines at the end of the file."), CURRENT_FUNCTION);
548520
}
549521

550-
/*--- Close the restart file ---*/
551-
552-
prestretch_file.close();
553-
554-
#ifdef HAVE_MPI
555-
/*--- We need to communicate here the prestretched geometry for the halo nodes. ---*/
556-
/*--- We avoid creating a new function as this may be reformatted. ---*/
557-
558-
unsigned short iMarker, MarkerS, MarkerR;
559-
unsigned long iVertex, nVertexS, nVertexR, nBufferS_Vector, nBufferR_Vector;
560-
su2double *Buffer_Receive_U = nullptr, *Buffer_Send_U = nullptr;
561-
562-
int send_to, receive_from;
563-
564-
for (iMarker = 0; iMarker < nMarker; iMarker++) {
565-
566-
if ((config->GetMarker_All_KindBC(iMarker) == SEND_RECEIVE) &&
567-
(config->GetMarker_All_SendRecv(iMarker) > 0)) {
568-
569-
MarkerS = iMarker; MarkerR = iMarker+1;
570-
571-
send_to = config->GetMarker_All_SendRecv(MarkerS)-1;
572-
receive_from = abs(config->GetMarker_All_SendRecv(MarkerR))-1;
573-
574-
nVertexS = geometry->nVertex[MarkerS]; nVertexR = geometry->nVertex[MarkerR];
575-
nBufferS_Vector = nVertexS*nVar; nBufferR_Vector = nVertexR*nVar;
576-
577-
/*--- Allocate Receive and send buffers ---*/
578-
Buffer_Receive_U = new su2double [nBufferR_Vector];
579-
Buffer_Send_U = new su2double[nBufferS_Vector];
580-
581-
/*--- Copy the solution that should be sent ---*/
582-
for (iVertex = 0; iVertex < nVertexS; iVertex++) {
583-
iPoint = geometry->vertex[MarkerS][iVertex]->GetNode();
584-
for (iVar = 0; iVar < nVar; iVar++)
585-
Buffer_Send_U[iVar*nVertexS+iVertex] = nodes->GetPrestretch(iPoint,iVar);
586-
}
587-
588-
/*--- Send/Receive information using Sendrecv ---*/
589-
SU2_MPI::Sendrecv(Buffer_Send_U, nBufferS_Vector, MPI_DOUBLE, send_to, 0,
590-
Buffer_Receive_U, nBufferR_Vector, MPI_DOUBLE, receive_from, 0, MPI_COMM_WORLD, nullptr);
591-
592-
/*--- Deallocate send buffer ---*/
593-
delete [] Buffer_Send_U;
594-
595-
/*--- Do the coordinate transformation ---*/
596-
for (iVertex = 0; iVertex < nVertexR; iVertex++) {
597-
598-
/*--- Find point and its type of transformation ---*/
599-
iPoint = geometry->vertex[MarkerR][iVertex]->GetNode();
600-
601-
/*--- Store received values back into the variable. ---*/
602-
for (iVar = 0; iVar < nVar; iVar++)
603-
nodes->SetPrestretch(iPoint, iVar, Buffer_Receive_U[iVar*nVertexR+iVertex]);
604-
605-
}
606-
607-
/*--- Deallocate receive buffer ---*/
608-
delete [] Buffer_Receive_U;
609-
610-
}
611-
612-
}
613-
#endif
614-
615522
}
616523

617524
void CFEASolver::Set_ReferenceGeometry(CGeometry *geometry, CConfig *config) {
618525

619-
unsigned long iPoint;
620-
621-
unsigned short iVar;
622-
unsigned short iZone = config->GetiZone();
623-
unsigned short file_format = config->GetRefGeom_FileFormat();
624-
625-
string filename;
626-
ifstream reference_file;
627-
526+
const auto iZone = config->GetiZone();
527+
const auto file_format = config->GetRefGeom_FileFormat();
628528

629529
/*--- Restart the solution from file information ---*/
630530

631-
filename = config->GetRefGeom_FEMFileName();
531+
auto filename = config->GetRefGeom_FEMFileName();
632532

633533
/*--- If multizone, append zone name ---*/
634534

635535
filename = config->GetMultizone_FileName(filename, iZone, ".csv");
636536

637-
reference_file.open(filename.data(), ios::in);
537+
ifstream reference_file;
538+
reference_file.open(filename);
638539

639540
/*--- In case there is no file ---*/
640541

@@ -644,24 +545,8 @@ void CFEASolver::Set_ReferenceGeometry(CGeometry *geometry, CConfig *config) {
644545

645546
if (rank == MASTER_NODE) cout << "Filename: " << filename << " and format " << file_format << "." << endl;
646547

647-
/*--- In case this is a parallel simulation, we need to perform the
648-
Global2Local index transformation first. ---*/
649-
650-
long *Global2Local = new long[geometry->GetGlobal_nPointDomain()];
651-
652-
/*--- First, set all indices to a negative value by default ---*/
653-
654-
for (iPoint = 0; iPoint < geometry->GetGlobal_nPointDomain(); iPoint++)
655-
Global2Local[iPoint] = -1;
656-
657-
/*--- Now fill array with the transform values only for local points ---*/
658-
659-
for (iPoint = 0; iPoint < nPointDomain; iPoint++)
660-
Global2Local[geometry->nodes->GetGlobalIndex(iPoint)] = iPoint;
661-
662548
/*--- Read all lines in the restart file ---*/
663549

664-
long iPoint_Local;
665550
unsigned long iPoint_Global_Local = 0, iPoint_Global = 0; string text_line;
666551

667552
/*--- The first line is the header ---*/
@@ -677,7 +562,7 @@ void CFEASolver::Set_ReferenceGeometry(CGeometry *geometry, CConfig *config) {
677562
Otherwise, the local index for this node on the current processor
678563
will be returned and used to instantiate the vars. ---*/
679564

680-
iPoint_Local = Global2Local[iPoint_Global];
565+
auto iPoint_Local = geometry->GetGlobal_to_Local_Point(iPoint_Global);
681566

682567
if (iPoint_Local >= 0) {
683568

@@ -692,7 +577,7 @@ void CFEASolver::Set_ReferenceGeometry(CGeometry *geometry, CConfig *config) {
692577
Sol[2] = PrintingToolbox::stod(point_line[6]);
693578
}
694579

695-
for (iVar = 0; iVar < nVar; iVar++)
580+
for (unsigned short iVar = 0; iVar < nVar; iVar++)
696581
nodes->SetReference_Geometry(iPoint_Local, iVar, Sol[iVar]);
697582

698583
iPoint_Global_Local++;
@@ -707,14 +592,6 @@ void CFEASolver::Set_ReferenceGeometry(CGeometry *geometry, CConfig *config) {
707592
string("It could be empty lines at the end of the file."), CURRENT_FUNCTION);
708593
}
709594

710-
/*--- Close the restart file ---*/
711-
712-
reference_file.close();
713-
714-
/*--- Free memory needed for the transformation ---*/
715-
716-
delete [] Global2Local;
717-
718595
}
719596

720597
void CFEASolver::Set_VertexEliminationSchedule(CGeometry *geometry, const vector<unsigned short>& markers) {
@@ -3387,9 +3264,8 @@ void CFEASolver::ExtractAdjoint_Variables(CGeometry *geometry, CConfig *config)
33873264
if (rank == MASTER_NODE) {
33883265
string filename = config->GetTopology_Optim_FileName();
33893266
ofstream file;
3390-
file.open(filename.c_str());
3267+
file.open(filename);
33913268
for(iElem=0; iElem<nElemDomain; ++iElem) file << rec_buf[iElem] << "\n";
3392-
file.close();
33933269
}
33943270

33953271
delete [] send_buf;

0 commit comments

Comments
 (0)