Ejemplo n.º 1
0
def channel_pixels(ROI,wavelength_A,flux,N,UMAT_nm,Amatrix_rot,fmodel_generator,output):
  energy_dependent_fmodel=False
  if energy_dependent_fmodel:
    fmodel_generator.reset_wavelength(wavelength_A)
    fmodel_generator.reset_specific_at_wavelength(
                   label_has="FE1",tables=Fe_oxidized_model,newvalue=wavelength_A)
    fmodel_generator.reset_specific_at_wavelength(
                   label_has="FE2",tables=Fe_reduced_model,newvalue=wavelength_A)
    print("USING scatterer-specific energy-dependent scattering factors")
    sfall_channel = fmodel_generator.get_amplitudes()
  elif use_g_sfall:
    global g_sfall
    if g_sfall is None:  g_sfall = fmodel_generator.get_amplitudes()
    sfall_channel = g_sfall
  else:
    sfall_channel = sfall_7122

  SIM = nanoBragg(detpixels_slowfast=(3000,3000),pixel_size_mm=0.11,Ncells_abc=(N,N,N),
    wavelength_A=wavelength_A,verbose=0)
  SIM.adc_offset_adu = 10 # Do not offset by 40
  SIM.region_of_interest = ROI
  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
  SIM.distance_mm=141.7
  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
  SIM.default_F=0
  SIM.Fhkl=sfall_channel
  SIM.Amatrix_RUB = Amatrix_rot
  SIM.xtal_shape=shapetype.Gauss # both crystal & RLP are Gaussian
  SIM.progress_meter=False
  # flux is always in photons/s
  SIM.flux=flux
  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

  from libtbx.development.timers import Profiler
  P = Profiler("nanoBragg")
  SIM.printout=True
  fast = ROI[1][0] + (ROI[1][1]-ROI[1][0])//2
  slow = ROI[0][0] + (ROI[0][1]-ROI[0][0])//2
  SIM.printout_pixel_fastslow=(slow,fast)
  from boost_adaptbx.boost.python import streambuf # will deposit printout into output as side effect
  SIM.add_nanoBragg_spots_nks(streambuf(output))
  del P
  print(("SIM count > 0",(SIM.raw_pixels>0).count(True)))
  return SIM
Ejemplo n.º 2
0
def simple_monochromatic_case_GPU(BEAM,
                                  DETECTOR,
                                  CRYSTAL,
                                  SF_model,
                                  argchk=False):
    Famp = SF_model.get_amplitudes(at_angstrom=BEAM.get_wavelength())

    # do the simulation
    SIM = nanoBragg(DETECTOR, BEAM, panel_id=0)
    SIM.Ncells_abc = (20, 20, 20)
    SIM.Fhkl = Famp
    SIM.Amatrix = sqr(CRYSTAL.get_A()).transpose()
    SIM.oversample = 2
    if argchk:
        print("\nmonochromatic case, GPU argchk")
        SIM.xtal_shape = shapetype.Gauss_argchk
    else:
        print("\nmonochromatic case, GPU no argchk")
        SIM.xtal_shape = shapetype.Gauss
    SIM.add_nanoBragg_spots_cuda()

    SIM.Fbg_vs_stol = water
    SIM.amorphous_sample_thick_mm = 0.02
    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.progress_meter = False
    SIM.add_background()
    return SIM
Ejemplo n.º 3
0
  def instantiate_diffBragg(self, verbose=0, oversample=0, device_Id=0,
                            adc_offset=0, default_F=1e3, interpolate=0, use_diffBragg=True,
                            auto_set_spotscale=False):

    if not use_diffBragg:
      self.D = nanoBragg(self.detector, self.beam.nanoBragg_constructor_beam,
                         verbose=verbose, panel_id=int(self.panel_id))
    else:
      self.D = diffBragg(self.detector,
                         self.beam.nanoBragg_constructor_beam,
                         verbose)
    self.using_diffBragg_spots = use_diffBragg
    self._seedlings()
    self.D.interpolate = interpolate
    self._crystal_properties()
    self._beam_properties()
    if auto_set_spotscale:
      self.D.spot_scale = self.determine_spot_scale()
    self.D.adc_offset_adu = adc_offset
    self.D.default_F = default_F

    if oversample > 0:
      self.D.oversample = int(oversample)

    self.D.device_Id = device_Id
    if not self.using_diffBragg_spots:
      self._full_roi = self.D.region_of_interest
    else:
      self.D.vectorize_umats()
Ejemplo n.º 4
0
def channel_pixels(wavelength_A, flux, N, UMAT_nm, Amatrix_rot,
                   fmodel_generator, local_data):
    fmodel_generator.reset_wavelength(wavelength_A)
    fmodel_generator.reset_specific_at_wavelength(
        label_has="FE1",
        tables=local_data.get("Fe_oxidized_model"),
        newvalue=wavelength_A)
    fmodel_generator.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")
    sfall_channel = fmodel_generator.get_amplitudes()
    SIM = nanoBragg(detpixels_slowfast=(3000, 3000),
                    pixel_size_mm=0.11,
                    Ncells_abc=(N, N, N),
                    wavelength_A=wavelength_A,
                    verbose=0)
    SIM.adc_offset_adu = 10  # Do not offset by 40
    SIM.mosaic_spread_deg = 0.05  # interpreted by UMAT_nm as a half-width stddev
    SIM.mosaic_domains = 25  # 77 seconds.  With 100 energy points, 7700 seconds (2 hours) per image
    SIM.distance_mm = 141.7
    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
    SIM.default_F = 0
    SIM.Fhkl = sfall_channel
    SIM.Amatrix_RUB = Amatrix_rot
    SIM.xtal_shape = shapetype.Gauss  # both crystal & RLP are Gaussian
    SIM.progress_meter = False
    # flux is always in photons/s
    SIM.flux = flux
    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

    from libtbx.development.timers import Profiler
    P = Profiler("nanoBragg")
    if add_spots_algorithm == "NKS":
        from boost_adaptbx.boost.python import streambuf  # will deposit printout into dummy StringIO as side effect
        SIM.add_nanoBragg_spots_nks(streambuf(StringIO()))
    elif add_spots_algorithm == "JH":
        SIM.add_nanoBragg_spots()
    elif add_spots_algorithm == "cuda":
        SIM.add_nanoBragg_spots_cuda()
    else:
        raise Exception("unknown spots algorithm")
    del P
    return SIM
Ejemplo n.º 5
0
    def several_wavelength_case_for_CPU(self):
        SIM = nanoBragg(self.DETECTOR, self.BEAM, panel_id=0)
        for x in range(len(self.wavlen)):
            SIM.flux = self.flux[x]
            SIM.wavelength_A = self.wavlen[x]
            print(
                "CPUnanoBragg_API+++++++++++++ Wavelength %d=%.6f, Flux %.6e, Fluence %.6e"
                % (x, SIM.wavelength_A, SIM.flux, SIM.fluence))
            SIM.Fhkl = self.sfall_channels[x]
            SIM.Ncells_abc = (20, 20, 20)
            SIM.Amatrix = sqr(self.CRYSTAL.get_A()).transpose()
            SIM.oversample = 2
            SIM.xtal_shape = shapetype.Gauss
            SIM.interpolate = 0
            SIM.add_nanoBragg_spots()

        SIM.wavelength_A = self.BEAM.get_wavelength()
        SIM.Fbg_vs_stol = water
        SIM.amorphous_sample_thick_mm = 0.02
        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.progress_meter = False
        SIM.add_background()
        return SIM
Ejemplo n.º 6
0
 def __init__(self):
     self.SIM = nanoBragg()
     self.SIM.progress_meter = False
     self.SIM.Fbg_vs_stol = water
     self.SIM.amorphous_sample_thick_mm = 0.1
     self.SIM.amorphous_density_gcm3 = 1
     self.SIM.amorphous_molecular_weight_Da = 18
     self.total_flux = self.SIM.flux = 1e12
     self.verbose_beams = False
