Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

Parsing NASA thermodynamic data in matlab.

DATA PARSING:

it refers to the process of converting information represented in one form another form to make it easier to work with.it is done by first partially analysing the data,understanding its logic and structure,then representing that structure in same code.

GOVERNING EQUATIONS:

properties like specific heat(c_p),enthalpy(h)&entropy(s) of a particular element or compound changes with respect to changes in temperature.

the following equation represent that below,

    Cp/R = a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4
    
    H/RT = a1 + a2 T /2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4 /5 + a6/T
    
    S/R  = a1 lnT + a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7

TERMS USED IN CODE:
*a1 to a14 = coefficients of thermal properties.

*R=Universal gas constant=8.314

*t=variable used for calculation in local temperature ranges

*lt1=local low temperature

*lt2=local mid temperature

*lt3=local high temperature

*cp=specific heat

*h=enthalpy

*s=entropy

*AW=atomic weight

FUNCTION CODE AND EXPLANATION:

1.In this code,we gives input values of all coefficients and temperature

2.open the new script and add function with tvalues name.since we are calculating three properties,we give three output variable i.e.,fv1,fv2,fv3.

function [fv1,fv2,fv3]=tvalues(a1,a2,a3,a4,a5,a6,a7,t,lt2,a8,a9,a10,a11,a12,a13,a14)

3.we know universal gas constant value is 8.314 which is given to a variable called R.

R=8.314

4.we know the temperature are from low to high.coefficient are substituted in formulas in two types.if the temperature is more than local mid_temperature,a1 to a7 coefficients are used for calculations.if the temperature is less than local mid_temperature,a8 to a14 coefficient are used for calculation.this can be done byu if-else statement.the formula is expresses intrest of specific heat,enthalpy,entropy.

if t>lt2
fv1=R*(a1+(a2*t)+(a3*(t).^2)+(a4*(t).^3)+(a5*(t).^4));
fv2=R*t.*(a1+((a2*t)./2)+((a3*(t).^2)/3)+((a4*(t).^3)/4)+((a5*(t).^4)/5)+(a6./t));
fv3=R*((a1*log(t))+(a2*t)+((a3*(t).^2)/2)+((a4*(t).^3)/3)+((a5*(t).^4)/4)+a7);

5.else statement is used for other temperatures.


else
fv1=R*(a8+(a9*t)+(a10*(t).^2)+(a11*(t).^3)+(a12*(t).^4));
fv2=R*t.*(a8+((a9*t)./2)+((a10*(t).^2)/3)+((a11*(t).^3)/4)+((a12*(t).^4)/5)+(a13./t));
fv3=R*((a8*log(t))+(a9*t)+((a10*(t).^2)/2)+((a11*(t).^3)/3)+((a12*(t).^4)/4)+a14);

6.close the function with the end command.

end

FUNCTION CODE:

function [fv1,fv2,fv3]=tvalues(a1,a2,a3,a4,a5,a6,a7,t,lt2,a8,a9,a10,a11,a12,a13,a14)

R=8.314;

if t>lt2
    fv1=R*(a1+(a2*t)+(a3*(t).^2)+(a4*(t).^3)+(a5*(t).^4));
    fv2=R*t.*(a1+((a2*t)./2)+((a3*(t).^2)/3)+((a4*(t).^3)/4)+((a5*(t).^4)/5)+(a6./t));
    fv3=R*((a1*log(t))+(a2*t)+((a3*(t).^2)/2)+((a4*(t).^3)/3)+((a5*(t).^4)/4)+a7);
    
else
    fv1=R*(a8+(a9*t)+(a10*(t).^2)+(a11*(t).^3)+(a12*(t).^4));
    fv2=R*t.*(a8+((a9*t)./2)+((a10*(t).^2)/3)+((a11*(t).^3)/4)+((a12*(t).^4)/5)+(a13./t));
    fv3=R*((a8*log(t))+(a9*t)+((a10*(t).^2)/2)+((a11*(t).^3)/3)+((a12*(t).^4)/4)+a14);
end

CODE PROCEDURE AND EXPLANATION:

1.Addthe statement as shown in below

clear all

close all

clc

2.first we need to open the data file.to open the data file,we use fopen command in reading mode.the file is used to a variable called fp.make sure that the file is in the same directory of MATLAB current running program.

file=fopen('THERMO.dat','r');

3.now,we need to read the data which can be done by the command fgetl.it is a command which reads the data line by line and every time we run it.we get next line.the file contains a header and we give name as the heading variable.

heading=fgetl(file);

4.next line contains global temperature of species.these are extracted by fgetl command.this is assigned to a gbt variable.

gbt=fgetl(file);

5.the output is in string format.we need to split the string and convert them into numbers.splitting is done by strsplit and converting can be done by str2num commands.

gts=strsplit(gbt,' ');
for i=1:length(gts)
gt{i}=str2num(gts{i});
end

