Project

Parsing NASA thermodynamic data

Aim:

  1. To write a prgram that extracts co-efficients and temperatures from NASA thermodynamic data and to calculate the enthalpy, entropy and specific heats for all the species in the data file.
  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
  4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name

Theory

File Parsing: Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating perfoming some kind of transformation on the data.

Specific heat :

The specific heat (also called specific heat capacity) is the amount of heat required to change a unit mass (or unit quantity, such as mole) of a substance by one degree in temperature. Therefore, unlike the extensive variable heat capacity which depends on the quantity of material, specific heat is an intensive variable and has units of energy per mass per degree (or energy per number of moles per degree).

The heat capacity of a substance can differ depending on what extensive variable are held constant, with the quantity being held constant usually being denoted with a subscript. For example, the specific heat at constant pressure is commonly denoted , while the specific heat at constant volume is

Enthalpy, the sum of the internal energy and the product of the pressure and volume of a thermodynamic system. Enthalpy is an energy-like property or state function—it has the dimensions of energy, and its value is determined entirely by the temperature, pressure, and composition of the system and not by its history. In symbols, the enthalpy, H, equals the sum of the internal energy, E, and the product of the pressure, P, and volume, V, of the system: H = E + PV.

Entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.

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.

Atomic mass is a basic physical property of matter. The mass of an atom or a molecule is referred to as the atomic mass. The atomic mass is used to find the average mass of elements and molecules and to solve stoichiometry problems.

The file used for the project is NASA thermodynamic data

Steps:

  • First I defined the variable R(Universal gas constant) with a value of 8.314 J / mol·K
  • Opened the data file using fopen command and assigned to a variable f1 which acts like a pointer for the file THERMO.dat
  • Using fgetl command read and saved the Header line and global temperature values. Then splited the global temperature values using strsplit and in the data file using str2double command to split the data from the string.
  • Then the next 3 line are not neccesary they defined only the command / text for their understanding and for that a for loop is used for three similar opeartion to the lines.
  • For loop is used to read the co-efficient of all 53 species
  • Then the fgetl command reads, splited and saved into the variable species
  • Using the strfind the 'G' character is found in the line and with respect to this G taken the values of Local low,mid,high temperaturevalues
  • Using the similar approach as above found out the 14 co-efficient for different species . strings are converted into numericals by using str2double command.Used strfind command to locate the character of 'E' in each line .co-efficient are assigned to the variables a1,a2,a3……..a14 which are also has been provided in the above function programs
  • Temperature is set with an array of Local low temperature to Local high temperature with 250 number of division using linspace
  • For different temperature range function calls and values of Cp,H and S are calculated for temperatures
  • Using mkdir function we have created new folder with name for all the output files
    Cd command changes the directory again
  • By using plot and figures commands plots have been created for all the different species
    saveas command is used to save all the plots into their respective folders
    Then called the molecular weight function program and prints the values of all the species in the command window.
  • Below are the main code, function code used for Specific Heat, Enthalpy, Entropy and the molecular weight calculation code.

Explanation: (Function)

  • As per the challange the first seven co-efficient are used to calculated Cp values with temperature greater than Local mid temperature (Higher value) and the rest co-efficient are used to calculate the Cp with Local low temperature (Lower values ) and similar concept is used for the Entropy and Enthalpy.
  • The Molecular weight is calculated by a for loop in which a element , atomic mass array is formed and in the for loop ran for length of length species .
  • Then with respect to that these species it is also ran to the length of atomic mass and comparing the string using strcmpi
  • The character is runned using the above but the number part is taken out from string to number using str2double and multipled with the previous character and by the way Molecular weight is calculated and called the function into the main program as mentioned above.

Main program

clear all
close all
clc

%Defining the variable to it's values 

%R is universal gas constant
R = 8.134;

%Reading the Header file
f1=fopen('THERMO.dat','r');
Header = fgetl(f1);

%Reading and Defining the global temperatures 
global_temp = fgetl(f1);
global_temp = strsplit(global_temp,' ');
global_low_temp = str2double(global_temp(2));
global_mid_temp = str2double(global_temp(3));
global_high_temp = str2double(global_temp(4));

%To skip the unwanted 3 lines in the data

for i=1:3
    skip=fgetl(f1);
end

%Reading every line of all species

