Download tests/test_physics_kernel.py from tailot/lingologos-physics: direct link, hf CLI and curl.
- Browser
- Download file 6.35 kB
-
https://huggingface.co/datasets/tailot/lingologos-physics/resolve/main/tests/test_physics_kernel.py
- Command line
-
hf download hf://datasets/tailot/lingologos-physics/tests/test_physics_kernel.py
-
curl -L -o test_physics_kernel.py https://huggingface.co/datasets/tailot/lingologos-physics/resolve/main/tests/test_physics_kernel.py
6.35 kB
| """The physics the nodes lean on: units, symbols, dimensional analysis, networks, limits, constants. | |
| Nothing here tests the chain; these are the small facts the chain is built out of. Each of them came | |
| from the physics itself - the seven base dimensions, the definition of the unit, the node equations of | |
| a lumped resistive network - and each is the kind of fact that a reader of the dataset should be able | |
| to check without reading a line of code. | |
| """ | |
| from fractions import Fraction | |
| import pytest | |
| from lgphys import circuit, consts, laws, parse_a, parse_b, pi, terms, tree, units | |
| BASE = (0, 0, 0, 0, 0, 0, 0) | |
| def test_the_seven_base_dimensions_are_the_si_seven(): | |
| assert units.BASES == ("M", "L", "T", "I", "Th", "N", "J") | |
| def test_a_unit_carries_its_factor_and_its_dimension(): | |
| factor, dim, text = units.parse_unit("km/h") | |
| assert factor == Fraction(5, 18) and dim == (0, 1, -1, 0, 0, 0, 0) and text == "km/h" | |
| assert units.parse_unit("ohm")[1] == (1, 2, -3, -2, 0, 0, 0) | |
| assert units.parse_unit("J")[1] == (1, 2, -2, 0, 0, 0, 0) == units.parse_unit("kg m^2 / s^2")[1] | |
| def test_an_unknown_unit_is_refused_and_not_guessed(): | |
| with pytest.raises(KeyError): | |
| units.parse_unit("banana") | |
| def test_the_symbol_table_gives_the_same_dimension_to_a_symbol_and_its_word(): | |
| for short, long in (("m", "mass"), ("v", "speed"), ("F", "force"), ("g", "gravity"), | |
| ("V", "voltage"), ("R", "resistance"), ("t", "time")): | |
| assert units.SYMBOLS[short] == units.SYMBOLS[long], short | |
| def test_two_sides_of_a_law_agree_when_their_dimensions_agree(): | |
| left = terms.evaluate(("mul", ("sym", "F"), ("sym", "t"))) | |
| right = terms.evaluate(("mul", ("sym", "m"), ("sym", "v"))) | |
| assert terms.same(left[1], right[1]) | |
| assert not terms.same(units.SYMBOLS["energy"], units.SYMBOLS["power"]) | |
| def test_an_unknown_symbol_is_an_error_rather_than_a_number(): | |
| with pytest.raises(terms.ParseError): | |
| terms.evaluate(("sym", "banana")) | |
| def test_dimensional_analysis_of_a_period_from_mass_and_stiffness(): | |
| found = pi.group(units.SYMBOLS["period"], [units.SYMBOLS["mass"], units.SYMBOLS["stiffness"]], | |
| ["mass", "stiffness"]) | |
| assert [Fraction(value) for value in found["exponents"]] == [Fraction(1, 2), Fraction(-1, 2)] | |
| # mass carries the kilogram out of the period and stiffness takes the second back out of it. | |
| assert found["names"] == ["mass", "stiffness"] | |
| # The seven base dimensions are split into the rows that solved the exponents and the rows that | |
| # were only asked whether they agree: a solution is believed because the rest accepted it too. | |
| assert sorted(found["rows_used"] + found["rows_checked"]) == list(range(7)) | |
| assert not set(found["rows_used"]) & set(found["rows_checked"]) | |
| def test_dimensional_analysis_of_a_speed_from_length_and_gravity(): | |
| found = pi.group(units.SYMBOLS["speed"], [units.SYMBOLS["length"], units.SYMBOLS["gravity"]], | |
| ["length", "gravity"]) | |
| assert [Fraction(value) for value in found["exponents"]] == [Fraction(1, 2), Fraction(1, 2)] | |
| def test_a_dimension_the_set_cannot_reach_is_said_so_rather_than_solved(): | |
| with pytest.raises(pi.ModelError): | |
| pi.group(units.SYMBOLS["charge"], [units.SYMBOLS["mass"], units.SYMBOLS["length"]], | |
| ["mass", "length"]) | |
| def test_a_network_with_a_node_touching_nothing_has_no_potentials(): | |
| open_network = parse_a.parse( | |
| "potential node 3 circuit 4 resistors [ 1 2 10 ; 2 3 20 ] sources [ 1 1 ]" | |
| " size 1/10 frequency 50") | |
| assert "connected_network" in circuit.validate(open_network) | |
| assert circuit.potentials(open_network) is None | |
| def test_two_resistors_between_the_same_pair_are_one_branch(): | |
| parallel = parse_a.parse( | |
| "potential node 1 circuit 2 resistors [ 1 2 10 ; 1 2 20 ] sources [ 1 1 ]" | |
| " size 1/10 frequency 50") | |
| assert circuit.validate(parallel) == [] | |
| # 10 and 20 ohm in parallel carry 1 A together: 20/3 V across the pair, exactly. | |
| assert circuit.by_cramer(parallel, 1) == Fraction(20, 3) | |
| def test_the_series_network_divides_the_node_potentials_by_the_resistances(): | |
| series = parse_a.parse( | |
| "potential node 1 circuit 3 resistors [ 1 2 10 ; 2 3 20 ] sources [ 1 1 ]" | |
| " size 1/10 frequency 50") | |
| assert circuit.by_cramer(series, 1) == Fraction(30) | |
| assert circuit.by_cramer(series, 2) == Fraction(20) | |
| def test_a_guard_holds_below_its_threshold_and_stops_above_it(): | |
| guard = laws.guards_for("potential")[0] | |
| assert guard["name"] == "quasistatic" and guard["comparison"] == "lt" | |
| assert laws.guard_holds(guard, Fraction(1, 200)) | |
| assert not laws.guard_holds(guard, Fraction(2)) | |
| assert laws.guards_for("convert") == [] | |
| def test_every_band_of_the_refused_by_design_table_is_triggered_by_its_own_words(): | |
| for band in laws.ABSTENTIONS: | |
| assert laws.abstention_for("tell me about " + band["trigger"])["name"] == band["name"] | |
| assert laws.abstention_for("convert 3 km to m") is None | |
| def test_the_ontology_declares_every_constant_of_the_table_exactly_once(): | |
| block = consts.ontology_block() | |
| declared = consts.read_ontology(block) | |
| assert sorted(declared) == sorted(consts.smt_name(name) for name in consts.names()) | |
| assert consts.compare_with_ontology(declared) == [] | |
| def test_a_measured_constant_carries_its_uncertainty_and_an_exact_one_does_not(): | |
| assert consts.value("c") == Fraction(299792458, 1) | |
| for name in consts.names(): | |
| middle, low, high = consts.bounds(name) | |
| assert low <= middle <= high, name | |
| if consts.status(name) != "exact": | |
| assert low < middle < high, name | |
| else: | |
| assert low == middle == high, name | |
| def test_both_roads_write_the_same_canonical_sentence(): | |
| for sentence in ("convert 5 km/h to m/s", "audit ( F * t ) = ( m * v )", | |
| "pi period from mass, stiffness"): | |
| assert tree.canon(parse_a.parse(sentence)) == tree.canon(parse_b.parse(sentence)) | |
| assert tree.canon(parse_a.parse("convert 5 km/h to m/s")) == "convert 5 km/h to m/s" | |
| def test_a_sentence_outside_the_fragment_is_a_parse_error_on_both_roads(): | |
| for reader in (parse_a, parse_b): | |
| with pytest.raises(tree.ParseError): | |
| reader.parse("what is the meaning of life") | |