Skip to content

Commit

Permalink
added serialization / deserialization of the config and the results
Browse files Browse the repository at this point in the history
fixes #2
  • Loading branch information
azimonti committed Aug 11, 2024
1 parent 551e06d commit af2138a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__/
tmp/
data/
57 changes: 49 additions & 8 deletions schrodinger_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from matplotlib.collections import PolyCollection
import numpy as np
import os
import pickle
from scipy import integrate, sparse
from scipy.special import hermite
import sys
Expand All @@ -20,7 +21,6 @@
from mod_config import cfg, palette, p1, electron_params
from mod_plotter import BasePlotter


c = palette
# select the set of parameters to use
p = electron_params if cfg.small_scale else p1
Expand Down Expand Up @@ -373,7 +373,6 @@ def create_barrier(self, ax):
ha='left', va='center', fontsize=20)

def init_graph(self, ax):

# plot the potential barrier
self.create_barrier(ax)
# initialize the plots and store the line objects
Expand Down Expand Up @@ -410,8 +409,8 @@ def plot(self):
self.create_axes(True)
self.init_graph(self._axs_p)
# Select with frame to plot without the need of animation
# frame_to_plot = self._total_frames - 1
frame_to_plot = 0
frame_to_plot = len(self._psi) - 1
# frame_to_plot = 0
for i in range(frame_to_plot):
self.frame_update(i, self._axs_p, True)

Expand Down Expand Up @@ -486,30 +485,72 @@ def plot_update(self, cur_step: int):


def make_plot(outfile: str):
global cfg, p
params = {
'high_res': True,
'ggplot': False,
'dark_background': False,
'do_plot': True,
'do_compute': True,
'do_animation': True,
'load_data': False,
'save_data': False,
'data_folder': 'data/simul',
'animation_format': 'gif',
'total_duration': 6,
'fps': 30
}
# init data so a plot can be done without computing
# init data so a plot can be done without computing
x = np.arange(-p.x_max, p.x_max, p.dx)
if cfg.superposition:
psi = [create_superposition(x)]
else:
psi = [create_wavepacket(x)]
t = [0.0]
# define the potential
V = create_potential(x)
v = create_potential(x)
# deserialize config
if params['load_data']:
folder = params['data_folder']
script_dir = os.path.dirname(os.path.abspath(__file__))
simul_dir = os.path.join(script_dir, folder)
if cfg.verbose:
print(f"Loading params ({simul_dir}/config.pkl)")
if not os.path.exists(simul_dir):
raise FileNotFoundError(f"path not found: {simul_dir}")
with open(simul_dir + '/config.pkl', 'rb') as file:
cfg = pickle.load(file)
p = pickle.load(file)
if params['do_compute']:
t, psi = compute(
x, t, psi, V, params['total_duration'] * params['fps'])
plotter = MyPlotter(params, x, t, psi, V, outfile)
x, t, psi, v, params['total_duration'] * params['fps'])
# serialize data
if params['save_data']:
folder = params['data_folder']
script_dir = os.path.dirname(os.path.abspath(__file__))
simul_dir = os.path.join(script_dir, folder)
if cfg.verbose:
print(f"Saving config and data ({simul_dir})")
if not os.path.exists(simul_dir):
os.makedirs(simul_dir)
with open(simul_dir + '/config.pkl', 'wb') as file:
pickle.dump(cfg, file)
pickle.dump(p, file)
with open(simul_dir + '/data.pkl', 'wb') as file:
pickle.dump(t, file)
pickle.dump(x, file)
pickle.dump(psi, file)
pickle.dump(v, file)
# deserialize data
if params['load_data']:
if cfg.verbose:
print(f"Loading data ({simul_dir}/data.pkl)")
with open(simul_dir + '/data.pkl', 'rb') as file:
t = pickle.load(file)
x = pickle.load(file)
psi = pickle.load(file)
v = pickle.load(file)
plotter = MyPlotter(params, x, t, psi, v, outfile)
plotter.plot()
plotter.save_plot()
plotter.init_animation()
Expand Down

0 comments on commit af2138a

Please sign in to comment.