def test_continuation_Bratu_problem(para, ds, nx=4, interactive=False): dim = 1 dof = 1 ny = 1 nz = 1 parameters = para interface = Interface(parameters, nx, ny, nz, dim, dof) continuation = Continuation(interface, parameters) x0 = numpy.zeros(dof * (nx-1) * ny * nz) x0 = continuation.newton(x0) target = 3 ds = ds maxit = int(4 / ds * 2) * 100 (x, para, u, u_norm, C_v, iterations) = continuation.continuation(x0, 'Bratu parameter', target, ds, maxit) # print(para[200]) # plt.plot(interface.discretization.x[0:15], u[200]) # plt.xlabel('x') # plt.ylabel('u(x)') # plt.show() assert numpy.linalg.norm(x) > 0 # if not interactive: # return # print(x) return para, u, u_norm, C_v, iterations
def continuation_2D(nx=4, interactive=False): dim = 2 dof = 3 ny = nx nz = 1 parameters = {'Reynolds Number': 0} interface = Interface(parameters, nx, ny, nz, dim, dof) continuation = Continuation(interface, parameters) x0 = numpy.zeros(dof * nx * ny * nz) x0 = continuation.newton(x0) target = 2000 ds = 100 maxit = 20 x = continuation.continuation(x0, 'Reynolds Number', target, ds, maxit) assert numpy.linalg.norm(x) > 0 if not interactive: return x print(x) x = plot_utils.create_state_mtx(x, nx, ny, nz, dof) plot_utils.plot_state(x[:, :, 0, 0], x[:, :, 0, 1], nx, ny)
def test_continuation(nx=4, interactive=False): dim = 3 dof = 4 ny = nx nz = nx parameters = {} interface = Interface(parameters, nx, ny, nz, dim, dof) continuation = Continuation(interface, parameters) x0 = numpy.zeros(dof * nx * ny * nz) x0 = continuation.newton(x0) start = 0 target = 100 ds = 100 x = continuation.continuation(x0, 'Reynolds Number', start, target, ds)[0] assert numpy.linalg.norm(x) > 0 if not interactive: return print(x) x = plot_utils.create_state_mtx(x, nx, ny, nz, dof) plot_utils.plot_velocity_magnitude(x[:, ny // 2, :, 0], x[:, ny // 2, :, 2], nx, nz)
def test_continuation_2D_stretched(nx=4, interactive=False): dim = 2 dof = 3 ny = nx nz = 1 xpos = utils.create_stretched_coordinate_vector(0, 1, nx, 1.5) ypos = utils.create_stretched_coordinate_vector(0, 1, ny, 1.5) parameters = {} interface = Interface(parameters, nx, ny, nz, dim, dof, xpos, ypos) continuation = Continuation(interface, parameters) x0 = numpy.zeros(dof * nx * ny * nz) x0 = continuation.newton(x0) start = 0 target = 2000 ds = 100 x = continuation.continuation(x0, 'Reynolds Number', start, target, ds)[0] assert numpy.linalg.norm(x) > 0 if not interactive: return x print(x) x = plot_utils.create_state_mtx(x, nx, ny, nz, dof) plot_utils.plot_velocity_magnitude(x[:, :, 0, 0], x[:, :, 0, 1], interface)
def HYMLS_Bratu_problem(nx=8, interactive=False): try: from fvm import HYMLSInterface from PyTrilinos import Epetra from PyTrilinos import Teuchos except ImportError: pytest.skip("HYMLS not found") dim = 1 dof = 1 ny = 1 nz = 1 parameters = Teuchos.ParameterList() parameters.set('Bratu parameter', 0) parameters.set('Bordered Solver', True) parameters.set('Problem Type', 'Bratu problem') comm = Epetra.PyComm() interface = HYMLSInterface.Interface(comm, parameters, nx, ny, nz, dim, dof) m = interface.map continuation = Continuation(interface, parameters) x0 = HYMLSInterface.Vector(m) x0.PutScalar(0.0) x0 = continuation.newton(x0) target = 3 ds = 0.1 maxit = int(3.6 / ds * 2) (x, paras, u) = continuation.continuation(x0, 'Bratu parameter', target, ds, maxit) assert x.Norm2() > 0 if not interactive: return x = gather(x) if comm.MyPID() == 0: print(x) # x = plot_utils.create_state_mtx(x, nx, ny, nz, dof) # plot_utils.plot_state(x[:, :, 0, 0], x[:, :, 0, 1], nx, ny) plt.plot(paras, u) plt.title('nx = ' + repr(nx)) plt.xlabel('Bratu parameter C') plt.ylabel('Infinity Norm of u(x)') plt.show()
def x(interface): from fvm import HYMLSInterface continuation = Continuation(interface, interface.parameters) x0 = HYMLSInterface.Vector(interface.map) x0 = continuation.newton(x0) start = 0 target = 2000 ds = 100 return continuation.continuation(x0, 'Reynolds Number', start, target, ds)[0]
def test_HYMLS_2D_stretched(nx=8, interactive=False): try: from fvm import HYMLSInterface from PyTrilinos import Epetra from PyTrilinos import Teuchos except ImportError: pytest.skip("HYMLS not found") dim = 3 dof = 4 ny = nx nz = 1 parameters = Teuchos.ParameterList() parameters.set('Reynolds Number', 0) parameters.set('Bordered Solver', True) parameters.set('Grid Stretching', True) parameters.set('Verbose', True) comm = Epetra.PyComm() interface = HYMLSInterface.Interface(comm, parameters, nx, ny, nz, dim, dof) m = interface.map continuation = Continuation(interface, parameters) x0 = HYMLSInterface.Vector(m) x0.PutScalar(0.0) x0 = continuation.newton(x0) start = 0 target = 2000 ds = 100 x = continuation.continuation(x0, 'Reynolds Number', start, target, ds)[0] assert x.Norm2() > 0 if not interactive: return x = gather(x) if comm.MyPID() == 0: print(x) xpos = utils.create_stretched_coordinate_vector(0, 1, nx, 1.5) ypos = utils.create_stretched_coordinate_vector(0, 1, ny, 1.5) x = plot_utils.create_velocity_magnitude_mtx(x, nx, ny, nz, dof) plot_utils.plot_velocity_magnitude(x[:, :, 0, 0], x[:, :, 0, 1], x=xpos, y=ypos)
def numpy_x(numpy_interface): n = numpy_interface.dof * numpy_interface.nx * numpy_interface.ny * numpy_interface.nz parameters = {} continuation = Continuation(numpy_interface, parameters) x0 = numpy.zeros(n) x0 = continuation.newton(x0) start = 0 target = 2000 ds = 100 return continuation.continuation(x0, 'Reynolds Number', start, target, ds)[0]
def solve_nonlinear_system(para, nx=4): dim = 1 dof = 1 ny = 1 nz = 1 parameters = para interface = Interface(parameters, nx, ny, nz, dim, dof) continuation = Continuation(interface, parameters) x0 = np.zeros(dof * (nx - 1) * ny * nz) x0 = continuation.newton(x0) return x0
def test_continuation_time_integration(nx=4, interactive=False): dim = 2 dof = 3 ny = nx nz = 1 parameters = {'Newton Tolerance': 1e-6} interface = Interface(parameters, nx, ny, nz, dim, dof) continuation = Continuation(interface, parameters) x0 = numpy.zeros(dof * nx * ny * nz) x0 = continuation.newton(x0) start = 0 target = 2000 ds = 100 x = continuation.continuation(x0, 'Reynolds Number', start, target, ds)[0] # Start from a perturbed solution x2 = utils.create_state_mtx(x, nx, ny, nz, dof) x2[1:nx - 1, 1:ny - 1, :, 0] += 0.1 * numpy.random.random( (nx - 2, ny - 2, nz)) x2 = utils.create_state_vec(x2, nx, ny, nz, dof) assert numpy.linalg.norm(x[0:len(x):dof] - x2[0:len(x):dof]) > 1e-2 assert numpy.linalg.norm(x[1:len(x):dof] - x2[1:len(x):dof]) < 1e-4 time_integration = TimeIntegration(interface, parameters) x3 = time_integration.integration(x2, 1, 1)[0] assert numpy.linalg.norm(x[0:len(x):dof] - x3[0:len(x):dof]) > 1e-2 assert numpy.linalg.norm(x[1:len(x):dof] - x3[1:len(x):dof]) > 1e-2 time_integration = TimeIntegration(interface, parameters) x3 = time_integration.integration(x2, 100, 1000)[0] assert numpy.linalg.norm(x[0:len(x):dof] - x3[0:len(x):dof]) < 1e-4 assert numpy.linalg.norm(x[1:len(x):dof] - x3[1:len(x):dof]) < 1e-4 # Start from zero x2[:] = 0 time_integration = TimeIntegration(interface, parameters) x3 = time_integration.integration(x2, 100, 1000)[0] assert numpy.linalg.norm(x[0:len(x):dof] - x3[0:len(x):dof]) < 1e-4 assert numpy.linalg.norm(x[1:len(x):dof] - x3[1:len(x):dof]) < 1e-4
def main(): ''' An example of performing a continuation for a 2D lid-driven cavity and detecting a bifurcation point''' dim = 2 dof = 3 nx = 32 ny = nx nz = 1 n = dof * nx * ny * nz # Define a point of interest poi = (nx // 2 - 1, ny // 4 - 1) # Define the problem parameters = { 'Problem Type': 'Lid-driven cavity', # Problem parameters 'Reynolds Number': 1, 'Lid Velocity': 0, # Use a stretched grid 'Grid Stretching Factor': 1.5, # Set a maximum step size ds 'Maximum Step Size': 500, # Give back extra output (this is also more expensive) 'Verbose': True, # Value describes the value that is traced in the continuation # and time integration methods 'Value': lambda x: utils.create_state_mtx(x, nx, ny, nz, dof)[poi[0], poi[1], 0, 0] } interface = Interface(parameters, nx, ny, nz, dim, dof) print('Looking at point ({}, {})'.format( interface.discretization.x[poi[0]], interface.discretization.y[poi[1]])) continuation = Continuation(interface, parameters) # Compute an initial guess x0 = numpy.zeros(n) x0 = continuation.continuation(x0, 'Lid Velocity', 0, 1, 0.1)[0] # Perform an initial continuation to Reynolds number 7000 without detecting bifurcation points ds = 100 target = 7000 x, mu, data1 = continuation.continuation(x0, 'Reynolds Number', 0, target, ds) parameters['Newton Tolerance'] = 1e-12 parameters['Destination Tolerance'] = 1e-4 parameters['Detect Bifurcation Points'] = True parameters['Maximum Step Size'] = 100 # parameters['Eigenvalue Solver'] = {} # parameters['Eigenvalue Solver']['Target'] = 3j # parameters['Eigenvalue Solver']['Tolerance'] = 1e-9 # parameters['Eigenvalue Solver']['Number of Eigenvalues'] = 20 # Now detect the bifurcation point target = 10000 x2, mu2, data2 = continuation.continuation(x, 'Reynolds Number', mu, target, ds) # Compute the unstable branch after the bifurcation parameters['Detect Bifurcation Points'] = False parameters['Maximum Step Size'] = 2000 target = 10000 parameters['Newton Tolerance'] = 1e-4 x3, mu3, data3 = continuation.continuation(x2, 'Reynolds Number', mu2, target, ds) # Plot a bifurcation diagram plt.plot(data1.mu, data1.value) plt.plot(data2.mu, data2.value) plt.plot(data3.mu, data3.value) plt.show() # Add a perturbation based on the eigenvector interface.set_parameter('Reynolds Number', mu2) _, v = interface.eigs(x2, True) v = v[:, 0].real v = plot_utils.create_state_mtx(v, nx, ny, nz, dof) # Plot the velocity magnutide plot_utils.plot_velocity_magnitude(v[:, :, 0, 0], v[:, :, 0, 1], interface) # Plot the pressure plot_utils.plot_value(v[:, :, 0, 2], interface)