-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathdiffBragg_cpu_kernel.cpp
More file actions
1303 lines (1177 loc) · 58.3 KB
/
diffBragg_cpu_kernel.cpp
File metadata and controls
1303 lines (1177 loc) · 58.3 KB
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
#include <simtbx/diffBragg/src/diffBragg.h>
#include <simtbx/diffBragg/src/diffuse_util.h>
#include <assert.h>
#include <stdbool.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#if defined(_OPENMP)
#include<omp.h>
#endif
namespace simtbx { namespace nanoBragg { // BEGIN namespace simtbx::nanoBragg
double diffBragg_cpu_kernel_polarization (Eigen::Vector3d incident, Eigen::Vector3d diffracted,
Eigen::Vector3d polarization_axis, double kahn_factor){
// component of diffracted unit vector along incident beam unit vector
double cos2theta = incident.dot(diffracted);
double cos2theta_sqr = cos2theta*cos2theta;
double sin2theta_sqr = 1-cos2theta_sqr;
double psi=0;
if(kahn_factor != 0.0){
// cross product to get "vertical" axis that is orthogonal to the cannonical "polarization"
Eigen::Vector3d B_in = polarization_axis.cross(incident);
// cross product with incident beam to get E-vector direction
Eigen::Vector3d E_in = incident.cross(B_in);
// get components of diffracted ray projected onto the E-B plane
double kEi = diffracted.dot(E_in);
double kBi = diffracted.dot(B_in);
// compute the angle of the diffracted ray projected onto the incident E-B plane
psi = -atan2(kBi,kEi);
}
// correction for polarized incident beam
double polar = 0.5*(1.0 + cos2theta_sqr - kahn_factor*cos(2*psi)*sin2theta_sqr);
return polar;
}
int get_bin(std::vector<double> dspace_bins, double dspace_sample){
// use binary search to find bin corresponding to dspace_sample as dspace_bins are unevenly sized
int start = 0;
int end = dspace_bins.size() - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (dspace_bins[mid] == dspace_sample)
return mid;
else if (dspace_bins[mid] < dspace_sample)
start = mid + 1;
else
end = mid - 1;
}
return end + 1;
}
std::vector<double> I_cell_ave(crystal& db_cryst, bool use_Fhkl_scale, int i_channel,
std::vector<double>& Fhkl_scale){
std::vector<double> ave;
std::vector<double> count;
for (int i=0; i < db_cryst.dspace_bins.size(); i++){
ave.push_back(0);
count.push_back(0);
}
#pragma omp parallel for
for (int i=0; i < db_cryst.ASU_dspace.size(); i++){
double d = db_cryst.ASU_dspace[i];
double F_cell = db_cryst.ASU_Fcell[i];
if (F_cell==0){
continue;
}
int bin = get_bin(db_cryst.dspace_bins, d);
if (bin == 0 || bin >= db_cryst.dspace_bins.size() )
continue;
double scale = 1;
if (use_Fhkl_scale)
scale = Fhkl_scale[i + db_cryst.Num_ASU*i_channel];
double summand=scale*F_cell*F_cell;
if (db_cryst.use_geometric_mean)
summand = log(summand);
#pragma omp atomic
ave[bin] += summand;
#pragma omp atomic
count[bin] ++;
}
for (int i=0; i <ave.size(); i++){
if (count[i]>0){
if (db_cryst.use_geometric_mean)
ave[i] = exp(ave[i]/count[i]);
else
ave[i] = ave[i]/count[i];
}
}
for (int i=0; i < count.size(); i++)
ave.push_back(count[i]);
return ave;
}
void Ih_grad_terms(crystal& db_cryst, int i_chan, std::vector<double>& Fhkl_scale, std::vector<double>& out){
std::vector<double> ave_and_count = I_cell_ave(db_cryst, true, i_chan, Fhkl_scale);
std::vector<double> ave, count;
for (int i=0; i < ave_and_count.size()/2; i++){
ave.push_back(ave_and_count[i]);
count.push_back(ave_and_count[i+ave_and_count.size()/2]);
}
ave_and_count = I_cell_ave(db_cryst, false, i_chan, Fhkl_scale);
std::vector<double> ave_init;
for (int i=0; i < ave_and_count.size()/2; i++){
ave_init.push_back(ave_and_count[i]);
}
#pragma omp parallel for
for (int i=0; i < db_cryst.Num_ASU; i++){
double dsp = db_cryst.ASU_dspace[i];
int bin = get_bin(db_cryst.dspace_bins, dsp);
if (bin==0 || bin>=db_cryst.dspace_bins.size())
continue;
if(count[bin]==0) continue;
double F_cell = db_cryst.ASU_Fcell[i];
int idx = i_chan*db_cryst.Num_ASU + i;
double scale_hkl = Fhkl_scale[idx];
double I_cell_init = F_cell*F_cell;
double I_cell = scale_hkl * I_cell_init;
double U = ave[bin] - ave_init[bin];
double grad_term;
if (db_cryst.use_geometric_mean){
double grad_term_right = 1/count[bin] * ave[bin] / scale_hkl;
grad_term = U/db_cryst.Fhkl_beta * grad_term_right;
}
else {
grad_term = U/db_cryst.Fhkl_beta * I_cell_init/count[bin];
}
#pragma omp atomic
out[i] += grad_term;
}
double ftarget=0;
for (int i=0; i < ave.size(); i++){
double U = (ave[i] - ave_init[i]);
ftarget += 0.5*( log(2*M_PI*db_cryst.Fhkl_beta) + U*U/db_cryst.Fhkl_beta);
}
out[db_cryst.Num_ASU] = ftarget; // this is an addition to the target function for this particular restraint, added on for convenience
}
//void Ih_grad_terms(crystal& db_cryst, int i_chan, std::vector<double>& Fhkl_scale, std::vector<double>& out){
// std::vector<double> ave_and_count = I_cell_ave(db_cryst, true, i_chan, Fhkl_scale);
// std::vector<double> ave, count;
// for (int i=0; i < ave_and_count.size()/2; i++){
// ave.push_back(ave_and_count[i]);
// count.push_back(ave_and_count[i+ave_and_count.size()/2]);
// }
//
// //ave_and_count = I_cell_ave(db_cryst, false, i_chan, Fhkl_scale);
// //std::vector<double> ave_init;
// //for (int i=0; i < ave_and_count.size()/2; i++){
// // ave_init.push_back(ave_and_count[i]);
// //}
//
// #pragma omp parallel for
// for (int i=0; i < db_cryst.Num_ASU; i++){
// double dsp = db_cryst.ASU_dspace[i];
// int bin = get_bin(db_cryst.dspace_bins, dsp);
// if (bin==0 || bin>=db_cryst.dspace_bins.size())
// continue;
// if(count[bin]==0) continue;
//
// double F_cell = db_cryst.ASU_Fcell[i];
// int idx = i_chan*db_cryst.Num_ASU + i;
// double scale_hkl = Fhkl_scale[idx];
// double I_cell_init = F_cell*F_cell;
// double I_cell = scale_hkl * I_cell_init;
//
// double U;
// if (bin==1){
// U = ave[bin] - ave[bin+1];
// }
// else if (bin==db_cryst.dspace_bins.size()-1) {
// U = ave[bin] - ave[bin-1];
// }
// else {
// U = 2*ave[bin] - ave[bin+1] - ave[bin-1];
// }
// double grad_term = U/db_cryst.Fhkl_beta * I_cell_init/count[bin];
//
// //if (db_cryst.use_geometric_mean){
// // double grad_term_right = 1/count[bin] * ave[bin] / scale_hkl;
// // grad_term = U/db_cryst.Fhkl_beta * grad_term_right;
// //}
// //else {
// // grad_term = U/db_cryst.Fhkl_beta * I_cell_init/count[bin];
// //}
//
// #pragma omp atomic
// out[i] += grad_term;
// }
//
// double ftarget=0;
// for (int i=1; i < ave.size()-1; i++){
// double U = (ave[i] - ave[i+1]);
// ftarget += 0.5*( log(2*M_PI*db_cryst.Fhkl_beta) + U*U/db_cryst.Fhkl_beta);
// }
//
// out[db_cryst.Num_ASU] = ftarget;
//}
void Finit_grad_terms(crystal& db_cryst, int i_chan, std::vector<double>& Fhkl_scale, std::vector<double>& out){
std::vector<double> ave_and_count = I_cell_ave(db_cryst, true, i_chan, Fhkl_scale);
std::vector<double> ave, count;
for (int i=0; i < ave_and_count.size()/2; i++){
ave.push_back(ave_and_count[i]);
count.push_back(ave_and_count[i+ave_and_count.size()/2]);
}
double ftarget=0;
#pragma omp parallel for reduction(+:ftarget)
for (int i=0; i < db_cryst.Num_ASU; i++){
double dsp = db_cryst.ASU_dspace[i];
int bin = get_bin(db_cryst.dspace_bins, dsp);
if (bin==0 || bin>=db_cryst.dspace_bins.size())
continue;
double N = count[bin];
if(N==0) continue;
double F_cell = db_cryst.ASU_Fcell[i];
double I_cell_init = F_cell*F_cell;
int idx = i_chan*db_cryst.Num_ASU + i;
double scale_hkl = Fhkl_scale[idx];
double I_cell_current = scale_hkl * I_cell_init;
double I_ave = ave[bin];
double U = (I_cell_current - I_cell_init) / I_ave;
ftarget += U*U/2/db_cryst.Finit_beta;
double grad_term = U/db_cryst.Finit_beta * I_cell_init/I_ave *(1-U/N);
#pragma omp atomic
out[i] += grad_term;
}
out[db_cryst.Num_ASU] = ftarget;
}
void Friedel_grad_terms(crystal& db_cryst, int i_chan, std::vector<double>& Fhkl_scale, std::vector<double>& out){
SCITBX_ASSERT(db_cryst.neg_inds.size() == db_cryst.pos_inds.size()) ;
double log_beta = log(2*M_PI*db_cryst.Friedel_beta);
double ftarget = 0;
#pragma omp parallel for reduction(+:ftarget)
for (int i_pair=0; i_pair < db_cryst.neg_inds.size(); i_pair++){
int i_minus = db_cryst.neg_inds[i_pair];
int i_plus = db_cryst.pos_inds[i_pair] ;
int channel_offset = db_cryst.Num_ASU*i_chan;
double s_minus = Fhkl_scale[channel_offset + i_minus];
double s_plus = Fhkl_scale[channel_offset + i_plus];
double F_minus = db_cryst.ASU_Fcell[i_minus];
double I_minus = F_minus*F_minus;
double F_plus = db_cryst.ASU_Fcell[i_plus];
double I_plus = F_plus*F_plus;
double sI_plus = s_plus*I_plus;
double sI_minus = s_minus*I_minus;
double S = sI_plus + sI_minus; // 'S' is for summed intensity
if (S==0) continue;
double D = sI_plus - sI_minus; // 'D' is for difference intensity
double friedel_diff = 0.5*D/S; // normalized difference intensity - we want to restrain this ...
ftarget += friedel_diff*friedel_diff / db_cryst.Friedel_beta + log_beta;
double D_by_S = D/S;
double friedel_diff_by_beta = .5* friedel_diff / db_cryst.Friedel_beta;
double grad_incr_s_plus = friedel_diff_by_beta * I_plus/S *(1-D_by_S);
double grad_incr_s_minus = -friedel_diff_by_beta * I_minus/S * (1+D_by_S);
#pragma omp atomic
out[i_plus] += grad_incr_s_plus;
#pragma omp atomic
out[i_minus] += grad_incr_s_minus;
}
out[db_cryst.Num_ASU] = ftarget*0.5;
}
void diffBragg_sum_over_steps(
int Npix_to_model, std::vector<unsigned int>& panels_fasts_slows,
image_type& floatimage,
images& d_image,
images& d2_image,
step_arrays& db_steps,
detector& db_det,
beam& db_beam,
crystal& db_cryst,
flags& db_flags){
MAT3 anisoG_local;
MAT3 anisoU_local;
MAT3 laue_mats[24];
MAT3 dG_dgam[3];
int laue_group_num = db_cryst.laue_group_num;
int num_laue_mats = 1;
int dhh=0, dkk=0, dll=0;
if (db_flags.use_diffuse){
anisoG_local = db_cryst.anisoG;
anisoU_local = db_cryst.anisoU;
if (laue_group_num < 1 || laue_group_num >14 ){
throw std::string("Laue group number not in range 1-14");
}
num_laue_mats = gen_laue_mats(laue_group_num, laue_mats, db_cryst.rotate_principal_axes);
for (int i_gam=0; i_gam<3; i_gam++){
dG_dgam[i_gam] << 0,0,0,0,0,0,0,0,0;
dG_dgam[i_gam](i_gam, i_gam) = 1;
}
dhh = dkk = dll = db_cryst.stencil_size; // Limits of stencil for diffuse calc
}
VEC3 Hmin(db_cryst.h_min, db_cryst.k_min, db_cryst.l_min);
VEC3 Hmax(db_cryst.h_max, db_cryst.k_max, db_cryst.l_max);
VEC3 dHH(dhh,dkk,dll);
VEC3 Hrange(db_cryst.h_range, db_cryst.k_range, db_cryst.l_range);
if (db_flags.track_Fhkl_indices)
db_cryst.Fhkl_grad_idx_tracker.clear();
#if defined(_OPENMP)
int dyn_state = omp_get_dynamic();
int nthread_state = omp_get_max_threads();
if (db_flags.track_Fhkl_indices){
omp_set_dynamic(0);
omp_set_num_threads(1);
}
#endif
#pragma omp parallel for
for (int i_pix=0; i_pix < Npix_to_model; i_pix++){
#if defined(_OPENMP)
if (db_flags.track_Fhkl_indices)
SCITBX_ASSERT(omp_get_num_threads()==1);
#endif
if (db_flags.using_trusted_mask){
if (! d_image.trusted[i_pix])
continue;
}
int pid = panels_fasts_slows[i_pix*3];
int fpixel = panels_fasts_slows[i_pix*3+1];
int spixel = panels_fasts_slows[i_pix*3+2];
double Fhkl_deriv_coef=0;
double Fhkl_hessian_coef=0;
if (db_flags.Fhkl_gradient_mode){
double u = d_image.residual[i_pix];
double one_by_v = 1/d_image.variance[i_pix];
double Gterm = 1 - 2*u - u*u*one_by_v;
Fhkl_deriv_coef = 0.5 * Gterm*one_by_v / d_image.freq[i_pix];
if (db_flags.Fhkl_errors_mode){
Fhkl_hessian_coef = -0.5*one_by_v*(one_by_v*Gterm - 2 - 2*u*one_by_v -u*u*one_by_v*one_by_v)/d_image.freq[i_pix];
}
}
double close_distance = db_det.close_distances[pid];
//std::unordered_map<int, int> Fhkl_tracker;
std::unordered_map<std::string, int> Fhkl_tracker;
bool use_nominal_hkl = false;
if (!db_cryst.nominal_hkl.empty())
use_nominal_hkl = true;
// reset photon count for this pixel
double I=0;
double Ilambda = 0;
double Imiller_h =0;
double Imiller_k =0;
double Imiller_l =0;
double II_max = -1;
double max_stats[11] = {0,0,0,0,0,
0,0,0,0,0,0};
double max_stats2[11] = {0,0,0,0,0,
0,0,0,0,0,0};
// reset derivative photon counts for the various parameters
double rot_manager_dI[3] = {0,0,0};
double rot_manager_dI2[3] = {0,0,0};
double ucell_manager_dI[6]= {0,0,0,0,0,0};
double ucell_manager_dI2[6]= {0,0,0,0,0,0};
double Ncells_manager_dI[6]= {0,0,0,0,0,0};
double Ncells_manager_dI2[6]= {0,0,0,0,0,0};
double pan_orig_manager_dI[3]= {0,0,0};
double pan_orig_manager_dI2[3]= {0,0,0};
double pan_rot_manager_dI[3]= {0,0,0};
double pan_rot_manager_dI2[3]= {0,0,0};
double fcell_manager_dI=0;
double fcell_manager_dI2=0;
double eta_manager_dI[3] = {0,0,0};
double eta_manager_dI2[3] = {0,0,0};
double lambda_manager_dI[2] = {0,0};
double lambda_manager_dI2[2] = {0,0};
double fp_fdp_manager_dI[2] = {0,0};
double dI_latt_diffuse[6] = {0,0,0,0,0,0};
// compute unit cell volume
Eigen::Vector3d ap_vec(1e10*db_cryst.eig_B(0,0), 1e10*db_cryst.eig_B(1,0), 1e10*db_cryst.eig_B(2,0));
Eigen::Vector3d bp_vec(1e10*db_cryst.eig_B(0,1), 1e10*db_cryst.eig_B(1,1), 1e10*db_cryst.eig_B(2,1));
Eigen::Vector3d cp_vec(1e10*db_cryst.eig_B(0,2), 1e10*db_cryst.eig_B(1,2), 1e10*db_cryst.eig_B(2,2));
double cell_vol = ap_vec.dot(bp_vec.cross(cp_vec)); // cubic Angstrom
double xtal_size = pow(db_cryst.Na*db_cryst.Nb*db_cryst.Nc*cell_vol, (double)1/double(3)); // Angstrom
for (int i_step=0; i_step < db_steps.Nsteps; i_step++){
int subS = db_steps.subS_pos[i_step];
int subF = db_steps.subF_pos[i_step];
int thick_tic = db_steps.thick_pos[i_step];
int source = db_steps.source_pos[i_step];
int phi_tic = db_steps.phi_pos[i_step];
int mos_tic = db_steps.mos_pos[i_step];
/* absolute mm position on detector (relative to its origin) */
double Fdet = db_det.subpixel_size*(fpixel*db_det.oversample + subF ) + db_det.subpixel_size/2.0;
double Sdet = db_det.subpixel_size*(spixel*db_det.oversample + subS ) + db_det.subpixel_size/2.0;
/* assume "distance" is to the front of the detector sensor layer */
double Odet = thick_tic*db_det.detector_thickstep;
int pid_x = pid*3;
int pid_y = pid*3+1;
int pid_z = pid*3+2;
Eigen::Vector3d o_vec(db_det.odet_vectors[pid_x], db_det.odet_vectors[pid_y], db_det.odet_vectors[pid_z]);
double pixposX = Fdet*db_det.fdet_vectors[pid_x]+Sdet*db_det.sdet_vectors[pid_x]+Odet*db_det.odet_vectors[pid_x]+db_det.pix0_vectors[pid_x];
double pixposY = Fdet*db_det.fdet_vectors[pid_y]+Sdet*db_det.sdet_vectors[pid_y]+Odet*db_det.odet_vectors[pid_y]+db_det.pix0_vectors[pid_y];
double pixposZ = Fdet*db_det.fdet_vectors[pid_z]+Sdet*db_det.sdet_vectors[pid_z]+Odet*db_det.odet_vectors[pid_z]+db_det.pix0_vectors[pid_z];
Eigen::Vector3d pixel_pos(pixposX, pixposY, pixposZ);
double airpath = pixel_pos.norm();
Eigen::Vector3d diffracted = pixel_pos/airpath;
double omega_pixel = db_det.pixel_size*db_det.pixel_size/airpath/airpath*close_distance/airpath;
if(db_flags.point_pixel) omega_pixel = 1.0/airpath/airpath;
double capture_fraction = 1;
if(db_det.detector_thick > 0.0 && db_det.detector_attnlen > 0.0)
{
double parallax = diffracted.dot(o_vec) ;
capture_fraction = exp(-thick_tic*db_det.detector_thickstep/db_det.detector_attnlen/parallax)
-exp(-(thick_tic+1)*db_det.detector_thickstep/db_det.detector_attnlen/parallax);
}
Eigen::Vector3d incident(-db_beam.source_X[source], -db_beam.source_Y[source], -db_beam.source_Z[source]);
double lambda = db_beam.source_lambda[source];
double lambda_ang = lambda*1e10;
if (db_flags.use_lambda_coefficients){
lambda_ang = db_beam.lambda0 + db_beam.lambda1*lambda_ang;
lambda = lambda_ang*1e-10;
}
/* construct the incident beam unit vector while recovering source distance */
double source_path = incident.norm();
incident /= source_path;
/* construct the scattering vector for this pixel */
//vec3 scattering = (diffracted - incident) / lambda;
Eigen::Vector3d scattering = (diffracted - incident) / lambda;
/* sin(theta)/lambda is half the scattering vector length */
double stol = 0.5*(scattering.norm()); //magnitude(scattering);
// polarization
double polar_for_Fhkl_grad=1;
if (!db_flags.nopolar && db_flags.Fhkl_gradient_mode)
polar_for_Fhkl_grad = diffBragg_cpu_kernel_polarization(incident, diffracted,
db_beam.polarization_axis, db_beam.kahn_factor);
/* rough cut to speed things up when we aren't using whole detector */
if(db_cryst.dmin > 0.0 && stol > 0.0)
{
if(db_cryst.dmin > 0.5/stol)
{
continue;
}
}
Eigen::Matrix3d Bmat_realspace = 1e10*db_cryst.eig_B;
//if( phi != 0.0 )
//{
// double cosphi = cos(phi);
// double sinphi = sin(phi);
// ap_vec = ap_vec*cosphi + db_cryst.spindle_vec.cross(ap_vec)*sinphi + db_cryst.spindle_vec*(db_cryst.spindle_vec.dot(ap_vec))*(1-cosphi);
// bp_vec = bp_vec*cosphi + db_cryst.spindle_vec.cross(bp_vec)*sinphi + db_cryst.spindle_vec*(db_cryst.spindle_vec.dot(bp_vec))*(1-cosphi);
// cp_vec = cp_vec*cosphi + db_cryst.spindle_vec.cross(cp_vec)*sinphi + db_cryst.spindle_vec*(db_cryst.spindle_vec.dot(cp_vec))*(1-cosphi);
// Bmat_realspace << ap_vec[0], bp_vec[0], cp_vec[0],
// ap_vec[1], bp_vec[1], cp_vec[1],
// ap_vec[2], bp_vec[2], cp_vec[2];
//}
//Bmat_realspace *= 1e10;
if (db_flags.use_diffuse && db_flags.gamma_miller_units){
anisoG_local = anisoG_local * Bmat_realspace;
for (int i_gam=0; i_gam<3; i_gam++){
dG_dgam[i_gam] = dG_dgam[i_gam] * Bmat_realspace;
}
}
Eigen::Matrix3d U = db_cryst.eig_U;
Eigen::Matrix3d UBOt=U*Bmat_realspace*(db_cryst.eig_O.transpose());
double phi = db_cryst.phi0 + db_cryst.phistep*phi_tic;
if (phi != 0){
if (i_pix==0){
printf("phistep=%f, phi0=%f, phi=%f\n", db_cryst.phistep, db_cryst.phi0, phi);
}
double c = cos(phi);
double omc = 1-c;
double s = sin(phi);
Eigen::Matrix3d Rphi;
double gx = db_cryst.spindle_vec[0];
double gy = db_cryst.spindle_vec[1];
double gz = db_cryst.spindle_vec[2];
Rphi << c + gx*gx*omc, gx*gy*omc-gz*s, gx*gz*omc+gy*s,
gy*gx*omc + gz*s, c + gy*gy*omc, gy*gz*omc - gx*s,
gz*gx*omc - gy*s, gz*gy*omc + gx*s, c + gz*gz*omc;
UBOt = Rphi*UBOt;
}
Eigen::Matrix3d UBO = (db_cryst.UMATS_RXYZ[mos_tic]*UBOt).transpose();
Eigen::Matrix3d Ainv = UBO.inverse();
Eigen::Vector3d q_vec(scattering[0], scattering[1], scattering[2]);
q_vec *= 1e-10;
Eigen::Vector3d H_vec = UBO*q_vec;
double h = H_vec[0];
double k = H_vec[1];
double l = H_vec[2];
/* round off to nearest whole index */
int h0 = static_cast<int>(ceil(h - 0.5));
int k0 = static_cast<int>(ceil(k - 0.5));
int l0 = static_cast<int>(ceil(l - 0.5));
Eigen::Vector3d H0(h0, k0, l0);
Eigen::Matrix3d NABC;
NABC << db_cryst.Na, db_cryst.Nd, db_cryst.Nf,
db_cryst.Nd, db_cryst.Nb, db_cryst.Ne,
db_cryst.Nf, db_cryst.Ne, db_cryst.Nc;
double C = 2 / 0.63 * db_cryst.fudge;
Eigen::Vector3d delta_H = H_vec - H0;
Eigen::Vector3d V = NABC*delta_H;
double hrad_sqr = V.dot(V);
double NABC_determ = NABC.determinant();
double F_latt = 0;
if (db_cryst.xtal_shape==SQUARE){
F_latt = 1;
if (db_cryst.Na > 1)
F_latt *= sincg(M_PI * h, db_cryst.Na);
if (db_cryst.Nb > 1)
F_latt *= sincg(M_PI * k, db_cryst.Nb);
if (db_cryst.Nc > 1)
F_latt *= sincg(M_PI * l, db_cryst.Nc);
}
else { // gaussian relp model
double exparg;
if (db_cryst.xtal_shape==GAUSS_STAR){
Eigen::Vector3d delta_Q = UBO.inverse()*delta_H;
double rad_star_sqr = delta_Q.dot(delta_Q)*xtal_size*xtal_size;
exparg = rad_star_sqr *1.9* db_cryst.fudge ;
}
else
exparg = hrad_sqr / 0.63 * db_cryst.fudge;
F_latt = exp(-exparg);
if (! db_flags.no_Nabc_scale)
F_latt *= NABC_determ;
}
/* convert amplitudes into intensity (photons per steradian) */
if (!db_flags.oversample_omega && !db_flags.Fhkl_gradient_mode)
omega_pixel = 1;
double count_scale = db_beam.source_I[source]*capture_fraction*omega_pixel;
double I0 = 0;
double step_diffuse_param[6] = {0,0,0,0,0,0};
if (db_flags.use_diffuse){
calc_diffuse_at_hkl(H_vec,H0,dHH,Hmin,Hmax,Hrange,Ainv,&db_cryst.FhklLinear[0],num_laue_mats,laue_mats,anisoG_local,anisoU_local,dG_dgam,db_flags.refine_diffuse,&I0,step_diffuse_param);
}
/* increment to intensity */
double latt_scale = 1;
if (db_flags.only_diffuse)
latt_scale = 0;
double I_noFcell = (F_latt*F_latt*latt_scale + I0) * count_scale;
/* structure factor of the unit cell */
double F_cell = db_cryst.default_F;
double F_cell2 = 0;
int i_hklasu=0;
int Fhkl_linear_index=-1;
if ( (h0<=db_cryst.h_max) && (h0>=db_cryst.h_min) && (k0<=db_cryst.k_max) && (k0>=db_cryst.k_min) && (l0<=db_cryst.l_max) && (l0>=db_cryst.l_min) ) {
/* just take nearest-neighbor */
Fhkl_linear_index = (h0-db_cryst.h_min) * db_cryst.k_range * db_cryst.l_range +
(k0- db_cryst.k_min) * db_cryst.l_range +
(l0-db_cryst.l_min);
F_cell = db_cryst.FhklLinear[Fhkl_linear_index];
if (db_flags.track_Fhkl){
std::string hkl_s ;
hkl_s = std::to_string(h0) + ","+ std::to_string(k0) + "," + std::to_string(l0);
if (Fhkl_tracker.count(hkl_s))
Fhkl_tracker[hkl_s] += 1;
else
Fhkl_tracker[hkl_s] = 0;
continue;
}
if (db_flags.complex_miller) F_cell2 = db_cryst.Fhkl2Linear[Fhkl_linear_index];
if (db_flags.Fhkl_have_scale_factors)
i_hklasu = db_cryst.FhklLinear_ASUid[Fhkl_linear_index];
}
//else{
// F_cell = default_F;
//}
bool do_max = false;
if(db_flags.verbose > 3){//} && i_step==0){
double F2 = sqrt(F_cell*F_cell + F_cell2*F_cell2);
double II = I_noFcell*F2*F2;
if (I_noFcell > II_max){
do_max = true;
max_stats[0] = F_cell;
max_stats[1] = F_cell2;
max_stats[2] = I_noFcell;
max_stats[3] = F_latt;
max_stats[4] = II;
max_stats[5] = omega_pixel;
max_stats[6] = db_beam.source_I[source];
max_stats[7]= db_beam.source_lambda[source];
max_stats[8] = h;
max_stats[9] = k;
max_stats[10] = l;
II_max = I_noFcell;
}
else{
do_max=false;}
//printf("hkl= %f %f %f hkl1= %d %d %d |Fcell| before=%f, Ino=%f, I=%f, Flatt=%f, cap_frac=%f, omega_pix=%f, sourceI=%f\n", h,k,l,h0,k0,l0, F2, I_noFcell, II, F_latt,
//capture_fraction, omega_pixel, source_I[source] );
}
double c_deriv_Fcell_real = 0;
double c_deriv_Fcell_imag = 0;
double d_deriv_Fcell_real = 0;
double d_deriv_Fcell_imag = 0;
double c_deriv_Fcell = 0;
double d_deriv_Fcell = 0;
if (db_flags.complex_miller){
// TODO shouldnt this be constant for each HKL?
if (db_cryst.fpfdp.size() > 0){
double S_2 = 1.e-20*(scattering[0]*scattering[0]+scattering[1]*scattering[1]+scattering[2]*scattering[2]);
// fp is always followed by the fdp value
double val_fp = db_cryst.fpfdp[2*source];
double val_fdp = db_cryst.fpfdp[2*source+1];
double c_deriv_prime=0;
double c_deriv_dblprime=0;
double d_deriv_prime = 0;
double d_deriv_dblprime = 0;
if (db_flags.refine_fp_fdp){
// currently only supports two parameter model
int nsources_times_two = db_cryst.fpfdp.size();
int d_idx = 2*source;
c_deriv_prime = db_cryst.fpfdp_derivs[d_idx];
c_deriv_dblprime = db_cryst.fpfdp_derivs[d_idx+1];
d_deriv_prime = db_cryst.fpfdp_derivs[d_idx+nsources_times_two];
d_deriv_dblprime = db_cryst.fpfdp_derivs[d_idx+1+nsources_times_two];
}
// 5 valeus per atom: x,y,z,B,occupancy
int num_atoms = db_cryst.atom_data.size()/5;
for (int i_atom=0; i_atom < num_atoms; i_atom++){
if (db_flags.verbose>5)
printf("Processing atom %d, F_cell=%10.3f\n", i_atom, F_cell);
// fractional atomic coordinates
double atom_x = db_cryst.atom_data[i_atom*5];
double atom_y = db_cryst.atom_data[i_atom*5+1];
double atom_z = db_cryst.atom_data[i_atom*5+2];
double B = db_cryst.atom_data[i_atom*5+3]; // B factor
B = exp(-B*S_2/4.0); // TODO: speed me up?
double occ = db_cryst.atom_data[i_atom*5+4]; // occupancy
double r_dot_h = h0*atom_x + k0*atom_y + l0*atom_z;
double phase = 2*M_PI*r_dot_h;
double s_rdoth = sin(phase);
double c_rdoth = cos(phase);
double Bocc = B*occ;
double BC = Bocc*c_rdoth;
double BS = Bocc*s_rdoth;
double real_part = BC*val_fp - BS*val_fdp;
double imag_part = BS*val_fp + BC*val_fdp;
F_cell += real_part;
F_cell2 += imag_part;
if (db_flags.refine_fp_fdp){
c_deriv_Fcell_real += BC*c_deriv_prime - BS*c_deriv_dblprime;
c_deriv_Fcell_imag += BS*c_deriv_prime + BC*c_deriv_dblprime;
d_deriv_Fcell_real += BC*d_deriv_prime - BS*d_deriv_dblprime;
d_deriv_Fcell_imag += BS*d_deriv_prime + BC*d_deriv_dblprime;
}
}
}
double Freal = F_cell;
double Fimag = F_cell2;
F_cell = sqrt(pow(Freal,2) + pow(Fimag,2));// TODO work around the sqrt ?
//if (verbose> 3 && i_step==0){
// double II = I_noFcell*F_cell*F_cell;
// printf("hkl= %f %f %f hkl1= %d %d %d |Fcell| after=%f, Ino=%f, I=%f\n", h,k,l,h0,k0,l0, F_cell, I_noFcell, II);
// }
if (db_flags.refine_fp_fdp){
c_deriv_Fcell = Freal*c_deriv_Fcell_real + Fimag*c_deriv_Fcell_imag;
d_deriv_Fcell = Freal*d_deriv_Fcell_real + Fimag*d_deriv_Fcell_imag;
}
}
if(db_flags.verbose > 3){
double F2 = sqrt(F_cell*F_cell + F_cell2*F_cell2);
double II = I_noFcell*F2*F2;
if (do_max){
max_stats2[0] = F2 ;
max_stats2[1] = 0 ; //F_cell2;
max_stats2[2] = I_noFcell;
max_stats2[3] = F_latt;
max_stats2[4] = II;
max_stats2[5] = omega_pixel;
max_stats2[6] = db_beam.source_I[source];
max_stats2[7]= db_beam.source_lambda[source];
max_stats2[8] = h;
max_stats2[9] = k;
max_stats2[10] = l;
}
}
double I_cell = F_cell;
if (! db_flags.refine_Icell)
I_cell *= F_cell;
double s_hkl = 1;
int Fhkl_channel = 0;
if (! db_beam.Fhkl_channels.empty())
Fhkl_channel = db_beam.Fhkl_channels[source];
if (db_flags.Fhkl_have_scale_factors)
s_hkl = d_image.Fhkl_scale[i_hklasu + Fhkl_channel*db_cryst.Num_ASU];
if (db_flags.Fhkl_gradient_mode){
double Fhkl_deriv_scale = db_cryst.r_e_sqr*db_beam.fluence*db_cryst.spot_scale*polar_for_Fhkl_grad/db_steps.Nsteps;
double dfhkl = I_noFcell*I_cell * Fhkl_deriv_scale;
double grad_incr = dfhkl*Fhkl_deriv_coef;
int fhkl_grad_idx=i_hklasu + Fhkl_channel*db_cryst.Num_ASU;
if (db_flags.track_Fhkl_indices)
db_cryst.Fhkl_grad_idx_tracker.insert(fhkl_grad_idx);
// omega pixel is correctly in count_scale, and spot_scale should have been set to its refined value
#pragma omp atomic
d_image.Fhkl_scale_deriv[fhkl_grad_idx] += grad_incr;
if (db_flags.Fhkl_errors_mode){
double hessian_incr = Fhkl_hessian_coef*dfhkl*dfhkl;
#pragma omp atomic
d_image.Fhkl_hessian[fhkl_grad_idx] += hessian_incr;
}
continue; // move on to next diffraction step (note, thos will bypass the pintout_pixel settings)
}
double Iincrement = s_hkl*I_cell*I_noFcell;
I += Iincrement;
if(db_flags.wavelength_img){
Ilambda += Iincrement*lambda_ang;
Imiller_h += Iincrement * h ;
Imiller_k += Iincrement * k;
Imiller_l += Iincrement * l;
}
if (db_flags.refine_diffuse){
double step_scale = count_scale*I_cell;
for (int i_gam=0; i_gam <3; i_gam++){
int i_sig = i_gam + 3;
dI_latt_diffuse[i_gam] += step_scale*step_diffuse_param[i_gam];
dI_latt_diffuse[i_sig] += step_scale*step_diffuse_param[i_sig];
}
}
if (db_flags.refine_fp_fdp){
fp_fdp_manager_dI[0] += 2*I_noFcell * (c_deriv_Fcell);
fp_fdp_manager_dI[1] += 2*I_noFcell * (d_deriv_Fcell);
}
//if(verbose > 3)
// printf("hkl= %f %f %f hkl1= %d %d %d Fcell=%f\n", h,k,l,h0,k0,l0, F_cell);
double two_C = 2*C;
if (db_flags.refine_Umat[0]){
Eigen::Matrix3d RyRzUBOt = db_cryst.RotMats[1]*db_cryst.RotMats[2]*UBOt;
Eigen::Vector3d delta_H_prime = (db_cryst.UMATS[mos_tic]*db_cryst.dRotMats[0]*RyRzUBOt).transpose()*q_vec;
double V_dot_dV = V.dot(NABC*delta_H_prime);
double value = -two_C * V_dot_dV * Iincrement;
double value2 =0;
if (db_flags.compute_curvatures) {
Eigen::Vector3d delta_H_dbl_prime = (db_cryst.UMATS[mos_tic]*db_cryst.d2RotMats[0]*RyRzUBOt).transpose()*q_vec;
double dV_dot_dV = (NABC*delta_H_prime).dot(NABC*delta_H_prime);
double dV2_dot_V = (NABC*delta_H).dot(NABC*delta_H_dbl_prime);
value2 = two_C*(two_C*V_dot_dV*V_dot_dV - dV2_dot_V - dV_dot_dV)*Iincrement;
}
rot_manager_dI[0] += value;
rot_manager_dI2[0] += value2;
}
if (db_flags.refine_Umat[1]){
Eigen::Matrix3d UmosRx = db_cryst.UMATS[mos_tic]*db_cryst.RotMats[0];
Eigen::Matrix3d RzUBOt = db_cryst.RotMats[2]*UBOt;
Eigen::Vector3d delta_H_prime =(UmosRx*db_cryst.dRotMats[1]*RzUBOt).transpose()*q_vec;
double V_dot_dV = V.dot(NABC*delta_H_prime);
double value = -two_C * V_dot_dV * Iincrement;
double value2=0;
if (db_flags.compute_curvatures){
Eigen::Vector3d delta_H_dbl_prime = (UmosRx*db_cryst.d2RotMats[1]*RzUBOt).transpose()*q_vec;
double dV_dot_dV = (NABC*delta_H_prime).dot(NABC*delta_H_prime);
double dV2_dot_V = (NABC*delta_H).dot(NABC*delta_H_dbl_prime);
value2 = two_C*(two_C*V_dot_dV*V_dot_dV - dV2_dot_V - dV_dot_dV)*Iincrement;
}
rot_manager_dI[1] += value;
rot_manager_dI2[1] += value2;
}
if (db_flags.refine_Umat[2]){
Eigen::Matrix3d UmosRxRy = db_cryst.UMATS[mos_tic]*db_cryst.RotMats[0]*db_cryst.RotMats[1];
Eigen::Vector3d delta_H_prime = (UmosRxRy*db_cryst.dRotMats[2]*UBOt).transpose()*q_vec;
double V_dot_dV = V.dot(NABC*delta_H_prime);
double value = -two_C * V_dot_dV * Iincrement;
double value2=0;
if (db_flags.compute_curvatures){
Eigen::Vector3d delta_H_dbl_prime = (UmosRxRy*db_cryst.d2RotMats[2]*UBOt).transpose()*q_vec;
double dV_dot_dV = (NABC*delta_H_prime).dot(NABC*delta_H_prime);
double dV2_dot_V = (NABC*delta_H).dot(NABC*delta_H_dbl_prime);
value2 = two_C*(two_C*V_dot_dV*V_dot_dV - dV2_dot_V - dV_dot_dV)*Iincrement;
}
rot_manager_dI[2] += value;
rot_manager_dI2[2] += value2;
}
/*Checkpoint for unit cell derivatives*/
Eigen::Matrix3d Ot = db_cryst.eig_O.transpose();
Eigen::Matrix3d UmosRxRyRzU ;
Eigen::Vector3d delta_H_prime;
for(int i_uc=0; i_uc < 6; i_uc++ ){
if (db_flags.refine_Bmat[i_uc]){
UmosRxRyRzU = db_cryst.UMATS_RXYZ[mos_tic]*U;
delta_H_prime = ((UmosRxRyRzU*db_cryst.dB_Mats[i_uc]*Ot).transpose()*q_vec);
double V_dot_dV = V.dot(NABC*delta_H_prime);
double value = -two_C * V_dot_dV * Iincrement;
double value2 =0;
if (db_flags.compute_curvatures){
Eigen::Vector3d delta_H_dbl_prime = ((UmosRxRyRzU*db_cryst.dB2_Mats[i_uc]*Ot).transpose()*q_vec);
double dV_dot_dV = (NABC*delta_H_prime).dot(NABC*delta_H_prime);
double dV2_dot_V = (NABC*delta_H).dot(NABC*delta_H_dbl_prime);
value2 = two_C*(two_C*V_dot_dV*V_dot_dV - dV2_dot_V - dV_dot_dV)*Iincrement;
}
ucell_manager_dI[i_uc] += value;
ucell_manager_dI2[i_uc] += value2;
}
} /*end ucell deriv */
/* Checkpoint for Ncells manager */
if (db_flags.refine_Ncells[0]){
int num_ncell_deriv = 1;
if (! db_flags.isotropic_ncells)
num_ncell_deriv = 3;
for (int i_nc=0; i_nc < num_ncell_deriv; i_nc++) {
Eigen::Matrix3d dN;
if (!db_flags.isotropic_ncells){
dN << 0,0,0,0,0,0,0,0,0;
dN(i_nc, i_nc) = 1;
}
else
dN << 1,0,0,0,1,0,0,0,1;
double N_i = NABC(i_nc, i_nc);
Eigen::Vector3d dV_dN = dN*delta_H;
double determ_deriv = (NABC.inverse()*dN).trace(); // TODO speedops: precompute these
double deriv_coef = determ_deriv - C* ( dV_dN.dot(V));
double value = 2*Iincrement*deriv_coef;
double value2=0;
if(db_flags.compute_curvatures){
dN(i_nc, i_nc) = 0; // TODO check maths
value2 = ( -1/N_i/N_i - C*(dV_dN.dot(dV_dN))) *2*Iincrement;
value2 += deriv_coef*2*value;
}
Ncells_manager_dI[i_nc] += value;
Ncells_manager_dI2[i_nc] += value2;
}
} /* end Ncells manager deriv */
if (db_flags.refine_Ncells_def){
for (int i_nc =3; i_nc < 6; i_nc++ ){
Eigen::Matrix3d dN;
if (i_nc ==3)
dN << 0,1,0,1,0,0,0,0,0;
else if (i_nc == 4)
dN << 0,0,0,0,0,1,0,1,0;
else
dN << 0,0,1,0,0,0,1,0,0;
Eigen::Vector3d dV_dN = dN*delta_H;
double determ_deriv = (NABC.inverse()*dN).trace(); // TODO speedops: precompute these
//double deriv_coef = -C* (2* dV_dN.dot(V));
double deriv_coef = determ_deriv - C* (dV_dN.dot(V));
double value = 2*Iincrement*deriv_coef;
Ncells_manager_dI[i_nc] += value;
double value2 =0;
if (db_flags.compute_curvatures){
value2 = deriv_coef*value;
value2 += -2*C*Iincrement*(dV_dN.dot(dV_dN));
Ncells_manager_dI2[i_nc] += value2;
}
}
}
///* Checkpoint for Origin manager */
for (int i_pan_orig=0; i_pan_orig < 3; i_pan_orig++){
if (db_flags.refine_panel_origin[i_pan_orig]){
double per_k = 1/airpath;
double per_k3 = pow(per_k,3);
double per_k5 = pow(per_k,5);
double lambda_ang = lambda*1e10;
Eigen::Matrix3d M = -two_C*(NABC*UBO)/lambda_ang;
Eigen::Vector3d dk;
if (i_pan_orig == 0)
dk << 0,0,1;
else if (i_pan_orig == 1)
dk << 1,0,0;
else
dk << 0,1,0;
af::flex_double dI_and_dI2 = get_panel_increment(Iincrement, omega_pixel, M, db_det.subpixel_size*db_det.subpixel_size,
o_vec, pixel_pos, per_k, per_k3, per_k5, V, dk);
pan_orig_manager_dI[i_pan_orig] += dI_and_dI2[0];
pan_orig_manager_dI2[i_pan_orig] += dI_and_dI2[1];
} /* end origin manager deriv */
}
for (int i_pan_rot=0; i_pan_rot < 3; i_pan_rot++){
if(db_flags.refine_panel_rot[i_pan_rot]){
double per_k = 1/airpath;
double per_k3 = pow(per_k,3);
double per_k5 = pow(per_k,5);
double lambda_ang = lambda*1e10;
Eigen::Matrix3d M = -two_C*(NABC*UBO)/lambda_ang;
Eigen::Vector3d dk = Fdet*(db_det.dF_vecs[pid*3 + i_pan_rot]) + Sdet*(db_det.dS_vecs[pid*3 + i_pan_rot]);
af::flex_double dI_and_dI2 = get_panel_increment(Iincrement, omega_pixel, M, db_det.subpixel_size*db_det.subpixel_size,
o_vec, pixel_pos, per_k, per_k3, per_k5, V, dk);
pan_rot_manager_dI[i_pan_rot] += dI_and_dI2[0];
pan_rot_manager_dI2[i_pan_rot] += dI_and_dI2[1];
}
}
/* checkpoint for Fcell manager */
if (db_flags.refine_fcell){
// TODO rewrite so no divide by Fcell
int nom_h=h0;
int nom_k=k0;
int nom_l=l0;
//int f_cell_idx = 1;
if (use_nominal_hkl){
nom_h = db_cryst.nominal_hkl[i_pix*3];
nom_k = db_cryst.nominal_hkl[i_pix*3+1];
nom_l = db_cryst.nominal_hkl[i_pix*3+2];
//f_cell_idx = l0 - nom_l + 1;
}
double value;
if (db_flags.refine_Icell)
value = I_noFcell;
else
value = 2*I_noFcell*F_cell; //2*Iincrement/F_cell ;
double value2=0;
if (db_flags.compute_curvatures){
if (F_cell > 0)
value2 =2*I_noFcell;
}
//if (f_cell_idx >= 0 && f_cell_idx <= 2){
// NOTE use nominal hkl to regulate when gradients are compputed, as h,k,l can drift within a shoebox
if (use_nominal_hkl){
if (nom_h==h0 && nom_k==k0 && nom_l==l0 ){
fcell_manager_dI += value;
fcell_manager_dI2 += value2;
}
}
else{
fcell_manager_dI += value;
fcell_manager_dI2 += value2;
}
} /* end of fcell man deriv */
/* checkpoint for eta manager */
if (db_flags.refine_eta){
for (int i_eta=0; i_eta < 3; i_eta++){
if (i_eta != 0 && db_cryst.UMATS_RXYZ_prime.size()==db_cryst.UMATS_RXYZ.size()){
continue;
}