Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

To parse the Thermodynamic data file and then calculate the specific heat(Cp), Enthalpy(H), Entropy(S) and also calculate the molecular weight of each species.

Objective:

To write a program to extracts the 14 co-efficient and calculates the enthalpy, entropy and specific heats for all the species in the data file.

Calculate the molecular weight of each species

File Parsing:

File parsing is reading a data from a file and performing some kind of transformation on the data. It means to a computer about what ‘’reading text’’ means to a human being. You scan it,from start to finish and figure out what it means as you go-whether you are a person readding text or a computer parsing a file.

Use ‘fopen’ command to open the data file.

Syntax:

1.”fileID=fopen(filename,’r’)”

‘r’ – is used file reading.

2.”fileID=fopen(filename,’W’)”

‘W’-is used to file writing.

Specific Heat(Cp):

The Specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius.

Enthalpy:

Enthalpy is a thermodynamic property of a system. It is the sum of the internal energy added to the product of the pressure and volume of the system. It reflects the capacity to do non-mechanical work and the capacity to release heat.

H=E+PV

H=Enthalpy

E-Internal Energy

P-Pressure

V-volume

Entropy:

Entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system

Gas constant(R):

The Gas constant is the physical constant in the equation for the ideal gas law,

PV=nRT

P-Pressure

V-Volume

n -no.of.moles

T-Temperature

R-Gas Constant

Molecular Weight:

Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in chemistry to determine stoichimetry in chemical reactions and equations. Molecular weight is commonly abbreviated by M.W.

For example, the molecular formula of hexane is C6H14. The subscripts indicate the number of each type of atom, so there are 6 carbon atoms and 14 hydrogen atoms in each hexane molecule. The atomic weight of carbon and hydrogen may be found on a periodic table.

  • Atomic weight of carbon: 12.01
  • Atomic weight of hydrogen: 1.01

molecular weight = (number of carbon atoms) (C atomic weight) + (number of H atoms) (H atomic weight) so we calculate as follows:

  • molecular weight = (6 x 12.01) + (14 x 1.01)
  • molecular weight of hexane = 72.06 + 14.14
  • molecular weight of hexane = 86.20 amu

Main program:

clear all
close all
clc

f1=fopen('THERMO.dat','r')
fgetl(f1);

line=fgetl(f1);
a=strsplit(line,' ');
global_low_temp=str2num(a{2});
global_average_temp=str2num(a{3});
global_high_temp=str2num(a{4});
fgetl(f1);
fgetl(f1);
fgetl(f1);

for k=1:53
    line=fgetl(f1);
    a=strsplit(line,' ');
    Gas=a(1);
    b=findstr(line,'.');
    local_low_temp=str2num(line(b(1)-4:b(1)+4));
    local_high_temp=str2num(line(b(2)-4:b(2)+4));
    local_average_temp=str2num(line(b(3)-4:b(3)+4));
    
    
    ct=1;
    for j=1:3
        line=fgetl(f1);
        b=strfind(line,'E');
        for i=1:length(b)
            if ct<=7
                Ha(ct)=str2num(line(b(i)-11:b(i)+3));
                ct=ct+1;
            else
                La(ct-7)=str2num(line(b(i)-11:b(i)+3));
                ct=ct+1;
            end
        end
    end
    
    CP=0;
    H=0;
    S=0;
    
    [CP,H,S]=coefficient(local_low_temp,local_average_temp,local_high_temp,La,Ha);
    
    M=Weight(Gas)
    
    GAS=char(Gas);
    T=local_low_temp:local_high_temp;
    current=pwd;
    mkdir(GAS);
    cd(GAS)
    
    figure(1);
    plot(T,CP,'color','g','linewidth',2);
    txt1=['Molecular Weight:' num2str(M)];
    text(500,max(CP)*0.98,txt1,'color','r');
    grid on
    
    xlabel('temperature(K)');
    ylabel('specific heat(KJ/mol)');
    title('specific heat vs temperature');
    
    figure(2);
    plot(T,H,'color','r','linewidth',2);
    txt1=['molecular Weight:' num2str(M)];
    text(500,max(H)*0.98,txt1,'color','b');
    grid on
    
    xlabel('temperature(k)');
    ylabel('enthalpy(kj/mol)');
    title('enthalpy vs temperature');
    
    figure(3);
    plot(T,S,'color','b','linewidth',2);
    txt1=['Molecular Weight:' num2str(M)];
    text(500,max(S)*0.98,txt1,'color','b');
    grid on
    xlabel('temperature(k)');
    ylabel('entropy(kl/mol)');
    
    saveas(figure(1),'CP.jpg');
    saveas(figure(2),'H.jpg');
    saveas(figure(3),'S.jpg');
    
    cd(current);
