From 038f8a00e56bda8e8e2903fe4acf2ca7e3c83a57 Mon Sep 17 00:00:00 2001 From: sadiqebrahim <75269485+sadiqebrahim@users.noreply.github.com> Date: Sat, 29 Oct 2022 19:22:19 +0530 Subject: [PATCH] add electric conductivity algorithm (#7449) * add electric conductivity algorithm * Update electric_conductivity.py * Apply suggestions from code review Co-authored-by: Caeden Perelli-Harris * Update electric_conductivity.py * Update electric_conductivity.py * Update electric_conductivity.py * add algorithm Co-authored-by: Caeden Perelli-Harris --- electronics/electric_conductivity.py | 53 ++++++++++++++++++++++++++++ physics/sheer_stress.py | 51 ++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 electronics/electric_conductivity.py create mode 100644 physics/sheer_stress.py diff --git a/electronics/electric_conductivity.py b/electronics/electric_conductivity.py new file mode 100644 index 000000000..11f2a607d --- /dev/null +++ b/electronics/electric_conductivity.py @@ -0,0 +1,53 @@ +from __future__ import annotations + +ELECTRON_CHARGE = 1.6021e-19 # units = C + + +def electric_conductivity( + conductivity: float, + electron_conc: float, + mobility: float, +) -> tuple[str, float]: + """ + This function can calculate any one of the three - + 1. Conductivity + 2. Electron Concentration + 3. Electron Mobility + This is calculated from the other two provided values + Examples - + >>> electric_conductivity(conductivity=25, electron_conc=100, mobility=0) + ('mobility', 1.5604519068722301e+18) + >>> electric_conductivity(conductivity=0, electron_conc=1600, mobility=200) + ('conductivity', 5.12672e-14) + >>> electric_conductivity(conductivity=1000, electron_conc=0, mobility=1200) + ('electron_conc', 5.201506356240767e+18) + """ + if (conductivity, electron_conc, mobility).count(0) != 1: + raise ValueError("You cannot supply more or less than 2 values") + elif conductivity < 0: + raise ValueError("Conductivity cannot be negative") + elif electron_conc < 0: + raise ValueError("Electron concentration cannot be negative") + elif mobility < 0: + raise ValueError("mobility cannot be negative") + elif conductivity == 0: + return ( + "conductivity", + mobility * electron_conc * ELECTRON_CHARGE, + ) + elif electron_conc == 0: + return ( + "electron_conc", + conductivity / (mobility * ELECTRON_CHARGE), + ) + else: + return ( + "mobility", + conductivity / (electron_conc * ELECTRON_CHARGE), + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/physics/sheer_stress.py b/physics/sheer_stress.py new file mode 100644 index 000000000..74a2d36b1 --- /dev/null +++ b/physics/sheer_stress.py @@ -0,0 +1,51 @@ +from __future__ import annotations + + +def sheer_stress( + stress: float, + tangential_force: float, + area: float, +) -> tuple[str, float]: + """ + This function can calculate any one of the three - + 1. Sheer Stress + 2. Tangential Force + 3. Cross-sectional Area + This is calculated from the other two provided values + Examples - + >>> sheer_stress(stress=25, tangential_force=100, area=0) + ('area', 4.0) + >>> sheer_stress(stress=0, tangential_force=1600, area=200) + ('stress', 8.0) + >>> sheer_stress(stress=1000, tangential_force=0, area=1200) + ('tangential_force', 1200000) + """ + if (stress, tangential_force, area).count(0) != 1: + raise ValueError("You cannot supply more or less than 2 values") + elif stress < 0: + raise ValueError("Stress cannot be negative") + elif tangential_force < 0: + raise ValueError("Tangential Force cannot be negative") + elif area < 0: + raise ValueError("Area cannot be negative") + elif stress == 0: + return ( + "stress", + tangential_force / area, + ) + elif tangential_force == 0: + return ( + "tangential_force", + stress * area, + ) + else: + return ( + "area", + tangential_force / stress, + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod()