Challenge

Parsing NASA thermodynamic data (MATLAB)

Parsing NASA thermodynamic data

1. OBJECTIVES:

MATLAB program to fulfill the following objectives :

  • Writing a code in MATLAB to parse the NASA thermodynamic data file.
  • Writing a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
  • Calculating the molecular weight of each species and displaying it in the command window.
  • Plotting the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
  • Saving the plots as images with appropriate names and dumping them in separate folders for each species with suitable name.

2.THEORY:

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

The thermodynamic properties in terms of the coefficients are given as :

3.PROGRAM:-

3.1 MATLAB CODE:-

  • Main MATLAB code
%PROGRAM TO PRASE NASA'S THERMODYNAMIC DATA


clear all %clear the workspace
close all %clear all the figure window
clc  %clear the command window

%OPENING AND READING THE GIVEN FILE

file = fopen('THERMO.dat','r'); %Opening the THERMO.dat file to read.Here file is a scalar matlab integer,called file identifier          

%Heading
line1 = fgetl(file); %Assigning the heading to line1.fget1() returns the next line without newline characters        

%Global temperature values
line2 = fgetl(file); %Assigning the global temperature values to line2.fget1() returns the next line without newline characters      

%Next three lines are comments
des_1 = fgetl(file); %Assigning the next line to des_1.                           
des_2 = fgetl(file); %Assigning the next line to des_3.         
des_3 = fgetl(file); %Assigning the next line to des_3.    
  

%Assigning the Global temperatures
A = strsplit(line2,' '); %spliting the line2 using space character as an delimiter 

Global_low_temperature = str2num(A{2}); %Assigning the golbal low temperature from array A as a number
Global_mid_temperature = str2num(A{3}); %Assigning the golbal mid temperature from array A as a number
Global_high_temperature = str2num(A{4}); %Assigning the golbal high temperature from array A as a number


%SPECIES INFORMATION
for i=1:53 %Running a for loop for every molecule
    
species= fgetl(file) %Assigning the molecule data's 1st line from the file to species
    
species_name = species(1:6) %Assigning the species name from the 1st 6 elements of species array
  
%Local temperature values for each molecules
B = strsplit(species,' '); %spliting the species using space character as an delimiter    
Local_low_temperature = str2num(B{end-3}); %Assigning the local low temperature from array B as a number
Local_high_temperature = str2num(B{end-2}); %Assigning the local high temperature from array B as a number
Local_mid_temperature = str2num(B{end-1}); %Assigning the local mid temperature from array B as a number
    
Line_1 = fgetl(file); %Assigning the molecule data's 2nd line from the file to Line_1

L1 = strfind(Line_1,'E'); %Finding the 'E' letter in Line_1 to differentiate the data

%Assigning the High temperature coefficients   
c1 = str2num(Line_1(1:(L1(1)+3))); 
c2 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
c3 = str2num(Line_1((L1(2)+4):(L1(3)+3)));
c4 = str2num(Line_1((L1(3)+4):(L1(4)+3)));
c5 = str2num(Line_1((L1(4)+4):(L1(5)+3)));


Line_2 = fgetl(file); %Assigning the molecule data's 3rd line from the file to Line_2

L2 = strfind(Line_2,'E'); %Finding the 'E' letter in Line_2 to differentiate the data

%Assigning the High temperature coefficients   
c6 = str2num(Line_2(1:(L2(1)+3)));
c7 = str2num(Line_2((L2(1)+4):(L2(2)+3)));

%Assigning the Low temperature coefficients   
c8 = str2num(Line_2((L2(2)+4):(L2(3)+3)));
c9 = str2num(Line_2((L2(3)+4):(L2(4)+3)));
c10 = str2num(Line_2((L2(4)+4):(L2(5)+3)));


Line_3 = fgetl(file); %Assigning the molecule data's 4th line from the file to Line_3

L3 = strfind(Line_3,'E'); %Finding the 'E' letter in Line_3 to differentiate the data

%Assigning the Low temperature coefficients   
c11 = str2num(Line_3(1:(L3(1)+3)));
c12 = str2num(Line_3((L3(1)+4):(L3(2)+3)));
c13 = str2num(Line_3((L3(2)+4):(L3(3)+3)));
c14 = str2num(Line_3((L3(3)+4):(L3(4)+3)));

%Creating folder for each species
mkdir(['C:UsersUSEROneDriveDocumentsMATLABFILE PARSING OF NASA THERMODYNAMIC DATASpecies',species_name]);

%Temperature ranges assumed for calculation purpose
Temperature = linspace(Local_low_temperature,Local_high_temperature,100); 

%Universal gas constant
R =8.314;                            

%CALCULATION OF PROPERTIES BASED ON THE LOWER AND UPPER TEMPERATURE

for j = 1:length(Temperature) %Running a for loop for every temperature values
    
%When the temperature is below the middle temperature
if Temperature(j)
  • MATLAB code for molecular_mass function
function MM = molecular_mass(species_name)
  
species = upper(species_name); %Converting lowercase characters to the corresponding uppercase characters

Constituents = ['H', 'C', 'N', 'O', 'A']; %Creating a character array of all constituents

Atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948]; %Creating an array, storing atomic weight of all constituents sequentially

MM = 0; %Initializing the MM varible

for i=1:length(species) %Running a for loop for each char of species
    
    for j=1:length(Constituents) %Running a for loop for each element of Constituents array
        
        if strcmp(species(i),Constituents(j)) %Comparing the ith char of species with jth char of contituens array 
           MM = MM + Atomic_weight(j); %Calculating molecular mass by summation
           u = j; %Assigning the j value in u variable
        end %Ending the if statement
        
    end %Ending the 2nd for loop
    
    %Calculating molecular mass when there is more than one atom is present of same type
    n = str2num(species(i)); %Converting the ith char of species to number(if ith element is a letter then it returns null matrix)
    
    if n>1 %When n greater than 1(i.e. more than one atom is present of same type)
        MM = MM + Atomic_weight(u)*(n-1); %Calculating molecular mass by summation
    end %Ending the is statement
    
end %ending the 1st for loop

end %Ending the function

3.2 EXPLANATION OF THE MATLAB CODE:-

  1. First we open the THERMO.dat file to read.
  2. Then read the Heading, Global temperatures & next 3 lines.
  3. Run a for loop for every species
  • Get the species name.
  • Get the local temperatures and after converting them into number we assign them in variables.
  • Get all 14 coefficients using strsplit().
  • Then calculate the Cp, H, and S using the given formula for two temperature range individually.
  • Plot the graph T-S, T-H & T-Cp graph and save then to specified folder according to their species name.
  • Calculate molecular mass calling the molecular_mass function.
  1. Close the file.
  2. The molecular_mass is defined in a different script (saved on the same folder).

3.3 OUTPUT:-

  • Screenshot of species folder
  • Outputs for o2
  • Outputs for N2
  • Outputs for Co2

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.