Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing NASA thermodynamic data:

AIM:

TO 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. The formulae are shown below.

Here R is the universal gas constant, T is the temeprature

R = 8.314;

The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.

Also use the LOCAL TEMPERATURE RANGES which is unique for each individual species, not the GLOBAL TEMPERATURE RANGE.

2. Calculate the molecular weight of each species and display it in the command window.

3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.

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

EXPLANATION:

1. FILE PARSING

we can read and write in the text file with the help of file parsing and use that file in program as inputs

we have to create a file with 'r' command to read certain file

and 'w' command to write in certain file if we have to send it to someone

we can add floating point numbers, skip it to next line and many more features in file parsing

Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar.

NASA thermodynamic file:

THERMO.dat consist of 14 coefficient of 53 elements. we have to parse 14 coefficient one by one to find

specific heat, enthalpy and entropy and it s molecular weight for each element.

53 elements in THERMO.dat

formula for specific heat,enthalpy and entropy:

Specific heat:

specific heat, the quantity of heat required to raise the temperature of one gram of a substance by one celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree.

Entropy:

entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecularmotion, the amount of entropyis also a measure of the molecular disorder, or randomness, of a system.

enthalpy:

Enthalpy is a thermodynamic property of a system. It is the sum of the internal energy added to the product of the pressure and volume of the system. It reflects the capacity to do non-mechanical work and the capacity to release heat. Enthalpy is denoted as H; specific enthalpy denoted as h.

MAIN CODE:

% main program

clear 
close all
clc


% opening and reading file from THERMO.dat
f1 = fopen('THERMO.dat','r');

% this code read the lines in THERMO.dat one by one
gt = fgetl(f1);

% we are doing it again to get a global temprature from that 
% header information
global_temperature = fgetl(f1);

% splitting the three temprature
T = strsplit(global_temperature);

% string to num
% finding global low medium and high temperature
global_low_temp = str2num(T{2});
global_medium_temp = str2num(T{3});
global_high_temp = str2num(T{4});

% R is the gas constant
R = 8.314;

temp = linspace(global_low_temp,global_high_temp,global_high_temp-global_low_temp);

% we have to skip the three lines in thermo.dat because it is commands
% we are using for loop so we dont have to type (fgetl) too many times

for i = 1:3
    sk = fgetl(f1);
end

 for i = 1:53
    sk = fgetl(f1);
    B = strsplit(sk,' ');
    % saving (B{1})
    Thermo_species = (B{1});

    all_species{i} = Thermo_species;

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

    % finding a1:a14 using the logic of findind where E is

    % for 1st line 

    a1 = line1(1:a(1)+3);
    a1 = str2num(a1);

    a2 = line1(a(1)+4:a(2)+3);
    a2 = str2num(a2);

    a3 = line1(a(2)+4:a(3)+3);
    a3 = str2num(a3);

    a4 = line1(a(3)+4:a(4)+3);
    a4 = str2num(a4);

    a5 = line1(a(4)+4:a(5)+3);
    a5 = str2num(a5);

    % for 2nd line
    % going to line 2 
    line2 = fgetl(f1);
    b = strfind(line2,'E');

    a6 = line2(1:b(1)+3);
    a6 = str2num(a6);
  
    a7 = line2(b(1)+4:b(2)+3);
    a7 = str2num(a7);

    a8 = line2(b(2)+4:b(3)+3);
    a8 = str2num(a8);
 
    a9 = line2(b(3)+4:b(4)+3);
    a9 = str2num(a9);

    a10 = line2(b(4)+4:b(5)+3);
    a10 = str2num(a10);


    % for 3rd line
    % going to line 3
    
    line3 = fgetl(f1);
    c = strfind(line3,'E');

    a11 = line3(1:c(1)+3);
    a11 = str2num(a11);
 
    a12 = line3(c(1)+4:c(2)+3);
    a12 = str2num(a12);

    a13 = line3(c(2)+4:c(3)+3);
    a13 = str2num(a13);
    
    a14 = line3(c(3)+4:c(4)+3);
    a14 = str2num(a14);



