Project

Nasa file parsing

AIM:

1. Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats .

2. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.

3. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.

4. The main program that you will submit will just take the \"THERMO.dat\" file as an input and generate Cp, H and S plots.

5. Please write a detailed report to describe your logic.

NASA thermodynamic data :

NASA have came up with a function to calculate specific heat , entropy , enthalpy where it is a functin of temperature and its coefficients whih is unique for different element , Here the aim is to extract the coefficient all the species for master file and finding out the functon for various temperatures.

In order to extract all the coefficient ,file parsing technique is used with aid of matlab

Listing below the functions

Formula to calculate Specific heat

`(cp)/R=a1+a2.t+a3.t^2+a4t^4+a5`

Formula to calculate the Enthalpy

`(H)/(R.T)=a1+a2.t/2+a3.t^2/3+a4.t^3/4+a5.t^4/5+a6/t`

Formula to calculate the Entropy

`S/R=a1.lnt+a2.t+a3.t^2/2+a4.t^3/3+a5.t^4/4+a7`

Code explanation:

%calling the file
f1=fopen(\'THERMO.dat\',\'r\');

fopen command is used to opent the file and read mode has been selected

or i = 1:53 
    line1=fgetl(f1);
    species_id{i}=strtok(line1);%species is stored in form of an array 
    fgetl(f1); %Skips coefficients line
    fgetl(f1); %Skips coefficients line
    fgetl(f1); %Skips coefficients line
end

To obtain the element data the code is executed for 53 times to extract all the variable and elements are stored in array species data

to extract temperature
for i=1:2
    temp_str=fgetl(f1); % extracts temperature in first line
end

% splits temperature by using delimiter space
b= strsplit(temp_str,\' \');

%to convert string to number
[t]=[ str2num(b{2}) str2num(b{3}) str2num(b{4})]; %storing temperature in form of array
T=t(1):20:t(3); %creating linspace of 20

here the temperature is extracted and splited using a delimiter space and the string array is been converted to numbers by means of str2num command

prompt= ([\'O,O2,H,H2,OH,H2O,HO2,H2O2,C,\',...
        \'\\nCH,CH2,CH2(S),CH3,CH4,CO,CO2,HCO,\',...
        \'\\nCH2O,CH2OH,CH3O,CH3OH,C2H,C2H2,C2H3,\',...
        \'\\nC2H4,C2H5,C2H6,CH2CO,HCCO,HCCOH,H2CN,\',...
        \'\\nHCN,HNO,N,NNH,N2O,NH,NH2,NH3,\',...
        \'\\nNO,NO2,HCNO,HOCN,HNCO,NCO,CN,HCNN,\',...
        \'\\nN2,AR,C3H8,C3H7,CH3CHO,CH2CHO\'])
y=input(prompt,\'s\');

prompt has been made while execution where the user can choose between the set of variables for which output has to be executed

for i = 1:length(species_id)
    if strcmpi(y,species_id{i})
        element_no = i
    end

Here the input by user is compared with the all the elements and result will be returned as the element number when condition true, the command strcmpi will compare the given strings ignoring the case sensitiveness

cline1{d}=fgetl(f1); % first coefficient line
Epos=findstr(cline1{d},\'E\'); % finding the position of E
a1str{d}=cline1{d}(1:Epos(1)+3);
a1(d)=str2num(a1str{d})% converting the string to number 
a2str{d}=cline1{d}(Epos(1)+4:Epos(2)+3);
a2(d)=str2num(a2str{d})
a3str{d}=cline1{d}(Epos(2)+4:Epos(3)+3);
a3(d)=str2num(a3str{d})
a4str{d}=cline1{d}(Epos(3)+4:Epos(4)+3);
a4(d)=str2num(a4str{d})
a5str{d}=cline1{d}(Epos(4)+4:Epos(5)+3);
a5(d)=str2num(a5str{d})

% To find coefficient in line 2
cline2{d}=fgetl(f1);
a6str{d}=cline2{d}(1:Epos(1)+3);
a6(d)=str2num(a6str{d})
a7str{d}=cline2{d}(Epos(1)+4:Epos(2)+3);
a7(d)=str2num(a7str{d})
a8str{d}=cline2{d}(Epos(2)+4:Epos(3)+3);
a8(d)=str2num(a8str{d})
a9str{d}=cline2{d}(Epos(3)+4:Epos(4)+3);
a9(d)=str2num(a9str{d})
a10str{d}=cline2{d}(Epos(4)+4:Epos(5)+3);
a10(d)=str2num(a10str{d})

% To find coefficient in line 3
cline3{d}=fgetl(f1);
a11str{d}=cline3{d}(1:Epos(1)+3);
a11(d)=str2num(a11str{d})
a12str{d}=cline3{d}(Epos(1)+4:Epos(2)+3);
a12(d)=str2num(a12str{d})
a13str{d}=cline3{d}(Epos(2)+4:Epos(3)+3);
a13(d)=str2num(a13str{d})
a14str{d}=cline3{d}(Epos(3)+4:Epos(4)+3);
a14(d)=str2num(a14str{d})
R=8.314472

here the coefficients are extracted with keeping position E as base and its been convered from string to numerical value.

or i=1:length(T)
    if T(i)<=1000
        cp(i,d)=R.*(a1(d)+a2(d)*T(i)+a2(d)*T(i)+a3(d)*T(i).^2+a4(d)*T(i).^3+a5(d)*T(i)^4)
         H(i,d)=R.*T(i).*(a1(d)+a2(d)*(T(i)/2)+a3(d)*(T(i)^2/3)+a4(d)*(T(i)^3/4)+a5(d)*(T(i)^4/5)+a6(d)/T(i))
         S(i,d)=R.*(a1(d)*2.303*T(i)+a2(d)*T(i)+a3(d)*(T(i)^2/2)+a4(d)*(T(i)^3/3)+a5(d)*(T(i)/4)+a7(d))
    else 
        cp(i,d)= R.*(a8(d)+a9(d)*T(i)+a10(d)*T(i)+a11(d)*T(i).^2+a12(d)*T(i).^3+a13(d)*T(i)^4)
         H(i,d)=R.*T(i).*(a8(d)+a9(d)*(T(i)/2)+a10(d)*(T(i)^2/3)+a11(d)*(T(i)^3/4)+a12(d)*(T(i)^4/5)+a13(d)/T(i))
         S(i,d)=R.*(a8(d)*2.303*T(i)+a9(d)*T(i)+a10(d)*(T(i)^2/2)+a11(d)*(T(i)^3/3)+a12(d)*(T(i)/4)+a14(d))
    end
end 

the function is iterated for different values of temperature and is been stored in 1*1 array for various values of temperature with respect to element

figure(d);
subplot(3,1,1)
plot(T,cp(:,d),\'linewidth\',2,\'color\',\'r\');
xlabel(\'Temperature\')
ylabel(\'Cp\')

subplot(3,1,2)
plot(T,H(:,d),\'linewidth\',2,\'color\',\'g\');
xlabel(\'teperature\')
ylabel(\'Enthapy\')

subplot(3,1,3)
 plot(T,S(:,d),\'linewidth\',2,\'color\',\'b\');
 xlabel(\'teperature\')
 ylabel(\'Entropy\')

loc=\'C:\\Users\\Harun prakash E\\Documents\\CATIA\'
saveas(figure(d),fullfile(loc,  species_id{d}),\'png\');
end

The results are plotted by means of using the subplot command and it is stored in certain locationby using fullfile command .

The result is executed for 53 times to store all the results.

Complete code is given below

         %NASA File parsing
         
clear all
close all
clc

%calling the file
f1=fopen(\'THERMO.dat\',\'r\');
fgetl(f1); %elimination of headers
fgetl(f1);
fgetl(f1);
fgetl(f1);
fgetl(f1);

%To obtain the species data
for i = 1:53 
    line1=fgetl(f1);
    species_id{i}=strtok(line1);%species is stored in form of an array 
    fgetl(f1); %Skips coefficients line
    fgetl(f1); %Skips coefficients line
    fgetl(f1); %Skips coefficients line
end

frewind(f1) %bring to first line

%to extract temperature
for i=1:2
    temp_str=fgetl(f1); % extracts temperature in first line
end

% splits temperature by using delimiter space
b= strsplit(temp_str,\' \');

%to convert string to number
[t]=[ str2num(b{2}) str2num(b{3}) str2num(b{4})]; %storing temperature in form of array
T=t(1):20:t(3); %creating linspace of 20

%by using below input it validates for variables
prompt= ([\'O,O2,H,H2,OH,H2O,HO2,H2O2,C,\',...
        \'\\nCH,CH2,CH2(S),CH3,CH4,CO,CO2,HCO,\',...
        \'\\nCH2O,CH2OH,CH3O,CH3OH,C2H,C2H2,C2H3,\',...
        \'\\nC2H4,C2H5,C2H6,CH2CO,HCCO,HCCOH,H2CN,\',...
        \'\\nHCN,HNO,N,NNH,N2O,NH,NH2,NH3,\',...
        \'\\nNO,NO2,HCNO,HOCN,HNCO,NCO,CN,HCNN,\',...
        \'\\nN2,AR,C3H8,C3H7,CH3CHO,CH2CHO\'])
    y=input(prompt,\'s\');
% code for conversion of input element i form of array in to element number
for i = 1:length(species_id)
    if strcmpi(y,species_id{i})
        element_no = i
    end
end
vuu=4.*element_no ; %counter variable 
 
for d=1:1
    element_guide = vuu;
    for i=1:element_guide  
     coeff=fgetl(f1); % to extract headers
     vuu=1
    end
    
cline1{d}=fgetl(f1); % first coefficient line
Epos=findstr(cline1{d},\'E\'); % finding the position of E
a1str{d}=cline1{d}(1:Epos(1)+3);
a1(d)=str2num(a1str{d})% converting the string to number 
a2str{d}=cline1{d}(Epos(1)+4:Epos(2)+3);
a2(d)=str2num(a2str{d})
a3str{d}=cline1{d}(Epos(2)+4:Epos(3)+3);
a3(d)=str2num(a3str{d})
a4str{d}=cline1{d}(Epos(3)+4:Epos(4)+3);
a4(d)=str2num(a4str{d})
a5str{d}=cline1{d}(Epos(4)+4:Epos(5)+3);
a5(d)=str2num(a5str{d})

% To find coefficient in line 2
cline2{d}=fgetl(f1);
a6str{d}=cline2{d}(1:Epos(1)+3);
a6(d)=str2num(a6str{d})
a7str{d}=cline2{d}(Epos(1)+4:Epos(2)+3);
a7(d)=str2num(a7str{d})
a8str{d}=cline2{d}(Epos(2)+4:Epos(3)+3);
a8(d)=str2num(a8str{d})
a9str{d}=cline2{d}(Epos(3)+4:Epos(4)+3);
a9(d)=str2num(a9str{d})
a10str{d}=cline2{d}(Epos(4)+4:Epos(5)+3);
a10(d)=str2num(a10str{d})

% To find coefficient in line 3
cline3{d}=fgetl(f1);
a11str{d}=cline3{d}(1:Epos(1)+3);
a11(d)=str2num(a11str{d})
a12str{d}=cline3{d}(Epos(1)+4:Epos(2)+3);
a12(d)=str2num(a12str{d})
a13str{d}=cline3{d}(Epos(2)+4:Epos(3)+3);
a13(d)=str2num(a13str{d})
a14str{d}=cline3{d}(Epos(3)+4:Epos(4)+3);
a14(d)=str2num(a14str{d})
R=8.314472

%To find enthalpy,entropy,specific heat
for i=1:length(T)
    if T(i)<=1000
        cp(i,d)=R.*(a1(d)+a2(d)*T(i)+a2(d)*T(i)+a3(d)*T(i).^2+a4(d)*T(i).^3+a5(d)*T(i)^4)
         H(i,d)=R.*T(i).*(a1(d)+a2(d)*(T(i)/2)+a3(d)*(T(i)^2/3)+a4(d)*(T(i)^3/4)+a5(d)*(T(i)^4/5)+a6(d)/T(i))
         S(i,d)=R.*(a1(d)*2.303*T(i)+a2(d)*T(i)+a3(d)*(T(i)^2/2)+a4(d)*(T(i)^3/3)+a5(d)*(T(i)/4)+a7(d))
    else 
        cp(i,d)= R.*(a8(d)+a9(d)*T(i)+a10(d)*T(i)+a11(d)*T(i).^2+a12(d)*T(i).^3+a13(d)*T(i)^4)
         H(i,d)=R.*T(i).*(a8(d)+a9(d)*(T(i)/2)+a10(d)*(T(i)^2/3)+a11(d)*(T(i)^3/4)+a12(d)*(T(i)^4/5)+a13(d)/T(i))
         S(i,d)=R.*(a8(d)*2.303*T(i)+a9(d)*T(i)+a10(d)*(T(i)^2/2)+a11(d)*(T(i)^3/3)+a12(d)*(T(i)/4)+a14(d))
    end
end 

figure(d);
subplot(3,1,1)
plot(T,cp(:,d),\'linewidth\',2,\'color\',\'r\');
xlabel(\'Temperature\')
ylabel(\'Cp\')

subplot(3,1,2)
plot(T,H(:,d),\'linewidth\',2,\'color\',\'g\');
xlabel(\'teperature\')
ylabel(\'Enthapy\')

subplot(3,1,3)
 plot(T,S(:,d),\'linewidth\',2,\'color\',\'b\');
 xlabel(\'teperature\')
 ylabel(\'Entropy\')

loc=\'C:\\Users\\Harun prakash E\\Documents\\CATIA\'
saveas(figure(d),fullfile(loc,  species_id{d}),\'png\');
end

 

Plots for o2:

\"\"

Plots for co2:

\"\"

plots for N2

\"\"

screenshot of file location:

\"\"

*****************************END********************************

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.