Example #1
0
def search():
    st = time.time()
    n_query = len(queries)
    print("min..", np.min(queries))
    print("max..", np.max(queries))

    diffusion = Diffusion(np.vstack([queries, gallery]), args.cache_dir)
    print("diffusion shape",diffusion.features.shape)

    # offline type : scipy.sparse.csr.csr_matrix (희소행렬)
    # offline shape : (5118 ,5118) >> query와 gallery의 합
    offline = diffusion.get_offline_results(args.truncation_size, args.kd)
    # offline.data 안에 nan 값이 존재함 
    print("offline..", len(offline.data))

    print("offline features shape..",offline.shape)

    offline.data = np.nan_to_num(offline.data, copy=False)

    # 컬럼별로 확률화(SUM=1), feature=(1-a)(1-aS)의 역행렬
    features = preprocessing.normalize(offline, norm="l2", axis=1)
    print("features..",features.shape)

    scores = features[:n_query] @ features[n_query:].T
    np.save("pirsData/scores/"+ args.cate +"_scores.npy", -scores.todense())

    print("1> features[:n_query].shape :", features[:n_query].shape)
    print("2> features[n_query:].shape :", features[n_query:].shape)
    print("3> scores.shape :", scores.shape)
    # scores.shape : (55, 5063) = (쿼리, 갤러리) = (row, col)

    ranks = np.argsort(-scores.todense())
    #np.save("pirsData/ranks/"+ args.cate +"_ranks.npy", ranks)
    print("ranks[0]...\n", ranks[:10,:10])
    print("time check...>>>", args.cate,">>>", time.time()-st, ">>>", len(queries))
    def test_middle(self):
        """
        Changing middle compartment, affects outer compartments by the same amount
        """
        # TODO: why does 'Compartment' fail the test (presumable an anion issue causing different steady-state cli)
        self.compBase = SimpleCompartment("c1", pkcc2=1e-8, z=-0.85)
        self.comp = self.compBase.copy("left")
        self.comp2 = self.comp.copy("right")

        # set diffusion value
        cli_D = 2.03
        cli_D *= 1e-7  # um2 to dm2 (D in dm2/ss)
        ki_D = 1.96
        ki_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        nai_D = 1.33
        nai_D *= 1e-7
        # create diffusion connection
        diffusion_object = Diffusion(self.comp, self.comp2, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})
        diffusion_object = Diffusion(self.comp2, self.compBase, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})

        self.sim.run(stop=100, dt=0.001, block_after=False)

        self.compBase.gx = 1e-8
        self.sim.run(continuefor=1, dt=1e-6, block_after=False)
        self.assertEqual(self.comp.cli, self.comp2.cli)

        self.compBase.gx = 0e-8
        self.sim.run(continuefor=1, dt=1e-6, block_after=False)
        self.assertEqual(self.comp.cli, self.comp2.cli)
Example #3
0
def search():
    n_query = len(queries)
    diffusion = Diffusion(np.vstack([queries, gallery]), args.cache_dir)
    offline = diffusion.get_offline_results(args.truncation_size, args.kd)
    features = preprocessing.normalize(offline, norm="l2", axis=1)
    scores = features[:n_query] @ features[n_query:].T
    ranks = np.argsort(-scores.todense())
    evaluate(ranks)
Example #4
0
    def __init__(self, configName):
        '''
        Sets up the initial spatial grid, time grid, accumulation rate, age, density, mass, stress, and temperature of the model run
        :param configName: name of json config file containing model configurations
        '''

        # load in json config file and parses the user inputs to a dictionary
        with open(configName, "r") as f:
            jsonString = f.read()
            self.c = json.loads(jsonString)

        # create directory to store results
        if os.path.exists(self.c['resultsFolder']):
            rmtree(self.c['resultsFolder'])
        os.makedirs(self.c['resultsFolder'])

        # read in initial temperatures and accumulation rates
        input_temp, input_year_temp = read_temp(self.c['InputFileNameTemp'])
        input_bdot, input_year_bdot = read_bdot(self.c['InputFileNamebdot'])

        # load in model parameters
        self.bdot0 = input_bdot[0]
        self.temp0 = input_temp[0]
        gridLen, dx, dt, t, stp, Ts, T_mean, bdotSec, rhos0 = self.define_parameters()

        # set up model grid
        gridheight = np.linspace(c['H'], c['HbaseSpin'], gridLen)
        self.z  = self.c['H'] - gridHeight
        self.dz = np.diff(self.z)
        self.dz = np.append(self.dz, self.dz[-1])

        # set up the initial age and density of the firn column using herron & langway analytic
        THL = input_temp[0]
        AHL = input_bdot[0]
        self.age, self.rho = hl_analytic(self.c['rhos0'], self.z, THL, AHL)

        # set up initial mass, stress, and mean accumulation rate
        self.mass  = self.rho * self.dz
        self.sigma = self.mass * dx * GRAVITY
        self.sigma = self.sigma.cumsum(axis = 0)
        self.mass_sum = self.mass.cumsum(axis = 0)
        self.bdot_mean = np.concatenate(([self.mass_sum[0] / (RHO_I * S_PER_YEAR)], self.mass_sum[1:] / (self.age[1:] * RHO_I / t)))

        # set up initial temperature grid as well as a class to handle heat/isotope diffusion
        init_Tz = input_temp[0] * np.ones(gridLen)
        self.diffu = Diffusion(self.z, stp, gridLen, init_Tz)

        # set up initial grain growth (if specified in config file)
        if self.c['physGrain']:
            if self.c['calcGrainSize']:
                r02 = -2.42e-9 * (self.c['Ts0']) + 9.46e-7
                self.r2 = r02 * np.ones(gridLen)
            else:
                self.r2 = np.linspace(self.c['r2s0'], (6 * self.c['r2s0']), gridLen)
Example #5
0
    def test_homo(self, gamma=3):
        n_trunc = 1000
        kq, kd = 15, 30
        lam = 0.2

        diffusion = Diffusion(self.cross_gallery_fc)
        inverse = diffusion.get_laplacian_inverse(n_trunc, kd)
        knn = KNN(self.cross_gallery_fc, method='cosine')
        sims, ids = knn.search(self.cross_query_fc, kq)
        sims[sims < 0] = 0
        sims /= np.sum(sims, axis=-1).reshape(-1, 1)
        sims = sims**gamma
        scores_qg = np.empty(
            (len(self.test_query_set), len(self.test_gallery_set)),
            dtype=np.float32)
        for i in range(len(self.test_query_set)):
            scores_qg[i] = (sims[i] @ inverse[ids[i]])

        diffusion = Diffusion(self.cross_query_fc)
        inverse = diffusion.get_laplacian_inverse(n_trunc, kd)
        knn = KNN(self.cross_query_fc, method='cosine')
        sims, ids = knn.search(self.cross_gallery_fc, kq)
        sims[sims < 0] = 0
        sims /= np.sum(sims, axis=-1).reshape(-1, 1)
        sims = sims**gamma
        scores_gq = np.empty(
            (len(self.test_gallery_set), len(self.test_query_set)),
            dtype=np.float32)
        for i in range(len(self.test_gallery_set)):
            scores_gq[i] = (sims[i] @ inverse[ids[i]])

        scores = lam * scores_qg + (1 - lam) * scores_gq.T
        self.evaluate(-scores)
    def test_mols(self):
        self.compBase = SimpleCompartment("c1", pkcc2=1e-8, z=-0.85,
                                          cli=0.015292947537423218,
                                          ki=0.023836660428807395,
                                          nai=0.1135388427892471)

        self.comp = self.compBase.copy("left")
        self.comp2 = self.comp.copy("right")
        self.compSingle = SimpleCompartment("cs", pkcc2=1e-8, z=-0.85,
                                            cli=0.015292947537423218,
                                            ki=0.023836660428807395,
                                            nai=0.1135388427892471,
                                            length=3 * default_length)
        # set diffusion value
        cli_D = 2.03
        cli_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        ki_D = 1.96
        ki_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        nai_D = 1.33
        nai_D *= 1e-7
        # create diffusion connection
        diffusion_object = Diffusion(self.comp, self.compBase, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})
        diffusion_object = Diffusion(self.comp2, self.compBase, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})

        self.sim.run(stop=100, dt=0.001, block_after=False)

        self.compBase.gx = 1e-8
        self.compSingle.gx = 1e-8
        self.sim.run(continuefor=1, dt=1e-6, block_after=False)

        xmol_3 = self.compBase.mols(self.compBase.xi) + self.comp.mols(self.comp.xi) + self.comp2.mols(self.comp2.xi)
        xmol_single = self.compSingle.mols(self.compSingle.xi)
        self.assertAlmostEqual(xmol_3, xmol_single)

        cmol_3 = self.compBase.mols(self.compBase.cli) + self.comp.mols(self.comp.cli) + self.comp2.mols(self.comp2.cli)
        cmol_single = self.compSingle.mols(self.compSingle.cli)
        self.assertAlmostEqual(cmol_3, cmol_single)
