From a1728735ba8b5ed405ce1316789b2345c1086a26 Mon Sep 17 00:00:00 2001 From: slowy07 Date: Sun, 28 Jan 2024 14:18:53 +0700 Subject: [PATCH] feat: meanambahkan fungsi fisika menambahkan beberapa fungsi dari - efek doppler - gaya sentripental Signed-off-by: slowy07 --- .pre-commit-config.yaml | 1 + OpenSeries/fisika.py | 51 +++++++++++++++++++++++++++++ example/fisika/efek_doppler.py | 5 +++ example/fisika/gaya_sentripental.py | 5 +++ testing/fisika_test.py | 49 +++++++++++++++++++++++++++ testing/main_test.py | 4 +++ 6 files changed, 115 insertions(+) create mode 100644 example/fisika/efek_doppler.py create mode 100644 example/fisika/gaya_sentripental.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f308e26..1246c29 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,6 +35,7 @@ repos: - --ignore-missing-imports - --non-interactive - --install-types + exclude: example additional_dependencies: [types-requests] - repo: https://github.com/pre-commit/mirrors-prettier diff --git a/OpenSeries/fisika.py b/OpenSeries/fisika.py index 84bb370..1d712e8 100644 --- a/OpenSeries/fisika.py +++ b/OpenSeries/fisika.py @@ -168,3 +168,54 @@ def ketinggian_barometrik( else: hasil = 44_330 * (1 - (tekanan / 101_325) ** (1 / 5.5255)) return hasil + + +def gaya_sentripental( + massa: Union[float, int], velocity: Union[float, int], radius: Union[float, int] +) -> Union[float, int, error.Error, error.ErrorTipeData]: + """ + fungsi untuk menghitung gaya sentripental. gaya sentripental adalah gaya yang bekerja + pada benda dalam gerak lengkung arahnya menuju ke sumbu rotasi atau pusat kelengkungan + + Parameter: + massa (float): masa benda + v (float): kecepatan dari benda + radius (float): jari-jari lintasan melingkar + """ + if not all(isinstance(data, (float, int)) for data in [massa, velocity, radius]): + return error.ErrorTipeData(["float", "int"]) + if massa < 0: + return error.Error("Massa tidak boleh negatif") + if radius <= 0: + return error.Error("Radius selalu angka positif") + return (massa * (velocity) ** 2) / radius + + +def efek_doppler( + org_frek: Union[float, int], + gelombang_vel: Union[float, int], + obs_vel: Union[float, int], + src_vel: Union[float, int], +) -> Union[float, error.ErrorDibagiNol, error.ErrorTipeData, error.Error]: + """ + fungsi untuk menghitung efek doppler + + parameter: + org_frek (int atau float): frekuensi gelombang sumber diam + gelombang_vel_vel (int atau float): kecepatan gelombang dalam medium + obs_vel (int atau float): kecepatan pengamatan + src_vel (int atau float): kecepatan sumber + """ + if not all( + isinstance(data, (float, int)) + for data in [org_frek, gelombang_vel, obs_vel, src_vel] + ): + return error.ErrorTipeData(["int", "float"]) + if gelombang_vel == src_vel: + return error.ErrorDibagiNol() + doppler = (org_frek * (gelombang_vel + obs_vel)) / (gelombang_vel - src_vel) + if doppler <= 0: + return error.Error( + "frekuensi tidak positif, kecepatan sumber relatif lebih besar dari kecepatan gelombang dalam medium" + ) + return doppler diff --git a/example/fisika/efek_doppler.py b/example/fisika/efek_doppler.py new file mode 100644 index 0000000..ebad7c3 --- /dev/null +++ b/example/fisika/efek_doppler.py @@ -0,0 +1,5 @@ +import OpenSeries.fisika as fisika + +# menghitung fungsi dari efek doppler +doppler_efek = fisika.efek_doppler(100, 330, 10) +print(doppler_efek) diff --git a/example/fisika/gaya_sentripental.py b/example/fisika/gaya_sentripental.py new file mode 100644 index 0000000..df365fa --- /dev/null +++ b/example/fisika/gaya_sentripental.py @@ -0,0 +1,5 @@ +import OpenSeries.fisika as fisika + +# menghitung gaya sentripental +hasil = round(fisika.gaya_sentripental(10, 15, 5)) +print(hasil) diff --git a/testing/fisika_test.py b/testing/fisika_test.py index 7dccf8f..5609a6a 100644 --- a/testing/fisika_test.py +++ b/testing/fisika_test.py @@ -80,3 +80,52 @@ def test_tekanan_tipe_data_salah(self): hasil = fisika.ketinggian_barometrik("12") with self.assertRaises(error.ErrorTipeData): raise hasil + + +class TestGayaSentripental(unittest.TestCase): + def test_gaya_sentripental(self): + hasil = fisika.gaya_sentripental(10, 5, 2) + self.assertEqual(hasil, 125.0) + + def test_gaya_sentripental_error_tipe_data(self): + hasil = fisika.gaya_sentripental("12", 5, 2) + self.assertIsInstance(hasil, error.ErrorTipeData) + with self.assertRaises(error.ErrorTipeData): + raise hasil + + def test_gaya_sentripental_minus(self): + hasil = fisika.gaya_sentripental(-10, 5, 2) + self.assertIsInstance(hasil, error.Error) + with self.assertRaises(error.Error): + raise hasil + + def test_gaya_sentripental_nol(self): + hasil = fisika.gaya_sentripental(10, 5, 0) + self.assertIsInstance(hasil, error.Error) + with self.assertRaises(error.Error): + raise hasil + + +class TestEfekDoppler(unittest.TestCase): + def test_efek_doppler(self): + hasil = fisika.efek_doppler(100, 340, 20, 10) + rounding_hasil = round(hasil) + self.assertEqual(rounding_hasil, 109) + + def test_efek_doppler_invalid_tipe_data(self): + hasil = fisika.efek_doppler("12", "340", "20", 10) + self.assertIsInstance(hasil, error.ErrorTipeData) + with self.assertRaises(error.ErrorTipeData): + raise hasil + + def test_efek_doppler_dibagi_nol(self): + hasil = fisika.efek_doppler(0, 0, 0, 0) + self.assertIsInstance(hasil, error.ErrorDibagiNol) + with self.assertRaises(error.ErrorDibagiNol): + raise hasil + + def test_efek_doppler_nilai_negatif(self): + hasil = fisika.efek_doppler(-100, -340, 20, 10) + self.assertIsInstance(hasil, error.Error) + with self.assertRaises(error.Error): + raise hasil diff --git a/testing/main_test.py b/testing/main_test.py index 78dc05d..e6aec45 100644 --- a/testing/main_test.py +++ b/testing/main_test.py @@ -20,6 +20,8 @@ TestGerakLurusBeraturan, TestEnergiKinetik, TestKetinggianBarometrik, + TestGayaSentripental, + TestEfekDoppler, ) from testing.statistika_test import TestFungsiEntropy, TestFungiStandardDeviasi @@ -46,6 +48,8 @@ TestGerakLurusBeraturan, TestEnergiKinetik, TestKetinggianBarometrik, + TestGayaSentripental, + TestEfekDoppler, ] testing_statistika: list = [