Project 1 - Parsing NASA thermodynamic data
AIM:
Parsing NASA thermodynamic data.
OBJECTIVES:
- Write a function that extracts the 14 co-efficient and calculates the enthalpy, entropy, and specific heats for all the species in the data file. You will be reading this file NASA thermodynamic data (Tip: use fopen and fgetl). The formulae are shown below.
Here R is the universal gas constant, T is the temperature
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.
- Calculate the molecular weight of each species and display it in the command window.
- Plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
- Save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All of these should be generated automatically.
- The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H, and S plots.
- Please write a detailed report to describe your logic.
TASKS:
- Screenshot of the window where all the folders are saved.
- Plots for o2, N2, and Co2 species (your program should be case insensitive if it is going to take the species name as an input).
THEORY or TOPIC REQUIRED FOR THE STUDY:
File Parsing and function creation for recording the data.
File Parsing:
Parsing in computer languages refers to the syntactic analysis of the input code into its components parts in order to facilitate the writing of compilers and interpreters. ... Parsing a file means reading in a data stream of some sort and building an in-memory model of the semantic content of that data.
Or in other words, file parsing is the technique or process used to read the data from an unknown data file and use it accordingly in Matlab and it also very useful to write the data in any file accordingly.
Function:
It is a way of optimizing your code script and make it more sophisticated, as there is a various function in Matlab which can do the tedious coding in a few lines.
Thus similarly we will be using if and else functional loop to create the data file we need.
GIVEN:
Data file named “THERMO.dat”.
And the equation for the Specific Heat, Entropy, and Enthalpy.
R = universal gas constant;
T = Temperatures coefficients for the higher and lower range.
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.
BODY OF THE CONTENT:
Start the program writing by creating a new script;
Always use the comments in the program for the validation of specific inputs and thus the commands have been used through the program;
- First, we have to write the function code to calculate the enthalpy, entropy, and specific heats for all the species in the data file.
To start, we will use the given equation to create the function with “if” “else” loop and that will give us the list of values for each element.
As you can see we have created the function with function name for higher and lower temperature coefficient range the specific heat, enthalpy, and entropy as the output.
We have also included the ‘T’ for the temperature range along with the ‘global medium temperature’ from the “THERMO,dat”.
In the given R is also mentioned for its value.
Now create a viable with ‘for loop’ for the length of ‘ T’.
Now start the ‘if loop’ by giving the condition as T(nvar) >=global medium temperature.
For the given equation of cp,h & S for high-temperature coefficient.
Now give the ‘else’ condition for cp,h & S to list equation with a lower temperature coefficient.
Now, end the function and all three loops.
Save the file as the function name.
2. Now we will write the function code for the molecular weight for given elements;
To start the program, write the function output equals the function name and input variable.
Now define the variable for given base elements as a string.
After element, create a variable to record the atomic weight of base elements.
And create a variable to assign the boundary condition.
Now, start the ‘for loop’ for the length of function input and again create a ‘for loop’ for the length of atomic weight.
To create and record output discretely use ‘if’ loop and mentioned two variable which has to be compared to get the molecular weight along with the location for same.
Now end the respective loops and again create the ‘if loop’ to record the molecular weight number & end the loops.
In the end, write ‘fprintf’ function to display the number and end the function.
Save the file as the function name.
3. Lastly, we will read and utilize the data given in “THERMO.dat” file to get the elements, cp, S, h, molecular weight along with saving the plots for all the elements in the file.
First, open the file with ‘fopen’ and record it by assigning it a variable.
Now get the header of the file with ‘fgetl’ and assign a variable to it.
Start recording the relevant line with ‘fgetl’
Such line -1, as it contains the global temperature range.
Thus get line-1 and save the global temperature range with respective variables by using ‘fgetl’, ‘strsplit’, and ‘str2double’ command.
Skip the next three lines with ‘fgetl’ as these are comments in the file.
From the next line onwards, each and every line will have the element data in a set of four lines.
And each line has a standard format in a group of four. Thus we can write ‘for loop’ for all the 53 elements in the dat file.
First write a code for first-line for element ‘O’ to record the local temperature range as shown below using ‘fgetl’, ‘strsplit’, ‘strfind’ commands.
After location, them, record them in the number using ‘str2double’ command and assign the variables.
Now assign the range to ‘T’ range with low & high temperature from the local range with a list of numbers to record.
Now record the 2nd line of the high-temperature coefficients with ‘fgetl’, ‘strfind’, and ‘str2double’.
It is very important to locate ‘E’ precisely and count the before and after number for the next number.
As we are using ‘str2double’, it will record the integer and this count with zero will only give us integer not a floating number.
Similarly, write down the code line for the next 2 lines for remaining higher and lower temperature coefficients.
After writing the code for lines; write down the function line for calculation cp,h, and S.
To save the output, create the folder (go to the directory you want to save the output in) and assign the name equal to ‘pwd’ directory.
Now create a new folder with ‘mkdir” command and mention the name of the folder.
Create a new command to guide the directory.
Now write the code for plot and each characteristic property again temperature, give the labels and title and also save the plot as image. (Three plot for cp, h, S against T)
Again guide the directory with ‘cd’ command.
Now write the function line for molecular weight and give a command to have a newline after molecular weight for each element.
And now end the loop.
save the file.
RESULT & EXPLAINATION
Specific heat, Enthalpy, Entropy, Molecular Weight has been calculated and recorded for all the species is data file.
Plots have been plotted and saved as images for each species.
Folders have created to save the data for each species.
Molecular weight is recorded for each species at the command window.
Molecular weight:
Plots for o2, N2, and Co2 species (your program should be case insensitive if it is going to take the species name as an input).
Plots for O2:
Plots for N2:
Plots for CO2:
Save species folder:
ERRORS OR ISSUES
The range was not correct.
Element wise multiplication, division, and power were needed.
Spelling mistakes and the element-wise division was needed.
REFERENCE
https://en.wikipedia.org/wiki/Parsing
https://in.mathworks.com/help/matlab/ref/saveas.html
https://in.mathworks.com/help/matlab/ref/mkdir.html