Skip to content

DynamicalLanczos Module

tdscha.DynamicalLanczos

Lanczos(ensemble=None, mode=None, unwrap_symmetries=False, select_modes=None, use_wigner=True, lo_to_split='random')

Bases: object

INITIALIZE THE LANCZOS

This function extracts the weights, the X and Y arrays for the d3 and d4 computation as well as the polarization vectors and frequencies.

Source code in tdscha/DynamicalLanczos.py
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
    def __init__(self, ensemble = None, mode = None, unwrap_symmetries = False, select_modes = None, use_wigner = True, lo_to_split = "random"):
        """
        INITIALIZE THE LANCZOS
        ======================

        This function extracts the weights, the X and Y arrays for the d3 and d4
        computation as well as the polarization vectors and frequencies.

        Parameters
        ----------
            ensemble : Ensemble.Ensemble()
                The ensemble upon which you want to compute the DynamicalResponce
            mode : int
                The mode of the speedup.
                   0) Slow python implementation 
                      Use this just for testing
                   1) Fast C serial code
                   2) Fast C parallel (MPI)
                   3) Fast Julia multithreading (only if julia is available)
            unwrap_symmetries : bool
                If true (default), the ensemble is unwrapped to respect the symmetries.
                This requires SPGLIB installed.
            select_modes : ndarray(size = n_modes, dtype = bool)
                A mask for each mode, if False, the mode is neglected. Use this to exclude some modes that you know are not
                involved in the calculation. If not specified, all modes are considered by default.  
            use_wigner: bool, if True Wigner equations are used.
                involved in the calculation. If not specified, all modes are considered by default.
            lo_to_split : string or ndarray
                Mode of lo_to_splitting. If empty or none, it is LO-TO splitting correction is neglected.
                If a ndarray is provided, it is the direction of q on which the LO-TO splitting is computed.
        """

        if is_julia_enabled():
            self.mode = MODE_FAST_JULIA
        else:
            self.mode = MODE_FAST_SERIAL

        if mode is not None:
            self.mode = mode

        # Define the order
        order = "C"
        #if self.mode >= 1:
        #    order = "F"

        # HERE DEFINE ALL THE VARIABLES FOR THE Dynamical Lanczos
        # The temperature
        self.verbose = True
        self.T = 0
        # Number of atoms in the supercell
        self.nat = 0
        # The array of masses in the supercell, np.shape = 3 * N_at_sc
        self.m = []
        # Auxiliary eigenmodes of SCHA
        self.w = []
        # Auxiliary eigenvectors of SCHA
        self.pols = []
        # The numbero of modes, translations excluded
        self.n_modes = 0
        self.ignore_harmonic = False 
        # Ignore D3 and D4
        self.ignore_v3 = False
        self.ignore_v4 = False
        # Number of configurations
        self.N = 0
        # The weights from the static calculations
        self.rho = []
        # Effective number of configurations
        self.N_eff = 0
        # The satic displacements in the polarization basis
        self.X = []
        # The static forces in the polarization basis
        self.Y = []
        # The vector on which we apply Lanczos
        self.psi = []
        self.eigvals = None
        self.eigvects = None
        # In the custom lanczos mode
        self.a_coeffs = [] # Coefficients on the diagonal
        self.b_coeffs = [] # Coefficients close to the diagonal
        self.c_coeffs = [] # Coefficients in the case of the biconjugate Lanczos
        self.krilov_basis = [] # The basis of the krilov subspace
        self.basis_P = [] # The basis of the P vectors for the biconjugate Lanczos (normalized)
        self.basis_Q = [] # The basis of the Q vectors for the biconjugate Lanczos
        self.s_norm = [] # Store the normalization of the s vector, this allows to rebuild the correct p when needed. 
        self.arnoldi_matrix = [] # If requested, the upper triangular arnoldi matrix
        self.reverse_L = False
        self.shift_value = 0
        self.symmetrize = False
        self.symmetries = None
        self.degenerate_space = None
        self.N_degeneracy = None
        self.initialized = False
        self.perturbation_modulus = 1
        self.dyn = None
        # Unit cell structure
        self.uci_structure = None
        # Structure for the supercell
        self.super_structure = None
        # Symmetries
        self.qe_sym = None
        # The application of L as a linear operator
        self.L_linop = None
        self.M_linop = None
        self.unwrapped = False
        self.sym_julia = None
        self.deg_julia = None
        self.n_syms = 1

        self.u_tilde = None
        # The static forces divided by the sqrt(masses)
        self.f_tilde = None

        self.sym_block_id = None

        # Gamma-only optimization: use only point-group symmetries in replicas,
        # impose translations directly on f_pert_av and d2v_pert_av
        self.gamma_only = False
        self.trans_projector = None    # (n_modes, n_modes) matrix P = (1/n_cells) Σ_R T_R^mode
        self.trans_operators = None    # list of (n_modes, n_modes) T_R^mode matrices

        # Set to True if we want to use the Wigner equations
        self.use_wigner = use_wigner

        # This flag is usefull to work with 1D or 2D systems
        # Default is False meaning that we ignore only translational modes
        self.ignore_small_w = False

        # Setup the attribute control
        self.__total_attributes__ = [item for item in self.__dict__.keys()]
        self.fixed_attributes = True # This must be the last attribute to be setted

        # Perform a bare initialization if the ensemble is not provided
        if ensemble is None:
            return


        # ========== END OF VARIABLE DEFINITION (EACH NEW DEFINITION FROM NOW ON RESULTS IN AN ERROR) =======
        self.dyn = ensemble.current_dyn.Copy() 
        self.uci_structure = ensemble.current_dyn.structure.copy()
        self.super_structure = self.dyn.structure.generate_supercell(self.dyn.GetSupercell())#superdyn.structure

        self.T = ensemble.current_T

        ws, pols = self.dyn.DiagonalizeSupercell(lo_to_split = lo_to_split)

        self.nat = self.super_structure.N_atoms
        n_cell = np.prod(self.dyn.GetSupercell())

        self.qe_sym = CC.symmetries.QE_Symmetry(self.dyn.structure)
        self.qe_sym.SetupQPoint()

        # Get the masses
        m = self.super_structure.get_masses_array()
        self.m = np.tile(m, (3,1)).T.ravel()

        # Remove the translations
        if lo_to_split is not None and self.dyn.effective_charges is not None:
            trans_mask = np.zeros(len(ws), dtype=bool)
            trans_mask[:3] = True
        if not ensemble.ignore_small_w:
            trans_mask = CC.Methods.get_translations(pols, m)
            good_mask  = ~trans_mask
        else:
            self.ignore_small_w = True
            trans_mask = np.abs(ws) < CC.Phonons.__EPSILON_W__
            good_mask  = ~trans_mask

        # If requested, isolate only the specified modes.
        if select_modes is not None:
            if len(select_modes) != len(trans_mask):
                raise ValueError("""
Error, 'select_modes' should be an array of the same lenght of the number of modes.
 n_modes = {} | len(select_modes) = {}
""".format(len(ws), len(select_modes)))
            print()
            print('Selecting some of the modes...')
            print()
            good_mask = (~trans_mask) & select_modes

        # Get the frequencies in Ry and polarization vectors
        self.w = ws[good_mask]
        self.pols = pols[:, good_mask]

        # Correctly reshape the polarization in case only one mode is selected
        if len(self.w) == 1:
            self.pols = self.pols.reshape((len(self.m), 1))

        # Get the number of modes
        self.n_modes = len(self.w)

        # Prepare the list of q point starting from the polarization vectors
        #q_list = CC.symmetries.GetQForEachMode(self.pols, self.uci_structure, self.super_structure, self.dyn.GetSupercell())
        # Store the q vectors in crystal space
        #bg = self.uci_structure.get_reciprocal_vectors() / 2* np.pi
        #self.q_vectors = np.zeros((self.n_modes, 3), dtype = np.double, order = "C")
        #for iq, q in enumerate(q_list):
        #    self.q_vectors[iq, :] = CC.Methods.covariant_coordinate(bg, q)

        # Ignore v3 or v4. You can set them for testing
        # This is no longer implemented in the fast Lanczos
        self.ignore_v3 = False
        self.ignore_v4 = False

        # The number of configurations
        self.N = ensemble.N
        rho = ensemble.rho.copy() 
        # Transform Angstrom -> Bohr
        u = ensemble.u_disps  / Ensemble.Bohr
        # Forces are in Ry/Angstrom for now only
        f = ensemble.forces.reshape(self.N, 3 * self.nat).copy()
        f -= ensemble.sscha_forces.reshape(self.N, 3 * self.nat)

        # Get the average force in the unit cell, (N_at_uc, 3)
        f_mean = ensemble.get_average_forces(get_error = False)

        # Perform the symmetrization of the average force
        qe_sym = CC.symmetries.QE_Symmetry(self.dyn.structure)
        qe_sym.SetupQPoint()
        qe_sym.SymmetrizeVector(f_mean)

        # Reproduce the average force on the full supercell
        f_mean = np.tile(f_mean, (np.prod(ensemble.current_dyn.GetSupercell()), 1)).ravel()

        # Transform forces in Ry/Bohr
        f_mean *= Ensemble.Bohr

        # Subtract also the average force to clean more the stochastic noise
        #av_force = ensemble.get_average_forces(get_error = False).ravel()
        #new_av_force = np.tile(av_force, (n_cell, 1)).ravel()

        #f -= np.tile(new_av_force, (self.N, 1)) 

        # Transform in Ry/Bohr
        f *= Ensemble.Bohr

        if unwrap_symmetries:
            u, f, rho = ensemble.get_unwrapped_ensemble()
            self.N = len(rho)
            self.unwrapped = True

            u /= Ensemble.Bohr
            f *= Ensemble.Bohr

        # Subtract the SSCHA GRADIENT on average position
        # In this way the calculation works even if the system is not in equilibrium
        #print(np.shape(f), np.shape(f_mean))
        f[:, :] -= np.tile(f_mean, (self.N, 1))

        # Perform the mass rescale to get the tilde variables
        u *= np.tile(np.sqrt(self.m), (self.N, 1)) 
        f /= np.tile(np.sqrt(self.m), (self.N, 1)) 

        # Get the info about the ensemble
        self.rho = rho
        self.N_eff = np.sum(self.rho)

        # Mass rescaled quantities
        self.u_tilde = u
        self.f_tilde = f

        # The dispalcements in BOHR mass resclaed and in polarization basis
        self.X = np.zeros((self.N, self.n_modes), order = order, dtype = TYPE_DP)
        # The forces in RY/BOHR mass resclaed and in polarization basis
        self.Y = np.zeros((self.N, self.n_modes), order = order, dtype = TYPE_DP)

        # Convert in the polarization space the displacements and the forces
        self.X[:, :] = self.u_tilde.dot(self.pols) #.T.dot(self.u_tilde)
        self.Y[:, :] = self.f_tilde.dot(self.pols) #self.pols.T.dot(self.f_tilde)

        # Prepare the variable used for the working
        # The len of psi = N_modes + 0.5 * N_modes * (N_modes + 1) + 0.5 * N_modes * (N_modes + 1)
        len_psi = self.n_modes
        #if self.T < __EPSILON__:
        #    len_psi += self.n_modes**2
        #else:
        len_psi += self.n_modes * (self.n_modes + 1)
        #print("N MODES:", self.n_modes)
        #print("LEN PSI:", len_psi)

        # In Wigner the variables are a'^(1) and b'^(1) 
        # In Standard the variables are Y^(1) and ReA^(1)

        ##########################################################
        # Psi contains R^(1), Upsilon^(1)-a'^(1), ReA^(1)-b'^(1) #
        ##########################################################

        # Everything is in the polarization basis
        self.psi = np.zeros(len_psi, dtype = TYPE_DP)

        ################################################################
        # For the matrices the code will store only the upper triangle #
        ################################################################

        # Prepare the L as a linear operator 
        # Prepare the possibility to transpose the matrix with L_transp
        def L_transp(psi):
            return self.apply_full_L(psi, transpose = True)
        self.L_linop = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)),\
                                                          matvec = self.apply_full_L, rmatvec = L_transp, dtype = TYPE_DP)

        # Define the preconditioner
        def M_transp(psi):
            return self.apply_L1_inverse_FT(psi, transpose = True)
        self.M_linop = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)),\
                                                          matvec = self.apply_L1_inverse_FT, rmatvec = M_transp, dtype = TYPE_DP)


        # Prepare the solution of the Lanczos algorithm
        self.eigvals  = None
        self.eigvects = None 

        # Store the basis and the coefficients of the Lanczos procedure
        # In the custom lanczos mode
        self.krilov_basis = [] # The basis of the krilov subspace
        self.arnoldi_matrix = [] # If requested, the upper triangular arnoldi matrix

        # These are some options that can be used to properly reverse and shift the L operator to
        # fasten the convergence of low energy modes
        self.reverse_L = False
        self.shift_value = 0

reset()

RESET THE LANCZOS

This function reset the Lanczos algorithm, allowing for a new responce function calculation with the same ensemble and the same settings.

Source code in tdscha/DynamicalLanczos.py
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
def reset(self):
    """
    RESET THE LANCZOS
    =================

    This function reset the Lanczos algorithm, allowing for a new responce function calculation
    with the same ensemble and the same settings.
    """

    # Prepare the variable used for the working
    len_psi = self.n_modes
    len_psi += self.n_modes * (self.n_modes + 1)
    self.psi = np.zeros(len_psi, dtype = TYPE_DP)


    # Prepare the solution of the Lanczos algorithm
    self.eigvals = None
    self.eigvects = None 

    # Store the basis and the coefficients of the Lanczos procedure
    # In the custom lanczos mode
    self.a_coeffs = [] # Coefficients on the diagonal
    self.b_coeffs = [] # Coefficients close to the diagonal
    self.c_coeffs = []

    # The krilov basis for the symmetric and unsymmetric Lanczos
    self.basis_P = []
    self.basis_Q = []
    self.s_norm = []
    self.krilov_basis = [] # The basis of the krilov subspace
    self.arnoldi_matrix = [] # If requested, the upper triangular arnoldi matrix

init(use_symmetries=True)

INITIALIZE THE CALCULATION

Perform everithing needed to initialize the calculation.

Source code in tdscha/DynamicalLanczos.py
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
def init(self, use_symmetries = True):
    """
    INITIALIZE THE CALCULATION
    ==========================

    Perform everithing needed to initialize the calculation.

    Parameters
    ----------
        use_symmetries : bool
            if False (default True) symmetries are neglected (unless the ensemble has been unwrapped).

    """
    # Prepare the variable used for the working
    len_psi  = self.n_modes
    len_psi += self.n_modes * (self.n_modes + 1)
    self.psi = np.zeros(len_psi, dtype = TYPE_DP)

    self.prepare_symmetrization(no_sym = not use_symmetries)
    self.initialized = True

interpolate(q_mesh, support_dyn=None, auto_init=True)

INTERPOLATION

This subroutine prepare the Lanczos algorithm to run on a bigger mesh than the one defined on the original supercell. This is fundamental to correctly converge resonances.

This method automatically initializes with symmetries the new Lanczos. You can disable this behaviour setting auto_init = False.

Results
interpolated_lanczos : Lanczos()
    A new Lanczos class object, with interpolated data.
Source code in tdscha/DynamicalLanczos.py
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
def interpolate(self, q_mesh, support_dyn = None, auto_init = True):
    """
    INTERPOLATION
    =============

    This subroutine prepare the Lanczos algorithm to run on a bigger mesh than the one defined on the original supercell.
    This is fundamental to correctly converge resonances.

    This method automatically initializes with symmetries the new Lanczos. 
    You can disable this behaviour setting auto_init = False.

    Parameters
    ----------
        q_mesh : list or ndarray (size=3, dtye = np.intc)
            The mesh of q points on which you want to perform the simulation.
            It should be bigger than the original supercell size.
        support_dyn : CC.Phonons.Phonons, optional
            By default, the original dynamical matrix will be Fourier interpolated in the new q_mesh.
            However, you can provide a new dynamical matrix in the q_mesh already interpolated.
            This is usefull if you want to use a custom interpolation
            (for example to interpolate only the differences between the SSCHA force constat matrix and the harmonic one).
        auto_init: bool
            If True, after the interpolation is performed, the new Lanczos object will be initialized (with symmetries).
            If you disable it, you must call the init function manually

    Results
    -------
        interpolated_lanczos : Lanczos()
            A new Lanczos class object, with interpolated data.

    """
    interpolated_lanczos = Lanczos()

    # Interpolate the original dynamical matrix
    if support_dyn is not None:
        interpolated_lanczos.dyn = support_dyn.Copy()
    else:
        interpolated_lanczos.dyn = self.dyn.Interpolate(q_mesh)

    # Prepare the new structure
    interpolated_lanczos.uci_structure = interpolated_lanczos.dyn.structure.copy()
    interpolated_lanczos.super_structure = interpolated_lanczos.uci_structure.generate_supercell(q_mesh)

    interpolated_lanczos.T = self.T 

    ws, pols = interpolated_lanczos.dyn.DiagonalizeSupercell()


    interpolated_lanczos.nat = interpolated_lanczos.super_structure.N_atoms
    n_cell = np.prod(q_mesh)

    interpolated_lanczos.qe_sym = CC.symmetries.QE_Symmetry(interpolated_lanczos.uci_structure)
    interpolated_lanczos.qe_sym.SetupQPoint()

    # Get the masses
    m = interpolated_lanczos.super_structure.get_masses_array()
    interpolated_lanczos.m = np.tile(m, (3,1)).T.ravel()

    # Remove the translations
    trans_mask = CC.Methods.get_translations(pols, m)

    # Isolate only translational modes
    good_mask = ~trans_mask

    # Get the polarization vectors
    interpolated_lanczos.w = ws[good_mask]
    interpolated_lanczos.pols = pols[:, good_mask]

    # Correctly reshape the polarization in case only one mode is selected
    if len(self.w) == 1:
        self.pols = self.pols.reshape((len(self.m), 1))

    interpolated_lanczos.n_modes = len(interpolated_lanczos.w)


    # Prepare the list of q point starting from the polarization vectors
    #q_list = CC.symmetries.GetQForEachMode(self.pols, self.uci_structure, self.super_structure, self.dyn.GetSupercell())
    # Store the q vectors in crystal space
    bg = interpolated_lanczos.uci_structure.get_reciprocal_vectors() / 2* np.pi
    interpolated_lanczos.q_vectors = np.zeros((interpolated_lanczos.n_modes, 3), dtype = np.double, order = "C")
    #for iq, q in enumerate(q_list):
    #    self.q_vectors[iq, :] = CC.Methods.covariant_coordinate(bg, q)


    # Prepare the interpolation of the ensemble
    interpolated_lanczos.rho = self.rho.copy()
    interpolated_lanczos.N_eff = np.sum(self.rho)

    interpolated_lanczos.u_tilde = self.u_tilde.copy()
    interpolated_lanczos.f_tilde = self.f_tilde.copy()

    interpolated_lanczos.X = np.zeros((interpolated_lanczos.N, interpolated_lanczos.n_modes), order = order, dtype = TYPE_DP)
    interpolated_lanczos.Y = np.zeros((interpolated_lanczos.N, interpolated_lanczos.n_modes), order = order, dtype = TYPE_DP)

    # TODO: Interpolation of the q vectors with the Tetrahedral method.
    raise NotImplementedError("Error, interpolation is not yet implemented.")
    interpolated_lanczos.X[:, :] = self.u_tilde.dot(self.pols) #.T.dot(self.u_tilde)
    interpolated_lanczos.Y[:, :] = self.f_tilde.dot(self.pols) #self.pols.T.dot(self.f_tilde)




    if auto_init:
        interpolated_lanczos.init()
    return interpolated_lanczos

prepare_symmetrization(no_sym=False, verbose=True, symmetries=None)

PREPARE THE SYMMETRIZATION

This function analyzes the character of the symmetry operations for each polarization vectors. This will allow the method do know how many modes are degenerate.

If the ensemble has been unwrapped, then the symmetries are not initialized.

Source code in tdscha/DynamicalLanczos.py
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
def prepare_symmetrization(self, no_sym = False, verbose = True, symmetries = None):
    """
    PREPARE THE SYMMETRIZATION
    ==========================

    This function analyzes the character of the symmetry operations for each polarization vectors.
    This will allow the method do know how many modes are degenerate.

    If the ensemble has been unwrapped, then the symmetries are not initialized.

    Parameters
    ----------
        no_sym : bool
            If True, the symmetries are neglected.
        symmetries : list of 3x4 matrices
            If None, spglib is employed to find the symmetries,
                     otherwise, the symmetries here contained are employed.
    """

    self.initialized = True

    # All the rest is deprecated in the Fast Lanczos implementation
    # As the symmetrization is performed by unwrapping the ensemble

    # Generate the dynamical matrix in the supercell
    super_structure = self.dyn.structure.generate_supercell(self.dyn.GetSupercell())
    w, pols = self.dyn.DiagonalizeSupercell()

    # Get the symmetries of the super structure
    if not __SPGLIB__ and not no_sym:
        raise ImportError("Error, spglib module required to perform symmetrization in a supercell. Otherwise, use no_sym")

    # Neglect the symmetries
    if no_sym or self.unwrapped:
        self.symmetries = [np.ones( (1,1,1), dtype = np.double)] * self.n_modes
        self.N_degeneracy = np.ones(self.n_modes, dtype = np.intc)
        self.degenerate_space = [np.array([i], dtype = np.intc) for i in range(self.n_modes)]
        self.sym_block_id = np.arange(self.n_modes).astype(np.intc)
        return

    t1 = time.time()
    if symmetries is None:
        super_symmetries = CC.symmetries.GetSymmetriesFromSPGLIB(spglib.get_symmetry(super_structure.get_spglib_cell()), False)

    else:
        super_symmetries = symmetries
    t2 = time.time()


    if verbose:
        print("Time to get the symmetries [{}] from spglib: {} s".format(len(super_symmetries), t2-t1))

    # Gamma-only optimization: separate point-group and translational symmetries
    if self.gamma_only:
        n_total_syms = len(super_symmetries)
        identity = np.eye(3)
        translations = []
        unique_rotations = {}
        for sym in super_symmetries:
            rot = sym[:, :3]
            rot_key = np.round(rot, 8).tobytes()
            if np.allclose(rot, identity, atol=1e-8):
                translations.append(sym)
            if rot_key not in unique_rotations:
                unique_rotations[rot_key] = sym
        pg_symmetries = list(unique_rotations.values())

        if verbose:
            print("Gamma-only: {} PG syms instead of {} total (speedup: {}x)".format(
                len(pg_symmetries), n_total_syms, n_total_syms // max(len(pg_symmetries), 1)))

        # Build translation operators in mode space: T_R^mode = pols^T @ P_R @ pols
        self.trans_operators = []
        nat_sc = super_structure.N_atoms
        for t_sym in translations:
            irt = CC.symmetries.GetIRT(super_structure, t_sym)
            # Build Cartesian permutation matrix P_R (3*nat_sc x 3*nat_sc)
            P_R = np.zeros((3 * nat_sc, 3 * nat_sc), dtype=np.double)
            for i_at in range(nat_sc):
                j_at = irt[i_at]
                P_R[3*j_at:3*j_at+3, 3*i_at:3*i_at+3] = np.eye(3)
            # Project into mode space
            T_mode = self.pols.T @ P_R @ self.pols  # (n_modes, n_modes)
            self.trans_operators.append(T_mode)

        self.trans_projector = np.mean(self.trans_operators, axis=0)

        if verbose:
            print("Built {} translation operators in mode space".format(len(translations)))

        # Replace super_symmetries with PG-only for the rest of the function
        super_symmetries = pg_symmetries

    # Get the symmetry matrix in the polarization space
    # Translations are needed, as this method needs a complete basis.
    pol_symmetries, basis = CC.symmetries.GetSymmetriesOnModesDeg(super_symmetries, super_structure, self.pols, self.w)
    #pol_symmetries = CC.symmetries.GetSymmetriesOnModes(super_symmetries, super_structure, pols)
    t1 = time.time()
    if verbose:
        print("Time to convert symmetries in the polarizaion space: {} s".format(t1-t2))

    self.symmetries = pol_symmetries
    self.degenerate_space = [np.array(x, dtype = np.intc) for x in basis]
    self.N_degeneracy = np.array([len(x) for x in basis], dtype = np.intc)
    self.sym_block_id = -np.ones(self.n_modes, dtype = np.intc)
    self.n_syms =  self.symmetries[0].shape[0]

    if self.mode is MODE_FAST_JULIA:
        # Get the max length
        max_val = 0
        nblocks = len(self.symmetries)
        for s in self.symmetries:
            m = s.shape[1]
            if m > max_val:
                max_val = m
        self.sym_julia = np.zeros((nblocks, self.n_syms, max_val, max_val), dtype = TYPE_DP)
        self.deg_julia = np.zeros((nblocks, max_val), dtype = np.int32)

        # Now fill the array
        for i, sblock in enumerate(self.symmetries):
            nsym, c, _ = np.shape(sblock)
            self.sym_julia[i, :, :c, :c] = sblock
            self.deg_julia[i, :c] = self.degenerate_space[i]

        # Pre-build and cache sparse symmetry matrices in Julia
        julia.Main.init_sparse_symmetries(
            self.sym_julia, self.N_degeneracy, self.deg_julia, self.sym_block_id)

    # Create the mapping between the modes and the block id.
    t1 = time.time()
    for i in range(self.n_modes):
        for j, block in enumerate(self.degenerate_space):
            if i in block:
                self.sym_block_id[i] = j 
                break

        assert self.sym_block_id[i] >= 0, "Error, something went wrong during the symmetrization"
    t2 = time.time()

    if verbose:
        print("Time to create the block_id array: {} s".format(t2-t1))

prepare_input_files(root_name='tdscha', n_steps=100, start_from_scratch=True, directory='.', run_symm=False)

PREPARE INPUT FILES

This method prepares the input files for the submission with the binary executable. This is usefull to prepare the input in a local computer and submit the calculation on a cluster, where it is easier to work.

Parameters:

Name Type Description Default
This
required
XXX
required
XXX
required
XXX
required
XXX
required
XXX
required
XXX
required
and
required
XXX
required
XXX
required
The
required
as
required
All
required
Source code in tdscha/DynamicalLanczos.py
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
def prepare_input_files(self, root_name = "tdscha", n_steps = 100, start_from_scratch = True, directory=".", run_symm = False):
    """
    PREPARE INPUT FILES
    ===================

    This method prepares the input files for the submission with the binary executable.
    This is usefull to prepare the input in a local computer and submit the calculation on a cluster,
    where it is easier to work.

    Parameters
    ----------
        root_name : string
            The title of the calculation.
        n_steps: int
            The number of steps to be performed in the lanczos calculation.
        start_from_scratch: bool
            If True the calculation is restarted from scratch.
        directory : string
            Path to the directory on which the input files will be saved
        run_symm : bool
            True if we use the Wigner representation


    This file will prepare inside the directory the following input files.
    (where XXX = root_name)

    XXX.json
    XXX.X.dat
    XXX.Y.dat
    XXX.syms.$i   ($i = 0, ..., N symmetries - 1)
    XXX.degs
    XXX.psi

    and the following optional files (in case the calculation should be restarted):

    XXX.Qbasis
    XXX.Pbasis


    The file XXX.json contains the generic information about the minimization,
    as well as all arrays not too big and that can be easily stored in a json file.

    All the other data contain 2d arrays or more sophisticated data.
    """

    Nsyms, _,_ = np.shape(self.symmetries[0])

    json_data = {"T" : self.T, 
                 "n_steps" : n_steps,
                 "ignore_v2" : self.ignore_harmonic,
                 "ignore_v3" : self.ignore_v3,
                 "ignore_v4" : self.ignore_v4,
                 "use_wigner" : self.use_wigner,
                 "run_sym": run_symm,
                 "data" : {
                     "n_configs" : int(self.N),
                     "n_modes" : int(self.n_modes),
                     "n_syms" : Nsyms,
                     "n_blocks" : len(self.symmetries),
                     "perturbation_modulus" : self.perturbation_modulus,
                     "reverse" : self.reverse_L,
                     "shift" : self.shift_value} }

    Parallel.barrier()

    if not start_from_scratch:
        raise NotImplementedError("Error, restarting from a previous calculation is not yet implemented.")

    if Parallel.am_i_the_master():
        root_fname = os.path.join(directory, root_name)

        if not os.path.exists(directory):
            os.makedirs(directory)

        # Writhe the json input file
        with open(root_fname + ".json", "w") as fp:
            json.dump(json_data, fp)

        # Save 1D arrays
        np.savetxt(root_fname + ".ndegs", self.N_degeneracy, fmt = "%d")
        np.savetxt(root_fname + ".blockid", self.sym_block_id, fmt = "%d")
        np.savetxt(root_fname + ".masses", self.m)
        np.savetxt(root_fname + ".freqs", self.w)
        np.savetxt(root_fname + ".rho", self.rho)

        # Save all the other data
        np.savetxt(root_fname + ".X.dat", self.X)
        np.savetxt(root_fname + ".Y.dat", self.Y)


        np.savetxt(root_fname + ".psi", self.psi)

        # Prepare the symmetry variables for the C code
        for i in range(len(self.symmetries)):
            np.savetxt(root_fname + ".block{:d}".format(i), self.degenerate_space[i], fmt = "%d")


            ns, b1, b2 = self.symmetries[i].shape
            with open(root_fname + ".symsb{:d}".format(i), "w") as fp:
                for isym in range(ns):
                    for k1 in range(b1):
                        for k2 in range(b2):
                            fp.write(" {:22.16f}".format(self.symmetries[i][isym, k1, k2]))
                        fp.write("\n")
                    fp.write("\n")

load_from_input_files(root_name='tdscha', directory='.')

Load the results from a calculation performed by the binary executable.

NOTE: You must initialize the ensemble as did before calling the prepare_input_files method. Then execute the lanczos run with the tdscha-lanczos.x executable. Then load the results of the lanczos with this method.

You must use the same keyword used in the prepare_input_files

Source code in tdscha/DynamicalLanczos.py
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
    def load_from_input_files(self, root_name = "tdscha", directory="."):
        """
        Load the results from a calculation performed by the binary executable.

        NOTE: You must initialize the ensemble as did before calling the prepare_input_files method.
        Then execute the lanczos run with the tdscha-lanczos.x executable.
        Then load the results of the lanczos with this method.

        You must use the same keyword used in the prepare_input_files
        """

        if not os.path.exists(directory):
            raise IOError("Error, the specified directory '{}' does not exist".format(directory))

        abc_file = os.path.join(directory, "{}.abc".format(root_name))
        if not os.path.exists(abc_file):
            errmsg = """
Error, the file '{}' does not exist. 
       please, check if you correctly run the tdscha-lanczos.x executable.
""".format(abc_file)
            print(errmsg)
            raise IOError(errmsg)


        data_abc = np.loadtxt(abc_file, dtype= np.double)
        self.a_coeffs = data_abc[:,0]
        self.b_coeffs = data_abc[:, 1]
        self.c_coeffs = data_abc[:, 2]


        # Load the Pbasis and Qbasis only if the files exists.
        # These are very heavy files, and they are not needed for the spectral function
        # in the continued fraction representation.
        # (they are needed to restart a calculation)

        qbasis_file = os.path.join(directory, "{}.qbasis.out".format(root_name))
        pbasis_file = os.path.join(directory, "{}.pbasis.out".format(root_name))
        snorm_file = os.path.join(directory, "{}.snorm.out".format(root_name))

        if os.path.exists(qbasis_file):
            self.basis_Q = np.loadtxt(qbasis_file, dtype = np.double)
        else:
            warnmsg = """
File {} not found. basis_Q not loaded.
""".format(qbasis_file)
            warnings.warn(warnmsg)

        if os.path.exists(pbasis_file):
            self.basis_P = np.loadtxt(pbasis_file, dtype = np.double)
        else:
            warnmsg = """
File {} not found. basis_P not loaded.
""".format(pbasis_file)
            warnings.warn(warnmsg)

        if os.path.exists(snorm_file):
            self.s_norm = np.loadtxt(snorm_file, dtype = np.double).ravel()
        else:
            warnmsg = """
File {} not found. S norm not loaded.
""".format(snorm_file)
            warnings.warn(warnmsg)

        # Load the Json file with other general variables
        with open(os.path.join(directory, root_name + ".json"), "r") as fp:
            json_data = json.load(fp)

        self.perturbation_modulus = json_data["data"]["perturbation_modulus"]
        self.T = json_data["T"]
        self.ignore_harmonic = json_data["ignore_v2"]
        self.ignore_v3 = json_data["ignore_v3"]
        self.ignore_v4 = json_data["ignore_v4"]
        self.N = json_data["data"]["n_configs"]
        self.n_modes = json_data["data"]["n_modes"]
        self.reverse_L = json_data["data"]["reverse"]
        self.shift_value = json_data["data"]["shift"]

prepare_raman(pol_vec_in=np.array([1, 0, 0]), pol_vec_out=np.array([1, 0, 0]), mixed=False, pol_in_2=None, pol_out_2=None, unpolarized=None)

PREPARE LANCZOS FOR RAMAN SPECTRUM

This subroutines prepare the perturbation for the Raman signal.

The raman tensor is read from the dynamical matrix provided by the original ensemble.

Source code in tdscha/DynamicalLanczos.py
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
def prepare_raman(self, pol_vec_in = np.array([1,0,0]), pol_vec_out = np.array([1,0,0]), mixed = False, pol_in_2 = None, pol_out_2 = None, unpolarized: int = None):
    """
    PREPARE LANCZOS FOR RAMAN SPECTRUM
    ==================================

    This subroutines prepare the perturbation for the Raman signal.

    The raman tensor is read from the dynamical matrix provided by the original ensemble.

    Parameters
    ----------
        pol_vec_in : ndarray (size =3)
            The polarization vector of the incoming light
        pol_vec_out : ndarray (size = 3)
            The polarization vector for the outcoming light
        unpolarized : int or None
            The perturbation for unpolarized raman (if different from None, overrides the behaviour
            of pol_vec_in and pol_vec_out). Indices goes from 0 to 6 (included).
            0 is alpha^2
            1 + 2 + 3 + 4 + 5 + 6 are beta^2
            alpha_0 = (xx + yy + zz)^2/9
            beta_1 = (xx -yy)^2 / 2
            beta_2 = (xx -zz)^2 / 2
            beta_3 = (yy -zz)^2 / 2
            beta_4 = 3xy^2
            beta_5 = 3xz^2
            beta_6 = 3yz^2

            The total unpolarized raman intensity is 45 alpha^2 + 7 beta^2
    """

    # Check if the raman tensor is present
    assert not self.dyn.raman_tensor is None, "Error, no Raman tensor found. Cannot initialize the Raman responce"

    # Get the raman vector (apply the ASR and contract the raman tensor with the polarization vectors)
    raman_v = self.dyn.GetRamanVector(pol_vec_in, pol_vec_out)

    if mixed:
        print('Prepare Raman')
        print('Adding other component of the Raman tensor')
        raman_v += self.dyn.GetRamanVector(pol_in_2, pol_out_2)

    # Get the raman vector in the supercelld
    n_supercell = np.prod(self.dyn.GetSupercell())

    if unpolarized is None:
        # Get the raman vector
        raman_v = self.dyn.GetRamanVector(pol_vec_in, pol_vec_out)

        # Get the raman vector in the supercelld
        new_raman_v = np.tile(raman_v.ravel(), n_supercell)

        # Convert in the polarization basis and store the intensity
        self.prepare_perturbation(new_raman_v, masses_exp=-1)
    else:
        px = np.array([1,0,0])
        py = np.array([0,1,0])
        pz = np.array([0,0,1])

        if unpolarized == 0:
            # Alpha
            raman_v = self.dyn.GetRamanVector(px, px)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / 3
            self.prepare_perturbation(new_raman_v, masses_exp=-1)

            raman_v = self.dyn.GetRamanVector(py, py)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / 3
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)

            raman_v = self.dyn.GetRamanVector(pz, pz)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / 3
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)
        elif unpolarized == 1:
            # (xx -yy)^2 / 2
            raman_v = self.dyn.GetRamanVector(px, px)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)

            raman_v = self.dyn.GetRamanVector(py, py)
            new_raman_v = - np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)
        elif unpolarized == 2:
            # beta_2 = (xx -zz)^2 / 2
            raman_v = self.dyn.GetRamanVector(px, px)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)

            raman_v = self.dyn.GetRamanVector(pz, pz)
            new_raman_v = - np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)
        elif unpolarized == 3:
            # beta_2 = (yy -zz)^2 / 2
            raman_v = self.dyn.GetRamanVector(py, py)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)

            raman_v = self.dyn.GetRamanVector(pz, pz)
            new_raman_v = - np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)
        elif unpolarized == 4:
            # beta_2 = 3 xy^2
            raman_v = self.dyn.GetRamanVector(px, py)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) * np.sqrt(3)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)
        elif unpolarized == 5:
            # beta_2 = 3 yz^2
            raman_v = self.dyn.GetRamanVector(py, pz)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) * np.sqrt(3)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)
        elif unpolarized == 6:
            # beta_2 = 3 xz^2
            raman_v = self.dyn.GetRamanVector(px, pz)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) * np.sqrt(3)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)
        else:
            raise ValueError("Error, unpolarized must be between [0, ... ,6] got invalid {}.".format(unpolarized))




    # Convert in the polarization basis and store the intensity
    self.prepare_perturbation(new_raman_v, masses_exp=-1)

get_prefactors_unpolarized_raman(index)

RETURNS THE PREFACTORS FOR COMPUTING THE UNPOLARIZED RAMAN

It returns a dictionary with the prefactors

The prefactors corresponds to the components of the unpolarized raman signal

Source code in tdscha/DynamicalLanczos.py
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
def get_prefactors_unpolarized_raman(self, index):
    """
    RETURNS THE PREFACTORS FOR COMPUTING THE UNPOLARIZED RAMAN
    ==========================================================

    It returns a dictionary with the prefactors

    The prefactors corresponds to the components of the unpolarized raman signal
    """
    labels = [i for i in range(7)]
    if not(index in labels):
        raise ValueError('{} should be in {}'.format(index, labels))

    dictionary = {'(xx+yy+zz)^2' : 45/9,\
                  '(xx-yy)^2'    : 7/2,\
                  '(xx-zz)^2'    : 7/2,\
                  '(yy-zz)^2'    : 7/2,\
                  '(xy)^2'       : 7*3,\
                  '(xz)^2'       : 7*3,\
                  '(yz)^2'       : 7*3}

    keys = list(dictionary.keys())


    return dictionary[keys[index]]

prepare_unpolarized_raman(index=0, debug=False)

PREPARE UNPOLARIZED RAMAN SIGNAL

The raman tensor is read from the dynamical matrix provided by the original ensemble.

The perturbations are prepared accordin to the formula (see https://doi.org/10.1021/jp5125266)

..math:

I_unpol = 45/9 (xx + yy + zz)^2
          + 7/2 [(xx-yy)^2 + (xx-zz)^2 + (yy-zz)^2]
          + 7 * 3 [(xy)^2 + (yz)^2 + (xz)^2]
Source code in tdscha/DynamicalLanczos.py
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
def prepare_unpolarized_raman(self, index = 0, debug = False):
    """
    PREPARE UNPOLARIZED RAMAN SIGNAL
    ================================

    The raman tensor is read from the dynamical matrix provided by the original ensemble.

    The perturbations are prepared accordin to the formula (see https://doi.org/10.1021/jp5125266)

    ..math:

        I_unpol = 45/9 (xx + yy + zz)^2
                  + 7/2 [(xx-yy)^2 + (xx-zz)^2 + (yy-zz)^2]
                  + 7 * 3 [(xy)^2 + (yz)^2 + (xz)^2]
    """
    # Check if the raman tensor is present
    assert not self.dyn.raman_tensor is None, "Error, no Raman tensor found. Cannot initialize the Raman responce"

    labels = [i for i in range(7)]
    if not(index in labels):
        raise ValueError('{} should be in {}'.format(index, labels))

    epols = {'x' : np.array([1,0,0]),\
             'y' : np.array([0,1,0]),\
             'z' : np.array([0,0,1])}

    # (xx + yy + zz)^2
    if index == 0:
        raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        raman_v += self.dyn.GetRamanVector(epols['y'], epols['y'])
        raman_v += self.dyn.GetRamanVector(epols['z'], epols['z'])
    # (xx - yy)^2    
    elif index == 1:
        raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        raman_v -= self.dyn.GetRamanVector(epols['y'], epols['y'])
    # (xx - zz)^2       
    elif index == 2:
        raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        raman_v -= self.dyn.GetRamanVector(epols['z'], epols['z'])
    # (yy - zz)^2   
    elif index == 3:
        raman_v  = self.dyn.GetRamanVector(epols['y'], epols['y'])
        raman_v -= self.dyn.GetRamanVector(epols['z'], epols['z'])
    # (xy)^2
    elif index == 4:
        raman_v = self.dyn.GetRamanVector(epols['x'], epols['y'])
    # (xz)^2
    elif index == 5:
        raman_v = self.dyn.GetRamanVector(epols['x'], epols['z'])
    # (yz)^2
    elif index == 6:
        raman_v = self.dyn.GetRamanVector(epols['y'], epols['z'])

    if debug:
        np.save('raman_v_{}'.format(index), raman_v)

    # Get the raman vector in the supercelld
    n_supercell = np.prod(self.dyn.GetSupercell())
    new_raman_v = np.tile(raman_v.ravel(), n_supercell)

    # Convert in the polarization basis and store the intensity
    self.prepare_perturbation(new_raman_v, masses_exp=-1)

    if debug:
        print('[NEW] Pertubation modulus with eq Raman tensors = {}'.format(self.perturbation_modulus))
    print()

    return

prepare_unpolarized_raman_FT(index=0, debug=False, eq_raman_tns=None, use_symm=True, ens_av_raman=None, raman_tns_ens=None, add_2ph=True)

PREPARE UNPOLARIZED RAMAN SIGNAL CONSIDERING FLUCTUATIONS OF THE RAMAN TENSOR

The raman tensor is read from the dynamical matrix provided by the original ensemble.

The perturbations are prepared accordin to the formula (see https://doi.org/10.1021/jp5125266)

..math:

I_unpol = 45/9 (xx + yy + zz)^2
          + 7/2 [(xx-yy)^2 + (xx-zz)^2 + (yy-zz)^2]
          + 7 * 3 [(xy)^2 + (yz)^2 + (xz)^2]
Parameters:
-index: the pol component of the unpolarized signal
-debug: if true we save the second order Raman tensor
-eq_raman_tns: np.array with shape (3, 3, 3 * N_at_uc), the equilibirum raman tensor
-use_symm: bool, if True symmetries are enforced
-ens_av_raman:  the ensemble on which we compute the averages of the Raman tensors
-raman_tns_ens: np.array with shape (N_conf, 3, 3, 3 * N_at_sc), the raman tensors on the displaced configruations
Source code in tdscha/DynamicalLanczos.py
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
def prepare_unpolarized_raman_FT(self, index = 0, debug = False, eq_raman_tns = None, use_symm = True,\
                                 ens_av_raman = None, raman_tns_ens = None, add_2ph = True):
    """
    PREPARE UNPOLARIZED RAMAN SIGNAL CONSIDERING FLUCTUATIONS OF THE RAMAN TENSOR
    =============================================================================

    The raman tensor is read from the dynamical matrix provided by the original ensemble.

    The perturbations are prepared accordin to the formula (see https://doi.org/10.1021/jp5125266)

    ..math:

        I_unpol = 45/9 (xx + yy + zz)^2
                  + 7/2 [(xx-yy)^2 + (xx-zz)^2 + (yy-zz)^2]
                  + 7 * 3 [(xy)^2 + (yz)^2 + (xz)^2]

    Parameters:
    -----------
        -index: the pol component of the unpolarized signal
        -debug: if true we save the second order Raman tensor
        -eq_raman_tns: np.array with shape (3, 3, 3 * N_at_uc), the equilibirum raman tensor
        -use_symm: bool, if True symmetries are enforced
        -ens_av_raman:  the ensemble on which we compute the averages of the Raman tensors
        -raman_tns_ens: np.array with shape (N_conf, 3, 3, 3 * N_at_sc), the raman tensors on the displaced configruations
    """
    # Check if the raman tensor is present
    assert not self.dyn.raman_tensor is None, "Error, no Raman tensor found. Cannot initialize the Raman responce"

    labels = [i for i in range(7)]
    if not(index in labels):
        raise ValueError('{} should be in {}'.format(index, labels))

    epols = {'x' : np.array([1,0,0]),\
             'y' : np.array([0,1,0]),\
             'z' : np.array([0,0,1])}

    # (xx + yy + zz)^2
    if index == 0:
        # raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        # raman_v += self.dyn.GetRamanVector(epols['y'], epols['y'])
        # raman_v += self.dyn.GetRamanVector(epols['z'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   = epols['x'], pol_out   = epols['x'],\
                                         mixed = True,\
                                         pol_in_2 = epols['y'], pol_out_2 = epols['y'],\
                                         pol_in_3 = epols['z'], pol_out_3 = epols['z'],\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xx_plus_yy_plus_zz')
    # (xx - yy)^2    
    elif index == 1:
        # raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        # raman_v -= self.dyn.GetRamanVector(epols['y'], epols['y'])
        # NB we put just one minus sign because the component is (xx - yy)^2
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['x'],  pol_out  =  epols['x'],\
                                         mixed = True,\
                                         pol_in_2 = -epols['y'], pol_out_2 =  epols['y'],\
                                         pol_in_3 = np.zeros(3), pol_out_3 = np.zeros(3),\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xx_minus_yy')
    # (xx - zz)^2       
    elif index == 2:
        # raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        # raman_v -= self.dyn.GetRamanVector(epols['z'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['x'],  pol_out  =  epols['x'],\
                                         mixed = True,\
                                         pol_in_2 = -epols['z'], pol_out_2 =  epols['z'],\
                                         pol_in_3 = np.zeros(3), pol_out_3 = np.zeros(3),\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xx_minus_zz')
    # (yy - zz)^2   
    elif index == 3:
        # raman_v  = self.dyn.GetRamanVector(epols['y'], epols['y'])
        # raman_v -= self.dyn.GetRamanVector(epols['z'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['y'],  pol_out  =  epols['y'],\
                                         mixed = True,\
                                         pol_in_2 = -epols['z'], pol_out_2 =  epols['z'],\
                                         pol_in_3 = np.zeros(3), pol_out_3 = np.zeros(3),\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'yy_minus_zz')
    # (xy)^2
    elif index == 4:
        # raman_v = self.dyn.GetRamanVector(epols['x'], epols['y'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['x'],  pol_out  =  epols['y'],\
                                         mixed = False,\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xy_square')
    # (xz)^2
    elif index == 5:
        # raman_v = self.dyn.GetRamanVector(epols['x'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['x'],  pol_out  =  epols['z'],\
                                         mixed = False,\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xz_square')
    # (yz)^2
    elif index == 6:
        # raman_v = self.dyn.GetRamanVector(epols['y'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['y'],  pol_out  =  epols['z'],\
                                         mixed = False,\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'yz_square')

    return

prepare_anharmonic_raman_FT(raman=None, raman_eq=None, pol_in=np.array([1.0, 0.0, 0.0]), pol_out=np.array([1.0, 0.0, 0.0]), mixed=False, pol_in_2=None, pol_out_2=None, pol_in_3=None, pol_out_3=None, add_two_ph=False, symmetrize=False, ensemble=None, save_raman_tensor2=False, file_raman_tensor2=None)

PREPARE THE PSI VECTOR FOR ANHARMONIC RAMAN SPECTRUM CALCULATION (NEW VERSION)

This works only with the Wigner representation if we add the two phonons effect. Prepare the psi vector for RAMAN spectrum considering position-dependent raman tensors.

Parameters:
-raman: nd.array (N_configs, E_comp, E_comp, 3 * N_at_sc),
     the Raman tensor for all configurations.
     Indices are: Number of configuration, electric field component,
     electric field component, atomic coordinates in sc.
rama_eq: nd.array, (E_comp, E_comp, 3 * N_at_uc), the effective charges at equilibrium.
     Indices are: electric field component,
     electric field component, atomic coordinate in uc.   
-pol_in: nd.array, the polarization of in-out light. default is x
-pol_out: nd.array, the polarization of in-out light. default is x
-mixed: if True we can study the one and two phonon response to 
        pol_in \cdto \Xi \cdot pol_in + pol_in_2 \cdto \Xi \cdot pol_in_2 + pol_in_3 \cdto \Xi \cdot pol_in_3
        (\Xi is the Raman tensor)
-pol_in_2:  nd.array, the polarization of in-out light. default is None
-pol_out_2: nd.array, the polarization of in-out light. default is None
-pol_in_3:  nd.array, the polarization of in-out light. default is None
-pol_out_3: nd.array, the polarization of in-out light. default is None
-add_two_ph: bool, if True two phonon processes are included in the calculation
-symmetrize: bool, if True the first/second order Raman tensors are symmetrized
-ensemble: a scha ensemble object for computing the averages
-save_raman_tensor2: bool if True we save the second order Raman tensor
Source code in tdscha/DynamicalLanczos.py
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
def prepare_anharmonic_raman_FT(self, raman = None, raman_eq = None,\
                                pol_in = np.array([1.,0.,0.]), pol_out = np.array([1.,0.,0.]),\
                                mixed = False, pol_in_2 = None, pol_out_2 = None,\
                                pol_in_3 = None, pol_out_3 = None,\
                                add_two_ph = False, symmetrize = False, ensemble = None,\
                                save_raman_tensor2 = False, file_raman_tensor2 = None):
    """
    PREPARE THE PSI VECTOR FOR ANHARMONIC RAMAN SPECTRUM CALCULATION (NEW VERSION)
    ===========================================================================

    This works only with the Wigner representation if we add the two phonons effect. 
    Prepare the psi vector for RAMAN spectrum considering position-dependent raman tensors.

    Parameters:
    -----------
        -raman: nd.array (N_configs, E_comp, E_comp, 3 * N_at_sc),
             the Raman tensor for all configurations.
             Indices are: Number of configuration, electric field component,
             electric field component, atomic coordinates in sc.
        rama_eq: nd.array, (E_comp, E_comp, 3 * N_at_uc), the effective charges at equilibrium.
             Indices are: electric field component,
             electric field component, atomic coordinate in uc.   
        -pol_in: nd.array, the polarization of in-out light. default is x
        -pol_out: nd.array, the polarization of in-out light. default is x
        -mixed: if True we can study the one and two phonon response to 
                pol_in \cdto \Xi \cdot pol_in + pol_in_2 \cdto \Xi \cdot pol_in_2 + pol_in_3 \cdto \Xi \cdot pol_in_3
                (\Xi is the Raman tensor)
        -pol_in_2:  nd.array, the polarization of in-out light. default is None
        -pol_out_2: nd.array, the polarization of in-out light. default is None
        -pol_in_3:  nd.array, the polarization of in-out light. default is None
        -pol_out_3: nd.array, the polarization of in-out light. default is None
        -add_two_ph: bool, if True two phonon processes are included in the calculation
        -symmetrize: bool, if True the first/second order Raman tensors are symmetrized
        -ensemble: a scha ensemble object for computing the averages
        -save_raman_tensor2: bool if True we save the second order Raman tensor
    """
    if not self.use_wigner and add_two_ph:
        raise NotImplementedError('The two phonon processes are implemented only in Wigner')

    if raman is None:
        raise ValueError('Must specify the raman tensors for all configurations!')

    if mixed:
        #Check that we have the other polarization vectors
        if (pol_in_2 is None) or (pol_out_2 is None):
            raise ValueError('Must specify pol_in_2 pol_out_2 if mixed = True!')

        if (pol_in_3 is None) or (pol_out_3 is None):
            raise ValueError('Must specify pol_in_3 pol_out_3 if mixed = True!')

        if len(pol_in_2) != 3 or len(pol_out_2) != 3:
            raise ValueError('pol_in_2 pol_out_2 must be array of len 3')

        if len(pol_in_3) != 3 or len(pol_out_3) != 3:
            raise ValueError('pol_in_3 pol_out_3 must be array of len 3')


    print()
    print('PREPARE THE RAMAN ANHARMONIC SPECTRUM CALCULATION')
    print('=================================================')
    print('Are we considering two ph effects? = {}'.format(add_two_ph))
    print('Are we using Wigner? = {}'.format(self.use_wigner))
    print('Are we symmetrizing the raman tensor? = {}'.format(symmetrize))
    print()
    if ensemble is not None:
        Nconf = ensemble.N
    else:
        Nconf = self.N

    required = 'N_conf - E_field - E_field - 3 * N_at_sc'
    assert raman.shape[0] == Nconf, 'The raman tensor in input have the wrong shape. The required is {}'.format(required)
    assert raman.shape[1] == 3, 'The raman tensor in input have the wrong shape. The required is {}'.format(required)
    assert raman.shape[2] == 3, 'The raman tensor in input have the wrong shape. The required is {}'.format(required)
    assert raman.shape[3] == self.nat * 3, 'The raman tensor in input have the wrong shape. The required is {}'.format(required)

    # alpha is the polarizability

    # Get the average of the raman tensor, np.array with shape = (3, 3, 3 * N_at_sc)
    d1alpha_dR_av = perturbations.get_d1alpha_dR_av(ensemble, raman, symmetrize = symmetrize)

    # Get the supercell dyn then set the raman tensor euqal to d1alpha_dR_av
    sc_dyn = self.dyn.GenerateSupercellDyn(self.dyn.GetSupercell())
    sc_dyn.raman_tensor = d1alpha_dR_av

    # Get the Raman vector np.array (3 * N_at_sc)
    raman_vector_sc = sc_dyn.GetRamanVector(pol_in, pol_out)

    if mixed:
        print('ONE PH SECTOR adding compoent pol_in_2 pol_out_2 of the Raman tensor')
        raman_vector_sc += sc_dyn.GetRamanVector(pol_in_2, pol_out_2)
        print('ONE PH SECTOR adding compoent pol_in_3 pol_out_3 of the Raman tensor')
        raman_vector_sc += sc_dyn.GetRamanVector(pol_in_3, pol_out_3)


    # Now rescale by the mass and go in polarizaiton basis
    self.prepare_perturbation(raman_vector_sc, masses_exp = -1)
    print('[NEW] Pertubation modulus with one ph effects only = {}'.format(self.perturbation_modulus))
    print()

    # NOW PREPARE THE SECOND RAMAN TENSOR
    if add_two_ph:
        if raman_eq is not None:
            print('[NEW] Getting the equilibirum RAMAN tensor...')
            print()
            n_supercell = np.prod(self.dyn.GetSupercell())
            # raman_eq is np.array with shape = (E_field, E_field, N_at_uc * 3)
            raman_eq_size = np.shape(raman_eq)
            MSG = """
            Error, raman tns of the wrong shape: {}
            """.format(raman_eq_size)
            assert len(raman_eq_size) == 3, MSG
            if not self.ignore_small_w:
                assert raman_eq_size[2] * n_supercell == self.nat * 3 #self.n_modes + 3
            assert raman_eq_size[0] == raman_eq_size[1] == 3

            # Get the raman tensor in the supercell (E_field, E_filed, 3 * N_at_sc)
            raman_eq_gamma = np.zeros((3, 3, 3 * n_supercell * self.dyn.structure.N_atoms), dtype = type(raman_eq[0,0,0]))
            raman_eq_gamma = np.tile(raman_eq, n_supercell)

        print('[NEW] Getting the two phonon contribution in RAMAN...')

        # d2M_dR np.array with shape = (3 * N_atoms, 3 * N_atoms, Efield)
        if raman_eq is not None:
            print('[NEW] Subtracting the equilibirum RAMAN tensor...')
            # raman - raman_eq_gamma, np.array with shape = (N_configs, Efield, Efield, 3 * N_at_sc)
            # THE RESULT HAS shape = (Efield, Efield, 3 * N_at_sc, 3 * N_at_sc)
            d2alpha_dR = perturbations.get_d2alpha_dR_av(ensemble, raman - raman_eq_gamma, None, symmetrize = symmetrize)
        else:
            # THE RESULT HAS shape = (Efield, Efield, 3 * N_at_sc, 3 * N_at_sc)
            d2alpha_dR = perturbations.get_d2alpha_dR_av(ensemble, raman, None, symmetrize = symmetrize)

        print('[NEW] Divide by the masses')
        # Divide by the masses of the atoms in the supercell shape =  (Efield, Efield, 3 * N_at_sc, 3 * N_at_sc)
        d2alpha_dR = np.einsum('c, abcd, d -> abcd', np.sqrt(self.m)**-1, d2alpha_dR, np.sqrt(self.m)**-1)

        if save_raman_tensor2:
            print('[NEW] Saving the second-order SCHA Raman tensor')
            np.save('{}'.format(file_raman_tensor2), d2alpha_dR)
            return

        print('[NEW] Go in polarization basis')
        # Now go in polarization basis, np.array with shape = (E_field, E_field, n_modes, n_modes)
        # d2alpha_dR_muspace = np.einsum('cm, abcd, dn -> abmn', self.pols, d2alpha_dR, self.pols)
        # -> substitute
        tmp                = np.einsum('abcd, cm -> abmd', d2alpha_dR, self.pols)
        d2alpha_dR_muspace = np.einsum('abmd, dn -> abmn', tmp, self.pols)

        # Project along the direction of the filed, np.array with shape = (n_modes, n_modes)
        dXi_dR_muspace = np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in, pol_out)

        if mixed:
            print('TWO PH SECTOR adding component pol_in_2 pol_out_2 of the Raman tensor')
            dXi_dR_muspace += np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in_2, pol_out_2)
            print('TWO PH SECTOR adding component pol_in_3 pol_out_3 of the Raman tensor')
            dXi_dR_muspace += np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in_3, pol_out_3)

        # Symmetrize in mu space, np.array with shape = (n_modes, n_modes)
        dXi_dR_muspace = 0.5 * (dXi_dR_muspace + dXi_dR_muspace.T)

        # Get chi_minus and chi_plus tensors, np.array with shape = (n_modes, n_modes)
        chi_minus = self.get_chi_minus()
        chi_plus  = self.get_chi_plus()

        # Get the pertubations on a'^(1) b'^(1)
        pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), dXi_dR_muspace)
        pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , dXi_dR_muspace)

        # Check if everything is symmetric
        assert np.all(np.abs(dXi_dR_muspace - dXi_dR_muspace.T) < 1e-10), "Second derivative of the polarizability is not symmetric in pol basis"
        assert np.all(np.abs(pert_a - pert_a.T) < 1e-10), "a'(1) pertubation is not symmetric in pol basis"
        assert np.all(np.abs(pert_b - pert_b.T) < 1e-10), "b'(1) pertubation is not symmetric in pol basis"

        # Now get the perturbation for a'^(1)
        current = self.n_modes
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
            current = current + self.n_modes - i

        # Now get the pertrubation for b'^(1)
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
            current = current + self.n_modes - i

        # Add the mask dot taking into account symmetric elements
        mask_dot = self.mask_dot_wigner()
        # OVERWRITE the pertubation modulus considering the two phonon sector
        self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

        print('[NEW] Perturbation modulus after adding two ph contributions RAMAN = {}'.format(self.perturbation_modulus))
        print()

    return

prepare_anharmonic_raman_FT_2ph(d2alpha_dR=None, pol_in=np.array([1.0, 0.0, 0.0]), pol_out=np.array([1.0, 0.0, 0.0]), mixed=False, pol_in_2=None, pol_out_2=None)

PREPARE THE PSI VECTOR FOR RAMAN SPECTRUM CALCULATION (NEW VERSION) DIRECTLY FROM 2nd ORDER RAMAN TENSOR

This function is useful if we want to interpolate the 2nd Raman tensor on a bigger supercell.

This works only with the Wigner representation if we add the two phonons effect. Prepare the psi vector for RAMAN spectrum considering position-dependent raman tensors.

NOTE: we completely neglect the frist order Raman scattering!

Parameters:
-d2alpha_dR: nd.array (E_comp, E_comp, 3 * N_at_sc, 3 * N_at_sc),
     2nd order Raman tensor.
     Indices are: Number of configuration, electric field component,
     electric field component, atomic coordinates in sc.   
-pol_in: nd.array, the polarization of in-out light. default is x
-pol_out: nd.array, the polarization of in-out light. default is x
-mixed: if True we can study the one and two phonon response to 
        pol_in \cdot \Xi \cdot pol_out + pol_in_2 \cdot \Xi \cdot pol_out_2
        (\Xi is the Raman tensor)
-pol_in_2: nd.array, the polarization of in-out light. default is x
-pol_out_2: nd.array, the polarization of in-out light. default is x
Source code in tdscha/DynamicalLanczos.py
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
def prepare_anharmonic_raman_FT_2ph(self, d2alpha_dR = None, pol_in = np.array([1.,0.,0.]), pol_out = np.array([1.,0.,0.]),\
                                mixed = False, pol_in_2 = None, pol_out_2 = None):
    """
    PREPARE THE PSI VECTOR FOR RAMAN SPECTRUM CALCULATION (NEW VERSION) DIRECTLY FROM 2nd ORDER RAMAN TENSOR
    ========================================================================================================

    This function is useful if we want to interpolate the 2nd Raman tensor on a bigger supercell.

    This works only with the Wigner representation if we add the two phonons effect. 
    Prepare the psi vector for RAMAN spectrum considering position-dependent raman tensors.

    NOTE: we completely neglect the frist order Raman scattering!

    Parameters:
    -----------
        -d2alpha_dR: nd.array (E_comp, E_comp, 3 * N_at_sc, 3 * N_at_sc),
             2nd order Raman tensor.
             Indices are: Number of configuration, electric field component,
             electric field component, atomic coordinates in sc.   
        -pol_in: nd.array, the polarization of in-out light. default is x
        -pol_out: nd.array, the polarization of in-out light. default is x
        -mixed: if True we can study the one and two phonon response to 
                pol_in \cdot \Xi \cdot pol_out + pol_in_2 \cdot \Xi \cdot pol_out_2
                (\Xi is the Raman tensor)
        -pol_in_2: nd.array, the polarization of in-out light. default is x
        -pol_out_2: nd.array, the polarization of in-out light. default is x
    """
    if not self.use_wigner:
        raise NotImplementedError('The two phonon processes are implemented only in Wigner')

    if d2alpha_dR is None:
        raise ValueError('Must specify the 2nd order Raman tensor!')

    exp_shape = (3, 3, self.nat * 3, self.nat * 3)
    if d2alpha_dR.shape != exp_shape:
        raise ValueError('The shape of the 2nd order Raman tensor is not correct, expected {}'.format(exp_shape))

    if mixed:
        if (pol_in_2 is None) or (pol_out_2 is None):
            raise ValueError('Must specify pol_in_2 pol_out_2 if mixed = True!')

        if len(pol_in_2) != 3 or len(pol_out_2) != 3:
            raise ValueError('pol_in_2 pol_out_2 must be array of len 3')


    print()
    print('PREPARE THE RAMAN ANHARMONIC SPECTRUM CALCULATION FROM 2nd ORDER RAMAN TENSOR')
    print('=============================================================================')
    # print('Are we considering two ph effects? = {}'.format(add_two_ph))
    print('Are we using Wigner? = {}'.format(self.use_wigner))
    # print('Are we symmetrizing the raman tensor? = {}'.format(symmetrize))
    print()

    print('TWO PH Going in polarization basis')
    # Now go in polarization basis, np.array with shape = (E_field, E_field, n_modes, n_modes)
    # d2alpha_dR_muspace = np.einsum('cm, abcd, dn -> abmn', self.pols, d2alpha_dR, self.pols)
    # -> substitute
    tmp                = np.einsum('abcd, cm -> abmd', d2alpha_dR, self.pols)
    d2alpha_dR_muspace = np.einsum('abmd, dn -> abmn', tmp, self.pols)
    # print(d2alpha_dR_muspace.shape)

    print('TWO PH Selecting the polarizations')
    # Project along the direction of the filed, np.array with shape = (n_modes, n_modes)
    dXi_dR_muspace = np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in, pol_out)
    # print(dXi_dR_muspace.shape)

    if mixed:
        print('TWO PH SECTOR adding component pol_in_2 pol_out_2 of the Raman tensor')
        dXi_dR_muspace += np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in_2, pol_out_2)

    # Symmetrize in mu space, np.array with shape = (n_modes, n_modes)
    dXi_dR_muspace = 0.5 * (dXi_dR_muspace + dXi_dR_muspace.T)

    # Get chi_minus and chi_plus tensors, np.array with shape = (n_modes, n_modes)
    chi_minus = self.get_chi_minus()
    chi_plus  = self.get_chi_plus()

    # Get the pertubations on a'^(1) b'^(1)
    pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), dXi_dR_muspace)
    pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , dXi_dR_muspace)

    # Check if everything is symmetric
    assert np.all(np.abs(dXi_dR_muspace - dXi_dR_muspace.T) < 1e-10), "Second derivative of the polarizability is not symmetric in pol basis"
    assert np.all(np.abs(pert_a - pert_a.T) < 1e-10), "a'(1) pertubation is not symmetric in pol basis"
    assert np.all(np.abs(pert_b - pert_b.T) < 1e-10), "b'(1) pertubation is not symmetric in pol basis"

    print('[NEW] Perturbation modulus = {}'.format(self.perturbation_modulus))
    print()

    # Now get the perturbation for a'^(1)
    current = self.n_modes
    for i in range(self.n_modes):
        self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
        current = current + self.n_modes - i

    # Now get the pertrubation for b'^(1)
    for i in range(self.n_modes):
        self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
        current = current + self.n_modes - i

    # Add the mask dot taking into account symmetric elements
    mask_dot = self.mask_dot_wigner()
    # OVERWRITE the pertubation modulus considering the two phonon sector
    self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

    print('[NEW] Perturbation modulus adding two ph contributions RAMAN = {}'.format(self.perturbation_modulus))
    print()

    return

prepare_ir(effective_charges=None, pol_vec=np.array([1, 0, 0]))

PREPARE LANCZOS FOR INFRARED SPECTRUM COMPUTATION

In this subroutine we prepare the lanczos algorithm for the computation of the infrared spectrum signal.

Source code in tdscha/DynamicalLanczos.py
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
def prepare_ir(self, effective_charges = None, pol_vec = np.array([1,0,0])):
    """
    PREPARE LANCZOS FOR INFRARED SPECTRUM COMPUTATION
    =================================================

    In this subroutine we prepare the lanczos algorithm for the computation of the
    infrared spectrum signal.

    Parameters
    ----------
        effective_charges : ndarray(size = (n_atoms, 3, 3), dtype = np.double)
            The effective charges. Indices are: Number of atoms in the unit cell,
            electric field component, atomic coordinate. If None, the effective charges
            contained in the dynamical matrix will be considered.
        pol_vec : ndarray(size = 3)
            The polarization vector of the light.
    """

    ec = self.dyn.effective_charges
    if not effective_charges is None:
        ec = effective_charges

    n_supercell = np.prod(self.dyn.GetSupercell())

    # Check the effective charges
    assert not ec is None, "Error, no effective charge found. Cannot initialize IR responce"

    ec_size = np.shape(ec)
    MSG = """
    Error, effective charges of the wrong shape: {}
    Number of modes : {}
    Dimension of the supercell : {}
    """.format(ec_size, self.n_modes, n_supercell)
    assert len(ec_size) == 3, MSG
    if not self.ignore_small_w:
        assert ec_size[0] * ec_size[2] * n_supercell == self.n_modes + 3, MSG
    assert ec_size[1] == ec_size[2] == 3, MSG

    # shape = (N_at_uc, 3)
    z_eff = np.einsum("abc, b", ec, pol_vec)

    # Get the gamma effective charge
    new_zeff = np.tile(z_eff.ravel(), n_supercell)

    self.prepare_perturbation(new_zeff, masses_exp = -1)

prepare_anharmonic_ir_FT(ec=None, ec_eq=None, pol_vec_light=np.array([1.0, 0.0, 0.0]), add_two_ph=False, symmetrize=False, ensemble=None)

PREPARE THE PSI VECTOR FOR ANHARMONIC IR SPECTRUM CALCULATION (NEW VERSION)

This works only with the Wigner representation if we add the two phonons effect. Prepare the psi vector for IR spectrum considering position-dependent effective charges.

The one phonon scetor is symmetrized by default

Parameters:
-effective_charges: nd.array (N_configs, N_atoms_sc, E_comp, cart_comp),
     the effective charges for all configurations.
     Indices are: Number of configuration, number of atoms in the super cell,
     electric field component, atomic coordinate.
-effective_charges_eq: nd.array, (N_atoms_uc, E_comp, cart_comp), the effective charges at equilibrium.
     Indices are: number of atoms in the unit cell,
     electric field component, atomic coordinate.   
-pol_vec_light: nd.array, the polarization of in-out light. default is x
-add_two_ph: bool, if True two phonon processes are included in the calculation
-symmetrize: bool, if True the first/second order effective charges are symmetrized
-ensemble: a scha ensemble object for computing the averages
Source code in tdscha/DynamicalLanczos.py
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
def prepare_anharmonic_ir_FT(self, ec = None, ec_eq = None, pol_vec_light = np.array([1.,0.,0.]), add_two_ph = False, symmetrize = False, ensemble = None):
    """
    PREPARE THE PSI VECTOR FOR ANHARMONIC IR SPECTRUM CALCULATION (NEW VERSION)
    ===========================================================================

    This works only with the Wigner representation if we add the two phonons effect. 
    Prepare the psi vector for IR spectrum considering position-dependent effective charges.

    The one phonon scetor is symmetrized by default

    Parameters:
    -----------
        -effective_charges: nd.array (N_configs, N_atoms_sc, E_comp, cart_comp),
             the effective charges for all configurations.
             Indices are: Number of configuration, number of atoms in the super cell,
             electric field component, atomic coordinate.
        -effective_charges_eq: nd.array, (N_atoms_uc, E_comp, cart_comp), the effective charges at equilibrium.
             Indices are: number of atoms in the unit cell,
             electric field component, atomic coordinate.   
        -pol_vec_light: nd.array, the polarization of in-out light. default is x
        -add_two_ph: bool, if True two phonon processes are included in the calculation
        -symmetrize: bool, if True the first/second order effective charges are symmetrized
        -ensemble: a scha ensemble object for computing the averages
    """
    if not self.use_wigner and add_two_ph:
        raise NotImplementedError('The two phonon processes are implemented only in Wigner')

    if ec is None:
        raise ValueError('Must specify the effective charges for all configurations!')


    print()
    print('PREPARE THE IR ANHARMONIC SPECTRUM CALCULATION')
    print('==============================================')
    print('Are we considering two ph effects? = {}'.format(add_two_ph))
    print('Are we using Wigner? = {}'.format(self.use_wigner))
    print('Are we symmetrizing the effective charges? = {}'.format(symmetrize))
    print()

    required = 'N_conf N_at_sc E_field cart'
    assert ec.shape[0] == ensemble.N, 'The effective charges in input have the wrong shape. The required is {}'.format(required)
    assert ec.shape[1] == self.nat, 'The effective charges in input have the wrong shape. The required is {}'.format(required)
    assert ec.shape[2] == ec.shape[3] == 3, 'The effective charges in input have the wrong shape. The required is {}'.format(required)

    # Get the average of the dipole moment, np.array with shape = (3 * N_at_sc, 3)
    d1M_dR_av = perturbations.get_d1M_dR_av(ensemble, ec, symmetrize = symmetrize)

    # Project along the direction of light polarization, (3 * N_at_sc)
    Z = np.einsum("ab, b -> a", d1M_dR_av, pol_vec_light)

    # Now rescale by the mass and go in polarizaiton basis
    self.prepare_perturbation(Z.ravel(), masses_exp = -1)
    print('Pertubation modulus with one ph effects only = {}'.format(self.perturbation_modulus))
    print()

    # NOW PREPARE THE SECOND ORDER DIPOLE MOMENT
    if add_two_ph:
        if ec_eq is not None:
            print('[NEW] Getting the equilibirum effective charges...')
            print()
            n_supercell = np.prod(self.dyn.GetSupercell())
            # ec_eq is np.array with shape = (N_at_uc, E_field, cart)
            ec_eq_size = np.shape(ec_eq)
            MSG = """
            Error, effective charges of the wrong shape: {}
            """.format(ec_eq_size)
            assert len(ec_eq_size) == 3, MSG
            if not self.ignore_small_w:
                assert ec_eq_size[0] * ec_eq_size[2] * n_supercell == self.n_modes + 3
            assert ec_eq_size[1] == ec_eq_size[2] == 3

            # Get the eq effective charges in the supercell (N_at_sc, E_field, 3)
            ec_eq_gamma = np.zeros((n_supercell * self.dyn.structure.N_atoms, 3, 3), dtype = type(ec_eq[0]))
            ec_eq_gamma = np.tile(ec_eq, (n_supercell,1,1))

        print('[NEW] Getting the two phonon contribution...')

        # d2M_dR np.array with shape = (3 * N_atoms, 3 * N_atoms, Efield)
        if ec_eq is not None:
            print('[NEW] Subtracting the equilibirum effective charges...')
            # ec - ec_eq_gamma, np.array with shape = (N_configs, N_at_sc, Efield, cart)
            d2M_dR = perturbations.get_d2M_dR_av(ensemble, ec - ec_eq_gamma, None, symmetrize = symmetrize)
        else:
            d2M_dR = perturbations.get_d2M_dR_av(ensemble, ec, None, symmetrize = symmetrize)

        # Divide by the masses of the atoms in the supercell
        d2M_dR = np.einsum('a, abc, b -> abc', np.sqrt(self.m)**-1, d2M_dR, np.sqrt(self.m)**-1)

        # Now go in polarization basis, np.array with shape = (n_modes, n_modes, E_filed)
        d2M_dR_muspace = np.einsum('am, abc, bn -> mnc', self.pols, d2M_dR, self.pols)

        # Project along the direction of the filed, np.array with shape = (n_modes, n_modes)
        dZ_dR_muspace = np.einsum('mnc, c -> mn', d2M_dR_muspace, pol_vec_light)

        # Symmetrize in mu space, np.array with shape = (n_modes, n_modes)
        dZ_dR_muspace = 0.5 * (dZ_dR_muspace + dZ_dR_muspace.T)

        # Get chi_minus and chi_plus tensors, np.array with shape = (n_modes, n_modes)
        chi_minus = self.get_chi_minus()
        chi_plus  = self.get_chi_plus()

        # Get the pertubations on a'^(1) b'^(1)
        pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), dZ_dR_muspace)
        pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , dZ_dR_muspace)

        # Check if everything is symmetric
        assert np.all(np.abs(dZ_dR_muspace - dZ_dR_muspace.T) < 1e-10), "Second derivative of the dipole is not symmetric in pol basis"
        assert np.all(np.abs(pert_a - pert_a.T) < 1e-10), "a'(1) pertubation is not symmetric in pol basis"
        assert np.all(np.abs(pert_b - pert_b.T) < 1e-10), "b'(1) pertubation is not symmetric in pol basis"

        # Now get the perturbation for a'^(1)
        current = self.n_modes
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
            current = current + self.n_modes - i

        # Now get the pertrubation for b'^(1)
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
            current = current + self.n_modes - i

        # Add the mask dot taking into account symmetric elements
        mask_dot = self.mask_dot_wigner()
        # OVERWRITE the pertubation modulus considering the two phonon sector
        self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

        print('[NEW] Perturbation modulus after adding two ph contributions = {}'.format(self.perturbation_modulus))
        print()

    return

prepare_anharmonic_ir(ec=None, ec_eq=None, pol_vec_light=np.array([1.0, 0.0, 0.0]), add_two_ph=False)

PREPARE THE PSI VECTOR FOR ANHARMONIC IR SPECTRUM CALCULATION

This works only with the Wigner representation if we add the two phonons effect. Prepare the psi vector for IR spectrum considering position-dependent effective charges.

Parameters:
-effective_charges: nd.array (N_configs, N_atoms_sc, E_comp, cart_comp),
     the effective charges for all configurations.
     Indices are: Number of configuration, number of atoms in the super cell,
     electric field component, atomic coordinate.
-effective_charges_eq: nd.array, the effective charges at equilibrium.
     Indices are: number of atoms in the unit cell,
     electric field component, atomic coordinate.   
-pol_vec_light: nd.array, the polarization of in-out light. default is x
-add_two_ph: bool, if True two phonon processes are included in the calculation
-symm_eff_charges: bool, if True the effective charges are symmetrized
-ensemble: a scha ensemble object to compute the effective charges
Source code in tdscha/DynamicalLanczos.py
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
def prepare_anharmonic_ir(self, ec = None, ec_eq = None, pol_vec_light = np.array([1.,0.,0.]), add_two_ph = False):
    """
    PREPARE THE PSI VECTOR FOR ANHARMONIC IR SPECTRUM CALCULATION
    =============================================================

    This works only with the Wigner representation if we add the two phonons effect. 
    Prepare the psi vector for IR spectrum considering position-dependent effective charges.

    Parameters:
    -----------
        -effective_charges: nd.array (N_configs, N_atoms_sc, E_comp, cart_comp),
             the effective charges for all configurations.
             Indices are: Number of configuration, number of atoms in the super cell,
             electric field component, atomic coordinate.
        -effective_charges_eq: nd.array, the effective charges at equilibrium.
             Indices are: number of atoms in the unit cell,
             electric field component, atomic coordinate.   
        -pol_vec_light: nd.array, the polarization of in-out light. default is x
        -add_two_ph: bool, if True two phonon processes are included in the calculation
        -symm_eff_charges: bool, if True the effective charges are symmetrized
        -ensemble: a scha ensemble object to compute the effective charges
    """
    if not self.use_wigner and add_two_ph:
        raise NotImplementedError('The two phonon processes are implemented only in Wigner')

    if ec is None:
        raise ValueError('Must specify the effective charges for all configurations!')

    print()
    print('PREPARE THE IR ANHARMONIC SPECTRUM CALCULATION')
    print('==============================================')
    print('Are we considering two ph effects? = {}'.format(add_two_ph))
    print('Are we using Wigner? = {}'.format(self.use_wigner))
    print()

    # The effective charges for each configuration (N_configs, N_at_sc, E_field_comp, 3)
    eff = np.zeros((self.N, self.nat, 3, 3))

    assert ec.shape == eff.shape, 'The effective charges in input have the wrong shape. The required is {}'.format(eff.shape)

    # Get the effective charges
    eff = ec.copy()

    # FIRST DERIVATIVE OF THE DIPOLE
    # Project along the direction of light polarization, (N_configs, N_at_sc, 3)
    z_eff = np.einsum("iabc, b -> iac", eff, pol_vec_light)

    # FIRST DERIVATIVE OF THE DIPOLE
    # Average of effective charges on the ensemble (N_at_sc, 3)
    d1_M = np.einsum('i, iab -> ab', self.rho, z_eff) /np.sum(self.rho)

    # Now rescale by the mass and go in polarizaiton basis
    self.prepare_perturbation(d1_M.ravel(), masses_exp = -1)
    print('Pertubation modulus with one ph effects only = {}'.format(self.perturbation_modulus))
    print()

    # NOW PREPARE THE SECOND ORDER DIPOLE MOMENT
    if add_two_ph:
        if ec_eq is not None:
            print('Subtracting the equilibirum effective charges...')
            print()
            n_supercell = np.prod(self.dyn.GetSupercell())
            ec_eq_size = np.shape(ec_eq)
            MSG = """
            Error, effective charges of the wrong shape: {}
            """.format(ec_eq_size)
            assert len(ec_eq_size) == 3, MSG
            if not self.ignore_small_w:
                assert ec_eq_size[0] * ec_eq_size[2] * n_supercell == self.n_modes + 3
            assert ec_eq_size[1] == ec_eq_size[2] == 3

            # Eq effective charges, (N_at_uc, 3)
            z_eff_eq = np.einsum("abc, b -> ac", ec_eq, pol_vec_light)
            # Eq effective charges at gamma, (N_at_sc, 3)
            z_eff_eq_gamma = np.tile(z_eff_eq.ravel(), n_supercell).reshape((self.nat, 3))

            # This should reduce the noise when computing the 2ph vertex
            z_eff -= z_eff_eq_gamma

        print('[OLD] Getting the two phonon contribution...')

        # Polarization vectors over mass, shape = (N_at_sc, n_modes)
        pols_mass = np.einsum('a, am -> am', np.sqrt(self.m)**-1, self.pols)

        # The mass rescaled projected effective charges in polarization basis, shape = (N_configs, n_modes)
        z_pols_mass = np.einsum('am, ia -> im ', pols_mass, z_eff.ravel().reshape((self.N, self.nat * 3)))

        # Eigenvalues of Upsilon mass rescaled, shape = (n_modes)
        xi2_inv = f_ups(self.w, self.T)

        # The mass rescaled displacements in polarization basis divided by xi2, shape = (N_configs, n_modes)
        u_xi2 = np.einsum('im, m -> im', self.X, xi2_inv)

        # Add the effective charges, shape = (N_configs, n_modes, n_modes)
        u_xi2_Z = np.einsum('in , im -> inm', u_xi2, z_pols_mass)

        # Get the reweighted average of the second derivative, shape = (n_modes, n_modes)
        d2_M = np.einsum('i, inm -> nm', self.rho, u_xi2_Z) /np.sum(self.rho)
        d2_M = 0.5 * (d2_M + d2_M.T)

        # Get chi_minus and chi_plus tensors
        chi_minus = self.get_chi_minus()
        chi_plus  = self.get_chi_plus()

        # Get the pertubations on a'^(1) b'^(1)
        pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), d2_M)
        pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , d2_M)

        # Check if everything is symmetric
        assert np.all(np.abs(d2_M - d2_M.T) < 1e-10), "Second derivative of the dipole is not symmetric in pol basis"
        assert np.all(np.abs(pert_a - pert_a.T) < 1e-10), "a'(1) pertubation is not symmetric in pol basis"
        assert np.all(np.abs(pert_b - pert_b.T) < 1e-10), "b'(1) pertubation is not symmetric in pol basis"

        # Now get the perturbation for a'^(1)
        current = self.n_modes
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
            current = current + self.n_modes - i

        # Now get the pertrubation for b'^(1)
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
            current = current + self.n_modes - i

        # Add the mask dot taking into account symmetric elements
        mask_dot = self.mask_dot_wigner()
        # OVERWRITE the pertubation modulus considering the two phonon sector
        self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

        print('[OLD] Perturbation modulus after adding two ph contributions = {}'.format(self.perturbation_modulus))
        print()

    return

prepare_perturbation(vector, masses_exp=1, add=False)

This function prepares the calculation for the Green function

Where |v> is the vector passed as input. If you want to compute the raman, for istance, it can be the vector of the Raman intensities.

The vector can be obtained contracting it with the polarization vectors. The contraction can be on the numerator or on the denumerator, depending on the observable.

NOTE: This function prepares the pertubation ONLY in the R sector Both IR and Raman has masses_exp = -1

.. math ::

v_\mu = \sum_a v_a e_\mu^a \cdot \sqrt{m_a}

v_\mu = \sum_a v_a \frac{e_\mu^a}{  \sqrt{m_a}}
Source code in tdscha/DynamicalLanczos.py
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
def prepare_perturbation(self, vector, masses_exp = 1, add = False):
    r"""
    This function prepares the calculation for the Green function

    <v| G |v>

    Where |v> is the vector passed as input. If you want to compute the
    raman, for istance, it can be the vector of the Raman intensities.

    The vector can be obtained contracting it with the polarization vectors.
    The contraction can be on the numerator or on the denumerator, depending on the
    observable.

    NOTE: This function prepares the pertubation ONLY in the R sector
    Both IR and Raman has masses_exp = -1

    .. math ::

        v_\mu = \sum_a v_a e_\mu^a \cdot \sqrt{m_a} 

        v_\mu = \sum_a v_a \frac{e_\mu^a}{  \sqrt{m_a}} 

    Parameters
    ----------
        vector: ndarray( size = (3*natoms))
            The vector of the perturbation for the computation of the green function
        masses_exp : float
            The vector is multiplied by the square root of the masses raised to masses_exp.
            If you want to multiply each component by the square root of the masses use 1,
            if you want to divide by the quare root use -1, use 0 if you do not want to use the
            masses.
        add : bool
            If true, the perturbation is added on the top of the one already setup.
            Calling add does not cause a reset of the Lanczos
    """
    if not add:
        self.reset()
        self.psi = np.zeros(self.psi.shape, dtype = TYPE_DP)

    # Convert the vector in the polarization space
    m_on = np.sqrt(self.m) ** masses_exp
    print("SHAPE:", m_on.shape, vector.shape, self.pols.shape)
    new_v = np.einsum("a, a, ab->b", m_on, vector, self.pols)
    self.psi[:self.n_modes] += new_v

    # THIS IS OK IN THE WIGNER REPRESENTATION BECAUSE
    # THE PERTUBATION ENTERS ONLY IN THE R SECTOR
    self.perturbation_modulus = new_v.dot(new_v)

    if self.symmetrize:
        self.symmetrize_psi()

prepare_mode(index)

Prepare the perturbation on a single phonon mode. This is usefull to get the single mode contribution to the overall spectral function.

Source code in tdscha/DynamicalLanczos.py
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
def prepare_mode(self, index):
    """
    Prepare the perturbation on a single phonon mode.
    This is usefull to get the single mode contribution to the overall spectral function.

    Parameters
    ----------
        index : int
            The index of the mode in the supercell. Starting from 0 (lowest frequency, excluding acoustic modes at Gamma) 
    """
    self.reset()

    self.psi[:] = 0
    self.psi[index] = 1 
    self.perturbation_modulus = 1

prepare_two_ph(a, b)

Prepare the psi vector for a two phonon response. Available only in Winger.

Parameters:
a, b: int indices of the modes (acustic are excluded).
Source code in tdscha/DynamicalLanczos.py
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
def prepare_two_ph(self, a, b):
    """
    Prepare the psi vector for a two phonon response.
    Available only in Winger.

    Parameters:
    ----------
        a, b: int indices of the modes (acustic are excluded).
    """
    if not self.use_wigner:
        raise NotImplementedError('The two phonon response is available only in Wigner')

    if a > self.n_modes or b > self.n_modes:
        raise ValueError('The a-b indices must be smaller than {}'.format(self.n_modes))

    print()
    print('PREPARE THE TWO PHONON PERTUBATION')
    print('Indices selected a = {} b = {}'.format(a,b))
    print()

    self.reset()
    self.psi[:] = 0

    # Get chi minus and chi plus, shape = (n_modes, n_modes)
    chi_plus  = self.get_chi_plus()
    chi_minus = self.get_chi_minus()

    # The matrix for the second derivatives
    mat_modes = np.zeros((self.n_modes, self.n_modes))

    mat_modes[a,b] = 0.5
    mat_modes[b,a] += 0.5

    # Get the pertbations on a' b'
    pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), mat_modes)
    pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , mat_modes)

    # Now get the perturbation for a'^(1)
    current = self.n_modes
    for i in range(self.n_modes):
        self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
        current = current + self.n_modes - i

    # Now get the pertrubation for b'^(1)
    for i in range(self.n_modes):
        self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
        current = current + self.n_modes - i

    # Add the mask dot taking into account symmetric elements
    mask_dot = self.mask_dot_wigner()

    # Update the pertubation modulus
    self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

    return

get_vector_dyn_from_psi()

This function returns a standard vector and the dynamical matrix in cartesian coordinates

This can be used to symmetrize the vector.

The vector is returned in [Bohr] and the force constant matrix is returned in [Ry/bohr^2]

Source code in tdscha/DynamicalLanczos.py
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
def get_vector_dyn_from_psi(self):
    """
    This function returns a standard vector and the dynamical matrix in cartesian coordinates

    This can be used to symmetrize the vector.

    The vector is returned in [Bohr] and the force constant matrix is returned in [Ry/bohr^2]
    """


    vector = self.psi[:self.n_modes]
    dyn = self.psi[self.n_modes:].reshape((self.n_modes, self.n_modes))

    w_a = np.tile(self.w, (self.n_modes, 1))
    w_b = np.tile(self.w, (self.n_modes, 1)).T 

    dyn *= ( 2*(w_a + w_b) * np.sqrt(w_a*w_b*(w_a + w_b)))

    # Get back the real vectors
    real_v = np.einsum("b, ab->a",  vector, self.pols)  /np.sqrt(self.m)
    real_dyn = np.einsum("ab, ca, db-> cd", dyn, self.pols, self.pols)
    real_dyn *= np.outer(np.sqrt(self.m), np.sqrt(self.m))

    return real_v, real_dyn 

set_psi_from_vector_dyn(vector, dyn)

Set the psi vector from a given vector of positions [bohr] and a force constant matrix [Ry/bohr^2]. Used to reset the psi after the symmetrization.

Source code in tdscha/DynamicalLanczos.py
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
def set_psi_from_vector_dyn(self, vector, dyn):
    """
    Set the psi vector from a given vector of positions [bohr] and a force constant matrix [Ry/bohr^2].
    Used to reset the psi after the symmetrization.
    """

    new_v = np.einsum("a, ab->b",  np.sqrt(self.m) * vector, self.pols)

    new_dyn = dyn / np.outer(np.sqrt(self.m), np.sqrt(self.m))
    new_dyn = np.einsum("ab, ai, bj-> ij", new_dyn, self.pols, self.pols) 

    w_a = np.tile(self.w, (self.n_modes, 1))
    w_b = np.tile(self.w, (self.n_modes, 1)).T 

    new_dyn /= ( 2*(w_a + w_b) * np.sqrt(w_a*w_b*(w_a + w_b)))

    self.psi[:self.n_modes] = new_v
    self.psi[self.n_modes:] = new_dyn.ravel()

symmetrize_psi()

Symmetrize the psi vector.

Source code in tdscha/DynamicalLanczos.py
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
def symmetrize_psi(self):
    """
    Symmetrize the psi vector.
    """

    # First of all, get the vector and the dyn
    vector, dyn = self.get_vector_dyn_from_psi()

    print ("Vector before symmetries:")
    print (vector)

    # Symmetrize the vector
    self.qe_sym.SetupQPoint()
    new_v = np.zeros( (self.nat, 3), dtype = np.float64, order = "F")
    new_v[:,:] = vector.reshape((self.nat, 3))
    self.qe_sym.SymmetrizeVector(new_v)
    vector = new_v.ravel()

    print ("Vector after symmetries:")
    print (vector)

    # Symmetrize the dynamical matrix
    dyn_q = CC.Phonons.GetDynQFromFCSupercell(dyn, np.array(self.dyn.q_tot), self.uci_structure, self.super_structure)
    self.qe_sym.SymmetrizeFCQ(dyn_q, self.dyn.q_stars, asr = "custom")
    dyn = CC.Phonons.GetSupercellFCFromDyn(dyn_q, np.array(self.dyn.q_tot), self.uci_structure, self.super_structure)

    # Push everithing back into the psi
    self.set_psi_from_vector_dyn(vector, dyn)

set_max_frequency(freq)

SETUP THE REVERSE LANCZOS

This function prepares the Lanczos algorithm in order to find the lowest eigenvalues You should provide the maximum frequencies of the standard spectrum. Then the Lanczos is initialized in order to solve the problem

(Ia - L) x = b

where a is sqrt(2*freq) so that should match the maximum energy. Since Lanczos is very good in converging the biggest (in magnitude) eigenvectors, this procedure should accelerate the convergence of the low energy spectrum.

NOTE: This method should be executed BEFORE the Lanczos run.

Source code in tdscha/DynamicalLanczos.py
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
def set_max_frequency(self, freq):
    """
    SETUP THE REVERSE LANCZOS
    =========================

    This function prepares the Lanczos algorithm in order to find the lowest eigenvalues
    You should provide the maximum frequencies of the standard spectrum.
    Then the Lanczos is initialized in order to solve the problem

    (Ia - L) x = b

    where a is sqrt(2*freq) so that should match the maximum energy. 
    Since Lanczos is very good in converging the biggest (in magnitude) eigenvectors, this
    procedure should accelerate the convergence of the low energy spectrum.

    NOTE: This method should be executed BEFORE the Lanczos run.

    Parameters
    ----------
        freq : float
           The frequencies (in Ry) of the maximum eigenvalue.
    """

    self.shift_value = 4*freq*freq
    self.reverse_L = True

    print("Shift value:", self.shift_value)

apply_L1()

APPLY THE L1

This is the first part of the application, it involves only harmonic propagation.

Results
out_vect : ndarray(shape(self.psi))
    It returns the application of the harmonic part of the L matrix
Source code in tdscha/DynamicalLanczos.py
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
def apply_L1(self):
    """
    APPLY THE L1
    ============

    This is the first part of the application, it involves only harmonic propagation.

    Results
    -------
        out_vect : ndarray(shape(self.psi))
            It returns the application of the harmonic part of the L matrix
    """

    out_vect = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

    # Get the harmonic responce function
    out_vect[:self.n_modes] = (self.psi[:self.n_modes] * self.w) * self.w

    #print("freqsL1: ", self.w)
    #print("out 0:", out_vect[0])
    # Get the harmonic responce on the propagator
    w_a = np.tile(self.w, (self.n_modes, 1))
    w_b = np.tile(self.w, (self.n_modes, 1)).T 

    new_out = (w_a + w_b)**2
    out_vect[self.n_modes:] = new_out.ravel() * self.psi[self.n_modes:]

    #print("out 0 (just end):", out_vect[0])
    return out_vect

apply_L1_FT(transpose=False)

APPLY THE L1 AT FINITE TEMPERATURE

This is the first part of the application, it involves only HARMONIC propagation.

NORMAL: this method applies -L_harm: :: math . \begin{bmatrix} -Z'' & 0 & 0 \ 0 & -X & -Y \ 0 & -X' & -Y' \end{bmatrix} on the follwoing vector: :: math . \begin{bmatrix} \mathcal{R}^{(1)} \ \tilde{\Upsilon}^{(1)} \ \Re \tilde{A}^{(1)}. \end{bmatrix}

WIGNER: this method applies +L_harm: :: math . \begin{bmatrix} - \omega^2_\alpha & 0 & 0 \ 0 & -\omega^-{\alpha\beta}^2 & 0 \ 0 & 0 & -\omega^+{\alpha\beta}^2 \end{bmatrix} on the following vector: :: math . \begin{bmatrix} \tilde{\mathcal{R}}^{(1)}\alpha\ \tilde{a}'^{(1)}{\alpha\beta}\ \tilde{b}'^{(1)}_{\alpha\beta}. \end{bmatrix}

If transpose = True it applies the transpose.

Results
-out_vect : ndarray(shape(self.psi))
    It returns the application of the harmonic part of the L matrix
Source code in tdscha/DynamicalLanczos.py
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
    def apply_L1_FT(self, transpose = False):
        r"""
        APPLY THE L1 AT FINITE TEMPERATURE
        ==================================

        This is the first part of the application, it involves only HARMONIC propagation.

        NORMAL: this method applies -L_harm: 
        :: math .
            \begin{bmatrix}
            -Z'' &   0  &  0 \\
            0    &  -X  & -Y \\
            0    &  -X' & -Y'
            \end{bmatrix}
        on the follwoing vector:
        :: math .
            \begin{bmatrix}
            \mathcal{R}^{(1)} \\
            \tilde{\Upsilon}^{(1)} \\
            \Re \tilde{A}^{(1)}.
            \end{bmatrix}

        WIGNER: this method applies +L_harm: 
        :: math .
            \begin{bmatrix}
            - \omega^2_\alpha & 0 & 0 \\
            0 &  -\omega^-_{\alpha\beta}^2  & 0 \\ 
            0 & 0 &  -\omega^+_{\alpha\beta}^2 
            \end{bmatrix}
        on the following vector:
        :: math .
            \begin{bmatrix}
            \tilde{\mathcal{R}}^{(1)}_\alpha\\ 
            \tilde{a}'^{(1)}_{\alpha\beta}\\ 
            \tilde{b}'^{(1)}_{\alpha\beta}.
            \end{bmatrix}

        If transpose = True it applies the transpose.

        Results
        -------
            -out_vect : ndarray(shape(self.psi))
                It returns the application of the harmonic part of the L matrix
        """
        # Prepare the free propagator on the positions
        out_vect = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

        if self.ignore_harmonic:
            return out_vect 

        # The elements where w_a and w_b are exchanged are dependent
        # So we must avoid including them
        i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
        i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

        new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
        new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

        w_a = self.w[new_i_a]
        w_b = self.w[new_i_b]

        N_w2 = len(w_a)

        ##############################################
        # Get the harmonic responce function on R^(1)#
        ##############################################
        # Apply the diagonal free propagation 
        if not self.use_wigner:
#             print('[NORMAL]: harmonic R(1)')
            out_vect[:self.n_modes] = (self.psi[:self.n_modes] * self.w) * self.w
        else:
            # Use Wigner
#             print('[WIGNER]: harmonic R(1)')
            out_vect[:self.n_modes] = -(self.psi[:self.n_modes] * self.w) * self.w

        # Get the BE occupation number
        n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        if self.T > 0:
            n_a = 1 / (np.exp( w_a / np.double(self.T / __RyToK__)) - 1)
            n_b = 1 / (np.exp( w_b / np.double(self.T / __RyToK__)) - 1)


        # Apply the non interacting X operator
        # Where R^(1) ends and Upsilon^(1)-a'^(1) starts
        start_Y = self.n_modes
        # Where Upsilon^(1)-a'^(1) ends and ReA^(1)-b'^(1) starts
        start_A = self.n_modes + N_w2

        #########################################################################
        # The R^(1) perturbation ends at start_Y
        # The Upsilon^(1)/a'^{(1)} perturbation start at start_Y
        # The ReA^(1)/b'^{(1)} perturbation starts at start_Y + 0.5*N_modes*(N_modes + 1)
        #################################################################################

        #print("start_Y: {} | start_A: {} | end_A: {} | len_psi: {}".format(start_Y, start_A, start_A + N_w2, len(self.psi)))

        ERR_MSG ="""
ERROR,
The initial vector for the Lanczos algorithm has a wrong dimension. 
This may be caused by the Lanczos initialized at the wrong temperature.
"""
        assert len(self.psi) == start_A + N_w2, ERR_MSG

        ############################################################
        # Get the harmonic responce function on Upsilon^(1)-a'^(1) #
        ############################################################

        if not self.use_wigner:
#             print('[NORMAL]: harmonic Y(1)')
            # Apply the diagonal free propagation on Y
            X_ab_NI = -w_a**2 - w_b**2 - (2*w_a *w_b) /((2*n_a + 1) * (2*n_b + 1))
            out_vect[start_Y: start_A] = - X_ab_NI * self.psi[start_Y: start_A]

            # Apply the off diagonal free propagation Y-ReA
            Y_ab_NI = - (8 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))
            if not transpose:
                out_vect[start_Y : start_A] += - Y_ab_NI * self.psi[start_A: ]
            else:
                out_vect[start_A:] += - Y_ab_NI * self.psi[start_Y : start_A]

            #L_operator[start_Y : start_A, start_A:] = - np.diag(Y_ab_NI) * extra_count
            #L_operator[start_Y + np.arange(self.n_modes**2), start_A + exchange_frequencies] -=  Y_ab_NI / 2
        else:
#             print('[WIGNER]: harmonic a(1)')
            # Apply the diagonal free propagation in WIGNER on a'^{(1)}
            a_harm = -(w_a**2 + w_b**2 - 2. * w_a * w_b)
            out_vect[start_Y: start_A] = +a_harm * self.psi[start_Y: start_A]


        ########################################################
        # Get the harmonic responce function on ReA^(1)-b'^(1) #
        ########################################################

        if not self.use_wigner:
#             print('[NORMAL]: harmonic ReA(1)')
            # Apply the off diagonal free propagation ReA-Y
            X1_ab_NI = - (2*n_a*n_b + n_a + n_b) * (2*n_a*n_b + n_a + n_b + 1)*(2 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))

            # Apply the off-diagonal free propagation
            if not transpose:
                out_vect[start_A:] += - X1_ab_NI * self.psi[start_Y: start_A]
            else:
                out_vect[start_Y: start_A] += - X1_ab_NI * self.psi[start_A:]
            #L_operator[start_A:, start_Y : start_A] = - np.diag(X1_ab_NI) / 1 * extra_count
            #L_operator[start_A + np.arange(self.n_modes**2), start_Y + exchange_frequencies] -= X1_ab_NI / 2

            # Apply the diagonal free propagation ReA
            Y1_ab_NI = - w_a**2 - w_b**2 + (2*w_a *w_b) /( (2*n_a + 1) * (2*n_b + 1))
            out_vect[start_A:] += - Y1_ab_NI * self.psi[start_A:]
            #L_operator[start_A:, start_A:] = -np.diag(Y1_ab_NI) / 1 * extra_count
            #L_operator[start_A + np.arange(self.n_modes**2),  start_A + exchange_frequencies] -= Y1_ab_NI / 2
        else:
#             print('[WIGNER]: harmonic b(1)')
            # Apply the diagonal free propagation in WIGNER on b'^{(1)}
            b_harm = -(w_a**2 + w_b**2 + 2. * w_a * w_b)
            out_vect[start_A:] = +b_harm * self.psi[start_A:]

        return out_vect

apply_L1_inverse_FT(psi, transpose=False)

APPLY THE INVERSE L1 AT FINITE TEMPERATURE

This method allows for preconditioning, as L1 is a diagonal application

Results
out_vect : ndarray(shape(self.psi))
    It returns the application of the harmonic part of the inverse L matrix
Source code in tdscha/DynamicalLanczos.py
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
    def apply_L1_inverse_FT(self, psi, transpose = False):
        """
        APPLY THE INVERSE L1 AT FINITE TEMPERATURE
        ==========================================

        This method allows for preconditioning, as L1 is a diagonal application

        Results
        -------
            out_vect : ndarray(shape(self.psi))
                It returns the application of the harmonic part of the inverse L matrix
        """


        # Prepare the free propagator on the positions
        out_vect = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

        if self.ignore_harmonic:
            return out_vect 

        # The elements where w_a and w_b are exchanged are dependent
        # So we must avoid including them
        i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
        i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

        new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
        new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

        w_a = self.w[new_i_a]
        w_b = self.w[new_i_b]

        N_w2 = len(w_a)

        # Get the harmonic responce function
        out_vect[:self.n_modes] = (psi[:self.n_modes] / self.w) / self.w


        n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)

        not_populated_mask_a = 0.01 * w_a *__RyToK__ > self.T
        not_populated_mask_b = 0.01 * w_a *__RyToK__ > self.T

        if self.T > 0:
            n_a = 1 / (np.exp( w_a / np.double(self.T /__RyToK__)) - 1)
            n_b = 1 / (np.exp( w_b / np.double(self.T / __RyToK__)) - 1)
            n_a[not_populated_mask_a] = 0
            n_b[not_populated_mask_b] = 0


        # Apply the non interacting X operator
        start_Y = self.n_modes
        start_A = self.n_modes + N_w2


        ERR_MSG ="""
ERROR,
The initial vector for the Lanczos algorithm has a wrong dimension. 
This may be caused by the Lanczos initialized at the wrong temperature.
"""
        assert len(psi) == start_A + N_w2, ERR_MSG

        # Get the free two-phonon propagator
        X_ab_NI = -w_a**2 - w_b**2 - (2*w_a *w_b) /( (2*n_a + 1) * (2*n_b + 1))
        Y_ab_NI = - (8 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))
        X1_ab_NI = - (2*n_a*n_b + n_a + n_b) * (2*n_a*n_b + n_a + n_b + 1)*(2 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))
        Y1_ab_NI = - w_a**2 - w_b**2 + (2*w_a *w_b) /( (2*n_a + 1) * (2*n_b + 1))

        # Invert the propagator
        den = X_ab_NI * Y1_ab_NI - X1_ab_NI * Y_ab_NI

        # Regularize (avoid non invertibility when w_a = w_b and kbT << w)
        den_mask = den < __EPSILON__ 
        den[den_mask] = np.inf

        X_new = -Y1_ab_NI / den
        Y_new = Y_ab_NI / den
        X1_new = X1_ab_NI / den 
        Y1_new = - X_ab_NI / den

        X_new[den_mask] = - 1 / X_ab_NI[den_mask]

        # If T > 0, then also ReA could be inverted (only if w_a and w_b are thermally populated)
        if self.T > __EPSILON__:
            new_mask = (w_a == w_b) & (n_a > __EPSILON__)
            Y1_new[new_mask] = -1 / Y1_ab_NI[new_mask]

        out_vect[start_Y: start_A] = X_new * psi[start_Y: start_A]
        if not transpose:
            out_vect[start_Y : start_A] += Y_new * psi[start_A: ]
        else:
            out_vect[start_A:] += Y_new * psi[start_Y : start_A]

        #L_operator[start_Y : start_A, start_A:] = - np.diag(Y_ab_NI) * extra_count
        #L_operator[start_Y + np.arange(self.n_modes**2), start_A + exchange_frequencies] -=  Y_ab_NI / 2


        if not transpose:
            out_vect[start_A:] += X1_new * psi[start_Y: start_A]
        else:
            out_vect[start_Y: start_A] += X1_new * psi[start_A:]
        #L_operator[start_A:, start_Y : start_A] = - np.diag(X1_ab_NI) / 1 * extra_count
        #L_operator[start_A + np.arange(self.n_modes**2), start_Y + exchange_frequencies] -= X1_ab_NI / 2

        out_vect[start_A:] += Y1_new * psi[start_A:]
        #L_operator[start_A:, start_A:] = -np.diag(Y1_ab_NI) / 1 * extra_count
        #L_operator[start_A + np.arange(self.n_modes**2),  start_A + exchange_frequencies] -= Y1_ab_NI / 2


        return out_vect

apply_L1_static(psi, inverse=False, power=1)

Apply the harmonic part of the L matrix for computing the static case (at any temperature).

The inverse keyword, if True, compute the L^-1. If power is different from one, then multiply it to a specific power.

Source code in tdscha/DynamicalLanczos.py
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
    def apply_L1_static(self, psi, inverse = False, power = 1):
        """
        Apply the harmonic part of the L matrix for computing the static case (at any temperature).

        The inverse keyword, if True, compute the L^-1. If power is different from one, then multiply it to a specific power.

        """

        expected_dim = self.n_modes +  (self.n_modes * (self.n_modes + 1)) // 2
        ERRMSG = """
Error, for the static calculation the vector must be of dimension {}, got {}
""".format(expected_dim, len(psi))

        assert len(psi) == expected_dim, ERRMSG

        out_vect = np.zeros(psi.shape, dtype = TYPE_DP)

        if inverse:
            power *= -1

        # Here we apply the D2
        out_vect[:self.n_modes] = psi[:self.n_modes] * (self.w **(2 * power))
        #out_vect[:self.n_modes] = (psi[:self.n_modes] / self.w) / self.w 


        # Now we apply the inverse of the W matrix
        # The elements where w_a and w_b are exchanged are dependent
        # So we must avoid including them
        i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
        i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

        new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
        new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

        w_a = self.w[new_i_a]
        w_b = self.w[new_i_b]


        n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)

        not_populated_mask_a = 0.01 * w_a *__RyToK__ > self.T
        not_populated_mask_b = 0.01 * w_a *__RyToK__ > self.T

        diff_n_ab = np.zeros( np.shape(w_a), dtype = TYPE_DP)
        w_ab_equal = np.abs(w_a - w_b) < 1e-8
        w_equal = w_a[w_ab_equal]
        n_equal = n_a[w_ab_equal]

        if self.T > 0:
            beta = np.double(__RyToK__ / self.T)
            n_a = 1 / (np.exp( w_a * beta) - 1)
            n_b = 1 / (np.exp( w_b * beta) - 1)
            n_a[not_populated_mask_a] = 0
            n_b[not_populated_mask_b] = 0

            diff_n_ab[:] = (n_a - n_b) / (w_a - w_b)
            diff_n_ab[w_ab_equal] = - beta * np.exp(w_equal * beta) * n_equal**2

        Lambda =  (n_a + n_b + 1) / (w_a + w_b) - diff_n_ab
        Lambda /= 4 * w_a * w_b

        out_vect[self.n_modes:] =  psi[self.n_modes:] / Lambda**power

        return out_vect

get_chi_minus()

Get the chi^- equilibrium tensor in the Wigner formalism.

:: math . \tilde{\chi}^{-}{\mu\nu} = \frac{\hbar\left[\omega\alpha - \omega_\beta\right]\left[n_\alpha - n_\beta\right]}{2\omega_\alpha\omega_\beta}

Results:
-chi_minus: chi minus tensor, np.array with shape = (n_modes, n_modes)
Source code in tdscha/DynamicalLanczos.py
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
def get_chi_minus(self):
    r"""
    Get the chi^- equilibrium tensor in the Wigner formalism.

    :: math .
        \tilde{\chi}^{-}_{\mu\nu} = \frac{\hbar\left[\omega_\alpha - \omega_\beta\right]\left[n_\alpha - n_\beta\right]}{2\omega_\alpha\omega_\beta}

    Results:
    -------
        -chi_minus: chi minus tensor, np.array with shape = (n_modes, n_modes)

    """
    # Prepare the result
    chi_minus = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

    # Create the matrix with freqeuncies
    w = np.tile(self.w, (self.n_modes,1))

    # Create the Bose-Eninstein occupation number matrix
    n = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

    if self.T > __EPSILON__:
        n = 1.0 / (np.exp(w * 157887.32400374097 /self.T) - 1.0)

    chi_minus = (w - w.T) * (n - n.T) /(2. * w * w.T)

    return chi_minus

get_chi_plus()

Get the chi^+ equilibrium tensor in the Wigner formalism.

:: math . \tilde{\chi}^{+}{\mu\nu} = \frac{\hbar\left[\omega\alpha + \omega_\beta\right]\left[1 + n_\alpha + n_\beta\right]}{2\omega_\alpha\omega_\beta}

Results:
-chi_plus: chi plus tensor, np.array with shape = (n_modes, n_modes)
Source code in tdscha/DynamicalLanczos.py
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
def get_chi_plus(self):
    r"""
    Get the chi^+ equilibrium tensor in the Wigner formalism.

    :: math .
        \tilde{\chi}^{+}_{\mu\nu} = \frac{\hbar\left[\omega_\alpha + \omega_\beta\right]\left[1 + n_\alpha + n_\beta\right]}{2\omega_\alpha\omega_\beta}

    Results:
    -------
        -chi_plus: chi plus tensor, np.array with shape = (n_modes, n_modes)

    """
    # Prepare the result
    chi_plus = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

    # Create the matrix with freqeuncies
    w = np.tile(self.w, (self.n_modes,1))

    # Create the Bose-Eninstein occupation number matrix
    n = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

    if self.T > __EPSILON__:
        n = 1.0 / (np.exp(w * 157887.32400374097 /self.T) - 1.0)

    chi_plus = (w + w.T) * (1 + n + n.T) /(2. * w * w.T)

    return chi_plus

get_a1_b1_wigner(get_a1=True)

Get the the pertrubation on the a' matrix times sqrt(-0.5X^-) or on b' matrix from the alpha and beta pertrubation.

This function is to check the inverse of the change of variables.

:: math . \sqrt{-\frac{1}{2}\tilde{\chi}^-{\mu\nu}} \tilde{a}'^{(1)}{\mu\nu} = X_{\mu\nu} \cdot \left[\frac{+1}{2}\left(\frac{\hbar^2}{\omega_{\mu} \omega_{\nu}}\tilde{\alpha}^{(1)}{\mu\nu} + \tilde{\beta}^{(1)}{\mu\nu}\right)\right];\

:: math . \tilde{b}'^{(1)}{\mu\nu} = \frac{X{\mu\nu}}{\sqrt{\frac{1}{2}\tilde{\chi}^+{\mu\nu}}} \left[\frac{-1}{2} \left(\frac{\hbar^2}{\omega{\alpha} \omega_{\beta}}\tilde{ \alpha}{\mu\nu} - \tilde{ \beta}{\mu\nu}\right)\right].;\

Parameters:
-get_a1: bool. If true we return the a' perturbation rescaled or the b' pertrubation
Retruns:
-a1: np.array with shape = n_modes * (n_modes + 1)/2
Source code in tdscha/DynamicalLanczos.py
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
def get_a1_b1_wigner(self, get_a1 = True):
    r"""
    Get the the pertrubation on the a' matrix times sqrt(-0.5X^-) or on b' matrix
    from the alpha and beta pertrubation.

    This function is to check the inverse of the change of variables.

    :: math .
        \sqrt{-\frac{1}{2}\tilde{\chi}^-_{\mu\nu}} \tilde{a}'^{(1)}_{\mu\nu} = X_{\mu\nu} \cdot
        \left[\frac{+1}{2}\left(\frac{\hbar^2}{\omega_{\mu} \omega_{\nu}}\tilde{\alpha}^{(1)}_{\mu\nu} 
        + \tilde{\beta}^{(1)}_{\mu\nu}\right)\right];\\

    :: math .
       \tilde{b}'^{(1)}_{\mu\nu} = \frac{X_{\mu\nu}}{\sqrt{\frac{1}{2}\tilde{\chi}^+_{\mu\nu}}} 
       \left[\frac{-1}{2}
       \left(\frac{\hbar^2}{\omega_{\alpha} \omega_{\beta}}\tilde{ \alpha}_{\mu\nu} 
        - \tilde{ \beta}_{\mu\nu}\right)\right].;\\

    Parameters:
    -----------
        -get_a1: bool. If true we return the a' perturbation rescaled or the b' pertrubation

    Retruns:
    -------
        -a1: np.array with shape = n_modes * (n_modes + 1)/2
    """ 
    len_ = (self.n_modes * (self.n_modes + 1)) // 2

    # Get the beta1-alpha1 pertrubation as matrices, np.shape = (n_modes, n_modes)
    alpha_mat_1 = self.get_alpha1_beta1_wigner(get_alpha = True)
    beta_mat_1  = self.get_alpha1_beta1_wigner(get_alpha = False)

    # Now get alpha1 and beta1 as vectors
    alpha1 = np.zeros(len_, dtype = np.double)
    beta1  = np.zeros(len_, dtype = np.double)

    start = 0
    next = self.n_modes
    for i in range(self.n_modes):
        # Get the sum of the rescaled tensor
        alpha1[start : next] = alpha_mat_1[i, i:]
        beta1[start : next]  = beta_mat_1[i, i:]
        start = next 
        next = start + self.n_modes - i - 1 

    # Get the independent indeces
    # Avoid the exchange of w_a w_b
    i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
    i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

    new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
    new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

    # Get the independent indices
    w_a = self.w[new_i_a]
    w_b = self.w[new_i_b]

    n_a = np.zeros(np.shape(len(w_a)), dtype = TYPE_DP)
    n_b = np.zeros(np.shape(len(w_a)), dtype = TYPE_DP)

    if self.T > 0:
        n_a = 1 / (np.exp( w_a / np.double(self.T / __RyToK__)) - 1)
        n_b = 1 / (np.exp( w_b / np.double(self.T / __RyToK__)) - 1)

    # Get all the quantities to make the change of variables
    X = ((1 + 2 * n_a) * (1 + 2 * n_b) /8)
    w_a_b = (w_a * w_b)
    chi_minus = ((w_a - w_b) * (n_a - n_b)) /(2 * w_a * w_b)
    chi_plus  = ((w_a + w_b) * (1 + n_a + n_b)) /(2 * w_a * w_b)

    if get_a1:
        a1 = np.zeros(len_, dtype = np.double)
        a1 = 0.5 * X * (alpha1 / w_a_b + beta1)

        return a1
    else:
        b1 = -0.5 * X * (alpha1 /w_a_b - beta1)
        b1 /= np.sqrt(0.5 * chi_plus)

        return b1

get_alpha1_beta1_wigner(get_alpha=True)

Get the perturbation on the alpha/Upsilon or beta matrix from the psi vector in the Wigner formalism.

Recall that alpha and beta are the starting free parameters of the Gaussian Wigner distribution.

N.B.: This is the function that compute Upsilon^(1) matrix!

:: math . \tilde{\Upsilon}^{(1)}{\mu\nu} = \tilde{\alpha}^{(1)}{\mu\nu} = \frac{\omega_\mu \omega_\nu}{\hbar^2 X_{\mu\nu}} \left( +\sqrt{-\frac{1}{2}\tilde{\chi}^-{\mu\nu}} a'^{(1)}{\mu\nu} -\sqrt{+\frac{1}{2}\tilde{\chi}^+{\mu\nu}} b'^{(1)}{\mu\nu} \right)

:: math . \tilde{\beta}^{(1)}{\mu\nu} = \frac{1}{X{\mu\nu}} \left( \sqrt{-\frac{1}{2}\tilde{\chi}^-{\mu\nu}} a'^{(1)}{\mu\nu} + \sqrt{\frac{1}{2}\tilde{\chi}^+{\mu\nu}} b'^{(1)}{\mu\nu}\right)

Parameters:
-get_alpha: bool, if True alpha1 is returned. If False beta1 is returned.
Returns:
-alpha1 or beta1: array with shape = (n_modes, n_modes)
Source code in tdscha/DynamicalLanczos.py
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
def get_alpha1_beta1_wigner(self, get_alpha = True):
    r"""
    Get the perturbation on the alpha/Upsilon or beta matrix from the psi vector in the Wigner formalism.

    Recall that alpha and beta are the starting free parameters of the Gaussian Wigner distribution.

    N.B.: This is the function that compute Upsilon^(1) matrix!

    :: math .
        \tilde{\Upsilon}^{(1)}_{\mu\nu} = \tilde{\alpha}^{(1)}_{\mu\nu} =
        \frac{\omega_\mu \omega_\nu}{\hbar^2 X_{\mu\nu}}
        \left(
        +\sqrt{-\frac{1}{2}\tilde{\chi}^-_{\mu\nu}} a'^{(1)}_{\mu\nu} 
        -\sqrt{+\frac{1}{2}\tilde{\chi}^+_{\mu\nu}} b'^{(1)}_{\mu\nu}
        \right) 

    :: math .
        \tilde{\beta}^{(1)}_{\mu\nu} = 
        \frac{1}{X_{\mu\nu}}
        \left(
         \sqrt{-\frac{1}{2}\tilde{\chi}^-_{\mu\nu}} a'^{(1)}_{\mu\nu} 
        + \sqrt{\frac{1}{2}\tilde{\chi}^+_{\mu\nu}} b'^{(1)}_{\mu\nu}\right) 

    Parameters:
    -----------
        -get_alpha: bool, if True alpha1 is returned. If False beta1 is returned.

    Returns:
    --------
        -alpha1 or beta1: array with shape = (n_modes, n_modes)
    """
    # Where the arrays start
    start_a = self.n_modes
    start_b = self.n_modes +  (self.n_modes * (self.n_modes + 1)) // 2

    # GET THE PERTURBED PARAMETERS a'^(1) and b'^(1)
    a_all = self.psi[start_a : start_b]
    b_all = self.psi[start_b :]

    # Get the independent indeces
    # Avoid the exchange of w_a w_b
    i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
    i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

    # Get the independent indeces
    new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
    new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

    # Get the independent freqeuncies
    w_a = self.w[new_i_a]
    w_b = self.w[new_i_b]

    n_a = np.zeros(np.shape(len(w_a)), dtype = TYPE_DP)
    n_b = np.zeros(np.shape(len(w_a)), dtype = TYPE_DP)

    if self.T > 0:
        n_a = 1 / (np.exp( w_a / np.double(self.T / __RyToK__)) - 1)
        n_b = 1 / (np.exp( w_b / np.double(self.T / __RyToK__)) - 1)

    # Get all the quantities to make the change of variables
    X         = ((1 + 2 * n_a) * (1 + 2 * n_b) /8)
    w2_on_X   = (w_a * w_b) / X
    chi_minus = ((w_a - w_b) * (n_a - n_b)) /(2 * w_a * w_b)
    chi_plus  = ((w_a + w_b) * (1 + n_a + n_b)) /(2 * w_a * w_b)

    if get_alpha:
        # Now rescale a'^(1) 
        new_a =  w2_on_X * np.sqrt(- 0.5 * chi_minus) * a_all
        # Now rescale b'^(1)
        new_b =  w2_on_X * np.sqrt(+ 0.5 * chi_plus)  * b_all

        # Prepare the result
        # This is Y(1)
        alpha1 = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

        # Start filling
        start = 0
        next = self.n_modes
        for i in range(self.n_modes):
            # Get the difference of the rescaled tensor
            alpha1[i, i:] = new_a[start : next] - new_b[start : next]
            start = next 
            next = start + self.n_modes - i - 1 

            # Fill symmetric
            alpha1[i, :i] = alpha1[:i, i]

        return alpha1      
    else:
        # Now rescale a'^(1) 
        new_a =  (np.sqrt(- 0.5 * chi_minus) /X) * a_all
        # Now rescale b'^(1)
        new_b =  (np.sqrt(+ 0.5 * chi_plus)  /X) * b_all

        # Prepare the result
        beta1 = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)

        # Start filling
        start = 0
        next = self.n_modes
        for i in range(self.n_modes):
            # Get the sum of the rescaled tensor
            beta1[i, i:] = new_a[start : next] + new_b[start : next]
            start = next 
            next = start + self.n_modes - i - 1 

            # Fill symmetric
            beta1[i, :i] = beta1[:i, i]

        return beta1

get_Y1(half_off_diagonal=False)

Get the perturbation on the Y matrix from the psi vector. This is used in the standard code.

Source code in tdscha/DynamicalLanczos.py
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
def get_Y1(self, half_off_diagonal = False):
    """
    Get the perturbation on the Y matrix from the psi vector.
    This is used in the standard code.
    """
    start_Y = self.n_modes
    start_A = self.n_modes +  (self.n_modes * (self.n_modes + 1)) // 2

    Y_all = self.psi[start_Y : start_A]

    Y1 = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)

    start = 0
    next = self.n_modes
    for i in range(self.n_modes):
        Y1[i, i:] = Y_all[start : next]
        start = next 
        next = start + self.n_modes - i - 1 

        # Fill symmetric
        Y1[i, :i] = Y1[:i, i]

    # Normalize each term outside the diagonal
    if half_off_diagonal:
        norm_mask = np.ones((self.n_modes, self.n_modes), dtype = np.double) / 2
        np.fill_diagonal(norm_mask, 1)

        Y1 *= norm_mask


    return  Y1

get_ReA1(half_off_diagonal=False)

Get the perturbation on the ReA matrix from the psi vector.

If half_the_off_diagonal is true, divide by 2 the off-diagonal elements

Source code in tdscha/DynamicalLanczos.py
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
def get_ReA1(self, half_off_diagonal = False):
    """
    Get the perturbation on the ReA matrix from the psi vector.

    If half_the_off_diagonal is true, divide by 2 the off-diagonal elements
    """
    start_A = self.n_modes +  (self.n_modes * (self.n_modes + 1)) // 2

    ReA_all = self.psi[start_A:]

    ReA1 = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)
    start = 0
    next = self.n_modes
    for i in range(self.n_modes):
        ReA1[i, i:] = ReA_all[start : next]
        start = next 
        next = start + self.n_modes - i - 1 

        # Fill symmetric
        ReA1[i, :i] = ReA1[:i, i]

    # Normalize each term outside the diagonal
    if half_off_diagonal:
        norm_mask = np.ones((self.n_modes, self.n_modes), dtype = np.double) / 2
        np.fill_diagonal(norm_mask, 1)

        ReA1 *= norm_mask

    return  ReA1

apply_anharmonic_FT(transpose=False, test_weights=True, use_old_version=False)

APPLY ANHARMONIC EVOLUTION

This term involves the anharmonic evolution: This calculates self-consistently the anharmonic evolution from the vector.

NORMAL: this method applies -L_anharm (see Eq. (K4) in the Appendix of the Monacelli PRB): :: math . \begin{bmatrix} 0 & -X'' & 0 \ -Z & -X & 0 \ -Z' & -X' & 0 \end{bmatrix} on the follwoing vector: :: math . \begin{bmatrix} \mathcal{R}^{(1)} \ \tilde{\Upsilon}^{(1)} \ \Re \tilde{A}^{(1)}. \end{bmatrix}

WIGNER: this method applies +L_anh: :: math. \mathcal{L}{anh} \begin{bmatrix} \tilde{\mathcal{R}}^{(1)}\mu\ \ \tilde{a}'^{(1)}{\mu\nu}\ \ \tilde{b}'^{(1)}{\mu\nu} \end{bmatrix} = \begin{bmatrix} -\left\langle \frac{\partial \mathbb{V}}{\partial \tilde{Q}\alpha}\right\rangle{(1)}\ \ +\sqrt{-\frac{1}{2}\tilde{\chi}{\alpha\beta}^{-}} \left\langle \frac{\partial^2 \mathbb{V}}{\partial \tilde{Q}\alpha \partial \tilde{Q}\beta}\right\rangle{(1)}\ \ -\sqrt{\frac{1}{2}\tilde{\chi}{\alpha\beta}^{+}} \left\langle \frac{\partial^2 \mathbb{V}}{\partial \tilde{Q}\alpha \partial \tilde{Q}\beta}\right\rangle{(1)} \end{bmatrix}

Results:
-final_psi: np.array with shape = n_modes + n_modes * (n_modes + 1)
Source code in tdscha/DynamicalLanczos.py
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
    def apply_anharmonic_FT(self, transpose = False, test_weights = True, use_old_version = False):
        r"""
        APPLY ANHARMONIC EVOLUTION
        ==========================

        This term involves the anharmonic evolution:
        This calculates self-consistently the anharmonic evolution from the vector.

        NORMAL: this method applies -L_anharm (see Eq. (K4) in the Appendix of the Monacelli PRB): 
        :: math .
            \begin{bmatrix}
             0   &  -X''  &  0 \\
            -Z   &  -X    & 0 \\
            -Z'  &  -X'   & 0
            \end{bmatrix}
        on the follwoing vector:
        :: math .
            \begin{bmatrix}
            \mathcal{R}^{(1)} \\
            \tilde{\Upsilon}^{(1)} \\
            \Re \tilde{A}^{(1)}.
            \end{bmatrix}

        WIGNER: this method applies +L_anh:
        :: math.
            \mathcal{L}_{anh} 
            \begin{bmatrix}
            \tilde{\mathcal{R}}^{(1)}_\mu\\ \\
            \tilde{a}'^{(1)}_{\mu\nu}\\ \\
            \tilde{b}'^{(1)}_{\mu\nu}
            \end{bmatrix} 
            = 
            \begin{bmatrix}
            -\left\langle \frac{\partial \mathbb{V}}{\partial \tilde{Q}_\alpha}\right\rangle_{(1)}\\ \\
            +\sqrt{-\frac{1}{2}\tilde{\chi}_{\alpha\beta}^{-}}
            \left\langle \frac{\partial^2 \mathbb{V}}{\partial \tilde{Q}_\alpha \partial \tilde{Q}_\beta}\right\rangle_{(1)}\\ \\
            -\sqrt{\frac{1}{2}\tilde{\chi}_{\alpha\beta}^{+}}
            \left\langle \frac{\partial^2 \mathbb{V}}{\partial \tilde{Q}_\alpha \partial \tilde{Q}_\beta}\right\rangle_{(1)}
            \end{bmatrix}

        Parameters
        ----------
            -transpose : bool
                If True, the transpose of L is computed.
            -test_weights : bool
                If True, the weights are tested against those computed with finite differences
                It is time consuming, activate only for debugging
            -use_old_version: bool
                If true, it employes an old version of the subroutine that does not satisfy the permutation symmetry.
                Use this option only for testing purpouses.

        Results:
        -------
            -final_psi: np.array with shape = n_modes + n_modes * (n_modes + 1)
        """
        #print("Starting with psi:", self.psi)

        # Get the perturbation R^(1)
        R1 = self.psi[: self.n_modes]

        if not self.use_wigner:
#             print('[NORMAL]: get Y(1)')
            # Get the perturbation Upsilon^(1)
            Y1 = self.get_Y1(half_off_diagonal = transpose)
        else:
#             print('[WIGNER]: get Y(1)')
            # Use the Wigner equations
            # Upsilon^(1) = alpha^(1)
            Y1 = self.get_alpha1_beta1_wigner(get_alpha = True)

        # Weights to perform the pertrubed average
        weights = np.zeros(self.N, dtype = np.double)

        if not self.use_wigner:
#             print('[NORMAL]: get static egivals')
            # The standard code
            # Create the multiplicative matrices for the rest of the anharmonicity
            n_mu = 0
            if self.T > __EPSILON__:
                n_mu = 1.0 /(np.exp(self.w * 157887.32400374097 / self.T) - 1.0)
            # Eigenvalues of Upsilon and ReA at equilibrium
            Y_w   = 2 * self.w / (2 * n_mu + 1)
            ReA_w = 2 * self.w * n_mu * (n_mu + 1) / (2*n_mu + 1)

            # Check if we must compute the transpose
            if transpose:
                ReA1 = self.get_ReA1(half_off_diagonal = transpose)

                # The equation is
                # Y^(1)_new = 2 Ya Yb^2 Y^(1) + 2 Yb Ya^2 Y^(1)
                coeff_Y = np.einsum("a, b, b -> ab", Y_w, Y_w, Y_w)
                coeff_Y += np.einsum("a, a, b -> ab", Y_w, Y_w, Y_w)
                coeff_Y *= 2

                coeff_RA = np.einsum("a, b, b -> ab", Y_w, ReA_w, Y_w)
                coeff_RA += np.einsum("a, a, b -> ab", Y_w, ReA_w, Y_w)
                coeff_RA *= 2

                # Get the new perturbation
                Y1_new = Y1 * coeff_Y + ReA1 * coeff_RA

                # Override the old perturbation
                Y1 = Y1_new    
        else:
#             print('[WIGNER]: get static egivals')
            # We are using Wigner equations
            # Get the chi +/- array (n_modes, n_modes)
            chi_minus = self.get_chi_minus()
            chi_plus  = self.get_chi_plus()

#             print('chi_minus = ')
#             print(chi_minus)
#             print('chi plus = ')
#             print(chi_plus)


        #print("X:", self.X)
        #print("w:", self.w)
        #print ("R1:", R1)
        #print("Y1:", Y1)
        #print("T:", self.T)

        # Compute the perturbed average of BO potential in the polarization basis
        f_pert_av   = np.zeros(self.n_modes, dtype = np.double)
        d2v_pert_av = np.zeros((self.n_modes, self.n_modes), dtype = np.double, order = "C")

        # Check if you need to compute the fourth order
        apply_d4 = 1
        if self.ignore_v4:
            print('Removing D4')
            apply_d4 = 0

        # NEW: we can remove the D3 effect
        if self.ignore_v3:
            print('Removing D3')
            R1[:] = 0.

        # Prepare the symmetry variables for the C code
        # deg_space_new = np.zeros(np.sum(self.N_degeneracy), dtype = np.intc)
        # i = 0
        # i_mode = 0
        # j_mode = 0
        # #print("Mapping degeneracies:", np.sum(n_degeneracies))
        # while i_mode < self.n_modes:
        #     #print("cross_modes: ({}, {}) | deg_i = {}".format(i_mode, j_mode, n_degeneracies[i_mode]))
        #     deg_space_new[i] = self.degenerate_space[i_mode][j_mode]
        #     j_mode += 1
        #     i += 1
        #     if j_mode == self.N_degeneracy[i_mode]:
        #         i_mode += 1
        #         j_mode = 0


        # Compute the perturbed averages (the time consuming part is HERE !!!)
        #print("Entering in get pert...")
        _t_pert_start = time.time()
        n_syms, _, _ = np.shape(self.symmetries[0])
        #print("DEG:")
        #print(self.degenerate_space)

        # OLD Implementation still working
        if self.mode in (MODE_FAST_MPI, MODE_FAST_SERIAL): 
            # Get the pertrubed averages
            sscha_HP_odd.GetPerturbAverageSym(self.X, self.Y, self.w, self.rho, R1, Y1, self.T, apply_d4, n_syms,
                                              self.symmetries, self.N_degeneracy, self.degenerate_space ,self.sym_block_id, 
                                              f_pert_av, d2v_pert_av)

        elif self.mode == MODE_FAST_JULIA:
            if not __JULIA_EXT__:
                raise ImportError("Error while importing julia. Try with python-jl after pip install julia.")

            if self.sym_julia is None:
                MSG = "Error, the initialization must be called AFTER you change mode to JULIA."
                raise ValueError(MSG)


            # Prepare the combined parallelization function
            # Pack both results (f vector + d2v matrix) into a single flat array
            # to use GoParallel with "+" reduction (avoids GoParallelTuple bug)
            n_modes = self.n_modes
            def get_combined_proc(start_end):
                start = int(start_end[0])
                end   = int(start_end[1])
                result = julia.Main.get_perturb_averages_sym(
                    self.X.T, self.Y.T, self.w, self.rho, R1, Y1,
                    np.float64(self.T), bool(apply_d4),
                    self.sym_julia, self.N_degeneracy, self.deg_julia,
                    self.sym_block_id, start, end)
                return np.concatenate([result[0], result[1].ravel()])

            # Divide the configurations and symmetries on different processors (Here we get the range of work for each process)
            n_total = self.n_syms * self.N
            n_processors = Parallel.GetNProc()
            count = n_total // n_processors
            remainer = n_total % n_processors

            # Assign which configurations should be computed by each processor
            indices = []
            for rank in range(n_processors):

                if rank < remainer:
                    start = np.int64(rank * (count + 1))
                    stop = np.int64(start + count + 1)
                else:
                    start = np.int64(rank * count + remainer)
                    stop = np.int64(start + count)

                indices.append([start + 1, stop])


            # Execute the combined call on each processor in parallel
            combined = Parallel.GoParallel(get_combined_proc, indices, "+")
            f_pert_av = combined[:n_modes]
            d2v_pert_av = combined[n_modes:].reshape(n_modes, n_modes)

        else:
            raise ValueError("Error, mode running {} not implemented.".format(self.mode))

        _t_pert_end = time.time()
        if self.verbose:
            print("Time for perturb averages (n_syms={}): {:.6f} s".format(n_syms, _t_pert_end - _t_pert_start))

        # Gamma-only: impose translational symmetry on the perturbed averages
        if self.gamma_only and self.trans_projector is not None:
            _t_trans_start = time.time()
            # Project f_pert_av: P_trans @ f
            f_pert_av = self.trans_projector @ f_pert_av

            # Project d2v_pert_av: (1/n_cells) Σ_R T_R @ d2v @ T_R^T
            n_cells = len(self.trans_operators)
            d2v_proj = np.zeros_like(d2v_pert_av)
            for T_R in self.trans_operators:
                d2v_proj += T_R @ d2v_pert_av @ T_R.T
            d2v_pert_av = d2v_proj / n_cells
            _t_trans_end = time.time()
            if self.verbose:
                print("Time for translational projection: {:.6f} s".format(_t_trans_end - _t_trans_start))

        # OLD PART OF THE CODE
        #print("D2V:")
        #np.set_printoptions(threshold = 10000)
        #print(d2v_pert_av[:10, :10])#print("Out get pert")

#         print("R1 = {}".format(R1))
#         print("Y1 = {}".format(Y1))
#         print("<f> pert = {}".format(f_pert_av))
#         print("<d2v/dr^2> pert = {}".format(d2v_pert_av))
#         print("<d2v/dr^2>T pert = {}".format(d2v_pert_av.T))
#         print("<d2v/dr^2>T - <d2v/dr^2>  pert = {}".format(d2v_pert_av.T - d2v_pert_av))
#         print()

        # Compute the average with the OLD VERSION
        if use_old_version:
            # Get the weights of the perturbation (psi vector)
            sscha_HP_odd.GetWeights(self.X, self.w, R1, Y1, self.T, weights)

            if test_weights:
                other_weights = get_weights_finite_differences(self.X, self.w, self.T, R1, Y1)

                # There is a constant factor that should come from the normalization
                # But this does not depend on the configuration
                shift = weights - other_weights 

                # Remove the constant shift coming from renormalization
                # 1/2 Tr (Y^-1 * Y^{(1)})
                shift -= np.mean(shift) 

                # Compare the weights
                disp = np.max(np.abs(shift))
                dispersion = np.std(weights)

                if disp / dispersion >= 1e-3:
                    print("Perturbation:")
                    print(self.psi)

                    print("Weights with C:")
                    print(weights)

                    print()
                    print("Weights by finite differences:")
                    print(other_weights)

                    print()
                    print("Shifts:")
                    print(weights - other_weights)

                    print()
                    print("Discrepancies (max = {}):".format(disp))
                    i_value = np.argmax(np.abs(shift))
                    print(shift)
                    print("I value of max: {}".format(i_value))

                    print("")
                    print("sigma = {}".format(dispersion))
                    print("Weights[{}] = {}".format(i_value, weights[i_value]))
                    print("OtherWeights[{}] = {}".format(i_value, other_weights[i_value]))


                assert disp / dispersion < 1e-3, "Error, the weights computed with the C did not pass the test"

            #print("Weights:", weights)

            # Get the averages on the perturbed ensemble
            w_is = np.tile(self.rho, (self.n_modes, 1)).T
            w_1 = np.tile(weights, (self.n_modes, 1)).T

            #print("rho shape:", np.shape(self.rho))
            #print("Shape w_is:", np.shape(w_is))

            # The force average
            avg_numbers = self.Y * w_is *  w_1 #np.einsum("ia, i, i -> ia", self.Y, w_is, w_1)
            #print("Shape Y:", np.shape(avg_numbers))
            f_pert_av = np.sum(avg_numbers, axis = 0) / self.N_eff


            #print("Shape F:", np.shape(f_pert_av))
            sscha_HP_odd.Get_D2DR2_PertV(self.X, self.Y, self.w, self.rho, weights, self.T, d2v_pert_av)


            #print("<f> pert = {}".format(f_pert_av))
            #print("<d2v/dr^2> pert = {}".format(d2v_pert_av))
            #print()

        # END OF THE OLD VERSION


        # Get the final vector
        final_psi = np.zeros(self.psi.shape, dtype = np.double)

        # Now get the perturbation for R^(1) (same in Wigner)
        final_psi[:self.n_modes] =  f_pert_av

        if not self.use_wigner:
#             print('[NORMAL]: anharmonic Y(1) ReA(1)')
            # Propagation for Upsilon^(1) and ReA^(1)
            if not transpose:
                # Get the perturbation D2 * Upsilon + Upsilon * D2
                pert_Y  = np.einsum("ab, a ->ab", d2v_pert_av, Y_w)
                pert_Y += np.einsum("ab, b ->ab", d2v_pert_av, Y_w)

                # Get the perturbation D2 * Re A +  Re A * D2
                pert_RA  = np.einsum("ab, a ->ab", d2v_pert_av, ReA_w)
                pert_RA += np.einsum("ab, b -> ab", d2v_pert_av, ReA_w)
            else:
                Y_inv = 1 / Y_w
                pert_Y = 0.5 * np.einsum("a, ab, b -> ab", Y_inv, d2v_pert_av, Y_inv)
                pert_RA = np.zeros(pert_Y.shape, dtype = np.double)

                # Now double the off diagonal values of pert_Y and pert_RA
                # This is to take into account the symmetric storage of psi
                sym_mask = np.ones(pert_Y.shape) * 2 
                np.fill_diagonal(sym_mask, 1) 
                pert_Y  *= sym_mask 
                pert_RA *= sym_mask
        else:
#             print("[WIGNER]: anharmonic a(1)' b(1)'")
            # We are using Wigner equations
            # Propagation for a'^(1)
            pert_Y  = np.einsum('ab, ab -> ab', +np.sqrt(-0.5 * chi_minus), d2v_pert_av)
            # Propagation for b'^(1)
            pert_RA = np.einsum('ab, ab -> ab', -np.sqrt(+0.5 * chi_plus),  d2v_pert_av)


#         print('pert_R = ')
#         print(f_pert_av)
#         print('pert_RA = ')
#         print(pert_RA)

        #####################
        # Update the vector #
        #####################

        # Note: the code deals with symmetric matrices in the following way.
        # Given a matrix you take the row on the right from the (1,1) element,
        # then you take the row on the right from the (2,2) element,
        # then you proceed with the (3,3) element.

        # Now get the perturbation for Upsilon^(1)/a'^(1)
        current = self.n_modes
        for i in range(self.n_modes):
            final_psi[current : current + self.n_modes - i] = pert_Y[i, i:]
            current = current + self.n_modes - i

#         print('final psi = ')
#         print(final_psi)

        # Now process the pertrubation of ReA^(1)/b'^(1)
        for i in range(self.n_modes):
            final_psi[current : current + self.n_modes - i] = pert_RA[i, i:]
            current = current + self.n_modes - i

#         print('final psi = ')
#         print(final_psi)

        # print("First element of pert_Y:", pert_Y[0,0])
        # print("Y_w = ", Y_w)
        # print("All pert Y:")
        # print(pert_Y)

        # print("Final psi:")
        # print(final_psi[self.n_modes: self.n_modes + 10])

        #print("Output:", final_psi)
        if not self.use_wigner:
#             print('[NORMAL]: anharmonic final')
            return -final_psi
        else:
#             print('[WIGNER]: anharmonic final')
            return +final_psi

apply_anharmonic_static()

Compute the anharmonic part of the L matrix for the static Hessian calculation.

Source code in tdscha/DynamicalLanczos.py
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
def apply_anharmonic_static(self):
    """
    Compute the anharmonic part of the L matrix for the static Hessian calculation.
    """ 

    Y1 = self.get_Y1()
    R1 = self.psi[: self.n_modes]


    # Create the Y matrix
    n_mu = 0
    if self.T > __EPSILON__:
        n_mu = 1.0 / ( np.exp(self.w * np.double(157887.32400374097) / self.T) - 1.0)
    Y_w = 2 * self.w / (2 * n_mu + 1)

    # prepare the modified of Y1
    Y1 = -2 * np.einsum("ab, a, b -> ab", Y1, Y_w, Y_w)

    # Compute the average SSCHA force and potential
    f_pert_av = np.zeros(self.n_modes, dtype = np.double)
    d2v_pert_av = np.zeros((self.n_modes, self.n_modes), dtype = np.double, order = "C")

    # Check if you need to compute the fourth order
    apply_d4 = 1
    if self.ignore_v4:
        apply_d4 = 0

    # Prepare the symmetry variables for the C code
    deg_space_new = np.zeros(np.sum(self.N_degeneracy), dtype = np.intc)
    i = 0
    i_mode = 0
    j_mode = 0
    #print("Mapping degeneracies:", np.sum(n_degeneracies))
    while i_mode < self.n_modes:
        #print("cross_modes: ({}, {}) | deg_i = {}".format(i_mode, j_mode, n_degeneracies[i_mode]))
        deg_space_new[i] = self.degenerate_space[i_mode][j_mode]
        j_mode += 1
        i += 1
        if j_mode == self.N_degeneracy[i_mode]:
            i_mode += 1
            j_mode = 0


    # Compute the perturbed averages (the time consuming part is HERE)
    #print("Entering in get pert...")
    sscha_HP_odd.GetPerturbAverageSym(self.X, self.Y, self.w, self.rho, R1, Y1, self.T, apply_d4, 
                                      self.symmetries, self.N_degeneracy, deg_space_new, 
                                      f_pert_av, d2v_pert_av)

    # Get the final vector
    final_psi = np.zeros(self.psi.shape, dtype = np.double)
    final_psi[:self.n_modes] =  - f_pert_av

    # Now get the perturbation on the vector
    current = self.n_modes
    for i in range(self.n_modes):
        final_psi[current : current + self.n_modes - i] = d2v_pert_av[i, i:]
        current = current + self.n_modes - i


    return final_psi

apply_L2()

APPLY THE L2

L2 is the part of the L operators that mixes the two spaces. It involves the phi3 matrix.

Source code in tdscha/DynamicalLanczos.py
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
def apply_L2(self):
    """
    APPLY THE L2
    ============

    L2 is the part of the L operators that mixes the two spaces.
    It involves the phi3 matrix.
    """

    if self.ignore_v3:
        return np.zeros(np.shape(self.psi), dtype = TYPE_DP)


    w_a = np.tile(self.w, (self.n_modes, 1)).ravel()
    w_b = np.tile(self.w, (self.n_modes, 1)).T.ravel()

    vector = self.psi[:self.n_modes]
    dyn = self.psi[self.n_modes:]
    new_dyn = -dyn * np.sqrt( (w_a + w_b)/(w_a*w_b)) / 2

    # Here the time consuming part
    if self.mode == 0:
        # DEBUGGING PYTHON VERSION (SLOW)
        out_v = SlowApplyD3ToDyn(self.X, self.Y, self.rho, self.w, self.T, new_dyn)
        out_d = SlowApplyD3ToVector(self.X, self.Y, self.rho, self.w, self.T, vector)
    elif self.mode >= 1:
        out_v = FastApplyD3ToDyn(self.X, self.Y, self.rho, self.w, self.T, new_dyn, self.symmetries,
                                 self.N_degeneracy, self.degenerate_space, self.mode)
        out_d = FastApplyD3ToVector(self.X, self.Y, self.rho, self.w, self.T, vector, self.symmetries,
                                    self.N_degeneracy, self.degenerate_space, self.mode)
    else:
        print("Error, mode %d not recognized." % self.mode)
        raise ValueError("Mode not recognized %d" % self.mode)

    out_d *= -np.sqrt( (w_a + w_b)/(w_a*w_b)) / 2

    out_vect = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

    out_vect[:self.n_modes] = out_v
    out_vect[self.n_modes:] = out_d
    return out_vect

apply_L2_FT(transpose=False)

Apply the full matrix at finite temperature.

Source code in tdscha/DynamicalLanczos.py
3465
3466
3467
3468
3469
3470
3471
3472
3473
def apply_L2_FT(self, transpose = False):
    """
    Apply the full matrix at finite temperature.
    """ 

    if self.ignore_v3:
        return np.zeros(self.psi.shape, dtype = TYPE_DP)

    return FastD3_FT(self.X, self.Y, self.rho, self.w, self.T, self.psi, self.symmetries, self.N_degeneracy, self.degenerate_space, self.mode, transpose)

apply_L3()

APPLY THE L3

This is the last part of the L matrix, it puts in communication the dyn part of psi with herselfs.

Source code in tdscha/DynamicalLanczos.py
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
def apply_L3(self):
    """
    APPLY THE L3
    ============

    This is the last part of the L matrix, it puts in communication 
    the dyn part of psi with herselfs.
    """

    w_a = np.tile(self.w, (self.n_modes, 1)).ravel()
    w_b = np.tile(self.w, (self.n_modes, 1)).T.ravel()

    simple_output = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

    #simple_output[self.n_modes:] = self.psi[self.n_modes:] * (w_a + w_b)**2

    if self.ignore_v4:
        return simple_output

    # Apply the D4

    dyn = self.psi[self.n_modes:] * np.sqrt((w_a + w_b) / (w_a * w_b)) / 2



    # Here the time consuming part [The most of all]!!!
    if self.mode == 0:
        # A very slow implementation
        # Use it just for debugging
        out_dyn = SlowApplyD4ToDyn(self.X, self.Y, self.rho, self.w, self.T, dyn)
    elif self.mode >= 1:
        # The fast C implementation
        #print ("Inside v4 MPI, this will take a while")
        out_dyn = FastApplyD4ToDyn(self.X, self.Y, self.rho, self.w, self.T, dyn,
                                   self.symmetries, self.N_degeneracy, self.degenerate_space, self.mode)       

    out_dyn *= np.sqrt((w_a + w_b) / (w_a * w_b)) / 2

    output = np.zeros(np.shape(self.psi), dtype = TYPE_DP)
    output[self.n_modes:] = out_dyn

    output += simple_output

    return output

apply_L3_FT(transpose=False)

APPLY THE L3

This is the last part of the L matrix, it puts in communication the dyn part of psi with herselfs.

Source code in tdscha/DynamicalLanczos.py
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
def apply_L3_FT(self, transpose = False):
    """
    APPLY THE L3
    ============

    This is the last part of the L matrix, it puts in communication 
    the dyn part of psi with herselfs.
    """

    simple_output = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

    #simple_output[self.n_modes:] = self.psi[self.n_modes:] * (w_a + w_b)**2

    if not self.ignore_v4:
        simple_output[:] = FastD4_FT(self.X, self.Y, self.rho, self.w, self.T, self.psi, self.symmetries, self.N_degeneracy, self.degenerate_space, self.mode, transpose)


    return simple_output

apply_full_L(target=None, force_t_0=False, force_FT=True, transpose=False, fast_lanczos=True)

APPLY THE L

This function applies the L operator to the specified target vector. The target vector is first copied into the local psi, and the computed. NOTE: This function will overwrite the current psi with the specified target.

Returns:
-self.psi: the updated vector
Source code in tdscha/DynamicalLanczos.py
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
def apply_full_L(self, target = None, force_t_0 = False, force_FT = True, transpose = False, fast_lanczos = True):
    """
    APPLY THE L 
    ===========

    This function applies the L operator to the specified target vector.
    The target vector is first copied into the local psi, and the computed.
    NOTE: This function will overwrite the current psi with the specified
    target.

    Parameters
    ----------
        target : ndarray ( size = shape(self.psi)), optional
            The garget vector to which you want to apply the
            full L matrix
        force_t_0 : bool
            If False (default) the temperature is looked to chose if use the T = 0 or the finite temperature.
            If True it is forced the T=0 method (This will lead to wrong results at finite temperature).
        force_FT : bool
            If True the finite temperature method is forced even if T = 0.
            The results should be good, use it for testing.
            NOTE: only one between force_t_0 and force_FT should be true
        fast_lanczos : bool
            If true this method applies the L2 and L3 using the self-consistent way.
            This is much quicker, but needs to be tested
        transpose : bool
            Default is False, if it is true we apply the transpose
        fast_lanczos : bool
            See force_t_0 for details.

    Returns:
    -------
        -self.psi: the updated vector

    """
    if force_t_0 and force_FT:
        raise ValueError("Error, only one between force_t_0 and force_FT can be True")

    # Setup the target vector to the self.psi
    if not target is None:
        self.psi = target 

    # Check the initialization
    if not self.initialized:
        raise ValueError("Error, this class must be initialized before lunching the computation:\n Use the .init()")

    #if self.symmetrize:
    #    self.symmetrize_psi()

    #print("Psi before:")
    #print(self.psi[:self.n_modes])


    # Apply the whole L step by step to self.psi
    t1 = timer()
    if (force_t_0 or self.T < __EPSILON__) and not force_FT:
        # Harmonic evolution
        output = self.apply_L1()
    else:
        #HARMONIC evolution at finite temperature
        output = self.apply_L1_FT(transpose)
    t2 = timer()

    # Apply the quick_lanczos
    t4 = timer()
    if fast_lanczos and (not self.ignore_v3):
        # AN-HARMONIC evolution finite temperature
        output += self.apply_anharmonic_FT(transpose)
        t3 = timer()
        t4 = t3
    # else:
    #     if (force_t_0 or self.T < __EPSILON__) and not force_FT:
    #         output += self.apply_L2()
    #     else:
    #         output += self.apply_L2_FT(transpose)
    #     t3 = timer()
    #     if (force_t_0 or self.T < __EPSILON__) and not force_FT:
    #         output += self.apply_L3()
    #     else:
    #         output += self.apply_L3_FT(transpose)
    #     t4 = timer()

    if self.verbose:
        print("Time to apply the full L: {}".format(t4 - t1))
        print("  Harmonic part (L1): {:.6f} s".format(t2 - t1))
        print("  Anharmonic part:    {:.6f} s".format(t4 - t2))

    # Apply the shift reverse
    #print ("Output before:")
    #print (output[:self.n_modes])
    if self.reverse_L:
        output *= -1
    output += self.shift_value * self.psi
    #print ("Output after:")
    #print (output[:self.n_modes])

    # Now return the output
    #print ("out just before return:", output[0])
    self.psi = output
    #if self.symmetrize:
    #    self.symmetrize_psi()

    return self.psi

save_abc(file)

Save only the a, b, and c coefficients from the lanczos. In this way the calculation cannot be restarted.

Source code in tdscha/DynamicalLanczos.py
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
def save_abc(self, file):
    """
    Save only the a, b, and c coefficients from the lanczos.
    In this way the calculation cannot be restarted.
    """

    total_len = len(self.c_coeffs)

    abc = np.zeros( (total_len, 3), dtype = np.double)
    abc[:,0] = self.a_coeffs[:total_len]
    abc[:,1] = self.b_coeffs
    abc[:,2] = self.c_coeffs

    np.savetxt(file, abc, header = "a; b; c")

load_abc(file)

Load only the a, b, and c coefficients from the ".abc" file

Source code in tdscha/DynamicalLanczos.py
3659
3660
3661
3662
3663
3664
3665
3666
3667
def load_abc(self, file):
    """
    Load only the a, b, and c coefficients from the ".abc" file
    """

    abc = np.loadtxt(file)
    self.a_coeffs = abc[:,0]
    self.b_coeffs = abc[:,1]
    self.c_coeffs = abc[:,2]

save_status(file)

Save the current data in npz compressed format, in order to reanalyze easily the result (or restart the Lanczos) later.

Source code in tdscha/DynamicalLanczos.py
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
def save_status(self, file):
    """
    Save the current data in npz compressed format, in order to reanalyze easily the result (or restart the Lanczos)
    later.

    Parameters
    ----------
        file : string
            Path to where you want to save the data. It must be an npz binary format. The extension
            will be added if it does not match the npz one
    """
    # Force all the processes to be here
    Parallel.barrier()

    # Add the correct extension
    if not ".npz" in file.lower():
        file += ".npz"

    # Save all the data
    if Parallel.am_i_the_master():
        np.savez_compressed(file, 
                            T = self.T,
                            nat = self.nat,
                            m = self.m,
                            w = self.w,
                            pols = self.pols,
                            n_modes = self.n_modes,
                            ignore_v3 = self.ignore_v3,
                            ignore_v4 = self.ignore_v4,
                            N = self.N,
                            rho = self.rho,
                            X = self.X,
                            Y = self.Y,
                            psi = self.psi,
                            a_coeffs = self.a_coeffs,
                            b_coeffs = self.b_coeffs,
                            c_coeffs = self.c_coeffs,
                            basis_Q = self.basis_Q,
                            basis_P = self.basis_P,
                            s_norm = self.s_norm,
                            krilov_basis = self.krilov_basis,
                            arnoldi_matrix = self.arnoldi_matrix,
                            reverse = self.reverse_L,
                            shift = self.shift_value,
                            perturbation_modulus = self.perturbation_modulus,
                            use_wigner = self.use_wigner,
                            ignore_small_w = self.ignore_small_w,
                            sym_julia = self.sym_julia,
                            deg_julia = self.deg_julia,
                            n_syms = self.n_syms)

load_status(file, is_file_instance=False)

Load a previously saved status from the speficied npz file. The file must be saved with save_status.

If is_file_instance is True, the file is assumed to be already loaded with open(...)

Source code in tdscha/DynamicalLanczos.py
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
def load_status(self, file, is_file_instance = False):
    """
    Load a previously saved status from the speficied npz file.
    The file must be saved with save_status.

    If is_file_instance is True, the file is assumed to be already loaded with open(...)
    """

    # Force all the process to be here
    Parallel.barrier()

    if not is_file_instance:
        # Add the correct extension
        if not ".npz" in file.lower():
            file += ".npz"

        # Check if the provided file exists
        if not os.path.exists(file):
            print ("Error while loading %s file.\n" % file)
            raise IOError("Error while loading %s" % file)

    data = {}
    # Read the data only with the master
    if Parallel.am_i_the_master():

        # Fix the allow pickle error in numpy >= 1.14.4
        try:
            data = np.load(file, allow_pickle = True)
        except ValueError:
            print("Error in pickling the data")
            raise
        except:
            print("Error, while loading with allow_pickle (numpy version < 1.14.4?)")
            print("       numpy version = {}".format(np.__version__))
            print("Trying without pickling...")
            data = np.load(file) 

    # Now bcast to everyone
    data = Parallel.broadcast(dict(data))

    self.T = np.double(data["T"])
    self.nat = np.intc(data["nat"])
    self.m = data["m"]
    self.w = data["w"]
    self.pols = data["pols"]
    self.n_modes = np.intc(data["n_modes"])
    self.ignore_v3 = bool(data["ignore_v3"])
    self.ignore_v4 = bool(data["ignore_v4"])
    self.N = np.intc(data["N"])
    self.rho = data["rho"]
    self.X = data["X"]
    self.Y = data["Y"]
    self.psi = data["psi"]
    self.a_coeffs = data["a_coeffs"]
    self.b_coeffs = data["b_coeffs"]
    if "c_coeffs" in data:
        self.c_coeffs = data["c_coeffs"]
    # Make them as lists
    self.a_coeffs = list(self.a_coeffs)
    self.b_coeffs = list(self.b_coeffs)
    self.c_coeffs = list(self.c_coeffs)
    self.krilov_basis = data["krilov_basis"]
    self.arnoldi_matrix = data["arnoldi_matrix"]

    try:
        self.sym_julia = data["sym_julia"]
        self.deg_julia = data["deg_julia"]
        self.n_syms = data["n_syms"]
    except:
        print('ATTENTION THE JULIA VARIABLES  WERE NOT LOADED')
        self.sym_julia = None
        self.deg_julia = None
        self.n_syms = 1


    self.basis_Q = data["basis_Q"]
    self.basis_P = data["basis_P"]
    self.s_norm = data["s_norm"]

    self.use_wigner = data["use_wigner"]
    try:
        self.ignore_small_w = data["ignore_small_w"]
    except:
        self.ignore_small_w = False #data["ignore_small_w"]

    if "reverse" in data.keys():
        self.reverse_L = data["reverse"]
        self.shift_value = data["shift"]

    if "symmetries" in data.keys():
        self.symmetries = data["symmetries"]
        self.N_degeneracy = data["N_degeneracy"]
        self.initialized = data["initialized"]
        self.degenerate_space = data["degenerate_space"]

    if "perturbation_modulus" in data.keys():
        self.perturbation_modulus = data["perturbation_modulus"]


    # Prepare the L as a linear operator (Prepare the possibility to transpose the matrix)
    def L_transp(psi):
        return self.apply_full_L(psi, transpose= True)
    self.L_linop = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)),\
                                                      matvec = self.apply_full_L, rmatvec = L_transp, dtype = TYPE_DP)

    # Define the preconditioner
    def M_transp(psi):
        return self.apply_L1_inverse_FT(psi, transpose = True)
    self.M_linop = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)),\
                                                      matvec = self.apply_L1_inverse_FT, rmatvec = M_transp, dtype = TYPE_DP)

run_biconjugate_gradient(verbose=True, tol=0.0005, maxiter=1000, save_g=None, save_each=1, use_preconditioning=True, algorithm='bicgstab')

STATIC RESPONSE

Get the static response inverting the green function. This is performed by exploiting the bi-conjugate gradient algorithm.

Results
G_inv: ndarray(size = (n_modes, n_modes))
    This is the mass-rescaled free energy Hessian.
    Its eigenvalues are the static frequencies, that determine the structure stability.
Source code in tdscha/DynamicalLanczos.py
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
    def run_biconjugate_gradient(self, verbose = True, tol = 5e-4, maxiter = 1000, save_g = None, save_each = 1, use_preconditioning = True, algorithm = "bicgstab"):
        """
        STATIC RESPONSE
        ===============

        Get the static response inverting the green function.
        This is performed by exploiting the bi-conjugate gradient algorithm.

        Parameters
        ----------
            verbose : bool
                If true, print the status of the algorithm in standard output
            tol : float
                Tollerance of the biconjugate gradient algorithm 
            maxiter: int
                The maximum number of iterations for the biconjugate gradient.
            save_g: string
                Path to the file on which save the green function. 
                It is saved after a number of steps specified by save_each.
            save_each: int
                Determines after how many steps to save the green function.
            use_preconditioning: bool
                If true, uses the preconditioning to solve the gradient.
                The precondition is obtained by inverting analytically only the Harmonic propagation of the
                L matrix
            algorithm: string
                The algorithm used to invert the L matrix. One between:
                   - bicg : Conjugate Gradient (Default)
                   - bicgstab : Stabilized Conjugate Gradient.
                   - cg-minimize: Conjugate Gradient with preconditioned minimization.
                         This algorithm minimizes the auxiliary function
                         .. math::

                            f(x) = \frac 12 r H^-1 r

                        where :math:`r = Lx - b` and :math:`H = L^\dagger L`. 
                        The hessian :math:`H` is guessed neglecting interaction (as for the perfectly harmonic case).
                It will invoke the corresponding scipy subroutine

        Results
        -------
            G_inv: ndarray(size = (n_modes, n_modes))
                This is the mass-rescaled free energy Hessian.
                Its eigenvalues are the static frequencies, that determine the structure stability.
        """

        G_one_phonon = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)

        if verbose:
            print()
            print("====================")
            print("BICONJUGATE GRADIENT")
            print("====================")
            print()
            print("We compute the static response with the")
            print("Biconjugate gradient algorithm.")
            print()


        # Check if the symmetries has been initialized
        if not self.initialized:
            self.prepare_symmetrization()

        j = np.zeros(1, dtype = np.intc)
        x_old = self.psi.copy()
        for i in range(self.n_modes):
            if verbose:
                # Print the status
                print()
                print()
                print("NEW STEP")
                print("--------")
                print()
                print("i = {} / {}".format(i + 1, self.n_modes))
                print()

            # Setup the known vector
            self.psi = np.zeros(self.psi.shape, dtype = type(self.psi[0]))
            self.psi[i] = 1

            x_old[:] = self.psi
            j[0] = 0
            def callback(xk, x_old = x_old, j = j):
                if np.isnan(np.sum(xk)):
                    raise ValueError("Error, NaN value found during the Biconjugate Gradient.") 
                if verbose:
                    disp = sum( (xk - x_old)**2)
                    print("BCG STEP {} | solution changed by {} (tol = {})".format(j[0], disp, tol))
                    j[0] += 1
                    x_old[:] = xk

            # Prepare the preconditioning
            M_prec = None
            x0 = self.M_linop.matvec(self.psi)
            if use_preconditioning:
                M_prec = self.M_linop
                #x0 = M_prec.matvec(self.psi)

            # Run the biconjugate gradient
            t1 = time.time()
            if algorithm.lower() == "bicgstab":
                res, info = scipy.sparse.linalg.bicgstab(self.L_linop, self.psi, x0 = x0, tol = tol, maxiter = maxiter, callback=callback, M = M_prec)
            elif algorithm.lower() == "bicg":
                res, info = scipy.sparse.linalg.bicg(self.L_linop, self.psi, x0 = x0, tol = tol, maxiter = maxiter, callback=callback, M = M_prec)
            elif algorithm.lower() == "cg-minimize":
                # This algorithm minimizes f(x) = 1/2  (Lx - b) H^-1 (Lx - b)
                # where H is the matrix H = L^T L (so it is positive definite). 
                # We pick the H inverse as the inverse of the SSCHA harmonic solution.  
                # To find x and compute x = A^-1 b

                # Here we define the function that returns f(x) and its gradient
                def func(x, b):
                    # Apply
                    r = self.L_linop.matvec(x) 
                    r -= b 

                    # Apply the precondition H^-1 = (L^t L)^-1 => M M^t
                    if use_preconditioning:
                        Hinv_r = self.M_linop.rmatvec(r)
                        Hinv_r = self.M_linop.matvec(Hinv_r)
                    else:
                        Hinv_r = r

                    # Now we get the gradient
                    gradient = self.L_linop.rmatvec(Hinv_r) 

                    # We get the function
                    f = 0.5 * np.sum(r * Hinv_r)

                    if verbose:
                        print("Evaluated function: value = {} | norm gradient = {}".format(f, np.sum(gradient**2)))
                        print()

                    return f, gradient


                psi_vector = self.psi.copy() 

                # Setup the minimization parameters
                options = {"gtol" : tol, "maxiter" : maxiter, "disp" : verbose, "norm" : 2}

                # Start the minimization
                results = scipy.optimize.minimize(func, x0, args = (psi_vector), method = "bfgs", jac = True, options=options)

                # Get the number of iterations
                j[0] = results.nit

                # Check the success
                if results.success:
                    info = 0
                else:
                    info = 1

                if verbose:
                    print("Minimization terminated after {} evaluations.".format(results.nfev))

                # Get the result
                res = results.x.copy()
            else:
                raise ValueError("""
Error, algorithm type '{}' in subroutine run_biconjugate_gradient not implemented.
       the only supported algorithms are ['bicgstab', 'bicg']
""".format(algorithm))
            t2 = time.time()

            if  verbose:
                print()
                print("Time to solve the linear system: {} s".format(t2 - t1))
                print()

            # Check if the minimization converged
            assert info >= 0, "Error on input or breakdown of biconjugate gradient algorithm (info = {})".format(info)

            if info > 0:
                print("The biconjugate gradient (step {}) algorithm did not converge after {} iterations.".format(i+1, maxiter))
                print("Try to either reduce the tollerance or increase the number of iteriations")
                print()
            else:
                print("The biconjugate gradient converged after {} iterations.".format(j[0]))


            G_one_phonon[i, :] = res[:self.n_modes]
            if i % save_each == 0:
                if save_g is not None:
                    np.save(save_g, G_one_phonon)


        if verbose:
            print()
            print(" ================================== ")
            print(" THE BICONJUGATE GRADIENT CONVERGED ")
            print(" ================================== ")
            print()
            print()


        if save_g is not None:
            np.save(save_g, G_one_phonon)

        # Check the hermitianeity
        disp = np.max(np.abs(G_one_phonon - G_one_phonon.T))
        assert disp < 1e-4, "Error, the resulting one-phonon Green function is not Hermitian."

        # Force hermitianity
        G = 0.5 * (G_one_phonon + G_one_phonon.T)

        # Invert the green function to get the Hessian Matrix (mass-rescaled)
        G_inv = np.linalg.inv(G) 

        return G_inv

run_hessian_calculation(verbose=True, eigen_shift=1, tol=0.0005, max_iters=1000, save_g=None, save_each=1, use_preconditioning=True, algorithm='cg')

STATIC RESPONSE

Get the static suscieptibility with the conjugate gradient algorithm. Then the hessian matrix is obtained inverting the static suscieptibility.

Results
G_inv: ndarray(size = (n_modes, n_modes))
    This is the mass-rescaled free energy Hessian.
    Its eigenvalues are the static frequencies, that determine the structure stability.
Source code in tdscha/DynamicalLanczos.py
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
    def run_hessian_calculation(self, verbose = True, eigen_shift = 1, tol = 5e-4, max_iters = 1000, save_g = None, save_each = 1, use_preconditioning = True, algorithm = "cg"):
        r"""
        STATIC RESPONSE
        ===============

        Get the static suscieptibility with the conjugate gradient algorithm. 
        Then the hessian matrix is obtained inverting the static suscieptibility.

        Parameters
        ----------
            verbose : bool
                If true, print the status of the algorithm in standard output
            eigen_shift : float
                The eigenvalue problem is shifted by :math:`\lambda` as

                .. math ::

                    L' \rightarrow L\left[ P + (1 - P) \lambda\right]

                This does not change the final solution as we are interested in

                .. math ::

                    P L^{-1} P = P {L'}^{-1}P

                So, if the matrix is not positive definite, we can exploit this force int positive definite.

            tol : float
                Tollerance of the biconjugate gradient algorithm 
            maxiter: int
                The maximum number of iterations for the biconjugate gradient.
            save_g: string
                Path to the file on which save the green function. 
                It is saved after a number of steps specified by save_each.
            save_each: int
                Determines after how many steps to save the green function.
            use_preconditioning: bool
                If true, uses the preconditioning to solve the gradient.
                The precondition is obtained by inverting analytically only the Harmonic propagation of the
                L matrix
            algorithm: string
                The algorithm used to invert the L matrix. One between:
                   - cg : Conjugate Gradient (Default)
                It will invoke the corresponding scipy subroutine

        Results
        -------
            G_inv: ndarray(size = (n_modes, n_modes))
                This is the mass-rescaled free energy Hessian.
                Its eigenvalues are the static frequencies, that determine the structure stability.
        """

        G_one_phonon = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)

        if verbose:
            print()
            print("=====================")
            print(" HESSIAN CALCULATION ")
            print("=====================")
            print()
            print("We compute the static response with the")
            print("Conjugate gradient algorithm.")
            print()


        # Check if the symmetries has been initialized
        if not self.initialized:
            self.prepare_symmetrization()

        # Prepare the psi vector
        # And the L operator for the efficient calculation of the static response
        self.psi = np.zeros( self.n_modes + self.n_modes*(self.n_modes + 1) // 2, dtype = TYPE_DP)

        def apply_static_L(psi):
            self.psi[:] = psi.copy()

            # Apply the shift
            self.psi[self.n_modes :] *= eigen_shift

            ret = self.apply_L1_static(self.psi)
            ret += self.apply_anharmonic_static()

            ret[self.n_modes :] *= eigen_shift
            return ret

        L_operator = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)), matvec = apply_static_L, dtype = TYPE_DP)

        j = np.zeros(1, dtype = np.intc)
        x_old = self.psi.copy()
        for i in range(self.n_modes):
            if verbose:
                # Print the status
                print()
                print()
                print("NEW STEP")
                print("--------")
                print()
                print("i = {} / {}".format(i + 1, self.n_modes))
                print()

            # Setup the known vector
            self.psi = np.zeros(self.psi.shape, dtype = type(self.psi[0]))
            self.psi[i] = 1

            x_old[:] = self.psi
            j[0] = 0
            def callback(xk, x_old = x_old, j = j):
                if np.isnan(np.sum(xk)):
                    raise ValueError("Error, NaN value found during the Conjugate Gradient.") 
                if verbose:
                    disp = sum( (xk - x_old)**2)
                    print("CG STEP {} | solution changed by {} (tol = {})".format(j[0], disp, tol))
                    j[0] += 1
                    x_old[:] = xk

            # Prepare the preconditioning

            M_prec = None

            def prec_half(x):
                return self.apply_L1_static(x, inverse = True, power = 0.5)

            if use_preconditioning:
                M_prec = scipy.sparse.linalg.LinearOperator(L_operator.shape, matvec = prec_half)

            x0 = self.psi.copy()# / self.w[i] / self.w[i]

            # Run the biconjugate gradient
            t1 = time.time()
            if algorithm.lower() == "cg":
                if not use_preconditioning:
                    res = Tools.minimum_residual_algorithm(L_operator, self.psi.copy(), x0 = x0, precond = None, max_iters = max_iters)
                else:
                    res = Tools.minimum_residual_algorithm_precond(L_operator, self.psi.copy(), M_prec, max_iters = max_iters)
                info = 0
                #res, info = scipy.sparse.linalg.cg(L_operator, self.psi, x0 = x0)#, tol = tol, maxiter = maxiter, callback=callback, M = M_prec)
            #elif algorithm.lower() == "bicg":
            #    res, info = scipy.sparse.linalg.bicg(self.L_linop, self.psi, x0 = x0, tol = tol, maxiter = maxiter, callback=callback, M = M_prec)
            elif algorithm.lower() == "minimize" or "minimize-quad":
                # This algorithm minimizes f(x) = 1/2  (Lx - b) H^-1 (Lx - b)
                # where H is the matrix H = L^T L (so it is positive definite). 
                # We pick the H inverse as the inverse of the SSCHA harmonic solution.  
                # To find x and compute x = A^-1 b

                # Here we define the function that returns f(x) and its gradient
                def func_quad(x, b):
                    # Apply
                    Lx = L_operator.matvec(x) 

                    # Apply the precondition H^-1 = (L^t L)^-1 => M M^t
                    # if use_preconditioning:
                    #     Hinv_r = self.M_linop.rmatvec(r)
                    #     Hinv_r = self.M_linop.matvec(Hinv_r)
                    # else:
                    #     Hinv_r = r

                    # Now we get the gradient
                    r = Lx - b

                    gradient = L_operator.matvec(r)

                    # We get the function
                    f = 0.5 * np.sum(r**2)

                    if verbose:
                        print("Evaluated function: value = {} | norm gradient = {}".format(f, np.sum(gradient**2)))
                        print()

                    return f, gradient

                def func_standard(x, b):
                    # Apply
                    Lx = L_operator.matvec(x) 

                    # Apply the precondition H^-1 = (L^t L)^-1 => M M^t
                    # if use_preconditioning:
                    #     Hinv_r = self.M_linop.rmatvec(r)
                    #     Hinv_r = self.M_linop.matvec(Hinv_r)
                    # else:
                    #     Hinv_r = r

                    # Now we get the gradient
                    r = Lx - b

                    gradient = r

                    # We get the function
                    f = 0.5 * x.dot(Lx) - b.dot(x) 

                    if verbose:
                        print("Evaluated function: value = {} | norm gradient = {}".format(f, np.sum(gradient**2)))
                        print()

                    return f, gradient

                psi_vector = self.psi.copy() 

                # Setup the minimization parameters
                options = {"gtol" : tol, "maxiter" : max_iters, "disp" : verbose, "norm" : 2}

                if algorithm.lower() == "minimize":
                    results = scipy.optimize.minimize(func_standard, x0, args = (psi_vector), method = "bfgs", jac = True, options=options)
                elif algorithm.lower() == "minimize-quad":
                    results = scipy.optimize.minimize(func_quad, x0, args = (psi_vector), method = "bfgs", jac = True, options=options)


                # Start the minimization

                # Get the number of iterations
                j[0] = results.nit

                # Check the success
                if results.success:
                    info = 0
                else:
                    info = 1

                if verbose:
                    print("Minimization terminated after {} evaluations.".format(results.nfev))

                # Get the result
                res = results.x.copy()
            else:
                raise ValueError("""
Error, algorithm type '{}' in subroutine run_biconjugate_gradient not implemented.
       the only supported algorithms are ['bicgstab', 'bicg']
""".format(algorithm))
            t2 = time.time()

            if  verbose:
                print()
                print("Time to solve the linear system: {} s".format(t2 - t1))
                print()

            # Check if the minimization converged
            assert info >= 0, "Error on input or breakdown of biconjugate gradient algorithm (info = {})".format(info)

            if info > 0:
                print("The biconjugate gradient (step {}) algorithm did not converge after {} iterations.".format(i+1, maxiter))
                print("Try to either reduce the tollerance or increase the number of iteriations")
                print()
            else:
                print("The biconjugate gradient converged after {} iterations.".format(j[0]))
                print("res = {}".format(res))


            G_one_phonon[i, :] = res[:self.n_modes]
            if i % save_each == 0:
                if save_g is not None:
                    np.save(save_g, G_one_phonon)


        if verbose:
            print()
            print(" ================================ ")
            print(" THE CONJUGATE GRADIENT CONVERGED ")
            print(" ================================ ")
            print()
            print()


        if save_g is not None:
            np.save(save_g, G_one_phonon)

        # Check the hermitianeity
        disp = np.max(np.abs(G_one_phonon - G_one_phonon.T))
        assert disp < 1e-4, "Error, the resulting one-phonon Green function is not Hermitian."

        # Force hermitianity
        G = 0.5 * (G_one_phonon + G_one_phonon.T)

        # Invert the green function to get the Hessian Matrix (mass-rescaled)
        G_inv = np.linalg.inv(G) 

        return G_inv

run_conjugate_gradient(eigval=0, n_iters=100, thr=1e-05, verbose=True, guess_x=None)

RUN THE CONJUGATE GRADIENT (WORK METHOD)

The conjugate gradient is a very fast algorithm that allows to compute the (in principle) exact green function.

Given the initial vector in the psi direction, it will edit it to obtain:

.. math ::

\left|x\right> = \left(\mathcal{L} - I\lambda\right)^{-1} \left| {\psi} \right>

At the end of the algorithm, the self.psi variable will contain the :math:\left|x\right> vector.

Source code in tdscha/DynamicalLanczos.py
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
def run_conjugate_gradient(self, eigval = 0, n_iters = 100, thr = 1e-5, verbose = True, guess_x = None):
    r"""
    RUN THE CONJUGATE GRADIENT (WORK METHOD)
    ========================================

    The conjugate gradient is a very fast algorithm 
    that allows to compute the (in principle) exact green function. 

    Given the initial vector in the psi direction, it will edit it to obtain:

    .. math ::

        \left|x\right> = \left(\mathcal{L} - I\lambda\right)^{-1} \left| {\psi} \right> 

    At the end of the algorithm, the self.psi variable will contain the :math:`\left|x\right>`
    vector.

    Parameters
    ----------
        eigval : float
            The value of the :math:`\lambda` for the inversion problem.
        n_iters : int
            The number of iterations
        thr : float
            The threshold between two iteration after which the algorithm is considered 
            to be converged.
        verbose : bool
            If true print the status of the iteration.

    """

    if guess_x is None:
        x = self.psi.copy()
    else:
        x = guess_x.copy()
    A_x = self.apply_full_L()

    r = x - A_x
    p = r.copy()

    mod_r = np.sqrt(r.dot(r))

    if verbose:
        print("   CG step %d : residual = %.4e | threshold = %.4e" % (0,mod_r, thr))

    if mod_r < thr:
        return x

    for i in range(n_iters):
        self.psi = p
        A_p = self.apply_full_L()
        alpha = r.dot(r) / p.dot(A_p)
        x += alpha * p 

        # Update
        r_new = r - alpha * A_p 
        beta = r_new.dot(r_new) / r.dot(r)

        r = r_new
        p = r + beta * p 


        # Check the new iteration
        mod_r = np.sqrt(r.dot(r))
        if verbose:
            print("   CG step %d : residual = %.4e | threshold = %.4e" % (i+1,mod_r, thr))

        if mod_r < thr:
            return x

    print("WARNING: CG ended before the convergence was achieved.") 
    return x

get_statical_responce_from_scratch(n_iters=100, thr=1e-05, verbose=True, sub_block=None, sub_space=None)

GET STATIC RESPONCE

This algorithm performs the CG minimization to obtain the static self-energy.

Results
fc_matrix : ndarray (size=(3*nat, 3*nat))
    The static self-energy.
Source code in tdscha/DynamicalLanczos.py
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
def get_statical_responce_from_scratch(self, n_iters = 100, thr = 1e-5, verbose = True, sub_block = None, sub_space = None):
    """
    GET STATIC RESPONCE
    ===================

    This algorithm performs the CG minimization to obtain the static self-energy.

    Parameters
    ----------
        n_iters : int
            The number of maximum iteration for a single CG step.
        thr : float
            The threshold for the convergence of the CG algorithm.
        verbose : bool
            If true (default) prints the info during the minimization
        sub_block : list
            A list of indices that identifies the modes id that that you want to select.
            The algorithm is performed only in the reduced space of (N_modes x N_modes)
            given by the length of this list. 
            In this way you will neglect mode interaction, but you can save a lot of time.
            Leave as None if you want the whole space.
        sub_space : ndarray(size = (N_dim, 3*n_at))
            Compute the self-energy only in the subspace given. Leave it as none
            if you do not want to use this option

    Results
    -------
        fc_matrix : ndarray (size=(3*nat, 3*nat))
            The static self-energy.
    """

    n_dim_space = self.n_modes


    PLinvP = np.zeros((n_dim_space, n_dim_space), dtype = TYPE_DP, order = "C")

    if (not sub_block is None) and (not sub_space is None):
        raise ValueError("Error, you cannot specify both sub_block and sub_space.")

    if not sub_block is None:
        n_dim_space = len(sub_block)
    elif not sub_space is None:
        n_dim_space = len(sub_space)

    # initialize the algorithm
    for i in range(n_dim_space):
        # Setup the vector
        self.psi = np.zeros((self.n_modes + 1)*self.n_modes, dtype = TYPE_DP)
        guess = self.psi.copy()

        # Create the subbasis
        if not sub_block is None:
            self.psi[sub_block[i]] = 1
            guess[sub_block[i]] = self.w[sub_block[i]] ** 2
        elif not sub_space is None:
            self.psi[:self.n_modes] = sub_block[i].dot(self.pols)
            guess = None
        else:
            self.psi[i] = 1
            guess[i] = self.w[i] ** 2

        if verbose:
            print("")
            print("==== NEW STATIC COMPUTATION ====")
            print("Iteration: %d out of %d" % (i+1, n_dim_space))


        new_v = self.run_conjugate_gradient(n_iters = n_iters, thr = thr, verbose = verbose, guess_x = guess)

        if not sub_space is None:
            v_out = self.pols.dot(new_v[:self.n_modes])
            PLinvP[i, :] = np.array(sub_space).dot(v_out)
        else:
            PLinvP[i, :] = new_v[:self.n_modes]



    # Invert the P L^-1 P 
    D = np.linalg.inv(PLinvP)
    # Transform to a force constant matrix in cartesian coordinates

    if not sub_space is None:
        fc_matrix = np.einsum("ab, ai, bj->ij", D, np.array(sub_space), np.array(sub_space))
    else:
        fc_matrix = np.einsum("ab, ia, jb->ij", D, self.pols, self.pols)
    fc_matrix *= np.sqrt(np.outer(self.m, self.m))

    return fc_matrix

run(n_iter, save_dir='.', verbose=True)

RUN LANCZOS ITERATIONS

This method performs the Lanczos algorithm to find the sequence of a and b coefficients that are the tridiagonal representation of the L matrix to be inverted.

Source code in tdscha/DynamicalLanczos.py
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
    def run(self, n_iter, save_dir = ".", verbose = True):
        """
        RUN LANCZOS ITERATIONS
        ======================

        This method performs the Lanczos algorithm to find
        the sequence of a and b coefficients that are the tridiagonal representation 
        of the L matrix to be inverted.

        Parameters
        ----------
            n_iter : int
                The number of iterations to be performed in the Lanczos algorithm.
            save_dir : string
                The directory in which you want to store the results step by step,
                in order to do a preliminar analysis or restart the calculation later.
            verbose : bool
                If true all the info during the minimization will be printed on output.
        """

        raise ValueError("Error, this run funciton has been deprecated, use run_FT instead.")

        # Check if the symmetries has been initialized
        if not self.initialized:
            self.prepare_symmetrization()

        # Get the current step
        i_step = len(self.a_coeffs)

        if verbose:
            header = """
<=====================================>
|                                     |
|          LANCZOS ALGORITHM          |
|                                     |
<=====================================>

Starting the algorithm. It may take a while.
Starting from step %d
""" % i_step
            print(header)

            OPTIONS = """
Should I ignore the third order effect? {}
Should I ignore the fourth order effect? {}
Max number of iterations: {}
""".format(self.ignore_v3, self.ignore_v4, n_iter)
            print(OPTIONS)


        # If this is the current step initialize the algorithm
        if i_step == 0:
            self.krilov_basis = []
            first_vector = self.psi / np.sqrt(self.psi.dot(self.psi))
            self.krilov_basis.append(first_vector)
        else:
            # Convert everything in a list
            self.krilov_basis = list(self.krilov_basis)
            self.a_coeffs = list(self.a_coeffs)
            self.b_coeffs = list(self.b_coeffs)
            self.arnoldi_matrix = list(self.arnoldi_matrix)

            if len(self.krilov_basis) != i_step + 1:
                print("Krilov dim: %d, number of steps perfomed: %d" % (len(self.krilov_basis), i_step))
                print("Error, the krilov basis dimension should be 1 more than the number of steps")
                raise ValueError("Error the starting krilov basis does not matches the matrix, Look stdout.")

        self.psi = self.krilov_basis[-1]

        for i in range(i_step, i_step+n_iter):
            if verbose:
                step_txt = """
 ===== NEW STEP %d =====

 """ % i
                print(step_txt)
                print("Length of the coefficiets: a = {}, b = {}".format(len(self.a_coeffs), len(self.b_coeffs)))
                print()

            # Apply the matrix L
            t1 = time.time()
            #self.psi = self.apply_full_L()
            self.psi = self.L_linop.dot(self.psi)
            t2 = time.time()

            if verbose:
                print("Time to apply the full L: %d s" % (t2 -t1))

            # Get the coefficients for the Lanczos/Arnoldi matrix
            t1 = time.time()
            arnoldi_row = []
            new_vect = self.psi.copy()


            # Lets repeat twice the orthogonalization
            converged = False
            for k_orth in range(N_REP_ORTH):
                for j in range(len(self.krilov_basis)):
                    coeff = new_vect.dot(self.krilov_basis[j])
                    if k_orth == 0:
                        arnoldi_row.append(self.psi.dot(self.krilov_basis[j]))

                    # Gram Schmidt
                    new_vect -= coeff * self.krilov_basis[j]

                # Add the new vector to the Krilov Basis
                norm = np.sqrt(new_vect.dot(new_vect))

                if verbose:
                    print("Vector norm after GS number {}: {:16.8e}".format(k_orth+1, norm))

                # Check the normalization (If zero the algorithm converged)
                if norm < __EPSILON__:
                    converged = True
                    if verbose:
                        print("Obtained a linear dependent vector.")
                        print("The algorithm converged.")
                    break

                new_vect /= norm 

            if not converged:
                self.krilov_basis.append(new_vect)
                self.psi = new_vect
            t2 = time.time()

            # Add the coefficients to the variables
            self.a_coeffs.append(arnoldi_row[-1])
            if len(arnoldi_row) > 1:
                self.b_coeffs.append(arnoldi_row[-2])
            self.arnoldi_matrix.append(arnoldi_row)

            if verbose:
                print("Time to perform the Gram-Schmidt and retrive the coefficients: %d s" % (t2-t1))
                print()
                print("a_%d = %.8e" % (i, self.a_coeffs[-1]))
                if i > 0:
                    print("b_%d = %.8e" % (i, self.b_coeffs[-1]))
                print()

            # Save the step
            if not save_dir is None:
                self.save_status("%s/LANCZOS_STEP%d" % (save_dir, i))

                if verbose:
                    print("Status saved into '%s/LANCZOS_STEP%d'" % (save_dir, i))

            if verbose:
                print("Lanczos step %d ultimated." % i)


            if converged:
                return

build_lanczos_matrix_from_coeffs(use_arnoldi=False)

BUILD THE LANCZOS MATRIX

This method builds the Lanczos matrix from the coefficients. To execute this method correctly you must have already completed the Lanczos algorithm (method run)

Source code in tdscha/DynamicalLanczos.py
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
def build_lanczos_matrix_from_coeffs(self, use_arnoldi=False):
    """
    BUILD THE LANCZOS MATRIX
    ========================

    This method builds the Lanczos matrix from the coefficients. 
    To execute this method correctly you must have already completed the Lanczos algorithm (method run)

    Parameters
    ----------
        use_arnoldi: bool
            If true the full matrix is computed, using all the coefficients from the
            Arnoldi iteration.
    """

    N_size = len(self.a_coeffs)
    matrix = np.zeros((N_size, N_size), dtype = TYPE_DP)
    if not use_arnoldi:
        for i in range(N_size):
            matrix[i,i] = self.a_coeffs[i]
            if i>= 1:
                # Use the non-symmetric Lanczos if also c_coeffs are present
                c_coeff = self.b_coeffs[i-1]
                if len(self.c_coeffs) == len(self.b_coeffs):
                    c_coeff = self.c_coeffs[i-1]
                matrix[i-1,i] = c_coeff
                matrix[i,i-1] = self.b_coeffs[i-1]
    else:
        # Check if there are c_coeffs, in this way arnoldi matrix is not computed
        assert len(self.b_coeffs) > len(self.c_coeffs), "Error, cannot Arnoldi with non-symmetric Lanczos not implemented"
        for i in range(N_size):
            matrix[:i+1, i] = self.arnoldi_matrix[i]
            matrix[i, :i+1] = self.arnoldi_matrix[i]


    sign = 1
    if self.reverse_L:
        sign = -1

    matrix =  sign*matrix - sign* np.eye(N_size) * self.shift_value

    return matrix

get_green_function_Lenmann(w_array, smearing, v_a, v_b, use_arnoldi=False)

GET GREEN FUNCTION

Compute the green function using the Lemman representation.

Source code in tdscha/DynamicalLanczos.py
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
def get_green_function_Lenmann(self, w_array, smearing, v_a, v_b, use_arnoldi = False):
    """
    GET GREEN FUNCTION
    ==================

    Compute the green function using the Lemman representation.

    Parameters
    ----------
        w_array : ndarray
            The list of frequencies in RY for which you want to compute the
            dynamical green function.
        smearing : float
            The smearing in RY to take a non zero imaginary part.
        v_a : ndarray(size = 3*self.nat)
            The perturbation operator (on atomic positions)
        v_b : ndarray(size = 3*self.nat)
            The probed responce operator (on atomic positions)
        use_arnoldi: bool
            If true the full arnoldi matrix is used to extract eigenvalues and 
            eigenvectors. Otherwise the tridiagonal Lanczos matrix is used.
            The first one prevents the loss of orthogonality problem.
    """

    # Get the Lanczos matrix
    matrix = self.build_lanczos_matrix_from_coeffs(use_arnoldi)

    assert len(self.c_coeffs) < len(self.b_coeffs), "Lenmann cannot be used with non-symmetric Lanczos"

    # Convert the vectors in the polarization basis
    new_va = np.einsum("a, a, ab->b", 1/np.sqrt(self.m), v_a, self.pols)
    new_vb = np.einsum("a, a, ab->b", 1/np.sqrt(self.m), v_b, self.pols)

    # Dyagonalize the Lanczos matrix
    eigvals, eigvects = np.linalg.eigh(matrix)

    kb = np.array(self.krilov_basis)
    kb = kb[:-1,:]
    #print (np.shape(eigvects), np.shape(kb))
    # Convert in krilov space
    new_eigv = np.einsum("ab, ac->cb", eigvects, kb)


    Na, Nb = np.shape(matrix)
    if Na != Nb:
        raise ValueError("Error, the Lanczos matrix must be square, dim (%d,%d)" % (Na, Nb))

    gf = np.zeros(len(w_array), dtype = np.complex128)

    for j in range(Na):
        eig_v = new_eigv[:self.n_modes, j]
        matrix_element = eig_v.dot(new_va) * new_vb.dot(eig_v)
        gf[:] += matrix_element / (eigvals[j]  - w_array**2 + 2j*w_array*smearing)

    return gf

get_static_odd_fc(use_arnoldi=False)

GET STATIC FORCE CONSTANT

Get the static force constant matrix

Source code in tdscha/DynamicalLanczos.py
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
def get_static_odd_fc(self, use_arnoldi = False):
    """
    GET STATIC FORCE CONSTANT
    =========================

    Get the static force constant matrix

    Parameters
    ----------
        use_arnoldi: bool
            If true the full arnoldi matrix is used, otherwise the Lanczos tridiagonal
            matrix is used.
    """

    # Get the Lanczos matrix
    matrix = self.build_lanczos_matrix_from_coeffs(use_arnoldi)

    # Dyagonalize the Lanczos matrix
    eigvals, eigvects = np.linalg.eigh(matrix)

    Nk = len(self.krilov_basis)

    kb = np.array(self.krilov_basis)

    # Lanczos did not converged, discard the last vector
    if Nk > len(eigvals):
        kb = kb[:-1,:]

    #print (np.shape(eigvects), np.shape(kb))
    new_eigv = np.einsum("ab, ac->cb", eigvects, kb)

    Na, Nb = np.shape(matrix)
    if Na != Nb:
        raise ValueError("Error, the Lanczos matrix must be square, dim (%d,%d)" % (Na, Nb))


    fc_matrix = np.zeros( (3*self.nat, 3*self.nat), dtype = TYPE_DP)

    # Get the dynamical matrix in the polarization basis
    D = np.einsum("ai, bi, i->ab", new_eigv[:self.n_modes,:], new_eigv[:self.n_modes, :], eigvals)

    # Convert it in the standard basis
    fc_matrix = np.einsum("ab, ia, jb->ij", D, self.pols, self.pols)

    # for i in range(3*self.nat):
    #     # Define the vector
    #     v = np.zeros(3*self.nat, dtype = TYPE_DP)
    #     v[i] = 1

    #     # Convert the vectors in the polarization basis
    #     new_v = np.einsum("a, a, ab->b", np.sqrt(self.m), v, self.pols)
    #     # Convert in the krilov space 
    #     mat_coeff = np.einsum("a, ab", new_v, new_eigv[:self.n_modes, :])
    #     new_w = np.einsum("a, ba, a", mat_coeff, new_eigv[:self.n_modes,:], eigvals)

    #     #v_kb = np.einsum("ab, b", kb[:, :self.n_modes], new_v)
    #     # Apply the L matrix
    #     #w_kb = matrix.dot(v_kb)
    #     # Convert back in the polarization space
    #     #new_w = np.einsum("ab, a", kb[:, :self.n_modes], w_kb)
    #     # Convert back in real spaceDoes anyone know if there is a windows binary or a source code to run QE with GPU enhancement on windows. 
    #     w = np.einsum("a, b, ab ->a", 1/np.sqrt(self.m), new_w, self.pols)

    #     fc_matrix[i, :] = w


    # This is the dynamical matrix now we can multiply by the masses
    fc_matrix *= np.sqrt(np.outer(self.m, self.m))

    return fc_matrix

get_all_green_functions(N_steps=100, mode_mixing=True, save_step_dir=None, verbose=True)

GET ALL THE GREEN FUNCTIONS

This will compute a set of lanczos coefficients for each element of the odd matrix. a_n and b_n. We will run lanczos for all the elements and all the crosses. In this way we have the whole evolution with frequency of the matrix.

NOTE: This can be a very intensive computation.

Results
a_ns : ndarray( (n_modes, n_modes, N_steps))
    The a coefficients for each element in the mode x mode space
b_ns : ndarray( (n_modes, n_modes, N_steps-1))
    The b_n coefficients for each mode in the space.
Source code in tdscha/DynamicalLanczos.py
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
def get_all_green_functions(self, N_steps = 100, mode_mixing = True, save_step_dir = None, verbose = True):
    """
    GET ALL THE GREEN FUNCTIONS
    ===========================

    This will compute a set of lanczos coefficients for each element of the odd matrix.
    a_n and b_n.
    We will run lanczos for all the elements and all the crosses.
    In this way we have the whole evolution with frequency of the matrix.

    NOTE: This can be a very intensive computation.

    Parameters
    ----------
        N_steps : int
            The number of Lanczos iteration for each green function
        mode_mixing : bool
            If True also non diagonal elements are computed, otherwise the 
            SSCHA eigenvector are supposed to be conserved, and only diagonal
            green functions are considered.
            If False the computation is much less expensive (a factor nat_sc),
            but it is approximated.
        save_step_dir : string
            If not None, the path to the directory in which you want to save 
            each step. So even if stopped the calculation can restart.
        verbose : bool
            If true print all the progress to standard output

    Results
    -------
        a_ns : ndarray( (n_modes, n_modes, N_steps))
            The a coefficients for each element in the mode x mode space
        b_ns : ndarray( (n_modes, n_modes, N_steps-1))
            The b_n coefficients for each mode in the space.
    """

    # Time the function
    t_start = time.time()

    # Check if the save directory exists
    # Otherwise we create it
    if not save_step_dir is None:
        if not os.path.exists(save_step_dir):
            makedirs(save_step_dir)

    # Load all the data
    a_ns = np.zeros( (self.n_modes, self.n_modes, N_steps), dtype = np.double)
    b_ns = np.zeros( (self.n_modes, self.n_modes, N_steps-1), dtype = np.double)

    # Incompatible with shift for now
    self.shift_value = 0

    # Compute the diagonal parts
    for i in range(self.n_modes):
        if verbose:
            print("\n")
            print("  ==========================  ")
            print("  |                        |  ")
            print("  |   DIAGONAL ELEMENTS    |  ")
            print("  |       STEP {:5d}       |  ".format(i))
            print("  |                        |  ")
            print("  ==========================  ")
            print()

        # Setup the Lanczos
        self.reset()

        # Prepare the perturbation
        self.psi[:] = 0
        self.psi[i] = 1

        # Run the Lanczos perturbation
        self.run(N_steps, save_dir = save_step_dir, verbose = verbose)

        if verbose:
            print()
            print("   ---- > LANCZOS RUN COMPLEATED < ----   ")
            print()

        # Save the status
        if save_step_dir:
            self.save_status("full_lanczos_diagonal_{}".format(i))

        # Fill the a_n and b_n
        a_tmp = np.zeros(N_steps, dtype = np.double)
        a_tmp[:len(self.a_coeffs)] = self.a_coeffs
        b_tmp = np.zeros(N_steps-1, dtype = np.double)
        b_tmp[:len(self.b_coeffs)] = self.b_coeffs
        a_ns[i, i, :] = a_tmp
        b_ns[i, i, :] = b_tmp

    # If we must compute the mode mixing
    if mode_mixing:
        for i in range(self.n_modes):
            for j in range(i+1, self.n_modes):
                # TODO: Neglect (i,j) forbidden by symmetries

                if verbose:
                    print("\n")
                    print("  ============================  ")
                    print("  |                          |  ")
                    print("  |   NON DIAGONAL ELEMENT   |  ")
                    print("  |    STEP ({:5d},{:5d})    |  ".format(i, j))
                    print("  |                          |  ")
                    print("  ============================  ")
                    print()

                # Setup the Lanczos
                self.reset()

                # Prepare the perturbation
                self.psi[:] = 0
                self.psi[i] = 1
                self.psi[j] = 1

                # Run the Lanczos perturbation
                self.run(N_steps, save_dir = save_step_dir, verbose = verbose)

                if verbose:
                    print()
                    print("   ---- > LANCZOS RUN COMPLEATED < ----   ")
                    print()

                # Save the status
                if save_step_dir:
                    self.save_status("full_lanczos_off_diagonal_{}_{}".format(i, j))

                # Fill the a_n and b_n
                a_tmp = np.zeros(N_steps, dtype = np.double)
                a_tmp[:len(self.a_coeffs)] = self.a_coeffs
                b_tmp = np.zeros(N_steps-1, dtype = np.double)
                b_tmp[:len(self.b_coeffs)] = self.b_coeffs
                a_ns[i, j, :] = a_tmp
                b_ns[i, j, :] = b_tmp
                a_ns[j, i, :] = a_tmp
                b_ns[j, i, :] = b_tmp

    t_end = time.time()

    total_time = t_end - t_start
    minutes = int(total_time / 60)
    hours = int(minutes / 60)
    minutes -= hours * 60
    seconds = int(total_time - hours*3600 - minutes * 60)

    if verbose:
        print()
        print()
        print("     ======================     ")
        print("     |                    |     ")
        print("     |        DONE        |      ")
        print("     |   In {:3d}:{:02d}:{:02d}s  |     ".format(hours, minutes, seconds))
        print("     ======================     ")
        print()
        print()

    return a_ns, b_ns

get_spectral_function_from_Lenmann(w_array, smearing, use_arnoldi=True)

GET SPECTRAL FUNCTION

This method computes the spectral function in the supercell using the Lenmann representation.

Source code in tdscha/DynamicalLanczos.py
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
def get_spectral_function_from_Lenmann(self, w_array, smearing, use_arnoldi=True):
    """
    GET SPECTRAL FUNCTION
    =====================

    This method computes the spectral function in the supercell
    using the Lenmann representation.

    Parameters
    ----------
        w_array : ndarray
            The list of frequencies for which you want to compute the
            dynamical green function.
        smearing : float
            The smearing to take a non zero imaginary part.
        use_arnoldi: bool
            If true the full arnoldi matrix is used to extract eigenvalues and 
            eigenvectors. Otherwise the tridiagonal Lanczos matrix is used.
            The first one prevents the loss of orthogonality problem.
    """
    # Get the Lanczos matrix
    matrix = self.build_lanczos_matrix_from_coeffs(use_arnoldi)

    # Dyagonalize the Lanczos matrix
    eigvals, eigvects = np.linalg.eigh(matrix)

    Na, Nb = np.shape(matrix)
    if Na != Nb:
        raise ValueError("Error, the Lanczos matrix must be square, dim (%d,%d)" % (Na, Nb))

    spectral = np.zeros(len(w_array), dtype = np.complex128)


    kb = np.array(self.krilov_basis)
    if np.shape(kb)[0] > Na:
        kb = kb[:-1,:]
    print ("Shape check: eigvects = {}, kb = {}".format( np.shape(eigvects), np.shape(kb)))
    new_eigv = np.einsum("ab, ac->cb", eigvects, kb)
    # TODO: Update for Lanczos biconjugate

    for j in range(Na):
        eig_v = new_eigv[:self.n_modes, j]
        matrix_element = np.conj(eig_v).dot(eig_v)
        spectral[:] += matrix_element / (eigvals[j]  - w_array**2 +2j*w_array*smearing)

    return -np.imag(spectral)

get_green_function_continued_fraction(w_array, use_terminator=True, last_average=1, smearing=0)

CONTINUED FRACTION GREEN FUNCTION

In this way the continued fraction for the green function is used. This should converge faster than the Lenmann representation, and has the advantage of adding the possibility to add a terminator. This avoids to define a smearing.

NORMAL: we invert: :: math . ,

WIGNER: we invert: :: math .

So in the continued fraction we have -/+ in front of the freqeuncy depending on the formalism used.

Source code in tdscha/DynamicalLanczos.py
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
    def get_green_function_continued_fraction(self, w_array : np.ndarray[np.float64], use_terminator : bool = True, last_average: int = 1, smearing : np.float64 = 0):
        r"""
        CONTINUED FRACTION GREEN FUNCTION
        =================================

        In this way the continued fraction for the green function is used.
        This should converge faster than the Lenmann representation, and
        has the advantage of adding the possibility to add a terminator.
        This avoids to define a smearing.

        NORMAL: we invert:
        :: math .
            <p|(-\mathcal{L} - \omega^2)^{-1}|q>,

        WIGNER: we invert:
        :: math .
            <p|(\mathcal{L}_w + \omega^2)^{-1}|q>

        So in the continued fraction we have -/+ in front of the freqeuncy depending on the formalism used.

        Parameters
        ----------
            w_array : ndarray
                The list of frequencies in RY in which you want to compute the green function
            use_terminator : bool
                If true (default) a standard terminator is used.
            last_average : int
                How many a and b coefficients are averaged to evaluate the terminator?
            smearing : float
                The smearing parameter in RY. If none
        """
        n_iters = len(self.a_coeffs)

        gf = np.zeros(np.shape(w_array), dtype = np.complex128)

        sign = 1
        if self.reverse_L:
            sign = -1

        INFO = """
GREEN FUNCTION FROM CONTINUED FRACTION
Am I using Wigner? {}
Should I use the terminator? {}
Perturbation modulus = {}
Sign = {}""".format(self.use_wigner, use_terminator, self.perturbation_modulus, sign)

        if self.verbose:
            print()
            print(INFO)

        # Get the terminator
        if use_terminator:
            # Get the last coeffs averaging
            a_av = np.mean(self.a_coeffs[-last_average:])
            b_av = np.mean(self.b_coeffs[-last_average:])
            c_av = b_av
            # Non-symmetric Lanczos
            if len(self.c_coeffs) == len(self.b_coeffs):
                c_av = np.mean(self.c_coeffs[-last_average:])

            a = a_av * sign - sign* self.shift_value
            b = b_av * sign
            c = c_av * sign

            if not self.use_wigner:
                gf[:] = (a - w_array**2 - np.sqrt( (a - w_array**2)**2 - 4*b*c + 0j))/(2*b*c)
            else:
                # Wigner
                gf[:] = (a + w_array**2 + np.sqrt( (a + w_array**2)**2 - 4*b*c + 0j))/(2*b*c)        
        else:
            # If we do not use the Terminator we get the last fraction
            a = self.a_coeffs[-1] * sign - sign* self.shift_value
            if not self.use_wigner:
                gf[:] = 1/ (a - w_array**2 + 2j*w_array*smearing)
            else:
                # Wigner
                gf[:] = 1/ (a + w_array**2 + 2j*w_array*smearing)

        # Continued fraction
        for i in range(n_iters-2, -1, -1):
            # Start getting the continued fraction from the last coeff
            a = self.a_coeffs[i] * sign - sign * self.shift_value
            b = self.b_coeffs[i] * sign
            c = b
            if len(self.c_coeffs) == len(self.b_coeffs): 
                c = self.c_coeffs[i] * sign

            if not self.use_wigner:
                gf = 1. / (a - w_array**2  + 2j*w_array*smearing - b * c * gf)
            else:
                # In Wigner we invert L + omega^2
                gf = 1. / (a + w_array**2  + 2j*w_array*smearing - b * c * gf)

        if not self.use_wigner:
            return gf * self.perturbation_modulus
        else:
            # Wigner
            return (-np.real(gf) + 1j * np.imag(gf)) * self.perturbation_modulus

get_full_L_debug_wigner(verbose=False, debug_d3=None, symmetrize=True, overwrite_L_operator=True)

GET THE FULL L OPERATOR FOR DEBUG WIGNER

Use this method to test if the change of variables that defines the symmetric Wigner representation works.

Note: make sure that self.use_wigner is Fasle so when we compute the Green function we get the correct sign in front of omega in the contnued fraction.

DO NOT USE FOR PRODUCTION: IT IS DANGEROUS

Results

L_op : ndarray(size = (nmodes * (2*nmodes + 1)), dtype = TYPE_DP) The full L operator.

Source code in tdscha/DynamicalLanczos.py
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
    def get_full_L_debug_wigner(self, verbose = False, debug_d3 = None, symmetrize = True, overwrite_L_operator = True):
        """
        GET THE FULL L OPERATOR FOR DEBUG WIGNER
        ========================================
        Use this method to test if the change of variables that defines the symmetric Wigner representation works.

        Note: make sure that self.use_wigner is Fasle so when we compute the Green function
        we get the correct sign in front of omega in the contnued fraction.

        DO NOT USE FOR PRODUCTION: IT IS DANGEROUS

        Results
        -------
           L_op : ndarray(size = (nmodes * (2*nmodes + 1)), dtype = TYPE_DP)
              The full L operator.
        """
        if self.use_wigner:
            raise ErrorValue('Please make sure that use_wigner is False')

        # Avoid the dependent freqeuncies
        i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
        i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

        new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
        new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

        w_a = self.w[new_i_a]
        w_b = self.w[new_i_b]

        # N_w2 is the number of independent indeces
        N_w2 = len(w_a)

        if verbose:
            print()
            print('Getting the DEBUG WIGNER L operator')
        # Prepare the operator
        L_operator = np.zeros(shape = (self.n_modes + 2*N_w2, self.n_modes + 2*N_w2), dtype = TYPE_DP)


        n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        if self.T > 0:
            n_a = 1 / (np.exp(w_a * 157887.32400374097/self.T) - 1)
            n_b = 1 / (np.exp(w_b * 157887.32400374097/self.T) - 1)

        if verbose:
            print("BE occ number", n_a[:self.n_modes])

        # Apply the non interacting X operator
        start_Y = self.n_modes
        start_A = self.n_modes + N_w2

        if not self.ignore_harmonic:
            if verbose:
                print('DEBUG WIGNER harmonic R(1) a(1) b(1)')
            # Harmonic evolution for R -> R sector
            L_operator[:self.n_modes, :self.n_modes] = +np.diag(self.w**2)

            # Harmonic evolution for a -> a sector
            a_a = w_a**2 + w_b**2 - 2 * w_a * w_b
            L_operator[start_Y: start_A, start_Y: start_A] = +np.diag(a_a)

            # Harmonic evolution for b -> b sector
            b_b = w_a**2 + w_b**2 + 2 * w_a * w_b
            L_operator[start_A:, start_A:] = +np.diag(b_b)


        # We ADDED all the non interacting (harmonic) propagators both for debug Wigner
        # In Wigner the Harmonic approach is working

        # Compute the d3 operator
        if debug_d3 is None:
            N_eff = np.sum(self.rho)
            Y_weighted = np.einsum("ia, i -> ia", self.Y, self.rho)
        if not self.ignore_v3:
            if verbose:
                print("Computing d3...")
            if not debug_d3 is None:
                d3 = debug_d3
            else:
                X_ups = np.einsum("ia, a -> ia", self.X, f_ups(self.w, self.T))

                d3_noperm = np.einsum("ia, ib, ic -> abc", X_ups, X_ups, Y_weighted)
                d3_noperm /= -N_eff 

                # Apply the permuatations
                d3 = d3_noperm.copy()
                d3 += np.einsum("abc->acb", d3_noperm)
                d3 += np.einsum("abc->bac", d3_noperm)
                d3 += np.einsum("abc->bca", d3_noperm)
                d3 += np.einsum("abc->cab", d3_noperm)
                d3 += np.einsum("abc->cba", d3_noperm)
                d3 /= 6

                if verbose:
                    np.save("d3_modes_nosym.npy", d3)

                # Perform the standard symmetrization
                if symmetrize:
                    # TODO: fix the symmetrize_d3_muspace
                    raise NotImplementedError('The symmetrizaiton on d3 is not implemented')
                    d3 = symmetrize_d3_muspace(d3, self.symmetries)

                    if verbose:
                        np.save("d3_modes_sym.npy", d3)
                        np.save("symmetries_modes.npy", self.symmetries)


            # Reshape the d3
            d3_small_space = np.zeros((N_w2, self.n_modes), dtype = np.double)
            # Get the d3 in the small space by getting the independent terms
            d3_small_space[:,:] = d3[new_i_a, new_i_b, :]

            if verbose:
                print("D3 of the following elements:")
                print(new_i_a)
                print(new_i_b)
                print("D3 small space")
                print(d3_small_space)
                print('D3 complete')
                print(d3)

            if verbose:
                print('DEBUG WIGNER getting the D3 contribution')
            # Chi for the independent indeces
            L2_minus = - (2 * (n_a - n_b) * (w_a - w_b)) /(w_a * w_b * (2 * n_a + 1) *  (2 * n_b + 1)) 
            L2_plus  = + (2 * (1 + n_a + n_b) * (w_a + w_b)) /(w_a * w_b * (2 * n_a + 1) *  (2 * n_b + 1)) 
            # X for the independent indices
            X = ((2 * n_a + 1) *  (2 * n_b + 1))/8

            # The shape of these tensors is (N_w2, n_modes) considering double counting
            extra_count_w = np.ones(N_w2, dtype = np.intc)
            extra_count_w[new_i_a != new_i_b] = 2
            d3_X  = np.einsum('ab, a -> ab', d3_small_space, X * extra_count_w)

            # The interacion between a rank 1 tensor a rank 2 tensor DOES require
            # to take into account double counting

            # The coeff betwee R(1) and a(1)
            L_operator[:start_Y, start_Y: start_A] = -d3_X.T
            # The coeff betwee R(1) and b(1)
            L_operator[:start_Y, start_A:] = +d3_X.T

            # The interaction between a rank 2 tensor a rank 1 tensor DOES NOT require
            # to take into account double counting

            # The shape of these tensors is (N_w2, n_modes)
            L2_minus_d3 = np.einsum('ab, a -> ab', d3_small_space, L2_minus)
            L2_plus_d3  = np.einsum('ab, a -> ab', d3_small_space, L2_plus)

            # The coeff betwee a(1) and R(1)
            L_operator[start_Y: start_A, :start_Y] = -L2_minus_d3
            # The coeff betwee b(1) and R(1)
            L_operator[start_A:, :start_Y] = +L2_plus_d3


        if not self.ignore_v4:
#             raise NotImplementedError('The symmetrizaiton on d4 is not implemented for debug Wigner')
            # Get the full D4 tensor in the polarization basis
            # it should be symmetric under permutations of the indices
            d4 =  np.einsum("ia, ib, ic, id -> abcd", X_ups, X_ups, X_ups, Y_weighted)
            d4 += np.einsum("ia, ib, ic, id -> abcd", X_ups, X_ups, Y_weighted, X_ups)
            d4 += np.einsum("ia, ib, ic, id -> abcd", X_ups, Y_weighted, X_ups, X_ups)
            d4 += np.einsum("ia, ib, ic, id -> abcd", Y_weighted, X_ups, X_ups, X_ups)
            d4 /= - 4 * N_eff

            if verbose:
                np.save("d4_modes_nosym.npy", d4)
            if symmetrize:
                raise NotImplementedError('The symmetrizaiton on d4 is not implemented')

            # Get the independent first two indep indices
            d4_small_space1 = np.zeros((N_w2, self.n_modes, self.n_modes), dtype = np.double)
            d4_small_space1[:,:,:] = d4[new_i_a, new_i_b, :, :]

            # Get the independent second two indep indices
            d4_small_space = np.zeros((N_w2, N_w2), dtype = np.double)
            d4_small_space[:,:] = d4_small_space1[:, new_i_a, new_i_b]

            if verbose:
                print("D4 of the following elements:")
                print(new_i_a)
                print(new_i_b)
                print("D4 in the reduced space")
                print(d4_small_space)
                print("D4 complete")
                print(d4)

            # Add the matrix elements
            L2_minus_d4_X = np.einsum('a, ab, b -> ab', L2_minus, d4_small_space, X * extra_count_w)
            L2_plus_d4_X  = np.einsum('a, ab, b -> ab', L2_plus, d4_small_space, X * extra_count_w)

            # Interaction of a(1)-a(1)
            L_operator[start_Y:start_A, start_Y:start_A] += L2_minus_d4_X

            # Interaction of b(1)-b(1)
            L_operator[start_A:, start_A:] += L2_plus_d4_X

            # Interaction of a(1)-b(1)
            L_operator[start_Y:start_A, start_A:] += -L2_minus_d4_X

            # Interaction of b(1)-a(1)
            L_operator[start_A:, start_Y:start_A] += -L2_plus_d4_X


        if verbose:
            print("L DEBUG WIGNER superoperator computed.")
            np.savez_compressed("L_super_analytical_debug_wigner.npz", L_operator)

        if overwrite_L_operator:
            if verbose:
                print('Overwriting the L operator with DEBUG WIGNER..')
            def matvec(x):
                return L_operator.dot(x)
            def rmatvec(x):
                return x.dot(L_operator)

            self.L_linop = scipy.sparse.linalg.LinearOperator(L_operator.shape, matvec = matvec, rmatvec = rmatvec)

        return L_operator

get_static_frequency(smearing=0)

GET THE STATIC FREQUENCY

The static frequency of a specific perturbation can be obtained as the limit of the dynamical green function for w -> 0.

.. math ::

\omega = \sqrt{\frac{1}{\Re G(\omega \rightarrow 0 + i\eta)}}

where :math:\eta is the smearing for the static frequency calculation. This frequency is the diagonal element of the free energy Hessian matrix acros the chosen perturbation.

If :math:\omega is imaginary, a negative value is returned.

Results
- frequency : float
    The frequency of the perturbation :math:`\omega`
Source code in tdscha/DynamicalLanczos.py
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
def get_static_frequency(self, smearing: np.float64 = 0) -> np.float64:
    r"""
    GET THE STATIC FREQUENCY
    ========================

    The static frequency of a specific perturbation can be obtained as the limit of the 
    dynamical green function for w -> 0. 

    .. math ::

        \omega = \sqrt{\frac{1}{\Re G(\omega \rightarrow 0 + i\eta)}} 


    where :math:`\eta` is the smearing for the static frequency calculation.
    This frequency is the diagonal element of the free energy Hessian matrix acros the chosen perturbation.

    If :math:`\omega` is imaginary, a negative value is returned.

    Parameters
    ----------
        - smearing : float
            The smearing in Ry of the calculation

    Results
    -------
        - frequency : float
            The frequency of the perturbation :math:`\omega`
    """



    gf = self.get_green_function_continued_fraction(np.array([0]), False, smearing = smearing)

    w2 = 1 / np.real(gf)
    return np.float64(np.sqrt(np.abs(w2)) * np.sign(w2))

get_full_L_operator(verbose=False, only_pert=False, symmetrize=True, debug_d3=None, overwrite_L_operator=True)

GET THE FULL L OPERATOR

Use this method to test everithing. I returns the full L operator as a matrix. It is very memory consuming, but it should be fast and practical for small systems.

Results

L_op : ndarray(size = (nmodes * (nmodes + 1)), dtype = TYPE_DP) The full L operator.

Source code in tdscha/DynamicalLanczos.py
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
    def get_full_L_operator(self, verbose = False, only_pert = False, symmetrize = True, debug_d3 = None, overwrite_L_operator = True):
        """
        GET THE FULL L OPERATOR
        =======================

        Use this method to test everithing. I returns the full L operator as a matrix.
        It is very memory consuming, but it should be fast and practical for small systems.


        Results
        -------
           L_op : ndarray(size = (nmodes * (nmodes + 1)), dtype = TYPE_DP)
              The full L operator.
        """
        # The L operator
        L_operator = np.zeros( shape = (self.n_modes + self.n_modes * self.n_modes, self.n_modes + self.n_modes * self.n_modes), dtype = TYPE_DP)

        # Fill the first part with the standard dynamical matrix
        if not only_pert:
            L_operator[:self.n_modes, :self.n_modes] = np.diag(self.w**2)


        w_a = np.tile(self.w, (self.n_modes,1)).ravel()
        w_b = np.tile(self.w, (self.n_modes,1)).T.ravel()
        chi_beta = -.5 * np.sqrt(w_a + w_b)/(np.sqrt(w_a)*np.sqrt(w_b))


        B_mat = (w_a + w_b)**2
        if not only_pert:
            L_operator[self.n_modes:, self.n_modes:] = np.diag(B_mat)


        # Compute the d3 operator
#         new_X = np.einsum("ia,a->ai", self.X, f_ups(self.w, self.T))
        if debug_d3 is None:
            N_eff = np.sum(self.rho)
            Y_weighted = np.einsum("ia, i->ia", self.Y, self.rho)
            #new_Y = np.einsum("ia,i->ai", self.Y, self.rho)

        if not self.ignore_v3:
            if not debug_d3 is None:
                d3 = debug_d3
            else:
                if verbose:
                    print("Computing d3...")
                d3_noperm = np.einsum("ia,ib,ic->abc", self.X, self.X, Y_weighted)
                d3_noperm /= -N_eff 

                # Apply the permuatations
                d3 = d3_noperm.copy()
                d3 += np.einsum("abc->acb", d3_noperm)
                d3 += np.einsum("abc->bac", d3_noperm)
                d3 += np.einsum("abc->bca", d3_noperm)
                d3 += np.einsum("abc->cab", d3_noperm)
                d3 += np.einsum("abc->cba", d3_noperm)
                d3 /= 6

                if verbose:
                    np.save("d3_modes_nosym.npy", d3)

                if symmetrize:
                    # Perform the standard symmetrization
                    d3 = symmetrize_d3_muspace(d3, self.symmetries)

                if verbose:
                    np.save("d3_modes_sym.npy", d3)
                    np.save("symmetries_modes.npy", self.symmetries)


            # Reshape the d3
            d3_reshaped = d3.reshape((self.n_modes, self.n_modes * self.n_modes))

            new_mat = np.einsum("ab,b->ab", d3_reshaped, chi_beta)

            L_operator[:self.n_modes, self.n_modes:] = new_mat
            L_operator[self.n_modes:, :self.n_modes] = new_mat.T

        if not self.ignore_v4:
            if verbose:
                print("Computing d4...")
            d4 =  np.einsum("ai,bi,ci,di", new_X, new_X, new_X, new_Y)
            d4 += np.einsum("ai,bi,ci,di", new_X, new_X, new_Y, new_X)
            d4 += np.einsum("ai,bi,ci,di", new_X, new_Y, new_X, new_X)
            d4 += np.einsum("ai,bi,ci,di", new_Y, new_X, new_X, new_X)
            d4 /= - 4 * N_eff

            if verbose:
                np.save("d4_modes_nosym.npy", d4)

            # Reshape the d4
            d4_reshaped = d4.reshape((self.n_modes * self.n_modes, self.n_modes * self.n_modes))

            new_mat = np.einsum("ab,a,b->ab", d4_reshaped, chi_beta, chi_beta)

            L_operator[self.n_modes:, self.n_modes:] += new_mat

        if verbose:
            print("L superoperator computed.")

        if overwrite_L_operator:
            print('overwriting the L operator')
            self.L_linop = L_operator

        return L_operator

get_full_L_operator_FT(verbose=False, debug_d3=None, symmetrize=True, overwrite_L_operator=True)

GET THE FULL L OPERATOR (FINITE TEMPERATURE)

Get the the full matrix L for the biconjugate Lanczos algorithm. Use this method to test everithing. I returns the full L operator as a matrix. It is very memory consuming, but it should be fast for small systems.

Maybe we need to drop the exchange between a,b because they are symmetric by definition. The double counting for the exchange of a,b is now fixed.

Now if self.use_harmonic == True we can compute the Wigner Lanczos matrix

Results

L_op : ndarray(size = (nmodes * (2*nmodes + 1)), dtype = TYPE_DP) The full L operator.

Source code in tdscha/DynamicalLanczos.py
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
def get_full_L_operator_FT(self, verbose = False, debug_d3 = None, symmetrize = True, overwrite_L_operator = True):
    """
    GET THE FULL L OPERATOR (FINITE TEMPERATURE)
    ============================================

    Get the the full matrix L for the biconjugate Lanczos algorithm.
    Use this method to test everithing. I returns the full L operator as a matrix.
    It is very memory consuming, but it should be fast for small systems.

    Maybe we need to drop the exchange between a,b because they are symmetric by definition.
    The double counting for the exchange of a,b is now fixed.

    Now if self.use_harmonic == True we can compute the Wigner Lanczos matrix

    Results
    -------
       L_op : ndarray(size = (nmodes * (2*nmodes + 1)), dtype = TYPE_DP)
          The full L operator.
    """
    # The elements where w_a and w_b are exchanged are dependent
    # So we must avoid including them
    i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
    i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

    new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
    new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

    w_a = self.w[new_i_a]
    w_b = self.w[new_i_b]

    # N_w2 is the number of independent indeces
    N_w2 = len(w_a)

    if verbose:
        print()
        print('Getting the analytical L operator')
        print('Am I using Wigner? {}'.format(self.use_wigner))
    # Prepare the operator
    L_operator = np.zeros(shape = (self.n_modes + 2*N_w2, self.n_modes + 2*N_w2), dtype = TYPE_DP)

    # Set the Z'' harmonic
    if not self.ignore_harmonic:
        if not self.use_wigner:
            if verbose:
                print('STANDARD harmonic on R(1)')
            L_operator[:self.n_modes, :self.n_modes] = np.diag(self.w**2)
        else:
            if verbose:
                print('WIGNER harmonic on R(1)')
            L_operator[:self.n_modes, :self.n_modes] = -np.diag(self.w**2)

    #w_a = np.tile(self.w, (self.n_modes,1)).ravel()
    #w_b = np.tile(self.w, (self.n_modes,1)).T.ravel()

    n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
    n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)
    if self.T > 0:
        n_a = 1 / (np.exp(w_a * 157887.32400374097/self.T) - 1)
        n_b = 1 / (np.exp(w_b * 157887.32400374097/self.T) - 1)

    if verbose:
        print("BE occ number", n_a[:self.n_modes])

    # Apply the non interacting X operator
    start_Y = self.n_modes
    start_A = self.n_modes + N_w2

    # Get the operator that exchanges the frequencies
    # For each index i (a,b), exchange_frequencies[i] is the index that correspond to (b,a)
    #exchange_frequencies = np.array([ (i // self.n_modes) + self.n_modes * (i % self.n_modes) for i in np.arange(self.n_modes**2)])
    #xx = np.tile(np.arange(self.n_modes), (self.n_modes, 1)).T.ravel()
    #yy = np.tile(np.arange(self.n_modes), (self.n_modes, 1)).ravel()
    #all_modes = np.arange(self.n_modes**2)
    #exchange_frequencies = xx + yy
    if not self.ignore_harmonic:
        if not self.use_wigner:
            if verbose:
                print('STANDARD harmonic Y(1) ReA(1)')    
            # NOTE the double counting is NOT required for harmonic propagation
            # just check the harmonic matrix element
            extra_count = np.ones(N_w2, dtype = np.intc)
            extra_count[new_i_a == new_i_b] = 1.

            # Harmonic evolution for Y -> Y sector
            X_ab_NI = -w_a**2 - w_b**2 - (2*w_a *w_b) /((2*n_a + 1) * (2*n_b + 1))
            L_operator[start_Y: start_A, start_Y:start_A] = - np.diag(X_ab_NI)  * extra_count
            #L_operator[start_Y + np.arange(self.n_modes**2) , start_Y + exchange_frequencies] -= X_ab_NI / 2

            # Harmonic evolution for Y -> ReA sector
            Y_ab_NI = - (8 * w_a * w_b) / ((2*n_a + 1) * (2*n_b + 1))
            L_operator[start_Y : start_A, start_A:] = - np.diag(Y_ab_NI) * extra_count
            #L_operator[start_Y + np.arange(self.n_modes**2), start_A + exchange_frequencies] -=  Y_ab_NI / 2


            # Harmonic evolution for ReA -> Y sector
            X1_ab_NI = - (2*n_a*n_b + n_a + n_b) * (2*n_a*n_b + n_a + n_b + 1)*(2 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))
            L_operator[start_A:, start_Y : start_A] = - np.diag(X1_ab_NI) / 1 * extra_count
            #L_operator[start_A + np.arange(self.n_modes**2), start_Y + exchange_frequencies] -= X1_ab_NI / 2

            # Harmonic evolution for ReA -> ReA sector
            Y1_ab_NI = - w_a**2 - w_b**2 + (2*w_a *w_b) /( (2*n_a + 1) * (2*n_b + 1))
            L_operator[start_A:, start_A:] = -np.diag(Y1_ab_NI) / 1 * extra_count
            #L_operator[start_A + np.arange(self.n_modes**2),  start_A + exchange_frequencies] -= Y1_ab_NI / 2
        else:
            # The harmonic application in Wigner is diagonal
            # NOTE the double counting is NOT required here
            # just check the harmonic matrix element
            if verbose:
                print("WIGNER harmonic a'(1) b'(1)")
            # Diagonal harmonic propagation in Winger for a(1)
            a_NI  = -(w_a**2 + w_b**2 - 2 * w_a * w_b)
            L_operator[start_Y: start_A, start_Y: start_A] = + np.diag(a_NI)  

            # Diagonal harmonic propagation in Winger for b(1)
            b_NI = -(w_a**2 + w_b**2 + 2. * w_a * w_b)
            L_operator[start_A:, start_A:] = + np.diag(b_NI)


    # We ADDED all the non interacting (harmonic) propagators both for standard and Wigner
    # In WIgner the Harmonic approach is working

    # Compute the d3 operator

    #new_X = np.einsum("ia,a->ai", self.X, f_ups(self.w, self.T))
    if debug_d3 is None:
        N_eff = np.sum(self.rho)
        Y_weighted = np.einsum("ia, i -> ia", self.Y, self.rho)
    #new_Y = np.einsum("ia,i->ai", self.Y, self.rho)

    if not self.ignore_v3:
        if verbose:
            print("Computing d3...")
        if not debug_d3 is None:
            d3 = debug_d3
        else:
            X_ups = np.einsum("ia, a -> ia", self.X, f_ups(self.w, self.T))

            d3_noperm = np.einsum("ia, ib, ic -> abc", X_ups, X_ups, Y_weighted)
            d3_noperm /= -N_eff 

            # Apply the permuatations
            d3 = d3_noperm.copy()
            d3 += np.einsum("abc->acb", d3_noperm)
            d3 += np.einsum("abc->bac", d3_noperm)
            d3 += np.einsum("abc->bca", d3_noperm)
            d3 += np.einsum("abc->cab", d3_noperm)
            d3 += np.einsum("abc->cba", d3_noperm)
            d3 /= 6

            if verbose:
                np.save("d3_modes_nosym.npy", d3)

            # Perform the standard symmetrization
            if symmetrize:
                # TODO: fix the symmetrize_d3_muspace
                raise NotImplementedError('The symmetrizaiton on d3 is not implemented')
                d3 = symmetrize_d3_muspace(d3, self.symmetries)

                if verbose:
                    np.save("d3_modes_sym.npy", d3)
                    np.save("symmetries_modes.npy", self.symmetries)


        # Reshape the d3
        d3_small_space = np.zeros((N_w2, self.n_modes), dtype = np.double)
        # Get the d3 in the small space by getting the independent terms
        d3_small_space[:,:] = d3[new_i_a, new_i_b, :]

        if verbose:
            print("D3 of the following elements:")
            print(new_i_a)
            print(new_i_b)
            print("D3 small space")
            print(d3_small_space)
            print('D3 complete')
            print(d3)

        #d3_reshaped = d3.reshape((self.n_modes* self.n_modes, self.n_modes))
        #d3_reshaped1 = d3.reshape((self.n_modes, self.n_modes* self.n_modes))

        if not self.use_wigner:
            if verbose:
                print('STANDARD getting the D3 contribution')
            # Get the Z coeff between Y with R
            Z_coeff = 2 * ((2*n_a + 1)*w_b + (2*n_b + 1)*w_a) / ((2*n_a + 1) * (2*n_b + 1))
            Z_coeff = np.einsum("ab, a -> ab", d3_small_space, Z_coeff)
            L_operator[start_Y: start_A, :start_Y] = -Z_coeff

            # Get the Z' coeff between ReA and R
            Z1_coeff = 2 *((2*n_a + 1)*w_b*n_b*(n_b + 1) + (2*n_b + 1)*w_a*n_a*(n_a+1)) / ((2*n_a + 1) * (2*n_b + 1))
            Z1_coeff = np.einsum("ab, a -> ab", d3_small_space, Z1_coeff)
            L_operator[start_A:, :start_Y] = - Z1_coeff

            # Get the X'' coeff between R and Y with double counting
            extra_count = np.ones(N_w2, dtype = np.intc)
            extra_count[new_i_a != new_i_b] = 2
            X2_coeff = (2*n_b + 1) * (2*n_a +1) / (8*w_a *w_b)
            X2_coeff = np.einsum("ab, a -> ba", d3_small_space, X2_coeff * extra_count)
            L_operator[:start_Y, start_Y: start_A] = -X2_coeff

            # The coeff between R and ReA is zero.
        else:
            if verbose:
                print('WIGNER getting the D3 contribution')
            # Chi for the independent indeces
            chi_minus = ((n_a - n_b) * (w_a - w_b) /(2 * w_a * w_b)) 
            chi_plus  = ((1 + n_a + n_b) * (w_a + w_b) /(2 * w_a * w_b))

            # The shape of these tensors is (N_w2, n_modes) considering double counting
            extra_count_w = np.ones(N_w2, dtype = np.intc)
            extra_count_w[new_i_a != new_i_b] = 2
            d3_chi_plus  = np.einsum('ab, a -> ab', d3_small_space, np.sqrt(+0.5 * chi_plus)  * extra_count_w)
            d3_chi_minus = np.einsum('ab, a -> ab', d3_small_space, np.sqrt(-0.5 * chi_minus) * extra_count_w)

            # The interacion between a rank 1 tensor a rank 2 tensor DOES require
            # to take into account double counting

            # The coeff betwee R'(1) and a'(1)
            L_operator[:start_Y, start_Y: start_A] = +d3_chi_minus.T
            # The coeff betwee R'(1) and b'(1)
            L_operator[:start_Y, start_A:] = -d3_chi_plus.T

            # The interacion between a rank 2 tensor a rank 1 tensor DOES NOT require
            # to take into account double counting

            # The shape of these tensors is (N_w2, n_modes)
            chi_plus_d3  = np.einsum('ab, a -> ab', d3_small_space, np.sqrt(+0.5 * chi_plus))
            chi_minus_d3 = np.einsum('ab, a -> ab', d3_small_space, np.sqrt(-0.5 * chi_minus))

            # The coeff betwee a'(1) and R'(1)
            L_operator[start_Y: start_A, :start_Y] = +chi_minus_d3
            # The coeff betwee b'(1) and R'(1)
            L_operator[start_A:, :start_Y] = -chi_plus_d3 


    if not self.ignore_v4:
        # Get the D4 tensor in the polarization basis
        # it should be symmetric under permutations of the indices
        d4 =  np.einsum("ia, ib, ic, id -> abcd", X_ups, X_ups, X_ups, Y_weighted)
        d4 += np.einsum("ia, ib, ic, id -> abcd", X_ups, X_ups, Y_weighted, X_ups)
        d4 += np.einsum("ia, ib, ic, id -> abcd", X_ups, Y_weighted, X_ups, X_ups)
        d4 += np.einsum("ia, ib, ic, id -> abcd", Y_weighted, X_ups, X_ups, X_ups)
        d4 /= - 4 * N_eff

        if verbose:
            np.save("d4_modes_nosym.npy", d4)

        # TODO: add the standard symmetrization
        if symmetrize:
            raise NotImplementedError('The symmetrizaiton on d4 is not implemented')

        # Get the independent first two indep indices
        d4_small_space1 = np.zeros((N_w2, self.n_modes, self.n_modes), dtype = np.double)
        d4_small_space1[:,:,:] = d4[new_i_a, new_i_b, :, :]

        # Get the independent second two indep indices
        d4_small_space = np.zeros((N_w2, N_w2), dtype = np.double)
        d4_small_space[:,:] = d4_small_space1[:, new_i_a, new_i_b]

        if verbose:
            print("D4 of the following elements:")
            print(new_i_a)
            print(new_i_b)
            print("D4 in the reduced space")
            print(d4_small_space)
            print("D4 complete")
            print(d4)

        if not self.use_wigner:
            if verbose:
                print('STANDARD getting the D4 contribution')
            # When two tensors of rank 2 interact we need a factor of 2 overall
            extra_count = np.ones(N_w2, dtype = np.intc)
            extra_count[new_i_a != new_i_b] = 2

            # Anharmonic interaction of Y(1) with Y(1)
            X_coeff_left = -((2 * w_a * n_b + 2 * w_b * n_a + w_a + w_b)) /(4 * (2*n_a + 1) * (2*n_b + 1))
            X_coeff_right = ((2 * n_a + 1) * (2 * n_b + 1)) /(w_a * w_b)
            X_coeff = np.einsum('a, ab, b -> ab', X_coeff_left, d4_small_space, X_coeff_right * extra_count)
            L_operator[start_Y:start_A, start_Y:start_A] += -X_coeff

            # Anharmonic interaction of ReA(1) with Y(1)
            X1_coeff_left = -(w_a * n_a * (n_a + 1) * (2 * n_b + 1) + w_b * n_b * (n_b + 1) * (2 * n_a + 1)) /(4 * (2*n_a + 1) * (2*n_b + 1))
            X1_coeff_right = ((2 * n_a + 1) * (2 * n_b + 1))/(w_a * w_b)
            X1_coeff = np.einsum('a, ab, b -> ab', X1_coeff_left, d4_small_space, X1_coeff_right * extra_count)
            L_operator[start_A:, start_Y:start_A] += -X1_coeff
        else:
            if verbose:
                print('WIGNER getting the D4 contribution')
            # When two tensors of rank 2 interact we need a factor of 2 overall
            extra_count_w = np.ones(N_w2, dtype = np.intc)
            extra_count_w[new_i_a != new_i_b] = 2

            chi_plus  = ((1 + n_a + n_b) * (w_a + w_b) /(2 * w_a * w_b))
            chi_minus  = ((n_a - n_b) * (w_a - w_b) /(2 * w_a * w_b))

            plus_D4_plus   = np.einsum('a, ab, b -> ab', np.sqrt(+0.5 * chi_plus),  d4_small_space, np.sqrt(+0.5 * chi_plus) * extra_count_w)
            minus_D4_minus = np.einsum('a, ab, b -> ab', np.sqrt(-0.5 * chi_minus), d4_small_space, np.sqrt(-0.5 * chi_minus) * extra_count_w)
            minus_D4_plus  = np.einsum('a, ab, b -> ab', np.sqrt(-0.5 * chi_minus), d4_small_space, np.sqrt(+0.5 * chi_plus) * extra_count_w)
            plus_D4_minus  = np.einsum('a, ab, b -> ab', np.sqrt(+0.5 * chi_plus) , d4_small_space, np.sqrt(-0.5 * chi_minus) * extra_count_w)

            # Anharmonic interaction of a'(1) and a'(1)
            L_operator[start_Y: start_A, start_Y: start_A] += -minus_D4_minus

            # Anharmonic interaction of b'(1) and b'(1)
            L_operator[start_A:, start_A:] += -plus_D4_plus

            # Anharmonic interaction of a'(1) and b'(1)
            L_operator[start_Y: start_A, start_A :] += +minus_D4_plus

            # Anharmonic interaction of b'(1) and a'(1)
            L_operator[start_A :, start_Y: start_A] += +plus_D4_minus

    if verbose:
        print("L superoperator computed.")
        if not self.use_wigner:
            np.savez_compressed("L_super_analytical_standard.npz", L_operator)
        else:
            np.savez_compressed("L_super_analytical_wigner.npz", L_operator)

    if overwrite_L_operator:
        if verbose:
            print('Overwriting the L operator..')
            print('Am I using Wigner? {}'.format(self.use_wigner))
        def matvec(x):
            return L_operator.dot(x)
        def rmatvec(x):
            return x.dot(L_operator)

        self.L_linop = scipy.sparse.linalg.LinearOperator(L_operator.shape, matvec = matvec, rmatvec = rmatvec)

    return L_operator

mask_dot_wigner(debug=False)

Builds a mask in order to do a symmetric Lanczos.

The Lanczos is symmetric in the basis where we store all the matrices, so when we run a Lanczos in all the scalar product we need to take into account a double counting for the dependent indeces

Returns:
-double_mask: nd.array with size = n_modes + n_modes * (n_modes + 1)
Source code in tdscha/DynamicalLanczos.py
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
def mask_dot_wigner(self, debug = False):
    """
    Builds a mask in order to do a symmetric Lanczos.

    The Lanczos is symmetric in the basis where we store all the matrices, so
    when we run a Lanczos in all the scalar product we need to take into account
    a double counting for the dependent indeces

    Returns:
    --------
        -double_mask: nd.array with size = n_modes + n_modes * (n_modes + 1)
    """
    # Prepare the result
    double_mask = np.ones((self.n_modes + self.n_modes * (self.n_modes + 1)))

    # Where a'(1) and b'(1) starts
    start_a = self.n_modes
    start_b = self.n_modes + (self.n_modes * (self.n_modes + 1))//2

    # Get the indep indices
    i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
    i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

    # Avoid the exchange of indices
    new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
    new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

    if debug:
        print()
        print("start_a'(1) = ", start_a)
        print("start_b'(1) = ", start_b)
        print('new_i_b')
        print(new_i_b)
        print('new_i_a')
        print(new_i_a)

    # Where we have dep indices insert a 2 for double ocunting
    double_mask[start_a: start_b][new_i_b < new_i_a] = 2
    double_mask[start_b:][new_i_b < new_i_a] = 2

    if debug:
        print('mask dot prod for R(1) = ')
        print(double_mask[:start_a])
        print("mask dot prod for a'(1) = ")
        print(double_mask[start_a: start_b])
        print("mask dot prod for b'(1) = ")
        print(double_mask[start_b:])
        print()

    return double_mask

run_FT(n_iter, save_dir=None, save_each=5, verbose=True, n_rep_orth=0, n_ortho=10, flush_output=True, debug=False, prefix='LANCZOS', run_simm=None, optimized=False)

RUN LANCZOS ITERATIONS FOR FINITE TEMPERATURE

This method performs the biconjugate Lanczos algorithm to find the sequence of a and b and c coefficients that are the tridiagonal representation of the L matrix to be inverted.

NOTE: when we use the Wigner formalism the Lanczos matrix is symmetric in the vector space where all the elements of the tensors are considered (also those that are related by symmetry). Since the application of L is done in the reduced space where we discart these elements we have to take into account this by multiplying by two the off diagonal components in the scalar products.

Source code in tdscha/DynamicalLanczos.py
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
    def run_FT(self, n_iter, save_dir = None, save_each = 5, verbose = True, n_rep_orth = 0, n_ortho = 10, flush_output = True, debug = False, prefix = "LANCZOS", run_simm = None, optimized = False):
        """
        RUN LANCZOS ITERATIONS FOR FINITE TEMPERATURE
        =============================================

        This method performs the biconjugate Lanczos algorithm to find
        the sequence of a and b and c coefficients that are the tridiagonal representation 
        of the L matrix to be inverted.

        NOTE: when we use the Wigner formalism the Lanczos matrix is symmetric in the vector space where all the elements
        of the tensors are considered (also those that are related by symmetry). Since the application of L
        is done in the reduced space where we discart these elements we have to take into
        account this by multiplying by two the off diagonal components in the scalar products.

        Parameters
        ----------
            n_iter : int
                The number of iterations to be performed in the Lanczos algorithm.
            save_dir : string
                The directory in which you want to store the results step by step,
                in order to do a preliminar analysis or restart the calculation later.
                If None (default), the steps are not saved.
            save_each : int
                If save dir is not None, the results are saved each N step, with N the value of save_each argument.
            verbose : bool
                If true all the info during the minimization will be printed on output.
            n_rep_orth : int
                The number of times in which the GS orthonormalization is repeated.
                The higher, the lower the precision of the Lanczos step, the lower, the higher
                the probability of finding ghost states
            n_ortho : int
                The number of vectors to be considered for the GS biorthogonalization. (if None, all are considered)
            flush_output : bool
                If true it flushes the output at each step. 
                This is usefull to avoid ending without any output if a calculation is killed before it ends normally.
                However, it could slow down things a bit on clusters.
            debug : bool
                If true prints a lot of more info about the Lanczos
                as the gram-shmidth procdeure and checks on the coefficients. 
                This is usefull to spot an error or the appeareance of ghost states due to numerical inaccuracy.
            run_simm : bool
                If true the biconjugate Lanczos is transformed in a simple Lanczos with corrections in the scalar product. This is only possible if we are using the Wigner representation, where the Lanczos matrix is symmetric in the full space.
            optimized : bool
                If True we pop the vectors P and Q that we do not use during the Lanczos
        """

        self.verbose = verbose
        # Check if the symmetries has been initialized
        if not self.initialized:
            if verbose:
                print('Not initialized. Now we symmetrize\n')
            self.prepare_symmetrization()

        # Check if the psi vector is prepared
        ERROR_MSG = """
Error, you must initialize a perturbation to start the Lanczos.
Use prepare_raman/ir or prepare_perturbation before calling the run method.
"""
        if self.psi is None:
            print(ERROR_MSG)
            raise ValueError(ERROR_MSG)

        psi_norm = np.sum(self.psi**2)
        if np.isnan(psi_norm) or psi_norm == 0:
            print(ERROR_MSG)
            raise ValueError(ERROR_MSG)

        # If save_dir does not exist, create it
        if Parallel.am_i_the_master():
            if save_dir is not None:
                if not os.path.exists(save_dir):
                    os.makedirs(save_dir)

        # Automatically use the symmetric Lanczos if we are using the Wigner representation
        if run_simm is None:
            run_simm = self.use_wigner

        # run_simm is allowed only if we use the wigner representation
        if run_simm and not self.use_wigner:
            raise NotImplementedError('The symmetric Lanczos works only with Wigner. Set use_wigner to True and make sure that you are not using the analytic wigner L!')


        # Getting the mask product for the Wigner implementation
        if run_simm:
            if verbose:
                print('Running the standard Lanczos algorithm with Wigner')
                print('Getting the mask dot product')
                print()
            mask_dot = self.mask_dot_wigner(debug)



        # Get the current step
        i_step = len(self.a_coeffs)

        if verbose:
            header = """
<=====================================>
|                                     |
|          LANCZOS ALGORITHM          |
|                                     |
<=====================================>

Starting the algorithm. It may take a while.
Starting from step %d
""" % i_step
            print(header)

            OPTIONS = """
Should I ignore the third order effect? {}
Should I ignore the fourth order effect? {}
Should I use the Wigner formalism? {}
Should I use a standard Lanczos? {}
Max number of iterations: {}
""".format(self.ignore_v3, self.ignore_v4, self.use_wigner, run_simm, n_iter)
            print(OPTIONS)


        # If this is the current step initialize the algorithm
        if i_step == 0:
            self.basis_Q = []
            self.basis_P = []
            self.s_norm = []
            # Normalize the first vector in the Standard or Wigner representation
            if not run_simm:
                first_vector = self.psi / np.sqrt(self.psi.dot(self.psi))
            else:
                first_vector = self.psi / np.sqrt(self.psi.dot(self.psi * mask_dot))
            self.basis_Q.append(first_vector)
            self.basis_P.append(first_vector)
            self.s_norm.append(1)
        else:
            print('Restarting the Lanczos')
            print('There is no control on the len of basis_Q')
            # Convert everything in a list
            self.basis_Q = list(self.basis_Q)
            self.basis_P = list(self.basis_P)
            self.s_norm  = list(self.s_norm)
            self.a_coeffs = list(self.a_coeffs)
            self.b_coeffs = list(self.b_coeffs)
            self.c_coeffs = list(self.c_coeffs)
            #self.arnoldi_matrix = list(self.arnoldi_matrix)


            # if len(self.basis_Q) != i_step + 1:
            #     print("Krilov dim: %d, number of steps perfomed: %d" % (len(self.basis_Q), i_step))
            #     print("Error, the Krilov basis dimension should be 1 more than the number of steps")
            #     raise ValueError("Error the starting krilov basis does not matches the matrix, Look stdout.")

        assert len(self.basis_Q) == len(self.basis_P), "Something wrong when restoring the Lanczos."
        assert len(self.s_norm) == len(self.basis_P), "Something wrong when restoring the Lanczos."
        assert len(self.b_coeffs) == len(self.c_coeffs), "Something wrong when restoring the Lanczos. len b = {} len c = {}".format(len(self.b_coeffs), len(self.c_coeffs))


        # Select the two vectors for the biconjugate Lanczos iterations
        psi_q = self.basis_Q[-1]
        psi_p = self.basis_P[-1]

        if debug:
            print("Q basis:", self.basis_Q)
            print("P basis:", self.basis_P)
            print("S norm:", self.s_norm)
            print("SHAPE PSI Q, P :", psi_q.shape, psi_p.shape)

        # Convergence flag
        next_converged = False

        # Here starts the Lanczos
        for i in range(i_step, i_step + n_iter):
            if verbose:
                step_txt = """
 ===== NEW STEP %d =====

 """ % (i + 1)
                print(step_txt)
                print("Length of the coefficiets: a = {}, b = {}".format(len(self.a_coeffs), len(self.b_coeffs)))
                print()

                if flush_output:
                    sys.stdout.flush()

            # Application of L
            t1 = time.time()
            if not self.use_wigner:
                if verbose:
                    print("Running the BICONJUGATE Lanczos with standard representation!\n")
                    print()
                L_q = self.L_linop.matvec(psi_q)
                # psi_p is normalized (this must be considered when computing c coeff) 
                p_L = self.L_linop.rmatvec(psi_p) 
            else:
                if verbose:
                    print("The Wigner representation is used!\n")
                    print()
                # Get the application on psi_q
                L_q = self.L_linop.matvec(psi_q)
                if run_simm:
                    # This is done because we are running the symmetric Lanczos q=p
                    if verbose:
                        print()
                        print("Running the SYMMETRIC Lanczos with Wigner!\n")
                    p_L = np.copy(L_q)
                else:
                    # This should be done only with the analytical Wigner Matrix only for testing
                    # This should be done in the case q != p
                    if verbose:
                        print()
                        print("Running the BICONJUGATE Lanczos with Wigner analytic!\n")
                    p_L = self.L_linop.rmatvec(psi_p)   
            t2 = time.time()
            # End of L application

            if debug:
                if not run_simm:
                    print("Modulus of L_q: {}".format(np.sqrt(L_q.dot(L_q))))
                    print("Modulus of p_L: {}".format(np.sqrt(p_L.dot(p_L))))
                else:
                    print("Modulus of L_q: {}".format(np.sqrt(L_q.dot(L_q * mask_dot))))
                    print("Modulus of p_L: {}".format(np.sqrt(p_L.dot(p_L * mask_dot))))

            # Get the normalization of p_k (with respect to s_k)
            c_old = 1
            if len(self.c_coeffs) > 0:
                c_old = self.c_coeffs[-1]
            p_norm = self.s_norm[-1] / c_old
            if debug:
                print("p_norm: {}".format(p_norm))

            # Get the a coefficient
            if not run_simm:
                a_coeff = psi_p.dot(L_q) * p_norm
            else:
                a_coeff = psi_p.dot(L_q * mask_dot) * p_norm

            # Check if something whent wrong
            if np.isnan(a_coeff):
                ERR_MSG = """
Invalid value encountered during the Lanczos.
Check if you have correctly initialized the algorithm.
This may happen if the SCHA matrix has imaginary or zero frequencies,
or if the acoustic sum rule is not satisfied.
"""
                raise ValueError(ERR_MSG)    

            # Get the two residual vectors
            rk = L_q - a_coeff * psi_q
            # If this is not the first step
            if len(self.basis_Q) > 1:
                rk -= self.c_coeffs[-1] * self.basis_Q[-2]

            sk = p_L - a_coeff * psi_p 
            old_p_norm = 0
            # If this is not the first step
            if len(self.basis_P) > 1:
                # Get the multiplication factor to rescale the old p to the normalization of the new one.
                if len(self.c_coeffs) < 2:
                    old_p_norm = self.s_norm[-2]
                else:
                    old_p_norm = self.s_norm[-2] / self.c_coeffs[-2] 
                    # C is smaller than s_norm as it does not contain the first vector
                    # But this does not matter as we are counting from the end of the array

                # TODO: Check whether it better to use this or the default norms to update sk
                sk -= self.b_coeffs[-1] * self.basis_P[-2] * (old_p_norm / p_norm)

            # Get the normalization of sk 
            if not run_simm:
                s_norm = np.sqrt(sk.dot(sk))
            else:
                s_norm = np.sqrt(sk.dot(sk * mask_dot))

            # This normalization regularizes the Lanczos
            sk_tilde = sk / s_norm 
            # Add the p normalization of L^t p that was divided from the s_k
            s_norm *= p_norm 

            # Get the b and c coeffs
            if not run_simm:
                b_coeff = np.sqrt(rk.dot(rk))
                c_coeff = (sk_tilde.dot(rk / b_coeff)) * s_norm 
            else:
                b_coeff = np.sqrt(rk.dot(rk * mask_dot))
                c_coeff = (sk_tilde.dot((rk / b_coeff) * mask_dot)) * s_norm 


            if debug:
                print("new_p_norm: {}".format(s_norm / c_coeff))
                print("old_p_norm: {}".format(old_p_norm))

                print("Modulus of rk: {}".format(b_coeff))
                if not run_simm:
                    print("Modulus of sk: {}".format(np.sqrt(sk.dot(sk))))
                else:
                    print("Modulus of sk: {}".format(np.sqrt(sk.dot(sk * mask_dot))))

                if verbose:
                    print("Direct computation resulted in:")
                    print("     |  a = {}".format(a_coeff))
                    print("     |  b = {}".format(b_coeff))
                    print("     |  c = {}".format(c_coeff))
                    if run_simm:
                        print("     |  |b-c| = {}".format(np.abs(b_coeff - c_coeff)))

            # Check the convergence
            self.a_coeffs.append(a_coeff)
            if np.abs(b_coeff) < __EPSILON__ or next_converged:
                if verbose:
                    print("Converged (b coefficient is {}, |b| < {})".format(b_coeff, __EPSILON__))
                converged = True
                break 
            if np.abs(c_coeff) < __EPSILON__ or next_converged:
                if verbose:
                    print("Converged (c coefficient is {}, |c| < {})".format(c_coeff, __EPSILON__))
                converged = True
                break


            # Get the vectors for the next iteration
            psi_q = rk / b_coeff

            # psi_p is the normalized p vector, the sk_tilde one
            psi_p = sk_tilde.copy()


            # AFTER THIS p_norm refers to the norm of P in the previous step as psi_p has been updated
            if debug:
                if not run_simm:
                    print("1) Check c = ", psi_q.dot(p_L) * p_norm)
                    print("2) Check b = ", psi_p.dot(L_q) * s_norm / c_coeff)
                else:
                    print("1) Check c = ", psi_q.dot(p_L * mask_dot) * p_norm)
                    print("2) Check b = ", psi_p.dot(L_q * mask_dot) * s_norm / c_coeff)

            if debug:
                # Check the tridiagonality
                print("Tridiagonal matrix: (lenp: {}, lens: {})".format(len(self.basis_P), len(self.s_norm)))
                for k in range(len(self.basis_P)):
                    if k >= 1:
                        pp_norm = self.s_norm[k] / self.c_coeffs[k-1]
                    else:
                        pp_norm = self.s_norm[k]

                    if not run_simm:
                        print("p_{:d} L q_{:d} = {} | p_{:d} norm = {}".format(k, len(self.basis_P)-1, pp_norm * self.basis_P[k].dot(L_q), k, pp_norm))
                    else:
                        print("p_{:d} L q_{:d} = {} | p_{:d} norm = {}".format(k, len(self.basis_P)-1, pp_norm * self.basis_P[k].dot(L_q * mask_dot), k, pp_norm))

                pp_norm = s_norm / c_coeff
                if not run_simm:
                    print("p_{:d} L q_{:d} = {} | p_{:d} norm = {}".format(len(self.basis_P), len(self.basis_P)-1, pp_norm * psi_p.dot(L_q), k+1, pp_norm))
                else:
                    print("p_{:d} L q_{:d} = {} | p_{:d} norm = {}".format(len(self.basis_P), len(self.basis_P)-1, pp_norm * psi_p.dot(L_q * mask_dot), k+1, pp_norm))


                # Check the tridiagonality
                print()
                print("Transposed:".format(len(self.basis_P), len(self.s_norm)))
                if not run_simm:
                    for k in range(len(self.basis_Q)):
                        print("q_{:d} L^T p_{:d} = {} | p_{:d} norm = {}".format(k, len(self.basis_P)-1, p_norm* self.basis_Q[k].dot(p_L), k, p_norm))
                    print("q_{:d} L^T p_{:d} = {} | p_{:d} norm = {}".format(len(self.basis_P), len(self.basis_P)-1, p_norm* psi_q.dot(p_L), k+1, p_norm))
                else:
                    for k in range(len(self.basis_Q)):
                        print("q_{:d} L^T p_{:d} = {} | p_{:d} norm = {}".format(k, len(self.basis_P)-1, p_norm* self.basis_Q[k].dot(p_L * mask_dot), k, p_norm))
                    print("q_{:d} L^T p_{:d} = {} | p_{:d} norm = {}".format(len(self.basis_P), len(self.basis_P)-1, p_norm* psi_q.dot(p_L * mask_dot), k+1, p_norm))


            t1 = time.time()


            # Lets repeat twice the orthogonalization
            converged = False
            new_q = psi_q.copy()
            new_p = psi_p.copy()

            if debug:
                if not run_simm:
                    norm_q = np.sqrt(new_q.dot(new_q))
                    norm_p = np.sqrt(new_p.dot(new_p))
                    print("Norm of q = {} and p = {} BEFORE Gram-Schmidt".format(norm_q, norm_p))
                    print("current p dot q = {} (should be 1)".format(new_q.dot(new_p) * s_norm / c_coeff))
                else:
                    norm_q = np.sqrt(new_q.dot(new_q * mask_dot))
                    norm_p = np.sqrt(new_p.dot(new_p * mask_dot))
                    print("Norm of q = {} and p = {} before Gram-Schmidt".format(norm_q, norm_p))
                    print("current p dot q = {} (should be 1)".format(new_q.dot(new_p * mask_dot) * s_norm / c_coeff))


                # Check the Gram-Schmidt
                print("GS orthogonality check: (should all be zeros)")
                print("step) Q dot old Ps  | P dot old Qs")
                for k in range(len(self.basis_P)):
                    if k >= 1:
                        pp_norm = self.s_norm[k] / self.c_coeffs[k-1]
                    else:
                        pp_norm = self.s_norm[k]

                    if not run_simm:
                        q_dot_pold = self.basis_P[k].dot(new_q) * pp_norm
                        p_dot_qold = self.basis_Q[k].dot(new_p) * pp_norm
                    else:
                        q_dot_pold = self.basis_P[k].dot(new_q * mask_dot) * pp_norm
                        p_dot_qold = self.basis_Q[k].dot(new_p * mask_dot) * pp_norm
                    print("{:4d}) {:16.8e} | {:16.8e}".format(k, q_dot_pold, p_dot_qold))

            # Start the Gram Schmidt procedure        
            for k_orth in range(n_rep_orth):
                ortho_q = 0
                ortho_p = 0

                # The starting vector
                start = 0
                # n_ortho says how many vectors we include in the GS
                if n_ortho is not None:
                    start = len(self.basis_P) - n_ortho
                    if start < 0:
                        start = 0

                for j in range(start, len(self.basis_P)):
                    if not run_simm:
                        coeff1 = self.basis_P[j].dot(new_q)
                        coeff2 = self.basis_Q[j].dot(new_p)
                    else:
                        coeff1 = self.basis_P[j].dot(new_q * mask_dot)
                        coeff2 = self.basis_Q[j].dot(new_p * mask_dot)

                    # Gram Schmidt
                    new_q -= coeff1 * self.basis_P[j]
                    new_p -= coeff2 * self.basis_Q[j]

                    #print("REP {} COEFF {}: scalar: {}".format(k_orth+1, j, coeff1))

                    ortho_q += np.abs(coeff1)
                    ortho_p += np.abs(ortho_p)

                # Add the new vector to the Krilov Basis
                if not run_simm:
                    normq = np.sqrt(new_q.dot(new_q))
                else:
                    normq = np.sqrt(new_q.dot(new_q * mask_dot))
                if verbose:
                    print("Vector norm (q) after GS number {}: {:16.8e}".format(k_orth+1, normq))

                # Check the normalization (If zero the algorithm converged)
                if normq < __EPSILON__:
                    next_converged = True
                    if verbose:
                        print("Obtained a linear dependent Q vector.")
                        print("The algorithm converged.")

                new_q /= normq

                # Normalize the p vector
                if not run_simm:
                    normp = new_p.dot(new_p)
                else:
                    normp = new_p.dot(new_p * mask_dot)
                if verbose:
                    print("Vector norm (p biconjugate) after GS number {}: {:16.8e}".format(k_orth, normp))

                # Check the normalization (If zero the algorithm converged)
                if np.abs(normp) < __EPSILON__:
                    next_converged = True
                    if verbose:
                        print("Obtained a linear dependent P vector.")
                        print("The algorithm converged.")

                new_p /= normp

                # Now we need to update s_norm to enforce p dot q = 1
                if not run_simm:
                    s_norm = c_coeff / new_p.dot(new_q)
                else:
                    s_norm = c_coeff / new_p.dot(new_q * mask_dot)

                # We have a correctly satisfied orthogonality condition
                if ortho_p < __EPSILON__ and ortho_q < __EPSILON__:
                    break


            if not converged:
                # Add the new q and p vectors
                self.basis_Q.append(new_q)
                self.basis_P.append(new_p)
                psi_q = new_q.copy()
                psi_p = new_p.copy()

                # Add the new coefficients to the Arnoldi matrix
                self.b_coeffs.append(b_coeff)
                self.c_coeffs.append(c_coeff)
                self.s_norm.append(s_norm)

                # Pop the elements we do not use to optimize the use of memory
                if optimized:
                    print('Optimize RAM consumption')
                    if len(self.basis_P) > 3:
                        print('P basis popping the elements we do not use')
                        self.basis_P.pop(-4)

                    if len(self.basis_Q) > 3:
                        print('Q basis popping the elements we do not use')
                        self.basis_Q.pop(-4)

                    if len(self.s_norm) > 3:
                        print('s norm popping the elements we do not use')
                        self.s_norm.pop(-4)

            t2 = time.time()


            if verbose:
                print("Time to perform the Gram-Schmidt and retrive the coefficients: %d s" % (t2-t1))
                print()
                print("a_%d = %.8e" % (i, self.a_coeffs[-1]))
                print("b_%d = %.8e" % (i, self.b_coeffs[-1]))
                print("c_%d = %.8e" % (i, self.c_coeffs[-1]))
                if run_simm:
                    print("|b_%d - c_%d| = %.8e" % (i, i, np.abs(self.b_coeffs[-1] - self.c_coeffs[-1])))
                print()

            # Save the step
            if not save_dir is None:
                if (i + 1) % save_each == 0:
                    self.save_status("%s/%s_STEP%d" % (save_dir, prefix, i+1))

                    if verbose:
                        print("Status saved into '%s/%s_STEP%d'" % (save_dir, prefix, i+1))

            if verbose:
                print("Lanczos step %d ultimated." % (i +1))


        if converged:
            print("   last a coeff = {}".format(a_coeff))    

run_full_diag(number, discard_dyn=True, n_iter=100)

FULL LANCZOS DIAGONALIZATION

This function runs the standard Lanczos iteration progress. It returns the eigenvalues and eigenvectors of the L operator. These can be used for computing the spectral function, and the full green function as:

.. math ::

G_{ab}(\omega) = \sum_{\alpha} \frac{\left<a | \lambda_\alpha\right>\left<\lambda_\alpha|b\right>}{\lambda_\alpha - \omega^2 + i\eta}

where :math:\lambda are eigenvalues and vectors returned by this method, while :math:\eta is a smearing parameter chosen by the user. Remember the eigenvectors are defined in the polarization basis and they comprend also the dynamical matrix degrees of freedom. Since in most application you want to discard the dynamical matrices, you can select discard_din = True.

The used Lanczos algorithm is the one by ARPACK, as implemented in scipy.sparse module

Source code in tdscha/DynamicalLanczos.py
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
def run_full_diag(self, number, discard_dyn = True, n_iter = 100):
    r"""
    FULL LANCZOS DIAGONALIZATION
    ============================

    This function runs the standard Lanczos iteration progress.
    It returns the eigenvalues and eigenvectors of the L operator.
    These can be used for computing the spectral function, and the full
    green function as:

    .. math ::

        G_{ab}(\omega) = \sum_{\alpha} \frac{\left<a | \lambda_\alpha\right>\left<\lambda_\alpha|b\right>}{\lambda_\alpha - \omega^2 + i\eta}

    where :math:`\lambda` are eigenvalues and vectors returned by this method, while :math:`\eta` is a
    smearing parameter chosen by the user. 
    Remember the eigenvectors are defined in the polarization basis and they comprend also the dynamical matrix degrees of freedom.
    Since in most application you want to discard the dynamical matrices, you can select discard_din = True.

    The used Lanczos algorithm is the one by ARPACK, as implemented in scipy.sparse module

    Parameters
    ----------
        number = int
            The number of the n highest eigenvalues to be found
        discard_dyn : bool, optional
            If True the dynamical matrix component of the output eigenvectors will be discarded.
        n_iter : int, optional
            The maximum number of Lanczos iterations. Usually must be much higher than the
            number of states you want to describe.
    """

    # Perform the lanczos operation
    eigvals, eigvects = scipy.sparse.linalg.eigsh(self.L_linop, k = number, v0 = self.psi, ncv= n_iter)

    self.eigvals = eigvals
    self.eigvects = eigvects

    # Check if the dynamical part must be discarded
    if discard_dyn:
        eigvects = eigvects[:self.n_modes, :]


    return eigvals, eigvects

is_parallel()

Returns True if the MPI parallelization is active, False otherwise

Source code in tdscha/Parallel.py
27
28
29
30
31
32
33
34
def is_parallel():
    """
    Returns True if the MPI parallelization is active,
    False otherwise
    """
    if True in AllParallel:
        return True 
    return False

pprint(*argv)

PARALLEL PRINTING

This will print on stdout only once in parallel execution of the code

Source code in tdscha/Parallel.py
54
55
56
57
58
59
60
61
62
63
64
def pprint(*argv):
    """
    PARALLEL PRINTING
    =================

    This will print on stdout only once in parallel execution of the code
    """
    #print("pypar:", __PYPAR__)
    #print("mpi4py:", __MPI4PY__)
    if am_i_the_master():
        print(*argv)

f_ups(w, T)

The eigenvalue of the upsilon matrix as a function of the frequency and the temperature. This is (xi^2_\mu)^-1.

Parameters:
-w, frequencies in Rydberg
-T, temperature in Kelvin
Source code in tdscha/DynamicalLanczos.py
144
145
146
147
148
149
150
151
152
153
154
155
156
def f_ups(w, T):
    r"""
    The eigenvalue of the upsilon matrix as a function of the frequency and the
    temperature. This is (xi^2_\mu)^-1.

    Parameters:
    ----------
        -w, frequencies in Rydberg
        -T, temperature in Kelvin
    """

    n_w = bose_occupation(w, T)
    return 2*w / (1 + 2 *n_w)

bose_occupation(w, T)

The Bose-Einstein occupation. Assumes T in K and w in Ry.

Source code in tdscha/DynamicalLanczos.py
158
159
160
161
162
163
def bose_occupation(w, T):
    """ The Bose-Einstein occupation. Assumes T in K and w in Ry."""
    n_w = 0
    if T > 0:
        n_w = 1 / (np.exp(w * __RyToK__ / T) - 1)
    return n_w

SlowApplyD3ToDyn(X, Y, rho, w, T, input_dyn)

Apply the D3 vector.

This is a testing function. It is slow, as it is a pure python implementation.

Source code in tdscha/DynamicalLanczos.py
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
def SlowApplyD3ToDyn(X, Y, rho, w, T, input_dyn):
    """
    Apply the D3 vector.

    This is a testing function. It is slow, as it is a pure python implementation.
    """

    new_X = np.einsum("ab,b->ab", X, f_ups(w, T))


    n_rand, n_modes = np.shape(X)
    N_eff = np.sum(rho)

    v_out = np.zeros(n_modes, dtype = TYPE_DP)
    for a in range(n_modes):
        for b in range(n_modes):
            for c in range(n_modes):
                # Prepare the D3 calculation
                in_av = new_X[:, a] * new_X[:, b] * Y[:, c]
                in_av +=  new_X[:, a] * new_X[:, c] * Y[:, b]
                in_av +=  new_X[:, c] * new_X[:, b] * Y[:, a]
                in_av *= rho

                # Apply D3
                v_out[a] += - np.sum(in_av) * input_dyn[n_modes*b + c] / (3*N_eff)

    return v_out

FastApplyD3ToDyn(X, Y, rho, w, T, input_dyn, symmetries, n_degeneracies, degenerate_space, mode=1, transpose=False)

Apply the D3 to dyn

This is a wrapper to the fast C function.

For details on the mode, look at the parameters list

Results

output_vector : ndarray (size = n_modes) The result of the calculation

Source code in tdscha/DynamicalLanczos.py
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
def FastApplyD3ToDyn(X, Y, rho, w, T, input_dyn,  symmetries, n_degeneracies, degenerate_space, mode = 1, transpose = False):
    """
    Apply the D3 to dyn
    ======================

    This is a wrapper to the fast C function.


    For details on the mode, look at the parameters list

    Parameters
    ----------
       X : ndarray(size = (n_modes, n_configs), dtype = np.double / np.float32)
           The X array (displacement in mode basis). Note that the dtype should match the mode
       Y : ndarray(size = (n_modes, n_configs))
           The Y array (forces in mode basis).
       rho : ndarray(size = n_configs)
           The weights of the configurations
       w : ndarray(size = n_modes)
           The list of frequencies
       T : float
           The temperature
       input_dyn : ndarray (size = n_modes*n_modes)
           The vector of the input dynamical matrix
       mode : int
           The mode for the execution:
              1) CPU : OpenMP parallelization
       symmetries : ndarray( size =(n_sym, n_modes, n_modes), dtype = np.double)
           The symmetries in the polarization basis.
       n_degeneracies : ndarray( size = n_modes, dtype = np.intc)
           The number of degenerate eigenvalues for each mode
       degenerate_space : list of lists
           The list of modes in the eigen subspace in which that mode belongs to.


    Results
    -------
       output_vector : ndarray (size = n_modes)
           The result of the calculation
    """

    n_modes = len(w)

    #transp = 0
    #if transpose:
    #    transp = 1

    output_vector = np.zeros(n_modes, dtype = TYPE_DP)
    #print( "Apply to dyn, nmodes:", n_modes, "shape:", np.shape(output_vector))

    deg_space_new = np.zeros(np.sum(n_degeneracies), dtype = np.intc)
    i = 0
    i_mode = 0
    j_mode = 0
    #print("len1 = ", len(deg_space_new), "len2 = ", n_modes)
    #print("Mapping degeneracies:", np.sum(n_degeneracies))
    while i_mode < n_modes:
        #print("i= ", i_mode, "Ndeg:", n_degeneracies[i_mode], "j = ", j_mode, "len = ", len(degenerate_space[i_mode]))
        #print("new_i = ", i, "tot = ", np.sum(n_degeneracies))
        #print("cross_modes: ({}, {}) | deg_imu = {} | i = {}".format(i_mode, j_mode, n_degeneracies[i_mode], i))

        deg_space_new[i] = degenerate_space[i_mode][j_mode]
        j_mode += 1
        i+=1

        if j_mode == n_degeneracies[i_mode]:
            i_mode += 1
            j_mode = 0


    sscha_HP_odd.ApplyV3ToDyn(X, Y, rho, w, T, input_dyn, output_vector, mode, symmetries, n_degeneracies, deg_space_new)
    return output_vector

FastApplyD3ToVector(X, Y, rho, w, T, input_vector, symmetries, n_degeneracies, degenerate_space, mode=1)

Apply the D3 to vector

This is a wrapper to the fast C function.

For details on the mode, look at the parameters list

Results

output_dyn : ndarray (size = n_modes*n_modes) The result of the calculation

Source code in tdscha/DynamicalLanczos.py
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
def FastApplyD3ToVector(X, Y, rho, w, T, input_vector, symmetries, n_degeneracies, degenerate_space, mode = 1):
    """
    Apply the D3 to vector
    ======================

    This is a wrapper to the fast C function.


    For details on the mode, look at the parameters list

    Parameters
    ----------
       X : ndarray(size = (n_modes, n_configs), dtype = np.double / np.float32)
           The X array (displacement in mode basis). Note that the dtype should match the mode
       Y : ndarray(size = (n_modes, n_configs))
           The Y array (forces in mode basis).
       rho : ndarray(size = n_configs)
           The weights of the configurations
       w : ndarray(size = n_modes)
           The list of frequencies
       T : float
           The temperature
       input_vector : ndarray (size = n_modes)
           The input dynamical matrix
       mode : int
           The mode for the execution:
              1) CPU : OpenMP parallelization
       symmetries : ndarray( size =(n_sym, n_modes, n_modes), dtype = np.double)
           The symmetries in the polarization basis.
       n_degeneracies : ndarray( size = n_modes, dtype = np.intc)
           The number of degenerate eigenvalues for each mode
       degenerate_space : list of lists
           The list of modes in the eigen subspace in which that mode belongs to.

    Results
    -------
       output_dyn : ndarray (size = n_modes*n_modes)
           The result of the calculation
    """
    n_modes = len(w)
    output_dyn = np.zeros(n_modes*n_modes, dtype = TYPE_DP)
    #print( "Apply to vector, nmodes:", n_modes, "shape:", np.shape(output_dyn))

    deg_space_new = np.zeros(np.sum(n_degeneracies), dtype = np.intc)
    i = 0
    i_mode = 0
    j_mode = 0
    #print("Mapping degeneracies:", np.sum(n_degeneracies))
    while i_mode < n_modes:
        #print("cross_modes: ({}, {}) | deg_i = {}".format(i_mode, j_mode, n_degeneracies[i_mode]))
        deg_space_new[i] = degenerate_space[i_mode][j_mode]
        j_mode += 1
        i += 1
        if j_mode == n_degeneracies[i_mode]:
            i_mode += 1
            j_mode = 0

    sscha_HP_odd.ApplyV3ToVector(X, Y, rho, w, T, input_vector, output_dyn, mode, symmetries, n_degeneracies, deg_space_new)
    return output_dyn

FastD3_FT(X, Y, rho, w, T, input_psi, symmetries, n_degeneracies, degenerate_space, mode=1, transpose=False)

Apply the D3 to vector

This is a wrapper to the fast C function.

For details on the mode, look at the parameters list

Results

output_psi : ndarray The output density matrix

Source code in tdscha/DynamicalLanczos.py
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
def FastD3_FT(X, Y, rho, w, T, input_psi, symmetries, n_degeneracies, degenerate_space, mode = 1, transpose = False):
    """
    Apply the D3 to vector
    ======================

    This is a wrapper to the fast C function.


    For details on the mode, look at the parameters list

    Parameters
    ----------
       X : ndarray(size = (n_modes, n_configs), dtype = np.double / np.float32)
           The X array (displacement in mode basis). Note that the dtype should match the mode
       Y : ndarray(size = (n_modes, n_configs))
           The Y array (forces in mode basis).
       rho : ndarray(size = n_configs)
           The weights of the configurations
       w : ndarray(size = n_modes)
           The list of frequencies
       T : float
           The temperature
       input_psi : ndarray
           The input density matrix
       mode : int
           The mode for the execution:
              1) CPU : OpenMP parallelization
       symmetries : ndarray( size =(n_sym, n_modes, n_modes), dtype = np.double)
           The symmetries in the polarization basis.
       n_degeneracies : ndarray( size = n_modes, dtype = np.intc)
           The number of degenerate eigenvalues for each mode
       degenerate_space : list of lists
           The list of modes in the eigen subspace in which that mode belongs to.

    Results
    -------
       output_psi : ndarray 
           The output density matrix
    """
    n_modes = len(w)


    transp = 0
    if transpose:
        transp = 1

    total_length = len(input_psi)

    output_psi = np.zeros(total_length, dtype = TYPE_DP)
    #print( "Apply to vector, nmodes:", n_modes, "shape:", np.shape(output_dyn))

    # Get the start and end_A
    start_A = ((n_modes + 1) * n_modes) // 2 + n_modes 
    end_A = n_modes + (n_modes + 1) * n_modes


    deg_space_new = np.zeros(np.sum(n_degeneracies), dtype = np.intc)
    i = 0
    i_mode = 0
    j_mode = 0
    #print("Mapping degeneracies:", np.sum(n_degeneracies))
    while i_mode < n_modes:
        #print("cross_modes: ({}, {}) | deg_i = {}".format(i_mode, j_mode, n_degeneracies[i_mode]))
        deg_space_new[i] = degenerate_space[i_mode][j_mode]
        j_mode += 1
        i += 1
        if j_mode == n_degeneracies[i_mode]:
            i_mode += 1
            j_mode = 0

    print ("Degenerate space: ")
    print (deg_space_new)

    sscha_HP_odd.ApplyV3_FT(X, Y, rho, w, T, input_psi, output_psi, mode, symmetries, n_degeneracies, deg_space_new, start_A, end_A, transp)
    return output_psi

FastD4_FT(X, Y, rho, w, T, input_psi, symmetries, n_degeneracies, degenerate_space, mode=1)

Apply the D4 to vector

This is a wrapper to the fast C function.

For details on the mode, look at the parameters list

Results

output_psi : ndarray The output density matrix

Source code in tdscha/DynamicalLanczos.py
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
def FastD4_FT(X, Y, rho, w, T, input_psi, symmetries, n_degeneracies, degenerate_space, mode = 1):
    """
    Apply the D4 to vector
    ======================

    This is a wrapper to the fast C function.


    For details on the mode, look at the parameters list

    Parameters
    ----------
       X : ndarray(size = (n_modes, n_configs), dtype = np.double / np.float32)
           The X array (displacement in mode basis). Note that the dtype should match the mode
       Y : ndarray(size = (n_modes, n_configs))
           The Y array (forces in mode basis).
       rho : ndarray(size = n_configs)
           The weights of the configurations
       w : ndarray(size = n_modes)
           The list of frequencies
       T : float
           The temperature
       input_psi : ndarray
           The input density matrix
       mode : int
           The mode for the execution:
              1) CPU : OpenMP parallelization
       symmetries : ndarray( size =(n_sym, n_modes, n_modes), dtype = np.double)
           The symmetries in the polarization basis.
       n_degeneracies : ndarray( size = n_modes, dtype = np.intc)
           The number of degenerate eigenvalues for each mode
       degenerate_space : list of lists
           The list of modes in the eigen subspace in which that mode belongs to.

    Results
    -------
       output_psi : ndarray 
           The output density matrix
    """
    n_modes = len(w)

    total_length = len(input_psi)

    output_psi = np.zeros(total_length, dtype = TYPE_DP)
    #print( "Apply to vector, nmodes:", n_modes, "shape:", np.shape(output_dyn))

    # Get the start and end_A
    start_A = ((n_modes + 1) * n_modes) // 2 + n_modes 
    end_A = n_modes + (n_modes + 1) * n_modes


    deg_space_new = np.zeros(np.sum(n_degeneracies), dtype = np.intc)
    i = 0
    i_mode = 0
    j_mode = 0
    #print("Mapping degeneracies:", np.sum(n_degeneracies))
    # Preparing the symmetry variables for the fast calculation
    while i_mode < n_modes:
        #print("cross_modes: ({}, {}) | deg_i = {}".format(i_mode, j_mode, n_degeneracies[i_mode]))
        deg_space_new[i] = degenerate_space[i_mode][j_mode]
        j_mode += 1
        i += 1
        if j_mode == n_degeneracies[i_mode]:
            i_mode += 1
            j_mode = 0

    sscha_HP_odd.ApplyV4_FT(X, Y, rho, w, T, input_psi, output_psi, mode, symmetries, n_degeneracies, deg_space_new, start_A, end_A)
    return output_psi

SlowApplyD3ToVector(X, Y, rho, w, T, input_vector)

Apply the D3 vector.

This is a testing function. It is slow, as it is a pure python implementation.

Source code in tdscha/DynamicalLanczos.py
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
def SlowApplyD3ToVector(X, Y, rho, w, T, input_vector):
    """
    Apply the D3 vector.

    This is a testing function. It is slow, as it is a pure python implementation.
    """

    new_X = np.einsum("ab,b->ab", X, f_ups(w, T))

    n_rand, n_modes = np.shape(X)
    N_eff = np.sum(rho)

    v_out = np.zeros(n_modes*n_modes, dtype = TYPE_DP)
    for a in range(n_modes):
        for b in range(n_modes):
            for c in range(n_modes):
                # Prepare the D3 calculation
                in_av = new_X[:, a] * new_X[:, b] * Y[:, c]
                in_av +=  new_X[:, a] * new_X[:, c] * Y[:, b]
                in_av +=  new_X[:, c] * new_X[:, b] * Y[:, a]
                in_av *= rho

                # Apply D3
                v_out[a*n_modes + b] += - np.sum(in_av) * input_vector[c] / (3*N_eff)

    return v_out

SlowApplyD4ToDyn(X, Y, rho, w, T, input_dyn)

Apply the D4 matrix.

This is a testing function. It is slow, as it is a pure python implementation.

Source code in tdscha/DynamicalLanczos.py
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
def SlowApplyD4ToDyn(X, Y, rho, w, T, input_dyn):
    """
    Apply the D4 matrix.

    This is a testing function. It is slow, as it is a pure python implementation.
    """


    new_X = np.einsum("ab,b->ab", X, f_ups(w, T))


    n_rand, n_modes = np.shape(X)
    N_eff = np.sum(rho)

    v_out = np.zeros(n_modes*n_modes, dtype = TYPE_DP)
    for a in range(n_modes):
        for b in range(n_modes):
            for c in range(n_modes):
                for d in range(n_modes):
                    # Prepare the D3 calculation
                    in_av =  new_X[:, a] * new_X[:, b] * new_X[:, c] * Y[:, d]
                    in_av += new_X[:, a] * new_X[:, b] * Y[:, c] * new_X[:, d]
                    in_av += new_X[:, a] * Y[:, b] * new_X[:, c] * new_X[:, d]
                    in_av += Y[:, a] * new_X[:, b] * new_X[:, c] * new_X[:, d]

                    in_av *= rho

                    # Apply D3
                    v_out[a*n_modes + b] += - np.sum(in_av) * input_dyn[n_modes*c + d] / (4*N_eff)

    return v_out

FastApplyD4ToDyn(X, Y, rho, w, T, input_dyn, symmetries, n_degeneracies, degenerate_space, mode=1)

Apply the D3 to vector
======================

This is a wrapper to the fast C function.

Remember to use the correct dtype value:
if mode == GPU:
   dtype = np.float32
if mode == CPU:
   dtype = np.float64

get_ For details on the mode, look at the parameters list

Parameters
   X : ndarray(size = (n_modes, n_configs), dtype = np.double / np.float32)
       The X array (displacement in mode basis). Note that the dtype should match the mode
   Y : ndarray(size = (n_modes, n_configs))
       The Y array (forces in mode basis).
   rho : ndarray(size = n_configs)
       The weights of the configurations
   w : ndarray(size = n_modes)
       The list of frequencies
   T : float
       The temperature
   input_dyn : ndarray (size = n_modes*n_modes)
       The input dynamical matrix
   symmetries : ndarray( size =(n_sym, n_modes, n_modes), dtype = np.double)
       The symmetries in the polarization basis.
   n_degeneracies : ndarray( size = n_modes, dtype = np.intc)
       The number of degenerate eigenvalues for each mode
   degenerate_space : list of lists
       The list of modes in the eigen subspace in which that mode belongs to.
   mode : int
       The mode for the execution:
          1) CPU : OpenMP parallelization
Results
   output_dyn : ndarray (size = n_modes*n_modes)
       The result of the calculation
Source code in tdscha/DynamicalLanczos.py
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
def FastApplyD4ToDyn(X, Y, rho, w, T, input_dyn, symmetries, n_degeneracies, degenerate_space, mode = 1):
    """
    Apply the D3 to vector
    ======================

    This is a wrapper to the fast C function.

    Remember to use the correct dtype value:
    if mode == GPU:
       dtype = np.float32
    if mode == CPU:
       dtype = np.float64
get_
    For details on the mode, look at the parameters list

    Parameters
    ----------
       X : ndarray(size = (n_modes, n_configs), dtype = np.double / np.float32)
           The X array (displacement in mode basis). Note that the dtype should match the mode
       Y : ndarray(size = (n_modes, n_configs))
           The Y array (forces in mode basis).
       rho : ndarray(size = n_configs)
           The weights of the configurations
       w : ndarray(size = n_modes)
           The list of frequencies
       T : float
           The temperature
       input_dyn : ndarray (size = n_modes*n_modes)
           The input dynamical matrix
       symmetries : ndarray( size =(n_sym, n_modes, n_modes), dtype = np.double)
           The symmetries in the polarization basis.
       n_degeneracies : ndarray( size = n_modes, dtype = np.intc)
           The number of degenerate eigenvalues for each mode
       degenerate_space : list of lists
           The list of modes in the eigen subspace in which that mode belongs to.
       mode : int
           The mode for the execution:
              1) CPU : OpenMP parallelization

    Results
    -------
       output_dyn : ndarray (size = n_modes*n_modes)
           The result of the calculation
    """
    n_modes = len(w)
    output_dyn = np.zeros(n_modes*n_modes, dtype = TYPE_DP)


    deg_space_new = np.zeros(np.sum(n_degeneracies), dtype = np.intc)
    i = 0
    i_mode = 0
    j_mode = 0
    #print("Mapping degeneracies:", np.sum(n_degeneracies))
    while i_mode < n_modes:
        #print("cross_modes: ({}, {}) | deg_i = {}".format(i_mode, j_mode, n_degeneracies[i_mode]))
        deg_space_new[i] = degenerate_space[i_mode][j_mode]
        j_mode += 1
        i += 1
        if j_mode == n_degeneracies[i_mode]:
            i_mode += 1
            j_mode = 0



    #print( "Apply to vector, nmodes:", n_modes, "shape:", np.shape(output_dyn))
    sscha_HP_odd.ApplyV4ToDyn(X, Y, rho, w, T, input_dyn, output_dyn, mode, symmetries, n_degeneracies, deg_space_new)
    return output_dyn

GetFreeEnergyCurvatureFromContinuedFraction(a_ns, b_ns, pols_sc, masses, mode_mixing=True, use_terminator=True, last_average=5, smearing=0)

GET THE FREE ENERGY CURVATURE FROM MANY LANCZOS

This function computes the free energy curvature from the result of a full Lanczos computation between all possible perturbations.

Results
odd_fc : ndarray( (3*nat_sc, 3*nat_sc))
    The free energy curvature in the supercell
Source code in tdscha/DynamicalLanczos.py
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
def GetFreeEnergyCurvatureFromContinuedFraction(a_ns, b_ns, pols_sc, masses, mode_mixing = True,\
    use_terminator = True, last_average = 5, smearing = 0):
    """
    GET THE FREE ENERGY CURVATURE FROM MANY LANCZOS
    ===============================================

    This function computes the free energy curvature from the result
    of a full Lanczos computation between all possible perturbations.

    Parameters
    ----------
        a_ns : ndarray(size = (n_modes, n_modes, N_steps))
            The a_n coefficients for each Lanczos perturbation
        b_ns : ndarray(size = (n_modes, n_modes, N_steps-1))
            The b_n coefficients for each Lanczos perturbation
        pols_sc : ndarray(size = (3*nat_sc, n_modes))
            The polarization vectors in the supercell
        masses : ndarray(size = (3*nat_sc))
            The mass associated to each component of pols_sc
        use_terminator : bool
            If true the infinite volume interpolation is performed trought the
            terminator trick
        last_average : int
            Used in combination with the terminator, average the last 'last_average'
            coefficients and replicate them.
        smearing : float
            The smearing for the green function calculation. 
            Usually not needed for this kind of calculation.

    Results
    -------
        odd_fc : ndarray( (3*nat_sc, 3*nat_sc))
            The free energy curvature in the supercell

    """

    n_modes = np.shape(pols_sc)[1]
    nat_sc = int(np.shape(pols_sc)[0] / 3)
    N_steps = np.shape(a_ns)[2]

    assert N_steps -1 == np.shape(b_ns)[2], "Error, an and bn has an incompatible size:\n a_n = {}, b_n = {}".format(np.shape(a_ns), np.shape(b_ns))


    mat_pol = np.zeros( (n_modes, n_modes), dtype = np.double)
    for i in range(n_modes):

        # Get the number of steps
        n_steps = np.arange(N_steps-1)[b_ns[i, i, :] == 0]
        if len(n_steps) == 0:
            n_steps = N_steps
        else:
            n_steps = n_steps[0] + 1


        # Create the Lanczos class
        lanc = Lanczos(None)
        lanc.a_coeffs = a_ns[i, i, :n_steps]
        lanc.b_coeffs = b_ns[i, i, :n_steps - 1]
        lanc.perturbation_modulus = 1


        print("Computing ({},{}) ... n_steps = {}".format(i, i, n_steps))

        # get the green function from continued fraction
        gf = lanc.get_green_function_continued_fraction(np.array([0]), use_terminator = use_terminator, \
            smearing = smearing, last_average = last_average)[0]

        mat_pol[i,i] = np.real(gf)

    # If there is the mode-mixing compute also the off-diagonal terms
    if mode_mixing:
        for i in range(n_modes):
            for j in range(i+1, n_modes):
                # Get the number of steps
                n_steps = np.arange(N_steps-1)[b_ns[i, j, :] == 0]
                if len(n_steps) == 0:
                    n_steps = N_steps
                else:
                    n_steps = n_steps[0] + 1


                # Create the Lanczos class)
                lanc = Lanczos(None)
                lanc.a_coeffs = a_ns[i, j, :n_steps]
                lanc.b_coeffs = b_ns[i, j, :n_steps-1]
                lanc.perturbation_modulus = 2

                print("Computing ({},{}) ..., n_steps = {}".format(i, j, n_steps))

                # get the green function from continued fraction
                gf = lanc.get_green_function_continued_fraction(np.array([0]), use_terminator = use_terminator, \
                    smearing = smearing, last_average = last_average)[0]

                # Lanczos can compute only diagonal green functions
                # Therefore we need to trick it to get the off-diagonal elements
                # <1|L|2> = 1/2*( <1+2|L|1+2> - <1|L|1>  - <2|L|2>)
                mat_pol[i,j] = (np.real(gf) - mat_pol[i,i] - mat_pol[j,j]) / 2
                mat_pol[j,i] = (np.real(gf) - mat_pol[i,i] - mat_pol[j,j]) / 2

    # The green function is the inverse of the free energy curvature
    np.savetxt("gf_mat.dat", mat_pol)
    fc_pols = np.linalg.inv(mat_pol)
    np.savetxt("fc_pols.dat", fc_pols)

    # Get back into real space
    epols_m = np.einsum("ab, a->ab", pols_sc, np.sqrt(masses)) 
    fc_odd = np.einsum("ab, ca, da ->cd", fc_pols, epols_m, epols_m)

    return fc_odd

symmetrize_d3_muspace(d3, symmetries)

SYMMETRIZE D3 IN MODE SPACE

This function symmetrizes the d3 in the mu space. It is quite fast.

Results
new_d3 : ndarray(n_modes, n_modes, n_modes)
    The d3 tensor symmetrized
Source code in tdscha/DynamicalLanczos.py
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
def symmetrize_d3_muspace(d3, symmetries):
    """
    SYMMETRIZE D3 IN MODE SPACE
    ===========================

    This function symmetrizes the d3 in the mu space.
    It is quite fast.

    Parameters
    ----------
        d3 : ndarray(n_modes, n_modes, n_modes)
            The d3 tensor to be symmetrized
        symmetries : ndarray(N_sym, n_modes, n_modes)
            The full symmetry matrix

    Results
    -------
        new_d3 : ndarray(n_modes, n_modes, n_modes)
            The d3 tensor symmetrized
    """

    print("Symmetrizing d3: SHAPE SYMMETRY:", symmetries.shape)

    new_d3 = np.zeros(np.shape(d3), dtype = np.double)

    N_sym, nmode, dumb = np.shape(symmetries)

    for i in range(N_sym):
        symmat = symmetries[i, :, :]
        print("SYM {}:".format(i+1))
        print(symmetries[i,:,:])

        ap = np.einsum("abc, lc ->abl", d3, symmat)
        ap = np.einsum("abc, lb ->alc", ap, symmat)
        ap = np.einsum("abc, la ->lbc", ap, symmat)
        #ap = np.einsum("abc, aa, bb, cc->abc", d3, symmat, symmat, symmat)

        new_d3 += ap 

    new_d3 /= N_sym
    return new_d3

get_weights_finite_differences(u_tilde, w, T, R1, Y1)

Computes the weights of the configurations using a finite difference approach. This is time consuiming, use it for testing purpouses.

Returns:

Type Description
weights : ndarray(size = N_random)

The weights that correspond to this perturbation

Source code in tdscha/DynamicalLanczos.py
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
def get_weights_finite_differences(u_tilde, w, T, R1, Y1):
    """
    Computes the weights of the configurations using a finite difference
    approach.
    This is time consuiming, use it for testing purpouses.

    Parameters
    ----------
        u_tilde : ndarray(size = (N_random, n_modes))
            The displacement in the polarization space (mass rescaled)
        w : ndarray(n_modes)
            the SCHA frequencies
        T : float
            Temperature
        R1 : ndarray(size = n_modes)
            The perturbation on the centroid positions
        Y1 : ndarray(size = (n_modes, n_modes), symmetric)
            The perturbation on the Y matrix

    Returns
    -------
        weights : ndarray(size = N_random)
            The weights that correspond to this perturbation
    """
    n_conf, n_modes = u_tilde.shape

    # get the Y matrix
    Y_mu = 2 * w 

    if T > __EPSILON__:
        n = 1. / ( np.exp(w * 157887.32400374097 / T) - 1)
        Y_mu /= (2 * n + 1)

    Y = np.diag(Y_mu) 

    lambda_small = 1e-9

    R1_norm = np.sum(R1**2)
    Y1_norm = np.sum(Y1**2)

    norm = np.sqrt(R1_norm + Y1_norm)

    print("Normalization: {}".format(norm))

    R1_direction = R1 / norm
    Y1_direction = Y1 / norm



    #print("DISP R:", R1)
    #print("DISP Y:", Y1)

    new_Y = Y + Y1_direction * lambda_small
    new_u_tilde = u_tilde - np.tile(R1_direction * lambda_small, (n_conf, 1))

    # Get the weights before and after the perturbation
    w_old = np.zeros(n_conf, dtype = np.double) 
    w_new = np.zeros(n_conf, dtype = np.double) 

    for i in range(n_conf):
        w_old[i] = np.exp(-.5 * u_tilde[i, :].dot(Y.dot(u_tilde[i, :])))
        w_new[i] = np.exp(-.5 * new_u_tilde[i, :].dot(new_Y.dot(new_u_tilde[i, :])))

    w_old *= np.sqrt(np.linalg.det(Y / (2 * np.pi))) 
    w_new *= np.sqrt(np.linalg.det(new_Y / (2 * np.pi)))

    # Test normalization
    #print("Normalization old:", np.sum(w_old) / n_conf)
    #print("Normalization new:", np.sum(w_new) / n_conf)

    xc = np.sum(u_tilde) / n_conf
    #print("Avg:", xc)
    Y_num = np.sum( (u_tilde - xc)**2) / n_conf
    #print("Y from ens:", Y_num, " (from w = {})".format(np.linalg.inv(Y)))

    # Get the derivative with respect to the parameter
    weights = (w_new/w_old - 1) / lambda_small * norm 

    return weights


    """
    Return the full L matrix from the Lanczos utilities, by exploiting the linear operator.
    This is very usefull for testing purpouses.

    NOTE: The memory required to store the full matrix may diverge.

    If static is true, instead of the Lanczos matrix, the symmetric one ad-hoc for the static case is employed.
    """

    L_op = lanczos.L_linop
    if static == True:
        lanczos.psi = np.zeros(lanczos.n_modes + lanczos.n_modes * (lanczos.n_modes + 1) // 2, dtype = TYPE_DP)

        def apply_static(v):
            lanczos.psi[:] = v
            out = np.zeros(v.shape, dtype = TYPE_DP) 
            if compute_harm:
                out[:] = lanczos.apply_L1_static(v) 
            if compute_anharm:
                out += lanczos.apply_anharmonic_static()
            return out 

        npsi = len(lanczos.psi)

        L_op =  scipy.sparse.linalg.LinearOperator( shape = (npsi, npsi), dtype = TYPE_DP, matvec = apply_static, rmatvec = apply_static)

    n_iters = len(lanczos.psi)

    v = np.zeros(lanczos.psi.shape, dtype = np.double)

    L_matrix = np.zeros((n_iters, n_iters), dtype = np.double)

    for i in range(n_iters):
        print("Step {} out of {}".format(i+1, n_iters))

        v[:] = 0.0
        v[i] = 1.0

        if transpose:
            L_matrix[:, i] = L_op.rmatvec(v)
        else:
            L_matrix[:, i] = L_op.matvec(v)

    return L_matrix

get_full_L_matrix(lanczos, transpose=False, static=False, compute_anharm=True, compute_harm=True)

Return the full L matrix from the Lanczos utilities, by exploiting the linear operator. This is very usefull for testing purpouses. NOTE: The memory required to store the full matrix may diverge. If static is true, instead of the Lanczos matrix, the symmetric one ad-hoc for the static case is employed.

Source code in tdscha/DynamicalLanczos.py
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
def get_full_L_matrix(lanczos, transpose = False, static = False, compute_anharm = True, compute_harm = True):
    """
    Return the full L matrix from the Lanczos utilities, by exploiting the linear operator.
    This is very usefull for testing purpouses.
    NOTE: The memory required to store the full matrix may diverge.
    If static is true, instead of the Lanczos matrix, the symmetric one ad-hoc for the static case is employed.
    """
    print()
    print('Getting the full L matrix')
    print('Are we using the Wigner representation = {}'.format(lanczos.use_wigner))
    print()

    L_op = lanczos.L_linop

    if static == True:
        lanczos.psi = np.zeros(lanczos.n_modes + lanczos.n_modes * (lanczos.n_modes + 1) // 2, dtype = TYPE_DP)

        def apply_static(v):
            lanczos.psi = v
            out = np.zeros(v.shape, dtype = TYPE_DP) 
            if compute_harm:
                out[:] = lanczos.apply_L1_static(v) 
            if compute_anharm:
                out += lanczos.apply_anharmonic_static()
            return out 

        npsi = len(lanczos.psi)

        L_op =  scipy.sparse.linalg.LinearOperator( shape = (npsi, npsi), dtype = TYPE_DP, matvec = apply_static, rmatvec = apply_static)

    n_iters = len(lanczos.psi)

    v = np.zeros(lanczos.psi.shape, dtype = np.double)

    L_matrix = np.zeros((n_iters, n_iters), dtype = np.double)

    # In this way we get the columns of L
    for i in range(n_iters):
        print("Step {} out of {}".format(i+1, n_iters))

        v[:] = 0.0
        v[i] = 1.0

        if transpose:
            L_matrix[:, i] = L_op.rmatvec(v)
        else:
            L_matrix[:, i] = L_op.matvec(v)

        print('The colum i = {}'.format(i+1))
        print(L_matrix[:,i])

    return L_matrix

min_stdes(func, args, x0, step=0.01, n_iters=100)

A simple steepest descend algorithm with fixed step. Used for testing purpouses

Source code in tdscha/DynamicalLanczos.py
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
def min_stdes(func, args, x0, step = 1e-2, n_iters = 100):
    """
    A simple steepest descend algorithm with fixed step. Used for testing purpouses
    """

    x = x0.copy()
    for i in range(n_iters):
        f, grad = func(x, args)

        x -= grad * step

        print("F: {} | G: {}".format( f, np.sqrt(np.sum(grad**2))))
    return x

Computation Mode Constants

Constant Value Description
MODE_SLOW_SERIAL 0 Pure Python implementation (testing only)
MODE_FAST_SERIAL 1 C extension with OpenMP
MODE_FAST_MPI 2 C extension with MPI parallelization
MODE_FAST_JULIA 3 Julia extension (fastest - default if julia is present)

Lanczos Class

tdscha.DynamicalLanczos.Lanczos(ensemble=None, mode=None, unwrap_symmetries=False, select_modes=None, use_wigner=True, lo_to_split='random')

Bases: object

INITIALIZE THE LANCZOS

This function extracts the weights, the X and Y arrays for the d3 and d4 computation as well as the polarization vectors and frequencies.

Source code in tdscha/DynamicalLanczos.py
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
    def __init__(self, ensemble = None, mode = None, unwrap_symmetries = False, select_modes = None, use_wigner = True, lo_to_split = "random"):
        """
        INITIALIZE THE LANCZOS
        ======================

        This function extracts the weights, the X and Y arrays for the d3 and d4
        computation as well as the polarization vectors and frequencies.

        Parameters
        ----------
            ensemble : Ensemble.Ensemble()
                The ensemble upon which you want to compute the DynamicalResponce
            mode : int
                The mode of the speedup.
                   0) Slow python implementation 
                      Use this just for testing
                   1) Fast C serial code
                   2) Fast C parallel (MPI)
                   3) Fast Julia multithreading (only if julia is available)
            unwrap_symmetries : bool
                If true (default), the ensemble is unwrapped to respect the symmetries.
                This requires SPGLIB installed.
            select_modes : ndarray(size = n_modes, dtype = bool)
                A mask for each mode, if False, the mode is neglected. Use this to exclude some modes that you know are not
                involved in the calculation. If not specified, all modes are considered by default.  
            use_wigner: bool, if True Wigner equations are used.
                involved in the calculation. If not specified, all modes are considered by default.
            lo_to_split : string or ndarray
                Mode of lo_to_splitting. If empty or none, it is LO-TO splitting correction is neglected.
                If a ndarray is provided, it is the direction of q on which the LO-TO splitting is computed.
        """

        if is_julia_enabled():
            self.mode = MODE_FAST_JULIA
        else:
            self.mode = MODE_FAST_SERIAL

        if mode is not None:
            self.mode = mode

        # Define the order
        order = "C"
        #if self.mode >= 1:
        #    order = "F"

        # HERE DEFINE ALL THE VARIABLES FOR THE Dynamical Lanczos
        # The temperature
        self.verbose = True
        self.T = 0
        # Number of atoms in the supercell
        self.nat = 0
        # The array of masses in the supercell, np.shape = 3 * N_at_sc
        self.m = []
        # Auxiliary eigenmodes of SCHA
        self.w = []
        # Auxiliary eigenvectors of SCHA
        self.pols = []
        # The numbero of modes, translations excluded
        self.n_modes = 0
        self.ignore_harmonic = False 
        # Ignore D3 and D4
        self.ignore_v3 = False
        self.ignore_v4 = False
        # Number of configurations
        self.N = 0
        # The weights from the static calculations
        self.rho = []
        # Effective number of configurations
        self.N_eff = 0
        # The satic displacements in the polarization basis
        self.X = []
        # The static forces in the polarization basis
        self.Y = []
        # The vector on which we apply Lanczos
        self.psi = []
        self.eigvals = None
        self.eigvects = None
        # In the custom lanczos mode
        self.a_coeffs = [] # Coefficients on the diagonal
        self.b_coeffs = [] # Coefficients close to the diagonal
        self.c_coeffs = [] # Coefficients in the case of the biconjugate Lanczos
        self.krilov_basis = [] # The basis of the krilov subspace
        self.basis_P = [] # The basis of the P vectors for the biconjugate Lanczos (normalized)
        self.basis_Q = [] # The basis of the Q vectors for the biconjugate Lanczos
        self.s_norm = [] # Store the normalization of the s vector, this allows to rebuild the correct p when needed. 
        self.arnoldi_matrix = [] # If requested, the upper triangular arnoldi matrix
        self.reverse_L = False
        self.shift_value = 0
        self.symmetrize = False
        self.symmetries = None
        self.degenerate_space = None
        self.N_degeneracy = None
        self.initialized = False
        self.perturbation_modulus = 1
        self.dyn = None
        # Unit cell structure
        self.uci_structure = None
        # Structure for the supercell
        self.super_structure = None
        # Symmetries
        self.qe_sym = None
        # The application of L as a linear operator
        self.L_linop = None
        self.M_linop = None
        self.unwrapped = False
        self.sym_julia = None
        self.deg_julia = None
        self.n_syms = 1

        self.u_tilde = None
        # The static forces divided by the sqrt(masses)
        self.f_tilde = None

        self.sym_block_id = None

        # Gamma-only optimization: use only point-group symmetries in replicas,
        # impose translations directly on f_pert_av and d2v_pert_av
        self.gamma_only = False
        self.trans_projector = None    # (n_modes, n_modes) matrix P = (1/n_cells) Σ_R T_R^mode
        self.trans_operators = None    # list of (n_modes, n_modes) T_R^mode matrices

        # Set to True if we want to use the Wigner equations
        self.use_wigner = use_wigner

        # This flag is usefull to work with 1D or 2D systems
        # Default is False meaning that we ignore only translational modes
        self.ignore_small_w = False

        # Setup the attribute control
        self.__total_attributes__ = [item for item in self.__dict__.keys()]
        self.fixed_attributes = True # This must be the last attribute to be setted

        # Perform a bare initialization if the ensemble is not provided
        if ensemble is None:
            return


        # ========== END OF VARIABLE DEFINITION (EACH NEW DEFINITION FROM NOW ON RESULTS IN AN ERROR) =======
        self.dyn = ensemble.current_dyn.Copy() 
        self.uci_structure = ensemble.current_dyn.structure.copy()
        self.super_structure = self.dyn.structure.generate_supercell(self.dyn.GetSupercell())#superdyn.structure

        self.T = ensemble.current_T

        ws, pols = self.dyn.DiagonalizeSupercell(lo_to_split = lo_to_split)

        self.nat = self.super_structure.N_atoms
        n_cell = np.prod(self.dyn.GetSupercell())

        self.qe_sym = CC.symmetries.QE_Symmetry(self.dyn.structure)
        self.qe_sym.SetupQPoint()

        # Get the masses
        m = self.super_structure.get_masses_array()
        self.m = np.tile(m, (3,1)).T.ravel()

        # Remove the translations
        if lo_to_split is not None and self.dyn.effective_charges is not None:
            trans_mask = np.zeros(len(ws), dtype=bool)
            trans_mask[:3] = True
        if not ensemble.ignore_small_w:
            trans_mask = CC.Methods.get_translations(pols, m)
            good_mask  = ~trans_mask
        else:
            self.ignore_small_w = True
            trans_mask = np.abs(ws) < CC.Phonons.__EPSILON_W__
            good_mask  = ~trans_mask

        # If requested, isolate only the specified modes.
        if select_modes is not None:
            if len(select_modes) != len(trans_mask):
                raise ValueError("""
Error, 'select_modes' should be an array of the same lenght of the number of modes.
 n_modes = {} | len(select_modes) = {}
""".format(len(ws), len(select_modes)))
            print()
            print('Selecting some of the modes...')
            print()
            good_mask = (~trans_mask) & select_modes

        # Get the frequencies in Ry and polarization vectors
        self.w = ws[good_mask]
        self.pols = pols[:, good_mask]

        # Correctly reshape the polarization in case only one mode is selected
        if len(self.w) == 1:
            self.pols = self.pols.reshape((len(self.m), 1))

        # Get the number of modes
        self.n_modes = len(self.w)

        # Prepare the list of q point starting from the polarization vectors
        #q_list = CC.symmetries.GetQForEachMode(self.pols, self.uci_structure, self.super_structure, self.dyn.GetSupercell())
        # Store the q vectors in crystal space
        #bg = self.uci_structure.get_reciprocal_vectors() / 2* np.pi
        #self.q_vectors = np.zeros((self.n_modes, 3), dtype = np.double, order = "C")
        #for iq, q in enumerate(q_list):
        #    self.q_vectors[iq, :] = CC.Methods.covariant_coordinate(bg, q)

        # Ignore v3 or v4. You can set them for testing
        # This is no longer implemented in the fast Lanczos
        self.ignore_v3 = False
        self.ignore_v4 = False

        # The number of configurations
        self.N = ensemble.N
        rho = ensemble.rho.copy() 
        # Transform Angstrom -> Bohr
        u = ensemble.u_disps  / Ensemble.Bohr
        # Forces are in Ry/Angstrom for now only
        f = ensemble.forces.reshape(self.N, 3 * self.nat).copy()
        f -= ensemble.sscha_forces.reshape(self.N, 3 * self.nat)

        # Get the average force in the unit cell, (N_at_uc, 3)
        f_mean = ensemble.get_average_forces(get_error = False)

        # Perform the symmetrization of the average force
        qe_sym = CC.symmetries.QE_Symmetry(self.dyn.structure)
        qe_sym.SetupQPoint()
        qe_sym.SymmetrizeVector(f_mean)

        # Reproduce the average force on the full supercell
        f_mean = np.tile(f_mean, (np.prod(ensemble.current_dyn.GetSupercell()), 1)).ravel()

        # Transform forces in Ry/Bohr
        f_mean *= Ensemble.Bohr

        # Subtract also the average force to clean more the stochastic noise
        #av_force = ensemble.get_average_forces(get_error = False).ravel()
        #new_av_force = np.tile(av_force, (n_cell, 1)).ravel()

        #f -= np.tile(new_av_force, (self.N, 1)) 

        # Transform in Ry/Bohr
        f *= Ensemble.Bohr

        if unwrap_symmetries:
            u, f, rho = ensemble.get_unwrapped_ensemble()
            self.N = len(rho)
            self.unwrapped = True

            u /= Ensemble.Bohr
            f *= Ensemble.Bohr

        # Subtract the SSCHA GRADIENT on average position
        # In this way the calculation works even if the system is not in equilibrium
        #print(np.shape(f), np.shape(f_mean))
        f[:, :] -= np.tile(f_mean, (self.N, 1))

        # Perform the mass rescale to get the tilde variables
        u *= np.tile(np.sqrt(self.m), (self.N, 1)) 
        f /= np.tile(np.sqrt(self.m), (self.N, 1)) 

        # Get the info about the ensemble
        self.rho = rho
        self.N_eff = np.sum(self.rho)

        # Mass rescaled quantities
        self.u_tilde = u
        self.f_tilde = f

        # The dispalcements in BOHR mass resclaed and in polarization basis
        self.X = np.zeros((self.N, self.n_modes), order = order, dtype = TYPE_DP)
        # The forces in RY/BOHR mass resclaed and in polarization basis
        self.Y = np.zeros((self.N, self.n_modes), order = order, dtype = TYPE_DP)

        # Convert in the polarization space the displacements and the forces
        self.X[:, :] = self.u_tilde.dot(self.pols) #.T.dot(self.u_tilde)
        self.Y[:, :] = self.f_tilde.dot(self.pols) #self.pols.T.dot(self.f_tilde)

        # Prepare the variable used for the working
        # The len of psi = N_modes + 0.5 * N_modes * (N_modes + 1) + 0.5 * N_modes * (N_modes + 1)
        len_psi = self.n_modes
        #if self.T < __EPSILON__:
        #    len_psi += self.n_modes**2
        #else:
        len_psi += self.n_modes * (self.n_modes + 1)
        #print("N MODES:", self.n_modes)
        #print("LEN PSI:", len_psi)

        # In Wigner the variables are a'^(1) and b'^(1) 
        # In Standard the variables are Y^(1) and ReA^(1)

        ##########################################################
        # Psi contains R^(1), Upsilon^(1)-a'^(1), ReA^(1)-b'^(1) #
        ##########################################################

        # Everything is in the polarization basis
        self.psi = np.zeros(len_psi, dtype = TYPE_DP)

        ################################################################
        # For the matrices the code will store only the upper triangle #
        ################################################################

        # Prepare the L as a linear operator 
        # Prepare the possibility to transpose the matrix with L_transp
        def L_transp(psi):
            return self.apply_full_L(psi, transpose = True)
        self.L_linop = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)),\
                                                          matvec = self.apply_full_L, rmatvec = L_transp, dtype = TYPE_DP)

        # Define the preconditioner
        def M_transp(psi):
            return self.apply_L1_inverse_FT(psi, transpose = True)
        self.M_linop = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)),\
                                                          matvec = self.apply_L1_inverse_FT, rmatvec = M_transp, dtype = TYPE_DP)


        # Prepare the solution of the Lanczos algorithm
        self.eigvals  = None
        self.eigvects = None 

        # Store the basis and the coefficients of the Lanczos procedure
        # In the custom lanczos mode
        self.krilov_basis = [] # The basis of the krilov subspace
        self.arnoldi_matrix = [] # If requested, the upper triangular arnoldi matrix

        # These are some options that can be used to properly reverse and shift the L operator to
        # fasten the convergence of low energy modes
        self.reverse_L = False
        self.shift_value = 0

reset()

RESET THE LANCZOS

This function reset the Lanczos algorithm, allowing for a new responce function calculation with the same ensemble and the same settings.

Source code in tdscha/DynamicalLanczos.py
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
def reset(self):
    """
    RESET THE LANCZOS
    =================

    This function reset the Lanczos algorithm, allowing for a new responce function calculation
    with the same ensemble and the same settings.
    """

    # Prepare the variable used for the working
    len_psi = self.n_modes
    len_psi += self.n_modes * (self.n_modes + 1)
    self.psi = np.zeros(len_psi, dtype = TYPE_DP)


    # Prepare the solution of the Lanczos algorithm
    self.eigvals = None
    self.eigvects = None 

    # Store the basis and the coefficients of the Lanczos procedure
    # In the custom lanczos mode
    self.a_coeffs = [] # Coefficients on the diagonal
    self.b_coeffs = [] # Coefficients close to the diagonal
    self.c_coeffs = []

    # The krilov basis for the symmetric and unsymmetric Lanczos
    self.basis_P = []
    self.basis_Q = []
    self.s_norm = []
    self.krilov_basis = [] # The basis of the krilov subspace
    self.arnoldi_matrix = [] # If requested, the upper triangular arnoldi matrix

init(use_symmetries=True)

INITIALIZE THE CALCULATION

Perform everithing needed to initialize the calculation.

Source code in tdscha/DynamicalLanczos.py
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
def init(self, use_symmetries = True):
    """
    INITIALIZE THE CALCULATION
    ==========================

    Perform everithing needed to initialize the calculation.

    Parameters
    ----------
        use_symmetries : bool
            if False (default True) symmetries are neglected (unless the ensemble has been unwrapped).

    """
    # Prepare the variable used for the working
    len_psi  = self.n_modes
    len_psi += self.n_modes * (self.n_modes + 1)
    self.psi = np.zeros(len_psi, dtype = TYPE_DP)

    self.prepare_symmetrization(no_sym = not use_symmetries)
    self.initialized = True

interpolate(q_mesh, support_dyn=None, auto_init=True)

INTERPOLATION

This subroutine prepare the Lanczos algorithm to run on a bigger mesh than the one defined on the original supercell. This is fundamental to correctly converge resonances.

This method automatically initializes with symmetries the new Lanczos. You can disable this behaviour setting auto_init = False.

Results
interpolated_lanczos : Lanczos()
    A new Lanczos class object, with interpolated data.
Source code in tdscha/DynamicalLanczos.py
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
def interpolate(self, q_mesh, support_dyn = None, auto_init = True):
    """
    INTERPOLATION
    =============

    This subroutine prepare the Lanczos algorithm to run on a bigger mesh than the one defined on the original supercell.
    This is fundamental to correctly converge resonances.

    This method automatically initializes with symmetries the new Lanczos. 
    You can disable this behaviour setting auto_init = False.

    Parameters
    ----------
        q_mesh : list or ndarray (size=3, dtye = np.intc)
            The mesh of q points on which you want to perform the simulation.
            It should be bigger than the original supercell size.
        support_dyn : CC.Phonons.Phonons, optional
            By default, the original dynamical matrix will be Fourier interpolated in the new q_mesh.
            However, you can provide a new dynamical matrix in the q_mesh already interpolated.
            This is usefull if you want to use a custom interpolation
            (for example to interpolate only the differences between the SSCHA force constat matrix and the harmonic one).
        auto_init: bool
            If True, after the interpolation is performed, the new Lanczos object will be initialized (with symmetries).
            If you disable it, you must call the init function manually

    Results
    -------
        interpolated_lanczos : Lanczos()
            A new Lanczos class object, with interpolated data.

    """
    interpolated_lanczos = Lanczos()

    # Interpolate the original dynamical matrix
    if support_dyn is not None:
        interpolated_lanczos.dyn = support_dyn.Copy()
    else:
        interpolated_lanczos.dyn = self.dyn.Interpolate(q_mesh)

    # Prepare the new structure
    interpolated_lanczos.uci_structure = interpolated_lanczos.dyn.structure.copy()
    interpolated_lanczos.super_structure = interpolated_lanczos.uci_structure.generate_supercell(q_mesh)

    interpolated_lanczos.T = self.T 

    ws, pols = interpolated_lanczos.dyn.DiagonalizeSupercell()


    interpolated_lanczos.nat = interpolated_lanczos.super_structure.N_atoms
    n_cell = np.prod(q_mesh)

    interpolated_lanczos.qe_sym = CC.symmetries.QE_Symmetry(interpolated_lanczos.uci_structure)
    interpolated_lanczos.qe_sym.SetupQPoint()

    # Get the masses
    m = interpolated_lanczos.super_structure.get_masses_array()
    interpolated_lanczos.m = np.tile(m, (3,1)).T.ravel()

    # Remove the translations
    trans_mask = CC.Methods.get_translations(pols, m)

    # Isolate only translational modes
    good_mask = ~trans_mask

    # Get the polarization vectors
    interpolated_lanczos.w = ws[good_mask]
    interpolated_lanczos.pols = pols[:, good_mask]

    # Correctly reshape the polarization in case only one mode is selected
    if len(self.w) == 1:
        self.pols = self.pols.reshape((len(self.m), 1))

    interpolated_lanczos.n_modes = len(interpolated_lanczos.w)


    # Prepare the list of q point starting from the polarization vectors
    #q_list = CC.symmetries.GetQForEachMode(self.pols, self.uci_structure, self.super_structure, self.dyn.GetSupercell())
    # Store the q vectors in crystal space
    bg = interpolated_lanczos.uci_structure.get_reciprocal_vectors() / 2* np.pi
    interpolated_lanczos.q_vectors = np.zeros((interpolated_lanczos.n_modes, 3), dtype = np.double, order = "C")
    #for iq, q in enumerate(q_list):
    #    self.q_vectors[iq, :] = CC.Methods.covariant_coordinate(bg, q)


    # Prepare the interpolation of the ensemble
    interpolated_lanczos.rho = self.rho.copy()
    interpolated_lanczos.N_eff = np.sum(self.rho)

    interpolated_lanczos.u_tilde = self.u_tilde.copy()
    interpolated_lanczos.f_tilde = self.f_tilde.copy()

    interpolated_lanczos.X = np.zeros((interpolated_lanczos.N, interpolated_lanczos.n_modes), order = order, dtype = TYPE_DP)
    interpolated_lanczos.Y = np.zeros((interpolated_lanczos.N, interpolated_lanczos.n_modes), order = order, dtype = TYPE_DP)

    # TODO: Interpolation of the q vectors with the Tetrahedral method.
    raise NotImplementedError("Error, interpolation is not yet implemented.")
    interpolated_lanczos.X[:, :] = self.u_tilde.dot(self.pols) #.T.dot(self.u_tilde)
    interpolated_lanczos.Y[:, :] = self.f_tilde.dot(self.pols) #self.pols.T.dot(self.f_tilde)




    if auto_init:
        interpolated_lanczos.init()
    return interpolated_lanczos

prepare_symmetrization(no_sym=False, verbose=True, symmetries=None)

PREPARE THE SYMMETRIZATION

This function analyzes the character of the symmetry operations for each polarization vectors. This will allow the method do know how many modes are degenerate.

If the ensemble has been unwrapped, then the symmetries are not initialized.

Source code in tdscha/DynamicalLanczos.py
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
def prepare_symmetrization(self, no_sym = False, verbose = True, symmetries = None):
    """
    PREPARE THE SYMMETRIZATION
    ==========================

    This function analyzes the character of the symmetry operations for each polarization vectors.
    This will allow the method do know how many modes are degenerate.

    If the ensemble has been unwrapped, then the symmetries are not initialized.

    Parameters
    ----------
        no_sym : bool
            If True, the symmetries are neglected.
        symmetries : list of 3x4 matrices
            If None, spglib is employed to find the symmetries,
                     otherwise, the symmetries here contained are employed.
    """

    self.initialized = True

    # All the rest is deprecated in the Fast Lanczos implementation
    # As the symmetrization is performed by unwrapping the ensemble

    # Generate the dynamical matrix in the supercell
    super_structure = self.dyn.structure.generate_supercell(self.dyn.GetSupercell())
    w, pols = self.dyn.DiagonalizeSupercell()

    # Get the symmetries of the super structure
    if not __SPGLIB__ and not no_sym:
        raise ImportError("Error, spglib module required to perform symmetrization in a supercell. Otherwise, use no_sym")

    # Neglect the symmetries
    if no_sym or self.unwrapped:
        self.symmetries = [np.ones( (1,1,1), dtype = np.double)] * self.n_modes
        self.N_degeneracy = np.ones(self.n_modes, dtype = np.intc)
        self.degenerate_space = [np.array([i], dtype = np.intc) for i in range(self.n_modes)]
        self.sym_block_id = np.arange(self.n_modes).astype(np.intc)
        return

    t1 = time.time()
    if symmetries is None:
        super_symmetries = CC.symmetries.GetSymmetriesFromSPGLIB(spglib.get_symmetry(super_structure.get_spglib_cell()), False)

    else:
        super_symmetries = symmetries
    t2 = time.time()


    if verbose:
        print("Time to get the symmetries [{}] from spglib: {} s".format(len(super_symmetries), t2-t1))

    # Gamma-only optimization: separate point-group and translational symmetries
    if self.gamma_only:
        n_total_syms = len(super_symmetries)
        identity = np.eye(3)
        translations = []
        unique_rotations = {}
        for sym in super_symmetries:
            rot = sym[:, :3]
            rot_key = np.round(rot, 8).tobytes()
            if np.allclose(rot, identity, atol=1e-8):
                translations.append(sym)
            if rot_key not in unique_rotations:
                unique_rotations[rot_key] = sym
        pg_symmetries = list(unique_rotations.values())

        if verbose:
            print("Gamma-only: {} PG syms instead of {} total (speedup: {}x)".format(
                len(pg_symmetries), n_total_syms, n_total_syms // max(len(pg_symmetries), 1)))

        # Build translation operators in mode space: T_R^mode = pols^T @ P_R @ pols
        self.trans_operators = []
        nat_sc = super_structure.N_atoms
        for t_sym in translations:
            irt = CC.symmetries.GetIRT(super_structure, t_sym)
            # Build Cartesian permutation matrix P_R (3*nat_sc x 3*nat_sc)
            P_R = np.zeros((3 * nat_sc, 3 * nat_sc), dtype=np.double)
            for i_at in range(nat_sc):
                j_at = irt[i_at]
                P_R[3*j_at:3*j_at+3, 3*i_at:3*i_at+3] = np.eye(3)
            # Project into mode space
            T_mode = self.pols.T @ P_R @ self.pols  # (n_modes, n_modes)
            self.trans_operators.append(T_mode)

        self.trans_projector = np.mean(self.trans_operators, axis=0)

        if verbose:
            print("Built {} translation operators in mode space".format(len(translations)))

        # Replace super_symmetries with PG-only for the rest of the function
        super_symmetries = pg_symmetries

    # Get the symmetry matrix in the polarization space
    # Translations are needed, as this method needs a complete basis.
    pol_symmetries, basis = CC.symmetries.GetSymmetriesOnModesDeg(super_symmetries, super_structure, self.pols, self.w)
    #pol_symmetries = CC.symmetries.GetSymmetriesOnModes(super_symmetries, super_structure, pols)
    t1 = time.time()
    if verbose:
        print("Time to convert symmetries in the polarizaion space: {} s".format(t1-t2))

    self.symmetries = pol_symmetries
    self.degenerate_space = [np.array(x, dtype = np.intc) for x in basis]
    self.N_degeneracy = np.array([len(x) for x in basis], dtype = np.intc)
    self.sym_block_id = -np.ones(self.n_modes, dtype = np.intc)
    self.n_syms =  self.symmetries[0].shape[0]

    if self.mode is MODE_FAST_JULIA:
        # Get the max length
        max_val = 0
        nblocks = len(self.symmetries)
        for s in self.symmetries:
            m = s.shape[1]
            if m > max_val:
                max_val = m
        self.sym_julia = np.zeros((nblocks, self.n_syms, max_val, max_val), dtype = TYPE_DP)
        self.deg_julia = np.zeros((nblocks, max_val), dtype = np.int32)

        # Now fill the array
        for i, sblock in enumerate(self.symmetries):
            nsym, c, _ = np.shape(sblock)
            self.sym_julia[i, :, :c, :c] = sblock
            self.deg_julia[i, :c] = self.degenerate_space[i]

        # Pre-build and cache sparse symmetry matrices in Julia
        julia.Main.init_sparse_symmetries(
            self.sym_julia, self.N_degeneracy, self.deg_julia, self.sym_block_id)

    # Create the mapping between the modes and the block id.
    t1 = time.time()
    for i in range(self.n_modes):
        for j, block in enumerate(self.degenerate_space):
            if i in block:
                self.sym_block_id[i] = j 
                break

        assert self.sym_block_id[i] >= 0, "Error, something went wrong during the symmetrization"
    t2 = time.time()

    if verbose:
        print("Time to create the block_id array: {} s".format(t2-t1))

prepare_input_files(root_name='tdscha', n_steps=100, start_from_scratch=True, directory='.', run_symm=False)

PREPARE INPUT FILES

This method prepares the input files for the submission with the binary executable. This is usefull to prepare the input in a local computer and submit the calculation on a cluster, where it is easier to work.

Parameters:

Name Type Description Default
This
required
XXX
required
XXX
required
XXX
required
XXX
required
XXX
required
XXX
required
and
required
XXX
required
XXX
required
The
required
as
required
All
required
Source code in tdscha/DynamicalLanczos.py
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
def prepare_input_files(self, root_name = "tdscha", n_steps = 100, start_from_scratch = True, directory=".", run_symm = False):
    """
    PREPARE INPUT FILES
    ===================

    This method prepares the input files for the submission with the binary executable.
    This is usefull to prepare the input in a local computer and submit the calculation on a cluster,
    where it is easier to work.

    Parameters
    ----------
        root_name : string
            The title of the calculation.
        n_steps: int
            The number of steps to be performed in the lanczos calculation.
        start_from_scratch: bool
            If True the calculation is restarted from scratch.
        directory : string
            Path to the directory on which the input files will be saved
        run_symm : bool
            True if we use the Wigner representation


    This file will prepare inside the directory the following input files.
    (where XXX = root_name)

    XXX.json
    XXX.X.dat
    XXX.Y.dat
    XXX.syms.$i   ($i = 0, ..., N symmetries - 1)
    XXX.degs
    XXX.psi

    and the following optional files (in case the calculation should be restarted):

    XXX.Qbasis
    XXX.Pbasis


    The file XXX.json contains the generic information about the minimization,
    as well as all arrays not too big and that can be easily stored in a json file.

    All the other data contain 2d arrays or more sophisticated data.
    """

    Nsyms, _,_ = np.shape(self.symmetries[0])

    json_data = {"T" : self.T, 
                 "n_steps" : n_steps,
                 "ignore_v2" : self.ignore_harmonic,
                 "ignore_v3" : self.ignore_v3,
                 "ignore_v4" : self.ignore_v4,
                 "use_wigner" : self.use_wigner,
                 "run_sym": run_symm,
                 "data" : {
                     "n_configs" : int(self.N),
                     "n_modes" : int(self.n_modes),
                     "n_syms" : Nsyms,
                     "n_blocks" : len(self.symmetries),
                     "perturbation_modulus" : self.perturbation_modulus,
                     "reverse" : self.reverse_L,
                     "shift" : self.shift_value} }

    Parallel.barrier()

    if not start_from_scratch:
        raise NotImplementedError("Error, restarting from a previous calculation is not yet implemented.")

    if Parallel.am_i_the_master():
        root_fname = os.path.join(directory, root_name)

        if not os.path.exists(directory):
            os.makedirs(directory)

        # Writhe the json input file
        with open(root_fname + ".json", "w") as fp:
            json.dump(json_data, fp)

        # Save 1D arrays
        np.savetxt(root_fname + ".ndegs", self.N_degeneracy, fmt = "%d")
        np.savetxt(root_fname + ".blockid", self.sym_block_id, fmt = "%d")
        np.savetxt(root_fname + ".masses", self.m)
        np.savetxt(root_fname + ".freqs", self.w)
        np.savetxt(root_fname + ".rho", self.rho)

        # Save all the other data
        np.savetxt(root_fname + ".X.dat", self.X)
        np.savetxt(root_fname + ".Y.dat", self.Y)


        np.savetxt(root_fname + ".psi", self.psi)

        # Prepare the symmetry variables for the C code
        for i in range(len(self.symmetries)):
            np.savetxt(root_fname + ".block{:d}".format(i), self.degenerate_space[i], fmt = "%d")


            ns, b1, b2 = self.symmetries[i].shape
            with open(root_fname + ".symsb{:d}".format(i), "w") as fp:
                for isym in range(ns):
                    for k1 in range(b1):
                        for k2 in range(b2):
                            fp.write(" {:22.16f}".format(self.symmetries[i][isym, k1, k2]))
                        fp.write("\n")
                    fp.write("\n")

load_from_input_files(root_name='tdscha', directory='.')

Load the results from a calculation performed by the binary executable.

NOTE: You must initialize the ensemble as did before calling the prepare_input_files method. Then execute the lanczos run with the tdscha-lanczos.x executable. Then load the results of the lanczos with this method.

You must use the same keyword used in the prepare_input_files

Source code in tdscha/DynamicalLanczos.py
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
    def load_from_input_files(self, root_name = "tdscha", directory="."):
        """
        Load the results from a calculation performed by the binary executable.

        NOTE: You must initialize the ensemble as did before calling the prepare_input_files method.
        Then execute the lanczos run with the tdscha-lanczos.x executable.
        Then load the results of the lanczos with this method.

        You must use the same keyword used in the prepare_input_files
        """

        if not os.path.exists(directory):
            raise IOError("Error, the specified directory '{}' does not exist".format(directory))

        abc_file = os.path.join(directory, "{}.abc".format(root_name))
        if not os.path.exists(abc_file):
            errmsg = """
Error, the file '{}' does not exist. 
       please, check if you correctly run the tdscha-lanczos.x executable.
""".format(abc_file)
            print(errmsg)
            raise IOError(errmsg)


        data_abc = np.loadtxt(abc_file, dtype= np.double)
        self.a_coeffs = data_abc[:,0]
        self.b_coeffs = data_abc[:, 1]
        self.c_coeffs = data_abc[:, 2]


        # Load the Pbasis and Qbasis only if the files exists.
        # These are very heavy files, and they are not needed for the spectral function
        # in the continued fraction representation.
        # (they are needed to restart a calculation)

        qbasis_file = os.path.join(directory, "{}.qbasis.out".format(root_name))
        pbasis_file = os.path.join(directory, "{}.pbasis.out".format(root_name))
        snorm_file = os.path.join(directory, "{}.snorm.out".format(root_name))

        if os.path.exists(qbasis_file):
            self.basis_Q = np.loadtxt(qbasis_file, dtype = np.double)
        else:
            warnmsg = """
File {} not found. basis_Q not loaded.
""".format(qbasis_file)
            warnings.warn(warnmsg)

        if os.path.exists(pbasis_file):
            self.basis_P = np.loadtxt(pbasis_file, dtype = np.double)
        else:
            warnmsg = """
File {} not found. basis_P not loaded.
""".format(pbasis_file)
            warnings.warn(warnmsg)

        if os.path.exists(snorm_file):
            self.s_norm = np.loadtxt(snorm_file, dtype = np.double).ravel()
        else:
            warnmsg = """
File {} not found. S norm not loaded.
""".format(snorm_file)
            warnings.warn(warnmsg)

        # Load the Json file with other general variables
        with open(os.path.join(directory, root_name + ".json"), "r") as fp:
            json_data = json.load(fp)

        self.perturbation_modulus = json_data["data"]["perturbation_modulus"]
        self.T = json_data["T"]
        self.ignore_harmonic = json_data["ignore_v2"]
        self.ignore_v3 = json_data["ignore_v3"]
        self.ignore_v4 = json_data["ignore_v4"]
        self.N = json_data["data"]["n_configs"]
        self.n_modes = json_data["data"]["n_modes"]
        self.reverse_L = json_data["data"]["reverse"]
        self.shift_value = json_data["data"]["shift"]

prepare_raman(pol_vec_in=np.array([1, 0, 0]), pol_vec_out=np.array([1, 0, 0]), mixed=False, pol_in_2=None, pol_out_2=None, unpolarized=None)

PREPARE LANCZOS FOR RAMAN SPECTRUM

This subroutines prepare the perturbation for the Raman signal.

The raman tensor is read from the dynamical matrix provided by the original ensemble.

Source code in tdscha/DynamicalLanczos.py
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
def prepare_raman(self, pol_vec_in = np.array([1,0,0]), pol_vec_out = np.array([1,0,0]), mixed = False, pol_in_2 = None, pol_out_2 = None, unpolarized: int = None):
    """
    PREPARE LANCZOS FOR RAMAN SPECTRUM
    ==================================

    This subroutines prepare the perturbation for the Raman signal.

    The raman tensor is read from the dynamical matrix provided by the original ensemble.

    Parameters
    ----------
        pol_vec_in : ndarray (size =3)
            The polarization vector of the incoming light
        pol_vec_out : ndarray (size = 3)
            The polarization vector for the outcoming light
        unpolarized : int or None
            The perturbation for unpolarized raman (if different from None, overrides the behaviour
            of pol_vec_in and pol_vec_out). Indices goes from 0 to 6 (included).
            0 is alpha^2
            1 + 2 + 3 + 4 + 5 + 6 are beta^2
            alpha_0 = (xx + yy + zz)^2/9
            beta_1 = (xx -yy)^2 / 2
            beta_2 = (xx -zz)^2 / 2
            beta_3 = (yy -zz)^2 / 2
            beta_4 = 3xy^2
            beta_5 = 3xz^2
            beta_6 = 3yz^2

            The total unpolarized raman intensity is 45 alpha^2 + 7 beta^2
    """

    # Check if the raman tensor is present
    assert not self.dyn.raman_tensor is None, "Error, no Raman tensor found. Cannot initialize the Raman responce"

    # Get the raman vector (apply the ASR and contract the raman tensor with the polarization vectors)
    raman_v = self.dyn.GetRamanVector(pol_vec_in, pol_vec_out)

    if mixed:
        print('Prepare Raman')
        print('Adding other component of the Raman tensor')
        raman_v += self.dyn.GetRamanVector(pol_in_2, pol_out_2)

    # Get the raman vector in the supercelld
    n_supercell = np.prod(self.dyn.GetSupercell())

    if unpolarized is None:
        # Get the raman vector
        raman_v = self.dyn.GetRamanVector(pol_vec_in, pol_vec_out)

        # Get the raman vector in the supercelld
        new_raman_v = np.tile(raman_v.ravel(), n_supercell)

        # Convert in the polarization basis and store the intensity
        self.prepare_perturbation(new_raman_v, masses_exp=-1)
    else:
        px = np.array([1,0,0])
        py = np.array([0,1,0])
        pz = np.array([0,0,1])

        if unpolarized == 0:
            # Alpha
            raman_v = self.dyn.GetRamanVector(px, px)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / 3
            self.prepare_perturbation(new_raman_v, masses_exp=-1)

            raman_v = self.dyn.GetRamanVector(py, py)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / 3
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)

            raman_v = self.dyn.GetRamanVector(pz, pz)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / 3
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)
        elif unpolarized == 1:
            # (xx -yy)^2 / 2
            raman_v = self.dyn.GetRamanVector(px, px)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)

            raman_v = self.dyn.GetRamanVector(py, py)
            new_raman_v = - np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)
        elif unpolarized == 2:
            # beta_2 = (xx -zz)^2 / 2
            raman_v = self.dyn.GetRamanVector(px, px)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)

            raman_v = self.dyn.GetRamanVector(pz, pz)
            new_raman_v = - np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)
        elif unpolarized == 3:
            # beta_2 = (yy -zz)^2 / 2
            raman_v = self.dyn.GetRamanVector(py, py)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)

            raman_v = self.dyn.GetRamanVector(pz, pz)
            new_raman_v = - np.tile(raman_v.ravel(), n_supercell) / np.sqrt(2)
            self.prepare_perturbation(new_raman_v, masses_exp=-1, add = True)
        elif unpolarized == 4:
            # beta_2 = 3 xy^2
            raman_v = self.dyn.GetRamanVector(px, py)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) * np.sqrt(3)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)
        elif unpolarized == 5:
            # beta_2 = 3 yz^2
            raman_v = self.dyn.GetRamanVector(py, pz)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) * np.sqrt(3)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)
        elif unpolarized == 6:
            # beta_2 = 3 xz^2
            raman_v = self.dyn.GetRamanVector(px, pz)
            new_raman_v = np.tile(raman_v.ravel(), n_supercell) * np.sqrt(3)
            self.prepare_perturbation(new_raman_v, masses_exp=-1)
        else:
            raise ValueError("Error, unpolarized must be between [0, ... ,6] got invalid {}.".format(unpolarized))




    # Convert in the polarization basis and store the intensity
    self.prepare_perturbation(new_raman_v, masses_exp=-1)

get_prefactors_unpolarized_raman(index)

RETURNS THE PREFACTORS FOR COMPUTING THE UNPOLARIZED RAMAN

It returns a dictionary with the prefactors

The prefactors corresponds to the components of the unpolarized raman signal

Source code in tdscha/DynamicalLanczos.py
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
def get_prefactors_unpolarized_raman(self, index):
    """
    RETURNS THE PREFACTORS FOR COMPUTING THE UNPOLARIZED RAMAN
    ==========================================================

    It returns a dictionary with the prefactors

    The prefactors corresponds to the components of the unpolarized raman signal
    """
    labels = [i for i in range(7)]
    if not(index in labels):
        raise ValueError('{} should be in {}'.format(index, labels))

    dictionary = {'(xx+yy+zz)^2' : 45/9,\
                  '(xx-yy)^2'    : 7/2,\
                  '(xx-zz)^2'    : 7/2,\
                  '(yy-zz)^2'    : 7/2,\
                  '(xy)^2'       : 7*3,\
                  '(xz)^2'       : 7*3,\
                  '(yz)^2'       : 7*3}

    keys = list(dictionary.keys())


    return dictionary[keys[index]]

prepare_unpolarized_raman(index=0, debug=False)

PREPARE UNPOLARIZED RAMAN SIGNAL

The raman tensor is read from the dynamical matrix provided by the original ensemble.

The perturbations are prepared accordin to the formula (see https://doi.org/10.1021/jp5125266)

..math:

I_unpol = 45/9 (xx + yy + zz)^2
          + 7/2 [(xx-yy)^2 + (xx-zz)^2 + (yy-zz)^2]
          + 7 * 3 [(xy)^2 + (yz)^2 + (xz)^2]
Source code in tdscha/DynamicalLanczos.py
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
def prepare_unpolarized_raman(self, index = 0, debug = False):
    """
    PREPARE UNPOLARIZED RAMAN SIGNAL
    ================================

    The raman tensor is read from the dynamical matrix provided by the original ensemble.

    The perturbations are prepared accordin to the formula (see https://doi.org/10.1021/jp5125266)

    ..math:

        I_unpol = 45/9 (xx + yy + zz)^2
                  + 7/2 [(xx-yy)^2 + (xx-zz)^2 + (yy-zz)^2]
                  + 7 * 3 [(xy)^2 + (yz)^2 + (xz)^2]
    """
    # Check if the raman tensor is present
    assert not self.dyn.raman_tensor is None, "Error, no Raman tensor found. Cannot initialize the Raman responce"

    labels = [i for i in range(7)]
    if not(index in labels):
        raise ValueError('{} should be in {}'.format(index, labels))

    epols = {'x' : np.array([1,0,0]),\
             'y' : np.array([0,1,0]),\
             'z' : np.array([0,0,1])}

    # (xx + yy + zz)^2
    if index == 0:
        raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        raman_v += self.dyn.GetRamanVector(epols['y'], epols['y'])
        raman_v += self.dyn.GetRamanVector(epols['z'], epols['z'])
    # (xx - yy)^2    
    elif index == 1:
        raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        raman_v -= self.dyn.GetRamanVector(epols['y'], epols['y'])
    # (xx - zz)^2       
    elif index == 2:
        raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        raman_v -= self.dyn.GetRamanVector(epols['z'], epols['z'])
    # (yy - zz)^2   
    elif index == 3:
        raman_v  = self.dyn.GetRamanVector(epols['y'], epols['y'])
        raman_v -= self.dyn.GetRamanVector(epols['z'], epols['z'])
    # (xy)^2
    elif index == 4:
        raman_v = self.dyn.GetRamanVector(epols['x'], epols['y'])
    # (xz)^2
    elif index == 5:
        raman_v = self.dyn.GetRamanVector(epols['x'], epols['z'])
    # (yz)^2
    elif index == 6:
        raman_v = self.dyn.GetRamanVector(epols['y'], epols['z'])

    if debug:
        np.save('raman_v_{}'.format(index), raman_v)

    # Get the raman vector in the supercelld
    n_supercell = np.prod(self.dyn.GetSupercell())
    new_raman_v = np.tile(raman_v.ravel(), n_supercell)

    # Convert in the polarization basis and store the intensity
    self.prepare_perturbation(new_raman_v, masses_exp=-1)

    if debug:
        print('[NEW] Pertubation modulus with eq Raman tensors = {}'.format(self.perturbation_modulus))
    print()

    return

prepare_unpolarized_raman_FT(index=0, debug=False, eq_raman_tns=None, use_symm=True, ens_av_raman=None, raman_tns_ens=None, add_2ph=True)

PREPARE UNPOLARIZED RAMAN SIGNAL CONSIDERING FLUCTUATIONS OF THE RAMAN TENSOR

The raman tensor is read from the dynamical matrix provided by the original ensemble.

The perturbations are prepared accordin to the formula (see https://doi.org/10.1021/jp5125266)

..math:

I_unpol = 45/9 (xx + yy + zz)^2
          + 7/2 [(xx-yy)^2 + (xx-zz)^2 + (yy-zz)^2]
          + 7 * 3 [(xy)^2 + (yz)^2 + (xz)^2]
Parameters:
-index: the pol component of the unpolarized signal
-debug: if true we save the second order Raman tensor
-eq_raman_tns: np.array with shape (3, 3, 3 * N_at_uc), the equilibirum raman tensor
-use_symm: bool, if True symmetries are enforced
-ens_av_raman:  the ensemble on which we compute the averages of the Raman tensors
-raman_tns_ens: np.array with shape (N_conf, 3, 3, 3 * N_at_sc), the raman tensors on the displaced configruations
Source code in tdscha/DynamicalLanczos.py
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
def prepare_unpolarized_raman_FT(self, index = 0, debug = False, eq_raman_tns = None, use_symm = True,\
                                 ens_av_raman = None, raman_tns_ens = None, add_2ph = True):
    """
    PREPARE UNPOLARIZED RAMAN SIGNAL CONSIDERING FLUCTUATIONS OF THE RAMAN TENSOR
    =============================================================================

    The raman tensor is read from the dynamical matrix provided by the original ensemble.

    The perturbations are prepared accordin to the formula (see https://doi.org/10.1021/jp5125266)

    ..math:

        I_unpol = 45/9 (xx + yy + zz)^2
                  + 7/2 [(xx-yy)^2 + (xx-zz)^2 + (yy-zz)^2]
                  + 7 * 3 [(xy)^2 + (yz)^2 + (xz)^2]

    Parameters:
    -----------
        -index: the pol component of the unpolarized signal
        -debug: if true we save the second order Raman tensor
        -eq_raman_tns: np.array with shape (3, 3, 3 * N_at_uc), the equilibirum raman tensor
        -use_symm: bool, if True symmetries are enforced
        -ens_av_raman:  the ensemble on which we compute the averages of the Raman tensors
        -raman_tns_ens: np.array with shape (N_conf, 3, 3, 3 * N_at_sc), the raman tensors on the displaced configruations
    """
    # Check if the raman tensor is present
    assert not self.dyn.raman_tensor is None, "Error, no Raman tensor found. Cannot initialize the Raman responce"

    labels = [i for i in range(7)]
    if not(index in labels):
        raise ValueError('{} should be in {}'.format(index, labels))

    epols = {'x' : np.array([1,0,0]),\
             'y' : np.array([0,1,0]),\
             'z' : np.array([0,0,1])}

    # (xx + yy + zz)^2
    if index == 0:
        # raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        # raman_v += self.dyn.GetRamanVector(epols['y'], epols['y'])
        # raman_v += self.dyn.GetRamanVector(epols['z'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   = epols['x'], pol_out   = epols['x'],\
                                         mixed = True,\
                                         pol_in_2 = epols['y'], pol_out_2 = epols['y'],\
                                         pol_in_3 = epols['z'], pol_out_3 = epols['z'],\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xx_plus_yy_plus_zz')
    # (xx - yy)^2    
    elif index == 1:
        # raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        # raman_v -= self.dyn.GetRamanVector(epols['y'], epols['y'])
        # NB we put just one minus sign because the component is (xx - yy)^2
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['x'],  pol_out  =  epols['x'],\
                                         mixed = True,\
                                         pol_in_2 = -epols['y'], pol_out_2 =  epols['y'],\
                                         pol_in_3 = np.zeros(3), pol_out_3 = np.zeros(3),\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xx_minus_yy')
    # (xx - zz)^2       
    elif index == 2:
        # raman_v  = self.dyn.GetRamanVector(epols['x'], epols['x'])
        # raman_v -= self.dyn.GetRamanVector(epols['z'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['x'],  pol_out  =  epols['x'],\
                                         mixed = True,\
                                         pol_in_2 = -epols['z'], pol_out_2 =  epols['z'],\
                                         pol_in_3 = np.zeros(3), pol_out_3 = np.zeros(3),\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xx_minus_zz')
    # (yy - zz)^2   
    elif index == 3:
        # raman_v  = self.dyn.GetRamanVector(epols['y'], epols['y'])
        # raman_v -= self.dyn.GetRamanVector(epols['z'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['y'],  pol_out  =  epols['y'],\
                                         mixed = True,\
                                         pol_in_2 = -epols['z'], pol_out_2 =  epols['z'],\
                                         pol_in_3 = np.zeros(3), pol_out_3 = np.zeros(3),\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'yy_minus_zz')
    # (xy)^2
    elif index == 4:
        # raman_v = self.dyn.GetRamanVector(epols['x'], epols['y'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['x'],  pol_out  =  epols['y'],\
                                         mixed = False,\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xy_square')
    # (xz)^2
    elif index == 5:
        # raman_v = self.dyn.GetRamanVector(epols['x'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['x'],  pol_out  =  epols['z'],\
                                         mixed = False,\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'xz_square')
    # (yz)^2
    elif index == 6:
        # raman_v = self.dyn.GetRamanVector(epols['y'], epols['z'])
        self.prepare_anharmonic_raman_FT(raman = raman_tns_ens, raman_eq = eq_raman_tns,\
                                         pol_in   =  epols['y'],  pol_out  =  epols['z'],\
                                         mixed = False,\
                                         add_two_ph = add_2ph, symmetrize = use_symm,\
                                         ensemble = ens_av_raman,\
                                         save_raman_tensor2 = debug, file_raman_tensor2 = 'yz_square')

    return

prepare_anharmonic_raman_FT(raman=None, raman_eq=None, pol_in=np.array([1.0, 0.0, 0.0]), pol_out=np.array([1.0, 0.0, 0.0]), mixed=False, pol_in_2=None, pol_out_2=None, pol_in_3=None, pol_out_3=None, add_two_ph=False, symmetrize=False, ensemble=None, save_raman_tensor2=False, file_raman_tensor2=None)

PREPARE THE PSI VECTOR FOR ANHARMONIC RAMAN SPECTRUM CALCULATION (NEW VERSION)

This works only with the Wigner representation if we add the two phonons effect. Prepare the psi vector for RAMAN spectrum considering position-dependent raman tensors.

Parameters:
-raman: nd.array (N_configs, E_comp, E_comp, 3 * N_at_sc),
     the Raman tensor for all configurations.
     Indices are: Number of configuration, electric field component,
     electric field component, atomic coordinates in sc.
rama_eq: nd.array, (E_comp, E_comp, 3 * N_at_uc), the effective charges at equilibrium.
     Indices are: electric field component,
     electric field component, atomic coordinate in uc.   
-pol_in: nd.array, the polarization of in-out light. default is x
-pol_out: nd.array, the polarization of in-out light. default is x
-mixed: if True we can study the one and two phonon response to 
        pol_in \cdto \Xi \cdot pol_in + pol_in_2 \cdto \Xi \cdot pol_in_2 + pol_in_3 \cdto \Xi \cdot pol_in_3
        (\Xi is the Raman tensor)
-pol_in_2:  nd.array, the polarization of in-out light. default is None
-pol_out_2: nd.array, the polarization of in-out light. default is None
-pol_in_3:  nd.array, the polarization of in-out light. default is None
-pol_out_3: nd.array, the polarization of in-out light. default is None
-add_two_ph: bool, if True two phonon processes are included in the calculation
-symmetrize: bool, if True the first/second order Raman tensors are symmetrized
-ensemble: a scha ensemble object for computing the averages
-save_raman_tensor2: bool if True we save the second order Raman tensor
Source code in tdscha/DynamicalLanczos.py
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
def prepare_anharmonic_raman_FT(self, raman = None, raman_eq = None,\
                                pol_in = np.array([1.,0.,0.]), pol_out = np.array([1.,0.,0.]),\
                                mixed = False, pol_in_2 = None, pol_out_2 = None,\
                                pol_in_3 = None, pol_out_3 = None,\
                                add_two_ph = False, symmetrize = False, ensemble = None,\
                                save_raman_tensor2 = False, file_raman_tensor2 = None):
    """
    PREPARE THE PSI VECTOR FOR ANHARMONIC RAMAN SPECTRUM CALCULATION (NEW VERSION)
    ===========================================================================

    This works only with the Wigner representation if we add the two phonons effect. 
    Prepare the psi vector for RAMAN spectrum considering position-dependent raman tensors.

    Parameters:
    -----------
        -raman: nd.array (N_configs, E_comp, E_comp, 3 * N_at_sc),
             the Raman tensor for all configurations.
             Indices are: Number of configuration, electric field component,
             electric field component, atomic coordinates in sc.
        rama_eq: nd.array, (E_comp, E_comp, 3 * N_at_uc), the effective charges at equilibrium.
             Indices are: electric field component,
             electric field component, atomic coordinate in uc.   
        -pol_in: nd.array, the polarization of in-out light. default is x
        -pol_out: nd.array, the polarization of in-out light. default is x
        -mixed: if True we can study the one and two phonon response to 
                pol_in \cdto \Xi \cdot pol_in + pol_in_2 \cdto \Xi \cdot pol_in_2 + pol_in_3 \cdto \Xi \cdot pol_in_3
                (\Xi is the Raman tensor)
        -pol_in_2:  nd.array, the polarization of in-out light. default is None
        -pol_out_2: nd.array, the polarization of in-out light. default is None
        -pol_in_3:  nd.array, the polarization of in-out light. default is None
        -pol_out_3: nd.array, the polarization of in-out light. default is None
        -add_two_ph: bool, if True two phonon processes are included in the calculation
        -symmetrize: bool, if True the first/second order Raman tensors are symmetrized
        -ensemble: a scha ensemble object for computing the averages
        -save_raman_tensor2: bool if True we save the second order Raman tensor
    """
    if not self.use_wigner and add_two_ph:
        raise NotImplementedError('The two phonon processes are implemented only in Wigner')

    if raman is None:
        raise ValueError('Must specify the raman tensors for all configurations!')

    if mixed:
        #Check that we have the other polarization vectors
        if (pol_in_2 is None) or (pol_out_2 is None):
            raise ValueError('Must specify pol_in_2 pol_out_2 if mixed = True!')

        if (pol_in_3 is None) or (pol_out_3 is None):
            raise ValueError('Must specify pol_in_3 pol_out_3 if mixed = True!')

        if len(pol_in_2) != 3 or len(pol_out_2) != 3:
            raise ValueError('pol_in_2 pol_out_2 must be array of len 3')

        if len(pol_in_3) != 3 or len(pol_out_3) != 3:
            raise ValueError('pol_in_3 pol_out_3 must be array of len 3')


    print()
    print('PREPARE THE RAMAN ANHARMONIC SPECTRUM CALCULATION')
    print('=================================================')
    print('Are we considering two ph effects? = {}'.format(add_two_ph))
    print('Are we using Wigner? = {}'.format(self.use_wigner))
    print('Are we symmetrizing the raman tensor? = {}'.format(symmetrize))
    print()
    if ensemble is not None:
        Nconf = ensemble.N
    else:
        Nconf = self.N

    required = 'N_conf - E_field - E_field - 3 * N_at_sc'
    assert raman.shape[0] == Nconf, 'The raman tensor in input have the wrong shape. The required is {}'.format(required)
    assert raman.shape[1] == 3, 'The raman tensor in input have the wrong shape. The required is {}'.format(required)
    assert raman.shape[2] == 3, 'The raman tensor in input have the wrong shape. The required is {}'.format(required)
    assert raman.shape[3] == self.nat * 3, 'The raman tensor in input have the wrong shape. The required is {}'.format(required)

    # alpha is the polarizability

    # Get the average of the raman tensor, np.array with shape = (3, 3, 3 * N_at_sc)
    d1alpha_dR_av = perturbations.get_d1alpha_dR_av(ensemble, raman, symmetrize = symmetrize)

    # Get the supercell dyn then set the raman tensor euqal to d1alpha_dR_av
    sc_dyn = self.dyn.GenerateSupercellDyn(self.dyn.GetSupercell())
    sc_dyn.raman_tensor = d1alpha_dR_av

    # Get the Raman vector np.array (3 * N_at_sc)
    raman_vector_sc = sc_dyn.GetRamanVector(pol_in, pol_out)

    if mixed:
        print('ONE PH SECTOR adding compoent pol_in_2 pol_out_2 of the Raman tensor')
        raman_vector_sc += sc_dyn.GetRamanVector(pol_in_2, pol_out_2)
        print('ONE PH SECTOR adding compoent pol_in_3 pol_out_3 of the Raman tensor')
        raman_vector_sc += sc_dyn.GetRamanVector(pol_in_3, pol_out_3)


    # Now rescale by the mass and go in polarizaiton basis
    self.prepare_perturbation(raman_vector_sc, masses_exp = -1)
    print('[NEW] Pertubation modulus with one ph effects only = {}'.format(self.perturbation_modulus))
    print()

    # NOW PREPARE THE SECOND RAMAN TENSOR
    if add_two_ph:
        if raman_eq is not None:
            print('[NEW] Getting the equilibirum RAMAN tensor...')
            print()
            n_supercell = np.prod(self.dyn.GetSupercell())
            # raman_eq is np.array with shape = (E_field, E_field, N_at_uc * 3)
            raman_eq_size = np.shape(raman_eq)
            MSG = """
            Error, raman tns of the wrong shape: {}
            """.format(raman_eq_size)
            assert len(raman_eq_size) == 3, MSG
            if not self.ignore_small_w:
                assert raman_eq_size[2] * n_supercell == self.nat * 3 #self.n_modes + 3
            assert raman_eq_size[0] == raman_eq_size[1] == 3

            # Get the raman tensor in the supercell (E_field, E_filed, 3 * N_at_sc)
            raman_eq_gamma = np.zeros((3, 3, 3 * n_supercell * self.dyn.structure.N_atoms), dtype = type(raman_eq[0,0,0]))
            raman_eq_gamma = np.tile(raman_eq, n_supercell)

        print('[NEW] Getting the two phonon contribution in RAMAN...')

        # d2M_dR np.array with shape = (3 * N_atoms, 3 * N_atoms, Efield)
        if raman_eq is not None:
            print('[NEW] Subtracting the equilibirum RAMAN tensor...')
            # raman - raman_eq_gamma, np.array with shape = (N_configs, Efield, Efield, 3 * N_at_sc)
            # THE RESULT HAS shape = (Efield, Efield, 3 * N_at_sc, 3 * N_at_sc)
            d2alpha_dR = perturbations.get_d2alpha_dR_av(ensemble, raman - raman_eq_gamma, None, symmetrize = symmetrize)
        else:
            # THE RESULT HAS shape = (Efield, Efield, 3 * N_at_sc, 3 * N_at_sc)
            d2alpha_dR = perturbations.get_d2alpha_dR_av(ensemble, raman, None, symmetrize = symmetrize)

        print('[NEW] Divide by the masses')
        # Divide by the masses of the atoms in the supercell shape =  (Efield, Efield, 3 * N_at_sc, 3 * N_at_sc)
        d2alpha_dR = np.einsum('c, abcd, d -> abcd', np.sqrt(self.m)**-1, d2alpha_dR, np.sqrt(self.m)**-1)

        if save_raman_tensor2:
            print('[NEW] Saving the second-order SCHA Raman tensor')
            np.save('{}'.format(file_raman_tensor2), d2alpha_dR)
            return

        print('[NEW] Go in polarization basis')
        # Now go in polarization basis, np.array with shape = (E_field, E_field, n_modes, n_modes)
        # d2alpha_dR_muspace = np.einsum('cm, abcd, dn -> abmn', self.pols, d2alpha_dR, self.pols)
        # -> substitute
        tmp                = np.einsum('abcd, cm -> abmd', d2alpha_dR, self.pols)
        d2alpha_dR_muspace = np.einsum('abmd, dn -> abmn', tmp, self.pols)

        # Project along the direction of the filed, np.array with shape = (n_modes, n_modes)
        dXi_dR_muspace = np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in, pol_out)

        if mixed:
            print('TWO PH SECTOR adding component pol_in_2 pol_out_2 of the Raman tensor')
            dXi_dR_muspace += np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in_2, pol_out_2)
            print('TWO PH SECTOR adding component pol_in_3 pol_out_3 of the Raman tensor')
            dXi_dR_muspace += np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in_3, pol_out_3)

        # Symmetrize in mu space, np.array with shape = (n_modes, n_modes)
        dXi_dR_muspace = 0.5 * (dXi_dR_muspace + dXi_dR_muspace.T)

        # Get chi_minus and chi_plus tensors, np.array with shape = (n_modes, n_modes)
        chi_minus = self.get_chi_minus()
        chi_plus  = self.get_chi_plus()

        # Get the pertubations on a'^(1) b'^(1)
        pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), dXi_dR_muspace)
        pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , dXi_dR_muspace)

        # Check if everything is symmetric
        assert np.all(np.abs(dXi_dR_muspace - dXi_dR_muspace.T) < 1e-10), "Second derivative of the polarizability is not symmetric in pol basis"
        assert np.all(np.abs(pert_a - pert_a.T) < 1e-10), "a'(1) pertubation is not symmetric in pol basis"
        assert np.all(np.abs(pert_b - pert_b.T) < 1e-10), "b'(1) pertubation is not symmetric in pol basis"

        # Now get the perturbation for a'^(1)
        current = self.n_modes
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
            current = current + self.n_modes - i

        # Now get the pertrubation for b'^(1)
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
            current = current + self.n_modes - i

        # Add the mask dot taking into account symmetric elements
        mask_dot = self.mask_dot_wigner()
        # OVERWRITE the pertubation modulus considering the two phonon sector
        self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

        print('[NEW] Perturbation modulus after adding two ph contributions RAMAN = {}'.format(self.perturbation_modulus))
        print()

    return

prepare_anharmonic_raman_FT_2ph(d2alpha_dR=None, pol_in=np.array([1.0, 0.0, 0.0]), pol_out=np.array([1.0, 0.0, 0.0]), mixed=False, pol_in_2=None, pol_out_2=None)

PREPARE THE PSI VECTOR FOR RAMAN SPECTRUM CALCULATION (NEW VERSION) DIRECTLY FROM 2nd ORDER RAMAN TENSOR

This function is useful if we want to interpolate the 2nd Raman tensor on a bigger supercell.

This works only with the Wigner representation if we add the two phonons effect. Prepare the psi vector for RAMAN spectrum considering position-dependent raman tensors.

NOTE: we completely neglect the frist order Raman scattering!

Parameters:
-d2alpha_dR: nd.array (E_comp, E_comp, 3 * N_at_sc, 3 * N_at_sc),
     2nd order Raman tensor.
     Indices are: Number of configuration, electric field component,
     electric field component, atomic coordinates in sc.   
-pol_in: nd.array, the polarization of in-out light. default is x
-pol_out: nd.array, the polarization of in-out light. default is x
-mixed: if True we can study the one and two phonon response to 
        pol_in \cdot \Xi \cdot pol_out + pol_in_2 \cdot \Xi \cdot pol_out_2
        (\Xi is the Raman tensor)
-pol_in_2: nd.array, the polarization of in-out light. default is x
-pol_out_2: nd.array, the polarization of in-out light. default is x
Source code in tdscha/DynamicalLanczos.py
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
def prepare_anharmonic_raman_FT_2ph(self, d2alpha_dR = None, pol_in = np.array([1.,0.,0.]), pol_out = np.array([1.,0.,0.]),\
                                mixed = False, pol_in_2 = None, pol_out_2 = None):
    """
    PREPARE THE PSI VECTOR FOR RAMAN SPECTRUM CALCULATION (NEW VERSION) DIRECTLY FROM 2nd ORDER RAMAN TENSOR
    ========================================================================================================

    This function is useful if we want to interpolate the 2nd Raman tensor on a bigger supercell.

    This works only with the Wigner representation if we add the two phonons effect. 
    Prepare the psi vector for RAMAN spectrum considering position-dependent raman tensors.

    NOTE: we completely neglect the frist order Raman scattering!

    Parameters:
    -----------
        -d2alpha_dR: nd.array (E_comp, E_comp, 3 * N_at_sc, 3 * N_at_sc),
             2nd order Raman tensor.
             Indices are: Number of configuration, electric field component,
             electric field component, atomic coordinates in sc.   
        -pol_in: nd.array, the polarization of in-out light. default is x
        -pol_out: nd.array, the polarization of in-out light. default is x
        -mixed: if True we can study the one and two phonon response to 
                pol_in \cdot \Xi \cdot pol_out + pol_in_2 \cdot \Xi \cdot pol_out_2
                (\Xi is the Raman tensor)
        -pol_in_2: nd.array, the polarization of in-out light. default is x
        -pol_out_2: nd.array, the polarization of in-out light. default is x
    """
    if not self.use_wigner:
        raise NotImplementedError('The two phonon processes are implemented only in Wigner')

    if d2alpha_dR is None:
        raise ValueError('Must specify the 2nd order Raman tensor!')

    exp_shape = (3, 3, self.nat * 3, self.nat * 3)
    if d2alpha_dR.shape != exp_shape:
        raise ValueError('The shape of the 2nd order Raman tensor is not correct, expected {}'.format(exp_shape))

    if mixed:
        if (pol_in_2 is None) or (pol_out_2 is None):
            raise ValueError('Must specify pol_in_2 pol_out_2 if mixed = True!')

        if len(pol_in_2) != 3 or len(pol_out_2) != 3:
            raise ValueError('pol_in_2 pol_out_2 must be array of len 3')


    print()
    print('PREPARE THE RAMAN ANHARMONIC SPECTRUM CALCULATION FROM 2nd ORDER RAMAN TENSOR')
    print('=============================================================================')
    # print('Are we considering two ph effects? = {}'.format(add_two_ph))
    print('Are we using Wigner? = {}'.format(self.use_wigner))
    # print('Are we symmetrizing the raman tensor? = {}'.format(symmetrize))
    print()

    print('TWO PH Going in polarization basis')
    # Now go in polarization basis, np.array with shape = (E_field, E_field, n_modes, n_modes)
    # d2alpha_dR_muspace = np.einsum('cm, abcd, dn -> abmn', self.pols, d2alpha_dR, self.pols)
    # -> substitute
    tmp                = np.einsum('abcd, cm -> abmd', d2alpha_dR, self.pols)
    d2alpha_dR_muspace = np.einsum('abmd, dn -> abmn', tmp, self.pols)
    # print(d2alpha_dR_muspace.shape)

    print('TWO PH Selecting the polarizations')
    # Project along the direction of the filed, np.array with shape = (n_modes, n_modes)
    dXi_dR_muspace = np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in, pol_out)
    # print(dXi_dR_muspace.shape)

    if mixed:
        print('TWO PH SECTOR adding component pol_in_2 pol_out_2 of the Raman tensor')
        dXi_dR_muspace += np.einsum('abmn, a, b -> mn', d2alpha_dR_muspace, pol_in_2, pol_out_2)

    # Symmetrize in mu space, np.array with shape = (n_modes, n_modes)
    dXi_dR_muspace = 0.5 * (dXi_dR_muspace + dXi_dR_muspace.T)

    # Get chi_minus and chi_plus tensors, np.array with shape = (n_modes, n_modes)
    chi_minus = self.get_chi_minus()
    chi_plus  = self.get_chi_plus()

    # Get the pertubations on a'^(1) b'^(1)
    pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), dXi_dR_muspace)
    pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , dXi_dR_muspace)

    # Check if everything is symmetric
    assert np.all(np.abs(dXi_dR_muspace - dXi_dR_muspace.T) < 1e-10), "Second derivative of the polarizability is not symmetric in pol basis"
    assert np.all(np.abs(pert_a - pert_a.T) < 1e-10), "a'(1) pertubation is not symmetric in pol basis"
    assert np.all(np.abs(pert_b - pert_b.T) < 1e-10), "b'(1) pertubation is not symmetric in pol basis"

    print('[NEW] Perturbation modulus = {}'.format(self.perturbation_modulus))
    print()

    # Now get the perturbation for a'^(1)
    current = self.n_modes
    for i in range(self.n_modes):
        self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
        current = current + self.n_modes - i

    # Now get the pertrubation for b'^(1)
    for i in range(self.n_modes):
        self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
        current = current + self.n_modes - i

    # Add the mask dot taking into account symmetric elements
    mask_dot = self.mask_dot_wigner()
    # OVERWRITE the pertubation modulus considering the two phonon sector
    self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

    print('[NEW] Perturbation modulus adding two ph contributions RAMAN = {}'.format(self.perturbation_modulus))
    print()

    return

prepare_ir(effective_charges=None, pol_vec=np.array([1, 0, 0]))

PREPARE LANCZOS FOR INFRARED SPECTRUM COMPUTATION

In this subroutine we prepare the lanczos algorithm for the computation of the infrared spectrum signal.

Source code in tdscha/DynamicalLanczos.py
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
def prepare_ir(self, effective_charges = None, pol_vec = np.array([1,0,0])):
    """
    PREPARE LANCZOS FOR INFRARED SPECTRUM COMPUTATION
    =================================================

    In this subroutine we prepare the lanczos algorithm for the computation of the
    infrared spectrum signal.

    Parameters
    ----------
        effective_charges : ndarray(size = (n_atoms, 3, 3), dtype = np.double)
            The effective charges. Indices are: Number of atoms in the unit cell,
            electric field component, atomic coordinate. If None, the effective charges
            contained in the dynamical matrix will be considered.
        pol_vec : ndarray(size = 3)
            The polarization vector of the light.
    """

    ec = self.dyn.effective_charges
    if not effective_charges is None:
        ec = effective_charges

    n_supercell = np.prod(self.dyn.GetSupercell())

    # Check the effective charges
    assert not ec is None, "Error, no effective charge found. Cannot initialize IR responce"

    ec_size = np.shape(ec)
    MSG = """
    Error, effective charges of the wrong shape: {}
    Number of modes : {}
    Dimension of the supercell : {}
    """.format(ec_size, self.n_modes, n_supercell)
    assert len(ec_size) == 3, MSG
    if not self.ignore_small_w:
        assert ec_size[0] * ec_size[2] * n_supercell == self.n_modes + 3, MSG
    assert ec_size[1] == ec_size[2] == 3, MSG

    # shape = (N_at_uc, 3)
    z_eff = np.einsum("abc, b", ec, pol_vec)

    # Get the gamma effective charge
    new_zeff = np.tile(z_eff.ravel(), n_supercell)

    self.prepare_perturbation(new_zeff, masses_exp = -1)

prepare_anharmonic_ir_FT(ec=None, ec_eq=None, pol_vec_light=np.array([1.0, 0.0, 0.0]), add_two_ph=False, symmetrize=False, ensemble=None)

PREPARE THE PSI VECTOR FOR ANHARMONIC IR SPECTRUM CALCULATION (NEW VERSION)

This works only with the Wigner representation if we add the two phonons effect. Prepare the psi vector for IR spectrum considering position-dependent effective charges.

The one phonon scetor is symmetrized by default

Parameters:
-effective_charges: nd.array (N_configs, N_atoms_sc, E_comp, cart_comp),
     the effective charges for all configurations.
     Indices are: Number of configuration, number of atoms in the super cell,
     electric field component, atomic coordinate.
-effective_charges_eq: nd.array, (N_atoms_uc, E_comp, cart_comp), the effective charges at equilibrium.
     Indices are: number of atoms in the unit cell,
     electric field component, atomic coordinate.   
-pol_vec_light: nd.array, the polarization of in-out light. default is x
-add_two_ph: bool, if True two phonon processes are included in the calculation
-symmetrize: bool, if True the first/second order effective charges are symmetrized
-ensemble: a scha ensemble object for computing the averages
Source code in tdscha/DynamicalLanczos.py
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
def prepare_anharmonic_ir_FT(self, ec = None, ec_eq = None, pol_vec_light = np.array([1.,0.,0.]), add_two_ph = False, symmetrize = False, ensemble = None):
    """
    PREPARE THE PSI VECTOR FOR ANHARMONIC IR SPECTRUM CALCULATION (NEW VERSION)
    ===========================================================================

    This works only with the Wigner representation if we add the two phonons effect. 
    Prepare the psi vector for IR spectrum considering position-dependent effective charges.

    The one phonon scetor is symmetrized by default

    Parameters:
    -----------
        -effective_charges: nd.array (N_configs, N_atoms_sc, E_comp, cart_comp),
             the effective charges for all configurations.
             Indices are: Number of configuration, number of atoms in the super cell,
             electric field component, atomic coordinate.
        -effective_charges_eq: nd.array, (N_atoms_uc, E_comp, cart_comp), the effective charges at equilibrium.
             Indices are: number of atoms in the unit cell,
             electric field component, atomic coordinate.   
        -pol_vec_light: nd.array, the polarization of in-out light. default is x
        -add_two_ph: bool, if True two phonon processes are included in the calculation
        -symmetrize: bool, if True the first/second order effective charges are symmetrized
        -ensemble: a scha ensemble object for computing the averages
    """
    if not self.use_wigner and add_two_ph:
        raise NotImplementedError('The two phonon processes are implemented only in Wigner')

    if ec is None:
        raise ValueError('Must specify the effective charges for all configurations!')


    print()
    print('PREPARE THE IR ANHARMONIC SPECTRUM CALCULATION')
    print('==============================================')
    print('Are we considering two ph effects? = {}'.format(add_two_ph))
    print('Are we using Wigner? = {}'.format(self.use_wigner))
    print('Are we symmetrizing the effective charges? = {}'.format(symmetrize))
    print()

    required = 'N_conf N_at_sc E_field cart'
    assert ec.shape[0] == ensemble.N, 'The effective charges in input have the wrong shape. The required is {}'.format(required)
    assert ec.shape[1] == self.nat, 'The effective charges in input have the wrong shape. The required is {}'.format(required)
    assert ec.shape[2] == ec.shape[3] == 3, 'The effective charges in input have the wrong shape. The required is {}'.format(required)

    # Get the average of the dipole moment, np.array with shape = (3 * N_at_sc, 3)
    d1M_dR_av = perturbations.get_d1M_dR_av(ensemble, ec, symmetrize = symmetrize)

    # Project along the direction of light polarization, (3 * N_at_sc)
    Z = np.einsum("ab, b -> a", d1M_dR_av, pol_vec_light)

    # Now rescale by the mass and go in polarizaiton basis
    self.prepare_perturbation(Z.ravel(), masses_exp = -1)
    print('Pertubation modulus with one ph effects only = {}'.format(self.perturbation_modulus))
    print()

    # NOW PREPARE THE SECOND ORDER DIPOLE MOMENT
    if add_two_ph:
        if ec_eq is not None:
            print('[NEW] Getting the equilibirum effective charges...')
            print()
            n_supercell = np.prod(self.dyn.GetSupercell())
            # ec_eq is np.array with shape = (N_at_uc, E_field, cart)
            ec_eq_size = np.shape(ec_eq)
            MSG = """
            Error, effective charges of the wrong shape: {}
            """.format(ec_eq_size)
            assert len(ec_eq_size) == 3, MSG
            if not self.ignore_small_w:
                assert ec_eq_size[0] * ec_eq_size[2] * n_supercell == self.n_modes + 3
            assert ec_eq_size[1] == ec_eq_size[2] == 3

            # Get the eq effective charges in the supercell (N_at_sc, E_field, 3)
            ec_eq_gamma = np.zeros((n_supercell * self.dyn.structure.N_atoms, 3, 3), dtype = type(ec_eq[0]))
            ec_eq_gamma = np.tile(ec_eq, (n_supercell,1,1))

        print('[NEW] Getting the two phonon contribution...')

        # d2M_dR np.array with shape = (3 * N_atoms, 3 * N_atoms, Efield)
        if ec_eq is not None:
            print('[NEW] Subtracting the equilibirum effective charges...')
            # ec - ec_eq_gamma, np.array with shape = (N_configs, N_at_sc, Efield, cart)
            d2M_dR = perturbations.get_d2M_dR_av(ensemble, ec - ec_eq_gamma, None, symmetrize = symmetrize)
        else:
            d2M_dR = perturbations.get_d2M_dR_av(ensemble, ec, None, symmetrize = symmetrize)

        # Divide by the masses of the atoms in the supercell
        d2M_dR = np.einsum('a, abc, b -> abc', np.sqrt(self.m)**-1, d2M_dR, np.sqrt(self.m)**-1)

        # Now go in polarization basis, np.array with shape = (n_modes, n_modes, E_filed)
        d2M_dR_muspace = np.einsum('am, abc, bn -> mnc', self.pols, d2M_dR, self.pols)

        # Project along the direction of the filed, np.array with shape = (n_modes, n_modes)
        dZ_dR_muspace = np.einsum('mnc, c -> mn', d2M_dR_muspace, pol_vec_light)

        # Symmetrize in mu space, np.array with shape = (n_modes, n_modes)
        dZ_dR_muspace = 0.5 * (dZ_dR_muspace + dZ_dR_muspace.T)

        # Get chi_minus and chi_plus tensors, np.array with shape = (n_modes, n_modes)
        chi_minus = self.get_chi_minus()
        chi_plus  = self.get_chi_plus()

        # Get the pertubations on a'^(1) b'^(1)
        pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), dZ_dR_muspace)
        pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , dZ_dR_muspace)

        # Check if everything is symmetric
        assert np.all(np.abs(dZ_dR_muspace - dZ_dR_muspace.T) < 1e-10), "Second derivative of the dipole is not symmetric in pol basis"
        assert np.all(np.abs(pert_a - pert_a.T) < 1e-10), "a'(1) pertubation is not symmetric in pol basis"
        assert np.all(np.abs(pert_b - pert_b.T) < 1e-10), "b'(1) pertubation is not symmetric in pol basis"

        # Now get the perturbation for a'^(1)
        current = self.n_modes
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
            current = current + self.n_modes - i

        # Now get the pertrubation for b'^(1)
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
            current = current + self.n_modes - i

        # Add the mask dot taking into account symmetric elements
        mask_dot = self.mask_dot_wigner()
        # OVERWRITE the pertubation modulus considering the two phonon sector
        self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

        print('[NEW] Perturbation modulus after adding two ph contributions = {}'.format(self.perturbation_modulus))
        print()

    return

prepare_anharmonic_ir(ec=None, ec_eq=None, pol_vec_light=np.array([1.0, 0.0, 0.0]), add_two_ph=False)

PREPARE THE PSI VECTOR FOR ANHARMONIC IR SPECTRUM CALCULATION

This works only with the Wigner representation if we add the two phonons effect. Prepare the psi vector for IR spectrum considering position-dependent effective charges.

Parameters:
-effective_charges: nd.array (N_configs, N_atoms_sc, E_comp, cart_comp),
     the effective charges for all configurations.
     Indices are: Number of configuration, number of atoms in the super cell,
     electric field component, atomic coordinate.
-effective_charges_eq: nd.array, the effective charges at equilibrium.
     Indices are: number of atoms in the unit cell,
     electric field component, atomic coordinate.   
-pol_vec_light: nd.array, the polarization of in-out light. default is x
-add_two_ph: bool, if True two phonon processes are included in the calculation
-symm_eff_charges: bool, if True the effective charges are symmetrized
-ensemble: a scha ensemble object to compute the effective charges
Source code in tdscha/DynamicalLanczos.py
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
def prepare_anharmonic_ir(self, ec = None, ec_eq = None, pol_vec_light = np.array([1.,0.,0.]), add_two_ph = False):
    """
    PREPARE THE PSI VECTOR FOR ANHARMONIC IR SPECTRUM CALCULATION
    =============================================================

    This works only with the Wigner representation if we add the two phonons effect. 
    Prepare the psi vector for IR spectrum considering position-dependent effective charges.

    Parameters:
    -----------
        -effective_charges: nd.array (N_configs, N_atoms_sc, E_comp, cart_comp),
             the effective charges for all configurations.
             Indices are: Number of configuration, number of atoms in the super cell,
             electric field component, atomic coordinate.
        -effective_charges_eq: nd.array, the effective charges at equilibrium.
             Indices are: number of atoms in the unit cell,
             electric field component, atomic coordinate.   
        -pol_vec_light: nd.array, the polarization of in-out light. default is x
        -add_two_ph: bool, if True two phonon processes are included in the calculation
        -symm_eff_charges: bool, if True the effective charges are symmetrized
        -ensemble: a scha ensemble object to compute the effective charges
    """
    if not self.use_wigner and add_two_ph:
        raise NotImplementedError('The two phonon processes are implemented only in Wigner')

    if ec is None:
        raise ValueError('Must specify the effective charges for all configurations!')

    print()
    print('PREPARE THE IR ANHARMONIC SPECTRUM CALCULATION')
    print('==============================================')
    print('Are we considering two ph effects? = {}'.format(add_two_ph))
    print('Are we using Wigner? = {}'.format(self.use_wigner))
    print()

    # The effective charges for each configuration (N_configs, N_at_sc, E_field_comp, 3)
    eff = np.zeros((self.N, self.nat, 3, 3))

    assert ec.shape == eff.shape, 'The effective charges in input have the wrong shape. The required is {}'.format(eff.shape)

    # Get the effective charges
    eff = ec.copy()

    # FIRST DERIVATIVE OF THE DIPOLE
    # Project along the direction of light polarization, (N_configs, N_at_sc, 3)
    z_eff = np.einsum("iabc, b -> iac", eff, pol_vec_light)

    # FIRST DERIVATIVE OF THE DIPOLE
    # Average of effective charges on the ensemble (N_at_sc, 3)
    d1_M = np.einsum('i, iab -> ab', self.rho, z_eff) /np.sum(self.rho)

    # Now rescale by the mass and go in polarizaiton basis
    self.prepare_perturbation(d1_M.ravel(), masses_exp = -1)
    print('Pertubation modulus with one ph effects only = {}'.format(self.perturbation_modulus))
    print()

    # NOW PREPARE THE SECOND ORDER DIPOLE MOMENT
    if add_two_ph:
        if ec_eq is not None:
            print('Subtracting the equilibirum effective charges...')
            print()
            n_supercell = np.prod(self.dyn.GetSupercell())
            ec_eq_size = np.shape(ec_eq)
            MSG = """
            Error, effective charges of the wrong shape: {}
            """.format(ec_eq_size)
            assert len(ec_eq_size) == 3, MSG
            if not self.ignore_small_w:
                assert ec_eq_size[0] * ec_eq_size[2] * n_supercell == self.n_modes + 3
            assert ec_eq_size[1] == ec_eq_size[2] == 3

            # Eq effective charges, (N_at_uc, 3)
            z_eff_eq = np.einsum("abc, b -> ac", ec_eq, pol_vec_light)
            # Eq effective charges at gamma, (N_at_sc, 3)
            z_eff_eq_gamma = np.tile(z_eff_eq.ravel(), n_supercell).reshape((self.nat, 3))

            # This should reduce the noise when computing the 2ph vertex
            z_eff -= z_eff_eq_gamma

        print('[OLD] Getting the two phonon contribution...')

        # Polarization vectors over mass, shape = (N_at_sc, n_modes)
        pols_mass = np.einsum('a, am -> am', np.sqrt(self.m)**-1, self.pols)

        # The mass rescaled projected effective charges in polarization basis, shape = (N_configs, n_modes)
        z_pols_mass = np.einsum('am, ia -> im ', pols_mass, z_eff.ravel().reshape((self.N, self.nat * 3)))

        # Eigenvalues of Upsilon mass rescaled, shape = (n_modes)
        xi2_inv = f_ups(self.w, self.T)

        # The mass rescaled displacements in polarization basis divided by xi2, shape = (N_configs, n_modes)
        u_xi2 = np.einsum('im, m -> im', self.X, xi2_inv)

        # Add the effective charges, shape = (N_configs, n_modes, n_modes)
        u_xi2_Z = np.einsum('in , im -> inm', u_xi2, z_pols_mass)

        # Get the reweighted average of the second derivative, shape = (n_modes, n_modes)
        d2_M = np.einsum('i, inm -> nm', self.rho, u_xi2_Z) /np.sum(self.rho)
        d2_M = 0.5 * (d2_M + d2_M.T)

        # Get chi_minus and chi_plus tensors
        chi_minus = self.get_chi_minus()
        chi_plus  = self.get_chi_plus()

        # Get the pertubations on a'^(1) b'^(1)
        pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), d2_M)
        pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , d2_M)

        # Check if everything is symmetric
        assert np.all(np.abs(d2_M - d2_M.T) < 1e-10), "Second derivative of the dipole is not symmetric in pol basis"
        assert np.all(np.abs(pert_a - pert_a.T) < 1e-10), "a'(1) pertubation is not symmetric in pol basis"
        assert np.all(np.abs(pert_b - pert_b.T) < 1e-10), "b'(1) pertubation is not symmetric in pol basis"

        # Now get the perturbation for a'^(1)
        current = self.n_modes
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
            current = current + self.n_modes - i

        # Now get the pertrubation for b'^(1)
        for i in range(self.n_modes):
            self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
            current = current + self.n_modes - i

        # Add the mask dot taking into account symmetric elements
        mask_dot = self.mask_dot_wigner()
        # OVERWRITE the pertubation modulus considering the two phonon sector
        self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

        print('[OLD] Perturbation modulus after adding two ph contributions = {}'.format(self.perturbation_modulus))
        print()

    return

prepare_perturbation(vector, masses_exp=1, add=False)

This function prepares the calculation for the Green function

Where |v> is the vector passed as input. If you want to compute the raman, for istance, it can be the vector of the Raman intensities.

The vector can be obtained contracting it with the polarization vectors. The contraction can be on the numerator or on the denumerator, depending on the observable.

NOTE: This function prepares the pertubation ONLY in the R sector Both IR and Raman has masses_exp = -1

.. math ::

v_\mu = \sum_a v_a e_\mu^a \cdot \sqrt{m_a}

v_\mu = \sum_a v_a \frac{e_\mu^a}{  \sqrt{m_a}}
Source code in tdscha/DynamicalLanczos.py
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
def prepare_perturbation(self, vector, masses_exp = 1, add = False):
    r"""
    This function prepares the calculation for the Green function

    <v| G |v>

    Where |v> is the vector passed as input. If you want to compute the
    raman, for istance, it can be the vector of the Raman intensities.

    The vector can be obtained contracting it with the polarization vectors.
    The contraction can be on the numerator or on the denumerator, depending on the
    observable.

    NOTE: This function prepares the pertubation ONLY in the R sector
    Both IR and Raman has masses_exp = -1

    .. math ::

        v_\mu = \sum_a v_a e_\mu^a \cdot \sqrt{m_a} 

        v_\mu = \sum_a v_a \frac{e_\mu^a}{  \sqrt{m_a}} 

    Parameters
    ----------
        vector: ndarray( size = (3*natoms))
            The vector of the perturbation for the computation of the green function
        masses_exp : float
            The vector is multiplied by the square root of the masses raised to masses_exp.
            If you want to multiply each component by the square root of the masses use 1,
            if you want to divide by the quare root use -1, use 0 if you do not want to use the
            masses.
        add : bool
            If true, the perturbation is added on the top of the one already setup.
            Calling add does not cause a reset of the Lanczos
    """
    if not add:
        self.reset()
        self.psi = np.zeros(self.psi.shape, dtype = TYPE_DP)

    # Convert the vector in the polarization space
    m_on = np.sqrt(self.m) ** masses_exp
    print("SHAPE:", m_on.shape, vector.shape, self.pols.shape)
    new_v = np.einsum("a, a, ab->b", m_on, vector, self.pols)
    self.psi[:self.n_modes] += new_v

    # THIS IS OK IN THE WIGNER REPRESENTATION BECAUSE
    # THE PERTUBATION ENTERS ONLY IN THE R SECTOR
    self.perturbation_modulus = new_v.dot(new_v)

    if self.symmetrize:
        self.symmetrize_psi()

prepare_mode(index)

Prepare the perturbation on a single phonon mode. This is usefull to get the single mode contribution to the overall spectral function.

Source code in tdscha/DynamicalLanczos.py
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
def prepare_mode(self, index):
    """
    Prepare the perturbation on a single phonon mode.
    This is usefull to get the single mode contribution to the overall spectral function.

    Parameters
    ----------
        index : int
            The index of the mode in the supercell. Starting from 0 (lowest frequency, excluding acoustic modes at Gamma) 
    """
    self.reset()

    self.psi[:] = 0
    self.psi[index] = 1 
    self.perturbation_modulus = 1

prepare_two_ph(a, b)

Prepare the psi vector for a two phonon response. Available only in Winger.

Parameters:
a, b: int indices of the modes (acustic are excluded).
Source code in tdscha/DynamicalLanczos.py
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
def prepare_two_ph(self, a, b):
    """
    Prepare the psi vector for a two phonon response.
    Available only in Winger.

    Parameters:
    ----------
        a, b: int indices of the modes (acustic are excluded).
    """
    if not self.use_wigner:
        raise NotImplementedError('The two phonon response is available only in Wigner')

    if a > self.n_modes or b > self.n_modes:
        raise ValueError('The a-b indices must be smaller than {}'.format(self.n_modes))

    print()
    print('PREPARE THE TWO PHONON PERTUBATION')
    print('Indices selected a = {} b = {}'.format(a,b))
    print()

    self.reset()
    self.psi[:] = 0

    # Get chi minus and chi plus, shape = (n_modes, n_modes)
    chi_plus  = self.get_chi_plus()
    chi_minus = self.get_chi_minus()

    # The matrix for the second derivatives
    mat_modes = np.zeros((self.n_modes, self.n_modes))

    mat_modes[a,b] = 0.5
    mat_modes[b,a] += 0.5

    # Get the pertbations on a' b'
    pert_a = -np.einsum('nm, nm -> nm', np.sqrt(-0.5 * chi_minus), mat_modes)
    pert_b = +np.einsum('nm, nm -> nm', np.sqrt(+0.5 * chi_plus) , mat_modes)

    # Now get the perturbation for a'^(1)
    current = self.n_modes
    for i in range(self.n_modes):
        self.psi[current : current + self.n_modes - i] = pert_a[i, i:]
        current = current + self.n_modes - i

    # Now get the pertrubation for b'^(1)
    for i in range(self.n_modes):
        self.psi[current : current + self.n_modes - i] = pert_b[i, i:]
        current = current + self.n_modes - i

    # Add the mask dot taking into account symmetric elements
    mask_dot = self.mask_dot_wigner()

    # Update the pertubation modulus
    self.perturbation_modulus = self.psi.dot(self.psi * mask_dot)

    return

get_vector_dyn_from_psi()

This function returns a standard vector and the dynamical matrix in cartesian coordinates

This can be used to symmetrize the vector.

The vector is returned in [Bohr] and the force constant matrix is returned in [Ry/bohr^2]

Source code in tdscha/DynamicalLanczos.py
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
def get_vector_dyn_from_psi(self):
    """
    This function returns a standard vector and the dynamical matrix in cartesian coordinates

    This can be used to symmetrize the vector.

    The vector is returned in [Bohr] and the force constant matrix is returned in [Ry/bohr^2]
    """


    vector = self.psi[:self.n_modes]
    dyn = self.psi[self.n_modes:].reshape((self.n_modes, self.n_modes))

    w_a = np.tile(self.w, (self.n_modes, 1))
    w_b = np.tile(self.w, (self.n_modes, 1)).T 

    dyn *= ( 2*(w_a + w_b) * np.sqrt(w_a*w_b*(w_a + w_b)))

    # Get back the real vectors
    real_v = np.einsum("b, ab->a",  vector, self.pols)  /np.sqrt(self.m)
    real_dyn = np.einsum("ab, ca, db-> cd", dyn, self.pols, self.pols)
    real_dyn *= np.outer(np.sqrt(self.m), np.sqrt(self.m))

    return real_v, real_dyn 

set_psi_from_vector_dyn(vector, dyn)

Set the psi vector from a given vector of positions [bohr] and a force constant matrix [Ry/bohr^2]. Used to reset the psi after the symmetrization.

Source code in tdscha/DynamicalLanczos.py
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
def set_psi_from_vector_dyn(self, vector, dyn):
    """
    Set the psi vector from a given vector of positions [bohr] and a force constant matrix [Ry/bohr^2].
    Used to reset the psi after the symmetrization.
    """

    new_v = np.einsum("a, ab->b",  np.sqrt(self.m) * vector, self.pols)

    new_dyn = dyn / np.outer(np.sqrt(self.m), np.sqrt(self.m))
    new_dyn = np.einsum("ab, ai, bj-> ij", new_dyn, self.pols, self.pols) 

    w_a = np.tile(self.w, (self.n_modes, 1))
    w_b = np.tile(self.w, (self.n_modes, 1)).T 

    new_dyn /= ( 2*(w_a + w_b) * np.sqrt(w_a*w_b*(w_a + w_b)))

    self.psi[:self.n_modes] = new_v
    self.psi[self.n_modes:] = new_dyn.ravel()

symmetrize_psi()

Symmetrize the psi vector.

Source code in tdscha/DynamicalLanczos.py
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
def symmetrize_psi(self):
    """
    Symmetrize the psi vector.
    """

    # First of all, get the vector and the dyn
    vector, dyn = self.get_vector_dyn_from_psi()

    print ("Vector before symmetries:")
    print (vector)

    # Symmetrize the vector
    self.qe_sym.SetupQPoint()
    new_v = np.zeros( (self.nat, 3), dtype = np.float64, order = "F")
    new_v[:,:] = vector.reshape((self.nat, 3))
    self.qe_sym.SymmetrizeVector(new_v)
    vector = new_v.ravel()

    print ("Vector after symmetries:")
    print (vector)

    # Symmetrize the dynamical matrix
    dyn_q = CC.Phonons.GetDynQFromFCSupercell(dyn, np.array(self.dyn.q_tot), self.uci_structure, self.super_structure)
    self.qe_sym.SymmetrizeFCQ(dyn_q, self.dyn.q_stars, asr = "custom")
    dyn = CC.Phonons.GetSupercellFCFromDyn(dyn_q, np.array(self.dyn.q_tot), self.uci_structure, self.super_structure)

    # Push everithing back into the psi
    self.set_psi_from_vector_dyn(vector, dyn)

set_max_frequency(freq)

SETUP THE REVERSE LANCZOS

This function prepares the Lanczos algorithm in order to find the lowest eigenvalues You should provide the maximum frequencies of the standard spectrum. Then the Lanczos is initialized in order to solve the problem

(Ia - L) x = b

where a is sqrt(2*freq) so that should match the maximum energy. Since Lanczos is very good in converging the biggest (in magnitude) eigenvectors, this procedure should accelerate the convergence of the low energy spectrum.

NOTE: This method should be executed BEFORE the Lanczos run.

Source code in tdscha/DynamicalLanczos.py
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
def set_max_frequency(self, freq):
    """
    SETUP THE REVERSE LANCZOS
    =========================

    This function prepares the Lanczos algorithm in order to find the lowest eigenvalues
    You should provide the maximum frequencies of the standard spectrum.
    Then the Lanczos is initialized in order to solve the problem

    (Ia - L) x = b

    where a is sqrt(2*freq) so that should match the maximum energy. 
    Since Lanczos is very good in converging the biggest (in magnitude) eigenvectors, this
    procedure should accelerate the convergence of the low energy spectrum.

    NOTE: This method should be executed BEFORE the Lanczos run.

    Parameters
    ----------
        freq : float
           The frequencies (in Ry) of the maximum eigenvalue.
    """

    self.shift_value = 4*freq*freq
    self.reverse_L = True

    print("Shift value:", self.shift_value)

apply_L1()

APPLY THE L1

This is the first part of the application, it involves only harmonic propagation.

Results
out_vect : ndarray(shape(self.psi))
    It returns the application of the harmonic part of the L matrix
Source code in tdscha/DynamicalLanczos.py
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
def apply_L1(self):
    """
    APPLY THE L1
    ============

    This is the first part of the application, it involves only harmonic propagation.

    Results
    -------
        out_vect : ndarray(shape(self.psi))
            It returns the application of the harmonic part of the L matrix
    """

    out_vect = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

    # Get the harmonic responce function
    out_vect[:self.n_modes] = (self.psi[:self.n_modes] * self.w) * self.w

    #print("freqsL1: ", self.w)
    #print("out 0:", out_vect[0])
    # Get the harmonic responce on the propagator
    w_a = np.tile(self.w, (self.n_modes, 1))
    w_b = np.tile(self.w, (self.n_modes, 1)).T 

    new_out = (w_a + w_b)**2
    out_vect[self.n_modes:] = new_out.ravel() * self.psi[self.n_modes:]

    #print("out 0 (just end):", out_vect[0])
    return out_vect

apply_L1_FT(transpose=False)

APPLY THE L1 AT FINITE TEMPERATURE

This is the first part of the application, it involves only HARMONIC propagation.

NORMAL: this method applies -L_harm: :: math . \begin{bmatrix} -Z'' & 0 & 0 \ 0 & -X & -Y \ 0 & -X' & -Y' \end{bmatrix} on the follwoing vector: :: math . \begin{bmatrix} \mathcal{R}^{(1)} \ \tilde{\Upsilon}^{(1)} \ \Re \tilde{A}^{(1)}. \end{bmatrix}

WIGNER: this method applies +L_harm: :: math . \begin{bmatrix} - \omega^2_\alpha & 0 & 0 \ 0 & -\omega^-{\alpha\beta}^2 & 0 \ 0 & 0 & -\omega^+{\alpha\beta}^2 \end{bmatrix} on the following vector: :: math . \begin{bmatrix} \tilde{\mathcal{R}}^{(1)}\alpha\ \tilde{a}'^{(1)}{\alpha\beta}\ \tilde{b}'^{(1)}_{\alpha\beta}. \end{bmatrix}

If transpose = True it applies the transpose.

Results
-out_vect : ndarray(shape(self.psi))
    It returns the application of the harmonic part of the L matrix
Source code in tdscha/DynamicalLanczos.py
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
    def apply_L1_FT(self, transpose = False):
        r"""
        APPLY THE L1 AT FINITE TEMPERATURE
        ==================================

        This is the first part of the application, it involves only HARMONIC propagation.

        NORMAL: this method applies -L_harm: 
        :: math .
            \begin{bmatrix}
            -Z'' &   0  &  0 \\
            0    &  -X  & -Y \\
            0    &  -X' & -Y'
            \end{bmatrix}
        on the follwoing vector:
        :: math .
            \begin{bmatrix}
            \mathcal{R}^{(1)} \\
            \tilde{\Upsilon}^{(1)} \\
            \Re \tilde{A}^{(1)}.
            \end{bmatrix}

        WIGNER: this method applies +L_harm: 
        :: math .
            \begin{bmatrix}
            - \omega^2_\alpha & 0 & 0 \\
            0 &  -\omega^-_{\alpha\beta}^2  & 0 \\ 
            0 & 0 &  -\omega^+_{\alpha\beta}^2 
            \end{bmatrix}
        on the following vector:
        :: math .
            \begin{bmatrix}
            \tilde{\mathcal{R}}^{(1)}_\alpha\\ 
            \tilde{a}'^{(1)}_{\alpha\beta}\\ 
            \tilde{b}'^{(1)}_{\alpha\beta}.
            \end{bmatrix}

        If transpose = True it applies the transpose.

        Results
        -------
            -out_vect : ndarray(shape(self.psi))
                It returns the application of the harmonic part of the L matrix
        """
        # Prepare the free propagator on the positions
        out_vect = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

        if self.ignore_harmonic:
            return out_vect 

        # The elements where w_a and w_b are exchanged are dependent
        # So we must avoid including them
        i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
        i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

        new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
        new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

        w_a = self.w[new_i_a]
        w_b = self.w[new_i_b]

        N_w2 = len(w_a)

        ##############################################
        # Get the harmonic responce function on R^(1)#
        ##############################################
        # Apply the diagonal free propagation 
        if not self.use_wigner:
#             print('[NORMAL]: harmonic R(1)')
            out_vect[:self.n_modes] = (self.psi[:self.n_modes] * self.w) * self.w
        else:
            # Use Wigner
#             print('[WIGNER]: harmonic R(1)')
            out_vect[:self.n_modes] = -(self.psi[:self.n_modes] * self.w) * self.w

        # Get the BE occupation number
        n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        if self.T > 0:
            n_a = 1 / (np.exp( w_a / np.double(self.T / __RyToK__)) - 1)
            n_b = 1 / (np.exp( w_b / np.double(self.T / __RyToK__)) - 1)


        # Apply the non interacting X operator
        # Where R^(1) ends and Upsilon^(1)-a'^(1) starts
        start_Y = self.n_modes
        # Where Upsilon^(1)-a'^(1) ends and ReA^(1)-b'^(1) starts
        start_A = self.n_modes + N_w2

        #########################################################################
        # The R^(1) perturbation ends at start_Y
        # The Upsilon^(1)/a'^{(1)} perturbation start at start_Y
        # The ReA^(1)/b'^{(1)} perturbation starts at start_Y + 0.5*N_modes*(N_modes + 1)
        #################################################################################

        #print("start_Y: {} | start_A: {} | end_A: {} | len_psi: {}".format(start_Y, start_A, start_A + N_w2, len(self.psi)))

        ERR_MSG ="""
ERROR,
The initial vector for the Lanczos algorithm has a wrong dimension. 
This may be caused by the Lanczos initialized at the wrong temperature.
"""
        assert len(self.psi) == start_A + N_w2, ERR_MSG

        ############################################################
        # Get the harmonic responce function on Upsilon^(1)-a'^(1) #
        ############################################################

        if not self.use_wigner:
#             print('[NORMAL]: harmonic Y(1)')
            # Apply the diagonal free propagation on Y
            X_ab_NI = -w_a**2 - w_b**2 - (2*w_a *w_b) /((2*n_a + 1) * (2*n_b + 1))
            out_vect[start_Y: start_A] = - X_ab_NI * self.psi[start_Y: start_A]

            # Apply the off diagonal free propagation Y-ReA
            Y_ab_NI = - (8 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))
            if not transpose:
                out_vect[start_Y : start_A] += - Y_ab_NI * self.psi[start_A: ]
            else:
                out_vect[start_A:] += - Y_ab_NI * self.psi[start_Y : start_A]

            #L_operator[start_Y : start_A, start_A:] = - np.diag(Y_ab_NI) * extra_count
            #L_operator[start_Y + np.arange(self.n_modes**2), start_A + exchange_frequencies] -=  Y_ab_NI / 2
        else:
#             print('[WIGNER]: harmonic a(1)')
            # Apply the diagonal free propagation in WIGNER on a'^{(1)}
            a_harm = -(w_a**2 + w_b**2 - 2. * w_a * w_b)
            out_vect[start_Y: start_A] = +a_harm * self.psi[start_Y: start_A]


        ########################################################
        # Get the harmonic responce function on ReA^(1)-b'^(1) #
        ########################################################

        if not self.use_wigner:
#             print('[NORMAL]: harmonic ReA(1)')
            # Apply the off diagonal free propagation ReA-Y
            X1_ab_NI = - (2*n_a*n_b + n_a + n_b) * (2*n_a*n_b + n_a + n_b + 1)*(2 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))

            # Apply the off-diagonal free propagation
            if not transpose:
                out_vect[start_A:] += - X1_ab_NI * self.psi[start_Y: start_A]
            else:
                out_vect[start_Y: start_A] += - X1_ab_NI * self.psi[start_A:]
            #L_operator[start_A:, start_Y : start_A] = - np.diag(X1_ab_NI) / 1 * extra_count
            #L_operator[start_A + np.arange(self.n_modes**2), start_Y + exchange_frequencies] -= X1_ab_NI / 2

            # Apply the diagonal free propagation ReA
            Y1_ab_NI = - w_a**2 - w_b**2 + (2*w_a *w_b) /( (2*n_a + 1) * (2*n_b + 1))
            out_vect[start_A:] += - Y1_ab_NI * self.psi[start_A:]
            #L_operator[start_A:, start_A:] = -np.diag(Y1_ab_NI) / 1 * extra_count
            #L_operator[start_A + np.arange(self.n_modes**2),  start_A + exchange_frequencies] -= Y1_ab_NI / 2
        else:
#             print('[WIGNER]: harmonic b(1)')
            # Apply the diagonal free propagation in WIGNER on b'^{(1)}
            b_harm = -(w_a**2 + w_b**2 + 2. * w_a * w_b)
            out_vect[start_A:] = +b_harm * self.psi[start_A:]

        return out_vect

apply_L1_inverse_FT(psi, transpose=False)

APPLY THE INVERSE L1 AT FINITE TEMPERATURE

This method allows for preconditioning, as L1 is a diagonal application

Results
out_vect : ndarray(shape(self.psi))
    It returns the application of the harmonic part of the inverse L matrix
Source code in tdscha/DynamicalLanczos.py
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
    def apply_L1_inverse_FT(self, psi, transpose = False):
        """
        APPLY THE INVERSE L1 AT FINITE TEMPERATURE
        ==========================================

        This method allows for preconditioning, as L1 is a diagonal application

        Results
        -------
            out_vect : ndarray(shape(self.psi))
                It returns the application of the harmonic part of the inverse L matrix
        """


        # Prepare the free propagator on the positions
        out_vect = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

        if self.ignore_harmonic:
            return out_vect 

        # The elements where w_a and w_b are exchanged are dependent
        # So we must avoid including them
        i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
        i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

        new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
        new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

        w_a = self.w[new_i_a]
        w_b = self.w[new_i_b]

        N_w2 = len(w_a)

        # Get the harmonic responce function
        out_vect[:self.n_modes] = (psi[:self.n_modes] / self.w) / self.w


        n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)

        not_populated_mask_a = 0.01 * w_a *__RyToK__ > self.T
        not_populated_mask_b = 0.01 * w_a *__RyToK__ > self.T

        if self.T > 0:
            n_a = 1 / (np.exp( w_a / np.double(self.T /__RyToK__)) - 1)
            n_b = 1 / (np.exp( w_b / np.double(self.T / __RyToK__)) - 1)
            n_a[not_populated_mask_a] = 0
            n_b[not_populated_mask_b] = 0


        # Apply the non interacting X operator
        start_Y = self.n_modes
        start_A = self.n_modes + N_w2


        ERR_MSG ="""
ERROR,
The initial vector for the Lanczos algorithm has a wrong dimension. 
This may be caused by the Lanczos initialized at the wrong temperature.
"""
        assert len(psi) == start_A + N_w2, ERR_MSG

        # Get the free two-phonon propagator
        X_ab_NI = -w_a**2 - w_b**2 - (2*w_a *w_b) /( (2*n_a + 1) * (2*n_b + 1))
        Y_ab_NI = - (8 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))
        X1_ab_NI = - (2*n_a*n_b + n_a + n_b) * (2*n_a*n_b + n_a + n_b + 1)*(2 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))
        Y1_ab_NI = - w_a**2 - w_b**2 + (2*w_a *w_b) /( (2*n_a + 1) * (2*n_b + 1))

        # Invert the propagator
        den = X_ab_NI * Y1_ab_NI - X1_ab_NI * Y_ab_NI

        # Regularize (avoid non invertibility when w_a = w_b and kbT << w)
        den_mask = den < __EPSILON__ 
        den[den_mask] = np.inf

        X_new = -Y1_ab_NI / den
        Y_new = Y_ab_NI / den
        X1_new = X1_ab_NI / den 
        Y1_new = - X_ab_NI / den

        X_new[den_mask] = - 1 / X_ab_NI[den_mask]

        # If T > 0, then also ReA could be inverted (only if w_a and w_b are thermally populated)
        if self.T > __EPSILON__:
            new_mask = (w_a == w_b) & (n_a > __EPSILON__)
            Y1_new[new_mask] = -1 / Y1_ab_NI[new_mask]

        out_vect[start_Y: start_A] = X_new * psi[start_Y: start_A]
        if not transpose:
            out_vect[start_Y : start_A] += Y_new * psi[start_A: ]
        else:
            out_vect[start_A:] += Y_new * psi[start_Y : start_A]

        #L_operator[start_Y : start_A, start_A:] = - np.diag(Y_ab_NI) * extra_count
        #L_operator[start_Y + np.arange(self.n_modes**2), start_A + exchange_frequencies] -=  Y_ab_NI / 2


        if not transpose:
            out_vect[start_A:] += X1_new * psi[start_Y: start_A]
        else:
            out_vect[start_Y: start_A] += X1_new * psi[start_A:]
        #L_operator[start_A:, start_Y : start_A] = - np.diag(X1_ab_NI) / 1 * extra_count
        #L_operator[start_A + np.arange(self.n_modes**2), start_Y + exchange_frequencies] -= X1_ab_NI / 2

        out_vect[start_A:] += Y1_new * psi[start_A:]
        #L_operator[start_A:, start_A:] = -np.diag(Y1_ab_NI) / 1 * extra_count
        #L_operator[start_A + np.arange(self.n_modes**2),  start_A + exchange_frequencies] -= Y1_ab_NI / 2


        return out_vect

apply_L1_static(psi, inverse=False, power=1)

Apply the harmonic part of the L matrix for computing the static case (at any temperature).

The inverse keyword, if True, compute the L^-1. If power is different from one, then multiply it to a specific power.

Source code in tdscha/DynamicalLanczos.py
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
    def apply_L1_static(self, psi, inverse = False, power = 1):
        """
        Apply the harmonic part of the L matrix for computing the static case (at any temperature).

        The inverse keyword, if True, compute the L^-1. If power is different from one, then multiply it to a specific power.

        """

        expected_dim = self.n_modes +  (self.n_modes * (self.n_modes + 1)) // 2
        ERRMSG = """
Error, for the static calculation the vector must be of dimension {}, got {}
""".format(expected_dim, len(psi))

        assert len(psi) == expected_dim, ERRMSG

        out_vect = np.zeros(psi.shape, dtype = TYPE_DP)

        if inverse:
            power *= -1

        # Here we apply the D2
        out_vect[:self.n_modes] = psi[:self.n_modes] * (self.w **(2 * power))
        #out_vect[:self.n_modes] = (psi[:self.n_modes] / self.w) / self.w 


        # Now we apply the inverse of the W matrix
        # The elements where w_a and w_b are exchanged are dependent
        # So we must avoid including them
        i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
        i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

        new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
        new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

        w_a = self.w[new_i_a]
        w_b = self.w[new_i_b]


        n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)

        not_populated_mask_a = 0.01 * w_a *__RyToK__ > self.T
        not_populated_mask_b = 0.01 * w_a *__RyToK__ > self.T

        diff_n_ab = np.zeros( np.shape(w_a), dtype = TYPE_DP)
        w_ab_equal = np.abs(w_a - w_b) < 1e-8
        w_equal = w_a[w_ab_equal]
        n_equal = n_a[w_ab_equal]

        if self.T > 0:
            beta = np.double(__RyToK__ / self.T)
            n_a = 1 / (np.exp( w_a * beta) - 1)
            n_b = 1 / (np.exp( w_b * beta) - 1)
            n_a[not_populated_mask_a] = 0
            n_b[not_populated_mask_b] = 0

            diff_n_ab[:] = (n_a - n_b) / (w_a - w_b)
            diff_n_ab[w_ab_equal] = - beta * np.exp(w_equal * beta) * n_equal**2

        Lambda =  (n_a + n_b + 1) / (w_a + w_b) - diff_n_ab
        Lambda /= 4 * w_a * w_b

        out_vect[self.n_modes:] =  psi[self.n_modes:] / Lambda**power

        return out_vect

get_chi_minus()

Get the chi^- equilibrium tensor in the Wigner formalism.

:: math . \tilde{\chi}^{-}{\mu\nu} = \frac{\hbar\left[\omega\alpha - \omega_\beta\right]\left[n_\alpha - n_\beta\right]}{2\omega_\alpha\omega_\beta}

Results:
-chi_minus: chi minus tensor, np.array with shape = (n_modes, n_modes)
Source code in tdscha/DynamicalLanczos.py
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
def get_chi_minus(self):
    r"""
    Get the chi^- equilibrium tensor in the Wigner formalism.

    :: math .
        \tilde{\chi}^{-}_{\mu\nu} = \frac{\hbar\left[\omega_\alpha - \omega_\beta\right]\left[n_\alpha - n_\beta\right]}{2\omega_\alpha\omega_\beta}

    Results:
    -------
        -chi_minus: chi minus tensor, np.array with shape = (n_modes, n_modes)

    """
    # Prepare the result
    chi_minus = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

    # Create the matrix with freqeuncies
    w = np.tile(self.w, (self.n_modes,1))

    # Create the Bose-Eninstein occupation number matrix
    n = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

    if self.T > __EPSILON__:
        n = 1.0 / (np.exp(w * 157887.32400374097 /self.T) - 1.0)

    chi_minus = (w - w.T) * (n - n.T) /(2. * w * w.T)

    return chi_minus

get_chi_plus()

Get the chi^+ equilibrium tensor in the Wigner formalism.

:: math . \tilde{\chi}^{+}{\mu\nu} = \frac{\hbar\left[\omega\alpha + \omega_\beta\right]\left[1 + n_\alpha + n_\beta\right]}{2\omega_\alpha\omega_\beta}

Results:
-chi_plus: chi plus tensor, np.array with shape = (n_modes, n_modes)
Source code in tdscha/DynamicalLanczos.py
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
def get_chi_plus(self):
    r"""
    Get the chi^+ equilibrium tensor in the Wigner formalism.

    :: math .
        \tilde{\chi}^{+}_{\mu\nu} = \frac{\hbar\left[\omega_\alpha + \omega_\beta\right]\left[1 + n_\alpha + n_\beta\right]}{2\omega_\alpha\omega_\beta}

    Results:
    -------
        -chi_plus: chi plus tensor, np.array with shape = (n_modes, n_modes)

    """
    # Prepare the result
    chi_plus = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

    # Create the matrix with freqeuncies
    w = np.tile(self.w, (self.n_modes,1))

    # Create the Bose-Eninstein occupation number matrix
    n = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

    if self.T > __EPSILON__:
        n = 1.0 / (np.exp(w * 157887.32400374097 /self.T) - 1.0)

    chi_plus = (w + w.T) * (1 + n + n.T) /(2. * w * w.T)

    return chi_plus

get_a1_b1_wigner(get_a1=True)

Get the the pertrubation on the a' matrix times sqrt(-0.5X^-) or on b' matrix from the alpha and beta pertrubation.

This function is to check the inverse of the change of variables.

:: math . \sqrt{-\frac{1}{2}\tilde{\chi}^-{\mu\nu}} \tilde{a}'^{(1)}{\mu\nu} = X_{\mu\nu} \cdot \left[\frac{+1}{2}\left(\frac{\hbar^2}{\omega_{\mu} \omega_{\nu}}\tilde{\alpha}^{(1)}{\mu\nu} + \tilde{\beta}^{(1)}{\mu\nu}\right)\right];\

:: math . \tilde{b}'^{(1)}{\mu\nu} = \frac{X{\mu\nu}}{\sqrt{\frac{1}{2}\tilde{\chi}^+{\mu\nu}}} \left[\frac{-1}{2} \left(\frac{\hbar^2}{\omega{\alpha} \omega_{\beta}}\tilde{ \alpha}{\mu\nu} - \tilde{ \beta}{\mu\nu}\right)\right].;\

Parameters:
-get_a1: bool. If true we return the a' perturbation rescaled or the b' pertrubation
Retruns:
-a1: np.array with shape = n_modes * (n_modes + 1)/2
Source code in tdscha/DynamicalLanczos.py
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
def get_a1_b1_wigner(self, get_a1 = True):
    r"""
    Get the the pertrubation on the a' matrix times sqrt(-0.5X^-) or on b' matrix
    from the alpha and beta pertrubation.

    This function is to check the inverse of the change of variables.

    :: math .
        \sqrt{-\frac{1}{2}\tilde{\chi}^-_{\mu\nu}} \tilde{a}'^{(1)}_{\mu\nu} = X_{\mu\nu} \cdot
        \left[\frac{+1}{2}\left(\frac{\hbar^2}{\omega_{\mu} \omega_{\nu}}\tilde{\alpha}^{(1)}_{\mu\nu} 
        + \tilde{\beta}^{(1)}_{\mu\nu}\right)\right];\\

    :: math .
       \tilde{b}'^{(1)}_{\mu\nu} = \frac{X_{\mu\nu}}{\sqrt{\frac{1}{2}\tilde{\chi}^+_{\mu\nu}}} 
       \left[\frac{-1}{2}
       \left(\frac{\hbar^2}{\omega_{\alpha} \omega_{\beta}}\tilde{ \alpha}_{\mu\nu} 
        - \tilde{ \beta}_{\mu\nu}\right)\right].;\\

    Parameters:
    -----------
        -get_a1: bool. If true we return the a' perturbation rescaled or the b' pertrubation

    Retruns:
    -------
        -a1: np.array with shape = n_modes * (n_modes + 1)/2
    """ 
    len_ = (self.n_modes * (self.n_modes + 1)) // 2

    # Get the beta1-alpha1 pertrubation as matrices, np.shape = (n_modes, n_modes)
    alpha_mat_1 = self.get_alpha1_beta1_wigner(get_alpha = True)
    beta_mat_1  = self.get_alpha1_beta1_wigner(get_alpha = False)

    # Now get alpha1 and beta1 as vectors
    alpha1 = np.zeros(len_, dtype = np.double)
    beta1  = np.zeros(len_, dtype = np.double)

    start = 0
    next = self.n_modes
    for i in range(self.n_modes):
        # Get the sum of the rescaled tensor
        alpha1[start : next] = alpha_mat_1[i, i:]
        beta1[start : next]  = beta_mat_1[i, i:]
        start = next 
        next = start + self.n_modes - i - 1 

    # Get the independent indeces
    # Avoid the exchange of w_a w_b
    i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
    i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

    new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
    new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

    # Get the independent indices
    w_a = self.w[new_i_a]
    w_b = self.w[new_i_b]

    n_a = np.zeros(np.shape(len(w_a)), dtype = TYPE_DP)
    n_b = np.zeros(np.shape(len(w_a)), dtype = TYPE_DP)

    if self.T > 0:
        n_a = 1 / (np.exp( w_a / np.double(self.T / __RyToK__)) - 1)
        n_b = 1 / (np.exp( w_b / np.double(self.T / __RyToK__)) - 1)

    # Get all the quantities to make the change of variables
    X = ((1 + 2 * n_a) * (1 + 2 * n_b) /8)
    w_a_b = (w_a * w_b)
    chi_minus = ((w_a - w_b) * (n_a - n_b)) /(2 * w_a * w_b)
    chi_plus  = ((w_a + w_b) * (1 + n_a + n_b)) /(2 * w_a * w_b)

    if get_a1:
        a1 = np.zeros(len_, dtype = np.double)
        a1 = 0.5 * X * (alpha1 / w_a_b + beta1)

        return a1
    else:
        b1 = -0.5 * X * (alpha1 /w_a_b - beta1)
        b1 /= np.sqrt(0.5 * chi_plus)

        return b1

get_alpha1_beta1_wigner(get_alpha=True)

Get the perturbation on the alpha/Upsilon or beta matrix from the psi vector in the Wigner formalism.

Recall that alpha and beta are the starting free parameters of the Gaussian Wigner distribution.

N.B.: This is the function that compute Upsilon^(1) matrix!

:: math . \tilde{\Upsilon}^{(1)}{\mu\nu} = \tilde{\alpha}^{(1)}{\mu\nu} = \frac{\omega_\mu \omega_\nu}{\hbar^2 X_{\mu\nu}} \left( +\sqrt{-\frac{1}{2}\tilde{\chi}^-{\mu\nu}} a'^{(1)}{\mu\nu} -\sqrt{+\frac{1}{2}\tilde{\chi}^+{\mu\nu}} b'^{(1)}{\mu\nu} \right)

:: math . \tilde{\beta}^{(1)}{\mu\nu} = \frac{1}{X{\mu\nu}} \left( \sqrt{-\frac{1}{2}\tilde{\chi}^-{\mu\nu}} a'^{(1)}{\mu\nu} + \sqrt{\frac{1}{2}\tilde{\chi}^+{\mu\nu}} b'^{(1)}{\mu\nu}\right)

Parameters:
-get_alpha: bool, if True alpha1 is returned. If False beta1 is returned.
Returns:
-alpha1 or beta1: array with shape = (n_modes, n_modes)
Source code in tdscha/DynamicalLanczos.py
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
def get_alpha1_beta1_wigner(self, get_alpha = True):
    r"""
    Get the perturbation on the alpha/Upsilon or beta matrix from the psi vector in the Wigner formalism.

    Recall that alpha and beta are the starting free parameters of the Gaussian Wigner distribution.

    N.B.: This is the function that compute Upsilon^(1) matrix!

    :: math .
        \tilde{\Upsilon}^{(1)}_{\mu\nu} = \tilde{\alpha}^{(1)}_{\mu\nu} =
        \frac{\omega_\mu \omega_\nu}{\hbar^2 X_{\mu\nu}}
        \left(
        +\sqrt{-\frac{1}{2}\tilde{\chi}^-_{\mu\nu}} a'^{(1)}_{\mu\nu} 
        -\sqrt{+\frac{1}{2}\tilde{\chi}^+_{\mu\nu}} b'^{(1)}_{\mu\nu}
        \right) 

    :: math .
        \tilde{\beta}^{(1)}_{\mu\nu} = 
        \frac{1}{X_{\mu\nu}}
        \left(
         \sqrt{-\frac{1}{2}\tilde{\chi}^-_{\mu\nu}} a'^{(1)}_{\mu\nu} 
        + \sqrt{\frac{1}{2}\tilde{\chi}^+_{\mu\nu}} b'^{(1)}_{\mu\nu}\right) 

    Parameters:
    -----------
        -get_alpha: bool, if True alpha1 is returned. If False beta1 is returned.

    Returns:
    --------
        -alpha1 or beta1: array with shape = (n_modes, n_modes)
    """
    # Where the arrays start
    start_a = self.n_modes
    start_b = self.n_modes +  (self.n_modes * (self.n_modes + 1)) // 2

    # GET THE PERTURBED PARAMETERS a'^(1) and b'^(1)
    a_all = self.psi[start_a : start_b]
    b_all = self.psi[start_b :]

    # Get the independent indeces
    # Avoid the exchange of w_a w_b
    i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
    i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

    # Get the independent indeces
    new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
    new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

    # Get the independent freqeuncies
    w_a = self.w[new_i_a]
    w_b = self.w[new_i_b]

    n_a = np.zeros(np.shape(len(w_a)), dtype = TYPE_DP)
    n_b = np.zeros(np.shape(len(w_a)), dtype = TYPE_DP)

    if self.T > 0:
        n_a = 1 / (np.exp( w_a / np.double(self.T / __RyToK__)) - 1)
        n_b = 1 / (np.exp( w_b / np.double(self.T / __RyToK__)) - 1)

    # Get all the quantities to make the change of variables
    X         = ((1 + 2 * n_a) * (1 + 2 * n_b) /8)
    w2_on_X   = (w_a * w_b) / X
    chi_minus = ((w_a - w_b) * (n_a - n_b)) /(2 * w_a * w_b)
    chi_plus  = ((w_a + w_b) * (1 + n_a + n_b)) /(2 * w_a * w_b)

    if get_alpha:
        # Now rescale a'^(1) 
        new_a =  w2_on_X * np.sqrt(- 0.5 * chi_minus) * a_all
        # Now rescale b'^(1)
        new_b =  w2_on_X * np.sqrt(+ 0.5 * chi_plus)  * b_all

        # Prepare the result
        # This is Y(1)
        alpha1 = np.zeros((self.n_modes, self.n_modes), dtype = np.double)

        # Start filling
        start = 0
        next = self.n_modes
        for i in range(self.n_modes):
            # Get the difference of the rescaled tensor
            alpha1[i, i:] = new_a[start : next] - new_b[start : next]
            start = next 
            next = start + self.n_modes - i - 1 

            # Fill symmetric
            alpha1[i, :i] = alpha1[:i, i]

        return alpha1      
    else:
        # Now rescale a'^(1) 
        new_a =  (np.sqrt(- 0.5 * chi_minus) /X) * a_all
        # Now rescale b'^(1)
        new_b =  (np.sqrt(+ 0.5 * chi_plus)  /X) * b_all

        # Prepare the result
        beta1 = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)

        # Start filling
        start = 0
        next = self.n_modes
        for i in range(self.n_modes):
            # Get the sum of the rescaled tensor
            beta1[i, i:] = new_a[start : next] + new_b[start : next]
            start = next 
            next = start + self.n_modes - i - 1 

            # Fill symmetric
            beta1[i, :i] = beta1[:i, i]

        return beta1

get_Y1(half_off_diagonal=False)

Get the perturbation on the Y matrix from the psi vector. This is used in the standard code.

Source code in tdscha/DynamicalLanczos.py
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
def get_Y1(self, half_off_diagonal = False):
    """
    Get the perturbation on the Y matrix from the psi vector.
    This is used in the standard code.
    """
    start_Y = self.n_modes
    start_A = self.n_modes +  (self.n_modes * (self.n_modes + 1)) // 2

    Y_all = self.psi[start_Y : start_A]

    Y1 = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)

    start = 0
    next = self.n_modes
    for i in range(self.n_modes):
        Y1[i, i:] = Y_all[start : next]
        start = next 
        next = start + self.n_modes - i - 1 

        # Fill symmetric
        Y1[i, :i] = Y1[:i, i]

    # Normalize each term outside the diagonal
    if half_off_diagonal:
        norm_mask = np.ones((self.n_modes, self.n_modes), dtype = np.double) / 2
        np.fill_diagonal(norm_mask, 1)

        Y1 *= norm_mask


    return  Y1

get_ReA1(half_off_diagonal=False)

Get the perturbation on the ReA matrix from the psi vector.

If half_the_off_diagonal is true, divide by 2 the off-diagonal elements

Source code in tdscha/DynamicalLanczos.py
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
def get_ReA1(self, half_off_diagonal = False):
    """
    Get the perturbation on the ReA matrix from the psi vector.

    If half_the_off_diagonal is true, divide by 2 the off-diagonal elements
    """
    start_A = self.n_modes +  (self.n_modes * (self.n_modes + 1)) // 2

    ReA_all = self.psi[start_A:]

    ReA1 = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)
    start = 0
    next = self.n_modes
    for i in range(self.n_modes):
        ReA1[i, i:] = ReA_all[start : next]
        start = next 
        next = start + self.n_modes - i - 1 

        # Fill symmetric
        ReA1[i, :i] = ReA1[:i, i]

    # Normalize each term outside the diagonal
    if half_off_diagonal:
        norm_mask = np.ones((self.n_modes, self.n_modes), dtype = np.double) / 2
        np.fill_diagonal(norm_mask, 1)

        ReA1 *= norm_mask

    return  ReA1

apply_anharmonic_FT(transpose=False, test_weights=True, use_old_version=False)

APPLY ANHARMONIC EVOLUTION

This term involves the anharmonic evolution: This calculates self-consistently the anharmonic evolution from the vector.

NORMAL: this method applies -L_anharm (see Eq. (K4) in the Appendix of the Monacelli PRB): :: math . \begin{bmatrix} 0 & -X'' & 0 \ -Z & -X & 0 \ -Z' & -X' & 0 \end{bmatrix} on the follwoing vector: :: math . \begin{bmatrix} \mathcal{R}^{(1)} \ \tilde{\Upsilon}^{(1)} \ \Re \tilde{A}^{(1)}. \end{bmatrix}

WIGNER: this method applies +L_anh: :: math. \mathcal{L}{anh} \begin{bmatrix} \tilde{\mathcal{R}}^{(1)}\mu\ \ \tilde{a}'^{(1)}{\mu\nu}\ \ \tilde{b}'^{(1)}{\mu\nu} \end{bmatrix} = \begin{bmatrix} -\left\langle \frac{\partial \mathbb{V}}{\partial \tilde{Q}\alpha}\right\rangle{(1)}\ \ +\sqrt{-\frac{1}{2}\tilde{\chi}{\alpha\beta}^{-}} \left\langle \frac{\partial^2 \mathbb{V}}{\partial \tilde{Q}\alpha \partial \tilde{Q}\beta}\right\rangle{(1)}\ \ -\sqrt{\frac{1}{2}\tilde{\chi}{\alpha\beta}^{+}} \left\langle \frac{\partial^2 \mathbb{V}}{\partial \tilde{Q}\alpha \partial \tilde{Q}\beta}\right\rangle{(1)} \end{bmatrix}

Results:
-final_psi: np.array with shape = n_modes + n_modes * (n_modes + 1)
Source code in tdscha/DynamicalLanczos.py
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
    def apply_anharmonic_FT(self, transpose = False, test_weights = True, use_old_version = False):
        r"""
        APPLY ANHARMONIC EVOLUTION
        ==========================

        This term involves the anharmonic evolution:
        This calculates self-consistently the anharmonic evolution from the vector.

        NORMAL: this method applies -L_anharm (see Eq. (K4) in the Appendix of the Monacelli PRB): 
        :: math .
            \begin{bmatrix}
             0   &  -X''  &  0 \\
            -Z   &  -X    & 0 \\
            -Z'  &  -X'   & 0
            \end{bmatrix}
        on the follwoing vector:
        :: math .
            \begin{bmatrix}
            \mathcal{R}^{(1)} \\
            \tilde{\Upsilon}^{(1)} \\
            \Re \tilde{A}^{(1)}.
            \end{bmatrix}

        WIGNER: this method applies +L_anh:
        :: math.
            \mathcal{L}_{anh} 
            \begin{bmatrix}
            \tilde{\mathcal{R}}^{(1)}_\mu\\ \\
            \tilde{a}'^{(1)}_{\mu\nu}\\ \\
            \tilde{b}'^{(1)}_{\mu\nu}
            \end{bmatrix} 
            = 
            \begin{bmatrix}
            -\left\langle \frac{\partial \mathbb{V}}{\partial \tilde{Q}_\alpha}\right\rangle_{(1)}\\ \\
            +\sqrt{-\frac{1}{2}\tilde{\chi}_{\alpha\beta}^{-}}
            \left\langle \frac{\partial^2 \mathbb{V}}{\partial \tilde{Q}_\alpha \partial \tilde{Q}_\beta}\right\rangle_{(1)}\\ \\
            -\sqrt{\frac{1}{2}\tilde{\chi}_{\alpha\beta}^{+}}
            \left\langle \frac{\partial^2 \mathbb{V}}{\partial \tilde{Q}_\alpha \partial \tilde{Q}_\beta}\right\rangle_{(1)}
            \end{bmatrix}

        Parameters
        ----------
            -transpose : bool
                If True, the transpose of L is computed.
            -test_weights : bool
                If True, the weights are tested against those computed with finite differences
                It is time consuming, activate only for debugging
            -use_old_version: bool
                If true, it employes an old version of the subroutine that does not satisfy the permutation symmetry.
                Use this option only for testing purpouses.

        Results:
        -------
            -final_psi: np.array with shape = n_modes + n_modes * (n_modes + 1)
        """
        #print("Starting with psi:", self.psi)

        # Get the perturbation R^(1)
        R1 = self.psi[: self.n_modes]

        if not self.use_wigner:
#             print('[NORMAL]: get Y(1)')
            # Get the perturbation Upsilon^(1)
            Y1 = self.get_Y1(half_off_diagonal = transpose)
        else:
#             print('[WIGNER]: get Y(1)')
            # Use the Wigner equations
            # Upsilon^(1) = alpha^(1)
            Y1 = self.get_alpha1_beta1_wigner(get_alpha = True)

        # Weights to perform the pertrubed average
        weights = np.zeros(self.N, dtype = np.double)

        if not self.use_wigner:
#             print('[NORMAL]: get static egivals')
            # The standard code
            # Create the multiplicative matrices for the rest of the anharmonicity
            n_mu = 0
            if self.T > __EPSILON__:
                n_mu = 1.0 /(np.exp(self.w * 157887.32400374097 / self.T) - 1.0)
            # Eigenvalues of Upsilon and ReA at equilibrium
            Y_w   = 2 * self.w / (2 * n_mu + 1)
            ReA_w = 2 * self.w * n_mu * (n_mu + 1) / (2*n_mu + 1)

            # Check if we must compute the transpose
            if transpose:
                ReA1 = self.get_ReA1(half_off_diagonal = transpose)

                # The equation is
                # Y^(1)_new = 2 Ya Yb^2 Y^(1) + 2 Yb Ya^2 Y^(1)
                coeff_Y = np.einsum("a, b, b -> ab", Y_w, Y_w, Y_w)
                coeff_Y += np.einsum("a, a, b -> ab", Y_w, Y_w, Y_w)
                coeff_Y *= 2

                coeff_RA = np.einsum("a, b, b -> ab", Y_w, ReA_w, Y_w)
                coeff_RA += np.einsum("a, a, b -> ab", Y_w, ReA_w, Y_w)
                coeff_RA *= 2

                # Get the new perturbation
                Y1_new = Y1 * coeff_Y + ReA1 * coeff_RA

                # Override the old perturbation
                Y1 = Y1_new    
        else:
#             print('[WIGNER]: get static egivals')
            # We are using Wigner equations
            # Get the chi +/- array (n_modes, n_modes)
            chi_minus = self.get_chi_minus()
            chi_plus  = self.get_chi_plus()

#             print('chi_minus = ')
#             print(chi_minus)
#             print('chi plus = ')
#             print(chi_plus)


        #print("X:", self.X)
        #print("w:", self.w)
        #print ("R1:", R1)
        #print("Y1:", Y1)
        #print("T:", self.T)

        # Compute the perturbed average of BO potential in the polarization basis
        f_pert_av   = np.zeros(self.n_modes, dtype = np.double)
        d2v_pert_av = np.zeros((self.n_modes, self.n_modes), dtype = np.double, order = "C")

        # Check if you need to compute the fourth order
        apply_d4 = 1
        if self.ignore_v4:
            print('Removing D4')
            apply_d4 = 0

        # NEW: we can remove the D3 effect
        if self.ignore_v3:
            print('Removing D3')
            R1[:] = 0.

        # Prepare the symmetry variables for the C code
        # deg_space_new = np.zeros(np.sum(self.N_degeneracy), dtype = np.intc)
        # i = 0
        # i_mode = 0
        # j_mode = 0
        # #print("Mapping degeneracies:", np.sum(n_degeneracies))
        # while i_mode < self.n_modes:
        #     #print("cross_modes: ({}, {}) | deg_i = {}".format(i_mode, j_mode, n_degeneracies[i_mode]))
        #     deg_space_new[i] = self.degenerate_space[i_mode][j_mode]
        #     j_mode += 1
        #     i += 1
        #     if j_mode == self.N_degeneracy[i_mode]:
        #         i_mode += 1
        #         j_mode = 0


        # Compute the perturbed averages (the time consuming part is HERE !!!)
        #print("Entering in get pert...")
        _t_pert_start = time.time()
        n_syms, _, _ = np.shape(self.symmetries[0])
        #print("DEG:")
        #print(self.degenerate_space)

        # OLD Implementation still working
        if self.mode in (MODE_FAST_MPI, MODE_FAST_SERIAL): 
            # Get the pertrubed averages
            sscha_HP_odd.GetPerturbAverageSym(self.X, self.Y, self.w, self.rho, R1, Y1, self.T, apply_d4, n_syms,
                                              self.symmetries, self.N_degeneracy, self.degenerate_space ,self.sym_block_id, 
                                              f_pert_av, d2v_pert_av)

        elif self.mode == MODE_FAST_JULIA:
            if not __JULIA_EXT__:
                raise ImportError("Error while importing julia. Try with python-jl after pip install julia.")

            if self.sym_julia is None:
                MSG = "Error, the initialization must be called AFTER you change mode to JULIA."
                raise ValueError(MSG)


            # Prepare the combined parallelization function
            # Pack both results (f vector + d2v matrix) into a single flat array
            # to use GoParallel with "+" reduction (avoids GoParallelTuple bug)
            n_modes = self.n_modes
            def get_combined_proc(start_end):
                start = int(start_end[0])
                end   = int(start_end[1])
                result = julia.Main.get_perturb_averages_sym(
                    self.X.T, self.Y.T, self.w, self.rho, R1, Y1,
                    np.float64(self.T), bool(apply_d4),
                    self.sym_julia, self.N_degeneracy, self.deg_julia,
                    self.sym_block_id, start, end)
                return np.concatenate([result[0], result[1].ravel()])

            # Divide the configurations and symmetries on different processors (Here we get the range of work for each process)
            n_total = self.n_syms * self.N
            n_processors = Parallel.GetNProc()
            count = n_total // n_processors
            remainer = n_total % n_processors

            # Assign which configurations should be computed by each processor
            indices = []
            for rank in range(n_processors):

                if rank < remainer:
                    start = np.int64(rank * (count + 1))
                    stop = np.int64(start + count + 1)
                else:
                    start = np.int64(rank * count + remainer)
                    stop = np.int64(start + count)

                indices.append([start + 1, stop])


            # Execute the combined call on each processor in parallel
            combined = Parallel.GoParallel(get_combined_proc, indices, "+")
            f_pert_av = combined[:n_modes]
            d2v_pert_av = combined[n_modes:].reshape(n_modes, n_modes)

        else:
            raise ValueError("Error, mode running {} not implemented.".format(self.mode))

        _t_pert_end = time.time()
        if self.verbose:
            print("Time for perturb averages (n_syms={}): {:.6f} s".format(n_syms, _t_pert_end - _t_pert_start))

        # Gamma-only: impose translational symmetry on the perturbed averages
        if self.gamma_only and self.trans_projector is not None:
            _t_trans_start = time.time()
            # Project f_pert_av: P_trans @ f
            f_pert_av = self.trans_projector @ f_pert_av

            # Project d2v_pert_av: (1/n_cells) Σ_R T_R @ d2v @ T_R^T
            n_cells = len(self.trans_operators)
            d2v_proj = np.zeros_like(d2v_pert_av)
            for T_R in self.trans_operators:
                d2v_proj += T_R @ d2v_pert_av @ T_R.T
            d2v_pert_av = d2v_proj / n_cells
            _t_trans_end = time.time()
            if self.verbose:
                print("Time for translational projection: {:.6f} s".format(_t_trans_end - _t_trans_start))

        # OLD PART OF THE CODE
        #print("D2V:")
        #np.set_printoptions(threshold = 10000)
        #print(d2v_pert_av[:10, :10])#print("Out get pert")

#         print("R1 = {}".format(R1))
#         print("Y1 = {}".format(Y1))
#         print("<f> pert = {}".format(f_pert_av))
#         print("<d2v/dr^2> pert = {}".format(d2v_pert_av))
#         print("<d2v/dr^2>T pert = {}".format(d2v_pert_av.T))
#         print("<d2v/dr^2>T - <d2v/dr^2>  pert = {}".format(d2v_pert_av.T - d2v_pert_av))
#         print()

        # Compute the average with the OLD VERSION
        if use_old_version:
            # Get the weights of the perturbation (psi vector)
            sscha_HP_odd.GetWeights(self.X, self.w, R1, Y1, self.T, weights)

            if test_weights:
                other_weights = get_weights_finite_differences(self.X, self.w, self.T, R1, Y1)

                # There is a constant factor that should come from the normalization
                # But this does not depend on the configuration
                shift = weights - other_weights 

                # Remove the constant shift coming from renormalization
                # 1/2 Tr (Y^-1 * Y^{(1)})
                shift -= np.mean(shift) 

                # Compare the weights
                disp = np.max(np.abs(shift))
                dispersion = np.std(weights)

                if disp / dispersion >= 1e-3:
                    print("Perturbation:")
                    print(self.psi)

                    print("Weights with C:")
                    print(weights)

                    print()
                    print("Weights by finite differences:")
                    print(other_weights)

                    print()
                    print("Shifts:")
                    print(weights - other_weights)

                    print()
                    print("Discrepancies (max = {}):".format(disp))
                    i_value = np.argmax(np.abs(shift))
                    print(shift)
                    print("I value of max: {}".format(i_value))

                    print("")
                    print("sigma = {}".format(dispersion))
                    print("Weights[{}] = {}".format(i_value, weights[i_value]))
                    print("OtherWeights[{}] = {}".format(i_value, other_weights[i_value]))


                assert disp / dispersion < 1e-3, "Error, the weights computed with the C did not pass the test"

            #print("Weights:", weights)

            # Get the averages on the perturbed ensemble
            w_is = np.tile(self.rho, (self.n_modes, 1)).T
            w_1 = np.tile(weights, (self.n_modes, 1)).T

            #print("rho shape:", np.shape(self.rho))
            #print("Shape w_is:", np.shape(w_is))

            # The force average
            avg_numbers = self.Y * w_is *  w_1 #np.einsum("ia, i, i -> ia", self.Y, w_is, w_1)
            #print("Shape Y:", np.shape(avg_numbers))
            f_pert_av = np.sum(avg_numbers, axis = 0) / self.N_eff


            #print("Shape F:", np.shape(f_pert_av))
            sscha_HP_odd.Get_D2DR2_PertV(self.X, self.Y, self.w, self.rho, weights, self.T, d2v_pert_av)


            #print("<f> pert = {}".format(f_pert_av))
            #print("<d2v/dr^2> pert = {}".format(d2v_pert_av))
            #print()

        # END OF THE OLD VERSION


        # Get the final vector
        final_psi = np.zeros(self.psi.shape, dtype = np.double)

        # Now get the perturbation for R^(1) (same in Wigner)
        final_psi[:self.n_modes] =  f_pert_av

        if not self.use_wigner:
#             print('[NORMAL]: anharmonic Y(1) ReA(1)')
            # Propagation for Upsilon^(1) and ReA^(1)
            if not transpose:
                # Get the perturbation D2 * Upsilon + Upsilon * D2
                pert_Y  = np.einsum("ab, a ->ab", d2v_pert_av, Y_w)
                pert_Y += np.einsum("ab, b ->ab", d2v_pert_av, Y_w)

                # Get the perturbation D2 * Re A +  Re A * D2
                pert_RA  = np.einsum("ab, a ->ab", d2v_pert_av, ReA_w)
                pert_RA += np.einsum("ab, b -> ab", d2v_pert_av, ReA_w)
            else:
                Y_inv = 1 / Y_w
                pert_Y = 0.5 * np.einsum("a, ab, b -> ab", Y_inv, d2v_pert_av, Y_inv)
                pert_RA = np.zeros(pert_Y.shape, dtype = np.double)

                # Now double the off diagonal values of pert_Y and pert_RA
                # This is to take into account the symmetric storage of psi
                sym_mask = np.ones(pert_Y.shape) * 2 
                np.fill_diagonal(sym_mask, 1) 
                pert_Y  *= sym_mask 
                pert_RA *= sym_mask
        else:
#             print("[WIGNER]: anharmonic a(1)' b(1)'")
            # We are using Wigner equations
            # Propagation for a'^(1)
            pert_Y  = np.einsum('ab, ab -> ab', +np.sqrt(-0.5 * chi_minus), d2v_pert_av)
            # Propagation for b'^(1)
            pert_RA = np.einsum('ab, ab -> ab', -np.sqrt(+0.5 * chi_plus),  d2v_pert_av)


#         print('pert_R = ')
#         print(f_pert_av)
#         print('pert_RA = ')
#         print(pert_RA)

        #####################
        # Update the vector #
        #####################

        # Note: the code deals with symmetric matrices in the following way.
        # Given a matrix you take the row on the right from the (1,1) element,
        # then you take the row on the right from the (2,2) element,
        # then you proceed with the (3,3) element.

        # Now get the perturbation for Upsilon^(1)/a'^(1)
        current = self.n_modes
        for i in range(self.n_modes):
            final_psi[current : current + self.n_modes - i] = pert_Y[i, i:]
            current = current + self.n_modes - i

#         print('final psi = ')
#         print(final_psi)

        # Now process the pertrubation of ReA^(1)/b'^(1)
        for i in range(self.n_modes):
            final_psi[current : current + self.n_modes - i] = pert_RA[i, i:]
            current = current + self.n_modes - i

#         print('final psi = ')
#         print(final_psi)

        # print("First element of pert_Y:", pert_Y[0,0])
        # print("Y_w = ", Y_w)
        # print("All pert Y:")
        # print(pert_Y)

        # print("Final psi:")
        # print(final_psi[self.n_modes: self.n_modes + 10])

        #print("Output:", final_psi)
        if not self.use_wigner:
#             print('[NORMAL]: anharmonic final')
            return -final_psi
        else:
#             print('[WIGNER]: anharmonic final')
            return +final_psi

apply_anharmonic_static()

Compute the anharmonic part of the L matrix for the static Hessian calculation.

Source code in tdscha/DynamicalLanczos.py
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
def apply_anharmonic_static(self):
    """
    Compute the anharmonic part of the L matrix for the static Hessian calculation.
    """ 

    Y1 = self.get_Y1()
    R1 = self.psi[: self.n_modes]


    # Create the Y matrix
    n_mu = 0
    if self.T > __EPSILON__:
        n_mu = 1.0 / ( np.exp(self.w * np.double(157887.32400374097) / self.T) - 1.0)
    Y_w = 2 * self.w / (2 * n_mu + 1)

    # prepare the modified of Y1
    Y1 = -2 * np.einsum("ab, a, b -> ab", Y1, Y_w, Y_w)

    # Compute the average SSCHA force and potential
    f_pert_av = np.zeros(self.n_modes, dtype = np.double)
    d2v_pert_av = np.zeros((self.n_modes, self.n_modes), dtype = np.double, order = "C")

    # Check if you need to compute the fourth order
    apply_d4 = 1
    if self.ignore_v4:
        apply_d4 = 0

    # Prepare the symmetry variables for the C code
    deg_space_new = np.zeros(np.sum(self.N_degeneracy), dtype = np.intc)
    i = 0
    i_mode = 0
    j_mode = 0
    #print("Mapping degeneracies:", np.sum(n_degeneracies))
    while i_mode < self.n_modes:
        #print("cross_modes: ({}, {}) | deg_i = {}".format(i_mode, j_mode, n_degeneracies[i_mode]))
        deg_space_new[i] = self.degenerate_space[i_mode][j_mode]
        j_mode += 1
        i += 1
        if j_mode == self.N_degeneracy[i_mode]:
            i_mode += 1
            j_mode = 0


    # Compute the perturbed averages (the time consuming part is HERE)
    #print("Entering in get pert...")
    sscha_HP_odd.GetPerturbAverageSym(self.X, self.Y, self.w, self.rho, R1, Y1, self.T, apply_d4, 
                                      self.symmetries, self.N_degeneracy, deg_space_new, 
                                      f_pert_av, d2v_pert_av)

    # Get the final vector
    final_psi = np.zeros(self.psi.shape, dtype = np.double)
    final_psi[:self.n_modes] =  - f_pert_av

    # Now get the perturbation on the vector
    current = self.n_modes
    for i in range(self.n_modes):
        final_psi[current : current + self.n_modes - i] = d2v_pert_av[i, i:]
        current = current + self.n_modes - i


    return final_psi

apply_L2()

APPLY THE L2

L2 is the part of the L operators that mixes the two spaces. It involves the phi3 matrix.

Source code in tdscha/DynamicalLanczos.py
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
def apply_L2(self):
    """
    APPLY THE L2
    ============

    L2 is the part of the L operators that mixes the two spaces.
    It involves the phi3 matrix.
    """

    if self.ignore_v3:
        return np.zeros(np.shape(self.psi), dtype = TYPE_DP)


    w_a = np.tile(self.w, (self.n_modes, 1)).ravel()
    w_b = np.tile(self.w, (self.n_modes, 1)).T.ravel()

    vector = self.psi[:self.n_modes]
    dyn = self.psi[self.n_modes:]
    new_dyn = -dyn * np.sqrt( (w_a + w_b)/(w_a*w_b)) / 2

    # Here the time consuming part
    if self.mode == 0:
        # DEBUGGING PYTHON VERSION (SLOW)
        out_v = SlowApplyD3ToDyn(self.X, self.Y, self.rho, self.w, self.T, new_dyn)
        out_d = SlowApplyD3ToVector(self.X, self.Y, self.rho, self.w, self.T, vector)
    elif self.mode >= 1:
        out_v = FastApplyD3ToDyn(self.X, self.Y, self.rho, self.w, self.T, new_dyn, self.symmetries,
                                 self.N_degeneracy, self.degenerate_space, self.mode)
        out_d = FastApplyD3ToVector(self.X, self.Y, self.rho, self.w, self.T, vector, self.symmetries,
                                    self.N_degeneracy, self.degenerate_space, self.mode)
    else:
        print("Error, mode %d not recognized." % self.mode)
        raise ValueError("Mode not recognized %d" % self.mode)

    out_d *= -np.sqrt( (w_a + w_b)/(w_a*w_b)) / 2

    out_vect = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

    out_vect[:self.n_modes] = out_v
    out_vect[self.n_modes:] = out_d
    return out_vect

apply_L2_FT(transpose=False)

Apply the full matrix at finite temperature.

Source code in tdscha/DynamicalLanczos.py
3465
3466
3467
3468
3469
3470
3471
3472
3473
def apply_L2_FT(self, transpose = False):
    """
    Apply the full matrix at finite temperature.
    """ 

    if self.ignore_v3:
        return np.zeros(self.psi.shape, dtype = TYPE_DP)

    return FastD3_FT(self.X, self.Y, self.rho, self.w, self.T, self.psi, self.symmetries, self.N_degeneracy, self.degenerate_space, self.mode, transpose)

apply_L3()

APPLY THE L3

This is the last part of the L matrix, it puts in communication the dyn part of psi with herselfs.

Source code in tdscha/DynamicalLanczos.py
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
def apply_L3(self):
    """
    APPLY THE L3
    ============

    This is the last part of the L matrix, it puts in communication 
    the dyn part of psi with herselfs.
    """

    w_a = np.tile(self.w, (self.n_modes, 1)).ravel()
    w_b = np.tile(self.w, (self.n_modes, 1)).T.ravel()

    simple_output = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

    #simple_output[self.n_modes:] = self.psi[self.n_modes:] * (w_a + w_b)**2

    if self.ignore_v4:
        return simple_output

    # Apply the D4

    dyn = self.psi[self.n_modes:] * np.sqrt((w_a + w_b) / (w_a * w_b)) / 2



    # Here the time consuming part [The most of all]!!!
    if self.mode == 0:
        # A very slow implementation
        # Use it just for debugging
        out_dyn = SlowApplyD4ToDyn(self.X, self.Y, self.rho, self.w, self.T, dyn)
    elif self.mode >= 1:
        # The fast C implementation
        #print ("Inside v4 MPI, this will take a while")
        out_dyn = FastApplyD4ToDyn(self.X, self.Y, self.rho, self.w, self.T, dyn,
                                   self.symmetries, self.N_degeneracy, self.degenerate_space, self.mode)       

    out_dyn *= np.sqrt((w_a + w_b) / (w_a * w_b)) / 2

    output = np.zeros(np.shape(self.psi), dtype = TYPE_DP)
    output[self.n_modes:] = out_dyn

    output += simple_output

    return output

apply_L3_FT(transpose=False)

APPLY THE L3

This is the last part of the L matrix, it puts in communication the dyn part of psi with herselfs.

Source code in tdscha/DynamicalLanczos.py
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
def apply_L3_FT(self, transpose = False):
    """
    APPLY THE L3
    ============

    This is the last part of the L matrix, it puts in communication 
    the dyn part of psi with herselfs.
    """

    simple_output = np.zeros(np.shape(self.psi), dtype = TYPE_DP)

    #simple_output[self.n_modes:] = self.psi[self.n_modes:] * (w_a + w_b)**2

    if not self.ignore_v4:
        simple_output[:] = FastD4_FT(self.X, self.Y, self.rho, self.w, self.T, self.psi, self.symmetries, self.N_degeneracy, self.degenerate_space, self.mode, transpose)


    return simple_output

apply_full_L(target=None, force_t_0=False, force_FT=True, transpose=False, fast_lanczos=True)

APPLY THE L

This function applies the L operator to the specified target vector. The target vector is first copied into the local psi, and the computed. NOTE: This function will overwrite the current psi with the specified target.

Returns:
-self.psi: the updated vector
Source code in tdscha/DynamicalLanczos.py
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
def apply_full_L(self, target = None, force_t_0 = False, force_FT = True, transpose = False, fast_lanczos = True):
    """
    APPLY THE L 
    ===========

    This function applies the L operator to the specified target vector.
    The target vector is first copied into the local psi, and the computed.
    NOTE: This function will overwrite the current psi with the specified
    target.

    Parameters
    ----------
        target : ndarray ( size = shape(self.psi)), optional
            The garget vector to which you want to apply the
            full L matrix
        force_t_0 : bool
            If False (default) the temperature is looked to chose if use the T = 0 or the finite temperature.
            If True it is forced the T=0 method (This will lead to wrong results at finite temperature).
        force_FT : bool
            If True the finite temperature method is forced even if T = 0.
            The results should be good, use it for testing.
            NOTE: only one between force_t_0 and force_FT should be true
        fast_lanczos : bool
            If true this method applies the L2 and L3 using the self-consistent way.
            This is much quicker, but needs to be tested
        transpose : bool
            Default is False, if it is true we apply the transpose
        fast_lanczos : bool
            See force_t_0 for details.

    Returns:
    -------
        -self.psi: the updated vector

    """
    if force_t_0 and force_FT:
        raise ValueError("Error, only one between force_t_0 and force_FT can be True")

    # Setup the target vector to the self.psi
    if not target is None:
        self.psi = target 

    # Check the initialization
    if not self.initialized:
        raise ValueError("Error, this class must be initialized before lunching the computation:\n Use the .init()")

    #if self.symmetrize:
    #    self.symmetrize_psi()

    #print("Psi before:")
    #print(self.psi[:self.n_modes])


    # Apply the whole L step by step to self.psi
    t1 = timer()
    if (force_t_0 or self.T < __EPSILON__) and not force_FT:
        # Harmonic evolution
        output = self.apply_L1()
    else:
        #HARMONIC evolution at finite temperature
        output = self.apply_L1_FT(transpose)
    t2 = timer()

    # Apply the quick_lanczos
    t4 = timer()
    if fast_lanczos and (not self.ignore_v3):
        # AN-HARMONIC evolution finite temperature
        output += self.apply_anharmonic_FT(transpose)
        t3 = timer()
        t4 = t3
    # else:
    #     if (force_t_0 or self.T < __EPSILON__) and not force_FT:
    #         output += self.apply_L2()
    #     else:
    #         output += self.apply_L2_FT(transpose)
    #     t3 = timer()
    #     if (force_t_0 or self.T < __EPSILON__) and not force_FT:
    #         output += self.apply_L3()
    #     else:
    #         output += self.apply_L3_FT(transpose)
    #     t4 = timer()

    if self.verbose:
        print("Time to apply the full L: {}".format(t4 - t1))
        print("  Harmonic part (L1): {:.6f} s".format(t2 - t1))
        print("  Anharmonic part:    {:.6f} s".format(t4 - t2))

    # Apply the shift reverse
    #print ("Output before:")
    #print (output[:self.n_modes])
    if self.reverse_L:
        output *= -1
    output += self.shift_value * self.psi
    #print ("Output after:")
    #print (output[:self.n_modes])

    # Now return the output
    #print ("out just before return:", output[0])
    self.psi = output
    #if self.symmetrize:
    #    self.symmetrize_psi()

    return self.psi

save_abc(file)

Save only the a, b, and c coefficients from the lanczos. In this way the calculation cannot be restarted.

Source code in tdscha/DynamicalLanczos.py
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
def save_abc(self, file):
    """
    Save only the a, b, and c coefficients from the lanczos.
    In this way the calculation cannot be restarted.
    """

    total_len = len(self.c_coeffs)

    abc = np.zeros( (total_len, 3), dtype = np.double)
    abc[:,0] = self.a_coeffs[:total_len]
    abc[:,1] = self.b_coeffs
    abc[:,2] = self.c_coeffs

    np.savetxt(file, abc, header = "a; b; c")

load_abc(file)

Load only the a, b, and c coefficients from the ".abc" file

Source code in tdscha/DynamicalLanczos.py
3659
3660
3661
3662
3663
3664
3665
3666
3667
def load_abc(self, file):
    """
    Load only the a, b, and c coefficients from the ".abc" file
    """

    abc = np.loadtxt(file)
    self.a_coeffs = abc[:,0]
    self.b_coeffs = abc[:,1]
    self.c_coeffs = abc[:,2]

save_status(file)

Save the current data in npz compressed format, in order to reanalyze easily the result (or restart the Lanczos) later.

Source code in tdscha/DynamicalLanczos.py
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
def save_status(self, file):
    """
    Save the current data in npz compressed format, in order to reanalyze easily the result (or restart the Lanczos)
    later.

    Parameters
    ----------
        file : string
            Path to where you want to save the data. It must be an npz binary format. The extension
            will be added if it does not match the npz one
    """
    # Force all the processes to be here
    Parallel.barrier()

    # Add the correct extension
    if not ".npz" in file.lower():
        file += ".npz"

    # Save all the data
    if Parallel.am_i_the_master():
        np.savez_compressed(file, 
                            T = self.T,
                            nat = self.nat,
                            m = self.m,
                            w = self.w,
                            pols = self.pols,
                            n_modes = self.n_modes,
                            ignore_v3 = self.ignore_v3,
                            ignore_v4 = self.ignore_v4,
                            N = self.N,
                            rho = self.rho,
                            X = self.X,
                            Y = self.Y,
                            psi = self.psi,
                            a_coeffs = self.a_coeffs,
                            b_coeffs = self.b_coeffs,
                            c_coeffs = self.c_coeffs,
                            basis_Q = self.basis_Q,
                            basis_P = self.basis_P,
                            s_norm = self.s_norm,
                            krilov_basis = self.krilov_basis,
                            arnoldi_matrix = self.arnoldi_matrix,
                            reverse = self.reverse_L,
                            shift = self.shift_value,
                            perturbation_modulus = self.perturbation_modulus,
                            use_wigner = self.use_wigner,
                            ignore_small_w = self.ignore_small_w,
                            sym_julia = self.sym_julia,
                            deg_julia = self.deg_julia,
                            n_syms = self.n_syms)

load_status(file, is_file_instance=False)

Load a previously saved status from the speficied npz file. The file must be saved with save_status.

If is_file_instance is True, the file is assumed to be already loaded with open(...)

Source code in tdscha/DynamicalLanczos.py
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
def load_status(self, file, is_file_instance = False):
    """
    Load a previously saved status from the speficied npz file.
    The file must be saved with save_status.

    If is_file_instance is True, the file is assumed to be already loaded with open(...)
    """

    # Force all the process to be here
    Parallel.barrier()

    if not is_file_instance:
        # Add the correct extension
        if not ".npz" in file.lower():
            file += ".npz"

        # Check if the provided file exists
        if not os.path.exists(file):
            print ("Error while loading %s file.\n" % file)
            raise IOError("Error while loading %s" % file)

    data = {}
    # Read the data only with the master
    if Parallel.am_i_the_master():

        # Fix the allow pickle error in numpy >= 1.14.4
        try:
            data = np.load(file, allow_pickle = True)
        except ValueError:
            print("Error in pickling the data")
            raise
        except:
            print("Error, while loading with allow_pickle (numpy version < 1.14.4?)")
            print("       numpy version = {}".format(np.__version__))
            print("Trying without pickling...")
            data = np.load(file) 

    # Now bcast to everyone
    data = Parallel.broadcast(dict(data))

    self.T = np.double(data["T"])
    self.nat = np.intc(data["nat"])
    self.m = data["m"]
    self.w = data["w"]
    self.pols = data["pols"]
    self.n_modes = np.intc(data["n_modes"])
    self.ignore_v3 = bool(data["ignore_v3"])
    self.ignore_v4 = bool(data["ignore_v4"])
    self.N = np.intc(data["N"])
    self.rho = data["rho"]
    self.X = data["X"]
    self.Y = data["Y"]
    self.psi = data["psi"]
    self.a_coeffs = data["a_coeffs"]
    self.b_coeffs = data["b_coeffs"]
    if "c_coeffs" in data:
        self.c_coeffs = data["c_coeffs"]
    # Make them as lists
    self.a_coeffs = list(self.a_coeffs)
    self.b_coeffs = list(self.b_coeffs)
    self.c_coeffs = list(self.c_coeffs)
    self.krilov_basis = data["krilov_basis"]
    self.arnoldi_matrix = data["arnoldi_matrix"]

    try:
        self.sym_julia = data["sym_julia"]
        self.deg_julia = data["deg_julia"]
        self.n_syms = data["n_syms"]
    except:
        print('ATTENTION THE JULIA VARIABLES  WERE NOT LOADED')
        self.sym_julia = None
        self.deg_julia = None
        self.n_syms = 1


    self.basis_Q = data["basis_Q"]
    self.basis_P = data["basis_P"]
    self.s_norm = data["s_norm"]

    self.use_wigner = data["use_wigner"]
    try:
        self.ignore_small_w = data["ignore_small_w"]
    except:
        self.ignore_small_w = False #data["ignore_small_w"]

    if "reverse" in data.keys():
        self.reverse_L = data["reverse"]
        self.shift_value = data["shift"]

    if "symmetries" in data.keys():
        self.symmetries = data["symmetries"]
        self.N_degeneracy = data["N_degeneracy"]
        self.initialized = data["initialized"]
        self.degenerate_space = data["degenerate_space"]

    if "perturbation_modulus" in data.keys():
        self.perturbation_modulus = data["perturbation_modulus"]


    # Prepare the L as a linear operator (Prepare the possibility to transpose the matrix)
    def L_transp(psi):
        return self.apply_full_L(psi, transpose= True)
    self.L_linop = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)),\
                                                      matvec = self.apply_full_L, rmatvec = L_transp, dtype = TYPE_DP)

    # Define the preconditioner
    def M_transp(psi):
        return self.apply_L1_inverse_FT(psi, transpose = True)
    self.M_linop = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)),\
                                                      matvec = self.apply_L1_inverse_FT, rmatvec = M_transp, dtype = TYPE_DP)

run_biconjugate_gradient(verbose=True, tol=0.0005, maxiter=1000, save_g=None, save_each=1, use_preconditioning=True, algorithm='bicgstab')

STATIC RESPONSE

Get the static response inverting the green function. This is performed by exploiting the bi-conjugate gradient algorithm.

Results
G_inv: ndarray(size = (n_modes, n_modes))
    This is the mass-rescaled free energy Hessian.
    Its eigenvalues are the static frequencies, that determine the structure stability.
Source code in tdscha/DynamicalLanczos.py
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
    def run_biconjugate_gradient(self, verbose = True, tol = 5e-4, maxiter = 1000, save_g = None, save_each = 1, use_preconditioning = True, algorithm = "bicgstab"):
        """
        STATIC RESPONSE
        ===============

        Get the static response inverting the green function.
        This is performed by exploiting the bi-conjugate gradient algorithm.

        Parameters
        ----------
            verbose : bool
                If true, print the status of the algorithm in standard output
            tol : float
                Tollerance of the biconjugate gradient algorithm 
            maxiter: int
                The maximum number of iterations for the biconjugate gradient.
            save_g: string
                Path to the file on which save the green function. 
                It is saved after a number of steps specified by save_each.
            save_each: int
                Determines after how many steps to save the green function.
            use_preconditioning: bool
                If true, uses the preconditioning to solve the gradient.
                The precondition is obtained by inverting analytically only the Harmonic propagation of the
                L matrix
            algorithm: string
                The algorithm used to invert the L matrix. One between:
                   - bicg : Conjugate Gradient (Default)
                   - bicgstab : Stabilized Conjugate Gradient.
                   - cg-minimize: Conjugate Gradient with preconditioned minimization.
                         This algorithm minimizes the auxiliary function
                         .. math::

                            f(x) = \frac 12 r H^-1 r

                        where :math:`r = Lx - b` and :math:`H = L^\dagger L`. 
                        The hessian :math:`H` is guessed neglecting interaction (as for the perfectly harmonic case).
                It will invoke the corresponding scipy subroutine

        Results
        -------
            G_inv: ndarray(size = (n_modes, n_modes))
                This is the mass-rescaled free energy Hessian.
                Its eigenvalues are the static frequencies, that determine the structure stability.
        """

        G_one_phonon = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)

        if verbose:
            print()
            print("====================")
            print("BICONJUGATE GRADIENT")
            print("====================")
            print()
            print("We compute the static response with the")
            print("Biconjugate gradient algorithm.")
            print()


        # Check if the symmetries has been initialized
        if not self.initialized:
            self.prepare_symmetrization()

        j = np.zeros(1, dtype = np.intc)
        x_old = self.psi.copy()
        for i in range(self.n_modes):
            if verbose:
                # Print the status
                print()
                print()
                print("NEW STEP")
                print("--------")
                print()
                print("i = {} / {}".format(i + 1, self.n_modes))
                print()

            # Setup the known vector
            self.psi = np.zeros(self.psi.shape, dtype = type(self.psi[0]))
            self.psi[i] = 1

            x_old[:] = self.psi
            j[0] = 0
            def callback(xk, x_old = x_old, j = j):
                if np.isnan(np.sum(xk)):
                    raise ValueError("Error, NaN value found during the Biconjugate Gradient.") 
                if verbose:
                    disp = sum( (xk - x_old)**2)
                    print("BCG STEP {} | solution changed by {} (tol = {})".format(j[0], disp, tol))
                    j[0] += 1
                    x_old[:] = xk

            # Prepare the preconditioning
            M_prec = None
            x0 = self.M_linop.matvec(self.psi)
            if use_preconditioning:
                M_prec = self.M_linop
                #x0 = M_prec.matvec(self.psi)

            # Run the biconjugate gradient
            t1 = time.time()
            if algorithm.lower() == "bicgstab":
                res, info = scipy.sparse.linalg.bicgstab(self.L_linop, self.psi, x0 = x0, tol = tol, maxiter = maxiter, callback=callback, M = M_prec)
            elif algorithm.lower() == "bicg":
                res, info = scipy.sparse.linalg.bicg(self.L_linop, self.psi, x0 = x0, tol = tol, maxiter = maxiter, callback=callback, M = M_prec)
            elif algorithm.lower() == "cg-minimize":
                # This algorithm minimizes f(x) = 1/2  (Lx - b) H^-1 (Lx - b)
                # where H is the matrix H = L^T L (so it is positive definite). 
                # We pick the H inverse as the inverse of the SSCHA harmonic solution.  
                # To find x and compute x = A^-1 b

                # Here we define the function that returns f(x) and its gradient
                def func(x, b):
                    # Apply
                    r = self.L_linop.matvec(x) 
                    r -= b 

                    # Apply the precondition H^-1 = (L^t L)^-1 => M M^t
                    if use_preconditioning:
                        Hinv_r = self.M_linop.rmatvec(r)
                        Hinv_r = self.M_linop.matvec(Hinv_r)
                    else:
                        Hinv_r = r

                    # Now we get the gradient
                    gradient = self.L_linop.rmatvec(Hinv_r) 

                    # We get the function
                    f = 0.5 * np.sum(r * Hinv_r)

                    if verbose:
                        print("Evaluated function: value = {} | norm gradient = {}".format(f, np.sum(gradient**2)))
                        print()

                    return f, gradient


                psi_vector = self.psi.copy() 

                # Setup the minimization parameters
                options = {"gtol" : tol, "maxiter" : maxiter, "disp" : verbose, "norm" : 2}

                # Start the minimization
                results = scipy.optimize.minimize(func, x0, args = (psi_vector), method = "bfgs", jac = True, options=options)

                # Get the number of iterations
                j[0] = results.nit

                # Check the success
                if results.success:
                    info = 0
                else:
                    info = 1

                if verbose:
                    print("Minimization terminated after {} evaluations.".format(results.nfev))

                # Get the result
                res = results.x.copy()
            else:
                raise ValueError("""
Error, algorithm type '{}' in subroutine run_biconjugate_gradient not implemented.
       the only supported algorithms are ['bicgstab', 'bicg']
""".format(algorithm))
            t2 = time.time()

            if  verbose:
                print()
                print("Time to solve the linear system: {} s".format(t2 - t1))
                print()

            # Check if the minimization converged
            assert info >= 0, "Error on input or breakdown of biconjugate gradient algorithm (info = {})".format(info)

            if info > 0:
                print("The biconjugate gradient (step {}) algorithm did not converge after {} iterations.".format(i+1, maxiter))
                print("Try to either reduce the tollerance or increase the number of iteriations")
                print()
            else:
                print("The biconjugate gradient converged after {} iterations.".format(j[0]))


            G_one_phonon[i, :] = res[:self.n_modes]
            if i % save_each == 0:
                if save_g is not None:
                    np.save(save_g, G_one_phonon)


        if verbose:
            print()
            print(" ================================== ")
            print(" THE BICONJUGATE GRADIENT CONVERGED ")
            print(" ================================== ")
            print()
            print()


        if save_g is not None:
            np.save(save_g, G_one_phonon)

        # Check the hermitianeity
        disp = np.max(np.abs(G_one_phonon - G_one_phonon.T))
        assert disp < 1e-4, "Error, the resulting one-phonon Green function is not Hermitian."

        # Force hermitianity
        G = 0.5 * (G_one_phonon + G_one_phonon.T)

        # Invert the green function to get the Hessian Matrix (mass-rescaled)
        G_inv = np.linalg.inv(G) 

        return G_inv

run_hessian_calculation(verbose=True, eigen_shift=1, tol=0.0005, max_iters=1000, save_g=None, save_each=1, use_preconditioning=True, algorithm='cg')

STATIC RESPONSE

Get the static suscieptibility with the conjugate gradient algorithm. Then the hessian matrix is obtained inverting the static suscieptibility.

Results
G_inv: ndarray(size = (n_modes, n_modes))
    This is the mass-rescaled free energy Hessian.
    Its eigenvalues are the static frequencies, that determine the structure stability.
Source code in tdscha/DynamicalLanczos.py
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
    def run_hessian_calculation(self, verbose = True, eigen_shift = 1, tol = 5e-4, max_iters = 1000, save_g = None, save_each = 1, use_preconditioning = True, algorithm = "cg"):
        r"""
        STATIC RESPONSE
        ===============

        Get the static suscieptibility with the conjugate gradient algorithm. 
        Then the hessian matrix is obtained inverting the static suscieptibility.

        Parameters
        ----------
            verbose : bool
                If true, print the status of the algorithm in standard output
            eigen_shift : float
                The eigenvalue problem is shifted by :math:`\lambda` as

                .. math ::

                    L' \rightarrow L\left[ P + (1 - P) \lambda\right]

                This does not change the final solution as we are interested in

                .. math ::

                    P L^{-1} P = P {L'}^{-1}P

                So, if the matrix is not positive definite, we can exploit this force int positive definite.

            tol : float
                Tollerance of the biconjugate gradient algorithm 
            maxiter: int
                The maximum number of iterations for the biconjugate gradient.
            save_g: string
                Path to the file on which save the green function. 
                It is saved after a number of steps specified by save_each.
            save_each: int
                Determines after how many steps to save the green function.
            use_preconditioning: bool
                If true, uses the preconditioning to solve the gradient.
                The precondition is obtained by inverting analytically only the Harmonic propagation of the
                L matrix
            algorithm: string
                The algorithm used to invert the L matrix. One between:
                   - cg : Conjugate Gradient (Default)
                It will invoke the corresponding scipy subroutine

        Results
        -------
            G_inv: ndarray(size = (n_modes, n_modes))
                This is the mass-rescaled free energy Hessian.
                Its eigenvalues are the static frequencies, that determine the structure stability.
        """

        G_one_phonon = np.zeros( (self.n_modes, self.n_modes), dtype = np.double)

        if verbose:
            print()
            print("=====================")
            print(" HESSIAN CALCULATION ")
            print("=====================")
            print()
            print("We compute the static response with the")
            print("Conjugate gradient algorithm.")
            print()


        # Check if the symmetries has been initialized
        if not self.initialized:
            self.prepare_symmetrization()

        # Prepare the psi vector
        # And the L operator for the efficient calculation of the static response
        self.psi = np.zeros( self.n_modes + self.n_modes*(self.n_modes + 1) // 2, dtype = TYPE_DP)

        def apply_static_L(psi):
            self.psi[:] = psi.copy()

            # Apply the shift
            self.psi[self.n_modes :] *= eigen_shift

            ret = self.apply_L1_static(self.psi)
            ret += self.apply_anharmonic_static()

            ret[self.n_modes :] *= eigen_shift
            return ret

        L_operator = scipy.sparse.linalg.LinearOperator(shape = (len(self.psi), len(self.psi)), matvec = apply_static_L, dtype = TYPE_DP)

        j = np.zeros(1, dtype = np.intc)
        x_old = self.psi.copy()
        for i in range(self.n_modes):
            if verbose:
                # Print the status
                print()
                print()
                print("NEW STEP")
                print("--------")
                print()
                print("i = {} / {}".format(i + 1, self.n_modes))
                print()

            # Setup the known vector
            self.psi = np.zeros(self.psi.shape, dtype = type(self.psi[0]))
            self.psi[i] = 1

            x_old[:] = self.psi
            j[0] = 0
            def callback(xk, x_old = x_old, j = j):
                if np.isnan(np.sum(xk)):
                    raise ValueError("Error, NaN value found during the Conjugate Gradient.") 
                if verbose:
                    disp = sum( (xk - x_old)**2)
                    print("CG STEP {} | solution changed by {} (tol = {})".format(j[0], disp, tol))
                    j[0] += 1
                    x_old[:] = xk

            # Prepare the preconditioning

            M_prec = None

            def prec_half(x):
                return self.apply_L1_static(x, inverse = True, power = 0.5)

            if use_preconditioning:
                M_prec = scipy.sparse.linalg.LinearOperator(L_operator.shape, matvec = prec_half)

            x0 = self.psi.copy()# / self.w[i] / self.w[i]

            # Run the biconjugate gradient
            t1 = time.time()
            if algorithm.lower() == "cg":
                if not use_preconditioning:
                    res = Tools.minimum_residual_algorithm(L_operator, self.psi.copy(), x0 = x0, precond = None, max_iters = max_iters)
                else:
                    res = Tools.minimum_residual_algorithm_precond(L_operator, self.psi.copy(), M_prec, max_iters = max_iters)
                info = 0
                #res, info = scipy.sparse.linalg.cg(L_operator, self.psi, x0 = x0)#, tol = tol, maxiter = maxiter, callback=callback, M = M_prec)
            #elif algorithm.lower() == "bicg":
            #    res, info = scipy.sparse.linalg.bicg(self.L_linop, self.psi, x0 = x0, tol = tol, maxiter = maxiter, callback=callback, M = M_prec)
            elif algorithm.lower() == "minimize" or "minimize-quad":
                # This algorithm minimizes f(x) = 1/2  (Lx - b) H^-1 (Lx - b)
                # where H is the matrix H = L^T L (so it is positive definite). 
                # We pick the H inverse as the inverse of the SSCHA harmonic solution.  
                # To find x and compute x = A^-1 b

                # Here we define the function that returns f(x) and its gradient
                def func_quad(x, b):
                    # Apply
                    Lx = L_operator.matvec(x) 

                    # Apply the precondition H^-1 = (L^t L)^-1 => M M^t
                    # if use_preconditioning:
                    #     Hinv_r = self.M_linop.rmatvec(r)
                    #     Hinv_r = self.M_linop.matvec(Hinv_r)
                    # else:
                    #     Hinv_r = r

                    # Now we get the gradient
                    r = Lx - b

                    gradient = L_operator.matvec(r)

                    # We get the function
                    f = 0.5 * np.sum(r**2)

                    if verbose:
                        print("Evaluated function: value = {} | norm gradient = {}".format(f, np.sum(gradient**2)))
                        print()

                    return f, gradient

                def func_standard(x, b):
                    # Apply
                    Lx = L_operator.matvec(x) 

                    # Apply the precondition H^-1 = (L^t L)^-1 => M M^t
                    # if use_preconditioning:
                    #     Hinv_r = self.M_linop.rmatvec(r)
                    #     Hinv_r = self.M_linop.matvec(Hinv_r)
                    # else:
                    #     Hinv_r = r

                    # Now we get the gradient
                    r = Lx - b

                    gradient = r

                    # We get the function
                    f = 0.5 * x.dot(Lx) - b.dot(x) 

                    if verbose:
                        print("Evaluated function: value = {} | norm gradient = {}".format(f, np.sum(gradient**2)))
                        print()

                    return f, gradient

                psi_vector = self.psi.copy() 

                # Setup the minimization parameters
                options = {"gtol" : tol, "maxiter" : max_iters, "disp" : verbose, "norm" : 2}

                if algorithm.lower() == "minimize":
                    results = scipy.optimize.minimize(func_standard, x0, args = (psi_vector), method = "bfgs", jac = True, options=options)
                elif algorithm.lower() == "minimize-quad":
                    results = scipy.optimize.minimize(func_quad, x0, args = (psi_vector), method = "bfgs", jac = True, options=options)


                # Start the minimization

                # Get the number of iterations
                j[0] = results.nit

                # Check the success
                if results.success:
                    info = 0
                else:
                    info = 1

                if verbose:
                    print("Minimization terminated after {} evaluations.".format(results.nfev))

                # Get the result
                res = results.x.copy()
            else:
                raise ValueError("""
Error, algorithm type '{}' in subroutine run_biconjugate_gradient not implemented.
       the only supported algorithms are ['bicgstab', 'bicg']
""".format(algorithm))
            t2 = time.time()

            if  verbose:
                print()
                print("Time to solve the linear system: {} s".format(t2 - t1))
                print()

            # Check if the minimization converged
            assert info >= 0, "Error on input or breakdown of biconjugate gradient algorithm (info = {})".format(info)

            if info > 0:
                print("The biconjugate gradient (step {}) algorithm did not converge after {} iterations.".format(i+1, maxiter))
                print("Try to either reduce the tollerance or increase the number of iteriations")
                print()
            else:
                print("The biconjugate gradient converged after {} iterations.".format(j[0]))
                print("res = {}".format(res))


            G_one_phonon[i, :] = res[:self.n_modes]
            if i % save_each == 0:
                if save_g is not None:
                    np.save(save_g, G_one_phonon)


        if verbose:
            print()
            print(" ================================ ")
            print(" THE CONJUGATE GRADIENT CONVERGED ")
            print(" ================================ ")
            print()
            print()


        if save_g is not None:
            np.save(save_g, G_one_phonon)

        # Check the hermitianeity
        disp = np.max(np.abs(G_one_phonon - G_one_phonon.T))
        assert disp < 1e-4, "Error, the resulting one-phonon Green function is not Hermitian."

        # Force hermitianity
        G = 0.5 * (G_one_phonon + G_one_phonon.T)

        # Invert the green function to get the Hessian Matrix (mass-rescaled)
        G_inv = np.linalg.inv(G) 

        return G_inv

run_conjugate_gradient(eigval=0, n_iters=100, thr=1e-05, verbose=True, guess_x=None)

RUN THE CONJUGATE GRADIENT (WORK METHOD)

The conjugate gradient is a very fast algorithm that allows to compute the (in principle) exact green function.

Given the initial vector in the psi direction, it will edit it to obtain:

.. math ::

\left|x\right> = \left(\mathcal{L} - I\lambda\right)^{-1} \left| {\psi} \right>

At the end of the algorithm, the self.psi variable will contain the :math:\left|x\right> vector.

Source code in tdscha/DynamicalLanczos.py
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
def run_conjugate_gradient(self, eigval = 0, n_iters = 100, thr = 1e-5, verbose = True, guess_x = None):
    r"""
    RUN THE CONJUGATE GRADIENT (WORK METHOD)
    ========================================

    The conjugate gradient is a very fast algorithm 
    that allows to compute the (in principle) exact green function. 

    Given the initial vector in the psi direction, it will edit it to obtain:

    .. math ::

        \left|x\right> = \left(\mathcal{L} - I\lambda\right)^{-1} \left| {\psi} \right> 

    At the end of the algorithm, the self.psi variable will contain the :math:`\left|x\right>`
    vector.

    Parameters
    ----------
        eigval : float
            The value of the :math:`\lambda` for the inversion problem.
        n_iters : int
            The number of iterations
        thr : float
            The threshold between two iteration after which the algorithm is considered 
            to be converged.
        verbose : bool
            If true print the status of the iteration.

    """

    if guess_x is None:
        x = self.psi.copy()
    else:
        x = guess_x.copy()
    A_x = self.apply_full_L()

    r = x - A_x
    p = r.copy()

    mod_r = np.sqrt(r.dot(r))

    if verbose:
        print("   CG step %d : residual = %.4e | threshold = %.4e" % (0,mod_r, thr))

    if mod_r < thr:
        return x

    for i in range(n_iters):
        self.psi = p
        A_p = self.apply_full_L()
        alpha = r.dot(r) / p.dot(A_p)
        x += alpha * p 

        # Update
        r_new = r - alpha * A_p 
        beta = r_new.dot(r_new) / r.dot(r)

        r = r_new
        p = r + beta * p 


        # Check the new iteration
        mod_r = np.sqrt(r.dot(r))
        if verbose:
            print("   CG step %d : residual = %.4e | threshold = %.4e" % (i+1,mod_r, thr))

        if mod_r < thr:
            return x

    print("WARNING: CG ended before the convergence was achieved.") 
    return x

get_statical_responce_from_scratch(n_iters=100, thr=1e-05, verbose=True, sub_block=None, sub_space=None)

GET STATIC RESPONCE

This algorithm performs the CG minimization to obtain the static self-energy.

Results
fc_matrix : ndarray (size=(3*nat, 3*nat))
    The static self-energy.
Source code in tdscha/DynamicalLanczos.py
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
def get_statical_responce_from_scratch(self, n_iters = 100, thr = 1e-5, verbose = True, sub_block = None, sub_space = None):
    """
    GET STATIC RESPONCE
    ===================

    This algorithm performs the CG minimization to obtain the static self-energy.

    Parameters
    ----------
        n_iters : int
            The number of maximum iteration for a single CG step.
        thr : float
            The threshold for the convergence of the CG algorithm.
        verbose : bool
            If true (default) prints the info during the minimization
        sub_block : list
            A list of indices that identifies the modes id that that you want to select.
            The algorithm is performed only in the reduced space of (N_modes x N_modes)
            given by the length of this list. 
            In this way you will neglect mode interaction, but you can save a lot of time.
            Leave as None if you want the whole space.
        sub_space : ndarray(size = (N_dim, 3*n_at))
            Compute the self-energy only in the subspace given. Leave it as none
            if you do not want to use this option

    Results
    -------
        fc_matrix : ndarray (size=(3*nat, 3*nat))
            The static self-energy.
    """

    n_dim_space = self.n_modes


    PLinvP = np.zeros((n_dim_space, n_dim_space), dtype = TYPE_DP, order = "C")

    if (not sub_block is None) and (not sub_space is None):
        raise ValueError("Error, you cannot specify both sub_block and sub_space.")

    if not sub_block is None:
        n_dim_space = len(sub_block)
    elif not sub_space is None:
        n_dim_space = len(sub_space)

    # initialize the algorithm
    for i in range(n_dim_space):
        # Setup the vector
        self.psi = np.zeros((self.n_modes + 1)*self.n_modes, dtype = TYPE_DP)
        guess = self.psi.copy()

        # Create the subbasis
        if not sub_block is None:
            self.psi[sub_block[i]] = 1
            guess[sub_block[i]] = self.w[sub_block[i]] ** 2
        elif not sub_space is None:
            self.psi[:self.n_modes] = sub_block[i].dot(self.pols)
            guess = None
        else:
            self.psi[i] = 1
            guess[i] = self.w[i] ** 2

        if verbose:
            print("")
            print("==== NEW STATIC COMPUTATION ====")
            print("Iteration: %d out of %d" % (i+1, n_dim_space))


        new_v = self.run_conjugate_gradient(n_iters = n_iters, thr = thr, verbose = verbose, guess_x = guess)

        if not sub_space is None:
            v_out = self.pols.dot(new_v[:self.n_modes])
            PLinvP[i, :] = np.array(sub_space).dot(v_out)
        else:
            PLinvP[i, :] = new_v[:self.n_modes]



    # Invert the P L^-1 P 
    D = np.linalg.inv(PLinvP)
    # Transform to a force constant matrix in cartesian coordinates

    if not sub_space is None:
        fc_matrix = np.einsum("ab, ai, bj->ij", D, np.array(sub_space), np.array(sub_space))
    else:
        fc_matrix = np.einsum("ab, ia, jb->ij", D, self.pols, self.pols)
    fc_matrix *= np.sqrt(np.outer(self.m, self.m))

    return fc_matrix

run(n_iter, save_dir='.', verbose=True)

RUN LANCZOS ITERATIONS

This method performs the Lanczos algorithm to find the sequence of a and b coefficients that are the tridiagonal representation of the L matrix to be inverted.

Source code in tdscha/DynamicalLanczos.py
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
    def run(self, n_iter, save_dir = ".", verbose = True):
        """
        RUN LANCZOS ITERATIONS
        ======================

        This method performs the Lanczos algorithm to find
        the sequence of a and b coefficients that are the tridiagonal representation 
        of the L matrix to be inverted.

        Parameters
        ----------
            n_iter : int
                The number of iterations to be performed in the Lanczos algorithm.
            save_dir : string
                The directory in which you want to store the results step by step,
                in order to do a preliminar analysis or restart the calculation later.
            verbose : bool
                If true all the info during the minimization will be printed on output.
        """

        raise ValueError("Error, this run funciton has been deprecated, use run_FT instead.")

        # Check if the symmetries has been initialized
        if not self.initialized:
            self.prepare_symmetrization()

        # Get the current step
        i_step = len(self.a_coeffs)

        if verbose:
            header = """
<=====================================>
|                                     |
|          LANCZOS ALGORITHM          |
|                                     |
<=====================================>

Starting the algorithm. It may take a while.
Starting from step %d
""" % i_step
            print(header)

            OPTIONS = """
Should I ignore the third order effect? {}
Should I ignore the fourth order effect? {}
Max number of iterations: {}
""".format(self.ignore_v3, self.ignore_v4, n_iter)
            print(OPTIONS)


        # If this is the current step initialize the algorithm
        if i_step == 0:
            self.krilov_basis = []
            first_vector = self.psi / np.sqrt(self.psi.dot(self.psi))
            self.krilov_basis.append(first_vector)
        else:
            # Convert everything in a list
            self.krilov_basis = list(self.krilov_basis)
            self.a_coeffs = list(self.a_coeffs)
            self.b_coeffs = list(self.b_coeffs)
            self.arnoldi_matrix = list(self.arnoldi_matrix)

            if len(self.krilov_basis) != i_step + 1:
                print("Krilov dim: %d, number of steps perfomed: %d" % (len(self.krilov_basis), i_step))
                print("Error, the krilov basis dimension should be 1 more than the number of steps")
                raise ValueError("Error the starting krilov basis does not matches the matrix, Look stdout.")

        self.psi = self.krilov_basis[-1]

        for i in range(i_step, i_step+n_iter):
            if verbose:
                step_txt = """
 ===== NEW STEP %d =====

 """ % i
                print(step_txt)
                print("Length of the coefficiets: a = {}, b = {}".format(len(self.a_coeffs), len(self.b_coeffs)))
                print()

            # Apply the matrix L
            t1 = time.time()
            #self.psi = self.apply_full_L()
            self.psi = self.L_linop.dot(self.psi)
            t2 = time.time()

            if verbose:
                print("Time to apply the full L: %d s" % (t2 -t1))

            # Get the coefficients for the Lanczos/Arnoldi matrix
            t1 = time.time()
            arnoldi_row = []
            new_vect = self.psi.copy()


            # Lets repeat twice the orthogonalization
            converged = False
            for k_orth in range(N_REP_ORTH):
                for j in range(len(self.krilov_basis)):
                    coeff = new_vect.dot(self.krilov_basis[j])
                    if k_orth == 0:
                        arnoldi_row.append(self.psi.dot(self.krilov_basis[j]))

                    # Gram Schmidt
                    new_vect -= coeff * self.krilov_basis[j]

                # Add the new vector to the Krilov Basis
                norm = np.sqrt(new_vect.dot(new_vect))

                if verbose:
                    print("Vector norm after GS number {}: {:16.8e}".format(k_orth+1, norm))

                # Check the normalization (If zero the algorithm converged)
                if norm < __EPSILON__:
                    converged = True
                    if verbose:
                        print("Obtained a linear dependent vector.")
                        print("The algorithm converged.")
                    break

                new_vect /= norm 

            if not converged:
                self.krilov_basis.append(new_vect)
                self.psi = new_vect
            t2 = time.time()

            # Add the coefficients to the variables
            self.a_coeffs.append(arnoldi_row[-1])
            if len(arnoldi_row) > 1:
                self.b_coeffs.append(arnoldi_row[-2])
            self.arnoldi_matrix.append(arnoldi_row)

            if verbose:
                print("Time to perform the Gram-Schmidt and retrive the coefficients: %d s" % (t2-t1))
                print()
                print("a_%d = %.8e" % (i, self.a_coeffs[-1]))
                if i > 0:
                    print("b_%d = %.8e" % (i, self.b_coeffs[-1]))
                print()

            # Save the step
            if not save_dir is None:
                self.save_status("%s/LANCZOS_STEP%d" % (save_dir, i))

                if verbose:
                    print("Status saved into '%s/LANCZOS_STEP%d'" % (save_dir, i))

            if verbose:
                print("Lanczos step %d ultimated." % i)


            if converged:
                return

build_lanczos_matrix_from_coeffs(use_arnoldi=False)

BUILD THE LANCZOS MATRIX

This method builds the Lanczos matrix from the coefficients. To execute this method correctly you must have already completed the Lanczos algorithm (method run)

Source code in tdscha/DynamicalLanczos.py
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
def build_lanczos_matrix_from_coeffs(self, use_arnoldi=False):
    """
    BUILD THE LANCZOS MATRIX
    ========================

    This method builds the Lanczos matrix from the coefficients. 
    To execute this method correctly you must have already completed the Lanczos algorithm (method run)

    Parameters
    ----------
        use_arnoldi: bool
            If true the full matrix is computed, using all the coefficients from the
            Arnoldi iteration.
    """

    N_size = len(self.a_coeffs)
    matrix = np.zeros((N_size, N_size), dtype = TYPE_DP)
    if not use_arnoldi:
        for i in range(N_size):
            matrix[i,i] = self.a_coeffs[i]
            if i>= 1:
                # Use the non-symmetric Lanczos if also c_coeffs are present
                c_coeff = self.b_coeffs[i-1]
                if len(self.c_coeffs) == len(self.b_coeffs):
                    c_coeff = self.c_coeffs[i-1]
                matrix[i-1,i] = c_coeff
                matrix[i,i-1] = self.b_coeffs[i-1]
    else:
        # Check if there are c_coeffs, in this way arnoldi matrix is not computed
        assert len(self.b_coeffs) > len(self.c_coeffs), "Error, cannot Arnoldi with non-symmetric Lanczos not implemented"
        for i in range(N_size):
            matrix[:i+1, i] = self.arnoldi_matrix[i]
            matrix[i, :i+1] = self.arnoldi_matrix[i]


    sign = 1
    if self.reverse_L:
        sign = -1

    matrix =  sign*matrix - sign* np.eye(N_size) * self.shift_value

    return matrix

get_green_function_Lenmann(w_array, smearing, v_a, v_b, use_arnoldi=False)

GET GREEN FUNCTION

Compute the green function using the Lemman representation.

Source code in tdscha/DynamicalLanczos.py
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
def get_green_function_Lenmann(self, w_array, smearing, v_a, v_b, use_arnoldi = False):
    """
    GET GREEN FUNCTION
    ==================

    Compute the green function using the Lemman representation.

    Parameters
    ----------
        w_array : ndarray
            The list of frequencies in RY for which you want to compute the
            dynamical green function.
        smearing : float
            The smearing in RY to take a non zero imaginary part.
        v_a : ndarray(size = 3*self.nat)
            The perturbation operator (on atomic positions)
        v_b : ndarray(size = 3*self.nat)
            The probed responce operator (on atomic positions)
        use_arnoldi: bool
            If true the full arnoldi matrix is used to extract eigenvalues and 
            eigenvectors. Otherwise the tridiagonal Lanczos matrix is used.
            The first one prevents the loss of orthogonality problem.
    """

    # Get the Lanczos matrix
    matrix = self.build_lanczos_matrix_from_coeffs(use_arnoldi)

    assert len(self.c_coeffs) < len(self.b_coeffs), "Lenmann cannot be used with non-symmetric Lanczos"

    # Convert the vectors in the polarization basis
    new_va = np.einsum("a, a, ab->b", 1/np.sqrt(self.m), v_a, self.pols)
    new_vb = np.einsum("a, a, ab->b", 1/np.sqrt(self.m), v_b, self.pols)

    # Dyagonalize the Lanczos matrix
    eigvals, eigvects = np.linalg.eigh(matrix)

    kb = np.array(self.krilov_basis)
    kb = kb[:-1,:]
    #print (np.shape(eigvects), np.shape(kb))
    # Convert in krilov space
    new_eigv = np.einsum("ab, ac->cb", eigvects, kb)


    Na, Nb = np.shape(matrix)
    if Na != Nb:
        raise ValueError("Error, the Lanczos matrix must be square, dim (%d,%d)" % (Na, Nb))

    gf = np.zeros(len(w_array), dtype = np.complex128)

    for j in range(Na):
        eig_v = new_eigv[:self.n_modes, j]
        matrix_element = eig_v.dot(new_va) * new_vb.dot(eig_v)
        gf[:] += matrix_element / (eigvals[j]  - w_array**2 + 2j*w_array*smearing)

    return gf

get_static_odd_fc(use_arnoldi=False)

GET STATIC FORCE CONSTANT

Get the static force constant matrix

Source code in tdscha/DynamicalLanczos.py
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
def get_static_odd_fc(self, use_arnoldi = False):
    """
    GET STATIC FORCE CONSTANT
    =========================

    Get the static force constant matrix

    Parameters
    ----------
        use_arnoldi: bool
            If true the full arnoldi matrix is used, otherwise the Lanczos tridiagonal
            matrix is used.
    """

    # Get the Lanczos matrix
    matrix = self.build_lanczos_matrix_from_coeffs(use_arnoldi)

    # Dyagonalize the Lanczos matrix
    eigvals, eigvects = np.linalg.eigh(matrix)

    Nk = len(self.krilov_basis)

    kb = np.array(self.krilov_basis)

    # Lanczos did not converged, discard the last vector
    if Nk > len(eigvals):
        kb = kb[:-1,:]

    #print (np.shape(eigvects), np.shape(kb))
    new_eigv = np.einsum("ab, ac->cb", eigvects, kb)

    Na, Nb = np.shape(matrix)
    if Na != Nb:
        raise ValueError("Error, the Lanczos matrix must be square, dim (%d,%d)" % (Na, Nb))


    fc_matrix = np.zeros( (3*self.nat, 3*self.nat), dtype = TYPE_DP)

    # Get the dynamical matrix in the polarization basis
    D = np.einsum("ai, bi, i->ab", new_eigv[:self.n_modes,:], new_eigv[:self.n_modes, :], eigvals)

    # Convert it in the standard basis
    fc_matrix = np.einsum("ab, ia, jb->ij", D, self.pols, self.pols)

    # for i in range(3*self.nat):
    #     # Define the vector
    #     v = np.zeros(3*self.nat, dtype = TYPE_DP)
    #     v[i] = 1

    #     # Convert the vectors in the polarization basis
    #     new_v = np.einsum("a, a, ab->b", np.sqrt(self.m), v, self.pols)
    #     # Convert in the krilov space 
    #     mat_coeff = np.einsum("a, ab", new_v, new_eigv[:self.n_modes, :])
    #     new_w = np.einsum("a, ba, a", mat_coeff, new_eigv[:self.n_modes,:], eigvals)

    #     #v_kb = np.einsum("ab, b", kb[:, :self.n_modes], new_v)
    #     # Apply the L matrix
    #     #w_kb = matrix.dot(v_kb)
    #     # Convert back in the polarization space
    #     #new_w = np.einsum("ab, a", kb[:, :self.n_modes], w_kb)
    #     # Convert back in real spaceDoes anyone know if there is a windows binary or a source code to run QE with GPU enhancement on windows. 
    #     w = np.einsum("a, b, ab ->a", 1/np.sqrt(self.m), new_w, self.pols)

    #     fc_matrix[i, :] = w


    # This is the dynamical matrix now we can multiply by the masses
    fc_matrix *= np.sqrt(np.outer(self.m, self.m))

    return fc_matrix

get_all_green_functions(N_steps=100, mode_mixing=True, save_step_dir=None, verbose=True)

GET ALL THE GREEN FUNCTIONS

This will compute a set of lanczos coefficients for each element of the odd matrix. a_n and b_n. We will run lanczos for all the elements and all the crosses. In this way we have the whole evolution with frequency of the matrix.

NOTE: This can be a very intensive computation.

Results
a_ns : ndarray( (n_modes, n_modes, N_steps))
    The a coefficients for each element in the mode x mode space
b_ns : ndarray( (n_modes, n_modes, N_steps-1))
    The b_n coefficients for each mode in the space.
Source code in tdscha/DynamicalLanczos.py
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
def get_all_green_functions(self, N_steps = 100, mode_mixing = True, save_step_dir = None, verbose = True):
    """
    GET ALL THE GREEN FUNCTIONS
    ===========================

    This will compute a set of lanczos coefficients for each element of the odd matrix.
    a_n and b_n.
    We will run lanczos for all the elements and all the crosses.
    In this way we have the whole evolution with frequency of the matrix.

    NOTE: This can be a very intensive computation.

    Parameters
    ----------
        N_steps : int
            The number of Lanczos iteration for each green function
        mode_mixing : bool
            If True also non diagonal elements are computed, otherwise the 
            SSCHA eigenvector are supposed to be conserved, and only diagonal
            green functions are considered.
            If False the computation is much less expensive (a factor nat_sc),
            but it is approximated.
        save_step_dir : string
            If not None, the path to the directory in which you want to save 
            each step. So even if stopped the calculation can restart.
        verbose : bool
            If true print all the progress to standard output

    Results
    -------
        a_ns : ndarray( (n_modes, n_modes, N_steps))
            The a coefficients for each element in the mode x mode space
        b_ns : ndarray( (n_modes, n_modes, N_steps-1))
            The b_n coefficients for each mode in the space.
    """

    # Time the function
    t_start = time.time()

    # Check if the save directory exists
    # Otherwise we create it
    if not save_step_dir is None:
        if not os.path.exists(save_step_dir):
            makedirs(save_step_dir)

    # Load all the data
    a_ns = np.zeros( (self.n_modes, self.n_modes, N_steps), dtype = np.double)
    b_ns = np.zeros( (self.n_modes, self.n_modes, N_steps-1), dtype = np.double)

    # Incompatible with shift for now
    self.shift_value = 0

    # Compute the diagonal parts
    for i in range(self.n_modes):
        if verbose:
            print("\n")
            print("  ==========================  ")
            print("  |                        |  ")
            print("  |   DIAGONAL ELEMENTS    |  ")
            print("  |       STEP {:5d}       |  ".format(i))
            print("  |                        |  ")
            print("  ==========================  ")
            print()

        # Setup the Lanczos
        self.reset()

        # Prepare the perturbation
        self.psi[:] = 0
        self.psi[i] = 1

        # Run the Lanczos perturbation
        self.run(N_steps, save_dir = save_step_dir, verbose = verbose)

        if verbose:
            print()
            print("   ---- > LANCZOS RUN COMPLEATED < ----   ")
            print()

        # Save the status
        if save_step_dir:
            self.save_status("full_lanczos_diagonal_{}".format(i))

        # Fill the a_n and b_n
        a_tmp = np.zeros(N_steps, dtype = np.double)
        a_tmp[:len(self.a_coeffs)] = self.a_coeffs
        b_tmp = np.zeros(N_steps-1, dtype = np.double)
        b_tmp[:len(self.b_coeffs)] = self.b_coeffs
        a_ns[i, i, :] = a_tmp
        b_ns[i, i, :] = b_tmp

    # If we must compute the mode mixing
    if mode_mixing:
        for i in range(self.n_modes):
            for j in range(i+1, self.n_modes):
                # TODO: Neglect (i,j) forbidden by symmetries

                if verbose:
                    print("\n")
                    print("  ============================  ")
                    print("  |                          |  ")
                    print("  |   NON DIAGONAL ELEMENT   |  ")
                    print("  |    STEP ({:5d},{:5d})    |  ".format(i, j))
                    print("  |                          |  ")
                    print("  ============================  ")
                    print()

                # Setup the Lanczos
                self.reset()

                # Prepare the perturbation
                self.psi[:] = 0
                self.psi[i] = 1
                self.psi[j] = 1

                # Run the Lanczos perturbation
                self.run(N_steps, save_dir = save_step_dir, verbose = verbose)

                if verbose:
                    print()
                    print("   ---- > LANCZOS RUN COMPLEATED < ----   ")
                    print()

                # Save the status
                if save_step_dir:
                    self.save_status("full_lanczos_off_diagonal_{}_{}".format(i, j))

                # Fill the a_n and b_n
                a_tmp = np.zeros(N_steps, dtype = np.double)
                a_tmp[:len(self.a_coeffs)] = self.a_coeffs
                b_tmp = np.zeros(N_steps-1, dtype = np.double)
                b_tmp[:len(self.b_coeffs)] = self.b_coeffs
                a_ns[i, j, :] = a_tmp
                b_ns[i, j, :] = b_tmp
                a_ns[j, i, :] = a_tmp
                b_ns[j, i, :] = b_tmp

    t_end = time.time()

    total_time = t_end - t_start
    minutes = int(total_time / 60)
    hours = int(minutes / 60)
    minutes -= hours * 60
    seconds = int(total_time - hours*3600 - minutes * 60)

    if verbose:
        print()
        print()
        print("     ======================     ")
        print("     |                    |     ")
        print("     |        DONE        |      ")
        print("     |   In {:3d}:{:02d}:{:02d}s  |     ".format(hours, minutes, seconds))
        print("     ======================     ")
        print()
        print()

    return a_ns, b_ns

get_spectral_function_from_Lenmann(w_array, smearing, use_arnoldi=True)

GET SPECTRAL FUNCTION

This method computes the spectral function in the supercell using the Lenmann representation.

Source code in tdscha/DynamicalLanczos.py
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
def get_spectral_function_from_Lenmann(self, w_array, smearing, use_arnoldi=True):
    """
    GET SPECTRAL FUNCTION
    =====================

    This method computes the spectral function in the supercell
    using the Lenmann representation.

    Parameters
    ----------
        w_array : ndarray
            The list of frequencies for which you want to compute the
            dynamical green function.
        smearing : float
            The smearing to take a non zero imaginary part.
        use_arnoldi: bool
            If true the full arnoldi matrix is used to extract eigenvalues and 
            eigenvectors. Otherwise the tridiagonal Lanczos matrix is used.
            The first one prevents the loss of orthogonality problem.
    """
    # Get the Lanczos matrix
    matrix = self.build_lanczos_matrix_from_coeffs(use_arnoldi)

    # Dyagonalize the Lanczos matrix
    eigvals, eigvects = np.linalg.eigh(matrix)

    Na, Nb = np.shape(matrix)
    if Na != Nb:
        raise ValueError("Error, the Lanczos matrix must be square, dim (%d,%d)" % (Na, Nb))

    spectral = np.zeros(len(w_array), dtype = np.complex128)


    kb = np.array(self.krilov_basis)
    if np.shape(kb)[0] > Na:
        kb = kb[:-1,:]
    print ("Shape check: eigvects = {}, kb = {}".format( np.shape(eigvects), np.shape(kb)))
    new_eigv = np.einsum("ab, ac->cb", eigvects, kb)
    # TODO: Update for Lanczos biconjugate

    for j in range(Na):
        eig_v = new_eigv[:self.n_modes, j]
        matrix_element = np.conj(eig_v).dot(eig_v)
        spectral[:] += matrix_element / (eigvals[j]  - w_array**2 +2j*w_array*smearing)

    return -np.imag(spectral)

get_green_function_continued_fraction(w_array, use_terminator=True, last_average=1, smearing=0)

CONTINUED FRACTION GREEN FUNCTION

In this way the continued fraction for the green function is used. This should converge faster than the Lenmann representation, and has the advantage of adding the possibility to add a terminator. This avoids to define a smearing.

NORMAL: we invert: :: math . ,

WIGNER: we invert: :: math .

So in the continued fraction we have -/+ in front of the freqeuncy depending on the formalism used.

Source code in tdscha/DynamicalLanczos.py
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
    def get_green_function_continued_fraction(self, w_array : np.ndarray[np.float64], use_terminator : bool = True, last_average: int = 1, smearing : np.float64 = 0):
        r"""
        CONTINUED FRACTION GREEN FUNCTION
        =================================

        In this way the continued fraction for the green function is used.
        This should converge faster than the Lenmann representation, and
        has the advantage of adding the possibility to add a terminator.
        This avoids to define a smearing.

        NORMAL: we invert:
        :: math .
            <p|(-\mathcal{L} - \omega^2)^{-1}|q>,

        WIGNER: we invert:
        :: math .
            <p|(\mathcal{L}_w + \omega^2)^{-1}|q>

        So in the continued fraction we have -/+ in front of the freqeuncy depending on the formalism used.

        Parameters
        ----------
            w_array : ndarray
                The list of frequencies in RY in which you want to compute the green function
            use_terminator : bool
                If true (default) a standard terminator is used.
            last_average : int
                How many a and b coefficients are averaged to evaluate the terminator?
            smearing : float
                The smearing parameter in RY. If none
        """
        n_iters = len(self.a_coeffs)

        gf = np.zeros(np.shape(w_array), dtype = np.complex128)

        sign = 1
        if self.reverse_L:
            sign = -1

        INFO = """
GREEN FUNCTION FROM CONTINUED FRACTION
Am I using Wigner? {}
Should I use the terminator? {}
Perturbation modulus = {}
Sign = {}""".format(self.use_wigner, use_terminator, self.perturbation_modulus, sign)

        if self.verbose:
            print()
            print(INFO)

        # Get the terminator
        if use_terminator:
            # Get the last coeffs averaging
            a_av = np.mean(self.a_coeffs[-last_average:])
            b_av = np.mean(self.b_coeffs[-last_average:])
            c_av = b_av
            # Non-symmetric Lanczos
            if len(self.c_coeffs) == len(self.b_coeffs):
                c_av = np.mean(self.c_coeffs[-last_average:])

            a = a_av * sign - sign* self.shift_value
            b = b_av * sign
            c = c_av * sign

            if not self.use_wigner:
                gf[:] = (a - w_array**2 - np.sqrt( (a - w_array**2)**2 - 4*b*c + 0j))/(2*b*c)
            else:
                # Wigner
                gf[:] = (a + w_array**2 + np.sqrt( (a + w_array**2)**2 - 4*b*c + 0j))/(2*b*c)        
        else:
            # If we do not use the Terminator we get the last fraction
            a = self.a_coeffs[-1] * sign - sign* self.shift_value
            if not self.use_wigner:
                gf[:] = 1/ (a - w_array**2 + 2j*w_array*smearing)
            else:
                # Wigner
                gf[:] = 1/ (a + w_array**2 + 2j*w_array*smearing)

        # Continued fraction
        for i in range(n_iters-2, -1, -1):
            # Start getting the continued fraction from the last coeff
            a = self.a_coeffs[i] * sign - sign * self.shift_value
            b = self.b_coeffs[i] * sign
            c = b
            if len(self.c_coeffs) == len(self.b_coeffs): 
                c = self.c_coeffs[i] * sign

            if not self.use_wigner:
                gf = 1. / (a - w_array**2  + 2j*w_array*smearing - b * c * gf)
            else:
                # In Wigner we invert L + omega^2
                gf = 1. / (a + w_array**2  + 2j*w_array*smearing - b * c * gf)

        if not self.use_wigner:
            return gf * self.perturbation_modulus
        else:
            # Wigner
            return (-np.real(gf) + 1j * np.imag(gf)) * self.perturbation_modulus

get_full_L_debug_wigner(verbose=False, debug_d3=None, symmetrize=True, overwrite_L_operator=True)

GET THE FULL L OPERATOR FOR DEBUG WIGNER

Use this method to test if the change of variables that defines the symmetric Wigner representation works.

Note: make sure that self.use_wigner is Fasle so when we compute the Green function we get the correct sign in front of omega in the contnued fraction.

DO NOT USE FOR PRODUCTION: IT IS DANGEROUS

Results

L_op : ndarray(size = (nmodes * (2*nmodes + 1)), dtype = TYPE_DP) The full L operator.

Source code in tdscha/DynamicalLanczos.py
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
    def get_full_L_debug_wigner(self, verbose = False, debug_d3 = None, symmetrize = True, overwrite_L_operator = True):
        """
        GET THE FULL L OPERATOR FOR DEBUG WIGNER
        ========================================
        Use this method to test if the change of variables that defines the symmetric Wigner representation works.

        Note: make sure that self.use_wigner is Fasle so when we compute the Green function
        we get the correct sign in front of omega in the contnued fraction.

        DO NOT USE FOR PRODUCTION: IT IS DANGEROUS

        Results
        -------
           L_op : ndarray(size = (nmodes * (2*nmodes + 1)), dtype = TYPE_DP)
              The full L operator.
        """
        if self.use_wigner:
            raise ErrorValue('Please make sure that use_wigner is False')

        # Avoid the dependent freqeuncies
        i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
        i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

        new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
        new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

        w_a = self.w[new_i_a]
        w_b = self.w[new_i_b]

        # N_w2 is the number of independent indeces
        N_w2 = len(w_a)

        if verbose:
            print()
            print('Getting the DEBUG WIGNER L operator')
        # Prepare the operator
        L_operator = np.zeros(shape = (self.n_modes + 2*N_w2, self.n_modes + 2*N_w2), dtype = TYPE_DP)


        n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)
        if self.T > 0:
            n_a = 1 / (np.exp(w_a * 157887.32400374097/self.T) - 1)
            n_b = 1 / (np.exp(w_b * 157887.32400374097/self.T) - 1)

        if verbose:
            print("BE occ number", n_a[:self.n_modes])

        # Apply the non interacting X operator
        start_Y = self.n_modes
        start_A = self.n_modes + N_w2

        if not self.ignore_harmonic:
            if verbose:
                print('DEBUG WIGNER harmonic R(1) a(1) b(1)')
            # Harmonic evolution for R -> R sector
            L_operator[:self.n_modes, :self.n_modes] = +np.diag(self.w**2)

            # Harmonic evolution for a -> a sector
            a_a = w_a**2 + w_b**2 - 2 * w_a * w_b
            L_operator[start_Y: start_A, start_Y: start_A] = +np.diag(a_a)

            # Harmonic evolution for b -> b sector
            b_b = w_a**2 + w_b**2 + 2 * w_a * w_b
            L_operator[start_A:, start_A:] = +np.diag(b_b)


        # We ADDED all the non interacting (harmonic) propagators both for debug Wigner
        # In Wigner the Harmonic approach is working

        # Compute the d3 operator
        if debug_d3 is None:
            N_eff = np.sum(self.rho)
            Y_weighted = np.einsum("ia, i -> ia", self.Y, self.rho)
        if not self.ignore_v3:
            if verbose:
                print("Computing d3...")
            if not debug_d3 is None:
                d3 = debug_d3
            else:
                X_ups = np.einsum("ia, a -> ia", self.X, f_ups(self.w, self.T))

                d3_noperm = np.einsum("ia, ib, ic -> abc", X_ups, X_ups, Y_weighted)
                d3_noperm /= -N_eff 

                # Apply the permuatations
                d3 = d3_noperm.copy()
                d3 += np.einsum("abc->acb", d3_noperm)
                d3 += np.einsum("abc->bac", d3_noperm)
                d3 += np.einsum("abc->bca", d3_noperm)
                d3 += np.einsum("abc->cab", d3_noperm)
                d3 += np.einsum("abc->cba", d3_noperm)
                d3 /= 6

                if verbose:
                    np.save("d3_modes_nosym.npy", d3)

                # Perform the standard symmetrization
                if symmetrize:
                    # TODO: fix the symmetrize_d3_muspace
                    raise NotImplementedError('The symmetrizaiton on d3 is not implemented')
                    d3 = symmetrize_d3_muspace(d3, self.symmetries)

                    if verbose:
                        np.save("d3_modes_sym.npy", d3)
                        np.save("symmetries_modes.npy", self.symmetries)


            # Reshape the d3
            d3_small_space = np.zeros((N_w2, self.n_modes), dtype = np.double)
            # Get the d3 in the small space by getting the independent terms
            d3_small_space[:,:] = d3[new_i_a, new_i_b, :]

            if verbose:
                print("D3 of the following elements:")
                print(new_i_a)
                print(new_i_b)
                print("D3 small space")
                print(d3_small_space)
                print('D3 complete')
                print(d3)

            if verbose:
                print('DEBUG WIGNER getting the D3 contribution')
            # Chi for the independent indeces
            L2_minus = - (2 * (n_a - n_b) * (w_a - w_b)) /(w_a * w_b * (2 * n_a + 1) *  (2 * n_b + 1)) 
            L2_plus  = + (2 * (1 + n_a + n_b) * (w_a + w_b)) /(w_a * w_b * (2 * n_a + 1) *  (2 * n_b + 1)) 
            # X for the independent indices
            X = ((2 * n_a + 1) *  (2 * n_b + 1))/8

            # The shape of these tensors is (N_w2, n_modes) considering double counting
            extra_count_w = np.ones(N_w2, dtype = np.intc)
            extra_count_w[new_i_a != new_i_b] = 2
            d3_X  = np.einsum('ab, a -> ab', d3_small_space, X * extra_count_w)

            # The interacion between a rank 1 tensor a rank 2 tensor DOES require
            # to take into account double counting

            # The coeff betwee R(1) and a(1)
            L_operator[:start_Y, start_Y: start_A] = -d3_X.T
            # The coeff betwee R(1) and b(1)
            L_operator[:start_Y, start_A:] = +d3_X.T

            # The interaction between a rank 2 tensor a rank 1 tensor DOES NOT require
            # to take into account double counting

            # The shape of these tensors is (N_w2, n_modes)
            L2_minus_d3 = np.einsum('ab, a -> ab', d3_small_space, L2_minus)
            L2_plus_d3  = np.einsum('ab, a -> ab', d3_small_space, L2_plus)

            # The coeff betwee a(1) and R(1)
            L_operator[start_Y: start_A, :start_Y] = -L2_minus_d3
            # The coeff betwee b(1) and R(1)
            L_operator[start_A:, :start_Y] = +L2_plus_d3


        if not self.ignore_v4:
#             raise NotImplementedError('The symmetrizaiton on d4 is not implemented for debug Wigner')
            # Get the full D4 tensor in the polarization basis
            # it should be symmetric under permutations of the indices
            d4 =  np.einsum("ia, ib, ic, id -> abcd", X_ups, X_ups, X_ups, Y_weighted)
            d4 += np.einsum("ia, ib, ic, id -> abcd", X_ups, X_ups, Y_weighted, X_ups)
            d4 += np.einsum("ia, ib, ic, id -> abcd", X_ups, Y_weighted, X_ups, X_ups)
            d4 += np.einsum("ia, ib, ic, id -> abcd", Y_weighted, X_ups, X_ups, X_ups)
            d4 /= - 4 * N_eff

            if verbose:
                np.save("d4_modes_nosym.npy", d4)
            if symmetrize:
                raise NotImplementedError('The symmetrizaiton on d4 is not implemented')

            # Get the independent first two indep indices
            d4_small_space1 = np.zeros((N_w2, self.n_modes, self.n_modes), dtype = np.double)
            d4_small_space1[:,:,:] = d4[new_i_a, new_i_b, :, :]

            # Get the independent second two indep indices
            d4_small_space = np.zeros((N_w2, N_w2), dtype = np.double)
            d4_small_space[:,:] = d4_small_space1[:, new_i_a, new_i_b]

            if verbose:
                print("D4 of the following elements:")
                print(new_i_a)
                print(new_i_b)
                print("D4 in the reduced space")
                print(d4_small_space)
                print("D4 complete")
                print(d4)

            # Add the matrix elements
            L2_minus_d4_X = np.einsum('a, ab, b -> ab', L2_minus, d4_small_space, X * extra_count_w)
            L2_plus_d4_X  = np.einsum('a, ab, b -> ab', L2_plus, d4_small_space, X * extra_count_w)

            # Interaction of a(1)-a(1)
            L_operator[start_Y:start_A, start_Y:start_A] += L2_minus_d4_X

            # Interaction of b(1)-b(1)
            L_operator[start_A:, start_A:] += L2_plus_d4_X

            # Interaction of a(1)-b(1)
            L_operator[start_Y:start_A, start_A:] += -L2_minus_d4_X

            # Interaction of b(1)-a(1)
            L_operator[start_A:, start_Y:start_A] += -L2_plus_d4_X


        if verbose:
            print("L DEBUG WIGNER superoperator computed.")
            np.savez_compressed("L_super_analytical_debug_wigner.npz", L_operator)

        if overwrite_L_operator:
            if verbose:
                print('Overwriting the L operator with DEBUG WIGNER..')
            def matvec(x):
                return L_operator.dot(x)
            def rmatvec(x):
                return x.dot(L_operator)

            self.L_linop = scipy.sparse.linalg.LinearOperator(L_operator.shape, matvec = matvec, rmatvec = rmatvec)

        return L_operator

get_static_frequency(smearing=0)

GET THE STATIC FREQUENCY

The static frequency of a specific perturbation can be obtained as the limit of the dynamical green function for w -> 0.

.. math ::

\omega = \sqrt{\frac{1}{\Re G(\omega \rightarrow 0 + i\eta)}}

where :math:\eta is the smearing for the static frequency calculation. This frequency is the diagonal element of the free energy Hessian matrix acros the chosen perturbation.

If :math:\omega is imaginary, a negative value is returned.

Results
- frequency : float
    The frequency of the perturbation :math:`\omega`
Source code in tdscha/DynamicalLanczos.py
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
def get_static_frequency(self, smearing: np.float64 = 0) -> np.float64:
    r"""
    GET THE STATIC FREQUENCY
    ========================

    The static frequency of a specific perturbation can be obtained as the limit of the 
    dynamical green function for w -> 0. 

    .. math ::

        \omega = \sqrt{\frac{1}{\Re G(\omega \rightarrow 0 + i\eta)}} 


    where :math:`\eta` is the smearing for the static frequency calculation.
    This frequency is the diagonal element of the free energy Hessian matrix acros the chosen perturbation.

    If :math:`\omega` is imaginary, a negative value is returned.

    Parameters
    ----------
        - smearing : float
            The smearing in Ry of the calculation

    Results
    -------
        - frequency : float
            The frequency of the perturbation :math:`\omega`
    """



    gf = self.get_green_function_continued_fraction(np.array([0]), False, smearing = smearing)

    w2 = 1 / np.real(gf)
    return np.float64(np.sqrt(np.abs(w2)) * np.sign(w2))

get_full_L_operator(verbose=False, only_pert=False, symmetrize=True, debug_d3=None, overwrite_L_operator=True)

GET THE FULL L OPERATOR

Use this method to test everithing. I returns the full L operator as a matrix. It is very memory consuming, but it should be fast and practical for small systems.

Results

L_op : ndarray(size = (nmodes * (nmodes + 1)), dtype = TYPE_DP) The full L operator.

Source code in tdscha/DynamicalLanczos.py
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
    def get_full_L_operator(self, verbose = False, only_pert = False, symmetrize = True, debug_d3 = None, overwrite_L_operator = True):
        """
        GET THE FULL L OPERATOR
        =======================

        Use this method to test everithing. I returns the full L operator as a matrix.
        It is very memory consuming, but it should be fast and practical for small systems.


        Results
        -------
           L_op : ndarray(size = (nmodes * (nmodes + 1)), dtype = TYPE_DP)
              The full L operator.
        """
        # The L operator
        L_operator = np.zeros( shape = (self.n_modes + self.n_modes * self.n_modes, self.n_modes + self.n_modes * self.n_modes), dtype = TYPE_DP)

        # Fill the first part with the standard dynamical matrix
        if not only_pert:
            L_operator[:self.n_modes, :self.n_modes] = np.diag(self.w**2)


        w_a = np.tile(self.w, (self.n_modes,1)).ravel()
        w_b = np.tile(self.w, (self.n_modes,1)).T.ravel()
        chi_beta = -.5 * np.sqrt(w_a + w_b)/(np.sqrt(w_a)*np.sqrt(w_b))


        B_mat = (w_a + w_b)**2
        if not only_pert:
            L_operator[self.n_modes:, self.n_modes:] = np.diag(B_mat)


        # Compute the d3 operator
#         new_X = np.einsum("ia,a->ai", self.X, f_ups(self.w, self.T))
        if debug_d3 is None:
            N_eff = np.sum(self.rho)
            Y_weighted = np.einsum("ia, i->ia", self.Y, self.rho)
            #new_Y = np.einsum("ia,i->ai", self.Y, self.rho)

        if not self.ignore_v3:
            if not debug_d3 is None:
                d3 = debug_d3
            else:
                if verbose:
                    print("Computing d3...")
                d3_noperm = np.einsum("ia,ib,ic->abc", self.X, self.X, Y_weighted)
                d3_noperm /= -N_eff 

                # Apply the permuatations
                d3 = d3_noperm.copy()
                d3 += np.einsum("abc->acb", d3_noperm)
                d3 += np.einsum("abc->bac", d3_noperm)
                d3 += np.einsum("abc->bca", d3_noperm)
                d3 += np.einsum("abc->cab", d3_noperm)
                d3 += np.einsum("abc->cba", d3_noperm)
                d3 /= 6

                if verbose:
                    np.save("d3_modes_nosym.npy", d3)

                if symmetrize:
                    # Perform the standard symmetrization
                    d3 = symmetrize_d3_muspace(d3, self.symmetries)

                if verbose:
                    np.save("d3_modes_sym.npy", d3)
                    np.save("symmetries_modes.npy", self.symmetries)


            # Reshape the d3
            d3_reshaped = d3.reshape((self.n_modes, self.n_modes * self.n_modes))

            new_mat = np.einsum("ab,b->ab", d3_reshaped, chi_beta)

            L_operator[:self.n_modes, self.n_modes:] = new_mat
            L_operator[self.n_modes:, :self.n_modes] = new_mat.T

        if not self.ignore_v4:
            if verbose:
                print("Computing d4...")
            d4 =  np.einsum("ai,bi,ci,di", new_X, new_X, new_X, new_Y)
            d4 += np.einsum("ai,bi,ci,di", new_X, new_X, new_Y, new_X)
            d4 += np.einsum("ai,bi,ci,di", new_X, new_Y, new_X, new_X)
            d4 += np.einsum("ai,bi,ci,di", new_Y, new_X, new_X, new_X)
            d4 /= - 4 * N_eff

            if verbose:
                np.save("d4_modes_nosym.npy", d4)

            # Reshape the d4
            d4_reshaped = d4.reshape((self.n_modes * self.n_modes, self.n_modes * self.n_modes))

            new_mat = np.einsum("ab,a,b->ab", d4_reshaped, chi_beta, chi_beta)

            L_operator[self.n_modes:, self.n_modes:] += new_mat

        if verbose:
            print("L superoperator computed.")

        if overwrite_L_operator:
            print('overwriting the L operator')
            self.L_linop = L_operator

        return L_operator

get_full_L_operator_FT(verbose=False, debug_d3=None, symmetrize=True, overwrite_L_operator=True)

GET THE FULL L OPERATOR (FINITE TEMPERATURE)

Get the the full matrix L for the biconjugate Lanczos algorithm. Use this method to test everithing. I returns the full L operator as a matrix. It is very memory consuming, but it should be fast for small systems.

Maybe we need to drop the exchange between a,b because they are symmetric by definition. The double counting for the exchange of a,b is now fixed.

Now if self.use_harmonic == True we can compute the Wigner Lanczos matrix

Results

L_op : ndarray(size = (nmodes * (2*nmodes + 1)), dtype = TYPE_DP) The full L operator.

Source code in tdscha/DynamicalLanczos.py
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
def get_full_L_operator_FT(self, verbose = False, debug_d3 = None, symmetrize = True, overwrite_L_operator = True):
    """
    GET THE FULL L OPERATOR (FINITE TEMPERATURE)
    ============================================

    Get the the full matrix L for the biconjugate Lanczos algorithm.
    Use this method to test everithing. I returns the full L operator as a matrix.
    It is very memory consuming, but it should be fast for small systems.

    Maybe we need to drop the exchange between a,b because they are symmetric by definition.
    The double counting for the exchange of a,b is now fixed.

    Now if self.use_harmonic == True we can compute the Wigner Lanczos matrix

    Results
    -------
       L_op : ndarray(size = (nmodes * (2*nmodes + 1)), dtype = TYPE_DP)
          The full L operator.
    """
    # The elements where w_a and w_b are exchanged are dependent
    # So we must avoid including them
    i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
    i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

    new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
    new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

    w_a = self.w[new_i_a]
    w_b = self.w[new_i_b]

    # N_w2 is the number of independent indeces
    N_w2 = len(w_a)

    if verbose:
        print()
        print('Getting the analytical L operator')
        print('Am I using Wigner? {}'.format(self.use_wigner))
    # Prepare the operator
    L_operator = np.zeros(shape = (self.n_modes + 2*N_w2, self.n_modes + 2*N_w2), dtype = TYPE_DP)

    # Set the Z'' harmonic
    if not self.ignore_harmonic:
        if not self.use_wigner:
            if verbose:
                print('STANDARD harmonic on R(1)')
            L_operator[:self.n_modes, :self.n_modes] = np.diag(self.w**2)
        else:
            if verbose:
                print('WIGNER harmonic on R(1)')
            L_operator[:self.n_modes, :self.n_modes] = -np.diag(self.w**2)

    #w_a = np.tile(self.w, (self.n_modes,1)).ravel()
    #w_b = np.tile(self.w, (self.n_modes,1)).T.ravel()

    n_a = np.zeros(np.shape(w_a), dtype = TYPE_DP)
    n_b = np.zeros(np.shape(w_a), dtype = TYPE_DP)
    if self.T > 0:
        n_a = 1 / (np.exp(w_a * 157887.32400374097/self.T) - 1)
        n_b = 1 / (np.exp(w_b * 157887.32400374097/self.T) - 1)

    if verbose:
        print("BE occ number", n_a[:self.n_modes])

    # Apply the non interacting X operator
    start_Y = self.n_modes
    start_A = self.n_modes + N_w2

    # Get the operator that exchanges the frequencies
    # For each index i (a,b), exchange_frequencies[i] is the index that correspond to (b,a)
    #exchange_frequencies = np.array([ (i // self.n_modes) + self.n_modes * (i % self.n_modes) for i in np.arange(self.n_modes**2)])
    #xx = np.tile(np.arange(self.n_modes), (self.n_modes, 1)).T.ravel()
    #yy = np.tile(np.arange(self.n_modes), (self.n_modes, 1)).ravel()
    #all_modes = np.arange(self.n_modes**2)
    #exchange_frequencies = xx + yy
    if not self.ignore_harmonic:
        if not self.use_wigner:
            if verbose:
                print('STANDARD harmonic Y(1) ReA(1)')    
            # NOTE the double counting is NOT required for harmonic propagation
            # just check the harmonic matrix element
            extra_count = np.ones(N_w2, dtype = np.intc)
            extra_count[new_i_a == new_i_b] = 1.

            # Harmonic evolution for Y -> Y sector
            X_ab_NI = -w_a**2 - w_b**2 - (2*w_a *w_b) /((2*n_a + 1) * (2*n_b + 1))
            L_operator[start_Y: start_A, start_Y:start_A] = - np.diag(X_ab_NI)  * extra_count
            #L_operator[start_Y + np.arange(self.n_modes**2) , start_Y + exchange_frequencies] -= X_ab_NI / 2

            # Harmonic evolution for Y -> ReA sector
            Y_ab_NI = - (8 * w_a * w_b) / ((2*n_a + 1) * (2*n_b + 1))
            L_operator[start_Y : start_A, start_A:] = - np.diag(Y_ab_NI) * extra_count
            #L_operator[start_Y + np.arange(self.n_modes**2), start_A + exchange_frequencies] -=  Y_ab_NI / 2


            # Harmonic evolution for ReA -> Y sector
            X1_ab_NI = - (2*n_a*n_b + n_a + n_b) * (2*n_a*n_b + n_a + n_b + 1)*(2 * w_a * w_b) / ( (2*n_a + 1) * (2*n_b + 1))
            L_operator[start_A:, start_Y : start_A] = - np.diag(X1_ab_NI) / 1 * extra_count
            #L_operator[start_A + np.arange(self.n_modes**2), start_Y + exchange_frequencies] -= X1_ab_NI / 2

            # Harmonic evolution for ReA -> ReA sector
            Y1_ab_NI = - w_a**2 - w_b**2 + (2*w_a *w_b) /( (2*n_a + 1) * (2*n_b + 1))
            L_operator[start_A:, start_A:] = -np.diag(Y1_ab_NI) / 1 * extra_count
            #L_operator[start_A + np.arange(self.n_modes**2),  start_A + exchange_frequencies] -= Y1_ab_NI / 2
        else:
            # The harmonic application in Wigner is diagonal
            # NOTE the double counting is NOT required here
            # just check the harmonic matrix element
            if verbose:
                print("WIGNER harmonic a'(1) b'(1)")
            # Diagonal harmonic propagation in Winger for a(1)
            a_NI  = -(w_a**2 + w_b**2 - 2 * w_a * w_b)
            L_operator[start_Y: start_A, start_Y: start_A] = + np.diag(a_NI)  

            # Diagonal harmonic propagation in Winger for b(1)
            b_NI = -(w_a**2 + w_b**2 + 2. * w_a * w_b)
            L_operator[start_A:, start_A:] = + np.diag(b_NI)


    # We ADDED all the non interacting (harmonic) propagators both for standard and Wigner
    # In WIgner the Harmonic approach is working

    # Compute the d3 operator

    #new_X = np.einsum("ia,a->ai", self.X, f_ups(self.w, self.T))
    if debug_d3 is None:
        N_eff = np.sum(self.rho)
        Y_weighted = np.einsum("ia, i -> ia", self.Y, self.rho)
    #new_Y = np.einsum("ia,i->ai", self.Y, self.rho)

    if not self.ignore_v3:
        if verbose:
            print("Computing d3...")
        if not debug_d3 is None:
            d3 = debug_d3
        else:
            X_ups = np.einsum("ia, a -> ia", self.X, f_ups(self.w, self.T))

            d3_noperm = np.einsum("ia, ib, ic -> abc", X_ups, X_ups, Y_weighted)
            d3_noperm /= -N_eff 

            # Apply the permuatations
            d3 = d3_noperm.copy()
            d3 += np.einsum("abc->acb", d3_noperm)
            d3 += np.einsum("abc->bac", d3_noperm)
            d3 += np.einsum("abc->bca", d3_noperm)
            d3 += np.einsum("abc->cab", d3_noperm)
            d3 += np.einsum("abc->cba", d3_noperm)
            d3 /= 6

            if verbose:
                np.save("d3_modes_nosym.npy", d3)

            # Perform the standard symmetrization
            if symmetrize:
                # TODO: fix the symmetrize_d3_muspace
                raise NotImplementedError('The symmetrizaiton on d3 is not implemented')
                d3 = symmetrize_d3_muspace(d3, self.symmetries)

                if verbose:
                    np.save("d3_modes_sym.npy", d3)
                    np.save("symmetries_modes.npy", self.symmetries)


        # Reshape the d3
        d3_small_space = np.zeros((N_w2, self.n_modes), dtype = np.double)
        # Get the d3 in the small space by getting the independent terms
        d3_small_space[:,:] = d3[new_i_a, new_i_b, :]

        if verbose:
            print("D3 of the following elements:")
            print(new_i_a)
            print(new_i_b)
            print("D3 small space")
            print(d3_small_space)
            print('D3 complete')
            print(d3)

        #d3_reshaped = d3.reshape((self.n_modes* self.n_modes, self.n_modes))
        #d3_reshaped1 = d3.reshape((self.n_modes, self.n_modes* self.n_modes))

        if not self.use_wigner:
            if verbose:
                print('STANDARD getting the D3 contribution')
            # Get the Z coeff between Y with R
            Z_coeff = 2 * ((2*n_a + 1)*w_b + (2*n_b + 1)*w_a) / ((2*n_a + 1) * (2*n_b + 1))
            Z_coeff = np.einsum("ab, a -> ab", d3_small_space, Z_coeff)
            L_operator[start_Y: start_A, :start_Y] = -Z_coeff

            # Get the Z' coeff between ReA and R
            Z1_coeff = 2 *((2*n_a + 1)*w_b*n_b*(n_b + 1) + (2*n_b + 1)*w_a*n_a*(n_a+1)) / ((2*n_a + 1) * (2*n_b + 1))
            Z1_coeff = np.einsum("ab, a -> ab", d3_small_space, Z1_coeff)
            L_operator[start_A:, :start_Y] = - Z1_coeff

            # Get the X'' coeff between R and Y with double counting
            extra_count = np.ones(N_w2, dtype = np.intc)
            extra_count[new_i_a != new_i_b] = 2
            X2_coeff = (2*n_b + 1) * (2*n_a +1) / (8*w_a *w_b)
            X2_coeff = np.einsum("ab, a -> ba", d3_small_space, X2_coeff * extra_count)
            L_operator[:start_Y, start_Y: start_A] = -X2_coeff

            # The coeff between R and ReA is zero.
        else:
            if verbose:
                print('WIGNER getting the D3 contribution')
            # Chi for the independent indeces
            chi_minus = ((n_a - n_b) * (w_a - w_b) /(2 * w_a * w_b)) 
            chi_plus  = ((1 + n_a + n_b) * (w_a + w_b) /(2 * w_a * w_b))

            # The shape of these tensors is (N_w2, n_modes) considering double counting
            extra_count_w = np.ones(N_w2, dtype = np.intc)
            extra_count_w[new_i_a != new_i_b] = 2
            d3_chi_plus  = np.einsum('ab, a -> ab', d3_small_space, np.sqrt(+0.5 * chi_plus)  * extra_count_w)
            d3_chi_minus = np.einsum('ab, a -> ab', d3_small_space, np.sqrt(-0.5 * chi_minus) * extra_count_w)

            # The interacion between a rank 1 tensor a rank 2 tensor DOES require
            # to take into account double counting

            # The coeff betwee R'(1) and a'(1)
            L_operator[:start_Y, start_Y: start_A] = +d3_chi_minus.T
            # The coeff betwee R'(1) and b'(1)
            L_operator[:start_Y, start_A:] = -d3_chi_plus.T

            # The interacion between a rank 2 tensor a rank 1 tensor DOES NOT require
            # to take into account double counting

            # The shape of these tensors is (N_w2, n_modes)
            chi_plus_d3  = np.einsum('ab, a -> ab', d3_small_space, np.sqrt(+0.5 * chi_plus))
            chi_minus_d3 = np.einsum('ab, a -> ab', d3_small_space, np.sqrt(-0.5 * chi_minus))

            # The coeff betwee a'(1) and R'(1)
            L_operator[start_Y: start_A, :start_Y] = +chi_minus_d3
            # The coeff betwee b'(1) and R'(1)
            L_operator[start_A:, :start_Y] = -chi_plus_d3 


    if not self.ignore_v4:
        # Get the D4 tensor in the polarization basis
        # it should be symmetric under permutations of the indices
        d4 =  np.einsum("ia, ib, ic, id -> abcd", X_ups, X_ups, X_ups, Y_weighted)
        d4 += np.einsum("ia, ib, ic, id -> abcd", X_ups, X_ups, Y_weighted, X_ups)
        d4 += np.einsum("ia, ib, ic, id -> abcd", X_ups, Y_weighted, X_ups, X_ups)
        d4 += np.einsum("ia, ib, ic, id -> abcd", Y_weighted, X_ups, X_ups, X_ups)
        d4 /= - 4 * N_eff

        if verbose:
            np.save("d4_modes_nosym.npy", d4)

        # TODO: add the standard symmetrization
        if symmetrize:
            raise NotImplementedError('The symmetrizaiton on d4 is not implemented')

        # Get the independent first two indep indices
        d4_small_space1 = np.zeros((N_w2, self.n_modes, self.n_modes), dtype = np.double)
        d4_small_space1[:,:,:] = d4[new_i_a, new_i_b, :, :]

        # Get the independent second two indep indices
        d4_small_space = np.zeros((N_w2, N_w2), dtype = np.double)
        d4_small_space[:,:] = d4_small_space1[:, new_i_a, new_i_b]

        if verbose:
            print("D4 of the following elements:")
            print(new_i_a)
            print(new_i_b)
            print("D4 in the reduced space")
            print(d4_small_space)
            print("D4 complete")
            print(d4)

        if not self.use_wigner:
            if verbose:
                print('STANDARD getting the D4 contribution')
            # When two tensors of rank 2 interact we need a factor of 2 overall
            extra_count = np.ones(N_w2, dtype = np.intc)
            extra_count[new_i_a != new_i_b] = 2

            # Anharmonic interaction of Y(1) with Y(1)
            X_coeff_left = -((2 * w_a * n_b + 2 * w_b * n_a + w_a + w_b)) /(4 * (2*n_a + 1) * (2*n_b + 1))
            X_coeff_right = ((2 * n_a + 1) * (2 * n_b + 1)) /(w_a * w_b)
            X_coeff = np.einsum('a, ab, b -> ab', X_coeff_left, d4_small_space, X_coeff_right * extra_count)
            L_operator[start_Y:start_A, start_Y:start_A] += -X_coeff

            # Anharmonic interaction of ReA(1) with Y(1)
            X1_coeff_left = -(w_a * n_a * (n_a + 1) * (2 * n_b + 1) + w_b * n_b * (n_b + 1) * (2 * n_a + 1)) /(4 * (2*n_a + 1) * (2*n_b + 1))
            X1_coeff_right = ((2 * n_a + 1) * (2 * n_b + 1))/(w_a * w_b)
            X1_coeff = np.einsum('a, ab, b -> ab', X1_coeff_left, d4_small_space, X1_coeff_right * extra_count)
            L_operator[start_A:, start_Y:start_A] += -X1_coeff
        else:
            if verbose:
                print('WIGNER getting the D4 contribution')
            # When two tensors of rank 2 interact we need a factor of 2 overall
            extra_count_w = np.ones(N_w2, dtype = np.intc)
            extra_count_w[new_i_a != new_i_b] = 2

            chi_plus  = ((1 + n_a + n_b) * (w_a + w_b) /(2 * w_a * w_b))
            chi_minus  = ((n_a - n_b) * (w_a - w_b) /(2 * w_a * w_b))

            plus_D4_plus   = np.einsum('a, ab, b -> ab', np.sqrt(+0.5 * chi_plus),  d4_small_space, np.sqrt(+0.5 * chi_plus) * extra_count_w)
            minus_D4_minus = np.einsum('a, ab, b -> ab', np.sqrt(-0.5 * chi_minus), d4_small_space, np.sqrt(-0.5 * chi_minus) * extra_count_w)
            minus_D4_plus  = np.einsum('a, ab, b -> ab', np.sqrt(-0.5 * chi_minus), d4_small_space, np.sqrt(+0.5 * chi_plus) * extra_count_w)
            plus_D4_minus  = np.einsum('a, ab, b -> ab', np.sqrt(+0.5 * chi_plus) , d4_small_space, np.sqrt(-0.5 * chi_minus) * extra_count_w)

            # Anharmonic interaction of a'(1) and a'(1)
            L_operator[start_Y: start_A, start_Y: start_A] += -minus_D4_minus

            # Anharmonic interaction of b'(1) and b'(1)
            L_operator[start_A:, start_A:] += -plus_D4_plus

            # Anharmonic interaction of a'(1) and b'(1)
            L_operator[start_Y: start_A, start_A :] += +minus_D4_plus

            # Anharmonic interaction of b'(1) and a'(1)
            L_operator[start_A :, start_Y: start_A] += +plus_D4_minus

    if verbose:
        print("L superoperator computed.")
        if not self.use_wigner:
            np.savez_compressed("L_super_analytical_standard.npz", L_operator)
        else:
            np.savez_compressed("L_super_analytical_wigner.npz", L_operator)

    if overwrite_L_operator:
        if verbose:
            print('Overwriting the L operator..')
            print('Am I using Wigner? {}'.format(self.use_wigner))
        def matvec(x):
            return L_operator.dot(x)
        def rmatvec(x):
            return x.dot(L_operator)

        self.L_linop = scipy.sparse.linalg.LinearOperator(L_operator.shape, matvec = matvec, rmatvec = rmatvec)

    return L_operator

mask_dot_wigner(debug=False)

Builds a mask in order to do a symmetric Lanczos.

The Lanczos is symmetric in the basis where we store all the matrices, so when we run a Lanczos in all the scalar product we need to take into account a double counting for the dependent indeces

Returns:
-double_mask: nd.array with size = n_modes + n_modes * (n_modes + 1)
Source code in tdscha/DynamicalLanczos.py
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
def mask_dot_wigner(self, debug = False):
    """
    Builds a mask in order to do a symmetric Lanczos.

    The Lanczos is symmetric in the basis where we store all the matrices, so
    when we run a Lanczos in all the scalar product we need to take into account
    a double counting for the dependent indeces

    Returns:
    --------
        -double_mask: nd.array with size = n_modes + n_modes * (n_modes + 1)
    """
    # Prepare the result
    double_mask = np.ones((self.n_modes + self.n_modes * (self.n_modes + 1)))

    # Where a'(1) and b'(1) starts
    start_a = self.n_modes
    start_b = self.n_modes + (self.n_modes * (self.n_modes + 1))//2

    # Get the indep indices
    i_a = np.tile(np.arange(self.n_modes), (self.n_modes,1)).ravel()
    i_b = np.tile(np.arange(self.n_modes), (self.n_modes,1)).T.ravel()

    # Avoid the exchange of indices
    new_i_a = np.array([i_a[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])
    new_i_b = np.array([i_b[i] for i in range(len(i_a)) if i_a[i] >= i_b[i]])

    if debug:
        print()
        print("start_a'(1) = ", start_a)
        print("start_b'(1) = ", start_b)
        print('new_i_b')
        print(new_i_b)
        print('new_i_a')
        print(new_i_a)

    # Where we have dep indices insert a 2 for double ocunting
    double_mask[start_a: start_b][new_i_b < new_i_a] = 2
    double_mask[start_b:][new_i_b < new_i_a] = 2

    if debug:
        print('mask dot prod for R(1) = ')
        print(double_mask[:start_a])
        print("mask dot prod for a'(1) = ")
        print(double_mask[start_a: start_b])
        print("mask dot prod for b'(1) = ")
        print(double_mask[start_b:])
        print()

    return double_mask

run_FT(n_iter, save_dir=None, save_each=5, verbose=True, n_rep_orth=0, n_ortho=10, flush_output=True, debug=False, prefix='LANCZOS', run_simm=None, optimized=False)

RUN LANCZOS ITERATIONS FOR FINITE TEMPERATURE

This method performs the biconjugate Lanczos algorithm to find the sequence of a and b and c coefficients that are the tridiagonal representation of the L matrix to be inverted.

NOTE: when we use the Wigner formalism the Lanczos matrix is symmetric in the vector space where all the elements of the tensors are considered (also those that are related by symmetry). Since the application of L is done in the reduced space where we discart these elements we have to take into account this by multiplying by two the off diagonal components in the scalar products.

Source code in tdscha/DynamicalLanczos.py
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
    def run_FT(self, n_iter, save_dir = None, save_each = 5, verbose = True, n_rep_orth = 0, n_ortho = 10, flush_output = True, debug = False, prefix = "LANCZOS", run_simm = None, optimized = False):
        """
        RUN LANCZOS ITERATIONS FOR FINITE TEMPERATURE
        =============================================

        This method performs the biconjugate Lanczos algorithm to find
        the sequence of a and b and c coefficients that are the tridiagonal representation 
        of the L matrix to be inverted.

        NOTE: when we use the Wigner formalism the Lanczos matrix is symmetric in the vector space where all the elements
        of the tensors are considered (also those that are related by symmetry). Since the application of L
        is done in the reduced space where we discart these elements we have to take into
        account this by multiplying by two the off diagonal components in the scalar products.

        Parameters
        ----------
            n_iter : int
                The number of iterations to be performed in the Lanczos algorithm.
            save_dir : string
                The directory in which you want to store the results step by step,
                in order to do a preliminar analysis or restart the calculation later.
                If None (default), the steps are not saved.
            save_each : int
                If save dir is not None, the results are saved each N step, with N the value of save_each argument.
            verbose : bool
                If true all the info during the minimization will be printed on output.
            n_rep_orth : int
                The number of times in which the GS orthonormalization is repeated.
                The higher, the lower the precision of the Lanczos step, the lower, the higher
                the probability of finding ghost states
            n_ortho : int
                The number of vectors to be considered for the GS biorthogonalization. (if None, all are considered)
            flush_output : bool
                If true it flushes the output at each step. 
                This is usefull to avoid ending without any output if a calculation is killed before it ends normally.
                However, it could slow down things a bit on clusters.
            debug : bool
                If true prints a lot of more info about the Lanczos
                as the gram-shmidth procdeure and checks on the coefficients. 
                This is usefull to spot an error or the appeareance of ghost states due to numerical inaccuracy.
            run_simm : bool
                If true the biconjugate Lanczos is transformed in a simple Lanczos with corrections in the scalar product. This is only possible if we are using the Wigner representation, where the Lanczos matrix is symmetric in the full space.
            optimized : bool
                If True we pop the vectors P and Q that we do not use during the Lanczos
        """

        self.verbose = verbose
        # Check if the symmetries has been initialized
        if not self.initialized:
            if verbose:
                print('Not initialized. Now we symmetrize\n')
            self.prepare_symmetrization()

        # Check if the psi vector is prepared
        ERROR_MSG = """
Error, you must initialize a perturbation to start the Lanczos.
Use prepare_raman/ir or prepare_perturbation before calling the run method.
"""
        if self.psi is None:
            print(ERROR_MSG)
            raise ValueError(ERROR_MSG)

        psi_norm = np.sum(self.psi**2)
        if np.isnan(psi_norm) or psi_norm == 0:
            print(ERROR_MSG)
            raise ValueError(ERROR_MSG)

        # If save_dir does not exist, create it
        if Parallel.am_i_the_master():
            if save_dir is not None:
                if not os.path.exists(save_dir):
                    os.makedirs(save_dir)

        # Automatically use the symmetric Lanczos if we are using the Wigner representation
        if run_simm is None:
            run_simm = self.use_wigner

        # run_simm is allowed only if we use the wigner representation
        if run_simm and not self.use_wigner:
            raise NotImplementedError('The symmetric Lanczos works only with Wigner. Set use_wigner to True and make sure that you are not using the analytic wigner L!')


        # Getting the mask product for the Wigner implementation
        if run_simm:
            if verbose:
                print('Running the standard Lanczos algorithm with Wigner')
                print('Getting the mask dot product')
                print()
            mask_dot = self.mask_dot_wigner(debug)



        # Get the current step
        i_step = len(self.a_coeffs)

        if verbose:
            header = """
<=====================================>
|                                     |
|          LANCZOS ALGORITHM          |
|                                     |
<=====================================>

Starting the algorithm. It may take a while.
Starting from step %d
""" % i_step
            print(header)

            OPTIONS = """
Should I ignore the third order effect? {}
Should I ignore the fourth order effect? {}
Should I use the Wigner formalism? {}
Should I use a standard Lanczos? {}
Max number of iterations: {}
""".format(self.ignore_v3, self.ignore_v4, self.use_wigner, run_simm, n_iter)
            print(OPTIONS)


        # If this is the current step initialize the algorithm
        if i_step == 0:
            self.basis_Q = []
            self.basis_P = []
            self.s_norm = []
            # Normalize the first vector in the Standard or Wigner representation
            if not run_simm:
                first_vector = self.psi / np.sqrt(self.psi.dot(self.psi))
            else:
                first_vector = self.psi / np.sqrt(self.psi.dot(self.psi * mask_dot))
            self.basis_Q.append(first_vector)
            self.basis_P.append(first_vector)
            self.s_norm.append(1)
        else:
            print('Restarting the Lanczos')
            print('There is no control on the len of basis_Q')
            # Convert everything in a list
            self.basis_Q = list(self.basis_Q)
            self.basis_P = list(self.basis_P)
            self.s_norm  = list(self.s_norm)
            self.a_coeffs = list(self.a_coeffs)
            self.b_coeffs = list(self.b_coeffs)
            self.c_coeffs = list(self.c_coeffs)
            #self.arnoldi_matrix = list(self.arnoldi_matrix)


            # if len(self.basis_Q) != i_step + 1:
            #     print("Krilov dim: %d, number of steps perfomed: %d" % (len(self.basis_Q), i_step))
            #     print("Error, the Krilov basis dimension should be 1 more than the number of steps")
            #     raise ValueError("Error the starting krilov basis does not matches the matrix, Look stdout.")

        assert len(self.basis_Q) == len(self.basis_P), "Something wrong when restoring the Lanczos."
        assert len(self.s_norm) == len(self.basis_P), "Something wrong when restoring the Lanczos."
        assert len(self.b_coeffs) == len(self.c_coeffs), "Something wrong when restoring the Lanczos. len b = {} len c = {}".format(len(self.b_coeffs), len(self.c_coeffs))


        # Select the two vectors for the biconjugate Lanczos iterations
        psi_q = self.basis_Q[-1]
        psi_p = self.basis_P[-1]

        if debug:
            print("Q basis:", self.basis_Q)
            print("P basis:", self.basis_P)
            print("S norm:", self.s_norm)
            print("SHAPE PSI Q, P :", psi_q.shape, psi_p.shape)

        # Convergence flag
        next_converged = False

        # Here starts the Lanczos
        for i in range(i_step, i_step + n_iter):
            if verbose:
                step_txt = """
 ===== NEW STEP %d =====

 """ % (i + 1)
                print(step_txt)
                print("Length of the coefficiets: a = {}, b = {}".format(len(self.a_coeffs), len(self.b_coeffs)))
                print()

                if flush_output:
                    sys.stdout.flush()

            # Application of L
            t1 = time.time()
            if not self.use_wigner:
                if verbose:
                    print("Running the BICONJUGATE Lanczos with standard representation!\n")
                    print()
                L_q = self.L_linop.matvec(psi_q)
                # psi_p is normalized (this must be considered when computing c coeff) 
                p_L = self.L_linop.rmatvec(psi_p) 
            else:
                if verbose:
                    print("The Wigner representation is used!\n")
                    print()
                # Get the application on psi_q
                L_q = self.L_linop.matvec(psi_q)
                if run_simm:
                    # This is done because we are running the symmetric Lanczos q=p
                    if verbose:
                        print()
                        print("Running the SYMMETRIC Lanczos with Wigner!\n")
                    p_L = np.copy(L_q)
                else:
                    # This should be done only with the analytical Wigner Matrix only for testing
                    # This should be done in the case q != p
                    if verbose:
                        print()
                        print("Running the BICONJUGATE Lanczos with Wigner analytic!\n")
                    p_L = self.L_linop.rmatvec(psi_p)   
            t2 = time.time()
            # End of L application

            if debug:
                if not run_simm:
                    print("Modulus of L_q: {}".format(np.sqrt(L_q.dot(L_q))))
                    print("Modulus of p_L: {}".format(np.sqrt(p_L.dot(p_L))))
                else:
                    print("Modulus of L_q: {}".format(np.sqrt(L_q.dot(L_q * mask_dot))))
                    print("Modulus of p_L: {}".format(np.sqrt(p_L.dot(p_L * mask_dot))))

            # Get the normalization of p_k (with respect to s_k)
            c_old = 1
            if len(self.c_coeffs) > 0:
                c_old = self.c_coeffs[-1]
            p_norm = self.s_norm[-1] / c_old
            if debug:
                print("p_norm: {}".format(p_norm))

            # Get the a coefficient
            if not run_simm:
                a_coeff = psi_p.dot(L_q) * p_norm
            else:
                a_coeff = psi_p.dot(L_q * mask_dot) * p_norm

            # Check if something whent wrong
            if np.isnan(a_coeff):
                ERR_MSG = """
Invalid value encountered during the Lanczos.
Check if you have correctly initialized the algorithm.
This may happen if the SCHA matrix has imaginary or zero frequencies,
or if the acoustic sum rule is not satisfied.
"""
                raise ValueError(ERR_MSG)    

            # Get the two residual vectors
            rk = L_q - a_coeff * psi_q
            # If this is not the first step
            if len(self.basis_Q) > 1:
                rk -= self.c_coeffs[-1] * self.basis_Q[-2]

            sk = p_L - a_coeff * psi_p 
            old_p_norm = 0
            # If this is not the first step
            if len(self.basis_P) > 1:
                # Get the multiplication factor to rescale the old p to the normalization of the new one.
                if len(self.c_coeffs) < 2:
                    old_p_norm = self.s_norm[-2]
                else:
                    old_p_norm = self.s_norm[-2] / self.c_coeffs[-2] 
                    # C is smaller than s_norm as it does not contain the first vector
                    # But this does not matter as we are counting from the end of the array

                # TODO: Check whether it better to use this or the default norms to update sk
                sk -= self.b_coeffs[-1] * self.basis_P[-2] * (old_p_norm / p_norm)

            # Get the normalization of sk 
            if not run_simm:
                s_norm = np.sqrt(sk.dot(sk))
            else:
                s_norm = np.sqrt(sk.dot(sk * mask_dot))

            # This normalization regularizes the Lanczos
            sk_tilde = sk / s_norm 
            # Add the p normalization of L^t p that was divided from the s_k
            s_norm *= p_norm 

            # Get the b and c coeffs
            if not run_simm:
                b_coeff = np.sqrt(rk.dot(rk))
                c_coeff = (sk_tilde.dot(rk / b_coeff)) * s_norm 
            else:
                b_coeff = np.sqrt(rk.dot(rk * mask_dot))
                c_coeff = (sk_tilde.dot((rk / b_coeff) * mask_dot)) * s_norm 


            if debug:
                print("new_p_norm: {}".format(s_norm / c_coeff))
                print("old_p_norm: {}".format(old_p_norm))

                print("Modulus of rk: {}".format(b_coeff))
                if not run_simm:
                    print("Modulus of sk: {}".format(np.sqrt(sk.dot(sk))))
                else:
                    print("Modulus of sk: {}".format(np.sqrt(sk.dot(sk * mask_dot))))

                if verbose:
                    print("Direct computation resulted in:")
                    print("     |  a = {}".format(a_coeff))
                    print("     |  b = {}".format(b_coeff))
                    print("     |  c = {}".format(c_coeff))
                    if run_simm:
                        print("     |  |b-c| = {}".format(np.abs(b_coeff - c_coeff)))

            # Check the convergence
            self.a_coeffs.append(a_coeff)
            if np.abs(b_coeff) < __EPSILON__ or next_converged:
                if verbose:
                    print("Converged (b coefficient is {}, |b| < {})".format(b_coeff, __EPSILON__))
                converged = True
                break 
            if np.abs(c_coeff) < __EPSILON__ or next_converged:
                if verbose:
                    print("Converged (c coefficient is {}, |c| < {})".format(c_coeff, __EPSILON__))
                converged = True
                break


            # Get the vectors for the next iteration
            psi_q = rk / b_coeff

            # psi_p is the normalized p vector, the sk_tilde one
            psi_p = sk_tilde.copy()


            # AFTER THIS p_norm refers to the norm of P in the previous step as psi_p has been updated
            if debug:
                if not run_simm:
                    print("1) Check c = ", psi_q.dot(p_L) * p_norm)
                    print("2) Check b = ", psi_p.dot(L_q) * s_norm / c_coeff)
                else:
                    print("1) Check c = ", psi_q.dot(p_L * mask_dot) * p_norm)
                    print("2) Check b = ", psi_p.dot(L_q * mask_dot) * s_norm / c_coeff)

            if debug:
                # Check the tridiagonality
                print("Tridiagonal matrix: (lenp: {}, lens: {})".format(len(self.basis_P), len(self.s_norm)))
                for k in range(len(self.basis_P)):
                    if k >= 1:
                        pp_norm = self.s_norm[k] / self.c_coeffs[k-1]
                    else:
                        pp_norm = self.s_norm[k]

                    if not run_simm:
                        print("p_{:d} L q_{:d} = {} | p_{:d} norm = {}".format(k, len(self.basis_P)-1, pp_norm * self.basis_P[k].dot(L_q), k, pp_norm))
                    else:
                        print("p_{:d} L q_{:d} = {} | p_{:d} norm = {}".format(k, len(self.basis_P)-1, pp_norm * self.basis_P[k].dot(L_q * mask_dot), k, pp_norm))

                pp_norm = s_norm / c_coeff
                if not run_simm:
                    print("p_{:d} L q_{:d} = {} | p_{:d} norm = {}".format(len(self.basis_P), len(self.basis_P)-1, pp_norm * psi_p.dot(L_q), k+1, pp_norm))
                else:
                    print("p_{:d} L q_{:d} = {} | p_{:d} norm = {}".format(len(self.basis_P), len(self.basis_P)-1, pp_norm * psi_p.dot(L_q * mask_dot), k+1, pp_norm))


                # Check the tridiagonality
                print()
                print("Transposed:".format(len(self.basis_P), len(self.s_norm)))
                if not run_simm:
                    for k in range(len(self.basis_Q)):
                        print("q_{:d} L^T p_{:d} = {} | p_{:d} norm = {}".format(k, len(self.basis_P)-1, p_norm* self.basis_Q[k].dot(p_L), k, p_norm))
                    print("q_{:d} L^T p_{:d} = {} | p_{:d} norm = {}".format(len(self.basis_P), len(self.basis_P)-1, p_norm* psi_q.dot(p_L), k+1, p_norm))
                else:
                    for k in range(len(self.basis_Q)):
                        print("q_{:d} L^T p_{:d} = {} | p_{:d} norm = {}".format(k, len(self.basis_P)-1, p_norm* self.basis_Q[k].dot(p_L * mask_dot), k, p_norm))
                    print("q_{:d} L^T p_{:d} = {} | p_{:d} norm = {}".format(len(self.basis_P), len(self.basis_P)-1, p_norm* psi_q.dot(p_L * mask_dot), k+1, p_norm))


            t1 = time.time()


            # Lets repeat twice the orthogonalization
            converged = False
            new_q = psi_q.copy()
            new_p = psi_p.copy()

            if debug:
                if not run_simm:
                    norm_q = np.sqrt(new_q.dot(new_q))
                    norm_p = np.sqrt(new_p.dot(new_p))
                    print("Norm of q = {} and p = {} BEFORE Gram-Schmidt".format(norm_q, norm_p))
                    print("current p dot q = {} (should be 1)".format(new_q.dot(new_p) * s_norm / c_coeff))
                else:
                    norm_q = np.sqrt(new_q.dot(new_q * mask_dot))
                    norm_p = np.sqrt(new_p.dot(new_p * mask_dot))
                    print("Norm of q = {} and p = {} before Gram-Schmidt".format(norm_q, norm_p))
                    print("current p dot q = {} (should be 1)".format(new_q.dot(new_p * mask_dot) * s_norm / c_coeff))


                # Check the Gram-Schmidt
                print("GS orthogonality check: (should all be zeros)")
                print("step) Q dot old Ps  | P dot old Qs")
                for k in range(len(self.basis_P)):
                    if k >= 1:
                        pp_norm = self.s_norm[k] / self.c_coeffs[k-1]
                    else:
                        pp_norm = self.s_norm[k]

                    if not run_simm:
                        q_dot_pold = self.basis_P[k].dot(new_q) * pp_norm
                        p_dot_qold = self.basis_Q[k].dot(new_p) * pp_norm
                    else:
                        q_dot_pold = self.basis_P[k].dot(new_q * mask_dot) * pp_norm
                        p_dot_qold = self.basis_Q[k].dot(new_p * mask_dot) * pp_norm
                    print("{:4d}) {:16.8e} | {:16.8e}".format(k, q_dot_pold, p_dot_qold))

            # Start the Gram Schmidt procedure        
            for k_orth in range(n_rep_orth):
                ortho_q = 0
                ortho_p = 0

                # The starting vector
                start = 0
                # n_ortho says how many vectors we include in the GS
                if n_ortho is not None:
                    start = len(self.basis_P) - n_ortho
                    if start < 0:
                        start = 0

                for j in range(start, len(self.basis_P)):
                    if not run_simm:
                        coeff1 = self.basis_P[j].dot(new_q)
                        coeff2 = self.basis_Q[j].dot(new_p)
                    else:
                        coeff1 = self.basis_P[j].dot(new_q * mask_dot)
                        coeff2 = self.basis_Q[j].dot(new_p * mask_dot)

                    # Gram Schmidt
                    new_q -= coeff1 * self.basis_P[j]
                    new_p -= coeff2 * self.basis_Q[j]

                    #print("REP {} COEFF {}: scalar: {}".format(k_orth+1, j, coeff1))

                    ortho_q += np.abs(coeff1)
                    ortho_p += np.abs(ortho_p)

                # Add the new vector to the Krilov Basis
                if not run_simm:
                    normq = np.sqrt(new_q.dot(new_q))
                else:
                    normq = np.sqrt(new_q.dot(new_q * mask_dot))
                if verbose:
                    print("Vector norm (q) after GS number {}: {:16.8e}".format(k_orth+1, normq))

                # Check the normalization (If zero the algorithm converged)
                if normq < __EPSILON__:
                    next_converged = True
                    if verbose:
                        print("Obtained a linear dependent Q vector.")
                        print("The algorithm converged.")

                new_q /= normq

                # Normalize the p vector
                if not run_simm:
                    normp = new_p.dot(new_p)
                else:
                    normp = new_p.dot(new_p * mask_dot)
                if verbose:
                    print("Vector norm (p biconjugate) after GS number {}: {:16.8e}".format(k_orth, normp))

                # Check the normalization (If zero the algorithm converged)
                if np.abs(normp) < __EPSILON__:
                    next_converged = True
                    if verbose:
                        print("Obtained a linear dependent P vector.")
                        print("The algorithm converged.")

                new_p /= normp

                # Now we need to update s_norm to enforce p dot q = 1
                if not run_simm:
                    s_norm = c_coeff / new_p.dot(new_q)
                else:
                    s_norm = c_coeff / new_p.dot(new_q * mask_dot)

                # We have a correctly satisfied orthogonality condition
                if ortho_p < __EPSILON__ and ortho_q < __EPSILON__:
                    break


            if not converged:
                # Add the new q and p vectors
                self.basis_Q.append(new_q)
                self.basis_P.append(new_p)
                psi_q = new_q.copy()
                psi_p = new_p.copy()

                # Add the new coefficients to the Arnoldi matrix
                self.b_coeffs.append(b_coeff)
                self.c_coeffs.append(c_coeff)
                self.s_norm.append(s_norm)

                # Pop the elements we do not use to optimize the use of memory
                if optimized:
                    print('Optimize RAM consumption')
                    if len(self.basis_P) > 3:
                        print('P basis popping the elements we do not use')
                        self.basis_P.pop(-4)

                    if len(self.basis_Q) > 3:
                        print('Q basis popping the elements we do not use')
                        self.basis_Q.pop(-4)

                    if len(self.s_norm) > 3:
                        print('s norm popping the elements we do not use')
                        self.s_norm.pop(-4)

            t2 = time.time()


            if verbose:
                print("Time to perform the Gram-Schmidt and retrive the coefficients: %d s" % (t2-t1))
                print()
                print("a_%d = %.8e" % (i, self.a_coeffs[-1]))
                print("b_%d = %.8e" % (i, self.b_coeffs[-1]))
                print("c_%d = %.8e" % (i, self.c_coeffs[-1]))
                if run_simm:
                    print("|b_%d - c_%d| = %.8e" % (i, i, np.abs(self.b_coeffs[-1] - self.c_coeffs[-1])))
                print()

            # Save the step
            if not save_dir is None:
                if (i + 1) % save_each == 0:
                    self.save_status("%s/%s_STEP%d" % (save_dir, prefix, i+1))

                    if verbose:
                        print("Status saved into '%s/%s_STEP%d'" % (save_dir, prefix, i+1))

            if verbose:
                print("Lanczos step %d ultimated." % (i +1))


        if converged:
            print("   last a coeff = {}".format(a_coeff))    

run_full_diag(number, discard_dyn=True, n_iter=100)

FULL LANCZOS DIAGONALIZATION

This function runs the standard Lanczos iteration progress. It returns the eigenvalues and eigenvectors of the L operator. These can be used for computing the spectral function, and the full green function as:

.. math ::

G_{ab}(\omega) = \sum_{\alpha} \frac{\left<a | \lambda_\alpha\right>\left<\lambda_\alpha|b\right>}{\lambda_\alpha - \omega^2 + i\eta}

where :math:\lambda are eigenvalues and vectors returned by this method, while :math:\eta is a smearing parameter chosen by the user. Remember the eigenvectors are defined in the polarization basis and they comprend also the dynamical matrix degrees of freedom. Since in most application you want to discard the dynamical matrices, you can select discard_din = True.

The used Lanczos algorithm is the one by ARPACK, as implemented in scipy.sparse module

Source code in tdscha/DynamicalLanczos.py
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
def run_full_diag(self, number, discard_dyn = True, n_iter = 100):
    r"""
    FULL LANCZOS DIAGONALIZATION
    ============================

    This function runs the standard Lanczos iteration progress.
    It returns the eigenvalues and eigenvectors of the L operator.
    These can be used for computing the spectral function, and the full
    green function as:

    .. math ::

        G_{ab}(\omega) = \sum_{\alpha} \frac{\left<a | \lambda_\alpha\right>\left<\lambda_\alpha|b\right>}{\lambda_\alpha - \omega^2 + i\eta}

    where :math:`\lambda` are eigenvalues and vectors returned by this method, while :math:`\eta` is a
    smearing parameter chosen by the user. 
    Remember the eigenvectors are defined in the polarization basis and they comprend also the dynamical matrix degrees of freedom.
    Since in most application you want to discard the dynamical matrices, you can select discard_din = True.

    The used Lanczos algorithm is the one by ARPACK, as implemented in scipy.sparse module

    Parameters
    ----------
        number = int
            The number of the n highest eigenvalues to be found
        discard_dyn : bool, optional
            If True the dynamical matrix component of the output eigenvectors will be discarded.
        n_iter : int, optional
            The maximum number of Lanczos iterations. Usually must be much higher than the
            number of states you want to describe.
    """

    # Perform the lanczos operation
    eigvals, eigvects = scipy.sparse.linalg.eigsh(self.L_linop, k = number, v0 = self.psi, ncv= n_iter)

    self.eigvals = eigvals
    self.eigvects = eigvects

    # Check if the dynamical part must be discarded
    if discard_dyn:
        eigvects = eigvects[:self.n_modes, :]


    return eigvals, eigvects

Performance Considerations

  • Use mode=MODE_FAST_JULIA if Julia is available (2-10× speedup)
  • For large systems, use MPI parallelization with mode=MODE_FAST_MPI
  • Enable gamma_only=True for Γ-point-only calculations
  • Use select_modes to exclude high-frequency modes if not needed