示例#1
0
def test2():
    v = Vector()
    v.p0 = np.ones(3)
    assert_true(np.allclose(v, -np.ones(3)))
    assert_true(np.allclose(v.p0, np.ones(3)))
    assert_true(np.allclose(v.p, np.zeros(3)))

    v = Vector(p=[1., 1., 1.], p0=[1., 1., 1.])
    assert_true(np.allclose(v, np.zeros(3)))
    assert_true(np.allclose(v.p0, np.ones(3)))
    assert_true(np.allclose(v.p, np.ones(3)))
    v.p0 = np.zeros(3)
    assert_true(np.allclose(v, np.ones(3)))
    assert_true(np.allclose(v.p0, np.zeros(3)))
    assert_true(np.allclose(v.p, np.ones(3)))
    v.x = 5.0
    assert_equal(v.x, 5.0)
    assert_equal(v.x, v.p.x)
    v.y = -5.0
    assert_equal(v.y, -5.0)
    assert_equal(v.y, v.p.y)
    v.z = 0.0
    assert_equal(v.z, 0.0)
    assert_equal(v.z, v.p.z)

    v.p0 = np.array([0.5, -10.0, 2.5])
    assert_equal(v.p0.x, 0.5)
    assert_equal(v.p0.y, -10.0)
    assert_equal(v.p0.z, 2.5)
示例#2
0
def test1():
    v = Vector()
    assert_true(np.allclose(v, np.zeros(3)))
    assert_true(np.allclose(v.p0, np.zeros(3)))
    assert_true(np.allclose(v.p, np.zeros(3)))

    v = Vector([1, 1, 1])
    assert_true(np.allclose(v, np.ones(3)))
    assert_true(np.allclose(v.p0, np.zeros(3)))
    assert_true(np.allclose(v.p, np.ones(3)))

    v = Vector(p=[1, 1, 1])
    assert_true(np.allclose(v, np.ones(3)))
    assert_true(np.allclose(v.p0, np.zeros(3)))
    assert_true(np.allclose(v.p, np.ones(3)))

    v = Vector(p0=[1, 1, 1])
    assert_true(np.allclose(v, -np.ones(3)))
    assert_true(np.allclose(v.p0, np.ones(3)))
    assert_true(np.allclose(v.p, np.zeros(3)))

    v = Vector(p=[1, 1, 1], p0=[1, 1, 1])
    assert_true(np.allclose(v, np.zeros(3)))
    assert_true(np.allclose(v.p0, np.ones(3)))
    assert_true(np.allclose(v.p, np.ones(3)))
示例#3
0
def test13():

    u = Vector([5, 6, 7])
    assert_true(np.allclose(
        u.projection(Vector([1, 0, 0])), Vector([5, 0, 0])))
    assert_true(np.allclose(
        u.projection(Vector([1, 1, 1])), Vector([6, 6, 6])))
示例#4
0
def test29():
    u = Vector([5, 0, 0])
    v = u.copy()

    u *= v

    assert_equal(u, 25)
示例#5
0
def test27():
    v = Vector([5, 0, 0])
    v += 5
    assert_equal(v, Vector([10, 5, 5]))

    v += v
    assert_equal(v, Vector([20, 10, 10]))
示例#6
0
    def __init__(self, o=None, u=None, v=None, w=None):

        self._o = Point()
        self._u = Vector()
        self._v = Vector()
        self._w = Vector()

        super().__init__()

        if o is None:
            o = [0, 0, 0]
        self.o = o

        if u is None:
            u = [1, 0, 0]
        self.u = u

        if v is None:
            v = [1, 1, 0]
        self.v = v

        if w is None:
            w = [0, 1, 1]
        self.w = w

        self.points.append(self.o)
        self.vectors.extend([self.u, self.v, self.w])

        self.fmtstr = "o={o!r}, u={u!r}, v={v!r}, w={w!r}"
示例#7
0
def test29():
    u = Vector([5, 0, 0])
    v = u.copy()

    u *= v

    assert_equal(u, 25)
