-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFM2Cantera.C
More file actions
191 lines (168 loc) · 5.21 KB
/
FM2Cantera.C
File metadata and controls
191 lines (168 loc) · 5.21 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
using namespace std;
map<string, string> names;
int main(int argc, char *argv[])
{
void species_mapping();
double FM2Can(const double &c, const string &na, const string &s, const string &n2);
double chi_f=stod(argv[1]);
// read the species name mapping between FlameMaster and Chemkin/Cantera
species_mapping();
ifstream fstrm("input");
// check input file
if(!fstrm.is_open())
throw runtime_error("File input not found");
// read information from input
string fuel,chi,TOF;
getline(fstrm,fuel);
getline(fstrm,TOF);
vector<double> chi_st;
while(getline(fstrm,chi)){
chi_st.push_back(FM2Can(chi_f,fuel,chi,TOF));
}
// fsrtm.close();
// write the chi_param.include file
ofstream chi_out("tables/chi_param.include");
chi_out << "chi_param" << endl;
chi_out << chi_st.size() << endl;
chi_out << "(" << endl;
for(auto &r : chi_st)
chi_out << scientific << r << endl;
chi_out << ");";
return 0;
}
void species_mapping()
{
// mixture fraction and temperature
// names.insert({"Z","Z"});
// names.insert({"temperature","T"});
// read in species translate file
ifstream fstrm;
fstrm.open("speciestranslated");
if(!fstrm.is_open())
throw runtime_error("File speciestranslated not found");
string line;
while(getline(fstrm, line)){
istringstream sstrm(line);
string name_FM, name_CK;
sstrm >> name_CK >> name_FM;
names.insert({name_FM,name_CK});
}
// output to check
// for ( const auto &w : names )
// cout << w.first << " " << w.second << endl;
}
double FM2Can(const double &chi_f, const string &fuel, const string &chi, const string &TOF)
{
vector<double> data_read(ifstream &fstrm, const int &pts);
string filename = fuel+chi+TOF;
ifstream FM_file(filename);
if(!FM_file.is_open())
throw runtime_error("File "+filename+" not found");
cout << "Reading " << filename << endl;
int grid_pts;
string line;
while(getline(FM_file,line)){
// the position of gridPoints is 0
if(!line.find("gridPoints")){
grid_pts=stoi(line.substr(line.find_first_of("0123456789")));
// cout << grid_pts << endl;
getline(FM_file,line);
getline(FM_file,line);
break;
}
}
// data
vector<string> var_name;
vector<vector<double>> all_data;
// mixture fraction
getline(FM_file,line);
var_name.push_back("Z");
all_data.push_back(data_read(FM_file,grid_pts));
// temperature
getline(FM_file,line);
var_name.push_back("T");
all_data.push_back(data_read(FM_file,grid_pts));
// species
for(int i=0;i!=names.size();++i)
{
getline(FM_file,line);
string FM_name=line.substr(line.find("-")+1);
// translate FM_name to CK_name
// cout << names[FM_name] << endl;
var_name.push_back(names[FM_name]);
all_data.push_back(data_read(FM_file,grid_pts));
}
// skip W and ZBilger
getline(FM_file,line);
data_read(FM_file,grid_pts);
getline(FM_file,line);
data_read(FM_file,grid_pts);
// scalar dissipation rate
getline(FM_file,line);
//var_name.push_back("chi");
//all_data.push_back(data_read(FM_file,grid_pts));
var_name.insert(var_name.begin()+2,"chi");
all_data.insert(all_data.begin()+2,data_read(FM_file,grid_pts));
// skip density, lambda, cp, lambdaOverCp
getline(FM_file,line);
data_read(FM_file,grid_pts);
getline(FM_file,line);
data_read(FM_file,grid_pts);
getline(FM_file,line);
data_read(FM_file,grid_pts);
getline(FM_file,line);
data_read(FM_file,grid_pts);
// viscosity
getline(FM_file,line);
var_name.push_back("mu");
all_data.push_back(data_read(FM_file,grid_pts));
// write to Table_chi.csv
// make filename for Cantera tables
ostringstream strm;
strm << stod(chi);
filename = "tables/Table_"+strm.str()+".csv";
cout << "Writing " << filename << endl;
ofstream out_file(filename);
// names of variable
for(int i=0;i!=var_name.size()-1;++i)
out_file << var_name[i] << ',';
out_file << var_name[var_name.size()-1] << endl;
// data
for(int j=0;j!=grid_pts;++j)
{
for(int i=0;i!=var_name.size()-1;++i)
out_file << scientific << all_data[i][j] << ',';
out_file << scientific << all_data[var_name.size()-1][j] << endl;
}
out_file.close();
// check the whether output mixture fraction
if ( stod(chi) == chi_f )
{
out_file.open("tables/Z_param.include");
out_file << "Z_param" << endl;
out_file << all_data[0].size() << endl;
out_file << "(" << endl;
for(auto &r : all_data[0])
out_file << scientific << r << endl;
out_file << ");";
}
return stod(chi);
}
vector<double> data_read(ifstream &fstrm, const int &pts)
{
double data_buff;
string line;
vector<double> var_data;
for(int i=0;i!=pts;++i)
{
fstrm >> data_buff;
var_data.push_back(data_buff);
}
getline(fstrm,line);
return var_data;
}