/
test_127.py
54 lines (42 loc) · 1.47 KB
/
test_127.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from dolfin import *
def tensor_conditional(predicate, tvalue, fvalue):
assert tvalue.ufl_shape == fvalue.ufl_shape
# Scalar
rank = len(tvalue.ufl_shape)
if rank == 0:
return conditional(predicate, tvalue, fvalue)
# Vector
elif rank == 1:
dim = tvalue.ufl_shape[0]
conds = [conditional(predicate, tvalue[i], fvalue[i]) for i in range(dim)]
# Matrices
elif rank == 2:
nrows, ncols = tvalue.ufl_shape
conds = [[conditional(predicate, tvalue[i, j], fvalue[i, j]) for j in range(ncols)]
for i in range(nrows)]
# Generalize later
else:
raise ValueError
return as_tensor(conds)
# ----------------------------------------------------------------------------
mesh = UnitSquareMesh(10, 10)
V = VectorFunctionSpace(mesh, 'CG', 1)
W = FunctionSpace(mesh, 'DG', 0)
w = interpolate(Expression('x[0]*x[0]+x[1]*x[1]'), W)
x, y = SpatialCoordinate(mesh)
try:
b = conditional(gt(w, 1), as_vector((x, y)), as_vector((-x, -y)))
f = project(b, V)
except Exception as e:
print '\033[1;37;31m%s\033[0m' % e
print 'Try with tensor_conditional'
b = tensor_conditional(gt(w, 1), as_vector((x, y)), as_vector((-x, -y)))
f = project(b, V)
plot(f)
V = FunctionSpace(mesh, 'CG', 1)
b = tensor_conditional(gt(w, 1),
as_tensor(((1, 0), (0, 2))),
Constant(1+1E-2)*as_tensor(((2, 0), (0, 1))))
f = project(det(b), V)
plot(f)
interactive()