/* lsq is a linear least squares optimization algorithm for finding optimal energy fluence weighting factors for radiation therapy. Tomotherapy has a patent on the algorithm. The algorithm is explained in detail by Dave Shepard in the publication: PMB 45 (2000) 69-90. */ #include "lsq.h" char errstr[200]; // globally available string for passing errors int firstIterFlag; // flag that is activated only on the first iteration #define OPT_INPUT_FILE "optInput.txt" int main(int argc, char *argv[]) // Expects one or zero command line input arguments. If a lsq header file is not // included as an input argument, then OPT_INPUT_FILE is used. { float weight_diff, start_time, end_time, update_factor; float clockStart, clockStop; int *usedVoxels; int i, j, iter_num; // filename strings char opt_input_file[200]; float *E; // objective function values char doseFileStr[200]; char weightFileStr[200]; // structures for the optimization PRESC presc; PRESCGRID prescGrid; OPT_PARAM op; GRID dose; GRID doseTot; GRID_ARRAY beamlets; // dummy file FILE *fid; // check to see if an input argument was supplied if (argc > 2) // calling the executable counts as one argument { printf("Expecting one or zero input command line arguments, received %d.\n",argc); return(FAILURE); } if (argc == 1) // only the executable was called, no other arguments strcpy(opt_input_file,OPT_INPUT_FILE); else // one arguments was supplied strcpy(opt_input_file,argv[1]); // read in optimization parameters if (read_in_opt_parameters(&op, opt_input_file) == FAILURE) { printf("Failed to read in optimization properties:\n%s\n",errstr); return(FAILURE); } else printf("Successfully read-in optimization parameters.\n"); // print optimization parameters for verification of readin printf("Niterations = %d\n",op.Niterations); printf("prescription_filename = %s\n",op.prescription_filename); printf("initial_beam_weights_filename = %s\n",op.initial_beam_weights_filename); printf("beamlet_header_file = %s\n",op.beamlet_header_file); printf("dose_batch_base_name = %s\n",op.dose_batch_base_name); printf("dose_batch_extension = %s\n",op.dose_batch_extension); printf("weight_batch_base_name = %s\n",op.weight_batch_base_name); printf("weight_batch_extension = %s\n",op.weight_batch_extension); printf("modFactor = %d\n",op.modFactor); // read in the prescription if (read_in_prescription(&presc, op.prescription_filename) == FAILURE) { printf("Failed to read in prescription:\n%s\n",errstr); return(FAILURE); } else printf("Successfully read-in prescription.\n"); fillPrescriptionGrid(&presc, &prescGrid); if ( (usedVoxels = (int *)malloc(sizeof(int)*presc.Nvox)) == NULL) { printf("Memory allocation for usedVoxels array failed.\n"); return(FAILURE); } fid = fopen("prescGridTest.bin","wb"); fwrite(prescGrid.numNodes,sizeof(int),prescGrid.Nvox,fid); fclose(fid); if (SMARTBEAMLETS == 1) find_used_voxels(&presc,usedVoxels); else for (i=0;i 0) free(prescGrid.array[i]); free(prescGrid.array); free(prescGrid.numNodes); printf("Elapsed optimization time %f\n",end_time-start_time); // create a file that signals that the optimization is done fid = fopen("optDone.txt","w"); fprintf(fid,"Optimization complete\n"); fclose(fid); return(SUCCESS); }