Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: To write a code to extract Coefficients and temperature values from the given THERMO.dat file and use it to calculate Specific heat capacity, enthalpy and entropy. calculate molecular weight at the given species. Plot graphs and save them in particular directories using the code itself.

Formulae used for calculating Cp,H,S :

Cp = (a1 + a2*t + a3*t^2 + a4*t^3 + a5*t^4)*R;

H = (a1 + a2*t/2 + a3*t^2/3 + a4*t^3/4 + a5*t^4/5 + a6/t)*R*t;

S = (a1*log(t) + a2*t + a3*t^2/2 + a4*t^3/3 + a5*t^4/4 + a7)*R;

Syntax for some commands used:

  • fileID = fopen(filename,permission) opens the file with the type of access specified by permission.
  • A = fscanf(fileID,formatSpec) reads data from an open text file into column vector A and interprets values in the file according to the format specified by formatSpec.
  • tline = fgetl(fileID) returns the next line of the specified file, removing the newline characters.
  • C = strsplit(str,delimiter) splits str at the delimiters specified by delimiter.
  • X = str2double(str) converts the text in str to double precision values. str contains text that represents real or complex numeric values.
  • k = strfind(str,pat) searches str for occurrences of pat. The output, k, indicates the starting index of each occurrence of pat in str.
  • y = linspace(x1,x2,n) generates n points. The spacing between the points is (x2-x1)/(n-1).
  • cd newFolder changes the current folder to newFolder.
  • mkdir parentFolder folderName creates folderName in parentFolder. If parentFolder does not exist, MATLAB attempts to create it.
  • path(newpath) changes the search path to newpath.
  • saveas(fig,filename,formattype) creates the file using the specified file format, formattype.
  • tf = strcmp(s1,s2) compares s1 and s2 and returns 1 (true) if the two are identical and 0 (false) otherwise.

Matlab Code:

Main code:

%File Parsing
clear all
close all
clc
f = fopen('THERMO.dat','r');% opening the dat file and assihning it to a pointer
fgetl(f)
%Getting Global temperatute

gt = fscanf(f,'%f');
global_low_temp = gt(1);
global_mid_temp = gt(2);
global_high_temp= gt(3);
for i=1:3
      fgetl(f);%skipping 3 commented lines in the file
end
% Defining R value
R = 8.3145 %j/(mol-K)
 for  j=1:53
% Getting Species Name 
tline = fgetl(f);
A = strsplit(tline,' ');% to split the string at spaces
Species_Name = A{1};
%getting local temperatures 
local_low_temp = str2double(A{end-3});%to covert string to number
local_mid_temp = str2double(A{end-1});
local_high_temp= str2double(A{end-2});

% Getting coefficients in the first line by converting string to number
% fgetl(f);% exclude a line
tline = fgetl(f);
a  = strfind(tline,'E');% finds the letter E positions
a1 = str2double(tline(1:a(1)+3));
a2 = str2double(tline(a(1)+4:a(2)+3));
a3 = str2double(tline(a(2)+4:a(3)+3));
a4 = str2double(tline(a(3)+4:a(4)+3));
a5 = str2double(tline(a(4)+4:a(5)+3));

%Getting coefficients in the second line 
% fgetl(f)
tline = fgetl(f);
a  = findstr(tline,'E')
a6 = str2double(tline(1:a(1)+3));
a7 = str2double(tline(a(1)+4:a(2)+3));
a8 = str2double(tline(a(2)+4:a(3)+3));
a9 = str2double(tline(a(3)+4:a(4)+3));
a10= str2double(tline(a(4)+4:a(5)+3));

%Getting coefficients in the Third line
% fgetl(f)
tline = fgetl(f);
a  = findstr(tline,'E');
a11 = str2double(tline(1:a(1)+3));
a12 = str2double(tline(a(1)+4:a(2)+3));
a13 = str2double(tline(a(2)+4:a(3)+3));
a14 = str2double(tline(a(3)+4:a(4)+3));

