I am trying to write a simple program that will take position and momentum values from 2D vectors and write them to a text log file. The text file will be updated every time new positions and momentum values are determined.
The problem: When trying to write more than one 2D vector to file, the program will fail and the text file will be incomplete, and in my real attempts will have the second vector columns filled with 0's. I think it may be a scope problem but not sure why isn't it writing the second vector, I am pretty sure its not empty, I checked its values before opening to write the text file.
I have provided a simplified code of my real attempt that you may run. I am new to stackoverflow and C++ coding in general. I would be very greatly thankful for your suggestions in approaching this problem. I am using codeblocks compiler on windows 10.
Simplified sample attempt:
#include<iostream>
#include<fstream>
#include<vector>
#include <iomanip>
#include <numeric>
#include <random>
using namespace std;
int main()
{
double variance = 1.0;
double mean = 0;
normal_distribution<double>dist(mean, variance); //create a Normal number distribution
mt19937 rng; // Mersenne Twister Random number generator
rng.seed(random_device{}()); //initialize with non-deterministic seed
double value;
double answer;
vector<vector<double>> x1;
vector<vector<double>> v2;
vector<double> temp1;
//make some data
for (int i = 0; i < 50; i++) {
vector<double>temp1;
value = dist(rng)*10 ;
temp1.push_back(value);
value = dist(rng)*10 ;
temp1.push_back(value);
value = dist(rng)*10 ;
temp1.push_back(value);
v2.push_back(temp1);
}
for (int i = 0; i < 50; i++) {
vector<double>temp1;
value = dist(rng)*100;
temp1.push_back(value);
value = dist(rng)*100 ;
temp1.push_back(value);
value = dist(rng)*100 ;
temp1.push_back(value);
x1.push_back(temp1);
}
ofstream outputfile;
outputfile.open("trajectory.txt");
if(outputfile.fail())
{
cout << "The file could not be created/opened!\n";
cout << "Possible errors:\n";
cout << "1. The file does not exist.\n";
cout << "2. The path was not found.\n";
exit(1); // just exit
}
else
{
cout<<"The file was created and opened successfully!\n";
cout<<"Writing data..\n";
outputfile <<"===========================================================================" << endl;
outputfile << "Time Step: " << 1 << endl;
outputfile << "x " << " " <<"y "<< " " << "z " << " "<< "vx " << " " << "vy " << " " << "vz "<<endl;
for(int i = 0; i <= 50; i++){
outputfile << std::setprecision(7) << std::fixed;
outputfile << i << setw(2)<< x1[i][0] << setw(2) << " ";
outputfile << x1[i][1] << setw(2) << " ";
outputfile << x1[i][2] << setw(2) << " ";
outputfile << v2[i][0] << setw(2) << " ";
outputfile << v2[i][1] << setw(2) << " ";
outputfile << v2[i][2] << setw(2) << " ";
outputfile << '\n';
}
outputfile.close();
if(outputfile.fail())
{
cout<<"The file could not be closed!\n";
cin >> answer;
exit(1);
}
// test if successful to close the file, do the following...
else
cout<<"The file was closed successfully!\n";
}
return 0;
}
my output: The file was created and opened successfully! Writing data..
Process returned -1073741819 (0xC0000005) execution time : 2.578 s Press any key to continue.
snippet of real code:
cout << "printing positions and momenta to ``trajectory.txt''..." << endl;
//ofstream outputfile2;
outputfile.open("trajectory.txt");
if(outputfile.fail())
{
cout << "The file could not be created/opened!\n";
cout << "Possible errors:\n";
cout << "1. The file does not exist.\n";
cout << "2. The path was not found.\n";
exit(1); // just exit
}
// else, if the file can be opened
else
{
cout<<"The file was created and opened successfully!\n";
cout<<"Writing data..\n";
outputfile <<"===========================================================================" << endl;
outputfile << "Time Step: " << 1 << endl;
outputfile << "x" << setw(2) <<"y"<< setw(2) << "z" << setw(2) << "px" << setw(2) << "py" << setw(2) << "pz"<<endl;
for(int i = 0; i <= atom_positionxyz.size(); i++){
outputfile << std::setprecision(4) << std::fixed;
outputfile << atom_new_positionxyz[i][0] << setw(2) << " ";
outputfile << atom_new_positionxyz[i][1] << setw(2) << " ";
outputfile << atom_new_positionxyz[i][2] << setw(2) << " ";
outputfile << atom_new_momentumxyz[i][0] << setw(2) << " ";
outputfile << atom_new_momentumxyz[i][1] << setw(2) << " ";
outputfile << atom_new_momentumxyz[i][2] << setw(2) << " ";
outputfile << '\n';
}
outputfile.close();
if(outputfile.fail())
{
cout<<"The file could not be closed!\n";
exit(1);
}
// test if successful to close the file, do the following...
else
cout<<"The file was closed successfully!\n";
}
What's most obvious from above is that your vector accessing is going out of bounds. Vector::size() returns the number of elements in the vector. Assuming it to be 50 in your case.
for(int i = 0; i <= atom_positionxyz.size(); i++){
the above code is actually going to access the 51st position on the vector. Which is out of range. The max value of i can be 1 less than size as i is starting from 0.
Changing to below should work out for you.
for(int i = 0; i < atom_positionxyz.size(); i++){
User contributions licensed under CC BY-SA 3.0