mcstasscript.helper.mcstas_objects.Component#
- class mcstasscript.helper.mcstas_objects.Component(instance_name, component_name, AT=None, AT_RELATIVE=None, ROTATED=None, ROTATED_RELATIVE=None, RELATIVE=None, WHEN=None, EXTEND=None, GROUP=None, JUMP=None, SPLIT=None, comment=None, c_code_before=None, c_code_after=None, save_parameters=False)#
A class describing a McStas component to be written to an instrument
This class is used by the instrument class when setting up components as dynamic subclasses to this class. Most information can be given on initialize using keyword arguments, but there are methods for setting the attributes describing the component. The class contains both methods to write the component to an instrument file and methods for printing to the python terminal for checking the information. The McStas_Instr class creates subclasses from this class that have attributes for all parameters for the given component. The component information is read directly from the component files in the McStas installation. This class is frozen after __init__ so that no new attributes can be created, which allows direct feedback to the user if a parameter name is misspelled.
- name#
Name of the component instance in McStas (must be unique)
- Type:
str
- component_name#
Name of the component code to use, e.g. Arm, Guide_gravity, …
- Type:
str
- AT_data#
Position data of the component
- Type:
list of 3 floats, default [0, 0, 0]
- AT_relative#
Name of former component to use as reference for position
- Type:
str, default “ABSOLUTE”
- ROTATED_data#
Rotation data of the component
- Type:
list of 3 floats, default [0, 0, 0]
- ROTATED_relative#
Name of former component to use as reference for position
- Type:
str, default “ABSOLUTE”
- WHEN#
String with logical c expression x for when component is active
- Type:
str, default “”
- EXTEND#
c code for McStas EXTEND section
- Type:
str, default “”
- GROUP#
Name of group the component should belong to
- Type:
str, default “”
- JUMP#
String describing use of JUMP, need to contain all after “JUMP”
- Type:
str, default “”
- SPLIT#
Integer setting SPLIT, splitting the neutron before this component
- Type:
int, default 0 (disabled)
- c_code_before#
C code inserted before the component
- Type:
str, default “”
- c_code_after#
C code inserted after the component
- Type:
str, default “”
- component_parameters#
Parameters to be used with component in dictionary
- Type:
dict
- comment#
Comment inserted before the component as an explanation
- Type:
str, default “”
- __isfrozen#
If true no new attributes can be created, when false they can
- Type:
bool
- set_AT(at_list, RELATIVE)#
Sets AT_data, can set AT_relative using keyword
- set_AT_RELATIVE(relative)#
Can set RELATIVE for position
- set_ROTATED(rotated_list, RELATIVE)#
Sets ROTATED_data, can set ROTATED_relative using keyword
- set_ROTATED_RELATIVE(relative)#
Can set RELATIVE for rotation
- set_RELATIVE(relative_name)#
Set both AT_relative and ROTATED_relative to relative_name
- set_parameters(dict_input)#
Adds dictionary entries to parameter dictionary
- set_WHEN(string)#
Sets WHEN string
- set_SPLIT(value)#
Sets SPLIT value, a value of 0 disables SPLIT
- set_GROUP(string)#
Sets GROUP name
- set_JUMP(string)#
Sets JUMP string
- append_EXTEND(string)#
Append string to EXTEND string
- set_c_code_before(string)#
Sets c code to be inserted before component
- set_c_code_before(string)#
Sets c code to be inserted after component
- set_comment(string)#
Sets comment for component
- write_component(fo)#
Writes component code to instrument file
- show_parameters()#
Prints current component state with all parameters
- print_long()#
Prints basic view of component code (not correct syntax)
- print_long_deprecated()#
Prints basic view of component code (obsolete)
- print_short(**kwargs)#
Prints short description, used in print_components
- set_keyword_input(**kwargs)#
Handle keyword arguments during initialize
- __setattr__(key, value)#
Overwriting __setattr__ to implement ability to freeze
- _freeze()#
Freeze the class so no new attributes can be defined
- _unfreeze()#
Unfreeze the class so new attributes can be defined again
- __init__(instance_name, component_name, AT=None, AT_RELATIVE=None, ROTATED=None, ROTATED_RELATIVE=None, RELATIVE=None, WHEN=None, EXTEND=None, GROUP=None, JUMP=None, SPLIT=None, comment=None, c_code_before=None, c_code_after=None, save_parameters=False)#
Initializes McStas component with specified name and component
- Parameters:
instance_name (str) – name of the instance of the component
component_name (str) – name of the component type e.g. Arm, Guide_gravity, …
arguments (keyword) –
- ATlist of 3 floats, default [0, 0, 0]
Sets AT_data describing position of component
- AT_RELATIVEstr, default “ABSOLUTE”
sets AT_relative, describing position reference
- ROTATEDlist of 3 floats, default [0, 0, 0]
Sets ROTATED_data, describing rotation of component
- ROTATED_RELATIVEstr, default “ABSOLUTE”
Sets ROTATED_relative, sets reference for rotation
- RELATIVEstr
Sets both AT_relative and ROTATED_relative
- WHENstr, default “”
Sets WHEN string, should contain logical c expression
- EXTENDstr, default “”
Sets initial EXTEND string, should contain c code
- GROUPstr, default “”
Sets name of group the component should belong to
- JUMPstr, default “”
Sets JUMP str
- SPLITint, default 0 (disabled)
Sets SPLIT value
- comment: str, default “”
Sets comment string
- c_code_before: str, default “”
Sets c code before component
- c_code_after: str, default “”
Sets c code after component
Methods
__init__(instance_name, component_name[, ...])Initializes McStas component with specified name and component
add_search(statement[, SHELL])Adds a search statement to the component
append_EXTEND(string)Method for adding line of c to EXTEND section of this component
check_parameters(instrument_variables)Checks the current component parameters are in given name list
Clears the component of all search statements
get_parameter_names()make_write_string()Prints information about the component
print_short([longest_name])Prints short description of component to list print
set_AT(at_list[, RELATIVE])Method for setting position of component
set_AT_RELATIVE(relative)Sets AT RELATIVE with string or Component instance
set_GROUP(string)Method for setting GROUP keyword of this component
set_JUMP(string)Method for setting JUMP expression of this component
set_RELATIVE(relative)Method for setting reference of component position and rotation
set_ROTATED(rotated_list[, RELATIVE])Method for setting rotation of component
set_ROTATED_RELATIVE(relative)Sets ROTATED RELATIVE with string or Component instance
set_SPLIT(value)Method for setting SPLIT value of this component
set_WHEN(string)Method for setting WHEN c expression of this component
set_c_code_after(string)Method for setting c code after this component
set_c_code_before(string)Method for setting c code before this component
set_comment(string)Sets a comment displayed before the component in written files
set_keyword_input([AT, AT_RELATIVE, ...])set_save_parameters(value)show_parameters([line_length])Shows available parameters and their defaults for the component
Shows available parameters and their defaults for the component
Shows all search statements of component
write_component(fo[, instrument_search])Method that writes component to file
- add_search(statement, SHELL=False)#
Adds a search statement to the component
The search line can be used to tell McStas to search for files in additional locations. Double quotes are added.
- Parameters:
statement (str) – The search statement
SHELL (bool (default False)) – if True, shell keyword is added
- append_EXTEND(string)#
Method for adding line of c to EXTEND section of this component
- Parameters:
EXTEND (str) – Line of c code added to EXTEND section of named component
- check_parameters(instrument_variables)#
Checks the current component parameters are in given name list
The name list will typically be a list of instrument parameters and declared variables.
- Parameters:
instrument_variables (list of str) – List of instrument variable names
- clear_search()#
Clears the component of all search statements
- print_long()#
Prints information about the component
Includes information on required parameters if they are not yet specified. Information on the components are added when the class is used as a superclass for classes describing each McStas component.
- print_short(longest_name=None)#
Prints short description of component to list print
- set_AT(at_list, RELATIVE=None)#
Method for setting position of component
Sets the position of the component using provided length 3 list as a vector. If only a float is given, it is considered along the beam direction, [0, 0, z]. The RELATIVE keyword is used to specify in relation to what the position is given, and can take either a string with a component name or a Component object.
- Parameters:
at_list (List of 3 floats or float) – Position of component relative to reference component
arguments (keyword) –
- RELATIVEstr
Sets reference component for position
- set_AT_RELATIVE(relative)#
Sets AT RELATIVE with string or Component instance
Set relative which becomes the reference for this components position, it is possible to use a string that match another component name or use a Component object where the name is used as the reference.
- Parameters:
relative (str or Component object) – Used as reference for component position
- set_GROUP(string)#
Method for setting GROUP keyword of this component
- Parameters:
GROUP (str) – Sets GROUP name for named McStas component
- set_JUMP(string)#
Method for setting JUMP expression of this component
Should contain all code needed after JUMP
- Parameters:
JUMP (str) – Sets JUMP expression for named McStas component
- set_RELATIVE(relative)#
Method for setting reference of component position and rotation
Input can be either a string matching an earlier component or a Component object.
- Parameters:
relative (str) – Reference component for position and rotation
- set_ROTATED(rotated_list, RELATIVE=None)#
Method for setting rotation of component
- Parameters:
rotated_list (List of 3 floats) – Rotation of component relative to reference component
arguments (keyword) –
- RELATIVEstr
Sets reference component for rotation
- set_ROTATED_RELATIVE(relative)#
Sets ROTATED RELATIVE with string or Component instance
Set relative which becomes the reference for this components rotation, it is possible to use a string that match another component name or use a Component object where the name is used as the reference.
- Parameters:
relative (str or Component object) – Used as reference for component rotation
- set_SPLIT(value)#
Method for setting SPLIT value of this component
- Parameters:
SPLIT (int or str) – Sets SPLIT value for named McStas component
- set_WHEN(string)#
Method for setting WHEN c expression of this component
- Parameters:
WHEN (str) – Sets WHEN c expression for named McStas component
- set_c_code_after(string)#
Method for setting c code after this component
- Parameters:
code (str) – Code to be pasted after component
- set_c_code_before(string)#
Method for setting c code before this component
- Parameters:
code (str) – Code to be pasted before component
- set_comment(string)#
Sets a comment displayed before the component in written files
- Parameters:
string (str) – Comment string
- show_parameters(line_length=None)#
Shows available parameters and their defaults for the component
Any value specified is not reflected in this view. The additional attributes defined when McStas_Instr creates subclasses for the individual components are required to run this method.
- show_parameters_simple()#
Shows available parameters and their defaults for the component
Any value specified is not reflected in this view. The additional attributes defined when McStas_Instr creates subclasses for the individual components are required to run this method.
- show_search()#
Shows all search statements of component
- write_component(fo, instrument_search=None)#
Method that writes component to file
Relies on attributes added when McStas_Instr creates a subclass based on the component class.