Exemple #1
0
def test_fenics_vector_copy():
    mesh = UnitSquare(3, 3)
    fs = FunctionSpace(mesh, "CG", 1)
    vec1 = FEniCSVector(Function(fs))
    vec1.coeffs = np.array(range(fs.dim()))
    assert_equal(vec1.coeffs[0], 0)
    vec2 = vec1.copy()
    vec2.coeffs[0] = 5
    assert_equal(vec1.coeffs[0], 0)
Exemple #2
0
def test_fenics_refine():
    mesh1 = UnitSquare(3, 3)
    fs1 = FunctionSpace(mesh1, "CG", 2)
    exa = Expression("1.+x[0]*x[1]")
    f1a = interpolate(exa, fs1)
    vec1a = FEniCSVector(f1a)
    (basis2, prolongate, restrict) = vec1a.basis.refine((1, 3, 15))
    vec2 = prolongate(vec1a)
    assert_equal(vec2.basis, basis2)
    vec1b = restrict(vec2)
    assert_equal(vec1b.basis, vec1a.basis)
    assert_almost_equal(vec1a.array(), vec1b.array())
def test6():
    ex = Expression('sin(2*a*pi*x[0])*cos(2*b*pi*x[1])', a=13, b=13)
    #V0 = FEniCSBasis(FunctionSpace(UnitSquare(5, 5), 'CG', 1))
    V0 = FEniCSBasis(FunctionSpace(UnitSquare(40, 40), 'CG', 1))
    F0 = FEniCSVector.from_basis(V0).interpolate(ex)
    for num in [5, 10, 20, 40]:
        for degree in [1, 2, 3, 4, 5, 6, 7, 8]:
            V1 = FEniCSBasis(FunctionSpace(UnitSquare(num, num), 'CG', degree))
            F1 = FEniCSVector.from_basis(V1).interpolate(F0._fefunc)
            F0d = FEniCSVector.from_basis(V0).interpolate(F1._fefunc) - F0
            print "num=%2d, deg=%s, norm=%7.4f" % (num, degree, norm(F0d._fefunc))
        print
def test5():
    ex = Expression('sin(2*a*pi*x[0])*cos(2*b*pi*x[1])', a=10, b=10)
    for num in [5, 10, 20, 40]:
        for degree in [1, 2, 3, 4, 5, 6]:
            V1 = FEniCSBasis(FunctionSpace(UnitSquare(num, num), 'CG', degree))
            F1 = FEniCSVector.from_basis(V1).interpolate(ex)
            print "num=%2d, deg=%s, norm=%7.4f" % (num, degree, norm(F1._fefunc))
        print
def test4():
    print "TEST 4"
    mis = (Multiindex(), Multiindex([1]), Multiindex([0, 1]), Multiindex([1, 1]))
    mv = MultiVectorWithProjection()
    V1 = FEniCSBasis(FunctionSpace(UnitSquare(5, 5), 'CG', 1))
    V2, _, _ = V1.refine()
    V3, _, _ = V2.refine()
    V4 = V1.refine_maxh(1 / 30, uniform=True)
    print "mesh1", V1.mesh, "maxh,minh=", V1.maxh, V1.minh
    print "mesh2", V2.mesh, "maxh,minh=", V2.maxh, V2.minh
    print "mesh3", V3.mesh, "maxh,minh=", V3.maxh, V3.minh
    print "mesh4", V4.mesh, "maxh,minh=", V4.maxh, V4.minh
    F2 = FEniCSVector.from_basis(V2)
    F3 = FEniCSVector.from_basis(V3)
    F4 = FEniCSVector.from_basis(V4)
    mv[mis[0]] = FEniCSVector.from_basis(V1)
    mv[mis[1]] = F2
    mv[mis[2]] = F3
    mv[mis[3]] = F4
    for j, ex in enumerate(EX):
        print "ex[", j, "] =================="
        for degree in range(1, 4):
            print "\t=== degree ", degree, "==="
            F2.interpolate(ex)
            F3.interpolate(ex)
            F4.interpolate(ex)
            err1 = mv.get_projection_error_function(mis[1], mis[0], degree, refine_mesh=False)
            err2 = mv.get_projection_error_function(mis[2], mis[0], degree, refine_mesh=False)
            err3 = mv.get_projection_error_function(mis[3], mis[0], degree, refine_mesh=False)
            print "\t[NO DESTINATION MESH REFINEMENT]"
            print "\t\tV2 L2", norm(err1._fefunc, 'L2'), "H1", norm(err1._fefunc, 'H1')
            print "\t\tV3 L2", norm(err2._fefunc, 'L2'), "H1", norm(err2._fefunc, 'H1')
            print "\t\tV4 L2", norm(err3._fefunc, 'L2'), "H1", norm(err3._fefunc, 'H1')
            err1 = mv.get_projection_error_function(mis[1], mis[0], degree, refine_mesh=True)
            err2 = mv.get_projection_error_function(mis[2], mis[0], degree, refine_mesh=True)
            err3 = mv.get_projection_error_function(mis[3], mis[0], degree, refine_mesh=True)
            print "\t[WITH DESTINATION MESH REFINEMENT]"
            print "\t\tV2 L2", norm(err1._fefunc, 'L2'), "H1", norm(err1._fefunc, 'H1')
            print "\t\tV3 L2", norm(err2._fefunc, 'L2'), "H1", norm(err2._fefunc, 'H1')
            print "\t\tV4 L2", norm(err3._fefunc, 'L2'), "H1", norm(err3._fefunc, 'H1')
