Challenge

Project 1 - Parsing NASA thermodynamic data

Aim : 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.

Given:

The formulae are shown below.

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

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.

These are used to calculate specific heat , enthalpy and entropy for different temperatures.

Main code:

clear all
close all
clc

file1 = fopen('THERMO.dat','r')
fgetl(file1)
temp = fgetl(file1)
A = strsplit(temp,' ')
global_low_temp = str2num(A{2})
global_mid_temp = str2num(A{3})
global_high_temp = str2num(A{4})
 for l = 1:3
     fgetl(file1)
 end

 for l = 1:53
  header = fgetl(file1)
 x = strsplit(header,' ')
 species_name = (x{1});
 local_low_temp = str2double(x{end-3})
 local_high_temp = str2double(x{end-2})
  local_mid_temp = str2double(x{end-1})
  
  t1 = local_low_temp
  t2 = local_mid_temp
  t3 = local_high_temp
  
  line_1  = fgetl(file1)
  a= strfind(line_1,'E')
  a1 = str2double(line_1(1:a(1)+3))
  a2 = str2double(line_1(a(1)+4:a(2)+3))
  a3 = str2double(line_1(a(2)+4:a(3)+3))
  a4 = str2double(line_1(a(3)+4:a(4)+3))
  a5 = str2double(line_1(a(4)+4:a(5)+3))
  
  line_2 = fgetl(file1)
  a_1= strfind(line_2,'E')
  a6 = str2double(line_2(1:a_1(1)+3))
  a7 = str2double(line_2(a_1(1)+4:a_1(2)+3))
  a8 = str2double(line_2(a_1(2)+4:a_1(3)+3))
  a9 = str2double(line_2(a_1(3)+4:a_1(4)+3))
  a10 = str2double(line_2(a_1(4)+4:a_1(5)+3))
  

  
  line_3 = fgetl(file1)
  a_2= strfind(line_3,'E')
  a11 = str2double(line_3(1:a_2(1)+3))
  a12= str2double(line_3(a_2(1)+4:a_2(2)+3))
  a13 = str2double(line_3(a_2(2)+4:a_2(3)+3))
  a14 = str2double(line_3(a_2(3)+4:a_2(4)+3))
  
  mkdir(['C:\Users\cheta\Desktop\matlab temp programs\nasa\file parsing\',species_name])
  T = linspace(t1,t3,1000)
  R = 8.314;
  
  for m = 1:length(T)
      if T(m)>t2 
      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.*T + (a3.*T.^2)/2 + (a4.*T.^3)/3 + (a5.*T.^4)/4 + a7)*R
      else
      cp = (a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4)*R;
      H = (a8+(a9.*T)/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)/3 + (a12.*T.^4)/4 + a14)*R
      end
  end
  
  %plotting
  figure(1)
  plot(T,cp)
  grid on
  title(species_name)
  xlabel('Temperature')
  ylabel('Specific heat')
  
  figure(2)
  plot(T,H)
  grid on
  title(species_name)
  xlabel('Temperature')
  ylabel('Enthalpy')
  
  figure(3)
  plot(T,cp)
  grid on
  title(species_name)
  xlabel('Temperature')
  ylabel('Entropy')
  
  saving=(['C:\Users\cheta\Desktop\matlab temp programs\nasa\file parsing\',species_name]);
  cd(saving);
  saveas(1,'specific heat','jpg');
  saveas(2,'enthalpy','jpg');
  saveas(3,'entropy','jpg');
 
 
 elements = ['H','C','O','N','A'];
 atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948];
 mol_mass = 0;
 for l = 1 : length(species_name)
     for m = 1:length(atomic_weight)
         if strcmp(species_name(l),elements(m))
             mol_mass = mol_mass + atomic_weight(m)
             position = m;
         end
     end
     
     n = str2double(species_name(l));
     if n > 1
         mol_mass = mol_mass + atomic_weight(position)*(n-1);
     end
 end
 sprintf('Molecular weight of %s %f u',species_name,mol_mass)
 end
 
 fclose(file1)
     
 
  
  
  
  

Steps:

1. We open the file by using fopen and save it into varaible file1

2.use fgetl to skip first line

3.store the next line as temperature using fgetl

4. split the temperature line by using strsplit and store it in array A

5. convert the string elements into numbers using str2num command

6. we skip next three lines using fgetl

7. we then use fgel to store the next line as header which contains species name , loacal low,high and mid temperature values

8.we then split the header using strsplit and convert each string to number uisng str2num

9.we store temperatures in varaibles as t1,t2 and t3

10.we then go to next line using fgetl

11. next three lines contains temperature values

12. first 7 corresponds to high temperature values and next to low temperature values

13.we do similar steps using fgetl and we use strfind to find the location of E

14.we then use the loaction of E and and locate where each temperature values ends.

15. we store these temperatures into variables a1,a2,a3,a4,a5,

16. since each species line contain 3 lines of temperature values, we repeat the steps 3 times to store all coefficient values from a1 to a14

17. we then make directory to store each individual species folder by using mkdir

18.we then create temperature array using linsapce

19.if local temperature is below the temperature we mentioned we use low temperature coefficients else we use high temperature coefficients

20.Then we plot the graph of Temperature vs cp,Temperature vs H,Temperature vs S

21.We then save these plots into folder we created

22. We then find the molecualr weight of each species and display it on output.

Output:

Plot of co2:

Plots of o2:

Plots of N2 :

Molecular weight:

Erros faced:

1.

This error is due to missing a bracket we fix it by adding a bracket.

2.

this is not actually an error but a warning showing directory already exists this is because when we run program more then one time we are trying to create the directory again whic already exists. To fix it we can delete the directory that was created by first run.

references:

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

2.https://in.mathworks.com/matlabcentral/answers/344409-saving-images-onto-a-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.