Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:

to write a program in matlab for parsing NASA thermodynamics data.

Objective :

1.To write function to extract coefficient and calculate enthalpy, entropy.

2.molecular weight of each species & display it in command window

3.To plot cp, enthalpy, entropy for each species and save the plots in image

%reading the given data
mainfile_1 = fopen('THERMO.dat','r');
header_1 = fgetl(mainfile_1);
tline_1 = fgetl(mainfile_1);
a = strsplit(tline_1,' ');
global_low_temp = str2double(a{2});
global_mid_temp = str2double(a{3});
global_high_temp = str2double(a{4});

comment_line_1 = fgetl(mainfile_1);
comment_line_2 = fgetl(mainfile_1);
comment_line_3 = fgetl(mainfile_1);

for i = 1 :53
    species_data = fgetl(mainfile_1);
    ss = strsplit(species_data,' ');
    species_name = (ss{1});

    local_low_temp = str2double(ss{end-3});
    local_mid_temp = str2double(ss{end-1});
    local_high_temp = str2double(ss{end-2});

    line_1 = fgetl(mainfile_1);
    line_2 = fgetl(mainfile_1);
    line_3 = fgetl(mainfile_1);

    x_1 = strfind(line_1,'E');
    x_2 = strfind(line_2,'E');
    x_3 = strfind(line_3,'E');

    a01 = str2double(line_1(1:x_1(1)+3));
    a02 = str2double(line_1(x_1(1)+4:x_1(2)+3));
    a03 = str2double(line_1(x_1(2)+4:x_1(3)+3));
    a04 = str2double(line_1(x_1(3)+4:x_1(4)+3));
    a05 = str2double(line_1(x_1(4)+4:x_1(5)+3));
    a06 = str2double(line_2(1:x_2(1)+3));
    a07 = str2double(line_2(x_2(1)+4:x_2(2)+3));
    a08 = str2double(line_2(x_2(2)+4:x_2(3)+3));
    a09 = str2double(line_2(x_2(3)+4:x_2(4)+3));
    a10 = str2double(line_2(x_2(4)+4:x_2(5)+3));
    a11 = str2double(line_3(1:x_3(1)+3));
    a12 = str2double(line_3(x_3(1)+4:x_3(2)+3));
    a13 = str2double(line_3(x_3(2)+4:x_3(3)+3));
    a14 = str2double(line_3(x_3(3)+4:x_3(4)+3));

%creating temperature array of 1000 evenly spaced points between local low
%temp and local high temp

T = linspace(local_low_temp, local_high_temp, 1000);

%creating a function of cp, h, s for storing formulas and to calculate it

[cp,h,s] = chs(a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,a12,a13,a14,T,local_mid_temp);

%creating a function of mol_wt for storing molecular formula and calculate
%it

[mol_wt] = molecular_weight(species_name);
molecular_weight_data = sprintf('molecular weight of %s is %f amu', species_name, mol_wt);

%identifying current folder and changing the current folder to a local
%folder and store the path (creating a new folder after each iteration)

currentfolder = pwd;
mkdir (species_name);
cd(species_name);

%plotting cp vs T graph

figure(1)
plot(T, cp, 'linewidth',3,'color','black')
xlabel('temperature(k)')
ylabel('specific heat(kj/kcalk)')
title(species_name)
grid on

figure(2)
plot(T, h, 'linewidth',3,'color','blue')
xlabel('temperature(k)')
ylabel('Enthalpy(kj)')
title(species_name)
grid on

%plotting S vs T graph

figure(3)
plot(T, s,'color','m','LineWidth',3)
xlabel('temperature(k)')
ylabel('Entropy(kj/k)')
title(species_name)
grid on

%saving the images of graph of each species in individual folders

saveas(1,'cp.png')
saveas(2,'enthalpy.png')
saveas(3,'entropy.png')
cd(currentfolder)
end
fclose(mainfile_1);


%function for chs

function [cp,h,s] = chs(a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,a12,a13,a14,T,local_mid_temp)

R = 8.314;
for i = 1:length(T)
    if(i>local_mid_temp)
        cp(i) = (a01+a02.*T(i)+a03.*T(i).^2+a04.*T(i).^3+a05.*T(i).^4)*R;
        h(i) = (a01+a02.*T(i)./2)+(a03.*T(i).^2./3)+(a04.*T(i).^3./4)+(a05.*T(i).^4./5)+(a06.*T(i).^5./6).*R.*T(i);
        s(i) = (a01.*log(T(i))+a02.*T*(i)+(a03.*T(i).^2)/2+(a04.*T(i).^3)/3+(a05.*T(i).^4)/4+a07)*R;
    else
        cp(i) = (a08+a09.*T(i)+a10.*T(i).^2+a11.*T(i).^3+a12.*T(i).^4)*R;
        h(i) = (a08+(a09.*T(i)./3)+(a10.*T(i).^2./3)+(a11.*T(i).^3./4)+(a12.*T(i).^4./5)+(a13.*T(i).^5./6)).*R.*T(i);
        S(i) = (a08.*log(T(i)))+(a09.*T(i))+(a10.*T(i).^2)/2+(a11.*T(i).^3)/3+(a12.*T(i).^4)/4+a14*R;
    end
end

%function code for mol_wt
function[mol_wt] = molecular_weight(species_name)
name_of_elements = {'H','C','N','O','AR'};
atomic_weight_of_elements = [1.0079, 12.0107, 14.007, 15.999, 39.948];

[mol_wt] = 0;
for i = 1:length(species_name)
    for j = 1:length(name_of_elements)
        if(strcmp(species_name(i),name_of_elements(j)))
            mol_wt = (mol_wt+atomic_weight_of_elements(i));
            pos = j;
        end
    end
    x = str2num(species_name(i));
    if(x>1)
        mol_wt = mol_wt + atomic_weight_of_elements(pos)*(x-1);
    end
end
end



Errors :

screenshot of all folders:

plot :

AR

C

C2H

C2H2

O

H

N

etc..,

coding for counting total lines and species :

clear all
close all
clc

mainfile_1 = fopen('THERMO.dat');
n = {};
while ~feof(mainfile_1)
    n{end+1,1}=fgetl(mainfile_1);
end
fclose(mainfile_1);
number_of_lines_in_file = numel(n)

A = n(6:4:end)
total_species_including_footer = numel(A)

output :

number_of_lines_in_file =

222


A =

55×1 cell array

{'O L 1/90O 1 G 200.000 3500.000 1000.000 1'}
{'O2 TPIS89O 2 G 200.000 3500.000 1000.000 1'}
{'H L 7/88H 1 G 200.000 3500.000 1000.000 1'}
{'H2 TPIS78H 2 G 200.000 3500.000 1000.000 1'}
{'OH RUS 78O 1H 1 G 200.000 3500.000 1000.000 1'}
{'H2O L 8/89H 2O 1 G 200.000 3500.000 1000.000 1'}
{'HO2 L 5/89H 1O 2 G 200.000 3500.000 1000.000 1'}
{'H2O2 L 7/88H 2O 2 G 200.000 3500.000 1000.000 1'}
{'C L11/88C 1 G 200.000 3500.000 1000.000 1'}
{'CH TPIS79C 1H 1 G 200.000 3500.000 1000.000 1'}
{'CH2 L S/93C 1H 2 G 200.000 3500.000 1000.000 1'}
{'CH2(S) L S/93C 1H 2 G 200.000 3500.000 1000.000 1'}
{'CH3 L11/89C 1H 3 G 200.000 3500.000 1000.000 1'}
{'CH4 L 8/88C 1H 4 G 200.000 3500.000 1000.000 1'}
{'CO TPIS79C 1O 1 G 200.000 3500.000 1000.000 1'}
{'CO2 L 7/88C 1O 2 G 200.000 3500.000 1000.000 1'}
{'HCO L12/89H 1C 1O 1 G 200.000 3500.000 1000.000 1'}
{'CH2O L 8/88H 2C 1O 1 G 200.000 3500.000 1000.000 1'}
{'CH2OH GUNL93C 1H 3O 1 G 200.000 3500.000 1000.000 1'}
{'CH3O 121686C 1H 3O 1 G 300.00 3000.00 1000.000 1'}
{'CH3OH L 8/88C 1H 4O 1 G 200.000 3500.000 1000.000 1'}
{'C2H L 1/91C 2H 1 G 200.000 3500.000 1000.000 1'}
{'C2H2 L 1/91C 2H 2 G 200.000 3500.000 1000.000 1'}
{'C2H3 L 2/92C 2H 3 G 200.000 3500.000 1000.000 1'}
{'C2H4 L 1/91C 2H 4 G 200.000 3500.000 1000.000 1'}
{'C2H5 L12/92C 2H 5 G 200.000 3500.000 1000.000 1'}
{'C2H6 L 8/88C 2H 6 G 200.000 3500.000 1000.000 1'}
{'CH2CO L 5/90C 2H 2O 1 G 200.000 3500.000 1000.000 1'}
{'HCCO SRIC91H 1C 2O 1 G 300.00 4000.00 1000.000 1'}
{'HCCOH SRI91C 2O 1H 2 G 300.000 5000.000 1000.000 1'}
{'H2CN 41687H 2C 1N 1 G 300.00 4000.000 1000.000 1'}
{'HCN GRI/98H 1C 1N 1 G 200.000 6000.000 1000.000 1'}
{'HNO And93 H 1N 1O 1 G 200.000 6000.000 1000.000 1'}
{'N L 6/88N 1 G 200.000 6000.000 1000.000 1'}
{'NNH T07/93N 2H 1 G 200.000 6000.000 1000.000 1'}
{'N2O L 7/88N 2O 1 G 200.000 6000.000 1000.000 1'}
{'NH And94 N 1H 1 G 200.000 6000.000 1000.000 1'}
{'NH2 And89 N 1H 2 G 200.000 6000.000 1000.000 1'}
{'NH3 J 6/77N 1H 3 G 200.000 6000.000 1000.000 1'}
{'NO RUS 78N 1O 1 G 200.000 6000.000 1000.000 1'}
{'NO2 L 7/88N 1O 2 G 200.000 6000.000 1000.000 1'}
{'HCNO BDEA94H 1N 1C 1O 1G 300.000 5000.000 1382.000 1'}
{'HOCN BDEA94H 1N 1C 1O 1G 300.000 5000.000 1368.000 1'}
{'HNCO BDEA94H 1N 1C 1O 1G 300.000 5000.000 1478.000 1'}
{'NCO EA 93 N 1C 1O 1 G 200.000 6000.000 1000.000 1'}
{'CN HBH92 C 1N 1 G 200.000 6000.000 1000.000 1'}
{'HCNN SRI/94C 1N 2H 1 G 300.000 5000.000 1000.000 1'}
{'N2 121286N 2 G 300.000 5000.000 1000.000 1'}
{'AR 120186AR 1 G 300.000 5000.000 1000.000 1'}
{'C3H8 L 4/85C 3H 8 G 300.000 5000.000 1000.000 1'}
{'C3H7 L 9/84C 3H 7 G 300.000 5000.000 1000.000 1'}
{'CH3CHO L 8/88C 2H 4O 1 G 200.000 6000.000 1000.000 1'}
{'CH2CHO SAND86O 1H 3C 2 G 300.000 5000.000 1000.000 1'}
{'END' }
{0×0 char }


total_species_including_footer =

55

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.