Example #7
0
def search_old(gamma=3):
    diffusion = Diffusion(gallery, args.cache_dir)
    offline = diffusion.get_offline_results(args.truncation_size, args.kd)

    time0 = time.time()
    print('[search] 1) k-NN search')
    sims, ids = diffusion.knn.search(queries, args.kq)
    sims = sims**gamma
    qr_num = ids.shape[0]

    print('[search] 2) linear combination')
    all_scores = np.empty((qr_num, args.truncation_size), dtype=np.float32)
    all_ranks = np.empty((qr_num, args.truncation_size), dtype=np.int)
    for i in tqdm(range(qr_num), desc='[search] query'):
        scores = sims[i] @ offline[ids[i]]
        parts = np.argpartition(-scores,
                                args.truncation_size)[:args.truncation_size]
        ranks = np.argsort(-scores[parts])
        all_scores[i] = scores[parts][ranks]
        all_ranks[i] = parts[ranks]
    print('[search] search costs {:.2f}s'.format(time.time() - time0))

    # 3) evaluation
    evaluate(all_ranks)
Example #8
0
def run_epidemic_experiment(params):
    graph = as_733().copy()
    results = defaultdict(list)

    b_list = np.arange(0, 0.005, 0.001)  # transmission probability
    for idx, b in enumerate(b_list):

        params['b'] = b

        if idx == 1:
            params['plot_transition'] = True
            params['gif_animation'] = True
            params['gif_snaps'] = True
        else:
            params['plot_transition'] = False
            params['gif_animation'] = False
            params['gif_snaps'] = False

        ds = Diffusion(graph, **params)

        result = ds.run_simulation()
        results[ds.get_effective_strength()] = result

    plot_results(graph, params, results)
Example #9
0
    def __init__(self, options=None):
        super().__init__(options)

        #Upper is interactive map preview
        #Lower is menu options
        self.layout.split(Layout(name='upper'), Layout(name='lower'))

        self.sleepTime = .5

        self.maps = dict()
        self.maps["diffusion"] = Diffusion(80, 30, 300)
        self.maps["life"] = Life(140, 30, 5, 80)

        self.layout["upper"].size = 35
        self.layout["lower"].update(self.menu)
        self.populate()
Example #10
0
    def __init__(self, gridSize = 256, coralliteSpacing = None):
        self.gridSize = gridSize
        if coralliteSpacing is not None:
           self.coralliteSpacing = coralliteSpacing

        self.initMesh()
        self.voxelizer = Voxelizer(gridSize)
        self.diffusion = Diffusion(gridSize)
        a = zeros([gridSize]*3, float32)
        col = linspace(0.0, 1.0, gridSize).astype(float32)
        a[:] = col[...,newaxis, newaxis]
        self.diffusion.src.set(a)
        
        self.setupKernels()
        self.getMeshArrays()
        self.calcAbsorb()
Example #11
0
    def __init__(self, options = None):
        super().__init__(options)
        self.layout.split(
            Layout(name='upper'),
            Layout(name='lower')
        )

        self.layout["lower"].update(self.menu)


        self.maps = dict()
        self.maps["diffusion"] = Diffusion(80,30, 300)
        self.maps["life"] = Life(140,30,5,80)

        self.layout["upper"].size = 35
        self.populate()
Example #12
0
def test_sis_model():
    params = {
        'model': 'SIS',
        'b': 0.00208,
        'd': 0.01,
        'c': 1,
        'runs': 10,
        'steps': 5000,

        'diffusion': 'max',
        'method': 'add_edge_random',
        'k': 15,

        'seed': 1,
        'plot_transition': False,
        'gif_animation': False
    }

    graph = karate()

    ds = Diffusion(graph, params)
    increased_diffusion = ds.run_simulation()

    params['diffusion'] = None
    params['method'] = None
    params['k'] = 0

    ds = Diffusion(graph, params)
    baseline_diffusion = ds.run_simulation()

    params['diffusion'] = 'min'
    params['method'] = 'ns_node'
    params['k'] = 4

    ds = Diffusion(graph, params)
    decreased_diffusion = ds.run_simulation()

    assert sum(decreased_diffusion) < sum(baseline_diffusion) < sum(increased_diffusion)
    def test_one_is_two(self):
        """
        Changing to the same values of all (2) compartments is the same as changing as if it were compartment
        """
        self.compBase = Compartment("c1", length=10e-5, pkcc2=0, z=-0.85,
                                    cli=0.015292947537423218,
                                    ki=0.023836660428807395,
                                    nai=0.1135388427892471)

        self.comp = Compartment("c1", length=5e-5, pkcc2=0, z=-0.85,
                                cli=0.015292947537423218,
                                ki=0.023836660428807395,
                                nai=0.1135388427892471)
        self.comp2 = self.comp.copy("c2")
        # set diffusion value
        cli_D = 2.03
        cli_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        ki_D = 1.96
        ki_D *= 1e-7  # um2 to dm2 (D in dm2/s)
        nai_D = 1.33
        nai_D *= 1e-7
        # create diffusion connection
        diffusion_object = Diffusion(self.comp, self.comp2, ions={'cli': cli_D, 'ki': ki_D, 'nai': nai_D})

        self.assertEqual(self.compBase.cli, self.comp.cli)

        self.sim.run(stop=100, dt=0.001, block_after=False)

        self.assertEqual(self.compBase.cli, self.comp.cli)
        self.assertEqual(self.comp.cli, self.comp2.cli)

        self.compBase.gx = self.comp.gx = self.comp2.gx = 1e-8

        self.sim.run(continuefor=1, dt=1e-6, block_after=False)

        self.assertEqual(self.compBase.cli, self.comp.cli)
        self.assertEqual(self.comp.cli, self.comp2.cli)