Ejemplo n.º 7
0
    def __call__(self, N, UMAT_nm, Amatrix_rot, sfall_channel):
        SIM = nanoBragg(detpixels_slowfast=(3000, 3000),
                        pixel_size_mm=0.11,
                        Ncells_abc=(N, N, N),
                        wavelength_A=self.wavlen[0],
                        verbose=0)
        SIM.adc_offset_adu = 10  # Do not offset by 40
        SIM.mosaic_domains = n_mosaic_domains  # 77 seconds.  With 100 energy points, 7700 seconds (2 hours) per image
        SIM.mosaic_spread_deg = mosaic_spread_deg  # interpreted by UMAT_nm as a half-width stddev
        SIM.distance_mm = distance_mm
        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.polarization = 1
        SIM.default_F = 0
        SIM.Fhkl = sfall_channel
        SIM.Amatrix_RUB = Amatrix_rot
        SIM.xtal_shape = shapetype.Gauss  # both crystal & RLP are Gaussian
        SIM.progress_meter = False
        # flux is always in photons/s

        #SIM.flux=0.8E10 # number of photons per 100 ps integrated pulse, 24-bunch APS
        SIM.flux = 0.8E12  # number of photons per 100 ps integrated pulse, 24-bunch APS
        SIM.xray_source_wavelengths_A = self.wavlen
        SIM.xray_source_intensity_fraction = self.norm_intensity
        SIM.xray_source_XYZ = self.source_XYZ

        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

        from libtbx.development.timers import Profiler
        P = Profiler("nanoBragg")
        if add_spots_algorithm is "NKS":
            print("USING NKS")
            from boost.python import streambuf  # will deposit printout into dummy StringIO as side effect
            SIM.add_nanoBragg_spots_nks(streambuf(StringIO()))
        elif add_spots_algorithm is "JH":
            print("USING JH")
            SIM.add_nanoBragg_spots()
        elif add_spots_algorithm is "cuda":
            print("USING cuda")
            SIM.add_nanoBragg_spots_cuda()
        else:
            raise Exception("unknown spots algorithm")
        del P
        return SIM
Ejemplo n.º 8
0
def channel_pixels(simparams=None,single_wavelength_A=None,single_flux=None,N=None,UMAT_nm=None, \
                Amatrix_rot=None,sfall_channel=None,rank=None):

    # print("## inside channel wavlength/flux = ", single_wavelength_A, single_flux/simparams.flux)

    SIM = nanoBragg(detpixels_slowfast=(simparams.detector_size_ny,simparams.detector_size_nx),pixel_size_mm=simparams.pixel_size_mm,\
                Ncells_abc=(N,N,N),wavelength_A=single_wavelength_A,verbose=0)
    SIM.adc_offset_adu = 10  # Do not offset by 40
    SIM.seed = 0
    SIM.mosaic_spread_deg = simparams.mosaic_spread_deg  # interpreted by UMAT_nm as a half-width stddev
    SIM.mosaic_domains = simparams.mosaic_domains  # 77 seconds.    With 100 energy points, 7700 seconds (2 hours) per image
    SIM.distance_mm = simparams.distance_mm
    SIM.set_mosaic_blocks(UMAT_nm)

    ######################
    SIM.beamcenter_convention = convention.ADXV
    SIM.beam_center_mm = (simparams.beam_center_x_mm,
                          simparams.beam_center_y_mm)  # 95.975 96.855
    ######################

    # get same noise each time this test is run
    SIM.seed = 0
    SIM.oversample = simparams.oversample
    SIM.wavelength_A = single_wavelength_A
    SIM.polarization = simparams.polarization
    SIM.default_F = simparams.default_F
    SIM.Fhkl = sfall_channel
    SIM.Amatrix_RUB = Amatrix_rot
    SIM.xtal_shape = shapetype.Gauss  # both crystal & RLP are Gaussian
    SIM.progress_meter = False
    # flux is always in photons/s
    SIM.flux = single_flux
    SIM.exposure_s = simparams.exposure_s  # so total fluence is e12
    # assumes round beam
    SIM.beamsize_mm = simparams.beamsize_mm  #cannot make this 3 microns; spots are too intense
    temp = SIM.Ncells_abc
    SIM.Ncells_abc = temp

    # SIM.show_params()

    add_spots_algorithm = simparams.add_spots_algorithm

    if add_spots_algorithm == "NKS":
        from boost.python import streambuf  # will deposit printout into dummy StringIO as side effect
        SIM.add_nanoBragg_spots_nks(streambuf(StringIO()))
    elif add_spots_algorithm == "JH":
        SIM.add_nanoBragg_spots()
    elif add_spots_algorithm == "cuda":
        devices_per_node = int(os.environ["DEVICES_PER_NODE"])
        SIM.device_Id = rank % devices_per_node
        #if rank==7:
        #    os.system("nvidia-smi")
        SIM.add_nanoBragg_spots_cuda()
    else:
        raise Exception("!!! unknown spots algorithm")
    return SIM
Ejemplo n.º 9
0
def channel_pixels(wavelength_A, flux, N, UMAT_nm, Amatrix_rot, rank,
                   sfall_channel):
    if rank in [0, 7]:
        print("USING scatterer-specific energy-dependent scattering factors")

    SIM = nanoBragg(detpixels_slowfast=(3000, 3000),
                    pixel_size_mm=0.11,
                    Ncells_abc=(N, N, N),
                    wavelength_A=wavelength_A,
                    verbose=0)
    SIM.adc_offset_adu = 10  # Do not offset by 40
    SIM.mosaic_spread_deg = 0.05  # interpreted by UMAT_nm as a half-width stddev
    SIM.mosaic_domains = 25  # 77 seconds.  With 100 energy points, 7700 seconds (2 hours) per image
    SIM.distance_mm = 141.7
    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
    SIM.default_F = 0
    SIM.Fhkl = sfall_channel
    SIM.Amatrix_RUB = Amatrix_rot
    SIM.xtal_shape = shapetype.Gauss  # both crystal & RLP are Gaussian
    SIM.progress_meter = False
    # flux is always in photons/s
    SIM.flux = flux
    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
    if rank in [0, 7]: print("Ncells_abc=", SIM.Ncells_abc)
    SIM.Ncells_abc = temp

    if rank in [0, 7]: P = Profiler("nanoBragg C++ rank %d" % (rank))
    if add_spots_algorithm == "NKS":
        from boost_adaptbx.boost.python import streambuf  # will deposit printout into dummy StringIO as side effect
        SIM.add_nanoBragg_spots_nks(streambuf(StringIO()))
    elif add_spots_algorithm == "JH":
        SIM.add_nanoBragg_spots()
    elif add_spots_algorithm == "cuda":
        SIM.xtal_shape = shapetype.Gauss_argchk
        devices_per_node = int(os.environ["DEVICES_PER_NODE"])
        SIM.device_Id = rank % devices_per_node
        #if rank==7:
        #  os.system("nvidia-smi")
        SIM.add_nanoBragg_spots_cuda()
    else:
        raise Exception("unknown spots algorithm")
    if rank in [0, 7]: del P
    return SIM
