コード例 #1
0
ファイル: test_multi_operator.py プロジェクト: SpuqTeam/spuq
def test_apply():
    N = 4
    #a = [ConstFunction(1.0), SimpleFunction(np.sin), SimpleFunction(np.cos)]
    mean_func = ConstFunction(2.0)
    a = [ConstFunction(3.0), ConstFunction(4.0)]
    rvs = [UniformRV(), NormalRV(mu=0.5)]
    coeff_field = ListCoefficientField(mean_func, a, rvs)

    A = MultiOperator(coeff_field, diag_assemble)
    mis = [Multiindex([0]),
           Multiindex([1]),
           Multiindex([0, 1]),
           Multiindex([0, 2])]
    vecs = [FlatVector(np.random.random(N), SimpleProjectBasis(N)),
            FlatVector(np.random.random(N), SimpleProjectBasis(N)),
            FlatVector(np.random.random(N), SimpleProjectBasis(N)),
            FlatVector(np.random.random(N), SimpleProjectBasis(N))]

    w = MultiVectorWithProjection()
    for i in range(len(mis)):
        w[mis[i]] = vecs[i]
    v = A * w

    L = LegendrePolynomials(normalised=True)
    H = StochasticHermitePolynomials(mu=0.5, normalised=True)
    v0_ex = (2 * vecs[0] + 
             3 * (L.get_beta(0)[1] * vecs[1] - L.get_beta(0)[0] * vecs[0]) + 
             4 * (H.get_beta(0)[1] * vecs[2] - H.get_beta(0)[0] * vecs[0]))
    v2_ex = (2 * vecs[2] + 4 * (H.get_beta(1)[1] * vecs[3] - 
                                H.get_beta(1)[0] * vecs[2] + 
                                H.get_beta(1)[-1] * vecs[0]))

    assert_equal(v[mis[0]], v0_ex)
    assert_equal(v[mis[2]], v2_ex)
コード例 #2
0
ファイル: test_multi_operator.py プロジェクト: SpuqTeam/spuq
def test_fenics_vector():
    def mult_assemble(a, basis):
        return MultiplicationOperator(a(0), basis)

    mean_func = ConstFunction(2)
    a = [ConstFunction(3), ConstFunction(4)]
    rvs = [UniformRV(), NormalRV(mu=0.5)]
    coeff_field = ListCoefficientField(mean_func, a, rvs)

    A = MultiOperator(coeff_field, mult_assemble)
    mis = [Multiindex([0]),
           Multiindex([1]),
           Multiindex([0, 1]),
           Multiindex([0, 2])]
    mesh = UnitSquare(4, 4)
    fs = FunctionSpace(mesh, "CG", 4)
    F = [interpolate(Expression("*".join(["x[0]"] * i)), fs) for i in range(1, 5)]
    vecs = [FEniCSVector(f) for f in F]

    w = MultiVectorWithProjection()
    for mi, vec in zip(mis, vecs):
        w[mi] = vec
    v = A * w

    L = LegendrePolynomials(normalised=True)
    H = StochasticHermitePolynomials(mu=0.5, normalised=True)
    ex0 = Expression("2*x[0] + 3*(l01*x[0]*x[0]-l00*x[0]) + 4*(h01*x[0]*x[0]*x[0]-h00*x[0])",
        l01=L.get_beta(0)[1], l00=L.get_beta(0)[0],
        h01=H.get_beta(0)[1], h00=H.get_beta(0)[0])
    vec0 = FEniCSVector(interpolate(ex0, fs))

    assert_almost_equal(v[mis[0]].array, vec0.array)

    # ======================================================================
    # test with different meshes
    # ======================================================================

    N = len(mis)
    meshes = [UnitSquare(i + 3, i + 3) for i in range(N)]
    fss = [FunctionSpace(mesh, "CG", 4) for mesh in meshes]
    F = [interpolate(Expression("*".join(["x[0]"] * (i + 1))), fss[i]) for i in range(N)]
    vecs = [FEniCSVector(f) for f in F]

    w = MultiVectorWithProjection()
    for mi, vec in zip(mis, vecs):
        w[mi] = vec
    v = A * w

    L = LegendrePolynomials(normalised=True)
    H = StochasticHermitePolynomials(mu=0.5, normalised=True)
    ex0 = Expression("2*x[0] + 3*(l01*x[0]*x[0]-l00*x[0]) + 4*(h01*x[0]*x[0]*x[0]-h00*x[0])",
        l01=L.get_beta(0)[1], l00=L.get_beta(0)[0],
        h01=H.get_beta(0)[1], h00=H.get_beta(0)[0])
    ex2 = Expression("2*x[0]*x[0]*x[0] + 4*(h11*x[0]*x[0]*x[0]*x[0] - h10*x[0]*x[0]*x[0] + h1m1*x[0])",
        h11=H.get_beta(1)[1], h10=H.get_beta(1)[0], h1m1=H.get_beta(1)[-1])
    vec0 = FEniCSVector(interpolate(ex0, fss[0]))
    vec2 = FEniCSVector(interpolate(ex2, fss[2]))

    assert_almost_equal(v[mis[0]].array, vec0.array)
    assert_almost_equal(v[mis[2]].array, vec2.array)