Debian Codegraph @

docs/other.rst

raw
1
2 Other useful functionality
3 ==========================
4
5 .. _sec:convert:
6
7 Conversion of binary data files to ascii
8 -----------------------------------------
9
10 splash has a command line feature which can be used to convert binary
11 SPH dump files into ascii format. The syntax is
12
13 ::
14
15 splash to ascii dump001 dump002 dump???
16
17 which will convert all of the dump files listed on the command line into
18 ascii format (called ``dump001.ascii``, ``dump002.ascii`` etc.), with
19 columns as would be listed in the main menu if you opened the dump file
20 in splash . Note that the output *includes* calculated extra quantities
21 such as the radius if these have been turned on [in the d) menu] and the
22 settings saved to the ``splash.defaults`` file. Similarly the data will
23 be output in physical units if a ``splash.units`` file is present.
24
25 For other command line options, see :ref:`sec:commandline`.
26
27 Conversion of binary data files to csv
28 -----------------------------------------
29
30 To export data in csv format, just use:
31
32 ::
33
34 splash to csv dump001 dump002 dump???
35
36 which will convert all of the files listed on the command line into
37 csv format (called ``dump001.csv``, ``dump002.csv`` etc.), with
38 columns as would be listed in the main menu if you opened the dump file
39 in splash.
40
41 See also :ref:`sec:commandline`.
42
43 .. _sec:converttogrid:
44
45 Interpolation of SPH data to 2D and 3D grids
46 ---------------------------------------------
47
48 splash has a command line feature which can be used to read binary SPH
49 dump files and output 3D gridded data in a variety of formats. The
50 syntax is
51
52 ::
53
54 splash to grid dump001 dump002 dump???
55
56 which will interpolate the density, velocity (if present) and magnetic
57 field (if present) onto a 3D grid and output the results to files (the
58 default output format is ascii, with one file for each quantity
59 interpolated). Other data columns in the SPH file can be interpolated
60 using the “allto” option, which interpolates *all* of the columns to the
61 grid:
62
63 ::
64
65 splash allto grid dump001 dump002 dump???
66
67 The grid interpolation uses the :math:`x`, :math:`y`, and :math:`z`
68 limits — as saved to the ``splash.limits`` file — for the box, and the
69 grid size is given by the “set number of pixels” option in the r)ender
70 menu — as saved to the ``splash.defaults`` file. Automatic pixel
71 determination also works (if npixels = 0) but there is a sensible upper
72 limit placed on the grid size determined in this manner to avoid
73 ridiculous memory/disk usage. Options can be set to change
74 various aspects of the grid interpolation behaviour. For example you to
75 use periodic boundary conditions use::
76
77 splash to grid dump001 --periodic
78
79 or alternatively you can set this as an environment variable::
80
81 export SPLASH_TO_GRID_PERIODIC=yes
82 splash to grid dump001
83
84 To change the number of pixels, you can use::
85
86 splash to grid dump001 --periodic --npix=100,100,000
87
88 and to interpolate only columns 4 and 5 you can use::
89
90 splash to grid dump001 --periodic --npix=100,100,000 --grid=4,5
91
92 For all possible output formats, use ``splash --help`` or see the full
93 list of command line options in :ref:`sec:commandline`.
94
95 Reading 3D cubes or grids generated by splash to grid into Python
96 ------------------------------------------------------------------
97
98 ascii format
99 ~~~~~~~~~~~~~
100 The default output of splash to grid is a simple ascii file per quantity.
101 The header of this file explains the format::
102
103 # SPLASH: A visualisation tool for SPH data (c)2004-2023 Daniel Price and contributors
104 # snap_012_density_grid.dat produced using "splash to grid" on file snap_012
105 #
106 # time:
107 # 4.7619100E-02
108 #
109 # xmin,xmax,ymin,ymax,zmin,zmax:
110 # -5.000000E+02 5.0000000E+02 -5.0000000E+02 5.0000000E+02 -5.0000000E+02 5.0000000E+02
111 #
112 # file contains:
113 # density interpolated to 3D cartesian x,y,z grid
114 #
115 # written in the form:
116 # do k=1,nz
117 # do j=1,ny
118 # write(*,*) (dat(i,j,k),i=1,nx)
119 # enddo
120 # enddo
121 #
122 # grid dimensions:
123 # nx ny nz
124 512 512 512
125 2.435691E-08 2.478849E-08 2.522732E-08 2.567299E-08 2.612505E-08 2.658309E-08 2.704713E-08 2.75
126
127 To read this file into Python you can use np.loadtxt with a reshape command::
128
129 import numpy as np
130 arraytmp = np.loadtxt('snap_012_density_grid.dat',skiprows=22)
131 array = arraytmp.reshape((512,512,512))
132
133 binary format
134 ~~~~~~~~~~~~~
135 Reading and writing pure text files can be slow. For this reason splash provides a variety
136 of binary output formats for gridded data::
137
138 Grid conversion mode ("splash to X dumpfiles"):
139 splash to grid : interpolate basic SPH data (density, plus velocity if present in data)
140 to 2D or 3D grid, write grid data to file (using default output=ascii)
141 to gridascii : as above, grid data written in ascii format
142 to gridascii2 : grid data written in ascii format, all in one file
143 to gridbinary : as above, grid data in simple unformatted binary format:
144 write(unit) nx,ny,nz,ncolumns,time,xmin,xmax,ymin,ymax,zmin,zmax [ 4x4,7x8 bytes ]
145 write(unit) (((rho(i,j,k),i=1,nx),j=1,ny),k=1,nz) [ 8 bytes each ]
146 write(unit) (((vx(i,j,k), i=1,nx),j=1,ny),k=1,nz) [ 8 bytes each ]
147 write(unit) (((vy(i,j,k), i=1,nx),j=1,ny),k=1,nz) [ 8 bytes each ]
148 write(unit) (((...(i,j,k),i=1,nx),j=1,ny),k=1,nz) [ 8 bytes each ]
149 to gridstream : grid data in byte-stream binary format (e.g. for python):
150 nx,ny,nz,ncolumns,time,xmin,xmax,ymin,ymax,zmin,zmax,rho [ 4,4,4,4,8*7,8*nx*ny*nz ]
151
152 For reading in python the most useful is the "gridstream" format::
153
154 splash to gridstream snap_012 --npix=512,512,512
155
156 which produces a file called::
157
158 ----> WRITING TO snap_012.gridstream on unit 89 (unformatted binary stream)
159
160 we can then read this file into Python as follows::
161
162 import numpy as np
163 with open("snap_012.gridstream", "rb") as file:
164 nx, ny, nz, ncolumns = np.fromfile(file, dtype=np.int32, count=4)
165 time, xmin, xmax, ymin, ymax, zmin, zmax = np.fromfile(file, dtype=np.float64, count=7)
166 rho = np.fromfile(file, dtype=np.float64, count=nx * ny * nz)
167 rho_array = rho.reshape((nx, ny, nz))
168
169
170 .. _sec:splashcalc:
171
172 Using splash to calculate global quantities as a function of time.
173 ------------------------------------------------------------------
174
175 splash has a command line feature that can be used to calculate global
176 quantities on the particles as a function of time, for example kinetic,
177 thermal, magnetic and total energy, total linear and angular momentum.
178 An example to calculate the energies in a sequence of dump files is:
179
180 ::
181
182 splash calc energies dump001 dump002 dump???
183
184 Other options are given by typing ’splash calc’, which currently has the
185 following options:
186
187 ::
188
189 splash calc energies : calculate KE,PE,total energy vs time
190 output to file called 'energy.out'
191 calc massaboverho : mass above a series of density thresholds vs time
192 output to file called 'massaboverho.out'
193 calc extinction : column density to all sink particles vs time
194 output to file called 'extinction.out'
195 calc max : maximum of each column vs. time
196 output to file called 'maxvals.out'
197 calc min : minimum of each column vs. time
198 output to file called 'minvals.out'
199 calc diff : (max - min) of each column vs. time
200 output to file called 'diffvals.out'
201 calc amp : 0.5*(max - min) of each column vs. time
202 output to file called 'ampvals.out'
203 calc delta : 0.5*(max - min)/mean of each column vs. time
204 output to file called 'deltavals.out'
205 calc mean : mean of each column vs. time
206 output to file called 'meanvals.out'
207 calc rms : (mass weighted) root mean square of each column vs. time
208 output to file called 'rmsvals.out'
209 calc tracks : track particle data vs time for selected particles,
210 --track=1,2,3 output to tracks-1.out,tracks-2.out,tracks-3.out
211 calc timeaverage : time average of *all* entries for every particle
212 output to file called 'time_average.out'
213 calc ratio : ratio of *all* entries in each file compared to first
214 output to file called 'ratio.out'
215
216 For the ``energies`` and ``massaboverho`` options to be successful, splash
217 must be aware of the locations of the corresponding columns in the data
218 (i.e., by the column identification given in the set_labels routine
219 corresponding to the data read). For the ``massaboverho`` option an input
220 file is required specifying the density thresholds (a default version is
221 written if the appropriate file is not already present).
222
223 Using splash to calculate line-of-sight extinction to sink particles
224 ---------------------------------------------------------------------
225
226 For observations one is often interested in how much obscuring material
227 is present in front of stars in a model. In splash stars are usually represented
228 as sink particles, so there is a simple command line function to compute
229 the column density of material between the observer and each "star".
230
231 To compute this, use::
232
233 splash calc extinction dump_0*
234
235 which will produce something like::
236
237 -----> CALCULATING EXTINCTION, TIME= 8.80E+02 yrs FILE # 20
238
239 projecting from particles to points...
240
241 Sigma to sink 1 = 1.63E-07 [g/cm^2]
242 Sigma to sink 2 = 1.19E-03 [g/cm^2]
243 Sigma to sink 3 = 2.18E-06 [g/cm^2]
244
245 and write the results to a file called "extinction.out". Note that
246 the observer is by default in the +z direction, but can be changed
247 by setting the viewing angles on the command line or in the splash.defaults file::
248
249 splash calc extinction dump_0* --anglex=30. --angley=60. --anglez=120.
250
251 Similarly physical unit scaling will be applied based on what is saved in the
252 splash.defaults and splash.units files. For example, to compute everything in
253 code units you can just use the --code flag::
254
255 splash calc extinction dump_0* --code
256
257
258 Using splash to time average a series of files
259 ----------------------------------------------
260
261 The ``splash calc timeaverage`` command line option (see
262 :ref:`sec:splashcalc`) can be used to produce a time average of a
263 series of files from any splash-readable format. This computes the
264 time-average of every individual entry in the file as represented in
265 splash as a table of rows (or ‘particles’) and columns (or ‘quantities
266 defined on particles’). The output is an ascii file with the same rows
267 and columns, averaged over all the snapshots on the command line. The
268 number of columns is doubled in the output, giving the standard
269 deviation for each quantity in the corresponding column (e.g., the
270 standard deviation for column 1 is output in column :math:`N + 1`).
271
272 Examples of how this could be use might be to produce the time-averaged
273 power spectrum from a series of ascii files containing power spectra for
274 individual output times, or the time averaged probability density
275 function (PDF) from PDFs produced by splash (see :ref:`sec:pdfs:`).
276
277 The resulting ascii file, called ``time_average.out`` can be plotted
278 using the ascii splash binary (asplash).
279
280 For other command line options, see :ref:`sec:commandline`.
281
282 .. _sec:batchmode:
283
284 Reading/processing data into images without having to answer prompts
285 --------------------------------------------------------------------
286
287 splash can be invoked with plot options on the command line. For example, use
288
289 ::
290
291 splash myrun* -x 1 -y 2 -render 8 -dev /png
292
293 or simply
294
295 ::
296
297 splash myrun* -r 8 -dev /png
298
299 which will assume sensible default values (2 and 1 respectively) for the
300 y and x axes. Similarly a vector plot can be specified with ``-vec`` and
301 a contour plot with ``-cont``. The full list of command-line flags is
302 given in :ref:`sec:commandline`.
303
304 If plotting options have been only partially specified on the command
305 line, no prompts will appear for the remaining options and default values will be assumed
306 for the remaining options. For example, the default device will be /xw giving an interactive plot.
307
308 .. _sec:pdfs:
309
310 Computing volume-weighted probability density functions from SPH data using SPLASH
311 -----------------------------------------------------------------------------------
312 The best way to compute a volume-weighted probability density function
313 on SPH particles is to interpolate the density field to a grid and compute
314 the histogram of the number of grid cells containing a given value of the desired quantity.
315
316 The grid2pdf utility included with splash can be used to compute the density PDF
317 from gridded data output by the ``splash to grid`` utility (see :ref:`sec:converttogrid`).
318
319 To use this feature, you will need to output grids in "binary" format, e.g::
320
321 splash to gridbinary turb_00020
322
323 or if you want to skip the velocity interpolation (assuming density in column 6)::
324
325 splash to gridbinary turb_00020 --grid=6
326
327 this produces a file called turb_00020.grid, then follow this with::
328
329 cd $SPLASH_DIR; make grid2pdf
330 cd -
331 $SPLASH_DIR/splash/bin/grid2pdf turb_00020.grid
332
333 which produces::
334
335 turb_00020.grid_pdf_ln_density.dat
336
337 this is just a two-column ascii file, so you can then plot this with your favourite plotting tool, e.g.::
338
339 splash -ev turb_00020.grid_pdf_ln_density.dat
340
341
342 Using special characters in the plot labels
343 -------------------------------------------
344
345 Several of the examples shown in this manual use special characters
346 (such as the :math:`\int` character) in the plot labels. In giza these
347 can be specified using TeX-like escape sequences, or with the escape
348 sequences used in pgplot. For example to plot the greek letter
349 :math:`\rho` we would use
350
351 ::
352
353 label = 'this would print the greek letter \rho'
354
355 or, in pgplot-style:
356
357 ::
358
359 label = 'this would print the greek letter \gr'
360
361 where ``\gr`` is the pgplot escape sequence for :math:`\rho`.
362
363 In giza , which uses real fonts rather than the bitmapped characters
364 used in pgplot, special characters are implemented with unicode
365 characters. Thus, you need to select a font that has the appropriate
366 characters included. The font can be changed using the ``GIZA_FONT``
367 environment variable.
368
369 For other characters the procedure is similar. For example for the
370 integral
371
372 .. math::
373
374 \int v_x \mathrm{dx}
375
376 we would use the TeX-like expression
377
378 ::
379
380 label = '\int v_x dx'
381
382 or equivalently, in pgplot-style
383
384 ::
385
386 label = '\(2268) v\d x \u dx'
387
388 where ``\(2268)`` is the pgplot escape sequence for the integral sign.
389 The ``\d`` indicates that what follows should be printed as subscript
390 and ``\u`` correspondingly indicates a return to normal script (or from
391 normal script to superscript). All of the escape sequences for special
392 characters are listed in the appendix to the pgplot user guide.
393
394 WARNING: Note that the use of escape characters can be compiler
395 dependent and may not therefore work on all compilers (for example
396 the intel compiler needs the -nbs flag).
397
398 .. _sec:writepixmap:
399
400 Outputting the raw pixel map to a file
401 --------------------------------------
402
403 The actual pixel map rendered to the graphics device (i.e., when a
404 quantity is rendered to pixels, not for particle plots) can be output
405 directly to a file, or series of files by using the ``-o`` command line
406 option when you invoke splash. This is useful if you need to compare the
407 image to the output from another code (e.g. using a different
408 visualisation tool) or if you wish to have a “raw” rendering, that is
409 without annotation on the plots.
410
411 Invoking splash with ``-o`` lists the currently implemented formats::
412
413 possible formats for -o option:
414 -o ppm : dump pixel map to portable pixel map file
415 -o pfm : dump pixel map to portable float map file
416 -o ascii : dump pixel map to ascii file
417
418 For example, to output the pixel map in ascii format, use::
419
420 splash discG_00300 -o ascii -r 6 -dev /png
421
422 giving::
423
424 > writing pixel map to file discG_00300_columndensitygcm2_proj.pix ...OK
425
426 This produces a file as follows::
427
428 $ more discG_00300_columndensitygcm2_proj.pix
429 # discG_00300_columndensitygcm2_proj.pix created by SPLASH
430 # Contains 2D pixel array 512 x 512 written as
431 # do j=1,512
432 # write(*,*) dat(1:512,j)
433 # enddo
434 # column density [g/cm^2]: min = 9.697428E-12 max = 7.487661E+03
435 # x axis: min = -4.000000E+03 max = 4.000000E+03
436 # y axis: min = -4.000000E+03 max = 4.000000E+03
437 # 512 512
438 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+0
439 ...
440
441 The number of pixels in the image can be controlled using the 'set number of pixels' option in the :ref:`sec:menu-r` (making sure you save the settings to the splash.defaults file using the :ref:`sec:menu-s`).
442
443 .. _sec:readpixmap:
444
445 Reading raw pixel maps from splash into Python
446 ----------------------------------------------
447
448 See above for how to output the raw pixel map to a file. The resulting .pix file can be read into Python using the command::
449
450 array = np.loadtxt('discG_00300_columndensitygcm2_proj.pix',skiprows=9)
451 print (array.shape)
452 plt.imshow(img)
453
454 A slightly more advanced script that also reads the x and y limits from the .pix file is provided in `splash/scripts/plot_pix.py <https://github.com/danieljprice/splash/blob/master/scripts/plot_pix.py>`_::
455
456 python plot_pix.py discG_00300_columndensitygcm2_proj.pix
457
458 For other command line options, see :ref:`sec:commandline`.