forked from sfepy/sfepy
/
eigen.py
executable file
·642 lines (508 loc) · 24.1 KB
/
eigen.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#!/usr/bin/env python
# 12.01.2007, c
import os.path as op
import shutil
from optparse import OptionParser
import sfepy
from sfepy.base.base import *
from sfepy.base.conf import ProblemConf, get_standard_keywords
from sfepy.fem import ProblemDefinition
from sfepy.fem.evaluate import assemble_by_blocks
from sfepy.homogenization.phono import transform_plot_data, plot_logs, \
plot_gaps, detect_band_gaps, compute_cat, compute_polarization_angles
from sfepy.homogenization.engine import HomogenizationEngine
from sfepy.applications import SimpleApp
from sfepy.solvers import Solver, eig
from sfepy.base.plotutils import plt
def make_save_hook( base_name, post_process_hook = None, file_per_var = None ):
def save_phono_correctors( state, problem, ir, ic ):
problem.save_state( (base_name % (ir, ic)) + '.vtk', state,
post_process_hook = post_process_hook,
file_per_var = file_per_var )
return save_phono_correctors
def try_set_defaults( obj, attr, defaults ):
try:
values = getattr( obj, attr )
set_defaults( values, defaults )
except:
values = defaults
return values
def report_iw_cat( iw_dir, christoffel ):
output( 'incident wave direction:' )
output( iw_dir )
output( 'Christoffel acoustic tensor:' )
output( christoffel )
class AcousticBandGapsApp( SimpleApp ):
def process_options( options ):
"""Application options setup. Sets default values for missing
non-compulsory options."""
get = options.get_default_attr
clear_cache = get( 'clear_cache', {} )
eigensolver = get( 'eigensolver', 'eig.sgscipy' )
eig_problem = get( 'eig_problem', 'simple' )
schur = get( 'schur', None )
elasticity_contrast = get( 'elasticity_contrast', 1.0 )
scale_epsilon = get( 'scale_epsilon', 1.0 )
incident_wave_dir = get( 'incident_wave_dir', None )
dispersion = get( 'dispersion', 'simple' )
dispersion_conf = get( 'dispersion_conf', None )
homogeneous = get( 'homogeneous', False )
save = get( 'save_eig_vectors', (0, 0) )
eig_range = get( 'eig_range', None )
freq_margins = get( 'freq_margins', (5, 5) )
# Given in per cent.
freq_margins = 0.01 * nm.array( freq_margins, dtype = nm.float64 )
fixed_eig_range = get( 'fixed_eig_range', None )
# Given in per cent.
freq_step = 0.01 * get( 'freq_step', 5 )
feps = get( 'feps', 1e-8 )
zeps = get( 'zeps', 1e-8 )
teps = get( 'teps', 1e-4 )
teps_rel = get( 'teps_rel', True )
eig_vector_transform = get( 'eig_vector_transform', None )
plot_transform = get( 'plot_transform', None )
plot_transform_wave = get( 'plot_transform_wave', None )
plot_transform_angle = get( 'plot_transform_angle', None )
plot_options = get( 'plot_options', {'show' : True,'legend' : False,} )
fig_name = get( 'fig_name', None )
fig_name_wave = get( 'fig_name_wave', None )
fig_name_angle = get( 'fig_name_angle', None )
aux = {
'resonance' : 'eigenfrequencies',
'masked' : 'masked eigenfrequencies',
'eig_min' : 'min eig($M^*$)',
'eig_mid' : 'mid eig($M^*$)',
'eig_max' : 'max eig($M^*$)',
'y_axis' : 'eigenvalues of mass matrix $M^*$',
}
plot_labels = try_set_defaults( options, 'plot_labels', aux )
aux = {
'resonance' : 'eigenfrequencies',
'masked' : 'masked eigenfrequencies',
'eig_min' : r'$\kappa$(min)',
'eig_mid' : r'$\kappa$(mid)',
'eig_max' : r'$\kappa$(max)',
'y_axis' : 'polarization angles',
}
plot_labels_angle = try_set_defaults( options, 'plot_labels_angle', aux )
aux = {
'resonance' : 'eigenfrequencies',
'masked' : 'masked eigenfrequencies',
'eig_min' : r'wave number (min)',
'eig_mid' : r'wave number (mid)',
'eig_max' : r'wave number (max)',
'y_axis' : 'wave numbers',
}
plot_labels_wave = try_set_defaults( options, 'plot_labels_wave', aux )
plot_rsc = {
'resonance' : {'linewidth' : 0.5, 'color' : 'r', 'linestyle' : '-' },
'masked' : {'linewidth' : 0.5, 'color' : 'r', 'linestyle' : ':' },
'x_axis' : {'linewidth' : 0.5, 'color' : 'k', 'linestyle' : '--' },
'eig_min' : {'linewidth' : 0.5, 'color' : 'b', 'linestyle' : '--' },
'eig_mid' : {'linewidth' : 0.5, 'color' : 'b', 'linestyle' : '-.' },
'eig_max' : {'linewidth' : 0.5, 'color' : 'b', 'linestyle' : '-' },
'strong_gap' : {'linewidth' : 0, 'facecolor' : (1, 1, 0.5) },
'weak_gap' : {'linewidth' : 0, 'facecolor' : (1, 1, 1) },
'propagation' : {'linewidth' : 0, 'facecolor' : (0.5, 1, 0.5) },
'params' : {'axes.labelsize': 'large',
'text.fontsize': 'large',
'legend.fontsize': 'large',
'xtick.labelsize': 'large',
'ytick.labelsize': 'large',
'text.usetex': False},
}
plot_rsc = try_set_defaults( options, 'plot_rsc', plot_rsc )
eigenmomentum = get( 'eigenmomentum', None,
'missing "eigenmomentum" in options!' )
region_to_material = get( 'region_to_material', None,
'missing "region_to_material" in options!' )
tensor_names = get( 'tensor_names', None,
'missing "tensor_names" in options!' )
volume = get( 'volume', None, 'missing "volume" in options!' )
if eig_problem == 'simple_liquid':
liquid_region = get('liquid_region', None,
'missing "liquid_region" in options!')
else:
liquid_region = None
return Struct( **locals() )
process_options = staticmethod( process_options )
def process_options_pv( options ):
"""Application options setup for phase velocity computation. Sets
default values for missing non-compulsory options."""
get = options.get_default_attr
clear_cache = get( 'clear_cache', {} )
eigensolver = get( 'eigensolver', 'eig.sgscipy' )
incident_wave_dir = get( 'incident_wave_dir', None )
dispersion = get( 'dispersion', 'simple' )
dispersion_conf = get( 'dispersion_conf', None )
homogeneous = get( 'homogeneous', False )
fig_suffix = get( 'fig_suffix', '.pdf' )
region_to_material = get( 'region_to_material', None,
'missing "region_to_material" in options!' )
tensor_names = get( 'tensor_names', None,
'missing "tensor_names" in options!' )
volume = get( 'volume', None, 'missing "volume" in options!' )
return Struct( **locals() )
process_options_pv = staticmethod( process_options_pv )
def __init__( self, conf, options, output_prefix, **kwargs ):
SimpleApp.__init__( self, conf, options, output_prefix,
init_equations = False )
self.setup_options()
self.cached_coefs = None
self.cached_iw_dir = None
self.cached_christoffel = None
self.cached_evp = None
output_dir = self.problem.output_dir
shutil.copyfile( conf._filename,
op.join( output_dir, op.basename( conf._filename ) ) )
def setup_options( self ):
SimpleApp.setup_options( self )
if self.options.phase_velocity:
process_options = AcousticBandGapsApp.process_options_pv
else:
process_options = AcousticBandGapsApp.process_options
self.app_options += process_options( self.conf.options )
def call( self ):
"""In parametric runs, cached data (homogenized coefficients,
Christoffel acoustic tensor and eigenvalue problem solution) are
cleared according to 'clear_cache' aplication options.
Example:
clear_cache = {'cached_christoffel' : True, 'cached_evp' : True}
"""
options = self.options
for key, val in self.app_options.clear_cache.iteritems():
if val and key.startswith('cached_'):
setattr(self, key, None)
if options.phase_velocity:
# No band gaps in this case.
return self.compute_phase_velocity()
evp = self.solve_eigen_problem()
self.fix_eig_range( evp.eigs.shape[0] )
if options.detect_band_gaps:
bg = detect_band_gaps( self.problem, evp.kind,
evp.eigs_rescaled, evp.eig_vectors,
self.app_options, self.conf.funmod )
if options.plot:
plot_range, teigs = transform_plot_data( bg.logs.eigs,
bg.opts.plot_transform,
self.conf.funmod )
plot_rsc = bg.opts.plot_rsc
plot_opts = bg.opts.plot_options
plot_labels = bg.opts.plot_labels
plt.rcParams.update( plot_rsc['params'] )
fig = plot_gaps( 1, plot_rsc, bg.gaps, bg.kinds,
bg.freq_range_margins, plot_range,
clear = True )
fig = plot_logs( 1, plot_rsc, plot_labels, bg.logs.freqs, teigs,
bg.valid[bg.eig_range],
bg.freq_range_initial,
plot_range, False,
show_legend = plot_opts['legend'],
new_axes = True )
fig_name = bg.opts.fig_name
if fig_name is not None:
fig.savefig( fig_name )
if plot_opts['show']:
plt.show()
elif options.analyze_dispersion:
christoffel, iw_dir = self.compute_cat(ret_iw_dir=True)
bg = detect_band_gaps( self.problem, evp.kind,
evp.eigs_rescaled, evp.eig_vectors,
self.app_options, self.conf.funmod,
christoffel = christoffel )
output( 'computing polarization angles...' )
pas = compute_polarization_angles( iw_dir, bg.logs.eig_vectors )
output( '...done' )
bg.polarization_angles = pas
output( 'computing phase velocity...' )
bg.phase_velocity = self.compute_phase_velocity()
output( '...done' )
if options.plot:
plot_rsc = bg.opts.plot_rsc
plot_opts = bg.opts.plot_options
plt.rcParams.update( plot_rsc['params'] )
aux = transform_plot_data( pas,
bg.opts.plot_transform_angle,
self.conf.funmod )
plot_range, pas = aux
plot_labels = bg.opts.plot_labels_angle
fig = plot_gaps( 1, plot_rsc, bg.gaps, bg.kinds,
bg.freq_range_margins, plot_range,
clear = True )
fig = plot_logs( 1, plot_rsc, plot_labels, bg.logs.freqs, pas,
bg.valid[bg.eig_range],
bg.freq_range_initial,
plot_range, False,
show_legend = plot_opts['legend'],
new_axes = True )
fig_name = bg.opts.fig_name_angle
if fig_name is not None:
fig.savefig( fig_name )
aux = transform_plot_data( bg.logs.eigs,
bg.opts.plot_transform_wave,
self.conf.funmod )
plot_range, teigs = aux
plot_labels = bg.opts.plot_labels_wave
fig = plot_gaps( 2, plot_rsc, bg.gaps, bg.kinds,
bg.freq_range_margins, plot_range,
clear = True )
fig = plot_logs( 2, plot_rsc, plot_labels, bg.logs.freqs, teigs,
bg.valid[bg.eig_range],
bg.freq_range_initial,
plot_range, False,
show_legend = plot_opts['legend'],
new_axes = True )
fig_name = bg.opts.fig_name_wave
if fig_name is not None:
fig.savefig( fig_name )
if plot_opts['show']:
plt.show()
else:
bg = None
return evp, bg
def fix_eig_range( self, n_eigs ):
eig_range = get_default( self.app_options.eig_range, (0, n_eigs) )
if eig_range[-1] < 0:
eig_range[-1] += n_eigs + 1
assert_( eig_range[0] < (eig_range[1] - 1) )
assert_( eig_range[1] <= n_eigs )
self.app_options.eig_range = eig_range
def solve_eigen_problem( self, ofn_trunk = None, post_process_hook = None ):
if self.cached_evp is not None:
return self.cached_evp
problem = self.problem
ofn_trunk = get_default( ofn_trunk, problem.ofn_trunk,
'output file name trunk missing!' )
post_process_hook = get_default( post_process_hook,
self.post_process_hook )
conf = self.conf
eig_problem = self.app_options.eig_problem
if eig_problem in ['simple', 'simple_liquid']:
problem.set_equations( conf.equations )
problem.time_update()
mtx_a = problem.evaluate(conf.equations['lhs'], mode='weak',
auto_init=True, dw_mode='matrix')
mtx_m = problem.evaluate(conf.equations['rhs'], mode='weak',
dw_mode='matrix')
elif eig_problem == 'schur':
# A = K + B^T D^{-1} B.
mtx = assemble_by_blocks( conf.equations, self.problem,
ebcs = conf.ebcs,
epbcs = conf.epbcs )
problem.set_equations( conf.equations )
problem.time_update()
ls = Solver.any_from_conf( problem.ls_conf,
presolve = True, mtx = mtx['D'] )
mtx_b, mtx_m = mtx['B'], mtx['M']
mtx_dib = nm.empty( mtx_b.shape, dtype = mtx_b.dtype )
for ic in xrange( mtx_b.shape[1] ):
mtx_dib[:,ic] = ls( mtx_b[:,ic].toarray().squeeze() )
mtx_a = mtx['K'] + mtx_b.T * mtx_dib
else:
raise NotImplementedError
## from sfepy.base.plotutils import spy, plt
## spy( mtx_b, eps = 1e-12 )
## plt.show()
## mtx_a.save( 'a.txt', format='%d %d %.12f\n' )
## mtx_b.save( 'b.txt', format='%d %d %.12f\n' )
## pause()
output( 'computing resonance frequencies...' )
tt = [0]
if isinstance( mtx_a, sc.sparse.spmatrix ):
mtx_a = mtx_a.toarray()
if isinstance( mtx_m, sc.sparse.spmatrix ):
mtx_m = mtx_m.toarray()
eigs, mtx_s_phi = eig(mtx_a, mtx_m, return_time=tt,
method=self.app_options.eigensolver)
eigs[eigs<0.0] = 0.0
output( '...done in %.2f s' % tt[0] )
output( 'original eigenfrequencies:' )
output( eigs )
opts = self.app_options
epsilon2 = opts.scale_epsilon * opts.scale_epsilon
eigs_rescaled = (opts.elasticity_contrast / epsilon2) * eigs
output( 'rescaled eigenfrequencies:' )
output( eigs_rescaled )
output( 'number of eigenfrequencies: %d' % eigs.shape[0] )
try:
assert_( nm.isfinite( eigs ).all() )
except ValueError:
debug()
# B-orthogonality check.
## print nm.dot( mtx_s_phi[:,5], nm.dot( mtx_m, mtx_s_phi[:,5] ) )
## print nm.dot( mtx_s_phi[:,5], nm.dot( mtx_m, mtx_s_phi[:,0] ) )
## debug()
n_eigs = eigs.shape[0]
variables = problem.get_variables()
mtx_phi = nm.empty( (variables.di.ptr[-1], mtx_s_phi.shape[1]),
dtype = nm.float64 )
make_full = variables.make_full_vec
if eig_problem in ['simple', 'simple_liquid']:
for ii in xrange( n_eigs ):
mtx_phi[:,ii] = make_full( mtx_s_phi[:,ii] )
eig_vectors = mtx_phi
elif eig_problem == 'schur':
# Update also eliminated variables.
schur = self.app_options.schur
primary_var = schur['primary_var']
eliminated_var = schur['eliminated_var']
mtx_s_phi_schur = - sc.dot( mtx_dib, mtx_s_phi )
aux = nm.empty( (variables.adi.ptr[-1],),
dtype = nm.float64 )
set = variables.set_state_part
for ii in xrange( n_eigs ):
set( aux, mtx_s_phi[:,ii], primary_var, stripped = True )
set( aux, mtx_s_phi_schur[:,ii], eliminated_var,
stripped = True )
mtx_phi[:,ii] = make_full( aux )
indx = variables.get_indx( primary_var )
eig_vectors = mtx_phi[indx,:]
save = self.app_options.save
out = {}
for ii in xrange( n_eigs ):
if (ii >= save[0]) and (ii < (n_eigs - save[1])): continue
aux = problem.state_to_output( mtx_phi[:,ii] )
for name, val in aux.iteritems():
out[name+'%03d' % ii] = val
if post_process_hook is not None:
out = post_process_hook( out, problem, mtx_phi )
problem.domain.mesh.write( ofn_trunk + '.vtk', io = 'auto', out = out )
fd = open( ofn_trunk + '_eigs.txt', 'w' )
eigs.tofile( fd, ' ' )
fd.close()
evp = Struct( kind = eig_problem,
eigs = eigs, eigs_rescaled = eigs_rescaled,
eig_vectors = eig_vectors )
self.cached_evp = evp
return evp
def eval_homogenized_coefs( self ):
if self.cached_coefs is not None:
return self.cached_coefs
opts = self.app_options
if opts.homogeneous:
rtm = opts.region_to_material
mat_region = rtm.keys()[0]
mat_name = rtm[mat_region]
self.problem.update_materials()
mat = self.problem.materials[mat_name]
coefs = mat.get_data( mat_region, 0, opts.tensor_names )
else:
dc = opts.dispersion_conf
dconf = ProblemConf.from_dict( dc['input'], dc['module'] )
dconf.materials = self.conf.materials
dconf.fe = self.conf.fe
dconf.regions.update( self.conf.regions )
dconf.options['output_dir'] = self.problem.output_dir
volume = opts.volume(self.problem, 'Y')
problem = ProblemDefinition.from_conf(dconf, init_equations=False)
he = HomogenizationEngine( problem, self.options, volume = volume )
coefs = he()
## print coefs
## pause()
output.prefix = self.output_prefix
self.cached_coefs = coefs
return coefs
def compute_cat( self, ret_iw_dir=False ):
"""Compute the Christoffel acoustic tensor, given the incident wave
direction."""
opts = self.app_options
iw_dir = nm.array( opts.incident_wave_dir, dtype = nm.float64 )
dim = self.problem.get_dim()
assert_( dim == iw_dir.shape[0] )
iw_dir = iw_dir / nla.norm( iw_dir )
if self.cached_christoffel is not None:
christoffel = self.cached_christoffel
else:
coefs = self.eval_homogenized_coefs()
christoffel = compute_cat( coefs, iw_dir,
self.app_options.dispersion )
report_iw_cat( iw_dir, christoffel )
self.cached_christoffel = christoffel
if ret_iw_dir:
return christoffel, iw_dir
else:
return christoffel
def compute_phase_velocity( self ):
from sfepy.homogenization.phono import compute_density_volume_info
opts = self.app_options
dim = self.problem.domain.mesh.dim
christoffel = self.compute_cat()
self.problem.update_materials()
dv_info = compute_density_volume_info( self.problem, opts.volume,
opts.region_to_material )
output( 'average density:', dv_info.average_density )
eye = nm.eye( dim, dim, dtype = nm.float64 )
mtx_mass = eye * dv_info.average_density
meigs, mvecs = eig( mtx_mass, mtx_b = christoffel,
eigenvectors = True, method = opts.eigensolver )
phase_velocity = 1.0 / nm.sqrt( meigs )
return phase_velocity
usage = """%prog [options] filename_in"""
help = {
'filename' :
'basename of output file(s) [default: <basename of input file>]',
'detect_band_gaps' :
'detect frequency band gaps',
'analyze_dispersion' :
'analyze dispersion properties (low frequency domain)',
'plot' :
'plot frequency band gaps, assumes -b',
'phase_velocity' :
'compute phase velocity (frequency-independet mass only)'
}
def main():
parser = OptionParser(usage = usage, version = "%prog " + sfepy.__version__)
parser.add_option( "-o", "", metavar = 'filename',
action = "store", dest = "output_filename_trunk",
default = None, help = help['filename'] )
parser.add_option( "-b", "--band-gaps",
action = "store_true", dest = "detect_band_gaps",
default = False, help = help['detect_band_gaps'] )
parser.add_option( "-d", "--dispersion",
action = "store_true", dest = "analyze_dispersion",
default = False, help = help['analyze_dispersion'] )
parser.add_option( "-p", "--plot",
action = "store_true", dest = "plot",
default = False, help = help['plot'] )
parser.add_option( "--phase-velocity",
action = "store_true", dest = "phase_velocity",
default = False, help = help['phase_velocity'] )
options, args = parser.parse_args()
if options.plot:
if plt is None:
output( 'matplotlib.pyplot cannot be imported, ignoring option -p!' )
options.plot = False
elif options.analyze_dispersion == False:
options.detect_band_gaps = True
if (len( args ) == 1):
filename_in = args[0];
else:
parser.print_help(),
return
required, other = get_standard_keywords()
required.remove( 'solver_[0-9]+|solvers' )
if options.phase_velocity:
required.remove( 'ebc_[0-9]+|ebcs' )
required.remove( 'equations' )
conf = ProblemConf.from_file( filename_in, required, other )
app = AcousticBandGapsApp( conf, options, 'eigen:' )
opts = conf.options
if hasattr( opts, 'parametric_hook' ): # Parametric study.
parametric_hook = getattr( conf, opts.parametric_hook )
app.parametrize( parametric_hook )
app()
if __name__ == '__main__':
## mtx_k = io.read_sparse_matrix_hdf5( '1todo/K.h5', output_format = 'csr' )
## print mtx_k.__repr__()
## mtx_m = io.read_sparse_matrix_hdf5( '1todo/M.h5', output_format = 'csr' )
## print mtx_m.__repr__()
## mtx_k.save( 'k.txt', format='%d %d %.12f\n' )
## mtx_m.save( 'm.txt', format='%d %d %.12f\n' )
## eigs, mtx_s_phi = eig( mtx_k.toarray(), mtx_m.toarray(),
## print_time = True )
## print eigs
## eigs, aux = eig( mtx_m.toarray(),
## print_time = True )
## print eigs
## pause()
main()