面向mcstas的python脚本接口
McStasScript的Python项目详细描述
mcstatsscript
用于从python脚本创建和运行mcstas工具的mcstas api
一个api的原型,它允许通过一个接口与mcstas交互,比如在panosc的wp5下创建的jupyter笔记本。
安装
可以使用pip安装软件包
python3 -m pip install McStasScript --upgrade
有必要对包进行配置,这样就可以找到mcstas安装,这里我们将展示适合ubuntu系统的代码。配置将永久保存,并且仅在更新mcstas时需要更新。
from mcstasscript.interface import functions
my_configurator = functions.Configurator()
my_configurator.set_mcrun_path("/usr/bin/")
my_configurator.set_mcstas_path("/usr/share/mcstas/2.5/")
基本使用说明:
导入接口
from mcstasscript.interface import instr, plotter, functions, reader
现在可以使用这个包了从创建新乐器开始,只需输入名称即可
my_instrument = instr.McStas_instr("my_instrument_file")
然后可以添加mcstas组件,这里我们添加一个源代码
my_source = my_instrument.add_component("source", "Source_simple")
my_source.show_parameters() # Can be used to show available parameters for Source simple
源的参数可以作为python对象的属性直接调整
my_source.xwidth = 0.12
my_source.yheight = 0.12
my_source.lambda0 = 3
my_source.dlambda = 2.2
my_source.focus_xw = 0.05
my_source.focus_yh = 0.05
还添加了监视器以从模拟中获取数据
PSD = my_instrument.add_component("PSD", "PSD_monitor", AT=[0,0,1], RELATIVE="source")
PSD.xwidth = 0.1
PSD.yheight = 0.1
PSD.nx = 200
PSD.ny = 200
PSD.filename = "\"PSD.dat\""
这个简单的模拟可以从
data = my_instrument.run_full_instrument(foldername="first_run", increment_folder_name=True)
监视器的结果将作为mcstasdata对象列表存储在返回的数据中。计数存储为numpy数组。我们可以直接读取和更改强度,并在绘制之前操作数据。
data[0].Intensity
绘图通常在记录的所有监控器的子批次中完成。
plot = plotter.make_sub_plot(data)
在现有项目中使用
如果希望使用McStScript处理现有项目,则有一个读取器,它将读取McStas文件文件并将相应的McStascript Python仪器写入磁盘。下面是转换PSI_DMC.instr示例的示例:
Reader = reader.McStas_file("PSI_DMC.instr")
Reader.write_python_file("PSI_DMC_generated.py")
强烈建议在生成文件的输出和原始文件之间运行检查,以确保过程成功。
方法概述
下面是对项目中主要类的可用方法的快速概述大多数都有更多的关键字参数选项,这些参数在手册中有说明,但在python帮助中也有,例如帮助(instr.mcstas_instr.show_components)。
instr
└── McStas_instr(str instr_name) # Returns McStas instrument object on initialize
├── show_components(str category_name) # Show available components in given category
├── component_help(str component_name) # Prints component parameters for given component name
├── add_component(str name, str component_name) # Adds component to instrument and returns object
├── add_parameter(str name) # Adds instrument parameter with name
├── add_declare_var(str type, str name) # Adds declared variable with type and name
├── append_initialize(str string) # Appends a line to initialize (c syntax)
├── print_components() # Prints list of components and their location
├── write_full_instrument() # Writes instrument to disk with given name + ".instr"
└── run_full_instrument() # Runs simulation. Options in keyword arguments. Returns list of McStasData
component # returned by add_component
├── set_AT(list at_list) # Sets component position (list of x,y,z positions in [m])
├── set_ROTATED(list rotated_list) # Sets component rotation (list of x,y,z rotations in [deg])
├── set_RELATIVE(str component_name) # Sets relative to other component name
├── set_parameters(dict input) # Set parameters using dict input
├── set_comment(str string) # Set comment explaining something about the component
└── print_long() # Prints currently contained information on component
functions
├── name_search(str name, list McStasData) # Returns data set with given name from McStasData list
├── name_plot_options(str name, list McStasData, kwargs) # Sends kwargs to dataset with given name
├── load_data(str foldername) # Loads data from folder with McStas data as McStasData list
└── Configurator()
├── set_mcrun_path(str path) # sets mcrun path
├── set_mcstas_path(str path) # sets mcstas path
└── set_line_length(int length) # sets maximum line length
plotter
├── make_plot(list McStasData) # Plots each data set individually
└── make_sub_plot(list McStasData) # Plots data as subplot
reader
└── McStas_file(str filename) # Returns a reader that can extract information from given instr file
InstrumentReader # returned by McStas_file
├── generate_python_file(str filename) # Writes python file with information contaiend in isntrument
└── add_to_instr(McStas_instr Instr) # Adds information from instrument to McStasScirpt instrument