Challenge

Project 1 - Parsing NASA thermodynamic data

Objective:- In the NASA Thermodynamic data file, the FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients. Use the LOCAL TEMPERATURE RANGES which is unique for each individual species, not the GLOBAL TEMPERATURE RANGE.

  1. Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species.
  2. Calculate the molecular weight of each species and display it in the command window.
  3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
  4. Plots for o2, N2 and Co2 species.

Approach:- Loading the NASA Thermodynamic data in the reading format. Using the FOR-Loop to read the file line by line. Using the FOR-Loop to read the species data line by line. Getting the Temperature values for the species i.e., Low, Moderate & High temperatures. Fetching the 14 different values of the data. Creating the functions for the operation i.e., Specific Heat, Enthalpy, Entropy & to calculate the Molecular Weight. Plotting the figures for all the three functions seperately and storing them all in the seperate directories. Using the value of R i.e., Gas Constant as 8.314.

Code:

clear all
close all
clc

%Opening the file
f1 = fopen('THERMO.dat','r');

%Using For-Loop for reading the first 5 lines
for i = 1:5
    a = fgetl(f1);
end

%Using For-Loop for the reading the 53 species
for i = 1:53

%Getting first header line
header_line = fgetl(f1);
%Splitting the string
a = strsplit(header_line, ' ');

%Getting the Species Name
species_name = a{1};

%Getting the Lower, Medium & Higher Temp.
llt = str2double(a{end-3});
lmt = str2double(a{end-1});
lht = str2double(a{end-2});

T = linspace(llt,lht,500);

%Getting Species with Exponential Coefficent
line1 = fgetl(f1);
%Finding location of Exponent(E)
e1 = strfind(line1, 'E');
%Converting string to number
a1 = str2double(line1(1:e1(1)+3));
a2 = str2double(line1(e1(1)+4:e1(2)+3));
a3 = str2double(line1(e1(2)+4:e1(3)+3));
a4 = str2double(line1(e1(3)+4:e1(4)+3));
a5 = str2double(line1(e1(4)+4:e1(5)+3));

%Getting Species with Exponential Coefficent
line2 = fgetl(f1);
%Finding location of Exponent(E)
e2 = strfind(line2,'E');
%Converting string to number
a6 = str2double(line2(1:e2(1)+3));
a7 = str2double(line2(e2(1)+4:e2(2)+3));
a8 = str2double(line2(e2(2)+4:e2(3)+3));
a9 = str2double(line2(e2(3)+4:e2(4)+3));
a10 = str2double(line2(e2(4)+4:e2(5)+3));

%Getting Species with Exponential Coefficent
line3 = fgetl(f1);
%Finding location of Exponent(E)
e3 = strfind(line3,'E');
%Converting string to number
a11 = str2double(line3(1:e3(1)+3));
a12 = str2double(line3(e3(1)+4:e3(2)+3));
a13 = str2double(line3(e3(2)+4:e3(3)+3));
a14 = str2double(line3(e3(3)+4:e3(4)+3));

%Defining the functions for operation
r = 8.314;
[Cp] = Specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,lmt,r);
[H] = Enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,lmt,r);
[S] = Entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,lmt,r);

%Creating New Folder for Species
mkdir(species_name)
%Entering current folder
cd(species_name)

%Plot 1
figure(1)
plot(T,Cp)
xlabel('Temperature')
ylabel('Specific Heat')
title('Specific heat vs Temperature', sprintf('%s',species_name))
%Saving figure to specific format
saveas(figure(1),'Cp VS T.jpg')

%Plot 2
figure(2)
plot(T,S)
xlabel('Temperature')
ylabel('Enthalpy')
title('Enthalpy vs Temperature', sprintf('%s',species_name))
%Saving figure to specific format
saveas(figure(2),'H VS T.jpg')

%Plot 3
figure(3)
plot(T,H)
xlabel('Temperature')
ylabel('Entropy')
title('Entropy vs Temperature', sprintf('%s',species_name))
%Saving figure to specific format
saveas(figure(3),'S VS T.jpg')

%returning back to initial directory
cd ..

%Molecular weight
[M] = Mol_Wt(species_name);
k = sprintf('%s weight = %f',species_name,M);
disp (k)
end

Specific Heat Function:

%NASA File Parsing - Specific_heat function Code
function Cp_Value = Specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14, T,lmt,r)
if(T>lmt)
    Cp_Value = (a1)+((a2*T)./2)+((a3*(T.^2))./3)+((a4*(T.^3))./4)+((a5*(T.^4))./5)+(a6./T).*(r*T);
