Project

Project 1 - Parsing NASA thermodynamic data

Objective:

To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.

Procedure:

  • Open the given 'THERMO' file and scan the file thoroughly. First five lines are header lines.
  • In 2nd line, global high low and mid temperatures are given.
  • Open the file using fopen command and provide file handle. In this code I have used f1.
  • Now, starting with the header. Using fgetl(f1), we read the next lines.
  • Now the first line is not usable, just use fgetl to read and skip the line.
  • 2nd line contains global temp data. using fgetl read the line and using strfind command locate position of '.' . Now find the range. Atlast use strsplit command split the string into cells. Now convert the cells into numbers using str2num command.
  • Next 3 lines can be skipped using forloop and fgetl command.
  • Now on the 6th line, which I have referred as l, have information about species name and local high low mid temp.
  • here use for loop and give range to i as i=1:53. As there are 53 species.
  • Now 6th line I have assigned as l.
  • Use strsplit command to split string and convert into strings. Here assigned 'A' as the cell array. Now the first cell is species. There are total 53 species.
  • find '.' using strfind command and after converting cell arrays to numbers, assign low mid and high global temp tags.
  • l1 to l3 has information about temp coefficients. Now , first 7 are high temp coeffn and last 7 are low temp coeffn. Total 14.
  • From l1 to l3 use same logic. Use fgetl to call and assign line to l1. Use strfind to find '.' , and using str2num convert string to number.
  • After finding all the coefficients and assgning them as "a-high temp coeffn and b-low temp coeffn", calculate sp heat, enthalpy anf entropy using given equations. Using if else, we can find values of cp, H and S for low and high temp coefficients.

Equation:

  • Provide temperatur range from low tempto high temp. Using linspace we can split range into equal interval. temperature_range=linspace(local_LT,local_HT,30); Here 30 temperatures.
  • Now mid temp is given in the data and in the problem asked to use local temp data, we will use "local_MT" instead of global_MT in the if else loop.
  • Now write to equations for low temp (b) and high temp (a) coefficients and use local_MT as pivote point. If T<local_MT, use equation contains coeffieint as b and if T>local_MT use equation contains coeffieint as a.
  • Now create directory using mkdir command. We are creating directory because we will be storing graphs of cp, H and S into each species folder.
  • molecule_name=char(species),,,,,, mkdir(molecule_name). this command will create folders to store graphs. cd(molecule_name) and this command will change the directory every time command hit the for loop.
  • So each value of i, a new folder gets created by species name. There will be 53 folders.
  • using plot command, plot the graph for cp, H and S.
  • using saveas command save the graphs.
  • after storing graphs we need to go back to the recent directory where our program is stored.
  • use cd to come back to the original directory.
  • In the last use function to find the molecular weight of each species.
  • Now close the file using fclose command.
clc,close all,clear all;

f1=fopen('thermo.dat','r')

%header data.... h-header
h1=fgetl(f1);
h2=fgetl(f1);

temp=strfind(h2,'.');   %strfin means string find.... used to find certain characters. 
temperature=h2((temp-3):(temp+23));    
temp_range=strsplit(temperature,' ');   %strsplit means splitting the string.... using this we can break the string into cells

%now convert string cells tonumbers using str2num,

global_LT=str2num(temp_range{1});   %global low temp
global_MT=str2num(temp_range{2});   %global mid temp
global_HT=str2num(temp_range{3});   %global high temp

for s=1:3     %Next three header lines we wont use in calculation 
              %its ok if we use 1:3 because fgetl we have already used for first two lines 
    h=fgetl(f1);
end

%creating folder for different species,

for i=1:53       %there are have 53 species 
    
%Now preparing line data for species.... l refers to line 
l=fgetl(f1);

A=strsplit(l,' ');
species=A(1);         %here species stores species..... matrix having 53 species 

B=strfind(l,'.');
temperatures=l(B-3:B+23);
temp_range1=strsplit(temperatures,' ');
local_LT=str2num(temp_range1{1});   %local low temp
local_HT=str2num(temp_range1{2});   %local high temp
local_MT=str2num(temp_range1{3});   %local mid temp

%finding high temp coefficient
%first 7 are high tempcoeffn,

l1=fgetl(f1);
C=strfind(l1,'.');   %even 'E' can be used instead of '.' 
first_HT_coeficient=l1(1:C(1)+12);  % first 7 are high temp coefficients
a1=str2num(first_HT_coeficient);

second_HT_coeficient=l1(C(2)-2:C(2)+12);
a2=str2num(second_HT_coeficient);