6.assigning global temperature.

global_low_temp=gt{2};
global_mid_temp=gt{3};
global_high_temp=gt{4};

7.next three lines are comments which can be neglected and drawn by fgetl command.

c1=fgetl(file);
c2=fgetl(file);
c3=fgetl(file);

8. the header is extracted and there are 53 species in the data.we can observation that each data consists of coefficient values are represented in the four lines. by applying logic we can extract these coefficients for calculating thermal properties.

for ce=1:53

ls=fgetl(file);
l1=strsplit(ls, ' ');
species=l1{1};

lt1=str2num(l1{end-3});
lt2=str2num(l1{end-2});
lt3=str2num(l1{end-1});

9.we got local temperatures for the species.these are the temperatures which are used for calculation.the temperature range can be given as follows.

t=linspace(lt1, lt2, 100);

10.there are three lines of coefficient in the data file.the valued are represented in the power of 'E'.these values are extracted by finding the string 'E' positions and converted into numbers.

ls1=fgetl(file);
l2=strfind(ls1,'E');

a1=str2num(ls1(1:l2(1)+3));
a2=str2num(ls1(l2(1)+4:l2(2)+3));
a3=str2num(ls1(l2(2)+4:l2(3)+3));
a4=str2num(ls1(l2(3)+4:l2(4)+3));
a5=str2num(ls1(l2(4)+4:l2(5)+3));

11.till now,we got coefficient from the second line and we need to extract from the third and fourth lines.the coefficients are represented from a1 to a14.next coefficient are also found in the same manner.

ls2=fgetl(file);
l3=strfind(ls2,'E');

a6=str2num(ls2(1:l3(1)+3));
a7=str2num(ls2(l3(1)+4:l3(2)+3));
a8=str2num(ls2(l3(2)+4:l3(3)+3));
a9=str2num(ls2(l3(3)+4:l3(4)+3));
a10=str2num(ls2(l3(4)+4:l3(5)+3));

ls3=fgetl(file);
l4=strfind(ls3,'E');

a11=str2num(ls3(1:l4(1)+3));
a12=str2num(ls3(l4(1)+4:l4(2)+3));
a13=str2num(ls3(l4(2)+4:l4(3)+3));
a14=str2num(ls3(l4(3)+4:l4(4)+3));

12.we got all the 4 values of coefficient for all 53 species.now we need to calculate the values of specific heat,enthalpy,entropy.we have the temperature higher than lt2.have coefficient from a1 to a7 and a temperature lower than lt2 have coefficient a8 to a14.

13.the formula for specific heat,enthalpy,entropy are given in another script.here we call the function and perform calculations.

[cp, h, s]=tvalues(a1,a2,a3,a4,a5,a6,a7,t,lt2,a8,a9,a10,a11,a12,a13,a14);

14.therefore we got the values of thermal properties.we should plot the graphs of these thermal properties individually against respective localtemperature.since we get three plots foe each species,we can't find the respective plot in the file directory.to avoid confusion,we create folders for plots of the respective species and store them in a very well manner.

15.creating a folder in the current directory can be done by the command mkdir.this create a seperate folder for each species and stores the plot in it.we are creating in the name of species.

mkdir(species)

16.we know that MATLAB process in a particular directory..to store the plots in the species folder,we need to change the directory.changing can be done by using another command cd.

cd(species)

17.we should plot three figures of specific heat,enthalpy and entropy against temperature.three figures are plotted seperately plotting graphs and labelling them are done.

figure(1)
plot(t,cp)
xlabel('temperature')
ylabel('specific heat')
title('t vs cp')
saveas(gcf, 't_vs_cp.jpg')

18.same command are use for second and third figures.

figure(2)
plot(t,h)
xlabel('temperature')
ylabel('enthalpy')
title('t vs h')
saveas(gcf, 't_vs_h.jpg')


figure(3)
plot(t,s)
xlabel('temperature')
ylabel('entropy')
title('t vs s')
saveas(gcf, 't_vs_s.jpg')

19.the plots are saves in species folder in the format of .jpg.since the program should run in the process in the previous directory,we need to change the running program into the previous directory.this can be done by using command cd ../

cd ../

20.till now,we solved values of each thermal properties and also plots are generated of these properties against temperature in seperate folders.

21.we should calculatee each species molecular weight.this can be done by the following code.

22.initially,species atomic names are assigned.

s_atomic_name={'H','C','N','O','AR'};

24.initially the weight values are zero for countless loops.

W=0;

25.add for loops with counter variables for q and p with lengths of species name and s_atomic_name.

for p=1:length(species)

for q =1:length(s_atomic_name)

26.add if statements for comparing two strings.two strings are species name and s_atomic_name

if strcmp(species(q),s_atomic_name(q))==1

27.add weight W counter with atomic weight AW.

W=W+AW(p);

28.position variable values is assignes with q.

position = q;

29.end the is statement with the end command

