def testPerpendicularTo(self): vector = Vector(1, 1, 3) vector_z = Vector(0, 0, 1) result = vector.perpendicularTo(vector_z) self.assertTrue(result == Vector(1, 1, 0))
def testOperatorNotEqual(self): vector_1 = Vector(1, 2, 3) vector_2 = Vector(1.0, 2.0, 3.0) vector_3 = Vector(-1, 0, 0) self.assertFalse(vector_1 != vector_2) self.assertTrue(vector_1 != vector_3)
def testGetNormalizedVector(self): vector_6 = Vector(2, 4, 4) normalized_vector = vector_6.getNormalizedVector() self.assertAlmostEqual(normalized_vector.norm(), 1.0) self.assertTrue(vector_6 == normalized_vector.scalarMultiplication(6))
def vectorH(self): """ Calculates the H vector, normal on the reflection lattice plane, with modulus 2 pi / d_spacing (SI). normal to Bragg planes obtained by rotating vnor an angle equal to minuns asymmetry angle (-alphaXOP) around X using rodrigues rotation (in the screw direction (cw) when looking in the axis direction), and then an angle phi (azimuthal angle) around Z :param return_normalized: if True the returned vector is normalized. :return: B_H vector """ # Edoardo: I use the geometrical convention from # M.Sanchez del Rio et al., J.Appl.Cryst.(2015). 48, 477-491. g_modulus = 2.0 * numpy.pi / (self.dSpacingSI()) # Let's start from a vector parallel to the surface normal (z axis). temp_normal_bragg = Vector(0, 0, 1).scalarMultiplication(g_modulus) # Let's now rotate this vector of an angle alphaX around the y axis (according to the right-hand-rule). alpha_x = self.asymmetryAngle() axis = self.vectorParallelSurface().crossProduct( self.vectorNormalSurface()) # should be Vector(1, 0, 0) temp_normal_bragg = temp_normal_bragg.rotateAroundAxis(axis, -alpha_x) # Let's now rotate this vector of an angle phi around the z axis (following the ISO standard 80000-2:2009). phi = self.azimuthalAngle() normal_bragg = temp_normal_bragg.rotateAroundAxis(Vector(0, 0, 1), phi) return normal_bragg
def testParallelTo(self): vector = Vector(1, 1, 3) vector_z = Vector(0, 0, 1) result = vector.parallelTo(vector_z) self.assertTrue(result == Vector(0, 0, 3))
def testSubtractVector(self): vector_1 = Vector(1, 2, 3) vector_2 = Vector(-1, 2, 1) vector_diff = Vector(2, 0, 2) result = vector_1.subtractVector(vector_2) self.assertTrue(result == vector_diff)
def test_operator_not_equal(self): candidate = PolarizedPhoton(8000.00, Vector(0.5, 1.5, 1.0), StokesVector([1.0, -1.0, 0.0, 0.0])) candidate1 = PolarizedPhoton( 8000.01, Vector(0.5000002, 1.5, 1.0), StokesVector([1.0 + 1e-4, -1.0, 0.0, 0.0])) self.assertTrue(self.polarized_photon != candidate1) self.assertFalse(self.polarized_photon != candidate)
def testGetOnePerpendicularVector(self): for vector in [Vector(1, 1, 3), Vector(10, 1222, 23), Vector(0.1, 12, -3), Vector(0, 0, 1)]: result = vector.getOnePerpendicularVector() self.assertAlmostEqual(result.scalarProduct(vector), 0.0)
def testAddVector(self): vector_1 = Vector(1, 2, 3) vector_2 = Vector(-1, 2, 1) vector_sum = Vector(0, 4, 4) result = vector_1.addVector(vector_2) self.assertTrue(result == vector_sum)
def testGetVectorWithAngle(self): for vector in [Vector(0, 1, 0), Vector(1, 2, 3), Vector(3, 2, -1)]: for angle in arange(0, pi, 0.1): vector_with_angle = vector.getVectorWithAngle(angle) self.assertAlmostEqual(vector.angle(vector_with_angle), angle)
def testOperatorNotEqual(self): photon_one = Photon(4000, Vector(0, 0, 5)) photon_two = Photon(4000, Vector(0, 1, 1)) photon_three = Photon(2000, Vector(0, 0, 5)) self.assertFalse(photon_one != photon_one) self.assertTrue(photon_one != photon_two) self.assertTrue(photon_one != photon_three) self.assertTrue(photon_two != photon_three)
def testConstructor(self): vector = Vector(1, 2, 3) self.assertIsInstance(vector, Vector) self.assertAlmostEqual(vector.components()[0], 1) self.assertAlmostEqual(vector.components()[1], 2) self.assertAlmostEqual(vector.components()[2], 3)
def testConstructor(self): photon = PolarizedPhoton(energy_in_ev=8000, direction_vector=Vector(0.0, 1.0, 0.0), stokes_vector=StokesVector( [1.0, 0.0, 1.0, 0.0])) self.assertIsInstance(photon, PolarizedPhoton) self.assertTrue(photon.unitDirectionVector() == Vector(0.0, 1.0, 0.0)) self.assertTrue( photon.stokesVector() == StokesVector([1.0, 0.0, 1.0, 0.0]))
def setUp(self): photon1_1 = PolarizedPhoton(8000, Vector(1, 1, 0), StokesVector([1, 0, 0, -1])) photon1_2 = PolarizedPhoton(254, Vector(1, 0.52, 1e-6), StokesVector([1e+8, 0, 0, -1])) photon1_3 = PolarizedPhoton(1e+5, Vector(2.00, 1, 0), StokesVector([1, 0.9002, 0, -1e-5])) self.photon_bunch1 = PhotonBunch([photon1_1, photon1_2, photon1_3]) # quasi-monochromatic bunch. photon2_1 = PolarizedPhoton(8000.0000, Vector(1, 1, 0), StokesVector([1, 0, 0, -1])) photon2_2 = PolarizedPhoton(8000.0000, Vector(0.5, 0.5, 1e-6), StokesVector([1e+8, 0, 0, -1])) photon2_3 = PolarizedPhoton(8000.0001, Vector(1.0, 1, 0), StokesVector([1, 0.9002, 0, -1e-5])) self.photon_bunch2 = PhotonBunch([photon2_1, photon2_2, photon2_3]) # quasi-unidirectional bunch. photon3_1 = PolarizedPhoton(8000, Vector(1, 1, 0), StokesVector([1, 0, 0, -1])) photon3_2 = PolarizedPhoton(254, Vector(0.50, 0.50, 0), StokesVector([1e+8, 0, 0, -1])) photon3_3 = PolarizedPhoton(1e+5, Vector(0.20000001, 0.20000, 0), StokesVector([1, 0.9002, 0, -1e-5])) self.photon_bunch3 = PhotonBunch([photon3_1, photon3_2, photon3_3])
def test_constructor(self): self.assertIsInstance(self.photon_bunch1.photon_bunch, list) self.assertIsInstance(self.photon_bunch1.photon_bunch[0], PolarizedPhoton) self.assertEqual(self.photon_bunch1.photon_bunch, [PolarizedPhoton(8000, Vector(1, 1, 0), StokesVector([1, 0, 0, -1])), PolarizedPhoton(254, Vector(1, 0.52, 1e-6), StokesVector([1e+8, 0, 0, -1])), PolarizedPhoton(1e+5, Vector(2.00, 1, 0), StokesVector([1, 0.9002, 0, -1e-5]))]) print(self.photon_bunch2.photon_bunch[1].energy(), self.photon_bunch2.photon_bunch[1].unitDirectionVector().components()) print(Vector(1, 0.52, 1e-6).getNormalizedVector().components()) self.assertTrue(self.photon_bunch2.photon_bunch[1] == PolarizedPhoton(8000.0000, Vector(1, 0.52, 1e-6), StokesVector([1e+8, 0, 0, -1])))
def testInheritatedMethods(self): ph1 = PolarizedPhoton(8000.0, Vector(2, 4, 5), StokesVector([1, 2, 3, 4])) ph1.setUnitDirectionVector(Vector(1, 0, 0)) ph1.setEnergy(9000) self.assertTrue(ph1.unitDirectionVector().components()[0] == 1) self.assertTrue(ph1.unitDirectionVector().components()[1] == 0) self.assertTrue(ph1.unitDirectionVector().components()[2] == 0) self.assertTrue(ph1.energy() == 9000.0)
def asymmetryFactor(self, energy, vector_k_in=None): if vector_k_in is None: vector_k_in = self.vectorK0(energy) v2 = vector_k_in.addVector(self.vectorKh(energy)).subtractVector( self.vectorK0(energy)) # ! asymmetry b factor vectorial value (Zachariasen, [3.115]) numerator = Vector.scalarProduct(self.vectorNormalSurface(), vector_k_in) denominator = Vector.scalarProduct(self.vectorNormalSurface(), v2) return numerator / denominator
def testAngle(self): # normalized scalar product !! vector_x = Vector(1, 0, 0) vector_y = Vector(0, 1, 0) vector_z = Vector(0, 0, 1) vector_xy = Vector(1, 1, 0) self.assertAlmostEqual(vector_x.angle(vector_y), pi / 2.0) self.assertAlmostEqual(vector_y.angle(vector_z), pi / 2.0) self.assertAlmostEqual(vector_x.angle(vector_xy), pi / 4.0)
def testFromList(self): npoint = 1000 vx = numpy.zeros(npoint) + 0.0 vy = numpy.zeros(npoint) + 1.0 vz = numpy.zeros(npoint) + 0.0 energy = numpy.zeros(npoint) + 3000.0 photon_bunch1 = PhotonBunch() photon_bunch2 = PhotonBunch() photons_list = list() for i in range(npoint): photon = Photon(energy_in_ev=energy[i], direction_vector=Vector(vx[i], vy[i], vz[i])) photon_bunch1.addPhoton(photon) photons_list.append(photon) photon_bunch2.addPhotonsFromList(photons_list) energies = photon_bunch1.getArrayByKey("energies") for energy in energies: self.assertAlmostEqual(energy, 3000.0) for i in range(len(photon_bunch1)): # print("checking photon %d "%i) self.assertTrue( photon_bunch1.getPhotonIndex(i) == photon_bunch2.getPhotonIndex(i))
def setUp(self): self.energy_in_ev = 8000 self.direction_vector = Vector(1, 3, 2).getNormalizedVector() self.stokes_vector = StokesVector([1, -1, 0, 0]) self.polarized_photon = PolarizedPhoton(self.energy_in_ev, self.direction_vector, self.stokes_vector)
def testChangePhotonValue(self): nphotons = 10 from crystalpy.util.Vector import Vector from crystalpy.util.StokesVector import StokesVector bunch = PolarizedPhotonBunch([]) for i in range(nphotons): polarized_photon = PolarizedPhoton( energy_in_ev=1000.0 + i, direction_vector=Vector(0, 1.0, 0), stokes_vector=StokesVector([1.0, 0, 1.0, 0])) bunch.addPhoton(polarized_photon) # photon5_stokes = bunch.get_photon_index(5).stokesVector().get_array(numpy=True) # print("photon 5 stokes ",photon5_stokes) photon5 = bunch.getPhotonIndex(5) photon5.setStokesVector(StokesVector([1, 0, 0, 0])) photon5_stokes_new = bunch.getPhotonIndex( 5).stokesVector().components() # print("photon 5 stokes new ",photon5_stokes_new) assert_almost_equal(photon5_stokes_new, numpy.array([1.0, 0, 0, 0]))
def from_shadow_beam_to_photon_bunch(self): vx = self.incoming_shadow_beam._beam.getshcol(4, nolost=1) vy = self.incoming_shadow_beam._beam.getshcol(5, nolost=1) vz = self.incoming_shadow_beam._beam.getshcol(6, nolost=1) s0 = self.incoming_shadow_beam._beam.getshcol(30, nolost=1) s1 = self.incoming_shadow_beam._beam.getshcol(31, nolost=1) s2 = self.incoming_shadow_beam._beam.getshcol(32, nolost=1) s3 = self.incoming_shadow_beam._beam.getshcol(33, nolost=1) energies = self.incoming_shadow_beam._beam.getshcol(11, nolost=1) photon_bunch = PolarizedPhotonBunch([]) photons_list = list() for i, energy in enumerate(energies): photon = PolarizedPhoton( energy_in_ev=energy, direction_vector=Vector(vx[i], vy[i], vz[i]), stokes_vector=StokesVector([s0[i], s1[i], s2[i], s3[i]])) #photon_bunch.add(photon) # print("<><> appending photon",i) photons_list.append(photon) photon_bunch.addPhotonsFromList(photons_list) return photon_bunch
def testDuplicate(self): v1 = Vector(1,2,3) v2 = v1.duplicate() self.assertTrue( v1.components()[0] == v2.components()[0]) self.assertTrue( v1.components()[1] == v2.components()[1]) self.assertTrue( v1.components()[2] == v2.components()[2]) v1.setComponents(3,4,5) self.assertFalse( v1.components()[0] == v2.components()[0]) self.assertFalse( v1.components()[1] == v2.components()[1]) self.assertFalse( v1.components()[2] == v2.components()[2])
def testDuplicate(self): photon_one = Photon(4000, Vector(0, 0, 5)) photon_two = photon_one.duplicate() self.assertTrue(photon_one == photon_two) photon_one.setEnergy(1000.0) self.assertFalse(photon_one == photon_two)
def test_add(self): photon_bunch1 = self.photon_bunch1 photon_bunch2 = self.photon_bunch2 to_be_added = [PolarizedPhoton(2000, Vector(1, 1, 0), StokesVector([1, 1, 0, 0])), PolarizedPhoton(3456, Vector(2, 1, 0), StokesVector([1, 1, 0, -1]))] photon_bunch1.add(to_be_added) # list photon_bunch2.add(to_be_added[1]) # single element self.assertIsInstance(photon_bunch1, PhotonBunch) self.assertIsInstance(photon_bunch2, PhotonBunch) self.assertIsInstance(photon_bunch1[3], PolarizedPhoton) self.assertTrue(photon_bunch1[3] == PolarizedPhoton(2000, Vector(1, 1, 0), StokesVector([1, 1, 0, 0]))) self.assertTrue(photon_bunch2[3] == PolarizedPhoton(3456, Vector(2, 1, 0), StokesVector([1, 1, 0, -1]))) self.assertEqual(len(photon_bunch1), 5) self.assertEqual(len(photon_bunch2), 4)
def testFromComponents(self): vector = Vector.initializeFromComponents(array([11, -2, 23])) self.assertAlmostEqual(vector.components()[0], 11) self.assertAlmostEqual(vector.components()[1], -2) self.assertAlmostEqual(vector.components()[2], 23)
def vectorNormalSurface(self): """ Returns the normal to the surface. (0,0,1) by definition. :return: Vector instance with Surface normal Vnor. """ # Edoardo: I use the geometrical convention from # M.Sanchez del Rio et al., J.Appl.Cryst.(2015). 48, 477-491. normal_surface = Vector(0, 0, 1) return normal_surface
def testWavevector(self): direction = Vector(0, 0, 1) photon = Photon(5000.0, direction) wavevector = photon.wavevector() self.assertAlmostEqual(wavevector.norm(), 25338653792.67, places=1) self.assertEqual(wavevector.getNormalizedVector(), direction)
def vectorParallelSurface(self): """ Returns the direction parallel to the crystal surface. (0,1,0) by definition. :return: Vector instance with Surface normal Vtan. """ # Edoardo: I use the geometrical convention from # M.Sanchez del Rio et al., J.Appl.Cryst.(2015). 48, 477-491. parallel_surface = Vector(0, 1, 0) return parallel_surface
def testSetComponents(self): vector = Vector(1, 2, 3) vector.setComponents(-3.0, -4.0, 0.0) self.assertAlmostEqual(vector.components()[0], -3.0) self.assertAlmostEqual(vector.components()[1], -4.0) self.assertAlmostEqual(vector.components()[2], 0.0) self.assertAlmostEqual(vector.getX(), -3.0) self.assertAlmostEqual(vector.getY(), -4.0) self.assertAlmostEqual(vector.getZ(), 0.0)