Challenge

Project 1 - Parsing NASA thermodynamic data

Aim

To write a program that extracts the 14 Co efficients of species and calculating entropy,enthalpy and specific heat of all species in the data file and plot them . Calculate the molecular weight of each species.

Objective,

To write a program extracts the 14 co-efficients of all species and Calculate, plot entropy,enthalpyt, specific heat of all species.

To calculate the molecular weighth of all species and display it command window

To save the plots as images with appropriate names and dumb all the species in separate folder with suitable name.

Introduction,

Parsing;

Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar.
Parsing a file:

Parsing a file means reading in a data stream and building a memory model of the content of that data. This aims at facilitating perfoming some kind of transformation on the data. Parsing splits a file or other input into pieces of data that can be easily stored or manipulated.

calculation of entropy,enthalpy and specific heat of all species

This is the formula that calculate the Specific heat, Entropy, Enthalpy for all species,

Program:

The program that exrtacts the 14 co efficients of all species

clear all
close all
clc
%Open the required given file and reading 
f1=fopen('THERMO.dat','r'); 
l1=fgetl(f1);   % first headerline of the file and skip line
tl2=fgetl(f1);  % Global temperature values

A=strsplit(tl2,' ');  % split the temp values and change to number
global_low_temp=str2num(A{2});
global_mid_temp=str2num(A{3});
global_high_temp=str2num(A{4});

l3=fgetl(f1);  % l3,l4 & l5 are Comments, so to be negligible these line
l4=fgetl(f1);
l5=fgetl(f1);

%Create a loop for calculating total species
%lets take first species 'O' in the given data

for k=1:53

st1=fgetl(f1);    % In species given  Local Temperature
st1=strsplit(st1,' '); %split the local temperature change to number 
species_name=st1{1};
local_low_temp=str2num(st1{end-3});% Local low temperature
local_mid_temp=str2num(st1{end-2});% Local mid temperature
local_high_temp=str2num(st1{end-1});% Local low temperature

sl1=fgetl(f1); % First line co efficients of Species 
sy1=strfind(sl1,'E'); % Have to find E where to be placed for using split the coefficients

 %Changing coeficients of the species string into number
 %Changing first line coefficients to number
a1=str2double(sl1(1:sy1(1)+3));
a2=str2num(sl1(sy1(1)+4:sy1(2)+3));
a3=str2num(sl1(sy1(2)+4:sy1(3)+3));
a4=str2num(sl1(sy1(3)+4:sy1(4)+3));
a5=str2num(sl1(sy1(4)+4:sy1(5)+3));

%Changing second line coefficients to number
sl2=fgetl(f1);
sy2=strfind(sl2,'E');
a6=str2num(sl2(1:sy2(1)+3));
a7=str2num(sl2(sy2(1)+4:sy2(2)+3));
a8=str2num(sl2(sy2(2)+4:sy2(3)+3));
a9=str2num(sl2(sy2(3)+4:sy2(4)+3));
a10=str2num(sl2(sy2(4)+4:sy2(5)+3));

%Changing third line coefficients to number
sl3=fgetl(f1);
sy3=strfind(sl3,'E');
a11=str2num(sl3(1:sy3(1)+3));
a12=str2num(sl3(sy3(1)+4:sy3(2)+3));
a13=str2num(sl3(sy3(2)+4:sy3(3)+3));
a14=str2num(sl3(sy3(3)+4:sy3(4)+3));

% Calculating Specific heat(Cp), Entropy(S)and Enthalpy(H)  with using Local
% temperatures

temp=linspace(local_low_temp,local_high_temp,100);

R=8.3145;  % Universal gas constant (R) unit is Joule/mole.kelvin

%using for loop to calculate properties of upper and lower temperature
for i=1:length(temp)
    if temp(i)>local_mid_temp

        Cp(i)=R*(a1+(a2*temp(i))+(a3.*temp(i).^2)+(a4.*temp(i).^3)+(a5.*temp(i).^4));

        H(i)=R.*temp(i)*(a1+((a2.*temp(i))/2)+((a3.*temp(i).^2)/3)+((a4.*temp(i).^3)/4)+((a5.*temp(i).^4)/5)+(a6./temp(i)));

        S(i)=R*(a1.*log(temp(i))+a2.*temp(i)+((a3.*temp(i).^2)/2)+((a4.*temp(i).^3)/3)+((a5.*temp(i).^4)/4)+a7);

    else 

        Cp(i)=R*(a8+(a9.*temp(i))+(a10.*temp(i).^2)+(a11.*temp(i).^3)+(a12.*temp(i).^4));

        H(i)=R.*temp(i).*(a8+((a9.*temp((i)))/2)+((a10.*temp(i).^2)/3)+((a11.*temp(i).^3)/4)+((a12.*temp(i).^4)/5)+(a13./temp(i)));

        S(i)=R*(a8.*log(temp(i))+a9.*temp(i)+((a10.*temp(i).^2)/2)+((a11.*temp(i).^3)/3)+((a12.*temp(i).^4)/4)+a14);
    end
end


%Plotting Temperature with Specific Heat of species 'O'
figure(1)
plot(temp,Cp,'linewidth',2,'Color','r');
xlabel('Temperature');
ylabel('Specific Heat');
title('Temperature vs Specific Heat');
grid on

%Plotting Temperature with Enthalpy of species 'O'
figure(2)
plot(temp,H,'linewidth',2,'Color','g');
xlabel('Temperature');
ylabel('Enthalpy');
title('Temperature vs Enthalpy');
grid on


%Plotting Temperature with Entropy of species 'O'
figure(3)
plot(temp,S,'linewidth',2,'Color','b');
xlabel('Temperature');
ylabel('Entropy');
title('Temperature vs Entropy');
grid on


