You are to create a program to help in the analysis of data obtained from an x-r
ID: 3595678 • Letter: Y
Question
You are to create a program to help in the analysis of data obtained from an x-ray powder diffractometer used to characterize materials. In the diffractometer a sample is exposed to x-rays of a specific wavelength and a detector placed on a goniometer is used to measure the angles where constructive interference of the diffracted beam occurs (called peaks) and the intensity of these peaks. The location of these peaks is dependent on the atomic structure of the material being examined. This relationship is given by Bragg’s law which states
n = 2dsin()
where n is the harmonic of the diffracted beam (n = 1 for these experiments), is the wavelength of the x-ray, d is the interplanar spacing between planes of specific orientation in the material and (theta) is the angle between the x-ray beam and the sample of the material. What is measured the goniometer is actually twice the angle () used Bragg’s law and is referred to as two-theta or 2.
You are to create a program that will read the 2-theta values and the intensities from a file and create a table that will contain the 2-theta values, the intensities, the d values and Q values where Q = 1/(sin())2 . The first line of the data file will contain the sample name which should be stored as a string object (note that this name may contain spaces). The second line will contain the anode (the elemental source of the x-ray) as a 2-character symbol which should also be stored as a string object. You will need to use this symbol to determine the wavelength of the x-ray using the table below. You should assume the format of the 2-character symbol will be an upper case letter followed by a lower case letter and the symbol will be one of those listed in the table below.
Anode Wavelength
Cu 1.54059
Cr 2.28973
Fe 1.93604
Co 1.78900
Mo 0.70932
The 2-theta (first column) and intensity values (second column) will start on the third line and continue on subsequent lines. The intensity values are whole numbers. You should design your program so it will continue to read data until the end of file is reached and count the number of peaks observed.
Your output should be a file that contains the name of the sample, the wavelength and a table containing the 2-theta and intensity values from the file plus the calculated d and Q values.
The wavelength should be output with 6 significant digits, the 2-theta values with 5 significant digits, the d values with 6 significant digits and the Q values with 7 significant digits. The table that you output should contain column headings then the values similar to the example given below. Your output file should have the extension “.txt”
Your program should allow the user to enter the name of the data file, but you do not need to worry about the folder path (assume the data file is in the same folder as your *.cpp file) and the name of the output file to be generated. A data file called “Project2data.dat” has been placed on CANVAS. This file contains different peaks than what is shown in the table below. This file can be opened with Notepad or Wordpad. Do not change the name of this file. Your program should also allow the user to analyze data from another file without rerunning the program.
"For sample Project 2 demo with anode of Cu and wavelength of 1.54059 the values are"
2-theta Intensity d values Q values
16.375 20 5.40889 49.30620
20.845 55 4.25800 30.55609
26.622 100 3.34567 18.86480
33.097 45 2.70443 12.32647
36.520 5 2.45842 10.18587
39.439 21 2.28293 8.783553
40.263 34 2.23809 8.441890
"There were 7 peaks in the file. Would you like to analyze data from another file (y/n)?"
Please do not use functions or arrays as we haven't learned them yet.
The contents of file "Project2data.dat" are as follows.
Sample XBN-2
Cr
12.002 35
13.867 10
19.659 5
23.095 17
24.137 21
27.942 33
30.507 100
31.320 78
34.398 95
36.556 45
39.928 1
41.842 6
42.464 34
44.882 3
46.631 67
47.202 2
49.438 88
51.067 57
51.602 46
53.702 22
55.241 51
57.745 34
59.214 10
59.699 7
61.614 13
63.028 23
63.496 41
65.347 27
66.718 25
67.172 2
68.972 18
70.308 42
72.510 24
73.818 16
74.253 5
75.980 9
77.268 12
77.695 19
80.671 3
81.094 11
82.780 18
84.041 7
86.136 35
87.390 4
87.808 8
89.479 9
90.731 11
91.149 2
92.819 1
94.074 16
94.493 10
96.170 22
97.431 6
99.542 2
100.814 8
101.239 11
102.947 18
104.236 2
104.667 1
106.400 9
107.710 14
108.149 2
109.915 8
111.253 9
113.511 19
114.884 2
115.345 4
117.207 7
118.624 1
121.031 5
122.504 12
123.000 3
125.014 6
126.558 7
129.203 2
130.839 8
131.393 1
133.659 2
135.416 7
136.013 2
Thank You
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
const double PI = 3.14159265358979323;
const double DEGREES_TO_RADIANS = PI / 180;
const double CU_WAVELENGTH = 1.54059;
const double CR_WAVELENGTH = 2.28973;
const double FE_WAVELENGTH = 1.93604;
const double CO_WAVELENGTH = 1.78900;
const double MO_WAVELENGTH = 0.70932;
int main()
{
bool run = true;
while (run)
{
string inputFileName, outputFileName, sampleName, element;
double twoTheta, intensity, wavelength, dValue, qValue;
int numPeaks = 0;
cout << "Please input a file name to import the data from." << endl;
getline(cin, inputFileName);
ifstream fileInputStream(inputFileName.append(".dat"));
cout << "Please input a file name to output the calculated information to." << endl;
getline(cin, outputFileName);
ofstream fileOutputStream(outputFileName.append(".txt"));
fileOutputStream << fixed;
getline(fileInputStream, sampleName);
getline(fileInputStream, element);
if (element == "Cu")
wavelength = CU_WAVELENGTH;
else if (element == "Cr")
wavelength = CR_WAVELENGTH;
else if (element == "Fe")
wavelength = FE_WAVELENGTH;
else if (element == "Co")
wavelength = CO_WAVELENGTH;
else if (element == "Mo")
wavelength = MO_WAVELENGTH;
fileOutputStream << "For sample "" << sampleName << "" with anode of " << element << " and wavelength " << setprecision(5) << wavelength << " the values are:" << endl << endl;
fileOutputStream << " 2-theta Intensity d values Q values" << endl;
cout << fixed;
while (fileInputStream >> twoTheta)
{
fileInputStream >> intensity;
dValue = wavelength / (2 * sin((twoTheta * DEGREES_TO_RADIANS) / 2)); // Calculate d-value, d = wavelength / (2 * sin(theta))
qValue = 1 / pow(sin((twoTheta * DEGREES_TO_RADIANS) / 2), 2); // Calculate Q-value, Q = 1/sin(theta)^2
// These integers are used to maintain the integrity of the significant figures as well as the document format.
int thetaShift = 0, dShift = 0, qShift = 0;
if (twoTheta > 100) thetaShift++;
if (dValue > 10) dShift++;
if (qValue > 10) qShift++;
fileOutputStream << " " << setprecision(4 - thetaShift) << twoTheta << " ";
fileOutputStream << setprecision(0) << intensity << " ";
fileOutputStream << setprecision(5 - dShift) << dValue << " ";
fileOutputStream << setprecision(6 - qShift) << qValue << endl;
numPeaks++;
}
fileInputStream.close();
fileOutputStream.close();
cout << "There were " << numPeaks << " peaks in the file. Would you like to analyze data from another file (y/n)?" << endl;
char input;
cin >> input;
while (input != 'y' && input != 'n')
{
cout << "You have input an invalid selection. Please select again. (y/n)" << endl;
cin >> input;
}
if (input == 'n')
run = false;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.