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.

OBJECTIVE:

  • To develop a function that extracts the 14 coefficients and calculations the enthalpy, entropy, and specific heat for all the species in the data file.
  • Develop a code to calculate the molecular weight. of each species and display it in the command window.
  • To plot the Cp, Enthalpy, and entropy for the local temperature range(low temperature, High temperature) specific for species.
  • To save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name.
  • All of these should be generated automatically.

THEORY

File Parsing: It is the method of splitting the file or other input into a piece of data that can be easily stored and manipulated. By doing this we are going to split a string into parts and then recognize the parts to convert it into something simpler than a string.

Specific Heat: Specific heat is the amount of heat energy required to raise the temperature of the body per unit of mass. Specific heat is also known as specific heat capacity or mass specific heat.

In SI units specific heat is the amount of heat in joules required to raise 1 gram of a substance 1 Kelvin

Enthalpy: when a substance changes at constant pressure, enthalpy tells how much heat and work was added or removed from the substance, Enthalpy is similar to energy but not the same. when a substance grows or shrinks, energy is used up or released

Entropy: The entropy of an object is a measure of the amount of energy that is unavailable to do work. entropy is also a measure of the number of possible arrangements of atoms a system can have. In this sense, entropy is the measure of uncertainty or randomness.

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 staichiometry in chemical reactions and equations.

Gas constant: The gas constant is a physical constant denoted by R and is expressed in terms of units of energy per temperature increment per mole. The gas constant value is equivalent to Boltzmann constant but expressed as the pressure-volume product instead of energy per increment of temperature per particle.

Principle: NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H, and S. They have also documented the coefficients that are required to evaluate these polynomials for 1000+ species.

Where,

Cp = Specific heat(J/k kg)

H = Enthalpy (J)

s = Entropy (J/k)

R = Universal gas constant = 8.314 (J/mol.k)

T = Temperature (k)

Explanation of Code:

1. The program begins with clear all, close all, clc commands to free up the command window and the workspace

2. we will then read the data from the external NASA thermodynamic data file using fopen command is used to open the file and fgetl command which is used to read the content in the file line by line

3. After we use the fopen command we will be assigning to the variable called f1. Thus, the fget(f1) is the command acts like a line cursor, which will retrieve the next time while running this command for the second time and so on.

4.Now, we will be skipping the first 5 lines to enter the pattern of values of the respective gas species. we will be using the for loop command to process the same for all the gas species

5. The name of the gas species we will be founf out sing findstr command and for finding out the species we are going to locate the string which is present in after each species name

6. And then we are going to use which tells s the exact location of the species name, using which the nameof the gas species is stored in the variable species name

7. The local temperatures for the respective gas species is found similarly using findstr command and we are going to locate the string.using which we will find out the values of low temoerature, mid temperature and high temperature which is assigned ti the variable t1,t2 and t3

8. Now, we are going to extract the coefficient for the gas soecies in which the coefficient locations are like in the 2nd row, next 5 in the 3 rd row and the rest in the 4 th row

9. We are going to extract these values similarly like we did for finding oout the local temoeratres by using the findstr and str2num commands and store it in the variable a1 to a14.

10. The values from a1 to a7 represent the high temperature coefficient and the values from a8 to a14 represent the low temprature coefficient

11. Temperature range values from low temperature to high temperature is assigned using the linspace command

12.the for loop command is used to calculate the values of specific heat,enthalpy and entropy and the if-else command is also used to find out the required values.

13. the values of molecular weight is calculated for the respectivve gas species and is displayed in the command window

14.now we are going to create plots for the obtain sopecific heat,enthalpy and entropy values and save it in respective species name folder automatically.

15.to save the plots in the species name folder, saveas command is used to save the plots in the current folder.

16. After which hto change the current directly to the main folder the oldfolder = cd() command is used.

MAIN CODE

clear all 
close all
clc

f1 = fopen('THERMO.dat','r'); % opening the file for readinf
first_line = fgetl(f1);       % reading the comment lines

R = 8.314  %R is the universal gas constant J/(mol-K)

temp = fgetl(f1)
%reading text from a header file
%reading the global temperature from the next line
 
A = strsplit(temp, ' ');

% cell arrat is generated 1x4 cell array
global_min_temperature = str2num(A{2});
global_med_temperature = str2num(A{3});
global_max_temperature = str2num(A{4});

%temperature range
Temperature = linspace(global_min_temperature,global_max_temperature,500);

%skipping the 3comment lines
for j = 1:3;
    k = fgetl(f1);
end

%running for loop  for 53 times as there are 53 species
%Extracting all species data

for i = 1:53
 
    %parsing of the species block
    tline = fgetl(f1);
    S = strsplit(tline,' ');
    Species_name = S{1};

    %Extracting coefficients from first line
    line2 = fgetl(f1);
    x = findstr(line2, 'E')

% finding the  the 5 coefficient
x1 = line2(1:x(1)+3);
x1 = str2num(x1);

x2 = line2(x(1)+4:x(2)+3);
x2 = str2num(x2);

x3 = line2(x(2)+4:x(3)+3);
x3 = str2num(x3);

x4 = line2(x(3)+4:x(4)+3);
x4 = str2num(x4);
  
x5 = line2(x(4)+4:x(5)+3);
x5 = str2num(x5);

