def test_pass_box(self, mb_ethane): mb_box = Box(lengths=[3, 3, 3]) top = from_mbuild(mb_ethane, box=mb_box) assert_allclose_units(top.box.lengths, [3, 3, 3] * u.nm, rtol=1e-5, atol=1e-8)
def test_power(): """ Take units to some power. """ from sympy import nsimplify from unyt import dimensionless pc_mks = m_per_pc mK_mks = 1e-3 u1_dims = mass * length**2 * time**-3 * temperature**4 u1 = Unit("kg * pc**2 * s**-3 * mK**4") u2 = u1**2 assert u2.dimensions == u1_dims**2 assert_allclose_units(u2.base_value, (pc_mks**2 * mK_mks**4)**2, 1e-12) u3 = u1**(-1.0 / 3) assert u3.dimensions == nsimplify(u1_dims**(-1.0 / 3)) assert_allclose_units(u3.base_value, (pc_mks**2 * mK_mks**4)**(-1.0 / 3), 1e-12) assert u1**0.0 == dimensionless
def test_double_conversion(self, templates): # Pick some OPLS parameters at random params = { 'k0': 1.38 * u.Unit('kJ/mol'), 'k1': -0.51 * u.Unit('kJ/mol'), 'k2': 2.2 * u.Unit('kJ/mol'), 'k3': -0.25 * u.Unit('kJ/mol'), 'k4': 1.44 * u.Unit('kJ/mol') } opls_torsion_potential = templates['OPLSTorsionPotential'] name = opls_torsion_potential.name expression = opls_torsion_potential.expression variables = opls_torsion_potential.independent_variables opls_connection_type = DihedralType(name=name, expression=expression, independent_variables=variables, parameters=params) # Convert connection to RB ryckaert_connection_type = convert_opls_to_ryckaert( opls_connection_type) # Convert connection back to OPLS final_connection_type = convert_ryckaert_to_opls( ryckaert_connection_type) assert_allclose_units([*opls_connection_type.parameters.values()], [*final_connection_type.parameters.values()], rtol=1e-5, atol=1e-8)
def test_box_dims(self, typed_ar_system): n_topology_sites = len(typed_ar_system.sites) omm_top = to_openmm(typed_ar_system) topology_lengths = typed_ar_system.box.lengths omm_lengths = omm_top.getUnitCellDimensions() assert_allclose_units(topology_lengths.value, omm_lengths._value, rtol=1e-5, atol=1e-8)
def test_multiplication(): """ Multiply two units. """ msun_mks = mass_sun_kg pc_mks = m_per_pc # Create symbols msun_sym = Symbol("Msun", positive=True) pc_sym = Symbol("pc", positive=True) s_sym = Symbol("s", positive=True) # Create units u1 = Unit("Msun") u2 = Unit("pc") # Mul operation u3 = u1 * u2 assert u3.expr == msun_sym * pc_sym assert_allclose_units(u3.base_value, msun_mks * pc_mks, 1e-12) assert u3.dimensions == mass * length # Pow and Mul operations u4 = Unit("pc**2") u5 = Unit("Msun * s") u6 = u4 * u5 assert u6.expr == pc_sym**2 * msun_sym * s_sym assert_allclose_units(u6.base_value, pc_mks**2 * msun_mks, 1e-12) assert u6.dimensions == length**2 * mass * time
def test_read_epsilon(self, filename=get_path('data.lammps')): read = read_lammpsdata(filename) lj = [i.parameters for i in read.atom_types][0] assert_allclose_units(lj['epsilon'], u.unyt_array(0.0717, (u.kcal / u.mol)), rtol=1e-5, atol=1e-8)
def test_read_sigma(self, filename=get_path('data.lammps')): read = read_lammpsdata(filename) lj = [i.parameters for i in read.atom_types][0] assert_allclose_units(lj['sigma'], u.unyt_array(3, u.angstrom), rtol=1e-5, atol=1e-8)
def test_read_mass(self, filename=get_path('data.lammps')): read = read_lammpsdata(filename) masses = [i.mass for i in read.atom_types] assert_allclose_units(masses, u.unyt_array(1.0079, u.g), rtol=1e-5, atol=1e-8)
def test_pass_box_bounding(self, mb_ethane): mb_ethane.periodicity = [0, 0, 0] top = from_mbuild(mb_ethane) assert_allclose_units( top.box.lengths, (mb_ethane.boundingbox.lengths + [0.5, 0.5, 0.5]) * u.nm, rtol=1e-5, atol=1e-8)
def test_read_charge(self, filename=get_path('data.lammps')): read = read_lammpsdata(filename) charge = [i.charge for i in read.atom_types] assert_allclose_units(charge, u.unyt_array(0, u.C), rtol=1e-5, atol=1e-8)
def test_scaled_vectors(self): box = Box(lengths=u.unyt_array((2, 2, 2), u.nm), angles=u.degree*[40.0, 50.0, 60.0]) vectors = box.get_vectors() test_vectors = np.array([[1, 0, 0], [0.5, 0.86603, 0], [0.64278, 0.51344, 0.56852]]) test_vectors = (test_vectors.T * box.lengths).T assert_allclose_units(vectors, test_vectors, rtol=1e-5, atol=u.nm*1e-3) assert vectors.units == u.nm
def test_scaling_unit_vectors(self): box = Box(lengths=u.unyt_array((2, 2, 2), u.nm), angles=u.degree*[40.0, 50.0, 60.0]) u_vectors = box.get_unit_vectors() scaled_u_vectors = (u_vectors.T * box.lengths).T scaled_vectors = box.get_vectors() assert_allclose_units(scaled_vectors, scaled_u_vectors, rtol=1e-5, atol=u.nm*1e-3) assert scaled_u_vectors.units == u.nm
def test_pass_box_bounding(self, mb_ethane): mb_ethane.periodicity = [False, False, False] top = from_mbuild(mb_ethane) assert_allclose_units( top.box.lengths, (mb_ethane.get_boundingbox().lengths) * u.nm, rtol=1e-5, atol=1e-8, )
def test_add_box(self): top = Topology() box = Box(2 * u.nm * np.ones(3)) assert top.box is None top.box = box assert top.box is not None assert_allclose_units( top.box.lengths, u.nm * 2 * np.ones(3), rtol=1e-5, atol=1e-8 )
def test_read_water(self, typed_water_system): write_lammpsdata(typed_water_system, filename='data.water') water = read_lammpsdata('data.water') assert_allclose_units(water.sites[0].charge, u.unyt_array(-0.834, u.elementary_charge), rtol=1e-5, atol=1e-8) assert water.n_sites == 6 assert water.n_connections == 6
def test_particle_positions(self, typed_ar_system): typed_ar_system.sites[0].position = (1, 1, 1) * u.nanometer omm_top = to_openmm(typed_ar_system, openmm_object="modeller") assert_allclose_units( omm_top.positions._value, typed_ar_system.positions.value, rtol=1e-5, atol=1e-8, )
def test_bel_neper(): assert Unit("B").dimensions == Unit("Np").dimensions a = 1 * Unit("B") / (np.log(10) / 2) assert_allclose_units(a.to("Np"), 1 * Unit("Np")) a = 2 * Unit("B") b = 20 * Unit("decibel") assert (a == b).all() c = 2 * Unit("Np") d = 20 * Unit("decineper") assert (c == d).all() assert Unit("dB")**1 == Unit("dB")
def test_full_io(self): original_top = read_xyz(get_fn("ethane.xyz")) write_xyz(original_top, "full_conversion.xyz") new_top = read_xyz("full_conversion.xyz") assert original_top.n_sites == new_top.n_sites assert original_top.n_connections == new_top.n_connections assert_allclose_units(original_top.positions, new_top.positions, rtol=1e-5, atol=1e-8)
def test_read_gro(self): top = read_gro(get_fn('acn.gro')) assert top.name == 'ACN' assert top.n_sites == 6 assert_allclose_units(top.box.lengths, 4*np.ones(3)*u.nm, rtol=1e-5, atol=1e-8) top = read_gro(get_fn('350-waters.gro')) assert top.name == 'Generic title' assert top.n_sites == 1050 assert_allclose_units(top.box.lengths, 2.20866*np.ones(3)*u.nm, rtol=1e-5, atol=1e-8)
def test_read_lammps_triclinic(self, typed_ar_system): typed_ar_system.box = Box(lengths=[1, 1, 1], angles=[60, 90, 120]) write_lammpsdata(typed_ar_system, filename='data.triclinic') read = read_lammpsdata('data.triclinic') assert_allclose_units(read.box.lengths, u.unyt_array([1, 1, 1], u.nm), rtol=1e-5, atol=1e-8) assert_allclose_units(read.box.angles, u.unyt_array([60, 90, 120], u.degree), rtol=1e-5, atol=1e-8)
def test_new_potential(self): new_potential = ParametricPotential( name='mypotential', expression='a*x+b', parameters={ 'a': 1.0*u.g, 'b': 1.0*u.m}, independent_variables={'x'} ) assert new_potential.name == 'mypotential' assert new_potential.expression == sympy.sympify('a*x+b') assert_allclose_units(new_potential.parameters['a'], 1.0 * u.g, rtol=1e-5, atol=1e-8) assert_allclose_units(new_potential.parameters['b'], 1.0 * u.m, rtol=1e-5, atol=1e-8) assert new_potential.independent_variables == {sympy.symbols('x')}
def test_from_parmed_basic(self, angles): struc = pmd.load_file(get_fn('ethane.mol2'), structure=True) top = from_parmed(struc, refer_type=False) for site in top.sites: assert site.atom_type is None for connection in top.connections: assert connection.connection_type is None assert top.n_sites == 8 assert top.n_bonds == 7 assert top.box is not None lengths = u.nm * [0.714, 0.7938, 0.6646] assert_allclose_units(top.box.lengths, lengths, rtol=1e-5, atol=1e-8) assert_allclose_units(top.box.angles, angles, rtol=1e-5, atol=1e-8)
def test_create_from_expr(): """ Create units from sympy Exprs and check attributes. """ pc_mks = m_per_pc yr_mks = sec_per_year # Symbol expr s1 = Symbol("pc", positive=True) s2 = Symbol("yr", positive=True) # Mul expr s3 = s1 * s2 # Pow expr s4 = s1**2 * s2**(-1) u1 = Unit(s1) u2 = Unit(s2) u3 = Unit(s3) u4 = Unit(s4) assert u1.expr == s1 assert u2.expr == s2 assert u3.expr == s3 assert u4.expr == s4 assert_allclose_units(u1.base_value, pc_mks, 1e-12) assert_allclose_units(u2.base_value, yr_mks, 1e-12) assert_allclose_units(u3.base_value, pc_mks * yr_mks, 1e-12) assert_allclose_units(u4.base_value, pc_mks**2 / yr_mks, 1e-12) assert u1.dimensions == length assert u2.dimensions == time assert u3.dimensions == length * time assert u4.dimensions == length**2 / time
def test_create_with_duplicate_dimensions(): """ Create units with overlapping dimensions. Ex: km/Mpc. """ u1 = Unit("J * s**-1") u2 = Unit("km/s/Mpc") km_mks = m_per_km Mpc_mks = m_per_mpc assert u1.base_value == 1 assert u1.dimensions == power assert_allclose_units(u2.base_value, km_mks / Mpc_mks, 1e-12) assert u2.dimensions == rate
def test_setters(self, charge, mass): new_type = AtomType() new_type.name = "SettingName" new_type.charge = -1.0 * charge new_type.mass = 1 * mass new_type.independent_variables = 'r' new_type.parameters = {'sigma': 1 * u.nm, 'epsilon': 10 * u.Unit('kcal / mol')} new_type.expression = 'r * sigma * epsilon' assert new_type.name == "SettingName" assert_allclose_units(new_type.charge, -1.0 * charge, rtol=1e-5, atol=1e-8) assert_allclose_units(new_type.mass, 1 * mass, rtol=1e-5, atol=1e-8) assert new_type.independent_variables == {sympy.symbols('r')} assert new_type.parameters == {'sigma': 1 * u.nm, 'epsilon': 10 * u.Unit('kcal / mol')} assert new_type.expression == sympy.sympify('r * sigma * epsilon')
def test_base_equivalent(): """ Check base equivalent of a unit. """ Msun_mks = mass_sun_kg Mpc_mks = m_per_mpc u1 = Unit("Msun * Mpc**-3") u2 = Unit("kg * m**-3") u3 = u1.get_base_equivalent() assert u2.expr == u3.expr assert u2 == u3 assert_allclose_units(u1.base_value, Msun_mks / Mpc_mks ** 3, 1e-12) assert u2.base_value == 1 assert u3.base_value == 1 mass_density = mass / length ** 3 assert u1.dimensions == mass_density assert u2.dimensions == mass_density assert u3.dimensions == mass_density assert_allclose_units( u1.get_conversion_factor(u3)[0], Msun_mks / Mpc_mks ** 3, 1e-12 ) with pytest.raises(UnitConversionError): u1.get_conversion_factor(Unit("m")) with pytest.raises(UnitConversionError): u1.get_conversion_factor(Unit("degF")) reg = UnitRegistry(unit_system=cgs_unit_system) u = Unit("kg", registry=reg) assert u.get_base_equivalent() == Unit("g") u = Unit("kg") assert u.get_base_equivalent() == Unit("kg") u = Unit("A") assert u.get_base_equivalent(unit_system="mks") == Unit("A")
def test_new_atom_type(self, charge, mass): new_type = AtomType(name='mytype', charge=charge, mass=mass, parameters={'sigma': 1 * u.nm, 'epsilon': 10 * u.Unit('kcal / mol')}, independent_variables={'r'}) assert new_type.name == 'mytype' assert_allclose_units(new_type.charge, charge, rtol=1e-5, atol=1e-8) assert_allclose_units(new_type.parameters['sigma'], 1 * u.nm, rtol=1e-5, atol=1e-8) assert_allclose_units(new_type.parameters['epsilon'], 10 * u.Unit('kcal / mol'), rtol=1e-5, atol=1e-8) assert_allclose_units(new_type.mass, mass, rtol=1e-5, atol=1e-8)
def test_from_parmed_parametrized_structure(self, angles): struc = pmd.load_file(get_fn('ethane.top'), xyz=get_fn('ethane.gro')) top = from_parmed(struc) assert top.n_sites == 8 assert top.n_bonds == 7 assert top.n_angles == 12 assert top.n_dihedrals == 9 assert top.n_connections == 28 for site in top.sites: assert site.atom_type is not None assert site.charge is not None for connection in top.connections: assert connection.connection_type is not None assert top.box is not None lengths = u.nm * [0.714, 0.7938, 0.6646] assert_allclose_units(top.box.lengths, lengths, rtol=1e-5, atol=1e-8) assert_allclose_units(top.box.angles, angles, rtol=1e-5, atol=1e-8)
def test_new_potential(self): new_potential = ParametricPotential( name="mypotential", expression="a*x+b", parameters={ "a": 1.0 * u.g, "b": 1.0 * u.m }, independent_variables={"x"}, ) assert new_potential.name == "mypotential" assert new_potential.expression == sympy.sympify("a*x+b") assert_allclose_units(new_potential.parameters["a"], 1.0 * u.g, rtol=1e-5, atol=1e-8) assert_allclose_units(new_potential.parameters["b"], 1.0 * u.m, rtol=1e-5, atol=1e-8) assert new_potential.independent_variables == {sympy.symbols("x")}
def test_division(): """ Divide two units. """ pc_mks = m_per_pc km_mks = m_per_km # Create symbols pc_sym = Symbol("pc", positive=True) km_sym = Symbol("km", positive=True) s_sym = Symbol("s", positive=True) # Create units u1 = Unit("pc") u2 = Unit("km * s") u3 = u1 / u2 assert u3.expr == pc_sym / (km_sym * s_sym) assert_allclose_units(u3.base_value, pc_mks / km_mks, 1e-12) assert u3.dimensions == 1 / time