Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing NASA thermodynamic data

Aim

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.

Calculate the molecular weight of each species and display it in the command window. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.

Save the plots as images with appropriate names and dump them in separate folders for reach species with suitable name. All these should be generated automatically

Nasa formulae

Code

Specific heat function

Temperature array is compared with mid temperature of each species and accoeding to the following if statements the specific heat is calculated

Code

Specific heat function

Temperature array is compared with mid temperature of each species and accoeding to the following if statements the specific heat is calculated

%specific heat

function Cp = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temperature,R,mid_local_temp)
if temperature <= mid_local_temp
    Cp = R.*(a8 + (a9.*temperature) + (a10.*(temperature.^2)) + (a11.*(temperature).^3) + (a12.*(temperature).^4));
else
    Cp = R.*(a1 + (a2.*temperature) + (a3.*(temperature.^2)) + (a4.*(temperature).^3) + (a5.*(temperature).^4));
end

entropy function

Temperature array is compared with mid temperature of each species and according to the following if statements the entropy is calculated

%entropy

function S = entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temperature,R,mid_local_temp)
if temperature <= mid_local_temp
    S = R.*(a8.*log(temperature) + (a9.*temperature) + (a10.*(temperature.^2)) + (a11.*(temperature).^3) + (a12.*(temperature).^4) + a14);
else
    S = R.*(a1.*log(temperature) + (a2.*temperature) + (a3.*(temperature.^2)) + (a4.*(temperature).^3) + (a5.*(temperature).^4) + a7);
end

enthalpy function

Temperature array is compared with mid temperature of each species and according to the following if statements the enthalpy is calculated

%enthalpy

function H = enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temperature,R,mid_local_temp)
if temperature <= mid_local_temp
    H = R*temperature.*(a8 + ((a9*temperature)/2) + ((a10.*(temperature.^2))/3) + ((a11.*(temperature).^3)/4) + ((a12.*(temperature).^4)/5) + (a13./temperature));
else
    H = R.*(a1 + (a2.*temperature)/2 + (a3.*(temperature.^2))/3 + (a4.*(temperature).^3)/4 + (a5.*(temperature).^4)/5 + (a6./temperature));
end

molecular weight function

sscanf function is used to scan species name and the strings are stroed in p.

p is then compared with elements of atms using a for loop.

If there is a match then sscanf is used to check if there is a number after the letter In the name.

If yes then molecular weight is calculated according to the following formulas

Issue – There is no syntax error but the value of the molecular weight is not correct. After having tried all I could I have submitted the final code.

molecular weight

function mol_wt = molecular_weight(species)
%atoms used in the document
atoms = ['H','C','O','N','AR','S'];
atomic_wt = [1.00784,12.0096,15.99903,14.00643,39.948,32.059];
mol_wt = zeros(53,1);

for i = 1:length(species)
    p = sscanf(species(i), '%s' , 1);

    for j = 1:length(atoms)
        if (p == atoms(j)) 
            p = sscanf(species(i),'%s%d');
            if isempty(p)
               mol_wt (i) = mol_wt(i) + atomic_wt(j);
            else
               mol_wt (i) = mol_wt (i) + atomic_wt(j)*p;
            end
        end
    end
      
  

    
                    
    

        
         
   
         
end
end

Main code

Fopen is used to open the file

Fgetl is used to read the file line by line.

Species and coefficients are read using for loop in conjunction with fgetl and strsplt

Calling above functions to calculate values for specific heat, entropy and enthalpy.

Cd is used to change directories

Mkdir is used to make folders for each species name

All variables are plotted and stored in the created folder.

This process is repeated using a for loop for total no of species.

Molecular weight is printed in the command window using fprintf and forloop.

%parsing nasa thermodynamic data

clear all;
close all;
clc;

%reading data file
f1 = fopen('THERMO.dat','r');

%reading title of the file
headline = fgetl(f1);

%getting global temperature
global_temp = fgetl(f1);
s = strsplit(global_temp,' ');

%converting strings to numbers
global_low_temp = str2num(s{2});
global_mid_temp = str2num(s{3});
global_high_temp = str2num(s{4});



%universal gas constant j/molK
R = 8.3145;

%reading comment lines in the file
for i = 1:3
    comment{i,1}= fgetl(f1);
end


