Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing :

Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammer.

Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other, which may also contain semantic and other information.

A parser is a software component that takes input data (frequently text) and builds a data structure – often some kind of parse tree, abstract syntax tree or other hierarchical structure, giving a structural representation of the input while checking for correct syntax.

Procedure to Parse the Function Input :

This shows the way to define required and optional inputs, assign defaults to optional inputs, and validate all inputs to a custom function using the Input Parser.

The Input Parser provides a consistent way to validate and assign defaults to inputs, improving the robustness and maintainability of your code. To validate the inputs, you can take advantage of existing MATLAB functions or write your own validation routines.

  • Define your function
  • Create an InputParser object
  • Add inputs to the scheme
  • Set properties to adjust parsing (Optional)
  • Parse the inputs
  • Use the inputs in your function
  • Call your function

Formulaes Used :

Required Matlab code with explaination :

%The program begins with the usual commands;
clc
clear all
close all

%Finding and opening the file
%The NASA thermodynamic data file i.e. 'THERMO.dat' was being read by fopen command.
file = fopen('THERMO.dat','r');

%Heading
%To navigate the command through data, 'fgetl' command is used.
%The header line 'THERMO' was skipped by this 'fgetl' command.
line1 = fgetl(file);

%Global temp values
The temp values were stord as a single string in variable.
%To split the string in to 3 strings, 'strsplit' command is used and to convert the string values into numerical values, 'str2num' command is used.
%3 temp values, min, mid and max were stored in seperate variables.
line2 = fgetl(file);
A = strsplit(line2,' ');
Global_min_Temp = str2num(A{2});
Global_mid_Temp = str2num(A{3});
Global_max_Temp = str2num(A{4});

%The 3 comment line were skipped using the 'fgetl' command.
line3 = fgetl(file);
line4 = fgetl(file);
line5 = fgetl(file);

%The 14 coefficients (C1-C14) for each string were calculated using 'for loop'.
%For each species, 3 lines carries this coefficients.
%To distingusih the coefficients, position of the exponents were determined for each of them using 'strfind' command.
for i = 1:53
    species = fgetl(file);
    species_name = species(1:6);
    
    Line_1 = fgetl(file);
    L1 = strfind(Line_1,'E');
    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);
    L2 = strfind(Line_2,'E');
    C6 = str2num(Line_2(1:(L2(1)+3)));
    C7 = str2num(Line_2((L2(1)+4):(L2(2)+3)));
    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);
    L3 = strfind(Line_3,'E');
    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('F:matlab practice skilllyncnewfolderSpecies',species_name)
    
    %Temp range assumed
    temp = linspace(300,5000,100);
    
    %Universal gas constant
    R = 8.314;
    
    %Calculation
    for j = 1:length(temp)
        if temp(j)<3000
            Cp = (C1+C2.*temp+C3.*temp.^2+C4.*temp.^3+C5.*temp.^4).*R;
            H = (C1+(C2.*temp)./2+(C3.*temp.^2)./3+(C4.*temp.^3)./4+(C5.*temp.^4/5)+C6./temp).*temp.*R;
            S = (C1.*log(temp)+C2.*temp+(C3.*temp.^2)./2+(C4.*temp.^3)./3+(C5.*temp.^4)./4+C7).*R;
        else
            Cp = (C8+C9.*temp+C10.*temp.^2+C11.*temp.^3+C12.*temp.^4).*R;
            H = (C8+(C9.*temp)./2+(C10.*temp.^2)./3+(C11.*temp.^3)./4+(C12.*temp.^4/5)+C13./temp).*temp.*R;
            S = (C8.*log(temp)+C9.*temp+(C10.*temp.^2)./2+(C11.*temp.^3)./3+(C12.*temp.^4)./4+C14).*R;
        end
    end
    
    %Plotting
    
    %Temp vs Entropy
    figure(1)
    plot(temp,S,'linewidth',3,'color','b')
    title(species_name)
    xlabel('Temperature')
    ylabel('Entropy')
    
    %Temp vs Enthalpy
    figure(2)
    plot(temp,H,'linewidth',3,'color','k')
    title(species_name)
    xlabel('Temperature')
    ylabel('Enthalpy')
    
    %Temp vs Specific Heat
    figure(3)
    plot(temp,Cp,'linewidth',3,'color','y')
    title(species_name)
    xlabel('Temperature')
    ylabel('Specific Heat')
    
    %Changing the file paths to save the plot
    directory = (['F:maltab practice skilllyncnewfolderSpecies',species_name]);
    cd(directory)        
    
    %Saving the plots
    
    saveas(1,'Entropy.jpg');
    saveas(1,'Enthalpy.jpg');
    saveas(1,'Specific_Heat.jpg');
    
    cd('F:matlab practice skilllync')
    
    MM = molecular_mass(species_name);
    
end

fclose(file);
%The molecular weights were calculated using a seperate function for each species.

function MM = molecular_mass(species_name)
species = upper(species_name);
Constituents = ['H', 'C', 'N', 'O', 'A'];
Atomic_Weight = [1.00794 12.0107 14.0067 15.9994 39.948];
MM = 0;

for i=1:length(species)
    for j=1:length(Constituents)
        if strcmp(species(i),Constituents(j))
            MM = MM + Atomic_Weight(j);
            u = j;
        end
    end
    n = str2num(species(i));
    if n>1
        MM = MM +Atomic_Weight(u)*(n-1);
    end
end
end

Output Plots :

1. For O2

a) Temp. vs Enthalpy

b) Temp. vs Entropy

c) Temp. vs Specific Heat

2. For N2

a) Temp. vs Enthalpy

b) Temp. vs Entropy

c) Temp. vs Specific Heat

3. For CO2

a) Temp. vs Enthalpy

b) Temp. vs Entropy

c) Temp. vs Specific Heat

Screenshot of the window where all the folders are saved :

Molecular weight displayed in command window :

Errors faced with solution to that error :

1. In the Molecular Mass Fuction Program, the species_names were not mentioned.

So, in order to correct this error, firstly I mentioned the species_names and then executed the program.

References :

1. NASA thermodynamic data

2. https://in.mathworks.com/help/matlab/matlab_prog/parse-function-inputs.html

3. https://en.wikipedia.org/wiki/Parsing

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.