Debian Codegraph @

python/demo/undocumented/block-matrix/demo_block-matrix.py

raw
1 """This demo illustrates basic usage of block matrices and vectors."""
2
3 # Copyright (C) 2008 Kent-Andre Mardal
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Modified by Anders Logg, 2008.
21 #
22 # First added: 2008-12-12
23 # Last changed: 2012-11-12
24
25
26 from dolfin import *
27
28 # Create a simple stiffness matrix and vector
29 mesh = UnitSquareMesh(32, 32)
30 V = FunctionSpace(mesh, "CG", 1)
31 u = TrialFunction(V)
32 v = TestFunction(V)
33 a = dot(grad(u), grad(v))*dx
34 A = assemble(a)
35 x0 = SpatialCoordinate(mesh)[0]
36 L = sin(x0)*v*dx
37 x = assemble(L)
38
39 # Create a block matrix
40 AA = BlockMatrix(2, 2)
41 AA[0, 0] = A
42 AA[1, 0] = A
43 AA[0, 1] = A
44 AA[1, 1] = A
45
46 # Create a block vector (that is compatible with A in parallel)
47 xx = BlockVector(2)
48 xx[0] = x
49 xx[1] = x
50
51 # Create a another block vector (that is compatible with A in parallel)
52 y = Vector()
53 A.init_vector(y, 0)
54 yy = BlockVector(2)
55 yy[0] = y
56 yy[1] = y
57
58 # Multiply
59 AA.mult(xx, yy)
60 print("||Ax|| =", y.norm("l2"))