~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/demo/fem/simple/python/demo.py

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# This demo program dmonstrates how to create simple finite
2
 
# element matrices like the stiffness matrix and mass matrix.
3
 
#
4
 
# Modified by Anders Logg, 2008
5
 
 
6
 
__author__ = "Kristian B. Oelgaard (k.b.oelgaard@tudelft.nl)"
7
 
__date__ = "2007-11-15 -- 2008-01-15"
8
 
__copyright__ = "Copyright (C) 2007 Kristian B. Oelgaard"
9
 
__license__  = "GNU LGPL Version 2.1"
10
 
 
11
 
from dolfin import *
12
 
import numpy
13
 
 
14
 
# Load reference mesh (just a simple tetrahedron)
15
 
mesh = Mesh("../tetrahedron.xml.gz");
16
 
 
17
 
# Assemble stiffness and mass matrices
18
 
element = FiniteElement("Lagrange", "tetrahedron", 1)
19
 
v = TestFunction(element)
20
 
u = TrialFunction(element)
21
 
A = assemble(dot(grad(v), grad(u))*dx, mesh)
22
 
M = assemble(v*u*dx, mesh)
23
 
 
24
 
# Create reference matrices and set entries
25
 
A0 = Matrix(4, 4)
26
 
M0 = Matrix(4, 4)
27
 
pos = numpy.array([0, 1, 2, 3], dtype='I')
28
 
A0.set(numpy.array([[1.0/2.0, -1.0/6.0, -1.0/6.0, -1.0/6.0],
29
 
                    [-1.0/6.0, 1.0/6.0, 0.0, 0.0],
30
 
                    [-1.0/6.0, 0.0, 1.0/6.0, 0.0],
31
 
                    [-1.0/6.0, 0.0, 0.0, 1.0/6.0]]), pos, pos)
32
 
 
33
 
M0.set(numpy.array([[1.0/60.0, 1.0/120.0, 1.0/120.0, 1.0/120.0],
34
 
                    [1.0/120.0, 1.0/60.0, 1.0/120.0, 1.0/120.0],
35
 
                    [1.0/120.0, 1.0/120.0, 1.0/60.0, 1.0/120.0],
36
 
                    [1.0/120.0, 1.0/120.0, 1.0/120.0, 1.0/60.0]]), pos, pos)
37
 
A0.apply()
38
 
M0.apply()
39
 
 
40
 
# Display matrices
41
 
print ""
42
 
print "Assembled stiffness matrix:"
43
 
print A
44
 
A.disp()
45
 
print ""
46
 
 
47
 
print "Reference stiffness matrix:"
48
 
A0.disp()
49
 
print ""
50
 
 
51
 
print "Assembled mass matrix:"
52
 
M.disp()
53
 
print ""
54
 
 
55
 
print "Reference mass matrix:"
56
 
M0.disp()
57
 
print ""