Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: -

Write a Matlab program that extracts 14 co-efficient and to calculate the entropy, enthalpy, and specific heats for all the species in the given NASA Thermodynamic data file data. Also, to calculate the molecular weight of each species.

File Parsing: -

Parsing, which is the process of identifying tokens within a data instance and looking for recognizable patterns. The parsing process segregates each word, attempts to determine the relationship between the word and previously defined token sets, and then forms patterns from sequences of tokens.

Program to calculate the Molecular weight of each species: -

%% Creating function file to calculate molecular weight of the species
function molecular_weight_calulation = molecular_weight(Species_name)
Species = (Species_name);
Elements = ['H','C','O','N','A','S'];
Atomic_weight = [1.00794 12.0107 15.999 14.0067 39.948 32];
molecular_weight_calulation = 0;
for i = 1:length(Species)
    for j = 1:length(Elements)
        if strcmp(Species(i),Elements(j))
            molecular_weight_calulation = molecular_weight_calulation + Atomic_weight(j);
            a = j;
        end
    end
n = str2double(Species(i));
if n>1
    molecular_weight_calulation = molecular_weight_calulation + Atomic_weight(a)*(n-1);
end
end
end

Program to calculate the entropy, enthalpy, specific heat: -

%% Creating the function file to give the inputs to calulate the values of Cp, H, S
function [Cp,H,S] = inputs(T,R,T_med_1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14)

for i = 1:length(T)
    if T(i)>T_med_1
        Cp = (c1 + c2.*T + c3.*T.^2 + c4.*T.^3+c5.*T.^4).*R;
        H = (c1 + (c2.*T)./2 + (c3.*T.^2)./2 + (c4.*T.^3)./4 +(c5.*T.^4)./5 + c6./T).*T.*R;
        S = (c1.*log(T) + c2.*T + (c3.*T.^2)./2 + (c4.*T.^3)./3 + (c5.*T.^4)./4 + c7)*R;
    else
        Cp = (c8 + c9.*T + c10.*T.^2 + c11.*T.^3 + c12.*T.^3 + c12.*T.^4)*R;
        H = (c8 + (c9.*T)./2 + (c10.*T.^2)./2 + (c11.*T.^3)./4 + (c12.*T.^4)./5 + c13./T)*R;
        S = (c8.*log(T) + (c9.*T) + (c10.*T.^2)./2 + (c11.*T.^3)./3 + (c12.*T.^4)./4 + c14)*R;
    end
end
end

Main Program: -

%% Program to find the molecular weight of the species and to make the directory 

clear all
close all
clc
%% Opening the .dat file of temp
F_1 = fopen("THERMO.dat",'r');
% Reading the lines from file using fgetl command to convert the string to integers
fgetl(F_1);
T_line = fgetl(F_1);
% Splitting the string 
A = strsplit(T_line, ' ');

%% Converting the global temperatures from string to double precision value.
global_low_temp = str2double(A{2});
global_mid_temp = str2double(A{3});
global_high_temp = str2double(A{4});

for i = 3:5
    g = fgetl(F_1);
end