%Creating folder for every species
  mkdir(['C:\Users\Gautham\Documents\MATLAB\fileparse\',Species_Name])     
% Defining Temperature range
 T = linspace(local_low_temp,local_high_temp,1000);
 % calling function calculation to get Cp,H,S values
 [Cp,H,S] = calculation(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_mid_temp);
 % calling function Molecular_weight to get M values
 M = Molecular_weight(Species_Name);
%  fprint('Molecular_Weight of %s ',Species_Name)
% fprint('%f',M)

% cd (Species_Name)
%plot for Temperature vs specific heat capacity 
figure(1)
plot(T,Cp,'linewidth',2,'color','g');
xlabel('Temperature(K)');
ylabel('Specific heat capacity_Cp(j/(kg-k))');
title('Temperature vs Specific heat capacity of %s',Species_Name);

%plot for Temperature vs Enthalpy
figure(2)
plot(T,H,'linewidth',2,'color','y');
xlabel('Temperature(K)');
ylabel('Enthalpy(j)');
title('Temperature vs Enthalpy of %s',Species_Name);
 
%plot for Temperature vs Entropy
figure(3)
plot(T,S,'linewidth',2,'color','b');
xlabel('Temperature(K)');
ylabel('Entropy(j/k)');
title('Temperature vs Entropy of %s',Species_Name);
 
%changing the directory

 cd(['C:\Users\Gautham\Documents\MATLAB\fileparse\',Species_Name]);%change directory
%creating file to save plots
  saveas(1,'T vs Cp.jpg');
  saveas(2,'T vs H.jpg');
  saveas(3,'T vs S.jpg');
%  cd(Species_Name(j));%changing path to be saved
 %saveas(figure(1),'plots.jpg');% save plots as jpg format

 %writing molecular wight to a file
 mole = fopen('Molecular_weight.txt','w')% opens a new file 
 fprintf(mole,'Molecular_Weight of %s is %d',Species_Name,M)% writes molecular weight to the 
 fclose(mole)%closes the file


  end
  
  

Sub code for calculating cp,H,S:

function [Cp,H,S] = calculation(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_mid_temp)
% T = linspace(local_low_temp(j),local_high_temp(j),50)
for i=1:length(T)
    t =T(i);
if t >= local_mid_temp
    Cp(i) = (a1 + a2*t + a3*t^2 + a4*t^3 + a5*t^4)*R;
    H(i) = (a1 + a2*t/2 + a3*t^2/3 + a4*t^3/4 + a5*t^4/5 + a6/t)*R*t;
    S(i) = (a1*log(t) + a2*t + a3*t^2/2 + a4*t^3/3 + a5*t^4/4 + a7)*R;
else
     Cp(i) = (a8 + a9*t + a10*t^2 + a11*t^3 + a12*t^4)*R;
      H(i) = (a8 + a9*t/2 + a10*t^2/3 + a11*t^3/4 + a12*t^4/5 + a13/t)*R*t;
      S(i) = (a8*log(t) + a9*t + a10*t^2/2 + a11*t^3/3 + a12*t^4/4 + a14)*R;

  

end
end
end

    

Sub code for calculating molecular weight:

function [M] = Molecular_weight(Species_Name)
 Elements = ['H','C','N','O','Ar'];
 Atomic_weight =[1,12,14,16,40];
 M=0;
   for i= 1:length(Species_Name);
     for  j = 1:length(Elements);
       tf = strcmp(Species_Name(i),Elements(j));% compares both the elements and and species returns  0 if same or 1 is Same
       if (tf==1)
           M = M+Atomic_weight(j);
           p = Atomic_weight(j);
       end
     end
       n = str2double(Species_Name(i));
if n>0
    M = M + (n-1)*p;
end
   end
fprintf('Molecular Weight of %s',Species_Name);
fprintf('%f',M);
display(' ');

 end          
 

Code Explanation:

  1. Start with getting the .dat file ,make sure that it is in the same location as code. Use command fopen(filename,permission).permission is whether we write or read the file.
  2. Skip the first line using fgetl command.We can get the global temperatures using scanf command and then assign to req variables.
  3. Then skipping 3 commented lines using for loop repeating fgetl for 3 times. Then for loop is started to continuously get values from every species.and it is defined to run 53 times as we have 53 species. Then we use fgetl command to get the data and assign it to the tline and it is slit using strsplit(tline,’ ’) to split at spaces.then it is assigned to the Species_Name ..so that we can use that data to get local temp values using ex: local_low_temp(j)= str2double(A{end-3}) str2double actually converts string to number
  4. Then coefficients need to be extracted. This is done with the help of strfind command it is used to find and return a letter positions.this positions are used to exactly extract 14 coefficients .str2double is used to convert string into number.
  5. The range of T is defined using the linspace command.
  6. Calculation function is called to get the values of Cp, H, S. In calculation sub function if else loop is used to use either lower temperature coefficients or higher temperature variables.for loop is used for inputting all 1000 t values.
  7. Molecular_weight function is called to get the M of all the species. In this sub function Atomic weight of atoms present in all the species are specified. strcomp is used to compare the atoms defined with the species if it returns 1 then molecular weight of each species is calculated. If there are 2 or more same atoms then str2double command is used to get the number and used to multiply it to the atomic weight.(ex: n = str2double(Species_Name(i));). Printf command is used to print the molecular weight of each species.
  8. cd command is used to change the directory, mkdir command is used to create a new files with each species name. the required plots are plotted, and saved in the directory of Species_Name for each of it.

Plots for the T vs Cp, T vs H, T vs S:

Screenshot of folders created:

Screenshot of Molecular weight displayed:

Results: The required data is extracted from the nasa file and calculations for Cp,H,S for each species are performed. Graphs ate plotted among Temperature and (Cp,H,S). And these graphs are saved into different files with the species name.

Error :

changed Species_Name = A(1); to Species_Name = A{1}; it is a cell so flower bracket gets whats inside the cell.

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.