C2 = Crystal(a2_real, b2_real, c2_real, symbol) C2.rotate_around_origin(rot_axis, rot_ang) assert np.allclose(C2.get_U(), C.get_U()) C2.rotate_around_origin(col(perturb_rot_axis), perturb_rot_ang) # Setup the simulation and create a realistic image # with background and noise # <><><><><><><><><><><><><><><><><><><><><><><><><> nbcryst = NBcrystal() nbcryst.dxtbx_crystal = C # simulate ground truth nbcryst.thick_mm = 0.1 nbcryst.isotropic_ncells = False if "eta" in args.perturb: nbcryst.n_mos_domains = 1000 ETA_ABC_GT = args.eta nbcryst.anisotropic_mos_spread_deg = ETA_ABC_GT NCELLS_GT = 12, 12, 11 else: NCELLS_GT = 12, 12, 11 nbcryst.Ncells_abc = NCELLS_GT SIM = SimData(use_default_crystal=True) #SIM.detector = SimData.simple_detector(150, 0.1, (513, 512)) if "eta" in args.perturb: shape = 513 * 3, 512 * 3 #detdist = 70 else: shape = 513, 512 detdist = 150 SIM.detector = SimData.simple_detector(detdist, 0.1, shape) SIM.crystal = nbcryst
def multipanel_sim( CRYSTAL, DETECTOR, BEAM, Famp, energies, fluxes, background_wavelengths=None, background_wavelength_weights=None, background_total_flux=None, background_sample_thick_mm=None, density_gcm3=1, molecular_weight=18, cuda=False, oversample=0, Ncells_abc=(50, 50, 50), mos_dom=1, mos_spread=0, mosaic_method="double_uniform", mos_aniso=None, beamsize_mm=0.001, crystal_size_mm=0.01, verbose=0, default_F=0, interpolate=0, profile="gauss", spot_scale_override=None, show_params=False, time_panels=False, add_water = False, add_air=False, water_path_mm=0.005, air_path_mm=0, adc_offset=0, readout_noise=3, psf_fwhm=0, gain=1, mosaicity_random_seeds=None, include_background=True, mask_file="",skip_numpy=False,relevant_whitelist_order=None): from simtbx.nanoBragg.nanoBragg_beam import NBbeam from simtbx.nanoBragg.nanoBragg_crystal import NBcrystal from simtbx.nanoBragg.sim_data import SimData from simtbx.nanoBragg.utils import get_xray_beams from scitbx.array_family import flex from scipy import constants import numpy as np ENERGY_CONV = 10000000000.0 * constants.c * constants.h / constants.electron_volt assert cuda # disallow the default nbBeam = NBbeam() nbBeam.size_mm = beamsize_mm nbBeam.unit_s0 = BEAM.get_unit_s0() wavelengths = ENERGY_CONV / np.array(energies) nbBeam.spectrum = list(zip(wavelengths, fluxes)) nbCrystal = NBcrystal(False) nbCrystal.dxtbx_crystal = CRYSTAL #nbCrystal.miller_array = None # use the gpu_channels_singleton mechanism instead nbCrystal.Ncells_abc = Ncells_abc nbCrystal.symbol = CRYSTAL.get_space_group().info().type().lookup_symbol() nbCrystal.thick_mm = crystal_size_mm nbCrystal.xtal_shape = profile nbCrystal.n_mos_domains = mos_dom nbCrystal.mos_spread_deg = mos_spread nbCrystal.anisotropic_mos_spread_deg = mos_aniso pid = 0 # remove the loop, use C++ iteration over detector panels use_exascale_api = True if use_exascale_api: S = SimData(use_default_crystal = False) S.detector = DETECTOR S.beam = nbBeam S.crystal = nbCrystal S.panel_id = pid S.add_air = add_air S.air_path_mm = air_path_mm S.add_water = add_water S.water_path_mm = water_path_mm S.readout_noise = readout_noise S.gain = gain S.psf_fwhm = psf_fwhm S.include_noise = False S.Umats_method = dict(double_random=0, double_uniform=5)[mosaic_method] if mosaicity_random_seeds is not None: S.mosaic_seeds = mosaicity_random_seeds S.instantiate_nanoBragg(verbose=verbose, oversample=oversample, interpolate=interpolate, device_Id=Famp.get_deviceID(),default_F=default_F, adc_offset=adc_offset) SIM = S.D # the nanoBragg instance assert Famp.get_deviceID()==SIM.device_Id if spot_scale_override is not None: SIM.spot_scale = spot_scale_override assert Famp.get_nchannels() == 1 # non-anomalous scenario from simtbx.gpu import exascale_api gpu_simulation = exascale_api(nanoBragg = SIM) gpu_simulation.allocate() # presumably done once for each image from simtbx.gpu import gpu_detector as gpud gpu_detector = gpud(deviceId=SIM.device_Id, detector=DETECTOR, beam=BEAM) gpu_detector.each_image_allocate() # revisit the allocate cuda for overlap with detector, sync up please x = 0 # only one energy channel if mask_file is "": # all-pixel kernel P = Profiler("%40s"%"from gpu amplitudes cuda") gpu_simulation.add_energy_channel_from_gpu_amplitudes( x, Famp, gpu_detector) elif type(mask_file) is flex.bool: # 1D bool array, flattened from ipanel, islow, ifast P = Profiler("%40s"%"from gpu amplitudes cuda with bool mask") gpu_simulation.add_energy_channel_mask_allpanel( channel_number = x, gpu_amplitudes = Famp, gpu_detector = gpu_detector, pixel_active_mask_bools = mask_file ) elif type(mask_file) is flex.int: # precalculated active_pixel_list P = Profiler("%40s"%"from gpu amplitudes cuda w/int whitelist") gpu_simulation.add_energy_channel_mask_allpanel( channel_number = x, gpu_amplitudes = Famp, gpu_detector = gpu_detector, pixel_active_list_ints = mask_file ) else: assert type(mask_file) is str from LS49.adse13_187.adse13_221.mask_utils import mask_from_file boolean_mask = mask_from_file(mask_file) P = Profiler("%40s"%"from gpu amplitudes cuda with file mask") gpu_simulation.add_energy_channel_mask_allpanel( x, Famp, gpu_detector, boolean_mask ) TIME_BRAGG = time()-P.start_el per_image_scale_factor = 1. gpu_detector.scale_in_place(per_image_scale_factor) # apply scale directly on GPU if include_background: t_bkgrd_start = time() SIM.beamsize_mm = beamsize_mm wavelength_weights = np.array(background_wavelength_weights) weights = wavelength_weights / wavelength_weights.sum() * background_total_flux spectrum = list(zip(background_wavelengths, weights)) xray_beams = get_xray_beams(spectrum, BEAM) SIM.xray_beams = xray_beams SIM.Fbg_vs_stol = water SIM.flux=sum(weights) SIM.amorphous_sample_thick_mm = background_sample_thick_mm SIM.amorphous_density_gcm3 = density_gcm3 SIM.amorphous_molecular_weight_Da = molecular_weight gpu_simulation.add_background(gpu_detector) TIME_BG = time()-t_bkgrd_start else: TIME_BG=0. if skip_numpy: P = Profiler("%40s"%"get short whitelist values") whitelist_only = gpu_detector.get_whitelist_raw_pixels(relevant_whitelist_order) # whitelist_only, flex_double pixel values # relevant_whitelist_order, flex.size_t detector addresses assert len(whitelist_only) == len(relevant_whitelist_order) # guard against shoebox overlap bug # when shoeboxes overlap, the overlapped pixels should be simulated once for each parent shoebox P = Profiler("%40s"%"each image free cuda") gpu_detector.each_image_free() return whitelist_only, TIME_BG, TIME_BRAGG, S.exascale_mos_blocks or None packed_numpy = gpu_detector.get_raw_pixels() gpu_detector.each_image_free() return packed_numpy.as_numpy_array(), TIME_BG, TIME_BRAGG, S.exascale_mos_blocks or None
a_real = M * col(a_real) b_real = M * col(b_real) c_real = M * col(c_real) C = Crystal(a_real, b_real, c_real, symbol) C.rotate_around_origin(rot_axis, rot_ang) # Setup the simulation and create a realistic image # with background and noise # <><><><><><><><><><><><><><><><><><><><><><><><><> nbcryst = NBcrystal(init_defaults=True) nbcryst.dxtbx_crystal = C # simulate ground truth nbcryst.thick_mm = 0.1 nbcryst.Ncells_abc = Ncells_gt # ground truth Ncells nbcryst.mos_spread_deg = MOS_SPREAD if args.aniso is not None: nbcryst.anisotropic_mos_spread_deg = ANISO_MOS_SPREAD assert nbcryst.has_anisotropic_mosaicity else: assert not nbcryst.has_anisotropic_mosaicity nbcryst.n_mos_domains = N_MOS_DOMAINS nbcryst.miller_array = miller_array_GT print("Ground truth ncells = %f" % (nbcryst.Ncells_abc[0])) # ground truth detector DET_gt = SimData.simple_detector(150, 0.177, (600, 600)) # initialize the simulator SIM = SimData() if args.aniso is None: SIM.Umats_method = 2