-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentralSpinSystem.m
3662 lines (2759 loc) · 105 KB
/
centralSpinSystem.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
% Load pdb file into structure
%
% Nuclei = parseNuclei(System,Method,datafile)
%
% Input:
% System structure with fields for the spin system
% Method structure with fields for the method
% pdbFileName name of PDB file
function [Nuclei, System]= centralSpinSystem(System,Method,Data,pdb_)
System = setCentralSpinSystemDefaults(System,Method);
ienum = 1;
PARTICLE_CENTRALSPIN = ienum; ienum = ienum + 1;
PARTICLE_ELECTRON = ienum; ienum = ienum + 1;
PARTICLE_HYDROGEN = ienum; ienum = ienum + 1;
PARTICLE_PROTIUM = ienum; ienum = ienum + 1;
PARTICLE_1H_EXCHANGEABLE = ienum; ienum = ienum + 1;
PARTICLE_1H_NONEXCHANGEABLE = ienum; ienum = ienum + 1;
PARTICLE_1H_METHYL = ienum; ienum = ienum + 1;
PARTICLE_DEUTERIUM = ienum; ienum = ienum + 1;
PARTICLE_2H_EXCHANGEABLE = ienum; ienum = ienum + 1;
PARTICLE_2H_NONEXCHANGEABLE = ienum; ienum = ienum + 1;
PARTICLE_2H_METHYL = ienum; ienum = ienum + 1;
PARTICLE_CARBON = ienum; ienum = ienum + 1;
PARTICLE_12C = ienum; ienum = ienum + 1;
PARTICLE_13C = ienum; ienum = ienum + 1;
PARTICLE_C_METHYL = ienum; ienum = ienum + 1;
PARTICLE_13C_METHYL = ienum; ienum = ienum + 1;
PARTICLE_NITROGEN = ienum; ienum = ienum + 1;
PARTICLE_14N = ienum; ienum = ienum + 1;
PARTICLE_15N = ienum; ienum = ienum + 1;
PARTICLE_OXYGEN = ienum; ienum = ienum + 1;
PARTICLE_16O = ienum; ienum = ienum + 1;
PARTICLE_17O = ienum; ienum = ienum + 1;
PARTICLE_SILICON = ienum; ienum = ienum + 1;
PARTICLE_28SI = ienum; ienum = ienum + 1;
PARTICLE_29SI = ienum; ienum = ienum + 1;
PARTICLE_UNSET = ienum; ienum = ienum + 1;
PARTICLE_NONE = ienum; ienum = ienum + 1;
PARTICLE_VOID = ienum; ienum = ienum + 1;
PARTICLE_ALL = ienum; ienum = ienum + 1;
PARTICLE_ENUM = ienum; ienum = ienum + 1;
if(PARTICLE_ENUM+1-ienum ~= 0)
error(['Error in centralSpinSystem(): ', 'enums are incorrect.']);
end
associatedParticleMatrix_ = sparse(0,0); % indexed by CluE indices.
associatedPDBMatrix_ = sparse(pdb_.number,pdb_.number); % indexed by pdb ID.
cellShifts_ = [];
% methylIDs_ = [];
moleculeID_ = [];
number_ = 0;
% numberMethyls_ = 0;
numberParticleClasses_ = 0;
particleClassID_ = [];
particles_ = cell(0,0);
%pdb_ = parsePDBfile(Data.InputData, System.angstrom);
originVec_ = getElectronCoordinates(...
System,[pdb_.x,pdb_.y,pdb_.z],pdb_.serial);
pdbID_ = [];
residueList_ = cell(0,0);
uniqueResidueList_ = {};
zeroIndex_ = {};
r0_ = System.load_radius;
r0_nonHalf_ = Method.vertexCutoff.radius_nonSpinHalf(1);
verbose_ = Method.verbose;
buildSystem();
% Finish setup.
coordinates_ = [];
refreshSystem();
% Print total number of particles.
fprintf('\nFound %d particles.\n', number_);
% Loop through all initialized ParticleClasses and print details.
for iitype = 1:numberParticleClasses_
printParticleInfo( particles_{iitype} );
end
Nuclei = struct();
assembleNuclei();
return;
% Start of nested functions
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function uniqueID = addParticle(particleEnum, resName, ...
associatedParticles, iparticle, ucpdb)
% Initialize return value.
uniqueID = -1;
% Get PDB number.
ipdb = mod(ucpdb, pdb_.number);
if ipdb==0, ipdb = pdb_.number; end
uc = ceil(ucpdb/pdb_.number);
% Check if particle type with resName has been initialized.
isUnique = true;
for itype=1:numberParticleClasses_
if (particleEnum==particles_{itype}.particleEnum) && ...
strcmp( resName, particles_{itype}.resName)
isUnique = false;
end
if ~isUnique
uniqueID = particles_{itype}.ID;
% Record which particle class this particle is in;
particleClassID_(iparticle) = itype;
break;
end
end
% Initialize if it does not already exist.
if isUnique && isParticleValid(particleEnum, pdb_.resName{ipdb})
addParticle_new(particleEnum, resName);
% Create new particle type.
uniqueID = particles_{numberParticleClasses_}.ID;
% Record which particle class this particle is in;
particleClassID_(iparticle) = particles_{numberParticleClasses_}.ID;
end
% Check if the particle is already a member of this particle class.
if any(pdbID_(particles_{uniqueID}.members)==ucpdb)
return;
end
% Add parttcle to member set.
particles_{uniqueID}.members = unique(...
[particles_{uniqueID}.members,iparticle]);
particles_{uniqueID}.number = length(particles_{uniqueID}.members);
% TO DO: REPLACE associatedParticlesCollection WITH associatedPDBMatrix.
particles_{uniqueID}.associatedParticlesCollection{...
particles_{uniqueID}.number} = associatedParticles;
if uc==1
for iAsso = 1:numel(associatedParticles)
iaParticle = associatedParticles(iAsso);
associatedPDBMatrix_(iaParticle,ipdb) = iaParticle;
end
end
% Record molecule ID number.
moleculeID_(iparticle) = pdb_.resSeq(ipdb) + (uc-1)*pdb_.number;
% Record PDB number.
if numel(pdbID_) >= iparticle && pdbID_(iparticle) ~= ucpdb
error(['Error in addParticle(): ',...
'particle ', num2str(iparticle), 'is already asigned pdb ID ',...
num2str(pdbID_(iparticle)), ' and cannot be re-asigned pdb ID ', ...
num2str(ucpdb), '.']);
end
pdbID_(iparticle) = ucpdb;
if iparticle == number_+1
% Count this particle.
number_ = number_ + 1;
elseif iparticle > number_ + 1
error('Error in addParticle(): particle count is incorrect.');
end
% Update residue list.
residueList_{iparticle} = resName;
if ~particles_{uniqueID}.exchangeable && ~any(moleculeID_(iparticle)...
==moleculeID_(particles_{uniqueID}.membersOnePerMolecule))
particles_{uniqueID}.membersOnePerMolecule = ...
[particles_{uniqueID}.membersOnePerMolecule,iparticle];
particles_{uniqueID}.numberMolecules = particles_{uniqueID}.numberMolecules+1;
end
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function addParticle_new(particleEnum, resName)
% Update particle type number.
numberParticleClasses_ = numberParticleClasses_ + 1;
% Set identification values.
particles_{numberParticleClasses_}.particleName = ...
getParticleString(particleEnum);
particles_{numberParticleClasses_}.resName = resName;
particles_{numberParticleClasses_}.particleEnum = particleEnum;
particles_{numberParticleClasses_}.ID = numberParticleClasses_;
% Initialize other values.
particles_{numberParticleClasses_}.members = [];
particles_{numberParticleClasses_}.membersOnePerMolecule = [];
particles_{numberParticleClasses_}.number = 0;
particles_{numberParticleClasses_}.numberMolecules = 0;
% Initialize option values.
particles_{numberParticleClasses_}.abundance = 1;
particles_{numberParticleClasses_}.active = true;
particles_{numberParticleClasses_}.atomType = PARTICLE_UNSET;
particles_{numberParticleClasses_}.associatedParticlesCollection = {};
particles_{numberParticleClasses_}.barrierPotential = 0;
particles_{numberParticleClasses_}.doRandomIsotopes = [];
particles_{numberParticleClasses_}.exchangeable = true;
particles_{numberParticleClasses_}.gFactor = 0;
particles_{numberParticleClasses_}.hf_Tzz = 0;
particles_{numberParticleClasses_}.hf_FermiContact = 0;
particles_{numberParticleClasses_}.hf_x = {};
particles_{numberParticleClasses_}.hf_y = {};
particles_{numberParticleClasses_}.hf_z = {};
particles_{numberParticleClasses_}.isNucleus = false;
particles_{numberParticleClasses_}.NQ_e2qQh = 0;
particles_{numberParticleClasses_}.NQ_eta = 0;
particles_{numberParticleClasses_}.NQ_x = {};
particles_{numberParticleClasses_}.NQ_y = {};
particles_{numberParticleClasses_}.NQ_z = {};
particles_{numberParticleClasses_}.spinMultiplicity = 1;
particles_{numberParticleClasses_}.switchParticle = PARTICLE_UNSET;
particles_{numberParticleClasses_}.extraCellSwitchParticle = PARTICLE_UNSET;
particles_{numberParticleClasses_}.tunnelSplitting = 0;
% Set default options.
setParticle(numberParticleClasses_, particleEnum,resName);
% Set user specified options.
setParticleOptions(numberParticleClasses_, System.particleOptions);
particleClassIndex = findParticleClasses(...
particles_{numberParticleClasses_}.switchParticle, resName);
if numel(particleClassIndex)==1 ...
&& particles_{particleClassIndex}.doRandomIsotopes
particles_{numberParticleClasses_}.abundance = ...
1 - particles_{particleClassIndex}.abundance;
elseif numel(particleClassIndex) > 1
error(['Error in addParticle_new():',...
' multiple switch particles found.']);
end
if isempty(particles_{numberParticleClasses_}.doRandomIsotopes)
particles_{numberParticleClasses_}.doRandomIsotopes = ...
(particles_{numberParticleClasses_}.abundance<1) && ...
particles_{numberParticleClasses_}.switchParticle ~= PARTICLE_UNSET;
end
return;
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function preliminaryParticle = analyzeParticle(index_pdb)
pdbParticle = getParticleClass(pdb_.element{index_pdb});
preliminaryParticle.particleEnum = PARTICLE_NONE;
preliminaryParticle.associatedParticles = [];
switch pdbParticle
% Hydrogen
case {PARTICLE_HYDROGEN, PARTICLE_PROTIUM, PARTICLE_DEUTERIUM}
if ~System.include_hydrogen
return;
end
% Assume protium unless specified otherwise.
isH1 = pdbParticle ~= PARTICLE_DEUTERIUM;
if (isH1 && ~System.include_1H) || (~isH1 && ~System.include_2H)
return;
end
isExchangeable = isHydronExchangeable(index_pdb);
% Set hydron type.
if isH1 && isExchangeable
preliminaryParticle.particleEnum = PARTICLE_1H_EXCHANGEABLE;
elseif ~isH1 && isExchangeable
preliminaryParticle.particleEnum = PARTICLE_2H_EXCHANGEABLE;
elseif isH1 && ~isExchangeable
preliminaryParticle.particleEnum = PARTICLE_1H_NONEXCHANGEABLE;
else
preliminaryParticle.particleEnum = PARTICLE_2H_NONEXCHANGEABLE;
end
% Carbon
case PARTICLE_CARBON
if System.Methyl.include
% Check if carbon is a methyl carbon.
methylHydrons = isMethyl(index_pdb);
% All indices will be unique if it is a methyl.
if ~(methylHydrons(1)==methylHydrons(2) ...
|| methylHydrons(1) == methylHydrons(3) ...
|| methylHydrons(2) == methylHydrons(3))
preliminaryParticle.particleEnum = PARTICLE_C_METHYL;
preliminaryParticle.associatedParticles = methylHydrons;
% numberMethyls_ = numberMethyls_ + 1;
% methylIDs_(numberMethyls_) = index_pdb;
end
elseif System.include_13C
preliminaryParticle.particleEnum = PARTICLE_12C;
end
% Nitrogen
case PARTICLE_NITROGEN
if System.include_14N
preliminaryParticle.particleEnum = PARTICLE_14N;
elseif System.include_15N
preliminaryParticle.particleEnum = PARTICLE_15N;
end
% Silicon
case PARTICLE_SILICON
if System.include_29Si
preliminaryParticle.particleEnum = PARTICLE_29SI;
end
% Electron
case PARTICLE_ELECTRON
if System.include_electron
preliminaryParticle.particleEnum = PARTICLE_ELECTRON;
end
otherwise
preliminaryParticle.particleEnum = PARTICLE_NONE;
end
return;
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
% TO DO: THIS FUNCTION IS TEMPORARY. CluE SHOULD BE RECONFIGURED TO WORK WITH
% THE DATA STRUCTURE OF centralSpinSystem(), WITHOUT A CONVERSION STEP.
function assembleNuclei()
% Initialize
Nuclei.Index = 1:(number_-1); % set here.
Nuclei.Type = cell(1,number_-1);
Nuclei.Element = cell(number_-1,1);
% Nuclei.Connected = cell(number_,1); % not needed.
Nuclei.Spin = zeros(1,number_-1);
Nuclei.StateMultiplicity = zeros(1,number_-1);
Nuclei.Nuclear_g = zeros(1,number_-1);
Nuclei.Coordinates = coordinates_(:,2:number_)'; % set here.
Nuclei.PDBCoordinates = zeros(number_ - 1,3);
Nuclei.pdbID = zeros(1,number_-1);
Nuclei.ucpdbID = zeros(1,number_-1);
Nuclei.MoleculeID = moleculeID_; % set here.
Nuclei.methylTunnelingSplitting = sparse(number_-1,number_-1);
Nuclei.Exchangeable = zeros(1,number_-1);
Nuclei.NumberStates = int8(zeros(1,number_-1));
Nuclei.MethylID = zeros(1,number_-1);
methylCounter = 0;
% Nuclei.valid = true(1,number_-1); % not needed.
% Nuclei.isWater = true(1,number_-1); % not needed.
Nuclei.isSolvent = true(1,number_-1);
Nuclei.valid = true(1,number_-1);
% Nuclei.hyperfine2lab = sparse(3*3,num_);
Nuclei.Atensor = sparse(number_-1,9);
Nuclei.FermiContact = sparse(number_-1,1);
Nuclei.hf_Tzz = sparse(number_-1,1);
Nuclei.associatedParticleMatrix = associatedParticleMatrix_;
Nuclei.associatedParticleMatrix(...
find(nonzeros(Nuclei.associatedParticleMatrix)));
Nuclei.number_1H_exchangeable = ...
findParticles(PARTICLE_1H_EXCHANGEABLE, 'ALL', true);
Nuclei.number_1H_nonExchangeable = ...
findParticles(PARTICLE_1H_NONEXCHANGEABLE, 'ALL', true)...
+ findParticles(PARTICLE_1H_METHYL, 'ALL', true);
Nuclei.number_2H_exchangeable = ...
findParticles(PARTICLE_2H_EXCHANGEABLE, 'ALL', true);
Nuclei.number_2H_nonExchangeable = ...
findParticles(PARTICLE_2H_NONEXCHANGEABLE, 'ALL', true)...
+ findParticles(PARTICLE_2H_METHYL, 'ALL', true);
if ~System.limitToSpinHalf
Nuclei.Qtensor = zeros(3,3,number_-1);
end
for iparticle = 2:number_
iNuc = iparticle - 1;
itype = particleClassID_(iparticle);
particleEnum = particles_{itype}.particleEnum;
ipdb = mod(pdbID_(iparticle),pdb_.number);
if ipdb==0, ipdb = pdb_.number; end
uc = ceil(pdbID_(iparticle)/pdb_.number);
Nuclei.Type{iNuc} = getParticleTypeString(particleEnum);
Nuclei.Element{iNuc} = pdb_.element{ipdb};
Nuclei.PDBCoordinates(iNuc,:) = [pdb_.x(ipdb),pdb_.y(ipdb),pdb_.z(ipdb)]...
+ cellShifts_(:,uc)';
Nuclei.pdbID(iNuc) = pdbID_(iparticle);
Nuclei.Spin(iNuc) = (particles_{itype}.spinMultiplicity - 1)/2;
Nuclei.StateMultiplicity(iNuc) = particles_{itype}.spinMultiplicity;
Nuclei.Nuclear_g(iNuc) = particles_{itype}.gFactor;
Nuclei.Exchangeable(iNuc) = particles_{itype}.exchangeable;
Nuclei.NumberStates(iNuc) = int8(particles_{itype}.spinMultiplicity);
Nuclei.isSolvent(iNuc) = isMoleculeSolvent(particles_{itype}.resName);
if Nuclei.valid(iNuc)
Nuclei.valid(iNuc) = particles_{itype}.active;
end
if particles_{itype}.NQ_e2qQh ~= 0
idx = particles_{itype}.members==iparticle;
Nuclei.Qtensor(:,:,iNuc) = particles_{itype}.Qtensor{idx};
end
if particles_{itype}.hf_Tzz ~= 0 || particles_{itype}.hf_FermiContact ~= 0
idx = particles_{itype}.members==iparticle;
Nuclei.Atensor(iNuc,:) = particles_{itype}.Atensor{idx}(:);
end
if particleEnum == PARTICLE_C_METHYL
methylCounter = methylCounter + 1;
% hydrons = getAssociatedParticles(itype,iparticle);
associatedWithMethyl = nonzeros(associatedParticleMatrix_(:,iparticle));
if System.Methyl.method==1
Nuclei.valid(associatedWithMethyl-1) = false;
end
hydrons = zeros(3,1);
hydronCounter = 0;
for iasso =associatedWithMethyl'
if particles_{particleClassID_(iasso)}.particleEnum ~=PARTICLE_1H_METHYL...
&& particles_{particleClassID_(iasso)}.particleEnum ~=PARTICLE_2H_METHYL
continue;
end
hydronCounter = hydronCounter + 1;
hydrons(hydronCounter) = iasso - 1;
end
if hydronCounter ~= 3
error(['Error in assembleNuclei(): ', ...
'methyl group has', num2str(hydronCounter), ' protons.']);
end
% hydrons = nonzeros(associatedParticleMatrix_(:,iparticle)) - 1;
nuT = particles_{itype}.tunnelSplitting;
for ih = 1:2
iH = hydrons(ih);
for jh = ih+1:3
jH = hydrons(jh);
Nuclei.methylTunnelingSplitting(iH,jH) = nuT;
Nuclei.methylTunnelingSplitting(jH,iH) = nuT;
end
end
Nuclei.MethylID(iNuc) = -methylCounter;
for ih = 1:3
iH = hydrons(ih);
Nuclei.MethylID(iH) = methylCounter;
end
% error(['Error in assembleNuclei(): ', ...
% 'Nuclei.MethylID not set. Please update source code.'])
end
end
defineSpinOperators();
Nuclei.nStates = System.nStates;
Nuclei.number = 0;
% Define spin operators.
%Nuclei =
%Nuclei,System, Method);
% Copy graphCriterion to Nuclei.
Nuclei.graphCriterion = Method.graphCriterion;
% Set electron coordinates in the pdb frame.
Nuclei.Electron_pdbCoordinates = originVec_';
if System.Methyl.include %&& System.Methyl.method ~= 2
Nuclei.Methyl_Data.number_methyls = methylCounter;
% error('Error in centralSpinSystem(): methyl code not yet implemented.');
else
Nuclei.Methyl_Data = [];
end
% Decide whether or not to add randomly distributed hard spheres.
if System.RandomEnsemble.include
error('Error in centralSpinSystem(): addRandomSpins() not yet implemented.');
end
% get number of nuclei
Nuclei.number = number_-1;
% Rotate coordinates if requested by user via System.X/Y/Z
% [Nuclei,System] =
setOrientation(); %Nuclei,System, pdbCoordinates);
% Clear excess entries.
% Nuclei =
% cleanUpNuclei(); %Nuclei,System,Method,Npdb);
% Get coupling statistics.
% Nuclei =
computeNuclearInteractions(); %Nuclei,System, Method,scaleFactor);
if Data.writeSpinPDB
% try
outPDBfile = Data.OutputData;
if numel(outPDBfile)>3 && strcmp(outPDBfile(end-3:end),'.mat')
outPDBfile = outPDBfile(1:end-4);
end
outPDBfile = [outPDBfile,'_spinSystem.pdb'];
writeSpinPDB(Nuclei,ones(1,Nuclei.number),...
outPDBfile,Data.outPDBoptions);
% catch
% warning('Warning: could not write spin pdb.')
% end
end
% Clean
% Nuclei.State = [];
if ~Method.getNuclearStatistics
Statistics = struct();
Statistics.Nuclear_Dipole = Nuclei.Statistics.Nuclear_Dipole;
Statistics.Nuclear_Dipole_x_iy_Z = Nuclei.Statistics.Nuclear_Dipole_x_iy_Z;
if strcmp(Method.method,'rCCE') ||strcmp(Method.method,'rCE')
Statistics.Modulation_Depth_methyl = Nuclei.Statistics.Modulation_Depth_methyl;
Statistics.Frequency_Pair_methyl = Nuclei.Statistics.Frequency_Pair_methyl;
end
Nuclei.Statistics = Statistics;
Nuclei.DistanceMatrix = [];
end
if ~Method.getNuclearContributions
Nuclei.PDBCoordinates = [];
Nuclei.Element = [];
end
% if System.newIsotopologuePerOrientation
Nuclei.MoleculeIDunique = unique(Nuclei.MoleculeID);
% else
% Nuclei.MoleculeID = [];
% Nuclei.Connected = [];
% Nuclei.isWater = [];
% end
checkNuclei();
return;
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function buildAssociatedParticleMatrix()
% Resize matrix.
associatedParticleMatrix_(number_,number_) = 0;
% Loop through all particles in system.
for iParticle = 1:number_
% Get pdb information.
ucpdb = pdbID_(iParticle);
uc = ceil(ucpdb/pdb_.number);
ipdb = mod(ucpdb,pdb_.number);
if ipdb==0, ipdb = pdb_.number; end
% Get non-zero column elements, the particles associated with ipdb.
associatedPDBcol = nonzeros(associatedPDBMatrix_(:,ipdb));
for iAsso = 1:numel(associatedPDBcol)
% Find associated particles if they exist.
assoiParticle = find(pdbID_ == ...
(associatedPDBcol(iAsso) + (uc-1)*pdb_.number));
% Add missing associated particles.
if isempty(assoiParticle)
itype = particleClassID_(iParticle);
assoiParticle = number_;
associatedPart = analyzeParticle(associatedPDBcol(iAsso));
addParticle(associatedPart.particleEnum, particles_{itype}.resName, ...
[], number_+1, (uc-1)*pdb_.number + associatedPDBcol(iAsso));
if assoiParticle==number_
error(['Error in buildAssociatedParticlematrix(): ', ...
'particle could not be added.']);
else
assoiParticle = number_;
end
elseif numel(assoiParticle)>1
error(['Error in buildAssociatedParticlematrix(): ', ...
'particle has been added multiple times.']);
end
associatedParticleMatrix_(assoiParticle,iParticle) = assoiParticle;
end
end
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function buildExtraCells()
cellShifts_ = getCellShifts()';
generateUniqueResidueList();
% Print matrix.
if verbose_
disp("cells shifts (meter) = ");
disp(cellShifts_);
end
% Copy pdbID before erasing.
pdbID = pdbID_;
% Remove all particles but leave settings.
clearBath();
numUnitCells = size( cellShifts_,2);
if numUnitCells == 0
return;
end
for itype = 1:numberParticleClasses_
% Get particle label.
particleEnum = particles_{itype}.particleEnum;
if particleEnum==PARTICLE_CENTRALSPIN
continue;
end
% Get number of particles in one cell.
if particles_{itype}.exchangeable
% Copy particle indices.
indices = particles_{itype}.members;
N = particles_{itype}.number;
else
% Copy particle indices.
indices = particles_{itype}.membersOnePerMolecule;
N = particles_{itype}.numberMolecules;
end
ucN = numUnitCells*N ;
clearMembers(itype);
useRandomParticles = false;
if particles_{itype}.switchParticle == PARTICLE_VOID ...
&& particles_{itype}.abundance < 1
useRandomParticles = true;
% Get total number of particles in all unit cells.
ucNtotal = numUnitCells*N;
% Get number to use.
pd = makedist('Binomial','N',ucNtotal,'p',particles_{itype}.abundance);
ucN = random(pd);
% Draw a random set of indices.
selectedIndices = randperm(ucNtotal,ucN);
elseif particles_{itype}.extraCellSwitchParticle == PARTICLE_VOID ...
&& particles_{itype}.abundance < 1
useRandomParticles = true;
% Get total number of particles in all unit cells outside the primary cell.
ucNtotal = (numUnitCells-1)*N;
% Get number to use.
pd = makedist('Binomial','N',ucNtotal,'p',particles_{itype}.abundance);
ucN = random(pd);
% Use all indices from the primary cell
% and draw a random set of indices for the extra cells.
selectedIndices =[1:N, N + randperm(ucNtotal,ucN)];
ucN = ucN + N;
end
% Loop through particles.
for ii=1:ucN
particleIndex = ii;
if useRandomParticles
particleIndex = selectedIndices(ii);
end
% Get unit cell. Note that N = particles_{itype}.number. The devision by
% N rather than pdb_.number is because particleIndex runs over particles
% of particles_{itype}.
uc = ceil( particleIndex/ N );
% Define index to store unit cell and ipdb info together.
ucIndexOffset = (uc - 1)*pdb_.number;
% Get index of particle copy in the primary cell.
particleIndex = mod( particleIndex, N);
if particleIndex==0, particleIndex = N; end
baseIndex = indices( particleIndex );
% Get PDB number.
ipdb = pdbID( baseIndex );
if particles_{itype}.exchangeable
sameMoleculeList = pdbID(baseIndex);
else
sameMoleculeList = pdb_.serial( pdb_.resSeq==pdb_.resSeq(ipdb) );
end
% Add particle copy of particle in this unit cell.
resName = particles_{itype}.resName;
for jpdb=sameMoleculeList'
if ~strcmp(pdb_.resName(jpdb),resName)
continue;
end
if ~isParticleWithinSystem(r0_, jpdb, cellShifts_(:,uc),true), continue; end
% Get particleEnum and associatedParticles.
preliminaryParticle = analyzeParticle(jpdb);
if preliminaryParticle.particleEnum ~= particleEnum, continue; end
% Define index to store unit cell and ipdb info together.
ucpdb = jpdb + ucIndexOffset;
% Add unit cell offset to associatedParticles.
associatedParticles = preliminaryParticle.associatedParticles ...
+ ucIndexOffset;
% Since methyl carbons add their hydrons, some particle may already be
% added.
alreadyAdded = find(pdbID_==ucpdb);
if ~isempty(alreadyAdded)
if numel(alreadyAdded) > 1
error(['Error in buidExtraCellss(): ', ...
'pdb ID ',num2str(ucpbd),' added multiple times.']);
end
alreadyAddedID = particleClassID_(alreadyAdded);
if particles_{alreadyAddedID}.particleEnum ...
~= preliminaryParticle.particleEnum
error(['Error in buildExtraCells: ',...
'cannot overwrite particle index ', num2str(alreadyAdded), ...
', ', getParticleString(particles_{alreadyAddedID}.particleEnum),...
' with ',getParticleString(preliminaryParticle.particleEnum), '.']);
end
continue;
end
addParticle(particleEnum,resName, associatedParticles, number_+1, ucpdb);
end
end
end
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function buildPrimaryCell()
% numberMethyls_ = 0;
% Loop through all nuclei.
for ipdb = 1:pdb_.number
% Ignore particles outside the system radius.
if ~isParticleWithinSystem(r0_, ipdb, 0, false), continue; end
preliminaryParticle = analyzeParticle(ipdb);
if preliminaryParticle.particleEnum ~= PARTICLE_NONE
% Add particle.
resName = pdb_.resName{ipdb};
addParticle(preliminaryParticle.particleEnum,resName,...
preliminaryParticle.associatedParticles, number_+1, ipdb);
end
end
return;
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function buildSystem()
% Initialize the observed spin.
addParticle_new(PARTICLE_CENTRALSPIN, 'detect');
particles_{numberParticleClasses_}.members = 0;
particles_{numberParticleClasses_}.number = 1;
residueList_{numberParticleClasses_} = 'detect';
particleClassID_(numberParticleClasses_) = numberParticleClasses_;
moleculeID_(numberParticleClasses_) = 0;
number_ = number_ + 1;
zeroIndex_ = number_;
generateUniqueResidueList();
buildPrimaryCell();
buildExtraCells();
buildAssociatedParticleMatrix()
% Adjust indices for dropped particles.
%adjustAssociatedParticles();
% Set methyls.
setMethyls();
return;
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function changeIsotope(iParticle)
% Get ParticleClass index number.
type_idx = particleClassID_(iParticle);
% Get the new particle type.
newParticle = particles_{type_idx}.switchParticle;
% Get the unit cell pdb number.
ucpdb = pdbID_(iParticle);
% Get unit cell index.
uc = ceil(ucpdb/pdb_.number);
% Check if the particle is outside the primary cell and has a different
% switch particle defined for the extra cell.
% Note, void particle should have accounted for already.
if uc > 1 ...
&& isParticleValid(particles_{type_idx}.extraCellSwitchParticle, ...
particles_{type_idx}.resName) ...
&& particles_{type_idx}.extraCellSwitchParticle ~= PARTICLE_VOID
newParticle = particles_{type_idx}.extraCellSwitchParticle;
end
% Get the indices for any associated particles.
associatedParticles = ...
particles_{type_idx}.associatedParticlesCollection{...
particles_{type_idx}.members==iParticle};
% Remove particle from current ParticleClass.
if ~removeMember(type_idx, iParticle)
error(['Error in changeIsotope(): could not remove particle ', ...
num2str(ucpdb), ' from particle class ', ...
getParticleString(particles_{type_idx}.particleEnum), ' ', ...
particles_{type_idx}.resName, '.']);
end
resName = particles_{type_idx}.resName;
addParticle(newParticle, resName, associatedParticles , iParticle, ucpdb);
return;
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function allPass = checkNuclei()
METHYLID = 1;
pass(METHYLID) = true;
if System.Methyl.include
for ii=1:Nuclei.number
if Nuclei.MethylID(ii) > 0
pass(METHYLID) = pass(METHYLID)*( ...
sum(Nuclei.MethylID==Nuclei.MethylID(ii)) ==3);
elseif Nuclei.MethylID(ii) < 0
pass(METHYLID) = pass(METHYLID)*( ...
sum(Nuclei.MethylID==Nuclei.MethylID(ii)) ==1);
end
if ~pass(METHYLID)
error(['Error in checkNuclei(): ', 'methylID is not correctly set up.']);
end
end
end
allPass = all(pass(METHYLID));
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
% This function checks the system for errors. More checks should be added as
% as bugs are are found and/or the code grows.
function checkSystem()
n = 0;
nCmethyl = 0;
nHmethyl = 0;
nDmethyl = 0;
allMembers = zeros(number_,1) - 1;
% Check that the number of particles matches the count from all particle
% classes.
for itype=1:numberParticleClasses_
n0 = particles_{itype}.number;
n1 = length(particles_{itype}.members);
if n0 ~= n1
error([ ...
'Error in checkSystem(): ', 'particle class ', ...