Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:

To calculate the thermodynamic properties of gas species provided in NASA thermodynamic data

Objective:

  • to write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file. You will be reading this file NASA thermodynamic data
  • Calculate the molecular weight of each species
  • Plot the Cp, Enthalpy, and Entropy for the local temperature range specified for each species.

Definition:

File parsing basically refers to how information can be read from a particular text file or any type of file and how that information can be used in computer programing

Data Provided:

Cp = Specific Heat

R = Gas Constant = 8.314

T = Temperature

H = Enthalpy

S = Entropy

Program:

  1. to Calculate the specific heat, enthalpy, entropy, for each species we need to create a function for each formula. and write the program to identify the global temperature.
  2. then create a for loop to identify each species of the block and read the coefficient for each species and store them into variables which will help to calculate the formula.
  3. after which plot graph for species and create the folder

Function for Specific Heat:

function [C_p] = specific_heat(b1,b2,b3,b4,b5,b8,b9,b10,b11,b12,local_mid_temp,T,R)
if (T>local_mid_temp)
   
    C_p = (b1 + b2*T + b3*(T.^2) + b4*(T.^3) + b5*(T.^4)) * R;
else
    
    C_p = (b8 + b9*T + b10*(T.^2) + b11*(T.^3) + b12*(T.^4)) * R;
end

Function for Enthalpy:

function [H] = enthalpy(b1,b2,b3,b4,b5,b6,b8,b9,b10,b11,b12,b13,local_mid_temp,T,R)

for i = 1:length(T)
    
if (T(i)>local_mid_temp)
    
    H(i) = ((b1*T(i)) + (b2*(T(i).^2)/2) + (b3*(T(i).^3)/3) + (b4*(T(i).^4)/4) + (b5*(T(i).^5)/5) + (b6)) * R;
    
else
    
    H(i) = ((b8*T(i)) + (b9*(T(i).^2)/2) + (b10*(T(i).^3)/3) + (b11*(T(i).^4)/4) + (b12*(T(i).^5)/5) + (b13)) * R;
    
end
end

Function for Entropy:

function [S] = entropy(b1,b2,b3,b4,b5,b7,b8,b9,b10,b11,b12,b14,local_mid_temp,T,R)

for i = 1:length(T)
    
if (T(i)>local_mid_temp)
    
    S(i) = (b1 * log(T(i)) + b2*T(i) + b3*((T(i).^2)/2) + b4*((T(i).^3)/3) + b5*((T(i).^4)/4) + b7) * R;
    
else
    
    S(i) = (b8 * log(T(i)) + b9*T(i) + b10*((T(i).^2)/2) + b11*((T(i).^3)/3) + b12*((T(i).^4)/4) + b14) * R; 
    
end
end

Function for molecular weight:

function [M] = mol_weight(spec_name)

atomic_mass = [16,1,12,14,40];
elements = ['O','H','C','N','A'];

wt = 0;

for i = 1:length(spec_name)
    for j = 1:length(atomic_mass)
        if strcmp(spec_name(i),elements(j))
            wt = wt + atomic_mass(j);
            k = j;
        end
    end
    x = str2double(spec_name(i));
    
    if(x>1)
        wt = wt+(atomic_mass(k) * (x-1));
    end
end
M = wt;
end

Full Program:

clear all
close all
clc
R = 8.134; %gas constant

a1 = fopen('THERMO.dat','r');
fgetl(a1);
tline = fgetl(a1);
A = strsplit(tline,' ');
global_low_temperature = str2double(A{1});
global_mid_temperature = str2double(A{2});
global_high_temperature = str2double(A{3});

fgetl(a1);
fgetl(a1);
fgetl(a1);

