Challenge

Project 1 - Parsing NASA thermodynamic data

clear all
close all
clc



% obtain Data File -NASA Thermodynamics
f1 = fopen('THERMO.dat','r');
gt = fgetl(f1);

% Obtain temperature -by reading header information
global_temp = fgetl(f1);

t = strsplit(global_temp);

% Covering string cells
global_low_temp = str2num(t{2});
global_medium_temp = str2num(t{3});
global_high_temp = str2num(t{4});

% Universal gas constant J/(mol-K)
R = 8.314;

% skiping comment Lines
for i = 1:3
    gt = fgetl(f1);
end

for i = 1:53
    gt = fgetl(f1);
    A = strsplit(gt,' ');
    species_sp = (A{1});

   local_low_temp = str2num(A{end-3})
   local_high_temp = str2num(A{end-2})
   local_medium_temp = str2num(A{end-1})
   Temp = linspace(local_low_temp,local_high_temp,500)
    line_1 = fgetl(f1);
    a = strfind(line_1,'E');

    % Extracting higher temperature coefficients
    a1 = line_1(1:a(1)+3);
    a1 = str2num(a1);

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

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

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

    line_2 = fgetl(f1);
    b = strfind(line_1,'E');
    a6 = line_2(1:b(1)+3);
    a6 = str2num(a6);

    a7 = line_2(a(1)+4:b(2)+3);
    a7 = str2num(a7);

    % Extracting loe temperature coefficients
    a8 = line_2(b(2)+4:b(3)+3);
    a8 = str2num(a8);

    a9 = line_2(b(3)+4:b(4)+3);
    a9 = str2num(a9);

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

    line_3 = fgetl(f1);
    c = strfind(line_1,'E');

    a11 = line_3(1:c(1)+3);
    a11 = str2num(a11);

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

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

    a14 = line_3(c(3)+4:c(4)+3);
    a14 = str2num(a14);


    % Specific Heat Calculations
    Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,local_medium_temp);

    % Enthalpy Calculations 
    H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,local_medium_temp);

   % Entropy calculation
    S = Entropy_clc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,local_medium_temp); 
    

   %  Calculating molecular weight of species
MolecularWeight(i) = Molecular_Weight(species_sp);

% Read and Write Molwcular list
  % Calculating Molecular Weight of species
    Molecular_list = fopen('Molecular_weight.txt','a+');
    fprintf(Molecular_list,'Molecular weight of %s is %d n',species_sp,MolecularWeight(i));
    fclose(Molecular_list);


   % Plotting(Cp,H,S,Temp,species_s)
   % Saving as a folder

   mkdir('C:\Users\Hp\Desktop\matlab programs',species_sp);
   cd(species_sp);

   % Plot 1,Specific heat (Cp) vs Temperature Rang (Temp)
   figure(1)
   plot(Temp,Cp,'LineWidth',3,'Color','r');
   xlabel('Temperature [K]');
   ylabel('Specific Heat [KJ]');
   title(sprintf('Varying Enthalpy With Temperature Range of %s',species_sp));
   grid on
   saveas(figure(1),'Specific Heat.jpg');


   figure(2)
   plot(Temp,H,'LineWIDTH',3,'Color','b');
   xlabel('Temperature[K]');
   ylabel('Enthalpy[Kj/(mol)]');
   title(sprintf('Varying Enthalpy with Temperature Range of %s',species_sp));
   grid on
   saveas(figure(2),'Enthalpy.jpg');

   % plot 3, Entropy (S) vs Temperature Range (Temp)
   figure(3)
   plot(Temp,S,'Linewidth',3,'Color','g');
   xlabel('Temperature(K)');
   ylabel('Entropy[KJ/(mol-K)]');
   title(sprintf('varying Entropy with Temperature Range of %s',species_sp));
   grid on
   saveas(figure(3),'Entropy.jpg');

   cd('C:\Users\Hp\Desktop\matlab programs');

   % Molecular weight
   M = Molecular_Weight(species_sp);
   f2 = fopen('molecular mass','W');
   fprintf('%s n %s n %f n','species','molecular mass',M)
   fclose(f2)

end


% function to find molecular weight of the different species

function Molecularweight = Molecular_Weight(species_Sp)

