Challenge

Project 1 - Parsing NASA thermodynamic data

AIM-To read the 'THERMO.dat' file and extract the data required.

EQUATIONS USED-To calculate Specific Heat,Enthalpy and Entropy of different species,using the formulae provided above.If the temperature is above the mid value of local temperture for each species, the first 7-high temperature coefficients (a1-a7) are used. If the temperature is below the mid value of local temperature for each species, the next 7 low temperature coefficients(a8-a14) are used.

WORKFLOW/STEPWISE EXPLANATION-We first read the given file 'THERMO.dat' in read('r') mode.We notice that the first 5 lines in the file are header information.These are extracted and stored in variables using the fgetl() function.The second line of the header information stored in variable 'l2' ,contains the global temperatures,which need to be extracted.This is done by splitting the line at the space ' ' using strsplit() function.Using this,the global low,mid and high temperatures are obtained.

In the 'THERMO.dat' file it is observed that in total,there are 53 species. The first line of each species contains the species name,followed by some spaces and characters, further followed by the local temperature ranges. The next three lines of data represent the 14 coefficients of the species.The first line contains 5(a1-a5),the second line the next 5(a6-a10) and the third line the remaining 4(a11-a14) coefficients.This is repeated for all 53 species in the file. In order to extract this data for each species,we run a for loop running from 1 to 53,1 iteration for each of the species.fgetl() by default, automatically extracts the next line from the file,which is the line containing the species name and local temperatures. We use the strsplit() function to split the line at the spaces ' '. This is stored in a cell array, whose first element is the species name.From the same line,we use strfind() to get the position of the character 'G',as seen in the text file. Using this position of 'G',we can extract the local low,mid and high temperatures by simply using their positions in the character array.For example,the local low temperature of the species begins at the position of 'G'+3, and ends at the position of 55.This is done for all 3 temperatures,and the values extracted are converted to double datatype (number) using the str2double() function.

The next task is to extract the 14 temperature coefficients of the species.The next line containing the first 5 coefficients is extracted using fgetl() command.We then again use strfind() to find the position of the letter 'E' in the extracted text line. This is used to get the coefficients.The first coefficient 'a1'  occupies positions from 1 to 1st occurrence of 'E',+3. The second coefficient 'a2' starts from the next position after the end of the first coefficient,ie,p2(1)+4.It continues till the second occurrence of 'E',+3.This pattern is used to extract all the 5 coefficients in the first line of text.Similar procedure is done for the second and third line of text,to get all the 14 coefficients.According to the question,the graphs are to be plotted for temperature points ranging from the low to high temperature.For this,the required points are generated using the linspace() command and stored in T.

The next part of the problem is the calculation of different quantities like specific heat,enthalpy and entropy for each species.For each computation a specific function is written and called.The required constants (a1-a14) temperature ranges (local low ,mid and high temperatures) and gas constant R are passed to each of these functions.

Functions Called- specific_heat() to compute specific heat,entropy() to calculate entropy and enthalpy() to calculate enthalpy of the given species.In each of these functions,first a check is performed using the 'if' statement.If the temperature array passed to the function,has values which are greater than the local mid temperature of the species,then the first 7 coefficients,i.e,a1-a7 (high temperature coefficients) will be used for calculation. If not, then the second set of coefficients,i.e,a8-a14(low temperature coefficients) will be used for calculation.The required values are computed using the given formula. Arithmetic operations and element wise operator ('.') is used for this because temperature 'T' is an array and each element of it has to be multiplied in the formula.The computed values are stored in appropriate variables and returned to the main function.

After this,plotting of the results is done. Temperature is plotted against specific heat(cp),enthalpy(h) and entropy(s).3 separate figures are called for this.The next part of the problem involves saving the three figures which have been plotted in a folder and naming the folder with the corresponding species name.For this the command mkdir() is used.It is a function used to create a directory or a folder.The name of the folder is the species name, which is passed to the mkdir() function.We then use the 'cd' command,or 'change directory' command to change the control to the current directory of the particular species.The figures are saved using the saveas() function.The control is transferred back to the original folder using the cd() command.

The next part involves calculating the molecular weight of each species,for which another function mol_weight() is coded and called. The weight of each species is then written into a file named 'Molecular Weight' using fprintf(),after opening the file in write('w') mode.In the molecular weight function,first the atoms of the species and their molecular weights are hard coded into the  program and stored as separate arrays.We then run a loop through the length of each species,and perform a character wise comparison of the species,with each of the elements.If a match is obtained,then the corresponding weight is added.If the character is a number,then it is converted to double datatype using str2double() and then multiplied with the corresponding element weight after subtracting 1(because it was already added once in the previous check).The final weight of the species is returned back to the main function and is written into the text file.

MAIN PROGRAM-

clear 
close all 
clc 

%% Program to read NASA Thermodynamic Data File,and extract the required information 

%Reading the file 
f1=fopen('THERMO.dat','r');
%R Value-Universal Gas Constant
R=8.314;
%Extracting first 5 lines of the file(Header Information)
l1=fgetl(f1);
l2=fgetl(f1);
l3=fgetl(f1);
l4=fgetl(f1);
l5=fgetl(f1);
%Extracting the Global Temperatures
global_temp=strsplit(l2,' ');
global_low_temp=str2double(global_temp{2});
global_mid_temp=str2double(global_temp{3});
global_high_temp=str2double(global_temp{4});