for i=1:53

    %reading species names
    b = fgetl(f1);
    a = strsplit(b,' ');
    species(i) = a(1);
    

    for j =1:3
    local_temp(i,j) = a(1,end-j);
    end

    line1 = fgetl(f1);
    A = strfind(line1,'E');

    line2 = fgetl(f1);
    B = strfind(line2,'E');

    coeff{i,1} = line1(1:A(1)+3);

    %reading high temperature coefficients
    for j = 2:5
        
        coeff{i,j} = line1(A(j-1)+4:A(j)+3);
    end

    %reading high temperature coefficients
    coeff{i,6} = line2(1:B(1)+3);
    coeff{i,7} = line2(B(1)+4:B(2)+3);
    

 %reading low temperature coefficients
    for j = 8:10
        coeff{i,j} = line2(B(j-6)+4:B(j-5)+3);
    end

    line3 = fgetl(f1);
    C = strfind(line3,'E');

    coeff{i,11} = line3(1:C(1)+3);
    for j = 12:14
        coeff{i,j} = line3(C(j-11)+4:C(j-10)+3);
    end




end

%temperature range
%converting cell array elements to double
min_local_temp = str2double(local_temp(:,3));
max_local_temp = str2double(local_temp(:,2));
mid_local_temp = str2double(local_temp(:,1));
for i = 1:53
temperature(i,:) = linspace(min_local_temp(i),max_local_temp(i),100);
end

%converting cell array elements to double
coeff = str2double(coeff);

species = char(species);




%calculation

for i = 1:53

%specific heat
Cp(i,:) = specific_heat(coeff(i,1),coeff(i,2),coeff(i,3),coeff(i,4),coeff(i,5),coeff(i,6),coeff(i,7),coeff(i,8),coeff(i,9),coeff(i,10),coeff(i,11),coeff(i,12),coeff(i,13),coeff(i,14),temperature(i,:),R,mid_local_temp(i));

%entropy
S(i,:) = entropy(coeff(i,1),coeff(i,2),coeff(i,3),coeff(i,4),coeff(i,5),coeff(i,6),coeff(i,7),coeff(i,8),coeff(i,9),coeff(i,10),coeff(i,11),coeff(i,12),coeff(i,13),coeff(i,14),temperature(i,:),R,mid_local_temp(i));

%enthalpy
H(i,:) = enthalpy(coeff(i,1),coeff(i,2),coeff(i,3),coeff(i,4),coeff(i,5),coeff(i,6),coeff(i,7),coeff(i,8),coeff(i,9),coeff(i,10),coeff(i,11),coeff(i,12),coeff(i,13),coeff(i,14),temperature(i,:),R,mid_local_temp(i));



end
%molecular weight
mol_wt = molecular_weight(species);


for i = 1:53
%write molecular lsit
molecular_list = fopen('Molecular weight.txt','a+');
fprintf(molecular_list,'molecular weight of %s  is  %f  \n',species(i,:),mol_wt(i));
fclose(molecular_list);
end





%plot
for i =1:53

%saving as afolder

cd ('D:\matlab projects\species');
mkdir(species(i,:));
cd(species(i,:))

figure(1)
plot(temperature(i,:),Cp(i,:),'Color','r');
xlabel('Temperature [K]');
ylabel('specific heat [Kj/kg-K]');
title(sprintf('specific heat vs temperature of %s',species(i)));
saveas(figure(1),'specific heat.jpg');

figure(2)
plot(temperature(i,:),S(i,:),'Color','b');
xlabel('Temperature [K]');
ylabel('entropy ');
title(sprintf('entropy vs temperature of %s',species(i)));
saveas(figure(2),'entropy.jpg');


figure(3)
plot(temperature(i,:),H(i,:),'Color','g');
xlabel('Temperature [K]');
ylabel('enthalpy [Kj]');
title(sprintf('enthalpy vs temperature of %s',species(i)));
saveas(figure(3),'enthalpy.jpg');







fprintf('molecular weight of  %s  is  %f \n',species(i,:),mol_wt(i))

cd ('D:\matlab projects');


end

Output

Plots for O2

Plots for N2

Plots for CO2

google drive link

https://drive.google.com/drive/folders/1MGqKmNTSHuT4ug-L_6Sm6yH-KfXu1Aij?usp=sharing

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.