%% Finding the Temperature and converting to string to double precision value. 
for j = 1:53
    Species =fgetl(F_1);
    t = strfind(Species, '.');

    Species(52-3:53+3);
    T_low = Species(52-3:t(1)+3);
    T_low_1 = str2double(T_low);

    Species(t(1)+6:62+3);
    T_high = Species(t(1)+6:t(2)+3);
    T_high_1 = str2double(T_high);

    Species(t(2)+6:72+3);
    T_med = Species(t(2)+6:t(3)+3);
    T_med_1 = str2double(T_med);

    %% Now finding the co-effcients of line 1 
    Species_name = Species(1:6);
    coeff_line_1 =fgetl(F_1);
    A_11 = strfind(coeff_line_1, 'E');
    coeff_line_1(1:12+3);

    a1 = coeff_line_1(1:A_11(1)+3);
    c1 = str2double(a1);
    a2 = coeff_line_1(A_11(1)+4:A_11(2)+3);
    c2 = str2double(a2);
    a3 = coeff_line_1(A_11(2)+4:A_11(3)+3);
    c3 = str2double(a3);
    a4 = coeff_line_1(A_11(3)+4:A_11(4)+3);
    c4 = str2double(a4);
    a5 = coeff_line_1(A_11(4)+4:A_11(5)+3);
    c5 = str2double(a5);

    %% Now finding the co-effcients of line 1 
    coeff_line_2 =fgetl(F_1);
    A_12 = strfind(coeff_line_2, 'E');
    coeff_line_2(1:12+3);

    a6 = coeff_line_2(1:A_12(1)+3);
    c6 = str2double(a6);
    a7 = coeff_line_2(A_12(1)+4:A_12(2)+3);
    c7 = str2double(a7);
    a8 = coeff_line_2(A_12(2)+4:A_12(3)+3);
    c8 = str2double(a8);
    a9 = coeff_line_2(A_12(3)+4:A_12(4)+3);
    c9 = str2double(a9);
    a10 = coeff_line_2(A_12(4)+4:A_12(5)+3);
    c10 = str2double(a10);

    %% Now finding the co-effcients of line 3
    coeff_line_3 =fgetl(F_1);
    A_13 = strfind(coeff_line_3, 'E');
    coeff_line_3(1:12+3);

    a11 = coeff_line_3(1:A_12(1)+3);
    c11 = str2double(a11);
    a12 = coeff_line_2(A_12(1)+4:A_12(2)+3);
    c12 = str2double(a12);
    a13 = coeff_line_2(A_12(2)+4:A_12(3)+3);
    c13 = str2double(a13);
    a14 = coeff_line_2(A_12(3)+4:A_12(4)+3);
    c14 = str2double(a14);

    %% Giving the input values 
    T =linspace(T_low_1, T_high_1,100);
    R = 8.314; % Universal gas constant
    % calling the function files
    [Cp, H, S] = inputs(T, R, T_med_1, c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14);
    molecular_weight_calculation = molecular_weight(Species_name)
   
    %% Plotting
    % Plot b/w Temperatue and Specific Heat
    figure(1)
    plot(T,Cp,'LineWidth',3,'Color','b');
    xlabel('Temperature');
    ylabel('Specific Heat');
    title(Species_name);

    % Plot b/w Temperature and Entropy
    figure(2)
    plot(T,H,'LineWidth',3,'Color','b');
    xlabel('Temperature');
    ylabel('Entropy');
    title(Species_name);

    % Plot b/w Temperature and Enthalpy
    figure(3)
    plot(T,S,'LineWidth',3,'Color','b');
    xlabel('Temperature');
    ylabel('Enthaply');
    title(Species_name);

    %% Making Directory and saving the species in different folders   
    mkdir ('E:\SKill_lync\Challenges\Nasa file parsing\species',Species_name)
    cd('E:\SKill_lync\Challenges\Nasa file parsing\species')
    % Saving as .png file 
    saveas(1,'Specific Heat.png')
    saveas(2,'Entropy.png')
    saveas(3,'Enthalpy.png')     
end
fclose(F_1)

Explanation of code: -

  1. First to calculate the entropy, enthalpy, specific heat, and molecular weight of each species we need a data file. So, we are taking the NASA Thermodynamic data file which is in .dat file.
  2. The data in the file is cannot be calculated directly using the Matlab or any software because it is in string format.
  3. So, to convert the required data from string to data which could be calculated using the software.
  4. Firstly, we should open the file using fopen which is a command to open a file in Matlab.
  5. Now, we have to read the lines in the data file which can be done using the command fgetl.
  6. The command fgetl read the lines in the data file, now we have to convert the global temperatures at low, medium, high which are in string. So, as to convert we are using the command str2double.
  7. After converting the global temperatures now, we are converting the remaining temperature values under the global temperatures.
  8. Now, after finding all the species temperature values, we can calculate the entropy, enthalpy, specific heat, and molecular weight by calling the function and giving the input values.
  9. after calling the function we find the results, these results are being plotted.
  10. In order to save the files of each species we have to make a directory using the commands mkdir, cd, save as.

Results: -

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.