r72: Initial C++ versions
[ctsim.git] / src / phm2rs.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 ** PURPOSE
6 **   Generate raysum projections from phantom object
7 **
8 ** HISTORY
9 **   1984 -- Final DOS version  
10 **   1999 -- First UNIX version
11 **
12 **  $Id: phm2rs.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
13 **  $Log: phm2rs.cpp,v $
14 **  Revision 1.1  2000/06/07 02:29:05  kevin
15 **  Initial C++ versions
16 **
17 **  Revision 1.12  2000/05/24 22:50:04  kevin
18 **  Added support for new SGP library
19 **
20 **  Revision 1.11  2000/05/16 04:33:59  kevin
21 **  Improved option processing
22 **
23 **  Revision 1.9  2000/05/08 20:02:32  kevin
24 **  ANSI C changes
25 **
26 **  Revision 1.8  2000/05/04 18:16:34  kevin
27 **  renamed filter definitions
28 **
29 **  Revision 1.7  2000/05/03 08:49:50  kevin
30 **  Code cleanup
31 **
32 **  Revision 1.6  2000/04/30 18:23:53  kevin
33 **  Code cleaning
34 **
35 **  Revision 1.5  2000/04/30 10:13:27  kevin
36 **  Fixed MPI bugs
37 **
38 **  Revision 1.4  2000/04/30 04:06:13  kevin
39 **  Update Raysum i/o routines
40 **  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
41 **
42 **  Revision 1.3  2000/04/29 23:24:56  kevin
43 **  *** empty log message ***
44 **
45 **  Revision 1.2  2000/04/28 13:50:45  kevin
46 **  Removed Makefile Makefile.in that are automatically generated by autoconf
47 **
48 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
49 **  Initial CVS import for first public release
50 **
51 **
52 **
53 **  This program is free software; you can redistribute it and/or modify
54 **  it under the terms of the GNU General Public License (version 2) as
55 **  published by the Free Software Foundation.
56 **
57 **  This program is distributed in the hope that it will be useful,
58 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
59 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
60 **  GNU General Public License for more details.
61 **
62 **  You should have received a copy of the GNU General Public License
63 **  along with this program; if not, write to the Free Software
64 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
65 ******************************************************************************/
66
67
68 #include "ct.h"
69
70 enum { O_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_PHMFILE,
71        O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
72
73 static struct option phm2rs_options[] = 
74 {
75   {"phantom", 1, 0, O_PHANTOM},
76   {"phmfile", 1, 0, O_PHMFILE},
77   {"desc", 1, 0, O_DESC},
78   {"nray", 1, 0, O_NRAY},
79   {"rotangle", 1, 0, O_ROTANGLE},
80   {"trace", 1, 0, O_TRACE},
81   {"verbose", 0, 0, O_VERBOSE},
82   {"help", 0, 0, O_HELP},
83   {"debug", 0, 0, O_DEBUG},
84   {"version", 0, 0, O_VERSION},
85   {0, 0, 0, 0}
86 };
87
88
89 void 
90 phm2rs_usage (const char *program)
91 {
92 #ifdef MPI_CT
93 if (mpi_ct.my_rank == 0)
94   {
95 #endif  
96   fprintf(stdout,"usage: %s outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n", kbasename(program));
97   fprintf(stdout,"Calculate raysums (projections) through phantom object, either\n");
98   fprintf(stdout,"a predefined --phantom or a --phmfile\n");
99   fprintf(stdout,"\n");
100   fprintf(stdout,"     outfile      Name of output file for raysums\n");
101   fprintf(stdout,"     ndet         Number of detectors\n");
102   fprintf(stdout,"     nview        Number of rotated views\n");
103   fprintf(stdout,"     --phantom    Phantom to use for projection\n");
104   fprintf(stdout,"        herman    Herman head phantom\n");
105   fprintf(stdout,"        rowland   Rowland head phantom\n");
106   fprintf(stdout,"        browland  Bordered Rowland head phantom\n");
107   fprintf(stdout,"        unitpulse Unit pulse phantom\n");
108   fprintf(stdout,"     --phmfile    Get Phantom from phantom file\n");
109   fprintf(stdout,"     --desc       Description of raysum\n");
110   fprintf(stdout,"     --nray       Number of rays per detector (default = 1)\n");
111   fprintf(stdout,"     --rotangle   Degrees to rotate view through, multiple of PI (default = 1)\n");
112   fprintf(stdout,"     --trace      Trace level to use\n");
113   fprintf(stdout,"        none      No tracing (default)\n");
114   fprintf(stdout,"        text      Trace text level\n");
115   fprintf(stdout,"        phm       Trace phantom image\n");
116   fprintf(stdout,"        rays      Trace rays\n");
117   fprintf(stdout,"        plot      Trace plot\n");
118   fprintf(stdout,"        clipping  Trace clipping\n");
119   fprintf(stdout,"     --verbose    Verbose mode\n");
120   fprintf(stdout,"     --debug      Debug mode\n");
121   fprintf(stdout,"     --version    Print version\n");
122   fprintf(stdout,"     --help       Print this help message\n");
123 #ifdef MPI_CT
124   }
125   MPI_Abort(mpi_ct.comm, 1);
126 #endif
127 }
128
129 #ifdef MPI_CT
130 void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug);
131 #endif
132
133 int 
134 phm2rs_main (const int argc, char *const argv[])
135 {
136   DETECTOR *det;
137   PHANTOM *phm = NULL;
138   RAYSUM *rs_global = NULL;
139   char str[256], *opt_outfile = NULL, opt_desc[MAXREMARK+1];
140   char opt_phmfilename[256];
141   char *endptr, *endstr;
142   int opt_ndet, opt_nview;
143   int opt_nray = 1;
144   int opt_trace = 0, opt_phmnum = -1;
145   int opt_verbose = 0;
146   int opt_debug = 0;
147   double opt_rotangle = 1;
148   double time_start = 0, time_end = 0;
149
150 #ifndef MPI_CT
151   time_start = td_current_sec();
152 #else
153   RAYSUM *rs_local;
154   int mpi_argc = argc;
155   char **mpi_argv = (char **) argv;
156   double mpi_t1 = 0, mpi_t2 = 0, mpi_t, mpi_t_g;
157
158   MPI_Init(&mpi_argc, &mpi_argv);
159   MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
160   MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
161   MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
162
163   if (mpi_ct.nproc > MPI_MAX_PROCESS) {
164     sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)", mpi_ct.nproc, MPI_MAX_PROCESS);
165     exit(1);
166   }
167   time_start = MPI_Wtime();
168 #endif
169   
170 #ifdef MPI_CT
171   if (mpi_ct.my_rank == 0) {
172 #endif
173     strcpy(opt_desc, "");
174     strcpy(opt_phmfilename, "");
175     while (1) {
176       int c = getopt_long(argc, argv, "", phm2rs_options, NULL);
177       char *endptr = NULL;
178       char *endstr;
179       
180       if (c == -1)
181         break;
182       
183       switch (c) {
184       case O_PHANTOM:
185         if ((opt_phmnum = opt_set_phantom (optarg)) < 0) {
186           phm2rs_usage(argv[0]);
187           return (1);
188         }
189         phm = phm_create (opt_phmnum);
190         break;
191       case O_PHMFILE:
192 #ifdef MPI_CT
193         if (mpi_ct.my_rank == 0)
194           fprintf(stderr, "Can not read phantom from file in MPI mode\n");
195         return (1);
196 #endif
197         strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
198         phm = phm_create_from_file(opt_phmfilename);
199         break;
200       case O_VERBOSE:
201         opt_verbose = 1;
202         break;
203       case O_DEBUG:
204         opt_debug = 1;
205         break;
206         break;
207       case O_TRACE:
208         if ((opt_trace = opt_set_trace(optarg)) < 0) {
209           phm2rs_usage(argv[0]);
210           return (1);
211         }
212         break;
213       case O_DESC:
214         strncpy(opt_desc, optarg, sizeof(opt_desc));
215         break;
216       case O_ROTANGLE:
217         opt_rotangle = strtod(optarg, &endptr);
218         endstr = optarg + strlen(optarg);
219         if (endptr != endstr) {
220           fprintf(stderr,"Error setting --rotangle to %s\n", optarg);
221           phm2rs_usage(argv[0]);
222           return (1);
223         }
224         break;
225       case O_NRAY:
226         opt_nray = strtol(optarg, &endptr, 10);
227         endstr = optarg + strlen(optarg);
228         if (endptr != endstr) {
229           fprintf(stderr,"Error setting --nray to %s\n", optarg);
230           phm2rs_usage(argv[0]);
231           return (1);
232         }
233         break;
234         case O_VERSION:
235 #ifdef VERSION
236           fprintf(stdout, "Version %s\n", VERSION);
237 #else
238           fprintf(stderr, "Unknown version number");
239 #endif
240           exit(0);
241       case O_HELP:
242       case '?':
243         phm2rs_usage(argv[0]);
244         return (0);
245       default:
246         phm2rs_usage(argv[0]);
247         return (1);
248       }
249     }
250   
251     if (phm == NULL) {
252       fprintf(stderr, "No phantom defined\n");
253       phm2rs_usage(argv[0]);
254       return (1);
255     }
256     if (optind + 3 != argc) {
257       phm2rs_usage(argv[0]);
258       return (1);
259     }
260
261     opt_outfile = argv[optind];
262     opt_ndet = strtol(argv[optind+1], &endptr, 10);
263     endstr = argv[optind+1] + strlen(argv[optind+1]);
264     if (endptr != endstr) {
265       fprintf(stderr,"Error setting --ndet to %s\n", argv[optind+1]);
266       phm2rs_usage(argv[0]);
267       return (1);
268     }
269     opt_nview = strtol(argv[optind+2], &endptr, 10);
270     endstr = argv[optind+2] + strlen(argv[optind+2]);
271     if (endptr != endstr) {
272       fprintf(stderr,"Error setting --nview to %s\n", argv[optind+2]);
273       phm2rs_usage(argv[0]);
274       return (1);
275     }
276
277     snprintf(str, sizeof(str), 
278              "Raysum_Collect: NDet=%d, Nview=%d, NRay=%d, RotAngle=%.2f, ", 
279              opt_ndet, opt_nview, opt_nray, opt_rotangle);
280     if (opt_phmfilename[0]) {
281       strncat(str, "Phantom=", sizeof(str));
282       strncat(str, opt_phmfilename, sizeof(str));
283     } else if (opt_phmnum != -1) {
284       strncat(str, "Phantom=", sizeof(str));
285       strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
286     }
287     if (opt_desc[0]) {
288       strncat(str, ": ", sizeof(str));
289       strncat(str, opt_desc, sizeof(str));
290     }
291     strncpy(opt_desc, str, sizeof(opt_desc));
292 #ifdef MPI_CT
293   }
294 #endif
295
296 #ifdef MPI_CT
297   MPI_Barrier(mpi_ct.comm);
298   MPI_Bcast(&opt_rotangle, 1, MPI_DOUBLE, 0, mpi_ct.comm);
299   MPI_Bcast(&opt_nview, 1, MPI_INT, 0, mpi_ct.comm);
300   MPI_Bcast(&opt_ndet, 1, MPI_INT, 0, mpi_ct.comm);
301   MPI_Bcast(&opt_nray, 1, MPI_INT, 0, mpi_ct.comm);
302   MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
303   MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
304   MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
305   MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
306
307   if (mpi_ct.my_rank > 0 && opt_phmnum >= 0)
308     phm = phm_create (opt_phmnum);
309 #endif
310
311   opt_rotangle *= PI;
312   det = detector_create (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle);
313
314 #ifdef MPI_CT
315   mpi_ct_calc_work_units(opt_nview);
316   if (mpi_ct.my_rank == 0) {
317     rs_global = raysum_create_from_det (opt_outfile, det);
318     raysum_alloc_views(rs_global);
319   }
320   
321   rs_local = raysum_create_from_det (NULL, det);
322   rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
323   if (opt_debug)
324     printf("rs_local->nview = %d (process %d)\n", rs_local->nview, mpi_ct.my_rank);
325
326   if (opt_verbose)
327     mpi_t1 = MPI_Wtime();
328   raysum_collect (rs_local, det, phm, mpi_ct.start_work_unit[mpi_ct.my_rank], opt_trace, FALSE);
329   if (opt_verbose) {
330     mpi_t2 = MPI_Wtime();
331     mpi_t = mpi_t2 - mpi_t1;
332     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
333     if (mpi_ct.my_rank == 0)
334       printf("Time to collect rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
335   }
336
337   if (opt_verbose)
338     mpi_t1 = MPI_Wtime();
339   mpi_gather_rs(rs_global, rs_local, opt_debug);
340   if (opt_verbose) {
341     mpi_t2 = MPI_Wtime();
342     mpi_t = mpi_t2 - mpi_t1;
343     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
344     if (mpi_ct.my_rank == 0)
345       printf("Time to gather rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
346   }
347 #else
348   rs_global = raysum_create_from_det (opt_outfile, det);
349   raysum_collect (rs_global, det, phm, 0, opt_trace, FALSE);
350 #endif
351   
352 #ifndef MPI_CT
353   time_end = td_current_sec();
354 #else
355   time_end = MPI_Wtime();
356   if (mpi_ct.my_rank == 0) 
357 #endif
358     {
359       rs_global->calctime = time_end - time_start;
360       strncpy (rs_global->remark, opt_desc, sizeof(rs_global->remark));
361       if (opt_verbose) {
362         fprintf(stdout, "Remark: %s\n", rs_global->remark);
363         fprintf(stdout, "Time active = %.2f secs\n", rs_global->calctime);
364       }
365     }
366
367 #ifdef MPI_CT
368   if (mpi_ct.my_rank == 0) 
369 #endif
370     {
371       raysum_write (rs_global);
372       raysum_close (rs_global);
373     }
374
375   phm_free (phm);
376   detector_free (det);
377
378   return (0);
379 }
380
381
382 /* FUNCTION
383  *    mpi_gather_rs
384  *
385  * SYNOPSIS
386  *    Gather's raysums from all processes in rs_local in rs_global in process 0
387  */
388
389 #ifdef MPI_CT
390 void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
391 {
392   int iproc;
393   int end_work_unit;
394   int iw;
395
396   end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
397   for (iw = 0; iw <= end_work_unit; iw++) {
398     MPI_Send(&rs_local->view[iw]->view_angle, 1, MPI_DOUBLE, 0, 0, mpi_ct.comm);
399     MPI_Send(&rs_local->view[iw]->ndet, 1, MPI_INT, 0, 0, mpi_ct.comm);
400     MPI_Send(rs_local->view[iw]->detval, rs_local->ndet, MPI_FLOAT, 0, 0, mpi_ct.comm);
401   }
402
403   if (mpi_ct.my_rank == 0) {
404     for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
405       end_work_unit = mpi_ct.start_work_unit[iproc] 
406         + mpi_ct.local_work_units[iproc] 
407         - 1;
408
409       if (opt_debug)
410         fprintf(stdout, "Recv rs data from process %d\n", iproc);
411
412       for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
413         MPI_Status status;
414
415         MPI_Recv(&rs_global->view[iw]->view_angle, 1, MPI_DOUBLE, iproc, 0, mpi_ct.comm, &status);
416         MPI_Recv(&rs_global->view[iw]->ndet, 1, MPI_INT, iproc, 0, mpi_ct.comm, &status);
417         MPI_Recv(rs_global->view[iw]->detval, rs_global->ndet, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
418       }
419     }
420   }
421
422 }
423 #endif
424
425
426 #ifndef NO_MAIN
427 int 
428 main (const int argc, char *const argv[])
429 {
430   return (phm2rs_main(argc, argv));
431 }
432 #endif
433