Example #1
0
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)
Example #2
0
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
Example #3
0
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)
Example #4
0
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)
Example #5
0
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()
Example #6
0
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]
Example #7
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)
Example #8
0
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]
Example #9
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
Example #10
0
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