def diffraction_pattern_calculation(self, params=[20, 20], instr=DEFAULT_INSTRUMENT_PATH, optimization=False, simdir=None): """ calculated the diffracted neutrons by the object. running the simulation. producing 1D and 2D plot Parameters ---------- params: list list geometrical parameters to be optimized instr: string path of the instrument file on which mcvine simulation will run simdir: string path of the simulation output file Returns: diffracted intensities, d-spacing, error: tuple output result ------- """ name = self.create_collimator_geometry(params, optimization) if simdir is None: simdir = os.path.join(PARENT_DIR, "out/%s" % (name)) run_script.run1_mpi( instr, simdir, beam=self.source_neutrons(), ncount=self.ncount, nodes=self.nodes, angleMons=self.angleMons, sample_path=self.sample_path, sample=self.sampleassembly_fileName, sourceTosample_x=self.sourceTosample_x, sourceTosample_y=self.sourceTosample_y, sourceTosample_z=self.sourceTosample_z, sampleTodetector_z=self.sampleTodetector_z, number_detectors=self.number_of_detectors, detector_width=self.detector_width, detector_height=self.detector_height, number_pixels_in_height=self.number_pixels_in_height, number_of_box_in_height=self.number_of_box_in_height, number_pixels_in_width=self.number_pixels_in_width, number_of_box_in_width=self.number_of_box_in_width, overwrite_datafiles=True) if self.instrument_definition_file is None: # calling the existing instrument defition file self.instrument_definition_file = _safe_path_join( self.nexus_path, 'SNAP_virtual_Definition.xml') if self.template is None: # the path where template output will be saved self.template = os.path.join(self.nexus_path, 'template.nxs') # creating template file nxs_template.create(self.instrument_definition_file, ntotpixels=self.number_of_total_DetectorPixels, outpath=self.template, pulse_time_end=1e5) # the path where nexus file of simulation will be save nexus_file_path = os.path.join(self.nexus_path, '{}.nxs'.format(name)) # creating the nexus file of the simulation det2nxs.create_nexus( simdir, nexus_file_path, self.template, numberOfPixels=self.number_of_total_DetectorPixels) # the path where the nexus file for updated instrument definition file will be saved nexus_file_correctDet_path = os.path.join( self.nexus_path, '{}_correctDet.nxs'.format(name)) #updating the nexus file with proper configuration of instrument detector geometry rot.detector_position_for_reduction(nexus_file_path, conf, self.instrument_definition_file, nexus_file_correctDet_path) if self.masking: # the path where the masked file will be saved masked_file_path = os.path.join(self.nexus_path, '{}_masked.nxs'.format(name)) mask.masking(nexus_file_correctDet_path, self.masked_template, masked_file_path) else: masked_file_path = nexus_file_correctDet_path binning = self.binning # reduction from nexus event file d_sim, I_sim, error = red.mantid_reduction(masked_file_path, binning) plt.figure() plt.errorbar(d_sim, I_sim, error) plt.xlabel('d_spacing ()') plt.show() np.save( os.path.join(self.result_path, 'I_d_{sample}.npy'.format(sample=name)), [d_sim, I_sim, error]) return (d_sim, I_sim, error)
def test_conver2nxs(): cx.create_nexus(datadir, saved_file_name, template) rot.detector_position_for_reduction(saved_file_name, conf, SNAP_definition_file, saved_file_name)
def diffraction_pattern_calculation(self, params=[20, 20]): max_coll_len, chann_size = params if self.coll_sim: create_collimator_geometry(channel_size=chann_size, max_coll_len=max_coll_len, Snap_angle=self.Snap_angle, detector_angles=[-45]) name = "length_%s-dist_%s" % (max_coll_len, chann_size) else: name = self.sampleassembly_fileName simdir = os.path.join(parent_dir, "out/%s" % (name)) instr = os.path.join(libpath, 'myinstrument.py') # scattered_neutrons ='clampcellSi_scattered-neutrons_1e9_det-50_105_new' # scatterer = {(sampleassembly_fileName, 'shapeColl', 'coll_geometry', 'H', 'xyz'), # } # # sa.makeSAXML(sampleassembly_fileName, scatterer) # sample= 'collimator_plastic' run_script.run_mpi(instr, simdir, beam=self.source_neutrons(), ncount=self.ncount, nodes=20, angleMon1=-50., angleMon2=105., sample=self.sampleassembly_fileName, sourceTosample_x=self.sourceTosample_x, sourceTosample_y=self.sourceTosample_y, sourceTosample_z=self.sourceTosample_z, detector_size=0.45, overwrite_datafiles=True) template = os.path.join(mantid_path, 'template.nxs') nexus_file_path = os.path.join(mantid_path, '{}.nxs'.format(name)) det2nxs.create_nexus(simdir, nexus_file_path, template) SNAP_definition_file = os.path.join(mantid_path, 'SNAP_virtual_Definition.xml') nexus_file_correctDet_path = os.path.join( mantid_path, '{}_correctDet.nxs'.format(name)) rot.detector_position_for_reduction(nexus_file_path, conf, SNAP_definition_file, nexus_file_correctDet_path) if self.masking: masked_file_path = os.path.join(mantid_path, '{}_masked.nxs'.format(name)) masked_template_path = os.path.join( mantid_path, '{}'.format(self.masked_template)) mask.masking(nexus_file_correctDet_path, masked_template_path, masked_file_path) else: masked_file_path = nexus_file_correctDet_path binning = [0.5, 0.01, 4.] d_sim, I_sim, error = red.mantid_reduction(masked_file_path, binning) plt.figure() plt.errorbar(d_sim, I_sim, error) plt.xlabel('d_spacing ()') plt.show() np.save( os.path.join(result_path, 'I_d_{sample}.npy'.format(sample=name)), [d_sim, I_sim, error]) return (d_sim, I_sim, error)
def diffraction_pattern_calculation(self, params=[501.9, 444.5]): channel_len, coll_front_end_from_center = params if self.coll_sim: create_collimator_geometry( coll_front_end_from_center=coll_front_end_from_center, chanel_length=channel_len, Snap_angle=self.Snap_angle, detector_angles=[150]) #150 name = "length_%s-dist_%s" % (channel_len, coll_front_end_from_center) else: name = self.sampleassembly_fileName simdir = os.path.join(parent_dir, "out/%s" % (name)) # instr = os.path.join(libpath, 'myinstrument_multipleDet.py') instr = os.path.join(libpath, 'myinstrument.py') # scattered_neutrons ='clampcellSi_scattered-neutrons_1e9_det-50_105_new' # scatterer = {(sampleassembly_fileName, 'shapeColl', 'coll_geometry', 'H', 'xyz'), # } # # sa.makeSAXML(sampleassembly_fileName, scatterer) # sample= 'collimator_plastic' import shutil if os.path.exists(simdir): shutil.rmtree(simdir) # run_script.run_mpi(instr, simdir, # beam=self.source_neutrons(), ncount=self.ncount, # nodes=20, angleMons=self.angleMons, # sample=self.sampleassembly_fileName, # sourceTosample_x=self.sourceTosample_x, sourceTosample_y=self.sourceTosample_y, # sourceTosample_z=self.sourceTosample_z, sampleTodetector_z=self.sampleTodetector_z, # detector_width=self.detector_width, detector_height=self.detector_height, # number_pixels_in_height=self.number_pixels_in_height, # number_of_box_in_height=self.number_of_box_in_height, # number_pixels_in_width=self.number_pixels_in_width, # number_of_box_in_width= self.number_of_box_in_width, # number_detectors=self.number_of_detectors, # overwrite_datafiles=True) run_script.run_mpi( instr, simdir, beam=self.source_neutrons(), ncount=self.ncount, nodes=20, angleMon1=150, sample=self.sampleassembly_fileName, sourceTosample_x=self.sourceTosample_x, sourceTosample_y=self.sourceTosample_y, sourceTosample_z=self.sourceTosample_z, sampleTodetector_z=self.sampleTodetector_z, detector_width=self.detector_width, detector_height=self.detector_height, number_pixels_in_height=self.number_pixels_in_height, number_of_box_in_height=self.number_of_box_in_height, number_pixels_in_width=self.number_pixels_in_width, number_of_box_in_width=self.number_of_box_in_width, multiple_detectors=False, overwrite_datafiles=True) SNAP_definition_file = os.path.join(mantid_path, 'SNAPOneDet_virtual_definition.xml' ) # Vulcan_virtual_Definition.xml template = os.path.join( mantid_path, 'template_snapOneDet.nxs') # vulcan_template.nxs nxs_template.create(SNAP_definition_file, ntotpixels=self.number_of_total_DetectorPixels, outpath=template, pulse_time_end=1e5) nexus_file_path = os.path.join(mantid_path, '{}.nxs'.format(name)) det2nxs.create_nexus(simdir, nexus_file_path, template, N=self.number_of_total_DetectorPixels) nexus_file_correctDet_path = os.path.join( mantid_path, '{}_correctDet.nxs'.format(name)) conf = Conf(self.moderatorTosample_z, self.angleMons, self.sampleTodetector_z) rot.detector_position_for_reduction(nexus_file_path, conf, SNAP_definition_file, nexus_file_correctDet_path) if self.masking: masked_file_path = os.path.join(mantid_path, '{}_masked.nxs'.format(name)) masked_template_path = os.path.join( mantid_path, '{}'.format(self.masked_template)) mask.masking(nexus_file_correctDet_path, masked_template_path, masked_file_path) else: masked_file_path = nexus_file_correctDet_path binning = [0.5, 0.01, 4.] d_sim, I_sim, error = red.mantid_reduction(masked_file_path, binning) plt.figure() plt.errorbar(d_sim, I_sim, error) plt.xlabel('d_spacing ()') plt.show() np.save( os.path.join(result_path, 'I_d_{sample}.npy'.format(sample=name)), [d_sim, I_sim, error]) return (d_sim, I_sim, error)