-
Notifications
You must be signed in to change notification settings - Fork 0
/
Liouvillian.py
1316 lines (1036 loc) · 41.7 KB
/
Liouvillian.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
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
692
693
694
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
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
973
974
975
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 22 15:46:57 2023
@author: albertsmith
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 17 15:06:02 2023
@author: albertsmith
"""
import numpy as np
from copy import copy
import warnings
from scipy.linalg import expm
from .Propagator import Propagator,PropCache
from . import Defaults
from .Tools import Ham2Super,BlockDiagonal
from .Hamiltonian import Hamiltonian
from . import RelaxMat
from .RelaxClass import RelaxClass
from .Sequence import Sequence
from .Para import ParallelManager, StepCalculator
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
# import importlib.util
# numba=importlib.util.find_spec('numba') is not None
# if numba:
# from .Parallel import prop
from .Parallel import prop,prop_static
class Liouvillian():
def __init__(self,*ex,kex=None):
"""
Creates the full Liouvillian, and provides some functions for
propagation of the Liouvillian
Parameters
----------
H : list
List of Hamiltonians or alternatively ExpSys
kex : np.array, optional
Exchange Matrix. The default is None.
Returns
-------
None.
"""
if len(ex)==1:ex=ex[0]
if hasattr(ex,'shape') or hasattr(ex,'B0'):ex=[ex]
self.H=[*ex]
for k,H in enumerate(self.H):
if not(hasattr(H,'Hinter')) and hasattr(H,'B0'):
H=Hamiltonian(H)
self.H[k]=H
assert hasattr(H,'Hinter'),'Liouvillian must be provided with Hamiltonian or ExpSys objects'
assert H.pwdavg==self.pwdavg,"All Hamiltonians must have the same powder average"
if H.rf is not self.rf:
H.expsys._rf=self.rf
self._PropCache=PropCache(self)
# self.sub=False
self._Lex=None
self._index=-1
self._Lrelax=None
self._Lrf=None
self._Ln=None
# self._Ln_H=None
self._LrelaxOS=RelaxClass(self)
if Defaults['cache']:self._Ln_H=[[None for _ in range(5)] for _ in range(len(self))]
self._fields=self.fields
if kex is None:kex=np.zeros([len(self.H),len(self.H)])
self.kex=kex
self.relax_info=[] #Keeps a short record of what kind of relaxation is used
def getBlock(self,block):
"""
Returns a reduced version of this Liouvillian defined by a given
block.
Parameters
----------
block : TYPE
DESCRIPTION.
Returns
-------
LiouvilleBlock
"""
return LiouvilleBlock(self, block)
@property
def block(self):
return np.ones(self.shape[0],dtype=bool)
def clear_cache(self):
self._Ln_H=None
self._PropCache.reset()
if self._LrelaxOS is not None:self.LrelaxOS.clear_cache()
return self
@property
def reduced(self):
return False
@property
def sub(self):
if self._index==-1:return False
return True
@property
def _ctype(self):
return Defaults['ctype']
@property
def _rtype(self):
return Defaults['rtype']
@property
def _parallel(self):
return Defaults['parallel']
def reset_prop_time(self,t:float=0):
"""
Resets the current time for propagators to t
(L.expsys._tprop=t)
Parameters
----------
t : float, optional
DESCRIPTION. The default is 0.
Returns
-------
None.
"""
self.expsys._tprop=t
@property
def isotropic(self):
return np.all([H0.isotropic for H0 in self.H])
@property
def static(self):
return self.expsys.vr==0 or self.isotropic
@property
def pwdavg(self):
return self.H[0].pwdavg
@property
def Peq(self):
# for ri in self.relax_info:
# if 'Peq' in ri[1] and ri[1]['Peq']:
# return True
if self._LrelaxOS.Peq:return True
for ri in self.relax_info:
if ri[0]=='recovery':return True
return False
@property
def expsys(self):
"""
Returns the expsys of the first stored Hamiltonian
Returns
-------
ExpSys
Description of the experimental conditions and system
"""
return self.H[0].expsys
@property
def taur(self):
"""
Length of a rotor period
Returns
-------
None.
"""
if self.isotropic or self.static:return None
return 1/self.expsys.vr
@property
def dt(self):
"""
Time step for changing the rotor angle
Returns
-------
float
"""
return self.taur/self.expsys.n_gamma
@property
def shape(self):
"""
Shape of the resulting Liouvillian
Returns
-------
tuple
"""
return np.prod(self.H[0].shape)*len(self.H),np.prod(self.H[0].shape)*len(self.H)
@property
def rf(self):
return self.H[0].rf
@property
def fields(self):
return self.rf.fields
def __setattr__(self,name,value):
"""
Resets certain parameters if edits occur
Parameters
----------
name : str
Parameter name.
value : TYPE
Parameter value.
Returns
-------
None.
"""
if name=='kex':
self._Lex=None
self._PropCache.reset()
if value is not None:
value=np.array(value)
assert value.shape[0]==value.shape[1],"Exchange matrix must be square"
assert value.shape[0]==len(self.H),f"For {len(self.H)} Hamiltonians, exchange matrix must be {len(self.H)}x{len(self.H)}"
if np.any(np.diag(value)>0):
warnings.warn("Diagonals of exchange matrix should not be positive")
elif np.any(np.abs(value.sum(0))>1e-10*np.mean(-np.diag(value))):
warnings.warn("Invalid exchange matrix. Columns should sum to 0. Expect unphysical behavior.")
super().__setattr__(name,value)
def __getitem__(self,i:int):
"""
Goes to a particular item of the powder average
Parameters
----------
i : int
Element of the powder average to go to.
Returns
-------
Liouvillian''
"""
out=copy(self)
out._LrelaxOS=copy(out.LrelaxOS)
out.H=[H0[i] for H0 in self.H]
out._Ln=None
out._Ln_H=None
out._index=i
out._PropCache=self._PropCache
out._PropCache.L=out
out._LrelaxOS.L=out
# The above line bothers me. If we extract a particular element and
# then later another element, the first element's propagator cache
# references the wrong element of the Liouvillian.
return out
def add_SpinEx(self,i:list,tc:float):
"""
Allows exchange among spins, for example, if a water molecule experiences
a two-site hop. The hop does not change the values in the overall Hamiltonian,
but changes the spin indexing. We can treat this kind of dynamics without
rebuilding the entire Liouvillian; instead we just introduce exchange
within a single Liouvillian.
One provides a list of the spins in exchange. Usually, this is just two
elements, but more is also possible. For example, methyl 3-site hopping
would have a list such as [1,2,3]. This means that we either have the
exchange process 1–>2, 2->3, and 3->1, or 1->3, 2->1, 3->2. The process must
always be cyclic, with equal populations.
The correlation time is the inverse of the mean hopping rate constant. For
two- and three-site exchange, there is only one unique hopping rate, but for
higher numbers of states, the mean will be calculated.
Note that this is implemented via the relaxation module (RelaxMat), and
can equivalently be introduced by running add_relax with Type='SpinExchange'
Parameters
----------
i : list
List of the spins in exchange. E.g. i=[1,2] would cause swaps between
spins 1 and 2. i=[1,2,3] might represent a methyl rotation, yielding
exchange such that either 1->2, 2->3, 3->1, and 1->3, 2->1, 3->2.
tc : float
Correlation time of the exchange (inverse of rate constant)
Returns
-------
self
"""
self.add_relax(Type='SpinExchange',i=i,tc=tc)
return self
def add_relax(self,M=None,Type:str=None,OS:bool=False,**kwargs):
"""
Add explicit relaxation to the Liouvillian. This is either provided
directly by the user via a matrix, or by type, where currently T1, T2,
and recovery are provided, where recovery forces the simulation to go
towards thermal equilibrium.
T1: provide T1 and i, specifying the spin's index
T2: provide T2 and i, specifying the spin's index
This is provided by a matrix,
M, directly. The matrix itself can be produced with the RelaxationMatrix
class. Note that the matrix can either have the same shape as the full
Liouvillian, or the shape for just one Hamiltonian. For example, for
a two-spin 1/2 system in two-site exchange, M may have size 16x16 or
32x32. The 32x32 matrix allows different relaxation properties for the
two sites.
Parameters
----------
M : np.array, optional
DESCRIPTION. The default is None.
Returns
-------
None.
"""
self._PropCache.reset()
if isinstance(M,str): #In case Type is input as the first argument, just fix for the user
Type=M
M=None
if hasattr(self,'recovery'):
warnings.warn('recovery should always be the last term added to Lrelax')
if Type in ['DynamicThermal']:OS=True #List methods only in RelaxClass here
if OS:
getattr(self.LrelaxOS,Type)(**kwargs)
kwargs.update({'OS':OS})
self.relax_info.append((Type,kwargs))
return self
if M is None:
if Type=='recovery':
M=RelaxMat.recovery(expsys=self.expsys,L=self)
self.relax_info.append(('recovery',{'OS':OS}))
# elif Type=='Thermal':
# self.LrelaxOS.Thermal()
# self.relax_info.append(('Thermal',{}))
# return self
elif hasattr(RelaxMat,Type):
M=getattr(RelaxMat,Type)(expsys=self.expsys,**kwargs)
self.relax_info.append((Type,kwargs))
else:
warnings.warn(f'Unknown relaxation type: {Type}')
return self
q=np.prod(self.H[0].shape)
self.Lrelax #Call to make sure it's pre-allocated
if M.shape[0]==self.shape[0]:
self._Lrelax+=M
elif M.shape[0]==q:
for k,H0 in enumerate(self.H):
self._Lrelax[k*q:(k+1)*q][:,k*q:(k+1)*q]+=M
else:
assert False,f"M needs to have size ({q},{q}) or {self.shape}"
return self
def clear_relax(self):
"""
Removes all explicitely defined relaxation
Returns
-------
None.
"""
self._PropCache.reset()
self.relax_info=[]
self._Lrelax=None
self._LrelaxOS.clear()
if hasattr(self,'recovery'):
delattr(self,'recovery')
self.clear_cache()
return self
def validate_relax(self):
"""
Checks if systems with T1 relaxation have T2 relaxation. Also returns
True if the system relaxes to an equilibrium value
Returns
-------
None.
"""
Long=False
Peq=False
Trans=False
for ri in self.relax_info:
if ri[0] in ['T1']: #Check for Longitudinal relaxation
Long=True
if 'Peq' in ri[1] and ri[1]['Peq']:
Peq=True
elif ri[0] in ['T2']: #Check for Transverse relaxation
Trans=True
if Long and Peq and not Trans:
warnings.warn('T1 relaxation and Peq included without T2 relaxation. System can diverge')
elif Long and not Trans:
warnings.warn('T1 relaxation included without T2 relaxation. Unphysical system')
@property
def Lrelax(self):
if self._Lrelax is None:
self._Lrelax=np.zeros(self.shape,dtype=self._ctype)
return self._Lrelax
@property
def LrelaxOS(self):
"""
Returns the orientation-specific relaxation matrix for the current
rotor step and orientation
Parameters
----------
step : int, optional
DESCRIPTION. The default is 0.
Returns
-------
TYPE
DESCRIPTION.
"""
return self._LrelaxOS
@property
def Lex(self):
"""
Returns the exchange component of the Liouvillian
Returns
-------
np.array
"""
if self._Lex is None:
if self.kex is None or self.kex.size!=len(self.H)**2 or self.kex.ndim!=2:
self.kex=np.zeros([len(self.H),len(self.H)],dtype=self._rtype)
if len(self.H)>1:print('Warning: Exchange matrix was not defined')
self._Lex=np.kron(self.kex.astype(self._rtype),np.eye(np.prod(self.H[0].shape),dtype=self._rtype))
return self._Lex
def Ln_H(self,n:int):
"""
Returns the nth rotating component of the Liouvillian resulting from
the Hamiltonians. That is, contributions from exchange and relaxation
matrices are not included.
Only works if we are at a particular index of the Liouvillian
L[0].Ln_H(0)
Other
Parameters
----------
n : int
Index of the rotating component (-2,-1,0,1,2).
Returns
-------
np.array
"""
assert self.sub,"Calling Ln_H requires indexing to a specific element of the powder average"
# self._Ln_H=None
# if self._Ln_H is not None and self._Ln_H[self._index][n+2] is not None:
# return copy(self._Ln_H[self._index][n+2])
out=np.zeros(self.shape,dtype=self._ctype)
q=np.prod(self.H[0].shape)
for k,H0 in enumerate(self.H):
# TODO The next line should not be necessary. H0 should automatically be updated to its index
# Maybe somewhere we just changed the index instead of calling for the specific item
H0=H0[H0._index]
out[k*q:(k+1)*q][:,k*q:(k+1)*q]=H0.Ln(n)
out*=-1j*2*np.pi
# if self._Ln_H is not None:self._Ln_H[self._index][n+2]=out
return copy(out)
def Ln(self,n:int):
"""
Returns the nth rotation component of the total Liouvillian.
Parameters
----------
n : int
DESCRIPTION.
Returns
-------
None.
"""
assert self.sub,"Calling Ln requires indexing to a specific element of the powder average"
if self._Ln is None:
self._Ln=[self.Ln_H(n) for n in range(-2,3)]
self._Ln[2]+=self.Lex+self.Lrelax
return self._Ln[n+2]
@property
def Lrf(self):
"""
Liouville matrix due to RF field
Returns
-------
None.
"""
if self._fields!=self.fields:
self._Lrf=None
if self._Lrf is None:
self._Lrf=np.zeros(self.shape,dtype=self._ctype)
n=self.H[0].shape[0]**2
Lrf0=Ham2Super(self.rf())
for k in range(len(self.H)):
self._Lrf[k*n:(k+1)*n][:,k*n:(k+1)*n]=Lrf0
self._Lrf*=-1j*2*np.pi
self._fields=copy(self.fields)
return self._Lrf
def L(self,step:int):
"""
Returns the Liouvillian for a given step in the rotor cycle (t=step*L.dt)
Parameters
----------
step : TYPE
DESCRIPTION.
Returns
-------
None.
"""
# Ln=[self.Ln(n) for n in range(-2,3)]
# n_gamma=self.expsys.n_gamma
# ph=np.exp(1j*2*np.pi*step/n_gamma)
# return np.sum([Ln0*ph**(-m) for Ln0,m in zip(Ln,range(-2,3))],axis=0)+self.Lrf
ph=np.exp(1j*2*np.pi*step/self.expsys.n_gamma)
return np.sum([self.Ln(m)*(ph**(-m)) for m in range(-2,3)],axis=0)+self.Lrf+self.LrelaxOS(step)
def Lcoh(self,step:int):
"""
Returns the coherent Liouvillian for a given step in the rotor cycle (t=step*L.dt)
Parameters
----------
step : TYPE
DESCRIPTION.
Returns
-------
None.
"""
# Ln=[self.Ln(n) for n in range(-2,3)]
# n_gamma=self.expsys.n_gamma
# ph=np.exp(1j*2*np.pi*step/n_gamma)
# return np.sum([Ln0*ph**(-m) for Ln0,m in zip(Ln,range(-2,3))],axis=0)+self.Lrf
ph=np.exp(1j*2*np.pi*step/self.expsys.n_gamma)
out=np.sum([self.Ln_H(m)*(ph**(-m)) for m in range(-2,3)],axis=0)
return out
def U(self,Dt:float=None,t0:float=None,calc_now:bool=False):
"""
Calculates the propagator between times t0 and t0+Dt. By default, t0 will
be set to align with the end of the last propagator calculated for
this system. By default, Dt will be one rotor period.
Note that the propagator in general will not be calculated until required.
To force calculation on creation, set calc_now to True.
Parameters
----------
Dt : float, optional
Length of the propagator.
t0 : float, optional
Initial time for the propagator. The default is None, which sets t0
to the end of the last calculated propagator
calc_now : bool, optional.
Calculates the propagator immediately, as opposed to only when required
Returns
-------
U : Propagator
"""
# assert self.sub,"Calling L.U requires indexing to a specific element of the powder average"
self.validate_relax()
if self.static:
assert Dt is not None,"For static/isotropic systems, one must specify Dt"
t0=0
else:
if t0 is None:t0=self.expsys._tprop%self.taur
if Dt is None:Dt=self.taur
tf=t0+Dt
self.expsys._tprop=0 if self.taur is None else tf%self.taur #Update current time
voff=np.array([x[-1] for x in self.rf.fields.values()])
ph_acc=(voff*Dt)*2*np.pi
if calc_now:
if self.sub:
if self.static:
L=self.L(0)
# U=expm(L*Dt)
d,v=np.linalg.eig(L)
[email protected](np.exp(d*Dt))@np.linalg.pinv(v)
return Propagator(U,t0=t0,tf=tf,taur=self.taur,L=self,isotropic=self.isotropic,phase_accum=ph_acc)
else:
# dt=self.dt
# n0=int(t0//dt)
# nf=int(tf//dt)
# tm1=t0-n0*dt
# tp1=tf-nf*dt
# if tm1<=0:tm1=dt
dt=self.dt
n0,nf,tm1,tp1=StepCalculator(t0=t0,Dt=Dt,dt=dt)
if tm1==dt:
U=self._PropCache[n0]
else:
L=self.L(n0)
U=expm(L*tm1)
for n in range(n0+1,nf):
U=self._PropCache[n]@U
# L=self.L(n)
# U=expm(L*dt)@U
if tp1>1e-10:
if tp1==dt:
U=self._PropCache[nf]@U
else:
L=self.L(nf)
U=expm(L*tp1)@U
return Propagator(U,t0=t0,tf=tf,taur=self.taur,L=self,isotropic=self.isotropic,phase_accum=ph_acc)
else:
if self.isotropic:
U=[L0.U(t0=t0,Dt=Dt,calc_now=calc_now).U for L0 in self]
return Propagator(U=U,t0=t0,tf=tf,taur=self.taur,L=self,isotropic=self.isotropic,phase_accum=ph_acc)
else:
pm=ParallelManager(L=self,t0=t0,Dt=Dt)
U=pm()
return Propagator(U=U,t0=t0,tf=tf,taur=self.taur,L=self,isotropic=self.isotropic,phase_accum=ph_acc)
# if self._parallel and not(self.static):
# dt=self.dt
# n0=int(t0//dt)
# nf=int(tf//dt)
# tm1=t0-n0*dt
# tp1=tf-nf*dt
# if tm1<=0:tm1=dt
# # Ln=[[L0.Ln(k) for k in range(-2,3)] for L0 in self]
# # U=prop(Ln,Lrf=np.array(self.Lrf),n0=n0,nf=nf,tm1=tm1,tp1=tp1,dt=dt,n_gamma=int(self.expsys.n_gamma))
# pm=ParallelManager(L=self,n0=n0,nf=nf,tm1=tm1,tp1=tp1,dt=dt,n_gamma=self.pwdavg.n_gamma)
# U=pm()
# return Propagator(U=U,t0=t0,tf=tf,taur=self.taur,L=self,isotropic=self.isotropic)
# elif self._parallel and not(self.isotropic) and False: #Why doesn't this work?
# L=[L0.L(0) for L0 in self]
# U=prop_static(L,Dt=tf-t0)
# else:
# U=[L0.U(t0=t0,Dt=Dt,calc_now=calc_now).U for L0 in self]
# return Propagator(U=U,t0=t0,tf=tf,taur=self.taur,L=self,isotropic=self.isotropic)
else:
dct=dict()
dct['t']=[t0,tf]
dct['v1']=np.zeros([len(self.fields),2])
dct['phase']=np.zeros([len(self.fields),2])
dct['voff']=np.zeros([len(self.fields),2])
for k,v in self.fields.items():
dct['v1'][k],dct['phase'][k],dct['voff'][k]=v
return Propagator(U=dct,t0=t0,tf=tf,taur=self.taur,L=self,isotropic=self.isotropic,phase_accum=ph_acc)
def Ueye(self,t0:float=None):
"""
Returns a propagator with length zero (identity propagator)
Returns
-------
t0 : float, optional
Initial time for the propagator. The default is 0.
U : Propagator
"""
if self.static:
t0=0
else:
if t0 is None:t0=self.expsys._tprop%self.taur
return Propagator(U=[np.eye(self.shape[0]) for _ in range(len(self))],
t0=t0,tf=t0,taur=self.taur,L=self,isotropic=self.isotropic,phase_accum=0)
def Udelta(self,channel,phi:float=np.pi,phase:float=0,t0:float=None):
"""
Provides a delta pulse on the chosen channel or specific spin. Channel
is provided with the nucleus name ('1H','13C','etc'), a specific spin
or spins is provided by setting channel to an integer or list or
integers
Parameters
----------
channel : TYPE
DESCRIPTION.
phi : float, optional
DESCRIPTION. The default is np.pi.
phase : float, optional
DESCRIPTION. The default is 0.
t0 : float, optional
DESCRIPTION. The default is None.
Returns
-------
None.
"""
if self.static:
t0=0
else:
if t0 is None:t0=self.expsys._tprop%self.taur
if isinstance(channel,str):
if channel.lower()=='e':channel='e-'
i=np.argwhere([channel==Nuc for Nuc in self.expsys.Nucs])[:,0]
else:
i=np.atleast_1d(channel)
H=np.zeros(self.H[0].shape,dtype=self._ctype)
for i0 in i:
Op=self.expsys.Op[i0]
H+=np.cos(phase)*Op.x+np.sin(phase)*Op.y
L0=Ham2Super(H)
L=np.zeros(self.shape,dtype=self._ctype)
n=self.H[0].shape[0]**2
for k in range(len(self.H)):
L[k*n:(k+1)*n][:,k*n:(k+1)*n]=L0
U=expm(-1j*phi*L)
return Propagator(U=[U for _ in range(len(self))],
t0=t0,tf=t0,taur=self.taur,L=self,isotropic=self.isotropic,phase_accum=0)
def Ueig(self):
"""
Returns eigenvalues and eigenvectors of U for one rotor period. Can
be used for fast propagation in the eigenbasis
Returns
-------
tuple
"""
# d,v=eig(self.U(),k=self.shape[0]-1)
d,v=np.linalg.eig(self.U())
i=np.abs(d)>1
d[i]/=np.abs(d[i])
return d,v
def Sequence(self,Dt:float=None,cyclic:bool=False,rho=None) -> Sequence:
"""
Returns a Sequence object initialized from this Liouvillian
Parameters
----------
Dt : float, optional
Timestep for the sequence. Typically only used if one intends to
make an empty sequence (no pulses). The default is None.
cyclic : bool, optional
If the sequence is execute for a Dt longer than the default sequence
length, then setting cyclic to True will cause the sequence to
repeat. If False, then the final state of the sequence will be
retained. The default is False.
rho : TYPE, optional
DESCRIPTION. The default is None.
Returns
-------
Sequence
Pulse sequence object with this Liouvillian.
"""
if Dt is not None:
seq=Sequence(self,cyclic=cyclic,rho=rho)
seq.add_channel(self.expsys.Nucs[0],t=Dt)
return seq
return Sequence(self,cyclic=cyclic,rho=rho)
@property
def ex_pop(self):
"""
Returns the populations resulting from chemical exchange.
Returns
-------
None.
"""
self.Lex #Forces a default kex if not defined
if np.abs(self.kex).max()==0: #All zeros– assume uniform population
return np.ones(self.kex.shape[0])/self.kex.shape[0]
d,v=np.linalg.eig(self.kex)
pop=v[:,np.argmax(d)]
pop/=pop.sum()
return pop
def rho_eq(self,Hindex:int=None,pwdindex:int=None,step:int=None,sub1:bool=False):
"""
Returns the equilibrium density operator for a given Hamiltonian and
element of the powder average.
Parameters
----------
Hindex : int, optional
Index of the Hamiltonian, i.e. in case there are multiple
Hamiltonians undergoing exchange. The default is None, which
will return the equilibrium density operator weighted by the
populations resulting from the exchange matrix.
pwdindex : int, optional
Index of the element of the powder average. Should not have an
influence unless the rotor is not at the magic angle or no
spinning is included (static, anisotropic). The default is 0.
step : int, optional
If provided, uses rho_eq for the particular step in the rotor cycle
required. Otherwise, the average Hamiltonian will be used, that is,
the rotating components will be omitted.
sub1 : bool, optional
Subtracts the identity from the density matrix. Primarily for
internal use.
The default is False
Returns
-------
None.
"""
if pwdindex is None:
pwdindex=0 if self._index==-1 else self._index
if Hindex is None:
pop=self.ex_pop
N=self.block.shape[0]//len(pop)
rho_eq=np.zeros(self.block.shape[0],dtype=self._ctype)
for k,p in enumerate(pop):
rho_eq[N*k:N*(k+1)]=self.rho_eq(k,pwdindex=pwdindex,step=step,sub1=sub1)*p
return rho_eq[self.block]
# pop=self.ex_pop
# # 6 July 2024. Why were we calculating this here and not just getting it from the Hamiltonian?
# H0=list()
# for H in self.H:
# if self.static and not(self.isotropic): #Include all terms Hn
# H0.append(np.sum([H[pwdindex].Hn(m) for m in range(-2,3)],axis=0))
# else:
# H0.append(H[pwdindex].Hn(0))
# for k,LF in enumerate(self.expsys.LF):
# if not(LF):
# H0[-1]+=H.expsys.v0[k]*self.expsys.Op[k].z
# ##Approach 1: same rho_eq0 for all states in exchange
# # H=(np.array(H0).T*pop).sum(-1).T
# # rho_eq0=expm(6.62607015e-34*H/(1.380649e-23*self.expsys.T_K))
# # rho_eq0/=np.trace(rho_eq0)
# # if sub1:
# # eye=np.eye(rho_eq0.shape[0])
# # rho_eq0-=np.trace(rho_eq0@eye)/rho_eq0.shape[0]*eye
# # rho_eq=np.zeros(self.shape[0],dtype=self._ctype)
# # n=self.H[0].shape[0]**2
# # for k,p in enumerate(pop):
# # rho_eq[k*n:(k+1)*n]=rho_eq0.flatten()*p
# ##Approach 2: different rho_eq0 for each state in exchange
# rho_eq=np.zeros(self.shape[0],dtype=self._ctype)
# n=self.H[0].shape[0]**2
# for k,(H,p) in enumerate(zip(H0,pop)):
# rho_eq0=expm(6.62607015e-34*H/(1.380649e-23*self.expsys.T_K))
# rho_eq0/=np.trace(rho_eq0)
# if sub1:
# eye=np.eye(rho_eq0.shape[0])
# rho_eq0-=np.trace(rho_eq0@eye)/rho_eq0.shape[0]*eye
# rho_eq[k*n:(k+1)*n]=rho_eq0.flatten()*p
# return rho_eq
else:
return self.H[Hindex].rho_eq(pwdindex=pwdindex,step=step,sub1=sub1).reshape(self.block.shape[0]//len(self.H))
@property
def Energy(self):
"""
Energy for each of the NxNxnHam states in the Liouvillian, including
energy from the Larmor frequency (regardless of whether in lab frame).
Neglects rotating terms, Hn, for n!=0
Returns
-------
None.
"""
Energy=np.zeros(self.shape[0])