Exemplo n.º 1
0
 def test_tth(self):
     # generate random gvector
     hkl = n.array([
         round(n.random.rand() * 10 - 5),
         round(n.random.rand() * 10 - 5),
         round(n.random.rand() * 10 - 5)
     ])
     ucell = n.array([
         3.5 + n.random.rand(), 3.5 + n.random.rand(),
         3.5 + n.random.rand(), 89.5 + n.random.rand(),
         89.5 + n.random.rand(), 89.5 + n.random.rand()
     ])
     B = tools.form_b_mat(ucell)
     U = tools.euler_to_u(n.random.rand() * 2. * n.pi,
                          n.random.rand() * 2. * n.pi,
                          n.random.rand() * n.pi)
     wavelength = 0.95 + n.random.rand() * 0.1
     gvec = n.dot(U, n.dot(B, hkl))
     tth = tools.tth(ucell, hkl, wavelength)
     tth2 = tools.tth2(gvec, wavelength)
     diff = n.abs(tth - tth2)
     self.assertAlmostEqual(diff, 0, 9)
Exemplo n.º 2
0
    def run(self, start_voxel, end_voxel, log=True, parallel=None):
        '''
        This is the main algorithmic loop that will calculate the diffraction pattern
        based on a per-voxel definition of the sample. The algorithm deals with one voxel at the
        time, computing all reflections that would be generated for that voxel in a single go.
        Therefore, the run time is linear with voxel number regardless of the number of y-scans.

        Input: start_voxel: Used for running in parrallel, equal to 0 for 1 cpu
               end_voxel:   Used for running in parrallel, equal to number of voxels for 1 cpu
               log:         used to determine of this process should print to stdout or be quiet.
               parallel:    Flag to indicate parallel mode. Such a mode requires that we pass the result
                            of the run to the calling process.
        '''
        self.param['lorentz_apply']=1


        tth_lower_bound = self.param['two_theta_lower_bound']*np.pi/180.
        tth_upper_bound = self.param['two_theta_upper_bound']*np.pi/180.


        spot_id = 0

        # Compute used quanteties once for speed
        #--------------------------------------------
        # Don't make static dictonary calls in loop, slower..
        no_voxels = self.param['no_voxels']
        beam_width = self.param['beam_width']
        omega_start = self.param['omega_start']*np.pi/180
        omega_end = self.param['omega_end']*np.pi/180
        wavelength = self.param['wavelength']
        scale_Gw = wavelength/(4.*np.pi)
        detz_center = self.param['detz_center']
        dety_center = self.param['dety_center']
        z_size = self.param['z_size']
        y_size = self.param['y_size']
        distance = self.param['distance']
        wavelength = self.param['wavelength']


        if log==True:
            print('no of voxels ',no_voxels)
        # print(start_voxel, end_voxel)
        # with open('solution','w') as f:
        #     for voxel_nbr in range(start_voxel, end_voxel):
        #         if len(self.param['phase_list']) == 1:
        #             phase = self.param['phase_list'][0]
        #         else:
        #             phase = self.param['phase_voxels_%s' %(self.param['voxel_list'][voxel_nbr])]
        #         unit_cell = self.param['unit_cell_phase_%i' %phase]
        #         U = self.param['U_voxels_%s' %(self.param['voxel_list'][voxel_nbr])]
        #         voxel_eps = np.array(self.param['eps_voxels_%s' %(self.param['voxel_list'][voxel_nbr])])
        #         B = tools.epsilon_to_b(voxel_eps,unit_cell)
        #         UB = np.dot(U,B)
        #         ub = UB.ravel()
        #         f.write("np.array([")
        #         for val in ub:
        #             f.write(str(val)+", ")
        #         f.write("])\n")
        # raise
        for voxel_nbr in range(start_voxel, end_voxel):
            A = []
            U = self.param['U_voxels_%s' %(self.param['voxel_list'][voxel_nbr])]
            if len(self.param['phase_list']) == 1:
                phase = self.param['phase_list'][0]
            else:
                phase = self.param['phase_voxels_%s' %(self.param['voxel_list'][voxel_nbr])]
            unit_cell = self.param['unit_cell_phase_%i' %phase]
            self.voxel[voxel_nbr] = variables.voxel_cont(U)
            voxel_pos = np.array(self.param['pos_voxels_%s' %(self.param['voxel_list'][voxel_nbr])])
            voxel_eps = np.array(self.param['eps_voxels_%s' %(self.param['voxel_list'][voxel_nbr])])
            # Calculate the B-matrix based on the strain tensor for each voxel
            B = tools.epsilon_to_b(voxel_eps,unit_cell)
            # add B matrix to voxel container
            self.voxel[voxel_nbr].B = B
            V = tools.cell_volume(unit_cell)
            voxel_vol = np.pi/6 * self.param['size_voxels_%s' %self.param['voxel_list'][voxel_nbr]]**3
            nrefl = 0

            for hkl in self.hkl[self.param['phase_list'].index(phase)]:

                check_input.interrupt(self.killfile)
                Gc = np.dot(B,hkl[0:3])
                Gw = np.dot(self.S,np.dot(U,Gc))
                # if hkl[0]==0 and hkl[1]==-2 and hkl[2]==0:
                #     print(np.dot(U,B))
                #     print(hkl[0:3])
                #     print(Gw)
                #     raise
                tth = tools.tth2(Gw,wavelength)

                # If specified in input file do not compute for two
                # thetaoutside range [tth_lower_bound, tth_upper_bound]
                # if tth_upper_bound<tth or tth<tth_lower_bound :
                #     continue

                costth = np.cos(tth)
                (Omega, Eta, Omega_mat, Gts) = self.find_omega_general(Gw*scale_Gw,scale_Gw,tth)

                for omega,eta,Om,Gt in zip(Omega, Eta, Omega_mat, Gts):

                    if  omega_start < omega and omega < omega_end:
                        #print(np.degrees(omega))
                        # Calc crystal position at present omega
                        [tx,ty,tz]= np.dot(Om,voxel_pos)

                        #The 3 beam y positions that could illuminate the voxel
                        beam_centre_1 = np.round(ty/beam_width)*beam_width
                        beam_centre_0 = beam_centre_1+beam_width
                        beam_centre_2 = beam_centre_1-beam_width

                        #Compute precentual voxel beam overlap for the three regions
                        overlaps_info = convexPolygon.compute_voxel_beam_overlap(tx,ty,omega,beam_centre_1, beam_width)

                        regions = [ beam_centre_0, beam_centre_1,beam_centre_2]
                        for beam_centre_y,info in zip(regions,overlaps_info):

                            # perhaps we should have a less restrictive
                            # comparision here, like: if intensity_modifier<0.0001,
                            # there is some possibilty to simulate nosie here

                            intensity_modifier = info[0]

                            if intensity_modifier==0:
                                continue


                            #Lorentz factor
                            if self.param['lorentz_apply'] == 1:
                                if eta != 0:
                                    L=1/(np.sin(tth)*abs(np.sin(eta)))
                                else:
                                    L=np.inf;
                            else:
                                L = 1.0

                            intensity = L*intensity_modifier*hkl[3]

                            # If no unit cells where activated we continue

                            dty = beam_centre_y*1000. # beam cetre in microns
                            cx = info[1]
                            cy = info[2]


                            # Compute peak based on cms of illuminated voxel fraction
                            tx = cx
                            ty = cy - beam_centre_y # The sample moves not the beam!
                            # print("tx",tx)
                            # print("ty",ty)
                            # print("")
                            tz = 0
                            (dety, detz) = self.det_coor(Gt,
                                                        costth,
                                                        wavelength,
                                                        distance,
                                                        y_size,
                                                        z_size,
                                                        dety_center,
                                                        detz_center,
                                                        self.R,
                                                        tx,ty,tz)

                            #If shoebox extends outside detector exclude it
                            if (-0.5 > dety) or\
                            (dety > self.param['dety_size']-0.5) or\
                            (-0.5 > detz) or\
                            (detz > self.param['detz_size']-0.5):
                                continue


                            if self.param['spatial'] != None :
                                # To match the coordinate system of the spline file
                                (x,y) = detector.detyz_to_xy([dety,detz],
                                                            self.param['o11'],
                                                            self.param['o12'],
                                                            self.param['o21'],
                                                            self.param['o22'],
                                                            self.param['dety_size'],
                                                            self.param['detz_size'])
                                # Do the spatial distortion
                                (xd,yd) = self.spatial.distort(x,y)
                                # transform coordinates back to dety,detz
                                (detyd,detzd) = detector.xy_to_detyz([xd,yd],
                                                        self.param['o11'],
                                                        self.param['o12'],
                                                        self.param['o21'],
                                                        self.param['o22'],
                                                        self.param['dety_size'],
                                                        self.param['detz_size'])
                            else:
                                detyd = dety
                                detzd = detz



                            if self.param['beampol_apply'] == 1:
                                #Polarization factor (Kahn, J. Appl. Cryst. (1982) 15, 330-337.)
                                rho = np.pi/2.0 + eta + self.param['beampol_direct']*np.pi/180.0
                                P = 0.5 * (1 + costth*costth -\
                                        self.param['beampol_factor']*np.cos(2*rho)*np.sin(tth)**2)
                            else:
                                P = 1.0

                            overlaps = 0 # set the number overlaps to zero

                            # if self.param['intensity_const'] != 1:
                            #     intensity = intensity_modifier*int_intensity(hkl[3],
                            #                                                 L,
                            #                                                 P,
                            #                                                 self.param['beamflux'],
                            #                                                 self.param['wavelength'],
                            #                                                 V,
                            #                                                 voxel_vol)

                            # else:
                            #     intensity = intensity_modifier*hkl[3]



                            #TODO: try and build up the images as we go
                            # Mapp reflection to the correct image

                            self.peak_merger.add_reflection_to_images([self.param['voxel_list'][voxel_nbr],
                                                        nrefl,spot_id,
                                                        hkl[0],hkl[1],hkl[2],
                                                        tth,omega,eta,
                                                        dety,detz,
                                                        detyd,detzd,
                                                        Gw[0],Gw[1],Gw[2],
                                                        L,P,hkl[3],intensity,overlaps,dty,1])

                            A.append([self.param['voxel_list'][voxel_nbr],
                                    nrefl,spot_id,
                                    hkl[0],hkl[1],hkl[2],
                                    tth,omega,eta,
                                    dety,detz,
                                    detyd,detzd,
                                    Gw[0],Gw[1],Gw[2],
                                    L,P,hkl[3],intensity,overlaps,dty,1])
                            nrefl = nrefl+1

                            #TODO: When run in parallel the spot id might be duplicated..
                            #but who cares about spot id anyways huh..
                            spot_id = spot_id+1


            # A = np.array(A)

            # if len(A) > 0:
            #     # sort rows according to omega
            #     A = A[np.argsort(A,0)[:,A_id['omega']],:]

            #     # Renumber the reflections
            #     A[:,A_id['ref_id']] = np.arange(nrefl)

            #     # Renumber the spot_id
            #     A[:,A_id['spot_id']] = np.arange(np.min(A[:,A_id['spot_id']]),
            #                                 np.max(A[:,A_id['spot_id']])+1)
            # else:
            #     A = np.zeros((0,len(A_id)))

            # save reflection info in voxel container, if we are not in parallel mode this
            # will be the same object that we ran the run() method upon. Otherwise it is by
            # defualt a copy of that object and we will need to pass the result to the calling
            # process at the end of the algorithm

            self.voxel[voxel_nbr].refs = A

            if parallel and log==True:
                progress = int(100*(voxel_nbr+1-start_voxel)/float(end_voxel-start_voxel))
                print('\rDone approximately %3i percent' %progress, end=' ')
                sys.stdout.flush()
            elif log==True:
                print('\rDone approximately %3i voxel(s) of %3i' %(voxel_nbr+1,self.param['no_voxels']), end=' ')
                sys.stdout.flush()

        if log==True:
            print('\n')


        # if we are running in parrallel the memory cannot be shared (easily)
        # so wee return our result in order to merge all results
        # into a single find_refl.py Object.

        if parallel:
            result = {"start_voxel": start_voxel,
                      "end_voxel": end_voxel,
                      "refl_list": self.voxel}
            return result
        else:
            # if not in parallel we may merge here
            # this allows us to include analyse_images_as_clusters()
            # easy in profiling
            self.peak_merger.analyse_images_as_clusters()
