Challenge

Project 1 - Parsing NASA thermodynamic data

Specific Heat Functional Code:-

function Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp)          
% Comparing with global temperature
% If less than global temperature - minimum coefficients are taken
if  Temp <= global_medium_temp
    Cp = R*(a8 + (a9*Temp) + (a10*(Temp).^2) + (a11*(Temp).^3) + (a12*(Temp).^4));
else
    Cp = R*(a1 + (a2*Temp) + (a3*(Temp).^2) + (a4*(Temp).^3) + (a5*(Temp).^4));
    % Else maximum coefficients are taken
end

Entropy Functional Code:

function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp)         
% Comparing with global temperature
% If less than global temperature - minimum coefficients are taken
if Temp <= global_medium_temp
    S = R.*(a8.*log(Temp) + (a9.*Temp) + (a10*((Temp).^2)/2) + (a11*((Temp).^3)/3) + (a12*((Temp).^4)/4) + a14);
else
    % Else - maximum coefficients are taken
    S = R.*(a1.*log(Temp) + (a2.*Temp) + (a3*((Temp).^2)/2) + (a4*((Temp).^3)/3) + (a5*((Temp).^4)/4) + a7);
end

Enthalpy Functional Code:

function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp)    
% Comparing with global temperature
% If less than global temperature - minimum coefficients are taken
if Temp <= global_medium_temp
    H = R*Temp.*(a8 + ((a9*Temp)/2) + ((a10*(Temp).^2)/3) + ((a11*(Temp).^3)/4) + ((a12*(Temp).^4)/5) +((a13./Temp)));
else
% Else maximum coefficients are taken
H = R*Temp.*(a1 + ((a2*Temp)/2) + ((a3*(Temp).^2)/3) + ((a4*(Temp).^3)/4) + ((a5*(Temp).^4)/5) +((a6./Temp)));
end

Molecular Weight Functional Code:

%function calculate molecular mass
function mw = mol_wt(sp_name)
%storing value
compound_name = (sp_name)
% creating an array of elements
key_at = ['C','H','O','N','A'];
%storing the atomic mss value in the array
value_at = [12.0107 1.00794 15.994 14.0067 39.948];

%asssigning molecular mass as 0
mw =0 ;
%creating for loop to perform the task
for i = 1:length(compound_name)
    for j = 1:length(key_at)
        %comparing the string value 
        if strcmp(compound_name(i),key_at(j))
            %if above comparision is true value of molecular mass is
            %calculated
            mw =mw + value_at(j);
            %storing the position for further use
            pos = j;
          
        end
    end
    %converting the srting to number
    k = str2num(compound_name(i));
    %comparing if the value of k is greater than executed the further statements 
    if(k>1)
        %molecular mass calculation
        mw = mw + value_at(pos)*(k-1);
    end
end
%command to print the molecular masss of the molecule
fprintf('Molecular Weight of the species %s is %f',compound_name ,mw)
end

Data Extraction Code or Main Code:

%File Parsing for a file
clc
clear
close all

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

%R is the universal gas constant J/(mol-K)

R = 8.314;
temp = fgetl(f1);

%Reading text from a header file
%Reading the global temperature from the next line

A = strsplit(temp,' ');

%Cell array is generated 1x4 cell array
glob_lowtemp = str2num(A{2});
glob_midtemp = str2num(A{3});
glob_hightemp =str2num(A{4});

%Temperature range
Temperature = linspace(glob_lowtemp,glob_hightemp,1000);
%skipping the three comment lines
for j =1:3;
    k =fgetl(f1);
