Challenge

Project 1 - Parsing NASA thermodynamic data

Main Program:

% START OF THE MAIN PROGRAM

clear all
close all
clc

R=8.314

% OPENING DATA FILE

f1= fopen('THERMO.dat','r')
fgetl(f1)

% GLOBAL TEMPERATURE DATA

global_temp_string=fgetl(f1)
global_temp=strsplit(global_temp_string,' ')
global_low_temp=str2double(global_temp{2});
global_mid_temp=str2double(global_temp{3})
global_high_temp=str2double(global_temp{4})
clear global_temp_string
clear global_temp

% SPECIES CALCULATION

frewind(f1)
string=fgetl(f1)

i = 1
while strcmp(string,'END')==0
    string = fgetl(f1)
    i = i + 1
end

clear string
total_species = (i-6)/4
frewind(f1)
for i = 1:5
    fgetl(f1)
end

clear i

% EXTRACTING SPECIES NAME, LOCAL TEMPERATURES AND COEFFICIENT

species_data=species_data_collect(f1,total_species)

% CALCULATION OF SPECIFIC HEAT

species_data=specific_heat_calc(species_data,R)

% CALCULATION OF ENTHALPY

species_data=enthaply_calc(species_data,R)

% CALCULATION OF ENTROPY

species_data=entropy_calc(species_data,R)

% CALCULATION OF MOLECULAR WEIGHT

species_data=molecular_weight(species_data)

% PLOTTING CP VS T, H VS T AND S VS T

plot_func(species_data)

% DISPLAYING MOLECULAR WEIGHTS

for i = 1:total_species
    disp(['Molecular Weight of ',species_data(i).Name,'is',num2str(species_data(i).Molec_Wt)])
end
fclose(f1);

% ASKING USER TO SHOW PLOTS OF ANY ELEMENT OR NOT

element_plot_showcase(species_data)

Element Plot Showcase Function:

% FUNCTION TO VIEW PLOTS OF ANY ELEMENT

function [] = element_plot_showcase(maindata)

% GETTING RESPONSE FROM THE USER

choice = questdlg('Do you want to view H-T, S-T,C-T plot of any element?','User Response','Yes','No','No')
ctrl = 0

while ctrl == 0
    
    switch choice
       
        case 'Yes'
           
            h = msgbox({'The elements present in the database are as follows:'...
                'O, O2, H, H2, OH, H2O, HO2, H2O2, C, CH, CH2, CH2(S)'...
                'CH3, CH4, CO, CO2, HCO, CH2O, CH2OH, H3O, CH3OH, C2H'...
                'C2H2, C2H3, C2H4, C2H5, C2H6, CH2CO, HCCO, HCCOH, H2CN'...
                'HCN, HNO, N, NNH, N2O, NH, NH2, NH3, NO, NO2, HCNO, HOCN'...
                'HNCO, NCO, CN, HCNN, N2, AR, C3H8, C3H7, CH3CHO,CH2CHO'})
            
            user = inputdlg('Enter the element name: ')
            
            delete(h)
            
            pos = 0
            
            for n = 1:length(maindata)
                temp_name = maindata(n).Name
                if strcmpi(temp_name,user)==1
                    pos = n
                    break
                end
            end
            
            if pos ~= 0
                path = 'F:Skill Lync Courses Matlab Fileparsing Results'
                filename1 = fullfile(path,temp_name,'Enthalpy_vs_Temperature_Plot.jpg')
                image1 = imread(filename1)
                figure(1)
                imshow(image1)
                filename2 = fullfile(path,temp_name,'Entropy_vs_Temperature_Plot.jpg')
                image2 = imread(filename2)
                figure(2)
                imshow(image2)
                filename3 = fullfile(path,temp_name,'Specific_Heat_vs_Temperature_Plot.jpg')
                image3 = imread(filename3)
                figure(3)
                imshow(image3)
                choice = questdlg('Do you want to view H-T, S-T, C-T plot of any other element?','User Response','Yes','No','No')
            else
                uiwait(msgbox('Please enter a valid element name...!'))
            end
        
        case 'No'
            ctrl = 1
    end
end
end

Plot Function:

% FUNCTION TO PLOT CP VS T, H VS T AND S VS T.

function [] = plot_func(maindata)

struct_size = length(maindata)

