-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCtr2OF.m
183 lines (153 loc) · 6.15 KB
/
Ctr2OF.m
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
%% Script to convert Cantera results to OpenFOAM fields
% code from: https://github.com/JSqueo299/Python/blob/main/Cantera/premixedFlames/Matlab2OF.m
% read variable names from .csv file
fname = 'adiabatic_flame.csv';
fid = fopen(fname);
vars = textscan(fid,' %s ','Delimiter',',','MultipleDelimsAsOne',1);
fclose(fid);
% read data from Cantera .csv generated file
fid = fopen(fname);
hdr = fgetl(fid);
num = numel(regexp(hdr,',','split'));
Cantera = textscan(fid,repmat('%f ',1,num),'Delimiter',',','headerlines',1,'EmptyValue',0);
fclose(fid); % Close the opened file
N_data = length(Cantera{1});
N_vars = length(Cantera) - 1;
x_Cantera = Cantera{1};
T_Cantera = Cantera{4};
Tu = T_Cantera(1); % unburnt gas T (K)
Tb = T_Cantera(end); % burnt gas T (K)
L = x_Cantera(end); % length of domain (m)
Sl = Cantera{3}(1);% laminar flame speed from Cantera (f.u[0])
% check laminar flame thickness calculation in Python with Matlab
dx_Cantera = diff(x_Cantera);
dT_Cantera = diff(T_Cantera);
Lf = (Tb - Tu) ./ max(dT_Cantera./dx_Cantera) ;
min_dx = Lf ./ 30;
min_cells = L ./ min_dx;
fprintf('Laminar flame speed: Sl = %.15g m/s \n',Sl)
fprintf('Laminar flame thickenss: Lf = %.15g m \n',Lf)
fprintf('Minimum grid spacing required: dx_min = %.15g m \n',min_dx)
fprintf('Minimum # of cells required: %.2f cells \n',min_cells)
varname = string(vars{1}(2:N_vars+1));
varname(1) = 'p';
varname(2) = 'U';
varname(3) = 'T';
% Interpolation
xStart = 0;
xEnd = L;
OF_gridSize = ceil(min_cells);
OF_grid = linspace(xStart,xEnd,OF_gridSize)';
fprintf('OF_gridSize: %.0f \n',OF_gridSize)
for i = 1:N_vars
F = griddedInterpolant(x_Cantera,Cantera{i+1},'spline') ;
data = F(OF_grid);
OF_data{i} = data;
end
% Write data into OpenFOAM format, 0/ folder
tFolder = 0;
mkdir(num2str(tFolder));
cd(num2str(tFolder));
for i = 1:N_vars
fid = fopen(varname(i),'w'); % write permission
fprintf(fid,'/*--------------------------------*- C++ -*----------------------------------*\\\n');
fprintf(fid,'| ========= | |\n');
fprintf(fid,'| \\\\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n');
fprintf(fid,'| \\\\ / O peration | Version: 5.x |\n');
fprintf(fid,'| \\\\ / A nd | Web: www.OpenFOAM.org |\n');
fprintf(fid,'| \\\\/ M anipulation | |\n');
fprintf(fid,'\\*---------------------------------------------------------------------------*/\n');
fprintf(fid,'FoamFile\n');
fprintf(fid,'{\n');
fprintf(fid,' version 2.0;\n');
fprintf(fid,' format ascii;\n');
if strcmp(varname(i),'U')
fprintf(fid,' class volVectorField;\n');
else
fprintf(fid,' class volScalarField;\n');
end
fprintf(fid,' location "%.15g";\n',tFolder);
fprintf(fid,' object %s;\n',varname(i));
fprintf(fid,'}\n');
fprintf(fid,'// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n');
fprintf(fid,'\n');
if strcmp(varname(i),'T')
fprintf(fid,'dimensions [0 0 0 1 0 0 0];\n');
fprintf(fid,'\n');
fprintf(fid,'internalField nonuniform List<scalar>\n');
fprintf(fid,'%d\n',OF_gridSize);
fprintf(fid,'(\n');
fprintf(fid,'%.15f\n',OF_data{i});
fprintf(fid,')\n;\n');
elseif strcmp(varname(i),'U')
fprintf(fid,'dimensions [0 1 -1 0 0 0 0];\n');
fprintf(fid,'\n');
fprintf(fid,'internalField nonuniform List<vector>\n');
fprintf(fid,'%d\n',OF_gridSize);
fprintf(fid,'(\n');
fprintf(fid,'(%.15f 0.0 0.0)\n',OF_data{i});
fprintf(fid,')\n;\n');
elseif strcmp(varname(i),'p')
fprintf(fid,'dimensions [1 -1 -2 0 0 0 0];\n');
fprintf(fid,'\n');
fprintf(fid,'internalField nonuniform List<scalar>\n');
fprintf(fid,'%d\n',OF_gridSize);
fprintf(fid,'(\n');
fprintf(fid,'%.15e\n',OF_data{i});
fprintf(fid,')\n;\n');
else
fprintf(fid,'dimensions [0 0 0 0 0 0 0];\n');
fprintf(fid,'\n');
fprintf(fid,'internalField nonuniform List<scalar>\n');
fprintf(fid,'%d\n',OF_gridSize);
fprintf(fid,'(\n');
fprintf(fid,'%.15e\n',OF_data{i});
fprintf(fid,')\n;\n');
end
fprintf(fid,'\nboundaryField\n');
fprintf(fid,'{\n');
fprintf(fid,' inlet\n');
fprintf(fid,' {\n');
if strcmp(varname(i),'T')
fprintf(fid,' type fixedValue;\n');
%fprintf(fid,' value uniform 300;\n');
fprintf(fid,' value uniform ');
fprintf(fid,'%.8f',Tu);
fprintf(fid,';\n');
elseif strcmp(varname(i),'U')
fprintf(fid,' type fixedValue;\n');
fprintf(fid,' value uniform ( ');
fprintf(fid,'%.8f',Sl);
fprintf(fid,' 0.000000 0.000000 );\n');
elseif strcmp(varname(i),'p')
fprintf(fid,' type zeroGradient;\n');
elseif Cantera{i+1}(1) > 1e-7
fprintf(fid,' type fixedValue;\n');
fprintf(fid,' value uniform ');
fprintf(fid,'%.8f',Cantera{i+1}(1));
fprintf(fid,';\n');
else
fprintf(fid,' type fixedValue;\n');
fprintf(fid,' value uniform 0;\n');
end
fprintf(fid,' }\n');
fprintf(fid,' outlet\n');
fprintf(fid,' {\n');
if strcmp(varname(i),'p')
fprintf(fid,' type waveTransmissive;\n');
fprintf(fid,' gamma 1.4;\n');
else
fprintf(fid,' type zeroGradient;\n');
end
fprintf(fid,' }\n');
fprintf(fid,' wall\n');
fprintf(fid,' {\n');
fprintf(fid,' type empty;\n');
fprintf(fid,' }\n');
fprintf(fid,'}\n');
fprintf(fid,'\n');
fprintf(fid,'\n');
fprintf(fid,'// ************************************************************************* //\n');
fclose(fid);
end
cd ..;