%line3 5 coefficients
line3 = fgetl(f1);
x = findstr(line3,'E');

x6 = line3(1:x(1)+3);
x6 = str2num(x6);

x7 = line3(x(1)+4:x(2)+3);
x7 = str2num(x7);

x8 = line3(x(2)+4:x(3)+3);
x8 = str2num(x8);

x9 = line3(x(3)+4:x(4)+3);
x9 = str2num(x9);

x10 = line3(x(4)+4:x(5)+3);
x10 = str2num(x10);

%line4 4 coefficients
line4 = fgetl(f1);
x = findstr(line4,'E');

x11 = line4(1:x(1)+3);
x11 = str2num(x11);

x12 = line4(x(1)+4:x(2)+3);
x12 = str2num(x12);

x13 = line4(x(2)+4:x(3)+3);
x13 = str2num(x13);

x14 = line4(x(3)+4:x(4)+3);
x14 = str2num(x14);

% entropy calculations
S = entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,Temperature,global_med_temperature,R);

% cp calculation means specific heat calculation
Cp = Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,Temperature,global_med_temperature,R)

% Enthalpy calculation is H
H = enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,Temperature,global_med_temperature,R)

mw(i) = mol_wt(Species_name);
 %Reading and writinh of the molecular wieght in the list of molecular
 %wieght
 Moll_wt = fopen('Molecular Weight.txt','w');
 fprintf(Moll_wt,'Molecular Weight of the species %s is %f ',Species_name,mw);
 fprintf(Moll_wt,'/n');
 fclose(Moll_wt);
 % Plotting on the graph for species fo the temperature and specific
 % heat
 %Creating a folder for saving the species with its properties
 directory_curr = pwd;
 cd(Species_name);


% plotting the graph in different plotting window
%plot 1 for the temperature and entropy
%plot 2 for the temperature and enthalpy
%plot 3 for the temperature and specific heat


%plot 1 enthalpy and Temperature

figure(1)
plot(Temperature,H,'linewidth',3,'color','r')
xlabel('Temperature in [k]');
ylabel('Enthalpy[KJ/mol]')
grid on
title(sprintf('Variation in Enthalpy vs Temperature %s',Species_name));
 saveas (figure(1),'Enthaly.jpg');


%plot 2 entropy and Temperature
figure(2)
plot(Temperature,S,'linewidth',3,'color','g');
xlabel('Temperature in [k]');
ylabel('Entropy [KJ/mol-k]')
grid on
 title(sprintf('Variation in Entropy vs Temperature %s',Species_name));
 saveas (figure(2),'Entropy.jpg');

%plot 3 specific heat and Temperature
figure(3)
plot(Temperature,Cp,'linewidth',3,'color','b');
xlabel('Temperature in [k]');
ylabel('specific heat [KJ]')
grid on
 title(sprintf('Variation in Specific Heat vs Temperature %s',Species_name));
 saveas (figure(3),'Specific Heat.jpg');
 cd(directory_curr);

end

FUNCTION CODE FOR MOLECULAR WEIGHT

%function calculate molecular mass
function mw = mol_wt(sp_name)
%storing value
compound_name = (sp_name)
% 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

FUNCTION CODE FOR ENTROPY

%Funtion for the entropy
function [S] = entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,temp,global_med_temperature,r)
%Checking for the temperaure
%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
if(temp<=global_med_temperature)
 S = r.*(x8.*log(temp) +(x9.*temp) +(x10*((temp).^2)/2) + (x11*((temp).^3)/3) + (x12*((temp).^4)/4) + (x14));
 
else
 S = r.*(x1.*log(temp) +(x2.*temp) +(x3*((temp).^2)/2) + (x4*((temp).^3)/3) + (x5*((temp).^4)/4) + (x7));
 
end

FUNCTION CODE FOR ENTHALPY

%Function for the enthalpy
function [H] = enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,temp,gb_m,r)
%Checking for the temperature
%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
if(temp<=gb_m)
 H = r*temp.*(x8 + ((x9*temp)/2) + ((x10*(temp).^2)/3) + ((x11*(temp).^3)/4) + ((x12*(temp).^4)/5) + ((x13./temp)));
else
 
 H = r*temp.*(x1 + ((x2*temp)/2) + ((x3*(temp).^2)/3) + ((x4*(temp).^3)/4) + ((x5*(temp).^4)/5) + ((x6./temp)));
end

FUNCTION CODE FOR SPECIFIC HEAT

%Funtion for the Specific Heat
function [Cp] = Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,temp,gb_m,r)
%Checking for the temperaure
%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
if(temp<=gb_m)
 Cp = r.*(x8 +(x9.*temp) +(x10*(temp).^2) + (x11*(temp).^3) + (x12*(temp).^4));
 
else
 Cp = r.*(x1 +(x2.*temp) +(x3*(temp).^2) + (x4*(temp).^3) + (x5*(temp).^4));
 
end

OUTPUT PLOTS

NO2 PLOTS

O2 PLOTS

CO2 PLOTS

FOLDERS CONTAINING ALL THE SPECIES NAMES

  • Mention the errors faced while programming.

wrote the coding errors in function code and also save the function file into another folder

  • Steps taken to overcome those errors.

checking entire code where the error is

  • Screenshots showing errors thrown 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.