% calling the function:

% 1. specific heat (cp)

    cp = specific_heat_function(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,R,global_medium_temp);

% 2. enthalpy (H)

    H = enthalpy_function(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,R,global_medium_temp);

% 3. entropy (S) 

    S = entropy_function(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,R,global_medium_temp);


% 4. molecular weight:

  %calculating molecular weight of thermo species.

    molecular_weight(i) = molecular_weight_function(Thermo_species);


    % naming every elements in molecular weight

    

    mkdir(Thermo_species)
    cd(Thermo_species)

   
    % plotting for temperature and specific heat

figure(1)
    plot(temp,cp,'linewidth',3,'color','r')
    xlabel('temperature [k]')
    ylabel('specific heat[kj]')
    title(sprintf('specific heat and temperature %s',Thermo_species))
    grid 'on'
    saveas(figure(1),'specific heat.jpg');

    % plotting for temperature and enthalpy

figure(2)
    plot(temp,H,'linewidth',3,'color','g')
    xlabel('temperature [k]')
    ylabel('Enthalpy [kj/mol] ')
    title(sprintf('enthalpy and temperature %s',Thermo_species))
    grid 'on'
    saveas(figure(2),'enthalpy.jpg');

    % plotting for temperature and entropy

figure(3)     
    plot(temp,S,'linewidth',3,'color','b')
    xlabel('temperature [k]')
    ylabel('entropy[kj/(mol-k]')
    title(sprintf('entropy and temperature %s',Thermo_species))
    grid 'on'
    saveas(figure(3),'entropy.jpg');

    cd ..



 end
    


for i = 1:53
    MW =  molecular_weight_function(all_species{i});
    fprintf('molecular weight of %s = %f\n',all_species{i},MW)
end    

CODE EXPLANATION:

clear all - clears workspace

close all - close allother tabs

clc - clears command window

fopen - to open the file thermo.dat and reading in it

fgetl - we can read the line one by one

using fgetl to read the global temperature

splitting all the three temperature using strsplit

providing low mid and high temp

skipping all three command lines using for loop command

and by using for loop and by the help of provided helping video we creating the species name and all the coefficients

such as a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14.

using formula and function command we are providing function for specific heat, entropy, enthalpy and to find molecular weight for each element.

and calling all the function in the main program.

mkdir - make directory

cd - current directory

plotting all the values

1. plotting for temperature and specific heat

2. plotting for temperature and enthalpy

3. plotting for temperature and entropy

labelling and plotting

giving a title

saving in the program as well

we have to write how to show the molecular weight, so we are taking all the species and running it in loop and printing how i want to display it eg: molecular weight of O = 15.999000

Specific heat Function code:

% specific heat function

function cp = specific_heat_function(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,R,global_medium_temp)

% if less than global medium  temprature minimum , low temprerature coefficients
% are taken
% if it is higher than global medium then higher coefficients are taken
for i = 1:length(temp)

   if  temp(i) <= global_medium_temp

   X(i) = R*(a1 + (a2*temp(i)) + (a3*(temp(i))^2) + (a4*(temp(i))^3) + (a5*(temp(i))^4));

   else
   X(i) = R*(a8 + (a9*temp(i)) + (a10*(temp(i))^2) + (a11*(temp(i))^3) + (a12*(temp(i))^4));

   end
end
cp = X;
end

Explanation:providing the formula for specific heat and substiuting all the coefficient in the loop. and calling the function in main program.

Enthalpy Function Code:

% enthalpy function

function H = enthalpy_function(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,R,global_medium_temp)

for i = 1:length(temp)

    if  temp(i) <= global_medium_temp
    
    X(i) = R*temp(i)*(a1 + ((a2*temp(i))/2) + ((a3*(temp(i))^2)/3) + ((a4*(temp(i))^3)/4) + ((a5*(temp(i))^4)/5) + ((a6/temp(i))));
    
    else
    
    X(i) = R*temp(i)*(a8 + ((a9*temp(i))/2) + ((a10*(temp(i))^2)/3) + ((a11*(temp(i))^3)/4) + ((a12*(temp(i))^4)/5) + ((a13/temp(i))));
    
    end
