gpe.imports#
Common imports to simplify notebooks.
Usage:
from gpe.imports import *
Classes#
Minimizer for states with the pytimeode.interfaces.IStateForABMEvolver |
|
Object for stacking plots. |
Functions#
Module Contents#
- class MinimizeState(state, real=False, fix_N=True, **kw)[source]#
Bases:
MinimizeMinimizer for states with the pytimeode.interfaces.IStateForABMEvolver interface.
- state#
- fix_N = True#
- real = False#
- _active_inds#
- property x#
Get current state. Must allow setting and unsetting.
- property _x_dtype#
- unpack(x, state=None)[source]#
Unpack x into state including factors of x_scale
Note: Do not scale or otherwise mutate x.
- pack(state)[source]#
This is not symmetric with unpack as it does not scale the solution by any factors. (This is because we unpack both states and Hpsi which have different scalings.).
- f_df(x)[source]#
Return the objective function and it’s derivative as a function of the unpacked state x.
- minimize(psi_tol=1e-08, E_tol=1e-12, callback=None, _debug=False, **kw)[source]#
Return the state with minimized energy.
If x_tol < sqrt(eps), then we use the canned L-BFGS-B algorithm from scipy as the is very robust. If we need higher precision on x, then we use a custom L-BFGS algorithm with a modified stopping criterion.
- Parameters:
f_tol (float) – Relative stopping tolerance for the energy.
x_tol (float) – Relative stopping tolerance for the wavefunction.
polish (bool) –
If True, then build an approximation to the hessian during the minimization phase so that this can be used with Broyden’s method to polish the solution to high accuracy after the energy is minimized. This allows us to ensure that the wavefunction is also converged to high accuracy. (Since the energy is quadratic about the minimum, it is common for the energy to be converged to machine precision eps even though the state is only converged to about sqrt(eps)).
Currently does not actually polish the solution - only stores the data in self.broyden_data. See use_scipy below.
broyden_alpha (None, float) – Initial inverse Jacobian approximation is alpha times the identity. If None, then this will be estimated by performing a line search in the descent direction.
broyden_opts (dict()) – Arguments to pass through to mmfutils.solve.broyden.DyadicSum().
use_scipy (bool) –
If True, then use the scipy solvers, otherwise, if x_tol < sqrt(eps), then use our custom solver that performs Broyden iterations after. The issue here is that the minimizer might bail out prematurely because of the f_tol criterion before reaching the desired x_tol because the minimum is quadratic. Since we have information about the hessian, we can in principle continue to refine the solution with Broyden iterations to minimize the norm of the residual.
This is the idea of the polish option, but one can “trick” scipy into doing this by setting f_tol=np.nan. This will always invalidate the criterion (even f_tol=0 can fail sometimes when the energy is very flat) and force scipy to continue until x_tol is met or the method fails. Always check your answer if you do this!
_debug (bool) – If True then return (x, f, df, locals()).
- Returns:
x (array) – Solution
f, df (functions) – Objective function and its gradient in the packed representation.
Developer notes
Do not mutate state0. Use state as a working copy if a – true state is needed.
2. Be careful which version of state or state0 you use as the – argument to get_Hy and get_energy since this determines the non-linear portions. In general one should use state, but if one implements a minimization on the norm of Hy then for the derivative one must be careful.
- evolve_to(state, t, Evolver=EvolverABM, dt_t_scale=0.1, callback=None, plot_steps=100)[source]#
Evolve state to time t.
- Parameters:
callback (None, function) – If provided, then call callback(state) every plot_steps steps.
plot_steps (int) – Steps to take between plots.
- evolve(state=None, history=None, Evolver=EvolverABM, t_max=np.inf, dt_t_scale=0.1, steps=100, display=True)[source]#
Iterator that runs an evolver.
- Parameters:
state (IState) – State to evolve.
history ([State]) – List of states. If provided, then use the last state to start. This is mutated.
Evolver (IEvolver) – Evolver to use.
t_max (float) – Maximum time to evolve to. (Default - evolve until interrupted.)
dt_t_scale (float) – Size of time-step in units of state.t_scale.
steps (int) – Steps to evolve between yielded states.
display (bool) – If True, then display the current figure (using plt.gcf()).
Example
evolve(s) (for y in) – plt.clf() y.plot()
- evolves(states=None, histories=None, Evolver=EvolverABM, t_max=np.inf, dt_t_scale=0.1, steps=100, fig=None, display=True)[source]#
Iterator that runs an evolver like evolve() but with multiple states.
- Parameters:
states (IState) – List of states to evolve. The first of these is used as the reference state for units such as t_scale etc.
histories ([[State]]) – List of histories: each is a list of states. If provided, then use the last states to start. These lists are mutated.
Evolver (IEvolver) – Evolver to use.
t_max (float) – Maximum time to evolve to. (Default - evolve until interrupted.)
dt_t_scale (float) – Size of time-step in units of state.t_scale.
steps (int) – Steps to evolve between yielded states.
display (bool) – If True, then display the current figure (using fig or plt.gcf()).
- class MPLGrid(fig=None, subplot_spec=None, ax=None, direction='down', right=False, scale=True, space=None, share=False, size=1, **kw)[source]#
Object for stacking plots.
After initializing the object, call next() to get the next axis. By default axes will stack vertically.
- down#
If True, then stack elements vertically growing down. Otherwise stack elements horizontally growing right.
- Type:
- direction#
Direction of growth.
- Type:
[‘down’, ‘right’]
- scale#
If False, then when a parent MPLGrid is being adjusted, the ratio for this grid component will be the sum of the ratios rather than the specified parent ratio. If True, then all the components will be scaled to fit into the parents ratio.
- Type:
If True, then axes in the growth direction are shared, and tick labels etc. are suppressed except for the final one.
- Type:
- space#
Space (hspace or wspace) between grids. If this is None, then the space is set of zero if the axes are shared and 0.1 otherwise.
- Type:
float, None
- fig = None#
- subplot_spec = None#
- shape = [0, 0]#
- kw#
- right = False#
- share = False#
- direction = 'down'#
- _ratios = []#
- axes = []#
- space = None#
- scale = True#
- size = 1#
- property ratios#
Return the size ratios for the various axes.
If the sub-axis is an MPLGrid instance with scale == False, then the ratio is the sum of the sub-grid’s ratios.
- property _gridspec#