def _run_wrapper(self, index):
        logger.info("Orbit {0}".format(index))

        # unpack input argument dictionary
        import gary.potential as gp
        potential = gp.load(os.path.join(self.cache_path, self.config.potential_filename))

        # read out just this initial condition
        norbits = len(self.w0)
        allfreqs = np.memmap(self.cache_file, mode='r',
                             shape=(norbits,), dtype=self.cache_dtype)

        # short-circuit if this orbit is already done
        if allfreqs['success'][index]:
            logger.debug("Orbit {0} already successfully completed.".format(index))
            return None

        # Only pass in things specified in _run_kwargs (w0 and potential required)
        kwargs = dict([(k,self.config[k]) for k in self.config.keys() if k in self._run_kwargs])
        res = self.run(w0=self.w0[index], potential=potential, **kwargs)
        res['index'] = index

        # cache res into a tempfile, return name of tempfile
        tmpfile = os.path.join(self._tmpdir, "{0}-{1}.pickle".format(self.__class__.__name__, index))
        with open(tmpfile, 'w') as f:
            pickle.dump(res, f)
        return tmpfile
def main(path, n=None, m_scale=None, overwrite=False, seed=None):
    potential = gp.load(os.path.join(path, "potential.yml"))

    for name in sorted(three_orbits.keys()): # enforce same order
        w0 = three_orbits[name]
        ew0 = create_ensemble(w0, potential, n=n, m_scale=m_scale)
        fn = os.path.join(path, "w0-{0}.npy".format(name))
        if not os.path.exists(fn) or (os.path.exists(fn) and overwrite):
            np.save(fn, ew0)
Beispiel #3
0
def main(potential_name, E, ic_func, run_name=None, output_path=None, overwrite=False, plot=False, **kwargs):
    """ Calls one of the grid-making utility functions to generate a
        grid of initial conditions for frequency mapping, and saves the
        grid to a file.
    """

    # read the potential from the registry
    potential_path = os.path.join(project_path, "potentials/{0}.yml".format(potential_name))
    potential = gp.load(potential_path)

    # create path
    if output_path is None:
        output_path = os.path.join(project_path, "output")

        if run_name is None:
            run_name = 'E{:.3f}_{}'.format(E, ic_func.func_name)
        path = os.path.join(output_path, 'freqmap', potential_name, run_name)
    else:
        path = output_path

    logger.info("Caching to: {}".format(path))
    if not os.path.exists(path):
        os.makedirs(path)

    # path to initial conditions cache
    w0path = os.path.join(path, 'w0.npy')
    pot_path = os.path.join(path, 'potential.yml')

    if os.path.exists(w0path) and overwrite:
        os.remove(w0path)

    if not os.path.exists(w0path):
        # initial conditions
        w0 = ic_func(E=E, potential=potential, **kwargs)
        np.save(w0path, w0)
        logger.info("Create initial conditions file:\n\t{}".format(w0path))

        # save potential
        potential.save(pot_path)

    else:
        w0 = np.load(w0path)
        logger.info("Initial conditions file already exists!\n\t{}".format(w0path))

    if plot:
        fig,ax = plt.subplots(1, 1, figsize=(8,8))
        ax.plot(w0[:,0], w0[:,2], marker='.', linestyle='none')
        fig.savefig(os.path.join(path, 'w0.png'))

    logger.info("Number of initial conditions: {}".format(len(w0)))
import os
import sys
import cPickle as pickle
from astropy import log as logger
import numpy as np
import gary.potential as gp
from gary.util import get_pool
from streammorphology.freqvar import FreqVariance

path = "/vega/astro/users/amp2217/projects/morphology/output/paper1-subgrid-17/"
cache_path = os.path.join(path, "convergence-test")
potential = gp.load(os.path.join(path, "potential.yml"))
_w0 = np.array([17.0, 0.0, 25.555555555555557, 0.0, 0.1331002672911241, 0.0])

def worker(args):
    p, window_width, nsteps_per_period = args
    cache_file = os.path.join(cache_path, "regular-p{0}_ww{1}_{2}perperiod.pickle".format(p, window_width, nsteps_per_period))
    if os.path.exists(cache_file):
        return

    result = FreqVariance.run(_w0, potential, nsteps_per_period=nsteps_per_period, 
                              window_stride=2., window_width=window_width,
                              total_nperiods=window_width+64, hamming_p=p)

    with open(cache_file, 'w') as f:
        pickle.dump(result, f)

    return

def main(mpi=False):
    pool = get_pool(mpi=mpi)
Beispiel #5
0
# Third-party
import numpy as np
from astropy import log as logger
import gary.potential as gp

# Project
from ... import project_path
from ..core import align_ensemble, compute_align_matrix

logger.setLevel(logging.DEBUG)

plot_path = "plots/tests/TODO"
if not os.path.exists(plot_path):
    os.makedirs(plot_path)

potential = gp.load(os.path.join(project_path, 'potentials/triaxial-NFW.yml'))


def test_align_orbit():
    # start with an orbit that circulates x
    w0 = [1., 0., 30., 0., 0.15, -0.1]
    t, w = potential.integrate_orbit(w0, dt=1., nsteps=25000)
    w = w[:, 0]

    # fig = gd.plot_orbits(w, marker=None)
    # plt.show()

    R = compute_align_matrix(w[-1])
    new_x = np.array(R.dot(w[:, :3].T).T)
    new_v = np.array(R.dot(w[:, 3:].T).T)
    new_L = np.cross(new_x[-1], new_v[-1])[0]