Exemplo n.º 3
0
def find_refl(inp):
        """
        From U, (x,y,z) and B determined for the far-field case 
        calculate the possible reflection on the near-field detector 
        output[grainno][reflno]=[h,k,l,omega,dety,detz,tth,eta]
        """
        S = n.array([[1, 0, 0],[0, 1, 0],[0, 0, 1]])
        R = tools.detect_tilt(inp.param['tilt_x'],
                              inp.param['tilt_y'],
                              inp.param['tilt_z'])
        inp.possible = []

        # if structure info is given use this
        if  inp.files['structure_file'] != None:
            inp.param['structure_phase_0'] = inp.files['structure_file']
            xtal_structure = reflections.open_structure(inp.param,0)
            HKL = reflections.gen_miller(inp.param,0)
        else:
            inp.param['unit_cell_phase_0'] = inp.unit_cell		
            inp.param['sgno_phase_0'] = inp.fit['sgno']
            HKL = reflections.gen_miller(inp.param,0)

        for grainno in range(inp.no_grains):
            inp.possible.append([])
            if grainno+1 not in inp.fit['skip']:
                B = tools.epsilon_to_b(n.array([inp.values['epsaa%s' %grainno],
                                                inp.values['epsab%s' %grainno],
                                                inp.values['epsac%s' %grainno],
                                                inp.values['epsbb%s' %grainno],
                                                inp.values['epsbc%s' %grainno],
                                                inp.values['epscc%s' %grainno]]),
                                       inp.unit_cell)
                U = tools.rod_to_u([inp.rod[grainno][0]+inp.values['rodx%s' %grainno],
                                    inp.rod[grainno][1]+inp.values['rody%s' %grainno],
                                    inp.rod[grainno][2]+inp.values['rodz%s' %grainno]])
                gr_pos = n.array([inp.values['x%s' %grainno],
                                  inp.values['y%s' %grainno],
                                  inp.values['z%s' %grainno]])
            

  
                for hkl in HKL:
                    Gc = n.dot(B,hkl[0:3])
                    Gw = n.dot(S,n.dot(U,Gc))
                    tth = tools.tth2(Gw,inp.param['wavelength'])