for i = 1:struct_size
    name = maindata(i).Name
    mkdir('Results',name);
   
    % SEPECIFIC HEAT VS TEMPERATURES
    
    figure(1)
    plot(maindata(i).Temp_Values,maindata(i).Sp_Heat_Values,'color','r','linewidth',3)
    xlabel('TEMPERATURE')
    ylabel('SPECIFIC HEAT')
    title(sprintf('SPECIFIC HEAT v/s TEMPERATURE Plot for %s', name))
    
    % ENTHALPY VS TEMPERATURE
    
    figure(2)
    plot(maindata(i).Temp_Values,maindata(i).Enthalpy_Values,'color','m','linewidth',3)
    xlabel('TEMPERATURE')
    ylabel('ENTHALPY')
    title(sprintf('ENTHALPY v/s TEMPERATURE Plot for %s',name))
    
    % ENTROPY VS TEMPERATURE
    
    figure(3)
    plot(maindata(i).Temp_Values,maindata(i).Entropy_Values,'color','b','linewidth',3)
    xlabel('TEMPERATURE')
    ylabel('ENTROPY')
    title(sprintf('ENTROPY v/s TEMPERATURE Plot for %s',name))
    
    path = 'F:Skill Lync Courses MATLab FileParsing Results'
    
    plot_1 = fullfile(path,name,'Specific_Heat_vs_Temperature_Plot.jpg')
    plot_2 = fullfile(path,name,'Enthalpy_vs_Temperature_Plot.jpg')
    plot_3 = fullfile(path,name,'Entropy_vs_Temperature_Plot.jpg')
    
    saveas(figure(1),plot_1)
    saveas(figure(2),plot_2)
    saveas(figure(3),plot_3)
end
end

Molecular Weight Function:

% MOLECULAR WEIGHT FUNCTION

function maindata = molecular_weight(maindata)

% CREATING CHARACTER VECTOR

std_element = {'C','H','O','N','A','S'}

% CREATING ATOMIC WEIGHT VECTOR

atomic_wt = [12,1,16,14,40,32]
struct_size = length(maindata)

for i = 1:struct_size
    name = maindata(i).Name
    mol_wt = 0
    
    for j = 1:length(name)
       
        for k = 1:length(std_element)
            
            if strcmp(name(j),std_element(k))==1
                mol_wt = mol_wt + atomic_wt(k)
                pos = k
                break
            end
        end
        
        % CONVERSION OF NUMBER(ELEMENT NAME) TO NUMBER
        
        num = str2double(name(j))
        
        if num>1
            mol_wt = mol_wt + (atomic_wt(pos).*(num-1))
        end 
    end
   
    % SAVING THE MOLECULAR WEIGHT VALUES IN MAINDATA
    
    maindata(i).Molec_Wt = mol_wt
end
end

Entropy Function:

% ENTROPY FUNCTION

function maindata = entropy_calc(maindata,R)

struct_size = length(maindata)

for i = 1:struct_size
    lmt = maindata(i).Local_Temp(3)
    T = maindata(i).Temp_Values
    a = maindata(i).Coeff
    
    for k = 1:length(T)
        
        if T(k)>= lmt
           
            % HERE S WILL BE CALCULATED WITH HIGH TEMPERATURE
           
            S(k) = (a(1)*log(T(k)) + a(2)*T(k) + (a(3)*T(k)^2)/2 + (a(4)*T(k)^3)/3 + (a(5)*T(k)^4)/4 + a(7))*R
        else
            
            % HERE S WILL BE CALCULATED WITH LOW TEMPERATURE
            
            S(k) = (a(8)*log(T(k)) + a(9)*T(k) + (a(10)*T(k)^2)/2 + (a(11)*T(k)^3)/3 + (a(12)*T(k)^4)/4 + a(14))*R
        end
    end
    
    % SAVING ENTROPY VALUES IN MAINDATA
   
    maindata(i).Entropy_Values = S
end
end

Enthalpy Function:

% ENTHALPY FUNCTION

function maindata = enthalpy_calc(maindata,R)

struct_size = length(maindata)

for i = 1:struct_size
    lmt = maindata(i).Local_Temp(3)
    T = maindata(i).Temp_Values
    a = maindata(i).Coeff
    
    for k = 1:length(T)
        
        if T(k)>= lmt
           
            % HERE H WILL BE CALCULATED WITH HIGH TEMPERATURE
            
            H(k) = (a(1) + (a(2)*T(k))/2 + (a(3)*T(k)^2)/3 + (a(4)*T(k)^3)/4 + (a(5)*T(k)^4)/5 + a(6)/T(k))*R*T(k)
        else
            
            % HERE H WILL BE CALCULATED WITH LOW TEMPERATURE
           
            H(k) = (a(8) + (a(9)*T(k))/2 + (a(10)*T(k)^2)/3 + (a(11)*T(k)^3)/4 + (a(12)*T(k)^4)/5 + a(13)/T(k))*R*T(k)
        end
    end
    
    % SAVING ENTHAPLY VALUES IN MAINDATA
   
    maindata(i).Enthalpy_Values = H
end
end

Specific Heat Function:

% SPECIFIC HEAT FUNCTION

function maindata = specific_heat_calc(maindata,R)

struct_size = length(maindata)

for i = 1:struct_size
    llt = maindata(i).Local_Temp(1)
    lht = maindata(i).Local_Temp(2)
    lmt = maindata(i).Local_Temp(3)
    T = linspace(llt,lht,500)
    a = maindata(i).Coeff
   
    for k = 1:length(T)
        
       if T(k)>= lmt
           
           % HERE CP WILL BE CALCULATED WITH HIGH TEMPERATURE
           
            Cp(k) = (a(1) + a(2)*T(k) + a(3)*T(k)^2 + a(4)*T(k)^3 + a(5)*T(k)^4)*R
        else
           
            % HERE CP WILL BE CALCULATED WITH LOW TEMPERATURE
           
            Cp(k) = (a(8) + a(9)*T(k) + a(10)*T(k)^2 + a(11)*T(k)^3 + a(12)*T(k)^4)*R
        end 
    end
    
    % SAVING THE TEMPERATURE AND CP VALUES OF SPECIES IN MAINDATA
    
    maindata(i).Temp_Values = T
    maindata(i).Sp_Heat_Values = Cp
