# Saving data to disk in the middle of a simulation in standalone mode

**URL:** <https://brian.discourse.group/t/saving-data-to-disk-in-the-middle-of-a-simulation-in-standalone-mode/1149>\
**Category:** Support\
**Tags:** cpp\_standalone, brian2cuda, storage\
**Created:** [28 February 2024 11:16 UTC](https://brian.discourse.group/t/saving-data-to-disk-in-the-middle-of-a-simulation-in-standalone-mode/1149 "2024-02-28T11:16:36Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [28 February 2024 12:26 UTC](https://brian.discourse.group/t/saving-data-to-disk-in-the-middle-of-a-simulation-in-standalone-mode/1149/2 "2024-02-28T12:26:36Z")

</div>

Hi @nvar. Indeed, this approach does not work, since all the `run` calls will end up together in a single C++/CUDA file – you cannot run Python code in between. This is a topic that we’ve been discussing for quite a while, unfortunately we still do not have an elegant solution for it (if anyone wants to work on it, I’d be happy to give a few pointers 😊 ). As a minor note: the `StateMonitor` is recording everything with the default time step – quite often this is excessive, e.g. using `dt=1*ms` would directly reduce the necessary memory by 10.

But as a complete solution/workaround, for now, the best approach would be to write your own custom implementation of a recording function in C++, instead of using the `StateMonitor`. Slightly adapted from my answer [here](https://brian.discourse.group/t/real-time-monitor/173/2), you could add the following function definition to your script:

```python
@implementation('cpp','''
// Note that functions always need a return value at the moment
double store_state(int i, double time, double value) {
    static std::ofstream f("/some/path/values.txt"); // opens the file the first time
    f << i << "\t" << time << "\t" << value << "\n";
    return 0.; // unused
}
'''
@check_units(i=1, t=second, value=volt, result=1)
def store_state(i, t, value):
    raise NotImplementedError('Use standalone mode')

```

Instead of adding a `StateMonitor`, your script would call the function like this:

```auto
G.run_regularly("dummy = store_state(i, t, v)", dt=defaultclock.dt)

```

This will call your function at every time step for every neuron, and write the values to a text file.

(I didn’t run the above function, so there might be minor errors)

Does that sound that a possible approach for you?

---

_[View the full topic](https://brian.discourse.group/t/saving-data-to-disk-in-the-middle-of-a-simulation-in-standalone-mode/1149)._
