Пример #1
0
def __is_sim_dir__(path='.'):
    """ Checks if a path is pointing at a pencil code simulation."""
    if __isdir__(__join__(path, 'data')) and __exists__(__join__(path, 'run.in')) \
    and __exists__(__join__(path, 'start.in')) and __exists__(__join__(path, 'src/cparam.local')) \
    and __exists__(__join__(path, 'src/Makefile.local')):
        return True
    return False
Пример #2
0
def is_sim_dir(path='.'):
    """Checks if a path is pointing at a pencil code simulation directory. Therefor, it checks the existance of start.in, run.in, src/cparam.local and src/Makefile.local"""

    from os.path import isdir as __isdir__
    from os.path import join as __join__
    from os.path import exists as __exists__

    if __exists__(__join__(path, 'run.in')) and __exists__(__join__(path, 'start.in')) and __exists__(__join__(path, 'src/cparam.local')) and __exists__(__join__(path, 'src/Makefile.local')):
        return True
    return False
Пример #3
0
    def __init__(self, path='.', hidden=False, quiet=False):
        import os
        from os.path import join as __join__
        from os.path import exists as __exists__
        from os.path import split as __split__
        #from pen.intern.hash_sim import hash_sim
        import pencilnew

        # find out name and store it
        self.name = __split__(path)[-1]
        if self.name == '.' or self.name == '':
            self.name = __split__(os.getcwd())[-1]

        # store paths
        self.path = os.path.abspath(path)
        self.dir = self.path
        if (not quiet): print '# Creating Simulation object for ' + self.path
        self.data_dir = __join__(self.path, 'data')
        self.pc_dir = __join__(self.path, '.pc')
        self.pc_data_dir = __join__(self.path, 'data', '.pc')

        # generate status hash identification
        #self.status_hash = hash_sim(path)

        # hidden is default False
        self.hidden = hidden

        # read params into SIM object
        self.param = {}
        if __exists__(__join__(self.data_dir, 'param.nml')):
            param = pencilnew.read.Param().read(quiet=True,
                                                data_dir=self.data_dir)
            for key in dir(param):
                if key.startswith('__'): continue
                self.param[key] = getattr(param, key)
        else:
            print '?? WARNING: Couldnt find param.nml in simulation ' + self.name + '! Simulation is now hidden from calculations!'
            self.param['UNSTARTED'] = True
            self.hidden = True

        try:
            self.grid = pencilnew.read.grid(data_dir=self.data_dir,
                                            trim=True,
                                            quiet=True)
            self.ghost_grid = pencilnew.read.grid(data_dir=self.data_dir,
                                                  trim=False,
                                                  quiet=True)
        except:
            self.grid = None
            self.ghost_grid = None
Пример #4
0
def pkl_load(name, folder=False, sim=False, ascii=True):
    """This scripts loads an pkl-file. It automatically checks known folders if no folder is specified.

  Args:
	name:		Name of pkl-file  (<name>.pkl)
	folder:		Folder containing pkl file
    sim:        Simulation for checking automatically folders for
	ascii:		Switch to False if pkl file was NOT written in ascii

  Example:
	to read ".pc/sim.pkl" use: pkl_load('sim', '.pc')
	or simply pkl_load('sim'), since pkl_load checks for following folders automatically: '.pc', 'data/.pc'
  """

    import pickle
    from os.path import join as __join__
    from os.path import exists as __exists__

    if (not name.endswith('.pkl')):
        name = name + '.pkl'  # add .pkl to name if not already ending with

    # if folder is not defined try to find the pkl-file at typical places
    sim_path = '.'
    if sim: sim_path = sim.path
    if not folder:
        if __exists__(__join__(sim_path, '.pc', name)):
            folder = __join__(sim_path, '.pc')
            print '~ Found ' + name + ' in ' + folder
        elif __exists__(__join__(sim_path, 'data/.pc', name)):
            folder = __join__(sim_path, 'data/.pc')
            print '~ Found ' + name + ' in ' + folder
        elif __exists__(__join__(sim_path, '.', name)):
            folder = __join__(sim_path, '.')
            print '~ Found ' + name + ' in ' + folder
        else:
            print '~ Couldnt find file ' + name
            return False

    # open file
    file = __join__(folder, name)
    try:  # check on existance
        if not __exists__(file) or not __exists__(__join__(sim_path, file)):
            print '!! ERROR: pkl_load couldnt load ' + file
            return False
        try:  # open file and return it
            with open(file, 'r') as f:
                return pickle.load(f)
        except:
            with open(__join__(sim_path, file), 'r') as f:
                return pickle.load(f)

    except:  # if anything goes wrong
        print '!! ERROR: Something went wrong while importing pkl-file!'
        return False
