Skip to content

Commit

Permalink
Patch symbols 1 (#904)
Browse files Browse the repository at this point in the history
* Derive law

* Rename unit

* Derive law

* Add link

* Rename files

* Create outline of law

* Create law and test

* Create law and test

* Update documentation

* Update documentation

* Create law and test

* Create first part of proof

* Create law and test

* Finish proof

* Move law

* Create proof

* Add condition

* Remove unnecessary law

* Refactor documentation

* Refactor documentation

* Refactor symbols

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Refactor documentation

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Refactor documentation

* Add symbols

* Add symbols

* Add symbols

* Refactor documentation

* Add symbols

* Refactor documentation

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Refactor documentation

* Add symbols

* Add symbols

* Add symbols

* Refactor documentation

* Refactor documentation

* Add symbols

* Add symbols

* Add symbols

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Refactor documentation

* Add symbols

* Fix issues

* Fix documentation

* Fix printer

* Add symbols

* Add symbols

* Add symbols

* Refactor documentation

* Add symbols

* Add symbols

* Refactor documentation

* Refactor documentation

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Add symbols

* Fix issues

* Fix symbols

* Fix issues

* Fix symbols

* Fix issue

* Add direction to rst

* Fix cloning functions

* Fix cloning

* Fix documentation

* Fix symbol cloning

* Fix printing of indexed symbols

* Fix symbol cloning

* Fix issues

* Fix symbol cloning

* Fix issues

* Add role preprocessor

* Fix role preprocessor

* Add new flag to build

* Fix role preprocessor

* Fix definitions

* Fix documentation

* Fix documentation

* Add role preprocessor

* Fix quantities notation

* Add note

* Fix indexed symbols

* Fix calculation function

* Fix mypy

* Fix plots

* Fix issues

* Fix documentation

* Fix documentation

* Fix types

* Add latex symbols

* Fix linter

---------

Co-authored-by: blackyblack <[email protected]>
  • Loading branch information
alesanter and blackyblack authored Sep 19, 2024
1 parent 14da600 commit e4176ca
Show file tree
Hide file tree
Showing 294 changed files with 2,396 additions and 1,997 deletions.
37 changes: 32 additions & 5 deletions docs/build.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env python3

import argparse
import pathlib
import shutil
import sys
from typing import Sequence
from pathlib import Path
from sphinx.application import Sphinx

from symplyphysics.docs.build import generate_laws_docs
from symplyphysics.docs import symbols_role, quantity_notation_role


def get_parser() -> argparse.ArgumentParser:
Expand Down Expand Up @@ -51,30 +52,56 @@ def get_parser() -> argparse.ArgumentParser:
dest="conf_dir",
default="docs",
help="directory where conf.py file is stored")

parser.add_argument("-q",
"--quiet",
action="store_true",
dest="quiet",
help="suppress rST files generation text",
)

parser.add_argument("-R",
"--rst-only",
action="store_true",
dest="rst_only",
help="only generate rST files and suppress HTML generation",
)

return parser


def process_generated_files(generated_dir: str) -> None:
for file_path in Path(generated_dir).iterdir():
with open(file_path, "r+", encoding="utf-8") as file:
doc = file.read()

# processing logic goes here
doc = symbols_role.process_string(doc, file_path)
doc = quantity_notation_role.process_string(doc, file_path)

file.seek(0)
file.truncate(0)
file.write(doc)


def main(argv: Sequence[str] = ()) -> None:
args = get_parser().parse_args(argv or sys.argv[1:])

# Generate Symplyphysics rst files
generate_laws_docs(args.laws_source_dir, args.generated_dir, args.exclude_dirs, args.quiet)

# Copy index.rst to 'generated' folder
index_file = pathlib.Path(args.conf_dir).joinpath("index.rst")
out_index_file = pathlib.Path(args.generated_dir).joinpath("index.rst")
index_file = Path(args.conf_dir) / "index.rst"
out_index_file = Path(args.generated_dir) / "index.rst"
shutil.copyfile(index_file, out_index_file, follow_symlinks=True)

process_generated_files(args.generated_dir)

if args.rst_only:
return

# Build HTML docs
doctrees = pathlib.Path(args.output_dir).joinpath(".doctrees/")
doctrees = Path(args.output_dir) / ".doctrees/"
app = Sphinx(args.generated_dir, args.conf_dir, "html", doctrees, args.output_dir)
app.build()

