-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateSignal.m
1100 lines (884 loc) · 30.7 KB
/
calculateSignal.m
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
% calculateSignal Calculates the echo signal
%
% [Signal,AuxSignals,Signals] = calculateSignal(System,Method,Nuclei,Clusters)
%
% Input:
% System system structure
% Method method structure
% Nuclei nuclei structure
% Clusters information about clusters
%
% Output:
% Signal
% AuxSignals
% Signals
% Clusters = Clusters(cluster index , 1:size ,order)
% Clusters(cluster index , size > order ,order) = 0.
function [Signal,AuxiliarySignals,Signals] ...
= calculateSignal(System,Method,Nuclei,Clusters)
% Extract physical constants.
ge = System.ge;
geff = System.gMatrix(3,3);
muB = System.muB;
muN = System.muN;
mu0 = System.mu0;
hbar = System.hbar;
% Extract experimental parameters.
magneticField = System.magneticField;
dt1 = System.dt(1);
dt2 = System.dt(2);
N1 = System.nPoints(1);
N2 = System.nPoints(2);
Npoints = N1+N2;
B1y = System.RF.B1y;
% ENUM
FID = 1; HAHN = 2; CPMG = 3; CPMG_CONST = 4; CPMG_2D = 5; %HAHN_TR = 6;
CP_N = 7; UHRIG_N = 8;
% Define the theory to use at the given cluster size.
Theory = System.Theory;
theory = Theory(Method.order,:);
useMeanFields = theory(10);
if useMeanFields
Nuclear_Dipole_z_Z = zeroDiag(Nuclei.Statistics.Nuclear_Dipole);
Nuclear_Dipole_x_iy_Z = zeroDiag(Nuclei.Statistics.Nuclear_Dipole_x_iy_Z);
else
Nuclear_Dipole_z_Z = [];
Nuclear_Dipole_x_iy_Z = [];
end
if Theory(Method.order,10)
Method_extraOrder = Method.extraOrder;
maxSuperclusterSize = Method_extraOrder;
else
Method_extraOrder = Method.order;
maxSuperclusterSize = Method.order;
end
% Convert variable to gpu compatible forms
dimensionality = 1;
doTR = false;
switch System.experiment
case 'FID'
EXPERIMENT = FID;
total_time = System.Time(end);
case 'Hahn'
EXPERIMENT = HAHN;
total_time = 2*System.Time(end);
case 'CPMG'
EXPERIMENT = CPMG;
total_time = 4*System.Time(end);
case 'CPMG-const'
EXPERIMENT = CPMG_CONST;
total_time = 4*System.Time(end);
case 'CPMG-2D'
EXPERIMENT = CPMG_2D;
total_time = 4*System.Time(end);
dimensionality = 2;
case 'Hahn-TR'
EXPERIMENT = HAHN;
total_time = 2*System.Time(end);
doTR = true;
B1y = System.RF.B1y(1);
B1x2 = System.RF.B1x(2);
B1y2 = System.RF.B1y(2);
nuRF2 = System.RF.nuRF(2);
case 'CP_N'
EXPERIMENT = CP_N;
total_time = 2*System.nPulses*System.Time(end);
case 'Uhrig_N'
EXPERIMENT = UHRIG_N;
total_time = 2*System.nPulses*System.Time(end);
otherwise
error('The experiment ''%s'' is not supported.',System.experiment);
end
B1x = System.RF.B1x;
nuRF = System.RF.nuRF;
betaT = 2*pi*System.hbar/System.kT; % 1/Hz.
% Unpack spin operators.
Op = Nuclei.SpinOperators;
SpinXiXjOps = Nuclei.SpinXiXjOperators;
numberClusters = Nuclei.numberClusters(1:maxSuperclusterSize);
maxNumberClusters = max(numberClusters(1:maxSuperclusterSize));
% CluserArray(iCluster,:,clusterSize) = nuclear indices.
ClusterArray = zeros(maxNumberClusters,Method.order,Method.order);
% Change data type of Clusters to a 3D array.
for isize = 1:Method_extraOrder
for ii = 1:Nuclei.numberClusters(isize)
ClusterArray(ii,1:isize,isize) = Clusters{isize}(ii,:);
end
end
lockRotors = System.Methyl.lockRotors;
methylMethod1 = System.Methyl.method==0;
% Initialize coherences and subcluster indices.
[Coherences, SubclusterIndices, ...
rotationalMatrix_c1_m1, rotationalMatrix_d1_m1, ...
rotationalMatrix_c2_m1, rotationalMatrix_c2_m2, ...
rotationalMatrix_d2_m1, rotationalMatrix_d2_m2] ...
= initializeCoherences(Method, numberClusters, ...
Nuclei.rotationalMatrix,(N1+N2)^dimensionality,methylMethod1,...
lockRotors,System.Methyl.methylMethylCoupling);
% Methyl Groups
isMethylCarbon = strcmp(Nuclei.Type,'CH3');
isMethylHydron = strcmp(Nuclei.Type,'CH3_1H');
[Nuclei.ZeemanStates,Nuclei.ZeemanSpinStates] = setRandomZeemanState(Nuclei);
for clusterSize = 1:Method.order
% Find coherences
numClusters = numberClusters(clusterSize);
for iCluster = numClusters:-1:1
Cluster = ClusterArray(iCluster,1:clusterSize,clusterSize);
if System.Methyl.method ~= 2 && any(isMethylHydron(Cluster) )
error(['Error in calculateSignal(): ',...
'A methyl hydron is misplaced.']);
end
if System.Methyl.method == 2 && any(isMethylCarbon(Cluster) )
error(['Error in calculateSignal(): ',...
'A methyl carbon is misplaced.']);
end
if( length(unique(Cluster)) < length( Cluster ) )
error(['Error in calculateSignal(): ',...
'Cluster constains duplicate indices.']);
end
% Check if iCluster is valid.
if Cluster(1,1) == 0, continue; end
% Determin the number of spins in the cluster
thisClusterSize = clusterSize + 2*sum(isMethylCarbon(Cluster));
% Initialize cluster
thisCluster = zeros(1,thisClusterSize);
% Initialize counters.
thisIndex = 0;
methyl_number = 0;
% Loop over the cluster to assign methyl rotors.
for ii = 1:clusterSize
if isMethylCarbon(Cluster(ii))
% Count the number of metyls.
methyl_number = methyl_number + 1;
% Unpack spins from methyl pseudo-particle.
if Method.useCentralSpinSystem
hydrons = ...
nonzeros(Nuclei.associatedParticleMatrix(:,Cluster(ii)+1 ))' -1;
else
hydrons = Cluster(ii) + [1,2,3];
end
thisCluster(thisIndex+1:thisIndex+numel(hydrons)) = hydrons;
thisIndex = thisIndex + numel(hydrons);
else
thisIndex = thisIndex + 1;
thisCluster(thisIndex) = Cluster(ii);
end
end
if System.Methyl.method ==1
thisClusterSize = numel(thisCluster);
end
if( length(unique(thisCluster)) < length( thisCluster ) )
error(['Error in calculateSignal(): ',...
'This cluster constains duplicate indices.']);
end
if System.Methyl.method ~= 2 && mod(sum(isMethylHydron(thisCluster) ),3) ~= 0
error(['Error in calculateSignal(): ',...
'This cluster contains a partial methyl group.']);
end
if any(isMethylCarbon(thisCluster) )
error(['Error in calculateSignal(): ',...
'A methyl carbon is misplaced.']);
end
% Generate methyl permutations
cyclicPermutation = thisCluster;
if lockRotors || ~methylMethod1
nPerm = 1;
else
cyclicPermutation = getCyclicPermutations(isMethylCarbon(Cluster));
nPerm = size(cyclicPermutation,1);
cyclicPermutation = thisCluster(cyclicPermutation);
end
% Decide if the cluster should be skipped:
% check if all spin have the same I, and that if I >= 1 is enabled.
skipCluster = (System.spinHalfOnly && Nuclei.StateMultiplicity(thisCluster(1)) > 2);
skipCluster = skipCluster || (~all(Nuclei.StateMultiplicity(thisCluster) ...
== Nuclei.StateMultiplicity(thisCluster(1))));
if skipCluster
Coherences{clusterSize}(iCluster,:) = ones(size(Coherences{clusterSize}(iCluster,:) ));
end
% Set subcluster indices.
SubclusterIndices{clusterSize}(:,:,iCluster) = findSubclusters(...
ClusterArray,clusterSize,iCluster,clusterSize);
% Select the appropriate spin operator.
spinMultiplicity = Nuclei.StateMultiplicity(thisCluster(1));
SpinOp = Op{spinMultiplicity}{thisClusterSize};
if spinMultiplicity==3
SpinXiXjOp = SpinXiXjOps{thisClusterSize};
else
SpinXiXjOp = [];
end
H_alphaMF = 0;
H_betaMF = 0;
% density matrix size
SpinSpaceDim = prod(Nuclei.StateMultiplicity(thisCluster));
HilbertSpaceDim = SpinSpaceDim*nPerm;
switch nPerm
case 1 % 0 rotors
Hb = zeros(HilbertSpaceDim);
case 3 % 1 rotor
switch SpinSpaceDim
case 8 % CH3
Hb = rotationalMatrix_c1_m1;
case 27 % CD3
Hb = rotationalMatrix_d1_m1;
case 16 % H, CH3
Hb = rotationalMatrix_c2_m1;
case 81 % D, CD3
Hb = rotationalMatrix_d2_m1;
end
case 9 % 2 rotors
switch SpinSpaceDim
case 64 % CH3, CH3
Hb = rotationalMatrix_c2_m2;
case 729 % CD3, CD3
Hb = rotationalMatrix_d2_m2;
end
otherwise
Hb = zeros(HilbertSpaceDim);
end
Ha = Hb;
for iave = 1:System.nStates(clusterSize)
if useMeanFields
ZeemanStates = Nuclei.ZeemanStates(iave,:);
spinState = Nuclei.ZeemanSpinStates(iave,:);
spinState(thisCluster) = 0;
if(size(spinState,2)>1)
spinState = spinState';
end
mean_Dipole_z_Z = Nuclear_Dipole_z_Z*spinState;
mean_Dipole_x_iy_Z = Nuclear_Dipole_x_iy_Z*spinState;
% mean_Dipole_z_Z = mean_Dipole_z_Z;
% mean_Dipole_x_iy_Z = mean_Dipole_x_iy_Z;
else
mean_Dipole_z_Z = [];
mean_Dipole_x_iy_Z = [];
end
% Loop over cyclic permutations.
for iPerm = 1:nPerm
% Permute rotor spins.
thisCluster = cyclicPermutation(iPerm,:);
% Get interaction tensors.
[tensors,~] = pairwisetensors(Nuclei.Nuclear_g, ...
Nuclei.Coordinates,thisCluster,Nuclei.Atensor,magneticField,ge,geff,...
muB,muN,mu0,hbar,theory,B1x,B1y,nuRF, ...
mean_Dipole_z_Z, mean_Dipole_x_iy_Z);
if ~System.limitToSpinHalf
qtensors = Nuclei.Qtensor(:,:,thisCluster);
else
qtensors = 0;
end
if doTR
[tensors_TR,~] = pairwisetensors(Nuclei.Nuclear_g, ...
Nuclei.Coordinates,thisCluster,Nuclei.Atensors,magneticField,ge,geff,...
muB, muN, mu0, hbar,theory,B1x2,B1y2,nuRF2,...
mean_Dipole_z_Z, mean_Dipole_x_iy_Z);
if ~System.limitToSpinHalf
qtensors = Nuclei.Qtensors(:,:,thisCluster);
else
qtensors = 0;
end
else
tensors_TR = [];
end
% Build Hamiltonians.
% [H_alpha,H_beta] = assembleHamiltonian(state_multiplicity,...
% tensors,SpinOp,Qtensors,SpinXiXjOp,...
% theory,...
% isMethyl,...
% methylMethod, ...
% methylTunnelingSplitting, ...
% methylID)
[H_alpha,H_beta] = ...
assembleHamiltonian(Nuclei.StateMultiplicity(thisCluster),...
tensors,SpinOp,qtensors,SpinXiXjOp, theory,...
isMethylHydron(thisCluster),...
System.Methyl.method,...
Nuclei.methylTunnelingSplitting(thisCluster,thisCluster),...
Nuclei.MethylID(thisCluster) ...
);
if ~isempty(tensors_TR)
[H_alpha_TR,H_beta_TR] = ...
assembleHamiltonian(Nuclei.StateMultiplicity(thisCluster),...
tensors,SpinOp,qtensors,SpinXiXjOp, theory,...
isMethylHydron(thisCluster), ...
System.Methyl.method, ...
Nuclei.methylTunnelingSplitting(thisCluster,thisCluster),...
Nuclei.MethylID(thisCluster));
else
H_alpha_TR = [];
H_beta_TR = [];
end
if System.useThermalEnsemble
densityMatrix = [];
else
densityMatrix = getDensityMatrix(ZeemanStates(thisCluster),...
Nuclei.StateMultiplicity(thisCluster),thisCluster);
end
% Add mean field term to the Hamiltonian.
Halpha = H_alpha + H_alphaMF;
Hbeta = H_beta + H_betaMF;
% Determine which block in the rotational Hamiltonian
% this configuration is assigned to.
hRange = (iPerm-1)*SpinSpaceDim+(1:SpinSpaceDim);
% Add configuration Hamiltonian to the full Hamiltonian.
Hb(hRange,hRange) = Hbeta;
Ha(hRange,hRange) = Halpha;
if ~isempty(H_alpha_TR)
Hb_TR(hRange,hRange) = H_beta_TR;
Ha_TR(hRange,hRange) = H_alpha_TR;
else
Hb_TR = [];
Ha_TR = [];
end
end
% Calculate cluster coherence.
Coherences{clusterSize}(iCluster,:) = Coherences{clusterSize}(iCluster,:) + ...
propagate(total_time,N1+N2,dt1,dt2,N1,Hb,Ha,Hb_TR,Ha_TR, ...
EXPERIMENT,densityMatrix, System.useThermalEnsemble, betaT, ...
System.nPulses);
end
Coherences{clusterSize}(iCluster,:) = ...
Coherences{clusterSize}(iCluster,:)/Coherences{clusterSize}(iCluster,1);
end
end
% Unused coherences need to be re-initialized to unity.
for clusterSize = 1:Method.order
unusedClusters = find( Coherences{clusterSize}(:,1)==0 )';
if ~isempty(unusedClusters)
Coherences{clusterSize}(unusedClusters,:) = 1;
end
end
% Calculate signal
%-------------------------------------------------------------------------------
[Signals, AuxiliarySignals] = doClusterCorrelationExpansion(...
Coherences,ClusterArray,SubclusterIndices,...
Npoints,dimensionality, Method.order,numberClusters);
Signal = Signals(Method.order,:);
%-------------------------------------------------------------------------------
end
% ==============================================================================
% Generate Density Matrix
% ==============================================================================
function densityMatrix = getDensityMatrix(states,multiplicities,Cluster)
clustersize = length(Cluster);
prod_state = states(clustersize);
offset_factor = multiplicities(clustersize);
for iSpin = clustersize-1:-1:1
prod_state = prod_state + offset_factor*(states(iSpin) - 1);
offset_factor = offset_factor*multiplicities(iSpin);
end
densityMatrix = zeros(offset_factor);
densityMatrix(prod_state,prod_state) = 1;
end
% ========================================================================
% Propagate Function
% ========================================================================
function Signal = propagate(total_time,Npoints,dt,dt2,N1,...
Ham_beta,Ham_alpha,...
Ham_beta_TR,Ham_alpha_TR ,...
EXPERIMENT, densityMatrix, useThermalEnsemble, betaT, nPulses)
% ENUM
FID = 1; HAHN = 2; CPMG = 3; CPMG_CONST = 4; CPMG_2D = 5; HAHN_TR = 6;
CP_N = 7; UHRIG_N = 8;
% Ensure sub-Hamiltonians are Hermitian.
Ham_beta = (Ham_beta+Ham_beta')/2;
Ham_alpha = (Ham_alpha+Ham_alpha')/2;
% Get density matrix.
if useThermalEnsemble
DensityMatrix = propagator_eig((Ham_alpha+Ham_beta)/2,-1i*betaT,0);
else
DensityMatrix = densityMatrix;
end
% Vectorize density matrix.
vecDensityMatrixT = reshape(DensityMatrix.',1,[])/trace(DensityMatrix);
t_Uhrig = 0;
% Generate propagators for time steps dt and dt2
[dU_beta,dU_beta2] = propagator_eig(Ham_beta,dt,dt2);
[dU_alpha,dU_alpha2] = propagator_eig(Ham_alpha,dt,dt2);
doTR = ~isempty(Ham_beta_TR);
if doTR
% Ensure subHamiltonians are Hermitian
Ham_beta_TR = (Ham_beta_TR+Ham_beta_TR')/2;
Ham_alpha_TR = (Ham_alpha_TR+Ham_alpha_TR')/2;
% Generate propagators for time steps dt and dt2
[dU_beta_TR, dU_beta2_TR] = propagator_eig(Ham_beta_TR,dt,dt2);
[dU_alpha_TR, dU_alpha2_TR] = propagator_eig(Ham_alpha_TR,dt, dt2);
end
% Initialize propagators
nStates = length(Ham_beta);
U_beta = eye(nStates);
U_alpha = eye(nStates);
% Initialize signal
if EXPERIMENT==CPMG_2D
v = ones(Npoints,Npoints);
else
v = ones(1,Npoints);
end
% Loop over time points
for iTime = 1:Npoints
switch EXPERIMENT
case FID
U = U_beta'*U_alpha;
v(iTime) = vecDensityMatrixT*U(:);
case HAHN
U = U_beta'*U_alpha'*U_beta*U_alpha;
v(iTime) = vecDensityMatrixT*U(:);
case CPMG
U = U_alpha'*U_beta' * ...
(U_beta'*U_alpha' * U_beta*U_alpha) * U_alpha*U_beta;
v(iTime) = vecDensityMatrixT*U(:);
case CPMG_CONST
% THIS NEEDS TO BE UPDATED TO USE dt2 WHEN iTime > N1.
[U_beta, U_beta_2] = propagator_eig(...
Ham_beta,(iTime-1)*dt, total_time/4-(iTime-1)*dt);
[U_alpha, U_alpha_2] = propagator_eig(...
Ham_alpha,(iTime-1)*dt, total_time/4-(iTime-1)*dt);
U = U_alpha_2'*U_beta_2' * ...
(U_beta'*U_alpha' * U_beta*U_alpha) * U_alpha_2*U_beta_2;
v(iTime) = vecDensityMatrixT*U(:);
case CPMG_2D
U_beta_2 = eye(nStates);
U_alpha_2 = eye(nStates);
% Loop over second experimental dimension.
for jTime = 1:iTime
% Generate time dependent detection operator.
U = U_alpha_2'*U_beta_2' * ...
(U_beta'*U_alpha' * U_beta*U_alpha) * U_alpha_2*U_beta_2;
v(iTime,jTime) = vecDensityMatrixT*U(:);
v(jTime,iTime) = v(iTime,jTime)';
% Increment propagator.
if jTime < N1
U_beta_2 = dU_beta*U_beta_2;
U_alpha_2 = dU_alpha*U_alpha_2;
else
U_beta_2 = dU_beta2*U_beta_2;
U_alpha_2 = dU_alpha2*U_alpha_2;
end
end
case HAHN_TR
U = U_beta'*U_beta_TR'*U_alpha'*U_alpha_TR' ...
* U_beta_TR*U_beta*U_alpha_TR*U_alpha;
v(iTime) = vecDensityMatrixT*U(:);
case CP_N
U_aa = U_alpha*U_alpha;
U_bb = U_beta*U_beta;
exponent = fix((nPulses-1)/2);
AABB = (U_aa*U_bb)^exponent;
BBAA = (U_bb*U_aa)^exponent;
if mod(nPulses,2)==0
U = ( U_alpha*BBAA*U_bb*U_alpha )' ...
*( U_beta*AABB*U_aa*U_beta );
else
U = ( U_beta*AABB*U_alpha )' ...
*( U_alpha*BBAA*U_beta );
end
v(iTime) = vecDensityMatrixT*U(:);
case UHRIG_N
% t_i = (2*tau)*sin^2[ (pi*i)/(2*(N+1)) ].
if iTime< N1
t_Uhrig = t_Uhrig + dt*2*nPulses;
else
t_Uhrig = t_Uhrig + dt2*2*nPulses;
end
v(iTime) = getUhrigN(t_Uhrig, Ham_beta,Ham_alpha,...
vecDensityMatrixT, nPulses);
end
% Increment propagators
if iTime < N1
U_beta = dU_beta*U_beta;
U_alpha = dU_alpha*U_alpha;
else
U_beta = dU_beta2*U_beta;
U_alpha = dU_alpha2*U_alpha;
end
if doTR
if iTime < N1
U_beta_TR = dU_beta_TR*U_beta_TR;
U_alpha_TR = dU_alpha_TR*U_alpha_TR;
else
U_beta_TR = dU_beta2_TR*U_beta_TR;
U_alpha_TR = dU_alpha2_TR*U_alpha_TR;
end
end
end
% Return signal as a vector
if EXPERIMENT == CPMG_2D
Signal = reshape(v.',1,[]);
else
Signal = v;
end
if any(abs(v) - 1 > 1e-9)
error('Coherence error: coherence cannot be larger than 1.');
end
end
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function v_iTime = getUhrigN(total_time, Hamiltonian_beta,Hamiltonian_alpha,...
vecDensityMatrixT, nPulses)
% t_i = T*sin^2[ (pi*i)/(2*(N+1)) ].
% delays = T*1/2*(cos(pi*([indices,N+1]-1)/( (N+1) ) ) ...
% - cos(pi*[indices,N+1]/( (N+1) ) ));
% Since the Uhrig pulse sequence is symmetric about the half-way point,
% only half the delays need to explicitly propogated.
if mod(nPulses,2)==0
% Since the Uhrig pulse sequence is symmetric about the half-way point,
% only half the delays need to explicitly propogated.
indices = 0:ceil( (nPulses+1)/2) ;
delta_t = total_time * diff(sin(indices*pi/( 2*(nPulses+1) ) ).^2);
% Split the last delat in two parts if needed.
delta_t(end) = delta_t(end)/2;
U_ket0 = 1;
U_ket1 = 1;
U_bra0 = 1;
U_bra1 = 1;
useHbeta = true;
for t = delta_t
if useHbeta
H_ket = Hamiltonian_beta;
H_bra = Hamiltonian_alpha;
else
H_ket = Hamiltonian_alpha;
H_bra = Hamiltonian_beta;
end
useHbeta = ~useHbeta;
U_prop = propagator_eig(H_ket,t);
U_ket0 = U_prop*U_ket0;
U_ket1 = U_ket1*U_prop;
U_prop = propagator_eig(H_bra,t);
U_bra0 = U_prop*U_bra0;
U_bra1 = U_bra1*U_prop;
end
else
% indices = 0:ceil(nPulses+1);
% delta_t = total_time * diff(sin(indices*pi/( 2*(nPulses+1) ) ).^2);
indices = 0:( (nPulses+1)/2) ;
delta_t = total_time * diff(sin(indices*pi/( 2*(nPulses+1) ) ).^2);
U_ket0 = 1;
U_ket1 = 1;
U_bra0 = 1;
U_bra1 = 1;
useHbeta = true;
for t = delta_t
if useHbeta
H_ket = Hamiltonian_beta;
H_bra = Hamiltonian_alpha;
else
H_ket = Hamiltonian_alpha;
H_bra = Hamiltonian_beta;
end
useHbeta = ~useHbeta;
U_prop = propagator_eig(H_ket,t);
U_ket0 = U_prop*U_ket0;
U_bra1 = U_bra1*U_prop;
U_prop = propagator_eig(H_bra,t);
U_bra0 = U_prop*U_bra0;
U_ket1 = U_ket1*U_prop;
end
end
U_ = (U_bra1*U_bra0)' * (U_ket1*U_ket0);
v_iTime = vecDensityMatrixT*U_(:);
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
% ========================================================================
% Subcluster Function
% ========================================================================
function Indices = findSubclusters(Clusters,clusterSize,iCluster,...
reference_clusterSize)
% Given the ith cluster of size clusterSize,
% Indices(jCluster,subCluster_size) = jth cluster of sizesubCluster_size
% that is a subcluster of the ith cluster of size clusterSize
%
% SubclusterIndices_clusterSize(jCluster,subCluster_size, iCluster) =
% the jth cluster of size subCluster_size that is a subcluster of
% the ith ccluster of size clusterSize.
% In a valid cluster, all indices must be natural numbers.
Indices = zeros(nchoosek(reference_clusterSize, ...
ceil(reference_clusterSize/2)),reference_clusterSize);
jCluster = 0;
% Each cluster is a subset of itself.
Indices(1,clusterSize)=iCluster;
if clusterSize==1
% All non-empty subsets have been found.
return;
end
% Loop over all cluster sizes up to clusterSize.
for index = 1:clusterSize
% CluserArray(iCluster,:,clusterSize) = nuclear indices.
Cluster = Clusters(iCluster, 1:clusterSize ,clusterSize);
% Cluster(Cluster==0) = [];
% Remove one element labeled by index from
% Clusters{clusterSize}(iCluster:) to get a sub-cluster with one less element.
SubCluster = [Cluster(1:index-1), Cluster(index+1:end)];
% Search for a valid cluster that equals the subcluster.
Search = Clusters(:, 1:clusterSize -1, clusterSize -1)==SubCluster;
% Locate where the subcluster is.
subclusterIndex = find( all(Search,2) );
% Skip if there is no subclustere.
if isempty(subclusterIndex)
continue;
end
jCluster = jCluster + 1;
Indices(jCluster,clusterSize -1) = subclusterIndex;
if clusterSize > 2
% Given the ith cluster of size clusterSize,
% Indices(jCluster,subCluster_size) = jth cluster of sizesubCluster_size
% that is a subcluster of the ith cluster of size clusterSize
% Recursively find smaller subclusters
Subindices = findSubclusters(Clusters,clusterSize -1, ...
subclusterIndex,reference_clusterSize);
% Assign subcluster indices.
Indices(:,1:clusterSize-1) = Subindices(:,1:clusterSize-1);
end
end
end
% ========================================================================
% Calculate propagator using diagonalization
% ========================================================================
function [U1,U2] = propagator_eig(Ham,dt1,dt2)
[EigenVectors,EigenValues] = eig(Ham);
Udiag1 = exp(-2i*pi*diag(EigenValues)*dt1);
U1 = EigenVectors*diag(Udiag1)*EigenVectors';
if nargin>2 && dt2>0
Udiag2 = exp(-2i*pi*diag(EigenValues)*dt2);
U2 = EigenVectors*diag(Udiag2)*EigenVectors';
else
U2 = 1;
end
end
% ========================================================================
% New Function TEMPORARY
% ========================================================================
function Indices = findSubclusters0(Clusters,clusterSize,iCluster)
% Indices{size} = list of all jCluster such that Clusters{size}(jCluster,:)
% is a subcluster of Clusters{clusterSize}(iCluster,:)
% In a valid cluster, all indices must be natural numbers.
if Clusters{clusterSize}(iCluster,1) == 0
Indices = [];
return;
end
% Each cluster is a subset of itself.
Indices{clusterSize}=iCluster;
if clusterSize==1
% All non-empty subsets have been found.
return;
end
% Loop over all cluster sizes up to clusterSize.
for index = 1:clusterSize
% Remove one element labeled by index from Clusters{clusterSize}(iCluster:)
% to get a sub-cluster with one less element.
SubCluster = [Clusters{clusterSize}(iCluster,1:index-1), ...
Clusters{clusterSize}(iCluster,index+1:end)];
% Search for a valid cluster that equals the subcluster.
Search = Clusters{clusterSize -1}==SubCluster;
for ii = 2:size(Search,2)
Search(:,1) = Search(:,1).*Search(:,ii);
end
subclusterIndex = find(Search(:,1)==1);
if isempty(subclusterIndex)
continue;
end
Indices{clusterSize -1} = [Indices{clusterSize -1} , subclusterIndex];
if clusterSize > 2
Subindices = findSubclusters0(Clusters,clusterSize -1,subclusterIndex);
for isize = (clusterSize-2):-1:1
if ~isempty(Subindices{isize})
Indices{isize} = [Indices{isize},Subindices{isize}];
end
end
end
end
for isize = 1:clusterSize
Indices{isize} = unique(Indices{isize});
end
end
% ==============================================================================
% Generate cyclic permutation
% ==============================================================================
function cycPerm = getCyclicPermutations(boolCluster)
N = length(boolCluster);
numberRotors = sum(boolCluster);
% no methyls
if numberRotors == 0
cycPerm = 1:N;
return;
end
% 1-clusters
if N == 1
cycPerm = [1,2,3;2,3,1;3,1,2];
return;
end
% 2-clusters
if N == 2
if numberRotors == 2
cycPerm = [1,2,3,4,5,6;...
2,3,1,4,5,6; ...
3,1,2,4,5,6; ...
1,2,3,5,6,4;...
2,3,1,5,6,4; ...
3,1,2,5,6,4; ...
1,2,3,6,4,5;...
2,3,1,6,4,5; ...
3,1,2,6,4,5];
elseif boolCluster(1)
cycPerm = [1,2,3,4;2,3,1,4;3,1,2,4];
elseif boolCluster(2)
cycPerm = [1,2,3,4;1,3,4,2;1,4,2,3];
end
return;
end
% 3-clusters
if N==3
if numberRotors == 3
cycPerm = [1,2,3, 4,5,6, 7,8,9;...
2,3,1, 4,5,6, 7,8,9; ...
3,1,2, 4,5,6, 7,8,9; ...
1,2,3, 5,6,4, 7,8,9;...
2,3,1, 5,6,4, 7,8,9; ...
3,1,2, 5,6,4, 7,8,9; ...
1,2,3, 6,4,5, 7,8,9;...
2,3,1, 6,4,5, 7,8,9; ...
3,1,2, 6,4,5, 7,8,9; ...
2,3,1, 4,5,6, 7,8,9; ...
3,1,2, 4,5,6, 7,8,9; ...
1,2,3, 5,6,4, 9,7,8;...
2,3,1, 5,6,4, 9,7,8; ...
3,1,2, 5,6,4, 9,7,8; ...
1,2,3, 6,4,5, 9,7,8; ...
2,3,1, 6,4,5, 9,7,8; ...
3,1,2, 6,4,5, 9,7,8; ...
2,3,1, 4,5,6, 9,7,8; ...
3,1,2, 4,5,6, 9,7,8; ...
1,2,3, 5,6,4, 8,9,7;...
2,3,1, 5,6,4, 8,9,7; ...
3,1,2, 5,6,4, 8,9,7; ...
1,2,3, 6,4,5, 8,9,7;...
2,3,1, 6,4,5, 8,9,7; ...
3,1,2, 6,4,5, 8,9,7];
elseif numberRotors == 2
cycPerm = [1,2,3,4,5,6;...
2,3,1,4,5,6; ...
3,1,2,4,5,6; ...
1,2,3,5,6,4;...
2,3,1,5,6,4; ...
3,1,2,5,6,4; ...
1,2,3,6,4,5;...
2,3,1,6,4,5; ...
3,1,2,6,4,5];
if ~boolCluster(3)
cycPerm = [cycPerm,7*ones(9,1)];
elseif ~boolCluster(2)
cycPerm(:,4:6) = cycPerm(:,4:6) + 1;
cycPerm = [cycPerm(:,1:3), 4*ones(9,1), cycPerm(:,4:6)];
else
cycPerm = cycPerm + 1;
cycPerm = [1*ones(9,1),cycPerm];
end
elseif numberRotors == 1
cycPerm = [1,2,3;2,3,1;3,1,2];
if boolCluster(1)
cycPerm = [cycPerm,[4,5].*ones(3,2)];
elseif boolCluster(2)
cycPerm = cycPerm + 1;
cycPerm = [1*ones(3,1), cycPerm, 5*ones(3,1)];
else
cycPerm = cycPerm + 2;
cycPerm = [[1,2].*ones(3,2),cycPerm];
end
end
return;
end
error('Cannot calculate cyclic permutations.');
end
% ========================================================================
% Initialize coherences
% ========================================================================
function [Coherences, SubclusterIndices , ...
rotationalMatrix_c1_m1, rotationalMatrix_d1_m1, ...
rotationalMatrix_c2_m1, rotationalMatrix_c2_m2, ...
rotationalMatrix_d2_m1, rotationalMatrix_d2_m2] ...
= initializeCoherences(Method, numberClusters, ...
Nuclei_rotationalMatrix, nt, includeMethyls,lockRotor, ...
Methyl_methylMethylCoupling)
% includeMethyls = System.Methyl.include
SubclusterIndices = cell(1,Method.order);
Coherences = cell(Method.order,1);
for iclusterSize = 1:Method.order
Coherences{iclusterSize} = zeros(numberClusters(iclusterSize),nt);
% SubclusterIndices_clusterSize(jCluster,subCluster_size, iCluster) =
% the jth cluster of size subCluster_size that is a subcluster of
% the ith ccluster of size clusterSize.
subCluster_size_= ceil(iclusterSize/2);
SubclusterIndices{iclusterSize} = zeros(nchoosek(iclusterSize,subCluster_size_), iclusterSize , ...
numberClusters(iclusterSize));