Пример #1
0
def solve(nelem, h, deg, alpha, beta, gamma, k=None, b=None, c=None, f=None):
    #
    # Cast polynomial degree to enums
    deg = POLYNOMIAL(deg)
    #
    # Check inputs make sense
    assert (nelem > 0)
    assert (nelem == len(h))
    assert (deg in [POLYNOMIAL.LINEAR, POLYNOMIAL.QUADRATIC])
    assert (len(alpha) == 2)
    assert (len(beta) == 2)
    assert (len(gamma) == 2)

    #
    # Make sure functions are callable on ndarrays
    #
    # NOTE: functions passed as None will be treated
    # as a constant equal to zero.
    x_test = linspace(0, 1, 3)
    callable_functions = [k, b, c, f]
    for fun in callable_functions:
        if fun is not None:
            try:
                fun(x_test)
            except:
                try:
                    fun = lambda x: float(fun)
                except:
                    raise (Exception(
                        "One of the functions k, b, c, f is neither callable on ndarrays nor a number. Aborting!"
                    ))
        else:
            fun = lambda x: 0.

    #
    # Assemble the system
    K = construct_stiffness()
    F = construct_load()

    #
    # Solve the system
    u = np_solve(K, F)

    #
    # Solve auxiliary equations
    if False:
        print("Hello")

    #
    return u
Пример #2
0
gamma = [1., 4.]
k = lambda x: 2. + x
k = vectorize(k)
b = lambda x: 0.
b = vectorize(b)
c = lambda x: 0.
c = vectorize(c)
f = lambda x: -6 - 4 * x
f = vectorize(f)

K, K0, KN = construct_stiffness(elements, polynomial_order, alpha, beta, gamma,
                                k, b, c)

savetxt("stiffness_quad.csv", K, delimiter=" & ", fmt="%.2f")

F = construct_load(elements, polynomial_order, alpha, beta, gamma, K0, KN, f,
                   k)

savetxt("load_quad.csv", F, delimiter=" \\\\ ", fmt="%.2f")

x = linspace(0, 1, 100)
t = lambda x: (1 + x)**2
t = vectorize(t)
y = t(x)

# print("COND: ", cond(K))
xx = linspace(0, 1, nelems * polynomial_order + 1)
uu = np_solve(K, F).flatten()
u = set_essential_boundary_conditions(alpha, gamma, uu)

plt.cla()
plt.plot(x, y, 'k-', label='True Sol.')