Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:-Aim of this project is to read NASA thermodynamic data file using matlab code and calcualtes moleculter weigth, Enthlpy,Specfic heat and Entropy of 53 species given in data file using maltab fuction code.

Objective:- Objective of this project are

  1. First Read thermo data file using matlab code.
  2. Read globel temperature range and convert them to temperature varibles for matalb claucation
  3. Read speices name and caluclate theire molecular weigth using user define matlab function
  4. Reading low themperature and higth temperature coeffient and use them in function creating for calculating enthalpy, entropy and specific heat 
  5. plot the temperature vs enthaly , entropy and specific heat varion for each speiecs and save them to a sperate folder name as species name 

Theory:-

NASA Thermo data file:- It is a data file usualy present by ploynomial coefiecint for each species present in chemical kinematic so that we use these in a ploynomial equalstion to find out thermodyanamic properires like 

1. Specific Heat :- Specific heat, the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. It's unit is jule per kg per celsius degree or Jule per mol per K and it is represent by Cp

2. Enthalpy :-Enthalpy, a property of a thermodynamic system, is the sum of the system's internal energy and the product of its pressure and volume. 

3. Entropy:- Entropy is a scientific concept, as well as a measurable physical property, that is most commonly associated with a state of disorder, randomness, or uncertainty.

These are caluclate by these formula

Now if you lock at data file there are 14 coefficient for each species 

Fitst 7 are for lower temperature and next 7 are for higher temperature

Using these thing we make function code for claclating these themodyanamic properies

# Fuction code to calculate Specific Heat

%% Specific Heat function
function cp=Specifi_Heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,Temperature,global_medium_temperature,R)
  
  if Temperature<=global_medium_temperature
    
    %% if themperature value less then 1000 use first 7 coeficents in equalstion to calulate cp
    cp=R*(c1+c2.*Temperature+c3.*Temperature.^2+c4.*Temperature.^3+c5.*Temperature.^4);
  else
    
    %% temperature value is grater then 1000 use next 7 coeffient in equastion to calcualte cp
    cp=R*(c8+c9.*Temperature+c10.*Temperature.^2+c11.*Temperature.^3+c12.*Temperature.^4);
    
  endif

# Function code to calculate Enthlapy

%% Enthalpy function
function H=Entahlpy(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,Temperature,global_medium_temperature,R)
  
   %% if themperature value less then 1000 use first 7 coeficents in equalstion to clauclate H
  if Temperature<=global_medium_temperature
    H=R*Temperature.*(c1+(c2.*Temperature/2)+((c3.*Temperature.^2)/3)+((c4.*Temperature.^3)/4)+((c5.*Temperature.^4)/5)+(c6./Temperature));
  else
    
    %% temperature value is grater then 1000 use next 7 coeffient in equastion to clacualte H
    H=R*Temperature.*(c8+(c9.*Temperature/2)+((c10.*Temperature.^2)/3)+((c11.*Temperature.^3)/4)+((c12.*Temperature.^4)/5)+(c13./Temperature));
    
  endif

# Function code to calcualte Entropy

%% Entropy function
function S=Entropy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,Temperature,global_medium_temperature,R)
  if Temperature<=global_medium_temperature
    
    %% if themperature value less then 1000 use first 7 coeficents in equalstion to clacualte S
    S=R*((c1.*log(Temperature))+(c2.*Temperature)+((c3.*Temperature.^2)/2)+((c4.*Temperature.^3))+((c5.*Temperature.^4)/4)+c7);
  else
    
    %% temperature value is grater then 1000 use next 7 coeffient in equastion to claculate S
    S=R*((c8.*log(Temperature))+(c9.*Temperature)+((c10.*Temperature.^2)/2)+((c11.*Temperature.^3))+((c12.*Temperature.^4)/4)+c14);
    
  endif

# Logic Explain 

In each function we use common argument as tmperature ( array of 1000 index values ), Global medium tmperture , R (universal gass constant) and higher and low temperature coeficient.

using the condtional statement we use low and high temperature coeficent in the given equalstion to calculate thrmodyanamic properties and output as Cp, H and E

# Molecular weigth calcualtion Function

function MW=molecular_weight(Species_name)
  
 %% we have Species_name in our arugment which contain strings

%% Common atom use is these species are  H, C , O ,N and A
 %% make an arey of thses atoms
 
at=['H' , 'C', 'O', 'N', 'A'];

