Challenge

Project 1 - Parsing NASA thermodynamic data

NASA FILE PARSE

AIM:

To parse NASA's thermal coefficients file and produce Entropy(S), Enthalpy(H), Specific heat at constant pressure(Cp) for elements mention in that. And also find Molecular Wt of the elements.

INTRODUCTION:

In this program,'Thermo.txt' file is parsed and their elements have been read. Using this information Cp,H,S calculated using formulae listed below,

PROGRAM:

1. Main program.

  • File open by command 'fopen()' and read.
  • Using fgetl() and fscanf() strings and float values scanned and get in an array.
  • Then values separated and perfect co-efficient and local temperature obtain.
  • Using function Cp, H,S calculated using formula (Function).
  • Molecular Wt also calculated by another function.(Atom).
  • To show result 2 methods are used.
  1. Particular element search.

For this type element in caps and particular elements Cp,H,S will be created. Molecular weight will be shown in Cp plot. This process continues until you enter "y" or wrong value.

  1. All elements plotted.

All elements will be plotted and plots will be saved in separate folders.

clear all
close all
clc
%Get values in text file
A=fopen('Thermo.txt','r');
j=1;                                                 %Initial value
while ~feof(A)                                       % loop runs untill end of file
    Fget=fgetl(A);                                   %Gets string in file for each itreation
    Fscan=fscanf(A,'%f');                            %Get float values in file for each itreation
    C=length(Fscan);                                 %no of values taken by fscan in that itreation
        if C>0                                       %if Fscan takes float value it enter into "if" otherwise next itreation will be excecuted  
           for l_1=1:C                               % For 1 to length of C(Consider Fscanf takes C values
             Array_Value_1(l_1,j)=Fscan(l_1);        %Each values stored as a row for j coloum (Totally 17 values will be stored. 14 co-efficent + 3 row indicators
             Row=C;                                  %store No. of row
             L2=length(Fget);                        %No of string stored by fgetl
               
             for L_2=1:L2                            %For length of strings   
                Array_string(j,L_2)=Fget(L_2);       %Each line(string) stored as a coloum (Totally 80  
                end
             end
      Coloum=j;                                      %Stores No.of coloum
      j=j+1;                                         %Increment coloum for each value updated
    end
end

    %Extract perfect co-efficients
for C=1:Coloum;                             %For loop for coloum(54)
    j=1;                                    %Initial value
   for i=1:Row                              % For loop for row(17)
      if i~=6 &&i~=12 &&i~=17               %Eliminates Row indicators in file
      Co_efficient(j,C)=Array_Value_1(i,C); %stores Co-efficents as new array
      j=j+1;                                %New coloum increment(14)
      end
    end
end

%Split string
for J=1:Coloum                              %For 53 elemnts
    Inter=strsplit(Array_string(J,:));      %split strings in coloum and stored as intermidiate value
    a=length(Inter);                        %length of intermidieate value stored
    K(J,1:a)=strsplit(Array_string(J,:));   %Store splited string as k(12 string for each elements
end
K=K';                                       %Transpose of string for calculation 

%Get local temperature
Number_in_string = str2double(K);     %convert string(if No. format) to double values  
for kk=1:Coloum                       %for loop for no.of elements
  j=1;                                %Initial value
for itr=1:12                          %sting has 12 cells. excecute each cell
    A11=Number_in_string(itr,kk);     %A11 is temporary variable store cells
   
    if A11>=100                       %If values is above or equal to 100
        temperature(kk,j)=A11;        %that Values will be stored as temperature
        j=j+1;                        %Each elements has 3 temperature value
    end
end
end

temperature=temperature';                %transpose
Co_efficient;
%--------------------------------------------------------------------

%Find Cp,H,S for each elements
for itr=2:Coloum

[CpI HI SI TeI]=Function(Co_efficient,temperature,itr)   %function process Co-efficents,temperature and gives Cp,H,S
Cp(itr,1:100)=CpI(1,1:100);          %Cp stored as coloum for each itreation
H(itr,1:100)=HI(1,1:100);            %H stored as coloum for each itreation
S(itr,1:100)=SI(1,1:100);            %S stored as coloum for each itreation
Te(itr,1:100)=TeI(1,1:100);
cha=K(1,itr);                        %cha stores elements name with respect to matrix
[Atom_wtI]=Atom(cha)
Atom_Wt(itr)=Atom_wtI;
end
Method=input("Enter '1' for get particular elements\nEnter '2' to get every elements :")
%input value to choose method
if Method==1
%method 1 if '1' is entered
while string~='y'  %terminate while loop if y is entered
string=input('To end enter "y"\nElement to find: ','s');  %Element inputed by user as string
if string~='y'
ele=0;                                                     %initial value
    for i=1:Coloum
    Char=K(1,i)                                            %Each elements taken for comparision
    t=strcmp(Char,string)                                  %String compare, Compares Inputed string and element in file
    if t>0                                                 %If strcmp() true next step will be excecuted
        ele=i;                                             %Stores no. of itreation as ele
        end
    end
%Terminaion statement if any in-proper value
if ele==0
    Method=5;
    string='No';
    break;
end
%plotting results    
E1=K(1,ele)                                  %Element name stored as cell
disp(Atom_Wt(ele))
cha1=char(E1);                               %Cell converted into character

figure(1)                                    %Plot 1
plot(Te(ele,1:100),Cp(ele,1:100))                       %Temperature Vs Cp of selected element
 xlabel('Temperature [K]')                   %xlabel
 ylabel('Cp [Joules/Kg.K]')                  %y label
title({cha1,'Specific heat at constant pressure','Atomic Weight' Atom_Wt(ele)})
 figure(2)                                   %Plot 2
plot(Te(ele,1:100),H(ele,1:100))                        %Temperature Vs S of selected element
  xlabel('Temperature [K]')
  ylabel('H [Joules]')
title({cha1,'Enthalpy'})
  figure(3)                                  %Plot 3
plot(Te(ele,1:100),S(ele,1:100))                        %Temperature Vs H of selected element
xlabel('Temperature [K]')
ylabel('S [Joules/K]')
title({cha1,'Entropy'})                      %Titel of element name and plot name

end
end

elseif Method==2                             %Method 2 Plot every result

for value=2:Coloum
   itr=value;                                
   cha=K(1,itr);                             %Element name as cell
cha1=char(cha);                              %Element name as string

figure(1)
 h1=plot(Te(itr,1:100),Cp(itr,:),'linewidth',3)         %Temperature Vs Cp of selected element stored as h
 xlabel('Temperature [K]')
 ylabel('Cp [Joules/Kg.K]')
title({cha1,'Specific heat at constant pressure','Atomic Weight' Atom_Wt(itr)})
 figure(2)
 h2=plot(Te(itr,1:100),H(itr,:),'linewidth',3)          %Temperature Vs H of selected element stored as h
  xlabel('Temperature [K]')
  ylabel('H [Joules]')
title({cha1,'Enthalpy'})
  figure(3)
h3=plot(Te(itr,1:100),S(itr,:),'linewidth',3)           %Temperature Vs S of selected element stored as h
xlabel('Temperature [K]')
ylabel('S [Joules/K]')
title({cha1,'Entropy'})

Image_Name=cha1
variable='OH'
mkdir(Image_Name)
d1=sprintf('/%s/Cp.jpg',Image_Name)
saveas(h1,[pwd d1])

d2=sprintf('/%s/Entropy.jpg',Image_Name)
saveas(h2,[pwd d2])
d3=sprintf('/%s/Enthalpy.jpg',Image_Name)
saveas(h3,[pwd d3])
disp('Molecular Wight')
disp(Atom_Wt(itr))

end
end
    %If any wrong values entered program give message and end
if Method~=1 &&Method~=2
        disp("Incorrect Value  Restart Program");
end
      

FUNCTIONS:

1.Forumule.

function [Cp H S Te]=Function(Co_efficient,temperature,itr)   %Function Initialized
Temp=temperature(:,itr)                                    %Takes Temperature value
Coef=Co_efficient(:,itr)                                   %Takes Co-efficents                 
Temp_range=sort(Temp)                                    %Stores value in asscending order
T_low=linspace(Temp_range(1),Temp_range(2),50);            %50 Temperature values from Local Temp. to Middle Temp
T_hig=linspace(Temp_range(2),Temp_range(3),50);            %50 Temperature values from Middle Temp. to High Temp
Te=[T_low T_hig]                                           %Add T to get total value
Len_1=length(T_low);                                       %Length of T low
Len_2=length(T_hig);                                       %Length of T high
for i=1:Len_1                                              %For T low 
    T=T_low(i);                  %For low temp second set(8~14) of 7 co-efficents used
    Cp_R_1(i)=(Coef(8))+(Coef(9)*T)+(Coef(10)*(T^2))+(Coef(11)*(T^3))+(Coef(12)*(T^4));
    H_R_1(i)=((Coef(8))+(0.5*Coef(9)*T)+(0.3333*Coef(10)*(T^2))+(0.25*Coef(11)*(T^3))+(0.2*Coef(12)*(T^4))+(Coef(13)/T))*T;
    S_R_1(i)=(Coef(8)*log(T))+(Coef(9)*T)+(0.5*Coef(10)*(T^2))+(0.3333*Coef(11)*(T^3))+(0.25*Coef(12)*(T^4))+Coef(14);
end
 %For low temp first set(1~7) of 7 co-efficents used
for j=1:Len_2
    T=T_hig(j)
    Cp_R_2(j)=(Coef(1))+(Coef(2)*T)+(Coef(3)*(T^2))+(Coef(4)*(T^3))+(Coef(5)*(T^4));
    H_R_2(j)=((Coef(1))+(0.5*Coef(2)*T)+(0.3333*Coef(3)*(T^2))+(0.25*Coef(4)*(T^3))+(0.2*Coef(5)*(T^4))+(Coef(6)/T))*T;
    S_R_2(j)=(Coef(1)*log(T))+(Coef(2)*T)+(0.5*Coef(3)*(T^2))+(0.3333*Coef(4)*(T^3))+(0.25*Coef(5)*(T^4))+Coef(7);
end
%Combine 2 matries
Cp_R=[Cp_R_1 Cp_R_2];                  
H_R=[H_R_1 H_R_2];
S_R=[S_R_1 S_R_2];
%Multiplied by Universal gas constant and final value uptained
  Cp= Cp_R.*(8.314);
  H= H_R .*(8.314);
  S=S_R.*(8.314);
  Te=[T_low T_hig];
end  

2.Molecular weight

function [Atom_Wt]= Atom(cha)
Element=char(cha)                         %Cell converted into string            
Le=length(Element)                        %Length of string
%Initial Value
i=1;
j=1;
Atom_Wt=0;

while i~=Le+1                               %Loop runs untill every elements read
S1=Element(i);                              %One element taken
T=str2double(S1);                           %Convert string to double(if number as string)
if T>0                                      %Check wheather its number
    S1='num'                                %set value as 'num' for case
end
%Value will be switched according to Element
switch S1
    case 'H'
        N(j)=1;
        j=j+1;
    case 'C'
        N(j)=12;
        j=j+1;
    case 'N'
        N(j)=14;
        j=j+1;
    case 'O'
        N(j)=16;
        j=j+1;
    case 'A'
        N(j)=40
        j=j+1;
%If number then it will multiplied with previous one        
    case 'num'
        j=j-1;
        N(j)=T*N(j)
        j=j+1;
       end
i=i+1;            %Increase for next elements
end
for k=1:j-1
    Atom_Wt=Atom_Wt+N(k)         %add every value
end
end

Screenshot of folder:

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.