Storing Data From C++ Program to a file.

Click For Summary

Discussion Overview

The discussion revolves around the implementation of data storage in a C++ program using the Verlet algorithm to model satellite motion. Participants explore how to output position and velocity data at each timestep for later analysis in MATLAB, addressing issues related to file handling and data management.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach to writing data to a file at each timestep but expresses uncertainty about the file's creation location and the ability to append new data without overwriting previous entries.
  • Another participant clarifies that the file will be created in the current working directory, which may differ from the source or executable directory, depending on the operating system and configuration.
  • A different participant critiques the original approach, noting that repeatedly opening and closing the file is inefficient and suggests opening the file once at the start, writing data less frequently to improve performance.
  • This participant also emphasizes the need for careful consideration of timestep size when using the Verlet method, warning against excessive data points that could arise from recording every timestep.
  • They propose recording data every ten or hundred integration steps instead, which could still provide sufficient information for analysis while enhancing program efficiency.
  • Another participant reports successfully modifying their approach to open the file in append mode, allowing for the storage of time, speed, and altitude in a single file formatted for MATLAB.

Areas of Agreement / Disagreement

Participants generally agree on the inefficiencies of the original data storage method and the importance of managing file operations effectively. However, there remains no consensus on the optimal frequency for data recording or the best practices for error handling in file operations.

Contextual Notes

Limitations include potential misunderstandings about file paths and the implications of different operating systems on file creation. The discussion also highlights unresolved considerations regarding the balance between data accuracy and computational efficiency when using the Verlet algorithm.

engineer_ja
Messages
15
Reaction score
0
I am using a verlet algorithm in c++ to model the motion of a satellite, and want to output position and velocity data from each timestep so that I can then read it into MATLAB and plot graphs to see how well my model matches the 'real life' graphs. so far I have at the end of each iteration:

Code:
    ofstream fout; bool ok;
    
    ok = false;
    do{
        fout.clear();
        fout.open("height_data.dat");
        if (fout.good()) ok = true;
    }while (!ok);
    
    fout<<height<<endl; //height is the variable with current position to output and store
    
    fout.close();
As far as I have been taught, this should create the file height_data.dat in the same location as the c++program, but it doesn't.
I am also unsure about adding new data at each timestep without erasing previous data, and wondered if it is possible to store time, height and velocity all in one file to use with MATLAB?

Any hints on if and how I can achieve this would be greatly appreciated.
Thanks in advance
:)
 
Last edited by a moderator:
Technology news on Phys.org
engineer_ja said:
As far as I have been taught, this should create the file height_data.dat in the same location as the c++program

More like it should create a file in a current directory - whichever it is for your program. Doesn't have to be neither directory where the source is nor where the executable is. Details will depend on OS and configuration.
 
engineer_ja said:
I am using a verlet algorithm in c++ to model the motion of a satellite, and want to output position and velocity data from each timestep so that I can then read it into MATLAB and plot graphs to see how well my model matches the 'real life' graphs. so far I have at the end of each iteration:

Code:
    ofstream fout; bool ok;
    
    ok = false;
    do{
        fout.clear();
        fout.open("height_data.dat");
        if (fout.good()) ok = true;
    }while (!ok);
    
    fout<<height<<endl; //height is the variable with current position to output and store
    
    fout.close();

You don't want to do this for a number of reasons.

One problem is the call to open. The version of the open call you are using will blow away the previous version of the file. You will get a file containing one entry for the very last step. This can be fixed by opening the file in append mode, but that isn't the right solution.

Opening and closing files are very expensive operations. Formatted output is also expensive, but not near as expensive as opening and closing. Doing this every time step turns your fast but inaccurate verlet method into a ridiculously slow (but still inaccurate) technique. What you should be doing instead is to open the file at the start of your run, writing to it occasionally (more on this later), and closing it when finished.

How often should you record? Not every step, particularly so for something like verlet. Verlet is a second order technique. To get anything close to reasonable accuracy you need to take very, very small time steps with verlet. If, for example, you are simulating an object that is circularly orbiting some other object, you need to have the time steps be such that the object moves about 1/1000 of a degree per step. Make your time steps smaller and you will start running into truncation error problems. Make your time steps bigger and you will start running into limitations of the verlet method itself. If you record every step you will have 360,000 or so points per orbit. You don't need anything close to this number of points. If you instead add an entry to the file for every ten or even 100 integration steps you will still see everything you need to see in a graph, and your program will run a whole lot faster.

Now for some individual snippets:

Code:
    ok = false;
    do{
        fout.clear();
        fout.open("height_data.dat");
        if (fout.good()) ok = true;
    }while (!ok);

Why bother with the loop? If you don't open the file successfully the first time around, what makes you think it will work the second, or third, or thousandth time around? Some errors are fatal. There is no recovery from them. Failing to open a key output file is one of those fatal kinds of errors. There's no point in continuing the run if you can't record the results, and there's not much you can do to recover from the error. You are doing nothing to recover from the error other than retrying the same command over and over (and over and over). It won't work. It's much easier to write an error message to standard output and exit the program if the open call doesn't work.
 
OK, Thanks for your help.

I have managed to locate and change the working directory, so have now found my file.

This is just a one-off data collection function that I'm adding in, so I'm not to worried about it slowing the program down, but have altered it to work every 10 time steps to avoid an excess of data (I also replaced the loop with an error code).

By using the open in append mode, I have successfully stored time, speed and altitude in one file; all three on one line separated at a set width, with the next time on a new line. I'm now going to try and open this as a three column array in Matlab to plot the graphs.

Thanks.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
19
Views
4K
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K