%% These atom have konw atomic weigth sombrero
%% make another arry of these atoms weigth respectivly

AW=[1.008 12.011 15.999 14.007 39.948];

%% make initial molecular weigth 0

MW=0;

for i=1:length(Species_name)  %% loop for species name
  for j=1:length(at)          %% loop for std atoms array
    if strcmp(Species_name(i),at(j))    %% if string value of species name match to std atoms 
      
      MW=MW+AW(j);     %% molecular_weight is sum of privious mw to cuurent atom weigth
      
      po=j;             %�fine postion of atom 
    endif
  endfor
  
  n=str2num(Species_name(i));  %% count number of attom
  
  if n>1
    MW=MW+AW(po)*(n-1);       %�lcualte atomic weigth for compund to two or more then two same atom like O2 ,H2, N2
  endif
end

%% print molecular wigth in commond window
fprintf('Molecular Weight of the species %s is %f \n',Species_name,MW);


end

 # Function code logic

1. First in this function our argument is species name which is gernally a chemical formula which contain later and number both.

in these 53 species i found they are made of atoms like H,C,O ,N and A. So first i made a array of these atoms and then make an array of atomic weigth of these atom.

2. Now i run a for loop for each index of species name for example a Species name is H2O it contain 3 index point so it run 3 times 

now we run another for loop for array of atoms like H,C,O,N 

now i use conditional operatior with command strcmp

this command is use to compare two string and here two string are 

Species_name(i) and at(j)

here this if first for loop of species name and at so i and j both are 1

so this condition is true now my molecurur weigth 

MW=MW+AW(j);

Here our initial MW value is zero and AW(1) is 1.008

now po=j i.e. postion of maching atom is find 

now for second loop run till j=5 and all time condition not true so our MW and po not change 

now we use an another comman str2num to conver number from string Speciec_name(i)

here this first loop still runing for i=1, and first index of Speciec_name is H which is not a number

and a contion alos apply n>1 which is also not apply here so my first loop is end here for i=1.

3. Now it run again for i=2, now here Specie_name(2) is 2 first condtion is now true for all value of j so the value of MW and po not change now n=str2num(Speices_name(i)) is come to a vlaue 2.

now our contion n>1 is true 

so moleculer wieigth

MW=MW+AW(po)*(n-1)

ie.

AW(1) is 1.008

MW=1.008+1.008*(2-1)

MW=2.016

4. Now it run for next for i=3, now Specie_name(3) is O 

now if we run second loop when j=1 , the contion is not true it is trun ony when n=3. here O is match is both string 

so now moleculer weigth

MW=MW +AW(i)

MW=MW +AW(3)

MW=2.016+15.999

MW=18.015000

now second conition not true here and my loop also end here because i=3.

5. now at end use fprintf to print text and MW on common window 

fprintf('Molecular Weight of the species %s is %f \n',Species_name,MW);

Molecular Weight of the species H2O is 18.015000

# Main code

close all
clear all
clc

%% Input varible R which is universla gass constatnt in J.mol^-1.k^-1
R=8.3144568;

%% Reawing Thermo data file
f1=fopen('THERMO.dat','r');

gt=fgetl(f1);  %% get first line which is header

global_temp=fgetl(f1);  %% Second line is global temperature range

temperature=strsplit(global_temp);                %% Split this string to varible temperature
global_low_temperature=str2num(temperature{2});    %% Second index value of temperature is store in varible global_low_temperature
global_medium_temperature=str2num(temperature{3}); %% Thired index value of temperature is store in varible global_medium_temperature
global_heigh_temperature=str2num(temperature{4});  %% Fourth index value of temperature is store in varible global_heigh_temperature

%�fining a array name Temperature having 1000 index value range between global_low_temperature to global_heigh_temperature

Temperature=linspace(global_low_temperature,global_heigh_temperature,1000);

%% Next three lines are comments on header line which we don't needed so skip these
%% use for loop to skip  comments
for i=1:3
  gt=fgetl(f1);
end
%% Next all lines are set of four lines for 53 species which contain 
%% species name , local temperature range in first line
%% And coefficients for low tmeperature and high temperature in antother lines

for i=1:53
  
tline=fgetl(f1);         %% First lines read in tline
Ss=strsplit(tline,' ');   %% Split first line string after gap which is species name
Species_name=Ss{1};       %% This first index is store in species name

%% First line coeffients or second line of species

line1=fgetl(f1);        %% read this line as store in line1
Co=strfind(line1,'E');  %% find string in line one till E