Expand Down
8 changes: 4 additions & 4 deletions examples/electricity/two_resistors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from symplyphysics.laws.electricity.circuits import admittance_in_parallel_connection as total_admittance_law
from symplyphysics.laws.electricity import admittance_is_conductance_and_susceptance as admittance_law
from symplyphysics.laws.electricity.circuits import resistance_in_serial_connection as serial_resistance
from symplyphysics.definitions import electrical_conductivity_is_inversed_resistance as conductivity_law
from symplyphysics.definitions import electrical_conductance_is_inversed_resistance as conductivity_law

# Two resistors are connected across a 12 V battery with internal resistance of 1 Ohm.
# When they are connected in parallel, the current in the circuit is 4 A.
Expand All @@ -25,10 +25,10 @@
## Find resistance R12 using the law of conductance

sigma1 = solve(conductivity_law.definition,
conductivity_law.conductivity)[0].subs({conductivity_law.resistance: R1})
conductivity_law.conductance)[0].subs({conductivity_law.resistance: R1})

sigma2 = solve(conductivity_law.definition,
conductivity_law.conductivity)[0].subs({conductivity_law.resistance: R2})
conductivity_law.conductance)[0].subs({conductivity_law.resistance: R2})

index_local = Idx("index_local", (1, 2))

Expand All @@ -47,7 +47,7 @@
)

resistance_definition = conductivity_law.definition.subs(
{conductivity_law.conductivity: sigma_parallel})
{conductivity_law.conductance: sigma_parallel})
R12_parallel = solve(resistance_definition, conductivity_law.resistance)[0]

## The sum resistance R12 is still connected in series to the internal resistance of the battery
Expand Down
6 changes: 3 additions & 3 deletions plots/electricity/coulomb_force_for_electron.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from sympy import Symbol
from sympy.plotting import plot
from sympy.plotting.plot import MatplotlibBackend
from symplyphysics import print_expression, units
from symplyphysics import print_expression, quantities
from symplyphysics.core.convert import convert_to_si
from symplyphysics.laws.electricity import electrostatic_force_via_charges_and_distance as coulomb_law

CHARGE_OF_ELECTRON = -1.6 * 1E-19
VACUUM_PERMITTIVITY = convert_to_si(units.vacuum_permittivity)
VACUUM_PERMITTIVITY = convert_to_si(quantities.vacuum_permittivity)

print(f"Formula is:\n{print_expression(coulomb_law.law)}")

Expand All @@ -17,7 +17,7 @@
coulomb_law.first_charge: CHARGE_OF_ELECTRON,
coulomb_law.second_charge: CHARGE_OF_ELECTRON,
coulomb_law.distance: distance,
units.vacuum_permittivity: VACUUM_PERMITTIVITY,
quantities.vacuum_permittivity: VACUUM_PERMITTIVITY,
}).rhs

