Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:-Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.File NASA thermodynamic data.

The formulae are shown below.

Here R is the universal gas constant, T is the temperature.

The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.

Objective:-

1. Calculate the molecular weight of each species and display it in the command window.

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

The logic used behind code:-

In the given equation only seven coefficient were writesn but there are 14 coeffiecnt in Therm.dat file

so i have created Cp, H and S equation for other seven coeffiecien.

which are

Cp/R = a8 + a9 T + a10 T^2 + a11 T^3 + a12 T^4

H/RT = a8 + a9 T /2 + a10 T^2 /3 + a11 T^3 /4 + a12 T^4 /5 + a13/T

S/R = a8 lnT + a10 T + a11 T^2 /2 + a12 T^3 /3 + a13 T^4 /4 + a14

function used in code:-

1- To read a file "fopen" command has been used.

2- fgetl-read the file line by line.

3- strsplit:- Split string or character vector at the specified delimiter.

4- str2num:- Convert string into a number

5-currentfile: Identify current file
6-mkdir: creat new folder with species name
7-cd: change the current folder for the next species

The logic used to extract the local temperature and coefficients that.

1- identify any repeating element from the string and get the position by using "strfind" command.

2- After getting position converts the string into number and apply after and before numeric addition logic to calculate the coefficient.

3- above logic has to be applied for each line and all coefficients get extracted.

4- Applied the logic from 1-4 for all species using 'for' loop.

Code to create a function for Specific heat

function cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,temp,R,Local_mid_temp)
%cp=(a1+a2*T+a3*T^2+a4*T^3+a5*T^4)*R
T=temp;
if T>Local_mid_temp
    cp=(a1+(a2.*T)+(a3.*T.^2)+(a4.*T.^3)+(a5.*T.^4)).*R;
    else 
    cp=(a8+(a9.*T.^2)+(a10.*T.^3)+(a11.*T.^4)+(a12.*T.^5)).*R;
end

Code to create a function for Entropy

function S=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,R,Local_mid_temp)
%S=(a1*lnT+a2*T+a3*T^2/2+a4*T^3/3+a5*T^4/4+a7)*R
T=temp;
if T>Local_mid_temp
   S=((a1.*log(T))+(a2.*T)+((a3.*T.^2)/2)+((a4.*T.^3)/3)+((a5.*T.^4)/4)+a7)*R;
else
    S=((a8.*log(T))+(a9.*T)+((a10.*T.^2)/2)+((a11.*T.^3)/3)+((a12.*T.^4)/4)+a14)*R;
end

Code to create a function for Enthalpy

function S=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,R,Local_mid_temp)
%S=(a1*lnT+a2*T+a3*T^2/2+a4*T^3/3+a5*T^4/4+a7)*R
T=temp;
if T>Local_mid_temp
   S=((a1.*log(T))+(a2.*T)+((a3.*T.^2)/2)+((a4.*T.^3)/3)+((a5.*T.^4)/4)+a7)*R;
else
    S=((a8.*log(T))+(a9.*T)+((a10.*T.^2)/2)+((a11.*T.^3)/3)+((a12.*T.^4)/4)+a14)*R;
end

Code to create a function for Molecular weight

function [M]=molecular_weight(Species_name)
% Molecules=Name
Name=['H','O','N','C','A'];
standard_weight=[1.007,15.999,14.006,12.0096,39.792];
fprintf("molecular weight of ")
fprintf(Species_name)
fprintf(":")
M=0;  % intitial molecular wt.
for i=1:length(Species_name);
   for j=1:length(Name);
    if strcmp(Species_name(i),Name(j));
       M=M+standard_weight(j);
      g=j;
    end
   end
  n=str2num(Species_name(i));
  if n>1;
      M=M+standard_weight(g)*(n-1);
  end
end
end

Main body code

clear all
close all
clc

% code to calculate the Cp,H,S and M values for diffrent species from
% nasa.therm file
% taotal specied 53
% calulate the moleculer weight for 53 species and store the plot 

f1=fopen('THERMO.txt','r')
fgetl(f1);
global_temp=fgetl(f1);       % reading of first line
x=strsplit(global_temp,' ');  % spliting of string 
global_localtemp=str2num(x{2});  % converting string to number
global_midtemp=str2num(x{3});
global_hightemp=str2num(x{4});
fgetl(f1);  % reading second line
fgetl(f1);  % reading third line
fgetl(f1);  % reading fourth line

R=8.314  %universal gas constant J/mol-K

for i=1:53   % iteration to access the local temp and coefficient for 53 species.
   
