Skip to content

Commit

Permalink
Law: linear density (#853)
Browse files Browse the repository at this point in the history
* Create law and test

* Add definitions

* Fix documentation
  • Loading branch information
alesanter authored Jul 16, 2024
1 parent 5bbcae7 commit 6fbd82a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
14 changes: 14 additions & 0 deletions symplyphysics/laws/quantities/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Quantities
==========
Physical quantities can often be categorized as being either *extensive* or *intensive*, according to
how the property changes when the size or extent of the system changes.
Intensive quantity
A quantity whose magnitude is independent of the size of the system.
Extensive quantity
A quantity whose magnitude scales linearly with the size of the system,
i.e. it is additive with respect to its subsystems.
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
Extensive quantity is linear density times length
=================================================
An extensive quantity of interest can be obtained by multiplying the corresponding linear
density by length.
"""

from sympy import Eq, Symbol as SymSymbol
from symplyphysics import (
units,
Quantity,
Symbol,
validate_input,
)
from symplyphysics.core.dimensions import assert_equivalent_dimension

extensive_quantity = SymSymbol("extensive_quantity")
"""
Extensive quantity
Symbol:
X
"""

linear_density = SymSymbol("linear_density")
r"""
Intensive linear density
Symbol:
lambda_X
Latex:
:math:`\lambda_X`
"""

length = Symbol("length", units.length)
"""
Length
Symbol:
L
"""

law = Eq(extensive_quantity, linear_density * length)
r"""
X = lambda_X * L
Latex:
:math:`X = \lambda_X L`
"""


@validate_input(length_=length)
def calculate_extensive_quantity(
linear_density_: Quantity,
length_: Quantity,
) -> Quantity:
result_expr = law.rhs.subs({
linear_density: linear_density_,
length: length_,
})
result = Quantity(result_expr)

assert_equivalent_dimension(
result,
"return",
"calculate_extensive_quantity",
linear_density_.dimension * units.length,
)

return result
37 changes: 37 additions & 0 deletions test/quantities/quantity_is_linear_density_times_length_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from collections import namedtuple
from pytest import fixture, raises
from symplyphysics import (
assert_equal,
units,
Quantity,
errors,
)
from symplyphysics.laws.quantities import quantity_is_linear_density_times_length as law

Args = namedtuple("Args", "m q l")


@fixture(name="test_args")
def test_args_fixture() -> Args:
m = Quantity(4.4 * units.kilogram / units.meter)
q = Quantity(3.5 * units.coulomb / units.meter)
l = Quantity(0.1 * units.meter)
return Args(m=m, q=q, l=l)


def test_mass_law(test_args: Args) -> None:
result = law.calculate_extensive_quantity(test_args.m, test_args.l)
assert_equal(result, 0.44 * units.kilogram)


def test_charge_law(test_args: Args) -> None:
result = law.calculate_extensive_quantity(test_args.q, test_args.l)
assert_equal(result, 0.35 * units.coulomb)


def test_bad_length(test_args: Args) -> None:
lb = Quantity(units.second)
with raises(errors.UnitsError):
law.calculate_extensive_quantity(test_args.m, lb)
with raises(TypeError):
law.calculate_extensive_quantity(test_args.m, 100)

0 comments on commit 6fbd82a

Please sign in to comment.