-
Notifications
You must be signed in to change notification settings - Fork 16
/
icpc_fieldgen_opt.c
1491 lines (1356 loc) · 57.4 KB
/
icpc_fieldgen_opt.c
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
/* program to use in auto-optimization of ICPC geometries
author: D.C. Radford
based on mjd_fieldgen.c version from 30 Nov 2021
this version created Aug 2023
- add command line parameters for some geometric values, to over-ride config file valies
- calculate field at bias = 500 V over depletion voltage
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "mjd_siggen.h"
#include "detector_geometry.h"
#define MAX_ITS 50000 // default max number of iterations for relaxation
#define EMIN_OVERBIAS 500 // 500V over bias
static int report_config(FILE *fp_out, char *config_file_name);
static int grid_init(MJD_Siggen_Setup *setup);
static int ev_calc(MJD_Siggen_Setup *setup, MJD_Siggen_Setup *old_setup);
static int wp_calc(MJD_Siggen_Setup *setup, MJD_Siggen_Setup *old_setup);
static int write_ev(MJD_Siggen_Setup *setup);
static int write_wp(MJD_Siggen_Setup *setup);
static int do_relax(MJD_Siggen_Setup *setup, int ev_calc);
static int ev_relax_undep(MJD_Siggen_Setup *setup);
static int wp_relax_undep(MJD_Siggen_Setup *setup);
static int interpolate(MJD_Siggen_Setup *setup, MJD_Siggen_Setup *old_setup);
/* -------------------------------------- main ------------------- */
int main(int argc, char **argv)
{
MJD_Siggen_Setup setup, setup1, setup2;
float BV; // bias voltage
int WV = 0; // 0: do not write the V and E values to ppc_ev.dat
// 1: write the V and E values to ppc_ev.dat
// 2: write the V and E values for both +r, -r (for gnuplot, NOT for siggen)
int WD = 0; // 0: do not write out depletion surface
// 1: write out depletion surface to depl_<HV>.dat
int i, j, k;
int overbias = 0;
#ifdef EMIN_OVERBIAS
overbias = EMIN_OVERBIAS;
#endif
float value;
FILE *fp;
if (argc < 2 || argc%2 != 0 || read_config(argv[1], &setup)) {
printf("Usage: %s <config_file_name> [options]\n"
" Possible options:\n"
" -b bias_volts\n"
" -g<n> <value> override geometry spec from config file with <value>\n"
" n = 0/w : wrap_around_radius; n = 1/g: hole_length_gap; n = 2/h: hole_radius\n"
" n = 3/t : inner_taper_length; n = 4/a: taper_angle\n"
" n = 5/l : xtal_length; n = 6/r: xtal_radius\n"
" -w {0,1} (do_not/do write the field file)\n"
" -d {0,1} (do_not/do write the depletion surface)\n"
" -p {0,1} (do_not/do write the WP file)\n"
" -r rho_spectrum_file_name\n"
" -o overbias value (HV - V_depl); default is %d\n"
" -z <rho_spectrum_offset_mm>\n", argv[0], overbias);
return 1;
}
strncpy(setup.config_file_name, argv[1], sizeof(setup.config_file_name));
if (setup.xtal_grid < 0.001) setup.xtal_grid = 0.5;
BV = setup.xtal_HV;
WV = setup.write_field;
setup.rho_z_spe[0] = 0;
for (i=2; i<argc-1; i++) {
if (strstr(argv[i], "-b")) {
BV = setup.xtal_HV = atof(argv[++i]); // bias volts
} else if (strstr(argv[i], "-g")) {
printf("argv[%d] = %s\n", i, argv[i]);
if (*(argv[i]+2) >= 'a' && *(argv[i]+2) <= 'z') {
j = -1;
if (*(argv[i]+2) == 'w') j = 0; // wrap_around_radius
if (*(argv[i]+2) == 'g') j = 1; // hole_length_gap
if (*(argv[i]+2) == 'h') j = 2; // hole_radius
if (*(argv[i]+2) == 't') j = 3; // inner_taper_length
if (*(argv[i]+2) == 'a') j = 4; // taper_angle
if (*(argv[i]+2) == 'l') j = 5; // xtal length
if (*(argv[i]+2) == 'r') j = 6; // xtal radius
if (*(argv[i]+2) == 'z') j = 7; // z-cut position in mm (adds to any other -z input)
} else {
j = atoi(argv[i]+2);
}
value = atof(argv[++i]);
if (j==0) {
setup.wrap_around_radius = value;
printf(" g%d override: wrap_around_radius = %.1f\n", j, value);
} else if (j==1) {
setup.hole_length = setup.xtal_length - value;
printf(" g%d override: hole_length_gap = %.1f, hole_length = %.1f\n", j, value, setup.hole_length);
if (setup.inner_taper_length > setup.hole_length) {
setup.inner_taper_length = setup.hole_length;
printf(" ... and inner_taper_length = %.1f\n", setup.inner_taper_length);
}
} else if (j==2) {
setup.hole_radius = value;
printf(" g%d override: hole_radius = %.1f\n", j, value);
} else if (j==3) {
setup.inner_taper_length = value;
printf(" g%d override: inner_taper_length = %.1f\n", j, value);
} else if (j==4) {
setup.taper_angle = value;
printf(" g%d override: taper_angle = %.1f\n", j, value);
} else if (j==5) {
printf(" g%d override: xtal_length = %.1f\n", j, value);
if (setup.xtal_length != value) {
// maintain hole_length_gap by adjusting hole_length
setup.hole_length -= setup.xtal_length - value;
printf(" ... and hole_length = %.1f\n", setup.hole_length);
if (setup.inner_taper_length > setup.hole_length) {
setup.inner_taper_length = setup.hole_length;
printf(" ... and inner_taper_length = %.1f\n", setup.inner_taper_length);
}
}
setup.xtal_length = value;
} else if (j==6) {
setup.xtal_radius = value;
printf(" g%d override: xtal_radius = %.1f\n", j, value);
} else if (j==7) {
printf(" g%d override: xtal_cut_position = %.0f\n", j, value);
if (value >= 199) {
printf("Error; rho_spectrum_offset_mm = %.0f is too large! max = 200\n", value);
return 1;
} else if (value < 0 ) {
printf("Error; rho_spectrum_offset_mm = %.0f is < 0, setting to 0\n", value);
value = 0;
}
for (k=lrint(value); k <200; k++) setup.rho_z_spe[k-lrint(value)] = setup.rho_z_spe[k];
} else {
printf("\nERROR: illegal geometry parameter override %s\n\n", argv[i-1]);
return -1;
}
} else if (strstr(argv[i], "-w")) {
WV = atoi(argv[++i]); // write-out options
} else if (strstr(argv[i], "-d")) {
WD = atoi(argv[++i]); // write-out options
} else if (strstr(argv[i], "-p")) {
setup.write_WP = atoi(argv[++i]); // weighting-potential options
} else if (strstr(argv[i], "-r")) {
if (!(fp = fopen(argv[++i], "r"))) { // impurity-profile-spectrum file name
printf("\nERROR: cannot open impurity profile spectrum file %s\n\n", argv[i]);
return 1;
}
if (strstr(argv[i], ".spe")) fread(setup.rho_z_spe, 36, 1, fp); // skip over .spe-format header
for (j=0; j<1024; j++) setup.rho_z_spe[j] = 0;
fread(setup.rho_z_spe, sizeof(setup.rho_z_spe), 1, fp);
fclose(fp);
} else if (strstr(argv[i], "-z")) {
j = atoi(argv[++i]);
if (j < 0 || j >= 199) {
printf("Error; rho_spectrum_offset_mm = %d is < 0 or too large! max = 200\n", j);
return 1;
}
for (k=j; k <200; k++) setup.rho_z_spe[k-j] = setup.rho_z_spe[k];
} else if (strstr(argv[i], "-o")) {
overbias = atoi(argv[++i]);
} else {
printf("Possible options:\n"
" -b bias_volts\n"
" -g<n> <value> override geometry spec from config file with <value>\n"
" n = 0 : wrap_around_radius; n = 1: hole_length_gap; n = 2: hole_radius\n"
" n = 3 : inner_taper_length; n = 4: taper_angle\n"
" n = 5 : xtal_length; n = 6: xtal_radius\n"
" -w {0,1,2} (for WV options)\n"
" -p {0,1} (for WP options)\n"
" -d {0,1} (do_not/do write the depletion surface)\n"
" -r rho_spectrum_file_name\n"
" -o overbias value (HV - V_depl; default is %d)\n"
" -z <rho_spectrum_offset_mm>\n", overbias);
return 1;
}
}
if (setup.inner_taper_length > setup.hole_length) {
setup.inner_taper_length = setup.hole_length;
printf(" Warning: inner_taper_length limited to hole_length = %.1f\n", setup.hole_length);
}
/* a consistency check */
if (setup.inner_taper_length > setup.hole_length - setup.hole_bullet_radius)
setup.inner_taper_length = setup.hole_length - setup.hole_bullet_radius;
/* convert taper angle to taper widths */
if (setup.outer_taper_length > 0) {
setup.outer_taper_width =
setup.outer_taper_length * tan(setup.taper_angle * 3.14159/180.0);
}
if (setup.inner_taper_length > 0) {
setup.inner_taper_width =
setup.inner_taper_length * tan(setup.taper_angle * 3.14159/180.0);
}
if (setup.wrap_around_radius > 4 &&
setup.ditch_depth > 0 && setup.ditch_thickness > 0 &&
(setup.pc_radius >4 || setup.pc_radius > setup.wrap_around_radius - setup.ditch_thickness))
setup.pc_radius = setup.wrap_around_radius - setup.ditch_thickness;
if (setup.verbosity >= CHATTY && setup.rho_z_spe[0] != 0) {
printf(" z(mm) rho\n");
for (j=0; j < (int) setup.xtal_length; j++)
printf(" %3d %7.3f\n", j, setup.rho_z_spe[j]);
}
if (setup.xtal_length/setup.xtal_grid * setup.xtal_radius/setup.xtal_grid > 2500*2500) {
printf("Error: Crystal size divided by grid size is too large!\n");
return 1;
}
if (WV < 0 || WV > 2) WV = 0;
/* -------------- give details of detector geometry */
if (setup.verbosity >= CHATTY) {
printf("\n\n"
" Crystal: Radius x Length: %.1f x %.1f mm\n",
setup.xtal_radius, setup.xtal_length);
if (setup.hole_length > 0) {
if (setup.inner_taper_length > 0)
printf(" Core hole: Radius x length: %.1f x %.1f mm,"
" taper %.1f x %.1f mm (%2.f degrees)\n",
setup.hole_radius, setup.hole_length,
setup.inner_taper_width, setup.inner_taper_length, setup.taper_angle);
else
printf(" Core hole: Radius x length: %.1f x %.1f mm\n",
setup.hole_radius, setup.hole_length);
}
printf("Point contact: Radius x length: %.1f x %.1f mm\n",
setup.pc_radius, setup.pc_length);
if (setup.ditch_depth > 0) {
printf(" Wrap-around: Radius x ditch x gap: %.1f x %.1f x %.1f mm\n",
setup.wrap_around_radius, setup.ditch_depth, setup.ditch_thickness);
}
printf(" Bias: %.0f V\n", BV);
}
if ((BV < 0 && setup.impurity_z0 < 0) || (BV > 0 && setup.impurity_z0 > 0)) {
printf("ERROR: Expect bias and impurity to be opposite sign!\n");
return 1;
}
if (setup.impurity_z0 > 0) {
// swap polarity for n-type material; this lets me assume all voltages are positive
BV = -BV;
setup.xtal_HV *= -1.0;
setup.impurity_z0 *= -1.0;
setup.impurity_gradient *= -1.0;
setup.impurity_quadratic *= -1.0;
setup.impurity_surface *= -1.0;
setup.impurity_radial_add *= -1.0;
}
/* use an adaptive grid; start out coarse and then refine the grid */
memcpy(&setup1, &setup, sizeof(setup));
memcpy(&setup2, &setup, sizeof(setup));
setup1.xtal_grid *= 9.0;
setup2.xtal_grid *= 3.0;
if (grid_init(&setup1) != 0 ||
grid_init(&setup2) != 0 ||
grid_init(&setup) != 0) {
printf("failed to init field calculations\n");
return 1;
}
/* -------------- calculate electric potential/field */
if (setup.write_field) {
setup1.write_field = 0; // no need to save intermediate calculations
setup2.write_field = 0;
if (setup.xtal_grid > 0.4) {
ev_calc(&setup2, NULL);
} else {
ev_calc(&setup1, NULL);
ev_calc(&setup2, &setup1);
}
ev_calc(&setup, &setup2);
}
/* -------------- calculate weighting potential */
if (setup.write_WP) {
setup1.write_WP = 0; // no need to save intermediate calculations
setup2.write_WP = 0;
if (setup.xtal_grid > 0.4) {
wp_calc(&setup2, NULL);
} else {
wp_calc(&setup1, NULL);
wp_calc(&setup2, &setup1);
}
wp_calc(&setup, &setup2);
}
/* -------------- calculate capacitance
1/2 * epsilon * integral(E^2) = 1/2 * C * V^2
so C = epsilon * integral(E^2) / V^2 V = 1 volt
*/
double esum, esum2, pi=3.14159, Epsilon=(8.85*16.0/1000.0); // permittivity of Ge in pF/mm
float E_r, E_z;
float grid = setup.xtal_grid;
int r, z, test;
int L = lrint(setup.xtal_length/grid)+1;
int R = lrint(setup.xtal_radius/grid)+1;
int LC = lrint(setup.pc_length/grid)+1;
int RC = lrint(setup.pc_radius/grid)+1;
if (setup.write_WP) {
esum = esum2 = test = 0;
for (z=1; z<L; z++) {
for (r=2; r<R; r++) {
E_r = setup.eps_dr[z][r]/16.0 * (setup.v[1][z][r] - setup.v[1][z][r+1])/(0.1*grid);
E_z = setup.eps_dz[z][r]/16.0 * (setup.v[1][z][r] - setup.v[1][z+1][r])/(0.1*grid);
esum += (E_r*E_r + E_z*E_z) * (double) (r-1);
if ((r == RC && z <= LC) || (r <= RC && z == LC) ||
(r == RC+1 && z <= LC+1) || (r <= RC+1 && z == LC+1)) { // average over two different surfaces
if (setup.point_type[z+1][r+1] == PC) test = 1;
esum2 += 0.5 * sqrt(E_r*E_r + E_z*E_z) * (double) (r-1); // 0.5 since averaging over 2 surfaces
}
}
}
esum *= 2.0 * pi * 0.01 * Epsilon * pow(grid, 3.0);
// Epsilon is in pF/mm
// 0.01 converts (V/cm)^2 to (V/mm)^2, pow() converts to grid^3 to mm3
esum2 *= 2.0 * pi * 0.1 * Epsilon * pow(grid, 2.0);
// 0.1 converts (V/cm) to (V/mm), grid^2 to mm2
printf(" >> Calculated capacitance at %.0f V: %.3lf pF\n", BV, esum);
if (!test)
printf(" >> Alternative calculation of capacitance: %.3lf pF\n", esum2);
}
/* -------------- estimate depletion voltage */
double min = BV, min2 = BV, dV, dW, testv;
int vminr=0, vminz=0;
int dz[4] = {1, -1, 0, 0}, dr[4] = {0, 0, 1, -1};
if (setup.write_WP) {
if (setup.fully_depleted) {
// find minimum potential
for (z=1; z<LC+2; z++) {
for (r=1; r<RC+2; r++) {
if (setup.vsave[z][r] > 0 &&
min > setup.vsave[z][r] / (1.0 - setup.v[1][z][r])) {
min = setup.vsave[z][r] / (1.0 - setup.v[1][z][r]);
}
}
}
/* check for bubble depletion / pinch-off by seeing how much the bias
must be reduced for any pixel to be in a local potential minimum */
for (z=LC+2; z<L-2; z++) {
for (r=1; r<R-2; r++) {
if (setup.point_type[z][r] == INSIDE && setup.v[1][z][r] > 0.0001) {
testv = -1;
for (i=0; i<4; i++) {
if (r==1 && i==2) break; // do not check dr for r=1 (=0.0)
dV = setup.vsave[z+dz[i]][r+dr[i]] - setup.vsave[z][r]; // potential
dW = setup.v[1][z+dz[i]][r+dr[i]] - setup.v[1][z][r]; // WP
if (dW*grid > 0.00001 && dV < 0 && testv < -dV/dW) testv = -dV/dW;
}
if (testv >= 0 && min2 > testv) {
min2 = testv;
vminr = r; vminz = z;
}
}
}
}
if (min2 < min) {
printf("Estimated pinch-off voltage = %.0f V\n", BV - min);
printf(" min2 = %.1f at (r,z) = (%.1f, %.1f), so\n",
min2, (vminr-1)*grid, (vminz-1)*grid);
printf(" Full depletion (max pinch-off voltage) = %.0f\n", BV - min2);
min = min2;
} else {
printf("Estimated depletion voltage = %.0f V\n", BV - min);
}
}
printf("Minimum bulk field = %.2f V/cm at (r,z) = (%.1f, %.1f) mm\n\n",
setup.Emin, setup.rmin, setup.zmin);
}
if (overbias > 0) {
// calculate a new minimum electric field at a specified bias above depletion (usually 500 V)
if (min > 0) {
double db = overbias - min;
int k = 3.0/grid;
float E, ez, er, r, z;
// adjust potential
for (j = 1; j <= R; j++) {
for (i = 1; i <= L; i++) {
setup.vsave[i][j] += db * (1.0 - setup.v[1][i][j]);
}
}
setup.Emin = 99999.9;
setup.rmin = setup.zmin = 999.9;
/* calculate new field */
for (j = 1; j < R-k-1; j++) {
r = (j-1) * grid;
for (i = k+1; i < L-k-1; i++) {
z = (i-1) * grid;
if (setup.point_type[i][j] != INSIDE ||
setup.point_type[i + k][j] != INSIDE || // point is at least 3 mm from a boundary
setup.point_type[i - k][j] != INSIDE ||
setup.point_type[i][j + k] != INSIDE ||
(j > k+1 && setup.point_type[i][j - k] != INSIDE)) continue;
// calc E
er = 0;
if (j>1) er = (setup.vsave[i][j-1] - setup.vsave[i][j+1])/(0.2*grid);
ez = (setup.vsave[i-1][j] - setup.vsave[i+1][j])/(0.2*grid);
E = sqrt(er*er + ez*ez);
/* check for minimum field inside bulk of detector */
if (E > 0.1 && E < setup.Emin) {
setup.Emin = E;
setup.rmin = r;
setup.zmin = z;
}
}
}
printf("Minimum bulk field at %.0fV (%dV overbias) is %.2f V/cm at (r,z) = (%.1f, %.1f) mm\n",
BV-min+overbias, overbias, setup.Emin, setup.rmin, setup.zmin);
/* rewrite ev file */
for (j = 1; j <= R; j++) {
for (i = 1; i <= L; i++) {
setup.v[1][i][j] = setup.vsave[i][j];
}
}
setup.xtal_HV += db;
write_ev(&setup);
}
}
return 0;
} /* main */
/* -------------------------------------- ev_calc ------------------- */
int ev_calc(MJD_Siggen_Setup *setup, MJD_Siggen_Setup *old_setup) {
int i, j;
float grid = setup->xtal_grid;
int L = lrint(setup->xtal_length/grid)+3;
int R = lrint(setup->xtal_radius/grid)+3;
if (!old_setup) {
printf("\n\n ---- starting EV calculation --- \n");
for (i = 1; i < L; i++) {
for (j = 1; j < R; j++) {
setup->v[0][i][j] = setup->v[1][i][j] = setup->xtal_HV/2.0;
}
}
}
if (old_setup) interpolate(setup, old_setup);
setup->fully_depleted = 1;
setup->bubble_volts = 0;
/* set boundary voltages */
for (i = 1; i < L; i++) {
for (j = 1; j < R; j++) {
if (setup->point_type[i][j] == HVC)
setup->v[0][i][j] = setup->v[1][i][j] = setup->xtal_HV;
if (setup->point_type[i][j] == PC)
setup->v[0][i][j] = setup->v[1][i][j] = 0.0;
}
}
if (!old_setup || !old_setup->fully_depleted) ev_relax_undep(setup);
else do_relax(setup, 1);
if (setup->write_field) write_ev(setup);
if (setup->fully_depleted) {
printf("Detector is fully depleted.\n");
/* save potential close to point contact, to use later when calculating depletion voltage */
for (i = 1; i < L; i++) {
for (j = 1; j < R; j++) {
setup->vsave[i][j] = fabs(setup->v[1][i][j]);
}
}
} else {
printf("Detector is not fully depleted.\n");
if (setup->bubble_volts > 0)
printf("Pinch-off bubble at %.1f V potential\n", setup->bubble_volts);
if (!old_setup) {
// write a little file that shows any undepleted voxels in the crystal
FILE *file = fopen("undepleted.txt", "w");
for (j = R-1; j > 0; j--) {
setup->undepleted[j][L-1] = '\0';
fprintf(file, "%s\n", setup->undepleted[j]+1);
}
fclose(file);
}
}
return 0;
} /* ev_calc */
/* -------------------------------------- wp_calc ------------------- */
int wp_calc(MJD_Siggen_Setup *setup, MJD_Siggen_Setup *old_setup) {
int i, j, pinched_off = 0;
float grid = setup->xtal_grid;
int L = lrint(setup->xtal_length/grid)+3;
int R = lrint(setup->xtal_radius/grid)+3;
if (!old_setup) {
printf("\n\n ---- starting WP calculation --- \n");
for (i = 1; i < L; i++) {
for (j = 1; j < R; j++) {
setup->v[0][i][j] = setup->v[1][i][j] = 0.01;
}
}
}
if (old_setup) interpolate(setup, old_setup);
/* set boundary voltages */
for (i = 1; i < L; i++) {
for (j = 1; j < R; j++) {
if (setup->point_type[i][j] == HVC)
setup->v[0][i][j] = setup->v[1][i][j] = 0.0;
else if (setup->point_type[i][j] == PC)
setup->v[0][i][j] = setup->v[1][i][j] = 1.0;
else if (setup->undepleted[j][i] == '*') {
setup->point_type[i][j] = PC;
setup->v[0][i][j] = setup->v[1][i][j] = 1.0;
} else if (setup->undepleted[j][i] == 'B') {
setup->point_type[i][j] = PINCHOFF;
pinched_off = 1;
}
}
}
if (pinched_off) wp_relax_undep(setup);
else do_relax(setup, 0);
if (setup->write_WP) write_wp(setup);
return 0;
} /* wp_calc */
/* -------------------------------------- report_config ------------------- */
int report_config(FILE *fp_out, char *config_file_name) {
char *c, line[256];
FILE *file;
fprintf(fp_out, "# Config file: %s\n", config_file_name);
if (!(file = fopen(config_file_name, "r"))) return 1;
while (fgets(line, sizeof(line), file)) {
if (strlen(line) < 3 || *line == ' ' || *line == '\t' || *line == '#') continue;
if ((c = strchr(line, '#')) || (c = strchr(line, '\n'))) *c = '\0';
fprintf(fp_out, "# %s\n", line);
}
fclose(file);
return 0;
} /* report_config */
/* -------------------------------------- write_ev ------------------- */
int write_ev(MJD_Siggen_Setup *setup) {
int i, j, new=1;
float grid = setup->xtal_grid;
//int L = setup->xtal_length/grid + 2.99;
//int R = setup->xtal_radius/grid + 2.99;
int L = lrint(setup->xtal_length/grid)+2;
int R = lrint(setup->xtal_radius/grid)+2;
float r, z, E;
FILE *file;
double ***v = setup->v;
cyl_pt **e;
setup->Emin = 99999.9;
setup->rmin = setup->zmin = 999.9;
if (setup->impurity_z0 > 0) {
// swap voltages back to negative for n-type material
for (i=1; i<L; i++) {
for (j=1; j<R; j++) {
setup->v[new][i][j] = -setup->v[new][i][j];
}
}
}
/* write potential and field to output file */
if (!(file = fopen(setup->field_name, "w"))) {
printf("ERROR: Cannot open file %s for electric field...\n", setup->field_name);
return 1;
} else {
printf("Writing electric field data to file %s\n", setup->field_name);
}
/* copy configuration parameters to output file */
report_config(file, setup->config_file_name);
fprintf(file, "#\n# HV bias in fieldgen: %.1f V\n", setup->xtal_HV);
if (setup->fully_depleted) {
fprintf(file, "# Detector is fully depleted.\n");
} else {
fprintf(file, "# Detector is not fully depleted.\n");
if (setup->bubble_volts > 0.0f)
fprintf(file, "# Pinch-off bubble at %.0f V potential\n", setup->bubble_volts);
}
if ((e = (cyl_pt **) malloc(R*sizeof(*e))) == NULL) {
printf("ERROR: Malloc failed\n");
fclose(file);
return 1;
}
for (i = 0; i < R; i++){
if ((e[i] = (cyl_pt *) calloc(L, sizeof(*e[i]))) == NULL) {
printf("ERROR: Calloc failed\n");
fclose(file);
return 1;
}
}
for (j = 1; j < R; j++) {
r = (j-1) * grid;
for (i = 1; i < L; i++) {
z = (i-1) * grid;
// calc E in r-direction
if (j == 1) { // r = 0; symmetry implies E_r = 0
e[j][i].r = 0;
} else if (setup->point_type[i][j] == CONTACT_EDGE) {
e[j][i].r = ((v[new][i][j] - v[new][i][j+1])*setup->dr[1][i][j] +
(v[new][i][j-1] - v[new][i][j])*setup->dr[0][i][j]) / (0.2*grid);
} else if (setup->point_type[i][j] < INSIDE &&
setup->point_type[i][j-1] == CONTACT_EDGE) {
e[j][i].r = (v[new][i][j-1] - v[new][i][j]) * setup->dr[1][i][j-1] / ( 0.1*grid) ;
} else if (setup->point_type[i][j] < INSIDE &&
setup->point_type[i][j+1] == CONTACT_EDGE) {
e[j][i].r = (v[new][i][j] - v[new][i][j+1]) * setup->dr[0][i][j+1] / ( 0.1*grid) ;
} else if (j == R-1) {
e[j][i].r = (v[new][i][j-1] - v[new][i][j])/(0.1*grid);
} else {
e[j][i].r = (v[new][i][j-1] - v[new][i][j+1])/(0.2*grid);
}
// calc E in z-direction
if (setup->point_type[i][j] == CONTACT_EDGE) {
e[j][i].z = ((v[new][i][j] - v[new][i+1][j])*setup->dz[1][i][j] +
(v[new][i-1][j] - v[new][i][j])*setup->dz[0][i][j]) / (0.2*grid);
} else if (setup->point_type[i][j] < INSIDE &&
setup->point_type[i-1][j] == CONTACT_EDGE) {
e[j][i].z = (v[new][i-1][j] - v[new][i][j]) * setup->dz[1][i-1][j] / ( 0.1*grid) ;
} else if (setup->point_type[i][j] < INSIDE &&
setup->point_type[i+1][j] == CONTACT_EDGE) {
e[j][i].z = (v[new][i][j] - v[new][i+1][j]) * setup->dz[0][i+1][j] / ( 0.1*grid) ;
} else if (i == 1) {
e[j][i].z = (v[new][i][j] - v[new][i+1][j])/(0.1*grid);
} else if (i == L-1) {
e[j][i].z = (v[new][i-1][j] - v[new][i][j])/(0.1*grid);
} else {
e[j][i].z = (v[new][i-1][j] - v[new][i+1][j])/(0.2*grid);
}
/* temporarily store E in e[j][i].phi */
E = e[j][i].phi = sqrt(e[j][i].r*e[j][i].r + e[j][i].z*e[j][i].z);
/* check for minimum field inside bulk of detector */
int k = 3.0/grid;
if (E > 0.1 && E < setup->Emin &&
i > k+1 && j < R-k-1 && i < L-k-1 &&
setup->point_type[i][j] == INSIDE &&
setup->point_type[i + k][j] == INSIDE && // point is at least 3 mm from a boundary
setup->point_type[i - k][j] == INSIDE &&
setup->point_type[i][j + k] == INSIDE &&
(j < k+1 || setup->point_type[i][j - k] == INSIDE)) {
setup->Emin = E;
setup->rmin = r;
setup->zmin = z;
}
}
}
if (strstr(setup->field_name, "unf")) {
fprintf(file, "#\n## start of unformatted data\n");
i = R-1; j = L-1;
fwrite(&i, sizeof(int), 1, file);
fwrite(&j, sizeof(int), 1, file);
for (i = 1; i < R; i++) {
for (j = 1; j < L; j++) e[i][j].phi = 0;
if (fwrite(&e[i][1], sizeof(cyl_pt), L-1, file) != L-1) {
printf("ERROR while writing %s\n", setup->field_name);
}
}
} else {
fprintf(file, "#\n## r (mm), z (mm), V (V), E (V/cm), E_r (V/cm), E_z (V/cm)\n");
for (j = 1; j < R; j++) {
r = (j-1) * grid;
for (i = 1; i < L; i++) {
z = (i-1) * grid;
E = e[j][i].phi;
fprintf(file, "%7.2f %7.2f %7.1f %7.1f %7.1f %7.1f\n",
r, z, v[new][i][j], E, e[j][i].r, e[j][i].z);
}
fprintf(file, "\n");
}
}
fclose(file);
for (i = 0; i < R; i++) free(e[i]);
free(e);
if (!setup->write_WP)
printf("\n Minimum bulk field = %.2f V/cm at (r,z) = (%.1f, %.1f) mm\n\n",
setup->Emin, setup->rmin, setup->zmin);
if (0) { /* write point_type to output file */
file = fopen("fields/point_type.dat", "w");
for (j = 1; j < R; j++) {
for (i = 1; i < L; i++)
fprintf(file, "%7.2f %7.2f %2d\n",
(j-1)*grid, (i-1)*grid, setup->point_type[i][j]);
fprintf(file, "\n");
}
fclose(file);
}
return 0;
} /* write_ev */
/* -------------------------------------- write_wp ------------------- */
int write_wp(MJD_Siggen_Setup *setup) {
int i, j, new=1;;
float grid = setup->xtal_grid;
//int L = setup->xtal_length/grid + 2.99;
//int R = setup->xtal_radius/grid + 2.99;
int L = lrint(setup->xtal_length/grid)+2;
int R = lrint(setup->xtal_radius/grid)+2;
float r, z, w;
FILE *file;
if (!(file = fopen(setup->wp_name, "w"))) {
printf("ERROR: Cannot open file %s for weighting potential...\n", setup->wp_name);
return 1;
} else {
printf("Writing weighting potential to file %s\n\n", setup->wp_name);
}
/* copy configuration parameters to output file */
report_config(file, setup->config_file_name);
fprintf(file, "#\n# HV bias in fieldgen: %.1f V\n", setup->xtal_HV);
if (setup->fully_depleted) {
fprintf(file, "# Detector is fully depleted.\n");
} else {
fprintf(file, "# Detector is not fully depleted.\n");
if (setup->bubble_volts > 0.0f)
fprintf(file, "# Pinch-off bubble at %.0f V potential\n", setup->bubble_volts);
}
if (strstr(setup->field_name, "unf")) {
fprintf(file, "#\n## start of unformatted data\n");
i = R-1; j = L-1;
fwrite(&i, sizeof(int), 1, file);
fwrite(&j, sizeof(int), 1, file);
for (i = 1; i < R; i++) {
for (j = 1; j < L; j++) {
w = setup->v[new][j][i];
fwrite(&w, sizeof(float), 1, file);
}
}
} else {
fprintf(file, "#\n## r (mm), z (mm), WP\n");
for (j = 1; j < R; j++) {
r = (j-1) * grid;
for (i = 1; i < L; i++) {
z = (i-1) * grid;
fprintf(file, "%7.2f %7.2f %12.6e\n", r, z, setup->v[new][i][j]);
}
fprintf(file, "\n");
}
}
fclose(file);
return 0;
} /* write_wp */
/* -------------------------------------- dist_from_contact ------------------- */
float dist_from_contact(cyl_pt pt, cyl_pt delta, MJD_Siggen_Setup *setup) {
float factor = 1, d = 0.5;
cyl_pt test;
int n;
for (n=0; n<7; n++) { // 7 steps => 1/128 precision
test.r = pt.r + factor * delta.r;
test.z = pt.z + factor * delta.z;
if (outside_detector_cyl(test, setup)) {
factor -= d;
} else {
if (n == 0) return -1.0;
factor += d;
}
d /= 2.0;
}
return factor;
} /* dist_from_contact */
/* -------------------------------------- grid_init ------------------- */
#define SQ(x) ((x)*(x))
int grid_init(MJD_Siggen_Setup *setup) {
float grid = setup->xtal_grid;
int L = lrint(setup->xtal_length/grid)+3;
int R = lrint(setup->xtal_radius/grid)+3;
int i, j;
float r, z;
/* first malloc arrays in setup */
if ((setup->impurity = malloc(L * sizeof(*setup->impurity))) == NULL ||
(setup->eps = malloc(L * sizeof(*setup->eps))) == NULL ||
(setup->eps_dr = malloc(L * sizeof(*setup->eps_dr))) == NULL ||
(setup->eps_dz = malloc(L * sizeof(*setup->eps_dz))) == NULL ||
(setup->v[0] = malloc(L * sizeof(*setup->v[0]))) == NULL ||
(setup->v[1] = malloc(L * sizeof(*setup->v[1]))) == NULL ||
(setup->dr[0] = malloc(L * sizeof(*setup->dr[0]))) == NULL ||
(setup->dr[1] = malloc(L * sizeof(*setup->dr[1]))) == NULL ||
(setup->dz[0] = malloc(L * sizeof(*setup->dz[0]))) == NULL ||
(setup->dz[1] = malloc(L * sizeof(*setup->dz[1]))) == NULL ||
(setup->undepleted = malloc(R * sizeof(*setup->undepleted))) == NULL ||
(setup->s1 = malloc(R * sizeof(*setup->s1))) == NULL ||
(setup->s2 = malloc(R * sizeof(*setup->s2))) == NULL ||
(setup->vsave = malloc(L * sizeof(*setup->vsave))) == NULL ||
(setup->point_type = malloc(L * sizeof(*setup->point_type))) == NULL) {
printf("malloc failed\n");
return -1;
}
/* start from i=1 so that i=0 can be used for reflection symmetry around r=0 or z=0 */
for (i = 1; i < L; i++) {
if ((setup->impurity[i] = malloc(R * sizeof(**setup->impurity))) == NULL ||
(setup->v[0][i] = malloc(R * sizeof(**setup->v[0]))) == NULL ||
(setup->v[1][i] = malloc(R * sizeof(**setup->v[1]))) == NULL ||
(setup->dr[0][i] = malloc(R * sizeof(**setup->dr[0]))) == NULL ||
(setup->dr[1][i] = malloc(R * sizeof(**setup->dr[1]))) == NULL ||
(setup->dz[0][i] = malloc(R * sizeof(**setup->dz[0]))) == NULL ||
(setup->dz[1][i] = malloc(R * sizeof(**setup->dz[1]))) == NULL ||
(setup->vsave[i] = malloc(R * sizeof(**setup->vsave))) == NULL ||
(setup->point_type[i] = malloc(R * sizeof(**setup->point_type))) == NULL) {
printf("malloc failed\n");
return -1;
}
}
for (i = 1; i < L; i++) {
if ((setup->eps[i] = malloc(R * sizeof(**setup->eps))) == NULL ||
(setup->eps_dr[i] = malloc(R * sizeof(**setup->eps_dr))) == NULL ||
(setup->eps_dz[i] = malloc(R * sizeof(**setup->eps_dz))) == NULL) {
printf("malloc failed\n");
return -1;
}
for (j = 0; j < R; j++)
setup->eps[i][j] = setup->eps_dz[i][j] = setup->eps_dr[i][j] = 16.0;
}
for (j = 0; j < R; j++) {
if ((setup->undepleted[j] = malloc(L * sizeof(**setup->undepleted))) == NULL) {
printf("malloc failed\n");
return -1;
}
memset(setup->undepleted[j], ' ', L);
}
/* set up reflection symmetry around r=0 or z=0 */
setup->impurity[0] = malloc(R * sizeof(**setup->impurity));
setup->v[0][0] = setup->v[0][2];
setup->v[1][0] = setup->v[1][2];
setup->eps[0] = setup->eps[1];
setup->eps_dr[0] = setup->eps_dr[1];
setup->eps_dz[0] = setup->eps_dz[1];
setup->point_type[0] = setup->point_type[1];
/* ------------------------------------------------------------ */
/* weighting values for the relaxation alg. as a function of r
in the following we divide areas and volumes by pi
r_bin rmax A_top A_outside A_inside volume total_surf out/top tot/vol
0 1/2 1/4 1 0 1/4 1.5 4 6 << special case
1 3/2 2 3 1 2 8 3/2 4
2 5/2 4 5 3 4 16 5/4 4
3 7/2 6 7 5 6 24 7/6 4
r r+0.5 2r 2r+1 2r-1 2r 8r (2r+1)/2r 4
= 1+0.5/r
*/
setup->s1[1] = 4.0;
setup->s2[1] = 0.0;
for (i=2; i<R; i++) {
setup->s1[i] = 1.0 + 0.5 / (double) (i-1); // for r+1
setup->s2[i] = 1.0 - 0.5 / (double) (i-1); // for r-1
}
setup->s2[1] = setup->s1[1]; // special case for reflection symm at r=0
for (i = 1; i < L; i++) {
for (j = 0; j < R; j++) {
setup->dr[0][i][j] = setup->s2[j]; // for r-1
setup->dr[1][i][j] = setup->s1[j]; // for r+1
setup->dz[0][i][j] = 1; // for z-1
setup->dz[1][i][j] = 1; // for z+1
}
}
/* set up pixel point types for boundary conditions etc */
float d, g = 0.05 * grid, lith = setup->Li_thickness;
cyl_pt pt, pt1, pt2, pt3, pt4;
setup->rmax = setup->xtal_radius - lith;
setup->zmax = setup->xtal_length - lith;
setup->hole_radius += lith;
setup->hole_bullet_radius += lith;
setup->bottom_taper_length += 0.71*lith; // add top tapers?
for (i = 1; i < L; i++) {
pt.z = pt3.z = pt4.z = (i-1) * grid;
pt1.z = pt.z + g;
pt2.z = pt.z - g;
for (j = 1; j < R; j++) {
pt.r = pt1.r = pt2.r = (j-1) * grid;
pt3.r = pt.r + g;
pt4.r = pt.r - g;
setup->point_type[i][j] = INSIDE;
/* see if pixel is ouside (or very nearly outside) the detector bulk */
if (i == 1 || (pt.r >= setup->wrap_around_radius && pt.z - g < lith) ||
outside_detector_cyl(pt1, setup) || outside_detector_cyl(pt2, setup) ||
outside_detector_cyl(pt3, setup) || outside_detector_cyl(pt4, setup)) {
/* check for inside ditch
boundary condition at Ge-vacuum interface:
epsilon0 * E_vac = espilon_Ge * E_Ge */
if (setup->ditch_depth > 0 && pt.z < setup->ditch_depth + grid &&
pt.r <= setup->wrap_around_radius &&
pt.r >= setup->wrap_around_radius - setup->ditch_thickness) {
setup->point_type[i][j] = DITCH;
setup->eps[i][j] = setup->eps_dz[i][j] = setup->eps_dr[i][j] = 1.0;
/* check for inside (point) contact */
} else if (pt.z < setup->pc_length + g && pt.r < setup->pc_radius+g) {
setup->point_type[i][j] = PC;
/* check for passivated area */
} else if (i == 1 && pt.r < setup->wrap_around_radius && // BEGE/ICPC
pt.r < setup->rmax - setup->bottom_taper_length) { // PPC
setup->point_type[i][j] = PASSIVE;
/* only remaining surface is HV contact */
} else {
setup->point_type[i][j] = HVC;
}
}
}
}
/* find the pixels next to the contact surfaces */
cyl_pt dp1, dp2, dp3, dp4;
dp1.z = dp3.r = grid;
dp2.z = dp4.r = -grid;
dp1.r = dp2.r = dp3.z = dp4.z = 0;
for (i = 2; i < L-1; i++) {
pt.z = (i-1) * grid;
for (j = 1; j < R; j++) {
pt.r = (j-1) * grid;
if (setup->point_type[i][j] == INSIDE &&
(setup->point_type[i+1][j] < INSIDE || setup->point_type[i-1][j] < INSIDE ||
setup->point_type[i][j+1] < INSIDE || (j > 1 && setup->point_type[i][j-1] < INSIDE))) {
setup->point_type[i][j] = CONTACT_EDGE;
/* find distance to contact surface */
if (setup->point_type[i+1][j] < INSIDE && (d = dist_from_contact(pt, dp1, setup)) > 0)
setup->dz[1][i][j] = 1.0/d;
if (setup->point_type[i-1][j] < INSIDE && (d = dist_from_contact(pt, dp2, setup)) > 0)
setup->dz[0][i][j] = 1.0/d;
else if (setup->point_type[i-1][j] < INSIDE &&
pt.r >= setup->wrap_around_radius && pt.z - grid < lith)
setup->dz[0][i][j] = grid/(pt.z - lith);
if (setup->point_type[i][j+1] < INSIDE && (d = dist_from_contact(pt, dp3, setup)) > 0)
setup->dr[1][i][j] = setup->s1[j] * 1.0/d;
if (j > 1 && setup->point_type[i][j-1] < INSIDE &&
(d = dist_from_contact(pt, dp4, setup)) > 0)
setup->dr[0][i][j] = setup->s2[j] * 1.0/d;
}
}
}
setup->hole_radius -= lith;
setup->hole_bullet_radius -= lith;
setup->bottom_taper_length -= 0.71*lith;
/* for pixels adjacent to the ditch, set point_type to DITCH_EDGE
and for z=0, set flag for passivated surface */
for (i = 1; i < L; i++) {
for (j = 1; j < R; j++) {
setup->eps_dr[i][j-1] = (setup->eps[i][j-1] + setup->eps[i][j]) / 2.0f;
setup->eps_dz[i-1][j] = (setup->eps[i-1][j] + setup->eps[i][j]) / 2.0f;
if (setup->point_type[i][j] == INSIDE &&
(setup->point_type[i-1][j] == DITCH ||
setup->point_type[i][j-1] == DITCH ||
setup->point_type[i][j+1] == DITCH)) setup->point_type[i][j] = DITCH_EDGE;
}
setup->eps_dr[i][0] = setup->eps_dr[i][1];
}
/* set up impurity array */
double *imp_z, imp_ra = 0, imp_rm = 1;
double e_over_E = 11.310; // e/epsilon; for 1 mm2, charge units 1e10 e/cm3, espilon = 16*epsilon0
/* malloc local array */
if ((imp_z = malloc(L * sizeof(*imp_z))) == NULL) {
printf("malloc failed\n");
return -1;
}
if (setup->rho_z_spe[0] == 0) {
for (i = 1; i < L; i++) {
z = (i-1) * grid;
imp_z[i] = e_over_E * grid*grid / 4.0 *
(setup->impurity_z0 +
setup->impurity_gradient * z * 0.1 +