File: savstate.rst

package info (click to toggle)
neuron 8.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,760 kB
  • sloc: cpp: 149,571; python: 58,465; ansic: 50,329; sh: 3,510; xml: 213; pascal: 51; makefile: 35; sed: 5
file content (225 lines) | stat: -rwxr-xr-x 6,927 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
.. _savstate:

SaveState
---------



.. class:: SaveState

    The state includes :data:`t`, the voltage for all segments of all sections, 
    and all the STATEs defined in all the membrane and point process 
    mechanisms. With regard to model descriptions, it does not include 
    PARAMETERs, ASSIGNED variables. 
    It always includes 
    values for the ambiguous variable of ions such as 
    cai, ko, or ena. This can be an expensive object in terms of memory 
    storage. 
     
    The state also includes all the outstanding events (external and self) 
    and the weight vectors of all NetCon objects. For model descriptions 
    containing a NET_RECEIVE block, all the ASSIGNED variables are also included 
    in the state (this is because such models often use such variables to 
    store logic state and other values, such as the last event time t0, 
    needed to compute state variables at the next event.) 
     
    The outstanding event delivery times are absolute. 
    When restored, all outstanding 
    events will be cleared and the restored event times and NetCon info 
    will take their place. Note that it is not in general possible to 
    change the value of t in a network simulation since most NET_RECEIVE 
    blocks keep t0 (the last event time) as part of their state. 

    Example:

        .. code:: python

            from neuron import h, rxd
            from neuron.units import mV, ms
            h.load_file("stdrun.hoc")

            soma = h.Section(name="soma")
            soma.insert(h.hh)
            soma.nseg = 51
            cyt = rxd.Region(h.allsec(), name="cyt")
            c = rxd.Species(cyt, name="c", d=1, initial=lambda node: 1 if node.x < 0.5 else 0)
            c2 = rxd.Species(
                cyt, name="c2", d=0.6, initial=lambda node: 1 if node.x > 0.5 else 0
            )
            r = rxd.Rate(c, -c * (1 - c) * (0.3 - c))
            r2 = rxd.Reaction(c + c2 > c2, 1)

            h.finitialize(-65 * mV)
            soma(0).v = -30 * mV

            h.continuerun(5 * ms)

            def get_state():
                return (
                    soma(0.5).v,
                    c.nodes(soma(0.5)).concentration[0],
                    c2.nodes(soma(0.5)).concentration[0],
                )

            s1 = get_state()   # our local copy, just to prove we saved
            s = h.SaveState()
            s.save()

            # NOTE: calling s.save() stores the state to the s object; it does not
            # store the state to a file; use s.fwrite(file_obj) for that and 
            # s.fread(file_obj) to read state from a file before restoring.

            h.continuerun(10 * ms)

            # go back to the way things were at 5 * ms (when we called s.save())
            s.restore()

            # prove we successfully reverted
            assert get_state() == s1
            assert get_state() != s2


    .. versionchanged:: 8.1

        Prior to NEURON 8.1, :class:`SaveState` did not save 
        reaction-diffusion states.


    .. warning::
        The intention is that a save followed by 
        any number of simulation-continue,restore 
        pairs will give the same simulation result (assuming the simulation 
        is deterministic). Given the possibility that simulations can 
        be written to depend on a variety of computer states not saved in this 
        object, this is more an experimental question than an assertion. 
         
        Between a save and a restore, 
        it is important not to create or delete sections, NetCon objects, 
        or point processes. Do not 
        change the number of segments, insert or delete mechanisms, 
        or change the location of point processes. 
         
        Does work with the local variable timestep method if the stdrun system 
        is used since continuerun() uses cvode.solve(tstop) to integrate and 
        this returns with all states at tstop. However, if you advance using 
        fadvance() calls different cells will be at different t values in 
        general and SaveState will be useless. 

         

----



.. method:: SaveState.save


    Syntax:
        ``.save()``


    Description:
        t, voltage, state and event values are stored in the object. 

         

----



.. method:: SaveState.restore


    Syntax:
        ``.restore()``

        ``.restore(1)``


    Description:
        t, voltage, state  and event values are put back in the sections. 
        Between a save and a restore, 
        it is important not to create or delete sections, change 
        the number of segments, insert or delete mechanisms, 
        or change the location or number of point processes. 
        Before restoring states, the object checks for consistency 
        between its own data structure and the section structures. 
         
        If the arg is 1, then the event queue is not cleared and no saved events are 
        put back on the queue. Therefore any Vector.play and/or FInitializeHandler 
        events on the queue after finitialize() are not disturbed. 

         

----



.. method:: SaveState.fread


    Syntax:
        ``.fread(File)``

        ``.fread(File, close)``


    Description:
        Reads binary state data from a File object into the 
        SaveState object. (See File in ivochelp). This does 
        not change the state of the sections. (That is done with 
        \ ``.restore()``). This function opens the file defined 
        by the File object. On return the file is closed unless 
        the second arg exists and is 1. 
         
        Warning: file format depends on what 
        mechanisms are available in the executable and the order 
        that sections are created (and mechanisms inserted) 
        by the user. Also the order of NetCon, ArtificialCell, 
        PointProcess creation and just about everything else that 
        gets saved in the file. I.e. if you change your simulation 
        setup, old files may become incompatible. 
         
        In a parallel simulation, each host 
        :meth:`ParallelContext.id` , should 
        write an id specific file. Note that the set of files is 
        at least :meth:`ParallelContext.nhost` specific. 

         

----



.. method:: SaveState.fwrite


    Syntax:
        ``.fwrite(File)``


    Description:
        Opens the file defined by the *File* object, writes saved 
        binary state data to the beginning of the file. 
        On return the file is closed unless the second arg exists 
        and is 1. In that case, extra computer state information 
        may be written to the file, e.g. :meth:`Random.seq`.

         

----



.. method:: SaveState.writehoc


    Syntax:
        ``.writehoc(File)``


    Description:
        Writes saved state data as sequence of hoc statements that 
        can be read with \ ``xopen(...)``. Not implemented at this time.