Challenge

Project 1 - Parsing NASA thermodynamic data

Object:

To write a MATLAB program to parse the NASA thermodynamic data file.

Calculate htermodynamics properties of various gas species

Plot the Specific heat(CP),Enthalpy(H) and Entropy(S) for the local temperature range specific for each species.

Theory:

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. In otherwards, file parsing refers to how information can be read from a particular text file or any type of file and how that information can be used to in a computer program.

Text parsing is a common programming task that splits the given sequence of characters or values into smaller parts based on some rules. It has been used in a wide variety of application ranging from simple file parsing to large scale natural language processing.

NASA Thermodynamic data:

THERMO.data file is a data file containing 14 coefficients for 53 different elements given by Nasa. These coefficients are used to calculate the enthalpy, entropy and specific heats for all the specific heats for all the species in the data file. Each species also has its own range of local temperatures.

Specific heat capacity, Cp(kj/kg-k)

Specific heat capacity is the amount of heat to supplied to the unit mass of a system in order to increase its temperature by one degree in a thermodynamics process in which quantity X is imposed.

Enthalpy, H(kj)

It's a thermodynamics quantity equivalent to the total heat content of a system. It is equal to the internal energy of the system plus the product of pressure and volume.

Entropy, S(kj/k)

Entropy is the measure of a systems 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.

Code:

Main program:

% program code to parse the NASA thermodynamic data file 
clear all
close all
clc

%to open the NASA  thermodynamic data file
f1=fopen('THERMO.dat','r');

%to skip the first five consrcutive lines 
for  i=1:5
    tline = fgetl(f1);
end

% to read all the 53 species 
for j=1:53
    tline1=fgetl(f1); % to read the line1 of all the species
    tline2=fgetl(f1); % to read the line2 of all the species
    tline3=fgetl(f1); % to read the line3 of all the species
    tline4=fgetl(f1); % to read the line4 of all the species

    %to find the 'E' place and split all the co-efficient and make into
    %cell array
    A=strsplit(tline1);
    B=findstr(tline2,'E');
    B1=strsplit(tline2);
    C=findstr(tline3,'E');
    C1=strsplit(tline3);
    D=findstr(tline4,'E');
    D1=strsplit(tline4);

    %to extract all the species names
    species_name =(A{1});

    %to ectract  all the species values and split that into low,mid and
    %high temperature
    Local_low_temp=(A{end-3});
    T_L=str2double(Local_low_temp);
    Local_mid_temp=(A{end-1});
    T_M=str2double(Local_mid_temp);
    Local_high_temp=(A{end-2});
    T_H=str2double(Local_high_temp);
    
    %universal gas constant(J/Kg-k)
    R=8.314;

    %Temperature range
    T=linspace(T_L,T_H,1000);

    % to extract all the co-efficients and convert the strings into number
    
    % a1 to a7 are the high temperature coefficient
    a1 = tline2(1:B(1)+3);
    a1 = str2double(a1);
    a2 = tline2(B(1)+4:B(3)+3);
    a2 = str2double(a2);
    a3 = tline2(B(2)+4:B(3)+3);
    a3 = str2double(a3);
    a4 = tline2(B(3)+4:B(4)+3);
    a4 = str2double(a4);
    a5 = tline2(B(4)+4:B(5)+3);
    a5 = str2double(a5);
    a6 = tline3(1:C(1)+3);
    a6 = str2double(a6);
    a7 = tline3(C(1)+4:C(2)+3);
    a7 = str2double(a7);

    % a8 to a14 are the high temperature coefficient
    a8 = tline3(C(2)+4:C(3)+3);
    a8 = str2double(a8);
    a9 = tline3(C(3)+4:C(4)+3);
    a9 = str2double(a9);
    a10 = tline3(C(4)+4:C(5)+3);
    a10 = str2double(a10);
    a11 = tline4(1:D(1)+3);
    a11 = str2double(a11);
    a12 = tline4(D(1)+4:D(2)+3);
    a12 = str2double(a12);
    a13 = tline4(D(2)+4:D(3)+3);
    a13 = str2double(a13);
    a14 = tline4(D(3)+4:D(4)+3);
    a14 = str2double(a14);
    %calling function
    [CP, H, S]=THERMAL_DATA(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R);

    %calling molecular weight function 
    [M_M]=molecular_weight(species_name);

   %plotting the graphs for CP,H,S VS T

   %CP VS T 
   figure(1);
   plot(T,CP,'color','r','linewidth',4);
   xlabel('TEMPERATURE [K]')
   ylabel('SPECIFIC HEAT [CP]')
   title(species_name)


   %H VS T 
   figure(2);
   plot(T,CP,'color','g','linewidth',4);
   xlabel('TEMPERATURE [K]')
   ylabel('ENTHALPY [H]')
   title(species_name)
   

   %S VS T 
   figure(3);
   plot(T,CP,'color','y','linewidth',4);
   xlabel('TEMPERATURE [K]')
   ylabel('ENTROPY [S]')
   title(species_name)
   

   % To creat the folder
   mkdir(['C:userkrishna murthyDesktopmatlab codesplots of parsing',species_name]);

   %to save the respective plots inside the folder
   cd(['C:userkrishna murthyDesktopmatlab codesplots of parsing',species_name])
   
   %command to save the different plots
   saveas(1,'specific_heat','jpg')
   saveas(2,'Enthalpy','jpg')
   saveas(3,'Entropy','jpg')
end