Ejemplo n.º 10
0
def run_sim2smv(fileout):
    SIM = nanoBragg(detpixels_slowfast=(1000, 1000),
                    pixel_size_mm=0.1,
                    Ncells_abc=(5, 5, 5),
                    verbose=0)
    SIM.mosaic_spread_deg = MOSAIC_SPREAD  # apparently this is half width
    SIM.mosaic_domains = SAMPLE_SIZE
    SIM.distance_mm = 100  # this triggers the generation of mosaic distribution
    UMAT_th = SIM.get_mosaic_blocks()  # extract top-hat distributed U-mats

    #sanity checks on the top hat distribution
    th_angles = check_distributions.get_angular_rotation(UMAT_th)
    max_angle = flex.max(th_angles)
    assert max_angle <= MOSAIC_SPREAD + 0.0000001  # need to allow a small epsilon
    assert max_angle > 0.99 * MOSAIC_SPREAD  # insist that max angle is near the limit we gave it
    rms_angle = math.sqrt(flex.mean(th_angles * th_angles))
    print(rms_angle)
    assert rms_angle < MOSAIC_SPREAD

    import scitbx  # compute an array of normally-distributed U-mats into the simulator
    UMAT_nm = flex.mat3_double()
    mersenne_twister = flex.mersenne_twister(
        seed=0)  # set seed, get reproducible results
    scitbx.random.set_random_seed(4321)  # set seed, get reproducibe results
    rand_norm = scitbx.random.normal_distribution(mean=0,
                                                  sigma=rms_angle * math.pi /
                                                  180.)
    g = scitbx.random.variate(rand_norm)
    mosaic_rotation = g(SAMPLE_SIZE)
    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))

    # set the normally-distributed U-mats into the simulator:
    SIM.set_mosaic_blocks(UMAT_nm)

    # get them back
    new_UMAT_nm = SIM.get_mosaic_blocks()

    #double check that simulator does not alter the UMATs
    for iumat in range(0, SAMPLE_SIZE, 100):

        assert UMAT_nm[iumat] == new_UMAT_nm[iumat]

    #sanity check on the gaussian distribution
    nm_angles = check_distributions.get_angular_rotation(new_UMAT_nm)
    nm_rms_angle = math.sqrt(flex.mean(nm_angles * nm_angles))
    print(nm_rms_angle)
    assert approx_equal(rms_angle,nm_rms_angle,eps=1e-03), \
    "The top hat and gaussian models should have similar standard deviations"

    return UMAT_th, new_UMAT_nm
Ejemplo n.º 11
0
def sim_background(DETECTOR,
                   BEAM,
                   wavelengths,
                   wavelength_weights,
                   total_flux,
                   pidx=0,
                   beam_size_mm=0.001,
                   Fbg_vs_stol=None,
                   sample_thick_mm=100,
                   density_gcm3=1,
                   molecular_weight=18):
    """
  :param DETECTOR:
  :param BEAM: see sim_spots
  :param wavelengths: see sim_spots
  :param wavelength_weights: see sim_spots
  :param total_flux: see sim_spots
  :param pidx: see sim_spots
  :param beam_size_mm: see sim_spots
  :param Fbg_vs_stol: list of tuples where each tuple is (Fbg, sin theta over lambda)
  :param sample_thick_mm: path length of background that is exposed by the beam
  :param density_gcm3: density of background  (defaults to water)
  :param molecular_weight: molecular weight of background (defaults to water)
  :return: raw_pixels as flex array, these can be passed to sim_spots function below
  """
    wavelength_weights = np.array(wavelength_weights)
    weights = wavelength_weights / wavelength_weights.sum() * total_flux
    spectrum = list(zip(wavelengths, weights))
    xray_beams = get_xray_beams(spectrum, BEAM)
    SIM = nanoBragg(DETECTOR, BEAM, panel_id=(int(pidx)))
    SIM.beamsize_mm = beam_size_mm
    SIM.xray_beams = xray_beams
    if Fbg_vs_stol is None:
        Fbg_vs_stol = flex.vec2_double([(0, 2.57), (0.0365, 2.58), (0.07, 2.8),
                                        (0.12, 5), (0.162, 8), (0.18, 7.32),
                                        (0.2, 6.75), (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.flux = sum(weights)
    SIM.Fbg_vs_stol = Fbg_vs_stol
    SIM.amorphous_sample_thick_mm = sample_thick_mm
    SIM.amorphous_density_gcm3 = density_gcm3
    SIM.amorphous_molecular_weight_Da = molecular_weight
    SIM.progress_meter = False
    SIM.add_background()
    background_raw_pixels = SIM.raw_pixels.deep_copy()
    SIM.free_all()
    del SIM
    return background_raw_pixels
Ejemplo n.º 12
0
 def instantiate_nanoBragg(self, verbose=0, oversample=0, device_Id=0, adc_offset=0, default_F=1000.0, interpolate=0,
                           pid=0):
   self.D = nanoBragg(self.detector, self.beam.nanoBragg_constructor_beam, verbose=verbose,
                      panel_id=int(pid))
   self._seedlings()
   self.D.interpolate = interpolate
   self._crystal_properties()
   self._beam_properties()
   self.D.spot_scale = self.determine_spot_scale()
   self.D.adc_offset_adu = adc_offset
   self.D.default_F = default_F
   if oversample > 0:
     self.D.oversample = oversample
   if self.using_cuda:
     self.D.device_Id = device_Id
   self._full_roi = self.D.region_of_interest
Ejemplo n.º 13
0
 def __init__(self, parent, params,
              add_background=True,
              add_noise=True,
              randomize=True,
              pixel_scale=None,
              preview=True):
   Thread.__init__(self)
   self.SIM = nanoBragg(detpixels_slowfast=(1231, 1263),
                        pixel_size_mm=0.344,
                        Ncells_abc=(5, 5, 5))
   self.parent = parent
   self.params = params
   self.add_background = add_background
   self.add_noise = add_noise
   self.randomize = randomize
   self.crystal_size = pixel_scale
   self.preview = preview
Ejemplo n.º 14
0
 def __init__(self, BEAM, DETECTOR, CRYSTAL, SF_model):
     SIM = nanoBragg(DETECTOR, BEAM, panel_id=0)
     print("\nassume three energy channels")
     self.wavlen = flex.double([
         BEAM.get_wavelength() - 0.002,
         BEAM.get_wavelength(),
         BEAM.get_wavelength() + 0.002
     ])
     self.flux = flex.double([(1. / 6.) * SIM.flux, (3. / 6.) * SIM.flux,
                              (2. / 6.) * SIM.flux])
     self.sfall_channels = {}
     for x in range(len(self.wavlen)):
         self.sfall_channels[x] = SF_model.get_amplitudes(
             at_angstrom=self.wavlen[x])
     self.DETECTOR = DETECTOR
     self.BEAM = BEAM
     self.CRYSTAL = CRYSTAL
Ejemplo n.º 15
0
def channel_pixels(wavelength_A, flux, UMAT_nm, Amatrix_rot, rank,
                   sfall_channel):
    SIM = nanoBragg(detpixels_slowfast=detpixels_slowfast,
                    pixel_size_mm=pixsize_mm,
                    Ncells_abc=Ncells_abc,
                    wavelength_A=wavelength_A,
                    verbose=verbose)
    SIM.adc_offset_adu = offset_adu
    SIM.mosaic_spread_deg = mos_spread_deg  # interpreted by UMAT_nm as a half-width stddev
    SIM.mosaic_domains = mos_doms  # 77 seconds.  With 100 energy points, 7700 seconds (2 hours) per image
    SIM.distance_mm = distance_mm
    SIM.set_mosaic_blocks(UMAT_nm)

    # get same noise each time this test is run
    SIM.seed = 1
    SIM.oversample = 1
    SIM.wavelength_A = wavelength_A
    SIM.polarization = 1
    SIM.default_F = 0
    SIM.Fhkl = sfall_channel.amplitudes()
    SIM.Amatrix_RUB = Amatrix_rot
    SIM.xtal_shape = shapetype.Gauss  # both crystal & RLP are Gaussian
    SIM.progress_meter = True  # False
    # flux is always in photons/s
    SIM.flux = flux
    SIM.exposure_s = exposure_s
    # assumes round beam
    SIM.beamsize_mm = beam_size_mm
    temp = SIM.Ncells_abc
    SIM.Ncells_abc = temp

    #P = Profiler("nanoBragg C++ rank %d"%(rank))
    if add_spots_algorithm is "NKS":
        from boost.python import streambuf  # will deposit printout into dummy StringIO as side effect
        SIM.add_nanoBragg_spots_nks(streambuf(StringIO()))
    elif add_spots_algorithm is "JH":
        SIM.add_nanoBragg_spots()
    elif add_spots_algorithm is "cuda":
        SIM.timelog = timelog
        SIM.device_Id = device_Id
        SIM.add_nanoBragg_spots_cuda()
    else:
        raise Exception("unknown spots algorithm")
    #del P
    return SIM
Ejemplo n.º 16
0
def run_sim2smv(fileout):
  SIM = nanoBragg(detpixels_slowfast=(1000,1000),pixel_size_mm=0.1,Ncells_abc=(5,5,5),verbose=0)
  SIM.mosaic_domains = 10000
  SIM.mosaic_spread_deg = 2.0
  SIM.distance_mm=100 # this triggers the generation of mosaic distribution
  UMAT_th = SIM.get_mosaic_blocks()

  import scitbx
  UMAT_nm = flex.mat3_double()
  mersenne_twister = flex.mersenne_twister(seed=0)
  rand_norm = scitbx.random.normal_distribution(mean=0, sigma=math.pi/180.)
  g = scitbx.random.variate(rand_norm)
  mosaic_rotation = g(10000)
  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) )

  return UMAT_th, UMAT_nm
Ejemplo n.º 17
0
def run_sim2smv(fileout):
    SIM = nanoBragg(detpixels_slowfast=(1000, 1000),
                    pixel_size_mm=0.1,
                    Ncells_abc=(5, 5, 5),
                    verbose=0)
    SIM.mosaic_domains = 10000
    SIM.mosaic_spread_deg = MOSAIC_SPREAD
    SIM.distance_mm = 100  # this triggers the generation of mosaic distribution
    UMAT_th = SIM.get_mosaic_blocks()  # extract top-hat distributed U-mats

    #sanity checks on the top hat distribution
    th_angles = check_distributions.get_angular_rotation(UMAT_th)
    max_angle = flex.max(th_angles)
    assert max_angle <= MOSAIC_SPREAD + 0.0000001  # need to allow a small epsilon
    assert max_angle > 0.99 * MOSAIC_SPREAD  # insist that max angle is near the limit we gave it
    rms_angle = math.sqrt(flex.mean(th_angles * th_angles))
    print rms_angle

    import scitbx  # computer an array of normally-distributed U-mats into the simulator
    UMAT_nm = flex.mat3_double()
    mersenne_twister = flex.mersenne_twister(seed=0)
    rand_norm = scitbx.random.normal_distribution(mean=0,
                                                  sigma=rms_angle * math.pi /
                                                  180.)
    g = scitbx.random.variate(rand_norm)
    mosaic_rotation = g(10000)
    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))

    # set the normally-distributed U-mats into the simulator:
    SIM.set_mosaic_blocks(UMAT_nm)

    # get them back
    new_UMAT_nm = SIM.get_mosaic_blocks()

    #sanity check on the gaussian distribution
    nm_angles = check_distributions.get_angular_rotation(new_UMAT_nm)
    nm_rms_angle = math.sqrt(flex.mean(nm_angles * nm_angles))
    print nm_rms_angle

    return UMAT_th, new_UMAT_nm