end
%running for loop fopr 53 times as there are 53 species
for i = 1:53;

    %Parsing of the species block
        tline = fgetl(f1);
        S =strsplit(tline,' ');
        Species_name =S{1};

        %Extracting the coefficients from first line
        line2 =fgetl(f1);
        x =findstr(line2,'E');

        %Finding the 5 coefficient
        x1 = line2(1:x(1)+3);
        x1 = str2num(x1);

        x2 = line2(x(1)+4:x(2)+3);
        x2 = str2num(x2);

        x3 = line2(x(2)+4:x(3)+3);
        x3 = str2num(x3);

        x4 = line2(x(3)+4:x(4)+3);
        x4 = str2num(x4);

        x5 = line2(x(4)+4:x(5)+3);
        x5 = str2num(x5);

        %line3 5coefficients
        line3 = fgetl(f1);
        x = findstr(line3,'E');

        x6 = line3(1:x(1)+3);
        x6 = str2num(x6);

        x7 = line3(x(1)+4:x(2)+3);
        x7 = str2num(x7);

        x8 = line3(x(2)+4:x(3)+3);
        x8 = str2num(x8);

        x9 = line3(x(3)+4:x(4)+3);
        x9 = str2num(x9);

        x10 = line3(x(4)+4:x(5)+3);
        x10 = str2num(x10);

        %Line4  4coefficients

        line4=fgetl(f1);
        x = findstr(line4,'E');

        x11 = line4(1:x(1)+3);
        x11 = str2num(x11);

        x12 = line4(x(1)+4:x(2)+3);
        x12 = str2num(x12);

        x13 = line4(x(2)+4:x(3)+3);
        x13 = str2num(x13);

        x14 = line4(x(3)+4:x(4)+3);
        x14 = str2num(x14);


        % Entropy Calculation 

        S = Entropy(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,Temperature,glob_midtemp,R);

        %Cp calculation means Specific Heat calculation

        
        Cp = Specificheat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,Temperature,glob_midtemp,R);

        %Enthlpy Calculation is H

        H = Enthalpy(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,Temperature,glob_midtemp,R);

        %Molecular Weight calculation of different elements

        mw(i) = mol_wt(Species_name);

        %Reading and writinh of the molecular wieght in the list of molecular
        %wieght
        Moll_wt = fopen('Molecular Weight.txt','w');
        fprintf(Moll_wt,'Molecular Weight of the species %s is  %f ',Species_name,mw);
        fprintf(Moll_wt,'/n');
        fclose(Moll_wt);

        % Plotting on the graph for species fo the temperature and specific
        % heat

        %Creating a folder for saving the species with its properties

        directory_curr = pwd;
        mkdir(Species_name);
        cd(Species_name);

        %Plotting the graph in different plotting window 
        %plot 1 for the temperature and entropy
        %plot 2 for the temperature and enthalpy
        %plot 3 for the temperature and specific heat


        %plot 1 Enthalphy and Temperaure

        figure(1)
        plot(Temperature,H,'linewidth',3,'color','b');
        xlabel('Temperature in [K]');
        ylabel('Enthalphy [KJ/mol]');
        grid on
        title(sprintf('Variation in Enthalphy wrt Temperature %s',Species_name));

        saveas (figure(1),'Enthalhy.jpg');

         %plot 2 Entropy and Temperature

        figure(2)
        plot(Temperature,S,'linewidth',3,'color','r');
        xlabel('Temperature in [K]');
        ylabel('Entropy [KJ/mol-k]');
        grid on
        title(sprintf('Variation in Entropy wrt Temperature %s',Species_name));

        saveas (figure(2),'Entropy.jpg');

         %plot 3 Specific Heat and Temperature

        figure(3)
        plot(Temperature,Cp,'linewidth',3,'color','g');
        xlabel('Temperature in [K]');
        ylabel('Specific Heat [KJ]');
        grid on
        title(sprintf('Variation in Specific Heat wrt Temperature %s',Species_name));

        saveas (figure(3),'Specific Heat.jpg');

        cd(directory_curr);

end    

Results:

compound_name =

 

    'O'

 

Molecular Weight of the species O is 15.994000

compound_name =

 

    'O2'

 

Molecular Weight of the species O2 is 31.988000

compound_name =

 

    'H'

 

Molecular Weight of the species H is 1.007940

compound_name =

 

    'H2'

 

Molecular Weight of the species H2 is 2.015880

compound_name =

 

    'OH'

 

Molecular Weight of the species OH is 17.001940

compound_name =

 

    'H2O'

 

Molecular Weight of the species H2O is 18.009880

compound_name =

 

    'HO2'

 

Molecular Weight of the species HO2 is 32.995940

compound_name =

 

    'H2O2'

 

Molecular Weight of the species H2O2 is 34.003880

compound_name =

 

    'C'

 

Molecular Weight of the species C is 12.010700

compound_name =

 

    'CH'

 

Molecular Weight of the species CH is 13.018640

compound_name =

 

    'CH2'

 

Molecular Weight of the species CH2 is 14.026580