end

H = X;
end

Explanation:providing the formula for Enthalpy and substiuting all the coefficient in the loop. and calling the function in main program.

Entropy Function Code:

% entropy_function

function S = entropy_function(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,R,global_medium_temp)

for i = 1: length(temp)

    if   (temp(i) >= global_medium_temp)
    
    X(i) = R*(a1*log(temp(i)) + (a2*temp(i)) + (a3*((temp(i))^2)/2) + (a4*((temp(i))^3)/3) + (a5*((temp(i))^4)/4) + a7);
    
    else
    
    X(i) = R*(a8*log(temp(i)) + (a9*temp(i)) + (a10*((temp(i))^2)/2) + (a11*((temp(i))^3)/3) + (a12*((temp(i))^4)/4) + a14);
    
    end

    
end 
S = X;
end

Explanation:providing the formula for Entropy and substiuting all the coefficient in the loop. and calling the function in main program.

Molecular Weight Function Code:

% molecular weight function

function results = molecular_weight_function(Thermo_species)

% in all this 53 elements with combining all we only have six elements are
% hydrogen, carbon, oxygen, nitrogen, Argon.

atomic_name = ['H','N','A','C','O'];

% atomic names and corresponding weight:

atomic_weight = [ 1.00784 14.0067 39.948 12.0107 15.999];

% initial value of molecular weight

molecular_weight = 0;

% for obtaing each molecular weight:

for i = 1:length(Thermo_species)
    for j = 1:length(atomic_name)
    % comparing thermo species and atomic name for(if there is no number in
    % the element for eg: CH it can easily calculate the value if there is
    % a number we have to add another combination of code)

    if strcmpi (Thermo_species(i),atomic_name(j))
         molecular_weight = molecular_weight + atomic_weight(j);
         position = j;
    end     
    end
% changing string to number 
P = str2num(Thermo_species(i));

if P>1
    molecular_weight = molecular_weight + atomic_weight(position)*(P-1);

    % this is a code for if number exist in element such as H2O
    % we are adding P-1 because it will already add H as 1 element,if number 2 added
    % to it it will become 3.
end    

end
results = molecular_weight;

Explantion:

1.In all this 53 elements with combining all we only have six elements are
hydrogen, carbon, oxygen, nitrogen, Argon.

2. providing corresponding atomic weight.

3. providing initial value of molecular weight.

4. for obtaing each molecular weight comparing thermo species and atomic name for(if there is no number in the element for eg: CH it can easily calculate the value if there is a number we have to add another combination of code).

5. using strcmpi = comparing command

6. this is a code for if there is any number exist in element, such as H2Owe are adding P-1 because it will already add H as 1 element,if number 2 added to it it will become 3. so we are subracting 1.

7. end command ends the program.

Molecular weight for each elements in the command window:

Plots for specific heat, enthalpy, entropy for each element:

proving for 3 elements:

C2H:

enthalpy:

entropy:

Specific Heat:

H2O:

enthalpy:

entropy:

Specific heat:

HCNO:

enthalpy:

entropy:

Specific heat:

Screenshot of directory:

conclusion:

using matlab and With the help14 co-efficients and we calculated the enthalpy, entropy and specific heats for all the species in the data file. and plots are attached and molecular weight of each species are calculated and screesnshot of that are attached.

References:

https://in.mathworks.com/help/matlab/ref/strcmpi.html#:~:text=tf%20%3D%20strcmpi(%20s1%2Cs2%20)%20compares%20s1%20and%20s2,is%20of%20data%20type%20logical%20.

https://in.mathworks.com/help/matlab/ref/mkdir.html

https://in.mathworks.com/help/matlab/ref/sprintf.html

https://in.mathworks.com/help/matlab/ref/fgetl.html

https://in.mathworks.com/help/matlab/ref/fprintf.html

https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view?usp=sharing

for thermo.dat file.

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.