GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pyne/src/material.cpp at develop · walshjon/pyne

Expand file tree

/

material.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
1649 lines (1316 loc) · 48.7 KB
/

material.cpp

Copy path

File metadata and controls

1649 lines (1316 loc) · 48.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Material.cpp
// The very central Material class
// -- Anthony Scopatz
#include <string>
#include <vector>
#include <iomanip> // std::setprecision
#include <math.h> // modf
#include <stdexcept>
#ifndef PYNE_IS_AMALGAMATED
#include "material.h"
#endif
// h5wrap template
template double h5wrap::get_array_index(hid_t, int, hid_t);
/***************************/
/*** Protected Functions ***/
/***************************/
double pyne::Material::get_comp_sum() {
// Sums the weights in the composition dictionary
double sum = 0.0;
for (pyne::comp_iter i = comp.begin(); i != comp.end(); i++) {
sum = sum + i->second;
}
return sum;
};
void pyne::Material::norm_comp() {
double sum = get_comp_sum();
if (sum != 1.0 && sum != 0.0) {
for (comp_iter i = comp.begin(); i != comp.end(); i++)
i->second = i->second / sum;
}
if (mass < 0.0)
mass = sum;
}
void pyne::Material::_load_comp_protocol0(hid_t db, std::string datapath, int row) {
hid_t matgroup = H5Gopen2(db, datapath.c_str(), H5P_DEFAULT);
hid_t nucset;
double nucvalue;
ssize_t nuckeylen;
std::string nuckey;
// get the number of members in the material group
H5G_info_t group_info;
H5Gget_info(matgroup, &group_info);
hsize_t matG = group_info.nlinks;
// Iterate over datasets in the group.
for (int matg = 0; matg < matG; matg++) {
nuckeylen = 1 + H5Lget_name_by_idx(matgroup, ".", H5_INDEX_NAME, H5_ITER_INC, matg,
NULL, 0, H5P_DEFAULT);
char * nkey = new char[nuckeylen];
nuckeylen = H5Lget_name_by_idx(matgroup, ".", H5_INDEX_NAME, H5_ITER_INC, matg,
nkey, nuckeylen, H5P_DEFAULT);
nuckey = nkey;
nucset = H5Dopen2(matgroup, nkey, H5P_DEFAULT);
nucvalue = h5wrap::get_array_index<double>(nucset, row);
if (nuckey == "Mass" || nuckey == "MASS" || nuckey == "mass")
mass = nucvalue;
else
comp[pyne::nucname::id(nuckey)] = nucvalue;
H5Dclose(nucset);
delete[] nkey;
};
// Set meta data
atoms_per_molecule = -1.0;
};
void pyne::Material::_load_comp_protocol1(hid_t db, std::string datapath, int row) {
std::string nucpath;
hid_t data_set = H5Dopen2(db, datapath.c_str(), H5P_DEFAULT);
hsize_t data_offset[1] = {row};
if (row < 0) {
// Handle negative row indices
hid_t data_space = H5Dget_space(data_set);
hsize_t data_dims[1];
H5Sget_simple_extent_dims(data_space, data_dims, NULL);
data_offset[0] += data_dims[0];
};
// Grab the nucpath
hid_t nuc_attr = H5Aopen(data_set, "nucpath", H5P_DEFAULT);
H5A_info_t nuc_info;
H5Aget_info(nuc_attr, &nuc_info);
hsize_t nuc_attr_len = nuc_info.data_size;
hid_t str_attr = H5Tcopy(H5T_C_S1);
H5Tset_size(str_attr, nuc_attr_len);
char * nucpathbuf = new char [nuc_attr_len];
H5Aread(nuc_attr, str_attr, nucpathbuf);
nucpath = std::string(nucpathbuf, nuc_attr_len);
delete[] nucpathbuf;
// Grab the nuclides
std::vector<int> nuclides = h5wrap::h5_array_to_cpp_vector_1d<int>(db, nucpath, H5T_NATIVE_INT);
int nuc_size = nuclides.size();
hsize_t nuc_dims[1] = {nuc_size};
// Get the data hyperslab
hid_t data_hyperslab = H5Dget_space(data_set);
hsize_t data_count[1] = {1};
H5Sselect_hyperslab(data_hyperslab, H5S_SELECT_SET, data_offset, NULL, data_count, NULL);
// Get memory space for writing
hid_t mem_space = H5Screate_simple(1, data_count, NULL);
// Get material type
size_t material_data_size = sizeof(pyne::material_data) + sizeof(double)*nuc_size;
hid_t desc = H5Tcreate(H5T_COMPOUND, material_data_size);
hid_t comp_values_array_type = H5Tarray_create2(H5T_NATIVE_DOUBLE, 1, nuc_dims);
// make the data table type
H5Tinsert(desc, "mass", HOFFSET(pyne::material_data, mass), H5T_NATIVE_DOUBLE);
H5Tinsert(desc, "density", HOFFSET(pyne::material_data, density),
H5T_NATIVE_DOUBLE);
H5Tinsert(desc, "atoms_per_molecule", HOFFSET(pyne::material_data, atoms_per_mol),
H5T_NATIVE_DOUBLE);
H5Tinsert(desc, "comp", HOFFSET(pyne::material_data, comp), comp_values_array_type);
// make the data array, have to over-allocate
material_data * mat_data = new material_data [material_data_size];
// Finally, get data and put in on this instance
H5Dread(data_set, desc, mem_space, data_hyperslab, H5P_DEFAULT, mat_data);
mass = (*mat_data).mass;
density = (*mat_data).density;
atoms_per_molecule = (*mat_data).atoms_per_mol;
for (int i = 0; i < nuc_size; i++)
comp[nuclides[i]] = (double) (*mat_data).comp[i];
delete[] mat_data;
H5Tclose(str_attr);
//
// Get metadata from associated dataset, if available
//
std::string attrpath = datapath + "_metadata";
bool attrpath_exists = h5wrap::path_exists(db, attrpath);
if (!attrpath_exists)
return;
hid_t metadatapace, attrtype, metadataet, metadatalab, attrmemspace;
int attrrank;
hvl_t attrdata [1];
attrtype = H5Tvlen_create(H5T_NATIVE_CHAR);
// Get the metadata from the file
metadataet = H5Dopen2(db, attrpath.c_str(), H5P_DEFAULT);
metadatalab = H5Dget_space(metadataet);
H5Sselect_hyperslab(metadatalab, H5S_SELECT_SET, data_offset, NULL, data_count, NULL);
attrmemspace = H5Screate_simple(1, data_count, NULL);
H5Dread(metadataet, attrtype, attrmemspace, metadatalab, H5P_DEFAULT, attrdata);
// convert to in-memory JSON
Json::Reader reader;
reader.parse((char *) attrdata[0].p, (char *) attrdata[0].p+attrdata[0].len, metadata, false);
// close attr data objects
H5Fflush(db, H5F_SCOPE_GLOBAL);
H5Dclose(metadataet);
H5Sclose(metadatapace);
H5Tclose(attrtype);
// Close out the HDF5 file
H5Fclose(db);
};
void pyne::Material::from_hdf5(char * filename, char * datapath, int row, int protocol) {
std::string fname (filename);
std::string dpath (datapath);
from_hdf5(fname, dpath, row, protocol);
};
void pyne::Material::from_hdf5(std::string filename, std::string datapath, int row, int protocol) {
// Turn off annoying HDF5 errors
herr_t status;
H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
// Check that the file is there
if (!pyne::file_exists(filename))
throw pyne::FileNotFound(filename);
// Check to see if the file is in HDF5 format.
bool ish5 = H5Fis_hdf5(filename.c_str());
if (!ish5)
throw h5wrap::FileNotHDF5(filename);
//Set file access properties so it closes cleanly
hid_t fapl;
fapl = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fclose_degree(fapl,H5F_CLOSE_STRONG);
// Open the database
hid_t db = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, fapl);
bool datapath_exists = h5wrap::path_exists(db, datapath);
if (!datapath_exists)
throw h5wrap::PathNotFound(filename, datapath);
// Clear current content
comp.clear();
// Load via various protocols
if (protocol == 0)
_load_comp_protocol0(db, datapath, row);
else if (protocol == 1)
_load_comp_protocol1(db, datapath, row);
else
throw pyne::MaterialProtocolError();
// Close the database
status = H5Fclose(db);
// Renormalize the composition, just to be safe.
norm_comp();
};
void pyne::Material::write_hdf5(char * filename, char * datapath, char * nucpath, float row, int chunksize) {
std::string fname (filename);
std::string groupname (datapath);
std::string nuclist (nucpath);
write_hdf5(fname, groupname, nuclist, row, chunksize);
};
void pyne::Material::write_hdf5(std::string filename, std::string datapath,
std::string nucpath, float row, int chunksize) {
int row_num = (int) row;
// Turn off annoying HDF5 errors
H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
//Set file access properties so it closes cleanly
hid_t fapl;
fapl = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fclose_degree(fapl,H5F_CLOSE_STRONG);
// Create new/open datafile.
hid_t db;
if (pyne::file_exists(filename)) {
bool ish5 = H5Fis_hdf5(filename.c_str());
if (!ish5)
throw h5wrap::FileNotHDF5(filename);
db = H5Fopen(filename.c_str(), H5F_ACC_RDWR, fapl);
}
else
db = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, fapl);
//
// Read in nuclist if available, write it out if not
//
bool nucpath_exists = h5wrap::path_exists(db, nucpath);
std::vector<int> nuclides;
int nuc_size;
hsize_t nuc_dims[1];
if (nucpath_exists) {
nuclides = h5wrap::h5_array_to_cpp_vector_1d<int>(db, nucpath, H5T_NATIVE_INT);
nuc_size = nuclides.size();
nuc_dims[0] = nuc_size;
} else {
nuclides = std::vector<int>();
for (pyne::comp_iter i = comp.begin(); i != comp.end(); i++)
nuclides.push_back(i->first);
nuc_size = nuclides.size();
// Create the data if it doesn't exist
int nuc_data [nuc_size];
for (int n = 0; n != nuc_size; n++)
nuc_data[n] = nuclides[n];
nuc_dims[0] = nuc_size;
hid_t nuc_space = H5Screate_simple(1, nuc_dims, NULL);
hid_t nuc_set = H5Dcreate2(db, nucpath.c_str(), H5T_NATIVE_INT, nuc_space,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(nuc_set, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, nuc_data);
H5Fflush(db, H5F_SCOPE_GLOBAL);
};
//
// Write out the data itself to the file
//
hid_t data_set, data_space, data_hyperslab;
int data_rank = 1;
hsize_t data_dims[1] = {1};
hsize_t data_max_dims[1] = {H5S_UNLIMITED};
hsize_t data_offset[1] = {0};
size_t material_data_size = sizeof(pyne::material_data) + sizeof(double)*nuc_size;
hid_t desc = H5Tcreate(H5T_COMPOUND, material_data_size);
hid_t comp_values_array_type = H5Tarray_create2(H5T_NATIVE_DOUBLE, 1, nuc_dims);
// make the data table type
H5Tinsert(desc, "mass", HOFFSET(pyne::material_data, mass), H5T_NATIVE_DOUBLE);
H5Tinsert(desc, "density", HOFFSET(pyne::material_data, density),
H5T_NATIVE_DOUBLE);
H5Tinsert(desc, "atoms_per_molecule", HOFFSET(pyne::material_data, atoms_per_mol),
H5T_NATIVE_DOUBLE);
H5Tinsert(desc, "comp", HOFFSET(pyne::material_data, comp),
comp_values_array_type);
material_data * mat_data = new material_data[material_data_size];
(*mat_data).mass = mass;
(*mat_data).density = density;
(*mat_data).atoms_per_mol = atoms_per_molecule;
for (int n = 0; n != nuc_size; n++) {
if (0 < comp.count(nuclides[n]))
(*mat_data).comp[n] = comp[nuclides[n]];
else
(*mat_data).comp[n] = 0.0;
};
// get / make the data set
bool datapath_exists = h5wrap::path_exists(db, datapath);
if (datapath_exists) {
data_set = H5Dopen2(db, datapath.c_str(), H5P_DEFAULT);
data_space = H5Dget_space(data_set);
data_rank = H5Sget_simple_extent_dims(data_space, data_dims, data_max_dims);
// Determine the row size.
if (std::signbit(row))
row_num = data_dims[0] + row; // careful, row is negative
if (data_dims[0] <= row_num) {
// row == -0, extend to data set so that we can append, or
// row_num is larger than current dimension, resize to accomodate.
data_dims[0] = row_num + 1;
H5Dset_extent(data_set, data_dims);
}
data_offset[0] = row_num;
} else {
// Get full space
data_space = H5Screate_simple(1, data_dims, data_max_dims);
// Make data set properties to enable chunking
hid_t data_set_params = H5Pcreate(H5P_DATASET_CREATE);
hsize_t chunk_dims[1] ={chunksize};
H5Pset_chunk(data_set_params, 1, chunk_dims);
H5Pset_deflate(data_set_params, 1);
// Create the data set
data_set = H5Dcreate2(db, datapath.c_str(), desc, data_space, H5P_DEFAULT,
data_set_params, H5P_DEFAULT);
H5Dset_extent(data_set, data_dims);
// Add attribute pointing to nuc path
hid_t nuc_attr_type = H5Tcopy(H5T_C_S1);
H5Tset_size(nuc_attr_type, nucpath.length());
hid_t nuc_attr_space = H5Screate(H5S_SCALAR);
hid_t nuc_attr = H5Acreate2(data_set, "nucpath", nuc_attr_type, nuc_attr_space,
H5P_DEFAULT, H5P_DEFAULT);
H5Awrite(nuc_attr, nuc_attr_type, nucpath.c_str());
H5Fflush(db, H5F_SCOPE_GLOBAL);
};
// Get the data hyperslab
data_hyperslab = H5Dget_space(data_set);
hsize_t data_count[1] = {1};
H5Sselect_hyperslab(data_hyperslab, H5S_SELECT_SET, data_offset, NULL, data_count, NULL);
// Get a memory space for writing
hid_t mem_space = H5Screate_simple(1, data_count, data_max_dims);
// Write the row...
H5Dwrite(data_set, desc, mem_space, data_hyperslab, H5P_DEFAULT, mat_data);
// Close out the Dataset
H5Fflush(db, H5F_SCOPE_GLOBAL);
H5Dclose(data_set);
H5Sclose(data_space);
H5Tclose(desc);
//
// Write out the metadata to the file
//
std::string attrpath = datapath + "_metadata";
hid_t metadatapace, attrtype, metadataet, metadatalab, attrmemspace;
int attrrank;
attrtype = H5Tvlen_create(H5T_NATIVE_CHAR);
// get / make the data set
bool attrpath_exists = h5wrap::path_exists(db, attrpath);
if (attrpath_exists) {
metadataet = H5Dopen2(db, attrpath.c_str(), H5P_DEFAULT);
metadatapace = H5Dget_space(metadataet);
attrrank = H5Sget_simple_extent_dims(metadatapace, data_dims, data_max_dims);
if (data_dims[0] <= row_num) {
// row == -0, extend to data set so that we can append, or
// row_num is larger than current dimension, resize to accomodate.
data_dims[0] = row_num + 1;
H5Dset_extent(metadataet, data_dims);
}
data_offset[0] = row_num;
} else {
hid_t metadataetparams;
hsize_t attrchunkdims [1];
// Make data set properties to enable chunking
metadataetparams = H5Pcreate(H5P_DATASET_CREATE);
attrchunkdims[0] = chunksize;
H5Pset_chunk(metadataetparams, 1, attrchunkdims);
H5Pset_deflate(metadataetparams, 1);
hvl_t attrfillvalue [1];
attrfillvalue[0].len = 3;
attrfillvalue[0].p = (char *) "{}\n";
H5Pset_fill_value(metadataetparams, attrtype, &attrfillvalue);
// make dataset
metadatapace = H5Screate_simple(1, data_dims, data_max_dims);
metadataet = H5Dcreate2(db, attrpath.c_str(), attrtype, metadatapace,
H5P_DEFAULT, metadataetparams, H5P_DEFAULT);
H5Dset_extent(metadataet, data_dims);
H5Pclose(metadataetparams);
};
// set the attr string
hvl_t attrdata [1];
Json::FastWriter writer;
std::string metadatatr = writer.write(metadata);
attrdata[0].p = (char *) metadatatr.c_str();
attrdata[0].len = metadatatr.length();
// write the attr
metadatalab = H5Dget_space(metadataet);
H5Sselect_hyperslab(metadatalab, H5S_SELECT_SET, data_offset, NULL, data_count, NULL);
attrmemspace = H5Screate_simple(1, data_count, data_max_dims);
H5Dwrite(metadataet, attrtype, attrmemspace, metadatalab, H5P_DEFAULT, attrdata);
// close attr data objects
H5Fflush(db, H5F_SCOPE_GLOBAL);
H5Dclose(metadataet);
H5Sclose(metadatapace);
H5Tclose(attrtype);
// Close out the HDF5 file
H5Fclose(db);
// Remember the milk!
// ...by which I mean to deallocate
delete[] mat_data;
};
std::string pyne::Material::mcnp(std::string frac_type) {
//////////////////// Begin card creation ///////////////////////
std::ostringstream oss;
// 'name'
if (metadata.isMember("name")) {
oss << "C name: " << metadata["name"].asString() << std::endl;
}
// 'density'
if (density != -1.0) {
std::stringstream ds;
ds << std::setprecision(1) << std::fixed << "C density = " << density << std::endl;
oss << ds.str();
}
// 'source'
if (metadata.isMember("source")) {
oss << "C source: " << metadata["source"].asString() << std::endl;
}
// Metadata comments
if (metadata.isMember("comments")) {
std::string comment_string = "comments: " + metadata["comments"].asString();
// Include as is if short enough
if (comment_string.length() <= 77) {
oss << "C " << comment_string << std::endl;
}
else { // otherwise create a remainder string and iterate/update it
oss << "C " << comment_string.substr(0,77) << std::endl;
std::string remainder_string = comment_string.substr(77);
while (remainder_string.length() > 77) {
oss << "C " << remainder_string.substr(0,77) << std::endl;
remainder_string.erase(0,77);
}
if (remainder_string.length() > 0) {
oss << "C " << remainder_string << std::endl;
}
}
}
// Metadata mat_num
oss << "m";
if (metadata.isMember("mat_number")) {
int mat_num = metadata["mat_number"].asInt();
oss << mat_num << std::endl;
} else {
oss << "?" << std::endl;
}
// Set up atom or mass frac map
std::map<int, double> fracs;
std::string frac_sign;
if ("atom" == frac_type) {
fracs = to_atom_frac();
frac_sign = "";
} else {
fracs = comp;
frac_sign = "-";
}
// iterate through frac map
// This is an awkward pre-C++11 way to put an int to a string
std::stringstream ss;
std::string nucmcnp;
std::string table_item;
for(pyne::comp_iter i = fracs.begin(); i != fracs.end(); ++i) {
if (i->second > 0.0) {
// Clear first
ss.str(std::string());
ss.str("");
ss << pyne::nucname::mcnp(i->first);
nucmcnp = ss.str();
int mcnp_id;
mcnp_id = pyne::nucname::mcnp(i->first);
// Spaces are important for tests
table_item = metadata["table_ids"][nucmcnp].asString();
if (!table_item.empty()) {
oss << " " << mcnp_id << "." << table_item << " ";
} else {
oss << " " << mcnp_id << " ";
}
// The int needs a little formatting
std::stringstream fs;
fs << std::setprecision(4) << std::scientific << frac_sign << i->second \
<< std::endl;
oss << fs.str();
}
}
return oss.str();
}
///---------------------------------------------------------------------------//
/// Create a set out of the static string array.
std::set<std::string> fluka_builtin(pyne::fluka_mat_strings,
pyne::fluka_mat_strings+pyne::FLUKA_MAT_NUM);
///---------------------------------------------------------------------------//
/// not_fluka_builtin
///---------------------------------------------------------------------------//
/// Convenience function
/// This is written as a negative because that is what we care about
bool pyne::Material::not_fluka_builtin(std::string fluka_name) {
return (fluka_builtin.find(fluka_name) == fluka_builtin.end());
}
///---------------------------------------------------------------------------//
/// fluka
///---------------------------------------------------------------------------//
/// Main external call
std::string pyne::Material::fluka(int id, std::string frac_type) {
std::stringstream rs;
// Element, one nucid
if (comp.size() == 1) {
rs << fluka_material_str(id);
} else if (comp.size() > 1) {
// Compound
rs << fluka_compound_str(id, frac_type);
} else {
rs << "There is no nuclide information in the Material Object" << std::endl;
}
return rs.str();
}
///---------------------------------------------------------------------------//
/// fluka_material_str
///---------------------------------------------------------------------------//
///
/// Requirement: the material upon which this function is called has
/// exactly one nucid component, i.e. it is elemental
/// Do not assume fluka_name is defined in the metadata. This function
/// may be called from a user-defined material, i.e. on that is not
/// read out of a UW^2-tagged geometry file, and thus does not have
/// certain metadata.
std::string pyne::Material::fluka_material_str(int id) {
std::stringstream ms;
std::string fluka_name; // needed to determine if built-in
int nucid = comp.begin()->first;
// NOTE: first part of 'if' may never be called
if (metadata.isMember("fluka_name")) {
fluka_name = metadata["fluka_name"].asString();
} else { // Should be elemental
if (comp.size() > 1 ) {
std::cerr << "Error: this mix is a compound, there should be a fluka_name defined."
<< std::endl;
return ms.str();
}
fluka_name = nucname::fluka(nucid);
}
if (not_fluka_builtin(fluka_name)) {
ms << fluka_material_component(id, nucid, fluka_name);
}
// could be empty
return ms.str();
}
///---------------------------------------------------------------------------//
/// fluka_material_component
///---------------------------------------------------------------------------//
/// Material has only one component,
/// Density is either object density or it is ignored ==> use object density
/// This function is not called for a compound, but it is called on the
/// material-ized components of compounds
std::string pyne::Material::fluka_material_component(int fid, int nucid,
std::string fluka_name) {
int znum = pyne::nucname::znum(nucid);
double atomic_mass;
if (0 != pyne::NUC_DATA_PATH.length()) {
// for compounds (i.e., unrecognized nucids), this will be 0
atomic_mass = pyne::atomic_mass(nucid);
} else {
atomic_mass = 1.0;
}
return fluka_material_line(znum, atomic_mass, fid, fluka_name);
}
///---------------------------------------------------------------------------//
/// fluka_material_line
///---------------------------------------------------------------------------//
/// Given all the info, return the Material string
std::string pyne::Material::fluka_material_line(int znum, double atomic_mass,
int fid, std::string fluka_name) {
std::stringstream ls;
if (metadata.isMember("comments") ) {
std::string comment = metadata["comments"].asString();
ls << "* " << comment;
ls << std::endl;
}
ls << std::setw(10) << std::left << "MATERIAL";
ls << std::setprecision(0) << std::fixed << std::showpoint <<
std::setw(10) << std::right << (float)znum;
ls << fluka_format_field(atomic_mass);
// Note this is the current object density, and may or may not be meaningful
ls << fluka_format_field(std::sqrt(density*density));
ls << std::setprecision(0) << std::fixed << std::showpoint <<
std::setw(10) << std::right << (float)fid;
ls << std::setw(10) << std::right << "";
ls << std::setw(10) << std::right << "";
ls << std::setw(10) << std::left << fluka_name << std::endl;
return ls.str();
}
///---------------------------------------------------------------------------//
/// fluka_format_field
///---------------------------------------------------------------------------//
/// Convenience function that returns a 10-character formatted string
/// 999 -> 999.
/// 999.12 -> 999.12
/// 999.123 -> 999.123
/// 999.1234 -> 999.123
std::string pyne::Material::fluka_format_field(float field) {
std::stringstream ls;
double intpart;
modf (field, &intpart);
if (field == intpart) {
ls << std::setprecision(0) << std::fixed << std::showpoint
<< std::setw(10) << std::right << field;
} else {
// This will print however many digits after the decimal, up to a max of six
ls.unsetf(std::ios::showpoint);
ls.unsetf(std::ios::floatfield);
ls.precision(6);
ls << std::setw(10) << std::right << field;
}
return ls.str();
}
///---------------------------------------------------------------------------//
/// fluka_compound_str
///---------------------------------------------------------------------------//
/// Returns
/// -- MATERIAL line for compound
/// -- COMPOUND lines
std::string pyne::Material::fluka_compound_str(int id, std::string frac_type) {
std::stringstream ss;
std::map<double, std::string> frac_name_map;
std::string compound_string = "";
std::vector<std::string> material_names;
// The nucid doesn't make sense for a compound
int znum = 1;
double atomic_mass = 1.;
// This better be true
std::string compound_name;
if (metadata.isMember("fluka_name")) {
compound_name = metadata["fluka_name"].asString();
} else {
std::cerr << "Error: metadata \"fluka_name\" expected." << std::endl;
compound_name = "NotFound";
}
ss << fluka_material_line(znum, atomic_mass, id, compound_name);
std::string frac_sign;
if ("atom" == frac_type) {
frac_sign = "";
} else {
frac_sign = "-";
}
std::stringstream temp_s;
temp_s << std::scientific;
temp_s << std::setprecision(3);
int counter = comp.size();
pyne::comp_iter nuc = comp.begin();
// This will pick up multiples of 3 components
while (counter >= 3) {
ss << std::setw(10) << std::left << "COMPOUND";
temp_s << frac_sign << nuc->second;
ss << std::setw(10) << std::right << temp_s.str();
ss << std::setw(10) << std::right << nucname::fluka(nuc->first);
nuc++;
temp_s.str(""); // reset the stringstream for reuse
temp_s << frac_sign << nuc->second;
ss << std::setw(10) << std::right << temp_s.str();
ss << std::setw(10) << std::right << nucname::fluka(nuc->first);
nuc++;
temp_s.str("");
temp_s << frac_sign << nuc->second;
ss << std::setw(10) << std::right << temp_s.str();
ss << std::setw(10) << std::right << nucname::fluka(nuc->first);
nuc++;
temp_s.str("");
ss << std::setw(10) << std::left << compound_name;
ss << std::endl;
counter -= 3;
}
// Get the last (or only, as the case may be) one or two fractions
if (nuc != comp.end()) {
ss << std::setw(10) << std::left << "COMPOUND";
temp_s << frac_sign << nuc->second;
ss << std::setw(10) << std::right << temp_s.str();
ss << std::setw(10) << std::right << nucname::fluka(nuc->first);
nuc++;
temp_s.str("");
if (nuc != comp.end()) {
temp_s << frac_sign << nuc->second;
ss << std::setw(10) << std::right << temp_s.str();
ss << std::setw(10) << std::right << nucname::fluka(nuc->first);
nuc++;
temp_s.str("");
} else {
ss << std::setw(10) << std::right << "";
ss << std::setw(10) << std::right << "";
}
ss << std::setw(10) << std::right << "";
ss << std::setw(10) << std::right << "";
ss << std::setw(10) << std::left << compound_name;
ss << std::endl;
}
return ss.str();
}
void pyne::Material::from_text(char * filename) {
std::string fname (filename);
from_text(fname);
};
void pyne::Material::from_text(std::string filename) {
// Check that the file is there
if (!pyne::file_exists(filename))
throw pyne::FileNotFound(filename);
// New filestream
std::ifstream f;
f.open(filename.c_str());
// Read in
comp.clear();
std::string keystr, valstr;
while ( !f.eof() ) {
f >> keystr;
if (0 == keystr.length())
continue;
if (keystr == "Mass"){
f >> valstr;
mass = pyne::to_dbl(valstr);
} else if (keystr == "Density") {
f >> valstr;
density = pyne::to_dbl(valstr);
} else if (keystr == "APerM") {
f >> valstr;
atoms_per_molecule = pyne::to_dbl(valstr);
} else if (pyne::nucname::isnuclide(keystr)) {
f >> valstr;
comp[pyne::nucname::id(keystr)] = pyne::to_dbl(valstr);
} else {
getline(f, valstr);
valstr= valstr.substr(0, valstr.length()-1);
metadata[keystr]= valstr;
continue;
}
};
f.close();
norm_comp();
};
void pyne::Material::write_text(char * filename) {
std::string fname (filename);
write_text(fname);
};
void pyne::Material::write_text(std::string filename) {
std::ofstream f;
f.open(filename.c_str(), std::ios_base::trunc);
Json::Reader reader;
std::vector<std::string> obj = metadata.getMemberNames();
if (0 <= mass)
f << "Mass " << mass << "\n";
if (0 <= density)
f << "Density " << density << "\n";
if (0 <= atoms_per_molecule)
f << "APerM " << atoms_per_molecule << "\n";
for (int i=0; i < metadata.size(); i=i+2){
f <<metadata.get(obj.at(i), "") << metadata.get(obj.at(i+1), "");
}
std::string nuc_name;
for(pyne::comp_iter i = comp.begin(); i != comp.end(); i++) {
nuc_name = pyne::nucname::name( i->first ) + " ";
while (nuc_name.length() < 8)
nuc_name += " ";
f << nuc_name << i->second << "\n";
};
f.close();
};
void pyne::Material::load_json(Json::Value json) {
Json::Value::Members keys = json["comp"].getMemberNames();
Json::Value::Members::const_iterator ikey = keys.begin();
Json::Value::Members::const_iterator ikey_end = keys.end();
comp.clear();
for (; ikey != ikey_end; ++ikey)
comp[nucname::id(*ikey)] = json["comp"][*ikey].asDouble();
norm_comp();
mass = json["mass"].asDouble();
density = json["density"].asDouble();
atoms_per_molecule = json["atoms_per_molecule"].asDouble();
metadata = json["metadata"];
};
Json::Value pyne::Material::dump_json() {
Json::Value json = Json::Value(Json::objectValue);
Json::Value jcomp = Json::Value(Json::objectValue);
json["mass"] = mass;
json["density"] = density;
json["atoms_per_molecule"] = atoms_per_molecule;
json["metadata"] = metadata;
for(comp_iter i = comp.begin(); i != comp.end(); i++)
jcomp[nucname::name(i->first)] = (i->second);
json["comp"] = jcomp;
return json;
};
void pyne::Material::from_json(char * filename) {
std::string fname (filename);
from_json(fname);
};
void pyne::Material::from_json(std::string filename) {
if (!pyne::file_exists(filename))
throw pyne::FileNotFound(filename);
std::string s;
std::ifstream f (filename.c_str(), std::ios::in | std::ios::binary);
f.seekg(0, std::ios::end);
s.resize(f.tellg());
f.seekg(0, std::ios::beg);
f.read(&s[0], s.size());
f.close();
Json::Reader reader;
Json::Value json;
reader.parse(s, json);
load_json(json);
};
void pyne::Material::write_json(char * filename) {
std::string fname (filename);
write_json(fname);
};
void pyne::Material::write_json(std::string filename) {
Json::Value json = dump_json();
Json::StyledWriter writer;
std::string s = writer.write(json);
std::ofstream f;
f.open(filename.c_str(), std::ios_base::trunc);
f << s << "\n";
f.close();
};
/************************/
/*** Public Functions ***/
/************************/
/*--- Constructors ---*/
pyne::Material::Material() {
// Empty Material constructor
mass = -1.0;
density = -1.0;
atoms_per_molecule = -1.0;
metadata = Json::Value(Json::objectValue);
}
pyne::Material::Material(pyne::comp_map cm, double m, double d, double apm,
Json::Value attributes) {
// Initializes the mass stream based on an isotopic component dictionary.
comp = cm;
mass = m;
density=d;
atoms_per_molecule = apm;
metadata = attributes;
if (!comp.empty())
norm_comp();
};
pyne::Material::Material(char * filename, double m, double d, double apm,
Json::Value attributes) {
mass = m;
density=d;
View remainder of file in raw view
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.