-
Notifications
You must be signed in to change notification settings - Fork 81
/
__init__.py
590 lines (493 loc) · 23.1 KB
/
__init__.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
# Copyright 2019 TerraPower, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""
Blueprints describe the geometric and composition details of the objects in the reactor
(e.g. fuel assemblies, control rods, etc.).
Inputs captured within this blueprints module pertain to major design criteria like
custom material properties or basic structures like the assemblies in use.
This is essentially a wrapper for a yaml loader.
The given yaml file is expected to rigidly adhere to given key:value pairings.
See the :doc:`blueprints documentation </user/inputs/blueprints>` for more details.
The file structure is expectation is:
nuclide flags:
AM241: {burn: true, xs: true}
...
custom isotopics: {} # optional
blocks:
name:
component name:
component dimensions
...
assemblies:
name:
specifier: ABC
blocks: [...]
height: [...]
axial mesh points: [...]
xs types: [...]
# optional
myMaterialModification1: [...]
myMaterialModification2: [...]
# optionally extra settings (note this is probably going to be a removed feature)
# hotChannelFactors: TWRPclad
Examples
--------
>>> design = blueprints.Blueprints.load(self.yamlString)
>>> print(design.gridDesigns)
Notes
-----
The blueprints system was built to enable round trip translations between
text representations of input and objects in the code.
"""
from collections import OrderedDict
import collections
import copy
import os
import pathlib
import traceback
import typing
from ruamel.yaml import CLoader, RoundTripLoader
import ordered_set
import tabulate
import yamlize
import yamlize.objects
from armi import context
from armi import getPluginManager, getPluginManagerOrFail
from armi import plugins
from armi import runLog
from armi import settings
from armi.utils.customExceptions import InputError
from armi.nucDirectory import elements
from armi.nucDirectory import nuclideBases
from armi.reactor import assemblies
from armi.reactor import geometry
from armi.reactor import systemLayoutInput
from armi.scripts import migration
from armi.utils import textProcessors
# NOTE: using non-ARMI-standard imports because these are all a part of this package,
# and using the module imports would make the attribute definitions extremely long
# without adding detail
from armi.reactor.blueprints.reactorBlueprint import Systems, SystemBlueprint
from armi.reactor.blueprints.assemblyBlueprint import AssemblyKeyedList
from armi.reactor.blueprints.blockBlueprint import BlockKeyedList
from armi.reactor.blueprints import isotopicOptions
from armi.reactor.blueprints.gridBlueprint import Grids, Triplet
context.BLUEPRINTS_IMPORTED = True
context.BLUEPRINTS_IMPORT_CONTEXT = "".join(traceback.format_stack())
def loadFromCs(cs, roundTrip=False):
"""
Function to load Blueprints based on supplied ``CaseSettings``.
"""
# pylint: disable=import-outside-toplevel; circular import protection
from armi.utils import directoryChangers
with directoryChangers.DirectoryChanger(cs.inputDirectory, dumpOnException=False):
with open(cs["loadingFile"], "r") as bpYaml:
root = pathlib.Path(cs["loadingFile"]).parent.absolute()
bpYaml = textProcessors.resolveMarkupInclusions(bpYaml, root)
try:
bp = Blueprints.load(bpYaml, roundTrip=roundTrip)
except yamlize.yamlizing_error.YamlizingError as err:
if "cross sections" in err.args[0]:
runLog.error(
"The loading file {} contains invalid `cross sections` input. "
"Please run the `modify` entry point on this case to automatically convert."
"".format(cs["loadingFile"])
)
raise
return bp
class _BlueprintsPluginCollector(yamlize.objects.ObjectType):
"""
Simple metaclass for adding yamlize.Attributes from plugins to Blueprints.
This calls the defineBlueprintsSections() plugin hook to discover new class
attributes to add before the yamlize code fires off to make the root yamlize.Object.
Since yamlize.Object itself uses a metaclass to define the attributes to turn into
yamlize.Attributes, these need to be folded in early.
"""
def __new__(mcs, name, bases, attrs):
# pylint: disable=no-member
pm = getPluginManager()
if pm is None:
runLog.warning(
"Blueprints were instantiated before the framework was "
"configured with plugins. Blueprints cannot be imported before "
"ARMI has been configured."
)
else:
pluginSections = pm.hook.defineBlueprintsSections()
for plug in pluginSections:
for (attrName, section, resolver) in plug:
assert isinstance(section, yamlize.Attribute)
if attrName in attrs:
raise plugins.PluginError(
"There is already a section called '{}' in the reactor "
"blueprints".format(attrName)
)
attrs[attrName] = section
attrs["_resolveFunctions"].append(resolver)
newType = yamlize.objects.ObjectType.__new__(mcs, name, bases, attrs)
return newType
class Blueprints(yamlize.Object, metaclass=_BlueprintsPluginCollector):
"""Base Blueprintsobject representing all the subsections in the input file."""
nuclideFlags = yamlize.Attribute(
key="nuclide flags", type=isotopicOptions.NuclideFlags, default=None
)
customIsotopics = yamlize.Attribute(
key="custom isotopics", type=isotopicOptions.CustomIsotopics, default=None
)
blockDesigns = yamlize.Attribute(key="blocks", type=BlockKeyedList, default=None)
assemDesigns = yamlize.Attribute(
key="assemblies", type=AssemblyKeyedList, default=None
)
systemDesigns = yamlize.Attribute(key="systems", type=Systems, default=None)
gridDesigns = yamlize.Attribute(key="grids", type=Grids, default=None)
# These are used to set up new attributes that come from plugins. Defining its
# initial state here to make pylint happy
_resolveFunctions = []
def __new__(cls):
# yamlizable does not call __init__, so attributes that are not defined above
# need to be initialized here
self = yamlize.Object.__new__(cls)
self.assemblies = {}
self._prepped = False
self._assembliesBySpecifier = {}
self.allNuclidesInProblem = (
ordered_set.OrderedSet()
) # Better for performance since these are used for lookups
self.activeNuclides = ordered_set.OrderedSet()
self.inertNuclides = ordered_set.OrderedSet()
self.elementsToExpand = []
return self
def __init__(self):
# again, yamlize does not call __init__, instead we use Blueprints.load which
# creates and instance of a Blueprints object and initializes it with values
# using setattr. Since the method is never called, it serves the purpose of
# preventing pylint from issuing warnings about attributes not existing.
self._assembliesBySpecifier = {}
self._prepped = False
self.systemDesigns = Systems()
self.assemDesigns = AssemblyKeyedList()
self.blockDesigns = BlockKeyedList()
self.assemblies = {}
self.grids = Grids()
self.elementsToExpand = []
def __repr__(self):
return "<{} Assemblies:{} Blocks:{}>".format(
self.__class__.__name__, len(self.assemDesigns), len(self.blockDesigns)
)
def constructAssem(self, cs, name=None, specifier=None):
"""
Construct a new assembly instance from the assembly designs in this Blueprints object.
Parameters
----------
cs : CaseSettings object
Used to apply various modeling options when constructing an assembly.
name : str (optional, and should be exclusive with specifier)
Name of the assembly to construct. This should match the key that was used
to define the assembly in the Blueprints YAML file.
specifier : str (optional, and should be exclusive with name)
Identifier of the assembly to construct. This should match the identifier
that was used to define the assembly in the Blueprints YAML file.
Raises
------
ValueError
If neither name nor specifier are passed
Notes
-----
There is some possibility for "compiling" the logic with closures to make
constructing an assembly / block / component faster. At this point is is pretty
much irrelevant because we are currently just deepcopying already constructed
assemblies.
Currently, this method is backward compatible with other code in ARMI and
generates the `.assemblies` attribute (the BOL assemblies). Eventually, this
should be removed.
"""
self._prepConstruction(cs)
# TODO: this should be migrated assembly designs instead of assemblies
if name is not None:
assem = self.assemblies[name]
elif specifier is not None:
assem = self._assembliesBySpecifier[specifier]
else:
raise ValueError("Must supply assembly name or specifier to construct")
a = copy.deepcopy(assem)
# since a deepcopy has the same assembly numbers and block id's, we need to make it unique
a.makeUnique()
return a
def _prepConstruction(self, cs):
"""
This method initializes a bunch of information within a Blueprints object such
as assigning assembly and block type numbers, resolving the nuclides in the
problem, and pre-populating assemblies.
Ideally, it would not be necessary at all, but the ``cs`` currently contains a
bunch of information necessary to create the applicable model. If it were
possible, it would be terrific to override the Yamlizable.from_yaml method to
run this code after the instance has been created, but we need additional
information in order to build the assemblies that is not within the YAML file.
This method should not be called directly, but it is used in testing.
"""
if not self._prepped:
self._assignTypeNums()
for func in self._resolveFunctions:
func(self, cs)
self._resolveNuclides(cs)
self._assembliesBySpecifier.clear()
self.assemblies.clear()
for aDesign in self.assemDesigns:
a = aDesign.construct(cs, self)
self._assembliesBySpecifier[aDesign.specifier] = a
self.assemblies[aDesign.name] = a
self._checkAssemblyAreaConsistency(cs)
runLog.header("=========== Verifying Assembly Configurations ===========")
# pylint: disable=no-member
getPluginManagerOrFail().hook.afterConstructionOfAssemblies(
assemblies=self.assemblies.values(), cs=cs
)
self._prepped = True
def _assignTypeNums(self):
if self.blockDesigns is None:
# this happens when directly defining assemblies.
self.blockDesigns = BlockKeyedList()
for aDesign in self.assemDesigns:
for bDesign in aDesign.blocks:
if bDesign not in self.blockDesigns:
self.blockDesigns.add(bDesign)
def _resolveNuclides(self, cs):
"""
Process elements and determine how to expand them to natural isotopics.
Also builds meta-data about which nuclides are in the problem.
This system works by building a dictionary in the
``elementsToExpand`` attribute with ``Element`` keys
and list of ``NuclideBase`` values.
The actual expansion of elementals to isotopics occurs during
:py:meth:`Component construction <armi.reactor.blueprints.componentBlueprint.
ComponentBlueprint._constructMaterial>`.
"""
from armi import utils
actives = set()
inerts = set()
undefBurnChainActiveNuclides = set()
if self.nuclideFlags is None:
self.nuclideFlags = isotopicOptions.genDefaultNucFlags()
self.elementsToExpand = []
for nucFlag in self.nuclideFlags:
# this returns any nuclides that are flagged specifically for expansion by input
expandedElements = nucFlag.fileAsActiveOrInert(
actives, inerts, undefBurnChainActiveNuclides
)
self.elementsToExpand.extend(expandedElements)
inerts -= actives
self.customIsotopics = self.customIsotopics or isotopicOptions.CustomIsotopics()
(
elementalsToKeep,
expansions,
) = isotopicOptions.autoSelectElementsToKeepFromSettings(cs)
# Flag all elementals for expansion unless they've been flagged otherwise by
# user input or automatic lattice/datalib rules.
for elemental in nuclideBases.instances:
if not isinstance(elemental, nuclideBases.NaturalNuclideBase):
# `elemental` may be a NaturalNuclideBase or a NuclideBase
# skip all NuclideBases
continue
if elemental in elementalsToKeep:
continue
if elemental.name in actives:
currentSet = actives
actives.remove(elemental.name)
elif elemental.name in inerts:
currentSet = inerts
inerts.remove(elemental.name)
else:
# This was not specified in the nuclide flags at all.
# If a material with this in its composition is brought in
# it's nice from a user perspective to allow it.
# But current behavior is that all nuclides in problem
# must be declared up front.
continue
self.elementsToExpand.append(elemental.element)
if (
elemental.name in self.nuclideFlags
and self.nuclideFlags[elemental.name].expandTo
):
# user-input has precedence
newNuclides = [
nuclideBases.byName[nn]
for nn in self.nuclideFlags[elemental.element.symbol].expandTo
]
elif (
elemental in expansions
and elemental.element.symbol in self.nuclideFlags
):
# code-specific expansion required
newNuclides = expansions[elemental]
# overlay code details onto nuclideFlags for other parts of the code
# that will use them.
# TODO: would be better if nuclideFlags did this upon reading s.t.
# order didn't matter. On the other hand, this is the only place in
# the code where NuclideFlags get built and have user settings around
# (hence "resolve").
# This must be updated because the operative expansion code just uses the flags
#
# Also, if this element is not in nuclideFlags at all, we just don't add it
self.nuclideFlags[elemental.element.symbol].expandTo = [
nb.name for nb in newNuclides
]
else:
# expand to all possible natural isotopics
newNuclides = elemental.element.getNaturalIsotopics()
for nb in newNuclides:
currentSet.add(nb.name)
if self.elementsToExpand:
runLog.info(
"Will expand {} elementals to have natural isotopics".format(
", ".join(element.symbol for element in self.elementsToExpand)
)
)
self.activeNuclides = ordered_set.OrderedSet(sorted(actives))
self.inertNuclides = ordered_set.OrderedSet(sorted(inerts))
self.allNuclidesInProblem = ordered_set.OrderedSet(
sorted(actives.union(inerts))
)
# Inform user which nuclides are truncating the burn chain.
if undefBurnChainActiveNuclides:
runLog.info(
tabulate.tabulate(
[
[
"Nuclides truncating the burn-chain:",
utils.createFormattedStrWithDelimiter(
list(undefBurnChainActiveNuclides)
),
]
],
tablefmt="plain",
),
single=True,
)
def _checkAssemblyAreaConsistency(self, cs):
references = None
for a in self.assemblies.values():
if references is None:
references = (a, a.getArea())
continue
assemblyArea = a.getArea()
if isinstance(a, assemblies.RZAssembly):
# R-Z assemblies by definition have different areas, so skip the check
continue
if abs(references[1] - assemblyArea) > 1e-9:
runLog.error("REFERENCE COMPARISON ASSEMBLY:")
references[0][0].printContents()
runLog.error("CURRENT COMPARISON ASSEMBLY:")
a[0].printContents()
raise InputError(
"Assembly {} has a different area {} than assembly {} {}. Check inputs for accuracy".format(
a, assemblyArea, references[0], references[1]
)
)
blockArea = a[0].getArea()
for b in a[1:]:
if (
abs(b.getArea() - blockArea) / blockArea
> cs["acceptableBlockAreaError"]
):
runLog.error("REFERENCE COMPARISON BLOCK:")
a[0].printContents(includeNuclides=False)
runLog.error("CURRENT COMPARISON BLOCK:")
b.printContents(includeNuclides=False)
for c in b.getChildren():
runLog.error(
"{0} area {1} effective area {2}"
"".format(c, c.getArea(), c.getVolume() / b.getHeight())
)
raise InputError(
"Block {} has a different area {} than block {} {}. Check inputs for accuracy".format(
b, b.getArea(), a[0], blockArea
)
)
@classmethod
def migrate(cls, inp: typing.TextIO):
"""Given a stream representation of a blueprints file, migrate it.
Parameters
----------
inp : typing.TextIO
Input stream to migrate.
"""
for migI in migration.ACTIVE_MIGRATIONS:
if issubclass(migI, migration.base.BlueprintsMigration):
mig = migI(stream=inp)
inp = mig.apply()
return inp
@classmethod
def load(cls, stream, roundTrip=False):
"""This class method is a wrapper around the `yamlize.Object.load()` method.
The reason for the wrapper is to allow us to default to `Cloader`. Essentially,
the `CLoader` class is 10x faster, but doesn't allow for "round trip" (read-
write) access to YAMLs; for that we have the `RoundTripLoader`.
"""
loader = RoundTripLoader if roundTrip else CLoader
return super().load(stream, Loader=loader)
def migrate(bp: Blueprints, cs):
"""
Apply migrations to the input structure.
This is a good place to perform migrations that address changes to the system design
description (settings, blueprints, geom file). We have access to all three here, so
we can even move stuff between files. Namely, this:
- creates a grid blueprint to represent the core layout from the old ``geomFile``
setting, and applies that grid to a ``core`` system.
- moves the radial and azimuthal submesh values from the ``geomFile`` to the
assembly designs, but only if they are uniform (this is limiting, but could be
made more sophisticated in the future, if there is need)
This allows settings-driven core map to still be used for backwards compatibility.
At some point once the input stabilizes, we may wish to move this out to the
dedicated migration portion of the code, and not perform the migration so
implicitly.
"""
from armi.reactor.blueprints import gridBlueprint
if bp.systemDesigns is None:
bp.systemDesigns = Systems()
if bp.gridDesigns is None:
bp.gridDesigns = gridBlueprint.Grids()
if "core" in [rd.name for rd in bp.gridDesigns]:
raise ValueError("Cannot auto-create a 2nd `core` grid. Adjust input.")
geom = systemLayoutInput.SystemLayoutInput()
geom.readGeomFromFile(os.path.join(cs.inputDirectory, cs["geomFile"]))
gridDesigns = geom.toGridBlueprints("core")
for design in gridDesigns:
bp.gridDesigns[design.name] = design
if "core" in [rd.name for rd in bp.systemDesigns]:
raise ValueError(
"Core map is defined in both the ``geometry`` setting and in "
"the blueprints file. Only one definition may exist. "
"Update inputs."
)
bp.systemDesigns["core"] = SystemBlueprint("core", "core", Triplet())
if geom.geomType in (geometry.GeomType.RZT, geometry.GeomType.RZ):
aziMeshes = {indices[4] for indices, _ in geom.assemTypeByIndices.items()}
radMeshes = {indices[5] for indices, _ in geom.assemTypeByIndices.items()}
if len(aziMeshes) > 1 or len(radMeshes) > 1:
raise ValueError(
"The system layout described in {} has non-uniform "
"azimuthal and/or radial submeshing. This migration is currently "
"only smart enough to handle a single radial and single azimuthal "
"submesh for all assemblies.".format(cs["geomFile"])
)
radMesh = next(iter(radMeshes))
aziMesh = next(iter(aziMeshes))
for _, aDesign in bp.assemDesigns.items():
aDesign.radialMeshPoints = radMesh
aDesign.azimuthalMeshPoints = aziMesh
# Someday: write out the migrated file. At the moment this messes up the case
# title and doesn't yet have the other systems in place so this isn't the right place.
# cs.writeToXMLFile(cs.caseTitle + '.migrated.xml')
# with open(os.path.split(cs['loadingFile'])[0] + '.migrated.' + '.yaml', 'w') as loadingFile:
# blueprints.Blueprints.dump(bp, loadingFile)