source: CIVL/examples/xsbench/src/XSutils.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#include "XSbench_header.h"
2
3// Allocates nuclide matrix
4NuclideGridPoint ** gpmatrix(size_t m, size_t n)
5{
6 int i,j;
7 NuclideGridPoint * full = (NuclideGridPoint *) malloc( m * n *
8 sizeof( NuclideGridPoint ) );
9 NuclideGridPoint ** M = (NuclideGridPoint **) malloc( m *
10 sizeof(NuclideGridPoint *) );
11
12 for( i = 0, j=0; i < m*n; i++ )
13 if( i % n == 0 )
14 M[j++] = &full[i];
15
16 return M;
17}
18
19// Frees nuclide matrix
20void gpmatrix_free( NuclideGridPoint ** M )
21{
22 free( *M );
23 free( M );
24}
25
26// Compare function for two grid points. Used for sorting during init
27int NGP_compare( const void * a, const void * b )
28{
29 NuclideGridPoint *i, *j;
30
31 i = (NuclideGridPoint *) a;
32 j = (NuclideGridPoint *) b;
33
34 if( i->energy > j->energy )
35 return 1;
36 else if ( i->energy < j->energy)
37 return -1;
38 else
39 return 0;
40}
41
42
43
44// Binary Search function for nuclide grid
45// Returns ptr to energy less than the quarry that is closest to the quarry
46int binary_search( NuclideGridPoint * A, double quarry, int n )
47{
48 int min = 0;
49 int max = n-1;
50 int mid;
51
52 // checks to ensure we're not reading off the end of the grid
53 if( A[0].energy > quarry )
54 return 0;
55 else if( A[n-1].energy < quarry )
56 return n-2;
57
58 // Begins binary search
59 while( max >= min )
60 {
61 mid = min + floor( (max-min) / 2.0);
62 if( A[mid].energy < quarry )
63 min = mid+1;
64 else if( A[mid].energy > quarry )
65 max = mid-1;
66 else
67 return mid;
68 }
69 return max;
70}
71
72// Park & Miller Multiplicative Conguential Algorithm
73// From "Numerical Recipes" Second Edition
74double rn(unsigned long * seed)
75{
76 double ret;
77 unsigned long n1;
78 unsigned long a = 16807;
79 unsigned long m = 2147483647;
80 n1 = ( a * (*seed) ) % m;
81 *seed = n1;
82 ret = (double) n1 / m;
83 return ret;
84}
85
86
87
88// RNG Used for Verification Option.
89// This one has a static seed (must be set manually in source).
90// Park & Miller Multiplicative Conguential Algorithm
91// From "Numerical Recipes" Second Edition
92double rn_v(void)
93{
94 static unsigned long seed = 1337;
95 double ret;
96 unsigned long n1;
97 unsigned long a = 16807;
98 unsigned long m = 2147483647;
99 n1 = ( a * (seed) ) % m;
100 seed = n1;
101 ret = (double) n1 / m;
102 return ret;
103}
104
105unsigned int hash(unsigned char *str, int nbins)
106{
107 unsigned int hash = 5381;
108 int c;
109
110 while (c = *str++)
111 hash = ((hash << 5) + hash) + c;
112
113 return hash % nbins;
114}
115
116size_t estimate_mem_usage( Inputs in )
117{
118 size_t single_nuclide_grid = in.n_gridpoints * sizeof( NuclideGridPoint );
119 size_t all_nuclide_grids = in.n_isotopes * single_nuclide_grid;
120 size_t size_GridPoint = sizeof(GridPoint) + in.n_isotopes*sizeof(int);
121 size_t size_UEG = in.n_isotopes*in.n_gridpoints * size_GridPoint;
122 size_t memtotal;
123
124 memtotal = all_nuclide_grids + size_UEG;
125 all_nuclide_grids = all_nuclide_grids / 1048576;
126 size_UEG = size_UEG / 1048576;
127 memtotal = memtotal / 1048576;
128 return memtotal;
129}
130
131void binary_dump(long n_isotopes, long n_gridpoints, NuclideGridPoint ** nuclide_grids, GridPoint * energy_grid)
132{
133 FILE * fp = fopen("XS_data.dat", "wb");
134 // Dump Nuclide Grid Data
135 for( long i = 0; i < n_isotopes; i++ )
136 fwrite(nuclide_grids[i], sizeof(NuclideGridPoint), n_gridpoints, fp);
137 // Dump UEG Data
138 for( long i = 0; i < n_isotopes * n_gridpoints; i++ )
139 {
140 // Write energy level
141 fwrite(&energy_grid[i].energy, sizeof(double), 1, fp);
142
143 // Write index data array (xs_ptrs array)
144 fwrite(energy_grid[i].xs_ptrs, sizeof(int), n_isotopes, fp);
145 }
146
147 fclose(fp);
148}
149
150void binary_read(long n_isotopes, long n_gridpoints, NuclideGridPoint ** nuclide_grids, GridPoint * energy_grid)
151{
152 int stat;
153 FILE * fp = fopen("XS_data.dat", "rb");
154 // Read Nuclide Grid Data
155 for( long i = 0; i < n_isotopes; i++ )
156 stat = fread(nuclide_grids[i], sizeof(NuclideGridPoint), n_gridpoints, fp);
157 // Dump UEG Data
158 for( long i = 0; i < n_isotopes * n_gridpoints; i++ )
159 {
160 // Write energy level
161 stat = fread(&energy_grid[i].energy, sizeof(double), 1, fp);
162
163 // Write index data array (xs_ptrs array)
164 stat = fread(energy_grid[i].xs_ptrs, sizeof(int), n_isotopes, fp);
165 }
166
167 fclose(fp);
168
169}
Note: See TracBrowser for help on using the repository browser.