示例#8
0
    def __init__(self, a=None, b=None, gamma=None,
                 a1=None, a2=None, cell_matrix=None,
                 orientation_matrix=None, offset=None):

        if cell_matrix is not None:
            cell_matrix = np.asarray(cell_matrix)
            a1 = np.array(cell_matrix[0, :])
            a2 = np.array(cell_matrix[1, :])

        if a1 is not None and a2 is not None:
            a1 = Vector(a1, nd=3)
            a2 = Vector(a2, nd=3)
            a = a1.length
            b = a2.length
            gamma = np.degrees(a1.angle(a2))
            cell_matrix = np.matrix(np.vstack((np.asarray(a1),
                                               np.asarray(a2),
                                               np.asarray([0, 0, 1]))))

        self._a = a
        self._b = b
        self._gamma = gamma

        if None not in (a, b, gamma):
            self._update_ortho_matrix()

        super().__init__(nd=2, cell_matrix=cell_matrix,
                         orientation_matrix=orientation_matrix,
                         offset=offset)

        self.fmtstr = "a={a!r}, b={b!r}, gamma={gamma!r}"
示例#9
0
def test32():
    u = Vector([5, 0, 0])
    v = u.copy()

    with assert_raises(TypeError):
        v **= v

    assert_equal(u, v)
示例#10
0
def test18():
    u = Vector([1, 2, 3])
    v = Vector([1, 2, 3])
    w = Vector([1, 1, 1])
    assert_equals(u, v)
    assert_true(u == v)
    assert_not_equals(u, w)
    assert_false(u == w)
示例#11
0
def test32():
    u = Vector([5, 0, 0])
    v = u.copy()

    with assert_raises(TypeError):
        v **= v

    assert_equal(u, v)
示例#12
0
def test35():
    v0 = Vector(np.arange(3) + 2)
    I = np.identity(3)
    v1 = Vector((v0.row_matrix * I).A.flatten(), p0=v0.p0)
    v2 = Vector((I * v0.column_matrix).A.flatten(), p0=v0.p0)

    assert_true(np.allclose(v0, v1))
    assert_true(np.allclose(v0, v2))
示例#13
0
def test20():
    a = Vector([1, 1, 1])
    b = Vector([1, 0, 0])
    assert_equals(vec.scalar_projection(a, b), 1.0)

    a = Vector([1, 0, 0])
    b = Vector([1, 1, 1])
    assert_equals(vec.scalar_projection(a, b), 1.0 / np.sqrt(3))
示例#14
0
    def generate_bundle_coords(self):
        """Generate coordinates of bundle tubes."""
        self.r1 = Vector()
        self.r2 = Vector()
        self.bundle_coords = []

        self.r1.x = self.dt + 2 * self.vdw_radius
        if self.bundle_packing in ('cubic', 'ccp'):
            self.r2.y = self.r1.x
        else:
            self.r2.x = self.r1.x * np.cos(2 * np.pi / 3)
            self.r2.y = self.r1.x * np.sin(2 * np.pi / 3)
            if self.bundle_packing is None:
                self._bundle_packing = 'hcp'

        if self.bundle_geometry == 'hexagon':
            nrows = max(self.nx, self.ny, 3)
            if nrows % 2 != 1:
                nrows += 1

            ntubes_per_end_rows = int((nrows + 1) / 2)

            row = 0
            ntubes_per_row = nrows
            while ntubes_per_row >= ntubes_per_end_rows:
                if row == 0:
                    for n in range(ntubes_per_row):
                        dr = n * self.r1
                        self.bundle_coords.append(dr)
                else:
                    for nx in range(ntubes_per_row):
                        for ny in (-row, row):
                            dr = Vector()
                            dr.x = abs(ny * self.r2.x)
                            dr.y = ny * self.r2.y
                            dr = nx * self.r1 + dr
                            self.bundle_coords.append(dr)
                row += 1
                ntubes_per_row = nrows - row

        elif self.bundle_geometry == 'rectangle':
            Lx = 10 * self.Lx
            for nx in range(self.nx):
                for ny in range(self.ny):
                    dr = nx * self.r1 + ny * self.r2
                    while dr.x < 0:
                        dr.x += Lx
                    self.bundle_coords.append(dr)

        elif self.bundle_geometry == 'square':
            pass
        elif self.bundle_geometry == 'triangle':
            pass
        else:
            for nx in range(self.nx):
                for ny in range(self.ny):
                    dr = nx * self.r1 + ny * self.r2
                    self.bundle_coords.append(dr)
