Project

NASA File Parsing to evaluate values of Thermodynamic Properties at different Temperature Range

Aim:- To calculate the enthalpy,specific heat,entropy,molecular weight values of the 53 species by reading the 14 coefficients and inputing their values in the formula . Also plotting the enthalpy,entropy,specific heat vs temperature .

Introduction:- File parsing is a technique of reading the file given by NASA which has polynomials which are used to evalute the value of thermodynamic properties such entropy,enthalpy,specific heat.

Explanation:-

  1. Open the file with syntax fileopen(\'Filename.dat\',\'r\') where \'r\' is mentioned when we have to read the file while \'w\' is written when we have to overwrite some data in the file .
  2. fgetl used to read line from the file and the first line is the header so we assign header to value printed.
  3. fscanf command is used to scan the next line as float variable which comes out as array of 1X3.This array consists value of global high temperature, mid temperature and low temperature . 1st element is assigned value of global low temperature , 2nd element is assigned value of global mid temperature and 3rd element is assigned value of high temperature.
  4. Next 3 lines are skipped by using fgetl command 3 times.Since there are total of 53 species the for loop will run for 53 times.
  5. Seperating the string by space and then converting it to number and assigning lower,mid,high temperature for the species which we use in the formula.
  6. Subsequent line has the species name then in the next line which is the string we find the letter \'E\' as the reference for acquiring the coefficients.
  7. str2double command is used to convert string to double precision values . Thus we acquire all the 14 coefficients in the same manner.
  8. mkdir command is used to make a new directory for each species after the command is executed .
  9. Function for specific heat,entropy,enthalpy takes value of the 14 coeffcients as the input and Universal gas constant and temperature value when entered in the below mentioned formula gives the value for the specific species .

10.mkdir is used to create directory in the system with the subfolder name as the species name.

11.Linear spacing of the lower and high temperature of the species is done for plotting.

12.Function is created for specific heat,entropy,enthalpy where inputs are the 14 coefficients,Universal gas constant and the temperature range values.

13.Figure command is used to plot three graphs of entropy,enthalpy and specific heat vs over temperature range .

14.cd is used to change the directory and in the next line molecular mass function is defined.

15.saveas command is used to save the plot in the different directory specified by cd command and then values of molecular mass are plotted in the command window.

Program:-

clear all
close all
clc

%universal gas constant
R=8.314;

%reading header 
f1=fopen(\'THERMO.dat\',\'r\');
Header=fgetl(f1);

%global temperatures
A=fscanf(f1,\'%f \\n\',[1 3]);
global_low_temp=A(1);
global_mid_temp=A(2);
global_high_temp=A(3);

%skipping next 3 lines
line1=fgetl(f1);
line2=fgetl(f1);
line3=fgetl(f1);
 for i=1:53;
     name_line=fgetl(f1);
     
     %extracting name of species
     S=strsplit(name_line,\' \');
     species=name_line(1:6);
     LLT=str2num(S{end-3});
     LHT=str2num(S{end-2});
     LMT=str2num(S{end-1});
     %Reading coefficients
     tline1=fgetl(f1);
     %Extracting coefficients
     y1= strfind(tline1,\'E\');
     a1=tline1(1:y1(1)+3);
     a1=str2double(a1);
     a2=tline1((y1(1)+4:(y1(2)+3)));
     a2=str2double(a2);
     a3=tline1((y1(2)+4:(y1(3)+3)));
     a3=str2double(a3);
     a4=tline1((y1(3)+4:(y1(4)+3)));
     a4=str2double(a4);
     a5=tline1((y1(4)+4:(y1(5)+3)));
     a5=str2double(a5);
     tline2=fgetl(f1);
     y2= strfind(tline2,\'E\');
     a6=tline2(1:y2(1)+3);
     a6=str2double(a6);
     a7=tline2((y2(1)+4:(y2(2)+3)));
     a7=str2double(a7);
     a8=tline2((y2(2)+4:(y2(3)+3)));
     a8=str2double(a8);
     a9=tline2((y2(3)+4:(y2(4)+3)));
     a9=str2double(a9);
     a10=tline2((y2(4)+4:(y2(5)+3)));
     a10=str2double(a10);
     tline3=fgetl(f1);
     y3= strfind(tline3,\'E\');
     a11=tline3(1:y3(1)+3);
     a11=str2double(a11);
     a12=tline3((y3(1)+4:(y3(2)+3)));
     a12=str2double(a12);
     a13=tline3((y3(2)+4:(y3(3)+3)));
     a13=str2double(a13);
     a14=tline3((y3(3)+4:(y3(4)+3)));
     a14=str2double(a14);
     
     %Creating folders with species name 
     subfolder_name=species;
     mkdir([\'C:\\Users\\Amogh\\Documents\\project1\',subfolder_name]);
     
     T=linspace(LLT,LHT,100);
     %function for cp
     Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,global_low_temp,global_mid_temp,global_high_temp);
     
     %function for entropy
      S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,global_low_temp,global_mid_temp,global_high_temp);
      
      %function for enthalpy
      H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,global_low_temp,global_mid_temp,global_high_temp);
  
     %graphs
     
     figure(1) 
     %T vs specific heat
     plot(T,Cp)
     title(subfolder_name)
     xlabel(\'Temp\')
     ylabel(\'Specific Heat\')
     
     figure(2) 
     %For T vs Enthalpy
     plot(T,H)
     title(species)
     xlabel(\'Temp\')
     ylabel(\'Enthalpy\')
     
     figure(3)
     %For T vs Entropy
     plot(T,S)
     title(species)
     xlabel(\'Temp\')
     ylabel(\'Entropy\')
     
     %Changing the directory 
     cd(\'C:\\Users\\Amogh\\Documents\')
     
     %Function for molecular_mass
     mm=molecular_mass(species)
     
     %Changing directory to save the graphs in respective species folder 
     filepath = ([\'C:\\Users\\Amogh\\Documents\\project1\',subfolder_name]);
     cd(filepath)
     saveas(1,\'T vs specific heat.jpg\')
     saveas(2,\'T vs enthalpy.jpg\')
     saveas(3,\'T vs entropy.jpg\')
     
     %Saving the molecular mass in .txt file 
     f2=fopen(\'Molecular mass.txt\',\'w\');
     fprintf(f2,\'%s \\n %s %f \\n\',species,\'Molecular mass\',mm);
     
     fclose(f2);
       %Changing the directory 
     cd(\'C:\\Program Files\\MATLAB\\MATLAB Production Server\\R2015a\\bin\')
     
 end
 fclose(f1)

Function for molecular mass:

function [mm]=molecular_mass(species);
%A program for calculating molecular mass of each species
atom=[\'H\',\'C\',\'N\',\'O\',\'AR\'];
%atomic weights
aw=[1,12,14,16,40];
%intial value for molecular mass
mm=0;

for i=1:4
    for j=1:4
        if (strcmp(species(i),atom(j))==1);
mm=mm+aw(j);
        end
        end
end      
    %Adding the weights of the repeating atoms
    for g = 1:length(species)
        
n=str2double(species(g));
if n>1
    p=(g-1);
mm=mm+aw(p)*(n-1);
end
end
%Atomic weight of argon
for l=1:length(species)
    if strcmp(species(l),\'R\')
        mm=mm+38.8;
    end 
end

Function for specific heat:

function Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,global_low_temp,global_mid_temp,global_high_temp);

if T<=1000
    Cp=(a1+a2.*T+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4)).*R;
else 
     Cp=(a8+a9.*T+a10.*(T.^2)+a11.*(T.^3)+a5.*(T.^4)).*R;
end

Function for enthalpy:

function H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,global_low_temp,global_mid_temp,global_high_temp)

if T<=1000
 H=(a1.*T+a2.*(T.^2)./2+a3.*(T.^3)./3+a4.*(T.^4)./4+a5.*(T.^5)./5+a6)*R;
 else 
 H=(a8.*T+a9.*(T.^2)./2+a10.*(T.^3)./3+a11.*(T.^4)./4+a12.*(T.^5)./5+a13)*R;     
end

Function for entropy:

function S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,global_low_temp,global_mid_temp,global_high_temp);
if T<=1000
     S=(a1.*log(T)+a2.*T+(a3.*T.^2)./2+(a4.*T.^3)./3+(a5.*T.^4)./4+a7).*R;
else
     S=(a8.*log(T)+a9.*T+(a10.*T.^2)./2+(a11.*T.^3)./3+(a12.*T.^4)./4+a14).*R; 
end

Errors faced:-

Since the directory of the saving the images and that of function was different therefore after images were being saved program had to be redirected to the original directory .

Fig.Before cd was used to change the directory

Fig.After cd was used at the end of program to change the directory

References:-

https://in.mathworks.com/help/matlab/ref/str2double.html

https://in.mathworks.com/help/matlab/ref/str2num.html

https://in.mathworks.com/help/matlab/ref/fgetl.html

https://in.mathworks.com/help/matlab/ref/mkdir.html

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.