Ejemplo n.º 18
0
def memory_leak_1(fileout):
  SIM = nanoBragg(detpixels_slowfast=(1000,1000),pixel_size_mm=0.1,Ncells_abc=(5,5,5),verbose=0)
  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.distance_mm=100
  SIM.seed = 1
  SIM.oversample=1
  SIM.wavelength_A=1
  SIM.polarization=1
  print(("unit_cell_Adeg=",SIM.unit_cell_Adeg))
  print(("unit_cell_tuple=",SIM.unit_cell_tuple))
  SIM.default_F=100
  print(("mosaic_seed=",SIM.mosaic_seed))
  print(("seed=",SIM.seed))
  print(("calib_seed=",SIM.calib_seed))
  print(("missets_deg =", SIM.missets_deg))
  SIM.free_all()
Ejemplo n.º 19
0
def channel_pixels(wavelength_A, flux, N, UMAT_nm, Amatrix_rot, sfall):
    SIM = nanoBragg(detpixels_slowfast=(3000, 3000),
                    pixel_size_mm=0.11,
                    Ncells_abc=(N, N, N),
                    wavelength_A=wavelength_A,
                    verbose=0)
    SIM.adc_offset_adu = 10  # Do not offset by 40
    SIM.mosaic_spread_deg = 0.05  # interpreted by UMAT_nm as a half-width stddev
    SIM.mosaic_domains = 25  # 77 seconds.  With 100 energy points, 7700 seconds (2 hours) per image
    SIM.distance_mm = 141.7
    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
    SIM.default_F = 0
    SIM.Fhkl = sfall
    SIM.Amatrix_RUB = Amatrix_rot
    SIM.xtal_shape = shapetype.Gauss  # both crystal & RLP are Gaussian
    SIM.progress_meter = False
    # flux is always in photons/s
    SIM.flux = flux
    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

    from libtbx.development.timers import Profiler
    P = Profiler("nanoBragg")
    SIM.add_nanoBragg_spots_nks()
    del P
    return SIM
Ejemplo n.º 20
0
    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