示例#15
0
def test25():
    assert_equal(e1 * e1, e1.dot(e1))
    assert_equal(e1 * e1, 1.0)

    with assert_raises(ValueError):
        e1.dot(Vector([1.0, 0.0]))

    with assert_raises(ValueError):
        Vector([1.0, 0.0]).dot(e1)
示例#16
0
def test6():
    v1 = Vector([1.0, 0.0, 0.0])
    v2 = Vector([1.0, 1.0, 0.0])

    c = np.dot(np.asarray(v1), np.asarray(v2))
    assert_equal(c, 1.0)

    c = vec.dot(v1, v2)
    assert_equal(c, 1.0)
示例#17
0
    def __init__(self,
                 a=None,
                 b=None,
                 c=None,
                 alpha=None,
                 beta=None,
                 gamma=None,
                 a1=None,
                 a2=None,
                 a3=None,
                 cell_matrix=None,
                 orientation_matrix=None,
                 offset=None):

        if all([
                p is None
                for p in (a, b, c, alpha, beta, gamma, a1, a2, a3, cell_matrix)
        ]):
            errmsg = 'Expected lattice parameters ' + \
                '`a`, `b`, `c`, `alpha`, `beta`, `gamma`\n' + \
                'or lattice vectors `a1`, `a2`, `a3`\n' + \
                'or a 3x3 matrix with lattice vectors for columns.'
            raise ValueError(errmsg)

        if cell_matrix is not None:
            cell_matrix = np.asarray(cell_matrix)
            a1 = np.array(cell_matrix[0, :])
            a2 = np.array(cell_matrix[1, :])
            a3 = np.array(cell_matrix[2, :])

        if all([v is not None for v in (a1, a2, a3)]):
            a1 = Vector(a1)
            a2 = Vector(a2)
            a3 = Vector(a3)
            a = a1.length
            b = a2.length
            c = a3.length
            alpha = np.degrees(a2.angle(a3))
            beta = np.degrees(a3.angle(a1))
            gamma = np.degrees(a1.angle(a2))
            cell_matrix = np.matrix(
                np.vstack((np.asarray(a1), np.asarray(a2), np.asarray(a3))))

        self._a = a
        self._b = b
        self._c = c
        self._alpha = alpha
        self._beta = beta
        self._gamma = gamma

        if all([p is not None for p in (a, b, c, alpha, beta, gamma)]):
            self._update_ortho_matrix()

        super().__init__(nd=3,
                         cell_matrix=cell_matrix,
                         orientation_matrix=orientation_matrix,
                         offset=offset)

        self.fmtstr = "a={a!r}, b={b!r}, c={c!r}, " + \
            "alpha={alpha!r}, beta={beta!r}, gamma={gamma!r}"
示例#18
0
def test10():
    v = Vector(p0=[1, 1, 1])
    assert_true(np.allclose(v, -np.ones(3)))
    assert_true(np.allclose(v.p0, np.ones(3)))
    assert_true(np.allclose(v.p, np.array([0, 0, 0])))
    v.rotate(np.pi/2, rot_axis='z', rot_point=[1, 1, 1])
    assert_is_instance(v.p0, Point)
    assert_is_instance(v.p, Point)
    assert_true(np.allclose(v, np.array([1, -1, -1])))
    assert_true(np.allclose(v.p0, np.array([1, 1, 1])))
    assert_true(np.allclose(v.p, np.array([2, 0, 0])))
