| [788bdd2] | 1 | #include "XSbench_header.h"
|
|---|
| 2 |
|
|---|
| 3 | #ifdef MPI
|
|---|
| 4 | #include<mpi.h>
|
|---|
| 5 | #endif
|
|---|
| 6 |
|
|---|
| 7 | // Generates randomized energy grid for each nuclide
|
|---|
| 8 | // Note that this is done as part of initialization (serial), so
|
|---|
| 9 | // rand() is used.
|
|---|
| 10 | void generate_grids( NuclideGridPoint ** nuclide_grids,
|
|---|
| 11 | long n_isotopes, long n_gridpoints ) {
|
|---|
| 12 | for( long i = 0; i < n_isotopes; i++ )
|
|---|
| 13 | for( long j = 0; j < n_gridpoints; j++ )
|
|---|
| 14 | {
|
|---|
| 15 | nuclide_grids[i][j].energy =((double)rand()/(double)RAND_MAX);
|
|---|
| 16 | nuclide_grids[i][j].total_xs =((double)rand()/(double)RAND_MAX);
|
|---|
| 17 | nuclide_grids[i][j].elastic_xs =((double)rand()/(double)RAND_MAX);
|
|---|
| 18 | nuclide_grids[i][j].absorbtion_xs=((double)rand()/(double)RAND_MAX);
|
|---|
| 19 | nuclide_grids[i][j].fission_xs =((double)rand()/(double)RAND_MAX);
|
|---|
| 20 | nuclide_grids[i][j].nu_fission_xs=((double)rand()/(double)RAND_MAX);
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | // Verification version of this function (tighter control over RNG)
|
|---|
| 25 | void generate_grids_v( NuclideGridPoint ** nuclide_grids,
|
|---|
| 26 | long n_isotopes, long n_gridpoints ) {
|
|---|
| 27 | for( long i = 0; i < n_isotopes; i++ )
|
|---|
| 28 | for( long j = 0; j < n_gridpoints; j++ )
|
|---|
| 29 | {
|
|---|
| 30 | nuclide_grids[i][j].energy = rn_v();
|
|---|
| 31 | nuclide_grids[i][j].total_xs = rn_v();
|
|---|
| 32 | nuclide_grids[i][j].elastic_xs = rn_v();
|
|---|
| 33 | nuclide_grids[i][j].absorbtion_xs= rn_v();
|
|---|
| 34 | nuclide_grids[i][j].fission_xs = rn_v();
|
|---|
| 35 | nuclide_grids[i][j].nu_fission_xs= rn_v();
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | // Sorts the nuclide grids by energy (lowest -> highest)
|
|---|
| 40 | void sort_nuclide_grids( NuclideGridPoint ** nuclide_grids, long n_isotopes,
|
|---|
| 41 | long n_gridpoints )
|
|---|
| 42 | {
|
|---|
| 43 | int (*cmp) (const void *, const void *);
|
|---|
| 44 | cmp = NGP_compare;
|
|---|
| 45 |
|
|---|
| 46 | for( long i = 0; i < n_isotopes; i++ )
|
|---|
| 47 | qsort( nuclide_grids[i], n_gridpoints, sizeof(NuclideGridPoint),
|
|---|
| 48 | cmp );
|
|---|
| 49 |
|
|---|
| 50 | // error debug check
|
|---|
| 51 | /*
|
|---|
| 52 | for( int i = 0; i < n_isotopes; i++ )
|
|---|
| 53 | {
|
|---|
| 54 | printf("NUCLIDE %d ==============================\n", i);
|
|---|
| 55 | for( int j = 0; j < n_gridpoints; j++ )
|
|---|
| 56 | printf("E%d = %lf\n", j, nuclide_grids[i][j].energy);
|
|---|
| 57 | }
|
|---|
| 58 | */
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | // Allocates unionized energy grid, and assigns union of energy levels
|
|---|
| 62 | // from nuclide grids to it.
|
|---|
| 63 | GridPoint * generate_energy_grid( long n_isotopes, long n_gridpoints,
|
|---|
| 64 | NuclideGridPoint ** nuclide_grids) {
|
|---|
| 65 | int mype = 0;
|
|---|
| 66 |
|
|---|
| 67 | #ifdef MPI
|
|---|
| 68 | MPI_Comm_rank(MPI_COMM_WORLD, &mype);
|
|---|
| 69 | #endif
|
|---|
| 70 |
|
|---|
| 71 | if( mype == 0 ) printf("Generating Unionized Energy Grid...\n");
|
|---|
| 72 |
|
|---|
| 73 | long n_unionized_grid_points = n_isotopes*n_gridpoints;
|
|---|
| 74 | int (*cmp) (const void *, const void *);
|
|---|
| 75 | cmp = NGP_compare;
|
|---|
| 76 |
|
|---|
| 77 | GridPoint * energy_grid = (GridPoint *)malloc( n_unionized_grid_points
|
|---|
| 78 | * sizeof( GridPoint ) );
|
|---|
| 79 | if( mype == 0 ) printf("Copying and Sorting all nuclide grids...\n");
|
|---|
| 80 |
|
|---|
| 81 | NuclideGridPoint ** n_grid_sorted = gpmatrix( n_isotopes, n_gridpoints );
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 | memcpy( n_grid_sorted[0], nuclide_grids[0], n_isotopes*n_gridpoints*
|
|---|
| 85 | sizeof( NuclideGridPoint ) );
|
|---|
| 86 |
|
|---|
| 87 | qsort( &n_grid_sorted[0][0], n_unionized_grid_points,
|
|---|
| 88 | sizeof(NuclideGridPoint), cmp);
|
|---|
| 89 |
|
|---|
| 90 | if( mype == 0 ) printf("Assigning energies to unionized grid...\n");
|
|---|
| 91 |
|
|---|
| 92 | for( long i = 0; i < n_unionized_grid_points; i++ )
|
|---|
| 93 | energy_grid[i].energy = n_grid_sorted[0][i].energy;
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 | gpmatrix_free(n_grid_sorted);
|
|---|
| 97 |
|
|---|
| 98 | int * full = (int *) malloc( n_isotopes * n_unionized_grid_points
|
|---|
| 99 | * sizeof(int) );
|
|---|
| 100 | if( full == NULL )
|
|---|
| 101 | {
|
|---|
| 102 | fprintf(stderr,"ERROR - Out Of Memory!\n");
|
|---|
| 103 | exit(1);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | for( long i = 0; i < n_unionized_grid_points; i++ )
|
|---|
| 107 | energy_grid[i].xs_ptrs = &full[n_isotopes * i];
|
|---|
| 108 |
|
|---|
| 109 | // debug error checking
|
|---|
| 110 | /*
|
|---|
| 111 | for( int i = 0; i < n_unionized_grid_points; i++ )
|
|---|
| 112 | printf("E%d = %lf\n", i, energy_grid[i].energy);
|
|---|
| 113 | */
|
|---|
| 114 |
|
|---|
| 115 | return energy_grid;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // Searches each nuclide grid for the closest energy level and assigns
|
|---|
| 119 | // pointer from unionized grid to the correct spot in the nuclide grid.
|
|---|
| 120 | // This process is time consuming, as the number of binary searches
|
|---|
| 121 | // required is: binary searches = n_gridpoints * n_isotopes^2
|
|---|
| 122 | void set_grid_ptrs( GridPoint * energy_grid, NuclideGridPoint ** nuclide_grids,
|
|---|
| 123 | long n_isotopes, long n_gridpoints )
|
|---|
| 124 | {
|
|---|
| 125 | int mype = 0;
|
|---|
| 126 |
|
|---|
| 127 | #ifdef MPI
|
|---|
| 128 | MPI_Comm_rank(MPI_COMM_WORLD, &mype);
|
|---|
| 129 | #endif
|
|---|
| 130 |
|
|---|
| 131 | if( mype == 0 ) printf("Assigning pointers to Unionized Energy Grid...\n");
|
|---|
| 132 | #pragma omp parallel for default(none) \
|
|---|
| 133 | shared( energy_grid, nuclide_grids, n_isotopes, n_gridpoints, mype )
|
|---|
| 134 | for( long i = 0; i < n_isotopes * n_gridpoints ; i++ )
|
|---|
| 135 | {
|
|---|
| 136 | double quarry = energy_grid[i].energy;
|
|---|
| 137 | if( INFO && mype == 0 && omp_get_thread_num() == 0 && i % 200 == 0 )
|
|---|
| 138 | printf("\rAligning Unionized Grid...(%.0lf%% complete)",
|
|---|
| 139 | 100.0 * (double) i / (n_isotopes*n_gridpoints /
|
|---|
| 140 | omp_get_num_threads()) );
|
|---|
| 141 | for( long j = 0; j < n_isotopes; j++ )
|
|---|
| 142 | {
|
|---|
| 143 | // j is the nuclide i.d.
|
|---|
| 144 | // log n binary search
|
|---|
| 145 | energy_grid[i].xs_ptrs[j] =
|
|---|
| 146 | binary_search( nuclide_grids[j], quarry, n_gridpoints);
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | if( mype == 0 ) printf("\n");
|
|---|
| 150 |
|
|---|
| 151 | //test
|
|---|
| 152 | /*
|
|---|
| 153 | for( int i=0; i < n_isotopes * n_gridpoints; i++ )
|
|---|
| 154 | for( int j = 0; j < n_isotopes; j++ )
|
|---|
| 155 | printf("E = %.4lf\tNuclide %d->%p->%.4lf\n",
|
|---|
| 156 | energy_grid[i].energy,
|
|---|
| 157 | j,
|
|---|
| 158 | energy_grid[i].xs_ptrs[j],
|
|---|
| 159 | (energy_grid[i].xs_ptrs[j])->energy
|
|---|
| 160 | );
|
|---|
| 161 | */
|
|---|
| 162 | }
|
|---|