else
    Cp_Value = (a8)+((a9*T)./2)+((a10*(T.^2))./3)+((a11*(T.^3))./4)+((a12*(T.^4))./5)+(a13./T).*(r*T);
end
end

Enthalpy Function:

%NASA File Parsing - Specific_heat function Code
function Cp_Value = Specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14, T,lmt,r)
if(T>lmt)
    Cp_Value = (a1)+((a2*T)./2)+((a3*(T.^2))./3)+((a4*(T.^3))./4)+((a5*(T.^4))./5)+(a6./T).*(r*T);
else
    Cp_Value = (a8)+((a9*T)./2)+((a10*(T.^2))./3)+((a11*(T.^3))./4)+((a12*(T.^4))./5)+(a13./T).*(r*T);
end
end

Entropy Function:

%NASA File Parsing - Entropy function Code
function S_Value = Entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,lmt,r)
if(T>lmt)
    S_Value = (a1)+((a2*T)./2)+((a3*(T.^2))./3)+((a4*(T.^3))./4)+((a5*(T.^4))./5)+(a6./T).*(r);
else
    S_Value = (a8)+((a9*T)./2)+((a10*(T.^2))./3)+((a11*(T.^3))./4)+((a12*(T.^4))./5)+(a13./T).*(r);
end
end

Molecular Weight:

%NASA File Parsing - Molecular Weight
function [M]= Mol_Wt(species_name)
Weight = 0;
elements = ['H','C','N','O','A'];
mass = [1.008,12.011,14.007,15.999,39.948];
for i = 1:length(species_name)
    for j = 1:length(elements)
        if strcmpi(species_name(i), elements(j))
            Weight = Weight+mass(j);
            position = j;
        end
    end
    number = str2double(species_name(i));
    if(number>1)
        Weight = Weight+(mass(position))*(number-1);
    end
    M = Weight;
end
end

Steps:

Step-1: File writing i.e., using fopen() command

Step-2: Using 2 seperate For-Loops to read the first 5 lines and to read & perform the operations on all 53 species.

Step-3: Preparing the data i.e., getting the Species Name, getting 1st Header line and splitting the strings using strsplit() to remove the space as delimiter.

Step-4: Getting the Lower, Medium & Higher Temp values for the species using the location of the values using the str2double() command.
Also, defining the space for the operation of Temperature using the linspace() command.
where:
  • llt - Lower Low Temp.
  • lmt - Lower Moderate Temp.
  • lht - Lower High Temp.

Step- 5: Getting values of 14 Exponential Coefficent i.e., finding location of Exponent (E) using strfind() command and converting the string to numbers using str2double() command.

**Performing Step-5 operations for line 1, 2 & 3 respectively**

Step-6: Defining the functions for operations i.e., Specific Heat, Enthalpy & Entropy.

Step-7: Creating the functions for Specific Heat, Enthalpy & Entropy using the If-Else Conditional statement with the condition (T>lmt) i.e.,

  • Cp_Value
  • H_value
  • S_Value

Step-8: Creating New Folder for specific Species i.e., creating seperate directories using mkdir() command.

Step-8: Plotting the graphs for 'Specific heat vs Temperature','Enthalpy vs Temperature' & Entropy vs Temperature' i.e.,

  • Cp vs T
  • H vs T
  • S vs T

Step- 9: Saving the figures to specific formats and to the specific directories. Using cd .. for returning back to initial directory after every operation.

Step- 10: Calculating the Molecular weight for all the 53 species by creating a seperate function i.e., Mol_Wt() using FOR-Loop & If-Else condititon statement & considering Weight = 0 and Elements = ['H','C','N','O','A'].
Specific Heat:
Definition: It is the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree.
Formulae:
= heat energy
= mass
= specific heat capacity
= change in temperature

Enthalpy:

Definition: It is an energy-like property or state function. It is the measure of total heat present in the thermodynamic system where the pressure is constant.

Formulae:

= enthalpy
= internal energy
= pressure
= volume

Entropy:

Definition: It is a measure of the amount of energy which is unavailable to do work. Entropy is also a measure of the number of possible arrangements the atoms in a system can have.

Formulae:

= entropy
= Boltzmann constant
= natural Log
= number of microscopic configurations

Molecular weight:

Definition: The sum of the atomic masses of all atoms in a molecule, based on a scale in which the atomic masses of hydrogen, carbon, nitrogen, and oxygen are 1, 12, 14, and 16, respectively.

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.