示例#19
0
def test10():
    v = Vector(p0=[1, 1, 1])
    assert_true(np.allclose(v, -np.ones(3)))
    assert_true(np.allclose(v.p0, np.ones(3)))
    assert_true(np.allclose(v.p, np.array([0, 0, 0])))
    v.rotate(np.pi / 2, rot_axis='z', rot_point=[1, 1, 1])
    assert_is_instance(v.p0, Point)
    assert_is_instance(v.p, Point)
    assert_true(np.allclose(v, np.array([1, -1, -1])))
    assert_true(np.allclose(v.p0, np.array([1, 1, 1])))
    assert_true(np.allclose(v.p, np.array([2, 0, 0])))
示例#20
0
def test12():

    v1 = Vector()
    dr = Vector(np.ones(3))
    v3 = Vector()
    v3 = Vector(v1 + dr)
    #v3[:] = v1 + dr
    #print('v3: {}'.format(v3))
    #print('v3.p: {}'.format(v3.p))
    assert_true(np.allclose(v3, np.ones(3)))
    assert_true(np.allclose(v3.p0, np.zeros(3)))
    assert_true(np.allclose(v3.p, np.ones(3)))
示例#21
0
def test13():

    u = Vector([5, 6, 7])
    assert_true(np.allclose(u.projection(Vector([1, 0, 0])), Vector([5, 0,
                                                                     0])))
    assert_true(np.allclose(u.projection(Vector([1, 1, 1])), Vector([6, 6,
                                                                     6])))
示例#22
0
def test2():
    v = Vector()
    v.p0 = np.ones(3)
    assert_true(np.allclose(v, -np.ones(3)))
    assert_true(np.allclose(v.p0, np.ones(3)))
    assert_true(np.allclose(v.p, np.zeros(3)))

    v = Vector(p=[1., 1., 1.], p0=[1., 1., 1.])
    assert_true(np.allclose(v, np.zeros(3)))
    assert_true(np.allclose(v.p0, np.ones(3)))
    assert_true(np.allclose(v.p, np.ones(3)))
    v.p0 = np.zeros(3)
    assert_true(np.allclose(v, np.ones(3)))
    assert_true(np.allclose(v.p0, np.zeros(3)))
    assert_true(np.allclose(v.p, np.ones(3)))
    v.x = 5.0
    assert_equal(v.x, 5.0)
    assert_equal(v.x, v.p.x)
    v.y = -5.0
    assert_equal(v.y, -5.0)
    assert_equal(v.y, v.p.y)
    v.z = 0.0
    assert_equal(v.z, 0.0)
    assert_equal(v.z, v.p.z)

    v.p0 = np.array([0.5, -10.0, 2.5])
    assert_equal(v.p0.x, 0.5)
    assert_equal(v.p0.y, -10.0)
    assert_equal(v.p0.z, 2.5)
示例#23
0
def test33():
    v1 = Vector([1, 0, 0])
    v2 = Vector([0, 1, 0])
    v3 = v1.cross(v2)
    assert_is_instance(v3, Vector)
    assert_true(np.allclose(v3, np.cross(np.asarray(v1), np.asarray(v2))))
    assert_true(np.allclose(v3.p0, v1.p0))
    assert_true(np.allclose(v3.p0, v2.p0))

    v1 = Vector([1, 0, 0], p0=[1, 1, 1])
    v2 = Vector([0, 1, 0], p0=[1, 1, 1])

    v3 = v1.cross(v2)
    assert_is_instance(v3, Vector)
    assert_true(np.allclose(v3, np.cross(np.asarray(v1), np.asarray(v2))))
    assert_true(np.allclose(v3.p0, v1.p0))
    assert_true(np.allclose(v3.p0, v2.p0))

    v1 = Vector([1, 0, 0], p0=[1, 0, 0])
    v2 = Vector([0, 1, 0], p0=[0, 1, 0])

    v3 = v1.cross(v2)
    assert_is_instance(v3, Vector)
    assert_true(np.allclose(v3, np.cross(np.asarray(v1), np.asarray(v2))))
    assert_true(np.allclose(v3.p0, v1.p0))
    assert_false(np.allclose(v3.p0, v2.p0))

    v1 = Vector([1, 2, 3], p0=[1, 0, 0])
    v2 = Vector([4, 5, 6], p0=[0, 1, 0])
    v3 = v1.cross(v2)
    assert_is_instance(v3, Vector)
    assert_true(np.allclose(v3, np.cross(np.asarray(v1), np.asarray(v2))))
    assert_true(np.allclose(v3.p0, v1.p0))
    assert_false(np.allclose(v3.p0, v2.p0))
