Exemplo n.º 1
0
def test_to_expression_2D():
    mesh = UnitSquareMesh(3, 3)
    V = VectorFunctionSpace(mesh, "CG", 1)

    v1 = expression.to_expression([1., 2.])
    v2 = expression.to_expression(["1.", 2.])
    v3 = expression.to_expression(["1.", "2."])

    f1 = interpolate(v1, V)
    f2 = interpolate(v2, V)
    f3 = interpolate(v3, V)

    assert (np.allclose(f1.dat.data_ro, f2.dat.data_ro)
            and np.allclose(f2.dat.data_ro, f3.dat.data_ro)
            and (f1.dat.data_ro[:, 0] == 1).all()
            and (f1.dat.data_ro[:, 1] == 2).all())
Exemplo n.º 2
0
 def function_arg(self, g):
     '''Set the value of this boundary condition.'''
     if isinstance(g, function.Function
                   ) and g.function_space() != self._function_space:
         raise RuntimeError("%r is defined on incompatible FunctionSpace!" %
                            g)
     if not isinstance(g, expression.Expression):
         try:
             # Bare constant?
             as_ufl(g)
         except UFLException:
             try:
                 # List of bare constants? Convert to Expression
                 g = expression.to_expression(g)
             except:
                 raise ValueError(
                     "%r is not a valid DirichletBC expression" % (g, ))
     if isinstance(g, expression.Expression) or has_type(
             as_ufl(g), SpatialCoordinate):
         if isinstance(g, expression.Expression):
             self._expression_state = g._state
         try:
             g = function.Function(self._function_space).interpolate(g)
         # Not a point evaluation space, need to project onto V
         except NotImplementedError:
             g = projection.project(g, self._function_space)
     self._function_arg = g
     self._currently_zeroed = False
Exemplo n.º 3
0
def test_to_expression_2D():
    mesh = UnitSquareMesh(3, 3)
    V = VectorFunctionSpace(mesh, "CG", 1)

    v1 = expression.to_expression([1., 2.])
    v2 = expression.to_expression(["1.", 2.])
    v3 = expression.to_expression(["1.", "2."])

    f1 = interpolate(v1, V)
    f2 = interpolate(v2, V)
    f3 = interpolate(v3, V)

    assert (np.allclose(f1.dat.data_ro, f2.dat.data_ro) and
            np.allclose(f2.dat.data_ro, f3.dat.data_ro) and
            (f1.dat.data_ro[:, 0] == 1).all() and
            (f1.dat.data_ro[:, 1] == 2).all())
Exemplo n.º 4
0
def test_to_expression_1D():
    mesh = UnitIntervalMesh(5)
    V = FunctionSpace(mesh, "CG", 1)

    v1 = expression.to_expression(-1.)
    v2 = expression.to_expression([-1.])
    v3 = expression.to_expression("-1.")
    v4 = expression.to_expression(["-1."])

    f1 = interpolate(v1, V)
    f2 = interpolate(v2, V)
    f3 = interpolate(v3, V)
    f4 = interpolate(v4, V)
    v = f1.vector()

    assert (np.allclose(f1.dat.data_ro, f2.dat.data_ro)
            and np.allclose(f3.dat.data_ro, f4.dat.data_ro)
            and np.allclose(f1.dat.data_ro, f4.dat.data_ro)
            and (v.array() == -1.0).all())
Exemplo n.º 5
0
def test_to_expression_1D():
    mesh = UnitIntervalMesh(5)
    V = FunctionSpace(mesh, "CG", 1)

    v1 = expression.to_expression(-1.)
    v2 = expression.to_expression([-1.])
    v3 = expression.to_expression("-1.")
    v4 = expression.to_expression(["-1."])

    f1 = interpolate(v1, V)
    f2 = interpolate(v2, V)
    f3 = interpolate(v3, V)
    f4 = interpolate(v4, V)
    v = f1.vector()

    assert (np.allclose(f1.dat.data_ro, f2.dat.data_ro) and
            np.allclose(f3.dat.data_ro, f4.dat.data_ro) and
            np.allclose(f1.dat.data_ro, f4.dat.data_ro) and
            (v.array() == -1.0).all())
Exemplo n.º 6
0
 def function_arg(self, g):
     '''Set the value of this boundary condition.'''
     if isinstance(g, function.Function) and g.function_space() != self._function_space:
         raise RuntimeError("%r is defined on incompatible FunctionSpace!" % g)
     if not isinstance(g, expression.Expression):
         try:
             # Bare constant?
             as_ufl(g)
         except UFLException:
             try:
                 # List of bare constants? Convert to Expression
                 g = expression.to_expression(g)
             except:
                 raise ValueError("%r is not a valid DirichletBC expression" % (g,))
     if isinstance(g, expression.Expression):
         self._expression_state = g._state
         try:
             g = function.Function(self._function_space).interpolate(g)
         # Not a point evaluation space, need to project onto V
         except NotImplementedError:
             g = projection.project(g, self._function_space)
     self._function_arg = g
     self._currently_zeroed = False