-
Notifications
You must be signed in to change notification settings - Fork 8
/
initialize.cu
executable file
·1226 lines (1070 loc) · 43.1 KB
/
initialize.cu
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
#ifndef __INITIALIZE_CU__
#define __INITIALIZE_CU__
#include "gPET.h"
#include "externCUDA.h"
int nmat_h;
using namespace std;
Isotopes loadIsotopes()
{
Isotopes isotopes;
ifstream infile("data/isotopes.txt");
FILEEXIST(infile);
printf("reading /data/isotopes.txt\n");
infile >> isotopes.Ntype;
infile.ignore(512,'#');
isotopes.halftime=new float[isotopes.Ntype];
isotopes.decayRatio=new float[isotopes.Ntype];
isotopes.coef=new float[isotopes.Ntype*8];
for(int i=0;i<isotopes.Ntype;i++)
{
infile >> isotopes.halftime[i]>>isotopes.decayRatio[i]>>isotopes.coef[8*i]>>isotopes.coef[8*i+1]>>isotopes.coef[8*i+2];
infile >> isotopes.coef[8*i+3]>>isotopes.coef[8*i+4]>>isotopes.coef[8*i+5]>>isotopes.coef[8*i+6]>>isotopes.coef[8*i+7];
cout << i <<" "<< isotopes.halftime[i]<<" " <<isotopes.decayRatio[i]<<" "<< \
isotopes.coef[8*i]<<" "<< isotopes.coef[8*i+7]<<endl;
}
printf("finish read isotopes;\n\n");
infile.close();
return isotopes;
}
Phantom loadPhantom(char matfile[100], char denfile[100],int* pdim, float* poffset, float* psize)
/*******************************************************************
c* Reads voxel geometry from an input file * *
c******************************************************************/
{
Phantom phantom;
cout<<"\n\nloading phantom\n";
phantom.Unxvox = pdim[0];
phantom.Unyvox = pdim[1];
phantom.Unzvox = pdim[2];
printf("CT dimension: %f %f %f\n", phantom.Unxvox, phantom.Unyvox, phantom.Unzvox);
phantom.Offsetx = poffset[0];
phantom.Offsety = poffset[1];
phantom.Offsetz = poffset[2];
printf("CT offset: %f %f %f\n", phantom.Offsetx, phantom.Offsety, phantom.Offsetz);
phantom.Sizex = psize[0];
phantom.Sizey = psize[1];
phantom.Sizez = psize[2];
printf("CT Size: %f %f %f\n", phantom.Sizex, phantom.Sizey, phantom.Sizez);
int numvox=phantom.Unxvox*phantom.Unyvox*phantom.Unzvox;
phantom.mat = new int[numvox];
ifstream infilemat(matfile,ios::binary);
FILEEXIST(infilemat);
infilemat.read(reinterpret_cast <char*> (&phantom.mat[0]), sizeof(int)*numvox);
infilemat.close();
phantom.dens = new float[numvox];
ifstream infileden(denfile,ios::binary);
FILEEXIST(infileden);
infileden.read(reinterpret_cast <char*> (&phantom.dens[0]), sizeof(float)*numvox);
infileden.close();
phantom.dx = phantom.Sizex/phantom.Unxvox;
phantom.dy = phantom.Sizey/phantom.Unyvox;
phantom.dz = phantom.Sizez/phantom.Unzvox;
cout<<"finish loading phantom"<<endl;
cout<<"resolution is "<<phantom.dx << phantom.dy<< phantom.dz<<endl;
return phantom;
}
Particle readParticle(char sourcefile[100],int NParticle)
{
Particle particle;
double data[8];
ifstream infile(sourcefile,ios::binary);
FILEEXIST(infile);
printf("reading %s\n", sourcefile);
int start, stop;
start=infile.tellg();
infile.seekg(0, ios::end);
stop=infile.tellg();
if(NParticle>(stop-start)/64)
{
cout<<"Do not have enough particles in PSF, Changing simulated number from "<<NParticle<<" to "<< (stop-start)/64 <<endl;
NParticle = (stop-start)/64;
}
particle.xbuffer=new float3[NParticle];
particle.vxbuffer=new float4[NParticle];
particle.eventid=new int[NParticle];
particle.time=new double[NParticle];
infile.seekg(0, ios::beg);
for(int i=0;i<NParticle;i++)
{
infile.read(reinterpret_cast <char*> (&data), sizeof(data));
particle.xbuffer[i]=make_float3(data[0],data[1],data[2]);
particle.vxbuffer[i]=make_float4(data[4],data[5],data[6],data[7]);
particle.eventid[i]=i;
particle.time[i] = data[3];
if(i<6)
{
printf("the first %d particle: %f %f %f\n",i,particle.xbuffer[i].x,particle.vxbuffer[i].x,particle.time[i] );
}
}
printf("finish read: source PSF;\n\n");
infile.close();
particle.NParticle = NParticle;
return particle;
}
Source readSource(char sourcefile[100])
{
Source source;
ifstream infile(sourcefile);
FILEEXIST(infile);
printf("reading %s\n", sourcefile);
infile >> source.NSource;
cout<< source.NSource<<"\n";
infile.ignore(512,'#');
source.natom=new unsigned int[source.NSource];
source.type=new int[source.NSource];
source.shape=new int[source.NSource];
source.shapecoeff=new float[source.NSource*6];
for(int i=0;i<source.NSource;i++)
{
infile >> source.natom[i] >> source.type[i] >> source.shape[i];
cout<< i <<" "<< source.natom[i]<<" " << source.type[i]<<" "<< source.shape[i];
for(int j=0;j<6;j++)
{
infile>>source.shapecoeff[6*i+j];
cout<<" "<<source.shapecoeff[6*i+j];
}
cout<<endl;
}//*/
printf("finish read: source;\n\n");
infile.close();
return source;
}
void spline(float *X, float *Y, float *A, float *B, float *C, float *D, float S1, float SN, int N)
// possible error from FORTRAN to C
/* CUBIC SPLINE INTERPOLATION BETWEEN TABULATED DATA.
C INPUT:
C X(I) (I=1, ...,N) ........ GRID POINTS.
C (THE X VALUES MUST BE IN INCREASING ORDER).
C Y(I) (I=1, ...,N) ........ CORRESPONDING FUNCTION VALUES.
C S1,SN ..... SECOND DERIVATIVES AT X(1) AND X(N).
C (THE NATURAL SPLINE CORRESPONDS TO TAKING S1=SN=0).
C N ........................ NUMBER OF GRID POINTS.
C
C THE INTERPOLATING POLYNOMIAL IN THE I-TH INTERVAL, FROM
C X(I) TO X(I+1), IS PI(X)=A(I)+X*(B(I)+X*(C(I)+X*D(I))).
C
C OUTPUT:
C A(I),B(I),C(I),D(I) ...... SPLINE COEFFICIENTS.
C
C REF.: M.J. MARON, 'NUMERICAL ANALYSIS: A PRACTICAL
C APPROACH', MACMILLAN PUBL. CO., NEW YORK 1982.
C*************************************************************/
{
// linear interpolation, you can use the for loop here and comment the following lines.
/* for(int i = 0; i< N-1; i++)
{
B[i] = (Y[i+1]-Y[i])/(X[i+1]-X[i]);
A[i] = (Y[i]*X[i+1] - X[i]*Y[i+1])/(X[i+1]-X[i]);
C[i] = 0.0;
D[i] = 0.0;
}*/
//IMPLICIT DOUBLE PRECISION (A-H,O-Z)
// DIMENSION X(N),Y(N),A(N),B(N),C(N),D(N)
if(N < 4)
{
printf("SPLINE INTERPOLATION CANNOT BE PERFORMED WITH %d POINTS. STOP.\n",N);
exit(1);
}
int N1 = N-1;
int N2 = N-2;
// AUXILIARY ARRAYS H(=A) AND DELTA(=D).
for(int i = 0; i < N1; i++)
{
if(X[i+1]-X[i] < 1.0e-10)
{
printf("SPLINE X VALUES NOT IN INCREASING ORDER. STOP.\n");
exit(1);
}
A[i] = X[i+1] - X[i];
D[i] = (Y[i+1] - Y[i])/A[i];
}
// SYMMETRIC COEFFICIENT MATRIX (AUGMENTED).
for(int i = 0; i < N2; i++)
{
B[i] = 2.0F * (A[i] + A[i+1]);
int k = N1 - i - 1;
D[k] = 6.0F * (D[k] - D[k-1]);
}
D[1] -= A[0] * S1;
D[N1-1] -= A[N1-1] * SN;
// GAUSS SOLUTION OF THE TRIDIAGONAL SYSTEM.
for(int i = 1; i < N2; i++)
{
float R = A[i]/B[i-1];
B[i] -= R * A[i];
D[i+1] -= R * D[i];
}
// THE SIGMA COEFFICIENTS ARE STORED IN ARRAY D.
D[N1-1] = D[N1-1]/B[N2-1];
for(int i = 1; i < N2; i++)
{
int k = N1 - i - 1;
D[k] = (D[k] - A[k] * D[k+1])/B[k-1];
}
D[N-1] = SN;
// SPLINE COEFFICIENTS.
float SI1 = S1;
for(int i = 0; i < N1; i++)
{
float SI = SI1;
SI1 = D[i+1];
float H = A[i];
float HI = 1.0F/H;
A[i] = (HI/6.0F)*(SI*X[i+1]*X[i+1]*X[i+1]-SI1*X[i]*X[i]*X[i])
+HI*(Y[i]*X[i+1]-Y[i+1]*X[i])
+(H/6.0F)*(SI1*X[i]-SI*X[i+1]);
B[i] = (HI/2.0F)*(SI1*X[i]*X[i]-SI*X[i+1]*X[i+1])
+HI*(Y[i+1]-Y[i])+(H/6.0F)*(SI-SI1);
C[i] = (HI/2.0F)*(SI*X[i+1]-SI1*X[i]);
D[i] = (HI/6.0F)*(SI1-SI);
}
return;
}
void inirngG()
/*******************************************************************
c* Set iseed1 and iseed2 for all threads with random numbers *
c* *
c* Input: *
c* Output: *
c* iseed1 -> random number *
c* iseed2 -> random number *
c******************************************************************/
{
srand( (unsigned int)time(NULL) );
// generate randseed at CPU
int *iseed1_h = (int*) malloc(sizeof(int)*NRAND);
for(int i = 0; i < NRAND; i++)
{
iseed1_h[i] = rand();
}
int *iseed1;
cudaMalloc((void**) &iseed1, sizeof(int)*NRAND);
// copy to GPU
cudaMemcpy(iseed1, iseed1_h, sizeof(int)*NRAND, cudaMemcpyHostToDevice);
free(iseed1_h);
int nblocks;
nblocks = 1 + (NRAND - 1)/NTHREAD_PER_BLOCK_GPET ;
setupcuseed<<<nblocks, NTHREAD_PER_BLOCK_GPET>>>(iseed1);
cudaDeviceSynchronize();
cudaFree(iseed1);
}
void rmater(float *eminph, float *emax)
/*******************************************************************
c* Reads material data from file *
c* *
c* Output: *
c* fname -> input file name *
c* [Emin,Eminph,Emax] -> interval where data will be gen (eV) *
c* refz -> total atomic no of the reference material *
c* refz2 -> atomic no^2 of the reference material *
c* refmas -> atomic weight of the reference material *
c* refden -> density of the reference material (g/cm^3) *
c******************************************************************/
{
char buffer[100];
float shigh,slow,ecross, temp,wcc,wcb;
//char fname[] = "data/pre4phot.matter";
char fname[] = "data/input4gPET.matter";
printf("rmater: Reading %s\n", fname);
FILE *fp = fopen(fname,"r");
FILEEXIST(fp);
fgets(buffer, 100, fp);
fgets(buffer, 100, fp);
fgets(buffer, 100, fp);
fgets(buffer, 100, fp);
printf("%s\n",buffer);
fscanf(fp,"%f %f %f\n",eminph, &temp, emax);
printf("%e %e %e\n",*eminph,temp, *emax);
fgets(buffer, 100, fp);
//printf("%s\n",buffer);
fscanf(fp,"%f %f\n",&wcc, &wcb);
//printf("%e %e\n",wcc,wcb);
fgets(buffer, 100, fp);
//printf("%s\n",buffer);
fscanf(fp,"%f %f %f\n",&shigh,&slow,&ecross);
//printf("%e %e %e\n",shigh,slow,ecross);
fgets(buffer, 100, fp);
//printf("%s\n",buffer);
fscanf(fp,"%d\n", &nmat_h);
//printf("%d\n",nmat_h);
cudaMemcpyToSymbol(nmat, &nmat_h, sizeof(int), 0, cudaMemcpyHostToDevice) ;
if (nmat_h > MAXMAT)
{
printf("rmater:error: Too many materials.\n");
exit(1);
}
for(int i = 0; i < nmat_h; i++)
{
// Read name of material, remove trailing blanks:
float matden;
int nelem;
fgets(buffer,100,fp);
//printf("%s\n", buffer);
fgets(buffer, 100, fp);
//printf("%s\n", buffer);
fscanf(fp,"%f\n", &matden);
//printf("%e\n", matden);
fgets(buffer, 100, fp);
//printf("%s\n",buffer);
fscanf(fp,"%d\n",&nelem);
//printf("%d\n", nelem);
for(int j = 0; j < nelem; j++)
{
fgets(buffer, 100, fp);
//printf("%s\n",buffer);
}
fgets(buffer, 100, fp);
//printf("%s\n",buffer);
float atnotemp,atno2temp;
fscanf(fp,"%f %f %f\n",&atnotemp, &atno2temp, &temp);
//printf("%e %e\n", atnotemp,atno2temp);
fgets(buffer, 100, fp);
//printf("%s\n",buffer);
float mass;
fscanf(fp,"%f\n", &mass);
//printf("%e\n", mass);
fgets(buffer, 100, fp);
//printf("%s\n",buffer);
float zmass,z2mass;
fscanf(fp,"%f %f\n", &zmass,&z2mass);
//printf("%e %e\n", zmass,z2mass);
}
fclose(fp);
printf("\nread material: Done.\n");
}
void rlamph()
/*******************************************************************
c* Reads photon total inverse mean free path data from file and *
c* sets up interpolation matrices *
c* *
c* Input: *
c* fname -> input file name *
c******************************************************************/
{
char buffer[100];
int ndata;
float dummya[NLAPH],dummyb[NLAPH],dummyc[NLAPH],dummyd[NLAPH];
//char fname[] = "data/pre4phot.lamph";
char fname[]="data/input4gPET.lamph";
printf("rlamph: Reading %s\n", fname);
FILE *fp = fopen(fname,"r");
FILEEXIST(fp);
fgets(buffer,100,fp);
fgets(buffer,100,fp);
for(int j = 0; j < nmat_h; j++)
{
fgets(buffer,100,fp);
float temp;
fscanf(fp,"%d %f %f %f %f\n",&ndata,&temp,&temp,&temp,&temp);
if (ndata != NLAPH)
{
printf("rlamph:error: Array dim do not match:\n");
printf("%d %d\n", ndata,NLAPH);
exit(1);
}
fgets(buffer,100,fp);
// Preparing interpolation:
for(int i = 0; i < NLAPH; i++)
{
fscanf(fp,"%f %f\n",&elaph_h[i],&lamph_h[ind2To1(j,i,MAXMAT,NLAPH)]);//excess ind2To1 equal to j*NLAPH+i,linearization
//if(i<3)
//printf("material %d, energy %e, cross section %e\n",j, elaph_h[i],lamph_h[ind2To1(j,i,MAXMAT,NLAPH)]);
}
fgets(buffer,100,fp);
spline(elaph_h, &lamph_h[ind2To1(j,0,MAXMAT,NLAPH)],dummya,dummyb,dummyc,dummyd,0.0F,0.0F,NLAPH);
// Loading dummy arrays into multimaterial sp matrices:
for(int i = 0; i < NLAPH; i++)
{
lampha_h[ind2To1(j,i,MAXMAT,NLAPH)] = dummya[i];
lamphb_h[ind2To1(j,i,MAXMAT,NLAPH)] = dummyb[i];
lamphc_h[ind2To1(j,i,MAXMAT,NLAPH)] = dummyc[i];
lamphd_h[ind2To1(j,i,MAXMAT,NLAPH)] = dummyd[i];
}
}
fclose(fp);
idleph_h = (NLAPH-1)/(elaph_h[NLAPH-1]-elaph_h[0]);
cudaMemcpyToSymbol(idleph, &idleph_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(elaph0, &elaph_h[0], sizeof(float), 0, cudaMemcpyHostToDevice);
cudaMallocArray(&lamph, &lamph_tex.channelDesc, NLAPH*MAXMAT, 1);
cudaMemcpyToArray(lamph, 0, 0, lamph_h, sizeof(float)*NLAPH*MAXMAT, cudaMemcpyHostToDevice);
lamph_tex.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(lamph_tex, lamph);
}
void rcompt()
/*******************************************************************
c* Reads Compton inverse mean free path data from file and sets *
c* up interpolation matrices *
c* *
c* Input: *
c* fname -> input file name *
c******************************************************************/
{
char buffer[100];
int ndata;
//char fname[] = "data/pre4phot.compt";
char fname[]= "data/input4gPET.compt";
printf("rcompt: Reading %s\n", fname);
FILE *fp = fopen(fname, "r");
FILEEXIST(fp);
fgets(buffer,100,fp);
fgets(buffer,100,fp);
for(int j = 0; j < nmat_h; j++)
{
fgets(buffer,100,fp);
float temp;
fscanf(fp,"%d %f %f %f %f\n",&ndata,&temp,&temp,&temp,&temp);
if (ndata != NCMPT)
{
printf("rcompt:error: Array dim do not match:\n");
printf("%d %d \n", ndata,NCMPT);
exit(1);
}
fgets(buffer,100,fp);
// Preparing interpolation:
for(int i = 0; i <NCMPT; i++)
{
fscanf(fp,"%f %f\n",&ecmpt_h[i],&compt_h[ind2To1(j,i,MAXMAT,NCMPT)]);
// if(j == nmat-1)
// printf("%e %e\n",ecmpt[i],compt[i]);
}
fgets(buffer,100,fp);
}
fclose(fp);
idlecp_h = (NCMPT-1)/(ecmpt_h[NCMPT-1]-ecmpt_h[0]);
cudaMemcpyToSymbol(idlecp, &idlecp_h, sizeof(float), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(ecmpt0, &ecmpt_h[0], sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMallocArray(&compt, &compt_tex.channelDesc, NCMPT*MAXMAT, 1);
cudaMemcpyToArray(compt, 0, 0, compt_h, sizeof(float)*NCMPT*MAXMAT, cudaMemcpyHostToDevice);
compt_tex.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(compt_tex, compt);
}
void rcmpsf()
/*******************************************************************
c* Reads Compton scattering function data from file and *
c* sets up interpolation matrices *
c******************************************************************/
{
char buffer[100];
//char fname[] = "data/pre4phot.cmpsf";
char fname[]= "data/input4gPET.cmpsf";
printf("rcmpsf: Reading %s\n", fname);
FILE *fp = fopen(fname,"r");
fgets(buffer,100,fp);
fgets(buffer,100,fp);
for(int j = 0; j < nmat_h; j++)
{
// read sf data
fgets(buffer,100,fp);
float temp;
int ndata;
fscanf(fp,"%d %f %f %f\n",&ndata,&temp,&temp,&temp);
fgets(buffer,100,fp);
for(int i = 0; i < ndata; i++)
{
fscanf(fp,"%f %f %f\n",&temp, &temp, &temp);
}
// read s surface
fgets(buffer,100,fp);
int ncp, ne;
float dcp, de;
fscanf(fp,"%d %f %f %f %d %f %f %f\n", &ncp, &temp, &temp, &dcp, &ne, &temp, &temp, &de);
if (ncp != NCPCM)
{
printf("rcmpsf:error: NCP dim do not match:\n");
printf("%d %d\n", ncp,NCPCM);
exit(1);
}
if (ne != NECM)
{
printf("rcmpsf:error: NE dim do not match:\n");
printf("%d %d\n", ne,NECM);
exit(1);
}
idcpcm_h = 1.0f/dcp;
idecm_h = 1.0f/de;
for(int icp=0; icp <ncp; icp++)
fscanf(fp,"%f ",&temp);
fscanf(fp,"\n");
for(int ie=0; ie <ne; ie++)
fscanf(fp,"%f ",&temp);
fscanf(fp,"\n");
for(int icp=0; icp <ncp; icp++)
{
for(int ie = 0; ie<ne; ie++)
{
fscanf(fp,"%f ",&mucmpt_h[j*NCPCM*NECM+icp*NECM+ie]);
// if(mucmpt_h[j*NCPCM*NECM+icp*NECM+ie] > 1.0f || mucmpt_h[j*NCPCM*NECM+icp*NECM+ie]<-1.0f)
// cout << "error in data" << mucmpt_h[j*NCPCM*NECM+icp*NECM+ie] << endl;
}
fscanf(fp,"\n");
}
fscanf(fp,"\n");
}
fclose(fp);
// load to GPU
cudaMemcpyToSymbol(idcpcm, &idcpcm_h, sizeof(float), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(idecm, &idecm_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
const cudaExtent volumeSize = make_cudaExtent(NECM, NCPCM, MAXMAT);
cudaMalloc3DArray(&sArray, &channelDesc, volumeSize) ;
cudaMemcpy3DParms copyParams = {0};
copyParams.srcPtr = make_cudaPitchedPtr((void*)mucmpt_h, volumeSize.width*sizeof(float), volumeSize.width, volumeSize.height);
copyParams.dstArray = sArray;
copyParams.extent = volumeSize;
copyParams.kind = cudaMemcpyHostToDevice;
cudaMemcpy3D(©Params) ;
s_tex.normalized = false;
s_tex.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(s_tex, sArray, channelDesc);
}
void rphote()
/*******************************************************************
c* Reads photoelectric inverse mean free path data from file and*
c* sets up interpolation matrices *
c******************************************************************/
{
char buffer[100];
int ndata;
//char fname[] = "data/pre4phot.phote";
char fname[]= "data/input4gPET.phote";
printf("rphote: Reading %s\n", fname);
FILE *fp = fopen(fname,"r");
fgets(buffer,100,fp);
fgets(buffer,100,fp);
for(int j = 0; j < nmat_h; j++)
{
fgets(buffer,100,fp);
float temp;
fscanf(fp,"%d %f %f %f %f\n",&ndata,&temp,&temp,&temp,&temp);
if (ndata != NPHTE)
{
printf("rphote:error: Array dim do not match:\n");
printf("%d %d\n", ndata,NPHTE);
exit(1);
}
fgets(buffer,100,fp);
// Preparing interpolation
for(int i = 0; i < NPHTE; i++)
{
fscanf(fp,"%f %f\n",&ephte_h[i],&phote_h[ind2To1(j,i,MAXMAT,NPHTE)]);
// if(j == nmat-1)
// printf("%e %e\n",ephte[i],phote[i]);
}
fgets(buffer,100,fp);
}
fclose(fp);
idlepe_h = (NPHTE-1)/(ephte_h[NPHTE-1]-ephte_h[0]);
cudaMemcpyToSymbol(idlepe, &idlepe_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(ephte0, &ephte_h[0], sizeof(float), 0, cudaMemcpyHostToDevice);
cudaMallocArray(&phote, &phote_tex.channelDesc, NPHTE*MAXMAT, 1);
cudaMemcpyToArray(phote, 0, 0, phote_h, sizeof(float)*NPHTE*MAXMAT, cudaMemcpyHostToDevice);
phote_tex.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(phote_tex, phote);
}
void rrayle()
/*******************************************************************
c* Reads rayleigh inverse mean free path data from file and *
c* sets up interpolation matrices *
c* *
c* Input: *
c* fname -> input file name *
c******************************************************************/
{
char buffer[100];
int ndata;
//char fname[] = "data/pre4phot.rayle";
char fname[]="data/input4gPET.rayle";
printf("rrayle: Reading %s\n", fname);
FILE *fp = fopen(fname,"r");
fgets(buffer,100,fp);
fgets(buffer,100,fp);
for(int j = 0; j < nmat_h; j++)
{
fgets(buffer,100,fp);
float temp;
fscanf(fp,"%d %f %f %f %f\n",&ndata,&temp,&temp,&temp,&temp);
if (ndata != NRAYL)
{
printf("rrayle:error: Array dim do not match:\n");
printf("%d %d\n", ndata,NRAYL);
exit(1);
}
fgets(buffer,100,fp);
// Preparing interpolation
for(int i = 0; i < NRAYL; i++)
{
fscanf(fp,"%f %f\n",&erayl_h[i],&rayle_h[ind2To1(j,i,MAXMAT,NRAYL)]);
}
fgets(buffer,100,fp);
}
fclose(fp);
idlerl_h = (NRAYL-1)/(erayl_h[NRAYL-1]-erayl_h[0]);
cudaMemcpyToSymbol(idlerl, &idlerl_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(erayl0, &erayl_h[0], sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMallocArray(&rayle, &rayle_tex.channelDesc, NRAYL*MAXMAT, 1);
cudaMemcpyToArray(rayle, 0, 0, rayle_h, sizeof(float)*NRAYL*MAXMAT, cudaMemcpyHostToDevice);
rayle_tex.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(rayle_tex, rayle);
}
void rrayff()
/*******************************************************************
c* Reads Rayleigh scattering form factor data from file and *
c* sets up interpolation matrices *
c******************************************************************/
{
char buffer[100];
//char fname[] = "data/pre4phot.rayff";
char fname[]= "data/input4gPET.rayff";
printf("rrayff: Reading %s\n", fname);
FILE *fp = fopen(fname,"r");
fgets(buffer,100,fp);
fgets(buffer,100,fp);
for(int j = 0; j < nmat_h; j++)
{
// read ff data
fgets(buffer,100,fp);
float temp;
int ndata;
fscanf(fp,"%d %f %f %f\n",&ndata,&temp,&temp,&temp);
fgets(buffer,100,fp);
for(int i = 0; i < ndata; i++)
{
fscanf(fp,"%f %f %f\n",&temp, &temp, &temp);
}
// read f surface
fgets(buffer,100,fp);
int ncp, ne;
float dcp, de;
fscanf(fp,"%d %f %f %f %d %f %f %f\n", &ncp, &temp, &temp, &dcp, &ne, &temp, &temp, &de);
if (ncp != NCPRL)
{
printf("rrayff:error: NCP dim do not match:\n");
printf("%d %d\n", ncp,NCPRL);
exit(1);
}
if (ne != NERL)
{
printf("rrayff:error: NE dim do not match:\n");
printf("%d %d\n", ne,NERL);
exit(1);
}
idcprl_h = 1.0f/dcp;
iderl_h = 1.0f/de;
for(int icp=0; icp <ncp; icp++)
fscanf(fp,"%f ",&temp);
fscanf(fp,"\n");
for(int ie=0; ie <ne; ie++)
fscanf(fp,"%f ",&temp);
fscanf(fp,"\n");
for(int icp=0; icp <ncp; icp++)
{
for(int ie = 0; ie<ne; ie++)
{
fscanf(fp,"%f ",&murayl_h[j*NCPRL*NERL+icp*NERL+ie]);
// if(murayl_h[j*NCPRL*NERL+icp*NERL+ie] > 1.0f || murayl_h[j*NCPRL*NERL+icp*NERL+ie]<-1.0f)
// cout << "error in data" << murayl_h[j*NCPRL*NERL+icp*NERL+ie] << endl;
}
fscanf(fp,"\n");
}
fscanf(fp,"\n");
// cout << murayl_h[j*NCPRL*NERL+(NCPRL-2)*NERL+1] << endl;
}
fclose(fp);
// load to GPU
cudaMemcpyToSymbol(idcprl, &idcprl_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(iderl, &iderl_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
const cudaExtent volumeSize = make_cudaExtent(NERL, NCPRL, MAXMAT);
cudaMalloc3DArray(&fArray, &channelDesc, volumeSize) ;
cudaMemcpy3DParms copyParams = {0};
copyParams.srcPtr = make_cudaPitchedPtr((void*)murayl_h, volumeSize.width*sizeof(float), volumeSize.width, volumeSize.height);
copyParams.dstArray = fArray;
copyParams.extent = volumeSize;
copyParams.kind = cudaMemcpyHostToDevice;
cudaMemcpy3D(©Params);
f_tex.normalized = false;
f_tex.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(f_tex, fArray, channelDesc);
}
float itphip(int matid, float e)
/*******************************************************************
c* Photon total inverse mean free path --3spline interpolation *
c* *
c* Input: *
c* matid -> material id# *
c* e -> kinetic energy in eV *
c* Output: *
c* Total inverse mean free path in cm^2/g *
c******************************************************************/
{
int i;
i = int(idleph_h*(e-elaph_h[0]));
return lampha_h[ind2To1(matid,i,MAXMAT,NLAPH)]
+ e*(lamphb_h[ind2To1(matid,i,MAXMAT,NLAPH)]
+ e*(lamphc_h[ind2To1(matid,i,MAXMAT,NLAPH)]
+ e*lamphd_h[ind2To1(matid,i,MAXMAT,NLAPH)] ));
}
void iniwck(float eminph,float emax, Phantom phantom)
/*******************************************************************
c* Finds information used to transport photons with the Woodcock*
c* technique *
c* *
c* Input: *
c* eminph -> minimum photon energy in data files (eV) *
c* emax -> maximum photon energy in data files (eV) *
c* Output *
c* bytes -> space allocated for arrays *
c* Comments: *
c* -> common /dpmsrc/ must be loaded previously *
c* -> rlamph() must be called previously *
c* -> emax reduced to avoid reaching the end of interpol table*
c******************************************************************/
{
float maxden[MAXMAT],de,e,ymax,ycanbe;
const float eps = 1.0e-10F;
unsigned int NXYZ = phantom.Unxvox*phantom.Unyvox*phantom.Unzvox;
printf("iniwck phantom: Started.\n");
// Find the largest density for each present material:
for(int i = 0; i < MAXMAT; i++)
{
maxden[i] = 0.0F;
}
for(int vox = 0; vox < NXYZ; vox++)
{
if (phantom.dens[vox] > maxden[phantom.mat[vox]])
maxden[phantom.mat[vox]] = phantom.dens[vox];
}
// Prepare data:
wcke0_h = eminph;
de = (emax*(1.0F - eps ) - wcke0_h ) / NWCK;
idlewk_h = 1.0F/de;
for(int i = 0; i < NWCK; i++)
{
e = wcke0_h + de*i;
ymax = 0.0;
for(int j = 0; j < nmat_h; j++)
{
ycanbe = itphip(j,e)*maxden[j];
if (ycanbe > ymax) ymax = ycanbe;
}
woock_h[i] = 1.0F/ymax;
}
cudaMemcpyToSymbol(idlewk, &idlewk_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(wcke0, &wcke0_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMallocArray(&woock, &woock_tex.channelDesc, NWCK, 1);
cudaMemcpyToArray(woock, 0, 0, woock_h, sizeof(float)*NWCK, cudaMemcpyHostToDevice);
woock_tex.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(woock_tex, woock);
}
void initPhantom(Phantom phantom)
{
printf("CT dimension: %d %d %d\n", phantom.Unxvox, phantom.Unyvox, phantom.Unzvox);
printf("CT resolution: %f %f %f\n", phantom.dx, phantom.dy, phantom.dz);
cudaMemcpyToSymbol(Unxvox, &phantom.Unxvox, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(Unyvox, &phantom.Unyvox, sizeof(int), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(Unzvox, &phantom.Unzvox, sizeof(int), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(dx_gBrachy, &phantom.dx, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(dy_gBrachy, &phantom.dy, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(dz_gBrachy, &phantom.dz, sizeof(float), 0, cudaMemcpyHostToDevice) ;
float idx_gBrachy_h,idy_gBrachy_h,idz_gBrachy_h;
idx_gBrachy_h = 1.0F/phantom.dx;
cudaMemcpyToSymbol(idx_gBrachy, &idx_gBrachy_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
idy_gBrachy_h = 1.0F/phantom.dy;
cudaMemcpyToSymbol(idy_gBrachy, &idy_gBrachy_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
idz_gBrachy_h = 1.0F/phantom.dz;
cudaMemcpyToSymbol(idz_gBrachy, &idz_gBrachy_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(Offsetx_gBrachy, &phantom.Offsetx, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(Offsety_gBrachy, &phantom.Offsety, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(Offsetz_gBrachy, &phantom.Offsetz, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaExtent volumeSize = make_cudaExtent(phantom.Unxvox, phantom.Unyvox, phantom.Unzvox);
CUDA_CALL(cudaMalloc3DArray(&mat, &mat_tex.channelDesc, volumeSize));
CUDA_CALL(cudaMalloc3DArray(&dens, &dens_tex.channelDesc, volumeSize));
// create a 3d array on device
cudaMemcpy3DParms copyParams = {0};
copyParams.srcPtr = make_cudaPitchedPtr((void*)phantom.mat, volumeSize.width*sizeof(int), volumeSize.width, volumeSize.height);
copyParams.dstArray = mat;
copyParams.extent = volumeSize;
copyParams.kind = cudaMemcpyHostToDevice;
cudaMemcpy3D(©Params) ;
// copy data from host to device
mat_tex.normalized = false;
mat_tex.filterMode = cudaFilterModePoint;
cudaBindTextureToArray(mat_tex, mat, mat_tex.channelDesc);
// bind to texture memory
copyParams.srcPtr = make_cudaPitchedPtr((void*)phantom.dens, volumeSize.width*sizeof(float), volumeSize.width, volumeSize.height);
copyParams.dstArray = dens;
copyParams.extent = volumeSize;
copyParams.kind = cudaMemcpyHostToDevice;
cudaMemcpy3D(©Params) ;
// copy data from host to device
dens_tex.normalized = false;
dens_tex.filterMode = cudaFilterModePoint;
cudaBindTextureToArray(dens_tex, dens, dens_tex.channelDesc);
// bind to texture memory
}//*/
void init(Phantom phantom)
/*******************************************************************
c* Initializes the gCTD system *
c******************************************************************/
{
initPhantom(phantom);
cudaMemcpyToSymbol(eabsph, &eabsph_h, sizeof(float), 0, cudaMemcpyHostToDevice);
// in GPU, initialize rand seed with rand numbers
inirngG();
rmater(&eminph, &emax);//no use?
printf("\n");
if(eabsph_h <eminph)
{
printf("init:error: Eabs out of range.\n");
exit(1);
}
// load total cross section
rlamph();
// load compton cross section
rcompt();
rcmpsf();
// load photoelectric cross section
rphote();
// load rayleigh cross section and form factors
rrayle();
rrayff();
// iniwck must be called after reading esrc & eabsph:
iniwck(eminph, emax, phantom);
printf("\n\nInitialize : Done.\n");//*/
}
void iniwck(float eminph,float emax, struct object_v* objectMaterial) //for detector
{
float maxden[MAXMAT],de,e,ymax,ycanbe;
const float eps = 1.0e-10F;
printf("\n");
printf("\n");
printf("iniwck detector: Started.\n");
// Find the largest density for each present material:
for(int i = 0; i < MAXMAT; i++)
{
maxden[i] = 0.0F;
}
for(int i=0; i<2; i++)
{
if (objectMaterial[i].density > maxden[objectMaterial[i].material])
maxden[objectMaterial[i].material] = objectMaterial[i].density;
}
// Prepare data:
wcke0_h = eminph;
de = (emax*(1.0F - eps ) - wcke0_h ) / NWCK;
idlewk_h = 1.0F/de;
for(int i = 0; i < NWCK; i++)
{
e = wcke0_h + de*i;
ymax = 0.0;
for(int j = 0; j < nmat_h; j++)
{
ycanbe = itphip(j,e)*maxden[j];
if (ycanbe > ymax)
ymax = ycanbe;
}
woock_h[i] = 1.0F/ymax;
/*if (i<1100 && i>1090)
printf("1/lamda=%f\n",woock_h[i]);*/
}
cudaMemcpyToSymbol(idlewk, &idlewk_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMemcpyToSymbol(wcke0, &wcke0_h, sizeof(float), 0, cudaMemcpyHostToDevice) ;
cudaMallocArray(&woockde, &woockde_tex.channelDesc, NWCK, 1);
cudaMemcpyToArray(woockde, 0, 0, woock_h, sizeof(float)*NWCK, cudaMemcpyHostToDevice);
woockde_tex.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(woockde_tex, woockde);
}
void iniPanel(struct object_t* objectArray, struct object_v* objectMaterial,int totalOb)
/*******************************************************************
c* Initializes the module system *
c******************************************************************/
{
printf(" \n");
printf("init: Panel geometry;\n");
// copy arrays from host to device
int *ma=new int[2];
float *den=new float[2];
int *p_id=new int[totalOb];
float *lx_m=new float[totalOb];
float *ly_m=new float[totalOb];
float *lz_m=new float[totalOb];
float *Mx_m=new float[totalOb];
float *My_m=new float[totalOb];
float *Mz_m=new float[totalOb];
float *Msx_m=new float[totalOb];
float *Msy_m=new float[totalOb];
float *Msz_m=new float[totalOb];
float *Lx_m=new float[totalOb];
float *Ly_m=new float[totalOb];
float *Lz_m=new float[totalOb];
float *sx_m=new float[totalOb];
float *sy_m=new float[totalOb];