示例#24
0
def test7():
    v1 = Vector([1, 0, 0])
    v2 = Vector([0, 1, 0])
    v3 = vec.cross(v1, v2)
    assert_is_instance(v3, Vector)
    assert_true(np.allclose(v3, np.cross(np.asarray(v1), np.asarray(v2))))
    assert_true(np.allclose(v3.p0, v1.p0))
    assert_true(np.allclose(v3.p0, v2.p0))

    v1 = Vector([1, 0, 0], p0=[1, 1, 1])
    v2 = Vector([0, 1, 0], p0=[1, 1, 1])

    v3 = vec.cross(v1, v2)
    assert_is_instance(v3, Vector)
    assert_true(np.allclose(v3, np.cross(np.asarray(v1), np.asarray(v2))))
    assert_true(np.allclose(v3.p0, v1.p0))
    assert_true(np.allclose(v3.p0, v2.p0))

    v1 = Vector([1, 0, 0], p0=[1, 0, 0])
    v2 = Vector([0, 1, 0], p0=[0, 1, 0])

    v3 = vec.cross(v1, v2)
    assert_is_instance(v3, Vector)
    assert_true(np.allclose(v3, np.cross(np.asarray(v1), np.asarray(v2))))
    assert_true(np.allclose(v3.p0, v1.p0))
    assert_false(np.allclose(v3.p0, v2.p0))

    v1 = Vector([1, 2, 3], p0=[1, 0, 0])
    v2 = Vector([4, 5, 6], p0=[0, 1, 0])
    v3 = vec.cross(v1, v2)
    assert_is_instance(v3, Vector)
    assert_true(np.allclose(v3, np.cross(np.asarray(v1), np.asarray(v2))))
    assert_true(np.allclose(v3.p0, v1.p0))
    assert_false(np.allclose(v3.p0, v2.p0))
示例#25
0
def compute_centroid(coords):
    """Compute the centroid of coordinates.

    .. math::
       \\mathbf{C} =
       \\frac{\\sum_{i=1}^{N}m_i\\mathbf{r}_i}{\\sum_{i=1}^{N}m_i}

    Returns
    -------
    C : `~sknano.core.math.Vector`
        The position vector of the centroid coordinates.
    """
    C = Vector(np.mean(coords, axis=0))
    C.rezero()
    return C
示例#26
0
def test22():
    a = Vector([1, 1, 1])
    b = Vector([1, 0, 0])
    assert_equals(vec.vector_rejection(a, b), Vector([0, 1, 1]))

    a = Vector([1, 0, 0])
    b = Vector([1, 1, 1])
    assert_equals(vec.vector_rejection(a, b), Vector([2 / 3, -1 / 3, -1 / 3]))

    a = Vector([5, 6, 7])
    b = Vector([1, 1, 1])
    assert_equals(vec.vector_rejection(a, b), a - Vector(3 * [6]))
示例#27
0
def test21():
    a = Vector([1, 1, 1])
    b = Vector([1, 0, 0])
    assert_equals(vec.vector_projection(a, b), Vector([1, 0, 0]))

    a = Vector([1, 0, 0])
    b = Vector([1, 1, 1])
    assert_equals(vec.vector_projection(a, b), Vector(1 / 3 * np.ones(3)))

    a = Vector([5, 6, 7])
    b = Vector([1, 1, 1])
    assert_equals(vec.vector_projection(a, b), Vector(3 * [6]))
