-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCONVERT2SU2.cpp
More file actions
200 lines (185 loc) · 6.03 KB
/
CONVERT2SU2.cpp
File metadata and controls
200 lines (185 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* CONVERT2SU2 program, version 1.0
The program reads in a PLOT3D file containing the mesh
(it can be either a direct output from CONSTRUCT2D or a coarsened mesh file from COARSEN_P2D)
and two optional arguments:
1) scale_factor (default: 1.0), specifying any grid scaling applied to the output.
2) rot_angle (default: 0.0), specifying any grid rotation (along -Z axis) applied to the output.
The code then provides, as an output, a new grid file in the native format used by the
Stanford University Unstructured (SU2) suite (https://su2code.github.io/), which can be used
for example to simulate the flow-field around the selected aerofoil using its flow solver module (SU2_CFD).
If any of the optional argument are specified differently from defaults, the modified PLOT3D grid is also written.
Copyright (C) 2024 Mauro Minervino
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
int ni=0;
int nk=0;
double scale_factor=1;
double rot_angle=0;
if (argc > 2) {
scale_factor = atof(argv[2]);
cout << "scaling factor:" << scale_factor << "\n";
}
if (argc > 3) {
rot_angle = atof(argv[3]);
cout << "rotation angle:" << rot_angle << " degrees\n";
rot_angle = rot_angle*M_PI/180.0;
}
ifstream in_file(argv[1]);
if (in_file.is_open()) {
string line;
int i=0;
int m=0;
getline(in_file, line);
i = line.find_first_of("123456789");
m = line.find_last_of("123456789");
line=line.substr(i,m+1);
i = line.find_first_of("\t ");
m = line.find_last_of("\t ");
ni = atof(line.substr(0,i).data());
nk = atof(line.substr(m+1).data());
cout << "ni= " << ni << " ; nk= " << nk << "\n";
in_file.close();
}
vector < vector < vector < double > > > values(ni, vector < vector < double > > (nk, vector < double > (2,NAN)));
int ntot=ni*nk*2;
vector < double > in_values(ntot,NAN);
in_file.open(argv[1]);
if (in_file.is_open()) {
string line;
int i=0;
getline(in_file, line); //skip first line
while (getline(in_file, line)) {
in_values[i] = atof(line.data());
i++;
}
in_file.close();
}
for (int v = 0; v < 2; v++) {
for (int k = 0; k < nk; k++) {
for (int i = 0; i < ni; i++) {
values[i][k][v] = in_values[ni*nk*v+ni*k+i];
}
}
}
if (scale_factor!=1) {
for (int v = 0; v < 2; v++) {
for (int k = 0; k < nk; k++) {
for (int i = 0; i < ni; i++) {
values[i][k][v] = values[i][k][v]*scale_factor;
}
}
}
}
if (rot_angle!=0) {
double tmp=NAN;
for (int k = 0; k < nk; k++) {
for (int i = 0; i < ni; i++) {
tmp=values[i][k][0];
values[i][k][0] = values[i][k][0]*cos(rot_angle)+values[i][k][1]*sin(rot_angle);
values[i][k][1] = -tmp*sin(rot_angle)+values[i][k][1]*cos(rot_angle);
}
}
}
if ((rot_angle!=0)||(scale_factor!=1)) {
FILE* pFile;
string OutFileName=argv[1];
OutFileName.resize(OutFileName.length()-4);
OutFileName=OutFileName+"_mod.p3d";
pFile = fopen (OutFileName.c_str(),"w");
fprintf(pFile, "%d\t%d\n",ni,nk);
for (int v = 0; v < 2; v++) {
for (int k = 0; k < nk; k++) {
for (int i = 0; i < ni; i++) {
fprintf(pFile, "%15.8E\n",values[i][k][v]);
}
}
}
fclose (pFile);
}
FILE* pFile;
string OutFileName=argv[1];
OutFileName.resize(OutFileName.length()-3);
OutFileName=OutFileName+"su2";
pFile = fopen (OutFileName.c_str(),"w");
fprintf(pFile, "%s\n","NDIME= 2");
int nskip=0;
for (int k = 0; k < nk; k++) {
for (int i = 0; i < ni; i++) {
if ((k==0) && (values[i][k][0]>=1) && (i>ni/2)) {
nskip++;
}
}
}
fprintf(pFile, "%s%d\n","NPOIN= ",ni*nk-nskip);
for (int k = 0; k < nk; k++) {
for (int i = 0; i < ni; i++) {
if ((k==0) && (values[i][k][0]>=1) && (i>ni/2)) {
//do nothing
} else {
fprintf(pFile, "%15.8E\t%15.8E\n",values[i][k][0],values[i][k][1]);
}
}
}
cout << "nskip = " << nskip << "\n";
fprintf(pFile, "%s%d\n","NELEM= ",(ni-1)*(nk-1));
for (int k = 0; k < (nk-1); k++) {
for (int i = 0; i < (ni-1); i++) {
if (k==0) {
if (i<ni-nskip-1) {
fprintf(pFile, "%d\t%d\t%d\t%d\t%d\n",9, i, i+1, ni-nskip+i+1, ni-nskip+i);
} else {
if (i==ni-nskip-1) {
fprintf(pFile, "%d\t%d\t%d\t%d\t%d\n",9, i, ni-2-i, ni-nskip+i+1, ni-nskip+i);
} else {
fprintf(pFile, "%d\t%d\t%d\t%d\t%d\n",9, ni-1-i, ni-2-i, ni-nskip+i+1, ni-nskip+i);
}
}
} else {
fprintf(pFile, "%d\t%d\t%d\t%d\t%d\n",9,ni-nskip+(k-1)*ni+i, ni-nskip+(k-1)*ni+i+1, ni-nskip+k*ni+i+1, ni-nskip+k*ni+i);
}
}
}
fprintf(pFile, "%s%d\n","NMARK= ",2);
fprintf(pFile, "%s\n","MARKER_TAG= FF");
fprintf(pFile, "%s%d\n","MARKER_ELEMS= ",ni-1+nk-1+nk-1);
for (int k = 0; k < nk-1; k++) {
if (k==0) {
fprintf(pFile, "%d\t%d\t%d\n",3, 0, ni-nskip);
fprintf(pFile, "%d\t%d\t%d\n",3, 0, ni-nskip+ni-1);
} else {
fprintf(pFile, "%d\t%d\t%d\n",3, ni-nskip+(k-1)*ni, ni-nskip+k*ni);
fprintf(pFile, "%d\t%d\t%d\n",3, ni-nskip+k*ni-1, ni-nskip+(k+1)*ni-1);
}
}
for (int i = 0; i < ni-1; i++) {
fprintf(pFile, "%d\t%d\t%d\n",3, ni*nk-nskip-1-(ni-1)+i, ni*nk-nskip-1-(ni-1)+i+1);
}
fprintf(pFile, "%s\n","MARKER_TAG= AIRFOIL");
fprintf(pFile, "%s%d\n","MARKER_ELEMS= ",ni-2*nskip+2-1);
for (int i = nskip-1; i < ni-nskip; i++) {
if (i==ni-nskip-1) {
fprintf(pFile, "%d\t%d\t%d\n",3, i, nskip-1);
} else {
fprintf(pFile, "%d\t%d\t%d\n",3, i, i+1);
}
}
fclose (pFile);
}