Challenge

Project 1 - Parsing NASA thermodynamic data

AIM :

To plot the Specific heat (Cp), Enthalpy (H) and Entropy (S) against Temperature (T) for each species given in the data and to save as separate file. Also to calculate Molecular mass of each species.

GOVERNING EQUATIONS :

`(C_p)/T=a_1+a_2T+a_3T^2+a_4T^3+a_5T^4`

`H/(RT)=a_1+(a_2T)/2+(a_3T^2)/3+(a_4T^3)/4+(a_5T^4)/5+(a_6)/T`

`S/R=a_1lnT+a_2T+(a_3T^2)/2+(a_4T^3)/3+(a_5T^4)/4+a_7`

Where R is universal gas constant and T is Temperature.

First 7 coefficients are High Temperature coefficients and next 7 coefficients are Low Temperature coefficients in the given data.

OBJECTIVES :

  • To extract coefficients from the given data file for each species using suitable commands
  • To calculate Specific heat(Cp), Enthalpy(H) and Entropy(S) of each species with extracted coefficients
  • To create a folder in the name of species
  • To plot variation of Cp, H and S against Temperature and save in the corresponding folder
  • Defining a function to calculate molecular mass of the species and to write into a text file

CONTENT :

The given data file contain coefficients of 53 species. The first 5 lines contains non essential data, which can be neglected. So it is read from the file and assigned to a trash variable. The next line onwards the required data are provided.

The first line (header) contain species name and local minimum, maximum and middle temperatures. The header line also contain some unessential data, which can be avoided. But these data are arranged in non uniform way for each species. Thus with the given file, it is hard to extract local minimum, maximum and middle temperatures from this line. So a slight modification is made on the given file. That is unessential data are removed from the header line for each species. Thus the program become less complicated.

To calculate Molecular mass of each species, a function is defined. The function identifies the atoms in the species and calculate the molecular mass. All the species in the given file contain some common atoms. So a vector is defined in which the molecular mass of these atoms are included. The function is defined as follows.

function MM=mol_mass(species)
%Initializing molecular mass=0
mm=0;
%Creating an array of atoms in the all molecules
molecules=['H','C','N','O','S','A'];
%Creating an array of molecular mass of atoms in the same order of atoms
mole_mass=[1,12,14,16,32,40];
%Adding molecular masses of atoms in the molecule
for i=1:length(species)%Taking each atom in the species
    for j=1:length(molecules)%Taking each atoms in the defined array
        if(species(i)==molecules(j))
            %Comparing atoms and if they are same corresponding molecular
            %mass is added
            mm=mm+mole_mass(j);
        end
    end
    %Converting character into number, if it is alphabet it give a null
    %vector
    num=str2num(species(i));
    %if it is number same pattern is to be executed with previous atom as
    %in the above
    if(num>1)
        for j=1:length(molecules)
            if(species(i-1)==molecules(j))
                mm=mm+mole_mass(j);
            end
        end
    end
end
MM=mm;
end

With this function the main program is prepared. The program extract coefficients of each species and calculate Specific heat (Cp), Enthalpy (H) and Entropy (S). With the calculated values, Cp vs Temp, H vs Temp and S vs Temp plots are plotted for each species. All these plots are saved in a new folder of name as that of species. The defined function is called to calculate Molecular mass and it is wrote into a text file. So that at the end of program, a list contain species name and corresponding molecular mass is obtained. Also molecular mass will be printed on command window.

clear all
close all
clc
%Opening Molecular Mass text file with write permission
f2=fopen('Molecular Mass.txt','w');
%Writing title for the data
fprintf(f2,'%10.10s %5.15s','Species','Molecular Mass');
fprintf(f2,'\n');
%Open the thermal data in Matlab
f1=fopen('THERMO(1).dat','r');
%First 5 lines are not used in the program. Thus it can be avoided
%This is different for each species. So a change is to be made here.
for i=1:5
    none=fgetl(f1);
end
for i=1:53
%Reading header which include Species name, Min Temp, Max Temp and Mid Temp
header=fgetl(f1);
%Splitting the line based on Spaces
header=strsplit(header,' ');
%Identifying Species
species=header{1}
%Identifying Max, MIn, and Mid Temperatures
low_temp=str2num(header{2});
high_temp=str2num(header{3});
mid_temp=str2num(header{4});
%Reading First line of Coefficients
tline=fgetl(f1);
%finding the locations of alphabet 'E'
a=findstr(tline,'E');
%Identifying first coefficient
coeff=tline(1:a(1)+3);
%Converting string into number
c(1)=str2num(coeff);
%First line contain for more coefficients, which can be read using a loop
for j=1:4
    coeff=tline(a(j)+4:a(j+1)+3);
    c(j+1)=str2num(coeff);
end
%Reading next line and executing same patters as that of above program
tline=fgetl(f1);
a=findstr(tline,'E');
coeff=tline(1:a(1)+3);
c(6)=str2num(coeff);
for j=1:4
    coeff=tline(a(j)+4:a(j+1)+3);
    c(j+6)=str2num(coeff);