end
    
    
    

Function program to calculate molecular weight:

function Mo=Weight(Gas)

Weight=[1 12 14 16 32 0 40];
Gas1=['H' 'C' 'N' 'O' 'S' 'A' 'R'];
number=[1 2 3 4];

M=0;
GAS=char(Gas);
for i=1:length(GAS)
    for j=1:length(Gas1)
        if GAS(i)==Gas1(j)
            M(i)=Weight(j);
        end
        for j=1:length(number)
            if str2double(GAS(i))==number(j)
                M(i)=M(i-1)*(number(j)-1);
            end
        end
    end
end

Mo=sum(M);
end

Function program to calculate Specific heat,Enthalpy,Entropy.

function[CP,H,S]=coefficient(local_low_temp,local_average_temp,local_high_temp,La,Ha)

for i=1:local_high_temp-local_low_temp+1
    
    R=8.34;
    T=i+local_low_temp-1;
        
    if T<=local_average_temp
    CP(i)=R*(La(1)+La(2)*T+La(3)*T^2+La(4)*T^3+La(5)*T^4);
    H(i)=R*T*(La(1)+(La(2)*T)/2+(La(3)*T^2)/3+(La(4)*T^3)+(La(5)*T^4/5+La(6)/T));
    S(i)=R*(La(1)*log(T)+(La(2)*T)+((La(3)*T^2)/2)+((La(4)*T^3)/3)+((La(5)*T^4)/4)+(La(7)));
    
    else
        CP(i)=R*(Ha(1)+Ha(2)*T+Ha(3)*T^2+Ha(4)*T^3+Ha(5)*T^4);
        H(i)=R*T*(Ha(1)+((Ha(2)*T)/2)+(Ha(3)*T^2)/3+(Ha(4)*T^3)/4+(Ha(5)*T^4)/5+(Ha(6)/T));
        S(i)=R*(Ha(1)*log(T)+(Ha(2)*T)+(Ha(3)*T^2)/2)+(Ha(4)*T^3)/3+(Ha(5)*T^4)/4+(Ha(7));
    end
end
end

        
   
 

Program Debrief:

Open the given data with fopen(‘THERMO.dat’,’r’)

Getting the nextline from the data file by using 'fgetl' command.

Split the character vector of values on specific delimeter using strsplit command

Reading the temperature values distingused as local,average and high temperatures

Using strfind and strdouble command extract the temperature values and store temperature as low,average and high temperatures.

When all the global and local temperature are read we call functions which are written to calculate molecular weight, Specific heat,Enthalpy,Entropy.

In function program use “ifelse” to find the first 7co-efficients using high temperature and next 7 co-efficients using low temperature.

After getting the values of Cp,H,S return to the main program and plot the Cp,H,S and saving them in the folder.It is standard procedure to store in file in specific location, by using many command (pwd,mkdir,cd,saveas) written in this program.

We are using formulae to calculate Cp,H,S and also using forloop to calculate each value at local temperature range.

Using ifelse command to use low and high temperature coefficient as per average temperature.

If the temperature is less than average temperature then we use low temperature co-efficient and if temoerature is more than average temperature then use high temperature co-efficient.

Molecular weigtht is calculated in a different function which is later called in main function.

Conclusion:

Finally we get the specific heat,Enthalpy,Entropy and molecular weight values and also of CP,H,S for the temperature.

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.