Challenge

Project 1 - Parsing NASA thermodynamic data

Objectives

1. Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.

2. Calculate the molecular weight of each species

3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.

4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.

5. The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.

Equations to calculate Specific heat Cp, Enthalpy H and Entropy S.

Fopen is used to open a file and parse the file.

To traverse through the file, use the (fgetl) command to get the file's line number and read it line by line until you reach the required result.

Reading the header file's text and reading the file line by line

After using the string split command, a cell array is created. A cell array is a form of data structure that stores data of various types and complete the work at hand

The Cp, H, and S functions were written to compute them using values from the file data.

The molecular weight function is used to calculate the molecular weight, which is shown in the command window.

The plots for Cp, H, S are created for each species and saved in a folder in jpg format to access the species with different
images

Creating folders for different molecules and save the plots for specific heat, enthalpy, and entropy in that folder and display
the molecular weight of different species in the command window

Main Code

clear all
close all
clc
%Open the the THERMO.dat file
f1=fopen('THERMO.dat','r')
%Skipping the first line containing file name
fgetl(f1);
%Reading the local,median and high temperatures
line=fgetl(f1);
a=strsplit(line,' ');
global_low_temp=str2num(a{2});
global_median_temp=str2num(a{3});
global_high_temp=str2num(a{4});
%Skipping 3 lines of file details
fgetl(f1);
fgetl(f1);
fgetl(f1);
%extracting data for each gas
for k=1:53
%step7
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));
%extracting all 14 coefficients
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
%initializing specific heat,entropy and enthalpy values to zero
cp=0;
H=0;
S=0;
%Calling the function where Cp,H and S are calculated
[Cp,H,S] = coefficient(local_low_temp,local_average_temp,local_high_temp,La,Ha);
%Molecular weight calculating function
M=Weight(Gas)
%plotting
Element=char(Gas);
T=local_low_temp:local_high_temp;
current=pwd;
mkdir(Element);
cd(Element)
figure(1);
plot(T,Cp,'color','g','linewidth',2);
legend(Gas);
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 temperrature');
figure(2);
plot(T,H,'color','r','linewidth',2);
legend(Gas);
txt1=['molecular weight:' num2str(M)];
text(500,max(H)*0.98,txt1,'color','b');
grid on
xlabel('temperrature(K)');
ylabel('enthalpy(kj/mol)');
title('enthalpy vs temperature');
figure(3);
plot(T,S,'color','b','linewidth',2);
legend(Gas);
txt1=['Molecular weight:' num2str(M)];
text(500,max(S)*0.98,txt1,'color','b');
grid on
xlabel('temperature(K)');
ylabel('entropy (KJ/mol)');
title('entropy vs temperature');
saveas(figure(1),'Cp.jpg');
saveas(figure(2),'H.jpg');
saveas(figure(3),'S.jpg');
cd(current);
end
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 stoichiometry in chemical reactions and equations. Molecular weight is commonly abbreviated by M.W. or MW. Molecular weight is either unitless or expressed in terms of atomic mass units (amu) or Daltons (Da).
%function calculate molecular mass
function mw = mol_wt(Gas)
%storing value
compound_name = (Gas)
% creating an array of elements
key_at = ['C','H','O','N','A'];
%storing the atomic mss value in the array
value_at = [12.0107 1.00794 15.994 14.0067 39.948];
%asssigning molecular mass as 0
mw =0 ;
%creating for loop to perform the task
for i = 1:length(compound_name)
for j = 1:length(key_at)
%comparing the string value
if strcmp(compound_name(i),key_at(j))
%if above comparision is true value of molecular mass is
%calculated
mw =mw + value_at(j);
%storing the position for further use
pos = j;
end
end
%converting the srting to number
k = str2num(compound_name(i));
%comparing if the value of k is greater than executed the further statements
if(k>1)
%molecular mass calculation
mw = mw + value_at(pos)*(k-1);
end
end
%command to print the molecular masss of the molecule
fprintf('Molecular Weight of the species %s is %f',compound_name ,mw);
end
SPECIFIC HEAT
Specific heat, the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree.
%function to calculate the Cp.
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;
%for lower temperature values
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);
%for higher temperature values
else
Cp(i)=R*(Ha(1)+Ha(2)*T+Ha(3)*T^2+Ha(4)*T^3+Ha(5)*T^4);
end
end
end
ENTHALPY
A thermodynamic system's enthalpy is defined as the sum of the system's internal energy and the product of its pressure and volume. At constant pressure, it is a handy state function that is utilised in many measurements in chemical, biological, and physical systems.
%function to calculate the H.
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;
%for lower temperature values
if T<= local_average_temp
H(i)=R*T*(La(1)+(La(2)*T)/2+(La(3)*T^2)/3+(La(4)*T^3)/4+(La(5)*T^4)/5+La(6)/T);
%for higher temperature values
else
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));
end
end
end
ENTROPY
Entropy is a measured physical attribute that is most usually linked with a condition of disorder, unpredictability, or uncertainty.
%function to calculate the S.
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;
%for lower temperature values
if T<= local_average_temp
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)));
%for higher temperature values
else
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
Command usage and code logic
Firstly, we open the file THERMO.dat file and reading the file using fopen command.
Then, we will read first THERMO using 'fgetl 'command
Next we split the temperatures using 'strsplit' command to convert it into cell array and then we will convert this string to number using str2num command to find low, high and mid temperatures.
Next we assign the temperature range using linspace command.
Then the reamining command lines are skipped using 'fgetl'command inside the loop.
In next step we run the loop to find 53 element in the data file by assigning fgetl command and then spliting into the cell array to find the species line and then find the species name and then find the number of the temperatures in the species line.
Then we read the new line. In order to read the coefficient in the data file we will first find the 'E' string using 'strfind' command to find before and after numbers of the coefficients then we will find remaining 14 coefficients and convert them to numbers from strings using 'str2num' command.

Out of which we will find the first 7 Coefficients which are for Higher temperature or temperatures above median temperature and then Second 7 Coefficients which are for Lower temperature or below median temperature.

Now we will specify Specific heat Cp, Entropy S, Enthalpy H functions to find these parameters using these 14 coefficients for Lower and higher Temperature. then we will specify molecular weight W function to find the Molecular weight of the different species.

In the next step0 we write the Molecular weight.txt file and 'fprintf' all the molecular weights of the species. and then close the txt file using 'fclose' command.
Now we change the directory using 'cd' command and then creating directory folder where we have to save our files
using 'mkdir' command and then we will change the directory to this created folder.
Now, we will plot Specific heat, Entropy and Enthalpy with the Temperature for different species. We we will use 'scanf' command to in the title command to create different species title.
Then we will use saveas command to save the plots of different species in the directory folder. Now we will change the directory to which our main code file runs in the MATLAB.fgetl - To proceed to reading new line
Molecular Weight of Each Compound
Specific heat ,Enthalpy and entropy plots of O2,N2 & C02
Folders containing plots of all elements
So in this program we were able to succesfully read the thermodynamic data file, output the plots fo specific heat, enthalpy and entropy & also the molecular weight of each component
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.