for i=1:53
    line=fgetl(f1);
    x=strsplit(line,' ');
    sp=x(1);
    p=strfind(line,'G');
    lt=line(p+3:55);
    ht=line(p+13:65);
    mt=line(p+23:75);
    local_low_temp=str2double(lt);
    local_mid_temp=str2double(mt);
    local_high_temp=str2double(ht);
%Extracting Coefficients
%Line1 of coefficients contains 5 coefficients
    coeffl1=fgetl(f1);
    p2=strfind(coeffl1,'E');
    a1=str2double(coeffl1(1:p2(1)+3));
    a2=str2double(coeffl1(p2(1)+4:p2(2)+3));
    a3=str2double(coeffl1(p2(2)+4:p2(3)+3));
    a4=str2double(coeffl1(p2(3)+4:p2(4)+3));
    a5=str2double(coeffl1(p2(4)+4:p2(5)+3));
%Line2 contains the next 5 coefficients 
    coeffl2=fgetl(f1);
    p3=strfind(coeffl1,'E');
    a6=str2double(coeffl2(1:p3(1)+3));
    a7=str2double(coeffl2(p3(1)+4:p3(2)+3));
    a8=str2double(coeffl2(p3(2)+4:p3(3)+3));
    a9=str2double(coeffl2(p3(3)+4:p3(4)+3));
    a10=str2double(coeffl2(p3(4)+4:p3(5)+3));
%Line3 contains the next 4 coefficients 
    coeffl3=fgetl(f1);
    p4=strfind(coeffl1,'E');
    a11=str2double(coeffl3(1:p4(1)+3));
    a12=str2double(coeffl3(p4(1)+4:p4(2)+3));
    a13=str2double(coeffl3(p4(2)+4:p4(3)+3));
    a14=str2double(coeffl3(p4(3)+4:p4(4)+3));
%Calling functions to calculate specific heat,entropy and enthalpy
    %Temperature Array
    T=linspace(local_low_temp,local_high_temp,500);
    %Specific Heat Calculation
    cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_low_temp,local_mid_temp,local_high_temp);
    %Entropy Calculation
    s=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_low_temp,local_mid_temp,local_high_temp);
    %Enthalpy Calculation 
    h=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_low_temp,local_mid_temp,local_high_temp);
  %Plotting the results obtained
    %Cp v/s Temperature 
    figure(1)
    plot(T,cp,'linewidth',2.5,'color','r');
    xlabel('Temperature [K]');
    ylabel('Specific Heat [J/K-mol]');
    title('Specific Heat v/s Temperature');
    %Entropy v/s Temperature 
    figure(2)
    plot(T,s,'linewidth',2.5,'color','b');
    xlabel('Temperature [K]');
    ylabel('Entropy [J/K-mol]');
    title('Entropy v/s Temperature');
    %Enthalpy v/s Temperature 
    figure(3)
    plot(T,h,'linewidth',2.5,'color','g');
    xlabel('Temperature [K]');
    ylabel('Enthalpy [J/mol]');
    title('Enthalpy v/s Temperature');
    %Creating Folders
    species=char(sp);
    mkdir(species);
    cd(species);
    %Saving the plots
    saveas(figure(1),'CpvsTemp.jpg');
    saveas(figure(2),'EntropyvsTemp.jpg');
    saveas(figure(3),'EnthalpyvsTemp.jpg');
    cd('C:\Users\Arjun\Desktop\MATLAB-Skill LyncCourse');
    M=mol_weight(species);
    f2=fopen('Molecular Weights','a');
    fprintf(f2,'%s = %d\n',species,M);
    fclose(f2);
end    
fclose(f1);

SPECIFIC HEAT FUNCTION-

function cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_low_temp,local_mid_temp,local_high_temp)

if T>=local_mid_temp
    t1=a1+(a2.*T)+(a3.*(T.^2))+(a4.*(T.^3))+(a5.*(T.^4));
    cp=t1*R;
else
    t1=a8+(a9.*T)+(a10.*(T.^2))+(a11.*(T.^3))+(a12.*(T.^4));
    cp=t1*R;
end

end

ENTHALPY FUNCTION-

function h=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_low_temp,local_mid_temp,local_high_temp)

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

end

ENTROPY FUNCTION-

function s=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_low_temp,local_mid_temp,local_high_temp)

if T>=local_mid_temp
    t1=a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7;    
    s=t1*R;
else
    t1=a8.*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14;
    s=t1*R;
end

end

MOLECULAR WEIGHT FUNCTION-

function M=mol_weight(species)
atoms=['H','C','N','O','AR'];
weights=[1,12,14,16,40];    
M=0;
for i=1:length(species)
     for j=1:length(weights) 
        x=strcmp(species(i),atoms(j));
        if x==1
            M=M+weights(j);
            pos=j;
        end
     end
    n=str2double(species(i));
    if n>1
        M=M+(n-1)*weights(pos);
    end
end
end



     
 

OUTPUT/RESULTS-

Folder-

O2 Plots-

N2 Plots-

CO2 Plots-

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.