Ejemplo n.º 21
0
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)
Ejemplo n.º 22
0
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()
Ejemplo n.º 23
0
def run_sim2smv(fileout):
    SIM = nanoBragg(detpixels_slowfast=(1000, 1000),
                    pixel_size_mm=0.1,
                    Ncells_abc=(5, 5, 5),
                    verbose=9)
    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.distance_mm = 100
    #SIM = nanoBragg(detpixels_slowfast=(2527,2463),pixel_size_mm=0.172,Ncells_abc=(5,5,5),verbose=9)
    #SIM.distance_mm=200
    # get same noise each time this test is run
    SIM.seed = 1
    SIM.oversample = 1
    SIM.wavelength_A = 1
    SIM.polarization = 1
    #SIM.unit_cell_tuple=(50,50,50,90,90,90)
    print "unit_cell_Adeg=", SIM.unit_cell_Adeg
    print "unit_cell_tuple=", SIM.unit_cell_tuple
    # this will become F000, marking the beam center
    SIM.default_F = 100
    #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
    sfall = fcalc_from_pdb(resolution=1.6,
                           algorithm="direct",
                           wavelength=SIM.wavelength_A)
    # use crystal structure to initialize Fhkl array
    SIM.Fhkl = sfall
    # fastest option, least realistic
    SIM.xtal_shape = shapetype.Tophat
    # 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
    # assumes round beam
    SIM.beamsize_mm = 0.1
    SIM.exposure_s = 0.1
    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
    # now actually burn up some CPU
    SIM.add_nanoBragg_spots()
    # 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)
    SIM.raw_pixels *= 64e9
    SIM.to_smv_format(fileout="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.1
    SIM.exposure_s = 0.1
    SIM.add_background()
    SIM.to_smv_format(fileout="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_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()
    # set this to 0 or -1 to trigger automatic radius.  could be very slow with bright images
    SIM.detector_psf_kernel_radius_pixels = 5
    SIM.detector_psf_fwhm_mm = 0.08
    SIM.detector_psf_type = shapetype.Fiber
    #SIM.apply_psf()
    print SIM.raw_pixels[500000]
    SIM.to_smv_format(fileout="intimage_003.img")
    #SIM.detector_psf_fwhm_mm=0
    print "quantum_gain=", SIM.quantum_gain
    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
    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()

    #fileout = "intimage_001.img"
    print "raw_pixels=", SIM.raw_pixels
    SIM.to_smv_format(fileout="noiseimage_001.img", intfile_scale=1)

    # try to write as CBF
    import dxtbx
    from dxtbx.format.FormatCBFMini import FormatCBFMini
    img = dxtbx.load("noiseimage_001.img")
    print img
    FormatCBFMini.as_file(detector=img.get_detector(),
                          beam=img.get_beam(),
                          gonio=img.get_goniometer(),
                          scan=img.get_scan(),
                          data=img.get_raw_data(),
                          path=fileout)
    SIM.free_all()
Ejemplo n.º 24
0
def run_sim2smv(Nshot_max, odir, prefix, rank, n_jobs, save_bragg=False, 
            save_smv=True, save_h5 =False, return_pixels=False):

  from six.moves import range, StringIO
  from six.moves import cPickle as pickle
  from cxid9114.sim import sim_utils
  import os
  import h5py
  import math
  import sys
  import numpy as np
  from IPython import embed
  from cxid9114.bigsim.bigsim_geom import DET,BEAM
  import scitbx
  from scitbx.array_family import flex
  from scitbx.matrix import sqr,col
  from simtbx.nanoBragg import shapetype
  from simtbx.nanoBragg import nanoBragg
  import libtbx.load_env # possibly implicit
  from libtbx.development.timers import Profiler
  from cctbx import crystal,crystal_orientation
  from LS49.sim.step4_pad import microcrystal
  from cxid9114 import utils
  from cxid9114.parameters import ENERGY_CONV, ENERGY_HIGH, ENERGY_LOW
  from cxid9114.bigsim import sim_spectra
 
  
  odir_j = os.path.join( odir, "job%d" % rank)
  if not os.path.exists(odir_j):
      os.makedirs(odir_j)

  add_noise = False
  add_background = False
  overwrite = True #$False
  sample_thick_mm = 0.005  # 50 micron GDVN nozzle makes a ~5ish micron jet
  air_thick_mm =0  # mostly vacuum, maybe helium layer of 1 micron
  flux_ave=2e11
  add_spots_algorithm="cuda"
  big_data = "." # directory location for reference files
  detpixels_slowfast = (1800,1800)
  pixsize_mm=0.11
  distance_mm = 125
  offset_adu=30
  mos_spread_deg=0.015
  mos_doms=1000
  beam_size_mm=0.001
  exposure_s=1
  use_microcrystal=True #False
  Ncells_abc=(120,120,120)
  Deff_A = 2200
  length_um = 2.2
  timelog = False
  background = utils.open_flex("background")
 
  crystal = microcrystal(Deff_A = Deff_A, length_um = length_um, 
        beam_diameter_um = beam_size_mm*1000, verbose=False) 
  spec_file =  h5py.File("simMe_data_run62.h5", "r")
  spec_data = spec_file["hist_spec"]
  Umat_data = spec_file["Umats"]
  en_chans = spec_file["energy_bins"][()]
  ilow = np.abs(en_chans - ENERGY_LOW).argmin()
  ihigh = np.abs(en_chans - ENERGY_HIGH).argmin()
  wave_chans = ENERGY_CONV/en_chans
  sfall_main = sim_spectra.load_spectra("test_sfall.h5")
    
  Nshot = spec_data.shape[0]
    
  idx_range = np.array_split(np.arange(Nshot), n_jobs)
    
  Nshot_per_job = len(idx_range[rank])
  if Nshot_max  > 0 :
    Nshot_per_job = min( Nshot_max, Nshot_per_job)
  
  print ("Job %d: Simulating %d shots" % (rank, Nshot_per_job))
 
  istart = idx_range[rank][0]
  istop = istart + Nshot_per_job
  smi_stride = 10
  for idx in range( istart, istop): 
    print ("<><><><><><><><><><><><><><>")
    print ("Job %d; Image %d (%d - %d)" % (rank, idx+1, istart, istop))
    print ("<><><><><><><><><><><><><><>")
    
    smv_fileout = os.path.join( odir_j, prefix % idx + ".img")
    h5_fileout = smv_fileout + ".h5"
    
    if os.path.exists(smv_fileout) and not overwrite and save_smv:
        print("Shot %s exists: moving on" % smv_fileout)
        continue
    
    if os.path.exists(h5_fileout) and not overwrite and save_h5:
        print("Shot %s exists: moving on" % h5_fileout)
        continue
    
    if (rank==0 and idx % smi_stride==0):
      print("GPU status")
      os.system("nvidia-smi")
      
      print("\n\n")
      print("CPU memory usage")
      mem_usg= """ps -U dermen --no-headers -o rss | awk '{ sum+=$1} END {print int(sum/1024) "MB consumed by CPU user"}'"""
      os.system(mem_usg)
    spec = spec_data[2]
    rotation = sqr(Umat_data[2])
    wavelength_A = np.mean(wave_chans)
  
    spectra = iter([(wave_chans, spec, wavelength_A)])
  
    direct_algo_res_limit = 1.7

    wavlen, flux, wavelength_A = next(spectra) # list of lambdas, list of fluxes, average wavelength
    assert wavelength_A > 0
    assert (len(wavlen)==len(flux)==len(sfall_main))

    N = crystal.number_of_cells(sfall_main[0].unit_cell())
    if use_mcrocrystal:
      Ncells_abc = (N,N,N)  
    
    flux *= 0
    flux[ilow] = 1e12
    flux[ihigh]=1e12
    
    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=mos_spread_deg * math.pi/180.)
    g = scitbx.random.variate(rand_norm)
    mosaic_rotation = g(mos_doms)
    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) )
    
    Amatrix_rot = (rotation * sqr(sfall_main[0].unit_cell().orthogonalization_matrix())).transpose()
    
    #SIM = nanoBragg(detpixels_slowfast=detpixels_slowfast,
    #      pixel_size_mm=pixsize_mm,Ncells_abc=Ncells_abc,
    #      wavelength_A=wavelength_A,verbose=verbose)
    SIM = nanoBragg(detector=DET, beam=BEAM, panel_id=0,verbose=verbose)

    SIM.adc_offset_adu = offset_adu # Do not offset by 40
    SIM.mosaic_spread_deg = mos_spread_deg # interpreted by UMAT_nm as a half-width stddev
    SIM.mosaic_domains = mos_doms
    SIM.distance_mm=distance_mm
    SIM.set_mosaic_blocks(UMAT_nm)
    SIM.seed = 1
    SIM.oversample=1
    SIM.wavelength_A = wavelength_A
    SIM.polarization=1
    SIM.default_F=0
    SIM.Fhkl=sfall_main[0].amplitudes()
    SIM.progress_meter=False
    SIM.flux=flux_ave
    SIM.exposure_s=exposure_s 
    SIM.beamsize_mm=beam_size_mm 
    SIM.Ncells_abc=Ncells_abc
    SIM.xtal_shape=shapetype.Gauss 
    
    idxpath = "try3_idx2/job0/dump_0_data.pkl"
    Amat = sim_utils.Amatrix_dials2nanoBragg(utils.open_flex(idxpath)["crystalAB"])
    #SIM.Umatrix = rotation.elems
    #SIM.unit_cell_Adeg = sfall_main[0].unit_cell()
    
    #SIM2 = nanoBragg(detector=DET, beam=BEAM, panel_id=0,verbose=verbose)

    #SIM2.adc_offset_adu = offset_adu # Do not offset by 40
    #SIM2.mosaic_spread_deg = mos_spread_deg # interpreted by UMAT_nm as a half-width stddev
    #SIM2.mosaic_domains = mos_doms
    #SIM2.distance_mm=distance_mm
    #SIM2.set_mosaic_blocks(UMAT_nm)
    #SIM2.seed = 1
    #SIM2.oversample=1
    #SIM2.wavelength_A = wavelength_A
    #SIM2.polarization=1
    #SIM2.default_F=0
    #SIM2.Fhkl=sfall_main[0].amplitudes()
    #SIM2.progress_meter=False
    #SIM2.flux=flux_ave
    #SIM2.exposure_s=exposure_s 
    #SIM2.beamsize_mm=beam_size_mm 
    #SIM2.Ncells_abc=Ncells_abc
    #SIM2.xtal_shape=shapetype.Gauss 
    #SIM2.Umatrix = rotation.elems
    #SIM2.unit_cell_Adeg = sfall_main[0].unit_cell()
    
    #SIM.Amatrix_RUB = Amatrix_rot
    #SIM.Amatrix = Amatrix_rot.inverse()
    
    raw_pixel_sum = flex.double(len(SIM.raw_pixels))
    Nflux = len(flux)
    print("Beginning the loop")
    for x in range(Nflux):
      if x % 10==0:
        print("+++++++++++++++++++++++++++++++++++++++ Wavelength %d / %d" % (x+1, Nflux) , end="\r")
      if flux[x] ==0:
        continue
      #SIM.Amatrix = Amatrix_rot.inverse() 
      print (SIM.Ncells_abc)
      SIM.wavelength_A=wavlen[x]
      SIM.flux=flux[x]
      SIM.Fhkl=sfall_main[x].amplitudes()
      SIM.Amatrix = Amat#Amatrix_rot.inverse()
      
      #sim_utils.compare_sims(SIM, SIM2)
      #SIM.Ncells_abc=Ncells_abc
      #SIM.adc_offset_adu = offset_adu
      #SIM.mosaic_spread_deg = mos_spread_deg # interpreted by UMAT_nm as a half-width stddev
      #SIM.mosaic_domains = mos_doms  #
      #SIM.distance_mm=distance_mm
      #SIM.set_mosaic_blocks(UMAT_nm)
      #SIM.seed = 1
      #SIM.polarization=1
      #SIM.default_F=0
      #SIM.xtal_shape=shapetype.Gauss 
      #SIM.progress_meter=False 
      #SIM.exposure_s = exposure_s
      #SIM.beamsize_mm=beam_size_mm 

      SIM.timelog=timelog
      SIM.device_Id=rank
      SIM.raw_pixels *= 0  # just in case!
      SIM.add_nanoBragg_spots_cuda()
    
      if use_microcrystal:
        raw_pixel_sum += SIM.raw_pixels * crystal.domains_per_crystal

    print()

    SIM.raw_pixels = raw_pixel_sum
    if add_background:
        SIM.raw_pixels = SIM.raw_pixels + background 
    
    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.quantum_gain = 28.
    #SIM.apply_psf()
    if add_noise:
        SIM.add_noise() #converts phtons to ADU.
    extra = "PREFIX=%s;\nRANK=%d;\n"%(prefix,rank)
  
    out =  SIM.raw_pixels.as_numpy_array() 
  
    if save_smv:
      SIM.to_smv_format_py(fileout=smv_fileout,intfile_scale=1,rotmat=True,extra=extra,gz=True)
    elif save_h5:
      f = h5py.File(h5_fileout, "w")
      f.create_dataset("bigsim_d9114", 
        data=SIM.raw_pixels.as_numpy_array().astype(np.uint16).reshape(detpixels_slowfast), 
        compression="lzf")
      f.close()
 
    if npout is not None: 
        np.save(npout, SIM.raw_pixels.as_numpy_array())
    SIM.free_all()