Example #14
0
class FirnDensityNoSpin:
    def __init__(self, configName):
        '''
        Sets up the initial spatial grid, time grid, accumulation rate, age, density, mass, stress, temperature, and diffusivity of the model run
        :param configName: name of json config file containing model configurations
        '''

        # load in json config file and parses the user inputs to a dictionary
        with open(configName, "r") as f:
            jsonString = f.read()
            self.c = json.loads(jsonString)

        # read in initial depth, age, density, temperature
        initDepth, initAge, initDensity, initTemp = read_init(self.c['resultsFolder'])

        # set up the initial age and density of the firn column
        self.age     = initAge[1:]
        self.rho     = initDensity[1:]

        # set up model grid
        self.z  = initDepth[1:]
        self.dz = np.diff(self.z)
        self.dz = np.append(self.dz, self.dz[-1])

        # load in model parameters
        gridLen, dx, dt, t, modeltime, years, stp, Ts, T_mean, bdot, bdotSec, rhos0, D_surf = self.define_parameters()

        # set up the initial grid of diffusivity constant
        self.Dcon = self.c['D_surf'] * np.ones(gridLen)

        # set up vector of times data will be written
        self.TWrite = modeltime[INTE::INTE]

        # set up initial mass, stress, and mean accumulation rate
        self.mass = self.rho * self.dz
        self.sigma = self.mass * dx * GRAVITY
        self.sigma = self.sigma.cumsum(axis = 0)
        self.mass_sum = self.mass.cumsum(axis = 0)
        self.bdot_mean = np.concatenate(([self.mass_sum[0] / (RHO_I * S_PER_YEAR)], self.mass_sum[1:] / (self.age[1:] * RHO_I / t)))

        # set up class to handle heat/isotope diffusion using user provided data for initial temperature vector
        self.diffu = Diffusion(self.z, stp, gridLen, initTemp[1:])

        # set up initial values for density, temperature, age, depth, diffusivity, Clim??, and accumulation to write
        rho_time  = np.append(modeltime[0], self.rho)
        Tz_time   = np.append(modeltime[0], self.diffu.Tz)
        age_time  = np.append(modeltime[0], self.age)
        z_time    = np.append(modeltime[0], self.z)
        D_time    = np.append(modeltime[0], self.Dcon)
        Clim_time = np.append(modeltime[0], [bdot[0], Ts[0]])  # not sure if bdot or bdotSec
        bdot_time = np.append(modeltime[0], self.bdot_mean)

        # set up initial grain growth (if specified in config file)
        if self.c['physGrain']:
            initr2 = np.genfromtxt(r2Path, delimiter = ',')
            self.r2 = initr2
            r20 = r2
            r2_time = np.append(modeltime[0], self.r2)
        else:
            r2_time = None

        # write initial values to the results folder
        write_nospin(self.c['resultsFolder'], self.c['physGrain'], rho_time, Tz_time, age_time, z_time, D_time, Clim_time, bdot_time, r2_time)

        # set up initial values for bubble close-off depth & age, lock-in zone depth & age, and depth integrated porosity
        self.bcoAgeMartAll = []
        self.bcoDepMartAll = []
        self.bcoAge815All  = []
        self.bcoDep815All  = []
        self.LIZAgeAll     = []
        self.LIZDepAll     = []
        self.intPhiAll     = []

        update_BCO()
        update_LIZ()
        update_DIP()

    def time_evolve(self):
        '''
        Evolve the spatial grid, time grid, accumulation rate, age, density, mass, stress, temperature, and diffusivity through time
        based on the user specified number of timesteps in the model run. Updates the firn density using a user specified 
        '''

        # load in model parameters
        gridLen, dx, dt, t, modeltime, years, stp, Ts, T_mean, bdot, bdotSec, rhos0, D_surf = define_parameters()
        steps = 1 / t
        if not self.c['physGrain']:
            r2_time = None

        for iter in xrange(stp):
            mtime = modeltime[iter]

            # getting the right physics for firn density based on user input
            physics = {
                'HLdynamic',      HLdynamic,
                'HLSigfus',       HLSigfus,
                'Barnola1991',    Barnola1991,
                'Li2004',         Li2004,
                'Li2011',         Li2011,
                'Ligtenberg2011', Ligtenberg2011,
                'Arthern2010S',   Arthern2010S,
                'Simonsen2013',   Simonsen2013,
                'Morris2013',     MorrisHL2013,
                'Helsen2008',     Helsen2008,
                'Arthern2010T',   Arthern2010T,
                'Spencer2001',    Spencer2001,
                'Goujon2003',     Goujon2003,
            }

            parameters = {
                'HLdynamic',      [steps, gridLen, bdotSec, self.diffu.Tz, self.rho],
                'HLSigfus',       [steps, gridLen, bdotSec, self.diffu.Tz, self.rho, sigma],
                'Barnola1991',    [steps, gridLen, bdotSec, self.diffu.Tz, self.rho, sigma],
                'Li2004',         [steps, gridLen, bdotSec, T_mean, self.rho],
                'Li2011',         [steps, gridLen, bdotSec, self.bdot_mean, self.c['bdot_type'], self.diffu.Tz, T_mean, self.rho],
                'Ligtenberg2011', [steps, gridLen, bdotSec, self.bdot_mean, self.c['bdot_type'], self.diffu.Tz, T_mean, self.rho],
                'Arthern2010S',   [steps, gridLen, bdotSec, self.bdot_mean, self.c['bdot_type'], self.diffu.Tz, T_mean, self.rho],
                'Simonsen2013',   [steps, gridLen, bdotSec, self.bdot_mean, self.c['bdot_type'], self.diffu.Tz, T_mean, self.rho],
                'Morris2013',     [steps, gridLen, self.diffu.Tz, dt, self.rho, True, iter],
                'Helsen2008',     [steps, bdotSec, self.c['bdot_type'], self.bdot_mean, self.diffu.Tz, Ts, self.rho],
                'Arthern2010T',   [gridLen, self.diffu.Tz, self.rho, self.sigma, self.r2, self.c['physGrain']],
                'Spencer2001',    [],
                'Goujon2003',     [],

            }
            try:
                drho_dt = physics[self.c['physRho']](parameters[self.c['physRho']])
            except KeyError:
                default()

            # update density and age of firn
            self.age = np.concatenate(([0], self.age[:-1])) + dt
            self.rho = self.rho + dt * drho_dt
            self.rho  = np.concatenate(([rhos0[iter]], self.rho[:-1]))
            self.Dcon = np.concatenate(([D_surf[iter]], self.Dcon[:-1]))

            # update temperature grid and isotope grid if user specifies
            if self.c['heatDiff']:
                self.diffu.heatDiff(self.z, self.dz, Ts, self.rho)
            if self.c['heatDiff']:
                self.diffu.isoDiff(iter, self.z, self.dz, self.rho, self.c['iso'])

            # update model grid
            dzNew = bdotSec[iter] * RHO_I / rhos0[iter] * S_PER_YEAR
            self.dz = self.mass / self.rho * dx
            self.dz = np.concatenate(([dzNew], self.dz[:-1]))
            self.z = self.dz.cumsum(axis = 0)
            self.z = np.concatenate(([0], self.z[:-1]))

            # update mass, stress, and mean accumulation rate
            massNew = bdotSec[iter] * S_PER_YEAR * RHO_I
            self.mass = np.concatenate(([massNew], self.mass[:-1]))
            self.sigma = self.mass * dx * GRAVITY
            self.sigma = self.sigma.cumsum(axis = 0)
            self.mass_sum  = self.mass.cumsum(axis = 0)
            self.bdot_mean = np.concatenate(([mass_sum[0] / (RHO_I * S_PER_YEAR)], self.mass_sum[1:] * t / (self.age[1:] * RHO_I)))

            # update grain radius
            if self.c['physGrain']:
                self.r2 = grainGrowth(self.diffu.Tz, Ts, iter, dt)

            # write results as often as specified in the init method
            if [True for iter in self.TWrite if iter == mtime] == [True]:
                rho_time  = np.append(mtime, self.rho)
                Tz_time   = np.append(mtime, self.diffu.Tz)
                age_time  = np.append(mtime, self.age)
                z_time    = np.append(mtime, self.z)
                Dcon_time = np.append(mtime, self.Dcon)
                Clim_time = np.append(mtime, [bdot[iter], Ts[iter]])
                bdot_time = np.append(mtime, self.bdot_mean)
                if c['physGrain']:
                    r2_time = np.append(mtime, self.r2)

                write_nospin(self.c['resultsFolder'], self.c['physGrain'], rho_time, Tz_time, age_time, z_time, D_time, Clim_time, bdot_time, r2_time)

                update_BCO()
                update_LIZ()
                update_DIP()

        # write BCO, LIZ, DIP at the end of the time evolution
        write_nospin_BCO(self.c['resultsFolder'], self.bcoAgeMartAll, self.bcoDepMartAll, self.bcoAge815All, self.bcoDep815All)
        write_nospin_LIZ(self.c['resultsFolder'], self.LIZAgeAll, self.LIZDepAll)
        write_nospin_DIP(self.c['resultsFolder'], self.intPhiAll)

    def define_parameters(self):
        '''
        Return the parameters used in the model, for the initialization as well as the time evolution

        :returns gridLen: size of grid used in the model run
                         (unit: number of boxes, type: int)
        :returns dx: vector of width of each box, used for stress calculations
                    (unit: ???, type: array of ints)
        :returns dt: number of seconds per time step
                    (unit: seconds, type: float)
        :returns t: number of years per time step
                   (unit: years, type: float)
        :returns modeltime: linearly spaced time vector from indicated start year to indicated end year
                           (unit: years, type: array of floats)
        :returns years: total number of years in the model run
                       (unit: years, type: float)
        :returns stp: total number of steps in the model run
                     (unit: number of steps, type: int)
        :returns T_mean: interpolated temperature vector based on the model time and the initial user temperature data
                        (unit: ???, type: array of floats)
        :returns Ts: interpolated temperature vector based on the model time & the initial user temperature data
                    may have a seasonal signal imposed depending on number of years per time step (< 1)
                    (unit: ???, type: array of floats)
        :returns bdot: bdot is meters of ice equivalent/year. multiply by 0.917 for W.E. or 917.0 for kg/year
                      (unit: ???, type: )
        :returns bdotSec: accumulation rate vector at each time step
                         (unit: ???, type: array of floats)
        :returns rhos0: surface accumulate rate vector
                       (unit: ???, type: array of floats)
        :returns D_surf: diffusivity tracker
                        (unit: ???, type: array of floats)
        '''

        gridLen = np.size(self.z)
        dx = np.ones(gridLen)

        input_temp, input_year_temp = read_temp(self.c['InputFileNameTemp'])
        input_bdot, input_year_bdot = read_bdot(self.c['InputFileNamebdot'])

        yr_start  = max(input_year_temp[0], input_year_bdot[0])
        yr_end    = min(input_year_temp[-1], input_year_bdot[-1])
        modeltime = np.linspace(yr_start, yr_end, stp + 1)
        years     = (yr_end - yr_start) * 1.0
        stp       = int(years * self.c['stpsPerYear'])

        dt = years * S_PER_YEAR / stp
        t  = 1.0 / self.c['stpsPerYear']

        TPeriod = years
        Ts = np.interp(modeltime, input_year_temp, input_temp)
        T_mean = Ts
        if t < 1.0:
            Ts = Ts + self.c['TAmp'] * (np.cos(2 * np.pi * np.linspace(0, TPeriod, stp + 1)) + 0.3 * np.cos(4 * np.pi * np.linspace(0, TPeriod, stp + 1)))

        bdot = np.interp(modeltime, input_year_bdot, input_bdot)
        bdotSec = bdot / S_PER_YEAR / (stp / years)

        rhos0 = self.c['rhos0'] * np.ones(stp)
        D_surf = self.c['D_surf'] * np.ones(stp)

        return gridLen, dx, dt, t, modeltime, years, stp, Ts, T_mean, bdot, bdotSec, rhos0, D_surf

    def update_BCO(self):
        '''
        Updates the bubble close-off depth and age based on the Martinerie criteria as well as through assuming the critical density is 815 kg/m^3
        '''

        bcoMartRho = 1 / (1 / (917.0) + self.diffu.T10m * 6.95E-7 - 4.3e-5)  # Martinerie density at close off; see Buizert thesis (2011), Blunier & Schwander (2000), Goujon (2003)
        bcoAgeMart = min(self.age[selfrho >= bcoMartRho]) / S_PER_YEAR  # close-off age from Martinerie
        bcoDepMart = min(selfz[selfrho >= (bcoMartRho)])
        self.bcoAgeMartAll.append(bcoAgeMart)  # age at the 815 density horizon
        self.bcoDepMartAll.append(bcoDepMart)  # this is the 815 close off depth

        # bubble close-off age and depth assuming rho_crit = 815kg/m^3
        bcoAge815 = min(self.age[self.rho >= (RHO_2)]) / S_PER_YEAR  # close-off age where rho = 815 kg m^-3
        bcoDep815 = min(self.z[self.rho >= (RHO_2)])
        self.bcoAge815All.append(bcoAge815)  # age at the 815 density horizon
        self.bcoDep815All.append(bcoDep815)  # this is the 815 close off depth

    def update_LIZ(self):
        '''
        Updates the lock-in zone depth and age
        '''

        bcoMartRho = 1 / (1 / (917.0) + self.diffu.T10m * 6.95E-7 - 4.3e-5) # Martinerie density at close off; see Buizert thesis (2011), Blunier & Schwander (2000), Goujon (2003)
        LIZMartRho = bcoMartRho - 14.0  # LIZ depth (Blunier and Schwander, 2000)
        self.LIZAgeMart = min(self.age[self.rho > LIZMartRho]) / S_PER_YEAR  # lock-in age
        self.LIZDepMart = min(self.z[self.rho >= (LIZMartRho)])  # lock in depth
        self.LIZAgeAll.append(LIZAgeMart)
        self.LIZDepAll.append(LIZDepMart)

    def update_DIP(self):
        '''
        Updates the depth-integrated porosity
        '''

        bcoMartRho = 1 / (1 / (917.0) + self.diffu.T10m * 6.95E-7 - 4.3e-5) # Martinerie density at close off; see Buizert thesis (2011), Blunier & Schwander (2000), Goujon (2003)
        phi = 1 - self.rho / RHO_I  # total porosity
        phi[phi <= 0] = 1e-16
        phiC = 1 - bcoMartRho / RHO_I;  # porosity at close off
        phiClosed = 0.37 * phi * (phi / phiC) ** -7.6  # Closed porosity, from Goujon. See Buizert thesis (eq. 2.3) as well

        phiOpen = phi - phiClosed  # open porosity
        phiOpen[phiOpen <= 0] = 1.e-10  # don't want negative porosity.

        intPhi = np.sum(phi * self.dz)  # depth-integrated porosity
        self.intPhiAll.append(intPhi)