Function code to calculate the specific heat , enthalpy and entropy
%funnction code to calculate specific heat, enthalpy and entropy
function[CP, H, S] = THERMAL_DATA(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R)
    
    % T = linspace(200,6000,1000)
    for i = 1:length(T)

     % for higher temperature coefficient
     if T(i)>1000
         % to calculate specific heat
         CP(i)=R*(a1+a2*T(i)+a3*T(i)^2+a4*T(i)^3+a5*T(i)^4);
         % to calculate enthalpy
         H(i)=R*T(i)*(a1+a2*T(i)/2+a3*T(i)^2/3+a4*T(i)^3/4+a5*T(i)^4/5+a6/T(i));
         % to calculate entropy
         S(i)=R*(a1*log(T(i))+a2*T(i)+a3*T(i)^2/2+a4*T(i)^3/3+a5*T(i)^4/4+a7);
     else
          % to calculate specific heat
         CP(i)=R*(a8+a9*T(i)+a10*T(i)^2+a11*T(i)^3+a12*T(i)^4);
         % to calculate enthalpy
         H(i)=R*T(i)*(a8+a9*T(i)/2+a10*T(i)^2/3+a11*T(i)^3/4+a12*T(i)^4/5+a13/T(i));
         % to calculate entropy
         S(i)=R*(a8*log(T(i))+a9*T(i)+a10*T(i)^2/2+a11*T(i)^3/3+a12*T(i)^4/4+a14);
     end
     end
end

Function code to calculate the molecular weight

%funnctioon code to calculate molecular weight of the species
function[M_M]=molecular_weight(species_name)

    element=['H','C','O','N','AR'];

    %atomic mass of weight
    atomic_weight=[1.007 12.0107 15.999 14.0067 39.948]

    %initial value of range
    m_wt=0;

    %run to length of species
    for i=1:length(species_name)

    %run to length of atomic weight
    for j=1:length(atomic_weight)

    % to compare two strings
        if strcmp(species_name(i),element(j))
            m_wt=m_wt+atomic_weight(j);
            x=j;
        end
    end
    n = str2double(species_name(i));
    if (n>1)
        m_wt=m_wt+atomic_weight(x)*(n-1);
    end
 end
 M_M=m_wt;

end

Explanation:

The program first opens the data file and extracts the global temperature values. Then it calculates the total number of elements species by eliminating the number of unnecessary lines from total lines & dividing by number of lines containing the element info. The program then calls the function species_data_collect.m for extacting the element name, local temperature values and the coefficients of each element form the file and stores the data in the data structure named species_data. Then the program calls the function specific_heat_calc.m, enthalpy_calc.m, entropy_calc.m and molecular_weight.m for calculating the specific heats, enthalpy, entropy and molecular weights of each element and stores in the structure species_data.

Then the function plot_func.m is evoked for plotting Cp vs T, H vs T and S vs T graphs for each element and storing the graphs of each element in respective folders.

Finally, the user is provided with the option to view the graph of any particular element by evoking the element_plot_showcase.m function.

As input parameters the function accepts the file handle and number of element species. The function collects the elements names, local temperatures and coefficient of each element and saves the data into a structure named maindata under the fields Name, local_Temp and coeff respectively. Finally, the function returns the structure which is saved into structure species_data in the main program.

The function accepts the structure species_data and universal gas constant ‘R’ as input, the function first creates an array of temperature values for each element and calculates the specific heat values for each temperature value using the data from the input structure. Finally, the existing structure is updated with the temperature and corresponding specific heat values.

The function accepts the structure species_data and universal gas constant ‘R’ as input. The function calculates the enthalpy values foe each element using the temperature values and the coefficient values from the input structure. The structure is the returned after updating it with enthalpy values.

The function accepts the structure species_data and universal gas constant ‘R’ as input. The function calculates the entropy values foe each element using the temperature values and the coefficient values from the input structure. The structure is the returned after updating it with entropy values.

This function accepts the structure ass input and calculates the molecular weights of each element. To calculate the molecular weight, a character string of all the letters appearing in the name of element and an array of the atomic weight corresponding to the letters is required. A nested for loop & if statement is used to compare each letter of the element name by the character string. The molecular weight is calculated letter by letter and if any numeral values came in between then it is first converted from string to numeral and the atomic weight of the preceding element is multiplied by one value less then numeric value which is further added to the molecular weight. This way molecular weight of the element is calculated and id added into the data structure.

This function accepts the data structure as input parameters and plots the specific heat vs temperature, enthalpy vs temperature, entropy vs temperature graphs for each element. Additionally, it creates a ‘Results’ folder under which a separates folder for each element with element name. All 3 plots of an element are stored in its corresponding folder. All these are generated automatically.

This function accepts the data structure as input. The function asks the user whether or not to show plots of any particular element. So, a while loop with test condition ‘ctrl==0’ is used. The user enters the element name from the structure one by one. As soon as the element matches the element the index position is recorded. On successful matching the name of the element is concatenated to the path of the result folder and is provided to imread function with the specific name of the plot. The way all the 3 plots can be accessed by the user. The loop terminates only when the user enters ‘No’ as a choice to exit program.

Output:

Screenshot of the window where all the folders are saved

Plots:

Plots for O2

Specific Heat vs Temperature

Enthalpy vs Temperature

Entropy vs Temperature

Plots for N2

Specific Heat vs Temperature

Enthalpy vs Temperature

Entropy vs Temperature

Plots for CO2

Specific Heat vs Temperature

Enthalpy vs Temperature

Entropy vs Temperature

Molecular weight

I didn't face any errors while i run the program, when i run the program ant type of errors as not shown 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.