def run_sim(seed=1,
            wavelength=0.9,
            distance=500,
            random_orientation=False,
            phi=0,
            osc=0):
    SIM = nanoBragg(detpixels_slowfast=(2527, 2463),
                    pixel_size_mm=0.172,
                    Ncells_abc=(15, 15, 15),
                    verbose=0)
    # get same noise each time this test is run
    SIM.seed = seed
    # user may select random orientation
    if random_orientation:
        SIM.randomize_orientation()
    else:
        # fixed orientation, for doing rotation data sets
        SIM.missets_deg = (10, 20, 30)
    SIM.distance_mm = distance
    SIM.wavelength_A = wavelength
    # option to render a phi swell
    SIM.phi_deg = phi
    SIM.osc_deg = osc
    if osc >= 0:
        # need quite a few steps/deg for decent Rmeas
        SIM.phistep_deg = 0.001
    SIM.oversample = 1
    SIM.polarization = 1
    # this will become F000, marking the beam center
    SIM.F000 = 100
    print "mosaic_seed=", SIM.mosaic_seed
    print "seed=", SIM.seed
    print "calib_seed=", SIM.calib_seed
    print "missets_deg =", SIM.missets_deg
    # re-set the detector to be in
    SIM.beamcenter_convention = convention.DIALS
    SIM.beam_center_mm = (211, 214)
    sfall = fcalc_from_pdb(resolution=1.6,
                           algorithm="direct",
                           wavelength=SIM.wavelength_A)
    # use crystal structure to initialize Fhkl array
    SIM.Fhkl = sfall
    # fastest option, least realistic
    SIM.xtal_shape = shapetype.Tophat
    # 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
    # flux is always in photons/s
    SIM.flux = 1e12
    # assumes round beam
    SIM.beamsize_mm = 0.1
    SIM.exposure_s = 0.1
    # Pilatus detector is thick
    SIM.detector_thick_mm = 0.450
    # get attenuation depth in mm
    from cctbx.eltbx import attenuation_coefficient
    table = attenuation_coefficient.get_table("Si")
    SIM.detector_attenuation_length_mm = 10.0 / (table.mu_at_angstrom(
        SIM.wavelength_A))
    print "detector_attenuation_length =", SIM.detector_attenuation_length_mm
    #SIM.detector_thick_mm=0
    SIM.detector_thicksteps = 3
    #
    # will simulate module mis-alignment by adjusting beam center of each module
    beam_center0 = SIM.beam_center_mm
    # simulated crystal is only 3375 unit cells (42 nm wide)
    # amplify spot signal to simulate physical crystal of 100 um
    # this is equivalent to adding up 28e9 mosaic domains in exactly the same orientation, but a lot faster
    aggregate_xtal_volume_mm3 = 0.1 * 0.1 * 0.1
    mosaic_domain_volume_mm3 = SIM.xtal_size_mm[0] * SIM.xtal_size_mm[
        1] * SIM.xtal_size_mm[2]
    SIM.spot_scale = aggregate_xtal_volume_mm3 / mosaic_domain_volume_mm3
    #
    # option for detector rotations to be about beam center or sample position
    #SIM.detector_pivot=pivot.Sample
    SIM.detector_pivot = pivot.Beam
    print "pivoting detector about", SIM.detector_pivot
    # make the module roi list
    # and also make up per-module mis-alignments shifts and rotations
    import random
    # make sure shifts are the same from image to image
    random.seed(12345)
    module_size = (487, 195)
    gap_size = (7, 17)
    modules = []
    for mx in range(0, 5):
        for my in range(0, 12):
            # define region of interest for this module
            fmin = mx * (module_size[0] + gap_size[0])
            fmax = fmin + module_size[0]
            smin = my * (module_size[1] + gap_size[1])
            smax = smin + module_size[1]
            # assume misalignments are uniform
            # up to ~0.5 pixel and 0.0 deg in each direction
            dx = 0.07 * (random.random() - 0.5) * 2
            dy = 0.07 * (random.random() - 0.5) * 2
            rotx = 0.0 * (random.random() - 0.5) * 2
            roty = 0.0 * (random.random() - 0.5) * 2
            rotz = 0.0 * (random.random() - 0.5) * 2
            modules.append(
                (((fmin, fmax), (smin, smax)), (dx, dy), (rotx, roty, rotz)))
    #
    # defeat module-by-module rendering by uncommenting next 3 lines
    #dim=SIM.detpixels_fastslow
    #modules = [(( ((0,dim[0]),(0,dim[1])) ),(0,0),(0,0,0))]
    #print modules
    i = -1
    for roi, (dx, dy), drot in modules:
        i = i + 1
        print "rendering module:", i, "roi=", roi
        SIM.region_of_interest = roi
        # shift the beam center
        x = beam_center0[0] + dx
        y = beam_center0[1] + dy
        SIM.beam_center_mm = (x, y)
        # also tilt the module by up to 0.05 deg in all directions
        SIM.detector_rotation_deg = drot
        print "beam center: ", SIM.beam_center_mm, "rotations:", drot
        # now actually burn up some CPU
        SIM.add_nanoBragg_spots()
        # add water contribution
        SIM.Fbg_vs_stol = Fbg_water
        SIM.amorphous_sample_thick_mm = 0.1
        SIM.amorphous_density_gcm3 = 1
        SIM.amorphous_molecular_weight_Da = 18
        SIM.add_background()
        # add air contribution
        SIM.Fbg_vs_stol = Fbg_air
        SIM.amorphous_sample_thick_mm = 35  # between beamstop and collimator
        SIM.amorphous_density_gcm3 = 1.2e-3
        SIM.amorphous_sample_molecular_weight_Da = 28  # nitrogen = N2
        SIM.add_background()
        # add nanocrystalline cubic ice contribution (unscaled)
        SIM.Fbg_vs_stol = Fbg_nanoice
        SIM.amorphous_sample_thick_mm = 0.05  # between beamstop and collimator
        SIM.amorphous_density_gcm3 = 0.95
        SIM.amorphous_sample_molecular_weight_Da = 18  # H2O
        SIM.add_background()
    #
    # set this to 0 or -1 to trigger automatic radius.  could be very slow with bright images
    SIM.detector_psf_kernel_radius_pixels = 5
    # turn off the point spread function for Pilatus
    SIM.detector_psf_fwhm_mm = 0.0
    SIM.detector_psf_type = shapetype.Gauss
    SIM.adc_offset_adu = 0
    SIM.readout_noise_adu = 0
    dim = SIM.detpixels_fastslow
    SIM.region_of_interest = ((0, dim[0]), (0, dim[1]))
    SIM.add_noise()
    return SIM
