-
Notifications
You must be signed in to change notification settings - Fork 1
/
schroedinger.py
executable file
·492 lines (413 loc) · 16.3 KB
/
schroedinger.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python
"""
Electronic structure solver.
Type:
$ ./schroedinger.py
for usage and help.
"""
import os
import os.path as op
from optparse import OptionParser
from math import pi
from scipy.optimize import broyden3
from scipy.optimize.nonlin import excitingmixing
import init_sfepy
from sfepy.base.base import *
from sfepy.base.conf import ProblemConf, get_standard_keywords
from sfepy.base.la import eig
from sfepy.base.log import Log
from sfepy.fem import eval_term_op, MeshIO, ProblemDefinition
import sfepy.base.ioutils as io
from sfepy.solvers import Solver
##
# c: 22.02.2008, r: 22.02.2008
def update_state_to_output( out, pb, vec, name, fill_value = None ):
aux = pb.state_to_output( vec, fill_value )
key = aux.keys()[0]
out[name] = aux[key]
##
# c: 22.02.2008, r: 22.02.2008
def wrap_function( function, args ):
ncalls = [0]
times = []
def function_wrapper( x ):
ncalls[0] += 1
tt = time.time()
out = function( x, *args )
eigs, mtx_s_phi, vec_n, vec_vh, vec_vxc = out
print "-"*70
print "eigs",eigs
print "V_H",vec_vh
print "V_XC",vec_vxc
print "-"*70
tt2 = time.time()
if tt2 < tt:
raise RuntimeError, '%f >= %f' % (tt, tt2)
times.append( tt2 - tt )
return vec_vh + vec_vxc
return ncalls, times, function_wrapper
itercount = 0
##
# c: 22.02.2008, r: 03.03.2008
def iterate( vec_vhxc, pb, conf, eig_solver, n_eigs, mtx_b, log,
n_electron = 5 ):
global itercount
itercount += 1
from sfepy.physics import dft
pb.update_materials( extra_mat_args = {'mat_v' : {'vhxc' : vec_vhxc}} )
dummy = pb.create_state_vector()
output( 'assembling lhs...' )
tt = time.clock()
mtx_a = eval_term_op( dummy, conf.equations['lhs'], pb,
dw_mode = 'matrix', tangent_matrix = pb.mtx_a )
output( '...done in %.2f s' % (time.clock() - tt) )
print 'computing the Ax=Blx Kohn-Sham problem...'
eigs, mtx_s_phi = eig_solver( mtx_a, mtx_b, conf.options.n_eigs )
if len(eigs) < n_electron:
print len(eigs)
print eigs
raise Exception("Not enough eigenvalues have converged. Exiting.")
print "saving solutions, iter=%d" % itercount
out = {}
var_name = pb.variables.get_names( kind = 'state' )[0]
for ii in xrange( len(eigs) ):
vec_phi = pb.variables.make_full_vec( mtx_s_phi[:,ii] )
update_state_to_output( out, pb, vec_phi, var_name+'%03d' % ii )
pb.save_state( "tmp/iter%d.vtk" % itercount, out = out )
print "solutions saved"
vec_phi = nm.zeros_like( vec_vhxc )
vec_n = nm.zeros_like( vec_vhxc )
for ii in xrange( n_electron ):
vec_phi = pb.variables.make_full_vec( mtx_s_phi[:,ii] )
vec_n += vec_phi ** 2
vec_vxc = nm.zeros_like( vec_vhxc )
for ii, val in enumerate( vec_n ):
vec_vxc[ii] = dft.getvxc( val/(4*pi), 0 )
pb.set_equations( conf.equations_vh )
pb.time_update()
pb.variables['n'].data_from_data( vec_n )
print "Solving Ax=b Poisson equation"
vec_vh = pb.solve()
#sphere = eval_term_op( dummy, conf.equations['sphere'], pb)
#print sphere
norm = nla.norm( vec_vh + vec_vxc )
log( norm, norm )
return eigs, mtx_s_phi, vec_n, vec_vh, vec_vxc
##
# c: 01.02.2008, r: 03.03.2008
def solve_eigen_problem_n( conf, options ):
pb = ProblemDefinition.from_conf( conf )
dim = pb.domain.mesh.dim
pb.time_update()
dummy = pb.create_state_vector()
output( 'assembling rhs...' )
tt = time.clock()
mtx_b = eval_term_op( dummy, conf.equations['rhs'], pb,
dw_mode = 'matrix', tangent_matrix = pb.mtx_a.copy() )
output( '...done in %.2f s' % (time.clock() - tt) )
#mtxA.save( 'tmp/a.txt', format='%d %d %.12f\n' )
#mtxB.save( 'tmp/b.txt', format='%d %d %.12f\n' )
try:
n_eigs = conf.options.n_eigs
except AttributeError:
n_eigs = mtx_a.shape[0]
if n_eigs is None:
n_eigs = mtx_a.shape[0]
## mtx_a.save( 'a.txt', format='%d %d %.12f\n' )
## mtx_b.save( 'b.txt', format='%d %d %.12f\n' )
if options.plot:
log_conf = {
'is_plot' : True,
'aggregate' : 1,
'yscales' : ['linear', 'log'],
}
else:
log_conf = {
'is_plot' : False,
}
log = Log.from_conf( log_conf, ([r'$|F(x)|$'], [r'$|F(x)|$']) )
eig_conf = pb.get_solver_conf( conf.options.eigen_solver )
eig_solver = Solver.any_from_conf( eig_conf )
vec_vhxc = nm.zeros( (pb.variables.di.ptr[-1],), dtype = nm.float64 )
aux = wrap_function( iterate,
(pb, conf, eig_solver, n_eigs, mtx_b, log) )
ncalls, times, nonlin_v = aux
vec_vhxc = broyden3( nonlin_v, vec_vhxc, verbose = True )
out = iterate( vec_vhxc, pb, conf, eig_solver, n_eigs, mtx_b )
eigs, mtx_s_phi, vec_n, vec_vh, vec_vxc = out
if options.plot:
log( finished = True )
pause()
coor = pb.domain.get_mesh_coors()
r = coor[:,0]**2 + coor[:,1]**2 + coor[:,2]**2
vec_nr2 = vec_n * r
n_eigs = eigs.shape[0]
mtx_phi = nm.empty( (pb.variables.di.ptr[-1], mtx_s_phi.shape[1]),
dtype = nm.float64 )
for ii in xrange( n_eigs ):
mtx_phi[:,ii] = pb.variables.make_full_vec( mtx_s_phi[:,ii] )
save = get_default_attr( conf.options, 'save_eig_vectors', None )
out = {}
for ii in xrange( n_eigs ):
if save is not None:
if (ii >= save[0]) and (ii < (n_eigs - save[1])): continue
aux = pb.state_to_output( mtx_phi[:,ii] )
key = aux.keys()[0]
out[key+'%03d' % ii] = aux[key]
update_state_to_output( out, pb, vec_n, 'n' )
update_state_to_output( out, pb, vec_nr2, 'nr2' )
update_state_to_output( out, pb, vec_vh, 'vh' )
update_state_to_output( out, pb, vec_vxc, 'vxc' )
ofn_trunk = options.output_filename_trunk
pb.domain.mesh.write( ofn_trunk + '.vtk', io = 'auto', out = out )
fd = open( ofn_trunk + '_eigs.txt', 'w' )
eigs.tofile( fd, ' ' )
fd.close()
return Struct( pb = pb, eigs = eigs, mtx_phi = mtx_phi )
##
# c: 01.02.2008, r: 03.03.2008
def solve_eigen_problem1( conf, options ):
pb = ProblemDefinition.from_conf( conf )
dim = pb.domain.mesh.dim
pb.time_update()
dummy = pb.create_state_vector()
output( 'assembling lhs...' )
tt = time.clock()
mtx_a = eval_term_op( dummy, conf.equations['lhs'], pb,
dw_mode = 'matrix', tangent_matrix = pb.mtx_a )
output( '...done in %.2f s' % (time.clock() - tt) )
output( 'assembling rhs...' )
tt = time.clock()
mtx_b = eval_term_op( dummy, conf.equations['rhs'], pb,
dw_mode = 'matrix', tangent_matrix = pb.mtx_a.copy() )
output( '...done in %.2f s' % (time.clock() - tt) )
#mtxA.save( 'tmp/a.txt', format='%d %d %.12f\n' )
#mtxB.save( 'tmp/b.txt', format='%d %d %.12f\n' )
try:
n_eigs = conf.options.n_eigs
except AttributeError:
n_eigs = mtx_a.shape[0]
if n_eigs is None:
n_eigs = mtx_a.shape[0]
## mtx_a.save( 'a.txt', format='%d %d %.12f\n' )
## mtx_b.save( 'b.txt', format='%d %d %.12f\n' )
print 'computing resonance frequencies...'
eig = Solver.any_from_conf( pb.get_solver_conf( conf.options.eigen_solver ) )
eigs, mtx_s_phi = eig( mtx_a, mtx_b, conf.options.n_eigs )
from sfepy.fem.mesh import Mesh
bounding_box = Mesh.from_file("tmp/mesh.vtk").get_bounding_box()
# this assumes a box (3D), or a square (2D):
a = bounding_box[1][0] - bounding_box[0][0]
E_exact = None
if options.hydrogen or options.boron:
if options.hydrogen:
Z = 1
elif options.boron:
Z = 5
if options.dim == 2:
E_exact = [-float(Z)**2/2/(n-0.5)**2/4 for n in [1]+[2]*3+[3]*5 +\
[4]*8 + [5]*15]
elif options.dim == 3:
E_exact = [-float(Z)**2/2/n**2 for n in [1]+[2]*2**2+[3]*3**2 ]
if options.well:
if options.dim == 2:
E_exact = [pi**2/(2*a**2)*x for x in [2, 5, 5, 8, 10, 10, 13, 13,
17, 17, 18, 20, 20 ] ]
elif options.dim == 3:
E_exact = [pi**2/(2*a**2)*x for x in [3, 6, 6, 6, 9, 9, 9, 11, 11,
11, 12, 14, 14, 14, 14, 14, 14, 17, 17, 17] ]
if options.oscillator:
if options.dim == 2:
E_exact = [1] + [2]*2 + [3]*3 + [4]*4 + [5]*5 + [6]*6
elif options.dim == 3:
E_exact = [float(1)/2+x for x in [1]+[2]*3+[3]*6+[4]*10 ]
if E_exact is not None:
print "a=%f" % a
print "Energies:"
print "n exact FEM error"
for i, e in enumerate(eigs):
from numpy import NaN
if i < len(E_exact):
exact = E_exact[i]
err = 100*abs((exact - e)/exact)
else:
exact = NaN
err = NaN
print "%d: %.8f %.8f %5.2f%%" % (i, exact, e, err)
else:
print eigs
## import sfepy.base.plotutils as plu
## plu.spy( mtx_b, eps = 1e-12 )
## plu.pylab.show()
## pause()
n_eigs = eigs.shape[0]
mtx_phi = nm.empty( (pb.variables.di.ptr[-1], mtx_s_phi.shape[1]),
dtype = nm.float64 )
for ii in xrange( n_eigs ):
mtx_phi[:,ii] = pb.variables.make_full_vec( mtx_s_phi[:,ii] )
save = get_default_attr( conf.options, 'save_eig_vectors', None )
out = {}
for ii in xrange( n_eigs ):
if save is not None:
if (ii > save[0]) and (ii < (n_eigs - save[1])): continue
aux = pb.state_to_output( mtx_phi[:,ii] )
key = aux.keys()[0]
out[key+'%03d' % ii] = aux[key]
ofn_trunk = options.output_filename_trunk
pb.domain.mesh.write( ofn_trunk + '.vtk', io = 'auto', out = out )
fd = open( ofn_trunk + '_eigs.txt', 'w' )
eigs.tofile( fd, ' ' )
fd.close()
return Struct( pb = pb, eigs = eigs, mtx_phi = mtx_phi )
usage = """%prog [options] filename_in
Solver for electronic structure problems.
You need to create a mesh (optionally specify a dimension):
$ ./schroedinger.py --mesh --2d
and then pick a problem to solve, some examples below (the dimension is
determined by the mesh that you created above):
$ ./schroedinger.py --hydrogen
$ ./schroedinger.py --well
$ ./schroedinger.py --dft
and visualize the result:
$ paraview --data=mesh.vtk
"""
help = {
'filename' : 'basename of output file(s) [default: %default.vtk]',
'well' : "solve infinite potential well (particle in a box) problem",
'oscillator' : "solve spherically symmetric linear harmonic oscillator (1 electron) problem",
'hydrogen' : "solve the hydrogen atom",
'boron' : "solve the boron atom with 1 electron",
"mesh": "creates a mesh",
"dim": "Create a 2D mesh, instead of the default 3D",
"dft": "Do a DFT calculation",
"plot": "plot convergence of DFT iterations (with --dft)",
}
##
# c: 01.02.2008, r: 21.03.2008
def main():
version = open( op.join( init_sfepy.install_dir,
'VERSION' ) ).readlines()[0][:-1]
parser = OptionParser( usage = usage, version = "%prog " + version )
parser.add_option( "--mesh",
action = "store_true", dest = "mesh",
default = False, help = help['mesh'] )
parser.add_option( "--2d",
action = "store_true", dest = "dim2",
default = False, help = help['dim'] )
parser.add_option( "-o", "", metavar = 'filename',
action = "store", dest = "output_filename_trunk",
default = "mesh", help = help['filename'] )
parser.add_option( "--oscillator",
action = "store_true", dest = "oscillator",
default = False, help = help['oscillator'] )
parser.add_option( "--well",
action = "store_true", dest = "well",
default = False, help = help['well'] )
parser.add_option( "--hydrogen",
action = "store_true", dest = "hydrogen",
default = False, help = help['hydrogen'] )
parser.add_option( "--boron",
action = "store_true", dest = "boron",
default = False, help = help['boron'] )
parser.add_option( "--dft",
action = "store_true", dest = "dft",
default = False, help = help['dft'] )
parser.add_option( "-p", "--plot",
action = "store_true", dest = "plot",
default = False, help = help['plot'] )
options, args = parser.parse_args()
if len( args ) == 1:
filename_in = args[0];
elif len( args ) == 0:
if options.oscillator:
dim = MeshIO.any_from_filename("tmp/mesh.vtk").read_dimension()
if dim == 2:
filename_in = "input/quantum/oscillator2d.py"
else:
assert_( dim == 3 )
filename_in = "input/quantum/oscillator3d.py"
options.dim = dim
print "Dimension:", dim
elif options.well:
dim = MeshIO.any_from_filename("tmp/mesh.vtk").read_dimension()
if dim == 2:
filename_in = "input/quantum/well2d.py"
else:
assert_( dim == 3 )
filename_in = "input/quantum/well3d.py"
options.dim = dim
print "Dimension:", dim
elif options.hydrogen:
dim = MeshIO.any_from_filename("tmp/mesh.vtk").read_dimension()
if dim == 2:
filename_in = "input/quantum/hydrogen2d.py"
else:
assert_( dim == 3 )
filename_in = "input/quantum/hydrogen3d.py"
options.dim = dim
print "Dimension:", dim
elif options.boron:
dim = MeshIO.any_from_filename("tmp/mesh.vtk").read_dimension()
if dim == 2:
filename_in = "input/quantum/boron2d.py"
else:
assert_( dim == 3 )
filename_in = "input/quantum/boron3d.py"
options.dim = dim
print "Dimension:", dim
elif options.mesh:
try:
os.makedirs("tmp")
except OSError, e:
if e.errno != 17: # [Errno 17] File exists
raise
if options.dim2:
print "Dimension: 2"
os.system("cp database/quantum/square.geo tmp/mesh.geo")
os.system("gmsh -2 tmp/mesh.geo -format mesh")
os.system("script/mesh_to_vtk.py tmp/mesh.mesh tmp/mesh.vtk")
else:
print "Dimension: 3"
import geom
from sfepy.fem.mesh import Mesh
try:
from site_cfg import tetgen_path
except ImportError:
tetgen_path = '/usr/bin/tetgen'
os.system("gmsh -0 database/box.geo -o tmp/x.geo")
g = geom.read_gmsh("tmp/x.geo")
g.printinfo()
geom.write_tetgen(g, "tmp/t.poly")
geom.runtetgen("tmp/t.poly", a=0.03, Q=1.0, quadratic=False,
tetgenpath=tetgen_path)
m = Mesh.from_file("tmp/t.1.node")
m.write("tmp/mesh.vtk", io="auto")
print "Mesh written to tmp/mesh.vtk"
return
elif options.dft:
dim = MeshIO.any_from_filename("tmp/mesh.vtk").read_dimension()
if dim == 2:
filename_in = "input/quantum/dft2d.py"
else:
assert_( dim == 3 )
filename_in = "input/quantum/dft3d.py"
print "Dimension:", dim
options.dim = dim
else:
parser.print_help()
return
else:
parser.print_help()
return
required, other = get_standard_keywords()
conf = ProblemConf.from_file( filename_in, required, other )
if options.dft:
if options.plot:
from sfepy.base.plotutils import pylab
options.plot = pylab is not None
evp = solve_eigen_problem_n( conf, options )
else:
evp = solve_eigen_problem1( conf, options )
print "Solution saved to %s.vtk" % options.output_filename_trunk
if __name__ == '__main__':
main()