-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtestrun.py
996 lines (834 loc) · 31.8 KB
/
testrun.py
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
# -*- coding: utf-8 -*-
# Created on Mon Nov 4 16:52:09 2019
# @author: Davide Laghi
# Copyright 2021, the JADE Development Team. All rights reserved.
# This file is part of JADE.
# JADE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# JADE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with JADE. If not, see <http://www.gnu.org/licenses/>.
import inputfile as ipt
import matreader as mat
import os
import subprocess
import shutil
import pandas as pd
import numpy as np
import sys
from copy import deepcopy
from tqdm import tqdm
from parsersD1S import (IrradiationFile, ReactionFile, Reaction)
CODE_TAGS = {'mcnp6': 'mcnp6', 'D1S5': 'd1suned3.1.2'}
D1S_CODES = ['D1S5']
# colors
CRED = '\033[91m'
CORANGE = '\033[93m'
CEND = '\033[0m'
class Test():
def __init__(self, inp, lib, config, log, VRTpath, confpath):
"""
Class representing a general test. This class will have to be extended
for specific tests.
Parameters
----------
inp : str
path to inputfile blueprint.
lib : str
library suffix to use (e.g. 31c).
config : pd.DataFrame (single row)
configuration options for the test.
log : Log
Jade log file access.
VRTpath : path like object
path to the variance reduction folder.
confpath : path like object
path to the test configuration folder.
Raises
------
ValueError
if the code specified in config is not admissible.
Returns
-------
None.
"""
# Test Library
self.lib = lib
# Configuration options for the test
self.config = config
# MCNP original input
self.original_inp = inp
# Log for warnings
self.log = log
# VRT path
self.path_VRT = VRTpath
# Get the configuration files path
self.test_conf_path = confpath
# Chek for valid code
code = config['Code']
if code not in CODE_TAGS.keys():
raise ValueError(code+' is not an admissible value for code.\n' +
'Please double check the configuration file.')
else:
self.code = code # transport code to be used for the benchmark
# Generate input file template according to code
if code == 'D1S5':
self.inp = ipt.D1S5_InputFile.from_text(inp)
# It also have additional files then that must be in the
# VRT folder (irradiation and reaction files)
irrfile = os.path.join(VRTpath, self.inp.name,
self.inp.name+'_irrad')
reacfile = os.path.join(VRTpath, self.inp.name,
self.inp.name+'_react')
try:
self.irrad = IrradiationFile.from_text(irrfile)
self.react = ReactionFile.from_text(reacfile)
except FileNotFoundError:
# For instance in sphere test they are not provided
# There may be reasons why these files are not provided, it is
# responsability of the user to make them available or not.
self.irrad = None
self.react = None
else:
self.inp = ipt.InputFile.from_text(inp)
self.irrad = None
self.react = None
# Name of input file
self.name = self.inp.name
# Add the stop card according to config
config = config.dropna()
try:
nps = config['NPS cut-off']
except KeyError:
nps = None
if nps is np.nan:
nps = None
try:
ctme = config['CTME cut-off']
except KeyError:
ctme = None
if ctme is np.nan:
ctme = None
try:
tally = config['Relative Error cut-off'].split('-')[0]
error = config['Relative Error cut-off'].split('-')[1]
precision = (tally, error)
except KeyError:
precision = None
self.nps = nps
self.ctme = ctme
self.precision = precision
# Directory where the MCNP run will be performed
self.MCNPdir = None
def _translate_input(self, lib, libmanager):
"""
Translate the input template to selected library
Parameters
----------
lib : str or dic
There are many ways to provide a librart to be translated
check the matreader doc for more details.
libmanager : libmanager.LibManager
Manager dealing with libraries operations..
Returns
-------
None.
"""
if isinstance(self.inp, ipt.D1S_Input):
# Then it was the translation of a D1S input, additional
# actions are required
add = self.inp.translate(lib, libmanager,
original_irradfile=self.irrad,
original_reacfile=self.react)
newirradiations = add[0]
newreactions = add[1]
self.irrad.irr_schedules = newirradiations
self.react.reactions = newreactions
else:
self.inp.translate(lib, libmanager)
self.inp.update_zaidinfo(libmanager)
def generate_test(self, lib_directory, libmanager, MCNP_dir=None):
"""
Generate the test input files
Parameters
----------
lib_directory : path or string
Path to lib benchmarks input folders.
libmanager : libmanager.LibManager
Manager dealing with libraries operations.
MCNPdir : str or path
allows to ovewrite the MCNP dir if needed. The default is None
Returns
-------
None.
"""
# Translate the input
self._translate_input(self.lib, libmanager)
# Add stop card
self.inp.add_stopCard(self.nps, self.ctme, self.precision)
# Identify working directory
testname = self.inp.name
if MCNP_dir is None:
motherdir = os.path.join(lib_directory, testname)
else:
motherdir = MCNP_dir
self.MCNPdir = motherdir
# If previous results are present they are canceled
if os.path.exists(motherdir):
shutil.rmtree(motherdir)
os.mkdir(motherdir)
# edits_file = os.path.join(directoryVRT, 'inp_edits.txt')
# ww_file = os.path.join(directoryVRT, 'wwinp')
# if os.path.exists(directoryVRT):
# # This was tested only for sphere... be careful
# self.inp.add_edits(edits_file) # Add variance reduction
# Allow space for personalization getting additional modification
self.custom_inp_modifications()
# Write new input file
outinpfile = os.path.join(motherdir, testname)
self.inp.write(outinpfile)
# And accessory files if needed
if self.irrad is not None:
self.irrad.write(motherdir)
if self.react is not None:
self.react.write(motherdir)
# Get VRT files if available
wwinp = os.path.join(self.path_VRT, testname, 'wwinp')
if os.path.exists(wwinp):
outfile = os.path.join(motherdir, 'wwinp')
shutil.copyfile(wwinp, outfile)
def custom_inp_modifications(self):
"""
Perform additional operation on the input before generation. In this
parent object actually does nothing
Returns
-------
None.
"""
# It does not do anything in the default benchmark
pass
def run(self, cpu=1, timeout=None):
"""
run the input
Parameters
----------
cpu : int, optional
number of CPU to be used. The default is 1.
timeout : int, optional
number of seconds after the simulation should be killed.
The default is None.
Returns
-------
None.
"""
name = self.name
directory = self.MCNPdir
code_tag = CODE_TAGS[self.code]
self._runMCNP(code_tag, name, directory, cpu=cpu, timeout=timeout)
@staticmethod
def _runMCNP(code, name, directory, cpu=1, timeout=None):
"""
Run or continue test execution
Parameters
----------
code : str
tag of the code to be used (e.g. mcnp6)
name : str
MCNP inputfile name.
directory : str/path
path to the test.
cpu : int, optional
Number of CPU to use. The default is 1.
timeout : int, optional
Time in s for emergency timeout. The default is None.
Returns
-------
flagnotrun : Bool
If true the timeout was reached.
"""
command = 'name='+name+' wwinp=wwinp tasks '+str(cpu)
flagnotrun = False
try:
# cancel eventual previous output file
outputfile = os.path.join(directory, name+'o')
if os.path.exists(outputfile):
os.remove(outputfile)
# check if runtpe exits
runtpe = os.path.join(directory, name+'r')
if os.path.exists(runtpe):
command = command+' runtpe='+name+'r'
# Execution
subprocess.run([shutil.which(code), command], cwd=directory,
creationflags=subprocess.CREATE_NEW_CONSOLE,
timeout=timeout)
except subprocess.TimeoutExpired:
pass
return flagnotrun
class SphereTest(Test):
"""
Class handling the sphere test
"""
def generate_test(self, directory, libmanager, limit=None, lib=None):
"""
Generated all the sphere test for a selected library
Parameters
----------
directory : str or path
path to the sphere input folder.
libmanager : LibManager
manager of the nuclear data operations.
limit : int, optional
limit the test to the first n zaids and materials.
The default is None.
Returns
-------
None.
"""
if lib is None:
lib = self.lib
# Get typical materials input
dirmat = os.path.dirname(self.original_inp)
matpath = os.path.join(dirmat, 'TypicalMaterials')
inpmat = ipt.InputFile.from_text(matpath)
matlist = inpmat.matlist
# Get zaids available into the selected library
zaids = libmanager.get_libzaids(lib)
testname = self.inp.name
motherdir = os.path.join(directory, testname)
# If previous results are present they are canceled
if os.path.exists(motherdir):
shutil.rmtree(motherdir)
os.mkdir(motherdir)
# GET SETTINGS
# Zaids
settings = os.path.join(self.test_conf_path, 'ZaidSettings.csv')
settings = pd.read_csv(settings, sep=';').set_index('Z')
# Materials
settings_mat = os.path.join(self.test_conf_path,
'MaterialsSettings.csv')
settings_mat = pd.read_csv(settings_mat, sep=';').set_index('Symbol')
self.MCNPdir = motherdir
print(' Zaids:')
# for zaid in tqdm(zaids):
for zaid in tqdm(zaids[:limit]):
Z = int(zaid[:-3])
# Get Density
if zaid[-3:] == '235': # Special treatment for U235
density = 1
else:
density = settings.loc[Z, 'Density [g/cc]']
if settings.loc[Z, 'Let Override']:
# get stop parameters
if self.nps is None:
nps = settings.loc[Z, 'NPS cut-off']
if nps is np.nan:
nps = None
else:
nps = self.nps
if self.ctme is None:
ctme = settings.loc[Z, 'CTME cut-off']
if ctme is np.nan:
ctme = None
else:
ctme = self.ctme
if self.precision is None:
prec = settings.loc[Z, 'Relative Error cut-off']
if prec is np.nan:
precision = None
else:
tally = prec.split('-')[0]
error = prec.split('-')[1]
precision = (tally, error)
else:
precision = self.precision
# Zaid local settings are prioritized
else:
nps = settings.loc[Z, 'NPS cut-off']
if nps is np.nan:
nps = None
ctme = settings.loc[Z, 'CTME cut-off']
if ctme is np.nan:
ctme = None
prec = settings.loc[Z, 'Relative Error cut-off']
if prec is np.nan:
precision = None
else:
tally = prec.split('-')[0]
error = prec.split('-')[1]
precision = (tally, error)
self.generate_zaid_test(zaid, libmanager, testname,
motherdir, -1*density, nps, ctme,
precision)
print(' Materials:')
# for material in tqdm(matlist.materials):
for material in tqdm(matlist.materials[:limit]):
# Get density
density = settings_mat.loc[material.name.upper(), 'Density [g/cc]']
self.generate_material_test(material, -1*density, libmanager,
testname, motherdir)
def generate_zaid_test(self, zaid, libmanager, testname, motherdir,
density, nps, ctme, precision, addtag=None,
parentlist=None, lib=None):
"""
Generate input for a single zaid sphere leakage benchmark run.
Parameters
----------
zaid : str
zaid in string format.
libmanager : Libmanager
Jade Libmanager.
testname : str
name of the benchmark.
motherdir : str/path
Path to the benchmark folder.
density : (str/float)
Density value for the sphere.
nps : float
number of particles cut-off
ctme : float
computer time cut-off
precision : float
precision cut-off
addtag : str, optional
add tag at the end of the single zaid test name. The default is
None
parentlist : list, optional
add the PIKMT if requested (list of parent zaids)
Returns
-------
None.
"""
if lib is None:
lib = self.lib
# Get VRT files
directoryVRT = os.path.join(self.path_VRT, zaid)
edits_file = os.path.join(directoryVRT, 'inp_edits.txt')
ww_file = os.path.join(directoryVRT, 'wwinp')
# Adjourn the material cards for the zaid
zaid = mat.Zaid(1, zaid[:-3], zaid[-3:], lib)
name, formula = libmanager.get_zaidname(zaid)
submat = mat.SubMaterial('M1', [zaid],
header='C '+name+' '+formula)
material = mat.Material([zaid], None, 'M1', submaterials=[submat])
matlist = mat.MatCardsList([material])
# Generate the new input
newinp = deepcopy(self.inp)
newinp.matlist = matlist # Assign material
# adjourn density
newinp.change_density(density)
# assign stop card
newinp.add_stopCard(nps, ctme, precision)
# add PIKMT if requested
if parentlist is not None:
newinp.add_PIKMT_card(parentlist)
if os.path.exists(directoryVRT):
newinp.add_edits(edits_file) # Add variance reduction
# Write new input file
outfile, outdir = self._get_zaidtestname(testname, zaid, formula,
addtag=addtag)
outpath = os.path.join(motherdir, outdir)
os.mkdir(outpath)
outinpfile = os.path.join(outpath, outfile)
newinp.write(outinpfile)
# Copy also wwinp file
if os.path.exists(directoryVRT):
outwwfile = os.path.join(outpath, 'wwinp')
shutil.copyfile(ww_file, outwwfile)
@staticmethod
def _get_zaidtestname(testname, zaid, formula, addtag=None):
outfile = (testname+'_'+zaid.element+zaid.isotope+'_'+formula+'_')
outdir = testname+'_'+zaid.element+zaid.isotope+'_'+formula
if addtag is not None:
outfile = outfile+addtag+'_'
outdir = outdir+'_'+addtag
return outfile, outdir
def generate_material_test(self, material, density, libmanager, testname,
motherdir, parentlist=None, lib=None):
"""
Generate a sphere leakage benchmark input for a single typical
material.
Parameters
----------
material : matreader.Material
material object to be used for the new input.
density : float
densitiy value in g/cc
libmanager : Libmanager
Jade Libmanager.
testname : str
name of the benchmark.
motherdir : str/path
Path to the benchmark folder.
parentlist : list, optional
add the PIKMT if requested (list of parent zaids)
Returns
-------
None.
"""
if lib is None:
lib = self.lib
truename = material.name
# Get VRT file
directoryVRT = os.path.join(self.path_VRT, truename)
edits_file = os.path.join(directoryVRT, 'inp_edits.txt')
ww_file = os.path.join(directoryVRT, 'wwinp')
# Translate and assign the material
material.translate(lib, libmanager)
material.header = material.header+'C\nC True name:'+truename
material.name = 'M1'
matlist = mat.MatCardsList([material])
# Generate the new input
newinp = deepcopy(self.inp)
newinp.matlist = matlist # Assign material
# adjourn density
newinp.change_density(density)
# add stop card
newinp.add_stopCard(self.nps, self.ctme, self.precision)
# Add PIKMT card if required
if parentlist is not None:
newinp.add_PIKMT_card(parentlist)
if os.path.exists(directoryVRT):
newinp.add_edits(edits_file) # Add variance reduction
# Write new input file
outfile = testname+'_'+truename+'_'
outdir = testname+'_'+truename
outpath = os.path.join(motherdir, outdir)
os.mkdir(outpath)
outinpfile = os.path.join(outpath, outfile)
newinp.write(outinpfile)
# Copy also wwinp file
if os.path.exists(directoryVRT):
outwwfile = os.path.join(outpath, 'wwinp')
shutil.copyfile(ww_file, outwwfile)
def run(self, cpu=1, timeout=None):
"""
Sphere test needs an ad-hoc run method to run all zaids tests
"""
flagnotrun = False
for folder in tqdm(os.listdir(self.MCNPdir)):
path = os.path.join(self.MCNPdir, folder)
name = folder+'_'
code = 'mcnp6'
command = 'name='+name+' wwinp=wwinp tasks '+str(cpu)
try:
subprocess.run([code, command], cwd=path,
creationflags=subprocess.CREATE_NEW_CONSOLE,
timeout=timeout)
except subprocess.TimeoutExpired:
flagnotrun = True
self.log.adjourn(name+' reached timeout, eliminate folder')
continue
if flagnotrun:
print("""
Some MCNP run reached timeout, they are listed in the log file.
Please remove their folders before attempting to postprocess the library""")
class SphereTestSDDR(SphereTest):
def __init__(self, *args, **keyargs):
super().__init__(*args, **keyargs)
# Lib needs to be provided in the {activation lib}-{transportlib}
activationlib, transportlib = check_transport_activation(self.lib)
self.activationlib = activationlib
self.transportlib = transportlib
def generate_test(self, directory, libmanager, limit=None, lib=None):
super().generate_test(directory, libmanager, limit=limit,
lib=self.activationlib)
def generate_zaid_test(self, zaid, libmanager, testname, motherdir,
density, nps, ctme, precision):
"""
Generate input for a single zaid sphere SDDR benchmark run.
Depending on the number of reactions, multiple inputs may be generated
from a single zaid.
Parameters
----------
zaid : str
zaid in string format.
libmanager : Libmanager
Jade Libmanager.
testname : str
name of the benchmark.
motherdir : str/path
Path to the benchmark folder.
density : (str/float)
Density value for the sphere.
nps : float
number of particles cut-off
ctme : float
computer time cut-off
precision : float
precision cut-off
Returns
-------
None.
"""
# Recover the available reactions
reactions = libmanager.get_reactions(self.activationlib, zaid)
# Genearate a different test for each reaction
for reaction in reactions:
MT = reaction[0]
daughter = reaction[1]
# generate the input file
super().generate_zaid_test(zaid, libmanager, testname,
motherdir, density, nps, ctme,
precision, addtag=MT,
parentlist=[zaid],
lib=self.activationlib)
# --- Add the irradiation file ---
# generate file
reacfile = self._generate_reaction_file([(zaid, MT, daughter)])
# Recover ouput directory
name, formula = libmanager.get_zaidname(zaid)
zaidob = mat.Zaid(1, zaid[:-3], zaid[-3:], self.activationlib)
_, outdir = self._get_zaidtestname(testname, zaidob, formula,
addtag=MT)
outpath = os.path.join(motherdir, outdir)
reacfile.write(outpath)
# --- Add the irradiation file ---
irrfile, ans = self._generate_irradiation_file([daughter])
irrfile.write(outpath)
if not ans:
print(CORANGE +
' Warning: {} irr file was not generated'.format(outdir)+
CEND)
def generate_material_test(self, material, density, libmanager, testname,
motherdir):
"""
Generate a sphere leakage benchmark input for a single typical
material.
Parameters
----------
material : matreader.Material
material object to be used for the new input.
density : float
densitiy value in g/cc
libmanager : Libmanager
Jade Libmanager.
testname : str
name of the benchmark.
motherdir : str/path
Path to the benchmark folder.
Returns
-------
None.
"""
# there will only be one test for each material that includes
# all the possible reactions
truename = material.name
# --- Add the reaction file ---
# Recover all the reactions (for each isotope) in the material
reactions = []
parentlist = []
daughterlist = []
transportlist = []
for submat in material.submaterials:
for zaid in submat.zaidList:
parent = zaid.element+zaid.isotope
zaidreactions = libmanager.get_reactions(self.activationlib,
parent)
if len(zaidreactions) > 0:
# it is a parent only if reactions are available
parentlist.append(parent)
else:
# normal transport
transportlist.append(parent)
for MT, daughter in zaidreactions:
reactions.append((parent, MT, daughter))
daughterlist.append(daughter)
# eliminate duplicates
daughterlist = list(set(daughterlist))
parentlist = list(set(parentlist))
transportlist = list(set(transportlist))
# The generation of the inputs has to be done only if there is at
# least one parent
if len(parentlist) == 0:
return
else:
# generate the input
libs = {self.activationlib: parentlist,
self.transportlib: transportlist}
super().generate_material_test(material, density, libmanager,
testname,
motherdir, parentlist=parentlist,
lib=libs)
# Generate the reaction file
reac_file = self._generate_reaction_file(reactions)
# recover output directory and write file
outdir = testname+'_'+truename
outpath = os.path.join(motherdir, outdir)
reac_file.write(outpath)
# --- Add the irradiation file ---
irrfile, ans = self._generate_irradiation_file(set(daughterlist))
irrfile.write(outpath)
if not ans:
print(CORANGE +
' Warning: {} irr file was not generated'.format(outdir)+
CEND)
def _generate_reaction_file(self, reactions):
"""
Generate a reaction file object given the parents and reactions
selected
Parameters
----------
parent : str
parent zaid num (e.g. 1001).
reactions : list
list of reactions (parent, MT, daughter) to be used.
Returns
-------
ReactionFile
Reaction file associated with the test.
"""
reaction_list = []
for parent, MT, daughter in reactions:
parent = parent+'.'+self.activationlib
rx = Reaction(parent, MT, daughter)
reaction_list.append(rx)
return ReactionFile(reaction_list)
def _generate_irradiation_file(self, daughters):
"""
Generate a D1S irradiation file selecting irradiation schedules from
an existing file.
Parameters
----------
daughters : list.
daughter zaids to be selected
Returns
-------
irradfile : IrradiationFile
newly generated irradiation file
ans : bool
the object was created without issues
"""
try:
filepath = os.path.join(self.test_conf_path, 'irrad_'+self.activationlib)
except FileNotFoundError:
print(CRED+"""
Please provide an irradiation file summary for lib {}. Check the documentation
for additional details. The application will now exit.
""".format(self.activationlib)+CEND)
sys.exit()
irradfile = IrradiationFile.from_text(filepath)
# Keep only useful irradiations
new_irradiations = []
for irradiation in irradfile.irr_schedules:
if irradiation.daughter in daughters:
new_irradiations.append(irradiation)
if len(new_irradiations) != len(daughters):
print(CORANGE+"""
Warning: irradiations schedules were not find for all specified daughters.
"""+CEND)
ans = False
else:
ans = True
irradfile.irr_schedules = new_irradiations
return irradfile, ans
class FNGTest(Test):
def custom_inp_modifications(self):
# Add the tracking for daughters in tally 14
zaids = self.irrad.get_daughters()
self.inp.add_track_contribution('F14:p', zaids, who='daughter')
# Add the tracking for daughters in tally 24
zaids = self.react.get_parents()
self.inp.add_track_contribution('F24:p', zaids, who='parent')
class MultipleTest:
def __init__(self, inpsfolder, lib, config, log, VRTpath, confpath,
TestOb=Test):
"""
A collection of Tests
Parameters
----------
inpsfolder : path-like object
folder that contains all inputs of the tests.
lib : str
library suffix to use (e.g. 31c).
config : pd.DataFrame (single row)
configuration options for the test.
log : Log
Jade log file access.
VRTpath : path like object
path to the variance reduction folder.
confpath : path like object
path to the test configuration folder.
TestOb : testrun.Test, optional
type of test object to be used. The default is Test.
Returns
-------
None.
"""
tests = []
for folder in os.listdir(inpsfolder):
inp = os.path.join(inpsfolder, folder)
test = TestOb(inp, lib, config, log, VRTpath, confpath)
tests.append(test)
self.tests = tests
self.name = os.path.basename(inpsfolder)
def generate_test(self, lib_directory, libmanager):
"""
Generate all the tests of the collection
Parameters
----------
lib_directory : path-like
output directory where to generate the tests.
libmanager : libmanager.LibManager
object handling libraries operations.
Returns
-------
None.
"""
self.MCNPdir = os.path.join(lib_directory, self.name)
safe_override(self.MCNPdir)
for test in self.tests:
mcnp_dir = os.path.join(self.MCNPdir, test.name)
test.generate_test(lib_directory, libmanager, MCNP_dir=mcnp_dir)
def run(self, cpu=1, timeout=None):
"""
Run all the tests
Parameters
----------
cpu : int, optional
number of CPU to be used. The default is 1.
timeout : int, optional
number of seconds after each simulation is killed. The default is
None.
Returns
-------
None.
"""
for test in tqdm(self.tests):
test.run(cpu=cpu, timeout=timeout)
def safe_mkdir(directory):
if not os.path.exists(directory):
os.mkdir(directory)
def safe_override(directory):
if os.path.exists(directory):
shutil.rmtree(directory)
os.mkdir(directory)
def check_true(obj):
# It may not work! check needed
if obj is True:
return True
elif obj == 'True':
return True
elif obj == 'true':
return True
else:
return False
def check_transport_activation(lib):
# Operate on the newlib, should arrive in the 99c-31c format
errmsg = """
Please define the pair activation-transport lib for the FNG benchmark
(e.g. 99c-31c). See additional details on the documentation.
"""
try:
activationlib = lib.split('-')[0]
transportlib = lib.split('-')[1]
except IndexError:
raise ValueError(errmsg)
# Check that libraries have been correctly defined
if activationlib+'-'+transportlib != lib:
raise ValueError(errmsg)
return activationlib, transportlib