c1=line1(1:Co(1)+3);    %% read coefinect in Co from statrt to till E + 3 index
c1=str2num(c1);        %% conver string to number and store in c1

c2=line1(Co(1)+4:Co(2)+3); %% read coefinect in Co from last+1 to next E + 3 index
c2=str2num(c2);             %% conver string to number and store in c2

c3=line1(Co(2)+4:Co(3)+3);  %% read coefinect in Co from last+1 to next E + 3 index
c3=str2num(c3);               %% conver string to number and store in c3

c4=line1(Co(3)+4:Co(4)+3);  %% read coefinect in Co from last+1 to next E + 3 index
c4=str2num(c4);             %% conver string to number and store in c4

c5=line1(Co(4)+4:Co(5)+3); %% read coefinect in Co from last+1 to end index   
c5=str2num(c5);            %% conver string to number and store in c5

%% Second line coefficients

line2=fgetl(f1);          %% read this line as store in line2
Co=strfind(line2,'E');    %% find string in line one till E

c6=line2(1:Co(1)+3);      %% read coefinect in Co from statrt to till E + 3 index
c6=str2num(c6);           %% conver string to number and store in c6

c7=line2(Co(1)+4:Co(2)+3); %% read coefinect in Co from last+1 to next E + 3 index
c7=str2num(c7);             %% conver string to number and store in c7

c8=line2(Co(2)+4:Co(3)+3);  %% read coefinect in Co from last+1 to next E + 3 index
c8=str2num(c8);             %% conver string to number and store in c8

c9=line2(Co(3)+4:Co(4)+3);  %% read coefinect in Co from last+1 to next E + 3 index
c9=str2num(c9);             %% conver string to number and store in c9

c10=line2(Co(4)+4:Co(5)+3); %% read coefinect in Co from last+1 to end index  
c10=str2num(c10);           %% conver string to number and store in c10

%% Second line coefficients

line3=fgetl(f1);            %% read this line as store in line3
Co=strfind(line3,'E');      %% find string in line one till E

c11=line3(1:Co(1)+3);       %% read coefinect in Co from statrt to till E + 3 index
c11=str2num(c11);           %% conver string to number and store in c11

c12=line3(Co(1)+4:Co(2)+3); %% read coefinect in Co from last+1 to next E + 3 index
c12=str2num(c12);           %% conver string to number and store in c12

c13=line3(Co(2)+4:Co(3)+3); %% read coefinect in Co from last+1 to next E + 3 index
c13=str2num(c13);           %% conver string to number and store in c13

c14=line3(Co(3)+4:Co(4)+3); %% read coefinect in Co from last+1 to end index  
c14=str2num(c14);           %% conver string to number and store in c14


%�lling function of molecular weigth calculation with argument Specie_name

MW(i)=molecular_weight(Species_name);

%�lling function for Specific heat calculation with argument requred data as per equastion in problem
cp=Specifi_Heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,Temperature,global_medium_temperature,R);

%�lling function for Enthalpy calculation with argument requred data as per equastion in problem
H=Entahlpy(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,Temperature,global_medium_temperature,R);

%�lling function for Entropy calculation with argument requred data as per equastion in problem
S=Entropy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,Temperature,global_medium_temperature,R);


%% Creating directoy and saving files

dir=pwd;               %% store curent directoy path in varible dir
mkdir(Species_name);   %% create directoy name as species name
cd(Species_name);      %% Go to that directoy 

%% create txt document to store molecular weigth of species

Mol_wt=fopen('Moleculer weight.txt','w');     %% open text file and writing it 

fprintf(Mol_wt,'Molecular Weight of the species %s is %f ',Species_name,MW(i)); %% print data in txt file 

fclose(Mol_wt);  %% close txt file

%% plot figures

figure(1)
plot(Temperature,cp,'linewidth',2,'color','r');
title(sprintf('Temperature vs Specific Heat for %s',Species_name),'fontsize',14)
xlabel('Temperature in [K]')
ylabel('Specific Heat in [J/mol.K]')

%% save this figure in current directoy
saveas(figure(1),'Specific Heat')


figure(2)
plot(Temperature,H,'linewidth',2,'color','b');
title(sprintf('Temperature vs Enthalpy for %s',Species_name),'fontsize',14)
xlabel('Temperature in [K]')
ylabel('Enthalpy in [J/mol]')

%% save this figure in current directoy
saveas(figure(2),'Enthalpy')


