-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
798 lines (741 loc) · 30 KB
/
app.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
from json import dumps, load
from dash_table import DataTable
import dash
import dash_core_components as dcc
import dash_daq as daq
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output, State
from openmc import Material, calculate_cexs
from header_and_footer import header, footer
from reactions import reaction_names, ATOMIC_SYMBOL, convert_strings_to_numbers, zaid_to_isotope
downloaded_data = []
mcnp_downloaded_data = []
with open('options.json') as json_file:
element_names = load(json_file)
app = dash.Dash(
__name__,
prevent_initial_callbacks=True,
meta_tags=[
{
"name": "title",
"content": "XSPlot material cross section plotter"
},
{
"name": "description",
"content": "Online graph plotting tool for neutron macroscopic cross sections of materials",
},
{
"name": "keywords",
"keywords": "plot neutron nuclear cross section energy barns database plotter",
},
{
"name": "author",
"content": "Jonathan Shimwell"
},
{
"http-equiv": "X-UA-Compatible",
"content": "IE=edge"
},
{
"name": "viewport",
"content": "width=device-width, initial-scale=1.0"
},
{
"name": "charset",
"content": "UTF-8"
}
],
)
app.title = "XSPlot \U0001f4c9 neutron cross section plotter \U0001f4c8"
app.description = "Plot neutron cross sections. Nuclear data from the TENDL library."
app.config['suppress_callback_exceptions'] = True
server = app.server
app.layout = html.Div(header + [
dcc.Tabs(id='tabs', value='tab-1', children=[
dcc.Tab(label='One element/isotope at a time', children= [
html.Div(
[
html.Div([
html.H3(
[
"First select an element or isotope \U0001f449 then specficy a fraction \U0001f449 then add the element / isotope to the material table"
],
style={'text-align': 'center'}
),
html.H3(
[
"Continue adding elements / isotopes to the table to build up a material \U0000267b"
],
style={'text-align': 'center'}
),
html.H3(
[
'Specify the material density \U0001f449 then selection reactions of interest ', html.A("[reaction descriptions \U0001f517]", href="https://t2.lanl.gov/nis/endf/mts.html")
],
style={'text-align': 'center'}
),
html.H3(
[
'\U0001f4c8 The plot should update automatically \U0001f389'
],
style={'text-align': 'center'}
),
],
),
html.Br(),
html.Table(
[
html.Tr(
[
html.Th(
dcc.Dropdown(
options=element_names,
placeholder="Select an element / isotope ...",
style={"width": 250, "display": "inline-block"},
id="element_name",
),
),
html.Th(
dcc.Input(
id="fraction_value",
placeholder="Mass fraction",
value="",
type="number",
style={"padding": 10},
min=0,
# max=1, # users might want to user percents or ratios
step=0.01,
),
),
html.Th(
html.Button(
"Add Element / Isotope",
id="editing-rows-button",
n_clicks=0,
style={"height": 40, "width":200}
),
),
]
),
],
style={"width": "100%"},
),
html.Br(),
dash_table.DataTable(
id="adding-rows-table",
columns=[
{
"name": "Elements / Isotopes",
"id": "Elements",
},
{
"name": "Fractions",
"id": "Fractions",
},
],
data=[],
editable=False,
row_deletable=True,
style_cell={'textAlign': 'center', 'fontSize':16, 'font-family':'sans-serif'},
),
html.Br(),
html.Table(
[
html.Tr(
[
html.Th(
dcc.Input(
id="density_value",
placeholder="density in g/cm3",
value="",
type="number",
style={"padding": 10},
min=0,
step=0.01,
),
),
html.Th(
dcc.RadioItems(
options=[
{"label": "atom percent", "value": "ao"},
{"label": "weight percent", "value": "wo"},
],
value="ao",
id="fraction_type",
labelStyle={"display": "inline-block"},
),
),
html.Th(
dcc.Dropdown(
options=reaction_names,
placeholder="Select reaction(s) to plot",
style={"width": 400, "display": "inline-block"},
id="reaction_names",
multi=True,
),
),
]
)
],
style={"width": "100%"},
),
html.Br(),
]
),
html.Br(),
html.Table(
[
html.Tr(
[
html.Th(
dcc.RadioItems(
options=[
{"label": "log X axis", "value": "log"},
{"label": "linear X axis", "value": "linear"},
],
value="log",
id="xaxis_scale",
labelStyle={"display": "inline-block"},
),
),
html.Th(
html.Button(
"Download Plotted Data",
title="Download a text file of the data in JSON format",
id="download_button",
)
),
html.Th(
dcc.RadioItems(
options=[
{"label": "log Y axis", "value": "log"},
{"label": "linear y axis", "value": "linear"},
],
value="log",
id="yaxis_scale",
labelStyle={"display": "inline-block"},
),
)
]
)
],
style={"width": "100%"},
),
dcc.Loading(
id="loading-1",
type="default",
children=html.Div(id="graph_container")
),
dcc.Download(id="download-text"),
]+footer),
dcc.Tab(label='From MCNP material card', value='tab-2'),
]),
html.Div(id='tab-2-content'),
dcc.Download(id="download-text-mcnp"),
])
@app.callback(Output('tab-2-content', 'children'),
Input('tabs', 'value'))
def render_content(tab):
if tab == 'tab-2':
return html.Div([
html.Div([
html.H3(
[
" \U00002702 Copy a material card in MCNP format"
],
style={'text-align': 'center'}
),
html.H3(
[
"\U0001f4cb Paste the text into the input box bellow"
],
style={'text-align': 'center'}
),
html.H3(
[
'Finally specify the material density \U0001f449 then selection reactions of interest ', html.A("[reaction descriptions \U0001f517]", href="https://t2.lanl.gov/nis/endf/mts.html")
],
style={'text-align': 'center'}
),
html.H3(
[
'\U0001f4c8 The plot should update automatically \U0001f389'
],
style={'text-align': 'center'}
),
html.H3(
[
html.Label("\U0000269b If you make MCNP materials then you might be interested in the "),
html.A("neutronics material maker", href="https://docs.openmc.org/en/stable/"),
html.Label(" Python \U0001f40d package"),
],
style={'text-align': 'center'}
),
],
),
html.Br(),
html.Div(
[
html.Table(
[
html.Tr(
[
html.Th(
dcc.Textarea(
title='Enter the material card in MCNP format here',
id="mcnp_input_text",
placeholder=(
"Copy and paste your MCNP card here \n"
"\n"
"For exmple ...\n"
"\n"
"M24 001001 6.66562840e-01\n"
" 001002 1.03826667e-04\n"
" 008016 3.32540200e-01\n"
" 008017 1.26333333e-04\n"
" 008018 6.66800000e-04\n"
"\n"
),
value="",
style={'width': '50%', 'height': 300},
),
),
html.Th(id='processed_input_mcnp',children=[],
style={'width': '50%', 'height': 300}
),
]
),
],
style={"width": "100%"},
),
html.Br(),
html.Br(),
html.Table(
[
html.Tr(
[
html.Th(
dcc.Input(
id="mcnp_density_value",
placeholder="density in g/cm3",
value="",
type="number",
style={"padding": 10},
min=0,
step=0.01,
),
),
html.Th(
dcc.Dropdown(
options=reaction_names,
placeholder="Select reaction(s) to plot",
style={"width": 400, "display": "inline-block"},
id="mcnp_reaction_names",
multi=True,
),
),
]
)
],
style={"width": "100%"},
),
html.Br(),
]
),
html.Br(),
html.Table(
[
html.Tr(
[
html.Th(
dcc.RadioItems(
options=[
{"label": "log X axis", "value": "log"},
{"label": "linear X axis", "value": "linear"},
],
value="log",
id="mcnp_xaxis_scale",
labelStyle={"display": "inline-block"},
),
),
html.Th(
html.Button(
"Download Plotted Data",
title="Download a text file of the data in JSON format",
id="mcnp_download_button",
)
),
html.Th(
dcc.RadioItems(
options=[
{"label": "log Y axis", "value": "log"},
{"label": "linear y axis", "value": "linear"},
],
value="log",
id="mcnp_yaxis_scale",
labelStyle={"display": "inline-block"},
),
)
]
)
],
style={"width": "100%"},
),
dcc.Loading(
id="loading-1",
type="default",
children=html.Div(id="mcnp_graph_container")
),
])
@app.callback([
dash.dependencies.Output("mcnp_graph_container", "children"),
dash.dependencies.Output("processed_input_mcnp", "children"),
],
[
Input("mcnp_reaction_names", "value"),
Input("mcnp_input_text", "value"),
Input("mcnp_xaxis_scale", "value"),
Input("mcnp_yaxis_scale", "value"),
Input("mcnp_density_value", "value"),
],
)
def update_graph_from_mcnp(reaction_names, mcnp_input_text, xaxis_scale, yaxis_scale, density_value):
"""mcnp_input_text is and mcnp material card like the example below
M24 001001 6.66562840e-01
001002 1.03826667e-04
008016 3.32540200e-01
008017 1.26333333e-04
008018 6.66800000e-04
"""
no_density = html.H4(
'Specify a density in g/cm3',
style={"text-align": "center", "color": "red"},
)
no_mcnp_material_text = html.H4(
'Specify a material card in MCNP format',
style={"text-align": "center", "color": "red"},
)
no_mcnp_reaction = html.H4(
'Select a reaction',
style={"text-align": "center", "color": "red"},
)
if mcnp_input_text == None:
return [], no_mcnp_material_text
elif mcnp_input_text == '':
return [], no_mcnp_material_text
elif density_value == None:
return [], no_density
elif density_value == 0:
return [], no_density
elif density_value == '':
return [], no_density
elif reaction_names == None:
return [], no_mcnp_reaction
elif reaction_names == []:
return [], no_mcnp_reaction
else:
try:
# inputs look ok, but if the processing fails then return error
file_lines = mcnp_input_text.split('\n')
tokens = file_lines[0].split()
# makes the first string without the material number
material_string = f'{" ".join(tokens[1:])} '
# removes inline comments
if "$" in material_string:
position = material_string.find("$")
material_string = material_string[0:position]
current_line_number = 1
while True:
# end of the file check
if current_line_number == len(file_lines):
break
while True:
# end of the file check
if (current_line_number == len(file_lines)):
break
line = file_lines[current_line_number]
# handels mcnp continue line (which is 5 spaces)
if line[:5] == " ":
# removes inline comments
if "$" in line:
line = line[0:line.find("$")]
material_string = material_string + line
else: # new cell card
break
# increment the line number
current_line_number = current_line_number + 1
break
# removes end of line chars and splits up
tokens = material_string.replace("\n","").split()
if len(tokens)%2 != 0:
html.H4(
'The material string contains an odd number of zaids and fractions',
style={"text-align": "center", "color": "red"},
)
zaid_fraction_dict = []
while len(tokens) != 0:
nuclide = tokens[0].split(".")
isotope_name = zaid_to_isotope(nuclide[0])
fraction = convert_strings_to_numbers(tokens[1])
# removes two tokens from list
tokens.pop(0)
tokens.pop(0)
zaid_fraction_dict.append({
'element' : isotope_name,
'fraction' : fraction
})
my_mat = Material(name="my_mat")
table_contents = []
for entry in zaid_fraction_dict:
if entry['fraction'] < 0:
fraction_type = 'wo'
else:
fraction_type = 'ao'
if entry['element'][-1].isdigit():
my_mat.add_nuclide(
entry['element'], entry['fraction'], percent_type=fraction_type
)
else:
my_mat.add_element(
entry['element'], entry['fraction'], percent_type=fraction_type
)
table_contents.append(
html.Tr(
[
html.Td(
html.Label(f"{entry['element']}"),
style={"width":"25%","text-align": "left"}
),
html.Td(
html.Label(f"{entry['fraction']}"),
style={"width":"25%","text-align": "left"}
)
]
)
)
my_mat.set_density("g/cm3", density_value)
if len(my_mat.nuclides) == 0:
no_elements = html.H4(
'No elements or isotopes added',
style={"text-align": "center", "color": "red"},
)
return [], no_elements
else:
energy, xs_data_set = calculate_cexs(
my_mat, "material", reaction_names
)
global mcnp_downloaded_data
mcnp_downloaded_data = []
table_of_processed = [
html.Table(
[
html.Tr([
html.Th(
'Elements / isotopes',
style={"width":"25%","text-align": "left"}
),
html.Th(
'Fraction',
style={"width":"25%","text-align": "left"}
),
]
),
]+table_contents,
style={"width": "50%"},
)
]
for xs_data, reaction_name in zip(xs_data_set, reaction_names):
mcnp_downloaded_data.append(
{
"y": xs_data,
"x": energy,
"type": "scatter",
"name": f"MT {reaction_name}"
}
)
energy_units = "eV"
xs_units = "Macroscopic cross section [1/cm]"
return [
dcc.Graph(
config=dict(showSendToCloud=True),
figure={
"data": mcnp_downloaded_data,
"layout": {
"height": 800,
# "width":1600,
"margin": {"l": 3, "r": 2, "t": 15, "b": 60},
"xaxis": {
"title": {"text": f"Energy {energy_units}"},
"type": xaxis_scale,
"tickformat": ".1e",
"tickangle": 45,
},
"yaxis": {
"automargin": True,
"title": {"text": xs_units},
"type": yaxis_scale,
"tickformat": ".1e",
},
"showlegend": True,
},
},
)
], table_of_processed
except:
return [], html.H4(
'There was an error processing the MCNP material format',
style={"text-align": "center", "color": "red"},
)
def make_download_dict(data):
plotting_data = []
for entry in data:
plotting_data.append(
{
'energy (eV)':list(entry['x']),
'macroscopic cross section (1/cm)':list(entry['y']),
'reaction':entry['name'],
}
)
return dict(
content=dumps(plotting_data, indent=2),
filename="xsplot_download.json",
)
# uses a trigger to identify the callback and if the button is used then jsonifys the selected data
@app.callback(
Output("download-text", "data"),
[
Input("download_button", "n_clicks"),
],
)
def clicked_download(n_clicks):
trigger_id = dash.callback_context.triggered[0]["prop_id"].split(".")[0]
if trigger_id == "download_button":
if n_clicks is None:
raise dash.exceptions.PreventUpdate
else:
global downloaded_data
if len(downloaded_data) > 0:
download_dict = make_download_dict(downloaded_data)
return download_dict
# uses a trigger to identify the callback and if the button is used then jsonifys the selected data
@app.callback(
Output("download-text-mcnp", "data"),
[
Input("mcnp_download_button", "n_clicks"),
],
)
def clicked_mcnp_download(n_clicks):
trigger_id = dash.callback_context.triggered[0]["prop_id"].split(".")[0]
if trigger_id == "mcnp_download_button":
if n_clicks is None:
raise dash.exceptions.PreventUpdate
else:
global mcnp_downloaded_data
if len(mcnp_downloaded_data) > 0:
download_dict = make_download_dict(mcnp_downloaded_data)
return download_dict
@app.callback(
dash.dependencies.Output("graph_container", "children"),
[
Input("reaction_names", "value"),
Input("adding-rows-table", "data"),
Input("density_value", "value"),
Input("fraction_type", "value"),
Input("xaxis_scale", "value"),
Input("yaxis_scale", "value"),
],
)
def update_output(reaction_names, rows, density_value, fraction_type, xaxis_scale, yaxis_scale):
if (reaction_names != None) and (rows != None) and (density_value != None):
no_density = html.H4(
'Specify a density in g/cm3',
style={"text-align": "center", "color": "red"},
)
if density_value == None:
return no_density
elif density_value == 0:
return no_density
elif density_value == '':
return no_density
else:
my_mat = Material(name="my_mat")
for entry in rows:
if entry["Elements"][-1].isdigit():
my_mat.add_nuclide(
entry["Elements"], entry["Fractions"], percent_type=fraction_type
)
else:
my_mat.add_element(
entry["Elements"], entry["Fractions"], percent_type=fraction_type
)
my_mat.set_density("g/cm3", density_value)
if len(my_mat.nuclides) == 0:
no_elements = html.H4(
'No elements or isotopes added',
style={"text-align": "center", "color": "red"},
)
return no_elements
else:
energy, xs_data_set = calculate_cexs(
my_mat, "material", reaction_names
)
global downloaded_data
downloaded_data = []
for xs_data, reaction_name in zip(xs_data_set, reaction_names):
downloaded_data.append(
{
"y": xs_data,
"x": energy,
"type": "scatter",
"name": f"MT {reaction_name}"
}
)
energy_units = "eV"
xs_units = "Macroscopic cross section [1/cm]"
return [
dcc.Graph(
config=dict(showSendToCloud=True),
figure={
"data": downloaded_data,
"layout": {
"height": 800,
# "width":1600,
"margin": {"l": 3, "r": 2, "t": 15, "b": 60},
"xaxis": {
"title": {"text": f"Energy {energy_units}"},
"type": xaxis_scale,
"tickformat": ".1e",
"tickangle": 45,
},
"yaxis": {
"automargin": True,
"title": {"text": xs_units},
"type": yaxis_scale,
"tickformat": ".1e",
},
"showlegend": True,
},
},
)
]
else:
raise dash.exceptions.PreventUpdate
@app.callback(
Output("adding-rows-table", "data"),
Input("editing-rows-button", "n_clicks"),
State("adding-rows-table", "data"),
State("element_name", "value"),
State("fraction_value", "value"),
)
def add_row(n_clicks, rows, element_name, fraction_value):
if n_clicks > 0:
if element_name == None:
# no element selected
return rows
if fraction_value == "" or fraction_value == 0:
return rows
rows.append({"Elements": element_name, "Fractions": fraction_value})
return rows
if __name__ == "__main__":
app.run_server(debug=True)