|
| 1 | +#!/usr/bin/env python |
| 2 | +############################################################################## |
| 3 | +# |
| 4 | +# diffpy.structure Complex Modeling Initiative |
| 5 | +# (c) 2016 Brookhaven Science Associates, |
| 6 | +# Brookhaven National Laboratory. |
| 7 | +# All rights reserved. |
| 8 | +# |
| 9 | +# File coded by: Pavol Juhas |
| 10 | +# |
| 11 | +# See AUTHORS.rst for a list of people who contributed. |
| 12 | +# See LICENSE.rst for license information. |
| 13 | +# |
| 14 | +############################################################################## |
| 15 | +"""Tests for the expansion module utilities.""" |
| 16 | +import pytest |
| 17 | + |
| 18 | +from diffpy.structure.atom import Atom |
| 19 | +from diffpy.structure.expansion.makeellipsoid import make_ellipsoid, make_sphere, makeEllipsoid, makeSphere |
| 20 | +from diffpy.structure.expansion.shapeutils import find_center, findCenter |
| 21 | +from diffpy.structure.lattice import Lattice |
| 22 | +from diffpy.structure.structure import Structure |
| 23 | + |
| 24 | + |
| 25 | +def test_findCenter(): |
| 26 | + # C1: We have single atom, expect to return the index of the single atom. |
| 27 | + structure_1 = Structure([Atom("Ni", [0.8, 1.2, 0.9])], lattice=Lattice()) |
| 28 | + expected = 0 |
| 29 | + actual = findCenter(structure_1) |
| 30 | + assert actual == expected |
| 31 | + |
| 32 | + # C2: We have multiple atoms. |
| 33 | + # Expect to find the index of the atom which has the closest distance to [0.5, 0.5, 0.5]. |
| 34 | + # In this case it corresponds to atom "C". |
| 35 | + structure_2 = Structure( |
| 36 | + [Atom("Ni", [0.8, 1.2, 0.9]), Atom("C", [0.1, 0.2, 0.3])], |
| 37 | + lattice=Lattice(), |
| 38 | + ) |
| 39 | + actual = findCenter(structure_2) |
| 40 | + expected = 1 |
| 41 | + assert actual == expected |
| 42 | + |
| 43 | + |
| 44 | +def test_makeEllipsoid_and_makeSphere(): |
| 45 | + structure = Structure( |
| 46 | + [ |
| 47 | + Atom("Ni", [0.0, 0.0, 0.0]), |
| 48 | + Atom("C", [0.5, 0.5, 0.5]), |
| 49 | + Atom("O", [1.0, 0.0, 0.0]), |
| 50 | + Atom("H", [1.1, 0.0, 0.0]), |
| 51 | + ], |
| 52 | + lattice=Lattice(1, 1, 1, 90, 90, 90), |
| 53 | + ) |
| 54 | + # C1: set primary, secondary, and polar radius the same in makeEllipsoid, expect to be the same as makeSphere |
| 55 | + ellipsoid_1 = makeEllipsoid(structure, 1, 1, 1) |
| 56 | + sphere = makeSphere(structure, 1) |
| 57 | + assert [atom.element for atom in ellipsoid_1] == [atom.element for atom in sphere] |
| 58 | + assert [tuple(atom.xyz) for atom in ellipsoid_1] == [tuple(atom.xyz) for atom in sphere] |
| 59 | + # C2: set the radius to be 0.5, expect to exclude the atom that is too far from the center of ellipsoid. |
| 60 | + ellipsoid_2 = makeEllipsoid(structure, 0.5) |
| 61 | + actual = [(atom.element, tuple(atom.xyz)) for atom in ellipsoid_2] |
| 62 | + expected = [("C", (0.5, 0.5, 0.5))] |
| 63 | + assert actual == expected |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize( |
| 67 | + "atom_params, expected", |
| 68 | + [ |
| 69 | + pytest.param([Atom("Ni", [0.8, 1.2, 0.9])], 0), |
| 70 | + pytest.param([Atom("Ni", [0.8, 1.2, 0.9]), Atom("C", [0.1, 0.2, 0.3])], 1), |
| 71 | + ], |
| 72 | +) |
| 73 | +def test_find_center(atom_params, expected): |
| 74 | + structure = Structure(atom_params, lattice=Lattice()) |
| 75 | + actual = find_center(structure) |
| 76 | + assert actual == expected |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + ("ellipsoid_args", "sphere_radius"), |
| 81 | + [ |
| 82 | + ((1, 1, 1), 1), |
| 83 | + ((0.8, 0.8, 0.8), 0.8), |
| 84 | + ((0.5, 0.5, 0.5), 0.5), |
| 85 | + ], |
| 86 | +) |
| 87 | +def test_make_ellipsoid_equiv_to_make_sphere(ellipsoid_args, sphere_radius): |
| 88 | + structure = Structure( |
| 89 | + [ |
| 90 | + Atom("Ni", [0.0, 0.0, 0.0]), |
| 91 | + Atom("C", [0.5, 0.5, 0.5]), |
| 92 | + Atom("O", [1.0, 0.0, 0.0]), |
| 93 | + Atom("H", [1.1, 0.0, 0.0]), |
| 94 | + ], |
| 95 | + lattice=Lattice(1, 1, 1, 90, 90, 90), |
| 96 | + ) |
| 97 | + |
| 98 | + actual = [(atom.element, tuple(atom.xyz)) for atom in make_ellipsoid(structure, *ellipsoid_args)] |
| 99 | + expected = [(atom.element, tuple(atom.xyz)) for atom in make_sphere(structure, sphere_radius)] |
| 100 | + |
| 101 | + assert actual == expected |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.parametrize( |
| 105 | + ("ellipsoid_args", "expected"), |
| 106 | + [ |
| 107 | + ((0.5,), [("C", (0.5, 0.5, 0.5))]), |
| 108 | + ((0.4,), [("C", (0.5, 0.5, 0.5))]), |
| 109 | + ], |
| 110 | +) |
| 111 | +def test_make_ellipsoid(ellipsoid_args, expected): |
| 112 | + structure = Structure( |
| 113 | + [ |
| 114 | + Atom("Ni", [0.0, 0.0, 0.0]), |
| 115 | + Atom("C", [0.5, 0.5, 0.5]), |
| 116 | + Atom("O", [1.0, 0.0, 0.0]), |
| 117 | + Atom("H", [1.1, 0.0, 0.0]), |
| 118 | + ], |
| 119 | + lattice=Lattice(1, 1, 1, 90, 90, 90), |
| 120 | + ) |
| 121 | + actual = [(atom.element, tuple(atom.xyz)) for atom in make_ellipsoid(structure, *ellipsoid_args)] |
| 122 | + assert actual == expected |
0 commit comments