Challenge

Project 1 - Parsing NASA thermodynamic data

Aim : In this projects we will find Enthalpy,Entropy and speecific heat for various gases by reading NASA thermodynamic data file where various coificients were listed by nasa .After geting Entropy(s), Enthsalpy(h), and Specific heat(CP) we will plot them with rispect to temprature range

Objectives :

  • Load Thermodynamic data file
  • Read file using 'fopen' and 'fgetl' commands
  • Extract data (ie Species,Temprature values , constants a1 to a14) using 'strsplit' ,'strfind' commands
  • convert string to number using 'str2double'
  • Define function for Entropy,Enthalpy,Specific heat, Molecular weight
  • genrate loop in main code to extract data from each line (line 6 to line 59)
  • Plot s vs Temp range , h vs temp range , cp vs temp range
  • Write text file including calculated molecular weight for each molecule
  • Create folder for each molecule to save Plots for the same

Theory:

Thermodynamic properties:

Enthalpy: It is an total amount of heat contain in system ie(H=U + PV)

U=internal energy

PV=work energy

Entropy: Entropy is a thermal energy which is unavailable for doing useful work per unit temprature due to rndomness in molecules.

Specific heat :Amount of heat required to raise temprature of system by 1 degree cilcious per Kg of system.

what is File parsing

Comands used in program are

1)fopen
SYNTAX
fileID = fopen(filename)
fileID = fopen(filename,permission) ====Open file, or obtain information about open files

2)strfind
Find strings within other stringscollapse all in page
Syntax
k = strfind(str,pat)

3)strsplit
Split string or character vector at specified delimitercollapse all in page
Syntax
C = strsplit(str)
C = strsplit(str,delimiter)

4)str2double
Convert strings to double precision valuescollapse all in page
Syntax
X = str2double(str)
Description
example
X = str2double(str) converts the text in str to double precision values

5)mkdir
Make new foldercollapse all in page
Syntax
mkdir folderName
mkdir parentFolder folderName
status = mkdir(___)
[status,msg] = mkdir(___)
[status,msg,msgID] = mkdir(___)
Description
example
mkdir folderName creates the folder folderName. 

File writing

%file writing
clear all
close all
clc
f1=fopen('file writing_1.txt','w')
 %16s = after 16 charector gap
%f=float
\n=next line

fprintf(f1,'%16s %16s\n','time','Pressure')    
time=linspace(0,10,500);                         
pressure=rand(1,500);
for i=1:length(time)
    fprintf(f1,'%16.7f %16.7f',time(i),pressure(i));
     fprintf(f1,'\n');
end
fclose(f1)