end
%Reading last line and executing same pattern as that of above
tline=fgetl(f1);
a=findstr(tline,'E');
coeff=tline(1:a(1)+3);
c(11)=str2num(coeff);
%Last line contain only 4 coefficients, so loop need to be executed only
%thrice
for j=1:3
    coeff=tline(a(j)+4:a(j+1)+3);
    c(j+11)=str2num(coeff);
end
%Calling mol_mass function to calculate Molecular mass
molecular_mass=mol_mass(species)
%Opening Molecular Mass text file
%Writing molecule name and corresponding molecular mass
fprintf(f2,'%10.10s %5.2f',species,molecular_mass);
%To write next data on next line following command is used
fprintf(f2,'\n');
%Universal gas constant is 8.314
R=8.314;
%Creating array of temperatures in between Max and Min temperatures
temp=linspace(low_temp,high_temp,100);
%Mid temperature is used to split the temperature into Low and High
%temperatures, if temp is less than mid temp it is treated as low temp,
%otherwise High temp
for i=1:length(temp)
if temp(i)>mid_temp %If temp is high temperature
    %First 7 coefficients are used to calculate Cp,H and S according to
    %given formula
    cp(i)=R*(c(1)+(c(2)*temp(i))+(c(3)*(temp(i)^2))+(c(4)*(temp(i)^3))+(c(5)*(temp(i)^4)));
    H(i)=R*(c(1)+c(2)*temp(i)/2+c(3)*(temp(i)^2)/3+c(4)*(temp(i)^3)/4+c(5)*(temp(i)^4)/5+c(6)/temp(i))*temp(i);
    S(i)=R*(c(1)*log(temp(i))+c(2)*temp(i)+c(3)*(temp(i)^2)/2+c(4)*(temp(i)^3)/3+c(5)*(temp(i)^4)/4+c(7));
else %If temperature is low temperature
    %The last 7 coefficients are used to calculate Cp, H and S 
    cp(i)=R*(c(8)+c(9)*temp(i)+c(10)*(temp(i)^2)+c(11)*(temp(i)^3)+c(12)*(temp(i)^4));
    H(i)=R*(c(8)+c(9)*temp(i)/2+c(10)*(temp(i)^2)/3+c(11)*(temp(i)^3)/4+c(12)*(temp(i)^4)/5+c(13)/temp(i))*temp(i);
    S(i)=R*(c(8)*log(temp(i))+c(9)*temp(i)+c(10)*(temp(i)^2)/2+c(11)*(temp(i)^3)/3+c(12)*(temp(i)^4)/4+c(14));
end
end
%Plotting the required Graphs
figure(1)
%Plotting Specific heat against Temperature
plot(temp,cp,'linewidth',3,'color','g');
title(['Specific Heat vs Temperature for ',species])
%Labelling X axis as Temperature
xlabel('Temperature T');
%Labelling Y axis as Specific heat
ylabel('Specific Heat C_p');
figure(2)
%Plotting Enthalpy against Temperature
plot(temp,H,'linewidth',3,'color','g');
title(['Enthalpy vs Temperature for ',species]);
xlabel('Temperature');
%Labelling Y axis as Enthalpy
ylabel('Enthalpy (H)');
figure(3)
%Plotting Entropy against Temperature
plot(temp,S,'linewidth',3,'color','g');
title(['Entropy vs Temperature for ',species]);
xlabel('Temperature');
%Labelling Y axis as Entropy
ylabel('Entropy');
%Creating a Folder of name as that of species name
mkdir(species)
%Changing directory into newly created folder to save plots
cd(['C:\Users\User\Desktop\Matlab\01 Challenges\Pro NASA\',species]);
%Saving all the plots into the newly created folder
saveas(1,'Cp vs Temp.jpg')
saveas(2,'Enthalpy vs Temp.jpg')
saveas(3,'Entropy vs Temp.jpg')
cd('C:\Users\User\Desktop\Matlab\01 Challenges\Pro NASA');
end
fclose(f2)

RESULT :

  • Cp vs Temp, H vs Temp and S vs Temp plots are obtained for each species and it is saved in separate folders.
  • All the three plots are obtained for O2, N2 and CO2 also which is shown below.

Plots of Cp, H and S against temperature for O2 is given below,

Plots of Cp, H and S against temperature for N2 is given below,

Plots of Cp, H and S against temperature for CO2 is given below,

CONCLUSION :

On analysing the results obtained, it is clear that Specific heat, Enthalpy and Entropy of a gas depends on its temperature. Also the nature of variation is given as equations. However, Specific heat has lowest variation against temperature. It is almost constant compared to Enthalpy and Entropy. So that Enthalpy is directly varyiing with temperature. Entropy is also increasing with temperature but the incriment start to decrease on moving forward.

In the programming part, the given data file contain non uniformly distributed data in header file, thus a slight modification is made on the file to read local temperature limits in order to avoid complications in program. Otherwise, we can go with global temperature limits to avoid complications.

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.