Ejemplo n.º 26
0
def generate_simtbx_phil():
    ''' Generates a PHIL object from simTBX params

  @return: sim_phil: a PHIL object
  '''

    ui_phil_string = """
description = None
  .type = str
  .help = Run description (optional).
  .optional = True
output = None
  .type = path
  .help = Base output directory, current directory in command-line, can be set in GUI
  .optional = False
image_prefix = synthetic_image
  .type = str
  .help = Prefix for synthetic image filenames (numbers will be added)
image_format = *img cbf mccd pickle
  .type = choice
  .help = Will be format in the future, just extension for now
reference_coordinates = None
  .type = path
  .help = Coordinates (PDB) to generate FCalc
reference_FCalc = None
  .type = path
  .help = FCalc (MTZ) that can be used to generate the diffraction pattern
reference_image = None
  .type = path
  .help = Diffraction image that can be used to match orientation
radial_average_background = None
  .type = path
  .help = Radial average background (sin(theta) / lambda, STOL)
dataset
  .help = Synthetic dataset options
{
  start_phi = 0
    .type = float
    .help = phi angle for the first image in the dataset
  finish_phi = 90
    .type = float
    .help = phi angle for the last image in the dataset
  oscillation = 1
    .type = float
    .help = phi oscillation per dataset image
}
"""

    sim = nanoBragg()
    params = [name for (name, value) in inspect.getmembers(nanoBragg, isprop)]

    param_lines = ['simtbx', '{']
    for i in params:
        # The below properties result in boost errors (Fbg_vs_stol), TypeError
        # exceptions (xray_beams), or yield objects rather than values (the rest)
        if i in ('Fbg_vs_stol', 'Fhkl_tuple', 'amplitudes', 'indices',
                 'raw_pixels', 'xray_source_XYZ',
                 'xray_source_intensity_fraction', 'xray_beams',
                 'xray_source_wavelengths_A', 'unit_cell_tuple',
                 'progress_pixel'):
            continue

        try:
            param_value = getattr(sim, i)
            if type(param_value).__name__ == 'tuple':
                element_type = type(list(flatten(param_value))[0]).__name__
                param_type = "{}s".format(element_type)
                param_value = ' '.join(
                    str(i) for i in list(flatten(param_value)))
                param_doc = getattr(nanoBragg, i).__doc__
            else:
                param_type = type(param_value).__name__
                param_doc = getattr(nanoBragg, i).__doc__
                if param_type == 'unit_cell':
                    param_value = ' '.join(
                        str(i) for i in param_value.parameters())

            # Correct a number of weird vartypes
            if param_type in ('convention', 'pivot', 'shapetype'):
                param_type = 'str'

            # PHIL interprets ';' as a line break
            param_doc = param_doc.replace(';', '.')

            param_line = '  {} = {} \n    .type = {}\n    .help = {}' \
                         ''.format(i, param_value, param_type, param_doc)
            param_lines.append(param_line)

        except TypeError, e:
            print i, e
            pass
Ejemplo n.º 27
0
    def modularized_exafel_api_for_GPU(self,
                                       argchk=False,
                                       cuda_background=True):
        from simtbx.gpu import gpu_energy_channels
        gpu_channels_singleton = gpu_energy_channels(deviceId=0)

        SIM = nanoBragg(self.DETECTOR, self.BEAM, panel_id=0)
        SIM.device_Id = 0

        assert gpu_channels_singleton.get_deviceID() == SIM.device_Id
        assert gpu_channels_singleton.get_nchannels() == 0  # uninitialized
        for x in range(len(self.flux)):
            gpu_channels_singleton.structure_factors_to_GPU_direct_cuda(
                x, self.sfall_channels[x].indices(),
                self.sfall_channels[x].data())
        assert gpu_channels_singleton.get_nchannels() == len(self.flux)
        SIM.Ncells_abc = (20, 20, 20)
        SIM.Amatrix = sqr(self.CRYSTAL.get_A()).transpose()
        SIM.oversample = 2
        if argchk:
            print("\npolychromatic GPU argchk")
            SIM.xtal_shape = shapetype.Gauss_argchk
        else:
            print("\npolychromatic GPU no argchk")
            SIM.xtal_shape = shapetype.Gauss
        SIM.interpolate = 0
        # allocate GPU arrays
        from simtbx.gpu import exascale_api
        gpu_simulation = exascale_api(nanoBragg=SIM)
        gpu_simulation.allocate_cuda()

        from simtbx.gpu import gpu_detector as gpud
        gpu_detector = gpud(deviceId=SIM.device_Id,
                            detector=self.DETECTOR,
                            beam=self.BEAM)
        gpu_detector.each_image_allocate_cuda()

        # loop over energies
        for x in range(len(self.flux)):
            SIM.flux = self.flux[x]
            SIM.wavelength_A = self.wavlen[x]
            print(
                "USE_EXASCALE_API+++++++++++++ Wavelength %d=%.6f, Flux %.6e, Fluence %.6e"
                % (x, SIM.wavelength_A, SIM.flux, SIM.fluence))
            gpu_simulation.add_energy_channel_from_gpu_amplitudes_cuda(
                x, gpu_channels_singleton, gpu_detector)
        per_image_scale_factor = 1.0
        gpu_detector.scale_in_place_cuda(
            per_image_scale_factor)  # apply scale directly on GPU
        SIM.wavelength_A = self.BEAM.get_wavelength(
        )  # return to canonical energy for subsequent background

        if cuda_background:
            SIM.Fbg_vs_stol = water
            SIM.amorphous_sample_thick_mm = 0.02
            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
            gpu_simulation.add_background_cuda(gpu_detector)

            # deallocate GPU arrays afterward
            gpu_detector.write_raw_pixels_cuda(
                SIM)  # updates SIM.raw_pixels from GPU
            gpu_detector.each_image_free_cuda()
        else:
            # deallocate GPU arrays up front
            gpu_detector.write_raw_pixels_cuda(
                SIM)  # updates SIM.raw_pixels from GPU
            gpu_detector.each_image_free_cuda()

            SIM.Fbg_vs_stol = water
            SIM.amorphous_sample_thick_mm = 0.02
            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.progress_meter = False
            SIM.add_background()
        return SIM
Ejemplo n.º 28
0
from __future__ import division
# test that mosaic spread is working
#from scitbx.array_family import flex
from simtbx.nanoBragg import shapetype
#from simtbx.nanoBragg import convention
from simtbx.nanoBragg import nanoBragg

# create the simulation object, all parameters have sensible defaults
SIM = nanoBragg(detpixels_slowfast=(150,150),pixel_size_mm=0.1,verbose=0)

# fairly big cell
SIM.unit_cell_tuple = (100,100,100,90,90,90)

# nice, sharp spots
SIM.Ncells_abc = (50,50,50)
SIM.xtal_shape=shapetype.Tophat

# dont bother with importing a structure, we just want spots
SIM.default_F = 1
SIM.F000 = 0.03

# the usual wavelength
SIM.wavelength_A = 1.0

# detector far away
SIM.distance_mm = 1000

