Change BIFROST: from an .instr edit to niess¶
The BIFROST bundle attached to each niess release is an ordinary McStas instrument, and
editing a copy of it is fine for a study of your own. This page is for when the change
should be made where the model lives instead: to keep it, to share it, or to get the
NeXus structure and the other targets to agree with it. Each recipe starts from the edit
you would make to the .instr and gives the niess equivalent.
Every recipe ends with an instrument, bifrost, that you emit like the stock one:
from niess.mccode import to_mccode
from mccode_antlr.io.json import save_json
save_json(to_mccode(bifrost, insert_provenance_metadata=False), 'bifrost.json')
and then mccode-antlr extract as in the bundle's README.
Nothing to change for a parameter
Chopper speeds and phases, slit and jaw openings, attenuators, and the sample and
detector-tank angles are instrument parameters. Set them on the mcrun command line;
neither the .instr nor niess needs editing.
Swap the sample¶
In the .instr: replace the sample Arm, a placeholder in the bundle, with a sample
component.
from niess.bifrost.bifrost import instrument
from niess.bifrost.samples import Cylinder, sample, vanadium
# a vanadium rod, 5 mm across and 20 mm tall, in place of the empty `sample` Arm
rod = Cylinder(radius=0.0025, height=0.02)
bifrost = instrument(sample=sample(vanadium(rod)))
niess.bifrost.samples has helpers for McStas's own sample components: vanadium,
powder, tunneling, spinwave, isotropic_sqw, crystal and crystal_sqw. Pass
several to sample(...), each with a share, to scatter from more than one. The
focusing on the analyzers and the elastic monitor, and the weights that go with it, are
worked out for you.
Correct a calibrated value¶
In the .instr: change a number on one or more COMPONENT lines: a position, a
length, a coating.
Every BIFROST component is built from a calibration dictionary, so a correction is a change to that dictionary:
from niess.bifrost.bifrost import instrument
from niess.bifrost.parameters import primary_parameters
calibration = primary_parameters()
# m=3.5 side walls on all 47 straight-guide units, one COMPONENT line each in the .instr
for name, unit in calibration['straight'].items():
if name.endswith('_straight'):
unit['left'] = unit['right'] = 3.5
bifrost = instrument(calibration)
primary_parameters() holds the primary spectrometer, one entry per section and one per
component within it, named as in the .instr. The detector tank's calibration is
niess.bifrost.parameters.tank_parameters(). A correction that should become part of the
official model belongs in those functions; open an issue or a pull request.
Add or remove a component¶
In the .instr: insert a COMPONENT line, or delete one.
niess has no API for this yet. A section's components are the fields of its class, in beam order, so changing what a section holds means a section class with different fields. These two helpers make one, and put it in place of the original:
import msgspec
from msgspec.structs import replace
from niess.components import Section
def resection(section, parts: dict):
"""`section` with `parts` as its components, in this order -- which is beam order.
niess has no API for changing what a section contains: its components are the
fields of its class. This makes a stand-in class with the same name, which is
what McStas names the section's %include file after, and the same underscore
extras, and fills it.
"""
extras = [(f.name, f.type, getattr(section, f.name))
for f in msgspec.structs.fields(section) if f.name.startswith('_')]
cls = msgspec.defstruct(type(section).__name__,
[(n, type(p)) for n, p in parts.items()] + extras,
bases=(Section,), kw_only=True)
return cls(**parts)
def with_primary_section(bifrost, name: str, section):
"""`bifrost` with the primary spectrometer's section `name` replaced."""
mount, *others = bifrost.parts
primary = replace(mount.content, **{name: section})
return replace(bifrost, parts=(replace(mount, content=primary), *others))
Add a monitor¶
Opaque is any McStas component, given by its type name and parameters, exactly as on a
COMPONENT line. Here a PSD_monitor goes 10 cm after the bandwidth monitor:
from scipp import vector
from niess.bifrost.bifrost import instrument
from niess.components.opaque import Opaque
bifrost = instrument()
straight = bifrost.parts[0].content.straight
after = straight.bandwidth_monitor
# 10 cm downstream of the bandwidth monitor, along the guide; positions in a guide
# section are in the instrument's coordinates, as in the .instr
psd = Opaque(name='my_psd', mccode_type='PSD_monitor',
mccode_parameters={'nx': 100, 'ny': 100, 'filename': '"my_psd.dat"',
'xwidth': 0.1, 'yheight': 0.1, 'restore_neutron': 1},
position=after.position + after.orientation * vector([0, 0, 0.1], unit='m'),
orientation=after.orientation)
parts = {}
for name, part in straight.to_dict().items():
parts[name] = part
if name == 'bandwidth_monitor':
parts['my_psd'] = psd
bifrost = with_primary_section(bifrost, 'straight', resection(straight, parts))
Positions inside the primary spectrometer's sections are in the instrument's coordinates,
so place a new component from a neighbour's position and orientation, as here.
Remove a component¶
from niess.bifrost.bifrost import instrument
bifrost = instrument()
expanding = bifrost.parts[0].content.expanding
parts = {name: part for name, part in expanding.to_dict().items()
if name != 'overlap_monitor'}
bifrost = with_primary_section(bifrost, 'expanding', resection(expanding, parts))
Limits¶
- The new section class exists only in your session. It is not part of niess, so a niess release can rename or reorder the parts your code refers to.
- An
Opaqueemits McStas and nothing else: a NeXus structure gets its placement but no description, and CAD export has nothing to draw. - If a structural change should be part of the official model, ask for it on niess rather than carrying the recipe. The section's class is then changed, and every target follows.
The sections of the primary spectrometer, in beam order, are compressor, curved,
expanding, straight and closing. print(bifrost.parts[0].content.straight) lists
what one contains.