end
end

Species Data Collect Function:

% SPECIES DATA COLLECT FUNCTION

function maindata = species_data_collect(f1,species_count)

for i = 1:species_count
    string = fgetl(f1)
    name = strtok(string)
    temp_pos = strfind(string,'.')
    
    % GETTING THE LOCAL TEMPERATURES
    local_low_temp = str2double(string(temp_pos(1)-3:temp_pos(1)+3))
    local_high_temp = str2double(string(temp_pos(2)-4:temp_pos(2)+3))
    local_mid_temp = str2double(string(temp_pos(3)-4:temp_pos(3)+3))
    
    % SAVING LOCAL TEMPERATURE AND NAME OF SPECIES INTO THE MAINDATA
    
    maindata(i),Name = name
    maindata(i).Local_Temp = [local_low_temp,local_high_temp,local_mid_temp]
    
    % GETTING COEFFICIENTS FROM LINE ONE
    
    line1 = fgetl(f1)
    E_pos1 = strfind(line1,'E')
    a1 = str2double(line1(1:E_pos1(1)+3))
    a2 = str2double(line1(E_pos1(1)+4:E_pos1(2)+3))
    a3 = str2double(line1(E_pos1(2)+4:E_pos1(3)+3))
    a4 = str2double(line1(E_pos1(3)+4:E_pos1(4)+3))
    a5 = str2double(line1(E_pos1(4)+4:E_pos1(5)+3))
    
    % GETTING COEFFICIENTS FROM LINE TWO
    
    line2 = fgetl(f1)
    E_pos2 = strfind(line2,'E')
    a6 = str2double(line2(1:E_pos2(1)+3))
    a7 = str2double(line2(E_pos2(1)+4:E_pos2(2)+3))
    b1 = str2double(line2(E_pos2(2)+4:E_pos2(3)+3))
    b2 = str2double(line2(E_pos2(3)+4:E_pos2(4)+3))
    b3 = str2double(line2(E_pos2(4)+4:E_pos2(5)+3))
    
    % GETTING COEFFICIENTS FROM LINE THREE
    
    line3 = fgetl(f1)
    E_pos3 = strfind(line3,'E')
    b4 = str2double(line3(1:E_pos3(1)+3))
    b5 = str2double(line3(E_pos3(1)+4:E_pos3(2)+3))
    b6 = str2double(line3(E_pos3(2)+4:E_pos3(3)+3))
    b7 = str2double(line3(E_pos3(3)+4:E_pos3(4)+3))

    % SAVING THE COEFFICIENTS IN MAINDATA

    maindata(i).Coeff = [a1 a2 a3 a4 a5 a6 a7 b1 b2 b3 b4 b5 b6 b7]
end

* Parsing:

Parsing means reading a file in a data stream of some sort and building an memory model of the sematic content of that data, otherwise, parsing refers to how information can be read from particular text file or any type of file and how information can be used in a program.

* Specif Heat:

Specific Heat is the amount of heat to be supplied to the unit mass of a system in order to increase its temperature by 1 degree in thermodynamic process in which the quantity is imposed.

* Enthalpy:

It is a thermodynamic quantity equal to the total heat content of a system, it is equal to the internal energy of the system and product of pressure and volume.

* Entropy:

Entropy is the measure of systems thermal energy per unit temperature that is unavailable for doing useful work, because work is obtained in ordered molecular motion, the amount of entropy is also a measure of the molecular disorder of a system.

Explanation:

* Step 1: First step is to write the main program by starting it with 'clear all', 'close all' and 'clc' commands.

* Step 2: Give input 'universal gas constant', inputs for getting global temperature data and the inputs for species calculation.

* Step 3: Give input for extracting species name, local temperatures and coefficients.

* Step 4: Give the inputs for specific heat calculation.

Step 5: Give the inputs fot enthalpy calculation.

* Step 6: Give the inputs for entropy calculation.

* Step 7: Give the inputs for molecular weight calculation.

* Step 8: Give the inputs for plotting the graphs between ( CP vs T, H vs T and S vs T).

* Step 9: Give input to ask the user whether to show plots of any other element.

* Step 10: Write a function for viewing the plots of any element and save it.

* Step 11: Write a function to plot (CP vs T, H vs T and S vs T) graphs and save it.

* Step 12: Write a function to calculate molecular weight of each element save it.

* Step 13: Write a function to calculate entropy and save it.

* Step 14: Write a function to calculate enthalpy and save it.

* Step 15: Write a function to calculate specific heat and save it.

* Step 16: Write a function to get local temperature, name and coefficients of each species and save it.

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.