def test_brusselator2d(): problem = Brusselator2d() n = problem.mesh.control_volumes.shape[0] u0 = np.zeros((2, n)) b0 = 0.0 plt.ion() fig = plt.figure() ax = fig.add_subplot(111) plt.axis("square") plt.xlabel("b") plt.ylabel("$||u||_\\infty$") b_list = [] values_list = [] (line1,) = ax.plot(b_list, values_list, "-", color="#1f77f4") def callback(k, b, sol): b_list.append(b) line1.set_xdata(b_list) values_list.append(np.max(np.abs(sol))) line1.set_ydata(values_list) ax.set_xlim(0.0, 200.0) ax.set_ylim(0.0, 40.0) fig.canvas.draw() fig.canvas.flush_events() # Store the solution u, v = sol meshio.write_points_cells( f"sol{k:03d}.vtk", problem.mesh.points, {"triangle": problem.mesh.cells["points"]}, point_data={"u": u, "v": v}, ) pacopy.natural(problem, u0, b0, callback, max_steps=100)
def test_allen_cahn(): problem = AllenCahn() n = problem.mesh.control_volumes.shape[0] u0 = numpy.zeros(n, dtype=float) # delta0 = 0.04 delta0 = 0.2 plt.ion() fig = plt.figure() ax = fig.add_subplot(111) plt.axis("square") plt.xlabel("$\\mu$") plt.ylabel("$||u||_2^2 / |\\Omega|$") plt.grid() b_list = [] values_list = [] (line1, ) = ax.plot(b_list, values_list, "-", color="#1f77f4") area = numpy.sum(problem.mesh.control_volumes) def callback(k, b, sol): b_list.append(b) line1.set_xdata(b_list) values_list.append(problem.inner(sol, sol) / area) line1.set_ydata(values_list) ax.set_xlim(0.0, 1.0) ax.set_ylim(0.0, 1.0) ax.invert_yaxis() fig.canvas.draw() fig.canvas.flush_events() # Store the solution meshio.write_points_cells( f"sol{k:03d}.vtk", problem.mesh.node_coords, {"triangle": problem.mesh.cells["nodes"]}, point_data={"u": sol}, ) # input("Press") pacopy.natural( problem, u0, delta0, callback, max_steps=6, lambda_stepsize0=-1.0e-1, max_newton_steps=16, newton_tol=1.0e-10, )
def solve(self): if self.cont_method == 'Euler-Newton': pacopy.euler_newton(self, self.u0, self.lmbda0, self.callback, max_steps=self.max_steps, newton_tol=self.newton_tol) elif self.cont_method == 'Natural': pacopy.natural(self, self.u0, self.lmbda0, self.callback, max_steps=self.max_steps, newton_tol=self.newton_tol)
reynolds: float, uvp: np.ndarray, milestones=Iterable[float]): """Echo the Reynolds number and plot streamlines at milestones""" print(f'Re = {reynolds}') if reynolds in milestones: psi[reynolds] = bfs.streamfunction(bfs.split(uvp)[0]) if __name__ == '__main__': ax = bfs.streamlines(psi[reynolds]) ax.set_title(f'Re = {reynolds}') ax.get_figure().savefig(Path(__file__).with_name( f'{Path(__file__).stem}-{reynolds}-psi.png'), bbox_inches="tight", pad_inches=0) if __name__ == '__main__': milestones = [50., 150., 450., 750.] else: milestones = [50.] natural(bfs, bfs.make_vector(), 0., partial(callback, milestones=milestones), lambda_stepsize0=50., milestones=milestones)
def test_bratu(max_steps=10, update_plot=False): problem = Bratu1d() u0 = numpy.zeros(problem.n) lmbda0 = 0.0 # https://stackoverflow.com/a/4098938/353337 plt.ion() fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax1.set_xlabel("$\\lambda$") ax1.set_ylabel("$||u||_2$") ax1.set_xlim(0.0, 4.0) ax1.set_ylim(0.0, 6.0) ax1.grid() (line1, ) = ax1.plot([], [], "-x", color="C0") ax2 = fig.add_subplot(1, 2, 2) ax2.set_title("solution") ax2.set_xlim(0.0, 1.0) ax2.set_ylim(0.0, 5.0) ax2.grid() (line2, ) = ax2.plot([], [], "-", color="C0") line2.set_xdata(numpy.linspace(0.0, 1.0, problem.n)) # line3, = ax2.plot([], [], "-", color="C1") # line3.set_xdata(numpy.linspace(0.0, 1.0, problem.n)) milestones = numpy.arange(0.5, 3.2, 0.5) def callback(k, lmbda, sol): if update_plot: line1.set_xdata(numpy.append(line1.get_xdata(), lmbda)) # val = numpy.max(numpy.abs(sol)) val = numpy.sqrt(problem.inner(sol, sol)) line1.set_ydata(numpy.append(line1.get_ydata(), val)) # ax1.plot( # [lmbda_pre], [numpy.sqrt(problem.inner(u_pre, u_pre))], ".", color="C1" # ) line2.set_ydata(sol) # line3.set_ydata(du_dlmbda) fig.canvas.draw() fig.canvas.flush_events() # plt.savefig('bratu1d.png', transparent=True, bbox_inches="tight") pacopy.natural( problem, u0, lmbda0, callback, max_steps=max_steps, newton_tol=1e-10, milestones=milestones, ) # The condition number of the Jacobian is about 10^4, so we can only expect Newton # to converge up to about this factor above machine precision. pacopy.euler_newton(problem, u0, lmbda0, callback, max_steps=max_steps, newton_tol=1.0e-10)
bfs = BackwardFacingStep(lcar=.2) def callback(_: int, reynolds: float, uvp: np.ndarray, name: str, milestones=Iterable[float]): """Echo the Reynolds number and plot streamlines at milestones""" print(f'Re = {reynolds}') if reynolds in milestones: ax = bfs.streamlines(bfs.streamfunction(bfs.split(uvp)[0])) ax.set_title(f'Re = {reynolds}') ax.get_figure().savefig(f'{name}-{reynolds}-psi.png', bbox_inches="tight", pad_inches=0) if __name__ == '__main__': from os.path import splitext from sys import argv milestones = [50., 150., 450., 750.] natural(bfs, bfs.make_vector(), 0., partial(callback, name=splitext(argv[0])[0], milestones=milestones), lambda_stepsize0=50., milestones=milestones)
def test_bratu(max_steps=10, update_plot=False): problem = Bratu1d() u0 = numpy.zeros(problem.n) lmbda0 = 0.0 plt.ion() fig = plt.figure() ax1 = fig.add_subplot(111) ax1.set_xlabel("$\\lambda$") ax1.set_ylabel("$||u||_2$") ax1.grid() # ax2 = fig.add_subplot(122) # ax2.grid() (line1,) = ax1.plot([], [], "-x", color="#1f77f4") # line2, = ax2.plot([], [], "-", color="#1f77f4") # line2.set_xdata(numpy.linspace(0.0, 1.0, problem.n)) # line3, = ax2.plot([], [], "-", color="red") # line3.set_xdata(numpy.linspace(0.0, 1.0, problem.n)) milestones = numpy.arange(0.5, 3.2, 0.5) if update_plot: profile_fig, profile_ax = plt.subplots() def callback(k, lmbda, sol): if update_plot: line1.set_xdata(numpy.append(line1.get_xdata(), lmbda)) # val = numpy.max(numpy.abs(sol)) val = numpy.sqrt(problem.inner(sol, sol)) line1.set_ydata(numpy.append(line1.get_ydata(), val)) ax1.set_xlim(0.0, 4.0) ax1.set_ylim(0.0, 6.0) # ax1.plot([lmbda_pre], [numpy.sqrt(problem.inner(u_pre, u_pre))], ".r") # line2.set_ydata(sol) # line3.set_ydata(du_dlmbda) # ax2.set_xlim(0.0, 1.0) # ax2.set_ylim(0.0, 6.0) fig.canvas.draw() fig.canvas.flush_events() # plt.savefig('bratu1d.png', transparent=True, bbox_inches="tight") if lmbda in milestones: profile_ax.plot(numpy.linspace(0.0, 1.0, problem.n), sol, label=lmbda) pacopy.natural( problem, u0, lmbda0, callback, max_steps=max_steps, newton_tol=1e-10, milestones=milestones, ) # The condition number of the Jacobian is about 10^4, so we can only expect Newton # to converge up to about this factor above machine precision. pacopy.euler_newton( problem, u0, lmbda0, callback, max_steps=max_steps, newton_tol=1.0e-10 )