# beam center near one edge
SIM.beam_center_mm = (1,7)
#print SIM.XDS_ORGXY
#print SIM.dials_origin_mm
Ejemplo n.º 29
0
def run_sim2smv(img_prefix=None, simparams=None,pdb_lines=None,crystal=None,spectra=None,rotation=None,rank=None,fsave=None,sfall_cluster=None,quick=False):
    smv_fileout = fsave

    direct_algo_res_limit = simparams.direct_algo_res_limit

    wavlen, flux, real_wavelength_A = next(spectra) # list of lambdas, list of fluxes, average wavelength
    real_flux = flex.sum(flux)
    assert real_wavelength_A > 0
    # print(rank, " ## real_wavelength_A/real_flux = ", real_wavelength_A, real_flux*1.0/simparams.flux)

    if quick:
        wavlen = flex.double([real_wavelength_A])
        flux = flex.double([real_flux])

    # GF = gen_fmodel(resolution=simparams.direct_algo_res_limit,pdb_text=pdb_lines,algorithm=simparams.fmodel_algorithm,wavelength=real_wavelength_A)
    # GF.set_k_sol(simparams.k_sol) 
    # GF.make_P1_primitive()
    sfall_main = sfall_cluster["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())

    #print("## number of N = ", N)
    SIM = nanoBragg(detpixels_slowfast=(simparams.detector_size_ny,simparams.detector_size_nx),pixel_size_mm=simparams.pixel_size_mm,\
                Ncells_abc=(N,N,N),wavelength_A=real_wavelength_A,verbose=0)
        # workaround for problem with wavelength array, specify it separately in constructor.
        
    # SIM.adc_offset_adu = 0 # Do not offset by 40
    SIM.adc_offset_adu = 10 # Do not offset by 40
    
    
    SIM.seed = 0
    # SIM.randomize_orientation()
    SIM.mosaic_spread_deg = simparams.mosaic_spread_deg # interpreted by UMAT_nm as a half-width stddev
    SIM.mosaic_domains = simparams.mosaic_domains    # 77 seconds.                                                         
    SIM.distance_mm = simparams.distance_mm

    ## setup the mosaicity
    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.beamcenter_convention=convention.ADXV
    SIM.beam_center_mm=(simparams.beam_center_x_mm, simparams.beam_center_y_mm)  # 95.975 96.855
    ######################

    # get same noise each time this test is run
    SIM.seed = 0
    SIM.oversample=simparams.oversample
    SIM.wavelength_A = real_wavelength_A
    SIM.polarization=simparams.polarization
    # this will become F000, marking the beam center
    SIM.default_F=simparams.default_F
    #SIM.missets_deg= (10,20,30)
    
    SIM.Fhkl=sfall_main
    
    Amatrix_rot = (rotation * sqr(sfall_main.unit_cell().orthogonalization_matrix())).transpose()

    SIM.Amatrix_RUB = Amatrix_rot
    #workaround for failing init_cell, use custom written Amatrix setter
    # print("## inside run_sim2smv, Amat_rot = ", Amatrix_rot)
    
    Amat = sqr(SIM.Amatrix).transpose() # recovered Amatrix from SIM
    
    Ori = crystal_orientation.crystal_orientation(Amat, crystal_orientation.basis_type.reciprocal)
    
    SIM.xtal_shape=shapetype.Gauss # both crystal & RLP are Gaussian

    SIM.progress_meter=False

    # SIM.show_params()
    # flux is always in photons/s
    SIM.flux=real_flux
    SIM.exposure_s=simparams.exposure_s 
    # assumes round beam
    SIM.beamsize_mm=simparams.beamsize_mm #cannot make this 3 microns; spots are too intense
    temp=SIM.Ncells_abc
    SIM.Ncells_abc=temp
    

    # print("## domains_per_crystal = ", crystal.domains_per_crystal)
    SIM.raw_pixels *= crystal.domains_per_crystal # must calculate the correct scale!

    # print("## Initial raw_pixels = ", flex.sum(SIM.raw_pixels))

    for x in range(len(flux)):
        # CH = channel_pixels(wavlen[x],flux[x],N,UMAT_nm,Amatrix_rot,sfall_cluster[x],rank)
        # print("## in loop wavlen/flux/real_wavelength_A = ", wavlen[x], flux[x]/real_flux, real_wavelength_A)
        CH = channel_pixels(simparams=simparams,single_wavelength_A=wavlen[x],single_flux=flux[x],N=N,UMAT_nm=UMAT_nm, \
                Amatrix_rot=Amatrix_rot,sfall_channel=sfall_cluster[x],rank=rank)
        SIM.raw_pixels += CH.raw_pixels * crystal.domains_per_crystal
        CHDBG_singleton.extract(channel_no=x, data=CH.raw_pixels)
        CH.free_all()
        # print("## sum raw_pixels after ", x, "is", flex.sum(SIM.raw_pixels))

        
    # 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 = simparams.water_sample_thick_mm
    SIM.amorphous_density_gcm3 = simparams.water_density_gcm3
    SIM.amorphous_molecular_weight_Da = simparams.water_molecular_weight_Da
    SIM.flux=real_flux
    SIM.beamsize_mm=simparams.beamsize_mm   # square (not user specified)
    SIM.exposure_s=simparams.exposure_s     # 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 = simparams.air_sample_thick_mm # between beamstop and collimator
    SIM.amorphous_density_gcm3 = simparams.air_density_gcm3
    SIM.amorphous_sample_molecular_weight_Da = simparams.air_molecular_weight_Da  # nitrogen = N2
    SIM.add_background()

    #apply beamstop mask here

    # settings for CCD
    SIM.detector_psf_kernel_radius_pixels=simparams.detector_psf_kernel_radius_pixels
    SIM.detector_psf_type=shapetype.Unknown # for CSPAD
    SIM.detector_psf_fwhm_mm=simparams.detector_psf_fwhm_mm
    SIM.apply_psf()

    SIM.add_noise() 

    extra = "PREFIX=%s;\nRANK=%d;\n"%(img_prefix,rank)
    SIM.to_smv_format_py(fileout=smv_fileout,intfile_scale=1,rotmat=True,extra=extra,gz=True)
    SIM.free_all()
Ejemplo n.º 30
0
    def perform_simulations(self, spectrum, crystal, tophat_spectrum=True):
        #borrow code from util_partiality.
        #def run_sim2smv(ROI,prefix,crystal,spectra,rotation,rank,tophat_spectrum=True,quick=False):

        direct_algo_res_limit = 1.7

        self.wavlen, self.flux, self.wavelength_A = next(
            spectrum)  # list of lambdas, list of fluxes, average wavelength

        if tophat_spectrum:
            sum_flux = flex.sum(self.flux)
            ave_flux = sum_flux / 60.  # 60 energy channels
            for ix in range(len(self.wavlen)):
                energy = 12398.425 / self.wavlen[ix]
                if energy >= 7090 and energy <= 7150:
                    self.flux[ix] = ave_flux
                else:
                    self.flux[ix] = 0.

        # use crystal structure to initialize Fhkl array
        self.sfall_main.show_summary(prefix="Amplitudes used ")
        N = crystal.number_of_cells(self.sfall_main.unit_cell())
        self.crystal = crystal  # delete later
        self.N = N  # delete later
        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=self.wavelength_A,
            verbose=0)
        self.SIM = SIM
        SIM.adc_offset_adu = 10  # Do not offset by 40
        SIM.mosaic_spread_deg = 0.05  # interpreted by UMAT_nm as a half-width stddev
        SIM.mosaic_domains = 50  # mosaic_domains setter must come after mosaic_spread_deg setter
        #manuscript says 200, June 15 abc_cov was done with 50
        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)
        self.UMAT_nm = UMAT_nm  # delete later

        # get same noise each time this test is run
        SIM.seed = 1
        SIM.oversample = 1
        SIM.wavelength_A = self.wavelength_A
        SIM.polarization = 1
        # this will become F000, marking the beam center
        SIM.default_F = 0
        #SIM.Fhkl=self.sfall_main # no it turns out we don't use these calculations for abc_coverage

        # use local file with (open(something,"wb")) as F:
        with (open("confirm_sfall_P1_7122_amplitudes.pickle", "rb")) as F:
            sfall_P1_7122_amplitudes = pickle.load(F)
        SIM.Fhkl = sfall_P1_7122_amplitudes

        SIM.xtal_shape = shapetype.Gauss  # both crystal & RLP are Gaussian
        SIM.progress_meter = False
        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