p1 = plot(force_equation, (distance, 1, 5),
Expand Down
8 changes: 4 additions & 4 deletions plots/electricity/net_electric_field_of_two_charges.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

from sympy import symbols, plot
from sympy.plotting.plot import MatplotlibBackend
from symplyphysics import print_expression, units
from symplyphysics import print_expression, quantities
from symplyphysics.core.convert import convert_to_si
from symplyphysics.laws.electricity import electric_field_due_to_point_charge as electric_field

# Description
## Reference to example (net_field_is_zero_from_two_point_charges.py)[../../../examples/electricity/net_field_is_zero_from_two_point_charges.py]
## Plot the net field of two point charges along the x-axis.

VACUUM_PERMITTIVITY = convert_to_si(units.vacuum_permittivity)
VACUUM_PERMITTIVITY = convert_to_si(quantities.vacuum_permittivity)

CHARGE_1 = 2.1e-8 # C
CHARGE_2 = -4.0 * CHARGE_1
Expand All @@ -23,12 +23,12 @@
field_of_charge_1 = electric_field.law.rhs.subs({
electric_field.charge: CHARGE_1,
electric_field.distance: position - POSITION_OF_CHARGE_1,
units.vacuum_permittivity: VACUUM_PERMITTIVITY,
quantities.vacuum_permittivity: VACUUM_PERMITTIVITY,
})
field_of_charge_2 = electric_field.law.rhs.subs({
electric_field.charge: CHARGE_2,
electric_field.distance: position - POSITION_OF_CHARGE_2,
units.vacuum_permittivity: VACUUM_PERMITTIVITY,
quantities.vacuum_permittivity: VACUUM_PERMITTIVITY,
})
net_field = field_of_charge_1 + field_of_charge_2

Expand Down
19 changes: 14 additions & 5 deletions plots/thermodynamics/carnot_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sympy import Eq, solve, symbols
from sympy.plotting import plot
from sympy.plotting.plot import MatplotlibBackend
from symplyphysics import units
from symplyphysics import units, quantities
from symplyphysics.laws.thermodynamics import pressure_and_volume_in_isothermal_process as isothermal_law
from symplyphysics.laws.thermodynamics import pressure_and_volume_in_adiabatic_process as adiabatic_law
from symplyphysics.laws.thermodynamics.equations_of_state import ideal_gas_equation
Expand All @@ -12,13 +12,22 @@

_isothermal_condition = Eq(_temperature_start, _temperature_end)

ideal_gas_equation_start = ideal_gas_equation.law.subs({
ideal_gas_equation_subs = ideal_gas_equation.law.subs({
quantities.molar_gas_constant: units.molar_gas_constant,
})

adiabatic_law_subs = [
eqn.subs(quantities.molar_gas_constant, units.molar_gas_constant)
for eqn in adiabatic_law.law
]

ideal_gas_equation_start = ideal_gas_equation_subs.subs({
ideal_gas_equation.pressure: isothermal_law.initial_pressure,
ideal_gas_equation.volume: isothermal_law.initial_volume,
ideal_gas_equation.temperature: _temperature_start,
})

ideal_gas_equation_end = ideal_gas_equation.law.subs({
ideal_gas_equation_end = ideal_gas_equation_subs.subs({
ideal_gas_equation.pressure: isothermal_law.final_pressure,
ideal_gas_equation.volume: isothermal_law.final_volume,
ideal_gas_equation.temperature: _temperature_end,
Expand All @@ -31,15 +40,15 @@
)[0][isothermal_law.final_pressure]

adiabatic_pressure = solve(
adiabatic_law.law,
adiabatic_law_subs,
(adiabatic_law.initial_pressure, adiabatic_law.final_temperature, adiabatic_law.final_pressure),
dict=True,
)[0][adiabatic_law.final_pressure]

# We need adiabatic volume law to detect the point on the 'volume' axis where
# we adiabatic compression should begin in order to close the cycle
adiabatic_volume = solve(
adiabatic_law.law,
adiabatic_law_subs,
(adiabatic_law.initial_pressure, adiabatic_law.final_pressure, adiabatic_law.final_volume),
dict=True,
)[0][adiabatic_law.final_volume]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sympy import symbols, Eq, solve
from sympy.plotting import plot
from sympy.plotting.plot import MatplotlibBackend
from symplyphysics import print_expression
from symplyphysics import print_expression, quantities, units
from symplyphysics.laws.thermodynamics.equations_of_state.van_der_waals import equation as van_der_waals_law
from symplyphysics.laws.quantities import quantity_is_molar_quantity_times_amount_of_substance as molar_qty_law

Expand All @@ -23,6 +23,7 @@
})

state_equation = van_der_waals_law.law.subs({
quantities.molar_gas_constant: units.molar_gas_constant,
van_der_waals_law.pressure: pressure,
van_der_waals_law.temperature: temperature,
van_der_waals_law.molar_volume: molar_volume,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sympy import symbols, Eq, solve
from sympy.plotting import plot
from sympy.plotting.plot import MatplotlibBackend
from symplyphysics import print_expression
from symplyphysics import print_expression, quantities, units
from symplyphysics.laws.thermodynamics.equations_of_state.van_der_waals import equation as van_der_waals_law
from symplyphysics.laws.quantities import quantity_is_molar_quantity_times_amount_of_substance as molar_qty_law

Expand All @@ -23,6 +23,7 @@
})

state_equation = van_der_waals_law.law.subs({
quantities.molar_gas_constant: units.molar_gas_constant,
van_der_waals_law.pressure: pressure,
van_der_waals_law.temperature: temperature,
van_der_waals_law.molar_volume: molar_volume,
Expand Down
8 changes: 6 additions & 2 deletions plots/thermodynamics/fundamental_ideal_gas_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
from sympy import symbols, solve, Rational
from sympy.plotting import plot, plot_parametric
from sympy.plotting.plot import MatplotlibBackend
from symplyphysics import convert_to, units
from symplyphysics import convert_to, units, quantities
from symplyphysics.laws.thermodynamics import (
pressure_and_volume_in_adiabatic_process as adiabatic_law,)
from symplyphysics.laws.thermodynamics.equations_of_state import ideal_gas_equation as ideal_gas_law

ideal_gas_law_subs = ideal_gas_law.law.subs({
quantities.molar_gas_constant: units.molar_gas_constant,
})

pressure, volume, temperature = symbols("pressure volume temperature", positive=True)

ideal_gas_eqn = ideal_gas_law.law.subs({
ideal_gas_eqn = ideal_gas_law_subs.subs({
ideal_gas_law.amount_of_substance:
1,
units.molar_gas_constant:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
distribution = speed_distribution.law.rhs.subs({
speed_distribution.equilibrium_temperature:
convert_to(quantities.standard_conditions_temperature, units.kelvin),
units.boltzmann_constant:
quantities.boltzmann_constant:
convert_to(units.boltzmann_constant, units.joule / units.kelvin),
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from sympy.plotting import plot
from sympy.plotting.plot import MatplotlibBackend
from symplyphysics import print_expression, convert_to, Quantity, units
from symplyphysics import print_expression, convert_to, Quantity, units, quantities
from symplyphysics.laws.thermodynamics.maxwell_boltzmann_statistics import speed_distribution

argon_mass_ = convert_to(Quantity(39.948 * units.amu), units.kilogram)
Expand All @@ -23,7 +23,7 @@

distribution = speed_distribution.law.rhs.subs({
speed_distribution.particle_mass: argon_mass_,
units.boltzmann_constant: convert_to(units.boltzmann_constant, units.joule / units.kelvin),
quantities.boltzmann_constant: convert_to(units.boltzmann_constant, units.joule / units.kelvin),
})

for temperature_ in temperatures_:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sympy import solve
from sympy.plotting import plot
from sympy.plotting.plot import MatplotlibBackend
from symplyphysics import print_expression, units
from symplyphysics import print_expression, units, quantities
from symplyphysics.core.convert import convert_to_si
from symplyphysics.laws.thermodynamics.relativistic import (
maxwell_juettner_distribution as distribution_law,
Expand All @@ -18,8 +18,13 @@
reduced_law.reduced_temperature,
)

reduced_law_subs = reduced_law.law.subs({
quantities.boltzmann_constant: units.boltzmann_constant,
quantities.speed_of_light: units.speed_of_light,
})

rhs = solve(
(law, reduced_law.law),
(law, reduced_law_subs),
(distribution_law.distribution_function, reduced_law.reduced_temperature),
dict=True,
)[0][distribution_law.distribution_function]
Expand Down
10 changes: 7 additions & 3 deletions symplyphysics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .core.convert import convert_to, convert_to_float
from .core.operations.sum_indexed import SumIndexed
from .core.operations.product_indexed import ProductIndexed
from .core.symbols.symbols import Function, Symbol, print_expression, clone_symbol, SymbolIndexed, global_index, FunctionNew, SymbolNew, SymbolIndexedNew
from .core.symbols.symbols import Function, Symbol, print_expression, clone_as_symbol, SymbolIndexed, global_index, FunctionNew, SymbolNew, SymbolIndexedNew, clone_as_function
from .core.symbols.prefixes import prefixes
from .core.quantity_decorator import validate_input, validate_output
from .core.vectors.vectors import Vector, QuantityVector
Expand All @@ -32,9 +32,13 @@
"prefixes",
"print_expression",
"subs_list",
"clone_symbol",
"clone_as_symbol",
"SymbolIndexed",
"global_index",
"global_index",
"clone_as_function",
"FunctionNew",
"SymbolNew",
"SymbolIndexedNew",
# convert
"convert_to",
"convert_to_float",
Expand Down
4 changes: 2 additions & 2 deletions symplyphysics/core/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def convert_to_float(value: Expr) -> float:
}


def convert_to_si(value: Expr) -> Expr:
def convert_to_si(value: Expr | float) -> Expr:
quantity = value if isinstance(value, Quantity) else Quantity(value)
dependencies = dimsys_SI.get_dimensional_dependencies(quantity.dimension)
unit = S.One
for dimension, power in dependencies.items():
unit *= _si_conversions[dimension]**power
return convert_to(value, unit)
return convert_to(quantity, unit)


__all__ = [
Expand Down
Loading

0 comments on commit e4176ca

Please sign in to comment.