示例#28
0
def test1():
    print('generating graphene structure')
    graphene = GrapheneGenerator(armchair_edge_length=5, zigzag_edge_length=5)
    lattice = graphene.lattice
    print('graphene.bounds:\n{}'.format(graphene.bounds))
    print('graphene.centroid:\n{}'.format(graphene.centroid))
    print('graphene.lattice:\n{}'.format(lattice))
    print('graphene.lattice.a1:\n{}'.format(lattice.a1))
    print('graphene.lattice.a2:\n{}'.format(lattice.a2))
    print('graphene.lattice.a3:\n{}'.format(lattice.a3))
    print('graphene.lattice.orientation_matrix:\n{}'.format(
        lattice.orientation_matrix))
    print('rotating graphene')
    graphene.rotate(angle=-np.pi / 2, axis='x')
    print('graphene.bounds:\n{}'.format(graphene.bounds))
    print('graphene.centroid:\n{}'.format(graphene.centroid))
    print('graphene.lattice:\n{}'.format(lattice))
    print('graphene.lattice.a1:\n{}'.format(lattice.a1))
    print('graphene.lattice.a2:\n{}'.format(lattice.a2))
    print('graphene.lattice.a3:\n{}'.format(lattice.a3))
    print('graphene.lattice.orientation_matrix:\n{}'.format(
        lattice.orientation_matrix))

    assert_true(np.allclose(lattice.angles, 3 * [90.0]))
    lattice_region = Cuboid(pmax=lattice.lengths)

    # lattice_region = Parallelepiped(u=lattice.a * xhat,
    #                                 v=lattice.b * yhat,
    #                                 w=lattice.c * zhat)
    assert_equal(lattice_region.a, lattice.a)
    assert_equal(lattice_region.b, lattice.b)
    assert_equal(lattice_region.c, lattice.c)
    print('lattice_region:\n{}'.format(lattice_region))
    print('lattice_region.centroid:\n{}'.format(lattice_region.centroid))

    print('\nrotating lattice_region')
    lattice_region.rotate(transform_matrix=lattice.orientation_matrix)
    # assert_equal(lattice_region.a, lattice.a)
    # assert_equal(lattice_region.b, lattice.b)
    # assert_equal(lattice_region.c, lattice.c)
    print('lattice_region:\n{}'.format(lattice_region))
    print('lattice_region.centroid:\n{}'.format(lattice_region.centroid))

    print('\ncentering lattice_region on graphene centroid')
    tvec = Vector(Point(graphene.centroid) - lattice_region.centroid)
    lattice_region.translate(tvec)
    # assert_equal(lattice_region.a, lattice.a)
    # assert_equal(lattice_region.b, lattice.b)
    # assert_equal(lattice_region.c, lattice.c)
    print('lattice_region:\n{}'.format(lattice_region))
    print('lattice_region.centroid:\n{}'.format(lattice_region.centroid))

    bounding_box = generate_bounding_box(from_lattice=lattice,
                                         center=graphene.centroid,
                                         verbose=True)
    print('bounding_box:\n{}'.format(bounding_box))
    assert_equal(bounding_box, lattice_region)
    print('lattice_region.lengths: {}, {}, {}'.format(lattice_region.a,
                                                      lattice_region.b,
                                                      lattice_region.c))
示例#29
0
    def __init__(self, *args, vx=None, vy=None, vz=None, **kwargs):

        super().__init__(*args, **kwargs)

        self._v = Vector([vx, vy, vz])
        self.fmtstr = super().fmtstr + \
            ", vx={vx:.6f}, vy={vy:.6f}, vz={vz:.6f}"
示例#30
0
    def __init__(self, *args, px=None, py=None, pz=None, **kwargs):

        super().__init__(*args, **kwargs)

        self._p = Vector([px, py, pz])
        self.fmtstr = super().fmtstr + \
            ", px={px:.6f}, py={py:.6f}, pz={pz:.6f}"