for i = 1:53
    tline1 = fgetl(a1);
    B = strsplit(tline1,' ');
    spec_name = B{1};
    C = length(B);
    %C = strfind(tline1,'G');
    %local_temp = (C+4:length(tline1))
    %D = strsplit(local_temp,' ')
    local_low_temp = str2double(B(C-3));
    local_mid_temp = str2double(B(C-1));
    local_high_temp = str2double(B(C-2));
    
    tline = fgetl(a1);
    b = strfind(tline,'E');
    b1 = tline(1:b(1)+3);
    b1 = str2double(b1);
    b2 = tline(b(1)+4:b(2)+3);
    b2 = str2double(b2);
    b3 = tline(b(2)+4:b(3)+3);
    b3 = str2double(b3);
    b4 = tline(b(3)+4:b(4)+3);
    b4 = str2double(b4);
    b5 = tline(b(4)+4:b(5)+3);
    b5 = str2double(b5);
    
    line2 = fgetl(a1);
    b = strfind(line2,'E');
    b6 = line2(1:b(1)+3);
    b6 = str2double(b6);
    b7 = line2(b(1)+4:b(2)+3);
    b7 = str2double(b7);
    b8 = line2(b(2)+4:b(3)+3);
    b8 = str2double(b8);
    b9 = line2(b(3)+4:b(4)+3);
    b9 = str2double(b9);
    b10 = line2(b(4)+4:b(5)+3);
    b10 = str2double(b10);
    
    line3 = fgetl(a1);
    b = strfind(line3,'E');
    b11 = line3(1:b(1)+3);
    b11 = str2double(b11);
    b12 = line3(b(1)+4:b(2)+3);
    b12 = str2double(b12);
    b13 = line3(b(2)+4:b(3)+3);
    b13 = str2double(b13);
    b14 = line3(b(3)+4:b(4)+3);
    b14 = str2double(b14);
    
    T = linspace(local_low_temp,local_high_temp,100);
    
    C_p = specific_heat(b1,b2,b3,b4,b5,b8,b9,b10,b11,b12,local_mid_temp,T,R);
    H = enthalpy(b1,b2,b3,b4,b5,b6,b8,b9,b10,b11,b12,b13,local_mid_temp,T,R);
    S = entropy(b1,b2,b3,b4,b5,b7,b8,b9,b10,b11,b12,b14,local_mid_temp,T,R);
    
    %mkdir(['F:',spec_name])
    %cd(spec_name)
     mkdir(['F:\Matlab Prog\Matlab Basics for Mechanical\Week4_Parsing\Newplots\',spec_name]);
     cd(['F:\Matlab Prog\Matlab Basics for Mechanical\Week4_Parsing\Newplots\',spec_name]);

    figure(1)
    plot(T,C_p);
    title('specific_heat v/s temperature');
    xlabel('temperature');
    ylabel('specific_heat');
    %image = getframe(gcf);
    %imwrite(image.cdata,'cp v/s T.png');
    saveas(1,'specific heat','jpg')
    
    figure(2)
    plot(T,H);
    title('enthalpy v/s temperature');
    xlabel('temperature');
    ylabel('enthalpy');
    %image = getframe(gcf);
    %imwrite(image.cdata,'E v/s T.png');
    saveas(2,'enthalpy','jpg')
    
    figure(3)
    plot(T,S);
    title('entropy v/s temperature');
    xlabel('temperature');
    ylabel('entropy');
    %image = getframe(gcf);
    %imwrite(image.cdata,'S v/s T.png');
    saveas(3,'entropy','jpg')
    
    molecular_weight = mol_weight(spec_name);
    z = sprintf('%s mol_weight= %f',spec_name,molecular_weight);
    disp(z)
end

Output:

Plot for O2:

Plot for CO2:

Plot for N2:

Creating a separate folder to save all the different plots of different species

Calculation of molecular weight of each species:

Errors faced while running the program:

1. Wasnt able to read global temperatures from the Thermo data file but later I found out that I mistakenly edited the data file it self creating extra spaces because of which my program wasn't executing properly.

2. Wasn't able to code a program to calculate molecular weight for each species but I got advice and guidance from Raj Kumar Sir,

3. There was an issue where all the plots for all species were turning out to be the same and later by continuous evaluation I found out I was calculating the same coefficient of O2 for all the species which was later fixed.

4. got help from Raj Kumar Sir to code properly in the correct way to create a folder and save all plots.

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.