def remake_intensities_at_energy(energy, FE1_model, FE2_model): from LS49.sim.util_fmodel import gen_fmodel W2 = 12398.425 / float(energy) GF = gen_fmodel(resolution=1.7, pdb_text=local_data.get("pdb_lines"), algorithm="fft", wavelength=W2) GF.set_k_sol(0.435) GF.params2.fmodel.b_sol = 46. GF.params2.structure_factors_accuracy.grid_resolution_factor = 1 / 5. GF.params2.mask.grid_step_factor = 10. GF.reset_wavelength(W2) GF.reset_specific_at_wavelength(label_has="FE1", tables=FE1_model, newvalue=W2) GF.reset_specific_at_wavelength(label_has="FE2", tables=FE2_model, newvalue=W2) GF.make_P1_primitive() W2_reduced = GF.get_intensities() # Einsle paper: Reduced form has # buried irons, FE1, in Fe(III) state (absorption at higher energy, oxidized) # surface iron, FE2, in Fe(II) state (absorption at lower energy, reduced) W2i = W2_reduced.indices() intensity_dict = {} for iw in range(len(W2i)): intensity_dict[W2_reduced.indices()[iw]] = W2_reduced.data()[iw] return intensity_dict
def create_cpu_channels(utilize): wavelength_A = 1.74 # general ballpark X-ray wavelength in Angstroms wavlen = flex.double([12398.425 / (7070.5 + w) for w in range(utilize)]) direct_algo_res_limit = 1.7 local_data = data() # later put this through broadcast GF = gen_fmodel(resolution=direct_algo_res_limit, pdb_text=local_data.get("pdb_lines"), algorithm="fft", wavelength=wavelength_A) GF.set_k_sol(0.435) GF.make_P1_primitive() # Generating sf for my wavelengths sfall_channels = {} for x in range(len(wavlen)): GF.reset_wavelength(wavlen[x]) GF.reset_specific_at_wavelength( label_has="FE1", tables=local_data.get("Fe_oxidized_model"), newvalue=wavlen[x]) GF.reset_specific_at_wavelength( label_has="FE2", tables=local_data.get("Fe_reduced_model"), newvalue=wavlen[x]) sfall_channels[x] = GF.get_amplitudes() print("CPU channel", x) return sfall_channels
def get_C2_pdb_structure(resolution, wavelength): from LS49.sim.util_fmodel import gen_fmodel pdb_lines = open("./1m2a.pdb", "r").read() return gen_fmodel(resolution=resolution, pdb_text=pdb_lines, wavelength=wavelength, algorithm="fft")
def gen_fmodel_adapt(self): direct_algo_res_limit = 1.7 self.GF = gen_fmodel(resolution=direct_algo_res_limit,pdb_text=data( ).get("pdb_lines"),algorithm="fft",wavelength=7122) self.CB_OP_C_P = self.GF.xray_structure.change_of_basis_op_to_primitive_setting() # from C to P, for ersatz model self.GF.set_k_sol(0.435) self.GF.make_P1_primitive() self.sfall_main = self.GF.get_amplitudes()
def sfall_prepare(simparams=None, fpdb=None, spectra=None): sfall_cluster = {} fmodel_generator = gen_fmodel(resolution=simparams.direct_algo_res_limit,\ pdb_text=data(fpdb).get("pdb_lines"), algorithm=simparams.fmodel_algorithm, wavelength=simparams.wavelength_A) fmodel_generator.set_k_sol(simparams.k_sol) fmodel_generator.make_P1_primitive() sfall_cluster["main"] = fmodel_generator.get_amplitudes().copy() return sfall_cluster
def channel_wavelength_fmodel(create): from LS49.spectra.generate_spectra import spectra_simulation SS = spectra_simulation() iterator = SS.generate_recast_renormalized_images(20, energy=7120., total_flux=1e12) wavlen, flux, wavelength_A = next( iterator) # list of lambdas, list of fluxes, average wavelength direct_algo_res_limit = 1.7 from LS49.sim.util_fmodel import gen_fmodel GF = gen_fmodel(resolution=direct_algo_res_limit, pdb_text=get_pdb_lines(), algorithm="fft", wavelength=wavelength_A) GF.set_k_sol(0.435) GF.make_P1_primitive() for x in range(10, len(flux), 5): print("+++++++++++++++++++++++++++++++++++++++ Wavelength", x) GF.reset_wavelength(wavelength_A) GF.reset_specific_at_wavelength(label_has="FE1", tables=Fe_oxidized_model, newvalue=wavelength_A) GF.reset_specific_at_wavelength(label_has="FE2", tables=Fe_reduced_model, newvalue=wavelength_A) sfall_channel = GF.get_amplitudes() filename = "sf_reference_channel_%s" % ("%03d" % x) if create: # write the reference for the first time cPickle.dump( sfall_channel, open(os.path.join(ls49_big_data, "reference", filename), "wb"), cPickle.HIGHEST_PROTOCOL) else: # read the reference and assert sameness to sfall_channel print(os.path.join(ls49_big_data, "reference", filename)) if six.PY3: sfall_ref = cPickle.load(open( os.path.join(ls49_big_data, "reference", filename), "rb"), encoding="bytes") fix_unpickled_attributes(sfall_ref) else: sfall_ref = cPickle.load( open(os.path.join(ls49_big_data, "reference", filename), "rb")) T = sfall_channel S = sfall_ref assert S.space_group() == T.space_group() assert S.unit_cell().parameters() == T.unit_cell().parameters() assert S.indices() == T.indices() assert S.data() == T.data()
def single_wavelength_fmodel(create): from LS49.spectra.generate_spectra import spectra_simulation SS = spectra_simulation() iterator = SS.generate_recast_renormalized_images(20, energy=7120., total_flux=1e12) wavlen, flux, wavelength_A = next( iterator) # list of lambdas, list of fluxes, average wavelength direct_algo_res_limit = 1.7 from LS49.sim.util_fmodel import gen_fmodel for flag in [True, False]: GF = gen_fmodel(resolution=direct_algo_res_limit, pdb_text=get_pdb_lines(), algorithm="fft", wavelength=wavelength_A) GF.set_k_sol(0.435) if flag: GF.make_P1_primitive() sfall_main = GF.get_amplitudes() sfall_main.show_summary(prefix="Amplitudes used ") if create: # write the reference for the first time cPickle.dump( sfall_main, open( os.path.join(ls49_big_data, "reference", "sf_reference_cb_to_P1_%s" % (str(flag))), "wb"), cPickle.HIGHEST_PROTOCOL) else: # read the reference and assert sameness to sfall_main if six.PY3: sfall_ref = cPickle.load(open( os.path.join(ls49_big_data, "reference", "sf_reference_cb_to_P1_%s" % (str(flag))), "rb"), encoding="bytes") from LS49.tests.tst_sf_energies import fix_unpickled_attributes fix_unpickled_attributes(sfall_ref) else: sfall_ref = cPickle.load( open( os.path.join(ls49_big_data, "reference", "sf_reference_cb_to_P1_%s" % (str(flag))), "rb")) T = sfall_main S = sfall_ref assert S.space_group() == T.space_group() assert S.unit_cell().parameters() == T.unit_cell().parameters() assert S.indices() == T.indices() assert S.data() == T.data() print()
def remake_intensities_at_energy(energy): from LS49.sim.util_fmodel import gen_fmodel from LS49.sim.step5_pad import pdb_lines, Fe_oxidized_model, Fe_reduced_model W2 = 12398.425 / float(energy) GF = gen_fmodel(resolution=1.7, pdb_text=pdb_lines, algorithm="fft", wavelength=W2) GF.set_k_sol(0.435) GF.params2.fmodel.b_sol = 46. GF.params2.structure_factors_accuracy.grid_resolution_factor = 1 / 5. GF.params2.mask.grid_step_factor = 10. GF.reset_wavelength(W2) GF.reset_specific_at_wavelength(label_has="FE1", tables=Fe_oxidized_model, newvalue=W2) GF.reset_specific_at_wavelength(label_has="FE2", tables=Fe_reduced_model, newvalue=W2) W2_reduced = GF.get_intensities() # Einsle paper: Reduced form has # buried irons, FE1, in Fe(III) state (absorption at higher energy, oxidized) # surface iron, FE2, in Fe(II) state (absorption at lower energy, reduced) W2i = W2_reduced.indices() with (open("confirm_intensities_flex_array.data", "w")) as F: for iw in range(len(W2i)): print("%20s, %10.2f" % (W2_reduced.indices()[iw], W2_reduced.data()[iw]), file=F) intensity_dict = {} for iw in range(len(W2i)): intensity_dict[W2_reduced.indices()[iw]] = W2_reduced.data()[iw] return intensity_dict with (open("confirm_intensities_dict.pickle", "wb")) as F: pickle.dump(intensity_dict, F, pickle.HIGHEST_PROTOCOL) with (open("confirm_C2_sfall_7122_amplitudes.pickle", "wb")) as F: pickle.dump(GF.get_amplitudes(), F, pickle.HIGHEST_PROTOCOL) GF.make_P1_primitive() with (open("confirm_sfall_P1_7122_amplitudes.pickle", "wb")) as F: pickle.dump(GF.get_amplitudes(), F, pickle.HIGHEST_PROTOCOL)
def __init__(self, mosaic_domains=25, mosaic_spread_deg=0.05): local_data = data() direct_algo_res_limit = 1.7 wavelength_A = 1.3 # Angstroms, dummy value GF = gen_fmodel(resolution=direct_algo_res_limit, pdb_text=local_data.get("pdb_lines"), algorithm="fft", wavelength=wavelength_A) GF.set_k_sol(0.435) GF.make_P1_primitive() self.sfall_main = GF.get_amplitudes() SIM = nanoBragg( detpixels_slowfast=(3000, 3000), pixel_size_mm=0.11, Ncells_abc=(10, 10, 10), # workaround for problem with wavelength array, specify it separately in constructor. wavelength_A=wavelength_A, verbose=0) SIM.mosaic_spread_deg = mosaic_spread_deg # interpreted by UMAT_nm as a half-width stddev SIM.mosaic_domains = mosaic_domains # mosaic_domains setter must come after mosaic_spread_deg setter UMAT_nm = flex.mat3_double() mersenne_twister = flex.mersenne_twister(seed=0) scitbx.random.set_random_seed(1234) rand_norm = scitbx.random.normal_distribution( mean=0, sigma=SIM.mosaic_spread_deg * math.pi / 180.) g = scitbx.random.variate(rand_norm) mosaic_rotation = g(SIM.mosaic_domains) for m in mosaic_rotation: site = col(mersenne_twister.random_double_point_on_sphere()) UMAT_nm.append( site.axis_and_angle_as_r3_rotation_matrix(m, deg=False)) SIM.set_mosaic_blocks(UMAT_nm) self.SIM = SIM
def sfall_prepare(simparams=None, fpdb=None, spectra=None): sfall_cluster = {} fmodel_generator = gen_fmodel(resolution=simparams.direct_algo_res_limit,\ pdb_text=data(fpdb).get("pdb_lines"), algorithm=simparams.fmodel_algorithm, wavelength=simparams.wavelength_A) fmodel_generator.set_k_sol(simparams.k_sol) fmodel_generator.make_P1_primitive() sfall_cluster["main"] = fmodel_generator.get_amplitudes().copy() if simparams.quick: sfall_cluster[0] = fmodel_generator.get_amplitudes().copy() return sfall_cluster iterator = spectra.generate_recast_renormalized_image(image=0, energy=simparams.energy_eV, total_flux=simparams.flux) wavlen, flux, real_wavelength_A = next(iterator) for x in range(len(flux)): print("## processing pdb with wavelength_A: ", wavlen[x], fpdb) fmodel_generator.reset_wavelength(wavlen[x]) sfall_channel = fmodel_generator.get_amplitudes() sfall_cluster[ x ] = sfall_channel.copy() iterator = None return sfall_cluster
deltafast = flex.double() deltaslow = flex.double() from LS49.sim.step5_pad import data local_data = data() Fe_oxidized_model = local_data.get("Fe_oxidized_model") Fe_reduced_model = local_data.get("Fe_reduced_model") Fe_metallic_model = local_data.get("Fe_metallic_model") from LS49.sim.util_fmodel import gen_fmodel direct_algo_res_limit = 2.0 eV_to_angstrom = 12398.425 wavelength_A = eV_to_angstrom / 7122.0 GF = gen_fmodel(resolution=direct_algo_res_limit, pdb_text=local_data.get("pdb_lines"), algorithm="fft", wavelength=wavelength_A) GF.set_k_sol(0.435) GF.make_P1_primitive() sfall_main = GF.get_amplitudes() GF.reset_wavelength(wavelength_A) GF.reset_specific_at_wavelength(label_has="FE1", tables=local_data.get("Fe_oxidized_model"), newvalue=wavelength_A) GF.reset_specific_at_wavelength(label_has="FE2", tables=local_data.get("Fe_reduced_model"), newvalue=wavelength_A) print("USING scatterer-specific energy-dependent scattering factors") OX = sfall_channel_oxidized = GF.get_intensities() GF.reset_specific_at_wavelength(label_has="FE1",
def run_LY99_batch(test_without_mpi=False): params, options = parse_input() log_by_rank = bool(int(os.environ.get("LOG_BY_RANK", 0))) rank_profile = bool(int(os.environ.get("RANK_PROFILE", 1))) if log_by_rank: import io, sys if rank_profile: import cProfile pr = cProfile.Profile() pr.enable() if test_without_mpi: from LS49.adse13_196.mock_mpi import mpiEmulator MPI = mpiEmulator() else: from libtbx.mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() import omptbx workaround_nt = int(os.environ.get("OMP_NUM_THREADS", 1)) omptbx.omp_set_num_threads(workaround_nt) N_total = int(os.environ["N_SIM"]) # number of items to simulate N_stride = size # total number of worker tasks print("hello from rank %d of %d" % (rank, size), "with omp_threads=", omp_get_num_procs()) import datetime start_comp = time() # now inside the Python imports, begin energy channel calculation wavelength_A = 1.74 # general ballpark X-ray wavelength in Angstroms wavlen = flex.double([12398.425 / (7070.5 + w) for w in range(100)]) direct_algo_res_limit = 1.7 local_data = data() # later put this through broadcast GF = gen_fmodel(resolution=direct_algo_res_limit, pdb_text=local_data.get("pdb_lines"), algorithm="fft", wavelength=wavelength_A) GF.set_k_sol(0.435) GF.make_P1_primitive() # Generating sf for my wavelengths sfall_channels = {} for x in range(len(wavlen)): if rank > len(wavlen): break if x % size != rank: continue GF.reset_wavelength(wavlen[x]) GF.reset_specific_at_wavelength( label_has="FE1", tables=local_data.get("Fe_oxidized_model"), newvalue=wavlen[x]) GF.reset_specific_at_wavelength( label_has="FE2", tables=local_data.get("Fe_reduced_model"), newvalue=wavlen[x]) sfall_channels[x] = GF.get_amplitudes() reports = comm.gather(sfall_channels, root=0) if rank == 0: sfall_channels = {} for report in reports: sfall_channels.update(report) comm.barrier() print( rank, time(), "finished with the calculation of channels, now construct single broadcast" ) if rank == 0: print("Rank 0 time", datetime.datetime.now()) from LS49.spectra.generate_spectra import spectra_simulation from LS49.adse13_196.revapi.LY99_pad import microcrystal print("hello2 from rank %d of %d" % (rank, size)) SS = spectra_simulation() C = microcrystal( Deff_A=4000, length_um=4., beam_diameter_um=1.0) # assume smaller than 10 um crystals from LS49 import legacy_random_orientations random_orientations = legacy_random_orientations(N_total) transmitted_info = dict(spectra=SS, crystal=C, sfall_info=sfall_channels, random_orientations=random_orientations) else: transmitted_info = None transmitted_info = comm.bcast(transmitted_info, root=0) comm.barrier() parcels = list(range(rank, N_total, N_stride)) print(rank, time(), "finished with single broadcast, now set up the rank logger") if log_by_rank: expand_dir = os.path.expandvars(params.logger.outdir) log_path = os.path.join(expand_dir, "rank_%d.log" % rank) error_path = os.path.join(expand_dir, "rank_%d.err" % rank) #print("Rank %d redirecting stdout/stderr to"%rank, log_path, error_path) sys.stdout = io.TextIOWrapper(open(log_path, 'ab', 0), write_through=True) sys.stderr = io.TextIOWrapper(open(error_path, 'ab', 0), write_through=True) print( rank, time(), "finished with the rank logger, now construct the GPU cache container") import random gpu_instance = get_exascale("gpu_instance", params.context) gpu_energy_channels = get_exascale("gpu_energy_channels", params.context) gpu_run = gpu_instance(deviceId=rank % int(os.environ.get("DEVICES_PER_NODE", 1))) gpu_channels_singleton = gpu_energy_channels( deviceId=gpu_run.get_deviceID()) # singleton will instantiate, regardless of gpu, device count, or exascale API comm.barrier() while len(parcels) > 0: idx = random.choice(parcels) cache_time = time() print("idx------start-------->", idx, "rank", rank, time()) # if rank==0: os.system("nvidia-smi") tst_one( image=idx, spectra=transmitted_info["spectra"], crystal=transmitted_info["crystal"], random_orientation=transmitted_info["random_orientations"][idx], sfall_channels=transmitted_info["sfall_info"], gpu_channels_singleton=gpu_channels_singleton, rank=rank, params=params) parcels.remove(idx) print("idx------finis-------->", idx, "rank", rank, time(), "elapsed", time() - cache_time) comm.barrier() del gpu_channels_singleton # avoid Kokkos allocation "device_Fhkl" being deallocated after Kokkos::finalize was called print("Overall rank", rank, "at", datetime.datetime.now(), "seconds elapsed after srun startup %.3f" % (time() - start_elapse)) print("Overall rank", rank, "at", datetime.datetime.now(), "seconds elapsed after Python imports %.3f" % (time() - start_comp)) if rank_profile: pr.disable() pr.dump_stats("cpu_%d.prof" % rank)
def run_sim2smv(prefix, crystal, spectra, rotation, rank): local_data = data() smv_fileout = prefix + ".img" if not write_safe(smv_fileout): print("File %s already exists, skipping in rank %d" % (smv_fileout, rank)) return direct_algo_res_limit = 1.7 wavlen, flux, wavelength_A = next( spectra) # list of lambdas, list of fluxes, average wavelength assert wavelength_A > 0 GF = gen_fmodel(resolution=direct_algo_res_limit, pdb_text=local_data.get("pdb_lines"), algorithm="fft", wavelength=wavelength_A) GF.set_k_sol(0.435) GF.make_P1_primitive() sfall_main = GF.get_amplitudes() # use crystal structure to initialize Fhkl array sfall_main.show_summary(prefix="Amplitudes used ") N = crystal.number_of_cells(sfall_main.unit_cell()) mosaic_spread_deg = 0.05 mosaic_domains = 25 channel_sim = ChannelSimulator(rotation=rotation, sfall_main=sfall_main, N=N, mosaic_spread_deg=mosaic_spread_deg, mosaic_domains=mosaic_domains) for x in range(len(flux)): P = Profiler("nanoBragg Python and C++ rank %d" % (rank)) print("+++++++++++++++++++++++++++++++++++++++ Wavelength", x) channel_sim.add_channel_pixels(wavlen[x], flux[x], rank, algo=ADD_SPOTS_ALGORITHM) # whats this ? CHDBG_singleton.extract(channel_no=x, data=channel_sim.raw_pixels) del P SIM = channel_sim.SIM # FIXME: this hsould already be done by kernel... SIM.raw_pixels = channel_sim.raw_pixels * crystal.domains_per_crystal bg = flex.vec2_double([(0, 2.57), (0.0365, 2.58), (0.07, 2.8), (0.12, 5), (0.162, 8), (0.2, 6.75), (0.18, 7.32), (0.216, 6.75), (0.236, 6.5), (0.28, 4.5), (0.3, 4.3), (0.345, 4.36), (0.436, 3.77), (0.5, 3.17)]) SIM.Fbg_vs_stol = bg SIM.amorphous_sample_thick_mm = 0.1 SIM.amorphous_density_gcm3 = 1 SIM.amorphous_molecular_weight_Da = 18 SIM.flux = 1e12 SIM.beamsize_mm = 0.003 # square (not user specified) SIM.exposure_s = 1.0 # multiplies flux x exposure SIM.add_background() # rough approximation to air bg = flex.vec2_double([(0, 14.1), (0.045, 13.5), (0.174, 8.35), (0.35, 4.78), (0.5, 4.22)]) SIM.Fbg_vs_stol = bg #SIM.amorphous_sample_thick_mm = 35 # between beamstop and collimator SIM.amorphous_sample_thick_mm = 10 # between beamstop and collimator SIM.amorphous_density_gcm3 = 1.2e-3 SIM.amorphous_sample_molecular_weight_Da = 28 # nitrogen = N2 print("amorphous_sample_size_mm=", SIM.amorphous_sample_size_mm) print("amorphous_sample_thick_mm=", SIM.amorphous_sample_thick_mm) print("amorphous_density_gcm3=", SIM.amorphous_density_gcm3) print("amorphous_molecular_weight_Da=", SIM.amorphous_molecular_weight_Da) SIM.add_background() #apply beamstop mask here # set this to 0 or -1 to trigger automatic radius. could be very slow with bright images # settings for CCD SIM.detector_psf_kernel_radius_pixels = 5 #SIM.detector_psf_fwhm_mm=0.08; #SIM.detector_psf_type=shapetype.Fiber # rayonix=Fiber, CSPAD=None (or small Gaussian) SIM.detector_psf_type = shapetype.Unknown # for CSPAD SIM.detector_psf_fwhm_mm = 0 #SIM.apply_psf() print("One pixel-->", SIM.raw_pixels[500000]) # at this point we scale the raw pixels so that the output array is on an scale from 0 to 50000. # that is the default behavior (intfile_scale<=0), otherwise it applies intfile_scale as a multiplier on an abs scale. print("quantum_gain=", SIM.quantum_gain) #defaults to 1. converts photons to ADU print("adc_offset_adu=", SIM.adc_offset_adu) print("detector_calibration_noise_pct=", SIM.detector_calibration_noise_pct) print("flicker_noise_pct=", SIM.flicker_noise_pct) print("readout_noise_adu=", SIM.readout_noise_adu ) # gaussian random number to add to every pixel (0 for PAD) # apply Poissonion correction, then scale to ADU, then adc_offset. # should be 10 for most Rayonix, Pilatus should be 0, CSPAD should be 0. print("detector_psf_type=", SIM.detector_psf_type) print("detector_psf_fwhm_mm=", SIM.detector_psf_fwhm_mm) print("detector_psf_kernel_radius_pixels=", SIM.detector_psf_kernel_radius_pixels) SIM.add_noise() #converts phtons to ADU. print("raw_pixels=", SIM.raw_pixels) extra = "PREFIX=%s;\nRANK=%d;\n" % (prefix, rank) SIM.to_smv_format_py(fileout=smv_fileout, intfile_scale=1, rotmat=True, extra=extra, gz=True) SIM.free_all()
if icount>100: return yield image["millers"][i] asu[image["millers"][i]]=1 #print ("CC>70%%: %20s %5.2f"%(image["millers"][i], # image["cc"][i])) if __name__=="__main__": M = flex.miller_index() for i in generate_100(): print (i) M.append(i) W1 = 12398.425/7110. W2 = 12398.425/7122. GF = gen_fmodel(resolution=1.9,pdb_text=pdb_lines,algorithm="fft",wavelength=W1) GF.set_k_sol(0.435) #GF.make_P1_primitive() GF.reset_wavelength(W1) GF.reset_specific_at_wavelength(label_has="FE1",tables=Fe_oxidized_model,newvalue=W1) GF.reset_specific_at_wavelength(label_has="FE2",tables=Fe_reduced_model,newvalue=W1) W1_oxidized = GF.get_intensities() GF.reset_wavelength(W1) GF.reset_specific_at_wavelength(label_has="FE1",tables=Fe_reduced_model,newvalue=W1) GF.reset_specific_at_wavelength(label_has="FE2",tables=Fe_reduced_model,newvalue=W1) W1_reduced = GF.get_intensities() GF.reset_wavelength(W2) GF.reset_specific_at_wavelength(label_has="FE1",tables=Fe_oxidized_model,newvalue=W2) GF.reset_specific_at_wavelength(label_has="FE2",tables=Fe_reduced_model,newvalue=W2) W2_oxidized = GF.get_intensities() GF.reset_wavelength(W2)
def run_sim2smv(prefix, crystal, spectra, rotation, rank, quick=False): local_data = data() smv_fileout = prefix + ".img" if quick is not True: if not write_safe(smv_fileout): print("File %s already exists, skipping in rank %d" % (smv_fileout, rank)) return direct_algo_res_limit = 1.7 wavlen, flux, wavelength_A = next( spectra) # list of lambdas, list of fluxes, average wavelength if quick: wavlen = flex.double([wavelength_A]) flux = flex.double([flex.sum(flux)]) print("Quick sim, lambda=%f, flux=%f" % (wavelength_A, flux[0])) GF = gen_fmodel(resolution=direct_algo_res_limit, pdb_text=local_data.get("pdb_lines"), algorithm="fft", wavelength=wavelength_A) GF.set_k_sol(0.435) GF.make_P1_primitive() sfall_main = GF.get_amplitudes() # use crystal structure to initialize Fhkl array sfall_main.show_summary(prefix="Amplitudes used ") N = crystal.number_of_cells(sfall_main.unit_cell()) #SIM = nanoBragg(detpixels_slowfast=(2000,2000),pixel_size_mm=0.11,Ncells_abc=(5,5,5),verbose=0) SIM = nanoBragg( detpixels_slowfast=(3000, 3000), pixel_size_mm=0.11, Ncells_abc=(N, N, N), # workaround for problem with wavelength array, specify it separately in constructor. wavelength_A=wavelength_A, verbose=0) SIM.adc_offset_adu = 0 # Do not offset by 40 SIM.adc_offset_adu = 10 # Do not offset by 40 import sys if len(sys.argv) > 2: SIM.seed = -int(sys.argv[2]) print("GOTHERE seed=", SIM.seed) if len(sys.argv) > 1: if sys.argv[1] == "random": SIM.randomize_orientation() SIM.mosaic_spread_deg = 0.05 # interpreted by UMAT_nm as a half-width stddev SIM.mosaic_domains = n_mosaic_domains # 77 seconds. With 100 energy points, 7700 seconds (2 hours) per image # 3000000 images would be 100000 hours on a 60-core machine (dials), or 11.4 years # using 2 nodes, 5.7 years. Do this at SLAC? NERSC? combination of all? # SLAC downtimes: Tues Dec 5 (24 hrs), Mon Dec 11 (72 hrs), Mon Dec 18 light use, 24 days # mosaic_domains setter must come after mosaic_spread_deg setter SIM.distance_mm = 141.7 UMAT_nm = flex.mat3_double() mersenne_twister = flex.mersenne_twister(seed=0) scitbx.random.set_random_seed(1234) rand_norm = scitbx.random.normal_distribution(mean=0, sigma=SIM.mosaic_spread_deg * math.pi / 180.) g = scitbx.random.variate(rand_norm) mosaic_rotation = g(SIM.mosaic_domains) for m in mosaic_rotation: site = col(mersenne_twister.random_double_point_on_sphere()) UMAT_nm.append(site.axis_and_angle_as_r3_rotation_matrix(m, deg=False)) SIM.set_mosaic_blocks(UMAT_nm) #SIM.detector_thick_mm = 0.5 # = 0 for Rayonix #SIM.detector_thicksteps = 1 # should default to 1 for Rayonix, but set to 5 for CSPAD #SIM.detector_attenuation_length_mm = default is silicon # get same noise each time this test is run SIM.seed = 1 SIM.oversample = 1 SIM.wavelength_A = wavelength_A SIM.polarization = 1 # this will become F000, marking the beam center SIM.default_F = 0 #SIM.missets_deg= (10,20,30) print("mosaic_seed=", SIM.mosaic_seed) print("seed=", SIM.seed) print("calib_seed=", SIM.calib_seed) print("missets_deg =", SIM.missets_deg) SIM.Fhkl = sfall_main print("Determinant", rotation.determinant()) Amatrix_rot = ( rotation * sqr(sfall_main.unit_cell().orthogonalization_matrix())).transpose() print("RAND_ORI", prefix, end=' ') for i in Amatrix_rot: print(i, end=' ') print() SIM.Amatrix_RUB = Amatrix_rot #workaround for failing init_cell, use custom written Amatrix setter print("unit_cell_Adeg=", SIM.unit_cell_Adeg) print("unit_cell_tuple=", SIM.unit_cell_tuple) Amat = sqr(SIM.Amatrix).transpose() # recovered Amatrix from SIM from cctbx import crystal_orientation Ori = crystal_orientation.crystal_orientation( Amat, crystal_orientation.basis_type.reciprocal) print("Python unit cell from SIM state", Ori.unit_cell()) # fastest option, least realistic #SIM.xtal_shape=shapetype.Tophat # RLP = hard sphere #SIM.xtal_shape=shapetype.Square # gives fringes SIM.xtal_shape = shapetype.Gauss # both crystal & RLP are Gaussian #SIM.xtal_shape=shapetype.Round # Crystal is a hard sphere # only really useful for long runs SIM.progress_meter = False # prints out value of one pixel only. will not render full image! #SIM.printout_pixel_fastslow=(500,500) #SIM.printout=True SIM.show_params() # flux is always in photons/s SIM.flux = 1e12 SIM.exposure_s = 1.0 # so total fluence is e12 # assumes round beam SIM.beamsize_mm = 0.003 #cannot make this 3 microns; spots are too intense temp = SIM.Ncells_abc print("Ncells_abc=", SIM.Ncells_abc) SIM.Ncells_abc = temp print("Ncells_abc=", SIM.Ncells_abc) print("xtal_size_mm=", SIM.xtal_size_mm) print("unit_cell_Adeg=", SIM.unit_cell_Adeg) print("unit_cell_tuple=", SIM.unit_cell_tuple) print("missets_deg=", SIM.missets_deg) print("Amatrix=", SIM.Amatrix) print("beam_center_mm=", SIM.beam_center_mm) print("XDS_ORGXY=", SIM.XDS_ORGXY) print("detector_pivot=", SIM.detector_pivot) print("xtal_shape=", SIM.xtal_shape) print("beamcenter_convention=", SIM.beamcenter_convention) print("fdet_vector=", SIM.fdet_vector) print("sdet_vector=", SIM.sdet_vector) print("odet_vector=", SIM.odet_vector) print("beam_vector=", SIM.beam_vector) print("polar_vector=", SIM.polar_vector) print("spindle_axis=", SIM.spindle_axis) print("twotheta_axis=", SIM.twotheta_axis) print("distance_meters=", SIM.distance_meters) print("distance_mm=", SIM.distance_mm) print("close_distance_mm=", SIM.close_distance_mm) print("detector_twotheta_deg=", SIM.detector_twotheta_deg) print("detsize_fastslow_mm=", SIM.detsize_fastslow_mm) print("detpixels_fastslow=", SIM.detpixels_fastslow) print("detector_rot_deg=", SIM.detector_rot_deg) print("curved_detector=", SIM.curved_detector) print("pixel_size_mm=", SIM.pixel_size_mm) print("point_pixel=", SIM.point_pixel) print("polarization=", SIM.polarization) print("nopolar=", SIM.nopolar) print("oversample=", SIM.oversample) print("region_of_interest=", SIM.region_of_interest) print("wavelength_A=", SIM.wavelength_A) print("energy_eV=", SIM.energy_eV) print("fluence=", SIM.fluence) print("flux=", SIM.flux) print("exposure_s=", SIM.exposure_s) print("beamsize_mm=", SIM.beamsize_mm) print("dispersion_pct=", SIM.dispersion_pct) print("dispsteps=", SIM.dispsteps) print("divergence_hv_mrad=", SIM.divergence_hv_mrad) print("divsteps_hv=", SIM.divsteps_hv) print("divstep_hv_mrad=", SIM.divstep_hv_mrad) print("round_div=", SIM.round_div) print("phi_deg=", SIM.phi_deg) print("osc_deg=", SIM.osc_deg) print("phisteps=", SIM.phisteps) print("phistep_deg=", SIM.phistep_deg) print("detector_thick_mm=", SIM.detector_thick_mm) print("detector_thicksteps=", SIM.detector_thicksteps) print("detector_thickstep_mm=", SIM.detector_thickstep_mm) print("***mosaic_spread_deg=", SIM.mosaic_spread_deg) print("***mosaic_domains=", SIM.mosaic_domains) print("indices=", SIM.indices) print("amplitudes=", SIM.amplitudes) print("Fhkl_tuple=", SIM.Fhkl_tuple) print("default_F=", SIM.default_F) print("interpolate=", SIM.interpolate) print("integral_form=", SIM.integral_form) from libtbx.development.timers import Profiler P = Profiler("nanoBragg") # now actually burn up some CPU #SIM.add_nanoBragg_spots() del P # simulated crystal is only 125 unit cells (25 nm wide) # amplify spot signal to simulate physical crystal of 4000x larger: 100 um (64e9 x the volume) print(crystal.domains_per_crystal) SIM.raw_pixels *= crystal.domains_per_crystal # must calculate the correct scale! # Use single wavelength for all energy channels for the purpose of Fcalc wavelength_hi_remote = wavlen[-1] GF.reset_wavelength(wavelength_hi_remote) GF.reset_specific_at_wavelength(label_has="FE1", tables=local_data.get("Fe_oxidized_model"), newvalue=wavelength_hi_remote) GF.reset_specific_at_wavelength(label_has="FE2", tables=local_data.get("Fe_reduced_model"), newvalue=wavelength_hi_remote) sfall_channel = GF.get_amplitudes() # sources channel_source_XYZ = flex.vec3_double(len(flux), SIM.xray_source_XYZ[0]) CP = channel_pixels( wavlen, flux, channel_source_XYZ) # class interface for multi-wavelength print("+++++++++++++++++++++++++++++++++++++++ Multiwavelength call") CH = CP(N=N, UMAT_nm=UMAT_nm, Amatrix_rot=Amatrix_rot, sfall_channel=sfall_channel) SIM.raw_pixels += CH.raw_pixels * crystal.domains_per_crystal CH.free_all() if quick: SIM.to_smv_format(fileout=prefix + "_intimage_001.img") # rough approximation to water: interpolation points for sin(theta/lambda) vs structure factor bg = flex.vec2_double([(0, 2.57), (0.0365, 2.58), (0.07, 2.8), (0.12, 5), (0.162, 8), (0.2, 6.75), (0.18, 7.32), (0.216, 6.75), (0.236, 6.5), (0.28, 4.5), (0.3, 4.3), (0.345, 4.36), (0.436, 3.77), (0.5, 3.17)]) SIM.Fbg_vs_stol = bg SIM.amorphous_sample_thick_mm = 0.1 SIM.amorphous_density_gcm3 = 1 SIM.amorphous_molecular_weight_Da = 18 SIM.flux = 1e12 SIM.beamsize_mm = 0.003 # square (not user specified) SIM.exposure_s = 1.0 # multiplies flux x exposure SIM.add_background() if quick: SIM.to_smv_format(fileout=prefix + "_intimage_002.img") # rough approximation to air bg = flex.vec2_double([(0, 14.1), (0.045, 13.5), (0.174, 8.35), (0.35, 4.78), (0.5, 4.22)]) SIM.Fbg_vs_stol = bg #SIM.amorphous_sample_thick_mm = 35 # between beamstop and collimator SIM.amorphous_sample_thick_mm = 10 # between beamstop and collimator SIM.amorphous_density_gcm3 = 1.2e-3 SIM.amorphous_sample_molecular_weight_Da = 28 # nitrogen = N2 print("amorphous_sample_size_mm=", SIM.amorphous_sample_size_mm) print("amorphous_sample_thick_mm=", SIM.amorphous_sample_thick_mm) print("amorphous_density_gcm3=", SIM.amorphous_density_gcm3) print("amorphous_molecular_weight_Da=", SIM.amorphous_molecular_weight_Da) SIM.add_background() #apply beamstop mask here # set this to 0 or -1 to trigger automatic radius. could be very slow with bright images # settings for CCD SIM.detector_psf_kernel_radius_pixels = 5 #SIM.detector_psf_fwhm_mm=0.08; #SIM.detector_psf_type=shapetype.Fiber # rayonix=Fiber, CSPAD=None (or small Gaussian) SIM.detector_psf_type = shapetype.Unknown # for CSPAD SIM.detector_psf_fwhm_mm = 0 #SIM.apply_psf() print("One pixel-->", SIM.raw_pixels[500000]) # at this point we scale the raw pixels so that the output array is on an scale from 0 to 50000. # that is the default behavior (intfile_scale<=0), otherwise it applies intfile_scale as a multiplier on an abs scale. if quick: SIM.to_smv_format(fileout=prefix + "_intimage_003.img") print("quantum_gain=", SIM.quantum_gain) #defaults to 1. converts photons to ADU print("adc_offset_adu=", SIM.adc_offset_adu) print("detector_calibration_noise_pct=", SIM.detector_calibration_noise_pct) print("flicker_noise_pct=", SIM.flicker_noise_pct) print("readout_noise_adu=", SIM.readout_noise_adu ) # gaussian random number to add to every pixel (0 for PAD) # apply Poissonion correction, then scale to ADU, then adc_offset. # should be 10 for most Rayonix, Pilatus should be 0, CSPAD should be 0. print("detector_psf_type=", SIM.detector_psf_type) print("detector_psf_fwhm_mm=", SIM.detector_psf_fwhm_mm) print("detector_psf_kernel_radius_pixels=", SIM.detector_psf_kernel_radius_pixels) SIM.add_noise() #converts phtons to ADU. print("raw_pixels=", SIM.raw_pixels) extra = "PREFIX=%s;\nRANK=%d;\n" % (prefix, rank) SIM.to_smv_format_py(fileout=smv_fileout, intfile_scale=1, rotmat=True, extra=extra, gz=True) # try to write as CBF if False: import dxtbx from dxtbx.format.FormatCBFMiniPilatus import FormatCBFMiniPilatus img = dxtbx.load(prefix + ".img") print(img) FormatCBFMiniPilatus.as_file(detector=img.get_detector(), beam=img.get_beam(), gonio=img.get_goniometer(), scan=img.get_scan(), data=img.get_raw_data(), path=prefix + ".cbf") SIM.free_all()
def run_sim2smv(ROI,prefix,crystal,spectra,rotation,rank,quick=False): smv_fileout = prefix + ".img" direct_algo_res_limit = 1.7 wavlen, flux, wavelength_A = next(spectra) # list of lambdas, list of fluxes, average wavelength tophat_spectrum = True if tophat_spectrum: sum_flux = flex.sum(flux) #from IPython import embed; embed() ave_flux = sum_flux/60. # 60 energy channels for ix in range(len(wavlen)): energy = 12398.425 / wavlen[ix] if energy>=7090 and energy <=7150: flux[ix]=ave_flux else: flux[ix]=0. if quick: wavlen = flex.double([wavelength_A]); flux = flex.double([flex.sum(flux)]) print("Quick sim, lambda=%f, flux=%f"%(wavelength_A,flux[0])) GF = gen_fmodel(resolution=direct_algo_res_limit,pdb_text=pdb_lines,algorithm="fft",wavelength=wavelength_A) GF.set_k_sol(0.435) GF.make_P1_primitive() sfall_main = GF.get_amplitudes() # use crystal structure to initialize Fhkl array sfall_main.show_summary(prefix = "Amplitudes used ") N = crystal.number_of_cells(sfall_main.unit_cell()) #SIM = nanoBragg(detpixels_slowfast=(2000,2000),pixel_size_mm=0.11,Ncells_abc=(5,5,5),verbose=0) SIM = nanoBragg(detpixels_slowfast=(3000,3000),pixel_size_mm=0.11,Ncells_abc=(N,N,N), # workaround for problem with wavelength array, specify it separately in constructor. wavelength_A=wavelength_A,verbose=0) SIM.adc_offset_adu = 0 # Do not offset by 40 SIM.adc_offset_adu = 10 # Do not offset by 40 import sys if len(sys.argv)>2: SIM.seed = -int(sys.argv[2]) print("GOTHERE seed=",SIM.seed) if len(sys.argv)>1: if sys.argv[1]=="random" : SIM.randomize_orientation() SIM.mosaic_spread_deg = 0.05 # interpreted by UMAT_nm as a half-width stddev SIM.mosaic_domains = 50 # 77 seconds. With 100 energy points, 7700 seconds (2 hours) per image # 3000000 images would be 100000 hours on a 60-core machine (dials), or 11.4 years # using 2 nodes, 5.7 years. Do this at SLAC? NERSC? combination of all? # SLAC downtimes: Tues Dec 5 (24 hrs), Mon Dec 11 (72 hrs), Mon Dec 18 light use, 24 days # mosaic_domains setter must come after mosaic_spread_deg setter SIM.distance_mm=141.7 UMAT_nm = flex.mat3_double() mersenne_twister = flex.mersenne_twister(seed=0) scitbx.random.set_random_seed(1234) rand_norm = scitbx.random.normal_distribution(mean=0, sigma=SIM.mosaic_spread_deg * math.pi/180.) g = scitbx.random.variate(rand_norm) mosaic_rotation = g(SIM.mosaic_domains) for m in mosaic_rotation: site = col(mersenne_twister.random_double_point_on_sphere()) UMAT_nm.append( site.axis_and_angle_as_r3_rotation_matrix(m,deg=False) ) SIM.set_mosaic_blocks(UMAT_nm) #SIM.detector_thick_mm = 0.5 # = 0 for Rayonix #SIM.detector_thicksteps = 1 # should default to 1 for Rayonix, but set to 5 for CSPAD #SIM.detector_attenuation_length_mm = default is silicon # get same noise each time this test is run SIM.seed = 1 SIM.oversample=1 SIM.wavelength_A = wavelength_A SIM.polarization=1 # this will become F000, marking the beam center SIM.default_F=0 #SIM.missets_deg= (10,20,30) print("mosaic_seed=",SIM.mosaic_seed) print("seed=",SIM.seed) print("calib_seed=",SIM.calib_seed) print("missets_deg =", SIM.missets_deg) SIM.Fhkl=sfall_main print("Determinant",rotation.determinant()) Amatrix_rot = (rotation * sqr(sfall_main.unit_cell().orthogonalization_matrix())).transpose() print("RAND_ORI", prefix, end=' ') for i in Amatrix_rot: print(i, end=' ') print() SIM.Amatrix_RUB = Amatrix_rot #workaround for failing init_cell, use custom written Amatrix setter print("unit_cell_Adeg=",SIM.unit_cell_Adeg) print("unit_cell_tuple=",SIM.unit_cell_tuple) Amat = sqr(SIM.Amatrix).transpose() # recovered Amatrix from SIM from cctbx import crystal_orientation Ori = crystal_orientation.crystal_orientation(Amat, crystal_orientation.basis_type.reciprocal) print("Python unit cell from SIM state",Ori.unit_cell()) # fastest option, least realistic #SIM.xtal_shape=shapetype.Tophat # RLP = hard sphere #SIM.xtal_shape=shapetype.Square # gives fringes SIM.xtal_shape=shapetype.Gauss # both crystal & RLP are Gaussian #SIM.xtal_shape=shapetype.Round # Crystal is a hard sphere # only really useful for long runs SIM.progress_meter=False # prints out value of one pixel only. will not render full image! #SIM.printout_pixel_fastslow=(500,500) #SIM.printout=True SIM.show_params() # flux is always in photons/s SIM.flux=1e12 SIM.exposure_s=1.0 # so total fluence is e12 # assumes round beam SIM.beamsize_mm=0.003 #cannot make this 3 microns; spots are too intense temp=SIM.Ncells_abc print("Ncells_abc=",SIM.Ncells_abc) SIM.Ncells_abc=temp print("Ncells_abc=",SIM.Ncells_abc) print("xtal_size_mm=",SIM.xtal_size_mm) print("unit_cell_Adeg=",SIM.unit_cell_Adeg) print("unit_cell_tuple=",SIM.unit_cell_tuple) print("missets_deg=",SIM.missets_deg) print("Amatrix=",SIM.Amatrix) print("beam_center_mm=",SIM.beam_center_mm) print("XDS_ORGXY=",SIM.XDS_ORGXY) print("detector_pivot=",SIM.detector_pivot) print("xtal_shape=",SIM.xtal_shape) print("beamcenter_convention=",SIM.beamcenter_convention) print("fdet_vector=",SIM.fdet_vector) print("sdet_vector=",SIM.sdet_vector) print("odet_vector=",SIM.odet_vector) print("beam_vector=",SIM.beam_vector) print("polar_vector=",SIM.polar_vector) print("spindle_axis=",SIM.spindle_axis) print("twotheta_axis=",SIM.twotheta_axis) print("distance_meters=",SIM.distance_meters) print("distance_mm=",SIM.distance_mm) print("close_distance_mm=",SIM.close_distance_mm) print("detector_twotheta_deg=",SIM.detector_twotheta_deg) print("detsize_fastslow_mm=",SIM.detsize_fastslow_mm) print("detpixels_fastslow=",SIM.detpixels_fastslow) print("detector_rot_deg=",SIM.detector_rot_deg) print("curved_detector=",SIM.curved_detector) print("pixel_size_mm=",SIM.pixel_size_mm) print("point_pixel=",SIM.point_pixel) print("polarization=",SIM.polarization) print("nopolar=",SIM.nopolar) print("oversample=",SIM.oversample) print("region_of_interest=",SIM.region_of_interest) print("wavelength_A=",SIM.wavelength_A) print("energy_eV=",SIM.energy_eV) print("fluence=",SIM.fluence) print("flux=",SIM.flux) print("exposure_s=",SIM.exposure_s) print("beamsize_mm=",SIM.beamsize_mm) print("dispersion_pct=",SIM.dispersion_pct) print("dispsteps=",SIM.dispsteps) print("divergence_hv_mrad=",SIM.divergence_hv_mrad) print("divsteps_hv=",SIM.divsteps_hv) print("divstep_hv_mrad=",SIM.divstep_hv_mrad) print("round_div=",SIM.round_div) print("phi_deg=",SIM.phi_deg) print("osc_deg=",SIM.osc_deg) print("phisteps=",SIM.phisteps) print("phistep_deg=",SIM.phistep_deg) print("detector_thick_mm=",SIM.detector_thick_mm) print("detector_thicksteps=",SIM.detector_thicksteps) print("detector_thickstep_mm=",SIM.detector_thickstep_mm) print("***mosaic_spread_deg=",SIM.mosaic_spread_deg) print("***mosaic_domains=",SIM.mosaic_domains) print("indices=",SIM.indices) print("amplitudes=",SIM.amplitudes) print("Fhkl_tuple=",SIM.Fhkl_tuple) print("default_F=",SIM.default_F) print("interpolate=",SIM.interpolate) print("integral_form=",SIM.integral_form) from libtbx.development.timers import Profiler P = Profiler("nanoBragg") # now actually burn up some CPU #SIM.add_nanoBragg_spots() del P # simulated crystal is only 125 unit cells (25 nm wide) # amplify spot signal to simulate physical crystal of 4000x larger: 100 um (64e9 x the volume) print(crystal.domains_per_crystal) SIM.raw_pixels *= crystal.domains_per_crystal; # must calculate the correct scale! output = StringIO() # open("myfile","w") for x in range(0,100,2): #len(flux)): if flux[x]==0.0:continue print("+++++++++++++++++++++++++++++++++++++++ Wavelength",x) CH = channel_pixels(ROI,wavlen[x],flux[x],N,UMAT_nm,Amatrix_rot,GF,output) SIM.raw_pixels += CH.raw_pixels * crystal.domains_per_crystal; print(SIM.raw_pixels) CH.free_all() message = output.getvalue().split() miller = (int(message[4]),int(message[5]),int(message[6])) intensity = float(message[9]); #SIM.to_smv_format(fileout=prefix + "_intimage_001.img") pixels = SIM.raw_pixels roi_pixels = pixels[ROI[1][0]:ROI[1][1], ROI[0][0]:ROI[0][1]] print("Reducing full shape of",pixels.focus(),"to ROI of",roi_pixels.focus()) SIM.free_all() return dict(roi_pixels=roi_pixels,miller=miller,intensity=intensity)
from __future__ import division from six.moves import range from scitbx.array_family import flex pdb_lines = open("1m2a.pdb", "r").read() if __name__ == "__main__": from LS49.sim.util_fmodel import gen_fmodel GF = gen_fmodel(resolution=10.0, pdb_text=pdb_lines, algorithm="fft", wavelength=1.73424) ksol = 0.01 * flex.double([float(u) for u in range(70)]) sumarray = [] for u in ksol: GF.params2.fmodel.k_sol = u amplitudes = GF.get_amplitudes() asum = flex.sum(amplitudes.data()) #for i in xrange(amplitudes.size()): #print i,amplitudes.indices()[i],amplitudes.data()[i] print(("ksol = %f sum is %f" % (u, asum))) sumarray.append(asum) from matplotlib import pyplot as plt plt.plot(ksol, sumarray, 'r.') GF = gen_fmodel(resolution=7.0, pdb_text=pdb_lines, algorithm="fft", wavelength=1.73424) ksol = 0.01 * flex.double([float(u) for u in range(70)]) sumarray = []