Skip to content

Hopkinsons law #6524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions electronics/hopkinson's_law.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#https://en.wikipedia.org/wiki/Magnetic_circuit#Hopkinson's_law
from __future__ import annotations
def hopkinsons_law(magnetomotive_force:float, current:float, reluctance:float) -> dict[str, float]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def hopkinsons_law(magnetomotive_force:float, current:float, reluctance:float) -> dict[str, float]:
def hopkinsons_law(magnetomotive_force:float, current:float, reluctance:float) -> dict[str, float]:

"""
Apply Hopkinson's Law, on any two given electrical values, which can be magnetomotive force, current,
and reluctance, and then in a Python dict return name/value pair of the zero value.

>>> hopkinsons_law(magnetomotive_force=10, reluctance=5, current=0)
{'current': 2.0}
>>> hopkinsons_law(magnetomotive_force=0, current=0, reluctance=10)
Traceback (most recent call last):
...
ValueError: One and only one argument must be 0
>>> hopkinsons_law(magnetomotive_force=0, current=1, reluctance=-2)
Traceback (most recent call last):
...
ValueError: Reluctance cannot be negative
>>> hopkinsons_law(reluctance=0, magnetomotive_force=-10, current=1)
{'reluctance': -10.0}
>>> hopkinsons_law(magnetomotive_force=0, current=-1.5, reluctance=2)
{'magnetomotive_force': -3.0}
"""
if (magnetomotive_force, current, reluctance).count(0) != 1:
raise ValueError("One and only one argument must be 0")
if reluctance < 0:
raise ValueError("Reluctance cannot be negative")
if magnetomotive_force == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if magnetomotive_force == 0:
if magnetomotive_force == 0:

return {"magnetomotive_force": float(current * reluctance)}
elif current == 0:
return {"current": magnetomotive_force / reluctance}
elif reluctance == 0:
return {"reluctance": magnetomotive_force / current}
else:
raise ValueError("Exactly one argument must be 0")


if __name__ == "__main__":
import doctest
doctest.testmod()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
doctest.testmod()
doctest.testmod()