Project

Parsing NASA thermodynamic data using MATLAB

AIM

To parse a datafile in MATLAB and calculate the specific heat, enthalpy and entropy and molecular weight of the 53 different species included in the datafile

THEORY

File parsing means extracting relevant data from a dataset and using that data for other applications. The file that is being parsed in this project includes the following data:

1)Global minimum, medium and maximum temperatures

2) Local minimum, medium and maximum temperatures for 53 species

3) Seven low temperature and high temperature coefficients of 53 species

Using these local temperature values and coefficients of a particular species we can calculate the entropy, enthalpy and specific heat at a given temperature using the following equations:

However, above the value of the local medium temperature the high temperature coefficients of each species are used and upto the value of the local medium temperature the low temperature coefficients are used to calculate the entropy, enthalpy and specific heat of that particular species. The first seven coefficients in the dataset is high temperature coeffcients and rest is low temperature coefficients.

MAIN PROGRAM

clear all
close all
 clc
 R=8.314;
 f1=fopen('THERMO.dat','r');
 %Storing the comments
 for i=1:5
     comments=fgetl(f1);
 end
%Asking for a user-defined input
 prompt='Enter the species name'
 
 sp=input(prompt,'s');
 speciesname=upper(sp);
 mkdir('F:\Fileparsing\NASAfileparsing');
for i=1:53
     c=0;
    B{1,1}=fgetl(f1);
    f=strsplit(B{1,1});
     species=f{1};
if(strcmp(speciesname,species))
  b=strfind(B{1,1},'.');
 locallowtemp{1,1}=B{1,1}(b(1)-4:b(1)+3);
 local_low_temp=str2num(locallowtemp{1,1});
 localhightemp{1,1}=B{1,1}(b(2)-4:b(2)+3);
 local_high_temp=str2num(localhightemp{1,1});
 localmediumtemp{1,1}=B{1,1}(b(3)-4:b(3)+3);
 local_medium_temp=str2num(localmediumtemp{1,1});
 for i=1:3
B{1,1}=fgetl(f1);
 a=strfind(B{1,1},'E');
for i=1:length(a)
    if(i==1)
    coefficients{1,i+c}=B{1,1}(1:a(i)+3);
     coeffs(i+c)=str2num(coefficients{1,i+c});
    else
    coefficients{1,i+c}=B{1,1}(a(i-1)+4:a(i)+3);
    coeffs(i+c)=str2num(coefficients{1,i+c});
    end
end
c=c+length(a);
%calculation of molecular weight
[A,N,O,H,C]=deal(39.948,14.007,15.999,1.008,12.011);
Atomicmass=[A,N,O,H,C];
Elements=['A','N','O','H','C'];
g=species;
Mol_weight=0;
for i=1:length(g)
    for j=1:length(Elements)
        if(strcmp(g(i),Elements(j)))
            Mol_weight=Mol_weight+Atomicmass(j);
            k=j;
        end
    end
    num1=str2double(g(i));
    if num1>1
        Mol_weight=Mol_weight+Atomicmass(k)*(num1-1);
    end
end
M=Mol_weight;
 end
 Molecularweight=M;
T = sprintf('Molecular weight of %s is %f ',species,Molecularweight);
disp(T)
 temp=linspace(local_low_temp,local_high_temp,1000);
 %Calculation of properties