Пример #5
0
def pkl_save(obj, name, folder='.pc', ascii=True):
    """This scripts saves any kind of object as a pkl file in folder.

    Args:
    obj:		object you want to save in an pkl file
    name:		name of pkl file, '.pkl' will be added automatically if missing
    ascii:		if you dont want to save pkl file in ascii set this to false
    """
    from pencilnew.io.mkdir import mkdir as __mkdir__
    from os.path import join as __join__
    import pickle

    __mkdir__(folder)  ## prepare folder

    if (not name.endswith('.pkl')): name = name + '.pkl'

    with open(__join__(folder, name), 'wb') as f:
        if ascii:
            pickle.dump(obj, f, 0)
            return True
        else:
            pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
            return True

    return False
Пример #6
0
    def get_varfiles(self, pos=False, particle=False):
        """Get a list of all existing VAR# file names.

        pos = False:            give full list
        pos = 'last'/'first':       give newest/first var file
        post = list of numbers: give varfiles at this positions
        particle = True:        return PVAR isntead of VAR list"""
        import glob
        from os.path import join as __join__
        from os.path import basename
        from pencilnew.math import natural_sort
        varlist = natural_sort([
            basename(i)
            for i in glob.glob(__join__(self.data_dir, 'proc0') + '/VAR*')
        ])
        if particle: varlist = ['P' + i for i in varlist]
        if pos == False:
            return varlist
        elif pos == 'first':
            return [varlist[0]]
        elif pos == 'last':
            return [varlist[-1]]
        elif pos.startswith('last'):
            return varlist[-int(pos[4:]):]
        elif pos.startswith('first'):
            return varlist[:int(pos[4:])]
        elif type(pos) == type([]):
            if pos[0].startswith('VAR'): pos = [i[3:] for i in pos]
            if pos[0].startswith('PVAR'): pos = [i[3:] for i in pos]
            return [varlist[i] for i in pos]
        else:
            return varlist
Пример #7
0
def dill_load(name, folder=False, sim=False):
    """This scripts loads an dill-file. It automatically checks known folders if no folder is specified.
    Args:
        name:        Name of dill-file  (<name>.dill)
        folder:        Folder containing dill file
        sim:        Simulation for checking automatically folders for

    Example:
       to read ".pc/sim.dill" use: dill_load('sim', '.pc')
       or simply dill_load('sim'), since dill_load checks for following folders automatically: '.pc', 'data/.pc'
    """

    import pencilnew.backpack.dill as dill
    from os.path import join as __join__
    from os.path import exists as __exists__

    if (not name.endswith('.dill')):
        name = name + '.dill'  # add .dill to name if not already ending with

    # if folder is not defined try to find the dill-file at typical places
    sim_path = '.'
    if sim: sim_path = sim.path
    if not folder:
        if __exists__(__join__(sim_path, '.pc', name)):
            folder = __join__(sim_path, '.pc')
            print('~ Found ' + name + ' in ' + folder)
        elif __exists__(__join__(sim_path, 'data/.pc', name)):
            folder = __join__(sim_path, 'data/.pc')
            print('~ Found ' + name + ' in ' + folder)
        elif __exists__(__join__(sim_path, '.', name)):
            folder = __join__(sim_path, '.')
            print('~ Found ' + name + ' in ' + folder)
        else:
            print('~ Couldnt find file ' + name)
            return False

    # open file
    file = __join__(folder, name)
    try:  # check on existance
        if not __exists__(file) or not __exists__(__join__(sim_path, file)):
            print('!! ERROR: dill_load couldnt load ' + file)
            return False
        try:  # open file and return it
            with open(file, 'r') as f:
                return dill.load(f)
        except:
            with open(__join__(sim_path, file), 'r') as f:
                return dill.load(f)

    except:  # if anything goes wrong
        print('!! ERROR: Something went wrong while importing dill-file!')
        return False
Пример #8
0
def get_sim(path='.'):
    """Returns simulation object from 'path/.pc/' if already existing."""
    if __exists__(__join__(path, '.pc/sim.pkl')):
        return io.load('sim', folder='.pc')
    else:
        from pencilnew import __is_sim_dir__
        if __is_sim_dir__(path):
            return sim.Simulation(path)
        else:
            print('?? WARNING: No simulation found in ' + path +
                  '>. Try get_sims maybe?')
            return False
Пример #9
0
def dill_save(obj, name, folder='.pc'):
    """This scripts saves any kind of object as a dill file in a folder.

    Args:
        obj:		object you want to save in an pkl file
        name:		name of pkl file, '.pkl' will be added automatically if missing
    """
    from pencilnew.io.mkdir import mkdir as __mkdir__
    from os.path import join as __join__
    import pencilnew.backpack.dill as dill

    __mkdir__(folder)  ## prepare folder

    if (not name.endswith('.dill')): name = name + '.dill'

    with open(__join__(folder, name), 'wb') as f:
        dill.dump(obj, f)
        return True

    return False