-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlightcone_animations.py
342 lines (325 loc) · 16 KB
/
lightcone_animations.py
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python
'''
Created on 26 Dec 2020
This is a script to create animations of lightcones in causal sets.
@author: Christoph Minz
@license: BSD 3-Clause
'''
from __future__ import annotations
from typing import List, Tuple, Callable, Dict, Any, Union
from causets import EmbeddedCauset, CausetEvent, spacetimes, shapes
from causets import causetplotting as cplt, colorschemes as cs
import matplotlib as mpl
from matplotlib import pyplot as plt, axes as plta, animation
import numpy as np
import lightcone_animations_coordinates as LAC
# Path to movie encoder:
mpl.rcParams['animation.ffmpeg_path'] = \
r'C:\\Program Files\\FFmpeg\\ffmpeg-v4.3.2\\bin\\ffmpeg.exe'
# ==============================
# Causet and spacetime:
dt: float = 0.02 # time step: 0.002 for video length of about 20s
causets_data: List[Tuple[str, str, List[int], List[List[float]]]] = [
#('de Sitter', *LAC.get_1simplex(1.2, spacetime='de Sitter')),
#('de Sitter', *LAC.get_2simplex(1.2, spacetime='de Sitter')),
#('Minkowski', *LAC.get_1simplex(1.2, spacetime='Minkowski')),
#('Minkowski', *LAC.get_2simplex(1.2, spacetime='Minkowski')),
#('Minkowski', *LAC.get_2simplexFlipped(1.2, spacetime='Minkowski')),
#('Minkowski', *LAC.get_2simplexFlipped2(1.2, spacetime='Minkowski')),
#('Minkowski', *LAC.get_2simplexRotated3(1.2, 1, spacetime='Minkowski')),
#('Minkowski', *LAC.get_2simplexRotated3(1.2, -1, spacetime='Minkowski')),
#('Minkowski', *LAC.get_2simplexRotated5(1.2, 1, spacetime='Minkowski')),
#('Minkowski', *LAC.get_2simplexRotated5(1.2, -1, spacetime='Minkowski')),
('Minkowski', *LAC.get_2simplexRotated5(1.2, 0, spacetime='Minkowski')),
#('Minkowski', *LAC.get_3simplex(1.2, spacetime='Minkowski')),
#('Minkowski', *LAC.get_latticeD2(0.6, spacetime='Minkowski')),
#('Minkowski', *LAC.get_latticeD3_oct(0.6, spacetime='Minkowski')),
#('Minkowski',
# *LAC.get_latticeD3_oct_cut(0.6, spacetime='Minkowski')), # overwrites _oct!
#('Minkowski', *LAC.get_latticeD4_oct(0.6, spacetime='Minkowski')),
#('Minkowski', *LAC.get_latticeD3_hcp(0.45, spacetime='Minkowski')),
#('Minkowski', *LAC.get_latticeD3_fcc(0.45, spacetime='Minkowski')),
#('Minkowski', *LAC.get_latticeD3_rho(0.45, spacetime='Minkowski')),
#('Minkowski', *LAC.get_latticeD3_slab(0.27, spacetime='Minkowski')),
#('Minkowski', *LAC.get_latticeD3_slabpinf(0.27, spacetime='Minkowski')),
#('Minkowski', *LAC.get_latticeD3_slabfinf(0.27, spacetime='Minkowski')),
#('Minkowski', *LAC.get_latticeD3_slabpert(0.27, spacetime='Minkowski')),
#('Schwarzschild', *LAC.get_1simplex(1.2, spacetime='Schwarzschild')),
]
# ==============================
# General parameters:
createThumbnail: bool = True
createVideo: bool = True
output_fps: float = 60.0 # frames per second
output_dpi: float = 100.0 # dots per inch
output_size: List[float] = [19.2, 10.8] # inch
cs.setGlobalColorScheme('UniYork')
backgroundcolor: str = '#22262a'
foregroundcolor = 'white'
thumbnail_format: str = 'png'
thumbnail_fontsize: int = 20
thumbnail_eventcolor: str = foregroundcolor
thumbnail_linkcolor: str = 'cs:orange'
eventlabels: Any = False # {'color': foregroundcolor}
draw_shape: bool = True
t_increase: float = 0.25
shapecolor: str = foregroundcolor
titlefontweight: str = 'bold'
titlefontsize: int = 15
label_t: str = 'time coordinate'
label_s: str = 'space coordinate'
section_t: str = 'Spacetime diagram:'
section_s1: str = 'Space slice with the future fading in:'
section_s2: str = 'Space slice with the past fading out:'
section_fontstyle: str = 'italic'
labelfontweight: str = 'bold'
labelfontsize: int = 13
licencetext: str = '' # 'Copyright by C. Minz\n' + \
# 'BSD 3-Clause License\n' + \
# 'https://github.com/c-minz'
plt.rcParams['grid.color'] = 'gray'
plt.rcParams['grid.linewidth'] = 0.25
conelinewidth: float = 2.0
conealpha: float = 0.75
conealpha_low: float = 0.15
conefade: str = 'linear'
eventcolor: str = 'cs:orange'
eventsize: float = 9.0
linkwidth: float = 4.0
spacetimes.default_samplingsize = 64
shapes.default_samplingsize = 64
M_name: str
name: str
name_inline: str
perm: List[int]
coords: List[List[float]]
for M_name, name, perm, coords in causets_data:
name_inline = name.replace('\n', '')
# ==============================
# Create objects:
M_shape: Union[shapes.CoordinateShape, None] = None # default
if M_name in {'black hole', 'Schwarzschild'}:
M_shape = shapes.CoordinateShape(2, 'cuboid', edges=[2., 2.],
center=[0., 1.])
elif M_name == 'Minkowski':
M_shape = shapes.CoordinateShape(len(coords[0]), 'cube', edge=2.)
C: EmbeddedCauset = EmbeddedCauset(coordinates=coords, spacetime=M_name,
shape=M_shape)
M: spacetimes.Spacetime = C.Spacetime
M_shape = C.Shape
output_file: str = f'{M_name} {name_inline}'
title: str = 'Lightcones of a ' + name_inline + ' embedded in ' + \
str(M.Dim) + '-dimensional ' + M_name + ' spacetime'
C_diagram: List[CausetEvent] = [C.find(i) for i in perm] if perm else []
if createThumbnail and C_diagram:
fig = plt.figure(figsize=output_size, dpi=output_dpi,
facecolor=backgroundcolor)
cplt.plotDiagram([C.find(i) for i in perm], perm,
plotAxes=plt.gca(), labels=eventlabels,
events={'markersize': eventsize,
'markerfacecolor': thumbnail_eventcolor},
links={'linewidth': linkwidth,
'color': thumbnail_linkcolor},
linkgapsize=2 * eventsize,
axislim={'xlim': [-1.1, 1.1], 'ylim': [-1.1, 1.1]})
plt.suptitle(title, x=0.5, y=0.1, va='bottom',
fontsize=thumbnail_fontsize, color=foregroundcolor,
fontweight=titlefontweight)
plt.savefig(output_file + '.' + thumbnail_format,
format=thumbnail_format, dpi=output_dpi,
facecolor=backgroundcolor)
if createVideo:
# ==============================
# Animation parameters:
t_start: float
t_end: float
t_start, t_end = M_shape.Limits(0)
t_start = t_start - t_increase
t_end = t_end + t_increase
t_range: np.ndarray = np.arange(t_start, t_end + dt, dt)
t_len: float = t_end - t_start
t_depth: float = 1.00 if M_name == 'Minkowski' else 1.75
infotext: str = 'Above: Hasse diagram of \n' if perm else '\n'
infotext = infotext + ('a subset of a \n' + name
if name.find('lattice') >= 0
else 'a ' + name + '\n')
infotext = infotext + '\n\n\n\n' + \
'On the right: animated \n' + \
'slice through the past and \n' + \
'future lightcones of the \n' + \
'causal set embedded in \n' + \
f'{M.Dim}-dimensional {M_name} \n' + \
'spacetime.\n\n' + \
'The animated slice is at \n' + \
'time coordinate:\n'
conealpha_STfactor: float
if C.Dim > 3: # for high dimensions
conealpha_STfactor = 0.075
elif M_name == 'Minkowski':
conealpha_STfactor = 0.15
else:
conealpha_STfactor = 0.35
# ==============================
# Create animation:
Pmain_dims: List[int] = [1, 2, 3]
Pmain_conealpha: float = conealpha_STfactor * conealpha
Pmain_proj: Union[str, None] = '3d'
Psub_dims: List[int] = [1, 2]
if C.Dim == 2:
Pmain_dims = [1, 0]
Pmain_proj = None
Pmain_conealpha = conealpha
elif C.Dim == 3:
Pmain_dims = [1, 2, 0]
mpl.rcParams['savefig.facecolor'] = backgroundcolor
mpl.rcParams['axes.edgecolor'] = foregroundcolor
fig = plt.figure(figsize=output_size, dpi=output_dpi,
facecolor=backgroundcolor)
fig.suptitle(title, color=foregroundcolor,
fontsize=titlefontsize, fontweight=titlefontweight)
gs = mpl.gridspec.GridSpec(2, 32, figure=fig,
left=0.02, right=0.98, top=0.92, bottom=0.08)
ax_info = fig.add_subplot(gs[1, 0:5], facecolor=backgroundcolor)
info = mpl.patches.Rectangle((0., 0.), 2., 2., fill=False,
transform=ax_info.transAxes, clip_on=False)
ax_main = fig.add_subplot(gs[:, 5:23], facecolor=backgroundcolor,
projection=Pmain_proj)
ax_main.tick_params(color=foregroundcolor, labelcolor=foregroundcolor)
ax_top = fig.add_subplot(gs[0, -8:], facecolor=backgroundcolor)
ax_top.tick_params(color=foregroundcolor, labelcolor=foregroundcolor)
ax_bot = fig.add_subplot(gs[1, -8:], facecolor=backgroundcolor)
ax_bot.tick_params(color=foregroundcolor, labelcolor=foregroundcolor)
ax_top.set_axis_off()
ax_bot.set_axis_off()
if C_diagram:
cplt.plotDiagram(C_diagram, perm,
plotAxes=fig.add_subplot(gs[0, 0:5], alpha=0.0),
labels=eventlabels, linkgapsize=10.0,
events={'markerfacecolor': eventcolor},
axislim={'xlim': [-1.1, 1.1], 'ylim': [-1.1, 1.1]})
Pmain: Callable[[float], Dict[str, Any]]
if C.Dim > 3:
Pmain = cplt.Plotter(
C, plotAxes=ax_main, dims=Pmain_dims,
pastcones={'linewidth': conelinewidth,
'alpha': conealpha_STfactor * conealpha_low},
futurecones={'linewidth': conelinewidth,
'alpha': Pmain_conealpha},
conetimedepth=t_depth, conetimefade=conefade, timedepth=t_depth,
labels=eventlabels, links={'linewidth': linkwidth},
events={'markersize': eventsize,
'markerfacecolor': eventcolor},
axislim='shape', shape=C.Shape)
else:
Pmain = cplt.Plotter(
C, plotAxes=ax_main, dims=Pmain_dims,
pastcones={'linewidth': conelinewidth,
'alpha': Pmain_conealpha},
futurecones={'linewidth': conelinewidth,
'alpha': Pmain_conealpha},
conetimedepth=t_depth, conetimefade=conefade,
labels=eventlabels, links={'linewidth': linkwidth},
events={'markersize': eventsize,
'markerfacecolor': eventcolor},
axislim='shape', shape=C.Shape)
Ptop: Callable[[float], Dict[str, Any]]
Pbot: Callable[[float], Dict[str, Any]]
if C.Dim > 2:
Ptop = cplt.Plotter(
C, plotAxes=ax_top, dims=Psub_dims,
pastcones={'linewidth': conelinewidth, 'alpha': conealpha_low},
futurecones={'linewidth': conelinewidth, 'alpha': conealpha},
conetimedepth=t_depth, conetimefade=conefade, timedepth=t_depth,
labels=False, links={'linewidth': linkwidth},
events={'markersize': eventsize,
'markerfacecolor': eventcolor},
axislim='shape', shape=C.Shape)
Pbot = cplt.Plotter(
C, plotAxes=ax_bot, dims=Psub_dims,
pastcones={'linewidth': conelinewidth, 'alpha': conealpha},
futurecones={'linewidth': conelinewidth,
'alpha': conealpha_low},
conetimedepth=-t_depth, conetimefade=conefade, timedepth=-t_depth,
labels=False, links={'linewidth': linkwidth},
events={'markersize': eventsize,
'markerfacecolor': eventcolor},
axislim='shape', shape=C.Shape)
def set_axeslabels(ax: plta.Axes, dims: List[int]) -> None:
ax.set_xlabel(label_t if dims[0] == 0 else label_s,
color=foregroundcolor, fontweight=labelfontweight)
ax.set_ylabel(label_t if dims[1] == 0 else label_s,
color=foregroundcolor, fontweight=labelfontweight)
if len(dims) > 2:
ax.set_zlabel(label_t if dims[2] == 0 else label_s,
color=foregroundcolor, fontweight=labelfontweight)
# ==============================
# Run and show progress in console:
progressbar_len: int = 20
i: int = 1 # counter for console progress bar
def animator(t: float) -> None:
global i
ax_info.clear()
ax_info.set_xlim([0.0, 5.0])
ax_info.set_ylim([-2.5, 2.5])
ax_info.text(0.0, 3.5, infotext + ('t = %1.2f' % t),
ha='left', va='top', wrap=True,
color=foregroundcolor, fontsize=labelfontsize,
linespacing=1.5)
ax_info.text(0.0, -2.0, licencetext,
ha='left', va='bottom', wrap=True,
color='gray', fontsize=10,
linespacing=1.5)
ax_info.set_axis_off()
ax_main.clear()
if draw_shape:
if C.Dim > 2:
C.Shape.plot(Pmain_dims, ax_main,
edgecolor=None, color=shapecolor)
else:
C.Shape.plot(Pmain_dims, ax_main,
edgecolor=None, facecolor=shapecolor)
Pmain(t)
set_axeslabels(ax_main, Pmain_dims)
if C.Dim > 3:
ax_main.set_title(section_s1, color=foregroundcolor,
fontstyle=section_fontstyle, fontsize=labelfontsize)
else:
ax_main.set_title(section_t, color=foregroundcolor,
fontstyle=section_fontstyle, fontsize=labelfontsize)
if len(Pmain_dims) > 2:
ax_main_panecolor: Tuple[float, ...] = (0.0, 0.0, 0.0, 0.0)
ax_main.view_init(elev=0.0, azim=t * 360 / t_len)
ax_main.xaxis.set_pane_color(ax_main_panecolor)
ax_main.yaxis.set_pane_color(ax_main_panecolor)
ax_main.zaxis.set_pane_color(ax_main_panecolor)
if C.Dim > 2:
ax_top.clear()
if draw_shape:
C.Shape.plot(Psub_dims, ax_top,
edgecolor=None, facecolor=shapecolor)
Ptop(t)
set_axeslabels(ax_top, Psub_dims)
ax_top.set_title(section_s1, color=foregroundcolor,
fontstyle=section_fontstyle, fontsize=labelfontsize)
ax_bot.clear()
if draw_shape:
C.Shape.plot(Psub_dims, ax_bot,
edgecolor=None, facecolor=shapecolor)
Pbot(t)
set_axeslabels(ax_bot, Psub_dims)
ax_bot.set_title(section_s2, color=foregroundcolor,
fontstyle=section_fontstyle, fontsize=labelfontsize)
fig.set_facecolor(backgroundcolor)
if t - t_start - i * t_len / progressbar_len >= 0:
print('#', end='')
i += 1
print(f'Generating animation ({name_inline}, {M_name}):')
print('0% |' + '_' * progressbar_len + '| 100%\n |', end='')
anim = animation.FuncAnimation(fig, animator, frames=t_range)
animwriter = animation.FFMpegWriter(fps=output_fps)
anim.save(output_file + '.mp4', writer=animwriter)
for j in range(i, progressbar_len + 1):
print('#', end='')
print('|\n\n')
if createVideo:
print('Finished all animations in the list.\n\n')