compound_name =

 

    'CH2(S)'

 

Molecular Weight of the species CH2(S) is 14.026580

compound_name =

 

    'CH3'

 

Molecular Weight of the species CH3 is 15.034520

compound_name =

 

    'CH4'

 

Molecular Weight of the species CH4 is 16.042460

compound_name =

 

    'CO'

 

Molecular Weight of the species CO is 28.004700

compound_name =

 

    'CO2'

 

Molecular Weight of the species CO2 is 43.998700

compound_name =

 

    'HCO'

 

Molecular Weight of the species HCO is 29.012640

compound_name =

 

    'CH2O'

 

Molecular Weight of the species CH2O is 30.020580

compound_name =

 

    'CH2OH'

 

Molecular Weight of the species CH2OH is 31.028520

compound_name =

 

    'CH3O'

 

Molecular Weight of the species CH3O is 31.028520

compound_name =

 

    'CH3OH'

 

Molecular Weight of the species CH3OH is 32.036460

compound_name =

 

    'C2H'

 

Molecular Weight of the species C2H is 25.029340

compound_name =

 

    'C2H2'

 

Molecular Weight of the species C2H2 is 26.037280

compound_name =

 

    'C2H3'

 

Molecular Weight of the species C2H3 is 27.045220

compound_name =

 

    'C2H4'

 

Molecular Weight of the species C2H4 is 28.053160

compound_name =

 

    'C2H5'

 

Molecular Weight of the species C2H5 is 29.061100

compound_name =

 

    'C2H6'

 

Molecular Weight of the species C2H6 is 30.069040

compound_name =

 

    'CH2CO'

 

Molecular Weight of the species CH2CO is 42.031280

compound_name =

 

    'HCCO'

 

Molecular Weight of the species HCCO is 41.023340

compound_name =

 

    'HCCOH'

 

Molecular Weight of the species HCCOH is 42.031280

compound_name =

 

    'H2CN'

 

Molecular Weight of the species H2CN is 28.033280

compound_name =

 

    'HCN'

 

Molecular Weight of the species HCN is 27.025340

compound_name =

 

    'HNO'

 

Molecular Weight of the species HNO is 31.008640

compound_name =

 

    'N'

 

Molecular Weight of the species N is 14.006700

compound_name =

 

    'NNH'

 

Molecular Weight of the species NNH is 29.021340

compound_name =

 

    'N2O'

 

Molecular Weight of the species N2O is 44.007400

compound_name =

 

    'NH'

 

Molecular Weight of the species NH is 15.014640

compound_name =

 

    'NH2'

 

Molecular Weight of the species NH2 is 16.022580

compound_name =

 

    'NH3'

 

Molecular Weight of the species NH3 is 17.030520

compound_name =

 

    'NO'

 

Molecular Weight of the species NO is 30.000700

compound_name =

 

    'NO2'

 

Molecular Weight of the species NO2 is 45.994700

compound_name =

 

    'HCNO'

 

Molecular Weight of the species HCNO is 43.019340

compound_name =

 

    'HOCN'

 

Molecular Weight of the species HOCN is 43.019340

compound_name =

 

    'HNCO'

 

Molecular Weight of the species HNCO is 43.019340

compound_name =

 

    'NCO'

 

Molecular Weight of the species NCO is 42.011400

compound_name =

 

    'CN'

 

Molecular Weight of the species CN is 26.017400

compound_name =

 

    'HCNN'

 

Molecular Weight of the species HCNN is 41.032040

compound_name =

 

    'N2'

 

Molecular Weight of the species N2 is 28.013400

compound_name =

 

    'AR'

 

Molecular Weight of the species AR is 39.948000

compound_name =

 

    'C3H8'

 

Molecular Weight of the species C3H8 is 44.095620

compound_name =

 

    'C3H7'

 

Molecular Weight of the species C3H7 is 43.087680

compound_name =

 

    'CH3CHO'

 

Molecular Weight of the species CH3CHO is 44.047160

compound_name =

 

    'CH2CHO'

 

Molecular Weight of the species CH2CHO is 43.039220

Screenshots of saved filed

google drive link:

https://drive.google.com/drive/folders/1pIZIZ_cQdbzxPINr4rI9ZRzWe0VXuuj8?usp=drive_link

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.