-
Notifications
You must be signed in to change notification settings - Fork 0
/
harmosc.py
105 lines (84 loc) · 2.14 KB
/
harmosc.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
# coding=UTF-8
#
# FEM solution of the stationary Schroedinger equation
# for the harmonic oscillator potential.
#
# Dependancies:
# - FEnICS
# - PETSc
# - SLEPc
#
# Revisions:
#
# 2014-10-11 pstegmann Creation of draft from formula
# 2014-10-12 pstegmann Modification of draft to first working solution
from dolfin import *
import time
import math
import matplotlib.pyplot as plit
#define mesh and function space
mesh = IntervalMesh(200, -10, 10)
V = FunctionSpace(mesh, 'Lagrange', 3)
#define boundary
def boundary(x, on_boundary):
return on_boundary
#apply essential boundary conditions
bc = DirichletBC(V, 0, boundary)
#define functions
u = TrialFunction(V)
v = TestFunction(V)
x2 = Expression('x[0]*x[0]')
#define problem
a = (inner(grad(u), grad(v)) \
+ x2*u*v)*dx
m = u*v*dx
#assemble stiffness matrix
A = PETScMatrix()
assemble(a, tensor=A)
M = PETScMatrix()
assemble(m, tensor=M)
#create eigensolver
eigensolver = SLEPcEigenSolver(A,M)
eigensolver.parameters['spectrum'] = 'smallest magnitude'
eigensolver.parameters['solver'] = 'lapack'
eigensolver.parameters['tolerance'] = 1.e-15
#solve for eigenvalues
max = 10
eigensolver.solve(max)
#extract first eigenpair
r, c, rx, cx = eigensolver.get_eigenpair(0)
print 'eigenvalue:', r, 'Ground state ', 0
#assign eigenvector to function
u = Function(V)
u.vector()[:] = rx
#plot eigenfunction and probability density
plt = plot(u)
#interactive()
time.sleep(2) # 3 seconds pause
print ' Maximum converged eigenvalue: ', eigensolver.get_number_converged()
i = 1
#while i < eigensolver.get_number_converged():
while i < max:
# extract next eigenpair
r, c, rx, cx = eigensolver.get_eigenpair(i)
print 'eigenvalue:', r, i
#assign eigenvector to function
u.vector()[:] = rx
#plot eigenfunction
plt.plot(u)
#interactive()
time.sleep(2)
#print 'next plot'
#increment i
i = i + 1
print 'Finished.'
# extract last eigenpair
r, c, rx, cx = eigensolver.get_eigenpair(max)
print 'eigenvalue:', r, max
#assign eigenvector to function
u.vector()[:] = rx
#plot eigenfunction
plt.plot(u)
interactive()
##### EOF #############