for i=1:53
    species_line = fgetl(f1);
    species_split=strsplit(species_line,' ');
    species=species_split{1};
    A=strfind(species_line,'G');
    low_temp=species_line(A+3:55);
    high_temp=species_line(A+12:65);
    mid_temp=species_line(A+23:75);
    
    Local_low_temp=str2double(low_temp);
    Local_high_temp=str2double(high_temp);
    Local_mid_temp=str2double(mid_temp);
    
    line_1=fgetl(f1);
    l1=strfind(line_1,'E');
    a1=str2double(line_1(1:l1(1)+3));
    a2=str2double(line_1(l1(1)+4:l1(2)+3));
    a3=str2double(line_1(l1(2)+4:l1(3)+3));
    a4=str2double(line_1(l1(3)+4:l1(4)+3));
    a5=str2double(line_1(l1(4)+4:l1(5)+3));
    
    line_2=fgetl(f1);
    l2=strfind(line_2,'E');
    a6=str2double(line_2(1:l2(1)+3));
    a7=str2double(line_2(l2(1)+4:l2(2)+3));
    a8=str2double(line_2(l2(2)+4:l2(3)+3));
    a9=str2double(line_2(l2(3)+4:l2(4)+3));
    a10=str2double(line_2(l2(4)+4:l2(5)+3));
    
    line_3=fgetl(f1);
    l3=strfind(line_3,'E');
    a11=str2double(line_3(1:l3(1)+3));
    a12=str2double(line_3(l3(1)+4:l3(2)+3));
    a13=str2double(line_3(l3(2)+4:l3(3)+3));
    a14=str2double(line_3(l3(3)+4:l3(4)+3));
    
    %Ranging the temperature to find the values    
    T=linspace(Local_low_temp,Local_high_temp,250);
    
    % calling Cp(specific heat) function to the program
    Cp=Specific_Heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,Local_low_temp,Local_high_temp,Local_mid_temp);  
    
    % calling H(Enthalpy) function to the program.
    H = Enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,Local_low_temp,Local_high_temp,Local_mid_temp);
    
    % calling S(Entropy) function to the program.
    S = Entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,Local_low_temp,Local_high_temp,Local_mid_temp);
     
    %Plotting graphs
    
    folder=pwd;
    mkdir('MATLAB',species)
    figure(1)
    plot(T,Cp,'linewidth',2,'color','r');
    xlabel('Temperature (T)')
    ylabel('Specific Heat(Cp)')
    title('Specific heat(Cp) vs Temperature (T)')
    strl=sprintf('E:/Parsing -NASA PROJECT/MATLAB/%s',species);
    cd(strl)
    saveas(1,'CP.png');
    figure(2)
    plot(T,H,'linewidth',2,'color','b');
    xlabel('Temperature (T)')
    ylabel('Enthalpy(H)')
    title('Enthalpy(H) vs Temperature (T)')
    saveas(2,'H.png');
   figure(3)
    plot(T,S,'linewidth',2,'color','g');
    xlabel('Temperature (T)')
    ylabel('Entropy(S)')
    title('Entropy(S) vs Temperature (T)')
    saveas(3,'S.png');
    cd(folder)    
   
  

%Molecular Weight calculation
[MW] =Molecular_weight(species);
    P=sprintf('%s Molecular weight = %f',species,MW);
    disp(P)
end    

fclose(f1)  %close the file 

Specific Heat function

%Specific Heat function program

function [Cp]=Specific_Heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,Local_low_temp,Local_high_temp,Local_mid_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))+(a10.*(T.^2))+(a11.*(T.^3))+(a12.*(T.^4)))*R;
end

Entropy function

%Entropy function program

function [S] = Entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,Local_low_temp,Local_high_temp,Local_mid_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

Enthalpy function

%Enthalpy function program

function [H] = Enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,Local_low_temp,Local_high_temp,Local_mid_temp);
if (T>Local_mid_temp)
    H=((a1.*T)+(a2.*(T.^2)./2)+(a3.*(T.^3)./3)+(a4.*(T.^4)./4)+(a5.*(T.^5)./5)+a6)*R;
else
 H=((a8.*T)+(a9.*(T.^2)./2)+(a10.*(T.^3)./3)+(a11.*(T.^4)./4)+(a12.*(T.^5)./5)+a13)*R;
end

Molecular Weight Function

%Molecular Weight Function
function [MW] =Molecular_weight(species)
MW=0;
c=0;
%Finding Molecular Weights

elements=['O','H','C','S','N','AR'];
atomic_mass=[15.999,1.008,12.011,32.065,14.007,39.948];

for i=1:length(species)
    for j=1:length(atomic_mass)
        if strcmpi(species(i),elements(j))
            MW=MW+atomic_mass(j);
            c=j;
        end
    end
    num=str2double(species(i));
         if(num>1)
             MW=MW+(atomic_mass(c)*(num-1));
         end
end
end
            

Outputs:

O2 plot

CO2 Plot

N2 plot

Results

Extracted and splitted the data from the NASA data file provided and done calculation as per the requirement as well as ploted the graph.

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.