% creating folder to save  the plots of species
mkdir(['File_parsing\',species_name]);
cd(['File_parsing\',species_name]);
saveas(1,'Specific heat','png');
saveas(2,'Enthalpy','png');
saveas(3,'Entropy','png');
cd ..
cd ..

% Function to calculating molecular mass of each species
mm=Molecular_weight(species_name);
s=sprintf('%sMolecular_weight=%f',species_name,mm);
disp(s);

end

fclose(f1);

Explaination:

  • In this program first we have to open the given file (NASA thermodynamic data) by using fopen command and its take in a variable f1.then have to extract and skip the unneeded lines such as header which given in the file by using fgetl command.
  • Then we have to split the global temperatures in to the string and change to number by str2num command.after have to split next three lines thats we not need to refer in this program.
  • lets move from the 6 th line , there is 53 species in the file, thats we have to extract and calculating the specific heat, entropy and enthalpy for all species. lets take the first species 'O' to extract each coefficients and calculate the thermodynamic properties and its all calculating under the for loop becuse we have to calculate for all 53 species.
  • In the first line of the species , take the temperatures that called as local temperatures. to split the temperateure as low, mid and high in the string and change to number, then extract the each coefficients by line by line using fgetl and findstr (which used to find string 'E' in the line) and change all 14 coefficients to number.

Below program that calculating thermodynamic properties such as Specific heat, Entropy and Enthalpy for Species.

% Calculating Specific heat(Cp), Entropy(S)and Enthalpy(H)  with using Local
% temperatures

temp=linspace(local_low_temp,local_high_temp,100);

R=8.3145;  % Universal gas constant (R) unit is Joule/mole.kelvin

%using for loop to calculate properties of upper and lower temperature
for i=1:length(temp)
    if temp(i)>local_mid_temp

        Cp(i)=R*(a1+(a2*temp(i))+(a3.*temp(i).^2)+(a4.*temp(i).^3)+(a5.*temp(i).^4));

        H(i)=R.*temp(i)*(a1+((a2.*temp(i))/2)+((a3.*temp(i).^2)/3)+((a4.*temp(i).^3)/4)+((a5.*temp(i).^4)/5)+(a6./temp(i)));

        S(i)=R*(a1.*log(temp(i))+a2.*temp(i)+((a3.*temp(i).^2)/2)+((a4.*temp(i).^3)/3)+((a5.*temp(i).^4)/4)+a7);

    else 

        Cp(i)=R*(a8+(a9.*temp(i))+(a10.*temp(i).^2)+(a11.*temp(i).^3)+(a12.*temp(i).^4));

        H(i)=R.*temp(i).*(a8+((a9.*temp((i)))/2)+((a10.*temp(i).^2)/3)+((a11.*temp(i).^3)/4)+((a12.*temp(i).^4)/5)+(a13./temp(i)));

        S(i)=R*(a8.*log(temp(i))+a9.*temp(i)+((a10.*temp(i).^2)/2)+((a11.*temp(i).^3)/3)+((a12.*temp(i).^4)/4)+a14);
    end
end


%Plotting Temperature with Specific Heat of species 'O'
figure(1)
plot(temp,Cp,'linewidth',2,'Color','r');
xlabel('Temperature');
ylabel('Specific Heat');
title('Temperature vs Specific Heat');
grid on

%Plotting Temperature with Enthalpy of species 'O'
figure(2)
plot(temp,H,'linewidth',2,'Color','g');
xlabel('Temperature');
ylabel('Enthalpy');
title('Temperature vs Enthalpy');
grid on


%Plotting Temperature with Entropy of species 'O'
figure(3)
plot(temp,S,'linewidth',2,'Color','b');
xlabel('Temperature');
ylabel('Entropy');
title('Temperature vs Entropy');
grid on
  • In the program, we have to calculate the thermodynamics properties for each species. first we take the temperate values form low to high using linspace and write the universal gas constant value.
  • Then we determine the formula in loop for calculating in each temperature values and using if condition to find properties in low and high temperatutes and plot the specific heat,enthalpy and entropy with tempertaures.

Program to save plots of all species in separate folder

% creating folder to save  the plots of species
mkdir(['File_parsing\',species_name]);
cd(['File_parsing\',species_name]);
saveas(1,'Specific heat','png');
saveas(2,'Enthalpy','png');
saveas(3,'Entropy','png');
cd ..
cd ..

In the above program, create folder and saved all plots by using mkdir command (which used to save files in folder). then we saved the plots in each species using saveas command and what we want to save and change the current to change to next species.

Program that create a function to calculate molecular mass of all species.

 function [mm]=Molecular_weight(species_name)
% Calculating Molecular weight
atoms=['H','C','N','O','A','S'];
Atomic_weight=[1,12,14,16,40,32];
mm=0;

for i=1:length(species_name)
    for j=1:length(atoms)
        if strcmpi(species_name(i),atoms(j))
            mm=mm+Atomic_weight(j);
            pos=j;
        end
        num=str2num(species_name(i));
    end
    if num>1
        mm=mm+Atomic_weight(pos)*(num-1);
    end
end
 end

Results:

Calculated the molecular weights of all species,

Plots

Below shown some sample plots which calculating thermodynamic properties of species.

Plots of 'O'

PLots of 'N'

PLots of 'OH'

In below figure shown we saved all plots of thermodynamic properties of 53 species in separate folder.

Conclusion:

Here, the functions 14 coeffiecients of all species are extracted by given thermodynamics data in MATLAB Program, calculated the thermodynamic properties such as specific heat,entropy,enthalpy for High and low temperature of each species and those plots are dumped in the separate folders and its shown. Molecular weights are calculated for all 53 species and displayed in command window.

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.