@@ -47,7 +47,7 @@ Let's first wrap the code above into a single class named
47
47
explain in detail the content of this class.
48
48
49
49
.. literalinclude :: scripts/advection_model.py
50
- :lines: 3-32
50
+ :lines: 3-33
51
51
52
52
Process interface
53
53
~~~~~~~~~~~~~~~~~
@@ -94,9 +94,8 @@ methods that will be called during simulation runtime:
94
94
simulation. Here it is used to set the x-coordinate values of the
95
95
grid and the initial values of ``u `` along the grid (Gaussian
96
96
pulse).
97
- - ``.run_step() `` will be called at each time step iteration and have
98
- the current time step duration as required argument. This is where
99
- the Lax method is implemented.
97
+ - ``.run_step() `` will be called at each time step iteration. This is
98
+ where the Lax method is implemented.
100
99
- ``.finalize_step() `` will be called at each time step iteration too
101
100
but after having called ``run_step `` for all other processes (if
102
101
any). Its intended use is mainly to ensure that state variables like
@@ -106,6 +105,12 @@ A fourth method ``.finalize()`` could also be implemented, but it is
106
105
not needed in this case. This method is called once at the end of the
107
106
simulation, e.g., for some clean-up.
108
107
108
+ Each of these methods can be decorated with :func: `~xsimlab.runtime `
109
+ to pass some useful information during simulation runtime (e.g.,
110
+ current time step number, current time or time step duration), which
111
+ may be need for the computation. Without this decorator, runtime
112
+ methods must have no other parameter than ``self ``.
113
+
109
114
Getting / setting variable values
110
115
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111
116
@@ -133,7 +138,7 @@ need to provide a dictionary with the process class(es) that we want
133
138
to include in the model, e.g., with only the process created above:
134
139
135
140
.. literalinclude :: scripts/advection_model.py
136
- :lines: 35
141
+ :lines: 36
137
142
138
143
That's it! Now we have different tools already available to inspect
139
144
the model (see section :doc: `inspect_model `). We can also use that
@@ -168,15 +173,15 @@ This process declares all grid-related variables and computes
168
173
x-coordinate values.
169
174
170
175
.. literalinclude :: scripts/advection_model.py
171
- :lines: 38-47
176
+ :lines: 39-48
172
177
173
178
Grid x-coordinate values only need to be set once at the beginning of
174
179
the simulation ; there is no need to implement ``.run_step() `` here.
175
180
176
181
**ProfileU **
177
182
178
183
.. literalinclude :: scripts/advection_model.py
179
- :lines: 50-62
184
+ :lines: 51-63
180
185
181
186
``u_vars `` is declared as a :func: `~xsimlab.group ` variable, i.e., an
182
187
iterable of all variables declared elsewhere that belong the same
@@ -191,7 +196,7 @@ value from elsewhere.
191
196
**AdvectionLax **
192
197
193
198
.. literalinclude :: scripts/advection_model.py
194
- :lines: 65-83
199
+ :lines: 66-85
195
200
196
201
``u_advected `` represents the effect of advection on the evolution of
197
202
:math: `u` and therefore belongs to the group 'u_vars'.
@@ -207,7 +212,7 @@ handle them like if these were the original variables. For example,
207
212
**InitUGauss **
208
213
209
214
.. literalinclude :: scripts/advection_model.py
210
- :lines: 86-96
215
+ :lines: 88-98
211
216
212
217
A foreign variable can also be used to set values for variables that
213
218
are declared in other processes, as for ``u `` here with
@@ -218,7 +223,7 @@ are declared in other processes, as for ``u`` here with
218
223
We now have all the building blocks to create a more flexible model:
219
224
220
225
.. literalinclude :: scripts/advection_model.py
221
- :lines: 99-102
226
+ :lines: 101-104
222
227
223
228
The order in which processes are given doesn't matter (it is a
224
229
dictionary). A computationally consistent order, as well as model
@@ -243,7 +248,7 @@ original, simple version.
243
248
For this we create a new process:
244
249
245
250
.. literalinclude :: scripts/advection_model.py
246
- :lines: 105-130
251
+ :lines: 107-133
247
252
248
253
Some comments about this class:
249
254
@@ -263,13 +268,13 @@ profile instead of a Gaussian pulse. We create another (minimal)
263
268
process for that:
264
269
265
270
.. literalinclude :: scripts/advection_model.py
266
- :lines: 133-141
271
+ :lines: 136-144
267
272
268
273
Using one command, we can then update the model with these new
269
274
features:
270
275
271
276
.. literalinclude :: scripts/advection_model.py
272
- :lines: 144-145
277
+ :lines: 147-148
273
278
274
279
Compared to ``model2 ``, this new ``model3 `` have a new process named
275
280
'source' and a replaced process 'init'.
@@ -280,7 +285,7 @@ It is also possible to create new models by removing one or more
280
285
processes from existing Model instances, e.g.,
281
286
282
287
.. literalinclude :: scripts/advection_model.py
283
- :lines: 148
288
+ :lines: 151
284
289
285
290
In this latter case, users will have to provide initial values of
286
291
:math: `u` along the grid directly as an input array.
@@ -304,28 +309,28 @@ achieve this is to create a small new process class that sets
304
309
the values of ``spacing `` and ``length ``:
305
310
306
311
.. literalinclude :: scripts/advection_model.py
307
- :lines: 151-158
312
+ :lines: 154-161
308
313
309
314
However, one drawback of this "additive" approach is that the number
310
315
of processes in a model might become unnecessarily high:
311
316
312
317
.. literalinclude :: scripts/advection_model.py
313
- :lines: 161-161
318
+ :lines: 164
314
319
315
320
Alternatively, it is possible to write a process class that inherits
316
321
from ``UniformGrid1D ``, in which we can re-declare variables *and/or *
317
322
re-define "runtime" methods:
318
323
319
324
.. literalinclude :: scripts/advection_model.py
320
- :lines: 164-172
325
+ :lines: 167-175
321
326
322
327
We can here directly update the model and replace the original process
323
328
``UniformGrid1D `` by the inherited class ``FixedGrid ``. Foreign
324
329
variables that refer to ``UniformGrid1D `` will still correctly point
325
330
to the ``grid `` process in the updated model:
326
331
327
332
.. literalinclude :: scripts/advection_model.py
328
- :lines: 175-175
333
+ :lines: 178
329
334
330
335
.. warning ::
331
336
0 commit comments