-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.py
112 lines (92 loc) · 4.14 KB
/
save.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
import logging
import json
from pathlib import Path
import numpy as np
from tqdm import tqdm
from h5py import File
import matplotlib.pyplot as plt
from .simulation import Simulation
logger = logging.getLogger(__name__)
class SaveSimulation(object):
project_dir = Path(__file__).parents[1]
data_dir = project_dir / 'saved_data'
def __init__(self, name: str,
simulation: Simulation):
self.name = name
self.simulation = simulation
self.current_dir = self.data_dir / name
logger.debug(f'Current dir: {self.current_dir}')
def save_config(self, config_dict: dict):
with open(self.current_dir / 'config.json', 'w') as f:
json.dump(config_dict, f)
def save_parameters(self):
r = self.simulation.radius
c = self.simulation.c
centers = [(center, f'Center {i + 1}') for i, center in enumerate(self.simulation.centers)]
for param, name in ((r, 'Radius'),
(c, 'Concentration'),
*centers):
plt.figure()
for p in range(self.simulation.number_of_particles):
plt.plot(param[:, p])
plt.title(name)
ax = plt.gca()
ax.set_xlabel('Time')
ax.set_ylabel(name)
ax.grid(linestyle='--', linewidth=0.5)
plt.savefig(self.current_dir / f'parameter_{name}.png')
plt.close()
def save(self,
number_of_xpcs: int = 30,
save_animation: bool = True,
*,
mode: str = 'std',
q_min: float = 0,
q_max: float = None,
dq: float = 3,
save_kinetics: bool = True,
save_parameters: bool = True,
save_real_space: bool = False,
save_recip_space: bool = False,
):
Path.mkdir(self.current_dir)
if q_max is None:
q_max = self.simulation.x_size // 2
else:
q_max = min(q_max, self.simulation.x_size // 2)
qs = np.linspace(q_min, q_max, number_of_xpcs)
with File(self.current_dir / 'data.h5', 'w') as f:
f.attrs.update(self.simulation.map_size.asdict())
if save_real_space:
f.create_dataset('u', data=self.simulation.real_space)
if save_recip_space:
f.create_dataset('ft', data=self.simulation.recip_space)
if save_kinetics:
qs, rs, ts, kinetics = self.simulation.get_kinetics()
kinetics_group = f.create_group('kinetics')
kinetics_group.create_dataset('kinetics', data=kinetics)
kinetics_group.create_dataset('qs', data=qs)
kinetics_group.create_dataset('rs', data=rs)
kinetics_group.create_dataset('ts', data=ts)
self.simulation.plot_kinetics(
file_paths=(self.current_dir / 'kinetics_R.png',
self.current_dir / 'kinetics_Q.png')
)
ttc_group = f.create_group('ttcs')
for q in tqdm(qs):
logger.debug(f'Plotting q = {q:.2f}')
plt.figure()
ttc = self.simulation.get_two_time_corr_function(q, dq, mode=mode)
self.simulation.plot_xpcs(q, dq, mode=mode, ttc=ttc, show=False)
plt.savefig(self.current_dir / f'xpcs_q_{q:.2f}.png')
plt.close()
dset = ttc_group.create_dataset(str(q), data=ttc)
dset.attrs.update(dict(q=q, dq=dq, mode=mode))
if save_parameters:
param_group = f.create_group('parameters')
param_group.create_dataset('c', data=self.simulation.c)
param_group.create_dataset('centers', data=self.simulation.centers)
param_group.create_dataset('radii', data=self.simulation.radius)
self.save_parameters()
if save_animation:
self.simulation.save_animation(self.current_dir / f'animation_{self.name}.gif')