for i=1:1000
if(temp(i)<local_medium_temp)   
cp(i)=(coeffs(1)+(coeffs(2)*temp(i))+(coeffs(3)*((temp(i))^2))+(coeffs(4)*((temp(i))^3))+(coeffs(5)*((temp(i))^4)))*R;
S(i)=((coeffs(1)*(log(temp(i)))+(coeffs(2)*temp(i))+(coeffs(3)*((temp(i))^2)/2)+((coeffs(4)*((temp(i))^3))/3)+(coeffs(5)*((temp(i))^4))/4)+(coeffs(7)))*R;
H(i)=(coeffs(1)+((coeffs(2)*temp(i))/2)+(coeffs(3)*((temp(i))^2)/3)+(coeffs(4)*((temp(i))^3)/4)+(coeffs(5)*((temp(i))^4)/5)+(coeffs(6)/temp(i)))*R*temp(i);
else   
cp(i)=(coeffs(8)+(coeffs(9)*(temp(i)))+(coeffs(10)*(temp(i))^2)+(coeffs(11)*(temp(i))^3)+(coeffs(12)*(temp(i))^4))*R;
S(i)=((coeffs(8)*log(temp(i)))+(coeffs(9)*temp(i))+(coeffs(10)*((temp(i))^2)/2)+(coeffs(11)*((temp(i))^3)/3)+(coeffs(12)*((temp(i))^4)/4)+(coeffs(14)))*R;
H(i)=(coeffs(8)+(coeffs(9)*(temp(i))/2)+(coeffs(10)*((temp(i))^2)/3)+(coeffs(11)*((temp(i))^3)/4)+(coeffs(12)*((temp(i))^4)/5)+(coeffs(13)/(temp(i))))*R*temp(i);
end
end
%plotting
figure(1);
plot(temp,cp)
xlabel('Temperature [K]')
ylabel('Specific heat[kJ/kgK]')
figure(2);
plot(temp,H)
xlabel('Temperature [K]')
ylabel('Enthalpy[kJ/mol]')
figure(3);
plot(temp,S)
xlabel('Temperature [K]')
ylabel('Entropy[kJ/molK]')
mkdir('F:\Fileparsing\NASAfileparsing\',species);
dir=(['F:\Fileparsing\NASAfileparsing\',species]);
cd(dir)
saveas(1,'Specific_heat.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Entropy.jpg');
dir2=(['F:\Fileparsing\']);
cd(dir2);
else
for i = 1:3
    fgetl(f1);
end
end

    

EXPLANATION OF THE PROGRAM

1.fopen is used to open a particular datafile

2.fgetl('variable') is used to store a particular line into a cell

3.The first five line is stored in a cell called comments using fgetl()

4.Using prompt command, an input is taken from the user and stored in sp.

5.Using strcmp() function the input string and the name of the species is compared inside an if condition. If the condition is true, commands inside the loop will be executed ,else, the next three lines are skipped using fgetl() and the if condition is executed for the next species.

4. Using the strfind command a decimal point is located in the line containing the local temperature values and the local temperature values are stored as a number

5.Using the strfind command 'E' is located in the lines containing the temperature coefficients and the local temperature values are stored as numbers in a for loop in a single array called coeffs.

6. The plots and the molecular weight for each species is calculated inside the for loop and the plots are saved in a folder containing the name of the species.

7. The coeffs array is replaced in each loop by the coefficients of each species.

CODE CALCULATING THE PROPERTIES

 temp=linspace(local_low_temp,local_high_temp,1000);
 %Calculation of properties
for i=1:1000
if(temp(i)<local_medium_temp)   
cp(i)=(coeffs(1)+(coeffs(2)*temp(i))+(coeffs(3)*((temp(i))^2))+(coeffs(4)*((temp(i))^3))+(coeffs(5)*((temp(i))^4)))*R;
S(i)=((coeffs(1)*(log(temp(i)))+(coeffs(2)*temp(i))+(coeffs(3)*((temp(i))^2)/2)+((coeffs(4)*((temp(i))^3))/3)+(coeffs(5)*((temp(i))^4))/4)+(coeffs(7)))*R;
H(i)=(coeffs(1)+((coeffs(2)*temp(i))/2)+(coeffs(3)*((temp(i))^2)/3)+(coeffs(4)*((temp(i))^3)/4)+(coeffs(5)*((temp(i))^4)/5)+(coeffs(6)/temp(i)))*R*temp(i);
else   
cp(i)=(coeffs(8)+(coeffs(9)*(temp(i)))+(coeffs(10)*(temp(i))^2)+(coeffs(11)*(temp(i))^3)+(coeffs(12)*(temp(i))^4))*R;
S(i)=((coeffs(8)*log(temp(i)))+(coeffs(9)*temp(i))+(coeffs(10)*((temp(i))^2)/2)+(coeffs(11)*((temp(i))^3)/3)+(coeffs(12)*((temp(i))^4)/4)+(coeffs(14)))*R;
H(i)=(coeffs(8)+(coeffs(9)*(temp(i))/2)+(coeffs(10)*((temp(i))^2)/3)+(coeffs(11)*((temp(i))^3)/4)+(coeffs(12)*((temp(i))^4)/5)+(coeffs(13)/(temp(i))))*R*temp(i);
end
end

CODE CALCULATING THE MOLECULAR WEIGHT

%calculation of molecular weight
[A,N,O,H,C]=deal(39.948,14.007,15.999,1.008,12.011);
Atomicmass=[A,N,O,H,C];
Elements=['A','N','O','H','C'];
g=species;
Mol_weight=0;
for i=1:length(g)
    for j=1:length(Elements)
        if(strcmp(g(i),Elements(j)))
            Mol_weight=Mol_weight+Atomicmass(j);
            k=j;
        end
    end
    num1=str2double(g(i));
    if num1>1
        Mol_weight=Mol_weight+Atomicmass(k)*(num1-1);
    end
end
M=Mol_weight;
 end
 Molecularweight=M;
T = sprintf('Molecular weight of %s is %f ',species,Molecularweight);
disp(T)

RESULTS AND CONCLUSIONS

PLOTS OF O2

PLOTS OF N2

PLOTS OF CO2

SCREENSHOT OF THE FOLDER CONTAINING ALL THE PLOTS

MOLECULAR WEIGHTS OF THE SPECIES

File parsing is successfully and the plots of all 53 different species are plotted and molecular weights of respective species are calculated.

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.