line0=fgetl(f1);  % reading line 5 i.e first line for the species
y=strfind(line0,'.');   % find '.' form the string 
A=strsplit(line0);      % Split the string as find string consider the single value
Species_name=(A{1});    % store the individual species 
% to find the local temp 
t1=line0(49:y(1)+3);    
Local_min_temp=str2num(t1); 
t2=line0(y(1)+6:y(2)+3);
Local_max_temp=str2num(t2);
t3=line0(y(2)+6:y(3)+3);
Local_mid_temp=str2num(t3);

%local coefficient 
% hight temp coefficient
line1=fgetl(f1);                 %line 2 from the same species
a=strfind(line1,'.');
a1=line1(1:a(1)+12);
a1=str2num(a1);
a2=line1((a1)+13:a(2)+12);
a2=str2num(a2);
a3=line1(a(2)+13:a(3)+12);
a3=str2num(a3);
a4=line1(a(3)+13:a(4)+12);
a4=str2num(a4);
a5=line1(a(4)+13:a(5)+12);
a5=str2num(a5);
line2=fgetl(f1) ;                     %line third from the same species
b=strfind(line2,'.');
a6=line2(1:b(1)+12);
a6=str2num(a6);
a7=line2(b(1)+13:b(2)+12);
a7=str2num(a7);
% low temperature coefficient
a8=line2(b(2)+13:b(3)+12);
a8=str2num(a8);
a9=line2(b(3)+13:b(4)+12);
a9=str2num(a9);
a10=line2(b(4)+13:b(5)+12);
a10=str2num(a10);
line3=fgetl(f1) ;                  %line fourth for the same species
c=strfind(line3,'.');
a11=line3(1:c(1)+12);
a11=str2num(a11);
a12=line3(c(1)+13:c(2)+12);
a12=str2num(a12);
a13=line3(c(2)+13:c(3)+12);
a13=str2num(a13);
a14=line3(c(3)+13:c(4)+12);
a14=str2num(a14);  % total fourteen coefficient calculated for a speciies

% local teamp range 
temp=linspace(Local_min_temp,Local_max_temp,1000);
% function calling for specific heat
%Speciifc heat:%cp=(a1+a2*T+a3*T^2+a4*T^3+a5*T^4)*R
cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,temp,R,Local_mid_temp);
% function calling for enthalpy
% enthalpy H=(a1+a2*T/2+a3*T^2/3+a4*T^3/4+a5*T^4/5+a6/T)*R*T
H=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,R,Local_mid_temp);
% function calling for Entropy
%Entropy S=(a1*lnT+a2*T+a3*T^2/2+a4*T^3/3+a5*T^4/4+a7)*R
S=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,R,Local_mid_temp);
% function calling for moleculer weight 
%molecular weight
M=molecular_weight(Species_name)

% storing the values in diffrent file in directory 
currentfile=pwd;       % Identify current file 
mkdir(Species_name);   % creat new folder with species name
cd(Species_name);      %change the current folder for next species

% plot of temp vs Specifie heat 
figure(1);
plot(temp,cp,'linewidth',2,'color','b')
xlabel("Temperature[K]")
ylabel("Specific Heat [J/Kg.K]")
title("Temp Vs Specific Heat of Species")  
% save the fig in directory 
saveas(figure(1),'Specificheat.jpg')    

% plot of temp vs Enthalpy
figure(2);
plot(temp,H,'linewidth',2,'color','r')
xlabel("Temperature[K]")
ylabel("Enthalpy [J/Kg.K]")
title("Temp Vs Enthalpy of Species")
% save the fig in directory 
saveas(figure(2),'Enthaply.jpg')

% plot of temp Vs Entropy
figure(3)
plot(temp,S,'linewidth',2,'color','g')
xlabel("Temperature[K]")
ylabel("Entropy [J/Kg.K]")
title("Temp Vs Entropy of Species")
% save the fig in directory 
saveas(figure(3),'Entropy.jpg')
%save the plot in current file
cd(currentfile)
end

PLot for O2

PLOT for N2

PLot for CO2

Error faced :

1- Was not able to calculat the dia and tri atomic moleculer weight

the error i was getting because function value i specified as M and in for loop wt.

After taking help from Skill lync techical support person i was able to identify the error.

2- I was not able to identify the local temp because i gave written in the cammand windo y=strfind(line0,'G');

the output i was getting rendum .then after cunsulting with technical support person i was able to fin the local teamp using y=strfind(line0,' .');

3- I was getting issue to plot the specific heat and teamp, entropy & temp

The random plot i was getting .Then i find that the equation are missing . operation in multiplication and division.

Also i have written ln in place of log.

Species weight and folder location added in attachements.

References used.

https://in.mathworks.com/help/

https://www.britannica.com/science/atomic-weight

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.