% defining atomic name of the species
atomic_name = ['H','C','O','N','A'];


% Standard atomic number
Atomic_Weight = [1.008 12.011 15.999 14.007 39.948];

% Starting Range of Molecule
Molecularweight = 0;   % Molecular weight starting range

% Adding Atomic number of molecule
for i = 1:length(species_Sp)    % loops runs to the length of the species
    for j = 1:length(atomic_name)    % function 2 strings
        if strcmpi(species_Sp(i),atomic_name(j))
        Molecularweight = Molecularweight + Atomic_Weight(j);
        position = j;
        end
    end

n = str2num(species_Sp);    % Now we will find if there is more element in the species then incrementing atomic weight of the species

if n>1
    Molecularweight = Molecularweight + atomic_weight(position)*(n-1);
end
end

fprintf('Molecular weight %s:',species_Sp);
fprintf('%d',Molecularweight);
disp(' ');
end



function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,local_medium_temp)

% calculating enthalpy
% calculating enthalpy for higher and lower temperature respectivly
for i = 1:length(Temp)


if Temp(i)<=local_medium_temp
    H(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))));
else
    H(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))));
end
end
end



function Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,local_medium_temp)

% Comparising With global temperature
% If less than global temp.- minimum coefficient taken
for i = 1:length(Temp)

if Temp(i) <=local_medium_temp
    Cp(i) = R*(a8+(a9*Temp(i))+(a10*Temp(i))^2)+(a11*(Temp(i))^3)+(a12*(Temp(i))^4);
else

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

end
end
end



% Entropy calculation Function

function S = Entropy_clc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,local_medium_temp) 
% comparing with global temperature

 if Temp<=local_medium_temp
    S = R*(a8*log(Temp))+(a9*Temp)+(a10*((Temp).^2)/2)+(a11*((Temp).^3)/3)+(a12*((Temp).^4)/4+a14);
    else

    S = R*(a1*log(Temp)+(a2*Temp)+(a3*((Temp).^2)/2)+(a4*((Temp).^3)/3)+(a5*((Temp).^4)/4)+a7);
    end

end




Understanding the CODE-
*  mainly the given data file is read with help of fopen command with reading 
   function.Then successive lines in the data file is extracted for global temperature 
   range using fgetl command.
*  The desirde input from the user is stored as a string.
*  the input string is then compaired with the molecule avalable in data file using 
   strcmp. if a match is obtained , respective co-efficients are extracted from the 
   data file using fgetl command. different co-efficients in a particular line is 
   stored out by specifying the place in that line. similarly the local temperature 
   range for the input molecule is also extracted from the data file.
*  in order to get molecular weight, the function of calculating the molecular weight 
   is called stored to a variable and is displayed.
*  Now, the themodynamic property values (CP,S,h),the associated functions are called.
*  mkdir command is then used in order to create a directory to store the output 
   figures .
*  Now  for the different thermodynamic propertie values associated Cp v T,S v T and 
   h v T plots are created.
*  current folder is defined where thee figure should be stored and saveas command is 
   used to store the figure to that particular folder created. 
*  current folder is again defined to make the directory back to parent folder and all the the open files are closed with fclose command.


Understanding the Functions
1) Molecular Weight
 
* A character array is define for the atoms and array of atomic weights respective to 
  character array is define

* The input string is compaired with character array, when a match occures,associate 
  atomic weight is taken

* for molecules having numbers defining the number of atoms, that number is first 
  extracted, and calculation is made to account for multiple atomic weights.
 
* a variable is assigned to store the output value of the function.


2) Enthalpy, Entropy and Specific Heat
* Since the coefficients available in data file is having a temperature constraint, an 
  if condition is used

* First 7 coefficients are high temperature coefficients and last 7 are low temperature coefficients, to calculate the thermodynamic property values, coefficients are selected with referance to temperature range.


OUTPUT:
Some random plots and molecular weight calculation:



problems during programming-
1- many times program shows errors in many steps.
2- problem in function program.
3- in function program unnesasory elementwise operators are given by that output is 
   change.


Steps taken to overcom that problems-
1- i take the help of thechnical support engineer 
2- correct program by the reading the problem











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.