figure(3)
plot(Temperature,S,'linewidth',2,'color','k');
title(sprintf('Temperature vs Entropy for %s',Species_name),'fontsize',14)
xlabel('Temperature in [K]')
ylabel('Eentropy in [J/mol]')

%% save this figure in current directoy
saveas(figure(3),'Entropy')

%% come to root directoy dir 'where your program and function and data file are present'
cd(dir)
end        %% for loop end

Code logic explain

1. First we close , clear all figure and varible and clear comman window. Then input a varible R which is universal gass constant.

2. Now we open and write the THERMO.dat file in varible f1

Now we use command fgetl to read file line by line

first gt=fgetl(f1);

it runs first line of data file and store it in varible gt

second global_temp=fgetl(f1); 

so it read second line of data file store it in varible globa_temp

now if you look at global_temp it is 1x30 array i.e. it contain 30 index data not this is ony one index and it is ony one string. see varible window

Now we split this string value and store in varible temperature 

temperature=strsplit(global_temp);

now first index is emplty because in data file here space 

so we store index value 2, 3 and 4 of temperature array in varible

global_low_temperature,global_medium_temperature and global_high_temperature

Now we make an array of Temperature which have 1000 index value between 300 to 5000

Temperature=linspace(global_low_temperature,global_heigh_temperature,1000);

3. Now next 3 line are comment or header line so we have to skip these so we use for loop and store these in varible gt.

4. Now next four line containing species data which is repeted for all 53 species so we run a for loop for i=1 to 53

now first line of these four line conatin species_name and local temperature limit

Now firts index is our species name so store this in varible Species_name

now second line store in line1 varible and first find postion of E in this line and for E+3 postion we count as a one coeficent so we store it in varible c1,c2 and so on

Here logic it that we make an array which contain postion of E in it index

and we store string of line1 to E+3 point and convert this string to Nubmer and store it to coeficent 

and do it for all coeficent in this line 

now this is also repet for next line also and also for last line.

After this we get 14 coeficent varibles.c1,c2.....c14.

5. Call all the function that we created using these coeficent,specie_name and globle temperatures

and there output is save in varible MW ,Cp ,H and S for each value of i ie each species

6. Now in this for loop we creates directory and save datas so that each species have a seperates folder of datas

first we check our current directory by pwd and store this in dir varible

now using mkdir make a directory name of species

Now with command cd (come to directory) we come into this species name directory i.e. our curent path is this directory

7. now we write a file name moleculer weigth.txt and print data Molecular Weight of the species  is 

by using fprintf

fprintf(Mol_wt,'Molecular Weight of the species %s is %f ',Species_name,MW(i));

here it print text , current species name and molecurel weigth of current species

close this file

Now plot figure(1) , Temperature vs Cp and save as 

it save in current dirctory

now plot figure(2), Temperature vs H and save as

 also save it

now plot figure(3), Temperature vs s and alos save it

now after save all figure and data file come to our program directory by command cd

cd(dir)

and end the for loop.

8. This for loop run for 53 species and calculate moleculer weigth,Cp, H and S for each species in each excution of loop and creates an folder and save data and figure in each extion of loop.

# Plot of CP

for o2

for N2

For co2

# Enthaly plot

for O2

for N2

for CO2

 # Entropy Plot

for O2

for N2

For CO2

# Comman window screanshot

# Common error in program

1. While printing "molecular weigth of Species bane is mw'

we put this in function and use \n for next line other wise it comes in a single line

while calling this funtion if we dont use ; at end mw also print with this.

2. findstr function not work so use strfind .

3. In molecular weigth if we use j it not give rigth postion of at so we use varible po inside the loop  and use it after the loop.

# Conclusion

1. In this project we learn how to read and write data file using matlab code.

2. In this challange we learn about string and function strsplit,str2num,strfind ect.

4. In this challange we learn how to check directory and make directory using code pwd,mkdir and cd.

5. In this challange we understand NASA THERMO.data file and how to read it and we can use it to calculate thermodynamic properties like enthlpy, entropy and specific heat.

6. In this challange we learn how to calculate molecular weigth using species name.

# Referances

1. https://in.mathworks.com/help/matlab/ref/strfind.html

2. https://in.mathworks.com/help/matlab/ref/strcmp.html

3. https://in.mathworks.com/help/matlab/ref/str2num.html

4. https://in.mathworks.com/matlabcentral/answers/196582-best-way-to-parse-text-file

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.