Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: To write a code in Matlab to parse the NASA thermodynamics data file and then calculate thermodynamic properties, Molecular weight and plot the nature of the thermodynamic properties of various species.

Properties to calculate are shown below.

This code is too big so dividing the code into several parts, using the command "function" to call all those functions in the main program.

1. Function for specific heat CP

2. Function for enthalpy H

3. Function for Entropy S

4. Allplotting for plots for cp, H, S, and temp

5. Function for molecular weight

6. functions for reading and writing of mol_wt.dat (molecular weight) file.

MAIN CODE :

To write a function Allplotting for plots for cp, H, S, and temp.

"Allplotting" function consists of all the plots cp vs Temp, H vs Temp, S vs Temp, this does not have outputs but the plots and graphs, it only uses arguments from cp, H, S, temp.

In this function, the command is used 'mkdir' new folder is created and in the same current folder, 'pwd' folder is created with the species name.

The function runs 53 times in for loop and creates a separate folder for each element.

Using 'saveas' command the plot and fig are saved in the current folder.

and using 'cd' command folder is changed back to the original one.

clear all
close all
clc

f = fopen('THERMO.dat','r') % open and read the file
 fgetl(f)
 
 for i = 1:4          % header & reader
    fgetl(f)          % take the first line
 end
for i=1:53            % find the 53 species 

 a = fgetl(f);        % take the 1st line
b = strsplit(a,' ');  % split the line from string type to variable
species_name=b{1};    % species name mension

% extract the temp
L_T = str2double(b{end-3});        % split the temp from string,low temp 
mid_temp = str2double(b{end-1});   % split the temp from string, mid temp 
H_T = str2double(b{end-2});        % split the temp from string, high temp   

% extracte the coeff         % total 14 nos 1-7 - low & 8-14 - high
co_eff= fgetl(f);            % read the first line 
pos = strfind(co_eff,'E');   % we find the E value then convert the float type

a1=str2double(co_eff(1:pos(1)+3));
a2=str2double(co_eff(pos(1)+4:pos(2)+3));
a3=str2double(co_eff(pos(2)+4:pos(3)+3));
a4=str2double(co_eff(pos(3)+4:pos(4)+3));
a5=str2double(co_eff(pos(4)+4:pos(5)+3));

co_eff1=fgetl(f);
pos1 = strfind(co_eff1,'E');

a6=str2double(co_eff1(1:pos(1)+3));
a7=str2double(co_eff1(pos(1)+4:pos(2)+3));
a8=str2double(co_eff1(pos(2)+4:pos(3)+3));
a9=str2double(co_eff1(pos(3)+4:pos(4)+3));
a10=str2double(co_eff1(pos(4)+4:pos(5)+3));

co_eff2=fgetl(f);
pos2 = strfind(co_eff2,'E');

a11=str2double(co_eff2(1:pos(1)+3));
a12=str2double(co_eff2(pos(1)+4:pos(2)+3));
a13=str2double(co_eff2(pos(2)+4:pos(3)+3));
a14=str2double(co_eff2(pos(3)+4:pos(4)+3));

% gas constant value
R=8.314;

T=linspace(L_T,H_T,500); % create array for array 
%for i=1:length(T)       % use for for find the cp, h, s value 
   
    if (T<=mid_temp)
  
        CP1 = R*(a8+(a9*T)+a10*(T.^2)+a11*(T.^3)+a12*(T.^4));
        H1 = R*T.*(a8+a9*(T./2)+a10*((T.^2)./3)+a11*((T.^3)./4)+a12*((T.^4)./5)+(a13./T));
        S1 = R*(a8*log(T)+(a9*T)+a10*((T.^2)./2)+a11*((T.^3)./3)+a12*((T.^4)./4)+(a14));
    else
  
        CP1 = R*(a1+(a2*T)+a3*(T.^2)+a4*(T.^3)+a5*(T.^4));
        H1 = R*T.*(a1+a2*(T./2)+a3*((T.^2)./3)+a4*((T.^3)./4)+a5*((T.^4)./5)+(a6./T));
        S1 = R*(a1*log(T)+(a2*T)+a3*((T.^2)./2)+a4*((T.^3)./3)+a5*((T.^4)./4)+(a7)); 
    end
%end

cd('C:\Users\dell\OneDrive\Desktop\HEV\f_p') % create the folder
mkdir(species_name)
cd(species_name)
% plot
figure(1)
plot(T,CP1,'linewidth',3,'Color','r')
xlabel('CP')
ylabel('T')
title('CP VS T')
saveas(1,'sample1.jpg') % automatic save file name

figure(2)
plot(T,H1,'linewidth',3,'Color','y')
xlabel('H')
ylabel('T')
title('H VS T')
saveas(2,'sample2.jpg')

figure(3)
plot(T,S1,'linewidth',3,'Color','g')
xlabel('S')
ylabel('T')
title('S VS T')
saveas(3,'sample3.jpg')

mol_wt(i) = get_molecular_weight(species_name);  % find out 53 species molecular weight 
disp(mol_wt(i));
Molecular_weight = strcat(mol_wt(i),species_name);
weight = mol_wt(i)
Writing_mol(species_name,weight)
cd ..
end
fclose(f)

CO2 : CP VS T

CO2 : Enthalpy

CO2 : Entropy

N2 : Specific heat

N2 : enthalpy

N2 : ENtropy

O2 : specific heat

O2 : enthalpy

O2 : entropy

all species for 53 nos folder below

Function for molecular weight

In this function, the input argument is 'species' which is used to calculate the molecular weight of each element. for consideration 5 element is selected and stored in a variable called Atoms and Atomic_weight containing their weights. And called using get_molecular_weight

function molecular_weight = get_molecular_weight(species_name) % call function
molecular_weight = 0;
Atoms = ['O', 'H', 'N', 'C','A'];     % atoms name 
Atomic_weight = [16, 1, 14, 12,39];    % weights

for i = 1:length(species_name)     % create the for loop find the 53 species molecular weights
   for j = 1: length(Atoms)
    if strcmp(species_name(i),Atoms(j))
        molecular_weight = molecular_weight+ Atomic_weight(j);
        mark = j;
    end
end

K = str2num(species_name(i));

    if K>1
    
        molecular_weight = molecular_weight + Atomic_weight(mark)*(K-1);
    end
end
end

functions for reading and writing of mol_wt.dat (molecular weight) file.

Writing code :

function Writing_mol(species_name,weight)  % call functio for write code
dir_name = species_name
mol_file =fopen('mol_wt.dat','w');  
fprintf(mol_file,'molecular weight of %s : %f n',dir_name,weight);
fclose(mol_file);
end

Reading code :

clear all 
close all
clc
mol_file =fopen('mol_wt.dat','r')   % read the the code   
L1 = fgetl(mol_file)                % read the 1st line 
L2 =strsplit(L1,'nmolecular weighti of') % split the weight from the string type

ERROR :

this error some mistake found the molecular weight function.

totally, I had change the 'get_molecular_function' code.

https://drive.google.com/file/d/1z34BEK_UZyqaP6o2qScVq3WLS7PndG5H/view?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.