Example #15
0
def _infer(model, root_path, test_loader=None, local_val=False):
    """
    모델과 데이터가 주어졌을 때, 다음과 같은 데이터 구조를 반환하는 함수를 만들어야 합니다.

    [ [ query_image_id_1, predicted_database_image_id_1 ],
      [ query_image_id_2, predicted_database_image_id_2 ],
      ...
      [ query_image_id_N, predicted_database_image_id_N ] ]

    README 설명에서처럼 predicted_database_image_id_n 은 query_image_id_n 에 대해
    평가셋 이미지 1,...,n-1,n+1,...,N 를 데이터베이스로 간주했을 때에 가장 쿼리와 같은 카테고리를
    가질 것으로 예측하는 이미지입니다. 이미지 아이디는 test_loader 에서 extract 되는 첫번째
    인자인 data_id 를 사용합니다.

    Args:
      model: 이미지를 인풋으로 받아서 feature vector를 반환하는 모델
      root_path: 데이터가 저장된 위치
      test_loader: 사용되지 않음
      local_val: 사용되지 않음

    Returns:
      top1_reference_ids: 위에서 설명한 list 데이터 구조
    """
    if test_loader is None:
        test_loader = test_data_loader(
            root=os.path.join(root_path, 'test_data'))

    # TODO 모델의 아웃풋을 적당히 가공하고 연산하여 각 query에 대해 매치가 되는 데이터베이스
    # TODO 이미지의 ID를 찾는 모듈을 구현 (현재 구현은 베이스라인 - L2 정규화 및 내적으로 가장
    # TODO 비슷한 이미지 조회).
    feats = None
    data_ids = None
    s_t = time.time()
    for idx, data_package in enumerate(test_loader):
        if local_val:
            data_id, image, _ = data_package
        else:
            data_id, image = data_package
        image = image.cuda()
        feat = model(image, extract=True)
        feat = feat.detach().cpu().numpy()
        feat = feat / np.linalg.norm(feat, axis=1)[:, np.newaxis]
        if feats is None:
            feats = feat
        else:
            feats = np.append(feats, feat, axis=0)
        if data_ids is None:
            data_ids = data_id
        else:
            data_ids = np.append(data_ids, data_id, axis=0)

        if time.time() - s_t > 10:
            print('Infer batch {}/{}.'.format(idx + 1, len(test_loader)))

    diffusion = Diffusion(feats, cache_dir='./cache')
    offline = diffusion.get_offline_results(n_trunc=1000, kd=50)
    features = preprocessing.normalize(offline, norm='l2', axis=1)

    score_matrix = features @ features.T
    np.fill_diagonal(score_matrix, -np.inf)
    top1_reference_indices = np.argmax(score_matrix, axis=1)
    top1_reference_ids = [[
        data_ids[idx], data_ids[top1_reference_indices[idx]]
    ] for idx in range(len(data_ids))]

    return top1_reference_ids
