Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:
To plot the graph of the elements of given that O2,N2....

To  calculate the molecular weight of givem element 
CODE

clear all
close all
clc
% for getting the data file
f1=fopen('THERMO.dat','r');
fget1(f1);
% reading the first line of the file
header_global_temp=fget1(f1);
A=strsplit(header_global_temp,'');  % splitting the string
% obtaining global max,medium and minimum temp:
global_min=strsplit(A{2});
global_mid=strsplit(A{3});
global_max=strsplit(A{4});

% ignoring the comments in the file
for i=1:53
    header=fget1(f1);
    species_header=strsplit(header,'');
    species_name=species_header{1};
 % finding the string 
 loc_temp=strsplit(header,'');
 species_name=(loc_temp(1));
 loc_min_temp=str2double(loc_temp(end-3));
 loc_mid_temp=str2double(loc_temp(end-2));
 loc_max_temp=str2double(loc_temp(end-1));

 % finding the coefficients a1 to a14
 tline1=fget1(f1);
 tline2=fgetl(f1);
 tline3=fgetl(f1);

 a=strfind(tline1,'E');
 a1=tline1(1:a(1)+3);
 a1=str2double(a1);

 a2=tline1(a(1)+4:a(2)+3);
 a2=str2double(a2);

 a3=tline1(a(2)+4:a(3)+3);
 a3=str2double(a3);

 a4=tline1(a(3)+4:a(4)+3);
 a4=str2double(a4);

 a5=tline1(a(4)+4:a(5)+3);
 a5=str2double(a5);

 % finding strings and convert to numbers in line3
 a=strfind(tline2,'E');

 a6=tline2(1:a(1)+3);
 a6=str2double(a6);

 a7=tline2(a(1)+4:a(2)+3);
 a7=str2double(a7);

 a8=tline2(a(2)+4:a(3)+3);
 a8=str2double(a8);

 a9=tline2(a(3)+4:a(4)+3);
 a9=str2double(a9);

 a10=tline2(a(4)+4:a(5)+3);
 a10=str2double(a10);

 % finding strings and convert to numbers in line4

 a=strfind(tline3,'E');
 a11=tline3(1:a(1)+3);
 a11=str2double(a11);

 a12=tline3(a(1)+4:a(2)+3);
 a12=str2double(a12);

 a13=tline3(a(2)+4:a(3)+3);
 a13=str2double(a13);

 a14=tline3(a(3)+4:a(4)+3);
 a14=str2double(a14); 

 % creating folder
  mkdir(['C:UsersNIKHILKPDownloadspasingnasa',species_name]);


 T=linspace(loc_min_temp,loc_min_temp,loc_max_temp,1000);
 R=8.314;
 % for getting the array of cp,h.and s
 % a1-a7 is high temperature coefficients
 % a8-a14 is low temperature coefficients
 if T(k)lok_med_temp
     % high temperasture coefficients
     cp=(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4)*R;
     H=(a1+(a2.*T)/2+(a3.*T.^2)/3+(a4.*T.^3)/4+(a5.*T.^4)/5+a6./T))*R.*T;
     S=a1.*log(T)+a2.**+(a3.T.^2)/2+(a4.*T.^3)/3+(a5.*T.^4)/4+a7;
 else
     % low temperature coefficients
     cp=(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4)*R;
     H=(a8+(a9.*)/2+(a10.*T.^2)/3)+(a11.*T^3)/4+(a12.*T^4)/5+(a13./T))R.*T;
     S=a8.*log(T)+a9'*T+(a10.*T.^2)/2+(a11.*T^3)/4+(a12.*T.^4)/4+a14;
 end
end

% for i=1:53
  % loc_temp=strsplit(tline1,'');
  % species_name=(loc_temp(1));

% plotting the curves
% cp v/s T
figure(1);
plot(cp,T,'LineWidth',3,'color','r');
grid on
title(species_name);
xlabel('specific heat [j/k]');
ylabel('temperature [k]');
saveas(1,'cp_vs_temp.jpeg');

% H v/s T
figure(2);
plot(H,T,'LineWidth',3,'color','b');
title(species_name);
xlabel('Enthalpy [j]');
ylabel('temperature [k]');

% entropy v/s temperature
figure(3);
plot(S,T,'LineWidth',3,'color','g');
title(species_name);
xlabel('entropy [kj-1/mol]');
ylabel('temperature [k]');


% changing the directory and saving the file
saving=(['C:NIKHILKPDownloadspasingnasa',species_name']);
cd(saving)
saveas(1,['cp_vs_temp.jpeg');
saveas(2,'enthapy_vs_temp.jpeg');
saveas(3,'entropy_vs_temp.jpeg');
end


FUNCTION

function[w]=mol_weight(species_name)
atomic_names=['H','C','O','N','A'];
atomic_weights=[1,12,16,14,40];
w=0;
for i=1:length(species_name)
    for j=1:length(atomic_names)

        if strcmpi(species_name(i),atomic_names(j))
            W=w+atomic_weights(j);
            pos=j;
        end
        n=str2num(species_name(i));
    end
    if n>=1
        W=w+atomic_weigths(pos)*(n-1);
        k=fprintf('molecular weight of %s = %f',species_name,W);
    end
end
CODE EXPLANATION

*First command window and workspace are being cleared and closed.
*To start with we open the thermodynamic data file i.e THERMO.dat
*Following this we read the heading of the file with help of 'fgtel',reading the first line
*Using the strsplit command we extract and read global,low mid and high temperature given below.
*At the point we need to skip next three lines and this is done by using 'fgetl'command again 3 times.
*Following this next we have to extract the coefficients which are specific for each species.This is done by using'for loop'.We run the loop for 53 times as we have 53 different species in the file.
*With in the loop we read and extracted the local temperaturefor each species.this is done by using calculate the thermodynamic props.
*Using the 'findstr'command we specifically found out the number of exponentialin the first line of coefficients and accordingly extracted the number of coefficinets.
*This is done of the all the coefficients carrying line seprately.The coefficients which are being extracted are in string form and are converted to number with 'str2num' command.
*The first 7 coefficient are high temp coefficients and the next 7 are low temp coefficints.so to calculate these properties specifically for high and low temperature again 'for loop' is used and distinguished using 'if' condition statemenet.
*The value of gas constant is taken equal to 8.314 j/(mol.k) and the temperature range is taken 1000 values between them.
*Following the calculation of the thermodynamic properties ,the current directory is changed to the new directory which will be made for each species with that species name.
*For each species using plot command ,the plots are then be saved and dumped in the separate directory folder with their name as mentioned in the last step.
*'cd'command is used to get back to the original directory.
*Then now create function code to make the finding the molecular weigth of the element.as the command box the molecular weigth of seperate elements should be passed.
*This function code is called into the main data parsing code and the molecular command window,following which the loop is closed.
*And finally we close the THERMO.dat file using fclose command



 

 
 

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.