#                    print hkl[0:3],tth*180./n.pi
                    costth = n.cos(tth)
                    (Omega, Eta) = tools.find_omega_general(inp.param['wavelength']/(4.*n.pi)*Gw,
                                                            tth,
                                                            inp.values['wx']*n.pi/180,
                                                            inp.values['wy']*n.pi/180)  
                    if len(Omega) > 0:
                        for solution in range(len(Omega)):
                            omega = Omega[solution]
                            eta = Eta[solution]
                            for i in range(len(inp.fit['w_limit'])//2):
                                if  (inp.fit['w_limit'][2*i]*n.pi/180) < omega and\
                                    omega < (inp.fit['w_limit'][2*i+1]*n.pi/180):
                                # form Omega rotation matrix
                                    Om = tools.form_omega_mat_general(omega,inp.values['wx']*n.pi/180,inp.values['wy']*n.pi/180)  
                                    Gt = n.dot(Om,Gw) 
                                # Calc crystal position at present omega
                                    [tx,ty,tz]= n.dot(Om,gr_pos)
                                # Calc detector coordinate for peak 
                                    (dety, detz) = detector.det_coor(Gt,costth,
                                                                    inp.param['wavelength'],
                                                                    inp.param['distance'],
                                                                    inp.param['y_size'],
                                                                    inp.param['z_size'],
                                                                    inp.param['y_center'],
                                                                    inp.param['z_center'],
                                                                    R,tx,ty,tz)
                            #If peak within detector frame store it in possible
                                    if (-0.5 < dety) and\
                                        (dety < inp.fit['dety_size']-0.5) and\
                                        (-0.5 < detz) and\
                                        (detz < inp.fit['detz_size']-0.5):
                                        inp.possible[grainno].append([hkl[0],
                                                                      hkl[1],
                                                                      hkl[2],
                                                                      omega*180/n.pi,
                                                                      dety,
                                                                      detz,
                                                                      tth,
                                                                      eta])
Exemplo n.º 4
0
    def run(self):
        spot_id = 0
        # Generate orientations of the grains and loop over all grains
        print('no of grains ', self.param['no_grains'])
        for grainno in range(self.param['no_grains']):
            A = []
            U = self.param['U_grains_%s' % (self.param['grain_list'][grainno])]
            if len(self.param['phase_list']) == 1:
                phase = self.param['phase_list'][0]
            else:
                phase = self.param['phase_grains_%s' %
                                   (self.param['grain_list'][grainno])]
            unit_cell = self.param['unit_cell_phase_%i' % phase]
            self.grain.append(variables.grain_cont(U))
            gr_pos = n.array(self.param['pos_grains_%s' %
                                        (self.param['grain_list'][grainno])])
            gr_eps = n.array(self.param['eps_grains_%s' %
                                        (self.param['grain_list'][grainno])])
            # Calculate the B-matrix based on the strain tensor for each grain
            B = tools.epsilon_to_b(gr_eps, unit_cell)
            # add B matrix to grain container
            self.grain[grainno].B = B
            V = tools.cell_volume(unit_cell)
            grain_vol = n.pi / 6 * self.param[
                'size_grains_%s' % self.param['grain_list'][grainno]]**3

            #            print 'GRAIN NO: ',self.param['grain_list'][grainno]
            #            print 'GRAIN POSITION of grain ',self.param['grain_list'][grainno],': ',gr_pos
            #            print 'STRAIN TENSOR COMPONENTS (e11 e12 e13 e22 e23 e33) of grain ',self.param['grain_list'][grainno],':\n',gr_eps
            #            print 'U of grain ',self.param['grain_list'][grainno],':\n',U
            nrefl = 0

            # Calculate these values:
            # totalnr, grainno, refno, hkl, omega, 2theta, eta, dety, detz
            # For all reflections in Ahkl that fulfill omega_start < omega < omega_end.
            # All angles in Grain are in degrees
            for hkl in self.hkl[self.param['phase_list'].index(phase)]:
                check_input.interrupt(self.killfile)
                Gc = n.dot(B, hkl[0:3])
                Gw = n.dot(self.S, n.dot(U, Gc))
                tth = tools.tth2(Gw, self.param['wavelength'])
                costth = n.cos(tth)
                (Omega, Eta) = tools.find_omega_general(
                    Gw * self.param['wavelength'] / (4. * n.pi), tth, self.wx,
                    self.wy)
                if len(Omega) > 0:
                    for solution in range(len(Omega)):
                        omega = Omega[solution]
                        eta = Eta[solution]
                        if  (self.param['omega_start']*n.pi/180) < omega and\
                                omega < (self.param['omega_end']*n.pi/180):
                            # form Omega rotation matrix
                            Om = tools.form_omega_mat_general(
                                omega, self.wx, self.wy)
                            Gt = n.dot(Om, Gw)

                            # Calc crystal position at present omega and continue if this is outside the illuminated volume
                            [tx, ty, tz] = n.dot(Om, gr_pos)
                            if self.param['beam_width'] != None:
                                if n.abs(ty) > self.param['beam_width'] / 2.:
                                    continue

                            # Calc detector coordinate for peak
                            (dety, detz) = detector.det_coor(
                                Gt, costth, self.param['wavelength'],
                                self.param['distance'], self.param['y_size'],
                                self.param['z_size'],
                                self.param['dety_center'],
                                self.param['detz_center'], self.R, tx, ty, tz)

                            #If shoebox extends outside detector exclude it
                            if (-0.5 > dety) or\
                               (dety > self.param['dety_size']-0.5) or\
                               (-0.5 > detz) or\
                               (detz > self.param['detz_size']-0.5):
                                continue

                            if self.param['spatial'] != None:
                                # To match the coordinate system of the spline file
                                (x, y) = detector.detyz_to_xy(
                                    [dety, detz], self.param['o11'],
                                    self.param['o12'], self.param['o21'],
                                    self.param['o22'], self.param['dety_size'],
                                    self.param['detz_size'])
                                # Do the spatial distortion
                                (xd, yd) = self.spatial.distort(x, y)
                                # transform coordinates back to dety,detz
                                (detyd, detzd) = detector.xy_to_detyz(
                                    [xd, yd], self.param['o11'],
                                    self.param['o12'], self.param['o21'],
                                    self.param['o22'], self.param['dety_size'],
                                    self.param['detz_size'])
                            else:
                                detyd = dety
                                detzd = detz

                            if self.param['beampol_apply'] == 1:
                                #Polarization factor (Kahn, J. Appl. Cryst. (1982) 15, 330-337.)
                                rho = n.pi / 2.0 + eta + self.param[
                                    'beampol_direct'] * n.pi / 180.0
                                P = 0.5 * (1 + costth*costth -\
                                         self.param['beampol_factor']*n.cos(2*rho)*n.sin(tth)**2)
                            else:
                                P = 1.0

                            #Lorentz factor
                            if self.param['lorentz_apply'] == 1:
                                if eta != 0:
                                    L = 1 / (n.sin(tth) * abs(n.sin(eta)))
                                else:
                                    L = n.inf
                            else:
                                L = 1.0

                            overlaps = 0  # set the number overlaps to zero

                            if self.param['intensity_const'] != 1:
                                intensity = int_intensity(
                                    hkl[3], L, P, self.param['beamflux'],
                                    self.param['wavelength'], V, grain_vol)
                            else:
                                intensity = hkl[3]

                            A.append([
                                self.param['grain_list'][grainno], nrefl,
                                spot_id, hkl[0], hkl[1], hkl[2], tth, omega,
                                eta, dety, detz, detyd, detzd, Gw[0], Gw[1],
                                Gw[2], L, P, hkl[3], intensity, overlaps
                            ])
                            nrefl = nrefl + 1
                            spot_id = spot_id + 1

#           print 'Length of Grain', len(self.grain[0].refl)
            A = n.array(A)
            if len(A) > 0:
                # sort rows according to omega
                A = A[n.argsort(A, 0)[:, A_id['omega']], :]

                # Renumber the reflections
                A[:, A_id['ref_id']] = n.arange(nrefl)

                # Renumber the spot_id
                A[:,
                  A_id['spot_id']] = n.arange(n.min(A[:, A_id['spot_id']]),
                                              n.max(A[:, A_id['spot_id']]) + 1)
            else:
                A = n.zeros((0, len(A_id)))
            # save reflection info in grain container
            self.grain[grainno].refs = A
            print('\rDone %3i grain(s) of %3i' %
                  (grainno + 1, self.param['no_grains']),
                  end=' ')
            sys.stdout.flush()

        print('\n')
Exemplo n.º 5
0
pos = [0.0, 0.0, 0.0]

hkl = n.array([1, 1, 1]) * 3

unit_cell = [4.04975, 4.04975, 4.04975, 90, 90, 90]
B = tools.form_b_mat(unit_cell)
Gc = n.dot(B, hkl)
Gw = n.dot(U, Gc)

wavedisp = 1e-3
sigma_limit = 2.

wavelength_0 = 0.4859292

wavelength = wavelength_0
tth = tools.tth2(Gw, wavelength)
(Omega, Eta) = tools.find_omega_wedge(Gw, tth, wedge)
omega_0 = Omega[1] * 180. / n.pi
Om = tools.form_omega_mat(Omega[1])
Gt = n.dot(Om, Gw)
(dety, detz) = detector.det_coor(Gt, n.cos(tth), wavelength, distance,
                                 y_size, z_size, dety_center, detz_center,
                                 n.identity(3), 0, 0, 0)

# Calculate detector position MINUS sigma_limit times sigma
wavelength = wavelength_0 * (1.0 + wavedisp / 2. * sigma_limit)
tth = tools.tth2(Gw, wavelength)
(Omega, Eta) = tools.find_omega_wedge(Gw, tth, wedge)
omega_p = Omega[1] * 180. / n.pi
Om = tools.form_omega_mat(Omega[1])
Gt = n.dot(Om, Gw)
Exemplo n.º 6
0
    def forward_model(self, start_voxel, end_voxel, UB_matrices, gradient_mode='No', log=True, parallel=None):
        '''
        This is the main algorithmic loop that will calculate the diffraction pattern
        based on a per-voxel definition of the sample. The algorithm deals with one voxel at the
        time, computing all reflections that would be generated for that voxel in a single go.
        Therefore, the run time is linear with voxel number regardless of the number of y-scans.

        Input: start_voxel: Used for running in parrallel, equal to 0 for 1 cpu
               end_voxel:   Used for running in parrallel, equal to number of voxels for 1 cpu
               log:         used to determine of this process should print to stdout or be quiet.
               parallel:    Flag to indicate parallel mode. Such a mode requires that we pass the result
                            of the run to the calling process.
        '''


        for voxel_nbr in range(start_voxel, end_voxel):

            voxel_reflections = []
            if gradient_mode=='No':
                self.voxel[voxel_nbr]=variables.voxel_cont()
            UB = UB_matrices[voxel_nbr]

            # print(UB.ravel())
            # raise
            #UB = UBs_true[voxel_nbr]
            voxel_pos = self.voxel_positions[voxel_nbr] #should be np array

            # th_min=90
            # th_max=0

            for hkl in self.hkl[0]:
                Gw = np.dot( self.S , np.dot( UB, hkl[0:3] ) )
                tth = tools.tth2(Gw,self.wavelength)

                costth = np.cos(tth)
                (Omega, Eta, Omega_mat, Gts) = self.find_omega_general(Gw*self.scale_Gw,self.scale_Gw,tth)

                # if hkl[0]==1 and hkl[1]==0 and hkl[2]==-3:
                #     print(Omega*180./np.pi)
                #     raise
                for omega,eta,Om,Gt in zip(Omega, Eta, Omega_mat, Gts):
                    if  self.omega_start < omega and omega < self.omega_end:

                        # Calc crystal position at present omega
                        [tx,ty,tz]= np.dot(Om,voxel_pos)

                        #The 3 beam y positions that could illuminate the voxel
                        beam_centre_1 = np.round(ty/self.beam_width)*self.beam_width
                        beam_centre_0 = beam_centre_1+self.beam_width
                        beam_centre_2 = beam_centre_1-self.beam_width

                        #Compute precentual voxel beam overlap for the three regions
                        overlaps_info = convexPolygon.compute_voxel_beam_overlap(tx,ty,omega,beam_centre_1, self.beam_width)

                        regions = [ beam_centre_0, beam_centre_1,beam_centre_2]
                        for beam_centre_y,info in zip(regions,overlaps_info):

                            intensity_modifier = info[0]

                            if intensity_modifier==0:
                                continue

                            #Lorentz factor
                            if self.lorentz_apply == 1:
                                if eta != 0:
                                    L=1/(np.sin(tth)*abs(np.sin(eta)))
                                else:
                                    L=np.inf;
                            else:
                                L = 1.0

                            #intensity = L*intensity_modifier*hkl[3]
                            intensity = intensity_modifier

                            # If no unit cells where activated we continue


                            dty = beam_centre_y*1000. # beam cetre in microns
                            cx = info[1]
                            cy = info[2]

                            # Compute peak based on cms of illuminated voxel fraction
                            tx = cx
                            ty = cy - beam_centre_y # The sample moves not the beam!
                            tz = 0
                            (dety, detz) = self.det_coor(Gt,
                                                        costth,
                                                        self.wavelength,
                                                        self.distance,
                                                        self.y_size,
                                                        self.z_size,
                                                        self.dety_center,
                                                        self.detz_center,
                                                        self.R,
                                                        tx,ty,tz)

                            # if abs(hkl[0]-5)<0.01 and abs(hkl[1]+6)<0.01 and abs(hkl[2]+3)<0.01:
                            #     print("hkl",hkl)
                            #     print(tth*180/np.pi)
                            #     print(dty)
                            #     raise

                            #If shoebox extends outside detector exclude it
                            if (-0.5 > dety) or\
                            (dety > self.dety_size-0.5) or\
                            (-0.5 > detz) or\
                            (detz > self.detz_size-0.5):
                                continue

                            dty_as_index = self.get_index_of_dty( dty )

                            voxel_reflections.append([dety,detz,intensity,omega,dty,hkl[0],hkl[1],hkl[2],voxel_nbr,dty_as_index])


            if gradient_mode=='No':
                self.voxel[voxel_nbr].refs = voxel_reflections
            else:
                return (voxel_reflections, voxel_nbr)