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 | #include <iostream>
#include <fstream>
#include <cstdlib>
#include "mcnpmit.hpp"
#include "moab/CartVect.hpp"
#include <cmath>
moab::Interface* mb_instance();
// Parameters
// const double pi = 3.141592653589793;
const double c2pi = 0.1591549430918954;
// const double cpi = 0.3183098861837907;
MCNPError next_number( std::string, double&, int& );
int how_many_numbers( std::string );
MCNPError read_numbers( std::string, int, std::vector< double >& );
// Constructor
McnpData::McnpData()
{
// Default value for coordinate system
coord_system = 0;
// Default rotation matrix is identity matrix
for( int i = 0; i < 4; i++ )
{
for( int j = 0; j < 4; j++ )
{
if( i == j )
rotation_matrix[4 * i + j] = 1;
else
rotation_matrix[4 * i + j] = 0;
}
}
}
// Destructor
McnpData::~McnpData()
{
// Vertices and elements
MCNP_vertices.clear();
}
// Setting and retrieving coordinate sysem
MCNPError McnpData::set_coord_system( int k )<--- The function 'set_coord_system' is never used.
{
coord_system = k;
return MCNP_SUCCESS;
}
int McnpData::get_coord_system()<--- The function 'get_coord_system' is never used.
{
return coord_system;
}
// Setting and retrieving roation matrix
MCNPError McnpData::set_rotation_matrix( double r[16] )<--- The function 'set_rotation_matrix' is never used.
{
for( int i = 0; i < 16; i++ )
{
rotation_matrix[i] = r[i];
}
return MCNP_SUCCESS;
}
double* McnpData::get_rotation_matrix()<--- The function 'get_rotation_matrix' is never used.
{
return rotation_matrix;
}
// Set the filename
MCNPError McnpData::set_filename( std::string fname )<--- Function parameter 'fname' should be passed by const reference. [+]Parameter 'fname' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
MCNP_filename = fname;
return MCNP_SUCCESS;
}
std::string McnpData::get_filename()<--- The function 'get_filename' is never used.
{
return MCNP_filename;
}
// Reading the MCNP file
MCNPError McnpData::read_mcnpfile( bool skip_mesh )
{
MCNPError result;
moab::ErrorCode MBresult;
moab::CartVect tvect;
std::vector< double > xvec[3];
// Open the file
std::ifstream mcnpfile;
mcnpfile.open( MCNP_filename.c_str() );
if( !mcnpfile )
{
std::cout << "Unable to open MCNP data file." << std::endl;
return MCNP_FAILURE;
}
std::cout << std::endl;
std::cout << "Reading MCNP input file..." << std::endl;
// Prepare for file reading ...
char line[10000];
int mode = 0; // Set the file reading mode to read proper data
int nv[3];
// Read in the file ...
while( !mcnpfile.eof() )
{
mcnpfile.getline( line, 10000 );
// std::cout << line << std::endl;
switch( mode )
{
case 0: // First line is a title
mode++;
break;
case 1: // Coordinate system
mode++;
result = read_coord_system( line );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
break;
case 2: // Rotation matrix
mode++;
for( int i = 0; i < 4; i++ )
{
mcnpfile.getline( line, 10000 );
result = read_rotation_matrix( line, i );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
}
if( skip_mesh ) return MCNP_SUCCESS;
break;
case 3: // Read in vertices and build elements
mode++;
for( int i = 0; i < 3; i++ )
{
// How many points in the x[i]-direction
nv[i] = how_many_numbers( line );
if( nv[i] <= 0 ) return MCNP_FAILURE;
// Get space and read in these points
result = read_numbers( line, nv[i], xvec[i] );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
// Update to the next line
mcnpfile.getline( line, 10000 );
}
// Make the elements and vertices
result = make_elements( xvec, nv );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
break;
case 4: // Read in tally data, make, and tag elements
mode++;
moab::EntityHandle elemhandle;
moab::EntityHandle vstart, vijk;
moab::EntityHandle connect[8];
// double d[3];
// vstart = MCNP_vertices.front();
vstart = *( vert_handles.begin() );
for( int i = 0; i < nv[0] - 1; i++ )
{
for( int j = 0; j < nv[1] - 1; j++ )
{
for( int k = 0; k < nv[2] - 1; k++ )
{
vijk = vstart + ( i + j * nv[0] + k * nv[0] * nv[1] );
// std::cout << vijk << std::endl;
connect[0] = vijk;
connect[1] = vijk + 1;
connect[2] = vijk + 1 + nv[0];
connect[3] = vijk + nv[0];
connect[4] = vijk + nv[0] * nv[1];
connect[5] = vijk + 1 + nv[0] * nv[1];
connect[6] = vijk + 1 + nv[0] + nv[0] * nv[1];
connect[7] = vijk + nv[0] + nv[0] * nv[1];
MBresult = MBI->create_element( moab::MBHEX, connect, 8, elemhandle );
if( MBresult != moab::MB_SUCCESS ) return MCNP_FAILURE;
elem_handles.insert( elemhandle );
mcnpfile.getline( line, 10000 );
result = extract_tally_data( line, elemhandle );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
}
}
}
/*
for (MBRange::iterator rit=vert_handles.begin(); rit !=
vert_handles.end(); ++rit) { std::cout << *rit << std::endl;
}
for (int i=0; i < nv[0]-1; i++) {
for (int j=0; j < nv[1]-1; j++) {
for (int k=0; k < nv[2]-1; k++) {
vijk = vstart + (i + j*nv[0] + k*nv[0]*nv[1]);
connect[0] = vijk;
connect[1] = vijk + 1;
connect[2] = vijk + 1 + nv[0];
connect[3] = vijk + nv[0];
connect[4] = vijk + nv[0]*nv[1];
connect[5] = vijk + 1 + nv[0]*nv[1];
connect[6] = vijk + 1 + nv[0] + nv[0]*nv[1];
connect[7] = vijk + nv[0] + nv[0]*nv[1];
MBresult = MBI->create_element(MBHEX, connect, 8,
elemhandle); if (MBresult != MB_SUCCESS) return MCNP_FAILURE;
elem_handles.insert(elemhandle);
mcnpfile.getline(line, 10000);
result = extract_tally_data(line, elemhandle);
if (result == MCNP_FAILURE) return MCNP_FAILURE;
}
}
}
*/
break;
case 5: // Ckeck for weirdness at end of file
if( !mcnpfile.eof() ) return MCNP_FAILURE;
break;
}
}
std::cout << "SUCCESS! Read in " << elem_handles.size() << " elements!" << std::endl << std::endl;
// MCNP_vertices.clear();
vert_handles.clear();
MCNP_elems.clear();
return MCNP_SUCCESS;
}
MCNPError McnpData::read_coord_system( std::string s )
{
if( ( s.find( "Box" ) < 100 ) || ( s.find( "xyz" ) < 100 ) )<--- Inefficient usage of string::find() in condition; string::starts_with() could be faster. [+]Either inefficient or wrong usage of string::find(). string::starts_with() will be faster if string::find's result is compared with 0, because it will not scan the whole string. If your intention is to check that there are no findings in the string, you should compare with std::string::npos.
coord_system = CARTESIAN;
else if( s.find( "Cyl" ) < 100 )<--- Inefficient usage of string::find() in condition; string::starts_with() could be faster. [+]Either inefficient or wrong usage of string::find(). string::starts_with() will be faster if string::find's result is compared with 0, because it will not scan the whole string. If your intention is to check that there are no findings in the string, you should compare with std::string::npos.
coord_system = CYLINDRICAL;
else if( s.find( "Sph" ) < 100 )<--- Inefficient usage of string::find() in condition; string::starts_with() could be faster. [+]Either inefficient or wrong usage of string::find(). string::starts_with() will be faster if string::find's result is compared with 0, because it will not scan the whole string. If your intention is to check that there are no findings in the string, you should compare with std::string::npos.
coord_system = SPHERICAL;
else
return MCNP_FAILURE;
return MCNP_SUCCESS;
}
MCNPError McnpData::read_rotation_matrix( std::string s, int i )<--- Function parameter 's' should be passed by const reference. [+]Parameter 's' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
int fpos = 0;
MCNPError result;
for( int j = 0; j < 4; j++ )
{
result = next_number( s, rotation_matrix[4 * i + j], fpos );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
}
return MCNP_SUCCESS;
}
MCNPError McnpData::make_elements( std::vector< double > x[3], int* n )
{
// double v[3];
// MBEntityHandle dumhandle;
// MBEntityHandle vstart, vijk;
unsigned int num_verts = n[0] * n[1] * n[2];
double* coords;
coords = new double[3 * num_verts];
/*
// Enter the vertices ...
for (int k=0; k < n[2]; k++) {
v[2] = x[2].at(k);
for (int j=0; j < n[1]; j++) {
v[1] = x[1].at(j);
for (int i=0; i < n[0]; i++) {
v[0] = x[0].at(i);
MBresult = MBI->create_vertex(v, dumhandle);
if (MBresult != MB_SUCCESS) return MCNP_FAILURE;
MCNP_vertices.push_back(dumhandle);
}
}
}
*/
// Enter the vertices ...
for( int k = 0; k < n[2]; k++ )
{
for( int j = 0; j < n[1]; j++ )
{
for( int i = 0; i < n[0]; i++ )
{
unsigned int ijk = 3 * ( k * n[0] * n[1] + j * n[0] + i );
coords[ijk] = x[0][i];
coords[ijk + 1] = x[1][j];
coords[ijk + 2] = x[2][k];
// std::cout << coords[ijk] << " " << coords[ijk+1] << " "
// << coords[ijk+2] << std::endl;
// MCNP_vertices.push_back(dumhandle);
}
}
}
MBI->create_vertices( coords, num_verts, vert_handles );
delete[] coords;
return MCNP_SUCCESS;
}
MCNPError McnpData::initialize_tags()
{
MBI->tag_get_handle( TALLY_TAG, 1, moab::MB_TYPE_DOUBLE, tally_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT );
MBI->tag_get_handle( ERROR_TAG, 1, moab::MB_TYPE_DOUBLE, relerr_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT );
return MCNP_SUCCESS;
}
MCNPError McnpData::extract_tally_data( std::string s, moab::EntityHandle handle )<--- Function parameter 's' should be passed by const reference. [+]Parameter 's' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
int fpos = 0;
double d = 0;
MCNPError result;
moab::ErrorCode MBresult;
// Discard first three lines
for( int i = 0; i < 3; i++ )
{
result = next_number( s, d, fpos );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
}
// Need to read in tally entry and tag ...
result = next_number( s, d, fpos );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
MBresult = MBI->tag_set_data( tally_tag, &handle, 1, &d );
if( MBresult != moab::MB_SUCCESS ) return MCNP_FAILURE;
// Need to read in relative error entry and tag ...
result = next_number( s, d, fpos );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
MBresult = MBI->tag_set_data( relerr_tag, &handle, 1, &d );
if( MBresult != moab::MB_SUCCESS ) return MCNP_FAILURE;
return MCNP_SUCCESS;
}
MCNPError next_number( std::string s, double& d, int& p )
{
unsigned int slen = s.length();
unsigned int j;
for( unsigned int i = p; i < slen; i++ )
{
if( ( ( s[i] >= 48 ) && ( s[i] <= 57 ) ) || ( s[i] == 45 ) )
{
j = s.find( " ", i );
if( j > slen ) j = slen;
// Extract the number out of the string
d = std::atof( s.substr( i, j - i ).c_str() );
p = j + 1;
return MCNP_SUCCESS;
}
}
return DONE;
}
int how_many_numbers( std::string s )<--- Function parameter 's' should be passed by const reference. [+]Parameter 's' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
int n = -1;
int fpos = 0;
double d = 0;
MCNPError result = MCNP_SUCCESS;
while( result != DONE )
{
result = next_number( s, d, fpos );
n++;
}
return n;
}
MCNPError read_numbers( std::string s, int n, std::vector< double >& x )<--- Function parameter 's' should be passed by const reference. [+]Parameter 's' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
MCNPError result;
int fpos = 0;
double d;
for( int i = 0; i < n; i++ )
{
result = next_number( s, d, fpos );
if( result == MCNP_FAILURE ) return MCNP_FAILURE;
x.push_back( d );
}
return MCNP_SUCCESS;
}
MCNPError McnpData::transform_point( double* p, double* r, int csys, double* rmat )
{
double q[3];
// Apply the rotation matrix
for( unsigned int i = 0; i < 3; i++ )
{
q[i] = p[0] * rmat[4 * i] + p[1] * rmat[4 * i + 1] + p[2] * rmat[4 * i + 2] + rmat[4 * i + 3];
}
// Transform coordinate system
switch( csys )
{
case CARTESIAN:
r[0] = q[0];
r[1] = q[1];
r[2] = q[2]; // x, y, z
break;
case CYLINDRICAL:
r[0] = sqrt( q[0] * q[0] + q[1] * q[1] ); // r
r[1] = q[2]; // z
r[2] = c2pi * ( atan2( q[1], q[0] ) ); // theta (in rotations)
break;
case SPHERICAL:
return MCNP_FAILURE;
// break;
default:
return MCNP_FAILURE;
// break;
}
return MCNP_SUCCESS;
}
|