示例#31
0
    def __init__(self, *args, fx=None, fy=None, fz=None, **kwargs):

        super().__init__(*args, **kwargs)

        self._f = Vector([fx, fy, fz])
        self.fmtstr = super().fmtstr + \
            ", fx={fx:.6f}, fy={fy:.6f}, fz={fz:.6f}"
示例#32
0
def test2():
    H = Quaternion(np.arange(4))
    assert_equal(H.w, H.real)
    assert_equal(H.w, 0.)
    assert_is_instance(H.imag, list)
    assert_is_instance(H.v, Vector)
    assert_true(np.allclose(H.v, Vector(np.arange(3) + 1)))
示例#33
0
    def vector(self):
        """`Bond` :class:`~sknano.core.math.Vector`.

        `Bond` :class:`~sknano.core.math.Vector` points from
        :attr:`Bond.atom1` to :attr:`Bond.atom2`.

        """
        return Vector(self.atom2.r - self.atom1.r, p0=self.atom1.r.p)
示例#34
0
    def __init__(self,
                 nlayers=1,
                 layer_spacing=3.14,
                 sulphur_layer_spacing=3.01,
                 **kwargs):

        super().__init__(**kwargs)

        self.layer_mass = None
        self.Natoms = 0
        self.Natoms_per_layer = 0

        self.nlayers = nlayers
        self.layer_spacing = layer_spacing

        self.layer_shift = Vector()

        self.cell = Vector()
示例#35
0
    def centroid(self):
        """Centroid of `Atoms`.

        Computes the position vector of the centroid of the `Atoms`
        coordinates.

        .. math::
           \\mathbf{C} =
           \\frac{\\sum_{i=1}^{N_{\\mathrm{atoms}}}
           \\mathbf{r}_i}{N_{\\mathrm{atoms}}}

        Returns
        -------
        C : :class:`~sknano.core.math.Vector`
            The position vector of the centroid coordinates.
        """
        C = Vector(np.mean(self.coords, axis=0))
        C.rezero()
        return C
示例#36
0
    def centroid(self):
        """Centroid of `Atoms`.

        Computes the position vector of the centroid of the `Atoms`
        coordinates.

        .. math::
           \\mathbf{C} =
           \\frac{\\sum_{i=1}^{N_{\\mathrm{atoms}}}
           \\mathbf{r}_i}{N_{\\mathrm{atoms}}}

        Returns
        -------
        C : :class:`~sknano.core.math.Vector`
            The position vector of the centroid coordinates.
        """
        C = Vector(np.mean(self.coords, axis=0))
        C.rezero()
        return C
示例#37
0
    def p(self, value):
        """Set `DipoleAtom` dipole moment :math:`\\mathbf{p}=q\\mathbf{d}`.

        Parameters
        ----------
        value : array_like
            electric dipole moment vector

        """
        self._p[:] = Vector(value, nd=3)
示例#38
0
    def axis(self):
        """:class:`Cone` axis :class:`Vector` from \
            :math:`\\boldsymbol{\\ell}=p_2 - p_1`

        Returns
        -------
        :class:`Vector`

        """
        return Vector(p0=self.p1, p=self.p2)
示例#39
0
def test11():
    v = Vector()
    v += np.ones(3)
    assert_true(np.allclose(v, np.ones(3)))
    assert_true(np.allclose(v.p0, np.zeros(3)))
    assert_true(np.allclose(v.p, np.ones(3)))

    v.p += np.ones(3)
    assert_true(np.allclose(v, 2 * np.ones(3)))
    assert_true(np.allclose(v.p0, np.zeros(3)))
    assert_true(np.allclose(v.p, 2 * np.ones(3)))

    v1 = Vector()
    dr = Vector(np.ones(3))
    v2 = v1 + dr
    #print('v2: {}'.format(v2))
    #print('v2.p: {}'.format(v2.p))
    assert_true(np.allclose(v2, np.ones(3)))
    assert_true(np.allclose(v2.p0, np.zeros(3)))
    assert_true(np.allclose(v2.p, np.ones(3)))