Exemple #6
0
    def _euclidian_to_multivec(self, vec):
        assert vec.dim == self._dimsum

        if not self._last_vec:
            new_vec = MultiVector()
            for mu in self._basis.active_indices():
                vec_mu = FEniCSVector.from_basis(self._basis._basis[mu])
                new_vec[mu] = vec_mu
            self._last_vec = new_vec
        else:
            new_vec = self._last_vec

        start = 0
        basis = self._basis
        for mu in basis.active_indices():
            vec_mu = new_vec[mu]
            dim = basis._basis[mu].dim
            vec_mu.coeffs = vec.coeffs[start:start + dim]
            new_vec[mu] = vec_mu
            start += dim
        return new_vec
Exemple #7
0
 def get_ainfty(m, V):
     a0_f = coeff_field.mean_func
     if isinstance(a0_f, tuple):
         a0_f = a0_f[0]
     # determine min \overline{a} on D (approximately)
     f = FEniCSVector.from_basis(V, sub_spaces=0)
     f.interpolate(a0_f)
     min_a0 = f.min_val
     am_f, _ = coeff_field[m]
     if isinstance(am_f, tuple):
         am_f = am_f[0]
     # determine ||a_m/\overline{a}||_{L\infty(D)} (approximately)
     try:
         # use exact bounds if defined
         max_am = am_f.max_val
     except:
         # otherwise interpolate
         f.interpolate(am_f)
         max_am = f.max_val
     ainftym = max_am / min_a0
     assert isinstance(ainftym, float)
     return ainftym
Exemple #8
0
 def prepare_ainfty(Lambda, M):
     ainfty = []
     a0_f = coeff_field.mean_func
     if isinstance(a0_f, tuple):
         a0_f = a0_f[0]
     # retrieve (sufficiently fine) function space for maximum norm evaluation
     # NOTE: we use the deterministic mesh since it is assumed to be the finest
     V, _, _, _ = w[Multiindex()].basis.refine_maxh(maxh)
     # determine min \overline{a} on D (approximately)
     f = FEniCSVector.from_basis(V, sub_spaces=0)
     f.interpolate(a0_f)
     min_a0 = f.min_val
     for m in range(M):
         am_f, am_rv = coeff_field[m]
         if isinstance(am_f, tuple):
             am_f = am_f[0]
         # determine ||a_m/\overline{a}||_{L\infty(D)} (approximately)
         f.interpolate(am_f)
         max_am = f.max_val
         ainftym = max_am / min_a0
         assert isinstance(ainftym, float)
         ainfty.append(ainftym)
     return ainfty
Exemple #9
0
def test_fenics_vector():
    mesh = UnitSquare(3, 3)
    fs1 = FunctionSpace(mesh, "CG", 2)
    fs2 = FunctionSpace(mesh, "DG", 1)
    ex1 = Expression("1.+x[0]*x[1]")
    ex2 = Expression("2.*x[0] - x[1]")
    ex3 = Expression("1. + 2.*x[0] - x[1] + x[0]*x[1]")
    f1 = interpolate(ex1, fs1)
    f2 = interpolate(ex2, fs1)
    f3 = interpolate(ex1, fs2)
    f4 = interpolate(ex1, fs1)
    f5 = interpolate(ex3, fs1)
    vec1 = FEniCSVector(f1)
    vec2 = FEniCSVector(f2)
    vec3 = FEniCSVector(f3)
    vec4 = FEniCSVector(f4)
    vec5 = FEniCSVector(f5)

    assert_true(vec1 == vec1)
    assert_false(vec1 == vec2)
    assert_false(vec1 == vec3)
    assert_true(vec1 == vec4)
    assert_false(vec1 != vec1)
    assert_true(vec1 != vec2)
    assert_true(vec1 != vec3)
    assert_false(vec1 != vec4)

    vec12 = vec1 + vec2
    vec12b = vec1 + vec2
    vec1m = -vec1
    vec1m3 = 3 * vec1
    vec21 = vec2 + vec1
    vec14 = vec1 - vec4

    assert_equal(vec12, vec21)
    assert_almost_equal(vec12.array(), vec5.array())
    assert_almost_equal(vec12.array(), vec12b.array())
    assert_equal(vec14.array(), np.zeros(vec12.coeffs.size()))
    assert_equal(vec1m.array(), -vec1.array())
    assert_equal(vec1m3.array(), 3 * vec1.array())

    assert_almost_equal(vec1.eval([0.8, 0.4]), 1.32)
Exemple #10
0
def test_fenics_vector_inner():
    mesh = UnitSquare(3, 3)
    fs = FunctionSpace(mesh, "CG", 1)
    vec = FEniCSVector(Function(fs))
    vec.coeffs = np.array(range(fs.dim()))
    assert_equal(vec.__inner__(vec), 1240)