output''''

 time         Pressure
       0.0000000        0.5822492
       0.0200401        0.5407393
       0.0400802        0.8699410
       0.0601202        0.2647790
       0.0801603        0.3180741
       0.1002004        0.1192145
       0.1202405        0.9398295
       0.1402806        0.6455519
       0.1603206        0.4794632
       0.1803607        0.6393170
       0.2004008        0.5447161
       0.2204409        0.6473115
        continue....

Function code to calculate s,h,cp:

Entropy:

%function for entropy
function s_value=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,midlt,TRange,R);
if(TRange>midlt)
    s_value=(a1*log(TRange))+ (a2.*TRange)+((a3.*TRange.^2)./2)+((a4.*TRange.^3)./3)+((a5.*TRange.^4)/4)*(a7)*(R);
else 
    s_value=(a8*log(TRange))+ (a9.*TRange)+((a10.*TRange.^2)./2)+((a11*TRange.^3)./3)+((a12.*TRange.^4)/4)*(a14)*(R);
end
end 

Enthalpy:

%function for enthaulpy
function h_value=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,midlt,TRange,R)
if(TRange>midlt)
    h_value=(a1+((a2.*TRange)./2)+ ((a3.*TRange.^2)./3)+((a4.*TRange.^3)./4)+((a5.*TRange.^4)./5)+((a6.*TRange.^5)).*(R.*TRange));
else 
    h_value=(a8+((a9.*TRange)./2)+ ((a10.*TRange.^2)./3)+((a11.*TRange.^3)./4)+((a12.*TRange.^4)./5)+((a13.*TRange.^5)).*(R.*TRange));
end
end 

Specific heat

%code for cp_value
function cp_value=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,midlt,TRange,R);
if(TRange>midlt)
    cp_value=(a1+((a2.*TRange))+ ((a3.*TRange.^2))+((a4.*TRange.^3))+((a5.*TRange.^4)))*R;
else 
    cp_value=(a8+((a9.*TRange))+ ((a10.*TRange.^2))+((a11.*TRange.^3))+((a12.*TRange.^4)))*R;
end
end

Molecular weight

%function for molecular weeight
function [molecule_weight]=MW(species);%access species cell
weight=0;
atoms=['H','C','N','O','A'];
mass=[1,12,14,16,40];
for i=1:length(species)  %Determine no of strings in species
    for j=1:length(atoms)  %length of atoms array
        if strcmp(species(i),atoms(j)) %compare species and atoms 
%if match then it will pick value from mass array for same j'th number
            weight=weight+mass(j);
            y=j
        end
    end
    %if there are more than 1 atoms of same species them mass need to be
    %multipied so...
    number=str2num(species(i));
    if(number>1)
        weight= weight+(mass(y)*(number-1));
    end
end
molecule_weight=weight
end


Main code:

close all
clear all
clc
f1=fopen("THERMO.dat",'r');  % load file
fgetl(f1);           %read first line
data=fgetl(f1);
A=strsplit(data,' ');  %split line for delimeter as space ' '
                      
%==================================================================
%extract global tempratures
low_gt=str2double(A{2});
mid_gt=str2double(A{3});
hig_gt=str2double(A{4});

m1=fopen('molecular weight .txt','w') %create ttext file
fprintf(m1,'-------------MOLECULAR WEIGHT OF MOLECULES------------------')


for i=1:3
    g=fgetl(f1);% skip first 3 lines
end


for j =1:53    % read next 53 lines in loop
line2=fgetl(f1);   
s=strsplit(line2,' ');
species=s{1};                 %first element of s array is species ie atom name
a=strfind(line2,'G');          % find  G in line to seprate temprature values
temp_values=line2(a+4:length(line2));
b=strsplit(temp_values,' ');  %b= temprature array

lowlt=str2double(b{1});       %lt:local temprature
midlt=str2double(b{2});
highlt=str2double(b{3});

%==================Find constants a1 to a14===============================%

L1=fgetl(f1);
E=findstr(L1,'E');   


        a1=str2double(L1(1:(E(1))+3));
        a2=str2double(L1(E(1)+4:E(2)+3));
        a3=str2double(L1(E(2)+4:E(3)+3));
        a4=str2double(L1(E(3)+4:E(4)+3));
        a5=str2double(L1(E(4)+4:E(5)+3))
        
%--------------------------------------------------------------------------        

        L2=fgetl(f1);
%         E=strfind(L2,'E');
     
        a6=str2double(L2(1:(E(1))+3));
        a7=str2double(L2(E(1)+4:E(2)+3));
        a8=str2double(L2(E(2)+4:E(3)+3));
        a9=str2double(L2(E(3)+4:E(4)+3));
        a10=str2double(L2(E(4)+4:E(5)+3));
 %-------------------------------------------------------------------------      

        L3=fgetl(f1);
%         E=strfind(L3,'E')
        a11=str2double(L3(1:(E(1))+3));
        a12=str2double(L3(E(1)+4:E(2)+3));
        a13=str2double(L3(E(2)+4:E(3)+3));
        a14=str2double(L3(E(3)+4:E(4)+3));

        R=8.314; %universal gas constant
        TRange=linspace(lowlt,highlt,500); 

   
%===============use functions to deterine CP,H,S values===================%

   
%obtaining value of specific heat 
cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,midlt,TRange,R);
%obtaining value of entropy
s=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,midlt,TRange,R);
%obtaining value of enthalpy
h=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,midlt,TRange,R);

%===============================PLOT======================================% 

%plotting----------------------SPECIFIC HEAT
figure(1)
plot(TRange,cp,'LineWidth',3,'color','g');
xlabel('Temprature');
ylabel('Specific Heat');
title('Temprature Vs Specific Heat');

%Ploting----------------------ENTROPY
figure(2)
plot(TRange,s,'LineWidth',3,'color','r');
xlabel('Temprature');
ylabel('Entropy');
title('Temprature Vs Entropy');

%Ploting-----------------------ENTHALPY
figure(3)
plot(TRange,h,'LineWidth',3,'color','b');
xlabel('Temprature');
ylabel('Enthalpy');
title('Temprature Vs Enthalpy');

%=====================Make file to save plots data========================%
mkdir(species)
cd(species)      %current directory will made with  species name for each loop  
saveas(1,'specific heat','fig')
saveas(2,'Entropy','fig')
saveas(3,'Enthalpy','fig')

disp(species)
molecule_weight=MW(species)
cd("/MATLAB Drive")  %again set cd as parent drive
fprintf(m1,'\n%6s moleculer weight is %6f\n',species,molecule_weight)
%write molecular weight to file  

end
fclose(f1)
fclose(m1)


RESULTS:

Constants

PLOTING

Plots saved to created folder with name of species

Specific heat Plot for O2 and NH3

Entropy plot for O2 and NH3

Enthalpy PLot for O2 and NH3

From above plots of oxygen and nh3 we can study behaviour of thermodynamic properties with respect to temprature

Molecular weight text file writing

-------------MOLECULAR WEIGHT OF MOLECULES------------------
     O moleculer weight is 16.000000

    O2 moleculer weight is 32.000000

     H moleculer weight is 1.000000

    H2 moleculer weight is 2.000000

    OH moleculer weight is 17.000000

   H2O moleculer weight is 18.000000

   HO2 moleculer weight is 33.000000

  H2O2 moleculer weight is 34.000000

     C moleculer weight is 12.000000

    CH moleculer weight is 13.000000

   CH2 moleculer weight is 14.000000

CH2(S) moleculer weight is 14.000000

   CH3 moleculer weight is 15.000000

   CH4 moleculer weight is 16.000000

    CO moleculer weight is 28.000000

   CO2 moleculer weight is 44.000000

   HCO moleculer weight is 29.000000

  CH2O moleculer weight is 30.000000

 CH2OH moleculer weight is 31.000000

  CH3O moleculer weight is 31.000000

 CH3OH moleculer weight is 32.000000

   C2H moleculer weight is 25.000000

  C2H2 moleculer weight is 26.000000

  C2H3 moleculer weight is 27.000000

  C2H4 moleculer weight is 28.000000

  C2H5 moleculer weight is 29.000000

  C2H6 moleculer weight is 30.000000

 CH2CO moleculer weight is 42.000000

  HCCO moleculer weight is 41.000000

 HCCOH moleculer weight is 42.000000

  H2CN moleculer weight is 28.000000

   HCN moleculer weight is 27.000000

   HNO moleculer weight is 31.000000

     N moleculer weight is 14.000000

   NNH moleculer weight is 29.000000

   N2O moleculer weight is 44.000000

    NH moleculer weight is 15.000000

   NH2 moleculer weight is 16.000000

   NH3 moleculer weight is 17.000000

    NO moleculer weight is 30.000000

   NO2 moleculer weight is 46.000000

  HCNO moleculer weight is 43.000000

  HOCN moleculer weight is 43.000000

  HNCO moleculer weight is 43.000000

   NCO moleculer weight is 42.000000

    CN moleculer weight is 26.000000

  HCNN moleculer weight is 41.000000

    N2 moleculer weight is 28.000000

    AR moleculer weight is 40.000000

  C3H8 moleculer weight is 44.000000

  C3H7 moleculer weight is 43.000000

CH3CHO moleculer weight is 44.000000

CH2CHO moleculer weight is 43.000000

Conclusion:

With file parsing we extracted thermodynamic data from text file and represented it in graphical form which make it more easy to study thermodynamic properties of various gases

learned to genrate text files and write with data from main code.

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.