end

30.the string is converted to num by using command 'str2double' with n variable.

n=str2double(species(p));

31.add if statement and weight are calculated and ens it.the statement should be written in such a way thay,left and right sides should be the same numbdr of elements.

if n> 1
%finding weights
W=W+((AW(position))*(n-1));

end

32.end one loop

end

33.display molecular weights with the species name.

molecular_weight=sprintf('%s, %f',species,W)

34.end the final loop.

end

MAIN CODE:

clear all 
close all
clc

%file opening
file=fopen('THERMO.dat','r');

%extracting heading
heading=fgetl(file);

%global temperatures
gbt=fgetl(file);
gts=strsplit(gbt,' ');
for i=1:length(gts)
    gt{i}=str2num(gts{i});
end

%assigning global temperature
global_low_temp=gt{2};
global_mid_temp=gt{3};
global_high_temp=gt{4};

%extracting comments
c1=fgetl(file);
c2=fgetl(file);
c3=fgetl(file);

%extracting coefficients
for ce=1:53
    
    ls=fgetl(file);
    l1=strsplit(ls, ' ');
    species=l1{1};
    
    lt1=str2num(l1{end-3});
    lt2=str2num(l1{end-2});
    lt3=str2num(l1{end-1});
    
    t=linspace(lt1, lt2, 100); 
    
    %extracting line 2
    ls1=fgetl(file);
    l2=strfind(ls1,'E');
    
    a1=str2num(ls1(1:l2(1)+3));
    a2=str2num(ls1(l2(1)+4:l2(2)+3));
    a3=str2num(ls1(l2(2)+4:l2(3)+3));
    a4=str2num(ls1(l2(3)+4:l2(4)+3));
    a5=str2num(ls1(l2(4)+4:l2(5)+3));
    
    %extracting line 3
    ls2=fgetl(file);
    l3=strfind(ls2,'E');
    
    a6=str2num(ls2(1:l3(1)+3));
    a7=str2num(ls2(l3(1)+4:l3(2)+3));
    a8=str2num(ls2(l3(2)+4:l3(3)+3));
    a9=str2num(ls2(l3(3)+4:l3(4)+3));
    a10=str2num(ls2(l3(4)+4:l3(5)+3));
    
    %extracting line 4
    ls3=fgetl(file);
    l4=strfind(ls3,'E');
    
    a11=str2num(ls3(1:l4(1)+3));
    a12=str2num(ls3(l4(1)+4:l4(2)+3));
    a13=str2num(ls3(l4(2)+4:l4(3)+3));
    a14=str2num(ls3(l4(3)+4:l4(4)+3));
    
    %calculating the thermal properties values
    [cp, h, s]=tvalues(a1,a2,a3,a4,a5,a6,a7,t,lt2,a8,a9,a10,a11,a12,a13,a14);
    
    %creating floder
    mkdir(species)
    
    %changing directory
    cd(species)
      
    %plots are generated and saved in seperate folder
    figure(1)
    plot(t,cp)
    xlabel('temperature')
    ylabel('specific heat')
    title('t vs cp')
    saveas(gcf, 't_vs_cp.jpg')
    
    figure(2)
    plot(t,h)
    xlabel('temperature')
    ylabel('enthalpy')
    title('t vs h')
    saveas(gcf, 't_vs_h.jpg')
    
    
    figure(3)
    plot(t,s)
    xlabel('temperature')
    ylabel('entropy')
    title('t vs s')
    saveas(gcf, 't_vs_s.jpg')
    
    %changing folder to previous folder
    cd ../
   
    %calculating molecular weights
    s_atomic_name={'H','C','N','O','AR'}
    AW=[1 12 14 16 40];
    W=0;
    
    for p = 1:length(species)
        for q = 1:length(s_atomic_name)
            %comparing with strings
            if strcmp(species(p), s_atomic_name(q))==1
                W=W+AW(q);
                position=q;
            end
        end
        n=str2double(species(p));
        if n> 1
            %finding weights
            W=W+((AW(position))*(n-1));
        end
    end
    molecular_weight=sprintf('%s, %f',species,W)
end

  

PLOT GENERATION :

O

C:

H:

FOLDER CREATED IN FILE MANAGER:

POINTS TO REMEMBER:

* The data file should be in the same directory for opening the file.

*the logic should be applied in the code corectlly while extracting coefficient.

*fgetl is the command which is used for extracting data line.each time we use it,the next line is extracted.

*str2num is a command which is used to convert from string to number.

*strfind is used to find a particular string

*mkdir is used to create a folder and cd../ is used to change the running program into a different folder.

CONCLUSION:

Hence,by using MATLAB,coefficient are extracted from the data file proposed by nasa .values of the thermal properties are calculated in their respective temperature ranges.plots are created for all species of specific heat,enthalpy and entropy against respective local temperature range.each plot is created in species floder.molecular weight are calculated for all species.

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.