def main(cli_D=2.03,
         new_gx=0e-8,
         anion_flux=False,
         default_xz=-0.85,
         jkccup=1e-12,
         nrcomps=2,
         dz=1e-7,
         textra=100,
         say='',
         stretch=False):
    """
    cli_D # um2/s
    :return: sim, gui: it is useful to return these objects for access after simulation
    """
    print("main")
    sim = Simulator().get_instance()
    gui = sim.gui()
    dt = 0.001  # s

    length = 10e-5

    comp = Compartment("reference",
                       z=-0.85,
                       cli=0.0052,
                       ki=0.0123,
                       nai=0.014,
                       length=length,
                       radius=default_radius_short,
                       stretch_w=stretch)

    # copies left
    compl = comp.copy("dendrite left")

    # copies right
    compr = []
    compr.append(comp.copy("dendrite right " + str(1)))
    for i in range(nrcomps):
        compr.append(comp.copy("dendrite right " + str(i + 2)))

    # find steady-state values of ions
    sim.run(stop=100,
            dt=0.001,
            plot_update_interval=50,
            data_collect_interval=5,
            block_after=False)

    # set diffusion value
    cli_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    ki_D = 1.96
    ki_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    nai_D = 1.33
    nai_D *= 1e-7
    diffusion_object = []

    # connect with Diffusion
    diffusion_object.append(
        Diffusion(compl, comp, ions={
            'cli': cli_D,
            'ki': ki_D,
            'nai': nai_D
        }))
    diffusion_object.append(
        Diffusion(comp,
                  compr[0],
                  ions={
                      'cli': cli_D,
                      'ki': ki_D,
                      'nai': nai_D
                  }))
    for i in range(nrcomps):
        diffusion_object.append(
            Diffusion(compr[i],
                      compr[i + 1],
                      ions={
                          'cli': cli_D,
                          'ki': ki_D,
                          'nai': nai_D
                      }))

    # heatmap incorporating compartment heights
    sc = 1e7
    htplot = Colormap("cmap", 0, compr)
    totalht, initvals = htplot.heatmap(compl,
                                       comp,
                                       compr,
                                       sc,
                                       0,
                                       all=1,
                                       init_vals=None)
    htplot.heatmap(compl, comp, compr, sc, totalht, all=1, init_vals=initvals)

    voltage_reversal_graph_comp = gui.add_graph() \
        .add_ion_conc(comp, "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(comp, "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(comp, line_style='k', y_units_scale=1000, y_plot_units='mV')

    voltage_reversal_graph_compr = gui.add_graph() \
        .add_ion_conc(compr[-1], "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(compr[-1], "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(compr[-1], line_style='k', y_units_scale=1000, y_plot_units='mV')

    voltage_reversal_graph_compl = gui.add_graph() \
        .add_ion_conc(compl, "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(compl, "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(compl, line_style='k', y_units_scale=1000, y_plot_units='mV')

    voltage_reversal_graph_compr1 = gui.add_graph() \
        .add_ion_conc(compr[0], "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(compr[0], "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(compr[0], line_style='k', y_units_scale=1000, y_plot_units='mV')

    # run simulation with diffusion
    sim.run(continuefor=10,
            dt=dt,
            plot_update_interval=5,
            data_collect_interval=1)
    print(datetime.datetime.now())
    print_concentrations(
        [comp, compl, compr[-1]],
        title="Ion concentrations given diffusion between compartments")

    htplot.heatmap(compl, comp, compr, sc, totalht, all=1, init_vals=initvals)

    # (optionally) change anion conductance
    prev_comp_gx = comp.gx
    comp.gx = new_gx
    comp.dz = dz

    voltage_reversal_graph_comp.save(say + 'reference.eps')

    if dz != 0:
        z_graph = gui.add_graph() \
            .add_ion_conc(comp, "z", line_style='m')

    if comp.gx > 0:
        x_graph = gui.add_graph() \
            .add_ion_conc(comp, "absox", line_style='m') \
            .add_ion_conc(compl, "absox", line_style=':m') \
            .add_ion_conc(compr[0], "absox", line_style='m--')

    # (optionally) change anion flux
    if anion_flux:
        comp.xz = default_xz
        comp.xmz = (comp.z * comp.xi - comp.xz * comp.xi_temp) / comp.xm
        print('Anion flux with fixed anions having net charge', comp.xmz,
              'while a proportion of', (1 - comp.ratio),
              'of all impermeants are temporarily mobile anions of charge',
              comp.xz)

    z_graph = gui.add_graph() \
        .add_ion_conc(comp, "z", line_style='m')

    # (optionally) change kcc2
    prev_comp_pkcc2 = comp.pkcc2
    if jkccup is not None:
        comp.jkccup = jkccup
        g_graph = gui.add_graph() \
            .add_ion_conc(comp, "pkcc2", line_style='k')

    vol_graph = gui.add_graph() \
        .add_ion_conc(comp, "w", line_style='b') \
        .add_ion_conc(compl, "w", line_style=':b') \
        .add_ion_conc(compr[0], "w", line_style='b--')

    sim.run(continuefor=textra,
            dt=dt * 0.001,
            plot_update_interval=textra / 32,
            data_collect_interval=textra / 32)
    print(datetime.datetime.now())
    print_concentrations(
        [comp, compl, compr[-1]],
        title="Ion concentrations during event from the dendritic compartment")
    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_halfway_df.eps', say + 'all_halfway_ecl.eps',
                       say + 'all_halfway_vm.eps'
                   ])

    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 16)
    print(datetime.datetime.now())
    print_concentrations(
        [comp, compl, compr[-1]],
        title=
        "Ion concentrations immediately after event from the dendritic compartment"
    )
    # heatmap incorporating compartment heights
    htplot.heatmap(compl, comp, compr, sc, totalht, all=1, init_vals=initvals)

    comp.gx = prev_comp_gx
    comp.jkccup = 0
    comp.dz = 0

    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 1,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at almost steady state")

    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 1,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 2,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 2,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 2,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    return sim, gui
Example #17
0
class Coral(HasTraits):
    gridSize         = ReadOnly()
    diffuseStepNum   = Int(50) 
    growCoef         = Float(1.0)
    mouthDist        = Float(3.0)
    coralliteSpacing = Float(1.5)

    curDiffusionErr = Float(0.0)

    _ = Python(editable = False)

    def __init__(self, gridSize = 256, coralliteSpacing = None):
        self.gridSize = gridSize
        if coralliteSpacing is not None:
           self.coralliteSpacing = coralliteSpacing

        self.initMesh()
        self.voxelizer = Voxelizer(gridSize)
        self.diffusion = Diffusion(gridSize)
        a = zeros([gridSize]*3, float32)
        col = linspace(0.0, 1.0, gridSize).astype(float32)
        a[:] = col[...,newaxis, newaxis]
        self.diffusion.src.set(a)
        
        self.setupKernels()
        self.getMeshArrays()
        self.calcAbsorb()

    def initMesh(self):
        spacing = self.coralliteSpacing
        #verts, idxs = load_obj('data/icosahedron.obj')
        #verts *= spacing / 2.0
        verts, idxs = load_obj('data/shere_162.obj')
        verts *= spacing / 0.566
        
        verts += (self.gridSize/2, self.gridSize/2, spacing*2)
        self.mesh = mesh = _coralmesh.CoralMesh()
        for v in verts:
            mesh.add_vert(*v.tolist())
        for f in idxs:
            mesh.add_face(*f.tolist())
        mesh.update_normals()
        
    def setupKernels(self):
        self.gl2cudaBuf = cuda_gl.BufferObject(self.voxelizer.dumpToPBO().handle)
        
        code = Template('''
          {{g.cu_header}}
          
          #line 47
          texture<uint4, 1> voxelBits;
          texture<float, 1> srcTex;
          
          const int SliceDepth   = 128;
          const int ChannelDepth = 32;
          
          __device__ void prepareChannel(
            uint bits, 
            float * dst, 
            int ofs, int stride)
          {
            for (uint i = 0; i < ChannelDepth; ++i)
            {
              float v = {{ v.self.diffusion.OBSTACLE }}f;
              if ((bits & (1<<i)) == 0)
                v = max( 0.0f, tex1Dfetch(srcTex, ofs) );
              dst[ofs] = v;
              ofs += stride;
            }
          }
          
           
          extern "C"
          __global__ void PrepareDiffusionVolume(int size, float * dst)
          {
            int3 p;
            p.x = threadIdx.x + blockIdx.x * blockDim.x;
            p.y = threadIdx.y + blockIdx.y * blockDim.y;
            p.z = 0;
            
            int ofs = p.x + p.y*size;
            int stride_z = size*size;
            int stride_ch = stride_z * ChannelDepth;
            
            while (p.z < size)
            {
              uint4 bits = tex1Dfetch(voxelBits, p.x + p.y*size + p.z/SliceDepth*stride_z);
              prepareChannel(bits.x, dst, ofs, stride_z); ofs += stride_ch;
              prepareChannel(bits.y, dst, ofs, stride_z); ofs += stride_ch;
              prepareChannel(bits.z, dst, ofs, stride_z); ofs += stride_ch;
              prepareChannel(bits.w, dst, ofs, stride_z); ofs += stride_ch;
              p.z += SliceDepth;
            }

            ofs = p.x + p.y*size;
            dst[ofs] = 0.0;
            dst[ofs + stride_z*(size-1)] = 1.0;
          }
            
          __device__ bool ingrid(int3 p, int gridSize)
          {
            return p.x >= 0 && p.x < gridSize && 
                   p.y >= 0 && p.y < gridSize &&
                   p.z >= 0 && p.z < gridSize;
          }
          
          extern "C"
          __global__ void MarkSinks(int gridSize, float * grid, float mark, int sinkNum, const float3 * pos)
          {
            int idx = threadIdx.x + blockDim.x * blockIdx.x;
            if (idx >= sinkNum)
              return;
            float3 p = pos[idx];
            int3 c = make_int3(p);
            if (!ingrid(c, gridSize))
              return;
            grid[c.x + (c.y + c.z * gridSize) * gridSize] = mark;
          }
          
          extern "C"
          __global__ void FetchSinks(int gridSize, int sinkNum, const float3 * pos, float * dst)
          {
            int idx = threadIdx.x + blockDim.x * blockIdx.x;
            if (idx >= sinkNum)
              return;
            float3 p = pos[idx];
            int3 c = make_int3(p);
            if (!ingrid(c, gridSize))
              return;
            dst[idx] = tex1Dfetch(srcTex, c.x + (c.y + c.z * gridSize) * gridSize);
          }
        ''').render(v=vars(), g = globals())
        mod = SourceModule(code, include_dirs = [os.getcwd(), os.getcwd()+'/include'], no_extern_c = True)
        voxelBitsTex = mod.get_texref('voxelBits')
        voxelBitsTex.set_format(cu.array_format.UNSIGNED_INT32, 4)
        voxelBitsTex.set_flags(cu.TRSF_READ_AS_INTEGER)
        srcTex = mod.get_texref('srcTex')
        
        PrepareDiffusionVolume = mod.get_function("PrepareDiffusionVolume")
        def func(d_voxelBitsPtr, bitsSize):
            block = (16, 16, 1)
            grid = (self.gridSize/block[0], self.gridSize/block[1])
            self.diffusion.src.bind_to_texref(srcTex)
            voxelBitsTex.set_address(d_voxelBitsPtr, bitsSize)
            PrepareDiffusionVolume(int32(self.gridSize), self.diffusion.dst, 
                block = block, grid = grid, 
                texrefs = [voxelBitsTex, srcTex])
            self.diffusion.flipBuffers()
        self.PrepareDiffusionVolume = func
        
        MarkSinks = mod.get_function("MarkSinks")
        def func(d_sinkPos, mark):
            block = (256, 1, 1)
            n = len(d_sinkPos)
            MarkSinks( int32(self.gridSize), self.diffusion.src, float32(mark), int32(n), d_sinkPos,
              block = block, grid = ((n + block[0] - 1) /  block[0], 1) )
        self.MarkSinks = func
        
        FetchSinks = mod.get_function("FetchSinks")
        def func(d_sinkPos, d_absorb):
            block = (256, 1, 1)
            self.diffusion.src.bind_to_texref(srcTex)
            n = len(d_sinkPos)
            FetchSinks( int32(self.gridSize), int32(n), d_sinkPos, d_absorb,
              block = block, grid = ((n + block[0] - 1) /  block[0], 1), 
              texrefs = [srcTex])
        self.FetchSinks = func
        
        
    def getMeshArrays(self):
        self.positions = self.mesh.get_positions()
        self.normals = self.mesh.get_normals()
        self.faces = self.mesh.get_faces()

    @with_( profile("calcAbsorb") )
    def calcAbsorb(self):
        with glprofile('voxelize', log = (len(self.positions), len(self.faces))):
            with self.voxelizer:
                clearGLBuffers()
                s = 1.0 / self.gridSize
                glScale(s, s, s)
                drawArrays(GL_TRIANGLES, verts = self.positions, indices = self.faces)
            self.voxelizer.dumpToPBO()

        with cuprofile("PrepareDiffusionVolume", log = True):
            gl2cudaMap = self.gl2cudaBuf.map()
            self.PrepareDiffusionVolume(gl2cudaMap.device_ptr(), gl2cudaMap.size())
            gl2cudaMap.unmap()
        
        with cuprofile("Diffusion", log = True):
            d_sinks = ga.to_gpu(self.positions + self.normals * self.mouthDist)
            self.MarkSinks(d_sinks, Diffusion.SINK)
            for i in xrange(self.diffuseStepNum):
                self.diffusion.step()
            #self.MarkSinks(d_sinks, 0.0)
            self.diffusion.step(saturate=True)
            d_absorb = ga.zeros(len(d_sinks), float32)
            self.FetchSinks(d_sinks, d_absorb)
            absorb = d_absorb.get()

            '''
            ttt = self.diffusion.src.get()
            save('ttt', ttt)
            outLayer = ttt[-1]
            bottomLayer = ttt[0]
            outFlux = self.gridSize**2 - sum(outLayer)
            inFlux = sum(absorb) + sum(bottomLayer)
            print outFlux, inFlux, outFlux-inFlux
            #print sum(absorb)
            '''

        self.absorb = absorb / absorb.max()

    @with_( profile("growMesh", log=True) )
    def growMesh(self):
        mergeDist = 0.75 * self.coralliteSpacing
        splitDist = 1.5  * self.coralliteSpacing
        self.mesh.grow(mergeDist, splitDist, self.absorb*self.growCoef)
        self.getMeshArrays()

        
    @with_( profile("grow", log = True) )
    def grow(self):
        self.growMesh()
        self.calcAbsorb()
def grow(nr=3, textra=10):
    print("growing via anions")
    sim = Simulator().get_instance()
    gui = sim.gui()
    dt = 0.001  # s

    comp = []
    comp.append(
        Compartment("initial growth cone",
                    z=-0.85,
                    cli=0.00433925284075134,
                    ki=0.1109567493822927,
                    nai=0.0255226350779378,
                    length=5e-5,
                    radius=default_radius_short))

    # steady state
    sim.run(stop=100,
            dt=0.001,
            plot_update_interval=500,
            data_collect_interval=5,
            block_after=False)

    # set diffusion value
    cli_D = 2.03
    cli_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    ki_D = 1.96
    ki_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    nai_D = 1.33
    nai_D *= 1e-7

    #another compartment
    comp.append(comp[0].copy("compartment 1"))
    comp[1].L = 10e-5
    comp[1].w = np.pi * comp[1].r**2 * comp[1].L
    diffusion_object = [
        Diffusion(comp[0],
                  comp[1],
                  ions={
                      'cli': cli_D,
                      'ki': ki_D,
                      'nai': nai_D
                  })
    ]

    # heatmap incorporating compartment heights
    sc = 1e5
    htplot = Colormap("dendrite", comp[0].w + comp[1].w, comp)
    totalht, init_vals = htplot.smallheatmap(comp,
                                             sc,
                                             int(htplot.totalh * sc),
                                             all=0,
                                             init_val=None)

    # plot
    voltage_reversal_graph_comp = gui.add_graph() \
        .add_ion_conc(comp[0], "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(comp[0], "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(comp[0], line_style='k', y_units_scale=1000, y_plot_units='mV')

    volume_graph = gui.add_graph()
    volume_graph.add_var(volume_graph.time,
                         "time",
                         htplot,
                         "comp0w",
                         line_style='k')
    volume_graph.add_var(volume_graph.time,
                         "time",
                         htplot,
                         "totalh",
                         line_style='b')

    sim.run(continuefor=1,
            dt=dt * 0.001,
            plot_update_interval=0.5,
            data_collect_interval=textra / 16)

    # growth
    for i in range(nr):
        htplot.smallheatmap(comp,
                            sc,
                            totalht,
                            all=0,
                            init_val=init_vals,
                            name='graphs/grow_done' + str(i) + '.eps')
        comp[0].gx = 1

        # stop at certain length
        while comp[0].L < 15e-5:
            print("Fluxing compartment's length: " + str(comp[0].L))
            sim.run(continuefor=0.5,
                    dt=dt * 0.001,
                    plot_update_interval=0.25,
                    data_collect_interval=textra / 16)
            if 9.9e-5 < comp[0].L < 10.3e-5:
                htplot.smallheatmap(comp,
                                    sc,
                                    totalht,
                                    all=0,
                                    init_val=init_vals,
                                    name='graphs/grow_interim' + str(i) +
                                    '.eps')
        comp[0].gx = 0
        print_concentrations(comp, str(i))

        # split compartments
        comp.insert(0, comp[0].copy("compartment " + str(i)))
        comp[1].L -= 5e-5
        comp[0].L = 5e-5
        comp[0].w = np.pi * comp[0].r**2 * comp[0].L
        comp[1].w = np.pi * comp[1].r**2 * comp[1].L

        print_concentrations(comp, str(i))

        # update total height
        htplot.comp = comp

        # update diffusion
        diffusion_object.append(
            Diffusion(comp[0],
                      comp[1],
                      ions={
                          'cli': cli_D,
                          'ki': ki_D,
                          'nai': nai_D
                      }))

        for a in comp:
            print(a.name)

        for j in diffusion_object:
            print(j.name)

        sim.run(continuefor=10,
                dt=dt * 0.001,
                plot_update_interval=5,
                data_collect_interval=textra / 16)

    htplot.smallheatmap(comp,
                        sc,
                        totalht,
                        all=1,
                        init_val=init_vals,
                        name='graphs/grow_end.eps')
    sim.run(continuefor=4,
            dt=dt * 0.001,
            plot_update_interval=2,
            data_collect_interval=0.5)
    htplot.smallheatmap(comp,
                        sc,
                        totalht,
                        all=1,
                        init_val=init_vals,
                        name='graphs/grow_end.eps')

    return sim, gui
Example #19
0
class FirnDensitySpin:
    def __init__(self, configName):
        '''
        Sets up the initial spatial grid, time grid, accumulation rate, age, density, mass, stress, and temperature of the model run
        :param configName: name of json config file containing model configurations
        '''

        # load in json config file and parses the user inputs to a dictionary
        with open(configName, "r") as f:
            jsonString = f.read()
            self.c = json.loads(jsonString)

        # create directory to store results
        if os.path.exists(self.c['resultsFolder']):
            rmtree(self.c['resultsFolder'])
        os.makedirs(self.c['resultsFolder'])

        # read in initial temperatures and accumulation rates
        input_temp, input_year_temp = read_temp(self.c['InputFileNameTemp'])
        input_bdot, input_year_bdot = read_bdot(self.c['InputFileNamebdot'])

        # load in model parameters
        self.bdot0 = input_bdot[0]
        self.temp0 = input_temp[0]
        gridLen, dx, dt, t, stp, Ts, T_mean, bdotSec, rhos0 = self.define_parameters()

        # set up model grid
        gridheight = np.linspace(c['H'], c['HbaseSpin'], gridLen)
        self.z  = self.c['H'] - gridHeight
        self.dz = np.diff(self.z)
        self.dz = np.append(self.dz, self.dz[-1])

        # set up the initial age and density of the firn column using herron & langway analytic
        THL = input_temp[0]
        AHL = input_bdot[0]
        self.age, self.rho = hl_analytic(self.c['rhos0'], self.z, THL, AHL)

        # set up initial mass, stress, and mean accumulation rate
        self.mass  = self.rho * self.dz
        self.sigma = self.mass * dx * GRAVITY
        self.sigma = self.sigma.cumsum(axis = 0)
        self.mass_sum = self.mass.cumsum(axis = 0)
        self.bdot_mean = np.concatenate(([self.mass_sum[0] / (RHO_I * S_PER_YEAR)], self.mass_sum[1:] / (self.age[1:] * RHO_I / t)))

        # set up initial temperature grid as well as a class to handle heat/isotope diffusion
        init_Tz = input_temp[0] * np.ones(gridLen)
        self.diffu = Diffusion(self.z, stp, gridLen, init_Tz)

        # set up initial grain growth (if specified in config file)
        if self.c['physGrain']:
            if self.c['calcGrainSize']:
                r02 = -2.42e-9 * (self.c['Ts0']) + 9.46e-7
                self.r2 = r02 * np.ones(gridLen)
            else:
                self.r2 = np.linspace(self.c['r2s0'], (6 * self.c['r2s0']), gridLen)

    def time_evolve(self):
        '''
        Evolve the spatial grid, time grid, accumulation rate, age, density, mass, stress, and temperature through time
        based on the user specified number of timesteps in the model run. Updates the firn density using a user specified 
        '''
        
        # load in model parameters
        dx, dt, t, stp, Ts, T_mean, bdotSec, rhos0 = self.define_parameters()
        steps = 1 / t
        if not self.c['physGrain']:
            r2_time = None

        for iter in xrange(stp):
            # getting the right physics for firn density based on user input
            physics = {
                'HLdynamic',      HLdynamic,
                'HLSigfus',       HLSigfus,
                'Barnola1991',    Barnola1991,
                'Li2004',         Li2004,
                'Li2011',         Li2011,
                'Ligtenberg2011', Ligtenberg2011,
                'Arthern2010S',   Arthern2010S,
                'Simonsen2013',   Simonsen2013,
                'Morris2013',     MorrisHL2013,
                'Helsen2008',     Helsen2008,
                'Arthern2010T',   Arthern2010T,
                'Spencer2001',    Spencer2001,
                'Goujon2003',     Goujon2003,
            }

            parameters = {
                'HLdynamic',      [steps, gridLen, bdotSec, self.diffu.Tz, self.rho],
                'HLSigfus',       [steps, gridLen, bdotSec, self.diffu.Tz, self.rho, sigma],
                'Barnola1991',    [steps, gridLen, bdotSec, self.diffu.Tz, self.rho, sigma],
                'Li2004',         [steps, gridLen, bdotSec, T_mean, self.rho],
                'Li2011',         [steps, gridLen, bdotSec, self.bdot_mean, self.c['bdot_type'], self.diffu.Tz, T_mean, self.rho],
                'Ligtenberg2011', [steps, gridLen, bdotSec, self.bdot_mean, self.c['bdot_type'], self.diffu.Tz, T_mean, self.rho],
                'Arthern2010S',   [steps, gridLen, bdotSec, self.bdot_mean, self.c['bdot_type'], self.diffu.Tz, T_mean, self.rho],
                'Simonsen2013',   [steps, gridLen, bdotSec, self.bdot_mean, self.c['bdot_type'], self.diffu.Tz, T_mean, self.rho],
                'Morris2013',     [steps, gridLen, self.diffu.Tz, dt, self.rho, True, iter],
                'Helsen2008',     [steps, bdotSec, self.c['bdot_type'], self.bdot_mean, self.diffu.Tz, Ts, self.rho],
                'Arthern2010T',   [gridLen, self.diffu.Tz, self.rho, self.sigma, self.r2, self.c['physGrain']],
                'Spencer2001',    [],
                'Goujon2003',     [],
            }
            try:
                drho_dt = physics[self.c['physRho']](parameters[self.c['physRho']])
            except KeyError:
                default()

            # update density and age of firn
            self.age = np.concatenate(([0], self.age[:-1])) + dt
            self.rho = self.rho + dt * drho_dt
            self.rho  = np.concatenate(([rhos0[iter]], self.rho[:-1]))

            # update temperature grid and isotope grid if user specifies
            if self.c['heatDiff']:
                self.diffu.heatDiff(self.z, self.dz, Ts, self.rho)
            if self.c['heatDiff']:
                self.diffu.isoDiff(iter, self.z, self.dz, self.rho, self.c['iso'])

            # update model grid
            dzNew = bdotSec[iter] * RHO_I / rhos0[iter] * S_PER_YEAR
            self.dz = self.mass / self.rho * dx
            self.dz = np.concatenate(([dzNew], self.dz[:-1]))
            self.z = self.dz.cumsum(axis = 0)
            self.z = np.concatenate(([0], self.z[:-1]))

            # update mass, stress, and mean accumulation rate
            massNew = bdotSec[iter] * S_PER_YEAR * RHO_I
            self.mass = np.concatenate(([massNew], self.mass[:-1]))
            self.sigma = self.mass * dx * GRAVITY
            self.sigma = self.sigma.cumsum(axis = 0)
            self.mass_sum  = self.mass.cumsum(axis = 0)
            self.bdot_mean = np.concatenate(([self.mass_sum[0] / (RHO_I * S_PER_YEAR)], self.mass_sum[1:] * t / (self.age[1:] * RHO_I)))

            # update grain radius
            if self.c['physGrain']:
                self.r2 = grainGrowth(self.diffu.Tz, Ts, iter, dt)

            # write results at the end of the time evolution
            if (iter == (stp - 1)):
                rho_time = np.concatenate(([t * iter + 1], self.rho))
                Tz_time  = np.concatenate(([t * iter + 1], self.diffu.Tz))
                age_time = np.concatenate(([t * iter + 1], self.age))
                z_time   = np.concatenate(([t * iter + 1], self.z))
                if self.c['physGrain']:
                    r2_time = np.concatenate(([t * iter + 1], self.r2))

                write_spin(self.c['resultsFolder'], self.c['physGrain'], rho_time, Tz_time, age_time, z_time, r2_time)

    def define_parameters(self):
        '''
        Return the parameters used in the model, for the initialization as well as the time evolution

        :returns gridLen: size of grid used in the model run
                         (unit: number of boxes, type: int)
        :returns dx: vector of width of each box, used for stress calculations
                    (unit: ???, type: array of ints)
        :returns dt: number of seconds per time step
                    (unit: seconds, type: float)
        :returns t: number of years per time step
                   (unit: years, type: float)
        :returns stp: total number of steps in the model run
                     (unit: number of steps, type: int)
        :returns T_mean: interpolated temperature vector based on the model time and the initial user temperature data
                        (unit: ???, type: array of floats)
        :returns Ts: interpolated temperature vector based on the model time & the initial user temperature data
                    may have a seasonal signal imposed depending on number of years per time step (< 1)
                    (unit: ???, type: array of floats)
        :returns bdotSec: accumulation rate vector at each time step
                         (unit: ???, type: array of floats)
        :returns rhos0: surface accumulate rate vector
                       (unit: ???, type: array of floats)
        '''

        gridLen = int((self.c['H'] - self.c['HbaseSpin']) / (self.bdot0 / self.c['stpsPerYearSpin']))
        dx = np.ones(gridLen)

        zz = np.min(z[self.rho > 850.0])
        years = int(zz / self.bdot0)
        stp = int(years * self.c['stpsPerYearSpin'])
        dt = years * S_PER_YEAR / stp
        t  = 1.0 / self.c['stpsPerYearSpin']

        if self.c['stpsPerYearSpin'] or (stp / years) >= 1.:
            Ts = self.temp0 * np.ones(stp)
        else:
            TPeriod = self.c['yearSpin']
            Ts = self.temp0 + self.c['TAmp'] * (np.cos(2 * np.pi * np.linspace(0, TPeriod, stp)) + 0.3 * np.cos(4 * np.pi * np.linspace(0, TPeriod, stp)))
        T_mean = Ts

        bdotSec0 = self.bdot0 / S_PER_YEAR / self.c['stpsPerYearSpin']
        bdotSec = bdotSec0 * np.ones(stp)

        rhos0 = self.c['rhos0'] * np.ones(stp)

        return gridLen, dx, dt, t, stp, Ts, T_mean, bdotSec, rhos0
Example #20
0
def main():

	arg_parser = argparse.ArgumentParser()
	arg_parser.add_argument('train_images_path')
	arg_parser.add_argument('test_images_path')
	arg_parser.add_argument('predictions_path')
	args = arg_parser.parse_args()

	imsize=480
	train_path=args.train_images_path
	test_path=args.test_images_path
	outfile=args.predictions_path
	##read data##
	data_list=os.listdir(ipath)
	train_images = get_imlist(train_path)
	test_images = get_imlist(test_path)
	##RAMAC##
	RAMAC = extract_feature(train_images, 'resnet101', imsize)
	RAMAC_test = extract_feature(test_images, 'resnet101', imsize)
	##UEL##
	normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
	transform_train = transforms.Compose([
        transforms.ToPILImage(),
        transforms.RandomCrop(size=224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        normalize,
    ])
	transform_test = transforms.Compose([
        transforms.ToPILImage(),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        normalize,
    ])
	net = resnet101(pretrained=True,low_dim=128)
	model_path = './model/UEL.t'#After training UEL
	net.load_state_dict(torch.load())
	
	imset = DataLoader(path = train_path, transform=transform_test)
	train_loader = torch.utils.data.DataLoader(imset, batch_size=32, shuffle=False, num_workers=0)
	UEL = obtainf(net, train_loader)
	
	imset = DataLoader(path = test_path, transform=transform_test)
	test_loader = torch.utils.data.DataLoader(imset, batch_size=32, shuffle=False, num_workers=0)
	UEL_test = obtainf(net, test_loader)
	##GEM##
	image_size=1024
	multiscale='[1, 2**(1/2), 1/2**(1/2)]'
	state = torch.load('./model/retrievalSfM120k-vgg16-gem-b4dcdc6.pth')
	
	net_params = {}
	net_params['architecture'] = state['meta']['architecture']
	net_params['pooling'] = state['meta']['pooling']
	net_params['local_whitening'] = state['meta'].get('local_whitening', False)
	net_params['regional'] = state['meta'].get('regional', False)
	net_params['whitening'] = state['meta'].get('whitening', False)
	net_params['mean'] = state['meta']['mean']
	net_params['std'] = state['meta']['std']
	net_params['pretrained'] = False
	# load network
	net = init_network(net_params)
	net.load_state_dict(state['state_dict'])
        
	# if whitening is precomputed
	if 'Lw' in state['meta']:
		net.meta['Lw'] = state['meta']['Lw']
	ms = list(eval(multiscale))
	msp = net.pool.p.item()
	
	net.cuda()
	net.eval()
	# set up the transform
	normalize = transforms.Normalize(
		mean=net.meta['mean'],
		std=net.meta['std']
    )
	transform = transforms.Compose([
        transforms.ToTensor(),
        normalize
    ])
	
	GEM = extract_vectors(net,train_images , 480, transform, ms=ms, msp=msp).numpy().T
	GEM_test = extract_vectors(net,test_images , 480, transform, ms=ms, msp=msp).numpy().T
	##Retrieval##
	feats=np.concatenate((RAMAC,UEL,GEM),axis=1).astype('float32')
	query_feat=np.concatenate((RAMAC_test, UEL_test,GEM_test),axis=1).astype('float32')
	##diffusion##
	kq, kd = 7, 50
	gamma=80
	diffusion = Diffusion(feats, '/')
	offline = diffusion.get_offline_results(1024, kd)
	print('[search] 1) k-NN search')
	sims, ids = diffusion.knn.search(query_feat, kq)
	sims = sims ** gamma
	qr_num = ids.shape[0]
	print('[search] 2) linear combination')
	all_scores = np.empty((qr_num, 7), dtype=np.float32)
	all_ranks = np.empty((qr_num, 7), dtype=np.int)
	for i in range(qr_num):
		scores = sims[i] @ offline[ids[i]]
		parts = np.argpartition(-scores, 7)[:7]
		ranks = np.argsort(-scores[parts])
		all_scores[i] = scores[parts][ranks]
		all_ranks[i] = parts[ranks]
	I = all_ranks
	##output##
	out=pd.DataFrame(list(map(lambda x: x.split('/')[-1].split('.jpg')[0],timages)))
	out['1']=pd.DataFrame(I)[0].map(lambda x:data_list[x].split('.')[0] )
	out['2']=pd.DataFrame(I)[1].map(lambda x:data_list[x].split('.')[0] )
	out['3']=pd.DataFrame(I)[2].map(lambda x:data_list[x].split('.')[0] )
	out['4']=pd.DataFrame(I)[3].map(lambda x:data_list[x].split('.')[0] )
	out['5']=pd.DataFrame(I)[4].map(lambda x:data_list[x].split('.')[0] )
	out['6']=pd.DataFrame(I)[5].map(lambda x:data_list[x].split('.')[0] )
	out['7']=pd.DataFrame(I)[6].map(lambda x:data_list[x].split('.')[0] )
	out.to_csv(outfile,index=None,header=None)
Example #21
0
                        required=True,
                        help="""
                        Path to gallery features
                        """)
    parser.add_argument('--ground_truth_path',
                        type=str,
                        help="""
                        Path to ground-truth
                        """)
    parser.add_argument('-n',
                        '--truncation_size',
                        type=int,
                        default=1000,
                        help="""
                        Number of images in the truncated gallery
                        """)
    args = parser.parse_args()
    args.kq, args.kd = 10, 50
    return args


if __name__ == "__main__":
    args = parse_args()
    if not os.path.isdir(args.cache_dir):
        os.makedirs(args.cache_dir)
    if args.ground_truth_path:
        evaluator = OxfordParisEvaluator(args.ground_truth_path)
    dataset = Dataset(args.query_path, args.gallery_path)
    diffusion = Diffusion(dataset.gallery, args.cache_dir)
    search(args)
Example #22
0
  numpyart  = 108 # The total number of particles
  R0     = [0.7]#, 0.4, 0.9]     # Initial density
  fil = 50    # Sampling every 'filter' steps.
  T     = [0.5]#, 0.8, 1.0, 1.2]
  dt    = 0.001    # Stepsize
  stepnumber = 20000

  velDistributions = [] 
  for t in T:
      for r in R0:
          print "*"*50, 'r=', r, 'T=', t
          print 'r=', r, 'T=', t
          simulation = MDSim( numpyart, r, t, lennardjones, dt ) 
          simulation.run( stepnumber, t) 
          diff = Diffusion(simulation, fil, dt) 
          diff.calcMSD()
          diff.calcVACF() 

          #diff.plotMSD()
          #diff.plotVACF() 

          diff.output() 
          print 'diff.msd', diff.msd 
          print "diff.vacf", diff.vacf 

          linreg = diff.linReg()
          int = diff.integrate() 

          print "diff.msd", linreg[0]/6.0
          print "diff.vacf", int/3.0 
 def test_diffusion_compartments(self, **kwargs):
     self.d = Diffusion(self.comp, self.comp2, self.ions)
     self.run_diffusion(300, False, **kwargs)
Example #24
0
    def diffusion(
        self,
        slice_time,
        synergy,
        states=None,
        time_stamps=None,
        nb_steps=1,
        infected_node=1,
        cooperator_ratio=0.0,
        strategies=None,
        payoffs=None
        ):

        from diffusion import Diffusion
        from pgg import PublicGoodGames
        
        G = self._slices[int(slice_time)]['graph'].copy()

        if states == None or time_stamps == None:
            # Initialize the Public Good Game 
            PGG = PublicGoodGames(G=G,
                    cooperator_ratio=cooperator_ratio, nb_simulation_step=nb_steps, synergy=synergy)

            # Get the startegies list
            strategies = PGG.get_strategies()

            # Initialize the disffusion process with the startegies list
            D = Diffusion(G=G, nb_simulation_step=nb_steps,
                          time_step=self._time_step,
                          initial_time_stamp=slice_time,
                          strategies=strategies)
            
            # Run the diffusion Process
            D.run_steps(infected_node)

            # Run the Game  
            PGG.run_game()

            # Return the strategies list, the payoffs list, the timestamps list and the states list 
            return (D.get_states(), D.get_time_stamps(), PGG.get_strategies(), PGG.get_payoffs())
        elif strategies != None and payoffs != None:

            D = Diffusion(G=G, nb_simulation_step=nb_steps,
                          time_step=self._time_step,
                          initial_time_stamp=slice_time,
                          strategies=strategies)

            PGG = PublicGoodGames(G=G, nb_simulation_step=nb_steps, synergy=synergy)
            
            PGG.set_strategies(strategies)
            PGG.set_payoffs(payoffs)

            D.set_nodes_states(states)
            D.set_node_time_stamps(time_stamps)

            # Run the diffusion Process
            D.run_steps(infected_node)
            # Run the Game  
            PGG.run_game()
            # Return the strategies list, the payoffs list, the timestamps list and the states list 
            return (D.get_states(), D.get_time_stamps(), PGG.get_strategies(), PGG.get_payoffs())
Example #25
0
    def __init__(self, configName):
        '''
        Sets up the initial spatial grid, time grid, accumulation rate, age, density, mass, stress, temperature, and diffusivity of the model run
        :param configName: name of json config file containing model configurations
        '''

        # load in json config file and parses the user inputs to a dictionary
        with open(configName, "r") as f:
            jsonString = f.read()
            self.c = json.loads(jsonString)

        # read in initial depth, age, density, temperature
        initDepth, initAge, initDensity, initTemp = read_init(self.c['resultsFolder'])

        # set up the initial age and density of the firn column
        self.age     = initAge[1:]
        self.rho     = initDensity[1:]

        # set up model grid
        self.z  = initDepth[1:]
        self.dz = np.diff(self.z)
        self.dz = np.append(self.dz, self.dz[-1])

        # load in model parameters
        gridLen, dx, dt, t, modeltime, years, stp, Ts, T_mean, bdot, bdotSec, rhos0, D_surf = self.define_parameters()

        # set up the initial grid of diffusivity constant
        self.Dcon = self.c['D_surf'] * np.ones(gridLen)

        # set up vector of times data will be written
        self.TWrite = modeltime[INTE::INTE]

        # set up initial mass, stress, and mean accumulation rate
        self.mass = self.rho * self.dz
        self.sigma = self.mass * dx * GRAVITY
        self.sigma = self.sigma.cumsum(axis = 0)
        self.mass_sum = self.mass.cumsum(axis = 0)
        self.bdot_mean = np.concatenate(([self.mass_sum[0] / (RHO_I * S_PER_YEAR)], self.mass_sum[1:] / (self.age[1:] * RHO_I / t)))

        # set up class to handle heat/isotope diffusion using user provided data for initial temperature vector
        self.diffu = Diffusion(self.z, stp, gridLen, initTemp[1:])

        # set up initial values for density, temperature, age, depth, diffusivity, Clim??, and accumulation to write
        rho_time  = np.append(modeltime[0], self.rho)
        Tz_time   = np.append(modeltime[0], self.diffu.Tz)
        age_time  = np.append(modeltime[0], self.age)
        z_time    = np.append(modeltime[0], self.z)
        D_time    = np.append(modeltime[0], self.Dcon)
        Clim_time = np.append(modeltime[0], [bdot[0], Ts[0]])  # not sure if bdot or bdotSec
        bdot_time = np.append(modeltime[0], self.bdot_mean)

        # set up initial grain growth (if specified in config file)
        if self.c['physGrain']:
            initr2 = np.genfromtxt(r2Path, delimiter = ',')
            self.r2 = initr2
            r20 = r2
            r2_time = np.append(modeltime[0], self.r2)
        else:
            r2_time = None

        # write initial values to the results folder
        write_nospin(self.c['resultsFolder'], self.c['physGrain'], rho_time, Tz_time, age_time, z_time, D_time, Clim_time, bdot_time, r2_time)

        # set up initial values for bubble close-off depth & age, lock-in zone depth & age, and depth integrated porosity
        self.bcoAgeMartAll = []
        self.bcoDepMartAll = []
        self.bcoAge815All  = []
        self.bcoDep815All  = []
        self.LIZAgeAll     = []
        self.LIZDepAll     = []
        self.intPhiAll     = []

        update_BCO()
        update_LIZ()
        update_DIP()