FILE PARSING OF NASA S THERMAL DATA AND CALCULATION OF VARIOUS PARAMETERS
FILE PARSING
Often in the real world, a large amount of data which is used for development purpose are stored in .txt or.DAT file formats but these files contain very large amounts of data which is difficult to enter them back manually and make use of them
BUT
These files are stored in a pattern which when figured out can be inputted or converted easily into usable data with lines of code, this process is called file parsing
Now to give u an example
PART 1:
let's consider this data
THERMO
300.000 1000.000 5000.000
! GRI-Mech Version 3.0 Thermodynamics released 7/30/99
! NASA Polynomial format for CHEMKIN-II
! see README file for disclaimer
this data consists of coefficients to calculate specific heat, enthalpy and entropy
now if we start inputting this data manually like
global low temp =200
global mid temp=1000
global high temp=5000
a1=2.5694e+00
so to overcome that we need to observe a pattern
so if we see here
- in the first line, we have the name of the file
- in the next three lines, we have a description of the data
to get this data we are going to write a code in Matlab
f1=fopen(\'THERMO.DAT\',\'r\')
fgetl(f1)
a=fgetl(f1)
b=strsplit(a,\' \')
global_low_temperature=str2num(b{2})
global_mid_temperature=str2num(b{3})
global_high_temperature=str2num(b{4})
for i=1:3
a=fgetl(f1)
end
The first line of the code
f1=fopen(\'THERMO.DAT\',\'r\') --is used to open the file in which the data is stored and assign it to a variable f1,\'r\' represents the permission to read
second line
fgetl(f1) is a command used to get a line (we have to keep in mind that the number of times we use this command is the line number which it is going to print out, so if we have used this command 5 times on the sixth time it's going to get the sixth line)
third line
a=fgetl(f1); is used to load the second line and store it in variable 'a' (as it contains some of the usable data)
fourth line
b=strsplit(a,\' \') splits the string \'a\' into different string where ever there is a \'space\' with the command \'strsplit\' and stores the elements in array \'b\'
fifth line
global_low_temperature=str2num(b{2}); converts the string element which is stored at index of the array \'b\' into a integer value and store them into the variable
similarly sixth, the seventh line
8th,8th,10 is a loop is used to get the unwanted description
for 5TH LINE OF THE DATA, we observe useful content which is written in a specific pattern which repeats itself
O L 1/90O 1 G 200.000 3500.000 1000.000 1
2.56942078E+00-8.59741137E-05 4.19484589E-08-1.00177799E-11 1.22833691E-15 2
2.92175791E+04 4.78433864E+00 3.16826710E+00-3.27931884E-03 6.64306396E-06 3
-6.12806624E-09 2.11265971E-12 2.91222592E+04 2.05193346E+00 4
O2 TPIS89O 2 G 200.000 3500.000 1000.000 1
3.28253784E+00 1.48308754E-03-7.57966669E-07 2.09470555E-10-2.16717794E-14 2
-1.08845772E+03 5.45323129E+00 3.78245636E+00-2.99673416E-03 9.84730201E-06 3
-9.68129509E-09 3.24372837E-12-1.06394356E+03 3.65767573E+00 4
- The data starts with the species name and ends with local temp data
to get this data we can write a code
the first line consisting of different types of data like characters , integers so
c=fgetl(f1);
b=strsplit(c);
species=string(b(1));
llt=str2num(b{length(b)-3});
lmt=str2num(b{length(b)-1});
lht=str2num(b{length(b)-2});
here
in 1st line
c=fgetl(f1); we are loading the data from file and store in the variable in c
2nd line
b=strsplit(c); splits the line into cell elements and store them into an array \'b\'
3rd line
species(i)=string(b(1)); will convert cell elements to string elements and store them in the species variable, here b(1) represents the first element of array b
4th,5th,6th line
llt=str2num(b{length(b)-3});
lmt=str2num(b{length(b)-1});
lht=str2num(b{length(b)-2}); are used to convert string elements to integer values using\'str2num\' command
2.56942078E+00-8.59741137E-05 4.19484589E-08-1.00177799E-11 1.22833691E-15 2
2.92175791E+04 4.78433864E+00 3.16826710E+00-3.27931884E-03 6.64306396E-06 3
-6.12806624E-09 2.11265971E-12 2.91222592E+04 2.05193346E+00 4
these lines of data contain coefficients to retrieve them
c=fgetl(f1)
s=findstr(c,\'E\');
a1=str2num(c(1:s(1)+3));
a2=str2num(c(s(1)+4:s(2)+3));
a3=str2num(c(s(2)+4:s(3)+3));
a4=str2num(c(s(3)+4:s(4)+3));
a5=str2num(c(s(4)+4:s(5)+3));
here in 2nd line
s=findstr(c,\'E\'); we are finding a particular string \'E\' as it appears in every coefficient and notes the index of array \'c\' at which they appear and store them in another array \'s\'
3rd line
a1(i)=str2num(c(1:s(1)+3)); converts the strings into integers ,here c(1:s(1)+3) represents the string elements which lie in between 1 and (s(1)+3) as s(1) stores values of the indexes of \'E\' in string \'c\'
similarly 4th,5th,6th,7th
this code applies to the next 2 lines of the data also
PART 2:
But, what if we contain a large file of data which is in a pattern and repeats itself in a particular sequence
here comes the use of for loops
we can use the above codes and put them in a for loop, but this time them variables are going to arrays
so here is the code for repetitive data blocks
for i=1:53
for j=1:4
c=fgetl(f1);
if j==1
b=strsplit(c);
species(i)=string(b(1));
llt(i)=str2num(b{length(b)-3});
lmt(i)=str2num(b{length(b)-1});
lht(i)=str2num(b{length(b)-2});
elseif (j==2)
s=findstr(c,\'E\');
a1(i)=str2num(c(1:s(1)+3));
a2(i)=str2num(c(s(1)+4:s(2)+3));
a3(i)=str2num(c(s(2)+4:s(3)+3));
a4(i)=str2num(c(s(3)+4:s(4)+3));
a5(i)=str2num(c(s(4)+4:s(5)+3));
elseif (j==3)
s=findstr(c,\'E\');
a6(i)=str2num(c(1:s(1)+3));
a7(i)=str2num(c(s(1)+4:s(2)+3));
a8(i)=str2num(c(s(2)+4:s(3)+3));
a9(i)=str2num(c(s(3)+4:s(4)+3));
a10(i)=str2num(c(s(4)+4:s(5)+3));
else (j==4)
s=findstr(c,\'E\');
a11(i)=str2num(c(1:s(1)+3));
a12(i)=str2num(c(s(1)+4:s(2)+3));
a13(i)=str2num(c(s(2)+4:s(3)+3));
a14(i)=str2num(c(s(3)+4:s(4)+3));
end
end
end
by doing this we have successfully read the entire data file and allocated the values in a meaning full fashion and store them in variables
PAR 3:
Coming to the calculation part
here we are going to calculate different parameters sung the data we have parsed
element=input(\'enter the element name : \',\'s\')
i=find(species==element)
in the 1st line
element=input(\'enter the element name : \',\'s\')
we are asking to select a species and store it in the variable \'element\', \'s\' is needed when we are inputting aa string
2nd line
i=find(species==element) we are finding the index of the string which we have entered in the 'a' array \' species\'
1)MOLECULAR WEIGHT CALCULATION
mol_wt=molecular_weight(element)
here we are calling the function which calculates the molecular weight of the element which we have inputted above, we are sending the element string as an argument
here is the function
function [wt]=molecular_weight(element)
l=[\'H\',\'C\',\'N\',\'O\',\'A\',\'R\'];
atomic_weight=[1 12 14 16 40 0];
wt=0
for i=1:length(element)
v=isletter(element(i));
if v==1
n=find(l==element(i));
wt=wt+atomic_weight(n);
else
j=i-1;
n=find(l==element(j));
wt=wt+atomic_weight(n);
end
end
end
- here basically we have entered the elements that appear on the file and stored them into an array \'l\'
- then we have stored their respective atomic weights into another array
- we have initialized the variable \'wt\' to \'0\'
- we have created a for loop to calculate the weight of every element of the molecule
- we check if the particular character to the string is a number or a letter by using the command isletter() which returns 1 if it is a letter and 0 if it is a number
- if it is a letter we will search it the \'l\' array and find its index
- we will then use this index to find the atomic weight of that particular element in atomic_weight array
- the equation wt=wt+atomic_weight(n) updates the value of wt for every cycle
- but if the string is not a letter i mean that the before element has multiple atoms, so we should add te previous element again
- so v=0 now we will find the atomic weight of the element which is present at j=j-1
- we will then search for this element with index j=j-1 in \'l\' array and then use it to update the wt
let's take an example
Element=\'H2O2\'
length(element)=4
the first character of this element string is \'H\' which is a letter so
v=isletter(element(i)); will give ,v=1,j=j
2nd char =2,v=0,j=j-1
3rd char=O,v=1,j=j
2nd char =2,v=0,j=j-1
so the method will return
H+H+O+O=1+1+16+16=34
2)SPECIFIC HEAT CALCULATION
for calculation of specific heat we are going to call a function and pass all the coefficients and the temperature limits of the particular element as arguments to the function
cp=specific_heat(a1(i),a2(i),a3(i),a4(i),a5(i),a6(i),a7(i),a8(i),a9(i),a10(i),a11(i),a12(i),a13(i),a14(i),llt(i),lmt(i),lht(i));
here is the function
function [cp]=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,llt,lmt,lht)
x=llt
y=lmt
z=lht
r=0.08206;
t=linspace(x,z,(z-x))
for i=1:length(t)
if t(i)<=lmt
c(i)=(a1+a2.*t(i)+a3.*t(i).^2+a4.*t(i).^3+a5.*t(i).^4)*r;
else
c(i)=(a8+a9.*t(i)+a10.*t(i).^2+a11.*t(i).^3+a12.*t(i).^4)*r;
end
end
figure(1)
plot(t,c)
title(\'specific_heat\')
xlabel(\'temperature\')
ylabel(\'specific_heat\')
cp=c
end
- here we are going to define a temperature variable which ranges from Local Lower temperature to Local Higher Temperature of the element which we want
- now we are going to calculate for loop to calculate values of cp ate every temperature
- in the loop, we are going to check if the temperature is lower or higher w.t.r Local Mid Temperature as there are different coefficient to the 2 ranges
- after calculating these values are stored in C() array
- and then we are going to plot al these values w.r.t temperature
SPECIFIC HEAT VS TEMPERATURE graph for O2
SPECIFIC HEAT VS TEMPERATURE graph for N2
SPECIFIC HEAT VS TEMPERATURE graph for CO2
3)ENTHALPY CALCULATION
Similar to specific heat here also we call a function and send a similar set of arguments
here is the function
function [h]=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,llt,lmt,lht)
x=llt
y=lmt
z=lht
r=0.08206;
t=linspace(x,z,(z-x))
for i=1:length(t)
if t(i)<=lmt
H(i)=(a1+a2.*t(i)./2+a3.*(t(i).^2)./2+a4.*(t(i).^3)./3+a5.*(t(i).^4)./4+a6./t(i))*r.*t(i);
else
H(i)=(a8+a9.*t(i)./2+a10.*(t(i).^2)./2+a11.*(t(i).^3)./3+a12.*(t(i).^4)./4+a13./t(i))*r.*t(i);
end
end
figure(2)
plot(t,H)
title(\'Enthalpy\')
xlabel(\'temperature\')
ylabel(\'Enthalpy\')
h=H
end
this function is similar to that of specific heat with just the formulas to calculate the result differing
ENTHALPY VS TEMPERATURE graph for O2
ENTHALPY VS TEMPERATURE graph for N2
ENTHALPY VS TEMPERATURE graph for CO2
3)ENTROPY CALCULATION
Similar to the above calculations here also we call a function and send a similar set of arguments
here is the function
function [s]=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,llt,lmt,lht)
x=llt
y=lmt
z=lht
r=0.08206;
t=linspace(x,z,(z-x))
for i=1:length(t)
if t(i)<=lmt
S(i)=(a1.*log(t(i))+a2.*t(i)+a3.*(t(i).^2)./2+a4.*(t(i).^3)./3+a5.*(t(i).^4)./4+a7)*r;
else
S(i)=(a8.*log(t(i))+a9.*t(i)+a10.*(t(i).^2)./2+a11.*(t(i).^3)./3+a12.*(t(i).^4)./4+a14)*r;
end
end
figure(3)
plot(t,S)
title(\'Entropy\')
xlabel(\'temperature\')
ylabel(\'Entropy\')
s=S
end
this function is also similar to that of specific heat with just the formulas to calculate the result differing
ENTROPY VS TEMPERATURE graph for O2
ENTROPY VS TEMPERATURE graph for N2
ENTROPY VS TEMPERATURE graph for CO2