def main(): # Split the communicator into space and time communicator comm_world = MPI.COMM_WORLD comm_x, comm_t = split_communicator(comm_world, 2) # Define spatial domain # The domain is a 10x10 square with periodic boundary conditions in each direction. n = 20 mesh = PeriodicSquareMesh(n, n, 10, comm=comm_x) # Set up the problem diffusion0 = Diffusion2D(mesh=mesh, kappa=0.1, comm_space=comm_x, t_start=0, t_stop=10, nt=17) diffusion1 = Diffusion2D(mesh=mesh, kappa=0.1, comm_space=comm_x, t_start=0, t_stop=10, nt=9) # Setup three-level MGRIT solver with the space and time communicators and # solve the problem mgrit = Mgrit(problem=[diffusion0, diffusion1], comm_time=comm_t, comm_space=comm_x) info = mgrit.solve()
def run_mgrit(nt, space_procs, coarsening, freq=1, a=1.0, t_start=0, t_stop=1, cycle='V'): size = MPI.COMM_WORLD.Get_size() comm_x, comm_t = split_communicator(MPI.COMM_WORLD, space_procs) nx = ny = 65 dmda_coarse = PETSc.DMDA().create([nx, ny], stencil_width=1, comm=comm_x) dmda_fine = dmda_coarse.refine() t_interval = np.linspace(t_start, t_stop, nt) time_procs = int(size / space_procs) problem = [HeatPetsc(dmda=dmda_fine, comm_x=comm_x, freq=freq, a=a, t_interval=t_interval)] for i in range(0, len(coarsening)): problem.append(HeatPetsc(dmda=dmda_fine, comm_x=comm_x, freq=freq, a=a, t_interval=t_interval[::np.prod(coarsening[:i + 1], dtype=int)])) nested_iteration = True if len(coarsening) > 0 else False if cycle == 'V': mgrit = Mgrit(problem=problem, comm_time=comm_t, comm_space=comm_x, nested_iteration=nested_iteration) else: mgrit = Mgrit(problem=problem, comm_time=comm_t, comm_space=comm_x, nested_iteration=nested_iteration, cycle_type='F', cf_iter=0) info = mgrit.solve() return info['time_setup'], info['time_solve'], info['time_setup'] + info['time_solve']
def main(): def output_fcn(self): # Set path to solution path = 'results/petsc' # Create path if not existing pathlib.Path(path).mkdir(parents=True, exist_ok=True) # Save solution with corresponding time point to file np.save(path + '/petsc' + str(self.comm_time_rank) + str(self.comm_space_rank), [[[self.t[0][i], self.comm_space_rank, self.u[0][i].get_values().getArray()] for i in self.index_local[0]]]) # Split the communicator into space and time communicator comm_world = MPI.COMM_WORLD comm_x, comm_t = split_communicator(comm_world, 4) # Create PETSc DMDA grids nx = 129 ny = 129 dmda_coarse = PETSc.DMDA().create([nx, ny], stencil_width=1, comm=comm_x) dmda_fine = dmda_coarse.refine() # Set up the problem heat_petsc_0 = HeatPetsc(dmda=dmda_fine, comm_x=comm_x, freq=1, a=1.0, t_start=0, t_stop=1, nt=33) heat_petsc_1 = HeatPetsc(dmda=dmda_coarse, comm_x=comm_x, freq=1, a=1.0, t_interval=heat_petsc_0.t[::2]) heat_petsc_2 = HeatPetsc(dmda=dmda_coarse, comm_x=comm_x, freq=1, a=1.0, t_interval=heat_petsc_1.t[::2]) # Setup three-level MGRIT solver with the space and time communicators and # solve the problem mgrit = Mgrit(problem=[heat_petsc_0, heat_petsc_1, heat_petsc_2], transfer=[GridTransferPetsc(fine_prob=dmda_fine, coarse_prob=dmda_coarse), GridTransferCopy()], comm_time=comm_t, comm_space=comm_x, output_fcn=output_fcn) info = mgrit.solve() import time if comm_t.Get_rank() == 0: time.sleep(1) sol = [] path = 'results/petsc/' for filename in os.listdir(path): data = np.load(path + filename, allow_pickle=True).tolist()[0] sol += data sol = [item for item in sol if item[1] == comm_x.Get_rank()] sol.sort(key=lambda tup: tup[0]) u_e = heat_petsc_0.u_exact(t=heat_petsc_0.t[-1]).get_values().getArray() diff = sol[-1][2] - u_e print('Difference at time point', heat_petsc_0.t[-1], ':', np.linalg.norm(diff, np.inf), '(space rank',comm_x.Get_rank() , ')')
def run_timestepping(nt, space_procs, freq=1, a=1.0, t_start=0, t_stop=1): comm_x, comm_t = split_communicator(MPI.COMM_WORLD, space_procs) nx = ny = 65 dmda_coarse = PETSc.DMDA().create([nx, ny], stencil_width=1, comm=comm_x) dmda_fine = dmda_coarse.refine() t_interval = np.linspace(t_start, t_stop, nt) heat_petsc_0 = HeatPetsc(dmda=dmda_fine, comm_x=comm_x, freq=freq, a=a, t_interval=t_interval) import time start = time.time() sol = heat_petsc_0.vector_t_start for i in range(1, len(heat_petsc_0.t)): sol = heat_petsc_0.step(u_start=sol, t_start=heat_petsc_0.t[i - 1], t_stop=heat_petsc_0.t[i]) info = {'time_setup': 0, 'time_solve': time.time() - start} return info['time_setup'], info['time_solve'], info['time_setup'] + info['time_solve']