import mmf_setup
mmf_setup.nbinit()
This cell adds /home/docs/checkouts/readthedocs.org/user_builds/gpe/checkouts/latest/src to your path, and contains some definitions for equations and some CSS for styling the notebook. If things look a bit strange, please try the following:
- Choose "Trust Notebook" from the "File" menu.
- Re-execute this cell.
- Reload the notebook.
Simulations#
Here we discuss strategies for managing simulations performed locally, and on other servers like CoCalc or on clusters like Kamiak.
Organizing Experiments#
In order to reliably keep track of results, I recommend organizing everything in terms of “experiments”. The original idea here is to allow easy comparison with real experimental results, but the method can be extended to purely numerical work. To do this, we define two classes:
Experiment: This class should provide an interface to the problem. It’s main role should be to accept experimentally relevant parameters, and then produce an appropriate initial state that representing the experimental protocol. I delegate from the State class to the Experiment to determine this like time-dependence of parameters (e.g. because these may be most naturally expressed in experimental units.) It should provide the following methods:
Experiment.__init__(**kw): The constructor should only accept keyword arguments. These arguments will be stored with their values and used to generate a unique directory name for checkpoints. For this to work, there needs to be a one-to-one mapping between simulation runs and the parameters here.Experiment.get_state(): Should quickly return a validStateobject, but quickly. The data may be initialized using something like the Thomas Fermi approximation, but expensive minimization should not be performed as this will also be used by theSimulationclass which will insert checkpoint data into this object.Experiment.get_initial_state(): This should perform the appropriate experimental protocol for initializing the experimental state - i.e. the correct state at time \(t=0\). Thus, appropriate minimizations should be performed.Experiment.dir_name: This should be a property that returns a valid directory name (without the base path) where the experimental data should be stored. This name should encode all of the information passed to the constructor. I organize my work first into a directory name with the same name as the class, then into parameter-specific subfolders.
Note that only the constructor should take arguments (unless arguments are used for testing or debugging). This will be the reproducible path to generating data. To support this, you should only use Experiment.get_state() or Experiment.get_initial_state() to obtain a state object. Finally, the Experiment objects should be able to be made persistent and will generally be stored for reference along with the checkpoint data.
State: The state represents all of the actual physical quantities of interest, but should express these in the most natural way for the purpose of computation. It is the role of the Experiment class to convert from experimental units to the units that State uses. For my work, I may overload the default states slightly if modifications are needed, and generally pass the Experiment object to State.experiment through the State constructor so that I can delegate properties such as time-dependence of coupling constants or trapping potentials.
State.E_max: Maximum energy representable in the basis. (Used to determine the evolver timestep).State.hbar: ConvertsE_maxto a time for use by the evolver.
In designing your problem, you should consider what parameters make sense to vary in a single Experiment object, and when it is better to define a new Experiment class altogether.
from gpe import utils, soc_soliton
reload(utils)
reload(soc_soliton)
experiment = soc_soliton.Experiment1()
sim = utils.Simulation(experiment=experiment, dt_t_unit=0.5)
sim.initialize()
sim.run()
[I 03:38:20 numexpr.utils] NumExpr defaulting to 2 threads.
[I 03:38:20 root] Patching zope.interface.document.asReStructuredText to format code
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[2], line 1
----> 1 from gpe import utils, soc_soliton
2
3 reload(utils)
4 reload(soc_soliton)
ImportError: cannot import name 'soc_soliton' from 'gpe' (/home/docs/checkouts/readthedocs.org/user_builds/gpe/checkouts/latest/src/gpe/__init__.py)
%pylab inline --no-import-all
from gpe import utils, soc_soliton
reload(utils)
reload(soc_soliton)
experiment = soc_soliton.Experiment1()
sim = utils.Simulation(experiment=experiment, dt_t_unit=0.5)
plt.figure(figsize=(20, 5))
sim.view()
%pylab is deprecated, use %matplotlib inline and import the required libraries.
Populating the interactive namespace from numpy and matplotlib
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[3], line 2
1 get_ipython().run_line_magic('pylab', 'inline --no-import-all')
----> 2 from gpe import utils, soc_soliton
3
4 reload(utils)
5 reload(soc_soliton)
ImportError: cannot import name 'soc_soliton' from 'gpe' (/home/docs/checkouts/readthedocs.org/user_builds/gpe/checkouts/latest/src/gpe/__init__.py)
sim = utils.Simulation(
dt_t_unit=0.5,
dir_name="_data/Experiment1/Lxyz=(1000.0,)/Nxyz=(16384,)/Rx_TF=234.6/detuning_E_R=0.1/magnetic_field_gradient=1e-06/mu_Bs_mu_B=(0.5,0.0)/rabi_frequency_E_R=1.9/recoil_frequency_Hz=3683.8/states=((1,-1),(1,0))/t_SOC_on_ms=10/t_expand_ms=7/t_wait_ms=100/trapping_frequencies_Hz=(3,273,277)",
)
sim.initialize()
# state = expt.get_initial_state()
# a = Archive()
# a.insert(expt=expt)
# d = {}
# exec str(a) in d
# d['expt']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 sim = utils.Simulation(
2 dt_t_unit=0.5,
3 dir_name="_data/Experiment1/Lxyz=(1000.0,)/Nxyz=(16384,)/Rx_TF=234.6/detuning_E_R=0.1/magnetic_field_gradient=1e-06/mu_Bs_mu_B=(0.5,0.0)/rabi_frequency_E_R=1.9/recoil_frequency_Hz=3683.8/states=((1,-1),(1,0))/t_SOC_on_ms=10/t_expand_ms=7/t_wait_ms=100/trapping_frequencies_Hz=(3,273,277)",
4 )
TypeError: Simulation.__init__() got an unexpected keyword argument 'dt_t_unit'
persist.archive.Archive?
Object `persist.archive.Archive` not found.
Visualization#
Here we consider visualizing a simulation. First we generate some data:
sim.get_state(1.0)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[6], line 1
----> 1 sim.get_state(1.0)
NameError: name 'sim' is not defined
import signal
import ipyparallel as ipp
rc = ipp.Client()
engine_pids = rc[:].apply(os.getpid).get_dict()
def signal_engine(engine_id, sig=signal.SIGINT):
"""send a signal to a local engine"""
pid = engine_pids[engine_id]
os.kill(pid, sig)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[7], line 2
1 import signal
----> 2 import ipyparallel as ipp
3
4 rc = ipp.Client()
5 engine_pids = rc[:].apply(os.getpid).get_dict()
ModuleNotFoundError: No module named 'ipyparallel'
!rm -rf _data/ExperimentLoading/
%%px --noblock
from gpe import utils, soc_soliton
reload(utils)
reload(soc_soliton)
experiment = soc_soliton.ExperimentLoading(cells_x=100)
sim = utils.Simulation(dt_=1.0, experiment=experiment, dt_t_scale=0.1)
sim.run()
UsageError: Cell magic `%%px` not found.
from gpe import utils, soc_soliton
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[10], line 1
----> 1 from gpe import utils, soc_soliton
ImportError: cannot import name 'soc_soliton' from 'gpe' (/home/docs/checkouts/readthedocs.org/user_builds/gpe/checkouts/latest/src/gpe/__init__.py)
reload(utils)
reload(soc_soliton)
experiment = soc_soliton.ExperimentLoading(cells_x=100)
sim = utils.Simulation(dt_=1.0, experiment=experiment, dt_t_scale=0.1)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[11], line 1
----> 1 reload(utils)
2 reload(soc_soliton)
3 experiment = soc_soliton.ExperimentLoading(cells_x=100)
4 sim = utils.Simulation(dt_=1.0, experiment=experiment, dt_t_scale=0.1)
NameError: name 'reload' is not defined
def plot_state(state, fig=None):
if fig is None:
fig = plt.figure()
plt.plot(state.xyz[0], state.get_density()[0])
return fig
sim.view(plot_state=plot_state)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[13], line 1
----> 1 sim.view(plot_state=plot_state)
NameError: name 'sim' is not defined
map(signal_engine, engine_pids)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[14], line 1
----> 1 map(signal_engine, engine_pids)
NameError: name 'signal_engine' is not defined
s = sim.get_state(0)
s.experiment.trapping_frequencies_Hz
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[15], line 1
----> 1 s = sim.get_state(0)
2 s.experiment.trapping_frequencies_Hz
NameError: name 'sim' is not defined
plt.plot(s.get_density()[1])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[16], line 1
----> 1 plt.plot(s.get_density()[1])
NameError: name 's' is not defined
s.experiment.get_initial_state(psi_tol=1e-16).plot()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[17], line 1
----> 1 s.experiment.get_initial_state(psi_tol=1e-16).plot()
NameError: name 's' is not defined
%pylab inline --no-import-all
from IPython.display import clear_output
import logging
from gpe import utils, soc_soliton
reload(utils)
reload(soc_soliton)
experiment = soc_soliton.ExperimentLoading()
sim = utils.Simulation(
dt_=1.0, experiment=experiment, dt_t_scale=0.1, logging_level=logging.WARNING
)
%pylab is deprecated, use %matplotlib inline and import the required libraries.
Populating the interactive namespace from numpy and matplotlib
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[18], line 5
1 get_ipython().run_line_magic('pylab', 'inline --no-import-all')
2 from IPython.display import clear_output
3 import logging
4
----> 5 from gpe import utils, soc_soliton
6
7 reload(utils)
8 reload(soc_soliton)
ImportError: cannot import name 'soc_soliton' from 'gpe' (/home/docs/checkouts/readthedocs.org/user_builds/gpe/checkouts/latest/src/gpe/__init__.py)
import holoviews as hv
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[19], line 1
----> 1 import holoviews as hv
ModuleNotFoundError: No module named 'holoviews'
hv.extension("bokeh")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[20], line 1
----> 1 hv.extension("bokeh")
NameError: name 'hv' is not defined
import attr
from collections import namedtuple
@attr.s
class Sim:
sim = attr.ib()
@property
def ts(self):
return self.sim.saved_ts_
@property
def t(self):
return hv.Dimension("t", values=self.ts)
@property
def extents(self):
xmin = 0
nmin = np.inf
xmax = nmax = -np.inf
for t_ in self.ts:
state = self.sim.get_state(t_)
xmin = min(xmin, state.xyz[0].min())
xmax = max(xmax, state.xyz[0].max())
ns = state.get_density()
nmin = min(nmin, ns.min())
nmax = min(nmax, ns.max())
return (xmin, xmax, nmin, nmax)
def densities(self, t_):
state = self.sim.get_state(t_)
x = hv.Dimension("x", values=state.xyz[0])
na, nb = state.get_density()
return (
hv.Curve(na, kdims=[x], vdims=["na"])
* hv.Curve(nb, kdims=[x], vdims=["nb"])
* hv.Curve(na + nb, kdims=[x], vdims=["n"])
)
def plots(self, t_):
state = self.sim.get_state(t_)
state.plot()
fig = plt.gcf()
return Figure(fig)
class Figure(hv.Element2D):
pass
class FigurePlot(hv.plotting.mpl.ElementPlot):
def initialize_plot(self, ranges=None):
element = self.hmap.last
self.handles["fig"] = element.data
return self.handles["fig"]
def update_handles(self, key, axis, element, ranges, style):
self.handles["fig"] = element.data
def update_frame(self, key, ranges=None, element=None):
element = self._get_frame(key)
self.handles["fig"] = element.data
return self.handles["fig"]
hv.Store.register({Figure: FigurePlot}, "matplotlib")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[21], line 49
45 fig = plt.gcf()
46 return Figure(fig)
47
48
---> 49 class Figure(hv.Element2D):
50 pass
51
52
NameError: name 'hv' is not defined
hv.plotting.mpl.ElementPlot.update_handles?
Object `hv.plotting.mpl.ElementPlot.update_handles` not found.
#%%output size=150
#%%opts Curve [height=100 width=400]
s = Sim(sim)
# hv.DynamicMap(s.densities, kdims=[s.t])
hv.DynamicMap(s.plots, kdims=[s.t])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[23], line 4
1 #%%output size=150
2 #%%opts Curve [height=100 width=400]
3
----> 4 s = Sim(sim)
5 # hv.DynamicMap(s.densities, kdims=[s.t])
6 hv.DynamicMap(s.plots, kdims=[s.t])
NameError: name 'sim' is not defined
def plot_state(state, fig=None):
if fig is None:
fig = plt.figure()
state.plot_k()
return fig
plot_state = None
sim.view(plot_state=plot_state)
plt.close("all")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[24], line 9
5 return fig
6
7
8 plot_state = None
----> 9 sim.view(plot_state=plot_state)
10 plt.close("all")
NameError: name 'sim' is not defined
import numpy as np
frequencies = [0.5, 0.75, 1.0, 1.25]
def sine_curve(phase, freq):
xvals = [0.1 * i for i in range(100)]
return hv.Curve((xvals, [np.sin(phase + freq * x) for x in xvals]))
# When run live, this cell's output should match the behavior of the GIF below
dmap = hv.DynamicMap(sine_curve, kdims=["phase", "frequency"])
dmap.redim.range(phase=(0.5, 1)).redim.range(frequency=(0.5, 1.25))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[25], line 12
8 return hv.Curve((xvals, [np.sin(phase + freq * x) for x in xvals]))
9
10
11 # When run live, this cell's output should match the behavior of the GIF below
---> 12 dmap = hv.DynamicMap(sine_curve, kdims=["phase", "frequency"])
13 dmap.redim.range(phase=(0.5, 1)).redim.range(frequency=(0.5, 1.25))
NameError: name 'hv' is not defined