-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.hh
495 lines (358 loc) · 13.4 KB
/
random.hh
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
/*
random.hh - This file is part of MUSIC -
a code to generate multi-scale initial conditions
for cosmological simulations
Copyright (C) 2010 Oliver Hahn
*/
//... for testing purposes.............
//#define DEGRADE_RAND1
//#define DEGRADE_RAND2
//.....................................
#ifndef __RANDOM_HH
#define __RANDOM_HH
#define DEF_RAN_CUBE_SIZE 32
#include <fstream>
#include <algorithm>
#include <map>
#ifdef _OPENMP
#include <omp.h>
#endif
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "general.hh"
#include "mesh.hh"
#include "mg_operators.hh"
#include "constraints.hh"
#include "convolution_kernel.hh"
class RNG_plugin{
protected:
config_file *pcf_; //!< pointer to config_file from which to read parameters
const refinement_hierarchy *prefh_; //!< pointer to refinement hierarchy structure containing the grid sizes
public:
explicit RNG_plugin( config_file& cf )//, const refinement_hierarchy& refh )
: pcf_( &cf )//, prefh_( & refh )
{ }
virtual ~RNG_plugin() { }
virtual bool is_multiscale() const = 0;
virtual void fill_grid( int level, DensityGrid<real_t>& R ) = 0;
virtual void initialize_for_grid_structure( const refinement_hierarchy& refh ) = 0;
};
struct RNG_plugin_creator
{
virtual RNG_plugin * create( config_file& cf ) const = 0;
virtual ~RNG_plugin_creator() { }
};
std::map< std::string, RNG_plugin_creator *>&
get_RNG_plugin_map();
void print_RNG_plugins( void );
template< class Derived >
struct RNG_plugin_creator_concrete : public RNG_plugin_creator
{
//! register the plugin by its name
RNG_plugin_creator_concrete( const std::string& plugin_name )
{
get_RNG_plugin_map()[ plugin_name ] = this;
}
//! create an instance of the plugin
RNG_plugin* create( config_file& cf ) const //, const refinement_hierarchy& refh ) const
{
return new Derived( cf ); //, refh );
}
};
typedef RNG_plugin RNG_instance;
RNG_plugin *select_RNG_plugin( config_file& cf );//, const refinement_hierarchy& refh );
/*!
* @brief encapsulates all things for multi-scale white noise generation
*/
template< typename T >
class random_number_generator
{
protected:
config_file * pcf_;
//const refinement_hierarchy * prefh_;
RNG_plugin * generator_;
int levelmin_, levelmax_;
public:
//! constructor
random_number_generator( config_file& cf, transfer_function *ptf = NULL )
: pcf_( &cf )//, prefh_( &refh )
{
levelmin_ = pcf_->getValue<int>("setup","levelmin");
levelmax_ = pcf_->getValue<int>("setup","levelmax");
generator_ = select_RNG_plugin( cf );
}
//! destructor
~random_number_generator()
{
}
//! initialize_for_grid_structure
void initialize_for_grid_structure( const refinement_hierarchy& refh )
{
generator_->initialize_for_grid_structure( refh );
}
//! load random numbers to a new array
template< typename array >
void load( array& A, int ilevel )
{
generator_->fill_grid( ilevel, A );
}
};
//typedef random_numbers<real_t> rand_nums;
//typedef random_number_generator< rand_nums,real_t> rand_gen;
typedef random_number_generator<real_t> noise_generator;
#if 0
constraint_set constraints;
int levelmin_,
levelmax_,
levelmin_seed_;
std::vector<long> rngseeds_;
std::vector<std::string> rngfnames_;
bool disk_cached_;
bool restart_;
std::vector< std::vector<T>* > mem_cache_;
unsigned ran_cube_size_;
protected:
//! checks if the specified string is numeric
bool is_number(const std::string& s);
//! parses the random number parameters in the conf file
void parse_rand_parameters( void );
//! correct coarse grid averages for the change in small scale when using Fourier interpolation
void correct_avg( int icoarse, int ifine );
//! the main driver routine for multi-scale white noise generation
void compute_random_numbers( void );
//! store the white noise fields in memory or on disk
void store_rnd( int ilevel, rng* prng );
public:
//! constructor
random_number_generator( config_file& cf, refinement_hierarchy& refh, transfer_function *ptf = NULL );
//! destructor
~random_number_generator();
//! load random numbers to a new array
template< typename array >
void load( array& A, int ilevel )
{
if( restart_ )
LOGINFO("Attempting to restart using random numbers for level %d\n from file \'wnoise_%04d.bin\'.",ilevel,ilevel);
if( disk_cached_ )
{
char fname[128];
sprintf(fname,"wnoise_%04d.bin",ilevel);
LOGUSER("Loading white noise from file \'%s\'...",fname);
std::ifstream ifs( fname, std::ios::binary );
if( !ifs.good() )
{
LOGERR("White noise file \'%s\'was not found.",fname);
throw std::runtime_error("A white noise file was not found. This is an internal inconsistency and bad.");
}
int nx,ny,nz;
ifs.read( reinterpret_cast<char*> (&nx), sizeof(int) );
ifs.read( reinterpret_cast<char*> (&ny), sizeof(int) );
ifs.read( reinterpret_cast<char*> (&nz), sizeof(int) );
if( nx!=(int)A.size(0) || ny!=(int)A.size(1) || nz!=(int)A.size(2) )
{
if( nx==(int)A.size(0)*2 && ny==(int)A.size(1)*2 && nz==(int)A.size(2)*2 )
{
std::cerr << "CHECKPOINT" << std::endl;
int ox = nx/4, oy = ny/4, oz = nz/4;
std::vector<T> slice( ny*nz, 0.0 );
for( int i=0; i<nx; ++i )
{
ifs.read( reinterpret_cast<char*> ( &slice[0] ), ny*nz*sizeof(T) );
if( i<ox ) continue;
if( i>=3*ox ) break;
#pragma omp parallel for
for( int j=oy; j<3*oy; ++j )
for( int k=oz; k<3*oz; ++k )
A(i-ox,j-oy,k-oz) = slice[j*nz+k];
}
ifs.close();
}
else
{
LOGERR("White noise file is not aligned with array. File: [%d,%d,%d]. Mem: [%d,%d,%d].",
nx,ny,nz,A.size(0),A.size(1),A.size(2));
throw std::runtime_error("White noise file is not aligned with array. This is an internal inconsistency and bad.");
}
}else{
for( int i=0; i<nx; ++i )
{
std::vector<T> slice( ny*nz, 0.0 );
ifs.read( reinterpret_cast<char*> ( &slice[0] ), ny*nz*sizeof(T) );
#pragma omp parallel for
for( int j=0; j<ny; ++j )
for( int k=0; k<nz; ++k )
A(i,j,k) = slice[j*nz+k];
}
ifs.close();
}
}
else
{
LOGUSER("Copying white noise from memory cache...");
if( mem_cache_[ilevel-levelmin_] == NULL )
LOGERR("Tried to access mem-cached random numbers for level %d. But these are not available!\n",ilevel);
int nx( A.size(0) ), ny( A.size(1) ), nz( A.size(2) );
if ( (size_t)nx*(size_t)ny*(size_t)nz != mem_cache_[ilevel-levelmin_]->size() )
{
LOGERR("White noise file is not aligned with array. File: [%d,%d,%d]. Mem: [%d,%d,%d].",nx,ny,nz,A.size(0),A.size(1),A.size(2));
throw std::runtime_error("White noise file is not aligned with array. This is an internal inconsistency and bad");
}
#pragma omp parallel for
for( int i=0; i<nx; ++i )
for( int j=0; j<ny; ++j )
for( int k=0; k<nz; ++k )
A(i,j,k) = (*mem_cache_[ilevel-levelmin_])[((size_t)i*ny+(size_t)j)*nz+(size_t)k];
std::vector<T>().swap( *mem_cache_[ilevel-levelmin_] );
delete mem_cache_[ilevel-levelmin_];
mem_cache_[ilevel-levelmin_] = NULL;
}
}
};
/*!
* @brief encapsulates all things random number generator related
*/
template< typename T >
class random_numbers
{
public:
unsigned
res_, //!< resolution of the full mesh
cubesize_, //!< size of one independent random number cube
ncubes_; //!< number of random number cubes to cover the full mesh
long baseseed_; //!< base seed from which cube seeds are computed
protected:
//! vector of 3D meshes (the random number cubes) with random numbers
std::vector< Meshvar<T>* > rnums_;
//! map of 3D indices to cube index
std::map<size_t,size_t> cubemap_;
typedef std::map<size_t,size_t>::iterator cubemap_iterator;
protected:
//! register a cube with the hash map
void register_cube( int i, int j, int k);
//! fills a subcube with random numbers
double fill_cube( int i, int j, int k);
//! subtract a constant from an entire cube
void subtract_from_cube( int i, int j, int k, double val );
//! copy random numbers from a cube to a full grid array
template< class C >
void copy_cube( int i, int j, int k, C& dat )
{
int offi, offj, offk;
offi = i*cubesize_;
offj = j*cubesize_;
offk = k*cubesize_;
i = (i+ncubes_)%ncubes_;
j = (j+ncubes_)%ncubes_;
k = (k+ncubes_)%ncubes_;
size_t icube = (i*ncubes_+j)*ncubes_+k;
cubemap_iterator it = cubemap_.find( icube );
if( it == cubemap_.end() )
{
LOGERR("attempting to copy data from non-existing RND cube %d,%d,%d",i,j,k);
throw std::runtime_error("attempting to copy data from non-existing RND cube");
}
size_t cubeidx = it->second;
for( int ii=0; ii<(int)cubesize_; ++ii )
for( int jj=0; jj<(int)cubesize_; ++jj )
for( int kk=0; kk<(int)cubesize_; ++kk )
dat(offi+ii,offj+jj,offk+kk) = (*rnums_[cubeidx])(ii,jj,kk);
}
//! free the memory associated with a subcube
void free_cube( int i, int j, int k );
//! initialize member variables and allocate memory
void initialize( void );
//! fill a cubic subvolume of the full grid with random numbers
double fill_subvolume( int *i0, int *n );
//! fill an entire grid with random numbers
double fill_all( void );
//! fill an external array instead of the internal field
template< class C >
double fill_all( C& dat )
{
double sum = 0.0;
for( int i=0; i<(int)ncubes_; ++i )
for( int j=0; j<(int)ncubes_; ++j )
for( int k=0; k<(int)ncubes_; ++k )
{
int ii(i),jj(j),kk(k);
register_cube(ii,jj,kk);
}
#pragma omp parallel for reduction(+:sum)
for( int i=0; i<(int)ncubes_; ++i )
for( int j=0; j<(int)ncubes_; ++j )
for( int k=0; k<(int)ncubes_; ++k )
{
int ii(i),jj(j),kk(k);
ii = (ii+ncubes_)%ncubes_;
jj = (jj+ncubes_)%ncubes_;
kk = (kk+ncubes_)%ncubes_;
sum+=fill_cube(ii, jj, kk);
copy_cube(ii,jj,kk,dat);
free_cube(ii, jj, kk);
}
return sum/(ncubes_*ncubes_*ncubes_);
}
//! write the number of allocated random number cubes to stdout
void print_allocated( void );
public:
//! constructor
random_numbers( unsigned res, unsigned cubesize, long baseseed, int *x0, int *lx );
//! constructor for constrained fine field
random_numbers( random_numbers<T>& rc, unsigned cubesize, long baseseed,
bool kspace=false, bool isolated=false, int *x0_=NULL, int *lx_=NULL, bool zeromean=true );
//! constructor
random_numbers( unsigned res, unsigned cubesize, long baseseed, bool zeromean=true );
//! constructor to read white noise from file
random_numbers( unsigned res, std::string randfname, bool rndsign );
//! copy constructor for averaged field (not copying) hence explicit!
explicit random_numbers( /*const*/ random_numbers <T>& rc, bool kdegrade = true );
//! destructor
~random_numbers()
{
for( unsigned i=0; i<rnums_.size(); ++i )
if( rnums_[i] != NULL )
delete rnums_[i];
rnums_.clear();
}
//! access a random number, this allocates a cube and fills it with consistent random numbers
inline T& operator()( int i, int j, int k, bool fillrand=true )
{
int ic, jc, kc, is, js, ks;
if( ncubes_ == 0 )
throw std::runtime_error("random_numbers: internal error, not properly initialized");
//... determine cube
ic = (int)((double)i/cubesize_ + ncubes_) % ncubes_;
jc = (int)((double)j/cubesize_ + ncubes_) % ncubes_;
kc = (int)((double)k/cubesize_ + ncubes_) % ncubes_;
size_t icube = ((size_t)ic*ncubes_+(size_t)jc)*ncubes_+(size_t)kc;
cubemap_iterator it = cubemap_.find( icube );
if( it == cubemap_.end() )
{
LOGERR("Attempting to copy data from non-existing RND cube %d,%d,%d @ %d,%d,%d",ic,jc,kc,i,j,k);
throw std::runtime_error("attempting to copy data from non-existing RND cube");
}
size_t cubeidx = it->second;
if( rnums_[ cubeidx ] == NULL )
{
LOGERR("Attempting to access data from non-allocated RND cube %d,%d,%d",ic,jc,kc);
throw std::runtime_error("attempting to access data from non-allocated RND cube");
}
//... determine cell in cube
is = (i - ic * cubesize_ + cubesize_) % cubesize_;
js = (j - jc * cubesize_ + cubesize_) % cubesize_;
ks = (k - kc * cubesize_ + cubesize_) % cubesize_;
return (*rnums_[ cubeidx ])(is,js,ks);
}
//! free all cubes
void free_all_mem( void )
{
for( unsigned i=0; i<rnums_.size(); ++i )
if( rnums_[i] != NULL )
{
delete rnums_[i];
rnums_[i] = NULL;
}
}
};
#endif
#endif //__RANDOM_HH