3030const string CCGNSFileWriter::fileExt = " .cgns" ;
3131
3232CCGNSFileWriter::CCGNSFileWriter (string valFileName, CParallelDataSorter* valDataSorter, bool isSurf)
33- : CFileWriter(std::move(valFileName), valDataSorter, fileExt) {
34- isSurface = isSurf;
35- }
33+ : CFileWriter(std::move(valFileName), valDataSorter, fileExt), isSurface(isSurf) {}
3634
3735void CCGNSFileWriter::Write_Data () {
3836#ifdef HAVE_CGNS
@@ -62,7 +60,7 @@ void CCGNSFileWriter::Write_Data() {
6260 /* --- Initialize and write fields. ---*/
6361 InitializeFields ();
6462
65- auto & fieldNames = dataSorter->GetFieldNames ();
63+ const auto & fieldNames = dataSorter->GetFieldNames ();
6664 for (unsigned long i = nDim; i < fieldNames.size (); ++i) {
6765 WriteField (i, fieldNames[i]);
6866 }
@@ -85,15 +83,8 @@ void CCGNSFileWriter::InitializeMeshFile() {
8583 GlobalPoint = static_cast <cgsize_t >(dataSorter->GetnPointsGlobal ());
8684 cumulative = 0 ;
8785
88- if (typeid (dataPrecision) == typeid (float ))
89- dataType = RealSingle;
90- else if (typeid (dataPrecision) == typeid (double ))
91- dataType = RealDouble;
92- else
93- SU2_MPI::Error (" Wrong data type in CGNS output" , CURRENT_FUNCTION);
94-
9586 /* --- If surface file cell dimension is decreased. ---*/
96- auto nCell = static_cast <int >(nDim - isSurface);
87+ const auto nCell = static_cast <int >(nDim - isSurface);
9788
9889 if (rank == MASTER_NODE) {
9990 /* --- Remove the previous file if present. ---*/
@@ -118,8 +109,7 @@ void CCGNSFileWriter::InitializeMeshFile() {
118109
119110void CCGNSFileWriter::WriteField (int iField, const string& FieldName) {
120111 /* --- Check if field is coordinate. ---*/
121- bool isCoord = false ;
122- if (FieldName == " CoordinateX" || FieldName == " CoordinateY" || FieldName == " CoordinateZ" ) isCoord = true ;
112+ const bool isCoord = iField < nDim;
123113
124114 /* --- Create send buffer. ---*/
125115 sendBufferField.resize (nLocalPoints);
@@ -128,20 +118,18 @@ void CCGNSFileWriter::WriteField(int iField, const string& FieldName) {
128118 sendBufferField[iPoint] = static_cast <dataPrecision>(dataSorter->GetData (iField, iPoint));
129119 }
130120
131- #ifdef HAVE_MPI
132121 if (rank != MASTER_NODE) {
133122 SU2_MPI::Send (sendBufferField.data (), nLocalPoints * sizeof (dataPrecision), MPI_CHAR, MASTER_NODE, 0 ,
134123 SU2_MPI::GetComm ());
135124 return ;
136125 }
137- #endif
138126
139127 /* --- Coordinate vector is written in blocks, one for each process. ---*/
140128 cgsize_t nodeBegin = 1 ;
141129 cgsize_t nodeEnd = static_cast <cgsize_t >(nLocalPoints);
142130
143131 if (isCoord) {
144- int CoordinateNumber = iField + 1 ;
132+ int CoordinateNumber;
145133 CallCGNS (cg_coord_partial_write (cgnsFileID, cgnsBase, cgnsZone, dataType, FieldName.c_str (), &nodeBegin, &nodeEnd,
146134 sendBufferField.data (), &CoordinateNumber));
147135 } else {
@@ -150,20 +138,20 @@ void CCGNSFileWriter::WriteField(int iField, const string& FieldName) {
150138 &nodeEnd, sendBufferField.data (), &fieldNumber));
151139 }
152140
153- #ifdef HAVE_MPI
154141 for (int i = 0 ; i < size; ++i) {
155142 if (i == MASTER_NODE) continue ;
143+ /* --- In CGNS numbering starts form 1 and ranges are inclusive ---*/
156144 nodeBegin = static_cast <cgsize_t >(dataSorter->GetnPointCumulative (i) + 1 );
157145 nodeEnd = static_cast <cgsize_t >(dataSorter->GetnPointCumulative (i + 1 ));
158146
159- int recvSize = static_cast <int >(nodeEnd - nodeBegin + 1 );
147+ const auto recvSize = static_cast <int >(nodeEnd - nodeBegin + 1 );
160148 recvBufferField.resize (recvSize);
161149
162150 SU2_MPI::Recv (recvBufferField.data (), recvSize * sizeof (dataPrecision), MPI_CHAR, i, 0 , SU2_MPI::GetComm (),
163151 MPI_STATUS_IGNORE);
164152 if (recvSize <= 0 ) continue ;
165153 if (isCoord) {
166- int CoordinateNumber = iField + 1 ;
154+ int CoordinateNumber;
167155 CallCGNS (cg_coord_partial_write (cgnsFileID, cgnsBase, cgnsZone, dataType, FieldName.c_str (), &nodeBegin, &nodeEnd,
168156 recvBufferField.data (), &CoordinateNumber));
169157 } else {
@@ -172,81 +160,71 @@ void CCGNSFileWriter::WriteField(int iField, const string& FieldName) {
172160 &nodeBegin, &nodeEnd, recvBufferField.data (), &fieldNumber));
173161 }
174162 }
175- #endif
176163}
177164
178165void CCGNSFileWriter::WriteConnectivity (GEO_TYPE type, const string& SectionName) {
179- unsigned long nTotElem = dataSorter->GetnElemGlobal (type);
180- if (nTotElem > 0 ) {
181- /* --- Create a new CGNS node to store connectivity. ---*/
182- ElementType_t elementType = GetCGNSType (type);
166+ const auto nTotElem = dataSorter->GetnElemGlobal (type);
167+ if (nTotElem == 0 ) return ;
183168
184- cgsize_t firstElem = cumulative + 1 ;
185- cgsize_t endElem = cumulative + static_cast < cgsize_t >(nTotElem );
169+ /* --- Create a new CGNS node to store connectivity. --- */
170+ const auto elementType = GetCGNSType (type );
186171
187- int cgnsSection;
188- if (rank == MASTER_NODE)
189- CallCGNS (cg_section_partial_write (cgnsFileID, cgnsBase, cgnsZone, SectionName.c_str (), elementType, firstElem,
190- endElem, 0 , &cgnsSection));
172+ cgsize_t firstElem = cumulative + 1 ;
173+ cgsize_t endElem = cumulative + static_cast <cgsize_t >(nTotElem);
191174
192- /* --- Retrieve element distribution among processes. ---*/
193- unsigned long nLocalElem = dataSorter->GetnElem (type);
175+ int cgnsSection;
176+ if (rank == MASTER_NODE)
177+ CallCGNS (cg_section_partial_write (cgnsFileID, cgnsBase, cgnsZone, SectionName.c_str (), elementType, firstElem,
178+ endElem, 0 , &cgnsSection));
194179
195- #ifdef HAVE_MPI
196- vector<unsigned long > distElem;
197- distElem.resize (size);
180+ /* --- Retrieve element distribution among processes. ---*/
181+ const auto nLocalElem = dataSorter->GetnElem (type);
198182
199- SU2_MPI::Allgather (&nLocalElem, 1 , MPI_UNSIGNED_LONG, distElem. data (), 1 , MPI_UNSIGNED_LONG, SU2_MPI::GetComm () );
183+ vector< unsigned long > distElem (size );
200184
201- firstElem = cumulative + 1 ;
202- endElem = cumulative + static_cast <cgsize_t >(distElem[rank]);
203- #else
204- firstElem = cumulative + 1 ;
205- endElem = cumulative + static_cast <cgsize_t >(nLocalElem);
206- #endif
185+ SU2_MPI::Allgather (&nLocalElem, 1 , MPI_UNSIGNED_LONG, distElem.data (), 1 , MPI_UNSIGNED_LONG, SU2_MPI::GetComm ());
207186
208- /* --- Connectivity is stored in send buffer. ---*/
209- unsigned short nPointsElem = nPointsOfElementType (type);
210- sendBufferConnectivity.resize (nLocalElem * nPointsElem);
187+ firstElem = cumulative + 1 ;
188+ endElem = cumulative + static_cast <cgsize_t >(distElem[rank]);
211189
212- for (unsigned long iElem = 0 ; iElem < nLocalElem; iElem++) {
213- for (unsigned long iPoint = 0 ; iPoint < nPointsElem; iPoint++) {
214- sendBufferConnectivity[iPoint + nPointsElem * iElem] =
215- static_cast <cgsize_t >(dataSorter->GetElem_Connectivity (type, iElem, iPoint));
216- }
217- }
190+ /* --- Connectivity is stored in send buffer. ---*/
191+ const auto nPointsElem = nPointsOfElementType (type);
192+ sendBufferConnectivity.resize (nLocalElem * nPointsElem);
218193
219- #ifdef HAVE_MPI
220- auto bufferSize = static_cast <int >(nLocalElem * nPointsElem * sizeof (cgsize_t ));
221- if (rank != MASTER_NODE) {
222- SU2_MPI::Send (sendBufferConnectivity.data (), bufferSize, MPI_CHAR, MASTER_NODE, 1 , SU2_MPI::GetComm ());
223- return ;
194+ for (unsigned long iElem = 0 ; iElem < nLocalElem; iElem++) {
195+ for (unsigned long iPoint = 0 ; iPoint < nPointsElem; iPoint++) {
196+ sendBufferConnectivity[iPoint + nPointsElem * iElem] =
197+ static_cast <cgsize_t >(dataSorter->GetElem_Connectivity (type, iElem, iPoint));
224198 }
225- # endif
199+ }
226200
227- /* --- Connectivity vector is written in blocks, one for each process. ---*/
228- if (nLocalElem > 0 )
201+ const auto bufferSize = static_cast <int >(nLocalElem * nPointsElem * sizeof (cgsize_t ));
202+ if (rank != MASTER_NODE) {
203+ SU2_MPI::Send (sendBufferConnectivity.data (), bufferSize, MPI_CHAR, MASTER_NODE, 1 , SU2_MPI::GetComm ());
204+ return ;
205+ }
206+
207+ /* --- Connectivity vector is written in blocks, one for each process. ---*/
208+ if (nLocalElem > 0 )
209+ CallCGNS (cg_elements_partial_write (cgnsFileID, cgnsBase, cgnsZone, cgnsSection, firstElem, endElem,
210+ sendBufferConnectivity.data ()));
211+
212+ for (int i = 0 ; i < size; ++i) {
213+ if (i == MASTER_NODE) continue ;
214+ /* --- In CGNS numbering starts form 1 and ranges are inclusive ---*/
215+ firstElem += static_cast <cgsize_t >(distElem[i - 1 ]);
216+ endElem += static_cast <cgsize_t >(distElem[i]);
217+ const auto recvSize = static_cast <int >(endElem - firstElem + 1 );
218+ recvBufferConnectivity.resize (recvSize * nPointsElem);
219+
220+ const auto recvByte = static_cast <int >(recvBufferConnectivity.size () * sizeof (cgsize_t ));
221+ SU2_MPI::Recv (recvBufferConnectivity.data (), recvByte, MPI_CHAR, i, 1 , SU2_MPI::GetComm (), MPI_STATUS_IGNORE);
222+
223+ if (!recvBufferConnectivity.empty ())
229224 CallCGNS (cg_elements_partial_write (cgnsFileID, cgnsBase, cgnsZone, cgnsSection, firstElem, endElem,
230- sendBufferConnectivity.data ()));
231-
232- #ifdef HAVE_MPI
233- for (int i = 0 ; i < size; ++i) {
234- if (i == MASTER_NODE) continue ;
235- firstElem += static_cast <cgsize_t >(distElem[i - 1 ]);
236- endElem += static_cast <cgsize_t >(distElem[i]);
237- int recvSize = static_cast <int >(endElem - firstElem + 1 );
238- recvBufferConnectivity.resize (recvSize * nPointsElem);
239-
240- auto recvByte = static_cast <int >(recvBufferConnectivity.size () * sizeof (cgsize_t ));
241- SU2_MPI::Recv (recvBufferConnectivity.data (), recvByte, MPI_CHAR, i, 1 , SU2_MPI::GetComm (), MPI_STATUS_IGNORE);
242-
243- if (!recvBufferConnectivity.empty ())
244- CallCGNS (cg_elements_partial_write (cgnsFileID, cgnsBase, cgnsZone, cgnsSection, firstElem, endElem,
245- recvBufferConnectivity.data ()));
246- }
247- #endif
248- cumulative += static_cast <cgsize_t >(nTotElem);
225+ recvBufferConnectivity.data ()));
249226 }
227+ cumulative += static_cast <cgsize_t >(nTotElem);
250228}
251229
252230void CCGNSFileWriter::InitializeFields () {
0 commit comments