示例#40
0
    def center_of_mass(self):
        """Center-of-Mass coordinates of `Atoms`.

        Computes the position vector of the center-of-mass coordinates:

        .. math::

           \\mathbf{R}_{COM} = \\frac{1}{M}\\sum_{i=1}^{N_{\\mathrm{atoms}}}
           m_i\\mathbf{r}_i

        Returns
        -------
        com : :class:`~sknano.core.math.Vector`
            The position vector of the center of mass coordinates.

        """
        masses = np.asarray([self.masses])
        coords = self.coords
        MxR = masses.T * coords
        com = Vector(np.sum(MxR, axis=0) / np.sum(masses))
        com.rezero()
        return com
示例#41
0
    def __init__(self, a=None, b=None, c=None,
                 alpha=None, beta=None, gamma=None,
                 a1=None, a2=None, a3=None, cell_matrix=None,
                 orientation_matrix=None, offset=None):

        if all([p is None for p in (a, b, c, alpha, beta, gamma,
                                    a1, a2, a3, cell_matrix)]):
            errmsg = 'Expected lattice parameters ' + \
                '`a`, `b`, `c`, `alpha`, `beta`, `gamma`\n' + \
                'or lattice vectors `a1`, `a2`, `a3`\n' + \
                'or a 3x3 matrix with lattice vectors for columns.'
            raise ValueError(errmsg)

        if cell_matrix is not None:
            cell_matrix = np.asarray(cell_matrix)
            a1 = np.array(cell_matrix[0, :])
            a2 = np.array(cell_matrix[1, :])
            a3 = np.array(cell_matrix[2, :])

        if all([v is not None for v in (a1, a2, a3)]):
            a1 = Vector(a1)
            a2 = Vector(a2)
            a3 = Vector(a3)
            a = a1.length
            b = a2.length
            c = a3.length
            alpha = np.degrees(a2.angle(a3))
            beta = np.degrees(a3.angle(a1))
            gamma = np.degrees(a1.angle(a2))
            cell_matrix = np.matrix(np.vstack((np.asarray(a1),
                                               np.asarray(a2),
                                               np.asarray(a3))))

        self._a = a
        self._b = b
        self._c = c
        self._alpha = alpha
        self._beta = beta
        self._gamma = gamma

        if all([p is not None for p in (a, b, c, alpha, beta, gamma)]):
            self._update_ortho_matrix()

        super().__init__(nd=3, cell_matrix=cell_matrix,
                         orientation_matrix=orientation_matrix,
                         offset=offset)

        self.fmtstr = "a={a!r}, b={b!r}, c={c!r}, " + \
            "alpha={alpha!r}, beta={beta!r}, gamma={gamma!r}"
示例#42
0
 def a1(self):
     """2D lattice vector :math:`\\mathbf{a}_1=\\mathbf{a}`."""
     b2 = Vector()
     b2[:2] = self.b2
     return b2.cross(zhat) / self.cell_area
示例#43
0
def test_translation_matrix():
    for l in ([1, 1], [1, 1, 1]):
        v = Vector(l)
        assert_equal(v, Vector(translation_matrix(v.tolist())[:v.nd, v.nd]))
        assert_equal(v, translation_from_matrix(translation_matrix(v)))
示例#44
0
def test36():
    v0 = Vector(np.arange(10))
    assert_equal(v0.argmax(), len(v0)-1)
示例#45
0
def test19():
    u = Vector([10, 10, 17])
    v = Vector([10, 10, 17])
    assert_true(u == v)
    v.normalize()
    assert_true(np.allclose(u.unit_vector, v))
示例#46
0
 def b1(self):
     """2D reciprocal lattice vector :math:`\\mathbf{b}_1=\\mathbf{a}^{*}`.
     """
     a2 = Vector()
     a2[:2] = self.a2
     return a2.cross(zhat)[:2] / self.cell_area