def multiple_continuum_realizations(grid_path, save_folder, perm_path, dp_x, dp_y, n_particles,
                                    n_steps, n_runs, n_combine, n_buffer_ly = 23, ly = 4, std = 1.0):
    # loading the grid
    with open(grid_path, 'r') as input:
        grid = pickle.load(input)
    # load the permeability dataframe, for now reading all of them at the same time
    perm_frame = pd.read_csv(perm_path, usecols=range(n_runs))
    # initialize a linear system for the pressure fluctuations for the grid
    LS = LSGridPeriodicPurturbations(grid)
    # for the number of specified realizations run particle tracking and save the results
    big_x, big_y, big_t = (np.empty((0, n_steps + 1)) for i in range(3))
    counter = 0
    for i in range(n_runs):
        perm = np.exp(std * perm_frame.ix[:, i])
        grid.set_transmissibility(perm)
        # solve for fluctuations around mean pressure gradient
        # setting the left hand side of the equation
        LS.fill_matrix(grid.transmissibility)
        # for each cell add (dp_x/lx)*(T_down - T_up)_x + (dp_y/ly)*(T_down - T_up)_y
        # to the rhs
        rhs_vec = LS.periodic_rhs_vec(dp_x, dp_y)
        LS.rhs.set_neumann_pores_distributed(range(grid.nr_p), rhs_vec)
        # set a dirichlet cell: no fluctuation for cell 0
        LS.set_dirichlet_pores([0], 0.0)
        LS.solve()
        # perform particle tracking
        grid.pressure = LS.sol
        grid.face_velocities = LS.set_face_velocity(dp_x, dp_y)
        u, v = LS.get_cell_velocity()
        dn1 = dispersionSystemContinua(grid, n_particles, n_steps, tracking_type='exit')
        # dn1.init_particles_left_buffered(30)
        # dn1.init_particles_left_boundary()
        dn1.init_particles_ly_distance(n_buffer_ly, ly)
        dn1.follow_all_particles()
        # we want to combine the data for n_combine realizations as save them together
        # for more efficient save and load operations
        if (i > 0 and (i + 1) % n_combine == 0) or i + 1 == n_runs:
            # save the arrays
            print 'current realization: ', i, ', saving combined data'
            file_name = 'real_' + str(counter) + '.pkl'
            save_path = os.path.join(save_folder, file_name)
            # np.savez(save_path, x_array=big_x, y_array=big_y, t_array=big_t)
            save_results_pickle(save_path, big_x, big_y, big_t)
            # initialize the big arrays
            big_x, big_y, big_t = (np.empty((0, n_steps + 1)) for i in range(3))
            counter += 1
            # save useful information about the case
            total_n_particles = n_runs * n_particles
        else:
            # append to what we have sofar
            big_t = np.vstack((big_t, dn1.time_array))
            big_x = np.vstack((big_x, dn1.x_array))
            big_y = np.vstack((big_y, dn1.y_array))
    n_files = counter + 1
    save_case_info(save_folder, n_runs, n_particles, n_combine, n_files, n_steps)
 def test_init_particles_left_buffered(self):
     grid = structuredGrid(4, 3, 1.0, 1.0, boundaryType='full-periodic')
     n_particles = 2
     n_steps = 3
     ds1 = dispersionSystemContinua(grid,
                                    n_particles,
                                    n_steps,
                                    tracking_type='exit')
     ds1.init_particles_left_buffered(1)
     self.assertListEqual(list(ds1.cell_nr_array[:, 0]), [1, 2])
     expected_x = [0.5, 0.5]
     expected_y = [2.5, 1.5]
     self.assertListEqual(list(ds1.x_array[:, 0]), expected_x)
     self.assertListEqual(list(ds1.y_array[:, 0]), expected_y)
 def test_find_exit_conditions(self):
     grid = structuredGrid(4, 3, 1.0, 1.0, boundaryType='full-periodic')
     # set face velocities for grid
     grid.face_velocities = np.zeros(grid.nr_t)
     ngh_faces = [0, 2, 3, 1]
     ngh_cells = [8, 4, 1, 3]
     vl, vr = 4.0, 4.0
     vb, vt = 1.0, 1.0
     grid.face_velocities[ngh_faces] = [vl, vr, vb, vt]
     # starting position center of left face
     xs = grid.pores.x[0] - grid.dx / 2
     ys = grid.pores.y[0]
     ds1 = dispersionSystemContinua(grid, 1, 1, tracking_type='exit')
     exit_cell, exit_face, xe, ye, te = ds1.find_exit_conditions(
         0, xs, ys, 0.0)
     self.assertListEqual([exit_cell, xe, ye, te], [4, 1.0, 3.75, 0.25])
 def test_find_exit_conditions_1d(self):
     # case1: v1<v2 and both are positive
     grid = structuredGrid(4, 3, 1.0, 1.0, boundaryType='full-periodic')
     dx, x1, x2 = 1.0, 0.0, 1.0
     f1, f2 = 0, 1
     ds1 = dispersionSystemContinua(grid, 1, 1, tracking_type='exit')
     x = 0.0
     v1 = 10.0
     v2 = 12.0
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertListEqual([exit, exit_idx, xe, v, ve],
                          [True, f2, x2, v1, v2])
     self.assertAlmostEqual(dt, 0.0911, places=3)
     # case2: v1>v2 and both are negative
     x = 1.0
     v1 = -1.0
     v2 = -2.0
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertListEqual([exit, exit_idx, xe, v, ve],
                          [True, f1, x1, v2, v1])
     self.assertAlmostEqual(dt, 0.693, places=3)
     # case3: v1==v2 and both are negative
     x = 0.5
     v1 = -2.0
     v2 = v1
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertListEqual([exit, exit_idx, xe, v, ve],
                          [True, f1, x1, v2, v2])
     self.assertEqual(dt, 0.25)
     # case4: v1==v2 and both are positive
     x = 0.5
     v1 = 2.0
     v2 = v1
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertListEqual([exit, exit_idx, xe, v, ve],
                          [True, f2, x2, v2, v2])
     self.assertEqual(dt, 0.25)
     # case5: v1>0 v2<0 no way to get out
     x = 0.5
     v1 = 2.0
     v2 = -1.0
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertEqual(exit, False)
     # case5: v1<0 and v2>0, on the right side of the stagnation plane
     x = 0.7
     v1 = -1.0
     v2 = 1.0
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertListEqual([exit, exit_idx, xe, ve], [True, f2, x2, v2])
     self.assertAlmostEqual(dt, 0.458145, places=4)
     # case6: v1<0 and v2>0, on the left side of the stagnation plane
     x = 0.3
     v1 = -1.0
     v2 = 1.0
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertListEqual([exit, exit_idx, xe, ve], [True, f1, x1, v1])
     self.assertAlmostEqual(dt, 0.458145, places=4)
     # case7: v1<0 and v2>0, on the stagnation plane
     x = 0.5
     v1 = -1.0
     v2 = 1.0
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertEqual(exit, False)
     # case8: v1=0 and v2>0
     x = 0.5
     v1 = 0.0
     v2 = 1.0
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertListEqual([exit, exit_idx, xe, ve], [True, f2, x2, v2])
     # case9: v2=0 and v1<0
     x = 0.5
     v1 = -1.0
     v2 = 0.0
     exit, exit_idx, xe, v, ve, dt = ds1.find_exit_conditions_1d(
         dx, x, x1, f1, v1, x2, f2, v2)
     self.assertListEqual([exit, exit_idx, xe, ve], [True, f1, x1, v1])