third_HT_coeficient=l1(C(3)-2:C(3)+12);
a3= str2num(third_HT_coeficient);

fourth_HT_coeficient=l1(C(4)-2:C(4)+12);
a4=str2num(fourth_HT_coeficient);

fifth_HT_coeficient=l1(C(5)-2:C(5)+12);
a5=str2num(fifth_HT_coeficient);

l2=fgetl(f1);
D=strfind(l2,'.');

sixth_HT_coefficient=l2(D(1)-2:D(1)+12);
a6=str2num(sixth_HT_coefficient);

seventh_HT_coefficient=l2(D(2)-2:D(2)+12);
a7=str2num(seventh_HT_coefficient);

%finding low temp coefficient 
%last 7 are low tempcoeffn, 

first_LT_coefficient=l2(D(3)-2:D(3)+12);
b1=str2num(first_LT_coefficient);

second_LT_coefficient=l2(D(4)-2:D(4)+12);
b2=str2num(second_LT_coefficient);

third_LT_coefficient=l2(D(5)-2:D(5)+12);
b3=str2num(third_LT_coefficient);

l3=fgetl(f1);
E=strfind(l3,'.');
fourth_LT_coefficient=l3(E(1)-2:E(1)+12);
b4=str2num(fourth_LT_coefficient);

fifth_LT_coefficient=l3(E(2)-2:E(2)+12);
b5=str2num(fifth_LT_coefficient);

sixth_LT_coefficient=l3(E(3)-2:E(3)+12);
b6=str2num(sixth_LT_coefficient);

seventh_LT_coefficient=l3(E(4)-2:E(4)+12);
b7=str2num(seventh_LT_coefficient);



%calculating specific heat(cp), Enthalpy (H), Entropy(S) 
R=8.314;  %the gas constant.
temperature_range=linspace(local_LT,local_HT,30);
T=temperature_range;
k=1:length(T);
for j=1:length(k)
    if T(k)

Function to find molecular weight:

function [M]=molecular_mass(molecule_name);

fprintf('Molecular weight of species ');
fprintf(molecule_name);
fprintf(' = ');
atoms=['H','C','N','O','R'];
%R is for argon molecule, 

atom_weight=[1 12 14 16 40];
M=0;
for i=1:length(molecule_name);
  
        for j=1:length(atoms)
        if strcmpi(molecule_name(i),atoms(j));
            %strcmp means string comparision .it is used for checking that both elements are same or not. and by use strcmpi here,it take doesn't matter about elements are capital letter or small Example'h' =='H'means case insensitive condition.
            
           
            M=M+atom_weight(j);
           p=j;
           % here, we are storing the position of corrected match molecule characters to  the atoms
       
        end
        end
        n= str2num(molecule_name(i));
        %this all proces is doing for a number multiplier in molecule name.so the value of n will be come same as a number value present in that string name  
       
        if n > 1
            M=M+atom_weight(p)*(n-1);
            %Multiplied with (n-1) bacuase we are using this calculation
            %2nd time. If n=1, means there is one molecule sono need to
            %calculate again. 


        end

end
fprintf('%d',M);%d is for digit

disp(' ')

end

Explaination:

  • Here, molecular_mass is the function.
  • molecule_name is the name of the species. molecule_name=char(species);
  • using fprintf we will add some texts. using 2nd fprintf we will write name of the species.
  • we know the atoms which are in the species. So, atoms=['H','C','N','O','AR'] and atom_weight=[1 12 14 16 40].
  • then we use for loop. Value of i will change according to the length of the molecule name.
  • we will use one more for loop. here value of j will be from 1 to 5 as we have five atoms.
  • strcmpi means case insensitive string comparision. Here this command measures the length of string1 and string2 and the characters as well. It returns 1 if str1 and str2 are same without governing cases. upper case or lower case of character. S=s. case insensitive.
  • strcmpi(str1,str2) returns 1 if strings str1 and str2 are the same and 0 otherwise.
  • store value of j in p.
  • n= str2num(molecule_name(i)), converts string into number. If there is number it will output number. If no number then it ill output []. eg. In case of CH3OH, if i=2---- n=[],likewise for i=3-----n=3.
  • Now, if nis greater than 1, then code will execute M=M+atom_weight(p)*(n-1).
  • Multiplied by (n-1) because we have already used equation for that atom. If and only if there is more than one atom, code will go to the (n-1) equation.
  • fprintf('%d',M) will print 'M'. %d is for digit.
  • End.

These are the graphs of species O:

Folders of each species:

Molecular weights:

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.